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