/src/libwebsockets/lib/core-net/close.c
Line | Count | Source |
1 | | /* |
2 | | * libwebsockets - small server side websockets and web server implementation |
3 | | * |
4 | | * Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com> |
5 | | * |
6 | | * Permission is hereby granted, free of charge, to any person obtaining a copy |
7 | | * of this software and associated documentation files (the "Software"), to |
8 | | * deal in the Software without restriction, including without limitation the |
9 | | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
10 | | * sell copies of the Software, and to permit persons to whom the Software is |
11 | | * furnished to do so, subject to the following conditions: |
12 | | * |
13 | | * The above copyright notice and this permission notice shall be included in |
14 | | * all copies or substantial portions of the Software. |
15 | | * |
16 | | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
17 | | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
18 | | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
19 | | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
20 | | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
21 | | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
22 | | * IN THE SOFTWARE. |
23 | | */ |
24 | | |
25 | | #include "private-lib-core.h" |
26 | | #include "private-lib-async-dns.h" |
27 | | |
28 | | #if defined(LWS_WITH_CLIENT) |
29 | | static int |
30 | | lws_close_trans_q_leader(struct lws_dll2 *d, void *user) |
31 | 0 | { |
32 | 0 | struct lws *w = lws_container_of(d, struct lws, dll2_cli_txn_queue); |
33 | |
|
34 | 0 | __lws_close_free_wsi(w, (enum lws_close_status)-1, "trans q leader closing"); |
35 | |
|
36 | 0 | return 0; |
37 | 0 | } |
38 | | #endif |
39 | | |
40 | | void |
41 | | __lws_reset_wsi(struct lws *wsi) |
42 | 0 | { |
43 | 0 | if (!wsi) |
44 | 0 | return; |
45 | | |
46 | 0 | #if defined(LWS_WITH_CLIENT) |
47 | | |
48 | 0 | lws_free_set_NULL(wsi->cli_hostname_copy); |
49 | |
|
50 | 0 | #if defined(LWS_WITH_CONMON) |
51 | |
|
52 | 0 | if (wsi->conmon.dns_results_copy) { |
53 | 0 | lws_conmon_addrinfo_destroy(wsi->conmon.dns_results_copy); |
54 | 0 | wsi->conmon.dns_results_copy = NULL; |
55 | 0 | } |
56 | |
|
57 | 0 | wsi->conmon.ciu_dns = |
58 | 0 | wsi->conmon.ciu_sockconn = |
59 | 0 | wsi->conmon.ciu_tls = |
60 | 0 | wsi->conmon.ciu_txn_resp = 0; |
61 | 0 | #endif |
62 | | |
63 | | /* |
64 | | * if we have wsi in our transaction queue, if we are closing we |
65 | | * must go through and close all those first |
66 | | */ |
67 | 0 | if (wsi->a.vhost) { |
68 | | |
69 | | /* we are no longer an active client connection that can piggyback */ |
70 | 0 | lws_dll2_remove(&wsi->dll_cli_active_conns); |
71 | |
|
72 | 0 | lws_dll2_foreach_safe(&wsi->dll2_cli_txn_queue_owner, NULL, |
73 | 0 | lws_close_trans_q_leader); |
74 | | |
75 | | /* |
76 | | * !!! If we are closing, but we have pending pipelined |
77 | | * transaction results we already sent headers for, that's going |
78 | | * to destroy sync for HTTP/1 and leave H2 stream with no live |
79 | | * swsi.` |
80 | | * |
81 | | * However this is normal if we are being closed because the |
82 | | * transaction queue leader is closing. |
83 | | */ |
84 | 0 | lws_dll2_remove(&wsi->dll2_cli_txn_queue); |
85 | 0 | } |
86 | 0 | #endif |
87 | |
|
88 | 0 | if (wsi->a.vhost) { |
89 | 0 | lws_vhost_lock(wsi->a.vhost); |
90 | 0 | lws_dll2_remove(&wsi->vh_awaiting_socket); |
91 | 0 | lws_vhost_unlock(wsi->a.vhost); |
92 | 0 | } |
93 | | |
94 | | /* |
95 | | * Protocol user data may be allocated either internally by lws |
96 | | * or by specified the user. We should only free what we allocated. |
97 | | */ |
98 | 0 | if (wsi->a.protocol && wsi->a.protocol->per_session_data_size && |
99 | 0 | wsi->user_space && !wsi->user_space_externally_allocated) { |
100 | | /* confirm no sul left scheduled in user data itself */ |
101 | 0 | lws_sul_debug_zombies(wsi->a.context, wsi->user_space, |
102 | 0 | wsi->a.protocol->per_session_data_size, __func__); |
103 | 0 | lws_free_set_NULL(wsi->user_space); |
104 | 0 | } |
105 | | |
106 | | /* |
107 | | * Don't let buflist content or state from the wsi's previous life |
108 | | * carry over to the new life |
109 | | */ |
110 | |
|
111 | 0 | lws_buflist_destroy_all_segments(&wsi->buflist); |
112 | 0 | lws_dll2_remove(&wsi->dll_buflist); |
113 | 0 | lws_buflist_destroy_all_segments(&wsi->buflist_out); |
114 | 0 | #if defined(LWS_WITH_UDP) |
115 | 0 | if (wsi->udp) { |
116 | | /* confirm no sul left scheduled in wsi->udp itself */ |
117 | 0 | lws_sul_debug_zombies(wsi->a.context, wsi->udp, |
118 | 0 | sizeof(*wsi->udp), "close udp wsi"); |
119 | 0 | lws_free_set_NULL(wsi->udp); |
120 | 0 | } |
121 | 0 | #endif |
122 | 0 | wsi->retry = 0; |
123 | 0 | wsi->mount_hit = 0; |
124 | |
|
125 | 0 | #if defined(LWS_WITH_CLIENT) |
126 | 0 | lws_dll2_remove(&wsi->dll2_cli_txn_queue); |
127 | 0 | lws_dll2_remove(&wsi->dll_cli_active_conns); |
128 | 0 | if (wsi->cli_hostname_copy) |
129 | 0 | lws_free_set_NULL(wsi->cli_hostname_copy); |
130 | 0 | #endif |
131 | |
|
132 | | #if defined(LWS_WITH_SYS_ASYNC_DNS) |
133 | | lws_async_dns_cancel(wsi); |
134 | | #endif |
135 | |
|
136 | | #if defined(LWS_WITH_HTTP_PROXY) |
137 | | if (wsi->http.buflist_post_body) |
138 | | lws_buflist_destroy_all_segments(&wsi->http.buflist_post_body); |
139 | | #endif |
140 | |
|
141 | 0 | #if defined(LWS_WITH_HTTP_DIGEST_AUTH) |
142 | 0 | if (wsi->http.digest_auth_hdr) { |
143 | 0 | lws_free(wsi->http.digest_auth_hdr); |
144 | 0 | wsi->http.digest_auth_hdr = NULL; |
145 | 0 | } |
146 | 0 | #endif |
147 | |
|
148 | 0 | #if defined(LWS_WITH_SERVER) |
149 | 0 | lws_dll2_remove(&wsi->listen_list); |
150 | 0 | #endif |
151 | |
|
152 | 0 | #if defined(LWS_WITH_CLIENT) |
153 | 0 | if (wsi->a.vhost) |
154 | 0 | lws_dll2_remove(&wsi->dll_cli_active_conns); |
155 | 0 | #endif |
156 | |
|
157 | 0 | __lws_same_vh_protocol_remove(wsi); |
158 | 0 | #if defined(LWS_WITH_CLIENT) |
159 | | //lws_free_set_NULL(wsi->stash); |
160 | 0 | lws_free_set_NULL(wsi->cli_hostname_copy); |
161 | 0 | #endif |
162 | |
|
163 | | #if defined(LWS_WITH_PEER_LIMITS) |
164 | | lws_peer_track_wsi_close(wsi->a.context, wsi->peer); |
165 | | wsi->peer = NULL; |
166 | | #endif |
167 | | |
168 | | /* since we will destroy the wsi, make absolutely sure now */ |
169 | |
|
170 | | #if defined(LWS_WITH_OPENSSL) |
171 | | __lws_ssl_remove_wsi_from_buffered_list(wsi); |
172 | | #endif |
173 | 0 | __lws_wsi_remove_from_sul(wsi); |
174 | |
|
175 | 0 | if (lws_rops_fidx(wsi->role_ops, LWS_ROPS_destroy_role)) |
176 | 0 | lws_rops_func_fidx(wsi->role_ops, |
177 | 0 | LWS_ROPS_destroy_role).destroy_role(wsi); |
178 | |
|
179 | 0 | #if defined(LWS_ROLE_H1) || defined(LWS_ROLE_H2) |
180 | 0 | __lws_header_table_detach(wsi, 0); |
181 | 0 | #endif |
182 | |
|
183 | 0 | #if defined(LWS_ROLE_H2) |
184 | | /* |
185 | | * Let's try to clean out the h2-ness of the wsi |
186 | | */ |
187 | |
|
188 | 0 | memset(&wsi->h2, 0, sizeof(wsi->h2)); |
189 | |
|
190 | 0 | wsi->hdr_parsing_completed = wsi->mux_substream = |
191 | 0 | wsi->upgraded_to_http2 = wsi->mux_stream_immortal = |
192 | 0 | wsi->h2_acked_settings = wsi->seen_nonpseudoheader = |
193 | 0 | wsi->socket_is_permanently_unusable = wsi->favoured_pollin = |
194 | 0 | wsi->already_did_cce = wsi->told_user_closed = |
195 | 0 | wsi->waiting_to_send_close_frame = wsi->close_needs_ack = |
196 | 0 | wsi->parent_pending_cb_on_writable = wsi->seen_zero_length_recv = |
197 | 0 | wsi->close_when_buffered_out_drained = wsi->could_have_pending = 0; |
198 | 0 | #endif |
199 | |
|
200 | 0 | #if defined(LWS_WITH_CLIENT) |
201 | 0 | wsi->do_ws = wsi->chunked = wsi->client_rx_avail = |
202 | 0 | wsi->client_http_body_pending = wsi->transaction_from_pipeline_queue = |
203 | 0 | wsi->keepalive_active = wsi->keepalive_rejected = |
204 | 0 | wsi->redirected_to_get = wsi->client_pipeline = wsi->client_h2_alpn = |
205 | 0 | wsi->client_mux_substream = wsi->client_mux_migrated = |
206 | 0 | wsi->tls_session_reused = wsi->perf_done = 0; |
207 | |
|
208 | 0 | wsi->immortal_substream_count = 0; |
209 | 0 | #endif |
210 | 0 | } |
211 | | |
212 | | /* req cx lock */ |
213 | | |
214 | | void |
215 | | __lws_free_wsi(struct lws *wsi) |
216 | 0 | { |
217 | 0 | struct lws_vhost *vh; |
218 | |
|
219 | 0 | if (!wsi) |
220 | 0 | return; |
221 | | |
222 | 0 | lws_context_assert_lock_held(wsi->a.context); |
223 | | |
224 | | /* just in case */ |
225 | 0 | lws_dll2_remove(&wsi->pre_natal); |
226 | |
|
227 | 0 | #if defined(LWS_WITH_SECURE_STREAMS) |
228 | 0 | if (wsi->for_ss) { |
229 | |
|
230 | | #if defined(LWS_WITH_SECURE_STREAMS_PROXY_API) |
231 | | if (wsi->client_bound_sspc) { |
232 | | lws_sspc_handle_t *h = (lws_sspc_handle_t *) |
233 | | wsi->a.opaque_user_data; |
234 | | if (h) { |
235 | | h->txp_path.priv_onw = NULL; |
236 | | wsi->a.opaque_user_data = NULL; |
237 | | } |
238 | | } else |
239 | | #endif |
240 | 0 | { |
241 | | /* |
242 | | * Make certain it is disconnected from the ss by now |
243 | | */ |
244 | 0 | lws_ss_handle_t *h = (lws_ss_handle_t *) |
245 | 0 | wsi->a.opaque_user_data; |
246 | |
|
247 | 0 | if (h) { |
248 | 0 | h->wsi = NULL; |
249 | 0 | wsi->a.opaque_user_data = NULL; |
250 | 0 | } |
251 | 0 | } |
252 | 0 | } |
253 | 0 | #endif |
254 | |
|
255 | 0 | vh = wsi->a.vhost; |
256 | |
|
257 | 0 | __lws_reset_wsi(wsi); |
258 | 0 | __lws_wsi_remove_from_sul(wsi); |
259 | |
|
260 | 0 | if (vh) |
261 | | /* this may destroy vh */ |
262 | 0 | __lws_vhost_unbind_wsi(wsi); /* req cx + vh lock */ |
263 | |
|
264 | 0 | #if defined(LWS_WITH_CLIENT) |
265 | 0 | if (wsi->stash) |
266 | 0 | lws_free_set_NULL(wsi->stash); |
267 | 0 | #endif |
268 | |
|
269 | 0 | if (wsi->a.context->event_loop_ops->destroy_wsi) |
270 | 0 | wsi->a.context->event_loop_ops->destroy_wsi(wsi); |
271 | |
|
272 | 0 | lwsl_wsi_debug(wsi, "tsi fds count %d\n", |
273 | 0 | wsi->a.context->pt[(int)wsi->tsi].fds_count); |
274 | | |
275 | | /* confirm no sul left scheduled in wsi itself */ |
276 | 0 | lws_sul_debug_zombies(wsi->a.context, wsi, sizeof(*wsi), __func__); |
277 | |
|
278 | 0 | wsi->socket_is_permanently_unusable = 1; // !!! |
279 | |
|
280 | 0 | __lws_lc_untag(wsi->a.context, &wsi->lc); |
281 | 0 | lws_free(wsi); |
282 | 0 | } |
283 | | |
284 | | |
285 | | void |
286 | | lws_remove_child_from_any_parent(struct lws *wsi) |
287 | 0 | { |
288 | 0 | struct lws **pwsi; |
289 | 0 | int seen = 0; |
290 | |
|
291 | 0 | if (!wsi->parent) |
292 | 0 | return; |
293 | | |
294 | | /* detach ourselves from parent's child list */ |
295 | 0 | pwsi = &wsi->parent->child_list; |
296 | 0 | while (*pwsi) { |
297 | 0 | if (*pwsi == wsi) { |
298 | 0 | lwsl_wsi_info(wsi, "detach from parent %s", |
299 | 0 | lws_wsi_tag(wsi->parent)); |
300 | |
|
301 | 0 | if (wsi->parent->a.protocol) |
302 | 0 | wsi->parent->a.protocol->callback(wsi, |
303 | 0 | LWS_CALLBACK_CHILD_CLOSING, |
304 | 0 | wsi->parent->user_space, wsi, 0); |
305 | |
|
306 | 0 | *pwsi = wsi->sibling_list; |
307 | 0 | seen = 1; |
308 | 0 | break; |
309 | 0 | } |
310 | 0 | pwsi = &(*pwsi)->sibling_list; |
311 | 0 | } |
312 | 0 | if (!seen) |
313 | 0 | lwsl_wsi_err(wsi, "failed to detach from parent"); |
314 | |
|
315 | 0 | wsi->parent = NULL; |
316 | 0 | } |
317 | | |
318 | | #if defined(LWS_WITH_CLIENT) |
319 | | void |
320 | | lws_inform_client_conn_fail(struct lws *wsi, void *arg, size_t len) |
321 | 0 | { |
322 | 0 | lws_addrinfo_clean(wsi); |
323 | |
|
324 | 0 | if (wsi->already_did_cce) |
325 | 0 | return; |
326 | | |
327 | 0 | wsi->already_did_cce = 1; |
328 | |
|
329 | 0 | if (!wsi->a.protocol) |
330 | 0 | return; |
331 | | |
332 | 0 | if (!wsi->client_suppress_CONNECTION_ERROR) |
333 | 0 | wsi->a.protocol->callback(wsi, |
334 | 0 | LWS_CALLBACK_CLIENT_CONNECTION_ERROR, |
335 | 0 | wsi->user_space, arg, len); |
336 | 0 | } |
337 | | #endif |
338 | | |
339 | | void |
340 | | lws_addrinfo_clean(struct lws *wsi) |
341 | 0 | { |
342 | 0 | #if defined(LWS_WITH_CLIENT) |
343 | 0 | struct lws_dll2 *d = lws_dll2_get_head(&wsi->dns_sorted_list), *d1; |
344 | |
|
345 | 0 | while (d) { |
346 | 0 | lws_dns_sort_t *r = lws_container_of(d, lws_dns_sort_t, list); |
347 | |
|
348 | 0 | d1 = d->next; |
349 | 0 | lws_dll2_remove(d); |
350 | 0 | lws_free(r); |
351 | |
|
352 | 0 | d = d1; |
353 | 0 | } |
354 | 0 | #endif |
355 | 0 | } |
356 | | |
357 | | /* requires cx and pt lock */ |
358 | | |
359 | | void |
360 | | __lws_close_free_wsi(struct lws *wsi, enum lws_close_status reason, |
361 | | const char *caller) |
362 | 0 | { |
363 | 0 | struct lws_context_per_thread *pt; |
364 | 0 | const struct lws_protocols *pro; |
365 | 0 | #if defined(LWS_WITH_SECURE_STREAMS) |
366 | 0 | lws_ss_handle_t *hh = NULL; |
367 | 0 | #endif |
368 | 0 | struct lws_context *context; |
369 | 0 | struct lws *wsi1, *wsi2; |
370 | 0 | int n, ccb; |
371 | |
|
372 | 0 | if (!wsi) |
373 | 0 | return; |
374 | | |
375 | 0 | lwsl_wsi_info(wsi, "caller: %s", caller); |
376 | |
|
377 | 0 | lws_access_log(wsi); |
378 | |
|
379 | 0 | if (!lws_dll2_is_detached(&wsi->dll_buflist)) |
380 | 0 | lwsl_wsi_info(wsi, "going down with stuff in buflist"); |
381 | |
|
382 | 0 | context = wsi->a.context; |
383 | 0 | pt = &context->pt[(int)wsi->tsi]; |
384 | |
|
385 | 0 | if (pt->pipe_wsi == wsi) { |
386 | 0 | lws_plat_pipe_close(pt->pipe_wsi); |
387 | 0 | pt->pipe_wsi = NULL; |
388 | 0 | } |
389 | |
|
390 | | #if defined(LWS_WITH_SYS_METRICS) && \ |
391 | | (defined(LWS_WITH_CLIENT) || defined(LWS_WITH_SERVER)) |
392 | | /* wsi level: only reports if dangling caliper */ |
393 | | if (wsi->cal_conn.mt && wsi->cal_conn.us_start) { |
394 | | if ((lws_metrics_priv_to_pub(wsi->cal_conn.mt)->flags) & LWSMTFL_REPORT_HIST) { |
395 | | lws_metrics_caliper_report_hist(wsi->cal_conn, (struct lws *)NULL); |
396 | | } else { |
397 | | lws_metrics_caliper_report(wsi->cal_conn, METRES_NOGO); |
398 | | lws_metrics_caliper_done(wsi->cal_conn); |
399 | | } |
400 | | } else |
401 | | lws_metrics_caliper_done(wsi->cal_conn); |
402 | | #endif |
403 | |
|
404 | | #if defined(LWS_WITH_SYS_ASYNC_DNS) |
405 | | /* is this wsi handling the interface to a dns server? */ |
406 | | { |
407 | | lws_async_dns_server_t *dsrv = |
408 | | __lws_async_dns_server_find_wsi(&context->async_dns, wsi); |
409 | | |
410 | | if (dsrv) |
411 | | dsrv->wsi = NULL; |
412 | | } |
413 | | #endif |
414 | |
|
415 | 0 | lws_pt_assert_lock_held(pt); |
416 | |
|
417 | 0 | #if defined(LWS_WITH_CLIENT) |
418 | |
|
419 | 0 | lws_free_set_NULL(wsi->cli_hostname_copy); |
420 | 0 | wsi->client_mux_substream_was = wsi->client_mux_substream; |
421 | |
|
422 | 0 | lws_addrinfo_clean(wsi); |
423 | 0 | #endif |
424 | |
|
425 | 0 | #if defined(LWS_WITH_HTTP2) |
426 | 0 | if (wsi->mux_stream_immortal) |
427 | 0 | lws_http_close_immortal(wsi); |
428 | 0 | #endif |
429 | | |
430 | | /* if we have children, close them first */ |
431 | 0 | if (wsi->child_list) { |
432 | 0 | wsi2 = wsi->child_list; |
433 | 0 | while (wsi2) { |
434 | 0 | wsi1 = wsi2->sibling_list; |
435 | | // wsi2->parent = NULL; |
436 | | /* stop it doing shutdown processing */ |
437 | 0 | wsi2->socket_is_permanently_unusable = 1; |
438 | 0 | __lws_close_free_wsi(wsi2, reason, |
439 | 0 | "general child recurse"); |
440 | 0 | wsi2 = wsi1; |
441 | 0 | } |
442 | 0 | wsi->child_list = NULL; |
443 | 0 | } |
444 | |
|
445 | 0 | #if defined(LWS_ROLE_RAW_FILE) |
446 | 0 | if (wsi->role_ops == &role_ops_raw_file) { |
447 | 0 | lws_remove_child_from_any_parent(wsi); |
448 | 0 | __remove_wsi_socket_from_fds(wsi); |
449 | 0 | if (wsi->a.protocol) |
450 | 0 | wsi->a.protocol->callback(wsi, wsi->role_ops->close_cb[0], |
451 | 0 | wsi->user_space, NULL, 0); |
452 | 0 | goto async_close; |
453 | 0 | } |
454 | 0 | #endif |
455 | | |
456 | 0 | wsi->wsistate_pre_close = wsi->wsistate; |
457 | |
|
458 | | #ifdef LWS_WITH_CGI |
459 | | if (wsi->role_ops == &role_ops_cgi) { |
460 | | |
461 | | // lwsl_debug("%s: closing stdwsi index %d\n", __func__, (int)wsi->lsp_channel); |
462 | | |
463 | | /* we are not a network connection, but a handler for CGI io */ |
464 | | if (wsi->parent && wsi->parent->http.cgi) { |
465 | | |
466 | | /* |
467 | | * We need to keep the logical cgi around so we can |
468 | | * drain it |
469 | | */ |
470 | | |
471 | | // if (wsi->parent->child_list == wsi && !wsi->sibling_list) |
472 | | // lws_cgi_remove_and_kill(wsi->parent); |
473 | | |
474 | | /* end the binding between us and network connection */ |
475 | | if (wsi->parent->http.cgi && wsi->parent->http.cgi->lsp) |
476 | | wsi->parent->http.cgi->lsp->stdwsi[(int)wsi->lsp_channel] = |
477 | | NULL; |
478 | | } |
479 | | wsi->socket_is_permanently_unusable = 1; |
480 | | |
481 | | goto just_kill_connection; |
482 | | } |
483 | | |
484 | | if (wsi->http.cgi) |
485 | | lws_cgi_remove_and_kill(wsi); |
486 | | #endif |
487 | |
|
488 | 0 | #if defined(LWS_WITH_CLIENT) |
489 | 0 | if (!wsi->close_is_redirect) |
490 | 0 | lws_free_set_NULL(wsi->stash); |
491 | 0 | #endif |
492 | |
|
493 | 0 | if (wsi->role_ops == &role_ops_raw_skt) { |
494 | 0 | wsi->socket_is_permanently_unusable = 1; |
495 | 0 | goto just_kill_connection; |
496 | 0 | } |
497 | 0 | #if defined(LWS_WITH_FILE_OPS) && (defined(LWS_ROLE_H1) || defined(LWS_ROLE_H2)) |
498 | 0 | if (lwsi_role_http(wsi) && lwsi_role_server(wsi) && |
499 | 0 | wsi->http.fop_fd != NULL) |
500 | 0 | lws_vfs_file_close(&wsi->http.fop_fd); |
501 | 0 | #endif |
502 | |
|
503 | 0 | if (lwsi_state(wsi) == LRS_DEAD_SOCKET) |
504 | 0 | return; |
505 | | |
506 | 0 | if (wsi->socket_is_permanently_unusable || |
507 | 0 | reason == LWS_CLOSE_STATUS_NOSTATUS_CONTEXT_DESTROY || |
508 | 0 | lwsi_state(wsi) == LRS_SHUTDOWN) |
509 | 0 | goto just_kill_connection; |
510 | | |
511 | 0 | switch (lwsi_state_PRE_CLOSE(wsi)) { |
512 | 0 | case LRS_DEAD_SOCKET: |
513 | 0 | return; |
514 | | |
515 | | /* we tried the polite way... */ |
516 | 0 | case LRS_WAITING_TO_SEND_CLOSE: |
517 | 0 | case LRS_AWAITING_CLOSE_ACK: |
518 | 0 | case LRS_RETURNED_CLOSE: |
519 | 0 | goto just_kill_connection; |
520 | | |
521 | 0 | case LRS_FLUSHING_BEFORE_CLOSE: |
522 | 0 | if (lws_has_buffered_out(wsi) |
523 | | #if defined(LWS_WITH_HTTP_STREAM_COMPRESSION) |
524 | | || wsi->http.comp_ctx.buflist_comp || |
525 | | wsi->http.comp_ctx.may_have_more |
526 | | #endif |
527 | 0 | ) { |
528 | 0 | lws_callback_on_writable(wsi); |
529 | 0 | return; |
530 | 0 | } |
531 | 0 | lwsl_wsi_info(wsi, " end LRS_FLUSHING_BEFORE_CLOSE"); |
532 | 0 | goto just_kill_connection; |
533 | 0 | default: |
534 | 0 | if (lws_has_buffered_out(wsi) |
535 | | #if defined(LWS_WITH_HTTP_STREAM_COMPRESSION) |
536 | | || wsi->http.comp_ctx.buflist_comp || |
537 | | wsi->http.comp_ctx.may_have_more |
538 | | #endif |
539 | 0 | ) { |
540 | 0 | lwsl_wsi_info(wsi, "LRS_FLUSHING_BEFORE_CLOSE"); |
541 | 0 | lwsi_set_state(wsi, LRS_FLUSHING_BEFORE_CLOSE); |
542 | 0 | __lws_set_timeout(wsi, |
543 | 0 | PENDING_FLUSH_STORED_SEND_BEFORE_CLOSE, 5); |
544 | 0 | return; |
545 | 0 | } |
546 | 0 | break; |
547 | 0 | } |
548 | | |
549 | 0 | if (lwsi_state(wsi) == LRS_WAITING_CONNECT || |
550 | 0 | lwsi_state(wsi) == LRS_WAITING_DNS || |
551 | 0 | lwsi_state(wsi) == LRS_H1C_ISSUE_HANDSHAKE) |
552 | 0 | goto just_kill_connection; |
553 | | |
554 | 0 | if (!wsi->told_user_closed && wsi->user_space && wsi->a.protocol && |
555 | 0 | wsi->protocol_bind_balance) { |
556 | 0 | wsi->a.protocol->callback(wsi, |
557 | 0 | wsi->role_ops->protocol_unbind_cb[ |
558 | 0 | !!lwsi_role_server(wsi)], |
559 | 0 | wsi->user_space, (void *)__func__, 0); |
560 | 0 | wsi->protocol_bind_balance = 0; |
561 | 0 | } |
562 | | |
563 | | /* |
564 | | * signal we are closing, lws_write will |
565 | | * add any necessary version-specific stuff. If the write fails, |
566 | | * no worries we are closing anyway. If we didn't initiate this |
567 | | * close, then our state has been changed to |
568 | | * LRS_RETURNED_CLOSE and we will skip this. |
569 | | * |
570 | | * Likewise if it's a second call to close this connection after we |
571 | | * sent the close indication to the peer already, we are in state |
572 | | * LRS_AWAITING_CLOSE_ACK and will skip doing this a second time. |
573 | | */ |
574 | |
|
575 | 0 | if (lws_rops_fidx(wsi->role_ops, LWS_ROPS_close_via_role_protocol) && |
576 | 0 | lws_rops_func_fidx(wsi->role_ops, LWS_ROPS_close_via_role_protocol). |
577 | 0 | close_via_role_protocol(wsi, reason)) { |
578 | 0 | lwsl_wsi_info(wsi, "close_via_role took over (sockfd %d)", |
579 | 0 | wsi->desc.sockfd); |
580 | 0 | return; |
581 | 0 | } |
582 | | |
583 | 0 | just_kill_connection: |
584 | |
|
585 | 0 | lwsl_wsi_debug(wsi, "real just_kill_connection A: (sockfd %d)", |
586 | 0 | wsi->desc.sockfd); |
587 | |
|
588 | | #if defined(LWS_WITH_THREADPOOL) && defined(LWS_HAVE_PTHREAD_H) |
589 | | lws_threadpool_wsi_closing(wsi); |
590 | | #endif |
591 | |
|
592 | 0 | #if defined(LWS_WITH_FILE_OPS) && (defined(LWS_ROLE_H1) || defined(LWS_ROLE_H2)) |
593 | 0 | if (lwsi_role_http(wsi) && lwsi_role_server(wsi) && |
594 | 0 | wsi->http.fop_fd != NULL) |
595 | 0 | lws_vfs_file_close(&wsi->http.fop_fd); |
596 | 0 | #endif |
597 | |
|
598 | 0 | lws_sul_cancel(&wsi->sul_connect_timeout); |
599 | | #if defined(WIN32) |
600 | | lws_sul_cancel(&wsi->win32_sul_connect_async_check); |
601 | | #endif |
602 | | #if defined(LWS_WITH_SYS_ASYNC_DNS) |
603 | | lws_async_dns_cancel(wsi); |
604 | | #endif |
605 | |
|
606 | | #if defined(LWS_WITH_HTTP_PROXY) |
607 | | if (wsi->http.buflist_post_body) |
608 | | lws_buflist_destroy_all_segments(&wsi->http.buflist_post_body); |
609 | | #endif |
610 | 0 | #if defined(LWS_WITH_UDP) |
611 | 0 | if (wsi->udp) { |
612 | | /* confirm no sul left scheduled in wsi->udp itself */ |
613 | 0 | lws_sul_debug_zombies(wsi->a.context, wsi->udp, |
614 | 0 | sizeof(*wsi->udp), "close udp wsi"); |
615 | |
|
616 | 0 | lws_free_set_NULL(wsi->udp); |
617 | 0 | } |
618 | 0 | #endif |
619 | |
|
620 | 0 | if (lws_rops_fidx(wsi->role_ops, LWS_ROPS_close_kill_connection)) |
621 | 0 | lws_rops_func_fidx(wsi->role_ops, |
622 | 0 | LWS_ROPS_close_kill_connection). |
623 | 0 | close_kill_connection(wsi, reason); |
624 | |
|
625 | 0 | n = 0; |
626 | |
|
627 | 0 | if (!wsi->told_user_closed && wsi->user_space && |
628 | 0 | wsi->protocol_bind_balance && wsi->a.protocol) { |
629 | 0 | lwsl_debug("%s: %s: DROP_PROTOCOL %s\n", __func__, lws_wsi_tag(wsi), |
630 | 0 | wsi->a.protocol ? wsi->a.protocol->name: "NULL"); |
631 | 0 | if (wsi->a.protocol) |
632 | 0 | wsi->a.protocol->callback(wsi, |
633 | 0 | wsi->role_ops->protocol_unbind_cb[ |
634 | 0 | !!lwsi_role_server(wsi)], |
635 | 0 | wsi->user_space, (void *)__func__, 0); |
636 | 0 | wsi->protocol_bind_balance = 0; |
637 | 0 | } |
638 | |
|
639 | 0 | #if defined(LWS_WITH_CLIENT) |
640 | 0 | if (( |
641 | 0 | #if defined(LWS_ROLE_WS) |
642 | | /* |
643 | | * If our goal is a ws upgrade, effectively we did not reach |
644 | | * ESTABLISHED if we did not get the upgrade server reply |
645 | | */ |
646 | 0 | (lwsi_state(wsi) == LRS_WAITING_SERVER_REPLY && |
647 | 0 | wsi->role_ops == &role_ops_ws) || |
648 | 0 | #endif |
649 | 0 | lwsi_state(wsi) == LRS_WAITING_DNS || |
650 | 0 | lwsi_state(wsi) == LRS_WAITING_CONNECT) && |
651 | 0 | !wsi->already_did_cce && wsi->a.protocol && |
652 | 0 | !wsi->close_is_redirect) { |
653 | 0 | static const char _reason[] = "closed before established"; |
654 | |
|
655 | 0 | lwsl_wsi_debug(wsi, "closing in unestablished state 0x%x", |
656 | 0 | lwsi_state(wsi)); |
657 | 0 | wsi->socket_is_permanently_unusable = 1; |
658 | |
|
659 | 0 | lws_inform_client_conn_fail(wsi, |
660 | 0 | (void *)_reason, sizeof(_reason) - 1); |
661 | 0 | } |
662 | 0 | #endif |
663 | | |
664 | | /* |
665 | | * Testing with ab shows that we have to stage the socket close when |
666 | | * the system is under stress... shutdown any further TX, change the |
667 | | * state to one that won't emit anything more, and wait with a timeout |
668 | | * for the POLLIN to show a zero-size rx before coming back and doing |
669 | | * the actual close. |
670 | | */ |
671 | 0 | if (wsi->role_ops != &role_ops_raw_skt && !lwsi_role_client(wsi) && |
672 | 0 | lwsi_state(wsi) != LRS_SHUTDOWN && |
673 | 0 | lwsi_state(wsi) != LRS_UNCONNECTED && |
674 | 0 | reason != LWS_CLOSE_STATUS_NOSTATUS_CONTEXT_DESTROY && |
675 | 0 | !wsi->socket_is_permanently_unusable) { |
676 | |
|
677 | 0 | #if defined(LWS_WITH_TLS) |
678 | 0 | if (lws_is_ssl(wsi) && wsi->tls.ssl) { |
679 | 0 | n = 0; |
680 | 0 | switch (__lws_tls_shutdown(wsi)) { |
681 | 0 | case LWS_SSL_CAPABLE_DONE: |
682 | 0 | case LWS_SSL_CAPABLE_ERROR: |
683 | 0 | case LWS_SSL_CAPABLE_MORE_SERVICE_READ: |
684 | 0 | case LWS_SSL_CAPABLE_MORE_SERVICE_WRITE: |
685 | 0 | case LWS_SSL_CAPABLE_MORE_SERVICE: |
686 | 0 | if (wsi->lsp_channel++ == 8) { |
687 | 0 | lwsl_wsi_info(wsi, "avoiding shutdown spin"); |
688 | 0 | lwsi_set_state(wsi, LRS_SHUTDOWN); |
689 | 0 | } |
690 | 0 | break; |
691 | 0 | } |
692 | 0 | } else |
693 | 0 | #endif |
694 | 0 | { |
695 | 0 | lwsl_info("%s: shutdown conn: %s (sk %d, state 0x%x)\n", |
696 | 0 | __func__, lws_wsi_tag(wsi), (int)(lws_intptr_t)wsi->desc.sockfd, |
697 | 0 | lwsi_state(wsi)); |
698 | 0 | if (!wsi->socket_is_permanently_unusable && |
699 | 0 | lws_socket_is_valid(wsi->desc.sockfd)) { |
700 | 0 | wsi->socket_is_permanently_unusable = 1; |
701 | 0 | n = shutdown(wsi->desc.sockfd, SHUT_WR); |
702 | 0 | } |
703 | 0 | } |
704 | 0 | if (n) |
705 | 0 | lwsl_wsi_debug(wsi, "closing: shutdown (state 0x%x) ret %d", |
706 | 0 | lwsi_state(wsi), LWS_ERRNO); |
707 | | |
708 | | /* |
709 | | * This causes problems on WINCE / ESP32 with disconnection |
710 | | * when the events are half closing connection |
711 | | */ |
712 | 0 | #if !defined(_WIN32_WCE) && !defined(LWS_PLAT_FREERTOS) |
713 | | /* libuv: no event available to guarantee completion */ |
714 | 0 | if (!wsi->socket_is_permanently_unusable && |
715 | 0 | #if defined(LWS_WITH_CLIENT) |
716 | 0 | !wsi->close_is_redirect && |
717 | 0 | #endif |
718 | 0 | lws_socket_is_valid(wsi->desc.sockfd) && |
719 | 0 | lwsi_state(wsi) != LRS_SHUTDOWN && |
720 | 0 | (context->event_loop_ops->flags & LELOF_ISPOLL)) { |
721 | 0 | __lws_change_pollfd(wsi, LWS_POLLOUT, LWS_POLLIN); |
722 | 0 | lwsi_set_state(wsi, LRS_SHUTDOWN); |
723 | 0 | __lws_set_timeout(wsi, PENDING_TIMEOUT_SHUTDOWN_FLUSH, |
724 | 0 | (int)context->timeout_secs); |
725 | |
|
726 | 0 | return; |
727 | 0 | } |
728 | 0 | #endif |
729 | 0 | } |
730 | | |
731 | 0 | lwsl_wsi_info(wsi, "real just_kill_connection: sockfd %d\n", |
732 | 0 | wsi->desc.sockfd); |
733 | |
|
734 | | #ifdef LWS_WITH_HUBBUB |
735 | | if (wsi->http.rw) { |
736 | | lws_rewrite_destroy(wsi->http.rw); |
737 | | wsi->http.rw = NULL; |
738 | | } |
739 | | #endif |
740 | |
|
741 | 0 | if (wsi->http.pending_return_headers) |
742 | 0 | lws_free_set_NULL(wsi->http.pending_return_headers); |
743 | | |
744 | | /* |
745 | | * we won't be servicing or receiving anything further from this guy |
746 | | * delete socket from the internal poll list if still present |
747 | | */ |
748 | 0 | __lws_ssl_remove_wsi_from_buffered_list(wsi); |
749 | 0 | __lws_wsi_remove_from_sul(wsi); |
750 | | |
751 | | //if (wsi->told_event_loop_closed) // cgi std close case (dummy-callback) |
752 | | // return; |
753 | | |
754 | | /* checking return redundant since we anyway close */ |
755 | 0 | __remove_wsi_socket_from_fds(wsi); |
756 | |
|
757 | 0 | lwsi_set_state(wsi, LRS_DEAD_SOCKET); |
758 | 0 | lws_buflist_destroy_all_segments(&wsi->buflist); |
759 | 0 | lws_dll2_remove(&wsi->dll_buflist); |
760 | |
|
761 | 0 | if (lws_rops_fidx(wsi->role_ops, LWS_ROPS_close_role)) |
762 | 0 | lws_rops_func_fidx(wsi->role_ops, LWS_ROPS_close_role). |
763 | 0 | close_role(pt, wsi); |
764 | | |
765 | | /* tell the user it's all over for this guy */ |
766 | |
|
767 | 0 | ccb = 0; |
768 | 0 | if ((lwsi_state_est_PRE_CLOSE(wsi) || |
769 | | /* raw skt adopted but didn't complete tls hs should CLOSE */ |
770 | 0 | (wsi->role_ops == &role_ops_raw_skt && !lwsi_role_client(wsi)) || |
771 | 0 | lwsi_state_PRE_CLOSE(wsi) == LRS_WAITING_SERVER_REPLY) && |
772 | 0 | !wsi->told_user_closed && |
773 | 0 | wsi->role_ops->close_cb[lwsi_role_server(wsi)]) { |
774 | 0 | if (!wsi->upgraded_to_http2 || !lwsi_role_client(wsi)) |
775 | 0 | ccb = 1; |
776 | | /* |
777 | | * The network wsi for a client h2 connection shouldn't |
778 | | * call back for its role: the child stream connections |
779 | | * own the role. Otherwise h2 will call back closed |
780 | | * one too many times as the children do it and then |
781 | | * the closing network stream. |
782 | | */ |
783 | 0 | } |
784 | |
|
785 | 0 | if (!wsi->told_user_closed && |
786 | 0 | !lws_dll2_is_detached(&wsi->vh_awaiting_socket)) |
787 | | /* |
788 | | * He's a guy who go started with dns, but failed or is |
789 | | * caught with a shutdown before he got the result. We have |
790 | | * to issclient_mux_substream_wasue him a close cb |
791 | | */ |
792 | 0 | ccb = 1; |
793 | |
|
794 | 0 | lwsl_wsi_info(wsi, "cce=%d", ccb); |
795 | |
|
796 | 0 | pro = wsi->a.protocol; |
797 | |
|
798 | 0 | if (wsi->already_did_cce) |
799 | | /* |
800 | | * If we handled this by CLIENT_CONNECTION_ERROR, it's |
801 | | * mutually exclusive with CLOSE |
802 | | */ |
803 | 0 | ccb = 0; |
804 | |
|
805 | 0 | #if defined(LWS_WITH_CLIENT) |
806 | 0 | if (!wsi->close_is_redirect && !ccb && |
807 | 0 | (lwsi_state_PRE_CLOSE(wsi) & LWSIFS_NOT_EST) && |
808 | 0 | lwsi_role_client(wsi)) { |
809 | 0 | lws_inform_client_conn_fail(wsi, "Closed before conn", 18); |
810 | 0 | } |
811 | 0 | #endif |
812 | 0 | if (ccb |
813 | 0 | #if defined(LWS_WITH_CLIENT) |
814 | 0 | && !wsi->close_is_redirect |
815 | 0 | #endif |
816 | 0 | ) { |
817 | |
|
818 | 0 | if (!wsi->a.protocol && wsi->a.vhost && wsi->a.vhost->protocols) |
819 | 0 | pro = &wsi->a.vhost->protocols[0]; |
820 | |
|
821 | 0 | if (pro && pro->callback) |
822 | 0 | pro->callback(wsi, |
823 | 0 | wsi->role_ops->close_cb[lwsi_role_server(wsi)], |
824 | 0 | wsi->user_space, NULL, 0); |
825 | 0 | wsi->told_user_closed = 1; |
826 | 0 | } |
827 | |
|
828 | 0 | #if defined(LWS_ROLE_RAW_FILE) |
829 | 0 | async_close: |
830 | 0 | #endif |
831 | |
|
832 | 0 | #if defined(LWS_WITH_SECURE_STREAMS) |
833 | 0 | if (wsi->for_ss) { |
834 | 0 | lwsl_wsi_debug(wsi, "for_ss"); |
835 | | /* |
836 | | * We were adopted for a particular ss, but, eg, we may not |
837 | | * have succeeded with the connection... we are closing which is |
838 | | * good, but we have to invalidate any pointer the related ss |
839 | | * handle may be holding on us |
840 | | */ |
841 | | #if defined(LWS_WITH_SECURE_STREAMS_PROXY_API) |
842 | | |
843 | | if (wsi->client_proxy_onward) { |
844 | | /* |
845 | | * We are an onward proxied wsi at the proxy, |
846 | | * opaque is proxing "conn", we must remove its pointer |
847 | | * to us since we are destroying |
848 | | */ |
849 | | lws_proxy_clean_conn_ss(wsi); |
850 | | } else |
851 | | |
852 | | if (wsi->client_bound_sspc) { |
853 | | lws_sspc_handle_t *h = (lws_sspc_handle_t *)wsi->a.opaque_user_data; |
854 | | |
855 | | if (h) { // && (h->info.flags & LWSSSINFLAGS_ACCEPTED)) { |
856 | | |
857 | | #if defined(LWS_WITH_SYS_METRICS) |
858 | | /* |
859 | | * If any hanging caliper measurement, dump it, and free any tags |
860 | | */ |
861 | | lws_metrics_caliper_report_hist(h->cal_txn, (struct lws *)NULL); |
862 | | #endif |
863 | | |
864 | | h->txp_path.priv_onw = NULL; |
865 | | //wsi->a.opaque_user_data = NULL; |
866 | | } |
867 | | } else |
868 | | #endif |
869 | 0 | { |
870 | 0 | hh = (lws_ss_handle_t *)wsi->a.opaque_user_data; |
871 | |
|
872 | 0 | if (hh) { // && (h->info.flags & LWSSSINFLAGS_ACCEPTED)) { |
873 | | |
874 | | /* |
875 | | * ss level: only reports if dangling caliper |
876 | | * not already reported |
877 | | */ |
878 | 0 | lws_metrics_caliper_report_hist(hh->cal_txn, wsi); |
879 | |
|
880 | 0 | hh->wsi = NULL; |
881 | 0 | wsi->a.opaque_user_data = NULL; |
882 | 0 | } |
883 | 0 | } |
884 | 0 | } |
885 | 0 | #endif |
886 | | |
887 | |
|
888 | 0 | lws_remove_child_from_any_parent(wsi); |
889 | 0 | wsi->socket_is_permanently_unusable = 1; |
890 | |
|
891 | 0 | if (wsi->a.context->event_loop_ops->wsi_logical_close) |
892 | 0 | if (wsi->a.context->event_loop_ops->wsi_logical_close(wsi)) |
893 | 0 | return; |
894 | | |
895 | 0 | __lws_close_free_wsi_final(wsi); |
896 | |
|
897 | 0 | #if defined(LWS_WITH_SECURE_STREAMS) |
898 | 0 | if (hh && hh->ss_dangling_connected && |
899 | 0 | lws_ss_event_helper(hh, LWSSSCS_DISCONNECTED) == LWSSSSRET_DESTROY_ME) |
900 | 0 | lws_ss_destroy(&hh); |
901 | 0 | #endif |
902 | 0 | } |
903 | | |
904 | | |
905 | | /* cx + vh lock */ |
906 | | |
907 | | void |
908 | | __lws_close_free_wsi_final(struct lws *wsi) |
909 | 0 | { |
910 | 0 | int n; |
911 | |
|
912 | 0 | if (!wsi->shadow && |
913 | 0 | lws_socket_is_valid(wsi->desc.sockfd) && !lws_ssl_close(wsi)) { |
914 | 0 | lwsl_wsi_debug(wsi, "fd %d", wsi->desc.sockfd); |
915 | | |
916 | | /* |
917 | | * if this is the pt pipe, skip the actual close, |
918 | | * go through the motions though so we will reach 0 open wsi |
919 | | * on the pt, and trigger the pt destroy to close the pipe fds |
920 | | */ |
921 | 0 | if (!lws_plat_pipe_is_fd_assocated(wsi->a.context, wsi->tsi, |
922 | 0 | wsi->desc.sockfd)) { |
923 | 0 | n = compatible_close(wsi->desc.sockfd); |
924 | 0 | if (n) |
925 | 0 | lwsl_wsi_debug(wsi, "closing: close ret %d", |
926 | 0 | LWS_ERRNO); |
927 | 0 | } |
928 | |
|
929 | 0 | __remove_wsi_socket_from_fds(wsi); |
930 | 0 | if (lws_socket_is_valid(wsi->desc.sockfd)) |
931 | 0 | delete_from_fd(wsi->a.context, wsi->desc.sockfd); |
932 | |
|
933 | 0 | #if !defined(LWS_PLAT_FREERTOS) && !defined(WIN32) && !defined(LWS_PLAT_OPTEE) |
934 | 0 | delete_from_fdwsi(wsi->a.context, wsi); |
935 | 0 | #endif |
936 | |
|
937 | 0 | sanity_assert_no_sockfd_traces(wsi->a.context, wsi->desc.sockfd); |
938 | 0 | } |
939 | | |
940 | | /* ... if we're closing the cancel pipe, account for it */ |
941 | |
|
942 | 0 | { |
943 | 0 | struct lws_context_per_thread *pt = |
944 | 0 | &wsi->a.context->pt[(int)wsi->tsi]; |
945 | |
|
946 | 0 | if (pt->pipe_wsi == wsi) |
947 | 0 | pt->pipe_wsi = NULL; |
948 | 0 | if (pt->dummy_pipe_fds[0] == wsi->desc.sockfd) |
949 | 0 | { |
950 | 0 | #if !defined(LWS_PLAT_FREERTOS) |
951 | 0 | pt->dummy_pipe_fds[0] = LWS_SOCK_INVALID; |
952 | 0 | #endif |
953 | 0 | } |
954 | 0 | } |
955 | |
|
956 | 0 | wsi->desc.sockfd = LWS_SOCK_INVALID; |
957 | |
|
958 | 0 | #if defined(LWS_WITH_CLIENT) |
959 | 0 | lws_free_set_NULL(wsi->cli_hostname_copy); |
960 | 0 | if (wsi->close_is_redirect) { |
961 | |
|
962 | 0 | wsi->close_is_redirect = 0; |
963 | |
|
964 | 0 | lwsl_wsi_info(wsi, "picking up redirection"); |
965 | |
|
966 | 0 | lws_role_transition(wsi, LWSIFR_CLIENT, LRS_UNCONNECTED, |
967 | 0 | &role_ops_h1); |
968 | |
|
969 | 0 | #if defined(LWS_WITH_HTTP2) |
970 | 0 | if (wsi->client_mux_substream_was) |
971 | 0 | wsi->h2.END_STREAM = wsi->h2.END_HEADERS = 0; |
972 | 0 | #endif |
973 | 0 | #if defined(LWS_ROLE_H2) || defined(LWS_ROLE_MQTT) |
974 | 0 | if (wsi->mux.parent_wsi) { |
975 | 0 | lws_wsi_mux_sibling_disconnect(wsi); |
976 | 0 | wsi->mux.parent_wsi = NULL; |
977 | 0 | } |
978 | 0 | #endif |
979 | |
|
980 | 0 | #if defined(LWS_WITH_TLS) |
981 | 0 | memset(&wsi->tls, 0, sizeof(wsi->tls)); |
982 | 0 | #endif |
983 | | |
984 | | // wsi->a.protocol = NULL; |
985 | 0 | if (wsi->a.protocol) |
986 | 0 | lws_bind_protocol(wsi, wsi->a.protocol, "client_reset"); |
987 | 0 | wsi->pending_timeout = NO_PENDING_TIMEOUT; |
988 | 0 | wsi->hdr_parsing_completed = 0; |
989 | |
|
990 | 0 | #if defined(LWS_WITH_TLS) |
991 | 0 | if (wsi->stash->cis[CIS_ALPN]) |
992 | 0 | lws_strncpy(wsi->alpn, wsi->stash->cis[CIS_ALPN], |
993 | 0 | sizeof(wsi->alpn)); |
994 | 0 | #endif |
995 | |
|
996 | 0 | if (lws_header_table_attach(wsi, 0)) { |
997 | 0 | lwsl_wsi_err(wsi, "failed to get ah"); |
998 | 0 | return; |
999 | 0 | } |
1000 | | // } |
1001 | | //_lws_header_table_reset(wsi->http.ah); |
1002 | | |
1003 | 0 | #if defined(LWS_WITH_TLS) |
1004 | 0 | wsi->tls.use_ssl = (unsigned int)wsi->flags; |
1005 | 0 | #endif |
1006 | |
|
1007 | | #if defined(LWS_WITH_TLS_JIT_TRUST) |
1008 | | if (wsi->stash && wsi->stash->cis[CIS_ADDRESS]) { |
1009 | | struct lws_vhost *vh = NULL; |
1010 | | lws_tls_jit_trust_vhost_bind(wsi->a.context, |
1011 | | wsi->stash->cis[CIS_ADDRESS], |
1012 | | &vh); |
1013 | | if (vh) { |
1014 | | if (!vh->count_bound_wsi && vh->grace_after_unref) { |
1015 | | lwsl_wsi_info(wsi, "%s in use\n", |
1016 | | vh->lc.gutag); |
1017 | | lws_sul_cancel(&vh->sul_unref); |
1018 | | } |
1019 | | vh->count_bound_wsi++; |
1020 | | wsi->a.vhost = vh; |
1021 | | } |
1022 | | } |
1023 | | #endif |
1024 | |
|
1025 | 0 | return; |
1026 | 0 | } |
1027 | 0 | #endif |
1028 | | |
1029 | | /* outermost destroy notification for wsi (user_space still intact) */ |
1030 | 0 | if (wsi->a.vhost) |
1031 | 0 | wsi->a.vhost->protocols[0].callback(wsi, LWS_CALLBACK_WSI_DESTROY, |
1032 | 0 | wsi->user_space, NULL, 0); |
1033 | |
|
1034 | | #ifdef LWS_WITH_CGI |
1035 | | if (wsi->http.cgi) { |
1036 | | lws_spawn_piped_destroy(&wsi->http.cgi->lsp); |
1037 | | lws_sul_cancel(&wsi->http.cgi->sul_grace); |
1038 | | lws_free_set_NULL(wsi->http.cgi); |
1039 | | } |
1040 | | #endif |
1041 | |
|
1042 | | #if defined(LWS_WITH_SYS_FAULT_INJECTION) |
1043 | | lws_fi_destroy(&wsi->fic); |
1044 | | #endif |
1045 | |
|
1046 | 0 | __lws_wsi_remove_from_sul(wsi); |
1047 | 0 | sanity_assert_no_wsi_traces(wsi->a.context, wsi); |
1048 | 0 | __lws_free_wsi(wsi); |
1049 | 0 | } |
1050 | | |
1051 | | |
1052 | | void |
1053 | | lws_close_free_wsi(struct lws *wsi, enum lws_close_status reason, const char *caller) |
1054 | 0 | { |
1055 | 0 | struct lws_context *cx = wsi->a.context; |
1056 | 0 | struct lws_context_per_thread *pt = &wsi->a.context->pt[(int)wsi->tsi]; |
1057 | |
|
1058 | 0 | lws_context_lock(cx, __func__); |
1059 | |
|
1060 | 0 | lws_pt_lock(pt, __func__); |
1061 | | /* may destroy vhost, cannot hold vhost lock outside it */ |
1062 | 0 | __lws_close_free_wsi(wsi, reason, caller); |
1063 | 0 | lws_pt_unlock(pt); |
1064 | |
|
1065 | 0 | lws_context_unlock(cx); |
1066 | 0 | } |
1067 | | |
1068 | | |