uri processing reject paths not starting with slash
[platform/upstream/libwebsockets.git] / test-server / test-server-http.c
1 /*
2  * libwebsockets-test-server - libwebsockets test implementation
3  *
4  * Copyright (C) 2010-2016 Andy Green <andy@warmcat.com>
5  *
6  * This file is made available under the Creative Commons CC0 1.0
7  * Universal Public Domain Dedication.
8  *
9  * The person who associated a work with this deed has dedicated
10  * the work to the public domain by waiving all of his or her rights
11  * to the work worldwide under copyright law, including all related
12  * and neighboring rights, to the extent allowed by law. You can copy,
13  * modify, distribute and perform the work, even for commercial purposes,
14  * all without asking permission.
15  *
16  * The test apps are intended to be adapted for use in your code, which
17  * may be proprietary.  So unlike the library itself, they are licensed
18  * Public Domain.
19  */
20 #include "test-server.h"
21
22 /*
23  * This demo server shows how to use libwebsockets for one or more
24  * websocket protocols in the same server
25  *
26  * It defines the following websocket protocols:
27  *
28  *  dumb-increment-protocol:  once the socket is opened, an incrementing
29  *                              ascii string is sent down it every 50ms.
30  *                              If you send "reset\n" on the websocket, then
31  *                              the incrementing number is reset to 0.
32  *
33  *  lws-mirror-protocol: copies any received packet to every connection also
34  *                              using this protocol, including the sender
35  */
36
37 extern int debug_level;
38
39 enum demo_protocols {
40         /* always first */
41         PROTOCOL_HTTP = 0,
42
43         PROTOCOL_DUMB_INCREMENT,
44         PROTOCOL_LWS_MIRROR,
45
46         /* always last */
47         DEMO_PROTOCOL_COUNT
48 };
49
50 /*
51  * We take a strict whitelist approach to stop ../ attacks
52  */
53 struct serveable {
54         const char *urlpath;
55         const char *mimetype;
56 };
57
58 /*
59  * this is just an example of parsing handshake headers, you don't need this
60  * in your code unless you will filter allowing connections by the header
61  * content
62  */
63 void
64 dump_handshake_info(struct lws *wsi)
65 {
66         int n = 0, len;
67         char buf[256];
68         const unsigned char *c;
69
70         do {
71                 c = lws_token_to_string(n);
72                 if (!c) {
73                         n++;
74                         continue;
75                 }
76
77                 len = lws_hdr_total_length(wsi, n);
78                 if (!len || len > sizeof(buf) - 1) {
79                         n++;
80                         continue;
81                 }
82
83                 lws_hdr_copy(wsi, buf, sizeof buf, n);
84                 buf[sizeof(buf) - 1] = '\0';
85
86                 fprintf(stderr, "    %s = %s\n", (char *)c, buf);
87                 n++;
88         } while (c);
89 }
90
91 const char * get_mimetype(const char *file)
92 {
93         int n = strlen(file);
94
95         if (n < 5)
96                 return NULL;
97
98         if (!strcmp(&file[n - 4], ".ico"))
99                 return "image/x-icon";
100
101         if (!strcmp(&file[n - 4], ".png"))
102                 return "image/png";
103
104         if (!strcmp(&file[n - 5], ".html"))
105                 return "text/html";
106
107         if (!strcmp(&file[n - 4], ".css"))
108                 return "text/css";
109
110         return NULL;
111 }
112
113 /* this protocol server (always the first one) handles HTTP,
114  *
115  * Some misc callbacks that aren't associated with a protocol also turn up only
116  * here on the first protocol server.
117  */
118
119 int callback_http(struct lws *wsi, enum lws_callback_reasons reason, void *user,
120                   void *in, size_t len)
121 {
122         struct per_session_data__http *pss =
123                         (struct per_session_data__http *)user;
124         unsigned char buffer[4096 + LWS_PRE];
125         unsigned long amount, file_len, sent;
126         char leaf_path[1024];
127         const char *mimetype;
128         char *other_headers;
129         unsigned char *end;
130         struct timeval tv;
131         unsigned char *p;
132 #ifndef LWS_NO_CLIENT
133         struct per_session_data__http *pss1;
134         struct lws *wsi1;
135 #endif
136         char buf[256];
137         char b64[64];
138         int n, m;
139 #ifdef EXTERNAL_POLL
140         struct lws_pollargs *pa = (struct lws_pollargs *)in;
141 #endif
142
143         switch (reason) {
144         case LWS_CALLBACK_HTTP:
145
146                 if (debug_level & LLL_INFO) {
147                         dump_handshake_info(wsi);
148
149                         /* dump the individual URI Arg parameters */
150                         n = 0;
151                         while (lws_hdr_copy_fragment(wsi, buf, sizeof(buf),
152                                                      WSI_TOKEN_HTTP_URI_ARGS, n) > 0) {
153                                 lwsl_notice("URI Arg %d: %s\n", ++n, buf);
154                         }
155                 }
156
157                 {
158                         char name[100], rip[50];
159                         lws_get_peer_addresses(wsi, lws_get_socket_fd(wsi), name,
160                                                sizeof(name), rip, sizeof(rip));
161                         sprintf(buf, "%s (%s)", name, rip);
162                         lwsl_notice("HTTP connect from %s\n", buf);
163                 }
164
165                 if (len < 1) {
166                         lws_return_http_status(wsi,
167                                                 HTTP_STATUS_BAD_REQUEST, NULL);
168                         goto try_to_reuse;
169                 }
170
171 #ifndef LWS_NO_CLIENT
172                 if (!strncmp(in, "/proxytest", 10)) {
173                         struct lws_client_connect_info i;
174                         char *rootpath = "/";
175                         const char *p = (const char *)in;
176
177                         if (lws_get_child(wsi))
178                                 break;
179
180                         pss->client_finished = 0;
181                         memset(&i,0, sizeof(i));
182                         i.context = lws_get_context(wsi);
183                         i.address = "git.libwebsockets.org";
184                         i.port = 80;
185                         i.ssl_connection = 0;
186                         if (p[10])
187                                 i.path = (char *)in + 10;
188                         else
189                                 i.path = rootpath;
190                         i.host = "git.libwebsockets.org";
191                         i.origin = NULL;
192                         i.method = "GET";
193                         i.parent_wsi = wsi;
194                         i.uri_replace_from = "git.libwebsockets.org/";
195                         i.uri_replace_to = "/proxytest/";
196                         if (!lws_client_connect_via_info(&i)) {
197                                 lwsl_err("proxy connect fail\n");
198                                 break;
199                         }
200
201
202
203                         break;
204                 }
205 #endif
206
207 #if 1
208                 /* this example server has no concept of directories */
209                 if (strchr((const char *)in + 1, '/')) {
210                         lws_return_http_status(wsi, HTTP_STATUS_FORBIDDEN, NULL);
211                         goto try_to_reuse;
212                 }
213 #endif
214
215 #ifdef LWS_WITH_CGI
216                 if (!strncmp(in, "/cgitest", 8)) {
217                         static char *cmd[] = {
218                                 "/bin/sh",
219                                 "-c",
220                                 INSTALL_DATADIR"/libwebsockets-test-server/lws-cgi-test.sh",
221 //                              "/var/www/cgi-bin/cgit",
222                                 NULL
223                         };
224
225                         lwsl_notice("%s: cgitest\n", __func__);
226                         n = lws_cgi(wsi, cmd, 8, 5);
227                         if (n) {
228                                 lwsl_err("%s: cgi failed\n");
229                                 return -1;
230                         }
231                         p = buffer + LWS_PRE;
232                         end = p + sizeof(buffer) - LWS_PRE;
233
234                         if (lws_add_http_header_status(wsi, 200, &p, end))
235                                 return 1;
236                         if (lws_add_http_header_by_token(wsi, WSI_TOKEN_CONNECTION,
237                                         (unsigned char *)"close", 5, &p, end))
238                                 return 1;
239                         n = lws_write(wsi, buffer + LWS_PRE,
240                                       p - (buffer + LWS_PRE),
241                                       LWS_WRITE_HTTP_HEADERS);
242
243                         /* the cgi starts by outputting headers, we can't
244                          *  finalize the headers until we see the end of that
245                          */
246
247                         break;
248                 }
249 #endif
250
251                 /* if a legal POST URL, let it continue and accept data */
252                 if (lws_hdr_total_length(wsi, WSI_TOKEN_POST_URI))
253                         return 0;
254
255                 /* check for the "send a big file by hand" example case */
256
257                 if (!strcmp((const char *)in, "/leaf.jpg")) {
258                         if (strlen(resource_path) > sizeof(leaf_path) - 10)
259                                 return -1;
260                         sprintf(leaf_path, "%s/leaf.jpg", resource_path);
261
262                         /* well, let's demonstrate how to send the hard way */
263
264                         p = buffer + LWS_PRE;
265                         end = p + sizeof(buffer) - LWS_PRE;
266
267                         pss->fd = lws_plat_file_open(wsi, leaf_path, &file_len,
268                                                      LWS_O_RDONLY);
269
270                         if (pss->fd == LWS_INVALID_FILE) {
271                                 lwsl_err("faild to open file %s\n", leaf_path);
272                                 return -1;
273                         }
274
275                         /*
276                          * we will send a big jpeg file, but it could be
277                          * anything.  Set the Content-Type: appropriately
278                          * so the browser knows what to do with it.
279                          *
280                          * Notice we use the APIs to build the header, which
281                          * will do the right thing for HTTP 1/1.1 and HTTP2
282                          * depending on what connection it happens to be working
283                          * on
284                          */
285                         if (lws_add_http_header_status(wsi, 200, &p, end))
286                                 return 1;
287                         if (lws_add_http_header_by_token(wsi, WSI_TOKEN_HTTP_SERVER,
288                                         (unsigned char *)"libwebsockets",
289                                         13, &p, end))
290                                 return 1;
291                         if (lws_add_http_header_by_token(wsi,
292                                         WSI_TOKEN_HTTP_CONTENT_TYPE,
293                                         (unsigned char *)"image/jpeg",
294                                         10, &p, end))
295                                 return 1;
296                         if (lws_add_http_header_content_length(wsi,
297                                                                file_len, &p,
298                                                                end))
299                                 return 1;
300                         if (lws_finalize_http_header(wsi, &p, end))
301                                 return 1;
302
303                         /*
304                          * send the http headers...
305                          * this won't block since it's the first payload sent
306                          * on the connection since it was established
307                          * (too small for partial)
308                          *
309                          * Notice they are sent using LWS_WRITE_HTTP_HEADERS
310                          * which also means you can't send body too in one step,
311                          * this is mandated by changes in HTTP2
312                          */
313
314                         *p = '\0';
315                         lwsl_info("%s\n", buffer + LWS_PRE);
316
317                         n = lws_write(wsi, buffer + LWS_PRE,
318                                       p - (buffer + LWS_PRE),
319                                       LWS_WRITE_HTTP_HEADERS);
320                         if (n < 0) {
321                                 lws_plat_file_close(wsi, pss->fd);
322                                 return -1;
323                         }
324                         /*
325                          * book us a LWS_CALLBACK_HTTP_WRITEABLE callback
326                          */
327                         lws_callback_on_writable(wsi);
328                         break;
329                 }
330
331                 /* if not, send a file the easy way */
332                 if (!strncmp(in, "/cgit-data/", 11)) {
333                         in = (char *)in + 11;
334                         strcpy(buf, "/usr/share/cgit");
335                 } else
336                         strcpy(buf, resource_path);
337
338                 if (strcmp(in, "/")) {
339                         if (*((const char *)in) != '/')
340                                 strcat(buf, "/");
341                         strncat(buf, in, sizeof(buf) - strlen(buf) - 1);
342                 } else /* default file to serve */
343                         strcat(buf, "/test.html");
344                 buf[sizeof(buf) - 1] = '\0';
345
346                 /* refuse to serve files we don't understand */
347                 mimetype = get_mimetype(buf);
348                 if (!mimetype) {
349                         lwsl_err("Unknown mimetype for %s\n", buf);
350                         lws_return_http_status(wsi,
351                                       HTTP_STATUS_UNSUPPORTED_MEDIA_TYPE, NULL);
352                         return -1;
353                 }
354
355                 /* demonstrates how to set a cookie on / */
356
357                 other_headers = leaf_path;
358                 p = (unsigned char *)leaf_path;
359                 if (!strcmp((const char *)in, "/") &&
360                            !lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_COOKIE)) {
361                         /* this isn't very unguessable but it'll do for us */
362                         gettimeofday(&tv, NULL);
363                         n = sprintf(b64, "test=LWS_%u_%u_COOKIE;Max-Age=360000",
364                                 (unsigned int)tv.tv_sec,
365                                 (unsigned int)tv.tv_usec);
366
367                         if (lws_add_http_header_by_name(wsi,
368                                 (unsigned char *)"set-cookie:",
369                                 (unsigned char *)b64, n, &p,
370                                 (unsigned char *)leaf_path + sizeof(leaf_path)))
371                                 return 1;
372                 }
373                 if (lws_is_ssl(wsi) && lws_add_http_header_by_name(wsi,
374                                                 (unsigned char *)
375                                                 "Strict-Transport-Security:",
376                                                 (unsigned char *)
377                                                 "max-age=15768000 ; "
378                                                 "includeSubDomains", 36, &p,
379                                                 (unsigned char *)leaf_path +
380                                                         sizeof(leaf_path)))
381                         return 1;
382                 n = (char *)p - leaf_path;
383
384                 n = lws_serve_http_file(wsi, buf, mimetype, other_headers, n);
385                 if (n < 0 || ((n > 0) && lws_http_transaction_completed(wsi)))
386                         return -1; /* error or can't reuse connection: close the socket */
387
388                 /*
389                  * notice that the sending of the file completes asynchronously,
390                  * we'll get a LWS_CALLBACK_HTTP_FILE_COMPLETION callback when
391                  * it's done
392                  */
393                 break;
394
395         case LWS_CALLBACK_HTTP_BODY:
396                 strncpy(buf, in, 20);
397                 buf[20] = '\0';
398                 if (len < 20)
399                         buf[len] = '\0';
400
401                 lwsl_notice("LWS_CALLBACK_HTTP_BODY: %s... len %d\n",
402                                 (const char *)buf, (int)len);
403
404                 break;
405
406         case LWS_CALLBACK_HTTP_BODY_COMPLETION:
407                 lwsl_notice("LWS_CALLBACK_HTTP_BODY_COMPLETION\n");
408                 /* the whole of the sent body arrived, close or reuse the connection */
409                 lws_return_http_status(wsi, HTTP_STATUS_OK, NULL);
410                 goto try_to_reuse;
411
412         case LWS_CALLBACK_HTTP_FILE_COMPLETION:
413                 goto try_to_reuse;
414
415         case LWS_CALLBACK_HTTP_WRITEABLE:
416                 lwsl_info("LWS_CALLBACK_HTTP_WRITEABLE\n");
417
418                 if (pss->client_finished)
419                         return -1;
420
421                 if (pss->fd == LWS_INVALID_FILE)
422                         goto try_to_reuse;
423 #ifdef LWS_WITH_CGI
424                 if (pss->reason_bf & 1) {
425                         if (lws_cgi_write_split_stdout_headers(wsi) < 0)
426                                 goto bail;
427
428                         pss->reason_bf &= ~1;
429                         break;
430                 }
431 #endif
432 #ifndef LWS_NO_CLIENT
433                 if (pss->reason_bf & 2) {
434                         char *px = buf + LWS_PRE;
435                         int lenx = sizeof(buf) - LWS_PRE;
436                         /*
437                          * our sink is writeable and our source has something
438                          * to read.  So read a lump of source material of
439                          * suitable size to send or what's available, whichever
440                          * is the smaller.
441                          */
442                         pss->reason_bf &= ~2;
443                         wsi1 = lws_get_child(wsi);
444                         if (!wsi1)
445                                 break;
446                         if (lws_http_client_read(wsi1, &px, &lenx) < 0)
447                                 goto bail;
448
449                         if (pss->client_finished)
450                                 return -1;
451                         break;
452                 }
453 #endif
454                 /*
455                  * we can send more of whatever it is we were sending
456                  */
457                 sent = 0;
458                 do {
459                         /* we'd like the send this much */
460                         n = sizeof(buffer) - LWS_PRE;
461
462                         /* but if the peer told us he wants less, we can adapt */
463                         m = lws_get_peer_write_allowance(wsi);
464
465                         /* -1 means not using a protocol that has this info */
466                         if (m == 0)
467                                 /* right now, peer can't handle anything */
468                                 goto later;
469
470                         if (m != -1 && m < n)
471                                 /* he couldn't handle that much */
472                                 n = m;
473
474                         n = lws_plat_file_read(wsi, pss->fd,
475                                                &amount, buffer + LWS_PRE, n);
476                         /* problem reading, close conn */
477                         if (n < 0) {
478                                 lwsl_err("problem reading file\n");
479                                 goto bail;
480                         }
481                         n = (int)amount;
482                         /* sent it all, close conn */
483                         if (n == 0)
484                                 goto penultimate;
485                         /*
486                          * To support HTTP2, must take care about preamble space
487                          *
488                          * identification of when we send the last payload frame
489                          * is handled by the library itself if you sent a
490                          * content-length header
491                          */
492                         m = lws_write(wsi, buffer + LWS_PRE, n, LWS_WRITE_HTTP);
493                         if (m < 0) {
494                                 lwsl_err("write failed\n");
495                                 /* write failed, close conn */
496                                 goto bail;
497                         }
498                         if (m) /* while still active, extend timeout */
499                                 lws_set_timeout(wsi, PENDING_TIMEOUT_HTTP_CONTENT, 5);
500                         sent += m;
501
502                 } while (!lws_send_pipe_choked(wsi) && (sent < 1024 * 1024));
503 later:
504                 lws_callback_on_writable(wsi);
505                 break;
506 penultimate:
507                 lws_plat_file_close(wsi, pss->fd);
508                 pss->fd = LWS_INVALID_FILE;
509                 goto try_to_reuse;
510
511 bail:
512                 lws_plat_file_close(wsi, pss->fd);
513
514                 return -1;
515
516         /*
517          * callback for confirming to continue with client IP appear in
518          * protocol 0 callback since no websocket protocol has been agreed
519          * yet.  You can just ignore this if you won't filter on client IP
520          * since the default unhandled callback return is 0 meaning let the
521          * connection continue.
522          */
523         case LWS_CALLBACK_FILTER_NETWORK_CONNECTION:
524                 /* if we returned non-zero from here, we kill the connection */
525                 break;
526
527 #ifndef LWS_NO_CLIENT
528         case LWS_CALLBACK_ESTABLISHED_CLIENT_HTTP: {
529                 char ctype[64], ctlen = 0;
530                 lwsl_err("LWS_CALLBACK_ESTABLISHED_CLIENT_HTTP\n");
531                 p = buffer + LWS_PRE;
532                 end = p + sizeof(buffer) - LWS_PRE;
533                 if (lws_add_http_header_status(lws_get_parent(wsi), 200, &p, end))
534                         return 1;
535                 if (lws_add_http_header_by_token(lws_get_parent(wsi),
536                                 WSI_TOKEN_HTTP_SERVER,
537                                 (unsigned char *)"libwebsockets",
538                                 13, &p, end))
539                         return 1;
540
541                 ctlen = lws_hdr_copy(wsi, ctype, sizeof(ctype), WSI_TOKEN_HTTP_CONTENT_TYPE);
542                 if (ctlen > 0) {
543                         if (lws_add_http_header_by_token(lws_get_parent(wsi),
544                                 WSI_TOKEN_HTTP_CONTENT_TYPE,
545                                 (unsigned char *)ctype, ctlen, &p, end))
546                                 return 1;
547                 }
548 #if 0
549                 if (lws_add_http_header_content_length(lws_get_parent(wsi),
550                                                        file_len, &p, end))
551                         return 1;
552 #endif
553                 if (lws_finalize_http_header(lws_get_parent(wsi), &p, end))
554                         return 1;
555
556                 *p = '\0';
557                 lwsl_info("%s\n", buffer + LWS_PRE);
558
559                 n = lws_write(lws_get_parent(wsi), buffer + LWS_PRE,
560                               p - (buffer + LWS_PRE),
561                               LWS_WRITE_HTTP_HEADERS);
562                 if (n < 0)
563                         return -1;
564
565                 break; }
566         case LWS_CALLBACK_CLOSED_CLIENT_HTTP:
567                 //lwsl_err("LWS_CALLBACK_CLOSED_CLIENT_HTTP\n");
568                 return -1;
569                 break;
570         case LWS_CALLBACK_RECEIVE_CLIENT_HTTP:
571                 //lwsl_err("LWS_CALLBACK_RECEIVE_CLIENT_HTTP: wsi %p\n", wsi);
572                 assert(lws_get_parent(wsi));
573                 if (!lws_get_parent(wsi))
574                         break;
575                 // lwsl_err("LWS_CALLBACK_RECEIVE_CLIENT_HTTP: wsi %p: sock: %d, parent_wsi: %p, parent_sock:%d,  len %d\n",
576                 //              wsi, lws_get_socket_fd(wsi),
577                 //              lws_get_parent(wsi),
578                 //              lws_get_socket_fd(lws_get_parent(wsi)), len);
579                 pss1 = lws_wsi_user(lws_get_parent(wsi));
580                 pss1->reason_bf |= 2;
581                 lws_callback_on_writable(lws_get_parent(wsi));
582                 break;
583         case LWS_CALLBACK_RECEIVE_CLIENT_HTTP_READ:
584                 //lwsl_err("LWS_CALLBACK_RECEIVE_CLIENT_HTTP_READ len %d\n", len);
585                 assert(lws_get_parent(wsi));
586                 m = lws_write(lws_get_parent(wsi), (unsigned char *)in,
587                                 len, LWS_WRITE_HTTP);
588                 if (m < 0)
589                         return -1;
590                 break;
591         case LWS_CALLBACK_COMPLETED_CLIENT_HTTP:
592                 //lwsl_err("LWS_CALLBACK_COMPLETED_CLIENT_HTTP\n");
593                 assert(lws_get_parent(wsi));
594                 if (!lws_get_parent(wsi))
595                         break;
596                 pss1 = lws_wsi_user(lws_get_parent(wsi));
597                 pss1->client_finished = 1;
598                 break;
599 #endif
600
601 #ifdef LWS_WITH_CGI
602         /* CGI IO events (POLLIN/OUT) appear here our demo user code policy is
603          *
604          *  - POST data goes on subprocess stdin
605          *  - subprocess stdout goes on http via writeable callback
606          *  - subprocess stderr goes to the logs
607          */
608         case LWS_CALLBACK_CGI:
609                 pss->args = *((struct lws_cgi_args *)in);
610                 //lwsl_notice("LWS_CALLBACK_CGI: ch %d\n", pss->args.ch);
611                 switch (pss->args.ch) { /* which of stdin/out/err ? */
612                 case LWS_STDIN:
613                         /* TBD stdin rx flow control */
614                         break;
615                 case LWS_STDOUT:
616                         pss->reason_bf |= 1;
617                         /* when writing to MASTER would not block */
618                         lws_callback_on_writable(wsi);
619                         break;
620                 case LWS_STDERR:
621                         n = read(lws_get_socket_fd(pss->args.stdwsi[LWS_STDERR]),
622                                         buf, 127);
623                         //lwsl_notice("stderr reads %d\n", n);
624                         if (n > 0) {
625                                 if (buf[n - 1] != '\n')
626                                         buf[n++] = '\n';
627                                 buf[n] = '\0';
628                                 lwsl_notice("CGI-stderr: %s\n", buf);
629                         }
630                         break;
631                 }
632                 break;
633
634         case LWS_CALLBACK_CGI_TERMINATED:
635                 //lwsl_notice("LWS_CALLBACK_CGI_TERMINATED\n");
636                 /* because we sent on openended http, close the connection */
637                 return -1;
638
639         case LWS_CALLBACK_CGI_STDIN_DATA:  /* POST body for stdin */
640                 //lwsl_notice("LWS_CALLBACK_CGI_STDIN_DATA\n");
641                 pss->args = *((struct lws_cgi_args *)in);
642                 n = write(lws_get_socket_fd(pss->args.stdwsi[LWS_STDIN]),
643                           pss->args.data, pss->args.len);
644                 //lwsl_notice("LWS_CALLBACK_CGI_STDIN_DATA: write says %d", n);
645                 if (n < pss->args.len)
646                         lwsl_notice("LWS_CALLBACK_CGI_STDIN_DATA: sent %d only %d went",
647                                         n, pss->args.len);
648                 return n;
649 #endif
650
651         /*
652          * callbacks for managing the external poll() array appear in
653          * protocol 0 callback
654          */
655
656         case LWS_CALLBACK_LOCK_POLL:
657                 /*
658                  * lock mutex to protect pollfd state
659                  * called before any other POLL related callback
660                  * if protecting wsi lifecycle change, len == 1
661                  */
662                 test_server_lock(len);
663                 break;
664
665         case LWS_CALLBACK_UNLOCK_POLL:
666                 /*
667                  * unlock mutex to protect pollfd state when
668                  * called after any other POLL related callback
669                  * if protecting wsi lifecycle change, len == 1
670                  */
671                 test_server_unlock(len);
672                 break;
673
674 #ifdef EXTERNAL_POLL
675         case LWS_CALLBACK_ADD_POLL_FD:
676
677                 if (count_pollfds >= max_poll_elements) {
678                         lwsl_err("LWS_CALLBACK_ADD_POLL_FD: too many sockets to track\n");
679                         return 1;
680                 }
681
682                 fd_lookup[pa->fd] = count_pollfds;
683                 pollfds[count_pollfds].fd = pa->fd;
684                 pollfds[count_pollfds].events = pa->events;
685                 pollfds[count_pollfds++].revents = 0;
686                 break;
687
688         case LWS_CALLBACK_DEL_POLL_FD:
689                 if (!--count_pollfds)
690                         break;
691                 m = fd_lookup[pa->fd];
692                 /* have the last guy take up the vacant slot */
693                 pollfds[m] = pollfds[count_pollfds];
694                 fd_lookup[pollfds[count_pollfds].fd] = m;
695                 break;
696
697         case LWS_CALLBACK_CHANGE_MODE_POLL_FD:
698                 pollfds[fd_lookup[pa->fd]].events = pa->events;
699                 break;
700 #endif
701
702         case LWS_CALLBACK_GET_THREAD_ID:
703                 /*
704                  * if you will call "lws_callback_on_writable"
705                  * from a different thread, return the caller thread ID
706                  * here so lws can use this information to work out if it
707                  * should signal the poll() loop to exit and restart early
708                  */
709
710                 /* return pthread_getthreadid_np(); */
711
712                 break;
713
714         default:
715                 break;
716         }
717
718         return 0;
719
720         /* if we're on HTTP1.1 or 2.0, will keep the idle connection alive */
721 try_to_reuse:
722         if (lws_http_transaction_completed(wsi))
723                 return -1;
724
725         return 0;
726 }