Reset hdr_parsing_completed for WSI_STATE_HTTP:
[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 /*
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 libwebsocket_read(struct libwebsocket_context *context,
62                      struct libwebsocket *wsi, unsigned char *buf, size_t len)
63 {
64         size_t n;
65         int body_chunk_len;
66         unsigned char *last_char;
67
68         switch (wsi->state) {
69 http_new:
70         case WSI_STATE_HTTP:
71                 wsi->hdr_parsing_completed = 0;
72                 /* fallthru */
73         case WSI_STATE_HTTP_ISSUING_FILE:
74                 wsi->state = WSI_STATE_HTTP_HEADERS;
75                 wsi->u.hdr.parser_state = WSI_TOKEN_NAME_PART;
76                 wsi->u.hdr.lextable_pos = 0;
77                 /* fallthru */
78         case WSI_STATE_HTTP_HEADERS:
79                 lwsl_parser("issuing %d bytes to parser\n", (int)len);
80
81                 if (lws_handshake_client(wsi, &buf, len))
82                         goto bail;
83
84                 last_char = buf;
85                 if (lws_handshake_server(context, wsi, &buf, len))
86                         /* Handshake indicates this session is done. */
87                         goto bail;
88
89                 /* It's possible that we've exhausted our data already, but
90                  * lws_handshake_server doesn't update len for us. Figure out how
91                  * much was read, so that we can proceed appropriately: */
92                 len -= (buf - last_char);
93
94                 if (!wsi->hdr_parsing_completed)
95                         /* More header content on the way */
96                         goto read_ok;
97
98                 switch (wsi->state) {
99                         case WSI_STATE_HTTP:
100                         case WSI_STATE_HTTP_HEADERS:
101                                 goto http_complete;
102                         case WSI_STATE_HTTP_ISSUING_FILE:
103                                 goto read_ok;
104                         case WSI_STATE_HTTP_BODY:
105                                 wsi->u.http.content_remain = wsi->u.http.content_length;
106                                 goto http_postbody;
107                         default:
108                                 break;
109                 }
110                 break;
111
112         case WSI_STATE_HTTP_BODY:
113 http_postbody:
114                 while (len && wsi->u.http.content_remain) {
115                         /* Copy as much as possible, up to the limit of:
116                          * what we have in the read buffer (len)
117                          * remaining portion of the POST body (content_remain)
118                          */
119                         body_chunk_len = min(wsi->u.http.content_remain,len);
120                         wsi->u.http.content_remain -= body_chunk_len;
121                         len -= body_chunk_len;
122
123                         if (wsi->protocol->callback) {
124                                 n = wsi->protocol->callback(
125                                         wsi->protocol->owning_server, wsi,
126                                         LWS_CALLBACK_HTTP_BODY, wsi->user_space,
127                                         buf, body_chunk_len);
128                                 if (n)
129                                         goto bail;
130                         }
131                         buf += body_chunk_len;
132
133                         if (!wsi->u.http.content_remain)  {
134                                 /* he sent the content in time */
135                                 libwebsocket_set_timeout(wsi, NO_PENDING_TIMEOUT, 0);
136                                 if (wsi->protocol->callback) {
137                                         n = wsi->protocol->callback(
138                                                 wsi->protocol->owning_server, wsi,
139                                                 LWS_CALLBACK_HTTP_BODY_COMPLETION,
140                                                 wsi->user_space, NULL, 0);
141                                         if (n)
142                                                 goto bail;
143                                 }
144                                 goto http_complete;
145                         }
146                 }
147                 break;
148
149         case WSI_STATE_ESTABLISHED:
150         case WSI_STATE_AWAITING_CLOSE_ACK:
151                 if (lws_handshake_client(wsi, &buf, len))
152                         goto bail;
153                 switch (wsi->mode) {
154                 case LWS_CONNMODE_WS_SERVING:
155
156                         if (libwebsocket_interpret_incoming_packet(wsi, buf, len) < 0) {
157                                 lwsl_info("interpret_incoming_packet has bailed\n");
158                                 goto bail;
159                         }
160                         break;
161                 }
162                 break;
163         default:
164                 lwsl_err("libwebsocket_read: Unhandled state\n");
165                 break;
166         }
167
168 read_ok:
169         /* Nothing more to do for now. */
170         lwsl_debug("libwebsocket_read: read_ok\n");
171
172         return 0;
173
174 http_complete:
175         lwsl_debug("libwebsocket_read: http_complete\n");
176         /* Handle keep-alives, by preparing for a new request: */
177         if (wsi->u.http.connection_type == HTTP_CONNECTION_KEEP_ALIVE) {
178                 lwsl_debug("libwebsocket_read: keep-alive\n");
179                 wsi->state = WSI_STATE_HTTP;
180                 wsi->mode = LWS_CONNMODE_HTTP_SERVING;
181                 /* We might be streaming HTTP content, so leave the connection open.*/
182                 libwebsocket_set_timeout(wsi, NO_PENDING_TIMEOUT, 0);
183
184                 if (lws_allocate_header_table(wsi))
185                         goto bail;
186
187                 /* If we have more data, loop back around: */
188                 if (len)
189                         goto http_new;
190
191                 return 0;
192         }
193
194 bail:
195         lwsl_debug("closing connection at libwebsocket_read bail:\n");
196
197         libwebsocket_close_and_free_session(context, wsi,
198                                                      LWS_CLOSE_STATUS_NOSTATUS);
199
200         return -1;
201 }