introduce without extensions
[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 /*
50  * We have to take care about parsing because the headers may be split
51  * into multiple fragments.  They may contain unknown headers with arbitrary
52  * argument lengths.  So, we parse using a single-character at a time state
53  * machine that is completely independent of packet size.
54  */
55
56 int
57 libwebsocket_read(struct libwebsocket_context *context,
58                      struct libwebsocket *wsi, unsigned char * buf, size_t len)
59 {
60         size_t n;
61
62         switch (wsi->state) {
63         case WSI_STATE_HTTP_ISSUING_FILE:
64         case WSI_STATE_HTTP:
65                 wsi->state = WSI_STATE_HTTP_HEADERS;
66                 wsi->parser_state = WSI_TOKEN_NAME_PART;
67                 wsi->lextable_pos = 0;
68                 /* fallthru */
69         case WSI_STATE_HTTP_HEADERS:
70
71                 lwsl_parser("issuing %d bytes to parser\n", (int)len);
72 #ifdef _DEBUG
73                 //fwrite(buf, 1, len, stderr);
74 #endif
75
76 #ifndef LWS_NO_CLIENT
77
78 //              lwsl_info("mode=%d\n", wsi->mode);
79
80                 switch (wsi->mode) {
81                 case LWS_CONNMODE_WS_CLIENT_WAITING_PROXY_REPLY:
82                 case LWS_CONNMODE_WS_CLIENT_ISSUE_HANDSHAKE:
83                 case LWS_CONNMODE_WS_CLIENT_WAITING_SERVER_REPLY:
84                 case LWS_CONNMODE_WS_CLIENT_WAITING_EXTENSION_CONNECT:
85                 case LWS_CONNMODE_WS_CLIENT:
86                         for (n = 0; n < len; n++)
87                                 libwebsocket_client_rx_sm(wsi, *buf++);
88
89                         return 0;
90                 default:
91                         break;
92                 }
93 #endif
94 #ifndef LWS_NO_SERVER
95                 /* LWS_CONNMODE_WS_SERVING */
96
97                 extern int handshake_00(struct libwebsocket_context *context, struct libwebsocket *wsi);
98                 extern int handshake_0405(struct libwebsocket_context *context, struct libwebsocket *wsi);
99
100                 for (n = 0; n < len; n++)
101                         libwebsocket_parse(wsi, *buf++);
102
103                 if (wsi->parser_state != WSI_PARSING_COMPLETE)
104                         break;
105
106                 lwsl_parser("seem to be serving, mode is %d\n", wsi->mode);
107
108                 lwsl_parser("libwebsocket_parse sees parsing complete\n");
109
110                 /* is this websocket protocol or normal http 1.0? */
111
112                 if (!wsi->utf8_token[WSI_TOKEN_UPGRADE].token_len ||
113                              !wsi->utf8_token[WSI_TOKEN_CONNECTION].token_len) {
114                         wsi->state = WSI_STATE_HTTP;
115                         if (wsi->protocol->callback)
116                                 if (wsi->protocol->callback(context, wsi,
117                                                                 LWS_CALLBACK_HTTP, wsi->user_space,
118                                                                 wsi->utf8_token[WSI_TOKEN_GET_URI].token,
119                                                                 wsi->utf8_token[WSI_TOKEN_GET_URI].token_len)) {
120                                         lwsl_info("LWS_CALLBACK_HTTP wanted to close\n");
121                                         goto bail;
122                                 }
123                         return 0;
124                 }
125
126                 if (!wsi->protocol)
127                         lwsl_err("NULL protocol at libwebsocket_read\n");
128
129
130                 /*
131                  * It's websocket
132                  *
133                  * Make sure user side is happy about protocol
134                  */
135
136                 while (wsi->protocol->callback) {
137
138                         if (wsi->utf8_token[WSI_TOKEN_PROTOCOL].token == NULL) {
139                                 if (wsi->protocol->name == NULL)
140                                         break;
141                         } else
142                                 if (wsi->protocol->name && strcmp(
143                                      wsi->utf8_token[WSI_TOKEN_PROTOCOL].token,
144                                                       wsi->protocol->name) == 0)
145                                         break;
146
147                         wsi->protocol++;
148                 }
149
150                 /* we didn't find a protocol he wanted? */
151
152                 if (wsi->protocol->callback == NULL) {
153                         if (wsi->utf8_token[WSI_TOKEN_PROTOCOL].token == NULL)
154                                 lwsl_err("[no protocol] "
155                                         "not supported (use NULL .name)\n");
156                         else
157                                 lwsl_err("Requested protocol %s "
158                                                 "not supported\n",
159                                      wsi->utf8_token[WSI_TOKEN_PROTOCOL].token);
160                         goto bail;
161                 }
162
163                 /*
164                  * find out which spec version the client is using
165                  * if this header is not given, we default to 00 (aka 76)
166                  */
167
168                 if (wsi->utf8_token[WSI_TOKEN_VERSION].token_len)
169                         wsi->ietf_spec_revision =
170                                  atoi(wsi->utf8_token[WSI_TOKEN_VERSION].token);
171
172                 /*
173                  * Give the user code a chance to study the request and
174                  * have the opportunity to deny it
175                  */
176
177                 if ((wsi->protocol->callback)(wsi->protocol->owning_server, wsi,
178                                 LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION,
179                                                 &wsi->utf8_token[0], NULL, 0)) {
180                         lwsl_warn("User code denied connection\n");
181                         goto bail;
182                 }
183
184
185                 /*
186                  * Perform the handshake according to the protocol version the
187                  * client announced
188                  */
189
190                 switch (wsi->ietf_spec_revision) {
191                 case 0: /* applies to 76 and 00 */
192                         wsi->xor_mask = xor_no_mask;
193                         if (handshake_00(context, wsi)) {
194                                 lwsl_info("handshake_00 has failed the connection\n");
195                                 goto bail;
196                         }
197                         break;
198                 case 4: /* 04 */
199                         wsi->xor_mask = xor_mask_04;
200                         lwsl_parser("libwebsocket_parse calling handshake_04\n");
201                         if (handshake_0405(context, wsi)) {
202                                 lwsl_info("handshake_0405 has failed the connection\n");
203                                 goto bail;
204                         }
205                         break;
206                 case 5:
207                 case 6:
208                 case 7:
209                 case 8:
210                 case 13:
211                         wsi->xor_mask = xor_mask_05;
212                         lwsl_parser("libwebsocket_parse calling handshake_04\n");
213                         if (handshake_0405(context, wsi)) {
214                                 lwsl_info("handshake_0405 xor 05 has failed the connection\n");
215                                 goto bail;
216                         }
217                         break;
218
219                 default:
220                         lwsl_warn("Unknown client spec version %d\n",
221                                                        wsi->ietf_spec_revision);
222                         goto bail;
223                 }
224
225                 wsi->mode = LWS_CONNMODE_WS_SERVING;
226
227                 lwsl_parser("accepted v%02d connection\n",
228                                                        wsi->ietf_spec_revision);
229 #endif
230                 break;
231
232         case WSI_STATE_AWAITING_CLOSE_ACK:
233         case WSI_STATE_ESTABLISHED:
234 #ifndef LWS_NO_CLIENT
235                 switch (wsi->mode) {
236                 case LWS_CONNMODE_WS_CLIENT:
237                         for (n = 0; n < len; n++)
238                                 if (libwebsocket_client_rx_sm(wsi, *buf++) < 0) {
239                                         lwsl_info("client rx has bailed\n");
240                                         goto bail;
241                                 }
242
243                         return 0;
244                 default:
245                         break;
246                 }
247 #endif
248 #ifndef LWS_NO_SERVER
249                 /* LWS_CONNMODE_WS_SERVING */
250
251                 if (libwebsocket_interpret_incoming_packet(wsi, buf, len) < 0) {
252                         lwsl_info("interpret_incoming_packet has bailed\n");
253                         goto bail;
254                 }
255 #endif
256                 break;
257         default:
258                 lwsl_err("libwebsocket_read: Unhandled state\n");
259                 break;
260         }
261
262         return 0;
263
264 bail:
265         lwsl_info("closing connection at libwebsocket_read bail:\n");
266         libwebsocket_close_and_free_session(context, wsi,
267                                                      LWS_CLOSE_STATUS_NOSTATUS);
268
269         return -1;
270 }