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

时间:2017-02-14 01:27:47

标签: http tcp rust

我尝试编写静态HTTP文件服务器,但是当我尝试通过.jpg发送TcpStream文件时,我感到困惑。

即使我read该文件为二进制文件,浏览器似乎也无法对图像进行解码:

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);
}

我听说有某种&#34; HTTP服务器标准指南&#34;东西,任何想法在哪里找到它?

1 个答案:

答案 0 :(得分:5)

始终修复编译器警告,尤其是有关您未使用的结果的警告。返回并重新阅读error handling上的 The 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。你应该在任何有价值信息的地方公开它。