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