gen_tcp {packet, http} mode - Erlang
This is a very useful undocumented feature in Erlang and very soon they are going to make it official. But there is nothing to refer in erlang documentation yet, So i thought of writing someting about this. Yaws uses this mode very largely and also, http://www.trapexit.org/A_
is a good resource for the people who are further interested in about this mode.
In this mode the http header lines are comming line by line and in a format which is very easy to decode and use.
Passive mode:
You can turn your socket in to passive mode by setting the option {active, false}
Request line:
{ok, {http_request, Method, { absoluteURI, http, URL, undefined, Rest}, {Maj, Min}}}
Method – GET, POST, etc
Keep in mind the full PATH you want to get is: URL ++ Rest
{Maj, MIN} is the digits of the http version (i.e: {1, 1} for HTTP/1.1, {1, 0} for HTTP/1.0) , Maj-Min-integer
A sample request line is given below,
{ok,{http_request,'GET',{absoluteURI,http,"www.google.com",undefined,
"/"},{1,0}}}
Header lines:
{ok, {http_header, _Num, Header_Name, undefined, Value}} - This is common for both request and response.
Header_Name - name of the header usually an atom but for user defined headers it can be a string.
Value - value of the header (string)
End of header:
{ok, http _eoh} – This is common for both request and response.
Response Line:
{ok, {http_response, {Maj, Min}, Code, ResContent}}
{Maj, Min} – Same as in Request line.
Code – http status code (e.g: 100, 200) - integer
ResContent – the status code description. (e.g: OK, Moved temporary) - string
Active mode
tuples like this in active mode:
{http_request, Socket, ...}
{http_response, Socket, ...}
{http_header, Socket, ...}
The other fields will be same as the passive mode. But in future they are going to change this to the following,
{http, Socket, {http_request, ...}}
{http, Socket, {http_response, ...}}
{http, Socket, {http_header, ...}}
The purpose is to make the inner tuples look the same regardless of how they were received.