fops: refactor around lws_fops_fd_t
[platform/upstream/libwebsockets.git] / lib / server.c
1 /*
2  * libwebsockets - small server side websockets and web server implementation
3  *
4  * Copyright (C) 2010-2016 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
23 #include "private-libwebsockets.h"
24
25 #if defined (LWS_WITH_ESP8266)
26 #undef memcpy
27 void *memcpy(void *dest, const void *src, size_t n)
28 {
29         return ets_memcpy(dest, src, n);
30 }
31 #endif
32
33 int
34 lws_context_init_server(struct lws_context_creation_info *info,
35                         struct lws_vhost *vhost)
36 {
37 #if LWS_POSIX
38         int n, opt = 1, limit = 1;
39 #endif
40         lws_sockfd_type sockfd;
41         struct lws_vhost *vh;
42         struct lws *wsi;
43         int m = 0;
44
45         (void)opt;
46         /* set up our external listening socket we serve on */
47
48         if (info->port == CONTEXT_PORT_NO_LISTEN || info->port == CONTEXT_PORT_NO_LISTEN_SERVER)
49                 return 0;
50
51         vh = vhost->context->vhost_list;
52         while (vh) {
53                 if (vh->listen_port == info->port) {
54                         if ((!info->iface && !vh->iface) ||
55                             (info->iface && vh->iface &&
56                             !strcmp(info->iface, vh->iface))) {
57                                 vhost->listen_port = info->port;
58                                 vhost->iface = info->iface;
59                                 lwsl_notice(" using listen skt from vhost %s\n",
60                                             vh->name);
61                                 return 0;
62                         }
63                 }
64                 vh = vh->vhost_next;
65         }
66
67 #if LWS_POSIX
68 #if defined(__linux__)
69         limit = vhost->context->count_threads;
70 #endif
71
72         for (m = 0; m < limit; m++) {
73 #ifdef LWS_USE_UNIX_SOCK
74         if (LWS_UNIX_SOCK_ENABLED(vhost))
75                 sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
76         else
77 #endif
78 #ifdef LWS_USE_IPV6
79         if (LWS_IPV6_ENABLED(vhost))
80                 sockfd = socket(AF_INET6, SOCK_STREAM, 0);
81         else
82 #endif
83                 sockfd = socket(AF_INET, SOCK_STREAM, 0);
84
85         if (sockfd == -1) {
86 #else
87 #if defined(LWS_WITH_ESP8266)
88         sockfd = esp8266_create_tcp_listen_socket(vhost);
89         if (!lws_sockfd_valid(sockfd)) {
90 #endif
91 #endif
92                 lwsl_err("ERROR opening socket\n");
93                 return 1;
94         }
95 #if LWS_POSIX && !defined(LWS_WITH_ESP32)
96         /*
97          * allow us to restart even if old sockets in TIME_WAIT
98          */
99         if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR,
100                        (const void *)&opt, sizeof(opt)) < 0) {
101                 lwsl_err("reuseaddr failed\n");
102                 compatible_close(sockfd);
103                 return 1;
104         }
105
106 #if defined(LWS_USE_IPV6) && defined(IPV6_V6ONLY)
107         if (LWS_IPV6_ENABLED(vhost)) {
108                 if (vhost->options & LWS_SERVER_OPTION_IPV6_V6ONLY_MODIFY) {
109                         int value = (vhost->options & LWS_SERVER_OPTION_IPV6_V6ONLY_VALUE) ? 1 : 0;
110                         if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_V6ONLY,
111                                         (const void*)&value, sizeof(value)) < 0) {
112                                 compatible_close(sockfd);
113                                 return 1;
114                         }
115                 }
116         }
117 #endif
118
119 #if defined(__linux__) && defined(SO_REUSEPORT) && LWS_MAX_SMP > 1
120         if (vhost->context->count_threads > 1)
121                 if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEPORT,
122                                 (const void *)&opt, sizeof(opt)) < 0) {
123                         compatible_close(sockfd);
124                         return 1;
125                 }
126 #endif
127 #endif
128         lws_plat_set_socket_options(vhost, sockfd);
129
130 #if LWS_POSIX
131         n = lws_socket_bind(vhost, sockfd, info->port, info->iface);
132         if (n < 0)
133                 goto bail;
134         info->port = n;
135 #endif
136         vhost->listen_port = info->port;
137         vhost->iface = info->iface;
138
139         wsi = lws_zalloc(sizeof(struct lws));
140         if (wsi == NULL) {
141                 lwsl_err("Out of mem\n");
142                 goto bail;
143         }
144         wsi->context = vhost->context;
145         wsi->sock = sockfd;
146         wsi->mode = LWSCM_SERVER_LISTENER;
147         wsi->protocol = vhost->protocols;
148         wsi->tsi = m;
149         wsi->vhost = vhost;
150         wsi->listener = 1;
151
152 #ifdef LWS_USE_LIBUV
153         if (LWS_LIBUV_ENABLED(vhost->context))
154                 lws_uv_initvhost(vhost, wsi);
155 #endif
156
157         if (insert_wsi_socket_into_fds(vhost->context, wsi))
158                 goto bail;
159
160         vhost->context->count_wsi_allocated++;
161         vhost->lserv_wsi = wsi;
162
163 #if LWS_POSIX
164         n = listen(wsi->sock, LWS_SOMAXCONN);
165         if (n < 0) {
166                 lwsl_err("listen failed with error %d\n", LWS_ERRNO);
167                 vhost->lserv_wsi = NULL;
168                 vhost->context->count_wsi_allocated--;
169                 remove_wsi_socket_from_fds(wsi);
170                 goto bail;
171         }
172         } /* for each thread able to independently listen */
173 #else
174 #if defined(LWS_WITH_ESP8266)
175         esp8266_tcp_stream_bind(wsi->sock, info->port, wsi);
176 #endif
177 #endif
178         if (!lws_check_opt(info->options, LWS_SERVER_OPTION_EXPLICIT_VHOSTS)) {
179 #ifdef LWS_USE_UNIX_SOCK
180                 if (LWS_UNIX_SOCK_ENABLED(vhost))
181                         lwsl_notice(" Listening on \"%s\"\n", info->iface);
182                 else
183 #endif
184                         lwsl_notice(" Listening on port %d\n", info->port);
185         }
186
187         return 0;
188
189 bail:
190         compatible_close(sockfd);
191
192         return 1;
193 }
194
195 #if defined(LWS_WITH_ESP8266)
196 #undef strchr
197 #define strchr ets_strchr
198 #endif
199
200 struct lws_vhost *
201 lws_select_vhost(struct lws_context *context, int port, const char *servername)
202 {
203         struct lws_vhost *vhost = context->vhost_list;
204         const char *p;
205         int n, m, colon;
206
207         n = strlen(servername);
208         colon = n;
209         p = strchr(servername, ':');
210         if (p)
211                 colon = p - servername;
212
213         /* first try exact matches */
214
215         while (vhost) {
216                 if (port == vhost->listen_port &&
217                     !strncmp(vhost->name, servername, colon)) {
218                         lwsl_info("SNI: Found: %s\n", servername);
219                         return vhost;
220                 }
221                 vhost = vhost->vhost_next;
222         }
223
224         /*
225          * if no exact matches, try matching *.vhost-name
226          * unintentional matches are possible but resolve to x.com for *.x.com
227          * which is reasonable.  If exact match exists we already chose it and
228          * never reach here.  SSL will still fail it if the cert doesn't allow
229          * *.x.com.
230          */
231
232         vhost = context->vhost_list;
233         while (vhost) {
234                 m = strlen(vhost->name);
235                 if (port == vhost->listen_port &&
236                     m <= (colon - 2) &&
237                     servername[colon - m - 1] == '.' &&
238                     !strncmp(vhost->name, servername + colon - m, m)) {
239                         lwsl_info("SNI: Found %s on wildcard: %s\n",
240                                     servername, vhost->name);
241                         return vhost;
242                 }
243                 vhost = vhost->vhost_next;
244         }
245
246         return NULL;
247 }
248
249 LWS_VISIBLE LWS_EXTERN const char *
250 lws_get_mimetype(const char *file, const struct lws_http_mount *m)
251 {
252         int n = strlen(file);
253         const struct lws_protocol_vhost_options *pvo = NULL;
254
255         if (m)
256                 pvo = m->extra_mimetypes;
257
258         if (n < 5)
259                 return NULL;
260
261         if (!strcmp(&file[n - 4], ".ico"))
262                 return "image/x-icon";
263
264         if (!strcmp(&file[n - 4], ".gif"))
265                 return "image/gif";
266
267         if (!strcmp(&file[n - 3], ".js"))
268                 return "text/javascript";
269
270         if (!strcmp(&file[n - 4], ".png"))
271                 return "image/png";
272
273         if (!strcmp(&file[n - 4], ".jpg"))
274                 return "image/jpeg";
275
276         if (!strcmp(&file[n - 3], ".gz"))
277                 return "application/gzip";
278
279         if (!strcmp(&file[n - 4], ".JPG"))
280                 return "image/jpeg";
281
282         if (!strcmp(&file[n - 5], ".html"))
283                 return "text/html";
284
285         if (!strcmp(&file[n - 4], ".css"))
286                 return "text/css";
287
288         if (!strcmp(&file[n - 4], ".txt"))
289                 return "text/plain";
290
291         if (!strcmp(&file[n - 4], ".svg"))
292                 return "image/svg+xml";
293
294         if (!strcmp(&file[n - 4], ".ttf"))
295                 return "application/x-font-ttf";
296
297         if (!strcmp(&file[n - 5], ".woff"))
298                 return "application/font-woff";
299
300         if (!strcmp(&file[n - 4], ".xml"))
301                 return "application/xml";
302
303         while (pvo) {
304                 if (pvo->name[0] == '*') /* ie, match anything */
305                         return pvo->value;
306
307                 if (!strcmp(&file[n - strlen(pvo->name)], pvo->name))
308                         return pvo->value;
309
310                 pvo = pvo->next;
311         }
312
313         return NULL;
314 }
315
316 static int
317 lws_http_serve(struct lws *wsi, char *uri, const char *origin,
318                const struct lws_http_mount *m)
319 {
320         const struct lws_protocol_vhost_options *pvo = m->interpret;
321         struct lws_process_html_args args;
322         const char *mimetype;
323 #if !defined(_WIN32_WCE) && !defined(LWS_WITH_ESP8266) && !defined(LWS_WITH_ESP32)
324         struct stat st;
325         int spin = 0;
326 #endif
327         char path[256], sym[512];
328         unsigned char *p = (unsigned char *)sym + 32 + LWS_PRE, *start = p;
329         unsigned char *end = p + sizeof(sym) - 32 - LWS_PRE;
330 #if !defined(WIN32) && LWS_POSIX
331         size_t len;
332 #endif
333         int n;
334
335         lws_snprintf(path, sizeof(path) - 1, "%s/%s", origin, uri);
336
337 #if !defined(_WIN32_WCE) && !defined(LWS_WITH_ESP8266) && !defined(LWS_WITH_ESP32)
338         do {
339                 spin++;
340
341                 if (stat(path, &st)) {
342                         lwsl_info("unable to stat %s\n", path);
343                         goto bail;
344                 }
345
346                 lwsl_debug(" %s mode %d\n", path, S_IFMT & st.st_mode);
347 #if !defined(WIN32) && LWS_POSIX
348                 if ((S_IFMT & st.st_mode) == S_IFLNK) {
349                         len = readlink(path, sym, sizeof(sym) - 1);
350                         if (len) {
351                                 lwsl_err("Failed to read link %s\n", path);
352                                 goto bail;
353                         }
354                         sym[len] = '\0';
355                         lwsl_debug("symlink %s -> %s\n", path, sym);
356                         lws_snprintf(path, sizeof(path) - 1, "%s", sym);
357                 }
358 #endif
359                 if ((S_IFMT & st.st_mode) == S_IFDIR) {
360                         lwsl_debug("default filename append to dir\n");
361                         lws_snprintf(path, sizeof(path) - 1, "%s/%s/index.html",
362                                  origin, uri);
363                 }
364
365         } while ((S_IFMT & st.st_mode) != S_IFREG && spin < 5);
366
367         if (spin == 5)
368                 lwsl_err("symlink loop %s \n", path);
369
370         n = sprintf(sym, "%08lX%08lX", (unsigned long)st.st_size,
371                                    (unsigned long)st.st_mtime);
372
373         /* disable ranges if IF_RANGE token invalid */
374
375         if (lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_IF_RANGE))
376                 if (strcmp(sym, lws_hdr_simple_ptr(wsi, WSI_TOKEN_HTTP_IF_RANGE)))
377                         /* differs - defeat Range: */
378                         wsi->u.http.ah->frag_index[WSI_TOKEN_HTTP_RANGE] = 0;
379
380         if (lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_IF_NONE_MATCH)) {
381                 /*
382                  * he thinks he has some version of it already,
383                  * check if the tag matches
384                  */
385                 if (!strcmp(sym, lws_hdr_simple_ptr(wsi, WSI_TOKEN_HTTP_IF_NONE_MATCH))) {
386
387                         lwsl_debug("%s: ETAG match %s %s\n", __func__,
388                                    uri, origin);
389
390                         /* we don't need to send the payload */
391                         if (lws_add_http_header_status(wsi, 304, &p, end))
392                                 return -1;
393
394                         if (lws_add_http_header_by_token(wsi,
395                                         WSI_TOKEN_HTTP_ETAG,
396                                         (unsigned char *)sym, n, &p, end))
397                                 return -1;
398
399                         if (lws_finalize_http_header(wsi, &p, end))
400                                 return -1;
401
402                         n = lws_write(wsi, start, p - start,
403                                       LWS_WRITE_HTTP_HEADERS);
404                         if (n != (p - start)) {
405                                 lwsl_err("_write returned %d from %ld\n", n,
406                                          (long)(p - start));
407                                 return -1;
408                         }
409
410                         return lws_http_transaction_completed(wsi);
411                 }
412         }
413
414         if (lws_add_http_header_by_token(wsi, WSI_TOKEN_HTTP_ETAG,
415                         (unsigned char *)sym, n, &p, end))
416                 return -1;
417 #endif
418
419         mimetype = lws_get_mimetype(path, m);
420         if (!mimetype) {
421                 lwsl_err("unknown mimetype for %s\n", path);
422                goto bail;
423         }
424         if (!mimetype[0])
425                 lwsl_debug("sending no mimetype for %s\n", path);
426
427         wsi->sending_chunked = 0;
428
429         /*
430          * check if this is in the list of file suffixes to be interpreted by
431          * a protocol
432          */
433         while (pvo) {
434                 n = strlen(path);
435                 if (n > (int)strlen(pvo->name) &&
436                     !strcmp(&path[n - strlen(pvo->name)], pvo->name)) {
437                         wsi->sending_chunked = 1;
438                         wsi->protocol_interpret_idx = (char)(long)pvo->value;
439                         lwsl_info("want %s interpreted by %s\n", path,
440                                     wsi->vhost->protocols[(int)(long)(pvo->value)].name);
441                         wsi->protocol = &wsi->vhost->protocols[(int)(long)(pvo->value)];
442                         if (lws_ensure_user_space(wsi))
443                                 return -1;
444                         break;
445                 }
446                 pvo = pvo->next;
447         }
448
449         if (m->protocol) {
450                 const struct lws_protocols *pp = lws_vhost_name_to_protocol(
451                                                         wsi->vhost, m->protocol);
452
453                 if (lws_bind_protocol(wsi, pp))
454                         return 1;
455                 args.p = (char *)p;
456                 args.max_len = end - p;
457                 if (pp->callback(wsi, LWS_CALLBACK_ADD_HEADERS,
458                                           wsi->user_space, &args, 0))
459                         return -1;
460                 p = (unsigned char *)args.p;
461         }
462
463         n = lws_serve_http_file(wsi, path, mimetype, (char *)start, p - start);
464
465         if (n < 0 || ((n > 0) && lws_http_transaction_completed(wsi)))
466                 return -1; /* error or can't reuse connection: close the socket */
467
468         return 0;
469 bail:
470
471         return -1;
472 }
473
474 const struct lws_http_mount *
475 lws_find_mount(struct lws *wsi, const char *uri_ptr, int uri_len)
476 {
477         const struct lws_http_mount *hm, *hit = NULL;
478         int best = 0;
479
480         hm = wsi->vhost->mount_list;
481         while (hm) {
482                 if (uri_len >= hm->mountpoint_len &&
483                     !strncmp(uri_ptr, hm->mountpoint, hm->mountpoint_len) &&
484                     (uri_ptr[hm->mountpoint_len] == '\0' ||
485                      uri_ptr[hm->mountpoint_len] == '/' ||
486                      hm->mountpoint_len == 1)
487                     ) {
488                         if (hm->origin_protocol == LWSMPRO_CALLBACK ||
489                             ((hm->origin_protocol == LWSMPRO_CGI ||
490                              lws_hdr_total_length(wsi, WSI_TOKEN_GET_URI) ||
491                              hm->protocol) &&
492                             hm->mountpoint_len > best)) {
493                                 best = hm->mountpoint_len;
494                                 hit = hm;
495                         }
496                 }
497                 hm = hm->mount_next;
498         }
499
500         return hit;
501 }
502
503 #if LWS_POSIX
504
505 static int
506 lws_find_string_in_file(const char *filename, const char *string, int stringlen)
507 {
508         char buf[128];
509         int fd, match = 0, pos = 0, n = 0, hit = 0;
510
511         fd = open(filename, O_RDONLY);
512         if (fd < 0) {
513                 lwsl_err("can't open auth file: %s\n", filename);
514                 return 1;
515         }
516
517         while (1) {
518                 if (pos == n) {
519                         n = read(fd, buf, sizeof(buf));
520                         if (n <= 0) {
521                                 if (match == stringlen)
522                                         hit = 1;
523                                 break;
524                         }
525                         pos = 0;
526                 }
527
528                 if (match == stringlen) {
529                         if (buf[pos] == '\r' || buf[pos] == '\n') {
530                                 hit = 1;
531                                 break;
532                         }
533                         match = 0;
534                 }
535
536                 if (buf[pos] == string[match])
537                         match++;
538                 else
539                         match = 0;
540
541                 pos++;
542         }
543
544         close(fd);
545
546         return hit;
547 }
548
549 static int
550 lws_unauthorised_basic_auth(struct lws *wsi)
551 {
552         struct lws_context_per_thread *pt = &wsi->context->pt[(int)wsi->tsi];
553         unsigned char *start = pt->serv_buf + LWS_PRE,
554                       *p = start, *end = p + 512;
555         char buf[64];
556         int n;
557
558         /* no auth... tell him it is required */
559
560         if (lws_add_http_header_status(wsi, HTTP_STATUS_UNAUTHORIZED, &p, end))
561                 return -1;
562
563         n = lws_snprintf(buf, sizeof(buf), "Basic realm=\"lwsws\"");
564         if (lws_add_http_header_by_token(wsi,
565                         WSI_TOKEN_HTTP_WWW_AUTHENTICATE,
566                         (unsigned char *)buf, n, &p, end))
567                 return -1;
568
569         if (lws_finalize_http_header(wsi, &p, end))
570                 return -1;
571
572         n = lws_write(wsi, start, p - start, LWS_WRITE_HTTP_HEADERS);
573         if (n < 0)
574                 return -1;
575
576         return lws_http_transaction_completed(wsi);
577
578 }
579
580 #endif
581
582 int
583 lws_http_action(struct lws *wsi)
584 {
585         struct lws_context_per_thread *pt = &wsi->context->pt[(int)wsi->tsi];
586         enum http_connection_type connection_type;
587         enum http_version request_version;
588         char content_length_str[32];
589         struct lws_process_html_args args;
590         const struct lws_http_mount *hit = NULL;
591         unsigned int n, count = 0;
592         char http_version_str[10];
593         char http_conn_str[20];
594         int http_version_len;
595         char *uri_ptr = NULL, *s;
596         int uri_len = 0;
597         int meth = -1;
598
599         static const unsigned char methods[] = {
600                 WSI_TOKEN_GET_URI,
601                 WSI_TOKEN_POST_URI,
602                 WSI_TOKEN_OPTIONS_URI,
603                 WSI_TOKEN_PUT_URI,
604                 WSI_TOKEN_PATCH_URI,
605                 WSI_TOKEN_DELETE_URI,
606                 WSI_TOKEN_CONNECT,
607 #ifdef LWS_USE_HTTP2
608                 WSI_TOKEN_HTTP_COLON_PATH,
609 #endif
610         };
611 #if defined(_DEBUG) || defined(LWS_WITH_ACCESS_LOG)
612         static const char * const method_names[] = {
613                 "GET", "POST", "OPTIONS", "PUT", "PATCH", "DELETE", "CONNECT",
614 #ifdef LWS_USE_HTTP2
615                 ":path",
616 #endif
617         };
618 #endif
619         static const char * const oprot[] = {
620                 "http://", "https://"
621         };
622
623         /* it's not websocket.... shall we accept it as http? */
624
625         for (n = 0; n < ARRAY_SIZE(methods); n++)
626                 if (lws_hdr_total_length(wsi, methods[n]))
627                         count++;
628         if (!count) {
629                 lwsl_warn("Missing URI in HTTP request\n");
630                 goto bail_nuke_ah;
631         }
632
633         if (count != 1) {
634                 lwsl_warn("multiple methods?\n");
635                 goto bail_nuke_ah;
636         }
637
638         if (lws_ensure_user_space(wsi))
639                 goto bail_nuke_ah;
640
641         for (n = 0; n < ARRAY_SIZE(methods); n++)
642                 if (lws_hdr_total_length(wsi, methods[n])) {
643                         uri_ptr = lws_hdr_simple_ptr(wsi, methods[n]);
644                         uri_len = lws_hdr_total_length(wsi, methods[n]);
645                         lwsl_info("Method: %s request for '%s'\n",
646                                         method_names[n], uri_ptr);
647                         meth = n;
648                         break;
649                 }
650
651         (void)meth;
652
653         /* we insist on absolute paths */
654
655         if (uri_ptr[0] != '/') {
656                 lws_return_http_status(wsi, HTTP_STATUS_FORBIDDEN, NULL);
657
658                 goto bail_nuke_ah;
659         }
660
661         /* HTTP header had a content length? */
662
663         wsi->u.http.content_length = 0;
664         if (lws_hdr_total_length(wsi, WSI_TOKEN_POST_URI) ||
665                 lws_hdr_total_length(wsi, WSI_TOKEN_PATCH_URI) ||
666                 lws_hdr_total_length(wsi, WSI_TOKEN_PUT_URI))
667                 wsi->u.http.content_length = 100 * 1024 * 1024;
668
669         if (lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_CONTENT_LENGTH)) {
670                 lws_hdr_copy(wsi, content_length_str,
671                              sizeof(content_length_str) - 1,
672                              WSI_TOKEN_HTTP_CONTENT_LENGTH);
673                 wsi->u.http.content_length = atoi(content_length_str);
674         }
675
676         if (wsi->http2_substream) {
677                 wsi->u.http.request_version = HTTP_VERSION_2;
678         } else {
679                 /* http_version? Default to 1.0, override with token: */
680                 request_version = HTTP_VERSION_1_0;
681
682                 /* Works for single digit HTTP versions. : */
683                 http_version_len = lws_hdr_total_length(wsi, WSI_TOKEN_HTTP);
684                 if (http_version_len > 7) {
685                         lws_hdr_copy(wsi, http_version_str,
686                                         sizeof(http_version_str) - 1, WSI_TOKEN_HTTP);
687                         if (http_version_str[5] == '1' && http_version_str[7] == '1')
688                                 request_version = HTTP_VERSION_1_1;
689                 }
690                 wsi->u.http.request_version = request_version;
691
692                 /* HTTP/1.1 defaults to "keep-alive", 1.0 to "close" */
693                 if (request_version == HTTP_VERSION_1_1)
694                         connection_type = HTTP_CONNECTION_KEEP_ALIVE;
695                 else
696                         connection_type = HTTP_CONNECTION_CLOSE;
697
698                 /* Override default if http "Connection:" header: */
699                 if (lws_hdr_total_length(wsi, WSI_TOKEN_CONNECTION)) {
700                         lws_hdr_copy(wsi, http_conn_str, sizeof(http_conn_str) - 1,
701                                      WSI_TOKEN_CONNECTION);
702                         http_conn_str[sizeof(http_conn_str) - 1] = '\0';
703                         if (!strcasecmp(http_conn_str, "keep-alive"))
704                                 connection_type = HTTP_CONNECTION_KEEP_ALIVE;
705                         else
706                                 if (!strcasecmp(http_conn_str, "close"))
707                                         connection_type = HTTP_CONNECTION_CLOSE;
708                 }
709                 wsi->u.http.connection_type = connection_type;
710         }
711
712         n = wsi->protocol->callback(wsi, LWS_CALLBACK_FILTER_HTTP_CONNECTION,
713                                     wsi->user_space, uri_ptr, uri_len);
714         if (n) {
715                 lwsl_info("LWS_CALLBACK_HTTP closing\n");
716
717                 return 1;
718         }
719         /*
720          * if there is content supposed to be coming,
721          * put a timeout on it having arrived
722          */
723         lws_set_timeout(wsi, PENDING_TIMEOUT_HTTP_CONTENT,
724                         wsi->context->timeout_secs);
725 #ifdef LWS_OPENSSL_SUPPORT
726         if (wsi->redirect_to_https) {
727                 /*
728                  * we accepted http:// only so we could redirect to
729                  * https://, so issue the redirect.  Create the redirection
730                  * URI from the host: header and ignore the path part
731                  */
732                 unsigned char *start = pt->serv_buf + LWS_PRE, *p = start,
733                               *end = p + 512;
734
735                 if (!lws_hdr_total_length(wsi, WSI_TOKEN_HOST))
736                         goto bail_nuke_ah;
737
738                 n = sprintf((char *)end, "https://%s/",
739                             lws_hdr_simple_ptr(wsi, WSI_TOKEN_HOST));
740
741                 n = lws_http_redirect(wsi, HTTP_STATUS_MOVED_PERMANENTLY,
742                                       end, n, &p, end);
743                 if ((int)n < 0)
744                         goto bail_nuke_ah;
745
746                 return lws_http_transaction_completed(wsi);
747         }
748 #endif
749
750 #ifdef LWS_WITH_ACCESS_LOG
751         /*
752          * Produce Apache-compatible log string for wsi, like this:
753          *
754          * 2.31.234.19 - - [27/Mar/2016:03:22:44 +0800]
755          * "GET /aep-screen.png HTTP/1.1"
756          * 200 152987 "https://libwebsockets.org/index.html"
757          * "Mozilla/5.0 (Macint... Chrome/49.0.2623.87 Safari/537.36"
758          *
759          */
760         {
761                 static const char * const hver[] = {
762                         "http/1.0", "http/1.1", "http/2"
763                 };
764 #ifdef LWS_USE_IPV6
765                 char ads[INET6_ADDRSTRLEN];
766 #else
767                 char ads[INET_ADDRSTRLEN];
768 #endif
769                 char da[64];
770                 const char *pa, *me;
771                 struct tm *tmp;
772                 time_t t = time(NULL);
773                 int l = 256;
774
775                 if (wsi->access_log_pending)
776                         lws_access_log(wsi);
777
778                 wsi->access_log.header_log = lws_malloc(l);
779                 if (wsi->access_log.header_log) {
780
781                         tmp = localtime(&t);
782                         if (tmp)
783                                 strftime(da, sizeof(da), "%d/%b/%Y:%H:%M:%S %z", tmp);
784                         else
785                                 strcpy(da, "01/Jan/1970:00:00:00 +0000");
786
787                         pa = lws_get_peer_simple(wsi, ads, sizeof(ads));
788                         if (!pa)
789                                 pa = "(unknown)";
790
791                         if (meth >= 0)
792                                 me = method_names[meth];
793                         else
794                                 me = "unknown";
795
796                         lws_snprintf(wsi->access_log.header_log, l,
797                                  "%s - - [%s] \"%s %s %s\"",
798                                  pa, da, me, uri_ptr,
799                                  hver[wsi->u.http.request_version]);
800
801                         l = lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_USER_AGENT);
802                         if (l) {
803                                 wsi->access_log.user_agent = lws_malloc(l + 2);
804                                 if (wsi->access_log.user_agent)
805                                         lws_hdr_copy(wsi, wsi->access_log.user_agent,
806                                                         l + 1, WSI_TOKEN_HTTP_USER_AGENT);
807                                 else
808                                         lwsl_err("OOM getting user agent\n");
809                         }
810                         wsi->access_log_pending = 1;
811                 }
812         }
813 #endif
814
815         /* can we serve it from the mount list? */
816
817         hit = lws_find_mount(wsi, uri_ptr, uri_len);
818         if (!hit) {
819                 /* deferred cleanup and reset to protocols[0] */
820
821                 lwsl_info("no hit\n");
822
823                 if (lws_bind_protocol(wsi, &wsi->vhost->protocols[0]))
824                         return 1;
825
826                 n = wsi->protocol->callback(wsi, LWS_CALLBACK_HTTP,
827                                     wsi->user_space, uri_ptr, uri_len);
828
829                 goto after;
830         }
831
832         s = uri_ptr + hit->mountpoint_len;
833
834         /*
835          * if we have a mountpoint like https://xxx.com/yyy
836          * there is an implied / at the end for our purposes since
837          * we can only mount on a "directory".
838          *
839          * But if we just go with that, the browser cannot understand
840          * that he is actually looking down one "directory level", so
841          * even though we give him /yyy/abc.html he acts like the
842          * current directory level is /.  So relative urls like "x.png"
843          * wrongly look outside the mountpoint.
844          *
845          * Therefore if we didn't come in on a url with an explicit
846          * / at the end, we must redirect to add it so the browser
847          * understands he is one "directory level" down.
848          */
849         if ((hit->mountpoint_len > 1 ||
850              (hit->origin_protocol == LWSMPRO_REDIR_HTTP ||
851               hit->origin_protocol == LWSMPRO_REDIR_HTTPS)) &&
852             (*s != '/' ||
853              (hit->origin_protocol == LWSMPRO_REDIR_HTTP ||
854               hit->origin_protocol == LWSMPRO_REDIR_HTTPS)) &&
855             (hit->origin_protocol != LWSMPRO_CGI &&
856              hit->origin_protocol != LWSMPRO_CALLBACK //&&
857              //hit->protocol == NULL
858              )) {
859                 unsigned char *start = pt->serv_buf + LWS_PRE,
860                               *p = start, *end = p + 512;
861
862                 lwsl_debug("Doing 301 '%s' org %s\n", s, hit->origin);
863
864                 if (!lws_hdr_total_length(wsi, WSI_TOKEN_HOST))
865                         goto bail_nuke_ah;
866
867                 /* > at start indicates deal with by redirect */
868                 if (hit->origin_protocol == LWSMPRO_REDIR_HTTP ||
869                     hit->origin_protocol == LWSMPRO_REDIR_HTTPS)
870                         n = lws_snprintf((char *)end, 256, "%s%s",
871                                     oprot[hit->origin_protocol & 1],
872                                     hit->origin);
873                 else
874                         n = lws_snprintf((char *)end, 256,
875                             "%s%s%s/", oprot[lws_is_ssl(wsi)],
876                             lws_hdr_simple_ptr(wsi, WSI_TOKEN_HOST),
877                             uri_ptr);
878
879                 n = lws_http_redirect(wsi, HTTP_STATUS_MOVED_PERMANENTLY,
880                                       end, n, &p, end);
881                 if ((int)n < 0)
882                         goto bail_nuke_ah;
883
884                 return lws_http_transaction_completed(wsi);
885         }
886
887 #if LWS_POSIX
888         /* basic auth? */
889
890         if (hit->basic_auth_login_file) {
891                 char b64[160], plain[(sizeof(b64) * 3) / 4];
892                 int m;
893
894                 /* Did he send auth? */
895                 if (!lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_AUTHORIZATION))
896                         return lws_unauthorised_basic_auth(wsi);
897
898                 n = HTTP_STATUS_FORBIDDEN;
899
900                 m = lws_hdr_copy(wsi, b64, sizeof(b64), WSI_TOKEN_HTTP_AUTHORIZATION);
901                 if (m < 7) {
902                         lwsl_err("b64 auth too long\n");
903                         goto transaction_result_n;
904                 }
905
906                 b64[5] = '\0';
907                 if (strcasecmp(b64, "Basic")) {
908                         lwsl_err("auth missing basic: %s\n", b64);
909                         goto transaction_result_n;
910                 }
911
912                 /* It'll be like Authorization: Basic QWxhZGRpbjpPcGVuU2VzYW1l */
913
914                 m = lws_b64_decode_string(b64 + 6, plain, sizeof(plain));
915                 if (m < 0) {
916                         lwsl_err("plain auth too long\n");
917                         goto transaction_result_n;
918                 }
919
920 //              lwsl_notice(plain);
921
922                 if (!lws_find_string_in_file(hit->basic_auth_login_file, plain, m)) {
923                         lwsl_err("basic auth lookup failed\n");
924                         return lws_unauthorised_basic_auth(wsi);
925                 }
926
927                 lwsl_notice("basic auth accepted\n");
928
929                 /* accept the auth */
930         }
931 #endif
932
933         /*
934          * A particular protocol callback is mounted here?
935          *
936          * For the duration of this http transaction, bind us to the
937          * associated protocol
938          */
939         if (hit->origin_protocol == LWSMPRO_CALLBACK || hit->protocol) {
940                 const struct lws_protocols *pp;
941                 const char *name = hit->origin;
942                 if (hit->protocol)
943                         name = hit->protocol;
944
945                 pp = lws_vhost_name_to_protocol(wsi->vhost, name);
946                 if (!pp) {
947                         n = -1;
948                         lwsl_err("Unable to find plugin '%s'\n",
949                                  hit->origin);
950                         return 1;
951                 }
952
953                 if (lws_bind_protocol(wsi, pp))
954                         return 1;
955
956                 args.p = uri_ptr;
957                 args.len = uri_len;
958                 args.max_len = hit->auth_mask;
959                 args.final = 0; /* used to signal callback dealt with it */
960
961                 n = wsi->protocol->callback(wsi, LWS_CALLBACK_CHECK_ACCESS_RIGHTS,
962                                             wsi->user_space, &args, 0);
963                 if (n) {
964                         lws_return_http_status(wsi, HTTP_STATUS_UNAUTHORIZED,
965                                                NULL);
966                         goto bail_nuke_ah;
967                 }
968                 if (args.final) /* callback completely handled it well */
969                         return 0;
970
971                 if (hit->cgienv && wsi->protocol->callback(wsi,
972                                 LWS_CALLBACK_HTTP_PMO,
973                                 wsi->user_space, (void *)hit->cgienv, 0))
974                         return 1;
975
976                 if (lws_hdr_total_length(wsi, WSI_TOKEN_POST_URI)) {
977                         n = wsi->protocol->callback(wsi, LWS_CALLBACK_HTTP,
978                                             wsi->user_space,
979                                             uri_ptr + hit->mountpoint_len,
980                                             uri_len - hit->mountpoint_len);
981                         goto after;
982                 }
983         }
984
985 #ifdef LWS_WITH_CGI
986         /* did we hit something with a cgi:// origin? */
987         if (hit->origin_protocol == LWSMPRO_CGI) {
988                 const char *cmd[] = {
989                         NULL, /* replace with cgi path */
990                         NULL
991                 };
992                 unsigned char *p, *end, buffer[1024];
993
994                 lwsl_debug("%s: cgi\n", __func__);
995                 cmd[0] = hit->origin;
996
997                 n = 5;
998                 if (hit->cgi_timeout)
999                         n = hit->cgi_timeout;
1000
1001                 n = lws_cgi(wsi, cmd, hit->mountpoint_len, n,
1002                             hit->cgienv);
1003                 if (n) {
1004                         lwsl_err("%s: cgi failed\n", __func__);
1005                         return -1;
1006                 }
1007                 p = buffer + LWS_PRE;
1008                 end = p + sizeof(buffer) - LWS_PRE;
1009
1010                 if (lws_add_http_header_status(wsi, 200, &p, end))
1011                         return 1;
1012                 if (lws_add_http_header_by_token(wsi, WSI_TOKEN_CONNECTION,
1013                                 (unsigned char *)"close", 5, &p, end))
1014                         return 1;
1015                 n = lws_write(wsi, buffer + LWS_PRE,
1016                               p - (buffer + LWS_PRE),
1017                               LWS_WRITE_HTTP_HEADERS);
1018
1019                 goto deal_body;
1020         }
1021 #endif
1022
1023         n = strlen(s);
1024         if (s[0] == '\0' || (n == 1 && s[n - 1] == '/'))
1025                 s = (char *)hit->def;
1026         if (!s)
1027                 s = "index.html";
1028
1029         wsi->cache_secs = hit->cache_max_age;
1030         wsi->cache_reuse = hit->cache_reusable;
1031         wsi->cache_revalidate = hit->cache_revalidate;
1032         wsi->cache_intermediaries = hit->cache_intermediaries;
1033
1034         n = lws_http_serve(wsi, s, hit->origin, hit);
1035         if (n) {
1036                 /*
1037                  *      lws_return_http_status(wsi, HTTP_STATUS_NOT_FOUND, NULL);
1038                  */
1039                 if (hit->protocol) {
1040                         const struct lws_protocols *pp = lws_vhost_name_to_protocol(
1041                                         wsi->vhost, hit->protocol);
1042
1043                         if (lws_bind_protocol(wsi, pp))
1044                                 return 1;
1045
1046                         n = pp->callback(wsi, LWS_CALLBACK_HTTP,
1047                                          wsi->user_space,
1048                                          uri_ptr + hit->mountpoint_len,
1049                                          uri_len - hit->mountpoint_len);
1050                 } else
1051                         n = wsi->protocol->callback(wsi, LWS_CALLBACK_HTTP,
1052                                     wsi->user_space, uri_ptr, uri_len);
1053         }
1054
1055 after:
1056         if (n) {
1057                 lwsl_info("LWS_CALLBACK_HTTP closing\n");
1058
1059                 return 1;
1060         }
1061
1062 #ifdef LWS_WITH_CGI
1063 deal_body:
1064 #endif
1065         /*
1066          * If we're not issuing a file, check for content_length or
1067          * HTTP keep-alive. No keep-alive header allocation for
1068          * ISSUING_FILE, as this uses HTTP/1.0.
1069          *
1070          * In any case, return 0 and let lws_read decide how to
1071          * proceed based on state
1072          */
1073         if (wsi->state != LWSS_HTTP_ISSUING_FILE)
1074                 /* Prepare to read body if we have a content length: */
1075                 if (wsi->u.http.content_length > 0)
1076                         wsi->state = LWSS_HTTP_BODY;
1077
1078         return 0;
1079
1080 bail_nuke_ah:
1081         /* we're closing, losing some rx is OK */
1082         wsi->u.hdr.ah->rxpos = wsi->u.hdr.ah->rxlen;
1083         // lwsl_notice("%s: drop1\n", __func__);
1084         lws_header_table_detach(wsi, 1);
1085
1086         return 1;
1087 #if LWS_POSIX
1088 transaction_result_n:
1089         lws_return_http_status(wsi, n, NULL);
1090
1091         return lws_http_transaction_completed(wsi);
1092 #endif
1093 }
1094
1095 int
1096 lws_bind_protocol(struct lws *wsi, const struct lws_protocols *p)
1097 {
1098 //      if (wsi->protocol == p)
1099 //              return 0;
1100
1101         if (wsi->protocol)
1102                 wsi->protocol->callback(wsi, LWS_CALLBACK_HTTP_DROP_PROTOCOL,
1103                                         wsi->user_space, NULL, 0);
1104         if (!wsi->user_space_externally_allocated)
1105                 lws_free_set_NULL(wsi->user_space);
1106
1107         wsi->protocol = p;
1108         if (!p)
1109                 return 0;
1110
1111         if (lws_ensure_user_space(wsi))
1112                 return 1;
1113
1114         if (wsi->protocol->callback(wsi, LWS_CALLBACK_HTTP_BIND_PROTOCOL,
1115                                     wsi->user_space, NULL, 0))
1116                 return 1;
1117
1118         return 0;
1119 }
1120
1121
1122 int
1123 lws_handshake_server(struct lws *wsi, unsigned char **buf, size_t len)
1124 {
1125         struct lws_context *context = lws_get_context(wsi);
1126         struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
1127         struct _lws_header_related hdr;
1128         struct allocated_headers *ah;
1129         int protocol_len, n = 0, hit, non_space_char_found = 0;
1130         char protocol_list[128];
1131         char protocol_name[64];
1132         char *p;
1133
1134         if (len >= 10000000) {
1135                 lwsl_err("%s: assert: len %ld\n", __func__, (long)len);
1136                 assert(0);
1137         }
1138
1139         if (!wsi->u.hdr.ah) {
1140                 lwsl_err("%s: assert: NULL ah\n", __func__);
1141                 assert(0);
1142         }
1143
1144         while (len--) {
1145                 wsi->more_rx_waiting = !!len;
1146
1147                 if (wsi->mode != LWSCM_HTTP_SERVING &&
1148                     wsi->mode != LWSCM_HTTP_SERVING_ACCEPTED) {
1149                         lwsl_err("%s: bad wsi mode %d\n", __func__, wsi->mode);
1150                         goto bail_nuke_ah;
1151                 }
1152
1153                 if (lws_parse(wsi, *(*buf)++)) {
1154                         lwsl_info("lws_parse failed\n");
1155                         goto bail_nuke_ah;
1156                 }
1157
1158                 if (wsi->u.hdr.parser_state != WSI_PARSING_COMPLETE)
1159                         continue;
1160
1161                 lwsl_parser("%s: lws_parse sees parsing complete\n", __func__);
1162                 lwsl_debug("%s: wsi->more_rx_waiting=%d\n", __func__,
1163                                 wsi->more_rx_waiting);
1164
1165                 /* check for unwelcome guests */
1166
1167                 if (wsi->context->reject_service_keywords) {
1168                         const struct lws_protocol_vhost_options *rej =
1169                                         wsi->context->reject_service_keywords;
1170                         char ua[384], *msg = NULL;
1171
1172                         if (lws_hdr_copy(wsi, ua, sizeof(ua) - 1,
1173                                           WSI_TOKEN_HTTP_USER_AGENT) > 0) {
1174                                 ua[sizeof(ua) - 1] = '\0';
1175                                 while (rej) {
1176                                         if (strstr(ua, rej->name)) {
1177                                                 msg = strchr(rej->value, ' ');
1178                                                 if (msg)
1179                                                         msg++;
1180                                                 lws_return_http_status(wsi, atoi(rej->value), msg);
1181
1182                                                 wsi->vhost->conn_stats.rejected++;
1183
1184                                                 goto bail_nuke_ah;
1185                                         }
1186                                         rej = rej->next;
1187                                 }
1188                         }
1189                 }
1190
1191                 /* select vhost */
1192
1193                 if (lws_hdr_total_length(wsi, WSI_TOKEN_HOST)) {
1194                         struct lws_vhost *vhost = lws_select_vhost(
1195                                 context, wsi->vhost->listen_port,
1196                                 lws_hdr_simple_ptr(wsi, WSI_TOKEN_HOST));
1197
1198                         if (vhost)
1199                                 wsi->vhost = vhost;
1200                 } else
1201                         lwsl_info("no host\n");
1202
1203                 wsi->vhost->conn_stats.trans++;
1204                 if (!wsi->conn_stat_done) {
1205                         wsi->vhost->conn_stats.conn++;
1206                         wsi->conn_stat_done = 1;
1207                 }
1208
1209                 if (lws_hdr_total_length(wsi, WSI_TOKEN_CONNECT)) {
1210                         lwsl_info("Changing to RAW mode\n");
1211                         lws_union_transition(wsi, LWSCM_RAW);
1212                         if (!wsi->more_rx_waiting) {
1213                                 wsi->u.hdr.ah->rxpos = wsi->u.hdr.ah->rxlen;
1214
1215                                 //lwsl_notice("%p: dropping ah EST\n", wsi);
1216                                 lws_header_table_detach(wsi, 1);
1217                         }
1218                 }
1219
1220                 wsi->mode = LWSCM_PRE_WS_SERVING_ACCEPT;
1221                 lws_set_timeout(wsi, NO_PENDING_TIMEOUT, 0);
1222
1223                 /* is this websocket protocol or normal http 1.0? */
1224
1225                 if (lws_hdr_total_length(wsi, WSI_TOKEN_UPGRADE)) {
1226                         if (!strcasecmp(lws_hdr_simple_ptr(wsi, WSI_TOKEN_UPGRADE),
1227                                         "websocket")) {
1228                                 wsi->vhost->conn_stats.ws_upg++;
1229                                 lwsl_info("Upgrade to ws\n");
1230                                 goto upgrade_ws;
1231                         }
1232 #ifdef LWS_USE_HTTP2
1233                         if (!strcasecmp(lws_hdr_simple_ptr(wsi, WSI_TOKEN_UPGRADE),
1234                                         "h2c")) {
1235                                 wsi->vhost->conn_stats.http2_upg++;
1236                                 lwsl_info("Upgrade to h2c\n");
1237                                 goto upgrade_h2c;
1238                         }
1239 #endif
1240                         lwsl_info("Unknown upgrade\n");
1241                         /* dunno what he wanted to upgrade to */
1242                         goto bail_nuke_ah;
1243                 }
1244
1245                 /* no upgrade ack... he remained as HTTP */
1246
1247                 lwsl_info("No upgrade\n");
1248                 ah = wsi->u.hdr.ah;
1249
1250                 lws_union_transition(wsi, LWSCM_HTTP_SERVING_ACCEPTED);
1251                 wsi->state = LWSS_HTTP;
1252                 wsi->u.http.fop_fd = NULL;
1253
1254                 /* expose it at the same offset as u.hdr */
1255                 wsi->u.http.ah = ah;
1256                 lwsl_debug("%s: wsi %p: ah %p\n", __func__, (void *)wsi,
1257                            (void *)wsi->u.hdr.ah);
1258
1259                 n = lws_http_action(wsi);
1260
1261                 return n;
1262
1263 #ifdef LWS_USE_HTTP2
1264 upgrade_h2c:
1265                 if (!lws_hdr_total_length(wsi, WSI_TOKEN_HTTP2_SETTINGS)) {
1266                         lwsl_info("missing http2_settings\n");
1267                         goto bail_nuke_ah;
1268                 }
1269
1270                 lwsl_info("h2c upgrade...\n");
1271
1272                 p = lws_hdr_simple_ptr(wsi, WSI_TOKEN_HTTP2_SETTINGS);
1273                 /* convert the peer's HTTP-Settings */
1274                 n = lws_b64_decode_string(p, protocol_list,
1275                                           sizeof(protocol_list));
1276                 if (n < 0) {
1277                         lwsl_parser("HTTP2_SETTINGS too long\n");
1278                         return 1;
1279                 }
1280
1281                 /* adopt the header info */
1282
1283                 ah = wsi->u.hdr.ah;
1284
1285                 lws_union_transition(wsi, LWSCM_HTTP2_SERVING);
1286
1287                 /* http2 union member has http union struct at start */
1288                 wsi->u.http.ah = ah;
1289
1290                 lws_http2_init(&wsi->u.http2.peer_settings);
1291                 lws_http2_init(&wsi->u.http2.my_settings);
1292
1293                 /* HTTP2 union */
1294
1295                 lws_http2_interpret_settings_payload(&wsi->u.http2.peer_settings,
1296                                 (unsigned char *)protocol_list, n);
1297
1298                 strcpy(protocol_list,
1299                        "HTTP/1.1 101 Switching Protocols\x0d\x0a"
1300                       "Connection: Upgrade\x0d\x0a"
1301                       "Upgrade: h2c\x0d\x0a\x0d\x0a");
1302                 n = lws_issue_raw(wsi, (unsigned char *)protocol_list,
1303                                         strlen(protocol_list));
1304                 if (n != strlen(protocol_list)) {
1305                         lwsl_debug("http2 switch: ERROR writing to socket\n");
1306                         return 1;
1307                 }
1308
1309                 wsi->state = LWSS_HTTP2_AWAIT_CLIENT_PREFACE;
1310
1311                 return 0;
1312 #endif
1313
1314 upgrade_ws:
1315                 if (!wsi->protocol)
1316                         lwsl_err("NULL protocol at lws_read\n");
1317
1318                 /*
1319                  * It's websocket
1320                  *
1321                  * Select the first protocol we support from the list
1322                  * the client sent us.
1323                  *
1324                  * Copy it to remove header fragmentation
1325                  */
1326
1327                 if (lws_hdr_copy(wsi, protocol_list, sizeof(protocol_list) - 1,
1328                                  WSI_TOKEN_PROTOCOL) < 0) {
1329                         lwsl_err("protocol list too long");
1330                         goto bail_nuke_ah;
1331                 }
1332
1333                 protocol_len = lws_hdr_total_length(wsi, WSI_TOKEN_PROTOCOL);
1334                 protocol_list[protocol_len] = '\0';
1335                 p = protocol_list;
1336                 hit = 0;
1337
1338                 while (*p && !hit) {
1339                         n = 0;
1340                         non_space_char_found = 0;
1341                         while (n < sizeof(protocol_name) - 1 && *p &&
1342                                *p != ',') {
1343                                 // ignore leading spaces
1344                                 if (!non_space_char_found && *p == ' ') {
1345                                         n++;
1346                                         continue;
1347                                 }
1348                                 non_space_char_found = 1;
1349                                 protocol_name[n++] = *p++;
1350                         }
1351                         protocol_name[n] = '\0';
1352                         if (*p)
1353                                 p++;
1354
1355                         lwsl_info("checking %s\n", protocol_name);
1356
1357                         n = 0;
1358                         while (wsi->vhost->protocols[n].callback) {
1359                                 lwsl_info("try %s\n", wsi->vhost->protocols[n].name);
1360
1361                                 if (wsi->vhost->protocols[n].name &&
1362                                     !strcmp(wsi->vhost->protocols[n].name,
1363                                             protocol_name)) {
1364                                         wsi->protocol = &wsi->vhost->protocols[n];
1365                                         hit = 1;
1366                                         break;
1367                                 }
1368
1369                                 n++;
1370                         }
1371                 }
1372
1373                 /* we didn't find a protocol he wanted? */
1374
1375                 if (!hit) {
1376                         if (lws_hdr_simple_ptr(wsi, WSI_TOKEN_PROTOCOL)) {
1377                                 lwsl_info("No protocol from \"%s\" supported\n",
1378                                          protocol_list);
1379                                 goto bail_nuke_ah;
1380                         }
1381                         /*
1382                          * some clients only have one protocol and
1383                          * do not send the protocol list header...
1384                          * allow it and match to the vhost's default
1385                          * protocol (which itself defaults to zero)
1386                          */
1387                         lwsl_info("defaulting to prot handler %d\n",
1388                                 wsi->vhost->default_protocol_index);
1389                         n = 0;
1390                         wsi->protocol = &wsi->vhost->protocols[
1391                                       (int)wsi->vhost->default_protocol_index];
1392                 }
1393
1394                 /* allocate wsi->user storage */
1395                 if (lws_ensure_user_space(wsi))
1396                         goto bail_nuke_ah;
1397
1398                 /*
1399                  * Give the user code a chance to study the request and
1400                  * have the opportunity to deny it
1401                  */
1402                 if ((wsi->protocol->callback)(wsi,
1403                                 LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION,
1404                                 wsi->user_space,
1405                               lws_hdr_simple_ptr(wsi, WSI_TOKEN_PROTOCOL), 0)) {
1406                         lwsl_warn("User code denied connection\n");
1407                         goto bail_nuke_ah;
1408                 }
1409
1410                 /*
1411                  * Perform the handshake according to the protocol version the
1412                  * client announced
1413                  */
1414
1415                 switch (wsi->ietf_spec_revision) {
1416                 case 13:
1417                         lwsl_parser("lws_parse calling handshake_04\n");
1418                         if (handshake_0405(context, wsi)) {
1419                                 lwsl_info("hs0405 has failed the connection\n");
1420                                 goto bail_nuke_ah;
1421                         }
1422                         break;
1423
1424                 default:
1425                         lwsl_info("Unknown client spec version %d\n",
1426                                   wsi->ietf_spec_revision);
1427                         goto bail_nuke_ah;
1428                 }
1429
1430                 /*
1431                  * stitch protocol choice into the vh protocol linked list
1432                  * We always insert ourselves at the start of the list
1433                  *
1434                  * X <-> B
1435                  * X <-> pAn <-> pB
1436                  */
1437                 //lwsl_err("%s: pre insert vhost start wsi %p, that wsi prev == %p\n",
1438                 //              __func__,
1439                 //              wsi->vhost->same_vh_protocol_list[n],
1440                 //              wsi->same_vh_protocol_prev);
1441                 wsi->same_vh_protocol_prev = /* guy who points to us */
1442                         &wsi->vhost->same_vh_protocol_list[n];
1443                 wsi->same_vh_protocol_next = /* old first guy is our next */
1444                                 wsi->vhost->same_vh_protocol_list[n];
1445                 /* we become the new first guy */
1446                 wsi->vhost->same_vh_protocol_list[n] = wsi;
1447
1448                 if (wsi->same_vh_protocol_next)
1449                         /* old first guy points back to us now */
1450                         wsi->same_vh_protocol_next->same_vh_protocol_prev =
1451                                         &wsi->same_vh_protocol_next;
1452
1453
1454
1455                 /* we are upgrading to ws, so http/1.1 and keepalive +
1456                  * pipelined header considerations about keeping the ah around
1457                  * no longer apply.  However it's common for the first ws
1458                  * protocol data to have been coalesced with the browser
1459                  * upgrade request and to already be in the ah rx buffer.
1460                  */
1461
1462                 lwsl_info("%s: %p: inheriting ah in ws mode (rxpos:%d, rxlen:%d)\n",
1463                           __func__, wsi, wsi->u.hdr.ah->rxpos,
1464                           wsi->u.hdr.ah->rxlen);
1465                 lws_pt_lock(pt);
1466                 hdr = wsi->u.hdr;
1467
1468                 lws_union_transition(wsi, LWSCM_WS_SERVING);
1469                 /*
1470                  * first service is WS mode will notice this, use the RX and
1471                  * then detach the ah (caution: we are not in u.hdr union
1472                  * mode any more then... ah_temp member is at start the same
1473                  * though)
1474                  *
1475                  * Because rxpos/rxlen shows something in the ah, we will get
1476                  * service guaranteed next time around the event loop
1477                  *
1478                  * All union members begin with hdr, so we can use it even
1479                  * though we transitioned to ws union mode (the ah detach
1480                  * code uses it anyway).
1481                  */
1482                 wsi->u.hdr = hdr;
1483                 lws_pt_unlock(pt);
1484
1485                 lws_restart_ws_ping_pong_timer(wsi);
1486
1487                 /*
1488                  * create the frame buffer for this connection according to the
1489                  * size mentioned in the protocol definition.  If 0 there, use
1490                  * a big default for compatibility
1491                  */
1492
1493                 n = wsi->protocol->rx_buffer_size;
1494                 if (!n)
1495                         n = context->pt_serv_buf_size;
1496                 n += LWS_PRE;
1497                 wsi->u.ws.rx_ubuf = lws_malloc(n + 4 /* 0x0000ffff zlib */);
1498                 if (!wsi->u.ws.rx_ubuf) {
1499                         lwsl_err("Out of Mem allocating rx buffer %d\n", n);
1500                         return 1;
1501                 }
1502                 wsi->u.ws.rx_ubuf_alloc = n;
1503                 lwsl_debug("Allocating RX buffer %d\n", n);
1504 #if LWS_POSIX && !defined(LWS_WITH_ESP32)
1505                 if (setsockopt(wsi->sock, SOL_SOCKET, SO_SNDBUF,
1506                                (const char *)&n, sizeof n)) {
1507                         lwsl_warn("Failed to set SNDBUF to %d", n);
1508                         return 1;
1509                 }
1510 #endif
1511
1512                 lwsl_parser("accepted v%02d connection\n",
1513                             wsi->ietf_spec_revision);
1514
1515                 /* notify user code that we're ready to roll */
1516
1517                 if (wsi->protocol->callback)
1518                         if (wsi->protocol->callback(wsi, LWS_CALLBACK_ESTABLISHED,
1519                                                     wsi->user_space,
1520 #ifdef LWS_OPENSSL_SUPPORT
1521                                                     wsi->ssl,
1522 #else
1523                                                     NULL,
1524 #endif
1525                                                     0))
1526                                 return 1;
1527
1528                 /* !!! drop ah unreservedly after ESTABLISHED */
1529                 if (!wsi->more_rx_waiting) {
1530                         wsi->u.hdr.ah->rxpos = wsi->u.hdr.ah->rxlen;
1531
1532                         //lwsl_notice("%p: dropping ah EST\n", wsi);
1533                         lws_header_table_detach(wsi, 1);
1534                 }
1535
1536                 return 0;
1537         } /* while all chars are handled */
1538
1539         return 0;
1540
1541 bail_nuke_ah:
1542         /* drop the header info */
1543         /* we're closing, losing some rx is OK */
1544         wsi->u.hdr.ah->rxpos = wsi->u.hdr.ah->rxlen;
1545         //lwsl_notice("%s: drop2\n", __func__);
1546         lws_header_table_detach(wsi, 1);
1547
1548         return 1;
1549 }
1550
1551 static int
1552 lws_get_idlest_tsi(struct lws_context *context)
1553 {
1554         unsigned int lowest = ~0;
1555         int n = 0, hit = -1;
1556
1557         for (; n < context->count_threads; n++) {
1558                 if ((unsigned int)context->pt[n].fds_count !=
1559                     context->fd_limit_per_thread - 1 &&
1560                     (unsigned int)context->pt[n].fds_count < lowest) {
1561                         lowest = context->pt[n].fds_count;
1562                         hit = n;
1563                 }
1564         }
1565
1566         return hit;
1567 }
1568
1569 struct lws *
1570 lws_create_new_server_wsi(struct lws_vhost *vhost)
1571 {
1572         struct lws *new_wsi;
1573         int n = lws_get_idlest_tsi(vhost->context);
1574
1575         if (n < 0) {
1576                 lwsl_err("no space for new conn\n");
1577                 return NULL;
1578         }
1579
1580         new_wsi = lws_zalloc(sizeof(struct lws));
1581         if (new_wsi == NULL) {
1582                 lwsl_err("Out of memory for new connection\n");
1583                 return NULL;
1584         }
1585
1586         new_wsi->tsi = n;
1587         lwsl_debug("Accepted wsi %p to context %p, tsi %d\n", new_wsi,
1588                     vhost->context, new_wsi->tsi);
1589
1590         new_wsi->vhost = vhost;
1591         new_wsi->context = vhost->context;
1592         new_wsi->pending_timeout = NO_PENDING_TIMEOUT;
1593         new_wsi->rxflow_change_to = LWS_RXFLOW_ALLOW;
1594
1595         /* initialize the instance struct */
1596
1597         new_wsi->state = LWSS_HTTP;
1598         new_wsi->mode = LWSCM_HTTP_SERVING;
1599         new_wsi->hdr_parsing_completed = 0;
1600
1601 #ifdef LWS_OPENSSL_SUPPORT
1602         new_wsi->use_ssl = LWS_SSL_ENABLED(vhost);
1603 #endif
1604
1605         /*
1606          * these can only be set once the protocol is known
1607          * we set an unestablished connection's protocol pointer
1608          * to the start of the supported list, so it can look
1609          * for matching ones during the handshake
1610          */
1611         new_wsi->protocol = vhost->protocols;
1612         new_wsi->user_space = NULL;
1613         new_wsi->ietf_spec_revision = 0;
1614         new_wsi->sock = LWS_SOCK_INVALID;
1615         vhost->context->count_wsi_allocated++;
1616
1617         /*
1618          * outermost create notification for wsi
1619          * no user_space because no protocol selection
1620          */
1621         vhost->protocols[0].callback(new_wsi, LWS_CALLBACK_WSI_CREATE,
1622                                        NULL, NULL, 0);
1623
1624         return new_wsi;
1625 }
1626
1627 LWS_VISIBLE int LWS_WARN_UNUSED_RESULT
1628 lws_http_transaction_completed(struct lws *wsi)
1629 {
1630         int n = NO_PENDING_TIMEOUT;
1631
1632         lws_access_log(wsi);
1633
1634         lwsl_info("%s: wsi %p\n", __func__, wsi);
1635         /* if we can't go back to accept new headers, drop the connection */
1636         if (wsi->u.http.connection_type != HTTP_CONNECTION_KEEP_ALIVE) {
1637                 lwsl_info("%s: %p: close connection\n", __func__, wsi);
1638                 return 1;
1639         }
1640
1641         if (lws_bind_protocol(wsi, &wsi->vhost->protocols[0]))
1642                 return 1;
1643
1644         /* otherwise set ourselves up ready to go again */
1645         wsi->state = LWSS_HTTP;
1646         wsi->mode = LWSCM_HTTP_SERVING;
1647         wsi->u.http.content_length = 0;
1648         wsi->u.http.content_remain = 0;
1649         wsi->hdr_parsing_completed = 0;
1650 #ifdef LWS_WITH_ACCESS_LOG
1651         wsi->access_log.sent = 0;
1652 #endif
1653
1654         if (wsi->vhost->keepalive_timeout)
1655                 n = PENDING_TIMEOUT_HTTP_KEEPALIVE_IDLE;
1656         lws_set_timeout(wsi, n, wsi->vhost->keepalive_timeout);
1657
1658         /*
1659          * We already know we are on http1.1 / keepalive and the next thing
1660          * coming will be another header set.
1661          *
1662          * If there is no pending rx and we still have the ah, drop it and
1663          * reacquire a new ah when the new headers start to arrive.  (Otherwise
1664          * we needlessly hog an ah indefinitely.)
1665          *
1666          * However if there is pending rx and we know from the keepalive state
1667          * that is already at least the start of another header set, simply
1668          * reset the existing header table and keep it.
1669          */
1670         if (wsi->u.hdr.ah) {
1671                 lwsl_info("%s: wsi->more_rx_waiting=%d\n", __func__,
1672                                 wsi->more_rx_waiting);
1673
1674                 if (!wsi->more_rx_waiting) {
1675                         wsi->u.hdr.ah->rxpos = wsi->u.hdr.ah->rxlen;
1676                         lws_header_table_detach(wsi, 1);
1677                 } else
1678                         lws_header_table_reset(wsi, 1);
1679         }
1680
1681         /* If we're (re)starting on headers, need other implied init */
1682         wsi->u.hdr.ues = URIES_IDLE;
1683
1684         lwsl_info("%s: %p: keep-alive await new transaction\n", __func__, wsi);
1685
1686         return 0;
1687 }
1688
1689 LWS_VISIBLE struct lws *
1690 lws_adopt_socket_vhost2(struct lws_vhost *vh, lws_sockfd_type accept_fd,
1691                         int allow_ssl, int raw)
1692 {
1693         struct lws_context *context = vh->context;
1694         struct lws *new_wsi = lws_create_new_server_wsi(vh);
1695         int n;
1696
1697         if (!new_wsi) {
1698                 compatible_close(accept_fd);
1699                 return NULL;
1700         }
1701
1702         lwsl_debug("%s: new wsi %p, sockfd %d, cb %p\n", __func__, new_wsi,
1703                    accept_fd, context->vhost_list->protocols[0].callback);
1704
1705         new_wsi->sock = accept_fd;
1706
1707         /* the transport is accepted... give him time to negotiate */
1708         lws_set_timeout(new_wsi, PENDING_TIMEOUT_ESTABLISH_WITH_SERVER,
1709                         context->timeout_secs);
1710
1711 #if LWS_POSIX == 0
1712 #if defined(LWS_WITH_ESP8266)
1713         esp8266_tcp_stream_accept(accept_fd, new_wsi);
1714 #endif
1715 #endif
1716         /*
1717          * A new connection was accepted. Give the user a chance to
1718          * set properties of the newly created wsi. There's no protocol
1719          * selected yet so we issue this to the vhosts's default protocol,
1720          * itself by default protocols[0]
1721          */
1722         n = LWS_CALLBACK_SERVER_NEW_CLIENT_INSTANTIATED;
1723         if (raw)
1724                 n = LWS_CALLBACK_RAW_ADOPT;
1725         if ((context->vhost_list->protocols[vh->default_protocol_index].callback)(
1726                         new_wsi, n, NULL, NULL, 0)) {
1727                 /* force us off the timeout list by hand */
1728                 lws_set_timeout(new_wsi, NO_PENDING_TIMEOUT, 0);
1729                 compatible_close(new_wsi->sock);
1730                 lws_free(new_wsi);
1731                 return NULL;
1732         }
1733
1734         lws_libev_accept(new_wsi, new_wsi->sock);
1735         lws_libuv_accept(new_wsi, new_wsi->sock);
1736
1737         if (!LWS_SSL_ENABLED(new_wsi->vhost) || allow_ssl == 0) {
1738                 if (raw)
1739                         new_wsi->mode = LWSCM_RAW;
1740                 if (insert_wsi_socket_into_fds(context, new_wsi)) {
1741                         lwsl_err("%s: fail inserting socket\n", __func__);
1742                         goto fail;
1743                 }
1744         } else {
1745                 if (raw)
1746                         new_wsi->mode = LWSCM_SSL_INIT_RAW;
1747                 else
1748                         new_wsi->mode = LWSCM_SSL_INIT;
1749                 if (lws_server_socket_service_ssl(new_wsi, accept_fd)) {
1750                         lwsl_err("%s: fail ssl negotiation\n", __func__);
1751                         goto fail;
1752                 }
1753         }
1754
1755         if (!lws_header_table_attach(new_wsi, 0))
1756                 lwsl_debug("Attached ah immediately\n");
1757
1758         return new_wsi;
1759
1760 fail:
1761         lws_close_free_wsi(new_wsi, LWS_CLOSE_STATUS_NOSTATUS);
1762
1763         return NULL;
1764 }
1765
1766 LWS_VISIBLE struct lws *
1767 lws_adopt_socket_vhost(struct lws_vhost *vh, lws_sockfd_type accept_fd)
1768 {
1769         return lws_adopt_socket_vhost2(vh, accept_fd, 1, 0);
1770 }
1771
1772 LWS_VISIBLE struct lws *
1773 lws_adopt_socket(struct lws_context *context, lws_sockfd_type accept_fd)
1774 {
1775         return lws_adopt_socket_vhost2(context->vhost_list, accept_fd, 1, 0);
1776 }
1777
1778 /* Common read-buffer adoption for lws_adopt_*_readbuf */
1779 static struct lws*
1780 adopt_socket_readbuf(struct lws *wsi, const char *readbuf, size_t len)
1781 {
1782         struct lws_context_per_thread *pt;
1783         struct allocated_headers *ah;
1784         struct lws_pollfd *pfd;
1785
1786         if (!wsi)
1787                 return NULL;
1788
1789         if (!readbuf || len == 0)
1790                 return wsi;
1791
1792         if (len > sizeof(ah->rx)) {
1793                 lwsl_err("%s: rx in too big\n", __func__);
1794                 goto bail;
1795         }
1796
1797         /*
1798          * we can't process the initial read data until we can attach an ah.
1799          *
1800          * if one is available, get it and place the data in his ah rxbuf...
1801          * wsi with ah that have pending rxbuf get auto-POLLIN service.
1802          *
1803          * no autoservice because we didn't get a chance to attach the
1804          * readbuf data to wsi or ah yet, and we will do it next if we get
1805          * the ah.
1806          */
1807         if (wsi->u.hdr.ah || !lws_header_table_attach(wsi, 0)) {
1808                 ah = wsi->u.hdr.ah;
1809                 memcpy(ah->rx, readbuf, len);
1810                 ah->rxpos = 0;
1811                 ah->rxlen = len;
1812
1813                 lwsl_notice("%s: calling service on readbuf ah\n", __func__);
1814                 pt = &wsi->context->pt[(int)wsi->tsi];
1815
1816                 /* unlike a normal connect, we have the headers already
1817                  * (or the first part of them anyway).
1818                  * libuv won't come back and service us without a network
1819                  * event, so we need to do the header service right here.
1820                  */
1821                 pfd = &pt->fds[wsi->position_in_fds_table];
1822                 pfd->revents |= LWS_POLLIN;
1823                 lwsl_err("%s: calling service\n", __func__);
1824                 if (lws_service_fd_tsi(wsi->context, pfd, wsi->tsi))
1825                         /* service closed us */
1826                         return NULL;
1827
1828                 return wsi;
1829         }
1830         lwsl_err("%s: deferring handling ah\n", __func__);
1831         /*
1832          * hum if no ah came, we are on the wait list and must defer
1833          * dealing with this until the ah arrives.
1834          *
1835          * later successful lws_header_table_attach() will apply the
1836          * below to the rx buffer (via lws_header_table_reset()).
1837          */
1838         wsi->u.hdr.preamble_rx = lws_malloc(len);
1839         if (!wsi->u.hdr.preamble_rx) {
1840                 lwsl_err("OOM\n");
1841                 goto bail;
1842         }
1843         memcpy(wsi->u.hdr.preamble_rx, readbuf, len);
1844         wsi->u.hdr.preamble_rx_len = len;
1845
1846         return wsi;
1847
1848 bail:
1849         lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS);
1850
1851         return NULL;
1852 }
1853
1854 LWS_VISIBLE struct lws *
1855 lws_adopt_socket_readbuf(struct lws_context *context, lws_sockfd_type accept_fd,
1856                          const char *readbuf, size_t len)
1857 {
1858         return adopt_socket_readbuf(lws_adopt_socket(context, accept_fd), readbuf, len);
1859 }
1860
1861 LWS_VISIBLE struct lws *
1862 lws_adopt_socket_vhost_readbuf(struct lws_vhost *vhost, lws_sockfd_type accept_fd,
1863                          const char *readbuf, size_t len)
1864 {
1865         return adopt_socket_readbuf(lws_adopt_socket_vhost(vhost, accept_fd), readbuf, len);
1866 }
1867
1868 LWS_VISIBLE int
1869 lws_server_socket_service(struct lws_context *context, struct lws *wsi,
1870                           struct lws_pollfd *pollfd)
1871 {
1872         struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
1873         lws_sockfd_type accept_fd = LWS_SOCK_INVALID;
1874         struct allocated_headers *ah;
1875 #if LWS_POSIX
1876         struct sockaddr_in cli_addr;
1877         socklen_t clilen;
1878 #endif
1879         int n, len;
1880         
1881         // lwsl_notice("%s: mode %d\n", __func__, wsi->mode);
1882
1883         switch (wsi->mode) {
1884
1885         case LWSCM_HTTP_SERVING:
1886         case LWSCM_HTTP_SERVING_ACCEPTED:
1887         case LWSCM_HTTP2_SERVING:
1888
1889                 /* handle http headers coming in */
1890
1891                 /* pending truncated sends have uber priority */
1892
1893                 if (wsi->trunc_len) {
1894                         if (!(pollfd->revents & LWS_POLLOUT))
1895                                 break;
1896
1897                         if (lws_issue_raw(wsi, wsi->trunc_alloc +
1898                                                wsi->trunc_offset,
1899                                           wsi->trunc_len) < 0)
1900                                 goto fail;
1901                         /*
1902                          * we can't afford to allow input processing to send
1903                          * something new, so spin around he event loop until
1904                          * he doesn't have any partials
1905                          */
1906                         break;
1907                 }
1908
1909                 /* any incoming data ready? */
1910
1911                 if (!(pollfd->revents & pollfd->events & LWS_POLLIN))
1912                         goto try_pollout;
1913
1914                 /*
1915                  * If we previously just did POLLIN when IN and OUT were
1916                  * signalled (because POLLIN processing may have used up
1917                  * the POLLOUT), don't let that happen twice in a row...
1918                  * next time we see the situation favour POLLOUT
1919                  */
1920 #if !defined(LWS_WITH_ESP8266)
1921                 if (wsi->favoured_pollin &&
1922                     (pollfd->revents & pollfd->events & LWS_POLLOUT)) {
1923                         wsi->favoured_pollin = 0;
1924                         goto try_pollout;
1925                 }
1926 #endif
1927
1928                 /* these states imply we MUST have an ah attached */
1929
1930                 if (wsi->state == LWSS_HTTP ||
1931                     wsi->state == LWSS_HTTP_ISSUING_FILE ||
1932                     wsi->state == LWSS_HTTP_HEADERS) {
1933                         if (!wsi->u.hdr.ah) {
1934                                 
1935                                 //lwsl_err("wsi %p: missing ah\n", wsi);
1936                                 /* no autoservice beacuse we will do it next */
1937                                 if (lws_header_table_attach(wsi, 0)) {
1938                                         lwsl_err("wsi %p: failed to acquire ah\n", wsi);
1939                                         goto try_pollout;
1940                                 }
1941                         }
1942                         ah = wsi->u.hdr.ah;
1943
1944                         //lwsl_notice("%s: %p: rxpos:%d rxlen:%d\n", __func__, wsi,
1945                         //         ah->rxpos, ah->rxlen);
1946
1947                         /* if nothing in ah rx buffer, get some fresh rx */
1948                         if (ah->rxpos == ah->rxlen) {
1949                                 ah->rxlen = lws_ssl_capable_read(wsi, ah->rx,
1950                                                    sizeof(ah->rx));
1951                                 ah->rxpos = 0;
1952                                 //lwsl_notice("%s: wsi %p, ah->rxlen = %d\r\n",
1953                                 //         __func__, wsi, ah->rxlen);
1954                                 switch (ah->rxlen) {
1955                                 case 0:
1956                                         lwsl_info("%s: read 0 len\n", __func__);
1957                                         /* lwsl_info("   state=%d\n", wsi->state); */
1958 //                                      if (!wsi->hdr_parsing_completed)
1959 //                                              lws_header_table_detach(wsi);
1960                                         /* fallthru */
1961                                 case LWS_SSL_CAPABLE_ERROR:
1962                                         goto fail;
1963                                 case LWS_SSL_CAPABLE_MORE_SERVICE:
1964                                         ah->rxlen = ah->rxpos = 0;
1965                                         goto try_pollout;
1966                                 }
1967                         }
1968
1969                         if (!(ah->rxpos != ah->rxlen && ah->rxlen)) {
1970                                 lwsl_err("%s: assert: rxpos %d, rxlen %d\n",
1971                                          __func__, ah->rxpos, ah->rxlen);
1972
1973                                 assert(0);
1974                         }
1975                         
1976                         /* just ignore incoming if waiting for close */
1977                         if (wsi->state != LWSS_FLUSHING_STORED_SEND_BEFORE_CLOSE) {
1978                                 n = lws_read(wsi, ah->rx + ah->rxpos,
1979                                              ah->rxlen - ah->rxpos);
1980                                 if (n < 0) /* we closed wsi */
1981                                         return 1;
1982                                 if (wsi->u.hdr.ah) {
1983                                         if ( wsi->u.hdr.ah->rxlen)
1984                                                  wsi->u.hdr.ah->rxpos += n;
1985
1986                                         lwsl_debug("%s: wsi %p: ah read rxpos %d, rxlen %d\n", __func__, wsi, wsi->u.hdr.ah->rxpos, wsi->u.hdr.ah->rxlen);
1987
1988                                         if (wsi->u.hdr.ah->rxpos == wsi->u.hdr.ah->rxlen &&
1989                                             (wsi->mode != LWSCM_HTTP_SERVING &&
1990                                              wsi->mode != LWSCM_HTTP_SERVING_ACCEPTED &&
1991                                              wsi->mode != LWSCM_HTTP2_SERVING))
1992                                                 lws_header_table_detach(wsi, 1);
1993                                 }
1994                                 break;
1995                         }
1996
1997                         goto try_pollout;
1998                 }
1999
2000                 len = lws_ssl_capable_read(wsi, pt->serv_buf,
2001                                            context->pt_serv_buf_size);
2002                 lwsl_notice("%s: wsi %p read %d\r\n", __func__, wsi, len);
2003                 switch (len) {
2004                 case 0:
2005                         lwsl_info("%s: read 0 len\n", __func__);
2006                         /* lwsl_info("   state=%d\n", wsi->state); */
2007 //                      if (!wsi->hdr_parsing_completed)
2008 //                              lws_header_table_detach(wsi);
2009                         /* fallthru */
2010                 case LWS_SSL_CAPABLE_ERROR:
2011                         goto fail;
2012                 case LWS_SSL_CAPABLE_MORE_SERVICE:
2013                         goto try_pollout;
2014                 }
2015                 
2016                 if (wsi->mode == LWSCM_RAW) {
2017                         n = user_callback_handle_rxflow(wsi->protocol->callback,
2018                                         wsi, LWS_CALLBACK_RAW_RX,
2019                                         wsi->user_space, pt->serv_buf, len);
2020                         if (n < 0) {
2021                                 lwsl_info("raw writeable_fail\n");
2022                                 goto fail;
2023                         }
2024                         break;
2025                 }
2026
2027                 /* just ignore incoming if waiting for close */
2028                 if (wsi->state != LWSS_FLUSHING_STORED_SEND_BEFORE_CLOSE) {
2029                         /*
2030                          * this may want to send
2031                          * (via HTTP callback for example)
2032                          */
2033                         n = lws_read(wsi, pt->serv_buf, len);
2034                         if (n < 0) /* we closed wsi */
2035                                 return 1;
2036                         /*
2037                          *  he may have used up the
2038                          * writability above, if we will defer POLLOUT
2039                          * processing in favour of POLLIN, note it
2040                          */
2041                         if (pollfd->revents & LWS_POLLOUT)
2042                                 wsi->favoured_pollin = 1;
2043                         break;
2044                 }
2045
2046 try_pollout:
2047                 
2048                 /* this handles POLLOUT for http serving fragments */
2049
2050                 if (!(pollfd->revents & LWS_POLLOUT))
2051                         break;
2052
2053                 /* one shot */
2054                 if (lws_change_pollfd(wsi, LWS_POLLOUT, 0)) {
2055                         lwsl_notice("%s a\n", __func__);
2056                         goto fail;
2057                 }
2058
2059                 if (!wsi->hdr_parsing_completed)
2060                         break;
2061
2062                 if (wsi->state != LWSS_HTTP_ISSUING_FILE) {
2063                         n = user_callback_handle_rxflow(wsi->protocol->callback,
2064                                         wsi, LWS_CALLBACK_HTTP_WRITEABLE,
2065                                         wsi->user_space, NULL, 0);
2066                         if (n < 0) {
2067                                 lwsl_info("writeable_fail\n");
2068                                 goto fail;
2069                         }
2070                         break;
2071                 }
2072
2073                 /* >0 == completion, <0 == error */
2074                 n = lws_serve_http_file_fragment(wsi);
2075                 if (n < 0 || (n > 0 && lws_http_transaction_completed(wsi))) {
2076                         lwsl_info("completed\n");
2077                         goto fail;
2078                 }
2079
2080                 break;
2081
2082         case LWSCM_SERVER_LISTENER:
2083
2084 #if LWS_POSIX
2085                 /* pollin means a client has connected to us then */
2086
2087                 do {
2088                         if (!(pollfd->revents & LWS_POLLIN) || !(pollfd->events & LWS_POLLIN))
2089                                 break;
2090
2091                         /* listen socket got an unencrypted connection... */
2092
2093                         clilen = sizeof(cli_addr);
2094                         lws_latency_pre(context, wsi);
2095                         accept_fd  = accept(pollfd->fd, (struct sockaddr *)&cli_addr,
2096                                             &clilen);
2097                         lws_latency(context, wsi, "listener accept", accept_fd,
2098                                     accept_fd >= 0);
2099                         if (accept_fd < 0) {
2100                                 if (LWS_ERRNO == LWS_EAGAIN ||
2101                                     LWS_ERRNO == LWS_EWOULDBLOCK) {
2102                                         lwsl_err("accept asks to try again\n");
2103                                         break;
2104                                 }
2105                                 lwsl_err("ERROR on accept: %s\n", strerror(LWS_ERRNO));
2106                                 break;
2107                         }
2108
2109                         lws_plat_set_socket_options(wsi->vhost, accept_fd);
2110
2111                         lwsl_debug("accepted new conn  port %u on fd=%d\n",
2112                                           ntohs(cli_addr.sin_port), accept_fd);
2113
2114 #else
2115                         /* not very beautiful... */
2116                         accept_fd = (lws_sockfd_type)pollfd;
2117 #endif
2118                         /*
2119                          * look at who we connected to and give user code a chance
2120                          * to reject based on client IP.  There's no protocol selected
2121                          * yet so we issue this to protocols[0]
2122                          */
2123                         if ((wsi->vhost->protocols[0].callback)(wsi,
2124                                         LWS_CALLBACK_FILTER_NETWORK_CONNECTION,
2125                                         NULL, (void *)(long)accept_fd, 0)) {
2126                                 lwsl_debug("Callback denied network connection\n");
2127                                 compatible_close(accept_fd);
2128                                 break;
2129                         }
2130
2131                         if (!lws_adopt_socket_vhost(wsi->vhost, accept_fd))
2132                                 /* already closed cleanly as necessary */
2133                                 return 1;
2134
2135 #if LWS_POSIX
2136                 } while (pt->fds_count < context->fd_limit_per_thread - 1 &&
2137                          lws_poll_listen_fd(&pt->fds[wsi->position_in_fds_table]) > 0);
2138 #endif
2139                 return 0;
2140
2141         default:
2142                 break;
2143         }
2144
2145         if (!lws_server_socket_service_ssl(wsi, accept_fd))
2146                 return 0;
2147
2148 fail:
2149         lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS);
2150
2151         return 1;
2152 }
2153
2154 LWS_VISIBLE int
2155 lws_serve_http_file(struct lws *wsi, const char *file, const char *content_type,
2156                     const char *other_headers, int other_headers_len)
2157 {
2158         static const char * const intermediates[] = { "private", "public" };
2159         struct lws_context *context = lws_get_context(wsi);
2160         struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
2161 #if defined(LWS_WITH_RANGES)
2162         struct lws_range_parsing *rp = &wsi->u.http.range;
2163 #endif
2164         char cache_control[50], *cc = "no-store";
2165         unsigned char *response = pt->serv_buf + LWS_PRE;
2166         unsigned char *p = response;
2167         unsigned char *end = p + context->pt_serv_buf_size - LWS_PRE;
2168         unsigned long computed_total_content_length;
2169         int ret = 0, cclen = 8, n = HTTP_STATUS_OK;
2170         lws_fop_flags_t fflags = O_RDONLY;
2171 #if defined(LWS_WITH_RANGES)
2172         int ranges;
2173 #endif
2174
2175         if (lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_ACCEPT_ENCODING))
2176                 if (strstr("gzip",  lws_hdr_simple_ptr(wsi, WSI_TOKEN_HTTP_ACCEPT_ENCODING)) &&
2177                     strstr("deflate",  lws_hdr_simple_ptr(wsi, WSI_TOKEN_HTTP_ACCEPT_ENCODING))) {
2178                         lwsl_debug("client indicates GZIP is acceptable\n");
2179                         fflags |= LWS_FOP_FLAG_COMPR_ACCEPTABLE_GZIP;
2180                 }
2181
2182
2183         wsi->u.http.fop_fd = lws_plat_file_open(&wsi->context->fops, file,
2184                                                 &wsi->u.http.filelen,
2185                                                 &fflags);
2186         if (!wsi->u.http.fop_fd) {
2187                 lwsl_err("Unable to open '%s'\n", file);
2188
2189                 return -1;
2190         }
2191         computed_total_content_length = wsi->u.http.filelen;
2192
2193         if ((fflags & (LWS_FOP_FLAG_COMPR_ACCEPTABLE_GZIP |
2194                        LWS_FOP_FLAG_COMPR_IS_GZIP)) ==
2195             (LWS_FOP_FLAG_COMPR_ACCEPTABLE_GZIP | LWS_FOP_FLAG_COMPR_IS_GZIP)) {
2196                 if (lws_add_http_header_by_token(wsi,
2197                         WSI_TOKEN_HTTP_CONTENT_ENCODING,
2198                         (unsigned char *)"gzip, deflate", 13, &p, end))
2199                         return -1;
2200                 lwsl_debug("file is being provided in gzip\n");
2201         }
2202
2203 #if defined(LWS_WITH_RANGES)
2204         ranges = lws_ranges_init(wsi, rp, wsi->u.http.filelen);
2205
2206         lwsl_debug("Range count %d\n", ranges);
2207         /*
2208          * no ranges -> 200;
2209          *  1 range  -> 206 + Content-Type: normal; Content-Range;
2210          *  more     -> 206 + Content-Type: multipart/byteranges
2211          *              Repeat the true Content-Type in each multipart header
2212          *              along with Content-Range
2213          */
2214         if (ranges < 0) {
2215                 /* it means he expressed a range in Range:, but it was illegal */
2216                 lws_return_http_status(wsi, HTTP_STATUS_REQ_RANGE_NOT_SATISFIABLE, NULL);
2217                 if (lws_http_transaction_completed(wsi))
2218                         return -1; /* <0 means just hang up */
2219
2220                 return 0; /* == 0 means we dealt with the transaction complete */
2221         }
2222         if (ranges)
2223                 n = HTTP_STATUS_PARTIAL_CONTENT;
2224 #endif
2225
2226         if (lws_add_http_header_status(wsi, n, &p, end))
2227                 return -1;
2228
2229 #if defined(LWS_WITH_RANGES)
2230         if (ranges < 2 && content_type && content_type[0])
2231                 if (lws_add_http_header_by_token(wsi, WSI_TOKEN_HTTP_CONTENT_TYPE,
2232                                                  (unsigned char *)content_type,
2233                                                  strlen(content_type), &p, end))
2234                         return -1;
2235
2236         if (ranges >= 2) { /* multipart byteranges */
2237                 strncpy(wsi->u.http.multipart_content_type, content_type,
2238                         sizeof(wsi->u.http.multipart_content_type) - 1);
2239                 wsi->u.http.multipart_content_type[
2240                          sizeof(wsi->u.http.multipart_content_type) - 1] = '\0';
2241                 if (lws_add_http_header_by_token(wsi, WSI_TOKEN_HTTP_CONTENT_TYPE,
2242                                                  (unsigned char *)"multipart/byteranges; boundary=_lws",
2243                                                  20, &p, end))
2244                         return -1;
2245
2246                 /*
2247                  *  our overall content length has to include
2248                  *
2249                  *  - (n + 1) x "_lws\r\n"
2250                  *  - n x Content-Type: xxx/xxx\r\n
2251                  *  - n x Content-Range: bytes xxx-yyy/zzz\r\n
2252                  *  - n x /r/n
2253                  *  - the actual payloads (aggregated in rp->agg)
2254                  *
2255                  *  Precompute it for the main response header
2256                  */
2257
2258                 computed_total_content_length = (unsigned long)rp->agg +
2259                                                 6 /* final _lws\r\n */;
2260
2261                 lws_ranges_reset(rp);
2262                 while (lws_ranges_next(rp)) {
2263                         n = lws_snprintf(cache_control, sizeof(cache_control),
2264                                         "bytes %llu-%llu/%llu",
2265                                         rp->start, rp->end, rp->extent);
2266
2267                         computed_total_content_length +=
2268                                         6 /* header _lws\r\n */ +
2269                                         14 + strlen(content_type) + 2 + /* Content-Type: xxx/xxx\r\n */
2270                                         15 + n + 2 + /* Content-Range: xxxx\r\n */
2271                                         2; /* /r/n */
2272                 }
2273
2274                 lws_ranges_reset(rp);
2275                 lws_ranges_next(rp);
2276         }
2277
2278         if (ranges == 1) {
2279                 computed_total_content_length = (unsigned long)rp->agg;
2280                 n = lws_snprintf(cache_control, sizeof(cache_control), "bytes %llu-%llu/%llu",
2281                                 rp->start, rp->end, rp->extent);
2282
2283                 if (lws_add_http_header_by_token(wsi, WSI_TOKEN_HTTP_CONTENT_RANGE,
2284                                                  (unsigned char *)cache_control,
2285                                                  n, &p, end))
2286                         return -1;
2287         }
2288
2289         wsi->u.http.range.inside = 0;
2290
2291         if (lws_add_http_header_by_token(wsi, WSI_TOKEN_HTTP_ACCEPT_RANGES,
2292                                          (unsigned char *)"bytes", 5, &p, end))
2293                 return -1;
2294 #endif
2295
2296         if (!wsi->sending_chunked) {
2297                 if (lws_add_http_header_content_length(wsi,
2298                                                        computed_total_content_length,
2299                                                        &p, end))
2300                         return -1;
2301         } else {
2302                 if (lws_add_http_header_by_token(wsi, WSI_TOKEN_HTTP_TRANSFER_ENCODING,
2303                                                  (unsigned char *)"chunked",
2304                                                  7, &p, end))
2305                         return -1;
2306         }
2307
2308         if (wsi->cache_secs && wsi->cache_reuse) {
2309                 if (wsi->cache_revalidate) {
2310                         cc = cache_control;
2311                         cclen = sprintf(cache_control, "%s max-age: %u",
2312                                     intermediates[wsi->cache_intermediaries],
2313                                     wsi->cache_secs);
2314                 } else {
2315                         cc = "no-cache";
2316                         cclen = 8;
2317                 }
2318         }
2319
2320         if (lws_add_http_header_by_token(wsi, WSI_TOKEN_HTTP_CACHE_CONTROL,
2321                         (unsigned char *)cc, cclen, &p, end))
2322                 return -1;
2323
2324         if (wsi->u.http.connection_type == HTTP_CONNECTION_KEEP_ALIVE)
2325                 if (lws_add_http_header_by_token(wsi, WSI_TOKEN_CONNECTION,
2326                                 (unsigned char *)"keep-alive", 10, &p, end))
2327                         return -1;
2328
2329         if (other_headers) {
2330                 if ((end - p) < other_headers_len)
2331                         return -1;
2332                 memcpy(p, other_headers, other_headers_len);
2333                 p += other_headers_len;
2334         }
2335
2336         if (lws_finalize_http_header(wsi, &p, end))
2337                 return -1;
2338
2339         ret = lws_write(wsi, response, p - response, LWS_WRITE_HTTP_HEADERS);
2340         if (ret != (p - response)) {
2341                 lwsl_err("_write returned %d from %ld\n", ret, (long)(p - response));
2342                 return -1;
2343         }
2344
2345         wsi->u.http.filepos = 0;
2346         wsi->state = LWSS_HTTP_ISSUING_FILE;
2347
2348         return lws_serve_http_file_fragment(wsi);
2349 }
2350
2351 int
2352 lws_interpret_incoming_packet(struct lws *wsi, unsigned char **buf, size_t len)
2353 {
2354         int m;
2355
2356         lwsl_parser("%s: received %d byte packet\n", __func__, (int)len);
2357 #if 0
2358         lwsl_hexdump(*buf, len);
2359 #endif
2360
2361         /* let the rx protocol state machine have as much as it needs */
2362
2363         while (len) {
2364                 /*
2365                  * we were accepting input but now we stopped doing so
2366                  */
2367                 if (!(wsi->rxflow_change_to & LWS_RXFLOW_ALLOW)) {
2368                         lws_rxflow_cache(wsi, *buf, 0, len);
2369                         lwsl_parser("%s: cached %ld\n", __func__, (long)len);
2370                         return 1;
2371                 }
2372
2373                 if (wsi->u.ws.rx_draining_ext) {
2374                         m = lws_rx_sm(wsi, 0);
2375                         if (m < 0)
2376                                 return -1;
2377                         continue;
2378                 }
2379
2380                 /* account for what we're using in rxflow buffer */
2381                 if (wsi->rxflow_buffer)
2382                         wsi->rxflow_pos++;
2383
2384                 /* consume payload bytes efficiently */
2385                 if (wsi->lws_rx_parse_state ==
2386                     LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED) {
2387                         m = lws_payload_until_length_exhausted(wsi, buf, &len);
2388                         if (wsi->rxflow_buffer)
2389                                 wsi->rxflow_pos += m;
2390                 }
2391
2392                 /* process the byte */
2393                 m = lws_rx_sm(wsi, *(*buf)++);
2394                 if (m < 0)
2395                         return -1;
2396                 len--;
2397         }
2398
2399         lwsl_parser("%s: exit with %d unused\n", __func__, (int)len);
2400
2401         return 0;
2402 }
2403
2404 LWS_VISIBLE void
2405 lws_server_get_canonical_hostname(struct lws_context *context,
2406                                   struct lws_context_creation_info *info)
2407 {
2408         if (lws_check_opt(info->options, LWS_SERVER_OPTION_SKIP_SERVER_CANONICAL_NAME))
2409                 return;
2410 #if LWS_POSIX
2411         /* find canonical hostname */
2412         gethostname((char *)context->canonical_hostname,
2413                     sizeof(context->canonical_hostname) - 1);
2414
2415         lwsl_notice(" canonical_hostname = %s\n", context->canonical_hostname);
2416 #else
2417         (void)context;
2418 #endif
2419 }
2420
2421 #define LWS_MAX_ELEM_NAME 32
2422
2423 enum urldecode_stateful {
2424         US_NAME,
2425         US_IDLE,
2426         US_PC1,
2427         US_PC2,
2428
2429         MT_LOOK_BOUND_IN,
2430         MT_HNAME,
2431         MT_DISP,
2432         MT_TYPE,
2433         MT_IGNORE1,
2434         MT_IGNORE2,
2435 };
2436
2437 static const char * const mp_hdr[] = {
2438         "content-disposition: ",
2439         "content-type: ",
2440         "\x0d\x0a"
2441 };
2442
2443 typedef int (*lws_urldecode_stateful_cb)(void *data,
2444                 const char *name, char **buf, int len, int final);
2445
2446 struct lws_urldecode_stateful {
2447         char *out;
2448         void *data;
2449         char name[LWS_MAX_ELEM_NAME];
2450         char temp[LWS_MAX_ELEM_NAME];
2451         char content_type[32];
2452         char content_disp[32];
2453         char content_disp_filename[256];
2454         char mime_boundary[128];
2455         int out_len;
2456         int pos;
2457         int hdr_idx;
2458         int mp;
2459         int sum;
2460
2461         unsigned int multipart_form_data:1;
2462         unsigned int inside_quote:1;
2463         unsigned int subname:1;
2464         unsigned int boundary_real_crlf:1;
2465
2466         enum urldecode_stateful state;
2467
2468         lws_urldecode_stateful_cb output;
2469 };
2470
2471 static struct lws_urldecode_stateful *
2472 lws_urldecode_s_create(struct lws *wsi, char *out, int out_len, void *data,
2473                        lws_urldecode_stateful_cb output)
2474 {
2475         struct lws_urldecode_stateful *s = lws_zalloc(sizeof(*s));
2476         char buf[200], *p;
2477         int m = 0;
2478
2479         if (!s)
2480                 return NULL;
2481
2482         s->out = out;
2483         s->out_len  = out_len;
2484         s->output = output;
2485         s->pos = 0;
2486         s->sum = 0;
2487         s->mp = 0;
2488         s->state = US_NAME;
2489         s->name[0] = '\0';
2490         s->data = data;
2491
2492         if (lws_hdr_copy(wsi, buf, sizeof(buf), WSI_TOKEN_HTTP_CONTENT_TYPE) > 0) {
2493                 /* multipart/form-data; boundary=----WebKitFormBoundarycc7YgAPEIHvgE9Bf */
2494
2495                 if (!strncmp(buf, "multipart/form-data", 19)) {
2496                         s->multipart_form_data = 1;
2497                         s->state = MT_LOOK_BOUND_IN;
2498                         s->mp = 2;
2499                         p = strstr(buf, "boundary=");
2500                         if (p) {
2501                                 p += 9;
2502                                 s->mime_boundary[m++] = '\x0d';
2503                                 s->mime_boundary[m++] = '\x0a';
2504                                 s->mime_boundary[m++] = '-';
2505                                 s->mime_boundary[m++] = '-';
2506                                 while (m < sizeof(s->mime_boundary) - 1 &&
2507                                        *p && *p != ' ')
2508                                         s->mime_boundary[m++] = *p++;
2509
2510                                 s->mime_boundary[m] = '\0';
2511
2512                                 lwsl_notice("boundary '%s'\n", s->mime_boundary);
2513                         }
2514                 }
2515         }
2516
2517         return s;
2518 }
2519
2520 static int
2521 lws_urldecode_s_process(struct lws_urldecode_stateful *s, const char *in, int len)
2522 {
2523         int n, m, hit = 0;
2524         char c, was_end = 0;
2525
2526         while (len--) {
2527                 if (s->pos == s->out_len - s->mp - 1) {
2528                         if (s->output(s->data, s->name, &s->out, s->pos, 0))
2529                                 return -1;
2530
2531                         was_end = s->pos;
2532                         s->pos = 0;
2533                 }
2534                 switch (s->state) {
2535
2536                 /* states for url arg style */
2537
2538                 case US_NAME:
2539                         s->inside_quote = 0;
2540                         if (*in == '=') {
2541                                 s->name[s->pos] = '\0';
2542                                 s->pos = 0;
2543                                 s->state = US_IDLE;
2544                                 in++;
2545                                 continue;
2546                         }
2547                         if (*in == '&') {
2548                                 s->name[s->pos] = '\0';
2549                                 if (s->output(s->data, s->name, &s->out, s->pos, 1))
2550                                         return -1;
2551                                 s->pos = 0;
2552                                 s->state = US_IDLE;
2553                                 in++;
2554                                 continue;
2555                         }
2556                         if (s->pos >= sizeof(s->name) - 1) {
2557                                 lwsl_notice("Name too long\n");
2558                                 return -1;
2559                         }
2560                         s->name[s->pos++] = *in++;
2561                         break;
2562                 case US_IDLE:
2563                         if (*in == '%') {
2564                                 s->state++;
2565                                 in++;
2566                                 continue;
2567                         }
2568                         if (*in == '&') {
2569                                 s->out[s->pos] = '\0';
2570                                 if (s->output(s->data, s->name, &s->out, s->pos, 1))
2571                                         return -1;
2572                                 s->pos = 0;
2573                                 s->state = US_NAME;
2574                                 in++;
2575                                 continue;
2576                         }
2577                         if (*in == '+') {
2578                                 in++;
2579                                 s->out[s->pos++] = ' ';
2580                                 continue;
2581                         }
2582                         s->out[s->pos++] = *in++;
2583                         break;
2584                 case US_PC1:
2585                         n = char_to_hex(*in);
2586                         if (n < 0)
2587                                 return -1;
2588
2589                         in++;
2590                         s->sum = n << 4;
2591                         s->state++;
2592                         break;
2593
2594                 case US_PC2:
2595                         n = char_to_hex(*in);
2596                         if (n < 0)
2597                                 return -1;
2598
2599                         in++;
2600                         s->out[s->pos++] = s->sum | n;
2601                         s->state = US_IDLE;
2602                         break;
2603
2604
2605                 /* states for multipart / mime style */
2606
2607                 case MT_LOOK_BOUND_IN:
2608 retry_as_first:
2609                         if (*in == s->mime_boundary[s->mp] &&
2610                             s->mime_boundary[s->mp]) {
2611                                 in++;
2612                                 s->mp++;
2613                                 if (!s->mime_boundary[s->mp]) {
2614                                         s->mp = 0;
2615                                         s->state = MT_IGNORE1;
2616
2617                                         if (s->pos || was_end)
2618                                                 if (s->output(s->data, s->name,
2619                                                       &s->out, s->pos, 1))
2620                                                         return -1;
2621
2622                                         s->pos = 0;
2623
2624                                         s->content_disp[0] = '\0';
2625                                         s->name[0] = '\0';
2626                                         s->content_disp_filename[0] = '\0';
2627                                         s->boundary_real_crlf = 1;
2628                                 }
2629                                 continue;
2630                         }
2631                         if (s->mp) {
2632                                 n = 0;
2633                                 if (!s->boundary_real_crlf)
2634                                         n = 2;
2635
2636                                 memcpy(s->out + s->pos, s->mime_boundary + n, s->mp - n);
2637                                 s->pos += s->mp;
2638                                 s->mp = 0;
2639                                 goto retry_as_first;
2640                         }
2641
2642                         s->out[s->pos++] = *in;
2643                         in++;
2644                         s->mp = 0;
2645                         break;
2646
2647                 case MT_HNAME:
2648                         m = 0;
2649                         c =*in;
2650                         if (c >= 'A' && c <= 'Z')
2651                                 c += 'a' - 'A';
2652                         for (n = 0; n < ARRAY_SIZE(mp_hdr); n++)
2653                                 if (c == mp_hdr[n][s->mp]) {
2654                                         m++;
2655                                         hit = n;
2656                                 }
2657                         in++;
2658                         if (!m) {
2659                                 s->mp = 0;
2660                                 continue;
2661                         }
2662
2663                         s->mp++;
2664                         if (m != 1)
2665                                 continue;
2666
2667                         if (mp_hdr[hit][s->mp])
2668                                 continue;
2669
2670                         s->mp = 0;
2671                         s->temp[0] = '\0';
2672                         s->subname = 0;
2673
2674                         if (hit == 2)
2675                                 s->state = MT_LOOK_BOUND_IN;
2676                         else
2677                                 s->state += hit + 1;
2678                         break;
2679
2680                 case MT_DISP:
2681                         /* form-data; name="file"; filename="t.txt" */
2682
2683                         if (*in == '\x0d') {
2684 //                              lwsl_notice("disp: '%s', '%s', '%s'\n",
2685 //                                 s->content_disp, s->name,
2686 //                                 s->content_disp_filename);
2687
2688                                 if (s->content_disp_filename[0])
2689                                         if (s->output(s->data, s->name,
2690                                                       &s->out, s->pos, LWS_UFS_OPEN))
2691                                                 return -1;
2692                                 s->state = MT_IGNORE2;
2693                                 goto done;
2694                         }
2695                         if (*in == ';') {
2696                                 s->subname = 1;
2697                                 s->temp[0] = '\0';
2698                                 s->mp = 0;
2699                                 goto done;
2700                         }
2701
2702                         if (*in == '\"') {
2703                                 s->inside_quote ^= 1;
2704                                 goto done;
2705                         }
2706
2707                         if (s->subname) {
2708                                 if (*in == '=') {
2709                                         s->temp[s->mp] = '\0';
2710                                         s->subname = 0;
2711                                         s->mp = 0;
2712                                         goto done;
2713                                 }
2714                                 if (s->mp < sizeof(s->temp) - 1 &&
2715                                     (*in != ' ' || s->inside_quote))
2716                                         s->temp[s->mp++] = *in;
2717                                 goto done;
2718                         }
2719
2720                         if (!s->temp[0]) {
2721                                 if (s->mp < sizeof(s->content_disp) - 1)
2722                                         s->content_disp[s->mp++] = *in;
2723                                 s->content_disp[s->mp] = '\0';
2724                                 goto done;
2725                         }
2726
2727                         if (!strcmp(s->temp, "name")) {
2728                                 if (s->mp < sizeof(s->name) - 1)
2729                                         s->name[s->mp++] = *in;
2730                                 s->name[s->mp] = '\0';
2731                                 goto done;
2732                         }
2733
2734                         if (!strcmp(s->temp, "filename")) {
2735                                 if (s->mp < sizeof(s->content_disp_filename) - 1)
2736                                         s->content_disp_filename[s->mp++] = *in;
2737                                 s->content_disp_filename[s->mp] = '\0';
2738                                 goto done;
2739                         }
2740 done:
2741                         in++;
2742                         break;
2743
2744                 case MT_TYPE:
2745                         if (*in == '\x0d')
2746                                 s->state = MT_IGNORE2;
2747                         else {
2748                                 if (s->mp < sizeof(s->content_type) - 1)
2749                                         s->content_type[s->mp++] = *in;
2750                                 s->content_type[s->mp] = '\0';
2751                         }
2752                         in++;
2753                         break;
2754
2755                 case MT_IGNORE1:
2756                         if (*in == '\x0d')
2757                                 s->state = MT_IGNORE2;
2758                         in++;
2759                         break;
2760
2761                 case MT_IGNORE2:
2762                         s->mp = 0;
2763                         if (*in == '\x0a')
2764                                 s->state = MT_HNAME;
2765                         in++;
2766                         break;
2767                 }
2768         }
2769
2770         return 0;
2771 }
2772
2773 static int
2774 lws_urldecode_s_destroy(struct lws_urldecode_stateful *s)
2775 {
2776         int ret = 0;
2777
2778         if (s->state != US_IDLE)
2779                 ret = -1;
2780
2781         if (!ret)
2782                 if (s->output(s->data, s->name, &s->out, s->pos, 1))
2783                         ret = -1;
2784
2785         lws_free(s);
2786
2787         return ret;
2788 }
2789
2790 struct lws_spa {
2791         struct lws_urldecode_stateful *s;
2792         lws_spa_fileupload_cb opt_cb;
2793         const char * const *param_names;
2794         int count_params;
2795         char **params;
2796         int *param_length;
2797         void *opt_data;
2798
2799         char *storage;
2800         char *end;
2801         int max_storage;
2802 };
2803
2804 static int
2805 lws_urldecode_spa_lookup(struct lws_spa *spa,
2806                          const char *name)
2807 {
2808         int n;
2809
2810         for (n = 0; n < spa->count_params; n++)
2811                 if (!strcmp(spa->param_names[n], name))
2812                         return n;
2813
2814         return -1;
2815 }
2816
2817 static int
2818 lws_urldecode_spa_cb(void *data, const char *name, char **buf, int len,
2819                      int final)
2820 {
2821         struct lws_spa *spa =
2822                         (struct lws_spa *)data;
2823         int n;
2824
2825         if (spa->s->content_disp_filename[0]) {
2826                 if (spa->opt_cb) {
2827                         n = spa->opt_cb(spa->opt_data, name,
2828                                         spa->s->content_disp_filename,
2829                                         *buf, len, final);
2830
2831                         if (n < 0)
2832                                 return -1;
2833                 }
2834                 return 0;
2835         }
2836         n = lws_urldecode_spa_lookup(spa, name);
2837
2838         if (n == -1 || !len) /* unrecognized */
2839                 return 0;
2840
2841         if (!spa->params[n])
2842                 spa->params[n] = *buf;
2843
2844         if ((*buf) + len >= spa->end) {
2845                 lwsl_notice("%s: exceeded storage\n", __func__);
2846                 return -1;
2847         }
2848
2849         spa->param_length[n] += len;
2850
2851         /* move it on inside storage */
2852         (*buf) += len;
2853         *((*buf)++) = '\0';
2854
2855         spa->s->out_len -= len + 1;
2856
2857         return 0;
2858 }
2859
2860 LWS_VISIBLE LWS_EXTERN struct lws_spa *
2861 lws_spa_create(struct lws *wsi, const char * const *param_names,
2862                          int count_params, int max_storage,
2863                          lws_spa_fileupload_cb opt_cb, void *opt_data)
2864 {
2865         struct lws_spa *spa = lws_zalloc(sizeof(*spa));
2866
2867         if (!spa)
2868                 return NULL;
2869
2870         spa->param_names = param_names;
2871         spa->count_params = count_params;
2872         spa->max_storage = max_storage;
2873         spa->opt_cb = opt_cb;
2874         spa->opt_data = opt_data;
2875
2876         spa->storage = lws_malloc(max_storage);
2877         if (!spa->storage)
2878                 goto bail2;
2879         spa->end = spa->storage + max_storage - 1;
2880
2881         spa->params = lws_zalloc(sizeof(char *) * count_params);
2882         if (!spa->params)
2883                 goto bail3;
2884
2885         spa->s = lws_urldecode_s_create(wsi, spa->storage, max_storage, spa,
2886                                         lws_urldecode_spa_cb);
2887         if (!spa->s)
2888                 goto bail4;
2889
2890         spa->param_length = lws_zalloc(sizeof(int) * count_params);
2891         if (!spa->param_length)
2892                 goto bail5;
2893
2894         lwsl_notice("%s: Created SPA %p\n", __func__, spa);
2895
2896         return spa;
2897
2898 bail5:
2899         lws_urldecode_s_destroy(spa->s);
2900 bail4:
2901         lws_free(spa->params);
2902 bail3:
2903         lws_free(spa->storage);
2904 bail2:
2905         lws_free(spa);
2906
2907         return NULL;
2908 }
2909
2910 LWS_VISIBLE LWS_EXTERN int
2911 lws_spa_process(struct lws_spa *ludspa, const char *in, int len)
2912 {
2913         if (!ludspa) {
2914                 lwsl_err("%s: NULL spa\n", __func__);
2915                 return -1;
2916         }
2917         return lws_urldecode_s_process(ludspa->s, in, len);
2918 }
2919
2920 LWS_VISIBLE LWS_EXTERN int
2921 lws_spa_get_length(struct lws_spa *ludspa, int n)
2922 {
2923         if (n >= ludspa->count_params)
2924                 return 0;
2925
2926         return ludspa->param_length[n];
2927 }
2928
2929 LWS_VISIBLE LWS_EXTERN const char *
2930 lws_spa_get_string(struct lws_spa *ludspa, int n)
2931 {
2932         if (n >= ludspa->count_params)
2933                 return NULL;
2934
2935         return ludspa->params[n];
2936 }
2937
2938 LWS_VISIBLE LWS_EXTERN int
2939 lws_spa_finalize(struct lws_spa *spa)
2940 {
2941         if (spa->s) {
2942                 lws_urldecode_s_destroy(spa->s);
2943                 spa->s = NULL;
2944         }
2945
2946         return 0;
2947 }
2948
2949 LWS_VISIBLE LWS_EXTERN int
2950 lws_spa_destroy(struct lws_spa *spa)
2951 {
2952         int n = 0;
2953
2954         lwsl_notice("%s: destroy spa %p\n", __func__, spa);
2955
2956         if (spa->s)
2957                 lws_urldecode_s_destroy(spa->s);
2958
2959         lwsl_debug("%s\n", __func__);
2960
2961         lws_free(spa->param_length);
2962         lws_free(spa->params);
2963         lws_free(spa->storage);
2964         lws_free(spa);
2965
2966         return n;
2967 }
2968
2969 LWS_VISIBLE LWS_EXTERN int
2970 lws_chunked_html_process(struct lws_process_html_args *args,
2971                          struct lws_process_html_state *s)
2972 {
2973         char *sp, buffer[32];
2974         const char *pc;
2975         int old_len, n;
2976
2977         /* do replacements */
2978         sp = args->p;
2979         old_len = args->len;
2980         args->len = 0;
2981         s->start = sp;
2982         while (sp < args->p + old_len) {
2983
2984                 if (args->len + 7 >= args->max_len) {
2985                         lwsl_err("Used up interpret padding\n");
2986                         return -1;
2987                 }
2988
2989                 if ((!s->pos && *sp == '$') || s->pos) {
2990                         int hits = 0, hit = 0;
2991
2992                         if (!s->pos)
2993                                 s->start = sp;
2994                         s->swallow[s->pos++] = *sp;
2995                         if (s->pos == sizeof(s->swallow) - 1)
2996                                 goto skip;
2997                         for (n = 0; n < s->count_vars; n++)
2998                                 if (!strncmp(s->swallow, s->vars[n], s->pos)) {
2999                                         hits++;
3000                                         hit = n;
3001                                 }
3002                         if (!hits) {
3003 skip:
3004                                 s->swallow[s->pos] = '\0';
3005                                 memcpy(s->start, s->swallow, s->pos);
3006                                 args->len++;
3007                                 s->pos = 0;
3008                                 sp = s->start + 1;
3009                                 continue;
3010                         }
3011                         if (hits == 1 && s->pos == strlen(s->vars[hit])) {
3012                                 pc = s->replace(s->data, hit);
3013                                 if (!pc)
3014                                         pc = "NULL";
3015                                 n = strlen(pc);
3016                                 s->swallow[s->pos] = '\0';
3017                                 if (n != s->pos) {
3018                                         memmove(s->start + n,
3019                                                 s->start + s->pos,
3020                                                 old_len - (sp - args->p));
3021                                         old_len += (n - s->pos) + 1;
3022                                 }
3023                                 memcpy(s->start, pc, n);
3024                                 args->len++;
3025                                 sp = s->start + 1;
3026
3027                                 s->pos = 0;
3028                         }
3029                         sp++;
3030                         continue;
3031                 }
3032
3033                 args->len++;
3034                 sp++;
3035         }
3036
3037         /* no space left for final chunk trailer */
3038         if (args->final && args->len + 7 >= args->max_len)
3039                 return -1;
3040
3041         n = sprintf(buffer, "%X\x0d\x0a", args->len);
3042
3043         args->p -= n;
3044         memcpy(args->p, buffer, n);
3045         args->len += n;
3046
3047         if (args->final) {
3048                 sp = args->p + args->len;
3049                 *sp++ = '\x0d';
3050                 *sp++ = '\x0a';
3051                 *sp++ = '0';
3052                 *sp++ = '\x0d';
3053                 *sp++ = '\x0a';
3054                 *sp++ = '\x0d';
3055                 *sp++ = '\x0a';
3056                 args->len += 7;
3057         } else {
3058                 sp = args->p + args->len;
3059                 *sp++ = '\x0d';
3060                 *sp++ = '\x0a';
3061                 args->len += 2;
3062         }
3063
3064         return 0;
3065 }