http2 can keep upgraded connection up
[platform/upstream/libwebsockets.git] / lib / handshake.c
1 /*
2  * libwebsockets - small server side websockets and web server implementation
3  *
4  * Copyright (C) 2010-2013 Andy Green <andy@warmcat.com>
5  *
6  *  This library is free software; you can redistribute it and/or
7  *  modify it under the terms of the GNU Lesser General Public
8  *  License as published by the Free Software Foundation:
9  *  version 2.1 of the License.
10  *
11  *  This library is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  *  Lesser General Public License for more details.
15  *
16  *  You should have received a copy of the GNU Lesser General Public
17  *  License along with this library; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19  *  MA  02110-1301  USA
20  */
21
22 #include "private-libwebsockets.h"
23
24 /*
25  * -04 of the protocol (actually the 80th version) has a radically different
26  * handshake.  The 04 spec gives the following idea
27  *
28  *    The handshake from the client looks as follows:
29  *
30  *      GET /chat HTTP/1.1
31  *      Host: server.example.com
32  *      Upgrade: websocket
33  *      Connection: Upgrade
34  *      Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
35  *      Sec-WebSocket-Origin: http://example.com
36  *      Sec-WebSocket-Protocol: chat, superchat
37  *      Sec-WebSocket-Version: 4
38  *
39  *  The handshake from the server looks as follows:
40  *
41  *       HTTP/1.1 101 Switching Protocols
42  *       Upgrade: websocket
43  *       Connection: Upgrade
44  *       Sec-WebSocket-Accept: me89jWimTRKTWwrS3aRrL53YZSo=
45  *       Sec-WebSocket-Nonce: AQIDBAUGBwgJCgsMDQ4PEC==
46  *       Sec-WebSocket-Protocol: chat
47  */
48
49 #ifndef min
50 #define min(a, b) ((a) < (b) ? (a) : (b))
51 #endif
52 /*
53  * We have to take care about parsing because the headers may be split
54  * into multiple fragments.  They may contain unknown headers with arbitrary
55  * argument lengths.  So, we parse using a single-character at a time state
56  * machine that is completely independent of packet size.
57  */
58
59 LWS_VISIBLE int
60 libwebsocket_read(struct libwebsocket_context *context,
61                      struct libwebsocket *wsi, unsigned char *buf, size_t len)
62 {
63         size_t n;
64         int body_chunk_len;
65         unsigned char *last_char;
66
67         switch (wsi->state) {
68         case WSI_STATE_HTTP2_AWAIT_CLIENT_PREFACE:
69         case WSI_STATE_HTTP2_ESTABLISHED_PRE_SETTINGS:
70         case WSI_STATE_HTTP2_ESTABLISHED:
71                 n = 0;
72                 while (n < len) {
73                         /*
74                          * we were accepting input but now we stopped doing so
75                          */
76                         if (!(wsi->rxflow_change_to & LWS_RXFLOW_ALLOW)) {
77                                 lws_rxflow_cache(wsi, buf, n, len);
78
79                                 return 1;
80                         }
81
82                         /* account for what we're using in rxflow buffer */
83                         if (wsi->rxflow_buffer)
84                                 wsi->rxflow_pos++;
85                         if (lws_http2_parser(context, wsi, buf[n++]))
86                                 goto bail;
87                 }
88                 break;
89 http_new:
90         case WSI_STATE_HTTP:
91                 wsi->hdr_parsing_completed = 0;
92                 /* fallthru */
93         case WSI_STATE_HTTP_ISSUING_FILE:
94                 wsi->state = WSI_STATE_HTTP_HEADERS;
95                 wsi->u.hdr.parser_state = WSI_TOKEN_NAME_PART;
96                 wsi->u.hdr.lextable_pos = 0;
97                 /* fallthru */
98         case WSI_STATE_HTTP_HEADERS:
99                 lwsl_parser("issuing %d bytes to parser\n", (int)len);
100
101                 if (lws_handshake_client(wsi, &buf, len))
102                         goto bail;
103
104                 last_char = buf;
105                 if (lws_handshake_server(context, wsi, &buf, len))
106                         /* Handshake indicates this session is done. */
107                         goto bail;
108
109                 /* It's possible that we've exhausted our data already, but
110                  * lws_handshake_server doesn't update len for us. Figure out how
111                  * much was read, so that we can proceed appropriately: */
112                 len -= (buf - last_char);
113
114                 if (!wsi->hdr_parsing_completed)
115                         /* More header content on the way */
116                         goto read_ok;
117
118                 switch (wsi->state) {
119                         case WSI_STATE_HTTP:
120                         case WSI_STATE_HTTP_HEADERS:
121                                 goto http_complete;
122                         case WSI_STATE_HTTP_ISSUING_FILE:
123                                 goto read_ok;
124                         case WSI_STATE_HTTP_BODY:
125                                 wsi->u.http.content_remain = wsi->u.http.content_length;
126                                 goto http_postbody;
127                         default:
128                                 break;
129                 }
130                 break;
131
132         case WSI_STATE_HTTP_BODY:
133 http_postbody:
134                 while (len && wsi->u.http.content_remain) {
135                         /* Copy as much as possible, up to the limit of:
136                          * what we have in the read buffer (len)
137                          * remaining portion of the POST body (content_remain)
138                          */
139                         body_chunk_len = min(wsi->u.http.content_remain,len);
140                         wsi->u.http.content_remain -= body_chunk_len;
141                         len -= body_chunk_len;
142
143                         if (wsi->protocol->callback) {
144                                 n = wsi->protocol->callback(
145                                         wsi->protocol->owning_server, wsi,
146                                         LWS_CALLBACK_HTTP_BODY, wsi->user_space,
147                                         buf, body_chunk_len);
148                                 if (n)
149                                         goto bail;
150                         }
151                         buf += body_chunk_len;
152
153                         if (!wsi->u.http.content_remain)  {
154                                 /* he sent the content in time */
155                                 libwebsocket_set_timeout(wsi, NO_PENDING_TIMEOUT, 0);
156                                 if (wsi->protocol->callback) {
157                                         n = wsi->protocol->callback(
158                                                 wsi->protocol->owning_server, wsi,
159                                                 LWS_CALLBACK_HTTP_BODY_COMPLETION,
160                                                 wsi->user_space, NULL, 0);
161                                         if (n)
162                                                 goto bail;
163                                 }
164                                 goto http_complete;
165                         }
166                 }
167                 break;
168
169         case WSI_STATE_ESTABLISHED:
170         case WSI_STATE_AWAITING_CLOSE_ACK:
171                 if (lws_handshake_client(wsi, &buf, len))
172                         goto bail;
173                 switch (wsi->mode) {
174                 case LWS_CONNMODE_WS_SERVING:
175
176                         if (libwebsocket_interpret_incoming_packet(wsi, buf, len) < 0) {
177                                 lwsl_info("interpret_incoming_packet has bailed\n");
178                                 goto bail;
179                         }
180                         break;
181                 }
182                 break;
183         default:
184                 lwsl_err("libwebsocket_read: Unhandled state\n");
185                 break;
186         }
187
188 read_ok:
189         /* Nothing more to do for now. */
190         lwsl_debug("libwebsocket_read: read_ok\n");
191
192         return 0;
193
194 http_complete:
195         lwsl_debug("libwebsocket_read: http_complete\n");
196
197         /* Did the client want to keep the HTTP connection going? */
198
199         if (wsi->u.http.connection_type == HTTP_CONNECTION_KEEP_ALIVE) {
200                 lwsl_debug("libwebsocket_read: keep-alive\n");
201                 wsi->state = WSI_STATE_HTTP;
202                 wsi->mode = LWS_CONNMODE_HTTP_SERVING;
203
204                 /* He asked for it to stay alive indefinitely */
205                 libwebsocket_set_timeout(wsi, NO_PENDING_TIMEOUT, 0);
206
207                 if (lws_allocate_header_table(wsi))
208                         goto bail;
209
210                 /* If we're (re)starting on headers, need other implied init */
211                 wsi->u.hdr.ues = URIES_IDLE;
212
213                 /* If we have more data, loop back around: */
214                 if (len)
215                         goto http_new;
216
217                 return 0;
218         }
219
220 bail:
221         lwsl_debug("closing connection at libwebsocket_read bail:\n");
222
223         libwebsocket_close_and_free_session(context, wsi,
224                                                      LWS_CLOSE_STATUS_NOSTATUS);
225
226         return -1;
227 }