2 * libwebsockets web server application
4 * Copyright (C) 2010-2016 Andy Green <andy@warmcat.com>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public
8 * License as published by the Free Software Foundation:
9 * version 2.1 of the License.
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 * General Public License for more details.
16 * You should have received a copy of the GNU General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
23 /* http server gets files from this path */
24 #define LOCAL_RESOURCE_PATH INSTALL_DATADIR"/libwebsockets-test-server"
25 char *resource_path = LOCAL_RESOURCE_PATH;
30 * We take a strict whitelist approach to stop ../ attacks
37 const char * get_mimetype(const char *file)
44 if (!strcmp(&file[n - 4], ".ico"))
45 return "image/x-icon";
47 if (!strcmp(&file[n - 4], ".png"))
50 if (!strcmp(&file[n - 4], ".jpg"))
53 if (!strcmp(&file[n - 5], ".html"))
56 if (!strcmp(&file[n - 4], ".css"))
62 /* this protocol server (always the first one) handles HTTP,
64 * Some misc callbacks that aren't associated with a protocol also turn up only
65 * here on the first protocol server.
68 int callback_http(struct lws *wsi, enum lws_callback_reasons reason, void *user,
71 struct per_session_data__http *pss =
72 (struct per_session_data__http *)user;
73 unsigned char buffer[4096 + LWS_PRE];
81 struct per_session_data__http *pss1;
88 struct lws_pollargs *pa = (struct lws_pollargs *)in;
91 // lwsl_err("%s: reason %d\n", __func__, reason);
94 case LWS_CALLBACK_HTTP:
97 char name[100], rip[50];
98 lws_get_peer_addresses(wsi, lws_get_socket_fd(wsi), name,
99 sizeof(name), rip, sizeof(rip));
100 sprintf(buf, "%s (%s)", name, rip);
101 lwsl_notice("HTTP connect from %s\n", buf);
105 lws_return_http_status(wsi,
106 HTTP_STATUS_BAD_REQUEST, NULL);
110 #ifndef LWS_NO_CLIENT
111 if (!strncmp(in, "/proxytest", 10)) {
112 struct lws_client_connect_info i;
113 char *rootpath = "/";
114 const char *p = (const char *)in;
116 if (lws_get_child(wsi))
119 pss->client_finished = 0;
120 memset(&i,0, sizeof(i));
121 i.context = lws_get_context(wsi);
122 i.address = "git.libwebsockets.org";
124 i.ssl_connection = 0;
126 i.path = (char *)in + 10;
129 i.host = "git.libwebsockets.org";
133 i.uri_replace_from = "git.libwebsockets.org/";
134 i.uri_replace_to = "/proxytest/";
135 if (!lws_client_connect_via_info(&i)) {
136 lwsl_err("proxy connect fail\n");
144 /* this example server has no concept of directories */
145 if (strchr((const char *)in + 1, '/')) {
146 lws_return_http_status(wsi, HTTP_STATUS_FORBIDDEN, NULL);
151 /* if a legal POST URL, let it continue and accept data */
152 if (lws_hdr_total_length(wsi, WSI_TOKEN_POST_URI))
155 strncpy(buf, resource_path, sizeof(buf) - 1);
156 buf[sizeof(buf) - 1] = '\0';
157 if (strcmp(in, "/")) {
158 if (*((const char *)in) != '/')
160 strncat(buf, in, sizeof(buf) - strlen(buf) - 1);
161 } else /* default file to serve */
162 strcat(buf, "/test.html");
163 buf[sizeof(buf) - 1] = '\0';
165 /* refuse to serve files we don't understand */
166 mimetype = get_mimetype(buf);
168 lwsl_err("Unknown mimetype for %s\n", buf);
169 lws_return_http_status(wsi,
170 HTTP_STATUS_UNSUPPORTED_MEDIA_TYPE, NULL);
174 /* demonstrates how to set a cookie on / */
176 other_headers = leaf_path;
177 p = (unsigned char *)leaf_path;
178 if (!strcmp((const char *)in, "/") &&
179 !lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_COOKIE)) {
180 /* this isn't very unguessable but it'll do for us */
181 gettimeofday(&tv, NULL);
182 n = sprintf(b64, "test=LWS_%u_%u_COOKIE;Max-Age=360000",
183 (unsigned int)tv.tv_sec,
184 (unsigned int)tv.tv_usec);
186 if (lws_add_http_header_by_name(wsi,
187 (unsigned char *)"set-cookie:",
188 (unsigned char *)b64, n, &p,
189 (unsigned char *)leaf_path + sizeof(leaf_path)))
192 if (lws_is_ssl(wsi) && lws_add_http_header_by_name(wsi,
194 "Strict-Transport-Security:",
196 "max-age=15768000 ; "
197 "includeSubDomains", 36, &p,
198 (unsigned char *)leaf_path +
201 n = (char *)p - leaf_path;
203 n = lws_serve_http_file(wsi, buf, mimetype, other_headers, n);
204 if (n < 0 || ((n > 0) && lws_http_transaction_completed(wsi)))
205 return -1; /* error or can't reuse connection: close the socket */
208 * notice that the sending of the file completes asynchronously,
209 * we'll get a LWS_CALLBACK_HTTP_FILE_COMPLETION callback when
214 case LWS_CALLBACK_HTTP_BODY:
215 strncpy(buf, in, 20);
220 lwsl_notice("LWS_CALLBACK_HTTP_BODY: %s... len %d\n",
221 (const char *)buf, (int)len);
225 case LWS_CALLBACK_HTTP_BODY_COMPLETION:
226 lwsl_notice("LWS_CALLBACK_HTTP_BODY_COMPLETION\n");
227 /* the whole of the sent body arrived, close or reuse the connection */
228 lws_return_http_status(wsi, HTTP_STATUS_OK, NULL);
231 case LWS_CALLBACK_HTTP_FILE_COMPLETION:
234 case LWS_CALLBACK_HTTP_WRITEABLE:
235 // lwsl_notice("LWS_CALLBACK_HTTP_WRITEABLE\n");
238 if (pss->reason_bf & 1) {
239 if (lws_cgi_write_split_stdout_headers(wsi) < 0) {
240 lwsl_debug("lws_cgi_write_split_stdout_headers says close\n");
244 pss->reason_bf &= ~1;
250 #ifndef LWS_NO_CLIENT
251 if (pss->reason_bf & 2) {
252 char *px = buf + LWS_PRE;
253 int lenx = sizeof(buf) - LWS_PRE;
255 * our sink is writeable and our source has something
256 * to read. So read a lump of source material of
257 * suitable size to send or what's available, whichever
260 pss->reason_bf &= ~2;
261 wsi1 = lws_get_child(wsi);
264 if (lws_http_client_read(wsi1, &px, &lenx) < 0)
267 if (pss->client_finished)
274 #ifndef LWS_NO_CLIENT
276 case LWS_CALLBACK_ESTABLISHED_CLIENT_HTTP: {
277 char ctype[64], ctlen = 0;
278 lwsl_err("LWS_CALLBACK_ESTABLISHED_CLIENT_HTTP\n");
279 p = buffer + LWS_PRE;
280 end = p + sizeof(buffer) - LWS_PRE;
281 if (lws_add_http_header_status(lws_get_parent(wsi), 200, &p, end))
283 if (lws_add_http_header_by_token(lws_get_parent(wsi),
284 WSI_TOKEN_HTTP_SERVER,
285 (unsigned char *)"libwebsockets",
289 ctlen = lws_hdr_copy(wsi, ctype, sizeof(ctype), WSI_TOKEN_HTTP_CONTENT_TYPE);
291 if (lws_add_http_header_by_token(lws_get_parent(wsi),
292 WSI_TOKEN_HTTP_CONTENT_TYPE,
293 (unsigned char *)ctype, ctlen, &p, end))
297 if (lws_add_http_header_content_length(lws_get_parent(wsi),
301 if (lws_finalize_http_header(lws_get_parent(wsi), &p, end))
305 lwsl_info("%s\n", buffer + LWS_PRE);
307 n = lws_write(lws_get_parent(wsi), buffer + LWS_PRE,
308 p - (buffer + LWS_PRE),
309 LWS_WRITE_HTTP_HEADERS);
314 case LWS_CALLBACK_CLOSED_CLIENT_HTTP:
315 //lwsl_err("LWS_CALLBACK_CLOSED_CLIENT_HTTP\n");
318 case LWS_CALLBACK_RECEIVE_CLIENT_HTTP:
319 //lwsl_err("LWS_CALLBACK_RECEIVE_CLIENT_HTTP: wsi %p\n", wsi);
320 assert(lws_get_parent(wsi));
321 if (!lws_get_parent(wsi))
323 // lwsl_err("LWS_CALLBACK_RECEIVE_CLIENT_HTTP: wsi %p: sock: %d, parent_wsi: %p, parent_sock:%d, len %d\n",
324 // wsi, lws_get_socket_fd(wsi),
325 // lws_get_parent(wsi),
326 // lws_get_socket_fd(lws_get_parent(wsi)), len);
327 pss1 = lws_wsi_user(lws_get_parent(wsi));
328 pss1->reason_bf |= 2;
329 lws_callback_on_writable(lws_get_parent(wsi));
331 case LWS_CALLBACK_RECEIVE_CLIENT_HTTP_READ:
332 //lwsl_err("LWS_CALLBACK_RECEIVE_CLIENT_HTTP_READ len %d\n", len);
333 assert(lws_get_parent(wsi));
334 m = lws_write(lws_get_parent(wsi), (unsigned char *)in,
335 len, LWS_WRITE_HTTP);
339 case LWS_CALLBACK_COMPLETED_CLIENT_HTTP:
340 //lwsl_err("LWS_CALLBACK_COMPLETED_CLIENT_HTTP\n");
341 assert(lws_get_parent(wsi));
342 if (!lws_get_parent(wsi))
344 pss1 = lws_wsi_user(lws_get_parent(wsi));
345 pss1->client_finished = 1;
350 /* CGI IO events (POLLIN/OUT) appear here our demo user code policy is
352 * - POST data goes on subprocess stdin
353 * - subprocess stdout goes on http via writeable callback
354 * - subprocess stderr goes to the logs
356 case LWS_CALLBACK_CGI:
357 pss->args = *((struct lws_cgi_args *)in);
358 //lwsl_notice("LWS_CALLBACK_CGI: ch %d\n", pss->args.ch);
359 switch (pss->args.ch) { /* which of stdin/out/err ? */
361 /* TBD stdin rx flow control */
365 /* when writing to MASTER would not block */
366 lws_callback_on_writable(wsi);
369 n = read(lws_get_socket_fd(pss->args.stdwsi[LWS_STDERR]),
371 //lwsl_notice("stderr reads %d\n", n);
373 if (buf[n - 1] != '\n')
376 lwsl_notice("CGI-stderr: %s\n", buf);
382 case LWS_CALLBACK_CGI_TERMINATED:
383 //lwsl_notice("LWS_CALLBACK_CGI_TERMINATED\n");
384 /* because we sent on openended http, close the connection */
387 case LWS_CALLBACK_CGI_STDIN_DATA: /* POST body for stdin */
388 lwsl_notice("LWS_CALLBACK_CGI_STDIN_DATA\n");
389 pss->args = *((struct lws_cgi_args *)in);
390 pss->args.data[pss->args.len] = '\0';
391 //lwsl_err("(stdin fd = %d) %s\n", lws_get_socket_fd(pss->args.stdwsi[LWS_STDIN]), pss->args.data);
392 n = write(lws_get_socket_fd(pss->args.stdwsi[LWS_STDIN]),
393 pss->args.data, pss->args.len);
394 //lwsl_notice("LWS_CALLBACK_CGI_STDIN_DATA: write says %d", n);
395 if (n < pss->args.len)
396 lwsl_notice("LWS_CALLBACK_CGI_STDIN_DATA: sent %d only %d went",
402 * callbacks for managing the external poll() array appear in
403 * protocol 0 callback
406 case LWS_CALLBACK_LOCK_POLL:
407 test_server_lock(len);
410 case LWS_CALLBACK_UNLOCK_POLL:
411 test_server_unlock(len);
414 case LWS_CALLBACK_GET_THREAD_ID:
415 /* return pthread_getthreadid_np(); */
425 /* if we're on HTTP1.1 or 2.0, will keep the idle connection alive */
427 if (lws_http_transaction_completed(wsi))