Uhm so ive been working a TCP server using only std io and net and httparser. Yet, i am unable to get past this error mostly due to how vague it is. The error im talking about is:
“called Result::unwrap()
on an Err
value: Token”
The code that i believe might be at work here might be:
-
The section of code that checks the size of the stream in bytes to make a custom vector with the size of the size the incoming request.
-
The headers variable
-
The httparse Request
to be completely fair these are just guesses so… not much help but atleast that what i believe i might have done wrong.
without further ado here is the relevant function:
fn handle_connection(mut stream: TcpStream) {
let mut buffer = [0; 2048];
let stream_data_size = stream.read(&mut buffer).unwrap();
let buffer = vec![0; stream_data_size];
let mut headers = [httparse::EMPTY_HEADER; 4];
let mut req = httparse::Request::new(&mut headers);
println!("{buffer:?}");
let res = req.parse(&buffer).unwrap(); // error is here!! (I believe)
let buf_reader = BufReader::new(&mut stream);
let http_request: Vec<_> = buf_reader
.lines()
.map(|result| result.unwrap())
.take_while(|line| !line.is_empty())
.collect();
println!("Request: {:#?}", http_request);
let response = "HTTP/1.1 200 OKrnrn";
stream.write_all(response.as_bytes()).unwrap();
stream.flush().unwrap();
println!("Headers: {:?}", res);
}
I expected this function to:
- be called when a request is done to the relevant address (working)
- be able to read and write (return) the request (working)
- access the headers and body (not working)
Adrián Moya is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
6