Reputation: 701
I tried to write a static HTTP file server, but I'm stuck when I try to send a .jpg
file through TcpStream
.
Even if I read
the file as binary, the browser doesn't seem to be able to decode the image:
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);
}
I heard that there is some kind of "HTTP server standard guide book" stuff, any idea where to find it?
Upvotes: 1
Views: 2264
Reputation: 430318
Always fix compiler warnings, especially ones about results you aren't using. Go back and re-read The Rust Programming Language chapter on error handling to refresh yourself on the topic.
However, that's not your problem here. I don't know what you think that let mut buf = vec![0u8];
does, but you don't want it. That allocates a vector with exactly one value in it, an 8-bit zero. You then send that rogue byte before your JPEG, which makes it no longer valid JPEG. Removing that allows your program to work:
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),
}
}
However, your program has a giant security hole known as path traversal. You should not expose this anywhere that has any valuable information.
Upvotes: 7