9bae6e45700a9ef75626787399b73467487373cf
[platform/upstream/libwebsockets.git] / lib / handshake.c
1 /*
2  * libwebsockets - small server side websockets and web server implementation
3  *
4  * Copyright (C) 2010-2015 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 /*
54  * We have to take care about parsing because the headers may be split
55  * into multiple fragments.  They may contain unknown headers with arbitrary
56  * argument lengths.  So, we parse using a single-character at a time state
57  * machine that is completely independent of packet size.
58  *
59  * Returns <0 for error or length of chars consumed from buf (up to len)
60  */
61
62 LWS_VISIBLE int
63 lws_read(struct lws *wsi, unsigned char *buf, size_t len)
64 {
65         unsigned char *last_char, *oldbuf = buf;
66         int body_chunk_len;
67         size_t n;
68
69         lwsl_debug("%s: incoming len %d\n", __func__, (int)len);
70
71         switch (wsi->state) {
72 #ifdef LWS_USE_HTTP2
73         case LWSS_HTTP2_AWAIT_CLIENT_PREFACE:
74         case LWSS_HTTP2_ESTABLISHED_PRE_SETTINGS:
75         case LWSS_HTTP2_ESTABLISHED:
76                 n = 0;
77                 while (n < len) {
78                         /*
79                          * we were accepting input but now we stopped doing so
80                          */
81                         if (!(wsi->rxflow_change_to & LWS_RXFLOW_ALLOW)) {
82                                 lws_rxflow_cache(wsi, buf, n, len);
83
84                                 return 1;
85                         }
86
87                         /* account for what we're using in rxflow buffer */
88                         if (wsi->rxflow_buffer)
89                                 wsi->rxflow_pos++;
90                         if (lws_http2_parser(wsi, buf[n++]))
91                                 goto bail;
92                 }
93                 break;
94 #endif
95
96         case LWSS_HTTP:
97                 wsi->hdr_parsing_completed = 0;
98                 /* fallthru */
99         case LWSS_HTTP_ISSUING_FILE:
100                 wsi->state = LWSS_HTTP_HEADERS;
101                 wsi->u.hdr.parser_state = WSI_TOKEN_NAME_PART;
102                 wsi->u.hdr.lextable_pos = 0;
103                 /* fallthru */
104         case LWSS_HTTP_HEADERS:
105                 assert(wsi->u.hdr.ah);
106                 lwsl_parser("issuing %d bytes to parser\n", (int)len);
107
108                 if (lws_handshake_client(wsi, &buf, len))
109                         goto bail;
110
111                 last_char = buf;
112                 if (lws_handshake_server(wsi, &buf, len))
113                         /* Handshake indicates this session is done. */
114                         goto bail;
115
116                 /*
117                  * It's possible that we've exhausted our data already, or
118                  * rx flow control has stopped us dealing with this early,
119                  * but lws_handshake_server doesn't update len for us.
120                  * Figure out how much was read, so that we can proceed
121                  * appropriately:
122                  */
123                 len -= (buf - last_char);
124                 lwsl_debug("%s: thinks we have used %d\n", __func__, len);
125
126                 if (!wsi->hdr_parsing_completed)
127                         /* More header content on the way */
128                         goto read_ok;
129
130                 switch (wsi->state) {
131                         case LWSS_HTTP:
132                         case LWSS_HTTP_HEADERS:
133                                 goto http_complete;
134                         case LWSS_HTTP_ISSUING_FILE:
135                                 goto read_ok;
136                         case LWSS_HTTP_BODY:
137                                 wsi->u.http.content_remain =
138                                                 wsi->u.http.content_length;
139                                 if (wsi->u.http.content_remain)
140                                         goto http_postbody;
141
142                                 /* there is no POST content */
143                                 goto postbody_completion;
144                         default:
145                                 break;
146                 }
147                 break;
148
149         case LWSS_HTTP_BODY:
150 http_postbody:
151                 while (len && wsi->u.http.content_remain) {
152                         /* Copy as much as possible, up to the limit of:
153                          * what we have in the read buffer (len)
154                          * remaining portion of the POST body (content_remain)
155                          */
156                         body_chunk_len = min(wsi->u.http.content_remain,len);
157                         wsi->u.http.content_remain -= body_chunk_len;
158                         len -= body_chunk_len;
159
160                         n = wsi->protocol->callback(wsi,
161                                 LWS_CALLBACK_HTTP_BODY, wsi->user_space,
162                                 buf, body_chunk_len);
163                         if (n)
164                                 goto bail;
165
166                         buf += body_chunk_len;
167
168                         if (wsi->u.http.content_remain)  {
169                                 lws_set_timeout(wsi, PENDING_TIMEOUT_HTTP_CONTENT,
170                                                 wsi->context->timeout_secs);
171                                 break;
172                         }
173                         /* he sent all the content in time */
174 postbody_completion:
175                         lws_set_timeout(wsi, NO_PENDING_TIMEOUT, 0);
176                         n = wsi->protocol->callback(wsi,
177                                 LWS_CALLBACK_HTTP_BODY_COMPLETION,
178                                 wsi->user_space, NULL, 0);
179                         if (n)
180                                 goto bail;
181
182                         goto http_complete;
183                 }
184                 break;
185
186         case LWSS_ESTABLISHED:
187         case LWSS_AWAITING_CLOSE_ACK:
188         case LWSS_SHUTDOWN:
189                 if (lws_handshake_client(wsi, &buf, len))
190                         goto bail;
191                 switch (wsi->mode) {
192                 case LWSCM_WS_SERVING:
193
194                         if (lws_interpret_incoming_packet(wsi, &buf, len) < 0) {
195                                 lwsl_info("interpret_incoming_packet has bailed\n");
196                                 goto bail;
197                         }
198                         break;
199                 }
200                 break;
201         default:
202                 lwsl_err("%s: Unhandled state\n", __func__);
203                 break;
204         }
205
206 read_ok:
207         /* Nothing more to do for now */
208         lwsl_info("%s: read_ok, used %d\n", __func__, buf - oldbuf);
209
210         return buf - oldbuf;
211
212 http_complete:
213         lwsl_debug("%s: http_complete\n", __func__);
214
215 #ifndef LWS_NO_SERVER
216         /* Did the client want to keep the HTTP connection going? */
217         if (lws_http_transaction_completed(wsi))
218                 goto bail;
219 #endif
220         /* we may have next header set already, but return to event loop first
221          * so a heaily-pipelined http/1.1 connection cannot monopolize the
222          * service thread with GET hugefile.bin GET hugefile.bin etc
223          */
224         goto read_ok;
225
226 bail:
227         lwsl_debug("closing connection at lws_read bail:\n");
228         lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS);
229
230         return -1;
231 }