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