fb9aabfd87ac1b3dc66144c2305856dc3c9a38af
[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, f = !info->pvo;
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                 /* for compatibility, all protocols enabled on vhost if only
245                  * the default vhost exists.  Otherwise only vhosts who ask
246                  * for a protocol get it enabled.
247                  */
248
249                 if (info->options & LWS_SERVER_OPTION_EXPLICIT_VHOSTS)
250                         f = 0;
251
252                 while (plugin) {
253                         for (n = 0; n < plugin->caps.count_protocols; n++) {
254                                 /*
255                                  * for compatibility's sake, no pvo implies
256                                  * allow all protocols
257                                  */
258                                 if (f || lws_vhost_protocol_options(vh,
259                                     plugin->caps.protocols[n].name)) {
260                                         memcpy(&lwsp[m],
261                                                &plugin->caps.protocols[n],
262                                                sizeof(struct lws_protocols));
263                                         m++;
264                                         vh->count_protocols++;
265                                 }
266                         }
267                         plugin = plugin->list;
268                 }
269                 vh->protocols = lwsp;
270         } else
271 #endif
272                 vh->protocols = info->protocols;
273
274         vh->mount_list = mounts;
275
276         lwsl_notice("Creating Vhost '%s' port %d, %d protocols\n",
277                         vh->name, info->port, vh->count_protocols);
278
279         while (mounts) {
280                 lwsl_notice("   mounting %s%s to %s\n",
281                                 mount_protocols[mounts->origin_protocol],
282                                 mounts->origin, mounts->mountpoint);
283                 mounts = mounts->mount_next;
284         }
285
286 #ifndef LWS_NO_EXTENSIONS
287 #ifdef LWS_WITH_PLUGINS
288         if (context->plugin_extension_count) {
289
290                 m = 0;
291                 while (info->extensions && info->extensions[m].callback)
292                         m++;
293
294                 /*
295                  * give the vhost a unified list of extensions including the
296                  * ones that came from plugins
297                  */
298                 vh->extensions = lws_zalloc(sizeof(struct lws_extension) *
299                                            (m +
300                                            context->plugin_extension_count + 1));
301                 if (!vh->extensions)
302                         return NULL;
303
304                 memcpy((struct lws_extension *)vh->extensions, info->extensions,
305                        sizeof(struct lws_extension) * m);
306                 while (plugin) {
307                         memcpy((struct lws_extension *)&vh->extensions[m], plugin->caps.extensions,
308                                sizeof(struct lws_extension) *
309                                plugin->caps.count_extensions);
310                         m += plugin->caps.count_extensions;
311                         plugin = plugin->list;
312                 }
313         } else
314 #endif
315                 vh->extensions = info->extensions;
316 #endif
317
318         vh->listen_port = info->port;
319         vh->http_proxy_port = 0;
320         vh->http_proxy_address[0] = '\0';
321
322         /* either use proxy from info, or try get it from env var */
323
324         if (info->http_proxy_address) {
325                 /* override for backwards compatibility */
326                 if (info->http_proxy_port)
327                         vh->http_proxy_port = info->http_proxy_port;
328                 lws_set_proxy(vh, info->http_proxy_address);
329         } else {
330 #ifdef LWS_HAVE_GETENV
331                 p = getenv("http_proxy");
332                 if (p)
333                         lws_set_proxy(vh, p);
334 #endif
335         }
336
337         vh->ka_time = info->ka_time;
338         vh->ka_interval = info->ka_interval;
339         vh->ka_probes = info->ka_probes;
340
341         if (lws_context_init_server_ssl(info, vh))
342                 goto bail;
343
344         if (lws_context_init_client_ssl(info, vh))
345                 goto bail;
346
347         if (lws_context_init_server(info, vh))
348                 goto bail;
349
350         while (1) {
351                 if (!(*vh1)) {
352                         *vh1 = vh;
353                         break;
354                 }
355                 vh1 = &(*vh1)->vhost_next;
356         };
357
358         return vh;
359
360 bail:
361         lws_free(vh);
362
363         return NULL;
364 }
365
366 /**
367  * lws_create_context() - Create the websocket handler
368  * @info:       pointer to struct with parameters
369  *
370  *      This function creates the listening socket (if serving) and takes care
371  *      of all initialization in one step.
372  *
373  *      After initialization, it returns a struct lws_context * that
374  *      represents this server.  After calling, user code needs to take care
375  *      of calling lws_service() with the context pointer to get the
376  *      server's sockets serviced.  This must be done in the same process
377  *      context as the initialization call.
378  *
379  *      The protocol callback functions are called for a handful of events
380  *      including http requests coming in, websocket connections becoming
381  *      established, and data arriving; it's also called periodically to allow
382  *      async transmission.
383  *
384  *      HTTP requests are sent always to the FIRST protocol in @protocol, since
385  *      at that time websocket protocol has not been negotiated.  Other
386  *      protocols after the first one never see any HTTP callack activity.
387  *
388  *      The server created is a simple http server by default; part of the
389  *      websocket standard is upgrading this http connection to a websocket one.
390  *
391  *      This allows the same server to provide files like scripts and favicon /
392  *      images or whatever over http and dynamic data over websockets all in
393  *      one place; they're all handled in the user callback.
394  */
395 LWS_VISIBLE struct lws_context *
396 lws_create_context(struct lws_context_creation_info *info)
397 {
398         struct lws_context *context = NULL;
399         struct lws wsi;
400 #ifndef LWS_NO_DAEMONIZE
401         int pid_daemon = get_daemonize_pid();
402 #endif
403         int n, m;
404 #if defined(__ANDROID__)
405         struct rlimit rt;
406 #endif
407
408
409         lwsl_notice("Initial logging level %d\n", log_level);
410         lwsl_notice("Libwebsockets version: %s\n", library_version);
411 #if LWS_POSIX
412 #ifdef LWS_USE_IPV6
413         if (!lws_check_opt(info->options, LWS_SERVER_OPTION_DISABLE_IPV6))
414                 lwsl_notice("IPV6 compiled in and enabled\n");
415         else
416                 lwsl_notice("IPV6 compiled in but disabled\n");
417 #else
418         lwsl_notice("IPV6 not compiled in\n");
419 #endif
420         lws_feature_status_libev(info);
421         lws_feature_status_libuv(info);
422 #endif
423         lwsl_info(" LWS_DEF_HEADER_LEN    : %u\n", LWS_DEF_HEADER_LEN);
424         lwsl_info(" LWS_MAX_PROTOCOLS     : %u\n", LWS_MAX_PROTOCOLS);
425         lwsl_info(" LWS_MAX_SMP           : %u\n", LWS_MAX_SMP);
426         lwsl_info(" SPEC_LATEST_SUPPORTED : %u\n", SPEC_LATEST_SUPPORTED);
427         lwsl_info(" sizeof (*info)        : %u\n", sizeof(*info));
428 #if LWS_POSIX
429         lwsl_info(" SYSTEM_RANDOM_FILEPATH: '%s'\n", SYSTEM_RANDOM_FILEPATH);
430 #endif
431         if (lws_plat_context_early_init())
432                 return NULL;
433
434         context = lws_zalloc(sizeof(struct lws_context));
435         if (!context) {
436                 lwsl_err("No memory for websocket context\n");
437                 return NULL;
438         }
439 #ifndef LWS_NO_DAEMONIZE
440         if (pid_daemon) {
441                 context->started_with_parent = pid_daemon;
442                 lwsl_notice(" Started with daemon pid %d\n", pid_daemon);
443         }
444 #endif
445 #if defined(__ANDROID__)
446                 n = getrlimit ( RLIMIT_NOFILE,&rt);
447                 if (-1 == n) {
448                         lwsl_err("Get RLIMIT_NOFILE failed!\n");
449                         return NULL;
450                 }
451                 context->max_fds = rt.rlim_cur;
452 #else
453                 context->max_fds = getdtablesize();
454 #endif
455
456         if (info->count_threads)
457                 context->count_threads = info->count_threads;
458         else
459                 context->count_threads = 1;
460
461         if (context->count_threads > LWS_MAX_SMP)
462                 context->count_threads = LWS_MAX_SMP;
463
464         context->token_limits = info->token_limits;
465
466         context->options = info->options;
467
468         if (info->timeout_secs)
469                 context->timeout_secs = info->timeout_secs;
470         else
471                 context->timeout_secs = AWAITING_TIMEOUT;
472
473         lwsl_info(" default timeout (secs): %u\n", context->timeout_secs);
474
475         if (info->max_http_header_data)
476                 context->max_http_header_data = info->max_http_header_data;
477         else
478                 context->max_http_header_data = LWS_DEF_HEADER_LEN;
479         if (info->max_http_header_pool)
480                 context->max_http_header_pool = info->max_http_header_pool;
481         else
482                 context->max_http_header_pool = LWS_DEF_HEADER_POOL;
483
484         /*
485          * Allocate the per-thread storage for scratchpad buffers,
486          * and header data pool
487          */
488         for (n = 0; n < context->count_threads; n++) {
489                 context->pt[n].serv_buf = lws_zalloc(LWS_MAX_SOCKET_IO_BUF);
490                 if (!context->pt[n].serv_buf) {
491                         lwsl_err("OOM\n");
492                         return NULL;
493                 }
494
495                 context->pt[n].context = context;
496                 context->pt[n].tid = n;
497                 context->pt[n].http_header_data = lws_malloc(context->max_http_header_data *
498                                                        context->max_http_header_pool);
499                 if (!context->pt[n].http_header_data)
500                         goto bail;
501
502                 context->pt[n].ah_pool = lws_zalloc(sizeof(struct allocated_headers) *
503                                               context->max_http_header_pool);
504                 for (m = 0; m < context->max_http_header_pool; m++)
505                         context->pt[n].ah_pool[m].data =
506                                 (char *)context->pt[n].http_header_data +
507                                 (m * context->max_http_header_data);
508                 if (!context->pt[n].ah_pool)
509                         goto bail;
510
511                 lws_pt_mutex_init(&context->pt[n]);
512         }
513
514         if (info->fd_limit_per_thread)
515                 context->fd_limit_per_thread = info->fd_limit_per_thread;
516         else
517                 context->fd_limit_per_thread = context->max_fds /
518                                                context->count_threads;
519
520         lwsl_notice(" Threads: %d each %d fds\n", context->count_threads,
521                     context->fd_limit_per_thread);
522
523         memset(&wsi, 0, sizeof(wsi));
524         wsi.context = context;
525
526         if (!info->ka_interval && info->ka_time > 0) {
527                 lwsl_err("info->ka_interval can't be 0 if ka_time used\n");
528                 return NULL;
529         }
530
531 #ifdef LWS_USE_LIBEV
532         /* (Issue #264) In order to *avoid breaking backwards compatibility*, we
533          * enable libev mediated SIGINT handling with a default handler of
534          * lws_sigint_cb. The handler can be overridden or disabled
535          * by invoking lws_sigint_cfg after creating the context, but
536          * before invoking lws_initloop:
537          */
538         context->use_ev_sigint = 1;
539         context->lws_ev_sigint_cb = &lws_ev_sigint_cb;
540 #endif /* LWS_USE_LIBEV */
541 #ifdef LWS_USE_LIBUV
542         /* (Issue #264) In order to *avoid breaking backwards compatibility*, we
543          * enable libev mediated SIGINT handling with a default handler of
544          * lws_sigint_cb. The handler can be overridden or disabled
545          * by invoking lws_sigint_cfg after creating the context, but
546          * before invoking lws_initloop:
547          */
548         context->use_ev_sigint = 1;
549         context->lws_uv_sigint_cb = &lws_uv_sigint_cb;
550 #endif
551
552         lwsl_info(" mem: context:         %5u bytes (%d ctx + (%d thr x %d))\n",
553                   sizeof(struct lws_context) +
554                   (context->count_threads * LWS_MAX_SOCKET_IO_BUF),
555                   sizeof(struct lws_context),
556                   context->count_threads,
557                   LWS_MAX_SOCKET_IO_BUF);
558
559         lwsl_info(" mem: http hdr rsvd:   %5u bytes (%u thr x (%u + %u) x %u))\n",
560                     (context->max_http_header_data +
561                      sizeof(struct allocated_headers)) *
562                     context->max_http_header_pool * context->count_threads,
563                     context->count_threads,
564                     context->max_http_header_data,
565                     sizeof(struct allocated_headers),
566                     context->max_http_header_pool);
567         n = sizeof(struct lws_pollfd) * context->count_threads *
568             context->fd_limit_per_thread;
569         context->pt[0].fds = lws_zalloc(n);
570         if (context->pt[0].fds == NULL) {
571                 lwsl_err("OOM allocating %d fds\n", context->max_fds);
572                 goto bail;
573         }
574         lwsl_info(" mem: pollfd map:      %5u\n", n);
575
576 #if LWS_MAX_SMP > 1
577         /* each thread serves his own chunk of fds */
578         for (n = 1; n < (int)info->count_threads; n++)
579                 context->pt[n].fds = context->pt[n - 1].fds +
580                                      context->fd_limit_per_thread;
581 #endif
582
583         if (lws_plat_init(context, info))
584                 goto bail;
585
586         lws_context_init_ssl_library(info);
587
588         /*
589          * if he's not saying he'll make his own vhosts later then act
590          * compatibly and make a default vhost using the data in the info
591          */
592         if (!lws_check_opt(info->options, LWS_SERVER_OPTION_EXPLICIT_VHOSTS))
593                 if (!lws_create_vhost(context, info, NULL)) {
594                         lwsl_err("Failed to create default vhost\n");
595                         return NULL;
596                 }
597
598         lws_context_init_extensions(info, context);
599
600         context->user_space = info->user;
601
602         lwsl_notice(" mem: per-conn:        %5u bytes + protocol rx buf\n",
603                     sizeof(struct lws));
604
605         strcpy(context->canonical_hostname, "unknown");
606         lws_server_get_canonical_hostname(context, info);
607
608         context->uid = info->uid;
609         context->gid = info->gid;
610
611         /*
612          * drop any root privs for this process
613          * to listen on port < 1023 we would have needed root, but now we are
614          * listening, we don't want the power for anything else
615          */
616         if (!lws_check_opt(info->options, LWS_SERVER_OPTION_EXPLICIT_VHOSTS))
617                 lws_plat_drop_app_privileges(info);
618
619         /*
620          * give all extensions a chance to create any per-context
621          * allocations they need
622          */
623         if (info->port != CONTEXT_PORT_NO_LISTEN) {
624                 if (lws_ext_cb_all_exts(context, NULL,
625                         LWS_EXT_CB_SERVER_CONTEXT_CONSTRUCT, NULL, 0) < 0)
626                         goto bail;
627         } else
628                 if (lws_ext_cb_all_exts(context, NULL,
629                         LWS_EXT_CB_CLIENT_CONTEXT_CONSTRUCT, NULL, 0) < 0)
630                         goto bail;
631
632         return context;
633
634 bail:
635         lws_context_destroy(context);
636         return NULL;
637 }
638
639 /**
640  * lws_context_destroy() - Destroy the websocket context
641  * @context:    Websocket context
642  *
643  *      This function closes any active connections and then frees the
644  *      context.  After calling this, any further use of the context is
645  *      undefined.
646  */
647 LWS_VISIBLE void
648 lws_context_destroy(struct lws_context *context)
649 {
650         const struct lws_protocols *protocol = NULL;
651         struct lws_context_per_thread *pt;
652         struct lws_vhost *vh, *vh1;
653         struct lws wsi;
654         int n, m;
655
656         lwsl_notice("%s\n", __func__);
657
658         if (!context)
659                 return;
660
661         m = context->count_threads;
662         context->being_destroyed = 1;
663
664         memset(&wsi, 0, sizeof(wsi));
665         wsi.context = context;
666
667 #ifdef LWS_LATENCY
668         if (context->worst_latency_info[0])
669                 lwsl_notice("Worst latency: %s\n", context->worst_latency_info);
670 #endif
671
672         while (m--) {
673                 pt = &context->pt[m];
674
675                 for (n = 0; (unsigned int)n < context->pt[m].fds_count; n++) {
676                         struct lws *wsi = wsi_from_fd(context, pt->fds[n].fd);
677                         if (!wsi)
678                                 continue;
679
680                         lws_close_free_wsi(wsi,
681                                 LWS_CLOSE_STATUS_NOSTATUS_CONTEXT_DESTROY
682                                 /* no protocol close */);
683                         n--;
684                 }
685         }
686         /*
687          * give all extensions a chance to clean up any per-context
688          * allocations they might have made
689          */
690
691         n = lws_ext_cb_all_exts(context, NULL,
692                                 LWS_EXT_CB_SERVER_CONTEXT_DESTRUCT, NULL, 0);
693
694         n = lws_ext_cb_all_exts(context, NULL,
695                                 LWS_EXT_CB_CLIENT_CONTEXT_DESTRUCT, NULL, 0);
696
697         /*
698          * inform all the protocols that they are done and will have no more
699          * callbacks.
700          *
701          * We can't free things until after the event loop shuts down.
702          */
703         vh = context->vhost_list;
704         while (vh) {
705                 wsi.vhost = vh;
706                 protocol = vh->protocols;
707                 if (protocol) {
708                         n = 0;
709                         while (n < vh->count_protocols) {
710                                 wsi.protocol = protocol;
711                                 protocol->callback(&wsi, LWS_CALLBACK_PROTOCOL_DESTROY,
712                                                    NULL, NULL, 0);
713                                 protocol++;
714                                 n++;
715                         }
716                 }
717
718                 vh = vh->vhost_next;
719         }
720
721         for (n = 0; n < context->count_threads; n++) {
722                 pt = &context->pt[n];
723
724                 lws_libev_destroyloop(context, n);
725                 lws_libuv_destroyloop(context, n);
726
727                 lws_free_set_NULL(context->pt[n].serv_buf);
728                 if (pt->ah_pool)
729                         lws_free(pt->ah_pool);
730                 if (pt->http_header_data)
731                         lws_free(pt->http_header_data);
732         }
733         lws_plat_context_early_destroy(context);
734         lws_ssl_context_destroy(context);
735
736         if (context->pt[0].fds)
737                 lws_free_set_NULL(context->pt[0].fds);
738
739         /* free all the vhost allocations */
740
741         vh = context->vhost_list;
742         while (vh) {
743                 protocol = vh->protocols;
744                 if (protocol) {
745                         n = 0;
746                         while (n < vh->count_protocols) {
747                                 if (vh->protocol_vh_privs &&
748                                     vh->protocol_vh_privs[n]) {
749                                         lws_free(vh->protocol_vh_privs[n]);
750                                         vh->protocol_vh_privs[n] = NULL;
751                                 }
752                                 protocol++;
753                                 n++;
754                         }
755                 }
756                 if (vh->protocol_vh_privs)
757                         lws_free(vh->protocol_vh_privs);
758                 lws_ssl_SSL_CTX_destroy(vh);
759 #ifdef LWS_WITH_PLUGINS
760                 if (context->plugin_list)
761                         lws_free((void *)vh->protocols);
762 #ifndef LWS_NO_EXTENSIONS
763                 if (context->plugin_extension_count)
764                         lws_free((void *)vh->extensions);
765 #endif
766 #endif
767                 vh1 = vh->vhost_next;
768                 lws_free(vh);
769                 vh = vh1;
770         }
771
772         lws_plat_context_late_destroy(context);
773
774         lws_free(context);
775 }