refactor handshake client and server handling into client.c and server.c
[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 LWS_VISIBLE 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
64         case WSI_STATE_HTTP_BODY:
65 http_postbody:
66                 while (len--) {
67
68                         if (wsi->u.http.content_length_seen >= wsi->u.http.content_length)
69                                 break;
70
71                         wsi->u.http.post_buffer[wsi->u.http.body_index++] = *buf++;
72                         wsi->u.http.content_length_seen++;
73                         n = wsi->protocol->rx_buffer_size;
74                         if (!n)
75                                 n = LWS_MAX_SOCKET_IO_BUF;
76
77                         if (wsi->u.http.body_index != n &&
78                             wsi->u.http.content_length_seen != wsi->u.http.content_length)
79                                 continue;
80
81                         if (wsi->protocol->callback) {
82                                 n = wsi->protocol->callback(
83                                         wsi->protocol->owning_server, wsi,
84                                             LWS_CALLBACK_HTTP_BODY,
85                                             wsi->user_space, wsi->u.http.post_buffer,
86                                                         wsi->u.http.body_index);
87                                 wsi->u.http.body_index = 0;
88                                 if (n)
89                                         goto bail;
90                         }
91
92                         if (wsi->u.http.content_length_seen == wsi->u.http.content_length) {
93                                 /* he sent the content in time */
94                                 libwebsocket_set_timeout(wsi, NO_PENDING_TIMEOUT, 0);
95                                 n = wsi->protocol->callback(
96                                         wsi->protocol->owning_server, wsi,
97                                             LWS_CALLBACK_HTTP_BODY_COMPLETION,
98                                             wsi->user_space, NULL, 0);
99                                 wsi->u.http.body_index = 0;
100                                 if (n)
101                                         goto bail;
102                         }
103
104                 }
105
106                 /* 
107                  * we need to spill here so everything is seen in the case
108                  * there is no content-length
109                  */
110                 if (wsi->u.http.body_index && wsi->protocol->callback) {
111                         n = wsi->protocol->callback(
112                                 wsi->protocol->owning_server, wsi,
113                                     LWS_CALLBACK_HTTP_BODY,
114                                     wsi->user_space, wsi->u.http.post_buffer,
115                                                 wsi->u.http.body_index);
116                         wsi->u.http.body_index = 0;
117                         if (n)
118                                 goto bail;
119                 }
120                 break;
121
122         case WSI_STATE_HTTP_ISSUING_FILE:
123         case WSI_STATE_HTTP:
124                 wsi->state = WSI_STATE_HTTP_HEADERS;
125                 wsi->u.hdr.parser_state = WSI_TOKEN_NAME_PART;
126                 wsi->u.hdr.lextable_pos = 0;
127                 /* fallthru */
128         case WSI_STATE_HTTP_HEADERS:
129
130                 lwsl_parser("issuing %d bytes to parser\n", (int)len);
131
132                 if (lws_handshake_client(wsi, &buf, len))
133                         goto bail;
134
135                 switch (lws_handshake_server(context, wsi, &buf, len)) {
136                 case 1:
137                         goto bail;
138                 case 2:
139                         goto http_postbody;
140                 }
141                 break;
142
143         case WSI_STATE_AWAITING_CLOSE_ACK:
144         case WSI_STATE_ESTABLISHED:
145 #ifndef LWS_NO_CLIENT
146                 switch (wsi->mode) {
147                 case LWS_CONNMODE_WS_CLIENT:
148                         for (n = 0; n < len; n++)
149                                 if (libwebsocket_client_rx_sm(
150                                                              wsi, *buf++)) {
151                                         lwsl_debug("client rx has bailed\n");
152                                         goto bail;
153                                 }
154
155                         return 0;
156                 default:
157                         break;
158                 }
159 #endif
160 #ifndef LWS_NO_SERVER
161                 /* LWS_CONNMODE_WS_SERVING */
162
163                 if (libwebsocket_interpret_incoming_packet(wsi, buf, len) < 0) {
164                         lwsl_info("interpret_incoming_packet has bailed\n");
165                         goto bail;
166                 }
167 #endif
168                 break;
169         default:
170                 lwsl_err("libwebsocket_read: Unhandled state\n");
171                 break;
172         }
173
174         return 0;
175
176 bail:
177         lwsl_debug("closing connection at libwebsocket_read bail:\n");
178
179         libwebsocket_close_and_free_session(context, wsi,
180                                                      LWS_CLOSE_STATUS_NOSTATUS);
181
182         return -1;
183 }