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