C
C
CNNRNN2020-05-24 12:29:30
Web servers
CNNRNN, 2020-05-24 12:29:30

How to parse an HTTP request?

Hello!
Parsing http requests I understand so.
1) create a buffer where we write data from the network

// массив с размером 1024 байт
let mut buffer = [0; 1024];
// тут я читаю данные из сети и записываю в массив (buffer)
 stream.read(&mut buffer).unwrap();

2) then we extract data from the array
And here I have 2 questions.
1) What if the request size is larger than the buffer size?
1.1) Will it be necessary to read until there is CRLF in the buffer?
1.2) for this I need to go through the buffer and look for CRLF . if not then clear the buffer and continue reading ?
2) How to separate one http request from another?
If possible, you can give a link, where this process will be described from scratch and without libraries. Any language will do)
Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Belyaev, 2020-05-25
@CNNRNN

What if the request size is larger than the buffer size?
Read in a loop. read generally does not guarantee that it will fill the buffer completely, but it will definitely not read more than its size at a time. It also returns io::Result<usize>, in which it reports how many bytes were actually read.
1.1) Will it be necessary to read until there is CRLF in the buffer?
Until read returns Ok(0), or an error. Although not everything is so simple with the error, according to the dock , it can crash Err(io::ErrorKind::Interrupted)at which it is worth trying to read again. In general, there will be CRLF after each header, and when the headers run out there will be 2 CRLFs in a row, and then there may or may not be a request body.
1.2) for this I need to go through the buffer and look for CRLF . if not then clear the buffer and continue reading ?
no, you need to parse what came in, save it somewhere, and then continue reading.
2) How to separate one http request from another?
If we don’t have keep-alive, then each request will be in a separate connection, but keep-alive occurs only if both sides sent a header Connection: keep-alive. You can do it in a simple way and answer with the header Connection: close, you will still have no performance in the training project. But if you still want to get confused, then the rule is also not complicated - the next request begins in the next byte where the current one ended. The size of the request body in bytes can be found from the Content-Length header, and if it is not there, then you can assume that its value is 0.
What you should read for your idea:
1. http spec
2. hyper
sources 3. actix-web sources

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question