style cleanup
[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->u.hdr.parser_state = WSI_TOKEN_NAME_PART;
67                 wsi->u.hdr.lextable_pos = 0;
68                 /* fallthru */
69         case WSI_STATE_HTTP_HEADERS:
70
71                 lwsl_parser("issuing %d bytes to parser\n", (int)len);
72
73 #ifndef LWS_NO_CLIENT
74                 switch (wsi->mode) {
75                 case LWS_CONNMODE_WS_CLIENT_WAITING_PROXY_REPLY:
76                 case LWS_CONNMODE_WS_CLIENT_ISSUE_HANDSHAKE:
77                 case LWS_CONNMODE_WS_CLIENT_WAITING_SERVER_REPLY:
78                 case LWS_CONNMODE_WS_CLIENT_WAITING_EXTENSION_CONNECT:
79                 case LWS_CONNMODE_WS_CLIENT:
80                         for (n = 0; n < len; n++)
81                                 if (libwebsocket_client_rx_sm(wsi, *buf++)) {
82                                         lwsl_info("client_rx_sm failed\n");
83                                         goto bail;
84                                 }
85                         return 0;
86                 default:
87                         break;
88                 }
89 #endif
90 #ifndef LWS_NO_SERVER
91                 /* LWS_CONNMODE_WS_SERVING */
92
93                 for (n = 0; n < len; n++)
94                         if (libwebsocket_parse(wsi, *buf++)) {
95                                 lwsl_info("libwebsocket_parse failed\n");
96                                 goto bail;
97                         }
98
99                 if (wsi->u.hdr.parser_state != WSI_PARSING_COMPLETE)
100                         break;
101
102                 lwsl_parser("libwebsocket_parse sees parsing complete\n");
103
104                 /* is this websocket protocol or normal http 1.0? */
105
106                 if (!lws_hdr_total_length(wsi, WSI_TOKEN_UPGRADE) ||
107                              !lws_hdr_total_length(wsi, WSI_TOKEN_CONNECTION)) {
108                         wsi->state = WSI_STATE_HTTP;
109                         n = 0;
110                         if (wsi->protocol->callback)
111                                 n = wsi->protocol->callback(context, wsi,
112                                     LWS_CALLBACK_HTTP,
113                                     wsi->user_space,
114                                     lws_hdr_simple_ptr(wsi, WSI_TOKEN_GET_URI),
115                                   lws_hdr_total_length(wsi, WSI_TOKEN_GET_URI));
116
117                         /* drop the header info */
118                         if (wsi->u.hdr.ah)
119                                 free(wsi->u.hdr.ah);
120
121                         if (n) {
122                                 lwsl_info("LWS_CALLBACK_HTTP closing\n");
123                                 goto bail;
124                         }
125
126                         return 0;
127                 }
128
129                 if (!wsi->protocol)
130                         lwsl_err("NULL protocol at libwebsocket_read\n");
131
132                 /*
133                  * It's websocket
134                  *
135                  * Make sure user side is happy about protocol
136                  */
137
138                 while (wsi->protocol->callback) {
139
140                         if (!lws_hdr_total_length(wsi, WSI_TOKEN_PROTOCOL)) {
141                                 if (wsi->protocol->name == NULL)
142                                         break;
143                         } else
144                                 if (wsi->protocol->name && strcmp(
145                                         lws_hdr_simple_ptr(wsi,
146                                                 WSI_TOKEN_PROTOCOL),
147                                                       wsi->protocol->name) == 0)
148                                         break;
149
150                         wsi->protocol++;
151                 }
152
153                 /* we didn't find a protocol he wanted? */
154
155                 if (wsi->protocol->callback == NULL) {
156                         if (lws_hdr_simple_ptr(wsi, WSI_TOKEN_PROTOCOL) ==
157                                                                          NULL) {
158                                 lwsl_info("no protocol -> prot 0 handler\n");
159                                 wsi->protocol = &context->protocols[0];
160                         } else {
161                                 lwsl_err("Req protocol %s not supported\n",
162                                    lws_hdr_simple_ptr(wsi, WSI_TOKEN_PROTOCOL));
163                                 goto bail;
164                         }
165                 }
166
167                 /*
168                  * Give the user code a chance to study the request and
169                  * have the opportunity to deny it
170                  */
171
172                 if ((wsi->protocol->callback)(wsi->protocol->owning_server, wsi,
173                                 LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION,
174                                 lws_hdr_simple_ptr(wsi, WSI_TOKEN_PROTOCOL),
175                                                                      NULL, 0)) {
176                         lwsl_warn("User code denied connection\n");
177                         goto bail;
178                 }
179
180
181                 /*
182                  * Perform the handshake according to the protocol version the
183                  * client announced
184                  */
185
186                 switch (wsi->ietf_spec_revision) {
187                 case 13:
188                         lwsl_parser("lws_parse calling handshake_04\n");
189                         if (handshake_0405(context, wsi)) {
190                                 lwsl_info("hs0405 has failed the connection\n");
191                                 goto bail;
192                         }
193                         break;
194
195                 default:
196                         lwsl_warn("Unknown client spec version %d\n",
197                                                        wsi->ietf_spec_revision);
198                         goto bail;
199                 }
200
201                 /* drop the header info */
202
203                 if (wsi->u.hdr.ah)
204                         free(wsi->u.hdr.ah);
205
206                 wsi->mode = LWS_CONNMODE_WS_SERVING;
207
208                 /* union transition */
209                 memset(&wsi->u, 0, sizeof(wsi->u));
210
211                 /*
212                  * create the frame buffer for this connection according to the
213                  * size mentioned in the protocol definition.  If 0 there, use
214                  * a big default for compatibility
215                  */
216
217                 n = wsi->protocol->rx_buffer_size;
218                 if (!n)
219                         n = LWS_MAX_SOCKET_IO_BUF;
220                 n += LWS_SEND_BUFFER_PRE_PADDING + LWS_SEND_BUFFER_POST_PADDING;
221                 wsi->u.ws.rx_user_buffer = malloc(n);
222                 if (!wsi->u.ws.rx_user_buffer) {
223                         lwsl_err("Out of Mem allocating rx buffer %d\n", n);
224                         goto bail;
225                 }
226                 lwsl_info("Allocating RX buffer %d\n", n);
227
228                 lwsl_parser("accepted v%02d connection\n",
229                                                        wsi->ietf_spec_revision);
230 #endif
231                 break;
232
233         case WSI_STATE_AWAITING_CLOSE_ACK:
234         case WSI_STATE_ESTABLISHED:
235 #ifndef LWS_NO_CLIENT
236                 switch (wsi->mode) {
237                 case LWS_CONNMODE_WS_CLIENT:
238                         for (n = 0; n < len; n++)
239                                 if (libwebsocket_client_rx_sm(
240                                                              wsi, *buf++) < 0) {
241                                         lwsl_info("client rx has bailed\n");
242                                         goto bail;
243                                 }
244
245                         return 0;
246                 default:
247                         break;
248                 }
249 #endif
250 #ifndef LWS_NO_SERVER
251                 /* LWS_CONNMODE_WS_SERVING */
252
253                 if (libwebsocket_interpret_incoming_packet(wsi, buf, len) < 0) {
254                         lwsl_info("interpret_incoming_packet has bailed\n");
255                         goto bail;
256                 }
257 #endif
258                 break;
259         default:
260                 lwsl_err("libwebsocket_read: Unhandled state\n");
261                 break;
262         }
263
264         return 0;
265
266 bail:
267         lwsl_info("closing connection at libwebsocket_read bail:\n");
268
269         libwebsocket_close_and_free_session(context, wsi,
270                                                      LWS_CLOSE_STATUS_NOSTATUS);
271
272         return -1;
273 }