introduce urlencode decode and sql escape public apis
[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_notice("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                         char name[100], rip[50];
227                         lws_get_peer_addresses(wsi, lws_get_socket_fd(wsi), name,
228                                                sizeof(name), rip, sizeof(rip));
229                         sprintf(buf, "%s (%s)", name, rip);
230                         lwsl_notice("HTTP connect from %s\n", buf);
231                 }
232
233                 if (len < 1) {
234                         lws_return_http_status(wsi,
235                                                 HTTP_STATUS_BAD_REQUEST, NULL);
236                         goto try_to_reuse;
237                 }
238
239 #ifndef LWS_NO_CLIENT
240                 if (!strncmp(in, "/proxytest", 10)) {
241                         struct lws_client_connect_info i;
242                         char *rootpath = "/";
243                         const char *p = (const char *)in;
244
245                         if (lws_get_child(wsi))
246                                 break;
247
248                         pss->client_finished = 0;
249                         memset(&i,0, sizeof(i));
250                         i.context = lws_get_context(wsi);
251                         i.address = "git.libwebsockets.org";
252                         i.port = 80;
253                         i.ssl_connection = 0;
254                         if (p[10])
255                                 i.path = (char *)in + 10;
256                         else
257                                 i.path = rootpath;
258                         i.host = "git.libwebsockets.org";
259                         i.origin = NULL;
260                         i.method = "GET";
261                         i.parent_wsi = wsi;
262                         i.uri_replace_from = "git.libwebsockets.org/";
263                         i.uri_replace_to = "/proxytest/";
264                         if (!lws_client_connect_via_info(&i)) {
265                                 lwsl_err("proxy connect fail\n");
266                                 break;
267                         }
268
269
270
271                         break;
272                 }
273 #endif
274
275 #if 1
276                 /* this example server has no concept of directories */
277                 if (strchr((const char *)in + 1, '/')) {
278                         lws_return_http_status(wsi, HTTP_STATUS_NOT_ACCEPTABLE, NULL);
279                         goto try_to_reuse;
280                 }
281 #endif
282
283                 /* if a legal POST URL, let it continue and accept data */
284                 if (lws_hdr_total_length(wsi, WSI_TOKEN_POST_URI))
285                         return 0;
286
287                 /* check for the "send a big file by hand" example case */
288
289                 if (!strcmp((const char *)in, "/leaf.jpg")) {
290                         if (strlen(resource_path) > sizeof(leaf_path) - 10)
291                                 return -1;
292                         sprintf(leaf_path, "%s/leaf.jpg", resource_path);
293
294                         /* well, let's demonstrate how to send the hard way */
295
296                         p = buffer + LWS_PRE;
297                         end = p + sizeof(buffer) - LWS_PRE;
298
299                         pss->fd = lws_plat_file_open(wsi, leaf_path, &file_len,
300                                                      LWS_O_RDONLY);
301
302                         if (pss->fd == LWS_INVALID_FILE) {
303                                 lwsl_err("faild to open file %s\n", leaf_path);
304                                 return -1;
305                         }
306
307                         /*
308                          * we will send a big jpeg file, but it could be
309                          * anything.  Set the Content-Type: appropriately
310                          * so the browser knows what to do with it.
311                          *
312                          * Notice we use the APIs to build the header, which
313                          * will do the right thing for HTTP 1/1.1 and HTTP2
314                          * depending on what connection it happens to be working
315                          * on
316                          */
317                         if (lws_add_http_header_status(wsi, 200, &p, end))
318                                 return 1;
319                         if (lws_add_http_header_by_token(wsi, WSI_TOKEN_HTTP_SERVER,
320                                         (unsigned char *)"libwebsockets",
321                                         13, &p, end))
322                                 return 1;
323                         if (lws_add_http_header_by_token(wsi,
324                                         WSI_TOKEN_HTTP_CONTENT_TYPE,
325                                         (unsigned char *)"image/jpeg",
326                                         10, &p, end))
327                                 return 1;
328                         if (lws_add_http_header_content_length(wsi,
329                                                                file_len, &p,
330                                                                end))
331                                 return 1;
332                         if (lws_finalize_http_header(wsi, &p, end))
333                                 return 1;
334
335                         /*
336                          * send the http headers...
337                          * this won't block since it's the first payload sent
338                          * on the connection since it was established
339                          * (too small for partial)
340                          *
341                          * Notice they are sent using LWS_WRITE_HTTP_HEADERS
342                          * which also means you can't send body too in one step,
343                          * this is mandated by changes in HTTP2
344                          */
345
346                         *p = '\0';
347                         lwsl_info("%s\n", buffer + LWS_PRE);
348
349                         n = lws_write(wsi, buffer + LWS_PRE,
350                                       p - (buffer + LWS_PRE),
351                                       LWS_WRITE_HTTP_HEADERS);
352                         if (n < 0) {
353                                 lws_plat_file_close(wsi, pss->fd);
354                                 return -1;
355                         }
356                         /*
357                          * book us a LWS_CALLBACK_HTTP_WRITEABLE callback
358                          */
359                         lws_callback_on_writable(wsi);
360                         break;
361                 }
362
363                 /* if not, send a file the easy way */
364                 if (!strncmp(in, "/cgit-data/", 11)) {
365                         in = (char *)in + 11;
366                         strcpy(buf, "/usr/share/cgit");
367                 } else
368                         strcpy(buf, resource_path);
369
370                 if (strcmp(in, "/")) {
371                         if (*((const char *)in) != '/')
372                                 strcat(buf, "/");
373                         strncat(buf, in, sizeof(buf) - strlen(buf) - 1);
374                 } else /* default file to serve */
375                         strcat(buf, "/test.html");
376                 buf[sizeof(buf) - 1] = '\0';
377
378                 /* refuse to serve files we don't understand */
379                 mimetype = get_mimetype(buf);
380                 if (!mimetype) {
381                         lwsl_err("Unknown mimetype for %s\n", buf);
382                         lws_return_http_status(wsi,
383                                       HTTP_STATUS_UNSUPPORTED_MEDIA_TYPE, NULL);
384                         return -1;
385                 }
386
387                 /* demonstrates how to set a cookie on / */
388
389                 other_headers = leaf_path;
390                 p = (unsigned char *)leaf_path;
391                 if (!strcmp((const char *)in, "/") &&
392                            !lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_COOKIE)) {
393                         /* this isn't very unguessable but it'll do for us */
394                         gettimeofday(&tv, NULL);
395                         n = sprintf(b64, "test=LWS_%u_%u_COOKIE;Max-Age=360000",
396                                 (unsigned int)tv.tv_sec,
397                                 (unsigned int)tv.tv_usec);
398
399                         if (lws_add_http_header_by_name(wsi,
400                                 (unsigned char *)"set-cookie:",
401                                 (unsigned char *)b64, n, &p,
402                                 (unsigned char *)leaf_path + sizeof(leaf_path)))
403                                 return 1;
404                 }
405                 if (lws_is_ssl(wsi) && lws_add_http_header_by_name(wsi,
406                                                 (unsigned char *)
407                                                 "Strict-Transport-Security:",
408                                                 (unsigned char *)
409                                                 "max-age=15768000 ; "
410                                                 "includeSubDomains", 36, &p,
411                                                 (unsigned char *)leaf_path +
412                                                         sizeof(leaf_path)))
413                         return 1;
414                 n = (char *)p - leaf_path;
415
416                 n = lws_serve_http_file(wsi, buf, mimetype, other_headers, n);
417                 if (n < 0 || ((n > 0) && lws_http_transaction_completed(wsi)))
418                         return -1; /* error or can't reuse connection: close the socket */
419
420                 /*
421                  * notice that the sending of the file completes asynchronously,
422                  * we'll get a LWS_CALLBACK_HTTP_FILE_COMPLETION callback when
423                  * it's done
424                  */
425                 break;
426
427         case LWS_CALLBACK_HTTP_BODY:
428                 /* create the POST argument parser if not already existing */
429                 if (!pss->spa) {
430                         pss->spa = lws_spa_create(wsi, param_names,
431                                         ARRAY_SIZE(param_names), 1024,
432                                         file_upload_cb, pss);
433                         if (!pss->spa)
434                                 return -1;
435
436                         pss->filename[0] = '\0';
437                         pss->file_length = 0;
438                 }
439
440                 /* let it parse the POST data */
441                 if (lws_spa_process(pss->spa, in, len))
442                         return -1;
443                 break;
444
445         case LWS_CALLBACK_HTTP_BODY_COMPLETION:
446                 lwsl_debug("LWS_CALLBACK_HTTP_BODY_COMPLETION\n");
447                 /*
448                  * the whole of the sent body arrived,
449                  * respond to the client with a redirect to show the
450                  * results
451                  */
452
453                 /* call to inform no more payload data coming */
454                 lws_spa_finalize(pss->spa);
455
456                 p = (unsigned char *)pss->result + LWS_PRE;
457                 end = p + sizeof(pss->result) - LWS_PRE - 1;
458                 p += sprintf((char *)p,
459                         "<html><body><h1>Form results (after urldecoding)</h1>"
460                         "<table><tr><td>Name</td><td>Length</td><td>Value</td></tr>");
461
462                 for (n = 0; n < ARRAY_SIZE(param_names); n++)
463                         p += snprintf((char *)p, end - p,
464                                     "<tr><td><b>%s</b></td><td>%d</td><td>%s</td></tr>",
465                                     param_names[n],
466                                     lws_spa_get_length(pss->spa, n),
467                                     lws_spa_get_string(pss->spa, n));
468
469                 p += snprintf((char *)p, end - p, "</table><br><b>filename:</b> %s, <b>length</b> %ld",
470                                 pss->filename, pss->file_length);
471
472                 p += snprintf((char *)p, end - p, "</body></html>");
473                 pss->result_len = p - (unsigned char *)(pss->result + LWS_PRE);
474
475                 p = buffer + LWS_PRE;
476                 start = p;
477                 end = p + sizeof(buffer) - LWS_PRE;
478
479                 if (lws_add_http_header_status(wsi, 200, &p, end))
480                         return 1;
481
482                 if (lws_add_http_header_by_token(wsi, WSI_TOKEN_HTTP_CONTENT_TYPE,
483                                 (unsigned char *)"text/html", 9, &p, end))
484                         return 1;
485                 if (lws_add_http_header_content_length(wsi, pss->result_len, &p, end))
486                         return 1;
487                 if (lws_finalize_http_header(wsi, &p, end))
488                         return 1;
489
490                 n = lws_write(wsi, start, p - start, LWS_WRITE_HTTP_HEADERS);
491                 if (n < 0)
492                         return 1;
493
494                 n = lws_write(wsi, (unsigned char *)pss->result + LWS_PRE,
495                               pss->result_len, LWS_WRITE_HTTP);
496                 if (n < 0)
497                         return 1;
498                 goto try_to_reuse;
499
500         case LWS_CALLBACK_HTTP_FILE_COMPLETION:
501                 goto try_to_reuse;
502
503         case LWS_CALLBACK_HTTP_WRITEABLE:
504                 lwsl_info("LWS_CALLBACK_HTTP_WRITEABLE\n");
505
506                 if (pss->client_finished)
507                         return -1;
508
509                 if (pss->fd == LWS_INVALID_FILE)
510                         goto try_to_reuse;
511
512 #ifndef LWS_NO_CLIENT
513                 if (pss->reason_bf & 2) {
514                         char *px = buf + LWS_PRE;
515                         int lenx = sizeof(buf) - LWS_PRE;
516                         /*
517                          * our sink is writeable and our source has something
518                          * to read.  So read a lump of source material of
519                          * suitable size to send or what's available, whichever
520                          * is the smaller.
521                          */
522                         pss->reason_bf &= ~2;
523                         wsi1 = lws_get_child(wsi);
524                         if (!wsi1)
525                                 break;
526                         if (lws_http_client_read(wsi1, &px, &lenx) < 0)
527                                 goto bail;
528
529                         if (pss->client_finished)
530                                 return -1;
531                         break;
532                 }
533 #endif
534                 /*
535                  * we can send more of whatever it is we were sending
536                  */
537                 sent = 0;
538                 do {
539                         /* we'd like the send this much */
540                         n = sizeof(buffer) - LWS_PRE;
541
542                         /* but if the peer told us he wants less, we can adapt */
543                         m = lws_get_peer_write_allowance(wsi);
544
545                         /* -1 means not using a protocol that has this info */
546                         if (m == 0)
547                                 /* right now, peer can't handle anything */
548                                 goto later;
549
550                         if (m != -1 && m < n)
551                                 /* he couldn't handle that much */
552                                 n = m;
553
554                         n = lws_plat_file_read(wsi, pss->fd,
555                                                &amount, buffer + LWS_PRE, n);
556                         /* problem reading, close conn */
557                         if (n < 0) {
558                                 lwsl_err("problem reading file\n");
559                                 goto bail;
560                         }
561                         n = (int)amount;
562                         /* sent it all, close conn */
563                         if (n == 0)
564                                 goto penultimate;
565                         /*
566                          * To support HTTP2, must take care about preamble space
567                          *
568                          * identification of when we send the last payload frame
569                          * is handled by the library itself if you sent a
570                          * content-length header
571                          */
572                         m = lws_write(wsi, buffer + LWS_PRE, n, LWS_WRITE_HTTP);
573                         if (m < 0) {
574                                 lwsl_err("write failed\n");
575                                 /* write failed, close conn */
576                                 goto bail;
577                         }
578                         if (m) /* while still active, extend timeout */
579                                 lws_set_timeout(wsi, PENDING_TIMEOUT_HTTP_CONTENT, 5);
580                         sent += m;
581
582                 } while (!lws_send_pipe_choked(wsi) && (sent < 1024 * 1024));
583 later:
584                 lws_callback_on_writable(wsi);
585                 break;
586 penultimate:
587                 lws_plat_file_close(wsi, pss->fd);
588                 pss->fd = LWS_INVALID_FILE;
589                 goto try_to_reuse;
590
591 bail:
592                 lws_plat_file_close(wsi, pss->fd);
593
594                 return -1;
595
596         /*
597          * callback for confirming to continue with client IP appear in
598          * protocol 0 callback since no websocket protocol has been agreed
599          * yet.  You can just ignore this if you won't filter on client IP
600          * since the default unhandled callback return is 0 meaning let the
601          * connection continue.
602          */
603         case LWS_CALLBACK_FILTER_NETWORK_CONNECTION:
604                 /* if we returned non-zero from here, we kill the connection */
605                 break;
606
607 #ifndef LWS_NO_CLIENT
608         case LWS_CALLBACK_ESTABLISHED_CLIENT_HTTP: {
609                 char ctype[64], ctlen = 0;
610                 lwsl_err("LWS_CALLBACK_ESTABLISHED_CLIENT_HTTP\n");
611                 p = buffer + LWS_PRE;
612                 end = p + sizeof(buffer) - LWS_PRE;
613                 if (lws_add_http_header_status(lws_get_parent(wsi), 200, &p, end))
614                         return 1;
615                 if (lws_add_http_header_by_token(lws_get_parent(wsi),
616                                 WSI_TOKEN_HTTP_SERVER,
617                                 (unsigned char *)"libwebsockets",
618                                 13, &p, end))
619                         return 1;
620
621                 ctlen = lws_hdr_copy(wsi, ctype, sizeof(ctype), WSI_TOKEN_HTTP_CONTENT_TYPE);
622                 if (ctlen > 0) {
623                         if (lws_add_http_header_by_token(lws_get_parent(wsi),
624                                 WSI_TOKEN_HTTP_CONTENT_TYPE,
625                                 (unsigned char *)ctype, ctlen, &p, end))
626                                 return 1;
627                 }
628 #if 0
629                 if (lws_add_http_header_content_length(lws_get_parent(wsi),
630                                                        file_len, &p, end))
631                         return 1;
632 #endif
633                 if (lws_finalize_http_header(lws_get_parent(wsi), &p, end))
634                         return 1;
635
636                 *p = '\0';
637                 lwsl_info("%s\n", buffer + LWS_PRE);
638
639                 n = lws_write(lws_get_parent(wsi), buffer + LWS_PRE,
640                               p - (buffer + LWS_PRE),
641                               LWS_WRITE_HTTP_HEADERS);
642                 if (n < 0)
643                         return -1;
644
645                 break; }
646         case LWS_CALLBACK_CLOSED_CLIENT_HTTP:
647                 //lwsl_err("LWS_CALLBACK_CLOSED_CLIENT_HTTP\n");
648                 return -1;
649                 break;
650         case LWS_CALLBACK_RECEIVE_CLIENT_HTTP:
651                 //lwsl_err("LWS_CALLBACK_RECEIVE_CLIENT_HTTP: wsi %p\n", wsi);
652                 assert(lws_get_parent(wsi));
653                 if (!lws_get_parent(wsi))
654                         break;
655                 // lwsl_err("LWS_CALLBACK_RECEIVE_CLIENT_HTTP: wsi %p: sock: %d, parent_wsi: %p, parent_sock:%d,  len %d\n",
656                 //              wsi, lws_get_socket_fd(wsi),
657                 //              lws_get_parent(wsi),
658                 //              lws_get_socket_fd(lws_get_parent(wsi)), len);
659                 pss1 = lws_wsi_user(lws_get_parent(wsi));
660                 pss1->reason_bf |= 2;
661                 lws_callback_on_writable(lws_get_parent(wsi));
662                 break;
663         case LWS_CALLBACK_RECEIVE_CLIENT_HTTP_READ:
664                 //lwsl_err("LWS_CALLBACK_RECEIVE_CLIENT_HTTP_READ len %d\n", len);
665                 assert(lws_get_parent(wsi));
666                 m = lws_write(lws_get_parent(wsi), (unsigned char *)in,
667                                 len, LWS_WRITE_HTTP);
668                 if (m < 0)
669                         return -1;
670                 break;
671         case LWS_CALLBACK_COMPLETED_CLIENT_HTTP:
672                 //lwsl_err("LWS_CALLBACK_COMPLETED_CLIENT_HTTP\n");
673                 assert(lws_get_parent(wsi));
674                 if (!lws_get_parent(wsi))
675                         break;
676                 pss1 = lws_wsi_user(lws_get_parent(wsi));
677                 pss1->client_finished = 1;
678                 break;
679 #endif
680
681         /*
682          * callbacks for managing the external poll() array appear in
683          * protocol 0 callback
684          */
685
686         case LWS_CALLBACK_LOCK_POLL:
687                 /*
688                  * lock mutex to protect pollfd state
689                  * called before any other POLL related callback
690                  * if protecting wsi lifecycle change, len == 1
691                  */
692                 test_server_lock(len);
693                 break;
694
695         case LWS_CALLBACK_UNLOCK_POLL:
696                 /*
697                  * unlock mutex to protect pollfd state when
698                  * called after any other POLL related callback
699                  * if protecting wsi lifecycle change, len == 1
700                  */
701                 test_server_unlock(len);
702                 break;
703
704 #ifdef EXTERNAL_POLL
705         case LWS_CALLBACK_ADD_POLL_FD:
706
707                 if (count_pollfds >= max_poll_elements) {
708                         lwsl_err("LWS_CALLBACK_ADD_POLL_FD: too many sockets to track\n");
709                         return 1;
710                 }
711
712                 fd_lookup[pa->fd] = count_pollfds;
713                 pollfds[count_pollfds].fd = pa->fd;
714                 pollfds[count_pollfds].events = pa->events;
715                 pollfds[count_pollfds++].revents = 0;
716                 break;
717
718         case LWS_CALLBACK_DEL_POLL_FD:
719                 if (!--count_pollfds)
720                         break;
721                 m = fd_lookup[pa->fd];
722                 /* have the last guy take up the vacant slot */
723                 pollfds[m] = pollfds[count_pollfds];
724                 fd_lookup[pollfds[count_pollfds].fd] = m;
725                 break;
726
727         case LWS_CALLBACK_CHANGE_MODE_POLL_FD:
728                 pollfds[fd_lookup[pa->fd]].events = pa->events;
729                 break;
730 #endif
731
732         case LWS_CALLBACK_GET_THREAD_ID:
733                 /*
734                  * if you will call "lws_callback_on_writable"
735                  * from a different thread, return the caller thread ID
736                  * here so lws can use this information to work out if it
737                  * should signal the poll() loop to exit and restart early
738                  */
739
740                 /* return pthread_getthreadid_np(); */
741
742                 break;
743
744 #if defined(LWS_USE_POLARSSL)
745 #else
746 #if defined(LWS_USE_MBEDTLS)
747 #else
748 #if defined(LWS_OPENSSL_SUPPORT)
749         case LWS_CALLBACK_OPENSSL_PERFORM_CLIENT_CERT_VERIFICATION:
750                 /* Verify the client certificate */
751                 if (!len || (SSL_get_verify_result((SSL*)in) != X509_V_OK)) {
752                         int err = X509_STORE_CTX_get_error((X509_STORE_CTX*)user);
753                         int depth = X509_STORE_CTX_get_error_depth((X509_STORE_CTX*)user);
754                         const char* msg = X509_verify_cert_error_string(err);
755                         lwsl_err("LWS_CALLBACK_OPENSSL_PERFORM_CLIENT_CERT_VERIFICATION: SSL error: %s (%d), depth: %d\n", msg, err, depth);
756                         return 1;
757                 }
758                 break;
759 #if defined(LWS_HAVE_SSL_CTX_set1_param)
760         case LWS_CALLBACK_OPENSSL_LOAD_EXTRA_SERVER_VERIFY_CERTS:
761                 if (crl_path[0]) {
762                         /* Enable CRL checking */
763                         X509_VERIFY_PARAM *param = X509_VERIFY_PARAM_new();
764                         X509_VERIFY_PARAM_set_flags(param, X509_V_FLAG_CRL_CHECK);
765                         SSL_CTX_set1_param((SSL_CTX*)user, param);
766                         X509_STORE *store = SSL_CTX_get_cert_store((SSL_CTX*)user);
767                         X509_LOOKUP *lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file());
768                         n = X509_load_cert_crl_file(lookup, crl_path, X509_FILETYPE_PEM);
769                         X509_VERIFY_PARAM_free(param);
770                         if (n != 1) {
771                                 char errbuf[256];
772                                 n = ERR_get_error();
773                                 lwsl_err("LWS_CALLBACK_OPENSSL_LOAD_EXTRA_SERVER_VERIFY_CERTS: SSL error: %s (%d)\n", ERR_error_string(n, errbuf), n);
774                                 return 1;
775                         }
776                 }
777                 break;
778 #endif
779 #endif
780 #endif
781 #endif
782
783         default:
784                 break;
785         }
786
787         return 0;
788
789         /* if we're on HTTP1.1 or 2.0, will keep the idle connection alive */
790 try_to_reuse:
791         if (lws_http_transaction_completed(wsi))
792                 return -1;
793
794         return 0;
795 }