win32: enable 64-bit file lengths
[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, lws_filepos_t len)
64 {
65         unsigned char *last_char, *oldbuf = buf;
66         lws_filepos_t body_chunk_len;
67         size_t n;
68
69         lwsl_debug("%s: incoming len %d  state %d\n", __func__, (int)len, wsi->state);
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                                 lwsl_debug("%s: http2_parser bailed\n", __func__);
92                                 goto bail;
93                         }
94                 }
95                 break;
96 #endif
97
98         case LWSS_CLIENT_HTTP_ESTABLISHED:
99                 break;
100
101         case LWSS_HTTP:
102                 wsi->hdr_parsing_completed = 0;
103                 /* fallthru */
104         case LWSS_HTTP_ISSUING_FILE:
105                 wsi->state = LWSS_HTTP_HEADERS;
106                 wsi->u.hdr.parser_state = WSI_TOKEN_NAME_PART;
107                 wsi->u.hdr.lextable_pos = 0;
108                 /* fallthru */
109         case LWSS_HTTP_HEADERS:
110                 if (!wsi->u.hdr.ah) {
111                         lwsl_err("%s: LWSS_HTTP_HEADERS: NULL ah\n", __func__);
112                         assert(0);
113                 }
114                 lwsl_parser("issuing %d bytes to parser\n", (int)len);
115
116                 if (lws_handshake_client(wsi, &buf, (size_t)len))
117                         goto bail;
118
119                 last_char = buf;
120                 if (lws_handshake_server(wsi, &buf, (size_t)len))
121                         /* Handshake indicates this session is done. */
122                         goto bail;
123
124                 /* we might have transitioned to RAW */
125                 if (wsi->mode == LWSCM_RAW)
126                          /* we gave the read buffer to RAW handler already */
127                         goto read_ok;
128
129                 /*
130                  * It's possible that we've exhausted our data already, or
131                  * rx flow control has stopped us dealing with this early,
132                  * but lws_handshake_server doesn't update len for us.
133                  * Figure out how much was read, so that we can proceed
134                  * appropriately:
135                  */
136                 len -= (buf - last_char);
137                 lwsl_debug("%s: thinks we have used %ld\n", __func__, (long)len);
138
139                 if (!wsi->hdr_parsing_completed)
140                         /* More header content on the way */
141                         goto read_ok;
142
143                 switch (wsi->state) {
144                         case LWSS_HTTP:
145                         case LWSS_HTTP_HEADERS:
146                                 goto read_ok;
147                         case LWSS_HTTP_ISSUING_FILE:
148                                 goto read_ok;
149                         case LWSS_HTTP_BODY:
150                                 wsi->u.http.content_remain =
151                                                 wsi->u.http.content_length;
152                                 if (wsi->u.http.content_remain)
153                                         goto http_postbody;
154
155                                 /* there is no POST content */
156                                 goto postbody_completion;
157                         default:
158                                 break;
159                 }
160                 break;
161
162         case LWSS_HTTP_BODY:
163 http_postbody:
164                 while (len && wsi->u.http.content_remain) {
165                         /* Copy as much as possible, up to the limit of:
166                          * what we have in the read buffer (len)
167                          * remaining portion of the POST body (content_remain)
168                          */
169                         body_chunk_len = min(wsi->u.http.content_remain,len);
170                         wsi->u.http.content_remain -= body_chunk_len;
171                         len -= body_chunk_len;
172 #ifdef LWS_WITH_CGI
173                         if (wsi->cgi) {
174                                 struct lws_cgi_args args;
175
176                                 args.ch = LWS_STDIN;
177                                 args.stdwsi = &wsi->cgi->stdwsi[0];
178                                 args.data = buf;
179                                 args.len = body_chunk_len;
180
181                                 /* returns how much used */
182                                 n = user_callback_handle_rxflow(
183                                         wsi->protocol->callback,
184                                         wsi, LWS_CALLBACK_CGI_STDIN_DATA,
185                                         wsi->user_space,
186                                         (void *)&args, 0);
187                                 if ((int)n < 0)
188                                         goto bail;
189                         } else {
190 #endif
191                                 n = wsi->protocol->callback(wsi,
192                                         LWS_CALLBACK_HTTP_BODY, wsi->user_space,
193                                         buf, (size_t)body_chunk_len);
194                                 if (n)
195                                         goto bail;
196                                 n = (size_t)body_chunk_len;
197 #ifdef LWS_WITH_CGI
198                         }
199 #endif
200                         buf += n;
201
202                         if (wsi->u.http.content_remain)  {
203                                 lws_set_timeout(wsi, PENDING_TIMEOUT_HTTP_CONTENT,
204                                                 wsi->context->timeout_secs);
205                                 break;
206                         }
207                         /* he sent all the content in time */
208 postbody_completion:
209 #ifdef LWS_WITH_CGI
210                         /* if we're running a cgi, we can't let him off the hook just because he sent his POST data */
211                         if (wsi->cgi)
212                                 lws_set_timeout(wsi, PENDING_TIMEOUT_CGI, wsi->context->timeout_secs);
213                         else
214 #endif
215                         lws_set_timeout(wsi, NO_PENDING_TIMEOUT, 0);
216 #ifdef LWS_WITH_CGI
217                         if (!wsi->cgi)
218 #endif
219                         {
220                                 n = wsi->protocol->callback(wsi,
221                                         LWS_CALLBACK_HTTP_BODY_COMPLETION,
222                                         wsi->user_space, NULL, 0);
223                                 if (n)
224                                         goto bail;
225                         }
226
227                         break;
228                 }
229                 break;
230
231         case LWSS_ESTABLISHED:
232         case LWSS_AWAITING_CLOSE_ACK:
233         case LWSS_SHUTDOWN:
234                 if (lws_handshake_client(wsi, &buf, (size_t)len))
235                         goto bail;
236                 switch (wsi->mode) {
237                 case LWSCM_WS_SERVING:
238
239                         if (lws_interpret_incoming_packet(wsi, &buf, (size_t)len) < 0) {
240                                 lwsl_info("interpret_incoming_packet has bailed\n");
241                                 goto bail;
242                         }
243                         break;
244                 }
245                 break;
246         default:
247                 lwsl_err("%s: Unhandled state %d\n", __func__, wsi->state);
248                 break;
249         }
250
251 read_ok:
252         /* Nothing more to do for now */
253         lwsl_info("%s: read_ok, used %ld\n", __func__, (long)(buf - oldbuf));
254
255         return buf - oldbuf;
256
257 bail:
258         //lwsl_notice("closing connection at lws_read bail:\n");
259         lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS);
260
261         return -1;
262 }