| 1 |
[[modern_web]] |
| 2 |
== The modern Web |
| 3 |
|
| 4 |
Cowboy is a server for the modern Web. This chapter explains |
| 5 |
what it means and details all the standards involved. |
| 6 |
|
| 7 |
Cowboy supports all the standards listed in this document. |
| 8 |
|
| 9 |
=== HTTP/2 |
| 10 |
|
| 11 |
HTTP/2 is the most efficient protocol for consuming Web |
| 12 |
services. It enables clients to keep a connection open |
| 13 |
for long periods of time; to send requests concurrently; |
| 14 |
to reduce the size of requests through HTTP headers |
| 15 |
compression; and more. The protocol is binary, greatly |
| 16 |
reducing the resources needed to parse it. |
| 17 |
|
| 18 |
HTTP/2 also enables the server to push messages to the |
| 19 |
client. This can be used for various purposes, including |
| 20 |
the sending of related resources before the client requests |
| 21 |
them, in an effort to reduce latency. This can also be used |
| 22 |
to enable bidirectional communication. |
| 23 |
|
| 24 |
Cowboy provides transparent support for HTTP/2. Clients |
| 25 |
that know it can use it; others fall back to HTTP/1.1 |
| 26 |
automatically. |
| 27 |
|
| 28 |
HTTP/2 is compatible with the HTTP/1.1 semantics. |
| 29 |
|
| 30 |
HTTP/2 is defined by RFC 7540 and RFC 7541. |
| 31 |
|
| 32 |
=== HTTP/1.1 |
| 33 |
|
| 34 |
HTTP/1.1 is the previous version of the HTTP protocol. |
| 35 |
The protocol itself is text-based and suffers from numerous |
| 36 |
issues and limitations. In particular it is not possible |
| 37 |
to execute requests concurrently (though pipelining is |
| 38 |
sometimes possible), and it's also sometimes difficult |
| 39 |
to detect that a client disconnected. |
| 40 |
|
| 41 |
HTTP/1.1 does provide very good semantics for interacting |
| 42 |
with Web services. It defines the standard methods, headers |
| 43 |
and status codes used by HTTP/1.1 and HTTP/2 clients and |
| 44 |
servers. |
| 45 |
|
| 46 |
HTTP/1.1 also defines compatibility with an older version |
| 47 |
of the protocol, HTTP/1.0, which was never really standardized |
| 48 |
across implementations. |
| 49 |
|
| 50 |
The core of HTTP/1.1 is defined by RFC 7230, RFC 7231, |
| 51 |
RFC 7232, RFC 7233, RFC 7234 and RFC 7235. Numerous RFCs |
| 52 |
and other specifications exist defining additional HTTP |
| 53 |
methods, status codes, headers or semantics. |
| 54 |
|
| 55 |
=== Websocket |
| 56 |
|
| 57 |
xref:ws_protocol[Websocket] is a protocol built on top of HTTP/1.1 |
| 58 |
that provides a two-ways communication channel between the client and |
| 59 |
the server. Communication is asynchronous and can occur concurrently. |
| 60 |
|
| 61 |
It consists of a Javascript object allowing setting up a |
| 62 |
Websocket connection to the server, and a binary based |
| 63 |
protocol for sending data to the server or the client. |
| 64 |
|
| 65 |
Websocket connections can transfer either UTF-8 encoded text |
| 66 |
data or binary data. The protocol also includes support for |
| 67 |
implementing a ping/pong mechanism, allowing the server and |
| 68 |
the client to have more confidence that the connection is still |
| 69 |
alive. |
| 70 |
|
| 71 |
A Websocket connection can be used to transfer any kind of data, |
| 72 |
small or big, text or binary. Because of this Websocket is |
| 73 |
sometimes used for communication between systems. |
| 74 |
|
| 75 |
Websocket messages have no semantics on their own. Websocket |
| 76 |
is closer to TCP in that aspect, and requires you to design |
| 77 |
and implement your own protocol on top of it; or adapt an |
| 78 |
existing protocol to Websocket. |
| 79 |
|
| 80 |
Cowboy provides an interface known as xref:ws_handlers[Websocket handlers] |
| 81 |
that gives complete control over a Websocket connection. |
| 82 |
|
| 83 |
The Websocket protocol is defined by RFC 6455. |
| 84 |
|
| 85 |
=== Long-lived requests |
| 86 |
|
| 87 |
Cowboy provides an interface that can be used to support |
| 88 |
long-polling or to stream large amounts of data reliably, |
| 89 |
including using Server-Sent Events. |
| 90 |
|
| 91 |
Long-polling is a mechanism in which the client performs |
| 92 |
a request which may not be immediately answered by the |
| 93 |
server. It allows clients to request resources that may |
| 94 |
not currently exist, but are expected to be created soon, |
| 95 |
and which will be returned as soon as they are. |
| 96 |
|
| 97 |
Long-polling is essentially a hack, but it is widely used |
| 98 |
to overcome limitations on older clients and servers. |
| 99 |
|
| 100 |
Server-Sent Events is a small protocol defined as a media |
| 101 |
type, `text/event-stream`, along with a new HTTP header, |
| 102 |
`Last-Event-ID`. It is defined in the EventSource W3C |
| 103 |
specification. |
| 104 |
|
| 105 |
Cowboy provides an interface known as xref:loop_handlers[loop handlers] |
| 106 |
that facilitates the implementation of long-polling or stream |
| 107 |
mechanisms. It works regardless of the underlying protocol. |
| 108 |
|
| 109 |
=== REST |
| 110 |
|
| 111 |
xref:rest_principles[REST, or REpresentational State Transfer], |
| 112 |
is a style of architecture for loosely connected distributed |
| 113 |
systems. It can easily be implemented on top of HTTP. |
| 114 |
|
| 115 |
REST is essentially a set of constraints to be followed. |
| 116 |
Many of these constraints are purely architectural and |
| 117 |
solved by simply using HTTP. Some constraints must be |
| 118 |
explicitly followed by the developer. |
| 119 |
|
| 120 |
Cowboy provides an interface known as xref:rest_handlers[REST handlers] |
| 121 |
that simplifies the implementation of a REST API on top of |
| 122 |
the HTTP protocol. |