| 1 |
[[listeners]] |
| 2 |
== Listeners |
| 3 |
|
| 4 |
A listener is a set of processes that listens on a port for |
| 5 |
new connections. Incoming connections get handled by Cowboy. |
| 6 |
Depending on the connection handshake, one or another protocol |
| 7 |
may be used. |
| 8 |
|
| 9 |
This chapter is specific to Cowboy. Please refer to the |
| 10 |
https://ninenines.eu/docs/en/ranch/1.8/guide/listeners/[Ranch User Guide] |
| 11 |
for more information about listeners. |
| 12 |
|
| 13 |
Cowboy provides two types of listeners: one listening for |
| 14 |
clear TCP connections, and one listening for secure TLS |
| 15 |
connections. Both of them support the HTTP/1.1 and HTTP/2 |
| 16 |
protocols. |
| 17 |
|
| 18 |
=== Clear TCP listener |
| 19 |
|
| 20 |
The clear TCP listener will accept connections on the |
| 21 |
given port. A typical HTTP server would listen on port 80. |
| 22 |
Port 80 requires special permissions on most platforms |
| 23 |
however so a common alternative is port 8080. |
| 24 |
|
| 25 |
The following snippet starts listening for connections |
| 26 |
on port 8080: |
| 27 |
|
| 28 |
[source,erlang] |
| 29 |
---- |
| 30 |
start(_Type, _Args) -> |
| 31 |
Dispatch = cowboy_router:compile([ |
| 32 |
{'_', [{"/", hello_handler, []}]} |
| 33 |
]), |
| 34 |
{ok, _} = cowboy:start_clear(my_http_listener, |
| 35 |
[{port, 8080}], |
| 36 |
#{env => #{dispatch => Dispatch}} |
| 37 |
), |
| 38 |
hello_erlang_sup:start_link(). |
| 39 |
---- |
| 40 |
|
| 41 |
The xref:getting_started[Getting Started] chapter uses a |
| 42 |
clear TCP listener. |
| 43 |
|
| 44 |
Clients connecting to Cowboy on the clear listener port are |
| 45 |
expected to use either HTTP/1.1 or HTTP/2. |
| 46 |
|
| 47 |
Cowboy supports both methods of initiating a clear |
| 48 |
HTTP/2 connection: through the Upgrade mechanism |
| 49 |
(https://tools.ietf.org/html/rfc7540#section-3.2[RFC 7540 3.2]) |
| 50 |
or by sending the preface directly |
| 51 |
(https://tools.ietf.org/html/rfc7540#section-3.4[RFC 7540 3.4]). |
| 52 |
|
| 53 |
Compatibility with HTTP/1.0 is provided by Cowboy's HTTP/1.1 |
| 54 |
implementation. |
| 55 |
|
| 56 |
=== Secure TLS listener |
| 57 |
|
| 58 |
The secure TLS listener will accept connections on the |
| 59 |
given port. A typical HTTPS server would listen on port 443. |
| 60 |
Port 443 requires special permissions on most platforms |
| 61 |
however so a common alternative is port 8443. |
| 62 |
|
| 63 |
// @todo Make a complete list of restrictions. |
| 64 |
|
| 65 |
The function provided by Cowboy will ensure that the TLS |
| 66 |
options given are following the HTTP/2 RFC with regards |
| 67 |
to security. For example some TLS extensions or ciphers |
| 68 |
may be disabled. This also applies to HTTP/1.1 connections |
| 69 |
on this listener. If this is not desirable, Ranch can be |
| 70 |
used directly to set up a custom listener. |
| 71 |
|
| 72 |
[source,erlang] |
| 73 |
---- |
| 74 |
start(_Type, _Args) -> |
| 75 |
Dispatch = cowboy_router:compile([ |
| 76 |
{'_', [{"/", hello_handler, []}]} |
| 77 |
]), |
| 78 |
{ok, _} = cowboy:start_tls(my_https_listener, |
| 79 |
[ |
| 80 |
{port, 8443}, |
| 81 |
{certfile, "/path/to/certfile"}, |
| 82 |
{keyfile, "/path/to/keyfile"} |
| 83 |
], |
| 84 |
#{env => #{dispatch => Dispatch}} |
| 85 |
), |
| 86 |
hello_erlang_sup:start_link(). |
| 87 |
---- |
| 88 |
|
| 89 |
Clients connecting to Cowboy on the secure listener are |
| 90 |
expected to use the ALPN TLS extension to indicate what |
| 91 |
protocols they understand. Cowboy always prefers HTTP/2 |
| 92 |
over HTTP/1.1 when both are supported. When neither are |
| 93 |
supported by the client, or when the ALPN extension was |
| 94 |
missing, Cowboy expects HTTP/1.1 to be used. |
| 95 |
|
| 96 |
Cowboy also advertises HTTP/2 support through the older |
| 97 |
NPN TLS extension for compatibility. Note however that |
| 98 |
this support will likely not be enabled by default when |
| 99 |
Cowboy 2.0 gets released. |
| 100 |
|
| 101 |
Compatibility with HTTP/1.0 is provided by Cowboy's HTTP/1.1 |
| 102 |
implementation. |
| 103 |
|
| 104 |
=== Stopping the listener |
| 105 |
|
| 106 |
When starting listeners along with the application it is |
| 107 |
a good idea to also stop the listener when the application |
| 108 |
stops. This can be done by calling `cowboy:stop_listener/1` |
| 109 |
in the application's stop function: |
| 110 |
|
| 111 |
[source,erlang] |
| 112 |
---- |
| 113 |
stop(_State) -> |
| 114 |
ok = cowboy:stop_listener(my_http_listener). |
| 115 |
---- |
| 116 |
|
| 117 |
=== Protocol configuration |
| 118 |
|
| 119 |
The HTTP/1.1 and HTTP/2 protocols share the same semantics; |
| 120 |
only their framing differs. The first is a text protocol and |
| 121 |
the second a binary protocol. |
| 122 |
|
| 123 |
Cowboy doesn't separate the configuration for HTTP/1.1 and |
| 124 |
HTTP/2. Everything goes into the same map. Many options are |
| 125 |
shared. |
| 126 |
|
| 127 |
// @todo Describe good to know options for both protocols? |
| 128 |
// Maybe do that in separate chapters? |