Skip to content

Instantly share code, notes, and snippets.

@Inari-Whitebear
Created January 9, 2015 16:09
Show Gist options
  • Save Inari-Whitebear/d14b711bb08f56643843 to your computer and use it in GitHub Desktop.
Save Inari-Whitebear/d14b711bb08f56643843 to your computer and use it in GitHub Desktop.
fn handle_client(mut stream: TcpStream) {
let mut buf: [u8; 1024] = [0; 1024];
stream.read(&mut buf);
let data = str::from_utf8(&buf).unwrap();
stream.close_read();
let mut method: &str = "";
let mut path: &str = "";
let mut http_version: &str = "";
let mut headers: HashMap<&str, &str> = HashMap::new();
let mut error = None;
let mut first = true;
for line in data.lines_any() {
if first {
first = false;
let words: Vec<&str> = line.words().collect();
if words.len() != 3 {
error = Some("400 Bad Request");
break;
}
method = words[0];
if method != "GET" {
error = Some("501 Not Implemented");
break;
}
path = words[1];
http_version = words[2];
} else {
if line.contains(":") {
let loc = line.find_str(":").unwrap();
let field = line.slice_to(loc).trim();
let value = line.slice_from(loc + 1u).trim();
headers.insert(field, value);
}
}
}
let web_request = web::WebRequest {
headers: headers,
body: "",
http_version: http_version,
method: method,
path: path,
error: error,
stream: stream
};
handle_web_request(web_request);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment