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