reduce log spew and document test-server variants
[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 #if defined(LWS_USE_POLARSSL)
38 #else
39 #if defined(LWS_USE_MBEDTLS)
40 #else
41 #if defined(LWS_OPENSSL_SUPPORT) && defined(LWS_HAVE_SSL_CTX_set1_param)
42 /* location of the certificate revocation list */
43 extern char crl_path[1024];
44 #endif
45 #endif
46 #endif
47
48 extern int debug_level;
49
50 enum demo_protocols {
51         /* always first */
52         PROTOCOL_HTTP = 0,
53
54         PROTOCOL_DUMB_INCREMENT,
55         PROTOCOL_LWS_MIRROR,
56
57         /* always last */
58         DEMO_PROTOCOL_COUNT
59 };
60
61 /*
62  * We take a strict whitelist approach to stop ../ attacks
63  */
64 struct serveable {
65         const char *urlpath;
66         const char *mimetype;
67 };
68
69 /*
70  * this is just an example of parsing handshake headers, you don't need this
71  * in your code unless you will filter allowing connections by the header
72  * content
73  */
74 void
75 dump_handshake_info(struct lws *wsi)
76 {
77         int n = 0, len;
78         char buf[256];
79         const unsigned char *c;
80
81         do {
82                 c = lws_token_to_string(n);
83                 if (!c) {
84                         n++;
85                         continue;
86                 }
87
88                 len = lws_hdr_total_length(wsi, n);
89                 if (!len || len > sizeof(buf) - 1) {
90                         n++;
91                         continue;
92                 }
93
94                 lws_hdr_copy(wsi, buf, sizeof buf, n);
95                 buf[sizeof(buf) - 1] = '\0';
96
97                 fprintf(stderr, "    %s = %s\n", (char *)c, buf);
98                 n++;
99         } while (c);
100 }
101
102 const char * get_mimetype(const char *file)
103 {
104         int n = strlen(file);
105
106         if (n < 5)
107                 return NULL;
108
109         if (!strcmp(&file[n - 4], ".ico"))
110                 return "image/x-icon";
111
112         if (!strcmp(&file[n - 4], ".png"))
113                 return "image/png";
114
115         if (!strcmp(&file[n - 5], ".html"))
116                 return "text/html";
117
118         if (!strcmp(&file[n - 4], ".css"))
119                 return "text/css";
120
121         return NULL;
122 }
123
124
125 static const char * const param_names[] = {
126         "text",
127         "send",
128         "file",
129         "upload",
130 };
131
132 enum enum_param_names {
133         EPN_TEXT,
134         EPN_SEND,
135         EPN_FILE,
136         EPN_UPLOAD,
137 };
138
139 static int
140 file_upload_cb(void *data, const char *name, const char *filename,
141                char *buf, int len, enum lws_spa_fileupload_states state)
142 {
143         struct per_session_data__http *pss =
144                         (struct per_session_data__http *)data;
145         int n;
146
147         switch (state) {
148         case LWS_UFS_OPEN:
149                 strncpy(pss->filename, filename, sizeof(pss->filename) - 1);
150                 /* we get the original filename in @filename arg, but for
151                  * simple demo use a fixed name so we don't have to deal with
152                  * attacks  */
153                 pss->post_fd = open("/tmp/post-file",
154                                O_CREAT | O_TRUNC | O_RDWR, 0600);
155                 break;
156         case LWS_UFS_FINAL_CONTENT:
157         case LWS_UFS_CONTENT:
158                 if (len) {
159                         pss->file_length += len;
160
161                         /* if the file length is too big, drop it */
162                         if (pss->file_length > 100000)
163                                 return 1;
164
165                         n = write(pss->post_fd, buf, len);
166                         lwsl_notice("%s: write %d says %d\n", __func__, len, n);
167                 }
168                 if (state == LWS_UFS_CONTENT)
169                         break;
170                 close(pss->post_fd);
171                 pss->post_fd = LWS_INVALID_FILE;
172                 break;
173         }
174
175         return 0;
176 }
177
178 /* this protocol server (always the first one) handles HTTP,
179  *
180  * Some misc callbacks that aren't associated with a protocol also turn up only
181  * here on the first protocol server.
182  */
183
184 int callback_http(struct lws *wsi, enum lws_callback_reasons reason, void *user,
185                   void *in, size_t len)
186 {
187         struct per_session_data__http *pss =
188                         (struct per_session_data__http *)user;
189         unsigned char buffer[4096 + LWS_PRE];
190         unsigned long amount, file_len, sent;
191         char leaf_path[1024];
192         const char *mimetype;
193         char *other_headers;
194         unsigned char *end, *start;
195         struct timeval tv;
196         unsigned char *p;
197 #ifndef LWS_NO_CLIENT
198         struct per_session_data__http *pss1;
199         struct lws *wsi1;
200 #endif
201         char buf[256];
202         char b64[64];
203         int n, m;
204 #ifdef EXTERNAL_POLL
205         struct lws_pollargs *pa = (struct lws_pollargs *)in;
206 #endif
207
208
209         switch (reason) {
210         case LWS_CALLBACK_HTTP:
211
212                 lwsl_info("lws_http_serve: %s\n",in);
213
214                 if (debug_level & LLL_INFO) {
215                         dump_handshake_info(wsi);
216
217                         /* dump the individual URI Arg parameters */
218                         n = 0;
219                         while (lws_hdr_copy_fragment(wsi, buf, sizeof(buf),
220                                                      WSI_TOKEN_HTTP_URI_ARGS, n) > 0) {
221                                 lwsl_notice("URI Arg %d: %s\n", ++n, buf);
222                         }
223                 }
224
225                 {
226                         lws_get_peer_simple(wsi, buf, sizeof(buf));
227                         lwsl_info("HTTP connect from %s\n", buf);
228                 }
229
230                 if (len < 1) {
231                         lws_return_http_status(wsi,
232                                                 HTTP_STATUS_BAD_REQUEST, NULL);
233                         goto try_to_reuse;
234                 }
235
236 #ifndef LWS_NO_CLIENT
237                 if (!strncmp(in, "/proxytest", 10)) {
238                         struct lws_client_connect_info i;
239                         char *rootpath = "/";
240                         const char *p = (const char *)in;
241
242                         if (lws_get_child(wsi))
243                                 break;
244
245                         pss->client_finished = 0;
246                         memset(&i,0, sizeof(i));
247                         i.context = lws_get_context(wsi);
248                         i.address = "git.libwebsockets.org";
249                         i.port = 80;
250                         i.ssl_connection = 0;
251                         if (p[10])
252                                 i.path = (char *)in + 10;
253                         else
254                                 i.path = rootpath;
255                         i.host = "git.libwebsockets.org";
256                         i.origin = NULL;
257                         i.method = "GET";
258                         i.parent_wsi = wsi;
259                         i.uri_replace_from = "git.libwebsockets.org/";
260                         i.uri_replace_to = "/proxytest/";
261                         if (!lws_client_connect_via_info(&i)) {
262                                 lwsl_err("proxy connect fail\n");
263                                 break;
264                         }
265
266
267
268                         break;
269                 }
270 #endif
271
272 #if 1
273                 /* this example server has no concept of directories */
274                 if (strchr((const char *)in + 1, '/')) {
275                         lws_return_http_status(wsi, HTTP_STATUS_NOT_ACCEPTABLE, NULL);
276                         goto try_to_reuse;
277                 }
278 #endif
279
280                 /* if a legal POST URL, let it continue and accept data */
281                 if (lws_hdr_total_length(wsi, WSI_TOKEN_POST_URI))
282                         return 0;
283
284                 /* check for the "send a big file by hand" example case */
285
286                 if (!strcmp((const char *)in, "/leaf.jpg")) {
287                         if (strlen(resource_path) > sizeof(leaf_path) - 10)
288                                 return -1;
289                         sprintf(leaf_path, "%s/leaf.jpg", resource_path);
290
291                         /* well, let's demonstrate how to send the hard way */
292
293                         p = buffer + LWS_PRE;
294                         end = p + sizeof(buffer) - LWS_PRE;
295
296                         pss->fd = lws_plat_file_open(wsi, leaf_path, &file_len,
297                                                      LWS_O_RDONLY);
298
299                         if (pss->fd == LWS_INVALID_FILE) {
300                                 lwsl_err("faild to open file %s\n", leaf_path);
301                                 return -1;
302                         }
303
304                         /*
305                          * we will send a big jpeg file, but it could be
306                          * anything.  Set the Content-Type: appropriately
307                          * so the browser knows what to do with it.
308                          *
309                          * Notice we use the APIs to build the header, which
310                          * will do the right thing for HTTP 1/1.1 and HTTP2
311                          * depending on what connection it happens to be working
312                          * on
313                          */
314                         if (lws_add_http_header_status(wsi, 200, &p, end))
315                                 return 1;
316                         if (lws_add_http_header_by_token(wsi, WSI_TOKEN_HTTP_SERVER,
317                                         (unsigned char *)"libwebsockets",
318                                         13, &p, end))
319                                 return 1;
320                         if (lws_add_http_header_by_token(wsi,
321                                         WSI_TOKEN_HTTP_CONTENT_TYPE,
322                                         (unsigned char *)"image/jpeg",
323                                         10, &p, end))
324                                 return 1;
325                         if (lws_add_http_header_content_length(wsi,
326                                                                file_len, &p,
327                                                                end))
328                                 return 1;
329                         if (lws_finalize_http_header(wsi, &p, end))
330                                 return 1;
331
332                         /*
333                          * send the http headers...
334                          * this won't block since it's the first payload sent
335                          * on the connection since it was established
336                          * (too small for partial)
337                          *
338                          * Notice they are sent using LWS_WRITE_HTTP_HEADERS
339                          * which also means you can't send body too in one step,
340                          * this is mandated by changes in HTTP2
341                          */
342
343                         *p = '\0';
344                         lwsl_info("%s\n", buffer + LWS_PRE);
345
346                         n = lws_write(wsi, buffer + LWS_PRE,
347                                       p - (buffer + LWS_PRE),
348                                       LWS_WRITE_HTTP_HEADERS);
349                         if (n < 0) {
350                                 lws_plat_file_close(wsi, pss->fd);
351                                 return -1;
352                         }
353                         /*
354                          * book us a LWS_CALLBACK_HTTP_WRITEABLE callback
355                          */
356                         lws_callback_on_writable(wsi);
357                         break;
358                 }
359
360                 /* if not, send a file the easy way */
361                 if (!strncmp(in, "/cgit-data/", 11)) {
362                         in = (char *)in + 11;
363                         strcpy(buf, "/usr/share/cgit");
364                 } else
365                         strcpy(buf, resource_path);
366
367                 if (strcmp(in, "/")) {
368                         if (*((const char *)in) != '/')
369                                 strcat(buf, "/");
370                         strncat(buf, in, sizeof(buf) - strlen(buf) - 1);
371                 } else /* default file to serve */
372                         strcat(buf, "/test.html");
373                 buf[sizeof(buf) - 1] = '\0';
374
375                 /* refuse to serve files we don't understand */
376                 mimetype = get_mimetype(buf);
377                 if (!mimetype) {
378                         lwsl_err("Unknown mimetype for %s\n", buf);
379                         lws_return_http_status(wsi,
380                                       HTTP_STATUS_UNSUPPORTED_MEDIA_TYPE, NULL);
381                         return -1;
382                 }
383
384                 /* demonstrates how to set a cookie on / */
385
386                 other_headers = leaf_path;
387                 p = (unsigned char *)leaf_path;
388                 if (!strcmp((const char *)in, "/") &&
389                            !lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_COOKIE)) {
390                         /* this isn't very unguessable but it'll do for us */
391                         gettimeofday(&tv, NULL);
392                         n = sprintf(b64, "test=LWS_%u_%u_COOKIE;Max-Age=360000",
393                                 (unsigned int)tv.tv_sec,
394                                 (unsigned int)tv.tv_usec);
395
396                         if (lws_add_http_header_by_name(wsi,
397                                 (unsigned char *)"set-cookie:",
398                                 (unsigned char *)b64, n, &p,
399                                 (unsigned char *)leaf_path + sizeof(leaf_path)))
400                                 return 1;
401                 }
402                 if (lws_is_ssl(wsi) && lws_add_http_header_by_name(wsi,
403                                                 (unsigned char *)
404                                                 "Strict-Transport-Security:",
405                                                 (unsigned char *)
406                                                 "max-age=15768000 ; "
407                                                 "includeSubDomains", 36, &p,
408                                                 (unsigned char *)leaf_path +
409                                                         sizeof(leaf_path)))
410                         return 1;
411                 n = (char *)p - leaf_path;
412
413                 n = lws_serve_http_file(wsi, buf, mimetype, other_headers, n);
414                 if (n < 0 || ((n > 0) && lws_http_transaction_completed(wsi)))
415                         return -1; /* error or can't reuse connection: close the socket */
416
417                 /*
418                  * notice that the sending of the file completes asynchronously,
419                  * we'll get a LWS_CALLBACK_HTTP_FILE_COMPLETION callback when
420                  * it's done
421                  */
422                 break;
423
424         case LWS_CALLBACK_HTTP_BODY:
425                 /* create the POST argument parser if not already existing */
426                 if (!pss->spa) {
427                         pss->spa = lws_spa_create(wsi, param_names,
428                                         ARRAY_SIZE(param_names), 1024,
429                                         file_upload_cb, pss);
430                         if (!pss->spa)
431                                 return -1;
432
433                         pss->filename[0] = '\0';
434                         pss->file_length = 0;
435                 }
436
437                 /* let it parse the POST data */
438                 if (lws_spa_process(pss->spa, in, len))
439                         return -1;
440                 break;
441
442         case LWS_CALLBACK_HTTP_BODY_COMPLETION:
443                 lwsl_debug("LWS_CALLBACK_HTTP_BODY_COMPLETION\n");
444                 /*
445                  * the whole of the sent body arrived,
446                  * respond to the client with a redirect to show the
447                  * results
448                  */
449
450                 /* call to inform no more payload data coming */
451                 lws_spa_finalize(pss->spa);
452
453                 p = (unsigned char *)pss->result + LWS_PRE;
454                 end = p + sizeof(pss->result) - LWS_PRE - 1;
455                 p += sprintf((char *)p,
456                         "<html><body><h1>Form results (after urldecoding)</h1>"
457                         "<table><tr><td>Name</td><td>Length</td><td>Value</td></tr>");
458
459                 for (n = 0; n < ARRAY_SIZE(param_names); n++)
460                         p += snprintf((char *)p, end - p,
461                                     "<tr><td><b>%s</b></td><td>%d</td><td>%s</td></tr>",
462                                     param_names[n],
463                                     lws_spa_get_length(pss->spa, n),
464                                     lws_spa_get_string(pss->spa, n));
465
466                 p += snprintf((char *)p, end - p, "</table><br><b>filename:</b> %s, <b>length</b> %ld",
467                                 pss->filename, pss->file_length);
468
469                 p += snprintf((char *)p, end - p, "</body></html>");
470                 pss->result_len = p - (unsigned char *)(pss->result + LWS_PRE);
471
472                 p = buffer + LWS_PRE;
473                 start = p;
474                 end = p + sizeof(buffer) - LWS_PRE;
475
476                 if (lws_add_http_header_status(wsi, 200, &p, end))
477                         return 1;
478
479                 if (lws_add_http_header_by_token(wsi, WSI_TOKEN_HTTP_CONTENT_TYPE,
480                                 (unsigned char *)"text/html", 9, &p, end))
481                         return 1;
482                 if (lws_add_http_header_content_length(wsi, pss->result_len, &p, end))
483                         return 1;
484                 if (lws_finalize_http_header(wsi, &p, end))
485                         return 1;
486
487                 n = lws_write(wsi, start, p - start, LWS_WRITE_HTTP_HEADERS);
488                 if (n < 0)
489                         return 1;
490
491                 n = lws_write(wsi, (unsigned char *)pss->result + LWS_PRE,
492                               pss->result_len, LWS_WRITE_HTTP);
493                 if (n < 0)
494                         return 1;
495                 goto try_to_reuse;
496         case LWS_CALLBACK_HTTP_DROP_PROTOCOL:
497                 lwsl_debug("LWS_CALLBACK_HTTP_DROP_PROTOCOL\n");
498
499                 /* called when our wsi user_space is going to be destroyed */
500                 if (pss->spa) {
501                         lws_spa_destroy(pss->spa);
502                         pss->spa = NULL;
503                 }
504                 break;
505         case LWS_CALLBACK_HTTP_FILE_COMPLETION:
506                 goto try_to_reuse;
507
508         case LWS_CALLBACK_HTTP_WRITEABLE:
509                 lwsl_info("LWS_CALLBACK_HTTP_WRITEABLE\n");
510
511                 if (pss->client_finished)
512                         return -1;
513
514                 if (pss->fd == LWS_INVALID_FILE)
515                         goto try_to_reuse;
516
517 #ifndef LWS_NO_CLIENT
518                 if (pss->reason_bf & 2) {
519                         char *px = buf + LWS_PRE;
520                         int lenx = sizeof(buf) - LWS_PRE;
521                         /*
522                          * our sink is writeable and our source has something
523                          * to read.  So read a lump of source material of
524                          * suitable size to send or what's available, whichever
525                          * is the smaller.
526                          */
527                         pss->reason_bf &= ~2;
528                         wsi1 = lws_get_child(wsi);
529                         if (!wsi1)
530                                 break;
531                         if (lws_http_client_read(wsi1, &px, &lenx) < 0)
532                                 goto bail;
533
534                         if (pss->client_finished)
535                                 return -1;
536                         break;
537                 }
538 #endif
539                 /*
540                  * we can send more of whatever it is we were sending
541                  */
542                 sent = 0;
543                 do {
544                         /* we'd like the send this much */
545                         n = sizeof(buffer) - LWS_PRE;
546
547                         /* but if the peer told us he wants less, we can adapt */
548                         m = lws_get_peer_write_allowance(wsi);
549
550                         /* -1 means not using a protocol that has this info */
551                         if (m == 0)
552                                 /* right now, peer can't handle anything */
553                                 goto later;
554
555                         if (m != -1 && m < n)
556                                 /* he couldn't handle that much */
557                                 n = m;
558
559                         n = lws_plat_file_read(wsi, pss->fd,
560                                                &amount, buffer + LWS_PRE, n);
561                         /* problem reading, close conn */
562                         if (n < 0) {
563                                 lwsl_err("problem reading file\n");
564                                 goto bail;
565                         }
566                         n = (int)amount;
567                         /* sent it all, close conn */
568                         if (n == 0)
569                                 goto penultimate;
570                         /*
571                          * To support HTTP2, must take care about preamble space
572                          *
573                          * identification of when we send the last payload frame
574                          * is handled by the library itself if you sent a
575                          * content-length header
576                          */
577                         m = lws_write(wsi, buffer + LWS_PRE, n, LWS_WRITE_HTTP);
578                         if (m < 0) {
579                                 lwsl_err("write failed\n");
580                                 /* write failed, close conn */
581                                 goto bail;
582                         }
583                         if (m) /* while still active, extend timeout */
584                                 lws_set_timeout(wsi, PENDING_TIMEOUT_HTTP_CONTENT, 5);
585                         sent += m;
586
587                 } while (!lws_send_pipe_choked(wsi) && (sent < 1024 * 1024));
588 later:
589                 lws_callback_on_writable(wsi);
590                 break;
591 penultimate:
592                 lws_plat_file_close(wsi, pss->fd);
593                 pss->fd = LWS_INVALID_FILE;
594                 goto try_to_reuse;
595
596 bail:
597                 lws_plat_file_close(wsi, pss->fd);
598
599                 return -1;
600
601         /*
602          * callback for confirming to continue with client IP appear in
603          * protocol 0 callback since no websocket protocol has been agreed
604          * yet.  You can just ignore this if you won't filter on client IP
605          * since the default unhandled callback return is 0 meaning let the
606          * connection continue.
607          */
608         case LWS_CALLBACK_FILTER_NETWORK_CONNECTION:
609                 /* if we returned non-zero from here, we kill the connection */
610                 break;
611
612 #ifndef LWS_NO_CLIENT
613         case LWS_CALLBACK_ESTABLISHED_CLIENT_HTTP: {
614                 char ctype[64], ctlen = 0;
615                 lwsl_err("LWS_CALLBACK_ESTABLISHED_CLIENT_HTTP\n");
616                 p = buffer + LWS_PRE;
617                 end = p + sizeof(buffer) - LWS_PRE;
618                 if (lws_add_http_header_status(lws_get_parent(wsi), 200, &p, end))
619                         return 1;
620                 if (lws_add_http_header_by_token(lws_get_parent(wsi),
621                                 WSI_TOKEN_HTTP_SERVER,
622                                 (unsigned char *)"libwebsockets",
623                                 13, &p, end))
624                         return 1;
625
626                 ctlen = lws_hdr_copy(wsi, ctype, sizeof(ctype), WSI_TOKEN_HTTP_CONTENT_TYPE);
627                 if (ctlen > 0) {
628                         if (lws_add_http_header_by_token(lws_get_parent(wsi),
629                                 WSI_TOKEN_HTTP_CONTENT_TYPE,
630                                 (unsigned char *)ctype, ctlen, &p, end))
631                                 return 1;
632                 }
633 #if 0
634                 if (lws_add_http_header_content_length(lws_get_parent(wsi),
635                                                        file_len, &p, end))
636                         return 1;
637 #endif
638                 if (lws_finalize_http_header(lws_get_parent(wsi), &p, end))
639                         return 1;
640
641                 *p = '\0';
642                 lwsl_info("%s\n", buffer + LWS_PRE);
643
644                 n = lws_write(lws_get_parent(wsi), buffer + LWS_PRE,
645                               p - (buffer + LWS_PRE),
646                               LWS_WRITE_HTTP_HEADERS);
647                 if (n < 0)
648                         return -1;
649
650                 break; }
651         case LWS_CALLBACK_CLOSED_CLIENT_HTTP:
652                 //lwsl_err("LWS_CALLBACK_CLOSED_CLIENT_HTTP\n");
653                 return -1;
654                 break;
655         case LWS_CALLBACK_RECEIVE_CLIENT_HTTP:
656                 //lwsl_err("LWS_CALLBACK_RECEIVE_CLIENT_HTTP: wsi %p\n", wsi);
657                 assert(lws_get_parent(wsi));
658                 if (!lws_get_parent(wsi))
659                         break;
660                 // lwsl_err("LWS_CALLBACK_RECEIVE_CLIENT_HTTP: wsi %p: sock: %d, parent_wsi: %p, parent_sock:%d,  len %d\n",
661                 //              wsi, lws_get_socket_fd(wsi),
662                 //              lws_get_parent(wsi),
663                 //              lws_get_socket_fd(lws_get_parent(wsi)), len);
664                 pss1 = lws_wsi_user(lws_get_parent(wsi));
665                 pss1->reason_bf |= 2;
666                 lws_callback_on_writable(lws_get_parent(wsi));
667                 break;
668         case LWS_CALLBACK_RECEIVE_CLIENT_HTTP_READ:
669                 //lwsl_err("LWS_CALLBACK_RECEIVE_CLIENT_HTTP_READ len %d\n", len);
670                 assert(lws_get_parent(wsi));
671                 m = lws_write(lws_get_parent(wsi), (unsigned char *)in,
672                                 len, LWS_WRITE_HTTP);
673                 if (m < 0)
674                         return -1;
675                 break;
676         case LWS_CALLBACK_COMPLETED_CLIENT_HTTP:
677                 //lwsl_err("LWS_CALLBACK_COMPLETED_CLIENT_HTTP\n");
678                 assert(lws_get_parent(wsi));
679                 if (!lws_get_parent(wsi))
680                         break;
681                 pss1 = lws_wsi_user(lws_get_parent(wsi));
682                 pss1->client_finished = 1;
683                 break;
684 #endif
685
686         /*
687          * callbacks for managing the external poll() array appear in
688          * protocol 0 callback
689          */
690
691         case LWS_CALLBACK_LOCK_POLL:
692                 /*
693                  * lock mutex to protect pollfd state
694                  * called before any other POLL related callback
695                  * if protecting wsi lifecycle change, len == 1
696                  */
697                 test_server_lock(len);
698                 break;
699
700         case LWS_CALLBACK_UNLOCK_POLL:
701                 /*
702                  * unlock mutex to protect pollfd state when
703                  * called after any other POLL related callback
704                  * if protecting wsi lifecycle change, len == 1
705                  */
706                 test_server_unlock(len);
707                 break;
708
709 #ifdef EXTERNAL_POLL
710         case LWS_CALLBACK_ADD_POLL_FD:
711
712                 if (count_pollfds >= max_poll_elements) {
713                         lwsl_err("LWS_CALLBACK_ADD_POLL_FD: too many sockets to track\n");
714                         return 1;
715                 }
716
717                 fd_lookup[pa->fd] = count_pollfds;
718                 pollfds[count_pollfds].fd = pa->fd;
719                 pollfds[count_pollfds].events = pa->events;
720                 pollfds[count_pollfds++].revents = 0;
721                 break;
722
723         case LWS_CALLBACK_DEL_POLL_FD:
724                 if (!--count_pollfds)
725                         break;
726                 m = fd_lookup[pa->fd];
727                 /* have the last guy take up the vacant slot */
728                 pollfds[m] = pollfds[count_pollfds];
729                 fd_lookup[pollfds[count_pollfds].fd] = m;
730                 break;
731
732         case LWS_CALLBACK_CHANGE_MODE_POLL_FD:
733                 pollfds[fd_lookup[pa->fd]].events = pa->events;
734                 break;
735 #endif
736
737         case LWS_CALLBACK_GET_THREAD_ID:
738                 /*
739                  * if you will call "lws_callback_on_writable"
740                  * from a different thread, return the caller thread ID
741                  * here so lws can use this information to work out if it
742                  * should signal the poll() loop to exit and restart early
743                  */
744
745                 /* return pthread_getthreadid_np(); */
746
747                 break;
748
749 #if defined(LWS_USE_POLARSSL)
750 #else
751 #if defined(LWS_USE_MBEDTLS)
752 #else
753 #if defined(LWS_OPENSSL_SUPPORT)
754         case LWS_CALLBACK_OPENSSL_PERFORM_CLIENT_CERT_VERIFICATION:
755                 /* Verify the client certificate */
756                 if (!len || (SSL_get_verify_result((SSL*)in) != X509_V_OK)) {
757                         int err = X509_STORE_CTX_get_error((X509_STORE_CTX*)user);
758                         int depth = X509_STORE_CTX_get_error_depth((X509_STORE_CTX*)user);
759                         const char* msg = X509_verify_cert_error_string(err);
760                         lwsl_err("LWS_CALLBACK_OPENSSL_PERFORM_CLIENT_CERT_VERIFICATION: SSL error: %s (%d), depth: %d\n", msg, err, depth);
761                         return 1;
762                 }
763                 break;
764 #if defined(LWS_HAVE_SSL_CTX_set1_param)
765         case LWS_CALLBACK_OPENSSL_LOAD_EXTRA_SERVER_VERIFY_CERTS:
766                 if (crl_path[0]) {
767                         /* Enable CRL checking */
768                         X509_VERIFY_PARAM *param = X509_VERIFY_PARAM_new();
769                         X509_VERIFY_PARAM_set_flags(param, X509_V_FLAG_CRL_CHECK);
770                         SSL_CTX_set1_param((SSL_CTX*)user, param);
771                         X509_STORE *store = SSL_CTX_get_cert_store((SSL_CTX*)user);
772                         X509_LOOKUP *lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file());
773                         n = X509_load_cert_crl_file(lookup, crl_path, X509_FILETYPE_PEM);
774                         X509_VERIFY_PARAM_free(param);
775                         if (n != 1) {
776                                 char errbuf[256];
777                                 n = ERR_get_error();
778                                 lwsl_err("LWS_CALLBACK_OPENSSL_LOAD_EXTRA_SERVER_VERIFY_CERTS: SSL error: %s (%d)\n", ERR_error_string(n, errbuf), n);
779                                 return 1;
780                         }
781                 }
782                 break;
783 #endif
784 #endif
785 #endif
786 #endif
787
788         default:
789                 break;
790         }
791
792         return 0;
793
794         /* if we're on HTTP1.1 or 2.0, will keep the idle connection alive */
795 try_to_reuse:
796         if (lws_http_transaction_completed(wsi))
797                 return -1;
798
799         return 0;
800 }