Cowboy Server and HTTP Implementation

Hi everybody, Iam new to Cowboy Server I have read enough code to understand how it handle a simple request but I can’t understand how it implements the HTTP Protocol :

After the ranch_conns_sup receive the Accepted Socket it will Spawn a new Process to be the cowboy_http Handler and after this last send the Response to the Client it should close the connection Immediately but what I see is that it will loop again (loop(info(State, StreamID, Msg))) so why ?

the cowboy_http Process in its #state have a variable last_streamid=maps:get(max_keepalive, Opts, 1000)} and that means that this Process may receive up to 1000 requests (streams) : should not this Process receive just 1 request, reply to, close the connection and terminate normally ?

1 Like

The 1 request per connection is only required in the case of HTTP/1.0.

HTTP/1.1 uses the Connection header to define whether the connection should be closed after a request or not.

4 Likes

Thank you @asabil I know that but if you look we have in all cases loop(info(State, StreamID, Msg)) and that means that the Process will always receive other requests

1 Like

Are you trying to do something in particular or are stuck trying to do something? If so it might be best to post it as a specific question :smiley:

1 Like

@AstonJ I try just to understand how the Erlang HTTP Server work exactly, after that I want to customize it to meet my goals as a part of a Generic Erlang Server

2 Likes

Here is provided a lot of small and useful examples cowboy/examples at master · ninenines/cowboy · GitHub. You can get any of them and adapt for your requirements. In case of additional problems - you can create separate topics for their discussion. A lot of stuff is can be configurable - what mean that e.g. you can set max_keepalive any own value if you want. All those info also provided here Nine Nines: Cowboy Function Reference and here Nine Nines: Cowboy User Guide. Also note that cowboy has two important dependencies GitHub - ninenines/cowlib: Support library for manipulating Web protocols. and GitHub - ninenines/ranch: Socket acceptor pool for TCP protocols.

3 Likes

Thank you @vkatsuba, I have already read all these links but there is not a good explanation of the implementation of HTTP Protocol via code

1 Like

Okey, I have read the code Carefully Again and I think I find something in the connection function

1-If the Server is configured to handle just One Request and after that it should Close the Connection (last_streamid=1), then it will Send <<"connection : close" >> in its Reply Header and Logically the Client will not use this Socket again (Regardless of handler Module specification of <<"connection">> Value in headers parameter in its init function, for example this : headers here is the second Parameter in the function cowboy_req:reply and in this case there is no headers)

2-If the Server is configured to handle Many Requests per Socket and Keep-Alive the Connection and the handler Module want to Close the connection Immediately After Sending the Reply (have <<"connection : close">> in headers Parameter in its init function), then the Server will set (last_streamid=StreamID) and the Client Logically will not use this Socket again.

Okey that’s fine, in the two cases the HTTP Process will loop again with always this match

Okey it will Never receive Data from this Socket and even if something happens Abnormally and the Client Send Data via this Socket Again, the Process will Ignore it and Loop Again so :

Why the Server Keeps this Process Alive ? it’s a Waste of Resources especially with a lot of Connections(Even if the Process will do Nothing it still Keep Memory Space) so Why don’t just Terminate this Process ?

1 Like