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