lwsws redirect and correct vhost selection before accept
[platform/upstream/libwebsockets.git] / lib / context.c
1 /*
2  * libwebsockets - small server side websockets and web server implementation
3  *
4  * Copyright (C) 2010-2015 Andy Green <andy@warmcat.com>
5  *
6  *  This library is free software; you can redistribute it and/or
7  *  modify it under the terms of the GNU Lesser General Public
8  *  License as published by the Free Software Foundation:
9  *  version 2.1 of the License.
10  *
11  *  This library is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  *  Lesser General Public License for more details.
15  *
16  *  You should have received a copy of the GNU Lesser General Public
17  *  License along with this library; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19  *  MA  02110-1301  USA
20  */
21
22 #include "private-libwebsockets.h"
23
24 #ifndef LWS_BUILD_HASH
25 #define LWS_BUILD_HASH "unknown-build-hash"
26 #endif
27
28 static const char *library_version = LWS_LIBRARY_VERSION " " LWS_BUILD_HASH;
29
30 /**
31  * lws_get_library_version: get version and git hash library built from
32  *
33  *      returns a const char * to a string like "1.1 178d78c"
34  *      representing the library version followed by the git head hash it
35  *      was built from
36  */
37 LWS_VISIBLE const char *
38 lws_get_library_version(void)
39 {
40         return library_version;
41 }
42
43 static const char * const mount_protocols[] = {
44         "http://",
45         "https://",
46         "file://",
47         "cgi://",
48         ">http://",
49         ">https://",
50 };
51
52 LWS_VISIBLE LWS_EXTERN int
53 lws_write_http_mount(struct lws_http_mount *next, struct lws_http_mount **res,
54                      void *store, const char *mountpoint, const char *origin,
55                      const char *def)
56 {
57         struct lws_http_mount *m;
58         void *orig = store;
59         unsigned long l = (unsigned long)store;
60         int n;
61
62         if (l & 15)
63                 l += 16 - (l & 15);
64
65         store = (void *)l;
66         m = (struct lws_http_mount *)store;
67         *res = m;
68
69         m->def = def;
70         m->mountpoint = mountpoint;
71         m->mountpoint_len = (unsigned char)strlen(mountpoint);
72         m->mount_next = NULL;
73         if (next)
74                 next->mount_next = m;
75
76         for (n = 0; n < ARRAY_SIZE(mount_protocols); n++)
77                 if (!strncmp(origin, mount_protocols[n],
78                      strlen(mount_protocols[n]))) {
79                         m->origin_protocol = n;
80                         m->origin = origin + strlen(mount_protocols[n]);
81                         break;
82                 }
83
84         if (n == ARRAY_SIZE(mount_protocols)) {
85                 lwsl_err("unsupported protocol://\n");
86                 return 0; /* ie, fail */
87         }
88
89         return ((char *)store + sizeof(*m)) - (char *)orig;
90 }
91
92 LWS_VISIBLE void *
93 lws_protocol_vh_priv_zalloc(struct lws_vhost *vhost, const struct lws_protocols *prot,
94                             int size)
95 {
96         int n = 0;
97
98         /* allocate the vh priv array only on demand */
99         if (!vhost->protocol_vh_privs) {
100                 vhost->protocol_vh_privs = (void **)lws_zalloc(
101                                 vhost->count_protocols * sizeof(void *));
102                 if (!vhost->protocol_vh_privs)
103                         return NULL;
104         }
105
106         while (n < vhost->count_protocols && &vhost->protocols[n] != prot)
107                 n++;
108
109         if (n == vhost->count_protocols)
110                 return NULL;
111
112         vhost->protocol_vh_privs[n] = lws_zalloc(size);
113         return vhost->protocol_vh_privs[n];
114 }
115
116 LWS_VISIBLE void *
117 lws_protocol_vh_priv_get(struct lws_vhost *vhost, const struct lws_protocols *prot)
118 {
119         int n = 0;
120
121         if (!vhost->protocol_vh_privs)
122                 return NULL;
123
124         while (n < vhost->count_protocols && &vhost->protocols[n] != prot)
125                 n++;
126
127         if (n == vhost->count_protocols) {
128                 lwsl_err("%s: unknown protocol %p\n", __func__, prot);
129                 return NULL;
130         }
131
132         return vhost->protocol_vh_privs[n];
133 }
134
135 static struct lws_protocol_vhost_options *
136 lws_vhost_protocol_options(struct lws_vhost *vh, const char *name)
137 {
138         struct lws_protocol_vhost_options *pvo = vh->pvo;
139
140         while (pvo) {
141                 // lwsl_notice("%s: '%s' '%s'\n", __func__, pvo->name, name);
142                 if (!strcmp(pvo->name, name))
143                         return pvo;
144                 pvo = pvo->next;
145         }
146
147         return NULL;
148 }
149
150 int
151 lws_protocol_init(struct lws_context *context)
152 {
153         struct lws_vhost *vh = context->vhost_list;
154         struct lws_protocol_vhost_options *pvo;
155         struct lws wsi;
156         int n;
157
158         memset(&wsi, 0, sizeof(wsi));
159         wsi.context = context;
160
161         while (vh) {
162                 wsi.vhost = vh;
163
164                 /* initialize supported protocols on this vhost */
165
166                 for (n = 0; n < vh->count_protocols; n++) {
167                         wsi.protocol = &vh->protocols[n];
168
169                         pvo = lws_vhost_protocol_options(vh,
170                                                          vh->protocols[n].name);
171                         if (pvo)
172                                 /*
173                                  * linked list of options specific to
174                                  * vh + protocol
175                                  */
176                                 pvo = pvo->options;
177
178                         /*
179                          * inform all the protocols that they are doing their one-time
180                          * initialization if they want to.
181                          *
182                          * NOTE the wsi is all zeros except for the context, vh and
183                          * protocol ptrs so lws_get_context(wsi) etc can work
184                          */
185                         vh->protocols[n].callback(&wsi,
186                                 LWS_CALLBACK_PROTOCOL_INIT, NULL, pvo, 0);
187                 }
188
189                 vh = vh->vhost_next;
190         }
191
192         context->protocol_init_done = 1;
193
194         return 0;
195 }
196
197 LWS_VISIBLE struct lws_vhost *
198 lws_create_vhost(struct lws_context *context,
199                  struct lws_context_creation_info *info,
200                  struct lws_http_mount *mounts)
201 {
202         struct lws_vhost *vh = lws_zalloc(sizeof(*vh)),
203                          **vh1 = &context->vhost_list;
204 #ifdef LWS_WITH_PLUGINS
205         struct lws_plugin *plugin = context->plugin_list;
206         struct lws_protocols *lwsp;
207         int m, n;
208 #endif
209         char *p;
210
211         if (!vh)
212                 return NULL;
213
214         vh->context = context;
215         if (!info->vhost_name)
216                 vh->name = "default";
217         else
218                 vh->name = info->vhost_name;
219
220         vh->iface = info->iface;
221         for (vh->count_protocols = 0;
222              info->protocols[vh->count_protocols].callback;
223              vh->count_protocols++)
224                 ;
225
226         vh->pvo = info->pvo;
227
228 #ifdef LWS_WITH_PLUGINS
229         if (plugin) {
230                 /*
231                  * give the vhost a unified list of protocols including the
232                  * ones that came from plugins
233                  */
234                 lwsp = lws_zalloc(sizeof(struct lws_protocols) *
235                                            (vh->count_protocols +
236                                            context->plugin_protocol_count + 1));
237                 if (!lwsp)
238                         return NULL;
239
240                 m = vh->count_protocols;
241                 memcpy(lwsp, info->protocols,
242                        sizeof(struct lws_protocols) * m);
243
244                 while (plugin) {
245                         for (n = 0; n < plugin->caps.count_protocols; n++) {
246                                 /*
247                                  * for compatibility's sake, no pvo implies
248                                  * allow all protocols
249                                  */
250                                 if (!info->pvo || lws_vhost_protocol_options(vh,
251                                     plugin->caps.protocols[n].name)) {
252                                         memcpy(&lwsp[m],
253                                                &plugin->caps.protocols[n],
254                                                sizeof(struct lws_protocols));
255                                         m++;
256                                         vh->count_protocols++;
257                                 }
258                         }
259                         plugin = plugin->list;
260                 }
261                 vh->protocols = lwsp;
262         } else
263 #endif
264                 vh->protocols = info->protocols;
265
266         vh->mount_list = mounts;
267
268         lwsl_notice("Creating Vhost '%s' port %d, %d protocols\n",
269                         vh->name, info->port, vh->count_protocols);
270
271         while (mounts) {
272                 lwsl_notice("   mounting %s%s to %s\n",
273                                 mount_protocols[mounts->origin_protocol],
274                                 mounts->origin, mounts->mountpoint);
275                 mounts = mounts->mount_next;
276         }
277
278 #ifndef LWS_NO_EXTENSIONS
279 #ifdef LWS_WITH_PLUGINS
280         if (context->plugin_extension_count) {
281
282                 m = 0;
283                 while (info->extensions && info->extensions[m].callback)
284                         m++;
285
286                 /*
287                  * give the vhost a unified list of extensions including the
288                  * ones that came from plugins
289                  */
290                 vh->extensions = lws_zalloc(sizeof(struct lws_extension) *
291                                            (m +
292                                            context->plugin_extension_count + 1));
293                 if (!vh->extensions)
294                         return NULL;
295
296                 memcpy((struct lws_extension *)vh->extensions, info->extensions,
297                        sizeof(struct lws_extension) * m);
298                 while (plugin) {
299                         memcpy((struct lws_extension *)&vh->extensions[m], plugin->caps.extensions,
300                                sizeof(struct lws_extension) *
301                                plugin->caps.count_extensions);
302                         m += plugin->caps.count_extensions;
303                         plugin = plugin->list;
304                 }
305         } else
306 #endif
307                 vh->extensions = info->extensions;
308 #endif
309
310         vh->listen_port = info->port;
311         vh->http_proxy_port = 0;
312         vh->http_proxy_address[0] = '\0';
313
314         /* either use proxy from info, or try get it from env var */
315
316         if (info->http_proxy_address) {
317                 /* override for backwards compatibility */
318                 if (info->http_proxy_port)
319                         vh->http_proxy_port = info->http_proxy_port;
320                 lws_set_proxy(vh, info->http_proxy_address);
321         } else {
322 #ifdef LWS_HAVE_GETENV
323                 p = getenv("http_proxy");
324                 if (p)
325                         lws_set_proxy(vh, p);
326 #endif
327         }
328
329         vh->ka_time = info->ka_time;
330         vh->ka_interval = info->ka_interval;
331         vh->ka_probes = info->ka_probes;
332
333         if (lws_context_init_server_ssl(info, vh))
334                 goto bail;
335
336         if (lws_context_init_client_ssl(info, vh))
337                 goto bail;
338
339         if (lws_context_init_server(info, vh))
340                 goto bail;
341
342         while (1) {
343                 if (!(*vh1)) {
344                         *vh1 = vh;
345                         break;
346                 }
347                 vh1 = &(*vh1)->vhost_next;
348         };
349
350         return vh;
351
352 bail:
353         lws_free(vh);
354
355         return NULL;
356 }
357
358 /**
359  * lws_create_context() - Create the websocket handler
360  * @info:       pointer to struct with parameters
361  *
362  *      This function creates the listening socket (if serving) and takes care
363  *      of all initialization in one step.
364  *
365  *      After initialization, it returns a struct lws_context * that
366  *      represents this server.  After calling, user code needs to take care
367  *      of calling lws_service() with the context pointer to get the
368  *      server's sockets serviced.  This must be done in the same process
369  *      context as the initialization call.
370  *
371  *      The protocol callback functions are called for a handful of events
372  *      including http requests coming in, websocket connections becoming
373  *      established, and data arriving; it's also called periodically to allow
374  *      async transmission.
375  *
376  *      HTTP requests are sent always to the FIRST protocol in @protocol, since
377  *      at that time websocket protocol has not been negotiated.  Other
378  *      protocols after the first one never see any HTTP callack activity.
379  *
380  *      The server created is a simple http server by default; part of the
381  *      websocket standard is upgrading this http connection to a websocket one.
382  *
383  *      This allows the same server to provide files like scripts and favicon /
384  *      images or whatever over http and dynamic data over websockets all in
385  *      one place; they're all handled in the user callback.
386  */
387 LWS_VISIBLE struct lws_context *
388 lws_create_context(struct lws_context_creation_info *info)
389 {
390         struct lws_context *context = NULL;
391         struct lws wsi;
392 #ifndef LWS_NO_DAEMONIZE
393         int pid_daemon = get_daemonize_pid();
394 #endif
395         int n, m;
396 #if defined(__ANDROID__)
397         struct rlimit rt;
398 #endif
399
400
401         lwsl_notice("Initial logging level %d\n", log_level);
402         lwsl_notice("Libwebsockets version: %s\n", library_version);
403 #if LWS_POSIX
404 #ifdef LWS_USE_IPV6
405         if (!lws_check_opt(info->options, LWS_SERVER_OPTION_DISABLE_IPV6))
406                 lwsl_notice("IPV6 compiled in and enabled\n");
407         else
408                 lwsl_notice("IPV6 compiled in but disabled\n");
409 #else
410         lwsl_notice("IPV6 not compiled in\n");
411 #endif
412         lws_feature_status_libev(info);
413         lws_feature_status_libuv(info);
414 #endif
415         lwsl_info(" LWS_DEF_HEADER_LEN    : %u\n", LWS_DEF_HEADER_LEN);
416         lwsl_info(" LWS_MAX_PROTOCOLS     : %u\n", LWS_MAX_PROTOCOLS);
417         lwsl_info(" LWS_MAX_SMP           : %u\n", LWS_MAX_SMP);
418         lwsl_info(" SPEC_LATEST_SUPPORTED : %u\n", SPEC_LATEST_SUPPORTED);
419         lwsl_info(" sizeof (*info)        : %u\n", sizeof(*info));
420 #if LWS_POSIX
421         lwsl_info(" SYSTEM_RANDOM_FILEPATH: '%s'\n", SYSTEM_RANDOM_FILEPATH);
422 #endif
423         if (lws_plat_context_early_init())
424                 return NULL;
425
426         context = lws_zalloc(sizeof(struct lws_context));
427         if (!context) {
428                 lwsl_err("No memory for websocket context\n");
429                 return NULL;
430         }
431 #ifndef LWS_NO_DAEMONIZE
432         if (pid_daemon) {
433                 context->started_with_parent = pid_daemon;
434                 lwsl_notice(" Started with daemon pid %d\n", pid_daemon);
435         }
436 #endif
437 #if defined(__ANDROID__)
438                 n = getrlimit ( RLIMIT_NOFILE,&rt);
439                 if (-1 == n) {
440                         lwsl_err("Get RLIMIT_NOFILE failed!\n");
441                         return NULL;
442                 }
443                 context->max_fds = rt.rlim_cur;
444 #else
445                 context->max_fds = getdtablesize();
446 #endif
447
448         if (info->count_threads)
449                 context->count_threads = info->count_threads;
450         else
451                 context->count_threads = 1;
452
453         if (context->count_threads > LWS_MAX_SMP)
454                 context->count_threads = LWS_MAX_SMP;
455
456         context->token_limits = info->token_limits;
457
458         context->options = info->options;
459
460         if (info->timeout_secs)
461                 context->timeout_secs = info->timeout_secs;
462         else
463                 context->timeout_secs = AWAITING_TIMEOUT;
464
465         lwsl_info(" default timeout (secs): %u\n", context->timeout_secs);
466
467         if (info->max_http_header_data)
468                 context->max_http_header_data = info->max_http_header_data;
469         else
470                 context->max_http_header_data = LWS_DEF_HEADER_LEN;
471         if (info->max_http_header_pool)
472                 context->max_http_header_pool = info->max_http_header_pool;
473         else
474                 context->max_http_header_pool = LWS_DEF_HEADER_POOL;
475
476         /*
477          * Allocate the per-thread storage for scratchpad buffers,
478          * and header data pool
479          */
480         for (n = 0; n < context->count_threads; n++) {
481                 context->pt[n].serv_buf = lws_zalloc(LWS_MAX_SOCKET_IO_BUF);
482                 if (!context->pt[n].serv_buf) {
483                         lwsl_err("OOM\n");
484                         return NULL;
485                 }
486
487                 context->pt[n].context = context;
488                 context->pt[n].tid = n;
489                 context->pt[n].http_header_data = lws_malloc(context->max_http_header_data *
490                                                        context->max_http_header_pool);
491                 if (!context->pt[n].http_header_data)
492                         goto bail;
493
494                 context->pt[n].ah_pool = lws_zalloc(sizeof(struct allocated_headers) *
495                                               context->max_http_header_pool);
496                 for (m = 0; m < context->max_http_header_pool; m++)
497                         context->pt[n].ah_pool[m].data =
498                                 (char *)context->pt[n].http_header_data +
499                                 (m * context->max_http_header_data);
500                 if (!context->pt[n].ah_pool)
501                         goto bail;
502
503                 lws_pt_mutex_init(&context->pt[n]);
504         }
505
506         if (info->fd_limit_per_thread)
507                 context->fd_limit_per_thread = info->fd_limit_per_thread;
508         else
509                 context->fd_limit_per_thread = context->max_fds /
510                                                context->count_threads;
511
512         lwsl_notice(" Threads: %d each %d fds\n", context->count_threads,
513                     context->fd_limit_per_thread);
514
515         memset(&wsi, 0, sizeof(wsi));
516         wsi.context = context;
517
518         if (!info->ka_interval && info->ka_time > 0) {
519                 lwsl_err("info->ka_interval can't be 0 if ka_time used\n");
520                 return NULL;
521         }
522
523 #ifdef LWS_USE_LIBEV
524         /* (Issue #264) In order to *avoid breaking backwards compatibility*, we
525          * enable libev mediated SIGINT handling with a default handler of
526          * lws_sigint_cb. The handler can be overridden or disabled
527          * by invoking lws_sigint_cfg after creating the context, but
528          * before invoking lws_initloop:
529          */
530         context->use_ev_sigint = 1;
531         context->lws_ev_sigint_cb = &lws_ev_sigint_cb;
532 #endif /* LWS_USE_LIBEV */
533 #ifdef LWS_USE_LIBUV
534         /* (Issue #264) In order to *avoid breaking backwards compatibility*, we
535          * enable libev mediated SIGINT handling with a default handler of
536          * lws_sigint_cb. The handler can be overridden or disabled
537          * by invoking lws_sigint_cfg after creating the context, but
538          * before invoking lws_initloop:
539          */
540         context->use_ev_sigint = 1;
541         context->lws_uv_sigint_cb = &lws_uv_sigint_cb;
542 #endif
543
544         lwsl_info(" mem: context:         %5u bytes (%d ctx + (%d thr x %d))\n",
545                   sizeof(struct lws_context) +
546                   (context->count_threads * LWS_MAX_SOCKET_IO_BUF),
547                   sizeof(struct lws_context),
548                   context->count_threads,
549                   LWS_MAX_SOCKET_IO_BUF);
550
551         lwsl_info(" mem: http hdr rsvd:   %5u bytes (%u thr x (%u + %u) x %u))\n",
552                     (context->max_http_header_data +
553                      sizeof(struct allocated_headers)) *
554                     context->max_http_header_pool * context->count_threads,
555                     context->count_threads,
556                     context->max_http_header_data,
557                     sizeof(struct allocated_headers),
558                     context->max_http_header_pool);
559         n = sizeof(struct lws_pollfd) * context->count_threads *
560             context->fd_limit_per_thread;
561         context->pt[0].fds = lws_zalloc(n);
562         if (context->pt[0].fds == NULL) {
563                 lwsl_err("OOM allocating %d fds\n", context->max_fds);
564                 goto bail;
565         }
566         lwsl_info(" mem: pollfd map:      %5u\n", n);
567
568 #if LWS_MAX_SMP > 1
569         /* each thread serves his own chunk of fds */
570         for (n = 1; n < (int)info->count_threads; n++)
571                 context->pt[n].fds = context->pt[n - 1].fds +
572                                      context->fd_limit_per_thread;
573 #endif
574
575         if (lws_plat_init(context, info))
576                 goto bail;
577
578         lws_context_init_ssl_library(info);
579
580         /*
581          * if he's not saying he'll make his own vhosts later then act
582          * compatibly and make a default vhost using the data in the info
583          */
584         if (!lws_check_opt(info->options, LWS_SERVER_OPTION_EXPLICIT_VHOSTS))
585                 if (!lws_create_vhost(context, info, NULL)) {
586                         lwsl_err("Failed to create default vhost\n");
587                         return NULL;
588                 }
589
590         lws_context_init_extensions(info, context);
591
592         context->user_space = info->user;
593
594         lwsl_notice(" mem: per-conn:        %5u bytes + protocol rx buf\n",
595                     sizeof(struct lws));
596
597         strcpy(context->canonical_hostname, "unknown");
598         lws_server_get_canonical_hostname(context, info);
599
600         context->uid = info->uid;
601         context->gid = info->gid;
602
603         /*
604          * drop any root privs for this process
605          * to listen on port < 1023 we would have needed root, but now we are
606          * listening, we don't want the power for anything else
607          */
608         if (!lws_check_opt(info->options, LWS_SERVER_OPTION_EXPLICIT_VHOSTS))
609                 lws_plat_drop_app_privileges(info);
610
611         /*
612          * give all extensions a chance to create any per-context
613          * allocations they need
614          */
615         if (info->port != CONTEXT_PORT_NO_LISTEN) {
616                 if (lws_ext_cb_all_exts(context, NULL,
617                         LWS_EXT_CB_SERVER_CONTEXT_CONSTRUCT, NULL, 0) < 0)
618                         goto bail;
619         } else
620                 if (lws_ext_cb_all_exts(context, NULL,
621                         LWS_EXT_CB_CLIENT_CONTEXT_CONSTRUCT, NULL, 0) < 0)
622                         goto bail;
623
624         return context;
625
626 bail:
627         lws_context_destroy(context);
628         return NULL;
629 }
630
631 /**
632  * lws_context_destroy() - Destroy the websocket context
633  * @context:    Websocket context
634  *
635  *      This function closes any active connections and then frees the
636  *      context.  After calling this, any further use of the context is
637  *      undefined.
638  */
639 LWS_VISIBLE void
640 lws_context_destroy(struct lws_context *context)
641 {
642         const struct lws_protocols *protocol = NULL;
643         struct lws_context_per_thread *pt;
644         struct lws_vhost *vh, *vh1;
645         struct lws wsi;
646         int n, m;
647
648         lwsl_notice("%s\n", __func__);
649
650         if (!context)
651                 return;
652
653         m = context->count_threads;
654         context->being_destroyed = 1;
655
656         memset(&wsi, 0, sizeof(wsi));
657         wsi.context = context;
658
659 #ifdef LWS_LATENCY
660         if (context->worst_latency_info[0])
661                 lwsl_notice("Worst latency: %s\n", context->worst_latency_info);
662 #endif
663
664         while (m--) {
665                 pt = &context->pt[m];
666
667                 for (n = 0; (unsigned int)n < context->pt[m].fds_count; n++) {
668                         struct lws *wsi = wsi_from_fd(context, pt->fds[n].fd);
669                         if (!wsi)
670                                 continue;
671
672                         lws_close_free_wsi(wsi,
673                                 LWS_CLOSE_STATUS_NOSTATUS_CONTEXT_DESTROY
674                                 /* no protocol close */);
675                         n--;
676                 }
677         }
678         /*
679          * give all extensions a chance to clean up any per-context
680          * allocations they might have made
681          */
682
683         n = lws_ext_cb_all_exts(context, NULL,
684                                 LWS_EXT_CB_SERVER_CONTEXT_DESTRUCT, NULL, 0);
685
686         n = lws_ext_cb_all_exts(context, NULL,
687                                 LWS_EXT_CB_CLIENT_CONTEXT_DESTRUCT, NULL, 0);
688
689         /*
690          * inform all the protocols that they are done and will have no more
691          * callbacks.
692          *
693          * We can't free things until after the event loop shuts down.
694          */
695         vh = context->vhost_list;
696         while (vh) {
697                 wsi.vhost = vh;
698                 protocol = vh->protocols;
699                 if (protocol) {
700                         n = 0;
701                         while (n < vh->count_protocols) {
702                                 wsi.protocol = protocol;
703                                 protocol->callback(&wsi, LWS_CALLBACK_PROTOCOL_DESTROY,
704                                                    NULL, NULL, 0);
705                                 protocol++;
706                                 n++;
707                         }
708                 }
709
710                 vh = vh->vhost_next;
711         }
712
713         for (n = 0; n < context->count_threads; n++) {
714                 pt = &context->pt[n];
715
716                 lws_libev_destroyloop(context, n);
717                 lws_libuv_destroyloop(context, n);
718
719                 lws_free_set_NULL(context->pt[n].serv_buf);
720                 if (pt->ah_pool)
721                         lws_free(pt->ah_pool);
722                 if (pt->http_header_data)
723                         lws_free(pt->http_header_data);
724         }
725         lws_plat_context_early_destroy(context);
726         lws_ssl_context_destroy(context);
727
728         if (context->pt[0].fds)
729                 lws_free_set_NULL(context->pt[0].fds);
730
731         /* free all the vhost allocations */
732
733         vh = context->vhost_list;
734         while (vh) {
735                 protocol = vh->protocols;
736                 if (protocol) {
737                         n = 0;
738                         while (n < vh->count_protocols) {
739                                 if (vh->protocol_vh_privs &&
740                                     vh->protocol_vh_privs[n]) {
741                                         lws_free(vh->protocol_vh_privs[n]);
742                                         vh->protocol_vh_privs[n] = NULL;
743                                 }
744                                 protocol++;
745                                 n++;
746                         }
747                 }
748                 if (vh->protocol_vh_privs)
749                         lws_free(vh->protocol_vh_privs);
750                 lws_ssl_SSL_CTX_destroy(vh);
751 #ifdef LWS_WITH_PLUGINS
752                 if (context->plugin_list)
753                         lws_free((void *)vh->protocols);
754 #ifndef LWS_NO_EXTENSIONS
755                 if (context->plugin_extension_count)
756                         lws_free((void *)vh->extensions);
757 #endif
758 #endif
759                 vh1 = vh->vhost_next;
760                 lws_free(vh);
761                 vh = vh1;
762         }
763
764         lws_plat_context_late_destroy(context);
765
766         lws_free(context);
767 }