File: ss-h2.c

package info (click to toggle)
libwebsockets 4.3.5-3
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 31,404 kB
  • sloc: ansic: 194,409; javascript: 1,550; sh: 1,387; cpp: 505; java: 461; perl: 405; xml: 118; makefile: 76; awk: 5
file content (226 lines) | stat: -rw-r--r-- 6,187 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*
 * libwebsockets - small server side websockets and web server implementation
 *
 * Copyright (C) 2019 - 2020 Andy Green <andy@warmcat.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 */

#include <private-lib-core.h>

extern int
secstream_h1(struct lws *wsi, enum lws_callback_reasons reason, void *user,
	     void *in, size_t len);

static int
secstream_h2(struct lws *wsi, enum lws_callback_reasons reason, void *user,
	     void *in, size_t len)
{
	lws_ss_handle_t *h = (lws_ss_handle_t *)lws_get_opaque_user_data(wsi);
	lws_ss_state_return_t r;
	int n;

	switch (reason) {

	case LWS_CALLBACK_ESTABLISHED_CLIENT_HTTP:

		if (!h)
			return -1;

#if defined(LWS_WITH_SECURE_STREAMS_PROXY_API)
		if (h->being_serialized) {
			/*
			 * We are the proxy-side SS for a remote client... we
			 * need to inform the client about the initial tx credit
			 * to write to it that the remote h2 server set up
			 */
			lwsl_info("%s: reporting initial tx cr from server %d\n",
				  __func__, wsi->txc.tx_cr);
			ss_proxy_onward_txcr((void *)(h + 1), wsi->txc.tx_cr);
		}
#endif

		n = secstream_h1(wsi, reason, user, in, len);

		if (!n && (h->policy->flags & LWSSSPOLF_LONG_POLL)) {
			lwsl_notice("%s: h2 client %s entering LONG_POLL\n",
					__func__, lws_wsi_tag(wsi));
			lws_h2_client_stream_long_poll_rxonly(wsi);
		}
		return n;

	case LWS_CALLBACK_CLOSED_CLIENT_HTTP:
		/*
		 * Only allow the wsi that the handle believes is representing
		 * him to report closure up to h1
		 */
		if (!h || h->wsi != wsi)
			return 0;

		break;

	case LWS_CALLBACK_COMPLETED_CLIENT_HTTP:

		if (!h)
			return -1;

		// lwsl_err("%s: h2 COMPLETED_CLIENT_HTTP\n", __func__);
		r = 0;
		if (h->hanging_som)
			r = h->info.rx(ss_to_userobj(h), NULL, 0, LWSSS_FLAG_EOM);

		h->txn_ok = 1;
		lws_cancel_service(lws_get_context(wsi)); /* abort poll wait */
		if (h->hanging_som && r == LWSSSSRET_DESTROY_ME)
			return _lws_ss_handle_state_ret_CAN_DESTROY_HANDLE(r, wsi, &h);
		h->hanging_som = 0;
		break;

	case LWS_CALLBACK_WSI_TX_CREDIT_GET:

		if (!h)
			return -1;

		/*
		 * The peer has sent us additional tx credit...
		 */
		lwsl_info("%s: LWS_CALLBACK_WSI_TX_CREDIT_GET: %d\n",
			    __func__, (int)len);

#if defined(LWS_WITH_SECURE_STREAMS_PROXY_API)
		if (h->being_serialized)
			/* we are the proxy-side SS for a remote client */
			ss_proxy_onward_txcr((void *)(h + 1), (int)len);
#endif
		break;

	default:
		break;
	}

	return secstream_h1(wsi, reason, user, in, len);
}

const struct lws_protocols protocol_secstream_h2 = {
	"lws-secstream-h2",
	secstream_h2,
	0, 0, 0, NULL, 0
};

/*
 * Munge connect info according to protocol-specific considerations... this
 * usually means interpreting aux in a protocol-specific way and using the
 * pieces at connection setup time, eg, http url pieces.
 *
 * len bytes of buf can be used for things with scope until after the actual
 * connect.
 */

int
secstream_connect_munge_h2(lws_ss_handle_t *h, char *buf, size_t len,
			   struct lws_client_connect_info *i,
			   union lws_ss_contemp *ct)
{
	const char *pbasis = h->policy->u.http.url;
	size_t used_in, used_out;
	lws_strexp_t exp;

	/* i.path on entry is used to override the policy urlpath if not "" */

	if (i->path[0])
		pbasis = i->path;

	if (h->policy->flags & LWSSSPOLF_QUIRK_NGHTTP2_END_STREAM)
		i->ssl_connection |= LCCSCF_H2_QUIRK_NGHTTP2_END_STREAM;

	if (h->policy->flags & LWSSSPOLF_H2_QUIRK_OVERFLOWS_TXCR)
		i->ssl_connection |= LCCSCF_H2_QUIRK_OVERFLOWS_TXCR;

	if (h->policy->flags & LWSSSPOLF_HTTP_MULTIPART)
		i->ssl_connection |= LCCSCF_HTTP_MULTIPART_MIME;

	if (h->policy->flags & LWSSSPOLF_HTTP_X_WWW_FORM_URLENCODED)
		i->ssl_connection |= LCCSCF_HTTP_X_WWW_FORM_URLENCODED;

	if (h->policy->flags & LWSSSPOLF_HTTP_CACHE_COOKIES)
		i->ssl_connection |= LCCSCF_CACHE_COOKIES;

	i->ssl_connection |= LCCSCF_PIPELINE;

	i->alpn = "h2";

	/* initial peer tx credit */

	if (h->info.manual_initial_tx_credit) {
		i->ssl_connection |= LCCSCF_H2_MANUAL_RXFLOW;
		i->manual_initial_tx_credit = h->info.manual_initial_tx_credit;
		lwsl_info("%s: initial txcr %d\n", __func__,
				i->manual_initial_tx_credit);
	}

	if (!pbasis)
		return 0;

	/* protocol aux is the path part */

	i->path = buf;
	buf[0] = '/';

	lws_strexp_init(&exp, (void *)h, lws_ss_exp_cb_metadata, buf + 1, len - 1);

	if (lws_strexp_expand(&exp, pbasis, strlen(pbasis),
			      &used_in, &used_out) != LSTRX_DONE)
		return 1;

	return 0;
}

static int
secstream_tx_credit_add_h2(lws_ss_handle_t *h, int add)
{
	lwsl_info("%s: %s: add %d\n", __func__, lws_ss_tag(h), add);
	if (h->wsi)
		return lws_h2_update_peer_txcredit(h->wsi, (unsigned int)LWS_H2_STREAM_SID, add);

	return 0;
}

static int
secstream_tx_credit_est_h2(lws_ss_handle_t *h)
{
	if (h->wsi) {
		lwsl_info("%s: %s: est %d\n", __func__, lws_ss_tag(h),
				lws_h2_get_peer_txcredit_estimate(h->wsi));

		return lws_h2_get_peer_txcredit_estimate(h->wsi);
	}

	lwsl_info("%s: %s: Unknown (0)\n", __func__, lws_ss_tag(h));

	return 0;
}

const struct ss_pcols ss_pcol_h2 = {
	"h2",
	"h2",
	&protocol_secstream_h2,
	secstream_connect_munge_h2,
	secstream_tx_credit_add_h2,
	secstream_tx_credit_est_h2
};