api rationalization use new names internally
[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 #ifndef min
50 #define min(a, b) ((a) < (b) ? (a) : (b))
51 #endif
52 /*
53  * We have to take care about parsing because the headers may be split
54  * into multiple fragments.  They may contain unknown headers with arbitrary
55  * argument lengths.  So, we parse using a single-character at a time state
56  * machine that is completely independent of packet size.
57  */
58
59 LWS_VISIBLE int
60 lws_read(struct libwebsocket_context *context,
61                      struct libwebsocket *wsi, unsigned char *buf, size_t len)
62 {
63         size_t n;
64         int body_chunk_len;
65         unsigned char *last_char;
66
67         switch (wsi->state) {
68 #ifdef LWS_USE_HTTP2
69         case WSI_STATE_HTTP2_AWAIT_CLIENT_PREFACE:
70         case WSI_STATE_HTTP2_ESTABLISHED_PRE_SETTINGS:
71         case WSI_STATE_HTTP2_ESTABLISHED:
72                 n = 0;
73                 while (n < len) {
74                         /*
75                          * we were accepting input but now we stopped doing so
76                          */
77                         if (!(wsi->rxflow_change_to & LWS_RXFLOW_ALLOW)) {
78                                 lws_rxflow_cache(wsi, buf, n, len);
79
80                                 return 1;
81                         }
82
83                         /* account for what we're using in rxflow buffer */
84                         if (wsi->rxflow_buffer)
85                                 wsi->rxflow_pos++;
86                         if (lws_http2_parser(context, wsi, buf[n++]))
87                                 goto bail;
88                 }
89                 break;
90 #endif
91 http_new:
92         case WSI_STATE_HTTP:
93                 wsi->hdr_parsing_completed = 0;
94                 /* fallthru */
95         case WSI_STATE_HTTP_ISSUING_FILE:
96                 wsi->state = WSI_STATE_HTTP_HEADERS;
97                 wsi->u.hdr.parser_state = WSI_TOKEN_NAME_PART;
98                 wsi->u.hdr.lextable_pos = 0;
99                 /* fallthru */
100         case WSI_STATE_HTTP_HEADERS:
101                 lwsl_parser("issuing %d bytes to parser\n", (int)len);
102
103                 if (lws_handshake_client(wsi, &buf, len))
104                         goto bail;
105
106                 last_char = buf;
107                 if (lws_handshake_server(context, wsi, &buf, len))
108                         /* Handshake indicates this session is done. */
109                         goto bail;
110
111                 /* It's possible that we've exhausted our data already, but
112                  * lws_handshake_server doesn't update len for us. Figure out how
113                  * much was read, so that we can proceed appropriately: */
114                 len -= (buf - last_char);
115
116                 if (!wsi->hdr_parsing_completed)
117                         /* More header content on the way */
118                         goto read_ok;
119
120                 switch (wsi->state) {
121                         case WSI_STATE_HTTP:
122                         case WSI_STATE_HTTP_HEADERS:
123                                 goto http_complete;
124                         case WSI_STATE_HTTP_ISSUING_FILE:
125                                 goto read_ok;
126                         case WSI_STATE_HTTP_BODY:
127                                 wsi->u.http.content_remain = wsi->u.http.content_length;
128                                 if (!wsi->u.http.content_remain) {
129                                         /* there is no POST content */
130                                         lws_set_timeout(wsi, NO_PENDING_TIMEOUT, 0);
131                                         if (wsi->protocol->callback) {
132                                                 n = wsi->protocol->callback(
133                                                         wsi->protocol->owning_server, wsi,
134                                                         LWS_CALLBACK_HTTP_BODY_COMPLETION,
135                                                         wsi->user_space, NULL, 0);
136                                                 if (n)
137                                                         goto bail;
138                                         }
139                                         goto http_complete;
140                                 }
141                                 goto http_postbody;
142                         default:
143                                 break;
144                 }
145                 break;
146
147         case WSI_STATE_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                         if (wsi->protocol->callback) {
159                                 n = wsi->protocol->callback(
160                                         wsi->protocol->owning_server, wsi,
161                                         LWS_CALLBACK_HTTP_BODY, wsi->user_space,
162                                         buf, body_chunk_len);
163                                 if (n)
164                                         goto bail;
165                         }
166                         buf += body_chunk_len;
167
168                         if (!wsi->u.http.content_remain)  {
169                                 /* he sent the content in time */
170                                 lws_set_timeout(wsi, NO_PENDING_TIMEOUT, 0);
171                                 if (wsi->protocol->callback) {
172                                         n = wsi->protocol->callback(
173                                                 wsi->protocol->owning_server, wsi,
174                                                 LWS_CALLBACK_HTTP_BODY_COMPLETION,
175                                                 wsi->user_space, NULL, 0);
176                                         if (n)
177                                                 goto bail;
178                                 }
179                                 goto http_complete;
180                         } else
181                                 lws_set_timeout(wsi,
182                                         PENDING_TIMEOUT_HTTP_CONTENT,
183                                         AWAITING_TIMEOUT);
184                 }
185                 break;
186
187         case WSI_STATE_ESTABLISHED:
188         case WSI_STATE_AWAITING_CLOSE_ACK:
189                 if (lws_handshake_client(wsi, &buf, len))
190                         goto bail;
191                 switch (wsi->mode) {
192                 case LWS_CONNMODE_WS_SERVING:
193
194                         if (libwebsocket_interpret_incoming_packet(wsi, buf, len) < 0) {
195                                 lwsl_info("interpret_incoming_packet has bailed\n");
196                                 goto bail;
197                         }
198                         break;
199                 }
200                 break;
201         default:
202                 lwsl_err("lws_read: Unhandled state\n");
203                 break;
204         }
205
206 read_ok:
207         /* Nothing more to do for now. */
208         lwsl_debug("lws_read: read_ok\n");
209
210         return 0;
211
212 http_complete:
213         lwsl_debug("lws_read: http_complete\n");
214
215 #ifndef LWS_NO_SERVER
216         /* Did the client want to keep the HTTP connection going? */
217         if (lws_http_transaction_completed(wsi))
218                 goto bail;
219 #endif
220         /* If we have more data, loop back around: */
221         if (len)
222                 goto http_new;
223
224         return 0;
225
226 bail:
227         lwsl_debug("closing connection at lws_read bail:\n");
228
229         libwebsocket_close_and_free_session(context, wsi,
230                                                      LWS_CLOSE_STATUS_NOSTATUS);
231
232         return -1;
233 }