# Cowboy Server and HTTP Implementation

**URL:** <https://erlangforums.com/t/cowboy-server-and-http-implementation/966>\
**Category:** Questions / Help\
**Tags:** erlang-otp\
**Created:** [January 21, 2022, 8:12pm UTC](https://erlangforums.com/t/cowboy-server-and-http-implementation/966 "2022-01-21T20:12:38Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Abdelghani](https://erlangforums.com/letter_avatar_proxy/v4/letter/a/49beb7/32.png) [@Abdelghani](https://erlangforums.com/u/Abdelghani)\
**Post date:** [January 21, 2022, 8:12pm UTC](https://erlangforums.com/t/cowboy-server-and-http-implementation/966/1 "2022-01-21T20:12:38Z")

</div>

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 ?

---

<div class="post-metadata">

**Author:** ![asabil](https://erlangforums.com/user_avatar/erlangforums.com/asabil/32/381_2.png) [@asabil](https://erlangforums.com/u/asabil)\
**Post date:** [January 22, 2022, 9:23am UTC](https://erlangforums.com/t/cowboy-server-and-http-implementation/966/2 "2022-01-22T09:23:30Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Abdelghani](https://erlangforums.com/letter_avatar_proxy/v4/letter/a/49beb7/32.png) [@Abdelghani](https://erlangforums.com/u/Abdelghani)\
**Post date:** [January 22, 2022, 3:13pm UTC](https://erlangforums.com/t/cowboy-server-and-http-implementation/966/3 "2022-01-22T15:13:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![AstonJ](https://erlangforums.com/user_avatar/erlangforums.com/astonj/32/9_2.png) [@AstonJ](https://erlangforums.com/u/AstonJ)\
**Post date:** [January 22, 2022, 4:59pm UTC](https://erlangforums.com/t/cowboy-server-and-http-implementation/966/4 "2022-01-22T16:59:24Z")

</div>

> [@Abdelghani](#):
>
> I can’t understand how it implements the HTTP Protocol

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 😃

---

<div class="post-metadata">

**Author:** ![Abdelghani](https://erlangforums.com/letter_avatar_proxy/v4/letter/a/49beb7/32.png) [@Abdelghani](https://erlangforums.com/u/Abdelghani)\
**Post date:** [January 22, 2022, 6:49pm UTC](https://erlangforums.com/t/cowboy-server-and-http-implementation/966/5 "2022-01-22T18:49:32Z")

</div>

@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

---

<div class="post-metadata">

**Author:** ![vkatsuba](https://erlangforums.com/user_avatar/erlangforums.com/vkatsuba/32/314_2.png) [@vkatsuba](https://erlangforums.com/u/vkatsuba)\
**Post date:** [January 22, 2022, 9:10pm UTC](https://erlangforums.com/t/cowboy-server-and-http-implementation/966/6 "2022-01-22T21:10:46Z")

</div>

Here is provided a lot of small and useful examples [cowboy/examples at master · ninenines/cowboy · GitHub](https://github.com/ninenines/cowboy/tree/master/examples). 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](https://ninenines.eu/docs/en/cowboy/2.9/manual/) and here [Nine Nines: Cowboy User Guide](https://ninenines.eu/docs/en/cowboy/2.9/guide/). Also note that cowboy has two important dependencies [GitHub - ninenines/cowlib: Support library for manipulating Web protocols.](https://github.com/ninenines/cowlib) and [GitHub - ninenines/ranch: Socket acceptor pool for TCP protocols.](https://github.com/ninenines/ranch)

---

<div class="post-metadata">

**Author:** ![Abdelghani](https://erlangforums.com/letter_avatar_proxy/v4/letter/a/49beb7/32.png) [@Abdelghani](https://erlangforums.com/u/Abdelghani)\
**Post date:** [January 23, 2022, 12:02am UTC](https://erlangforums.com/t/cowboy-server-and-http-implementation/966/7 "2022-01-23T00:02:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Abdelghani](https://erlangforums.com/letter_avatar_proxy/v4/letter/a/49beb7/32.png) [@Abdelghani](https://erlangforums.com/u/Abdelghani)\
**Post date:** [January 23, 2022, 2:39am UTC](https://erlangforums.com/t/cowboy-server-and-http-implementation/966/8 "2022-01-23T02:39:44Z")

</div>

Okey, I have read the code Carefully Again and I think I find something in the [connection function](https://github.com/ninenines/cowboy/blob/master/src/cowboy_http.erl#L1359)

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](https://github.com/ninenines/cowboy/blob/master/test/handlers/hello_h.erl#L7) : `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](https://github.com/ninenines/cowboy/blob/master/src/cowboy_http.erl#L225)

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 ?
