<html><head></head><body><div style="color:#000; background-color:#fff; font-family:HelveticaNeue, Helvetica Neue, Helvetica, Arial, Lucida Grande, sans-serif;font-size:16px"><div id="yui_3_16_0_ym19_1_1487690829632_7373">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. </div><div id="yui_3_16_0_ym19_1_1487690829632_7373">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.</div><div id="yui_3_16_0_ym19_1_1487690829632_7373">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:</div><div id="yui_3_16_0_ym19_1_1487690829632_7373"><br></div><div id="yui_3_16_0_ym19_1_1487690829632_7373" dir="ltr"><font face="Courier New, courier, monaco, monospace, sans-serif">GET / HTTP/1.1\r\n</font><br></div><div id="yui_3_16_0_ym19_1_1487690829632_7373" dir="ltr"><br></div><div id="yui_3_16_0_ym19_1_1487690829632_7373" dir="ltr"><br></div><div id="yui_3_16_0_ym19_1_1487690829632_7373" dir="ltr">or </div><div id="yui_3_16_0_ym19_1_1487690829632_7373" dir="ltr"><br></div><div id="yui_3_16_0_ym19_1_1487690829632_7373"><font face="Courier New, courier, monaco, monospace, sans-serif" id="yui_3_16_0_ym19_1_1487690829632_8310">GET / HTTP/1.1\r\n</font></div><div id="yui_3_16_0_ym19_1_1487690829632_7373"><font face="Courier New, courier, monaco, monospace, sans-serif">Host:</font></div><div id="yui_3_16_0_ym19_1_1487690829632_7373"><br></div><div id="yui_3_16_0_ym19_1_1487690829632_7373" dir="ltr">After parsed the first line for method, uri, version, it will go back to read more Data from Socket to Buffer. </div><div id="yui_3_16_0_ym19_1_1487690829632_7373" dir="ltr">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.</div><div id="yui_3_16_0_ym19_1_1487690829632_7373"><br></div><div id="yui_3_16_0_ym19_1_1487690829632_7373">Thank you.</div><div id="yui_3_16_0_ym19_1_1487690829632_7373"><br></div><div id="yui_3_16_0_ym19_1_1487690829632_7373"><br></div><div id="yui_3_16_0_ym19_1_1487690829632_7373"><br></div></div></body></html>