gpt4 book ai didi

http - 如何通过 TcpStream 将 .jpg 作为 HTTP 发送?

转载 作者:可可西里 更新时间:2023-11-01 02:55:41 24 4
gpt4 key购买 nike

我尝试编写静态 HTTP 文件服务器,但当我尝试通过 TcpStream 发送 .jpg 文件时卡住了。

即使我读取二进制文件,浏览器似乎也无法解码图像:

extern crate chunked_transfer;
use std::net::{TcpListener, TcpStream};
use std::io::{Read, Write};
use std::thread;
use std::fs::File;
use chunked_transfer::Encoder;

fn main() {
let listener = TcpListener::bind("127.0.0.1:9527").unwrap();
println!("Listening for connections on port {}", 9527);
for stream in listener.incoming() {
match stream {
Ok(stream) => {
thread::spawn(|| handle_client(stream));
}
Err(e) => println!("Unable to connect: {}", e),
}
}
}

fn get_path(mut stream: &TcpStream) -> String {
let mut buf = [0u8; 4096];
match stream.read(&mut buf) {
Ok(_) => {
let req_str = String::from_utf8_lossy(&buf);
let path: Vec<&str> = req_str.lines().next().unwrap().split(" ").collect();
println!("GET {}", path[1]);
// println!("{}", req_str);
path[1].to_string()
}
Err(e) => {
println!("Unable to read stream: {}", e);
"/".to_string()
}
}
}

fn response(path: &str, mut stream: TcpStream) {
let file_path: &str = &("/home/xinbg/Downloads/wallpaper".to_string() + path);
println!("{}", file_path);
let mut buf = vec![0u8];
let file = File::open(file_path);
file.unwrap().read_to_end(&mut buf);

let mut encoded: Vec<u8> = vec![];
{
let mut encoder = Encoder::with_chunks_size(&mut encoded, 8);
encoder.write_all(&buf);
}

let headers =
["HTTP/1.1 200 OK", "Content-type: image/jpeg", "Transfer-Encoding: chunked", "\r\n"];
let mut response: Vec<u8> = headers.join("\r\n")
.to_string()
.into_bytes();
response.extend(encoded);

match stream.write(&response) {
Ok(_) => println!("Response sent"),
Err(e) => println!("Failed sending response: {}", e),
}
}

fn handle_client(stream: TcpStream) {
response(&get_path(&stream), stream);
}

我听说有某种“HTTP 服务器标准指南”之类的东西,知道在哪里可以找到它吗?

最佳答案

始终修复编译器警告,尤其是关于您未使用的结果的警告。返回并重新阅读关于 error handlingThe Rust Programming Language 章节刷新自己的话题。

但是,这不是您的问题。我不知道您认为 let mut buf = vec![0u8]; 会做什么,但您不想要它。这分配了一个向量,其中只有一个值,一个 8 位零。然后,您在 JPEG 之前发送该流氓字节,这使得它不再有效的 JPEG。删除它可以让您的程序运行:

fn response(path: &str, mut stream: TcpStream) {
let file_path = format!("/tmp/images/{}", path);

let mut buf = Vec::new();
let mut file = File::open(&file_path).unwrap();
file.read_to_end(&mut buf).unwrap();

let mut encoded = Vec::new();
{
let mut encoder = Encoder::with_chunks_size(&mut encoded, 8);
encoder.write_all(&buf).unwrap();
}

let headers = [
"HTTP/1.1 200 OK",
"Content-type: image/jpeg",
"Transfer-Encoding: chunked",
"\r\n"
];
let mut response = headers.join("\r\n")
.to_string()
.into_bytes();
response.extend(encoded);

match stream.write(&response) {
Ok(_) => println!("Response sent"),
Err(e) => println!("Failed sending response: {}", e),
}
}

但是,您的程序有一个巨大的安全漏洞,称为path traversal .你不应该在任何有任何有值(value)信息的地方暴露它。

关于http - 如何通过 TcpStream 将 .jpg 作为 HTTP 发送?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42216349/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com