[erlang-questions] cowboy http request parse

nickel chen nickelchen@REDACTED
Tue Feb 21 16:54:22 CET 2017


Hi, I'm new to erlang and being learning it, and I found cowboy is a very good project to learn how to implement a http protocol. It's based on ranch, which set socket option {packet, raw}, means it will deliver message {tcp, Socket, Data} to cowboy constantly. While reading parse_request function in cowboy_http.erl, I found it difficult to understand. As far as I figured out, when received message, this function append Data to Buffer unless the data contains a new line "\r\n", then it began to parse the Buffer.The Buffer may be part of the whole request header, may ends with "\r\n", or at least contains "\r\n". So it may be:
GET / HTTP/1.1\r\n


or 
GET / HTTP/1.1\r\nHost:
After parsed the first line for method, uri, version, it will go back to read more Data from Socket to Buffer. My question is, why not read the whole request header all at once, as request header and request body are seperated by two "\r\n" (Correct me if I mis-understand: one "\r\n" is for header line ending, the other is for the empty line between header and body). After reading whole request, then it will not bother going back again to read Socket Data while parsing.
Thank you.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20170221/72ee31ba/attachment.htm>


More information about the erlang-questions mailing list