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