Mercurial > hg > nginx-quic
annotate src/http/modules/ngx_http_grpc_module.c @ 8283:a9034b10dacc quic
HTTP/3: fixed format specifier.
author | Roman Arutyunyan <arut@nginx.com> |
---|---|
date | Mon, 01 Feb 2021 18:48:18 +0300 |
parents | 88eca63261c3 |
children | 7ce28b4cc57e |
rev | line source |
---|---|
7233 | 1 |
2 /* | |
3 * Copyright (C) Maxim Dounin | |
4 * Copyright (C) Nginx, Inc. | |
5 */ | |
6 | |
7 | |
8 #include <ngx_config.h> | |
9 #include <ngx_core.h> | |
10 #include <ngx_http.h> | |
11 | |
12 | |
13 typedef struct { | |
14 ngx_array_t *flushes; | |
15 ngx_array_t *lengths; | |
16 ngx_array_t *values; | |
17 ngx_hash_t hash; | |
18 } ngx_http_grpc_headers_t; | |
19 | |
20 | |
21 typedef struct { | |
22 ngx_http_upstream_conf_t upstream; | |
23 | |
24 ngx_http_grpc_headers_t headers; | |
25 ngx_array_t *headers_source; | |
26 | |
27 ngx_str_t host; | |
28 ngx_uint_t host_set; | |
29 | |
7619
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
30 ngx_array_t *grpc_lengths; |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
31 ngx_array_t *grpc_values; |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
32 |
7233 | 33 #if (NGX_HTTP_SSL) |
34 ngx_uint_t ssl; | |
35 ngx_uint_t ssl_protocols; | |
36 ngx_str_t ssl_ciphers; | |
37 ngx_uint_t ssl_verify_depth; | |
38 ngx_str_t ssl_trusted_certificate; | |
39 ngx_str_t ssl_crl; | |
40 ngx_str_t ssl_certificate; | |
41 ngx_str_t ssl_certificate_key; | |
42 ngx_array_t *ssl_passwords; | |
8183
1a719ee45526
Upstream: proxy_ssl_conf_command and friends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
8181
diff
changeset
|
43 ngx_array_t *ssl_conf_commands; |
7233 | 44 #endif |
45 } ngx_http_grpc_loc_conf_t; | |
46 | |
47 | |
48 typedef enum { | |
49 ngx_http_grpc_st_start = 0, | |
50 ngx_http_grpc_st_length_2, | |
51 ngx_http_grpc_st_length_3, | |
52 ngx_http_grpc_st_type, | |
53 ngx_http_grpc_st_flags, | |
54 ngx_http_grpc_st_stream_id, | |
55 ngx_http_grpc_st_stream_id_2, | |
56 ngx_http_grpc_st_stream_id_3, | |
57 ngx_http_grpc_st_stream_id_4, | |
58 ngx_http_grpc_st_payload, | |
59 ngx_http_grpc_st_padding | |
60 } ngx_http_grpc_state_e; | |
61 | |
62 | |
63 typedef struct { | |
64 size_t init_window; | |
65 size_t send_window; | |
66 size_t recv_window; | |
67 ngx_uint_t last_stream_id; | |
68 } ngx_http_grpc_conn_t; | |
69 | |
70 | |
71 typedef struct { | |
72 ngx_http_grpc_state_e state; | |
73 ngx_uint_t frame_state; | |
74 ngx_uint_t fragment_state; | |
75 | |
76 ngx_chain_t *in; | |
77 ngx_chain_t *out; | |
78 ngx_chain_t *free; | |
79 ngx_chain_t *busy; | |
80 | |
81 ngx_http_grpc_conn_t *connection; | |
82 | |
83 ngx_uint_t id; | |
84 | |
7379
57463f4e2fcd
gRPC: limited allocations due to ping and settings frames.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7371
diff
changeset
|
85 ngx_uint_t pings; |
57463f4e2fcd
gRPC: limited allocations due to ping and settings frames.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7371
diff
changeset
|
86 ngx_uint_t settings; |
57463f4e2fcd
gRPC: limited allocations due to ping and settings frames.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7371
diff
changeset
|
87 |
7983
39501ce97e29
gRPC: generate error when response size is wrong.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7893
diff
changeset
|
88 off_t length; |
39501ce97e29
gRPC: generate error when response size is wrong.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7893
diff
changeset
|
89 |
7233 | 90 ssize_t send_window; |
91 size_t recv_window; | |
92 | |
93 size_t rest; | |
94 ngx_uint_t stream_id; | |
95 u_char type; | |
96 u_char flags; | |
97 u_char padding; | |
98 | |
99 ngx_uint_t error; | |
100 ngx_uint_t window_update; | |
101 | |
102 ngx_uint_t setting_id; | |
103 ngx_uint_t setting_value; | |
104 | |
105 u_char ping_data[8]; | |
106 | |
107 ngx_uint_t index; | |
108 ngx_str_t name; | |
109 ngx_str_t value; | |
110 | |
111 u_char *field_end; | |
112 size_t field_length; | |
113 size_t field_rest; | |
114 u_char field_state; | |
115 | |
116 unsigned literal:1; | |
117 unsigned field_huffman:1; | |
118 | |
119 unsigned header_sent:1; | |
120 unsigned output_closed:1; | |
7350
67c6cb7f477c
gRPC: disabled keepalive when sending control frames was blocked.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7349
diff
changeset
|
121 unsigned output_blocked:1; |
7233 | 122 unsigned parsing_headers:1; |
123 unsigned end_stream:1; | |
7349
f6047a579ca1
gRPC: improved keepalive handling.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7321
diff
changeset
|
124 unsigned done:1; |
7233 | 125 unsigned status:1; |
7892
2096b21fcd10
gRPC: RST_STREAM(NO_ERROR) handling (ticket #1792).
Ruslan Ermilov <ru@nginx.com>
parents:
7619
diff
changeset
|
126 unsigned rst:1; |
7233 | 127 |
128 ngx_http_request_t *request; | |
7619
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
129 |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
130 ngx_str_t host; |
7233 | 131 } ngx_http_grpc_ctx_t; |
132 | |
133 | |
134 typedef struct { | |
135 u_char length_0; | |
136 u_char length_1; | |
137 u_char length_2; | |
138 u_char type; | |
139 u_char flags; | |
140 u_char stream_id_0; | |
141 u_char stream_id_1; | |
142 u_char stream_id_2; | |
143 u_char stream_id_3; | |
144 } ngx_http_grpc_frame_t; | |
145 | |
146 | |
7619
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
147 static ngx_int_t ngx_http_grpc_eval(ngx_http_request_t *r, |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
148 ngx_http_grpc_ctx_t *ctx, ngx_http_grpc_loc_conf_t *glcf); |
7233 | 149 static ngx_int_t ngx_http_grpc_create_request(ngx_http_request_t *r); |
150 static ngx_int_t ngx_http_grpc_reinit_request(ngx_http_request_t *r); | |
151 static ngx_int_t ngx_http_grpc_body_output_filter(void *data, ngx_chain_t *in); | |
152 static ngx_int_t ngx_http_grpc_process_header(ngx_http_request_t *r); | |
153 static ngx_int_t ngx_http_grpc_filter_init(void *data); | |
154 static ngx_int_t ngx_http_grpc_filter(void *data, ssize_t bytes); | |
155 | |
156 static ngx_int_t ngx_http_grpc_parse_frame(ngx_http_request_t *r, | |
157 ngx_http_grpc_ctx_t *ctx, ngx_buf_t *b); | |
158 static ngx_int_t ngx_http_grpc_parse_header(ngx_http_request_t *r, | |
159 ngx_http_grpc_ctx_t *ctx, ngx_buf_t *b); | |
160 static ngx_int_t ngx_http_grpc_parse_fragment(ngx_http_request_t *r, | |
161 ngx_http_grpc_ctx_t *ctx, ngx_buf_t *b); | |
162 static ngx_int_t ngx_http_grpc_validate_header_name(ngx_http_request_t *r, | |
163 ngx_str_t *s); | |
164 static ngx_int_t ngx_http_grpc_validate_header_value(ngx_http_request_t *r, | |
165 ngx_str_t *s); | |
166 static ngx_int_t ngx_http_grpc_parse_rst_stream(ngx_http_request_t *r, | |
167 ngx_http_grpc_ctx_t *ctx, ngx_buf_t *b); | |
168 static ngx_int_t ngx_http_grpc_parse_goaway(ngx_http_request_t *r, | |
169 ngx_http_grpc_ctx_t *ctx, ngx_buf_t *b); | |
170 static ngx_int_t ngx_http_grpc_parse_window_update(ngx_http_request_t *r, | |
171 ngx_http_grpc_ctx_t *ctx, ngx_buf_t *b); | |
172 static ngx_int_t ngx_http_grpc_parse_settings(ngx_http_request_t *r, | |
173 ngx_http_grpc_ctx_t *ctx, ngx_buf_t *b); | |
174 static ngx_int_t ngx_http_grpc_parse_ping(ngx_http_request_t *r, | |
175 ngx_http_grpc_ctx_t *ctx, ngx_buf_t *b); | |
176 | |
177 static ngx_int_t ngx_http_grpc_send_settings_ack(ngx_http_request_t *r, | |
178 ngx_http_grpc_ctx_t *ctx); | |
179 static ngx_int_t ngx_http_grpc_send_ping_ack(ngx_http_request_t *r, | |
180 ngx_http_grpc_ctx_t *ctx); | |
181 static ngx_int_t ngx_http_grpc_send_window_update(ngx_http_request_t *r, | |
182 ngx_http_grpc_ctx_t *ctx); | |
183 | |
184 static ngx_chain_t *ngx_http_grpc_get_buf(ngx_http_request_t *r, | |
185 ngx_http_grpc_ctx_t *ctx); | |
186 static ngx_http_grpc_ctx_t *ngx_http_grpc_get_ctx(ngx_http_request_t *r); | |
187 static ngx_int_t ngx_http_grpc_get_connection_data(ngx_http_request_t *r, | |
188 ngx_http_grpc_ctx_t *ctx, ngx_peer_connection_t *pc); | |
189 static void ngx_http_grpc_cleanup(void *data); | |
190 | |
191 static void ngx_http_grpc_abort_request(ngx_http_request_t *r); | |
192 static void ngx_http_grpc_finalize_request(ngx_http_request_t *r, | |
193 ngx_int_t rc); | |
194 | |
7234
c693daca57f7
gRPC: special handling of the TE request header.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7233
diff
changeset
|
195 static ngx_int_t ngx_http_grpc_internal_trailers_variable( |
c693daca57f7
gRPC: special handling of the TE request header.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7233
diff
changeset
|
196 ngx_http_request_t *r, ngx_http_variable_value_t *v, uintptr_t data); |
c693daca57f7
gRPC: special handling of the TE request header.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7233
diff
changeset
|
197 |
c693daca57f7
gRPC: special handling of the TE request header.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7233
diff
changeset
|
198 static ngx_int_t ngx_http_grpc_add_variables(ngx_conf_t *cf); |
7233 | 199 static void *ngx_http_grpc_create_loc_conf(ngx_conf_t *cf); |
200 static char *ngx_http_grpc_merge_loc_conf(ngx_conf_t *cf, | |
201 void *parent, void *child); | |
202 static ngx_int_t ngx_http_grpc_init_headers(ngx_conf_t *cf, | |
203 ngx_http_grpc_loc_conf_t *conf, ngx_http_grpc_headers_t *headers, | |
204 ngx_keyval_t *default_headers); | |
205 | |
206 static char *ngx_http_grpc_pass(ngx_conf_t *cf, ngx_command_t *cmd, | |
207 void *conf); | |
208 | |
209 #if (NGX_HTTP_SSL) | |
210 static char *ngx_http_grpc_ssl_password_file(ngx_conf_t *cf, | |
211 ngx_command_t *cmd, void *conf); | |
8183
1a719ee45526
Upstream: proxy_ssl_conf_command and friends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
8181
diff
changeset
|
212 static char *ngx_http_grpc_ssl_conf_command_check(ngx_conf_t *cf, void *post, |
1a719ee45526
Upstream: proxy_ssl_conf_command and friends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
8181
diff
changeset
|
213 void *data); |
7233 | 214 static ngx_int_t ngx_http_grpc_set_ssl(ngx_conf_t *cf, |
215 ngx_http_grpc_loc_conf_t *glcf); | |
216 #endif | |
217 | |
218 | |
219 static ngx_conf_bitmask_t ngx_http_grpc_next_upstream_masks[] = { | |
220 { ngx_string("error"), NGX_HTTP_UPSTREAM_FT_ERROR }, | |
221 { ngx_string("timeout"), NGX_HTTP_UPSTREAM_FT_TIMEOUT }, | |
222 { ngx_string("invalid_header"), NGX_HTTP_UPSTREAM_FT_INVALID_HEADER }, | |
223 { ngx_string("non_idempotent"), NGX_HTTP_UPSTREAM_FT_NON_IDEMPOTENT }, | |
224 { ngx_string("http_500"), NGX_HTTP_UPSTREAM_FT_HTTP_500 }, | |
225 { ngx_string("http_502"), NGX_HTTP_UPSTREAM_FT_HTTP_502 }, | |
226 { ngx_string("http_503"), NGX_HTTP_UPSTREAM_FT_HTTP_503 }, | |
227 { ngx_string("http_504"), NGX_HTTP_UPSTREAM_FT_HTTP_504 }, | |
228 { ngx_string("http_403"), NGX_HTTP_UPSTREAM_FT_HTTP_403 }, | |
229 { ngx_string("http_404"), NGX_HTTP_UPSTREAM_FT_HTTP_404 }, | |
230 { ngx_string("http_429"), NGX_HTTP_UPSTREAM_FT_HTTP_429 }, | |
231 { ngx_string("off"), NGX_HTTP_UPSTREAM_FT_OFF }, | |
232 { ngx_null_string, 0 } | |
233 }; | |
234 | |
235 | |
236 #if (NGX_HTTP_SSL) | |
237 | |
238 static ngx_conf_bitmask_t ngx_http_grpc_ssl_protocols[] = { | |
239 { ngx_string("SSLv2"), NGX_SSL_SSLv2 }, | |
240 { ngx_string("SSLv3"), NGX_SSL_SSLv3 }, | |
241 { ngx_string("TLSv1"), NGX_SSL_TLSv1 }, | |
242 { ngx_string("TLSv1.1"), NGX_SSL_TLSv1_1 }, | |
243 { ngx_string("TLSv1.2"), NGX_SSL_TLSv1_2 }, | |
244 { ngx_string("TLSv1.3"), NGX_SSL_TLSv1_3 }, | |
245 { ngx_null_string, 0 } | |
246 }; | |
247 | |
8183
1a719ee45526
Upstream: proxy_ssl_conf_command and friends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
8181
diff
changeset
|
248 static ngx_conf_post_t ngx_http_grpc_ssl_conf_command_post = |
1a719ee45526
Upstream: proxy_ssl_conf_command and friends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
8181
diff
changeset
|
249 { ngx_http_grpc_ssl_conf_command_check }; |
1a719ee45526
Upstream: proxy_ssl_conf_command and friends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
8181
diff
changeset
|
250 |
7233 | 251 #endif |
252 | |
253 | |
254 static ngx_command_t ngx_http_grpc_commands[] = { | |
255 | |
256 { ngx_string("grpc_pass"), | |
257 NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE1, | |
258 ngx_http_grpc_pass, | |
259 NGX_HTTP_LOC_CONF_OFFSET, | |
260 0, | |
261 NULL }, | |
262 | |
263 { ngx_string("grpc_bind"), | |
264 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE12, | |
265 ngx_http_upstream_bind_set_slot, | |
266 NGX_HTTP_LOC_CONF_OFFSET, | |
267 offsetof(ngx_http_grpc_loc_conf_t, upstream.local), | |
268 NULL }, | |
269 | |
7371
8b68d50090e4
Upstream: proxy_socket_keepalive and friends.
Vladimir Homutov <vl@nginx.com>
parents:
7350
diff
changeset
|
270 { ngx_string("grpc_socket_keepalive"), |
8b68d50090e4
Upstream: proxy_socket_keepalive and friends.
Vladimir Homutov <vl@nginx.com>
parents:
7350
diff
changeset
|
271 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG, |
8b68d50090e4
Upstream: proxy_socket_keepalive and friends.
Vladimir Homutov <vl@nginx.com>
parents:
7350
diff
changeset
|
272 ngx_conf_set_flag_slot, |
8b68d50090e4
Upstream: proxy_socket_keepalive and friends.
Vladimir Homutov <vl@nginx.com>
parents:
7350
diff
changeset
|
273 NGX_HTTP_LOC_CONF_OFFSET, |
8b68d50090e4
Upstream: proxy_socket_keepalive and friends.
Vladimir Homutov <vl@nginx.com>
parents:
7350
diff
changeset
|
274 offsetof(ngx_http_grpc_loc_conf_t, upstream.socket_keepalive), |
8b68d50090e4
Upstream: proxy_socket_keepalive and friends.
Vladimir Homutov <vl@nginx.com>
parents:
7350
diff
changeset
|
275 NULL }, |
8b68d50090e4
Upstream: proxy_socket_keepalive and friends.
Vladimir Homutov <vl@nginx.com>
parents:
7350
diff
changeset
|
276 |
7233 | 277 { ngx_string("grpc_connect_timeout"), |
278 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | |
279 ngx_conf_set_msec_slot, | |
280 NGX_HTTP_LOC_CONF_OFFSET, | |
281 offsetof(ngx_http_grpc_loc_conf_t, upstream.connect_timeout), | |
282 NULL }, | |
283 | |
284 { ngx_string("grpc_send_timeout"), | |
285 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | |
286 ngx_conf_set_msec_slot, | |
287 NGX_HTTP_LOC_CONF_OFFSET, | |
288 offsetof(ngx_http_grpc_loc_conf_t, upstream.send_timeout), | |
289 NULL }, | |
290 | |
291 { ngx_string("grpc_intercept_errors"), | |
292 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG, | |
293 ngx_conf_set_flag_slot, | |
294 NGX_HTTP_LOC_CONF_OFFSET, | |
295 offsetof(ngx_http_grpc_loc_conf_t, upstream.intercept_errors), | |
296 NULL }, | |
297 | |
298 { ngx_string("grpc_buffer_size"), | |
299 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | |
300 ngx_conf_set_size_slot, | |
301 NGX_HTTP_LOC_CONF_OFFSET, | |
302 offsetof(ngx_http_grpc_loc_conf_t, upstream.buffer_size), | |
303 NULL }, | |
304 | |
305 { ngx_string("grpc_read_timeout"), | |
306 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | |
307 ngx_conf_set_msec_slot, | |
308 NGX_HTTP_LOC_CONF_OFFSET, | |
309 offsetof(ngx_http_grpc_loc_conf_t, upstream.read_timeout), | |
310 NULL }, | |
311 | |
312 { ngx_string("grpc_next_upstream"), | |
313 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE, | |
314 ngx_conf_set_bitmask_slot, | |
315 NGX_HTTP_LOC_CONF_OFFSET, | |
316 offsetof(ngx_http_grpc_loc_conf_t, upstream.next_upstream), | |
317 &ngx_http_grpc_next_upstream_masks }, | |
318 | |
319 { ngx_string("grpc_next_upstream_tries"), | |
320 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | |
321 ngx_conf_set_num_slot, | |
322 NGX_HTTP_LOC_CONF_OFFSET, | |
323 offsetof(ngx_http_grpc_loc_conf_t, upstream.next_upstream_tries), | |
324 NULL }, | |
325 | |
326 { ngx_string("grpc_next_upstream_timeout"), | |
327 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | |
328 ngx_conf_set_msec_slot, | |
329 NGX_HTTP_LOC_CONF_OFFSET, | |
330 offsetof(ngx_http_grpc_loc_conf_t, upstream.next_upstream_timeout), | |
331 NULL }, | |
332 | |
333 { ngx_string("grpc_set_header"), | |
334 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE2, | |
335 ngx_conf_set_keyval_slot, | |
336 NGX_HTTP_LOC_CONF_OFFSET, | |
337 offsetof(ngx_http_grpc_loc_conf_t, headers_source), | |
338 NULL }, | |
339 | |
340 { ngx_string("grpc_pass_header"), | |
341 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | |
342 ngx_conf_set_str_array_slot, | |
343 NGX_HTTP_LOC_CONF_OFFSET, | |
344 offsetof(ngx_http_grpc_loc_conf_t, upstream.pass_headers), | |
345 NULL }, | |
346 | |
347 { ngx_string("grpc_hide_header"), | |
348 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | |
349 ngx_conf_set_str_array_slot, | |
350 NGX_HTTP_LOC_CONF_OFFSET, | |
351 offsetof(ngx_http_grpc_loc_conf_t, upstream.hide_headers), | |
352 NULL }, | |
353 | |
354 { ngx_string("grpc_ignore_headers"), | |
355 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE, | |
356 ngx_conf_set_bitmask_slot, | |
357 NGX_HTTP_LOC_CONF_OFFSET, | |
358 offsetof(ngx_http_grpc_loc_conf_t, upstream.ignore_headers), | |
359 &ngx_http_upstream_ignore_headers_masks }, | |
360 | |
361 #if (NGX_HTTP_SSL) | |
362 | |
363 { ngx_string("grpc_ssl_session_reuse"), | |
364 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG, | |
365 ngx_conf_set_flag_slot, | |
366 NGX_HTTP_LOC_CONF_OFFSET, | |
367 offsetof(ngx_http_grpc_loc_conf_t, upstream.ssl_session_reuse), | |
368 NULL }, | |
369 | |
370 { ngx_string("grpc_ssl_protocols"), | |
371 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE, | |
372 ngx_conf_set_bitmask_slot, | |
373 NGX_HTTP_LOC_CONF_OFFSET, | |
374 offsetof(ngx_http_grpc_loc_conf_t, ssl_protocols), | |
375 &ngx_http_grpc_ssl_protocols }, | |
376 | |
377 { ngx_string("grpc_ssl_ciphers"), | |
378 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | |
379 ngx_conf_set_str_slot, | |
380 NGX_HTTP_LOC_CONF_OFFSET, | |
381 offsetof(ngx_http_grpc_loc_conf_t, ssl_ciphers), | |
382 NULL }, | |
383 | |
384 { ngx_string("grpc_ssl_name"), | |
385 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | |
386 ngx_http_set_complex_value_slot, | |
387 NGX_HTTP_LOC_CONF_OFFSET, | |
388 offsetof(ngx_http_grpc_loc_conf_t, upstream.ssl_name), | |
389 NULL }, | |
390 | |
391 { ngx_string("grpc_ssl_server_name"), | |
392 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG, | |
393 ngx_conf_set_flag_slot, | |
394 NGX_HTTP_LOC_CONF_OFFSET, | |
395 offsetof(ngx_http_grpc_loc_conf_t, upstream.ssl_server_name), | |
396 NULL }, | |
397 | |
398 { ngx_string("grpc_ssl_verify"), | |
399 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG, | |
400 ngx_conf_set_flag_slot, | |
401 NGX_HTTP_LOC_CONF_OFFSET, | |
402 offsetof(ngx_http_grpc_loc_conf_t, upstream.ssl_verify), | |
403 NULL }, | |
404 | |
405 { ngx_string("grpc_ssl_verify_depth"), | |
406 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | |
407 ngx_conf_set_num_slot, | |
408 NGX_HTTP_LOC_CONF_OFFSET, | |
409 offsetof(ngx_http_grpc_loc_conf_t, ssl_verify_depth), | |
410 NULL }, | |
411 | |
412 { ngx_string("grpc_ssl_trusted_certificate"), | |
413 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | |
414 ngx_conf_set_str_slot, | |
415 NGX_HTTP_LOC_CONF_OFFSET, | |
416 offsetof(ngx_http_grpc_loc_conf_t, ssl_trusted_certificate), | |
417 NULL }, | |
418 | |
419 { ngx_string("grpc_ssl_crl"), | |
420 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | |
421 ngx_conf_set_str_slot, | |
422 NGX_HTTP_LOC_CONF_OFFSET, | |
423 offsetof(ngx_http_grpc_loc_conf_t, ssl_crl), | |
424 NULL }, | |
425 | |
426 { ngx_string("grpc_ssl_certificate"), | |
427 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | |
428 ngx_conf_set_str_slot, | |
429 NGX_HTTP_LOC_CONF_OFFSET, | |
430 offsetof(ngx_http_grpc_loc_conf_t, ssl_certificate), | |
431 NULL }, | |
432 | |
433 { ngx_string("grpc_ssl_certificate_key"), | |
434 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | |
435 ngx_conf_set_str_slot, | |
436 NGX_HTTP_LOC_CONF_OFFSET, | |
437 offsetof(ngx_http_grpc_loc_conf_t, ssl_certificate_key), | |
438 NULL }, | |
439 | |
440 { ngx_string("grpc_ssl_password_file"), | |
441 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | |
442 ngx_http_grpc_ssl_password_file, | |
443 NGX_HTTP_LOC_CONF_OFFSET, | |
444 0, | |
445 NULL }, | |
446 | |
8183
1a719ee45526
Upstream: proxy_ssl_conf_command and friends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
8181
diff
changeset
|
447 { ngx_string("grpc_ssl_conf_command"), |
1a719ee45526
Upstream: proxy_ssl_conf_command and friends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
8181
diff
changeset
|
448 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE2, |
1a719ee45526
Upstream: proxy_ssl_conf_command and friends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
8181
diff
changeset
|
449 ngx_conf_set_keyval_slot, |
1a719ee45526
Upstream: proxy_ssl_conf_command and friends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
8181
diff
changeset
|
450 NGX_HTTP_LOC_CONF_OFFSET, |
1a719ee45526
Upstream: proxy_ssl_conf_command and friends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
8181
diff
changeset
|
451 offsetof(ngx_http_grpc_loc_conf_t, ssl_conf_commands), |
1a719ee45526
Upstream: proxy_ssl_conf_command and friends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
8181
diff
changeset
|
452 &ngx_http_grpc_ssl_conf_command_post }, |
1a719ee45526
Upstream: proxy_ssl_conf_command and friends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
8181
diff
changeset
|
453 |
7233 | 454 #endif |
455 | |
456 ngx_null_command | |
457 }; | |
458 | |
459 | |
460 static ngx_http_module_t ngx_http_grpc_module_ctx = { | |
7234
c693daca57f7
gRPC: special handling of the TE request header.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7233
diff
changeset
|
461 ngx_http_grpc_add_variables, /* preconfiguration */ |
7233 | 462 NULL, /* postconfiguration */ |
463 | |
464 NULL, /* create main configuration */ | |
465 NULL, /* init main configuration */ | |
466 | |
467 NULL, /* create server configuration */ | |
468 NULL, /* merge server configuration */ | |
469 | |
470 ngx_http_grpc_create_loc_conf, /* create location configuration */ | |
471 ngx_http_grpc_merge_loc_conf /* merge location configuration */ | |
472 }; | |
473 | |
474 | |
475 ngx_module_t ngx_http_grpc_module = { | |
476 NGX_MODULE_V1, | |
477 &ngx_http_grpc_module_ctx, /* module context */ | |
478 ngx_http_grpc_commands, /* module directives */ | |
479 NGX_HTTP_MODULE, /* module type */ | |
480 NULL, /* init master */ | |
481 NULL, /* init module */ | |
482 NULL, /* init process */ | |
483 NULL, /* init thread */ | |
484 NULL, /* exit thread */ | |
485 NULL, /* exit process */ | |
486 NULL, /* exit master */ | |
487 NGX_MODULE_V1_PADDING | |
488 }; | |
489 | |
490 | |
491 static u_char ngx_http_grpc_connection_start[] = | |
492 "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n" /* connection preface */ | |
493 | |
494 "\x00\x00\x12\x04\x00\x00\x00\x00\x00" /* settings frame */ | |
495 "\x00\x01\x00\x00\x00\x00" /* header table size */ | |
496 "\x00\x02\x00\x00\x00\x00" /* disable push */ | |
497 "\x00\x04\x7f\xff\xff\xff" /* initial window */ | |
498 | |
499 "\x00\x00\x04\x08\x00\x00\x00\x00\x00" /* window update frame */ | |
500 "\x7f\xff\x00\x00"; | |
501 | |
502 | |
503 static ngx_keyval_t ngx_http_grpc_headers[] = { | |
504 { ngx_string("Content-Length"), ngx_string("$content_length") }, | |
7234
c693daca57f7
gRPC: special handling of the TE request header.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7233
diff
changeset
|
505 { ngx_string("TE"), ngx_string("$grpc_internal_trailers") }, |
7233 | 506 { ngx_string("Host"), ngx_string("") }, |
507 { ngx_string("Connection"), ngx_string("") }, | |
508 { ngx_string("Transfer-Encoding"), ngx_string("") }, | |
509 { ngx_string("Keep-Alive"), ngx_string("") }, | |
510 { ngx_string("Expect"), ngx_string("") }, | |
511 { ngx_string("Upgrade"), ngx_string("") }, | |
512 { ngx_null_string, ngx_null_string } | |
513 }; | |
514 | |
515 | |
516 static ngx_str_t ngx_http_grpc_hide_headers[] = { | |
517 ngx_string("Date"), | |
518 ngx_string("Server"), | |
519 ngx_string("X-Accel-Expires"), | |
520 ngx_string("X-Accel-Redirect"), | |
521 ngx_string("X-Accel-Limit-Rate"), | |
522 ngx_string("X-Accel-Buffering"), | |
523 ngx_string("X-Accel-Charset"), | |
524 ngx_null_string | |
525 }; | |
526 | |
527 | |
7234
c693daca57f7
gRPC: special handling of the TE request header.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7233
diff
changeset
|
528 static ngx_http_variable_t ngx_http_grpc_vars[] = { |
c693daca57f7
gRPC: special handling of the TE request header.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7233
diff
changeset
|
529 |
c693daca57f7
gRPC: special handling of the TE request header.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7233
diff
changeset
|
530 { ngx_string("grpc_internal_trailers"), NULL, |
c693daca57f7
gRPC: special handling of the TE request header.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7233
diff
changeset
|
531 ngx_http_grpc_internal_trailers_variable, 0, |
c693daca57f7
gRPC: special handling of the TE request header.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7233
diff
changeset
|
532 NGX_HTTP_VAR_NOCACHEABLE|NGX_HTTP_VAR_NOHASH, 0 }, |
c693daca57f7
gRPC: special handling of the TE request header.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7233
diff
changeset
|
533 |
c693daca57f7
gRPC: special handling of the TE request header.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7233
diff
changeset
|
534 ngx_http_null_variable |
c693daca57f7
gRPC: special handling of the TE request header.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7233
diff
changeset
|
535 }; |
c693daca57f7
gRPC: special handling of the TE request header.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7233
diff
changeset
|
536 |
c693daca57f7
gRPC: special handling of the TE request header.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7233
diff
changeset
|
537 |
7233 | 538 static ngx_int_t |
539 ngx_http_grpc_handler(ngx_http_request_t *r) | |
540 { | |
541 ngx_int_t rc; | |
542 ngx_http_upstream_t *u; | |
543 ngx_http_grpc_ctx_t *ctx; | |
544 ngx_http_grpc_loc_conf_t *glcf; | |
545 | |
546 if (ngx_http_upstream_create(r) != NGX_OK) { | |
547 return NGX_HTTP_INTERNAL_SERVER_ERROR; | |
548 } | |
549 | |
7619
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
550 ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_grpc_ctx_t)); |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
551 if (ctx == NULL) { |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
552 return NGX_HTTP_INTERNAL_SERVER_ERROR; |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
553 } |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
554 |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
555 ctx->request = r; |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
556 |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
557 ngx_http_set_ctx(r, ctx, ngx_http_grpc_module); |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
558 |
7233 | 559 glcf = ngx_http_get_module_loc_conf(r, ngx_http_grpc_module); |
560 | |
561 u = r->upstream; | |
562 | |
7619
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
563 if (glcf->grpc_lengths == NULL) { |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
564 ctx->host = glcf->host; |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
565 |
7233 | 566 #if (NGX_HTTP_SSL) |
7619
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
567 u->ssl = (glcf->upstream.ssl != NULL); |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
568 |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
569 if (u->ssl) { |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
570 ngx_str_set(&u->schema, "grpcs://"); |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
571 |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
572 } else { |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
573 ngx_str_set(&u->schema, "grpc://"); |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
574 } |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
575 #else |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
576 ngx_str_set(&u->schema, "grpc://"); |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
577 #endif |
7233 | 578 |
579 } else { | |
7619
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
580 if (ngx_http_grpc_eval(r, ctx, glcf) != NGX_OK) { |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
581 return NGX_HTTP_INTERNAL_SERVER_ERROR; |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
582 } |
7233 | 583 } |
584 | |
585 u->output.tag = (ngx_buf_tag_t) &ngx_http_grpc_module; | |
586 | |
587 u->conf = &glcf->upstream; | |
588 | |
589 u->create_request = ngx_http_grpc_create_request; | |
590 u->reinit_request = ngx_http_grpc_reinit_request; | |
591 u->process_header = ngx_http_grpc_process_header; | |
592 u->abort_request = ngx_http_grpc_abort_request; | |
593 u->finalize_request = ngx_http_grpc_finalize_request; | |
594 | |
595 u->input_filter_init = ngx_http_grpc_filter_init; | |
596 u->input_filter = ngx_http_grpc_filter; | |
597 u->input_filter_ctx = ctx; | |
598 | |
599 r->request_body_no_buffering = 1; | |
600 | |
601 rc = ngx_http_read_client_request_body(r, ngx_http_upstream_init); | |
602 | |
603 if (rc >= NGX_HTTP_SPECIAL_RESPONSE) { | |
604 return rc; | |
605 } | |
606 | |
607 return NGX_DONE; | |
608 } | |
609 | |
610 | |
611 static ngx_int_t | |
7619
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
612 ngx_http_grpc_eval(ngx_http_request_t *r, ngx_http_grpc_ctx_t *ctx, |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
613 ngx_http_grpc_loc_conf_t *glcf) |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
614 { |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
615 size_t add; |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
616 ngx_url_t url; |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
617 ngx_http_upstream_t *u; |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
618 |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
619 ngx_memzero(&url, sizeof(ngx_url_t)); |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
620 |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
621 if (ngx_http_script_run(r, &url.url, glcf->grpc_lengths->elts, 0, |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
622 glcf->grpc_values->elts) |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
623 == NULL) |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
624 { |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
625 return NGX_ERROR; |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
626 } |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
627 |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
628 if (url.url.len > 7 |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
629 && ngx_strncasecmp(url.url.data, (u_char *) "grpc://", 7) == 0) |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
630 { |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
631 add = 7; |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
632 |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
633 } else if (url.url.len > 8 |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
634 && ngx_strncasecmp(url.url.data, (u_char *) "grpcs://", 8) == 0) |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
635 { |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
636 |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
637 #if (NGX_HTTP_SSL) |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
638 add = 8; |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
639 r->upstream->ssl = 1; |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
640 #else |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
641 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
642 "grpcs protocol requires SSL support"); |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
643 return NGX_ERROR; |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
644 #endif |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
645 |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
646 } else { |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
647 add = 0; |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
648 } |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
649 |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
650 u = r->upstream; |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
651 |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
652 if (add) { |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
653 u->schema.len = add; |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
654 u->schema.data = url.url.data; |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
655 |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
656 url.url.data += add; |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
657 url.url.len -= add; |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
658 |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
659 } else { |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
660 ngx_str_set(&u->schema, "grpc://"); |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
661 } |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
662 |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
663 url.no_resolve = 1; |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
664 |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
665 if (ngx_parse_url(r->pool, &url) != NGX_OK) { |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
666 if (url.err) { |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
667 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
668 "%s in upstream \"%V\"", url.err, &url.url); |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
669 } |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
670 |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
671 return NGX_ERROR; |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
672 } |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
673 |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
674 u->resolved = ngx_pcalloc(r->pool, sizeof(ngx_http_upstream_resolved_t)); |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
675 if (u->resolved == NULL) { |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
676 return NGX_ERROR; |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
677 } |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
678 |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
679 if (url.addrs) { |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
680 u->resolved->sockaddr = url.addrs[0].sockaddr; |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
681 u->resolved->socklen = url.addrs[0].socklen; |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
682 u->resolved->name = url.addrs[0].name; |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
683 u->resolved->naddrs = 1; |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
684 } |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
685 |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
686 u->resolved->host = url.host; |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
687 u->resolved->port = url.port; |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
688 u->resolved->no_port = url.no_port; |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
689 |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
690 if (url.family != AF_UNIX) { |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
691 |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
692 if (url.no_port) { |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
693 ctx->host = url.host; |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
694 |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
695 } else { |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
696 ctx->host.len = url.host.len + 1 + url.port_text.len; |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
697 ctx->host.data = url.host.data; |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
698 } |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
699 |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
700 } else { |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
701 ngx_str_set(&ctx->host, "localhost"); |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
702 } |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
703 |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
704 return NGX_OK; |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
705 } |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
706 |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
707 |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
708 static ngx_int_t |
7233 | 709 ngx_http_grpc_create_request(ngx_http_request_t *r) |
710 { | |
711 u_char *p, *tmp, *key_tmp, *val_tmp, *headers_frame; | |
712 size_t len, tmp_len, key_len, val_len, uri_len; | |
713 uintptr_t escape; | |
714 ngx_buf_t *b; | |
715 ngx_uint_t i, next; | |
716 ngx_chain_t *cl, *body; | |
717 ngx_list_part_t *part; | |
718 ngx_table_elt_t *header; | |
7619
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
719 ngx_http_grpc_ctx_t *ctx; |
7233 | 720 ngx_http_upstream_t *u; |
721 ngx_http_grpc_frame_t *f; | |
722 ngx_http_script_code_pt code; | |
723 ngx_http_grpc_loc_conf_t *glcf; | |
724 ngx_http_script_engine_t e, le; | |
725 ngx_http_script_len_code_pt lcode; | |
726 | |
727 u = r->upstream; | |
728 | |
729 glcf = ngx_http_get_module_loc_conf(r, ngx_http_grpc_module); | |
730 | |
7619
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
731 ctx = ngx_http_get_module_ctx(r, ngx_http_grpc_module); |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
732 |
7233 | 733 len = sizeof(ngx_http_grpc_connection_start) - 1 |
734 + sizeof(ngx_http_grpc_frame_t); /* headers frame */ | |
735 | |
736 /* :method header */ | |
737 | |
738 if (r->method == NGX_HTTP_GET || r->method == NGX_HTTP_POST) { | |
739 len += 1; | |
740 tmp_len = 0; | |
741 | |
742 } else { | |
743 len += 1 + NGX_HTTP_V2_INT_OCTETS + r->method_name.len; | |
744 tmp_len = r->method_name.len; | |
745 } | |
746 | |
747 /* :scheme header */ | |
748 | |
749 len += 1; | |
750 | |
751 /* :path header */ | |
752 | |
753 if (r->valid_unparsed_uri) { | |
754 escape = 0; | |
755 uri_len = r->unparsed_uri.len; | |
756 | |
757 } else { | |
758 escape = 2 * ngx_escape_uri(NULL, r->uri.data, r->uri.len, | |
759 NGX_ESCAPE_URI); | |
760 uri_len = r->uri.len + escape + sizeof("?") - 1 + r->args.len; | |
761 } | |
762 | |
763 len += 1 + NGX_HTTP_V2_INT_OCTETS + uri_len; | |
764 | |
765 if (tmp_len < uri_len) { | |
766 tmp_len = uri_len; | |
767 } | |
768 | |
769 /* :authority header */ | |
770 | |
771 if (!glcf->host_set) { | |
7619
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
772 len += 1 + NGX_HTTP_V2_INT_OCTETS + ctx->host.len; |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
773 |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
774 if (tmp_len < ctx->host.len) { |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
775 tmp_len = ctx->host.len; |
7233 | 776 } |
777 } | |
778 | |
779 /* other headers */ | |
780 | |
781 ngx_http_script_flush_no_cacheable_variables(r, glcf->headers.flushes); | |
782 ngx_memzero(&le, sizeof(ngx_http_script_engine_t)); | |
783 | |
784 le.ip = glcf->headers.lengths->elts; | |
785 le.request = r; | |
786 le.flushed = 1; | |
787 | |
788 while (*(uintptr_t *) le.ip) { | |
789 | |
790 lcode = *(ngx_http_script_len_code_pt *) le.ip; | |
791 key_len = lcode(&le); | |
792 | |
793 for (val_len = 0; *(uintptr_t *) le.ip; val_len += lcode(&le)) { | |
794 lcode = *(ngx_http_script_len_code_pt *) le.ip; | |
795 } | |
796 le.ip += sizeof(uintptr_t); | |
797 | |
798 if (val_len == 0) { | |
799 continue; | |
800 } | |
801 | |
802 len += 1 + NGX_HTTP_V2_INT_OCTETS + key_len | |
803 + NGX_HTTP_V2_INT_OCTETS + val_len; | |
804 | |
805 if (tmp_len < key_len) { | |
806 tmp_len = key_len; | |
807 } | |
808 | |
809 if (tmp_len < val_len) { | |
810 tmp_len = val_len; | |
811 } | |
812 } | |
813 | |
814 if (glcf->upstream.pass_request_headers) { | |
815 part = &r->headers_in.headers.part; | |
816 header = part->elts; | |
817 | |
818 for (i = 0; /* void */; i++) { | |
819 | |
820 if (i >= part->nelts) { | |
821 if (part->next == NULL) { | |
822 break; | |
823 } | |
824 | |
825 part = part->next; | |
826 header = part->elts; | |
827 i = 0; | |
828 } | |
829 | |
830 if (ngx_hash_find(&glcf->headers.hash, header[i].hash, | |
831 header[i].lowcase_key, header[i].key.len)) | |
832 { | |
833 continue; | |
834 } | |
835 | |
836 len += 1 + NGX_HTTP_V2_INT_OCTETS + header[i].key.len | |
837 + NGX_HTTP_V2_INT_OCTETS + header[i].value.len; | |
838 | |
839 if (tmp_len < header[i].key.len) { | |
840 tmp_len = header[i].key.len; | |
841 } | |
842 | |
843 if (tmp_len < header[i].value.len) { | |
844 tmp_len = header[i].value.len; | |
845 } | |
846 } | |
847 } | |
848 | |
849 /* continuation frames */ | |
850 | |
851 len += sizeof(ngx_http_grpc_frame_t) | |
852 * (len / NGX_HTTP_V2_DEFAULT_FRAME_SIZE); | |
853 | |
854 | |
855 b = ngx_create_temp_buf(r->pool, len); | |
856 if (b == NULL) { | |
857 return NGX_ERROR; | |
858 } | |
859 | |
860 cl = ngx_alloc_chain_link(r->pool); | |
861 if (cl == NULL) { | |
862 return NGX_ERROR; | |
863 } | |
864 | |
865 cl->buf = b; | |
866 cl->next = NULL; | |
867 | |
868 tmp = ngx_palloc(r->pool, tmp_len * 3); | |
869 if (tmp == NULL) { | |
870 return NGX_ERROR; | |
871 } | |
872 | |
873 key_tmp = tmp + tmp_len; | |
874 val_tmp = tmp + 2 * tmp_len; | |
875 | |
876 /* connection preface */ | |
877 | |
878 b->last = ngx_copy(b->last, ngx_http_grpc_connection_start, | |
879 sizeof(ngx_http_grpc_connection_start) - 1); | |
880 | |
881 /* headers frame */ | |
882 | |
883 headers_frame = b->last; | |
884 | |
885 f = (ngx_http_grpc_frame_t *) b->last; | |
886 b->last += sizeof(ngx_http_grpc_frame_t); | |
887 | |
888 f->length_0 = 0; | |
889 f->length_1 = 0; | |
890 f->length_2 = 0; | |
891 f->type = NGX_HTTP_V2_HEADERS_FRAME; | |
892 f->flags = 0; | |
893 f->stream_id_0 = 0; | |
894 f->stream_id_1 = 0; | |
895 f->stream_id_2 = 0; | |
896 f->stream_id_3 = 1; | |
897 | |
898 if (r->method == NGX_HTTP_GET) { | |
899 *b->last++ = ngx_http_v2_indexed(NGX_HTTP_V2_METHOD_GET_INDEX); | |
900 | |
901 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
902 "grpc header: \":method: GET\""); | |
903 | |
904 } else if (r->method == NGX_HTTP_POST) { | |
905 *b->last++ = ngx_http_v2_indexed(NGX_HTTP_V2_METHOD_POST_INDEX); | |
906 | |
907 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
908 "grpc header: \":method: POST\""); | |
909 | |
910 } else { | |
911 *b->last++ = ngx_http_v2_inc_indexed(NGX_HTTP_V2_METHOD_INDEX); | |
912 b->last = ngx_http_v2_write_value(b->last, r->method_name.data, | |
913 r->method_name.len, tmp); | |
914 | |
915 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
916 "grpc header: \":method: %V\"", &r->method_name); | |
917 } | |
918 | |
919 #if (NGX_HTTP_SSL) | |
7619
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
920 if (u->ssl) { |
7233 | 921 *b->last++ = ngx_http_v2_indexed(NGX_HTTP_V2_SCHEME_HTTPS_INDEX); |
922 | |
923 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
924 "grpc header: \":scheme: https\""); | |
925 } else | |
926 #endif | |
927 { | |
928 *b->last++ = ngx_http_v2_indexed(NGX_HTTP_V2_SCHEME_HTTP_INDEX); | |
929 | |
930 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
931 "grpc header: \":scheme: http\""); | |
932 } | |
933 | |
934 if (r->valid_unparsed_uri) { | |
935 | |
936 if (r->unparsed_uri.len == 1 && r->unparsed_uri.data[0] == '/') { | |
937 *b->last++ = ngx_http_v2_indexed(NGX_HTTP_V2_PATH_ROOT_INDEX); | |
938 | |
939 } else { | |
940 *b->last++ = ngx_http_v2_inc_indexed(NGX_HTTP_V2_PATH_INDEX); | |
941 b->last = ngx_http_v2_write_value(b->last, r->unparsed_uri.data, | |
942 r->unparsed_uri.len, tmp); | |
943 } | |
944 | |
945 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
946 "grpc header: \":path: %V\"", &r->unparsed_uri); | |
947 | |
948 } else if (escape || r->args.len > 0) { | |
949 p = val_tmp; | |
950 | |
951 if (escape) { | |
952 p = (u_char *) ngx_escape_uri(p, r->uri.data, r->uri.len, | |
953 NGX_ESCAPE_URI); | |
954 | |
955 } else { | |
956 p = ngx_copy(p, r->uri.data, r->uri.len); | |
957 } | |
958 | |
959 if (r->args.len > 0) { | |
960 *p++ = '?'; | |
961 p = ngx_copy(p, r->args.data, r->args.len); | |
962 } | |
963 | |
964 *b->last++ = ngx_http_v2_inc_indexed(NGX_HTTP_V2_PATH_INDEX); | |
965 b->last = ngx_http_v2_write_value(b->last, val_tmp, p - val_tmp, tmp); | |
966 | |
967 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
968 "grpc header: \":path: %*s\"", p - val_tmp, val_tmp); | |
969 | |
970 } else { | |
971 *b->last++ = ngx_http_v2_inc_indexed(NGX_HTTP_V2_PATH_INDEX); | |
972 b->last = ngx_http_v2_write_value(b->last, r->uri.data, | |
973 r->uri.len, tmp); | |
974 | |
975 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
976 "grpc header: \":path: %V\"", &r->uri); | |
977 } | |
978 | |
979 if (!glcf->host_set) { | |
980 *b->last++ = ngx_http_v2_inc_indexed(NGX_HTTP_V2_AUTHORITY_INDEX); | |
7619
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
981 b->last = ngx_http_v2_write_value(b->last, ctx->host.data, |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
982 ctx->host.len, tmp); |
7233 | 983 |
984 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
7619
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
985 "grpc header: \":authority: %V\"", &ctx->host); |
7233 | 986 } |
987 | |
988 ngx_memzero(&e, sizeof(ngx_http_script_engine_t)); | |
989 | |
990 e.ip = glcf->headers.values->elts; | |
991 e.request = r; | |
992 e.flushed = 1; | |
993 | |
994 le.ip = glcf->headers.lengths->elts; | |
995 | |
996 while (*(uintptr_t *) le.ip) { | |
997 | |
998 lcode = *(ngx_http_script_len_code_pt *) le.ip; | |
999 key_len = lcode(&le); | |
1000 | |
1001 for (val_len = 0; *(uintptr_t *) le.ip; val_len += lcode(&le)) { | |
1002 lcode = *(ngx_http_script_len_code_pt *) le.ip; | |
1003 } | |
1004 le.ip += sizeof(uintptr_t); | |
1005 | |
1006 if (val_len == 0) { | |
1007 e.skip = 1; | |
1008 | |
1009 while (*(uintptr_t *) e.ip) { | |
1010 code = *(ngx_http_script_code_pt *) e.ip; | |
1011 code((ngx_http_script_engine_t *) &e); | |
1012 } | |
1013 e.ip += sizeof(uintptr_t); | |
1014 | |
1015 e.skip = 0; | |
1016 | |
1017 continue; | |
1018 } | |
1019 | |
1020 *b->last++ = 0; | |
1021 | |
1022 e.pos = key_tmp; | |
1023 | |
1024 code = *(ngx_http_script_code_pt *) e.ip; | |
1025 code((ngx_http_script_engine_t *) &e); | |
1026 | |
1027 b->last = ngx_http_v2_write_name(b->last, key_tmp, key_len, tmp); | |
1028 | |
1029 e.pos = val_tmp; | |
1030 | |
1031 while (*(uintptr_t *) e.ip) { | |
1032 code = *(ngx_http_script_code_pt *) e.ip; | |
1033 code((ngx_http_script_engine_t *) &e); | |
1034 } | |
1035 e.ip += sizeof(uintptr_t); | |
1036 | |
1037 b->last = ngx_http_v2_write_value(b->last, val_tmp, val_len, tmp); | |
1038 | |
1039 #if (NGX_DEBUG) | |
1040 if (r->connection->log->log_level & NGX_LOG_DEBUG_HTTP) { | |
1041 ngx_strlow(key_tmp, key_tmp, key_len); | |
1042 | |
1043 ngx_log_debug4(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
1044 "grpc header: \"%*s: %*s\"", | |
1045 key_len, key_tmp, val_len, val_tmp); | |
1046 } | |
1047 #endif | |
1048 } | |
1049 | |
1050 if (glcf->upstream.pass_request_headers) { | |
1051 part = &r->headers_in.headers.part; | |
1052 header = part->elts; | |
1053 | |
1054 for (i = 0; /* void */; i++) { | |
1055 | |
1056 if (i >= part->nelts) { | |
1057 if (part->next == NULL) { | |
1058 break; | |
1059 } | |
1060 | |
1061 part = part->next; | |
1062 header = part->elts; | |
1063 i = 0; | |
1064 } | |
1065 | |
1066 if (ngx_hash_find(&glcf->headers.hash, header[i].hash, | |
1067 header[i].lowcase_key, header[i].key.len)) | |
1068 { | |
1069 continue; | |
1070 } | |
1071 | |
1072 *b->last++ = 0; | |
1073 | |
1074 b->last = ngx_http_v2_write_name(b->last, header[i].key.data, | |
1075 header[i].key.len, tmp); | |
1076 | |
1077 b->last = ngx_http_v2_write_value(b->last, header[i].value.data, | |
1078 header[i].value.len, tmp); | |
1079 | |
1080 #if (NGX_DEBUG) | |
1081 if (r->connection->log->log_level & NGX_LOG_DEBUG_HTTP) { | |
1082 ngx_strlow(tmp, header[i].key.data, header[i].key.len); | |
1083 | |
1084 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
1085 "grpc header: \"%*s: %V\"", | |
1086 header[i].key.len, tmp, &header[i].value); | |
1087 } | |
1088 #endif | |
1089 } | |
1090 } | |
1091 | |
1092 /* update headers frame length */ | |
1093 | |
1094 len = b->last - headers_frame - sizeof(ngx_http_grpc_frame_t); | |
1095 | |
1096 if (len > NGX_HTTP_V2_DEFAULT_FRAME_SIZE) { | |
1097 len = NGX_HTTP_V2_DEFAULT_FRAME_SIZE; | |
1098 next = 1; | |
1099 | |
1100 } else { | |
1101 next = 0; | |
1102 } | |
1103 | |
1104 f = (ngx_http_grpc_frame_t *) headers_frame; | |
1105 | |
1106 f->length_0 = (u_char) ((len >> 16) & 0xff); | |
1107 f->length_1 = (u_char) ((len >> 8) & 0xff); | |
1108 f->length_2 = (u_char) (len & 0xff); | |
1109 | |
1110 /* create additional continuation frames */ | |
1111 | |
1112 p = headers_frame; | |
1113 | |
1114 while (next) { | |
1115 p += sizeof(ngx_http_grpc_frame_t) + NGX_HTTP_V2_DEFAULT_FRAME_SIZE; | |
1116 len = b->last - p; | |
1117 | |
1118 ngx_memmove(p + sizeof(ngx_http_grpc_frame_t), p, len); | |
1119 b->last += sizeof(ngx_http_grpc_frame_t); | |
1120 | |
1121 if (len > NGX_HTTP_V2_DEFAULT_FRAME_SIZE) { | |
1122 len = NGX_HTTP_V2_DEFAULT_FRAME_SIZE; | |
1123 next = 1; | |
1124 | |
1125 } else { | |
1126 next = 0; | |
1127 } | |
1128 | |
1129 f = (ngx_http_grpc_frame_t *) p; | |
1130 | |
1131 f->length_0 = (u_char) ((len >> 16) & 0xff); | |
1132 f->length_1 = (u_char) ((len >> 8) & 0xff); | |
1133 f->length_2 = (u_char) (len & 0xff); | |
1134 f->type = NGX_HTTP_V2_CONTINUATION_FRAME; | |
1135 f->flags = 0; | |
1136 f->stream_id_0 = 0; | |
1137 f->stream_id_1 = 0; | |
1138 f->stream_id_2 = 0; | |
1139 f->stream_id_3 = 1; | |
1140 } | |
1141 | |
1142 f->flags |= NGX_HTTP_V2_END_HEADERS_FLAG; | |
1143 | |
8218
a46fcf101cfc
Core: added format specifiers to output binary data as hex.
Vladimir Homutov <vl@nginx.com>
parents:
8183
diff
changeset
|
1144 ngx_log_debug4(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, |
a46fcf101cfc
Core: added format specifiers to output binary data as hex.
Vladimir Homutov <vl@nginx.com>
parents:
8183
diff
changeset
|
1145 "grpc header: %*xs%s, len: %uz", |
a46fcf101cfc
Core: added format specifiers to output binary data as hex.
Vladimir Homutov <vl@nginx.com>
parents:
8183
diff
changeset
|
1146 (size_t) ngx_min(b->last - b->pos, 256), b->pos, |
a46fcf101cfc
Core: added format specifiers to output binary data as hex.
Vladimir Homutov <vl@nginx.com>
parents:
8183
diff
changeset
|
1147 b->last - b->pos > 256 ? "..." : "", |
a46fcf101cfc
Core: added format specifiers to output binary data as hex.
Vladimir Homutov <vl@nginx.com>
parents:
8183
diff
changeset
|
1148 b->last - b->pos); |
7233 | 1149 |
1150 if (r->request_body_no_buffering) { | |
1151 | |
1152 u->request_bufs = cl; | |
1153 | |
1154 } else { | |
1155 | |
1156 body = u->request_bufs; | |
1157 u->request_bufs = cl; | |
1158 | |
1159 if (body == NULL) { | |
1160 f = (ngx_http_grpc_frame_t *) headers_frame; | |
1161 f->flags |= NGX_HTTP_V2_END_STREAM_FLAG; | |
1162 } | |
1163 | |
1164 while (body) { | |
1165 b = ngx_alloc_buf(r->pool); | |
1166 if (b == NULL) { | |
1167 return NGX_ERROR; | |
1168 } | |
1169 | |
1170 ngx_memcpy(b, body->buf, sizeof(ngx_buf_t)); | |
1171 | |
1172 cl->next = ngx_alloc_chain_link(r->pool); | |
1173 if (cl->next == NULL) { | |
1174 return NGX_ERROR; | |
1175 } | |
1176 | |
1177 cl = cl->next; | |
1178 cl->buf = b; | |
1179 | |
1180 body = body->next; | |
1181 } | |
1182 | |
1183 b->last_buf = 1; | |
1184 } | |
1185 | |
1186 u->output.output_filter = ngx_http_grpc_body_output_filter; | |
1187 u->output.filter_ctx = r; | |
1188 | |
1189 b->flush = 1; | |
1190 cl->next = NULL; | |
1191 | |
1192 return NGX_OK; | |
1193 } | |
1194 | |
1195 | |
1196 static ngx_int_t | |
1197 ngx_http_grpc_reinit_request(ngx_http_request_t *r) | |
1198 { | |
1199 ngx_http_grpc_ctx_t *ctx; | |
1200 | |
1201 ctx = ngx_http_get_module_ctx(r, ngx_http_grpc_module); | |
1202 | |
1203 if (ctx == NULL) { | |
1204 return NGX_OK; | |
1205 } | |
1206 | |
1207 ctx->state = 0; | |
1208 ctx->header_sent = 0; | |
1209 ctx->output_closed = 0; | |
7350
67c6cb7f477c
gRPC: disabled keepalive when sending control frames was blocked.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7349
diff
changeset
|
1210 ctx->output_blocked = 0; |
7233 | 1211 ctx->parsing_headers = 0; |
1212 ctx->end_stream = 0; | |
7349
f6047a579ca1
gRPC: improved keepalive handling.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7321
diff
changeset
|
1213 ctx->done = 0; |
7233 | 1214 ctx->status = 0; |
7892
2096b21fcd10
gRPC: RST_STREAM(NO_ERROR) handling (ticket #1792).
Ruslan Ermilov <ru@nginx.com>
parents:
7619
diff
changeset
|
1215 ctx->rst = 0; |
7233 | 1216 ctx->connection = NULL; |
1217 | |
1218 return NGX_OK; | |
1219 } | |
1220 | |
1221 | |
1222 static ngx_int_t | |
1223 ngx_http_grpc_body_output_filter(void *data, ngx_chain_t *in) | |
1224 { | |
1225 ngx_http_request_t *r = data; | |
1226 | |
1227 off_t file_pos; | |
1228 u_char *p, *pos, *start; | |
1229 size_t len, limit; | |
1230 ngx_buf_t *b; | |
1231 ngx_int_t rc; | |
1232 ngx_uint_t next, last; | |
1233 ngx_chain_t *cl, *out, **ll; | |
7349
f6047a579ca1
gRPC: improved keepalive handling.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7321
diff
changeset
|
1234 ngx_http_upstream_t *u; |
7233 | 1235 ngx_http_grpc_ctx_t *ctx; |
1236 ngx_http_grpc_frame_t *f; | |
1237 | |
1238 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
1239 "grpc output filter"); | |
1240 | |
1241 ctx = ngx_http_grpc_get_ctx(r); | |
1242 | |
1243 if (ctx == NULL) { | |
1244 return NGX_ERROR; | |
1245 } | |
1246 | |
1247 if (in) { | |
1248 if (ngx_chain_add_copy(r->pool, &ctx->in, in) != NGX_OK) { | |
1249 return NGX_ERROR; | |
1250 } | |
1251 } | |
1252 | |
1253 out = NULL; | |
1254 ll = &out; | |
1255 | |
1256 if (!ctx->header_sent) { | |
1257 /* first buffer contains headers */ | |
1258 | |
1259 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
1260 "grpc output header"); | |
1261 | |
1262 ctx->header_sent = 1; | |
1263 | |
1264 if (ctx->id != 1) { | |
1265 /* | |
1266 * keepalive connection: skip connection preface, | |
1267 * update stream identifiers | |
1268 */ | |
1269 | |
1270 b = ctx->in->buf; | |
1271 b->pos += sizeof(ngx_http_grpc_connection_start) - 1; | |
1272 | |
1273 p = b->pos; | |
1274 | |
1275 while (p < b->last) { | |
1276 f = (ngx_http_grpc_frame_t *) p; | |
1277 p += sizeof(ngx_http_grpc_frame_t); | |
1278 | |
1279 f->stream_id_0 = (u_char) ((ctx->id >> 24) & 0xff); | |
1280 f->stream_id_1 = (u_char) ((ctx->id >> 16) & 0xff); | |
1281 f->stream_id_2 = (u_char) ((ctx->id >> 8) & 0xff); | |
1282 f->stream_id_3 = (u_char) (ctx->id & 0xff); | |
1283 | |
1284 p += (f->length_0 << 16) + (f->length_1 << 8) + f->length_2; | |
1285 } | |
1286 } | |
1287 | |
1288 if (ctx->in->buf->last_buf) { | |
1289 ctx->output_closed = 1; | |
1290 } | |
1291 | |
1292 *ll = ctx->in; | |
1293 ll = &ctx->in->next; | |
1294 | |
1295 ctx->in = ctx->in->next; | |
1296 } | |
1297 | |
1298 if (ctx->out) { | |
1299 /* queued control frames */ | |
1300 | |
1301 *ll = ctx->out; | |
1302 | |
1303 for (cl = ctx->out, ll = &cl->next; cl; cl = cl->next) { | |
1304 ll = &cl->next; | |
1305 } | |
1306 | |
1307 ctx->out = NULL; | |
1308 } | |
1309 | |
1310 f = NULL; | |
1311 last = 0; | |
1312 | |
1313 limit = ngx_max(0, ctx->send_window); | |
1314 | |
1315 if (limit > ctx->connection->send_window) { | |
1316 limit = ctx->connection->send_window; | |
1317 } | |
1318 | |
1319 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
1320 "grpc output limit: %uz w:%z:%uz", | |
1321 limit, ctx->send_window, ctx->connection->send_window); | |
1322 | |
1323 #if (NGX_SUPPRESS_WARN) | |
1324 file_pos = 0; | |
1325 pos = NULL; | |
1326 cl = NULL; | |
1327 #endif | |
1328 | |
1329 in = ctx->in; | |
1330 | |
1331 while (in && limit > 0) { | |
1332 | |
1333 ngx_log_debug7(NGX_LOG_DEBUG_EVENT, r->connection->log, 0, | |
1334 "grpc output in l:%d f:%d %p, pos %p, size: %z " | |
1335 "file: %O, size: %O", | |
1336 in->buf->last_buf, | |
1337 in->buf->in_file, | |
1338 in->buf->start, in->buf->pos, | |
1339 in->buf->last - in->buf->pos, | |
1340 in->buf->file_pos, | |
1341 in->buf->file_last - in->buf->file_pos); | |
1342 | |
1343 if (ngx_buf_special(in->buf)) { | |
1344 goto next; | |
1345 } | |
1346 | |
1347 if (in->buf->in_file) { | |
1348 file_pos = in->buf->file_pos; | |
1349 | |
1350 } else { | |
1351 pos = in->buf->pos; | |
1352 } | |
1353 | |
1354 next = 0; | |
1355 | |
1356 do { | |
1357 | |
1358 cl = ngx_http_grpc_get_buf(r, ctx); | |
1359 if (cl == NULL) { | |
1360 return NGX_ERROR; | |
1361 } | |
1362 | |
1363 b = cl->buf; | |
1364 | |
1365 f = (ngx_http_grpc_frame_t *) b->last; | |
1366 b->last += sizeof(ngx_http_grpc_frame_t); | |
1367 | |
1368 *ll = cl; | |
1369 ll = &cl->next; | |
1370 | |
1371 cl = ngx_chain_get_free_buf(r->pool, &ctx->free); | |
1372 if (cl == NULL) { | |
1373 return NGX_ERROR; | |
1374 } | |
1375 | |
1376 b = cl->buf; | |
1377 start = b->start; | |
1378 | |
1379 ngx_memcpy(b, in->buf, sizeof(ngx_buf_t)); | |
1380 | |
1381 /* | |
1382 * restore b->start to preserve memory allocated in the buffer, | |
1383 * to reuse it later for headers and control frames | |
1384 */ | |
1385 | |
1386 b->start = start; | |
1387 | |
1388 if (in->buf->in_file) { | |
1389 b->file_pos = file_pos; | |
1390 file_pos += ngx_min(NGX_HTTP_V2_DEFAULT_FRAME_SIZE, limit); | |
1391 | |
1392 if (file_pos >= in->buf->file_last) { | |
1393 file_pos = in->buf->file_last; | |
1394 next = 1; | |
1395 } | |
1396 | |
1397 b->file_last = file_pos; | |
1398 len = (ngx_uint_t) (file_pos - b->file_pos); | |
1399 | |
1400 } else { | |
1401 b->pos = pos; | |
1402 pos += ngx_min(NGX_HTTP_V2_DEFAULT_FRAME_SIZE, limit); | |
1403 | |
1404 if (pos >= in->buf->last) { | |
1405 pos = in->buf->last; | |
1406 next = 1; | |
1407 } | |
1408 | |
1409 b->last = pos; | |
1410 len = (ngx_uint_t) (pos - b->pos); | |
1411 } | |
1412 | |
1413 b->tag = (ngx_buf_tag_t) &ngx_http_grpc_body_output_filter; | |
1414 b->shadow = in->buf; | |
1415 b->last_shadow = next; | |
1416 | |
1417 b->last_buf = 0; | |
1418 b->last_in_chain = 0; | |
1419 | |
1420 *ll = cl; | |
1421 ll = &cl->next; | |
1422 | |
1423 f->length_0 = (u_char) ((len >> 16) & 0xff); | |
1424 f->length_1 = (u_char) ((len >> 8) & 0xff); | |
1425 f->length_2 = (u_char) (len & 0xff); | |
1426 f->type = NGX_HTTP_V2_DATA_FRAME; | |
1427 f->flags = 0; | |
1428 f->stream_id_0 = (u_char) ((ctx->id >> 24) & 0xff); | |
1429 f->stream_id_1 = (u_char) ((ctx->id >> 16) & 0xff); | |
1430 f->stream_id_2 = (u_char) ((ctx->id >> 8) & 0xff); | |
1431 f->stream_id_3 = (u_char) (ctx->id & 0xff); | |
1432 | |
1433 limit -= len; | |
1434 ctx->send_window -= len; | |
1435 ctx->connection->send_window -= len; | |
1436 | |
1437 } while (!next && limit > 0); | |
1438 | |
1439 if (!next) { | |
1440 /* | |
1441 * if the buffer wasn't fully sent due to flow control limits, | |
1442 * preserve position for future use | |
1443 */ | |
1444 | |
1445 if (in->buf->in_file) { | |
1446 in->buf->file_pos = file_pos; | |
1447 | |
1448 } else { | |
1449 in->buf->pos = pos; | |
1450 } | |
1451 | |
1452 break; | |
1453 } | |
1454 | |
1455 next: | |
1456 | |
1457 if (in->buf->last_buf) { | |
1458 last = 1; | |
1459 } | |
1460 | |
1461 in = in->next; | |
1462 } | |
1463 | |
1464 ctx->in = in; | |
1465 | |
1466 if (last) { | |
1467 | |
1468 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
1469 "grpc output last"); | |
1470 | |
1471 ctx->output_closed = 1; | |
1472 | |
1473 if (f) { | |
1474 f->flags |= NGX_HTTP_V2_END_STREAM_FLAG; | |
1475 | |
1476 } else { | |
1477 cl = ngx_http_grpc_get_buf(r, ctx); | |
1478 if (cl == NULL) { | |
1479 return NGX_ERROR; | |
1480 } | |
1481 | |
1482 b = cl->buf; | |
1483 | |
1484 f = (ngx_http_grpc_frame_t *) b->last; | |
1485 b->last += sizeof(ngx_http_grpc_frame_t); | |
1486 | |
1487 f->length_0 = 0; | |
1488 f->length_1 = 0; | |
1489 f->length_2 = 0; | |
1490 f->type = NGX_HTTP_V2_DATA_FRAME; | |
1491 f->flags = NGX_HTTP_V2_END_STREAM_FLAG; | |
1492 f->stream_id_0 = (u_char) ((ctx->id >> 24) & 0xff); | |
1493 f->stream_id_1 = (u_char) ((ctx->id >> 16) & 0xff); | |
1494 f->stream_id_2 = (u_char) ((ctx->id >> 8) & 0xff); | |
1495 f->stream_id_3 = (u_char) (ctx->id & 0xff); | |
1496 | |
1497 *ll = cl; | |
1498 ll = &cl->next; | |
1499 } | |
1500 | |
1501 cl->buf->last_buf = 1; | |
1502 } | |
1503 | |
1504 *ll = NULL; | |
1505 | |
1506 #if (NGX_DEBUG) | |
1507 | |
1508 for (cl = out; cl; cl = cl->next) { | |
1509 ngx_log_debug7(NGX_LOG_DEBUG_EVENT, r->connection->log, 0, | |
1510 "grpc output out l:%d f:%d %p, pos %p, size: %z " | |
1511 "file: %O, size: %O", | |
1512 cl->buf->last_buf, | |
1513 cl->buf->in_file, | |
1514 cl->buf->start, cl->buf->pos, | |
1515 cl->buf->last - cl->buf->pos, | |
1516 cl->buf->file_pos, | |
1517 cl->buf->file_last - cl->buf->file_pos); | |
1518 } | |
1519 | |
1520 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
1521 "grpc output limit: %uz w:%z:%uz", | |
1522 limit, ctx->send_window, ctx->connection->send_window); | |
1523 | |
1524 #endif | |
1525 | |
1526 rc = ngx_chain_writer(&r->upstream->writer, out); | |
1527 | |
1528 ngx_chain_update_chains(r->pool, &ctx->free, &ctx->busy, &out, | |
1529 (ngx_buf_tag_t) &ngx_http_grpc_body_output_filter); | |
1530 | |
1531 for (cl = ctx->free; cl; cl = cl->next) { | |
1532 | |
1533 /* mark original buffers as sent */ | |
1534 | |
1535 if (cl->buf->shadow) { | |
1536 if (cl->buf->last_shadow) { | |
1537 b = cl->buf->shadow; | |
1538 b->pos = b->last; | |
1539 } | |
1540 | |
1541 cl->buf->shadow = NULL; | |
1542 } | |
1543 } | |
1544 | |
1545 if (rc == NGX_OK && ctx->in) { | |
1546 rc = NGX_AGAIN; | |
1547 } | |
1548 | |
7350
67c6cb7f477c
gRPC: disabled keepalive when sending control frames was blocked.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7349
diff
changeset
|
1549 if (rc == NGX_AGAIN) { |
67c6cb7f477c
gRPC: disabled keepalive when sending control frames was blocked.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7349
diff
changeset
|
1550 ctx->output_blocked = 1; |
67c6cb7f477c
gRPC: disabled keepalive when sending control frames was blocked.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7349
diff
changeset
|
1551 |
67c6cb7f477c
gRPC: disabled keepalive when sending control frames was blocked.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7349
diff
changeset
|
1552 } else { |
67c6cb7f477c
gRPC: disabled keepalive when sending control frames was blocked.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7349
diff
changeset
|
1553 ctx->output_blocked = 0; |
67c6cb7f477c
gRPC: disabled keepalive when sending control frames was blocked.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7349
diff
changeset
|
1554 } |
67c6cb7f477c
gRPC: disabled keepalive when sending control frames was blocked.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7349
diff
changeset
|
1555 |
7349
f6047a579ca1
gRPC: improved keepalive handling.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7321
diff
changeset
|
1556 if (ctx->done) { |
f6047a579ca1
gRPC: improved keepalive handling.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7321
diff
changeset
|
1557 |
f6047a579ca1
gRPC: improved keepalive handling.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7321
diff
changeset
|
1558 /* |
f6047a579ca1
gRPC: improved keepalive handling.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7321
diff
changeset
|
1559 * We have already got the response and were sending some additional |
f6047a579ca1
gRPC: improved keepalive handling.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7321
diff
changeset
|
1560 * control frames. Even if there is still something unsent, stop |
f6047a579ca1
gRPC: improved keepalive handling.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7321
diff
changeset
|
1561 * here anyway. |
f6047a579ca1
gRPC: improved keepalive handling.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7321
diff
changeset
|
1562 */ |
f6047a579ca1
gRPC: improved keepalive handling.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7321
diff
changeset
|
1563 |
f6047a579ca1
gRPC: improved keepalive handling.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7321
diff
changeset
|
1564 u = r->upstream; |
f6047a579ca1
gRPC: improved keepalive handling.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7321
diff
changeset
|
1565 u->length = 0; |
f6047a579ca1
gRPC: improved keepalive handling.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7321
diff
changeset
|
1566 |
f6047a579ca1
gRPC: improved keepalive handling.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7321
diff
changeset
|
1567 if (ctx->in == NULL |
f6047a579ca1
gRPC: improved keepalive handling.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7321
diff
changeset
|
1568 && ctx->out == NULL |
f6047a579ca1
gRPC: improved keepalive handling.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7321
diff
changeset
|
1569 && ctx->output_closed |
7350
67c6cb7f477c
gRPC: disabled keepalive when sending control frames was blocked.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7349
diff
changeset
|
1570 && !ctx->output_blocked |
7349
f6047a579ca1
gRPC: improved keepalive handling.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7321
diff
changeset
|
1571 && ctx->state == ngx_http_grpc_st_start) |
f6047a579ca1
gRPC: improved keepalive handling.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7321
diff
changeset
|
1572 { |
f6047a579ca1
gRPC: improved keepalive handling.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7321
diff
changeset
|
1573 u->keepalive = 1; |
f6047a579ca1
gRPC: improved keepalive handling.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7321
diff
changeset
|
1574 } |
f6047a579ca1
gRPC: improved keepalive handling.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7321
diff
changeset
|
1575 |
f6047a579ca1
gRPC: improved keepalive handling.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7321
diff
changeset
|
1576 ngx_post_event(u->peer.connection->read, &ngx_posted_events); |
f6047a579ca1
gRPC: improved keepalive handling.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7321
diff
changeset
|
1577 } |
f6047a579ca1
gRPC: improved keepalive handling.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7321
diff
changeset
|
1578 |
7233 | 1579 return rc; |
1580 } | |
1581 | |
1582 | |
1583 static ngx_int_t | |
1584 ngx_http_grpc_process_header(ngx_http_request_t *r) | |
1585 { | |
1586 ngx_str_t *status_line; | |
1587 ngx_int_t rc, status; | |
1588 ngx_buf_t *b; | |
1589 ngx_table_elt_t *h; | |
1590 ngx_http_upstream_t *u; | |
1591 ngx_http_grpc_ctx_t *ctx; | |
1592 ngx_http_upstream_header_t *hh; | |
1593 ngx_http_upstream_main_conf_t *umcf; | |
1594 | |
1595 u = r->upstream; | |
1596 b = &u->buffer; | |
1597 | |
8218
a46fcf101cfc
Core: added format specifiers to output binary data as hex.
Vladimir Homutov <vl@nginx.com>
parents:
8183
diff
changeset
|
1598 ngx_log_debug4(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, |
a46fcf101cfc
Core: added format specifiers to output binary data as hex.
Vladimir Homutov <vl@nginx.com>
parents:
8183
diff
changeset
|
1599 "grpc response: %*xs%s, len: %uz", |
a46fcf101cfc
Core: added format specifiers to output binary data as hex.
Vladimir Homutov <vl@nginx.com>
parents:
8183
diff
changeset
|
1600 (size_t) ngx_min(b->last - b->pos, 256), |
a46fcf101cfc
Core: added format specifiers to output binary data as hex.
Vladimir Homutov <vl@nginx.com>
parents:
8183
diff
changeset
|
1601 b->pos, b->last - b->pos > 256 ? "..." : "", |
a46fcf101cfc
Core: added format specifiers to output binary data as hex.
Vladimir Homutov <vl@nginx.com>
parents:
8183
diff
changeset
|
1602 b->last - b->pos); |
7233 | 1603 |
1604 ctx = ngx_http_grpc_get_ctx(r); | |
1605 | |
1606 if (ctx == NULL) { | |
1607 return NGX_ERROR; | |
1608 } | |
1609 | |
1610 umcf = ngx_http_get_module_main_conf(r, ngx_http_upstream_module); | |
1611 | |
1612 for ( ;; ) { | |
1613 | |
1614 if (ctx->state < ngx_http_grpc_st_payload) { | |
1615 | |
1616 rc = ngx_http_grpc_parse_frame(r, ctx, b); | |
1617 | |
1618 if (rc == NGX_AGAIN) { | |
1619 | |
1620 /* | |
1621 * there can be a lot of window update frames, | |
1622 * so we reset buffer if it is empty and we haven't | |
1623 * started parsing headers yet | |
1624 */ | |
1625 | |
1626 if (!ctx->parsing_headers) { | |
1627 b->pos = b->start; | |
1628 b->last = b->pos; | |
1629 } | |
1630 | |
1631 return NGX_AGAIN; | |
1632 } | |
1633 | |
1634 if (rc == NGX_ERROR) { | |
1635 return NGX_HTTP_UPSTREAM_INVALID_HEADER; | |
1636 } | |
1637 | |
1638 /* | |
1639 * RFC 7540 says that implementations MUST discard frames | |
1640 * that have unknown or unsupported types. However, extension | |
1641 * frames that appear in the middle of a header block are | |
1642 * not permitted. Also, for obvious reasons CONTINUATION frames | |
1643 * cannot appear before headers, and DATA frames are not expected | |
1644 * to appear before all headers are parsed. | |
1645 */ | |
1646 | |
1647 if (ctx->type == NGX_HTTP_V2_DATA_FRAME | |
1648 || (ctx->type == NGX_HTTP_V2_CONTINUATION_FRAME | |
1649 && !ctx->parsing_headers) | |
1650 || (ctx->type != NGX_HTTP_V2_CONTINUATION_FRAME | |
1651 && ctx->parsing_headers)) | |
1652 { | |
1653 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
1654 "upstream sent unexpected http2 frame: %d", | |
1655 ctx->type); | |
1656 return NGX_HTTP_UPSTREAM_INVALID_HEADER; | |
1657 } | |
1658 | |
1659 if (ctx->stream_id && ctx->stream_id != ctx->id) { | |
1660 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
1661 "upstream sent frame for unknown stream %ui", | |
1662 ctx->stream_id); | |
1663 return NGX_HTTP_UPSTREAM_INVALID_HEADER; | |
1664 } | |
1665 } | |
1666 | |
1667 /* frame payload */ | |
1668 | |
1669 if (ctx->type == NGX_HTTP_V2_RST_STREAM_FRAME) { | |
1670 | |
1671 rc = ngx_http_grpc_parse_rst_stream(r, ctx, b); | |
1672 | |
1673 if (rc == NGX_AGAIN) { | |
1674 return NGX_AGAIN; | |
1675 } | |
1676 | |
1677 if (rc == NGX_ERROR) { | |
1678 return NGX_HTTP_UPSTREAM_INVALID_HEADER; | |
1679 } | |
1680 | |
1681 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
1682 "upstream rejected request with error %ui", | |
1683 ctx->error); | |
1684 | |
1685 return NGX_HTTP_UPSTREAM_INVALID_HEADER; | |
1686 } | |
1687 | |
1688 if (ctx->type == NGX_HTTP_V2_GOAWAY_FRAME) { | |
1689 | |
1690 rc = ngx_http_grpc_parse_goaway(r, ctx, b); | |
1691 | |
1692 if (rc == NGX_AGAIN) { | |
1693 return NGX_AGAIN; | |
1694 } | |
1695 | |
1696 if (rc == NGX_ERROR) { | |
1697 return NGX_HTTP_UPSTREAM_INVALID_HEADER; | |
1698 } | |
1699 | |
1700 /* | |
1701 * If stream_id is lower than one we use, our | |
1702 * request won't be processed and needs to be retried. | |
1703 * If stream_id is greater or equal to the one we use, | |
1704 * we can continue normally (except we can't use this | |
1705 * connection for additional requests). If there is | |
1706 * a real error, the connection will be closed. | |
1707 */ | |
1708 | |
1709 if (ctx->stream_id < ctx->id) { | |
1710 | |
1711 /* TODO: we can retry non-idempotent requests */ | |
1712 | |
1713 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
1714 "upstream sent goaway with error %ui", | |
1715 ctx->error); | |
1716 | |
1717 return NGX_HTTP_UPSTREAM_INVALID_HEADER; | |
1718 } | |
1719 | |
1720 continue; | |
1721 } | |
1722 | |
1723 if (ctx->type == NGX_HTTP_V2_WINDOW_UPDATE_FRAME) { | |
1724 | |
1725 rc = ngx_http_grpc_parse_window_update(r, ctx, b); | |
1726 | |
1727 if (rc == NGX_AGAIN) { | |
1728 return NGX_AGAIN; | |
1729 } | |
1730 | |
1731 if (rc == NGX_ERROR) { | |
1732 return NGX_HTTP_UPSTREAM_INVALID_HEADER; | |
1733 } | |
1734 | |
1735 if (ctx->in) { | |
1736 ngx_post_event(u->peer.connection->write, &ngx_posted_events); | |
1737 } | |
1738 | |
1739 continue; | |
1740 } | |
1741 | |
1742 if (ctx->type == NGX_HTTP_V2_SETTINGS_FRAME) { | |
1743 | |
1744 rc = ngx_http_grpc_parse_settings(r, ctx, b); | |
1745 | |
1746 if (rc == NGX_AGAIN) { | |
1747 return NGX_AGAIN; | |
1748 } | |
1749 | |
1750 if (rc == NGX_ERROR) { | |
1751 return NGX_HTTP_UPSTREAM_INVALID_HEADER; | |
1752 } | |
1753 | |
1754 if (ctx->in) { | |
1755 ngx_post_event(u->peer.connection->write, &ngx_posted_events); | |
1756 } | |
1757 | |
1758 continue; | |
1759 } | |
1760 | |
1761 if (ctx->type == NGX_HTTP_V2_PING_FRAME) { | |
1762 | |
1763 rc = ngx_http_grpc_parse_ping(r, ctx, b); | |
1764 | |
1765 if (rc == NGX_AGAIN) { | |
1766 return NGX_AGAIN; | |
1767 } | |
1768 | |
1769 if (rc == NGX_ERROR) { | |
1770 return NGX_HTTP_UPSTREAM_INVALID_HEADER; | |
1771 } | |
1772 | |
1773 ngx_post_event(u->peer.connection->write, &ngx_posted_events); | |
1774 continue; | |
1775 } | |
1776 | |
1777 if (ctx->type == NGX_HTTP_V2_PUSH_PROMISE_FRAME) { | |
1778 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
1779 "upstream sent unexpected push promise frame"); | |
1780 return NGX_HTTP_UPSTREAM_INVALID_HEADER; | |
1781 } | |
1782 | |
1783 if (ctx->type != NGX_HTTP_V2_HEADERS_FRAME | |
1784 && ctx->type != NGX_HTTP_V2_CONTINUATION_FRAME) | |
1785 { | |
1786 /* priority, unknown frames */ | |
1787 | |
1788 if (b->last - b->pos < (ssize_t) ctx->rest) { | |
1789 ctx->rest -= b->last - b->pos; | |
1790 b->pos = b->last; | |
1791 return NGX_AGAIN; | |
1792 } | |
1793 | |
1794 b->pos += ctx->rest; | |
1795 ctx->rest = 0; | |
1796 ctx->state = ngx_http_grpc_st_start; | |
1797 | |
1798 continue; | |
1799 } | |
1800 | |
1801 /* headers */ | |
1802 | |
1803 for ( ;; ) { | |
1804 | |
1805 rc = ngx_http_grpc_parse_header(r, ctx, b); | |
1806 | |
1807 if (rc == NGX_AGAIN) { | |
1808 break; | |
1809 } | |
1810 | |
1811 if (rc == NGX_OK) { | |
1812 | |
1813 /* a header line has been parsed successfully */ | |
1814 | |
1815 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
1816 "grpc header: \"%V: %V\"", | |
1817 &ctx->name, &ctx->value); | |
1818 | |
1819 if (ctx->name.len && ctx->name.data[0] == ':') { | |
1820 | |
1821 if (ctx->name.len != sizeof(":status") - 1 | |
1822 || ngx_strncmp(ctx->name.data, ":status", | |
1823 sizeof(":status") - 1) | |
1824 != 0) | |
1825 { | |
1826 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
1827 "upstream sent invalid header \"%V: %V\"", | |
1828 &ctx->name, &ctx->value); | |
1829 return NGX_HTTP_UPSTREAM_INVALID_HEADER; | |
1830 } | |
1831 | |
1832 if (ctx->status) { | |
1833 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
1834 "upstream sent duplicate :status header"); | |
1835 return NGX_HTTP_UPSTREAM_INVALID_HEADER; | |
1836 } | |
1837 | |
1838 status_line = &ctx->value; | |
1839 | |
1840 if (status_line->len != 3) { | |
1841 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
1842 "upstream sent invalid :status \"%V\"", | |
1843 status_line); | |
1844 return NGX_HTTP_UPSTREAM_INVALID_HEADER; | |
1845 } | |
1846 | |
1847 status = ngx_atoi(status_line->data, 3); | |
1848 | |
1849 if (status == NGX_ERROR) { | |
1850 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
1851 "upstream sent invalid :status \"%V\"", | |
1852 status_line); | |
1853 return NGX_HTTP_UPSTREAM_INVALID_HEADER; | |
1854 } | |
1855 | |
1856 if (status < NGX_HTTP_OK) { | |
1857 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
1858 "upstream sent unexpected :status \"%V\"", | |
1859 status_line); | |
1860 return NGX_HTTP_UPSTREAM_INVALID_HEADER; | |
1861 } | |
1862 | |
1863 u->headers_in.status_n = status; | |
1864 | |
1865 if (u->state && u->state->status == 0) { | |
1866 u->state->status = status; | |
1867 } | |
1868 | |
1869 ctx->status = 1; | |
1870 | |
1871 continue; | |
1872 | |
1873 } else if (!ctx->status) { | |
1874 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
1875 "upstream sent no :status header"); | |
1876 return NGX_HTTP_UPSTREAM_INVALID_HEADER; | |
1877 } | |
1878 | |
1879 h = ngx_list_push(&u->headers_in.headers); | |
1880 if (h == NULL) { | |
1881 return NGX_ERROR; | |
1882 } | |
1883 | |
1884 h->key = ctx->name; | |
1885 h->value = ctx->value; | |
1886 h->lowcase_key = h->key.data; | |
1887 h->hash = ngx_hash_key(h->key.data, h->key.len); | |
1888 | |
1889 hh = ngx_hash_find(&umcf->headers_in_hash, h->hash, | |
1890 h->lowcase_key, h->key.len); | |
1891 | |
1892 if (hh && hh->handler(r, h, hh->offset) != NGX_OK) { | |
1893 return NGX_ERROR; | |
1894 } | |
1895 | |
1896 continue; | |
1897 } | |
1898 | |
1899 if (rc == NGX_HTTP_PARSE_HEADER_DONE) { | |
1900 | |
1901 /* a whole header has been parsed successfully */ | |
1902 | |
1903 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
1904 "grpc header done"); | |
1905 | |
7235
c2a0a838c40f
gRPC: special handling of "trailer only" responses.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7234
diff
changeset
|
1906 if (ctx->end_stream) { |
c2a0a838c40f
gRPC: special handling of "trailer only" responses.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7234
diff
changeset
|
1907 u->headers_in.content_length_n = 0; |
c2a0a838c40f
gRPC: special handling of "trailer only" responses.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7234
diff
changeset
|
1908 |
c2a0a838c40f
gRPC: special handling of "trailer only" responses.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7234
diff
changeset
|
1909 if (ctx->in == NULL |
c2a0a838c40f
gRPC: special handling of "trailer only" responses.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7234
diff
changeset
|
1910 && ctx->out == NULL |
c2a0a838c40f
gRPC: special handling of "trailer only" responses.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7234
diff
changeset
|
1911 && ctx->output_closed |
7350
67c6cb7f477c
gRPC: disabled keepalive when sending control frames was blocked.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7349
diff
changeset
|
1912 && !ctx->output_blocked |
7235
c2a0a838c40f
gRPC: special handling of "trailer only" responses.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7234
diff
changeset
|
1913 && b->last == b->pos) |
c2a0a838c40f
gRPC: special handling of "trailer only" responses.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7234
diff
changeset
|
1914 { |
c2a0a838c40f
gRPC: special handling of "trailer only" responses.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7234
diff
changeset
|
1915 u->keepalive = 1; |
c2a0a838c40f
gRPC: special handling of "trailer only" responses.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7234
diff
changeset
|
1916 } |
7233 | 1917 } |
1918 | |
1919 return NGX_OK; | |
1920 } | |
1921 | |
1922 /* there was error while a header line parsing */ | |
1923 | |
1924 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
1925 "upstream sent invalid header"); | |
1926 | |
1927 return NGX_HTTP_UPSTREAM_INVALID_HEADER; | |
1928 } | |
1929 | |
1930 /* rc == NGX_AGAIN */ | |
1931 | |
1932 if (ctx->rest == 0) { | |
1933 ctx->state = ngx_http_grpc_st_start; | |
1934 continue; | |
1935 } | |
1936 | |
1937 return NGX_AGAIN; | |
1938 } | |
1939 } | |
1940 | |
1941 | |
1942 static ngx_int_t | |
1943 ngx_http_grpc_filter_init(void *data) | |
1944 { | |
1945 ngx_http_grpc_ctx_t *ctx = data; | |
1946 | |
1947 ngx_http_request_t *r; | |
1948 ngx_http_upstream_t *u; | |
1949 | |
1950 r = ctx->request; | |
1951 u = r->upstream; | |
1952 | |
7983
39501ce97e29
gRPC: generate error when response size is wrong.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7893
diff
changeset
|
1953 if (u->headers_in.status_n == NGX_HTTP_NO_CONTENT |
39501ce97e29
gRPC: generate error when response size is wrong.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7893
diff
changeset
|
1954 || u->headers_in.status_n == NGX_HTTP_NOT_MODIFIED |
39501ce97e29
gRPC: generate error when response size is wrong.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7893
diff
changeset
|
1955 || r->method == NGX_HTTP_HEAD) |
39501ce97e29
gRPC: generate error when response size is wrong.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7893
diff
changeset
|
1956 { |
39501ce97e29
gRPC: generate error when response size is wrong.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7893
diff
changeset
|
1957 ctx->length = 0; |
39501ce97e29
gRPC: generate error when response size is wrong.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7893
diff
changeset
|
1958 |
39501ce97e29
gRPC: generate error when response size is wrong.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7893
diff
changeset
|
1959 } else { |
39501ce97e29
gRPC: generate error when response size is wrong.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7893
diff
changeset
|
1960 ctx->length = u->headers_in.content_length_n; |
39501ce97e29
gRPC: generate error when response size is wrong.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7893
diff
changeset
|
1961 } |
7233 | 1962 |
1963 if (ctx->end_stream) { | |
7983
39501ce97e29
gRPC: generate error when response size is wrong.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7893
diff
changeset
|
1964 |
39501ce97e29
gRPC: generate error when response size is wrong.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7893
diff
changeset
|
1965 if (ctx->length > 0) { |
39501ce97e29
gRPC: generate error when response size is wrong.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7893
diff
changeset
|
1966 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, |
39501ce97e29
gRPC: generate error when response size is wrong.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7893
diff
changeset
|
1967 "upstream prematurely closed stream"); |
39501ce97e29
gRPC: generate error when response size is wrong.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7893
diff
changeset
|
1968 return NGX_ERROR; |
39501ce97e29
gRPC: generate error when response size is wrong.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7893
diff
changeset
|
1969 } |
39501ce97e29
gRPC: generate error when response size is wrong.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7893
diff
changeset
|
1970 |
7233 | 1971 u->length = 0; |
8228
88eca63261c3
gRPC: RST_STREAM(NO_ERROR) handling after "trailer only" responses.
Pavel Pautov <p.pautov@f5.com>
parents:
8218
diff
changeset
|
1972 ctx->done = 1; |
7983
39501ce97e29
gRPC: generate error when response size is wrong.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7893
diff
changeset
|
1973 |
39501ce97e29
gRPC: generate error when response size is wrong.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7893
diff
changeset
|
1974 } else { |
39501ce97e29
gRPC: generate error when response size is wrong.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7893
diff
changeset
|
1975 u->length = 1; |
7233 | 1976 } |
1977 | |
1978 return NGX_OK; | |
1979 } | |
1980 | |
1981 | |
1982 static ngx_int_t | |
1983 ngx_http_grpc_filter(void *data, ssize_t bytes) | |
1984 { | |
1985 ngx_http_grpc_ctx_t *ctx = data; | |
1986 | |
1987 ngx_int_t rc; | |
1988 ngx_buf_t *b, *buf; | |
1989 ngx_chain_t *cl, **ll; | |
1990 ngx_table_elt_t *h; | |
1991 ngx_http_request_t *r; | |
1992 ngx_http_upstream_t *u; | |
1993 | |
1994 r = ctx->request; | |
1995 u = r->upstream; | |
1996 b = &u->buffer; | |
1997 | |
1998 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
1999 "grpc filter bytes:%z", bytes); | |
2000 | |
2001 b->pos = b->last; | |
2002 b->last += bytes; | |
2003 | |
2004 for (cl = u->out_bufs, ll = &u->out_bufs; cl; cl = cl->next) { | |
2005 ll = &cl->next; | |
2006 } | |
2007 | |
2008 for ( ;; ) { | |
2009 | |
2010 if (ctx->state < ngx_http_grpc_st_payload) { | |
2011 | |
2012 rc = ngx_http_grpc_parse_frame(r, ctx, b); | |
2013 | |
2014 if (rc == NGX_AGAIN) { | |
7349
f6047a579ca1
gRPC: improved keepalive handling.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7321
diff
changeset
|
2015 |
f6047a579ca1
gRPC: improved keepalive handling.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7321
diff
changeset
|
2016 if (ctx->done) { |
f6047a579ca1
gRPC: improved keepalive handling.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7321
diff
changeset
|
2017 |
7983
39501ce97e29
gRPC: generate error when response size is wrong.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7893
diff
changeset
|
2018 if (ctx->length > 0) { |
39501ce97e29
gRPC: generate error when response size is wrong.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7893
diff
changeset
|
2019 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, |
39501ce97e29
gRPC: generate error when response size is wrong.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7893
diff
changeset
|
2020 "upstream prematurely closed stream"); |
39501ce97e29
gRPC: generate error when response size is wrong.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7893
diff
changeset
|
2021 return NGX_ERROR; |
39501ce97e29
gRPC: generate error when response size is wrong.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7893
diff
changeset
|
2022 } |
39501ce97e29
gRPC: generate error when response size is wrong.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7893
diff
changeset
|
2023 |
7349
f6047a579ca1
gRPC: improved keepalive handling.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7321
diff
changeset
|
2024 /* |
f6047a579ca1
gRPC: improved keepalive handling.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7321
diff
changeset
|
2025 * We have finished parsing the response and the |
f6047a579ca1
gRPC: improved keepalive handling.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7321
diff
changeset
|
2026 * remaining control frames. If there are unsent |
f6047a579ca1
gRPC: improved keepalive handling.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7321
diff
changeset
|
2027 * control frames, post a write event to send them. |
f6047a579ca1
gRPC: improved keepalive handling.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7321
diff
changeset
|
2028 */ |
f6047a579ca1
gRPC: improved keepalive handling.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7321
diff
changeset
|
2029 |
f6047a579ca1
gRPC: improved keepalive handling.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7321
diff
changeset
|
2030 if (ctx->out) { |
f6047a579ca1
gRPC: improved keepalive handling.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7321
diff
changeset
|
2031 ngx_post_event(u->peer.connection->write, |
f6047a579ca1
gRPC: improved keepalive handling.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7321
diff
changeset
|
2032 &ngx_posted_events); |
f6047a579ca1
gRPC: improved keepalive handling.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7321
diff
changeset
|
2033 return NGX_AGAIN; |
f6047a579ca1
gRPC: improved keepalive handling.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7321
diff
changeset
|
2034 } |
f6047a579ca1
gRPC: improved keepalive handling.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7321
diff
changeset
|
2035 |
f6047a579ca1
gRPC: improved keepalive handling.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7321
diff
changeset
|
2036 u->length = 0; |
f6047a579ca1
gRPC: improved keepalive handling.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7321
diff
changeset
|
2037 |
f6047a579ca1
gRPC: improved keepalive handling.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7321
diff
changeset
|
2038 if (ctx->in == NULL |
f6047a579ca1
gRPC: improved keepalive handling.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7321
diff
changeset
|
2039 && ctx->output_closed |
7350
67c6cb7f477c
gRPC: disabled keepalive when sending control frames was blocked.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7349
diff
changeset
|
2040 && !ctx->output_blocked |
7349
f6047a579ca1
gRPC: improved keepalive handling.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7321
diff
changeset
|
2041 && ctx->state == ngx_http_grpc_st_start) |
f6047a579ca1
gRPC: improved keepalive handling.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7321
diff
changeset
|
2042 { |
f6047a579ca1
gRPC: improved keepalive handling.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7321
diff
changeset
|
2043 u->keepalive = 1; |
f6047a579ca1
gRPC: improved keepalive handling.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7321
diff
changeset
|
2044 } |
f6047a579ca1
gRPC: improved keepalive handling.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7321
diff
changeset
|
2045 |
f6047a579ca1
gRPC: improved keepalive handling.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7321
diff
changeset
|
2046 break; |
f6047a579ca1
gRPC: improved keepalive handling.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7321
diff
changeset
|
2047 } |
f6047a579ca1
gRPC: improved keepalive handling.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7321
diff
changeset
|
2048 |
7233 | 2049 return NGX_AGAIN; |
2050 } | |
2051 | |
2052 if (rc == NGX_ERROR) { | |
2053 return NGX_ERROR; | |
2054 } | |
2055 | |
2056 if ((ctx->type == NGX_HTTP_V2_CONTINUATION_FRAME | |
2057 && !ctx->parsing_headers) | |
2058 || (ctx->type != NGX_HTTP_V2_CONTINUATION_FRAME | |
2059 && ctx->parsing_headers)) | |
2060 { | |
2061 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
2062 "upstream sent unexpected http2 frame: %d", | |
2063 ctx->type); | |
2064 return NGX_ERROR; | |
2065 } | |
2066 | |
2067 if (ctx->type == NGX_HTTP_V2_DATA_FRAME) { | |
2068 | |
2069 if (ctx->stream_id != ctx->id) { | |
2070 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
2071 "upstream sent data frame " | |
2072 "for unknown stream %ui", | |
2073 ctx->stream_id); | |
2074 return NGX_ERROR; | |
2075 } | |
2076 | |
7983
39501ce97e29
gRPC: generate error when response size is wrong.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7893
diff
changeset
|
2077 if (ctx->length != -1) { |
39501ce97e29
gRPC: generate error when response size is wrong.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7893
diff
changeset
|
2078 if ((off_t) ctx->rest > ctx->length) { |
39501ce97e29
gRPC: generate error when response size is wrong.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7893
diff
changeset
|
2079 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, |
39501ce97e29
gRPC: generate error when response size is wrong.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7893
diff
changeset
|
2080 "upstream sent response body larger " |
39501ce97e29
gRPC: generate error when response size is wrong.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7893
diff
changeset
|
2081 "than indicated content length"); |
39501ce97e29
gRPC: generate error when response size is wrong.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7893
diff
changeset
|
2082 return NGX_ERROR; |
39501ce97e29
gRPC: generate error when response size is wrong.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7893
diff
changeset
|
2083 } |
39501ce97e29
gRPC: generate error when response size is wrong.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7893
diff
changeset
|
2084 |
39501ce97e29
gRPC: generate error when response size is wrong.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7893
diff
changeset
|
2085 ctx->length -= ctx->rest; |
39501ce97e29
gRPC: generate error when response size is wrong.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7893
diff
changeset
|
2086 } |
39501ce97e29
gRPC: generate error when response size is wrong.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7893
diff
changeset
|
2087 |
7233 | 2088 if (ctx->rest > ctx->recv_window) { |
2089 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
2090 "upstream violated stream flow control, " | |
2091 "received %uz data frame with window %uz", | |
2092 ctx->rest, ctx->recv_window); | |
2093 return NGX_ERROR; | |
2094 } | |
2095 | |
2096 if (ctx->rest > ctx->connection->recv_window) { | |
2097 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
2098 "upstream violated connection flow control, " | |
2099 "received %uz data frame with window %uz", | |
2100 ctx->rest, ctx->connection->recv_window); | |
2101 return NGX_ERROR; | |
2102 } | |
2103 | |
2104 ctx->recv_window -= ctx->rest; | |
2105 ctx->connection->recv_window -= ctx->rest; | |
2106 | |
2107 if (ctx->connection->recv_window < NGX_HTTP_V2_MAX_WINDOW / 4 | |
2108 || ctx->recv_window < NGX_HTTP_V2_MAX_WINDOW / 4) | |
2109 { | |
2110 if (ngx_http_grpc_send_window_update(r, ctx) != NGX_OK) { | |
2111 return NGX_ERROR; | |
2112 } | |
2113 | |
2114 ngx_post_event(u->peer.connection->write, | |
2115 &ngx_posted_events); | |
2116 } | |
2117 } | |
2118 | |
2119 if (ctx->stream_id && ctx->stream_id != ctx->id) { | |
2120 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
2121 "upstream sent frame for unknown stream %ui", | |
2122 ctx->stream_id); | |
2123 return NGX_ERROR; | |
2124 } | |
2125 | |
7892
2096b21fcd10
gRPC: RST_STREAM(NO_ERROR) handling (ticket #1792).
Ruslan Ermilov <ru@nginx.com>
parents:
7619
diff
changeset
|
2126 if (ctx->stream_id && ctx->done |
7893
716eddd74bc2
gRPC: WINDOW_UPDATE after END_STREAM handling (ticket #1797).
Ruslan Ermilov <ru@nginx.com>
parents:
7892
diff
changeset
|
2127 && ctx->type != NGX_HTTP_V2_RST_STREAM_FRAME |
716eddd74bc2
gRPC: WINDOW_UPDATE after END_STREAM handling (ticket #1797).
Ruslan Ermilov <ru@nginx.com>
parents:
7892
diff
changeset
|
2128 && ctx->type != NGX_HTTP_V2_WINDOW_UPDATE_FRAME) |
7892
2096b21fcd10
gRPC: RST_STREAM(NO_ERROR) handling (ticket #1792).
Ruslan Ermilov <ru@nginx.com>
parents:
7619
diff
changeset
|
2129 { |
7349
f6047a579ca1
gRPC: improved keepalive handling.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7321
diff
changeset
|
2130 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, |
f6047a579ca1
gRPC: improved keepalive handling.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7321
diff
changeset
|
2131 "upstream sent frame for closed stream %ui", |
f6047a579ca1
gRPC: improved keepalive handling.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7321
diff
changeset
|
2132 ctx->stream_id); |
f6047a579ca1
gRPC: improved keepalive handling.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7321
diff
changeset
|
2133 return NGX_ERROR; |
f6047a579ca1
gRPC: improved keepalive handling.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7321
diff
changeset
|
2134 } |
f6047a579ca1
gRPC: improved keepalive handling.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7321
diff
changeset
|
2135 |
7233 | 2136 ctx->padding = 0; |
2137 } | |
2138 | |
2139 if (ctx->state == ngx_http_grpc_st_padding) { | |
2140 | |
2141 if (b->last - b->pos < (ssize_t) ctx->rest) { | |
2142 ctx->rest -= b->last - b->pos; | |
2143 b->pos = b->last; | |
2144 return NGX_AGAIN; | |
2145 } | |
2146 | |
2147 b->pos += ctx->rest; | |
2148 ctx->rest = 0; | |
2149 ctx->state = ngx_http_grpc_st_start; | |
2150 | |
2151 if (ctx->flags & NGX_HTTP_V2_END_STREAM_FLAG) { | |
7349
f6047a579ca1
gRPC: improved keepalive handling.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7321
diff
changeset
|
2152 ctx->done = 1; |
7233 | 2153 } |
2154 | |
2155 continue; | |
2156 } | |
2157 | |
2158 /* frame payload */ | |
2159 | |
2160 if (ctx->type == NGX_HTTP_V2_RST_STREAM_FRAME) { | |
2161 | |
2162 rc = ngx_http_grpc_parse_rst_stream(r, ctx, b); | |
2163 | |
2164 if (rc == NGX_AGAIN) { | |
2165 return NGX_AGAIN; | |
2166 } | |
2167 | |
2168 if (rc == NGX_ERROR) { | |
2169 return NGX_ERROR; | |
2170 } | |
2171 | |
7892
2096b21fcd10
gRPC: RST_STREAM(NO_ERROR) handling (ticket #1792).
Ruslan Ermilov <ru@nginx.com>
parents:
7619
diff
changeset
|
2172 if (ctx->error || !ctx->done) { |
2096b21fcd10
gRPC: RST_STREAM(NO_ERROR) handling (ticket #1792).
Ruslan Ermilov <ru@nginx.com>
parents:
7619
diff
changeset
|
2173 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, |
2096b21fcd10
gRPC: RST_STREAM(NO_ERROR) handling (ticket #1792).
Ruslan Ermilov <ru@nginx.com>
parents:
7619
diff
changeset
|
2174 "upstream rejected request with error %ui", |
2096b21fcd10
gRPC: RST_STREAM(NO_ERROR) handling (ticket #1792).
Ruslan Ermilov <ru@nginx.com>
parents:
7619
diff
changeset
|
2175 ctx->error); |
2096b21fcd10
gRPC: RST_STREAM(NO_ERROR) handling (ticket #1792).
Ruslan Ermilov <ru@nginx.com>
parents:
7619
diff
changeset
|
2176 return NGX_ERROR; |
2096b21fcd10
gRPC: RST_STREAM(NO_ERROR) handling (ticket #1792).
Ruslan Ermilov <ru@nginx.com>
parents:
7619
diff
changeset
|
2177 } |
2096b21fcd10
gRPC: RST_STREAM(NO_ERROR) handling (ticket #1792).
Ruslan Ermilov <ru@nginx.com>
parents:
7619
diff
changeset
|
2178 |
2096b21fcd10
gRPC: RST_STREAM(NO_ERROR) handling (ticket #1792).
Ruslan Ermilov <ru@nginx.com>
parents:
7619
diff
changeset
|
2179 if (ctx->rst) { |
2096b21fcd10
gRPC: RST_STREAM(NO_ERROR) handling (ticket #1792).
Ruslan Ermilov <ru@nginx.com>
parents:
7619
diff
changeset
|
2180 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, |
2096b21fcd10
gRPC: RST_STREAM(NO_ERROR) handling (ticket #1792).
Ruslan Ermilov <ru@nginx.com>
parents:
7619
diff
changeset
|
2181 "upstream sent frame for closed stream %ui", |
2096b21fcd10
gRPC: RST_STREAM(NO_ERROR) handling (ticket #1792).
Ruslan Ermilov <ru@nginx.com>
parents:
7619
diff
changeset
|
2182 ctx->stream_id); |
2096b21fcd10
gRPC: RST_STREAM(NO_ERROR) handling (ticket #1792).
Ruslan Ermilov <ru@nginx.com>
parents:
7619
diff
changeset
|
2183 return NGX_ERROR; |
2096b21fcd10
gRPC: RST_STREAM(NO_ERROR) handling (ticket #1792).
Ruslan Ermilov <ru@nginx.com>
parents:
7619
diff
changeset
|
2184 } |
2096b21fcd10
gRPC: RST_STREAM(NO_ERROR) handling (ticket #1792).
Ruslan Ermilov <ru@nginx.com>
parents:
7619
diff
changeset
|
2185 |
2096b21fcd10
gRPC: RST_STREAM(NO_ERROR) handling (ticket #1792).
Ruslan Ermilov <ru@nginx.com>
parents:
7619
diff
changeset
|
2186 ctx->rst = 1; |
7233 | 2187 } |
2188 | |
2189 if (ctx->type == NGX_HTTP_V2_GOAWAY_FRAME) { | |
2190 | |
2191 rc = ngx_http_grpc_parse_goaway(r, ctx, b); | |
2192 | |
2193 if (rc == NGX_AGAIN) { | |
2194 return NGX_AGAIN; | |
2195 } | |
2196 | |
2197 if (rc == NGX_ERROR) { | |
2198 return NGX_ERROR; | |
2199 } | |
2200 | |
2201 /* | |
2202 * If stream_id is lower than one we use, our | |
2203 * request won't be processed and needs to be retried. | |
2204 * If stream_id is greater or equal to the one we use, | |
2205 * we can continue normally (except we can't use this | |
2206 * connection for additional requests). If there is | |
2207 * a real error, the connection will be closed. | |
2208 */ | |
2209 | |
2210 if (ctx->stream_id < ctx->id) { | |
2211 | |
2212 /* TODO: we can retry non-idempotent requests */ | |
2213 | |
2214 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
2215 "upstream sent goaway with error %ui", | |
2216 ctx->error); | |
2217 | |
2218 return NGX_ERROR; | |
2219 } | |
2220 | |
2221 continue; | |
2222 } | |
2223 | |
2224 if (ctx->type == NGX_HTTP_V2_WINDOW_UPDATE_FRAME) { | |
2225 | |
2226 rc = ngx_http_grpc_parse_window_update(r, ctx, b); | |
2227 | |
2228 if (rc == NGX_AGAIN) { | |
2229 return NGX_AGAIN; | |
2230 } | |
2231 | |
2232 if (rc == NGX_ERROR) { | |
2233 return NGX_ERROR; | |
2234 } | |
2235 | |
2236 if (ctx->in) { | |
2237 ngx_post_event(u->peer.connection->write, &ngx_posted_events); | |
2238 } | |
2239 | |
2240 continue; | |
2241 } | |
2242 | |
2243 if (ctx->type == NGX_HTTP_V2_SETTINGS_FRAME) { | |
2244 | |
2245 rc = ngx_http_grpc_parse_settings(r, ctx, b); | |
2246 | |
2247 if (rc == NGX_AGAIN) { | |
2248 return NGX_AGAIN; | |
2249 } | |
2250 | |
2251 if (rc == NGX_ERROR) { | |
2252 return NGX_ERROR; | |
2253 } | |
2254 | |
2255 if (ctx->in) { | |
2256 ngx_post_event(u->peer.connection->write, &ngx_posted_events); | |
2257 } | |
2258 | |
2259 continue; | |
2260 } | |
2261 | |
2262 if (ctx->type == NGX_HTTP_V2_PING_FRAME) { | |
2263 | |
2264 rc = ngx_http_grpc_parse_ping(r, ctx, b); | |
2265 | |
2266 if (rc == NGX_AGAIN) { | |
2267 return NGX_AGAIN; | |
2268 } | |
2269 | |
2270 if (rc == NGX_ERROR) { | |
2271 return NGX_ERROR; | |
2272 } | |
2273 | |
2274 ngx_post_event(u->peer.connection->write, &ngx_posted_events); | |
2275 continue; | |
2276 } | |
2277 | |
2278 if (ctx->type == NGX_HTTP_V2_PUSH_PROMISE_FRAME) { | |
2279 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
2280 "upstream sent unexpected push promise frame"); | |
2281 return NGX_ERROR; | |
2282 } | |
2283 | |
2284 if (ctx->type == NGX_HTTP_V2_HEADERS_FRAME | |
2285 || ctx->type == NGX_HTTP_V2_CONTINUATION_FRAME) | |
2286 { | |
2287 for ( ;; ) { | |
2288 | |
2289 rc = ngx_http_grpc_parse_header(r, ctx, b); | |
2290 | |
2291 if (rc == NGX_AGAIN) { | |
2292 break; | |
2293 } | |
2294 | |
2295 if (rc == NGX_OK) { | |
2296 | |
2297 /* a header line has been parsed successfully */ | |
2298 | |
2299 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
2300 "grpc trailer: \"%V: %V\"", | |
2301 &ctx->name, &ctx->value); | |
2302 | |
2303 if (ctx->name.len && ctx->name.data[0] == ':') { | |
2304 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
2305 "upstream sent invalid " | |
2306 "trailer \"%V: %V\"", | |
2307 &ctx->name, &ctx->value); | |
2308 return NGX_ERROR; | |
2309 } | |
2310 | |
2311 h = ngx_list_push(&u->headers_in.trailers); | |
2312 if (h == NULL) { | |
2313 return NGX_ERROR; | |
2314 } | |
2315 | |
2316 h->key = ctx->name; | |
2317 h->value = ctx->value; | |
2318 h->lowcase_key = h->key.data; | |
2319 h->hash = ngx_hash_key(h->key.data, h->key.len); | |
2320 | |
2321 continue; | |
2322 } | |
2323 | |
2324 if (rc == NGX_HTTP_PARSE_HEADER_DONE) { | |
2325 | |
2326 /* a whole header has been parsed successfully */ | |
2327 | |
2328 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
2329 "grpc trailer done"); | |
2330 | |
2331 if (ctx->end_stream) { | |
7349
f6047a579ca1
gRPC: improved keepalive handling.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7321
diff
changeset
|
2332 ctx->done = 1; |
f6047a579ca1
gRPC: improved keepalive handling.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7321
diff
changeset
|
2333 break; |
7233 | 2334 } |
2335 | |
2336 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
2337 "upstream sent trailer without " | |
2338 "end stream flag"); | |
2339 return NGX_ERROR; | |
2340 } | |
2341 | |
2342 /* there was error while a header line parsing */ | |
2343 | |
2344 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
2345 "upstream sent invalid trailer"); | |
2346 | |
2347 return NGX_ERROR; | |
2348 } | |
2349 | |
7349
f6047a579ca1
gRPC: improved keepalive handling.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7321
diff
changeset
|
2350 if (rc == NGX_HTTP_PARSE_HEADER_DONE) { |
f6047a579ca1
gRPC: improved keepalive handling.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7321
diff
changeset
|
2351 continue; |
f6047a579ca1
gRPC: improved keepalive handling.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7321
diff
changeset
|
2352 } |
f6047a579ca1
gRPC: improved keepalive handling.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7321
diff
changeset
|
2353 |
7233 | 2354 /* rc == NGX_AGAIN */ |
2355 | |
2356 if (ctx->rest == 0) { | |
2357 ctx->state = ngx_http_grpc_st_start; | |
2358 continue; | |
2359 } | |
2360 | |
2361 return NGX_AGAIN; | |
2362 } | |
2363 | |
2364 if (ctx->type != NGX_HTTP_V2_DATA_FRAME) { | |
2365 | |
2366 /* priority, unknown frames */ | |
2367 | |
2368 if (b->last - b->pos < (ssize_t) ctx->rest) { | |
2369 ctx->rest -= b->last - b->pos; | |
2370 b->pos = b->last; | |
2371 return NGX_AGAIN; | |
2372 } | |
2373 | |
2374 b->pos += ctx->rest; | |
2375 ctx->rest = 0; | |
2376 ctx->state = ngx_http_grpc_st_start; | |
2377 | |
2378 continue; | |
2379 } | |
2380 | |
2381 /* | |
2382 * data frame: | |
2383 * | |
2384 * +---------------+ | |
2385 * |Pad Length? (8)| | |
2386 * +---------------+-----------------------------------------------+ | |
2387 * | Data (*) ... | |
2388 * +---------------------------------------------------------------+ | |
2389 * | Padding (*) ... | |
2390 * +---------------------------------------------------------------+ | |
2391 */ | |
2392 | |
2393 if (ctx->flags & NGX_HTTP_V2_PADDED_FLAG) { | |
2394 | |
2395 if (ctx->rest == 0) { | |
2396 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
2397 "upstream sent too short http2 frame"); | |
2398 return NGX_ERROR; | |
2399 } | |
2400 | |
2401 if (b->pos == b->last) { | |
2402 return NGX_AGAIN; | |
2403 } | |
2404 | |
2405 ctx->flags &= ~NGX_HTTP_V2_PADDED_FLAG; | |
2406 ctx->padding = *b->pos++; | |
2407 ctx->rest -= 1; | |
2408 | |
2409 if (ctx->padding > ctx->rest) { | |
2410 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
2411 "upstream sent http2 frame with too long " | |
2412 "padding: %d in frame %uz", | |
2413 ctx->padding, ctx->rest); | |
2414 return NGX_ERROR; | |
2415 } | |
2416 | |
2417 continue; | |
2418 } | |
2419 | |
2420 if (ctx->rest == ctx->padding) { | |
2421 goto done; | |
2422 } | |
2423 | |
2424 if (b->pos == b->last) { | |
2425 return NGX_AGAIN; | |
2426 } | |
2427 | |
2428 cl = ngx_chain_get_free_buf(r->pool, &u->free_bufs); | |
2429 if (cl == NULL) { | |
2430 return NGX_ERROR; | |
2431 } | |
2432 | |
2433 *ll = cl; | |
2434 ll = &cl->next; | |
2435 | |
2436 buf = cl->buf; | |
2437 | |
2438 buf->flush = 1; | |
2439 buf->memory = 1; | |
2440 | |
2441 buf->pos = b->pos; | |
2442 buf->tag = u->output.tag; | |
2443 | |
2444 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
2445 "grpc output buf %p", buf->pos); | |
2446 | |
2447 if (b->last - b->pos < (ssize_t) ctx->rest - ctx->padding) { | |
2448 | |
2449 ctx->rest -= b->last - b->pos; | |
2450 b->pos = b->last; | |
2451 buf->last = b->pos; | |
2452 | |
2453 return NGX_AGAIN; | |
2454 } | |
2455 | |
2456 b->pos += ctx->rest - ctx->padding; | |
2457 buf->last = b->pos; | |
2458 ctx->rest = ctx->padding; | |
2459 | |
2460 done: | |
2461 | |
2462 if (ctx->padding) { | |
2463 ctx->state = ngx_http_grpc_st_padding; | |
2464 continue; | |
2465 } | |
2466 | |
2467 ctx->state = ngx_http_grpc_st_start; | |
2468 | |
2469 if (ctx->flags & NGX_HTTP_V2_END_STREAM_FLAG) { | |
7349
f6047a579ca1
gRPC: improved keepalive handling.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7321
diff
changeset
|
2470 ctx->done = 1; |
7233 | 2471 } |
2472 } | |
2473 | |
2474 return NGX_OK; | |
2475 } | |
2476 | |
2477 | |
2478 static ngx_int_t | |
2479 ngx_http_grpc_parse_frame(ngx_http_request_t *r, ngx_http_grpc_ctx_t *ctx, | |
2480 ngx_buf_t *b) | |
2481 { | |
2482 u_char ch, *p; | |
2483 ngx_http_grpc_state_e state; | |
2484 | |
2485 state = ctx->state; | |
2486 | |
2487 for (p = b->pos; p < b->last; p++) { | |
2488 ch = *p; | |
2489 | |
2490 #if 0 | |
2491 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
2492 "grpc frame byte: %02Xd, s:%d", ch, state); | |
2493 #endif | |
2494 | |
2495 switch (state) { | |
2496 | |
2497 case ngx_http_grpc_st_start: | |
2498 ctx->rest = ch << 16; | |
2499 state = ngx_http_grpc_st_length_2; | |
2500 break; | |
2501 | |
2502 case ngx_http_grpc_st_length_2: | |
2503 ctx->rest |= ch << 8; | |
2504 state = ngx_http_grpc_st_length_3; | |
2505 break; | |
2506 | |
2507 case ngx_http_grpc_st_length_3: | |
2508 ctx->rest |= ch; | |
2509 | |
2510 if (ctx->rest > NGX_HTTP_V2_DEFAULT_FRAME_SIZE) { | |
2511 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
2512 "upstream sent too large http2 frame: %uz", | |
2513 ctx->rest); | |
2514 return NGX_ERROR; | |
2515 } | |
2516 | |
2517 state = ngx_http_grpc_st_type; | |
2518 break; | |
2519 | |
2520 case ngx_http_grpc_st_type: | |
2521 ctx->type = ch; | |
2522 state = ngx_http_grpc_st_flags; | |
2523 break; | |
2524 | |
2525 case ngx_http_grpc_st_flags: | |
2526 ctx->flags = ch; | |
2527 state = ngx_http_grpc_st_stream_id; | |
2528 break; | |
2529 | |
2530 case ngx_http_grpc_st_stream_id: | |
2531 ctx->stream_id = (ch & 0x7f) << 24; | |
2532 state = ngx_http_grpc_st_stream_id_2; | |
2533 break; | |
2534 | |
2535 case ngx_http_grpc_st_stream_id_2: | |
2536 ctx->stream_id |= ch << 16; | |
2537 state = ngx_http_grpc_st_stream_id_3; | |
2538 break; | |
2539 | |
2540 case ngx_http_grpc_st_stream_id_3: | |
2541 ctx->stream_id |= ch << 8; | |
2542 state = ngx_http_grpc_st_stream_id_4; | |
2543 break; | |
2544 | |
2545 case ngx_http_grpc_st_stream_id_4: | |
2546 ctx->stream_id |= ch; | |
2547 | |
2548 ngx_log_debug4(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
2549 "grpc frame: %d, len: %uz, f:%d, i:%ui", | |
2550 ctx->type, ctx->rest, ctx->flags, ctx->stream_id); | |
2551 | |
2552 b->pos = p + 1; | |
2553 | |
2554 ctx->state = ngx_http_grpc_st_payload; | |
2555 ctx->frame_state = 0; | |
2556 | |
2557 return NGX_OK; | |
2558 | |
2559 /* suppress warning */ | |
2560 case ngx_http_grpc_st_payload: | |
2561 case ngx_http_grpc_st_padding: | |
2562 break; | |
2563 } | |
2564 } | |
2565 | |
2566 b->pos = p; | |
2567 ctx->state = state; | |
2568 | |
2569 return NGX_AGAIN; | |
2570 } | |
2571 | |
2572 | |
2573 static ngx_int_t | |
2574 ngx_http_grpc_parse_header(ngx_http_request_t *r, ngx_http_grpc_ctx_t *ctx, | |
2575 ngx_buf_t *b) | |
2576 { | |
2577 u_char ch, *p, *last; | |
2578 size_t min; | |
2579 ngx_int_t rc; | |
2580 enum { | |
2581 sw_start = 0, | |
2582 sw_padding_length, | |
2583 sw_dependency, | |
2584 sw_dependency_2, | |
2585 sw_dependency_3, | |
2586 sw_dependency_4, | |
2587 sw_weight, | |
2588 sw_fragment, | |
2589 sw_padding | |
2590 } state; | |
2591 | |
2592 state = ctx->frame_state; | |
2593 | |
2594 if (state == sw_start) { | |
2595 | |
2596 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
2597 "grpc parse header: start"); | |
2598 | |
2599 if (ctx->type == NGX_HTTP_V2_HEADERS_FRAME) { | |
2600 ctx->parsing_headers = 1; | |
2601 ctx->fragment_state = 0; | |
2602 | |
2603 min = (ctx->flags & NGX_HTTP_V2_PADDED_FLAG ? 1 : 0) | |
2604 + (ctx->flags & NGX_HTTP_V2_PRIORITY_FLAG ? 5 : 0); | |
2605 | |
2606 if (ctx->rest < min) { | |
2607 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
2608 "upstream sent headers frame " | |
2609 "with invalid length: %uz", | |
2610 ctx->rest); | |
2611 return NGX_ERROR; | |
2612 } | |
2613 | |
2614 if (ctx->flags & NGX_HTTP_V2_END_STREAM_FLAG) { | |
2615 ctx->end_stream = 1; | |
2616 } | |
2617 | |
2618 if (ctx->flags & NGX_HTTP_V2_PADDED_FLAG) { | |
2619 state = sw_padding_length; | |
2620 | |
2621 } else if (ctx->flags & NGX_HTTP_V2_PRIORITY_FLAG) { | |
2622 state = sw_dependency; | |
2623 | |
2624 } else { | |
2625 state = sw_fragment; | |
2626 } | |
2627 | |
2628 } else if (ctx->type == NGX_HTTP_V2_CONTINUATION_FRAME) { | |
2629 state = sw_fragment; | |
2630 } | |
2631 | |
2632 ctx->padding = 0; | |
7242
25a4353633a0
gRPC: fixed missing state save in frame header parsing.
Sergey Kandaurov <pluknet@nginx.com>
parents:
7240
diff
changeset
|
2633 ctx->frame_state = state; |
7233 | 2634 } |
2635 | |
2636 if (state < sw_fragment) { | |
2637 | |
2638 if (b->last - b->pos < (ssize_t) ctx->rest) { | |
2639 last = b->last; | |
2640 | |
2641 } else { | |
2642 last = b->pos + ctx->rest; | |
2643 } | |
2644 | |
2645 for (p = b->pos; p < last; p++) { | |
2646 ch = *p; | |
2647 | |
2648 #if 0 | |
2649 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
2650 "grpc header byte: %02Xd s:%d", ch, state); | |
2651 #endif | |
2652 | |
2653 /* | |
2654 * headers frame: | |
2655 * | |
2656 * +---------------+ | |
2657 * |Pad Length? (8)| | |
2658 * +-+-------------+----------------------------------------------+ | |
2659 * |E| Stream Dependency? (31) | | |
2660 * +-+-------------+----------------------------------------------+ | |
2661 * | Weight? (8) | | |
2662 * +-+-------------+----------------------------------------------+ | |
2663 * | Header Block Fragment (*) ... | |
2664 * +--------------------------------------------------------------+ | |
2665 * | Padding (*) ... | |
2666 * +--------------------------------------------------------------+ | |
2667 */ | |
2668 | |
2669 switch (state) { | |
2670 | |
2671 case sw_padding_length: | |
2672 | |
2673 ctx->padding = ch; | |
2674 | |
2675 if (ctx->flags & NGX_HTTP_V2_PRIORITY_FLAG) { | |
2676 state = sw_dependency; | |
2677 break; | |
2678 } | |
2679 | |
2680 goto fragment; | |
2681 | |
2682 case sw_dependency: | |
2683 state = sw_dependency_2; | |
2684 break; | |
2685 | |
2686 case sw_dependency_2: | |
2687 state = sw_dependency_3; | |
2688 break; | |
2689 | |
2690 case sw_dependency_3: | |
2691 state = sw_dependency_4; | |
2692 break; | |
2693 | |
2694 case sw_dependency_4: | |
2695 state = sw_weight; | |
2696 break; | |
2697 | |
2698 case sw_weight: | |
2699 goto fragment; | |
2700 | |
2701 /* suppress warning */ | |
2702 case sw_start: | |
2703 case sw_fragment: | |
2704 case sw_padding: | |
2705 break; | |
2706 } | |
2707 } | |
2708 | |
2709 ctx->rest -= p - b->pos; | |
2710 b->pos = p; | |
2711 | |
2712 ctx->frame_state = state; | |
2713 return NGX_AGAIN; | |
2714 | |
2715 fragment: | |
2716 | |
2717 p++; | |
2718 ctx->rest -= p - b->pos; | |
2719 b->pos = p; | |
2720 | |
2721 if (ctx->padding > ctx->rest) { | |
2722 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
2723 "upstream sent http2 frame with too long " | |
2724 "padding: %d in frame %uz", | |
2725 ctx->padding, ctx->rest); | |
2726 return NGX_ERROR; | |
2727 } | |
2728 | |
2729 state = sw_fragment; | |
2730 ctx->frame_state = state; | |
2731 } | |
2732 | |
2733 if (state == sw_fragment) { | |
2734 | |
2735 rc = ngx_http_grpc_parse_fragment(r, ctx, b); | |
2736 | |
2737 if (rc == NGX_AGAIN) { | |
2738 return NGX_AGAIN; | |
2739 } | |
2740 | |
2741 if (rc == NGX_ERROR) { | |
2742 return NGX_ERROR; | |
2743 } | |
2744 | |
2745 if (rc == NGX_OK) { | |
2746 return NGX_OK; | |
2747 } | |
2748 | |
2749 /* rc == NGX_DONE */ | |
2750 | |
2751 state = sw_padding; | |
2752 ctx->frame_state = state; | |
2753 } | |
2754 | |
2755 if (state == sw_padding) { | |
2756 | |
2757 if (b->last - b->pos < (ssize_t) ctx->rest) { | |
2758 | |
2759 ctx->rest -= b->last - b->pos; | |
2760 b->pos = b->last; | |
2761 | |
2762 return NGX_AGAIN; | |
2763 } | |
2764 | |
2765 b->pos += ctx->rest; | |
2766 ctx->rest = 0; | |
2767 | |
2768 ctx->state = ngx_http_grpc_st_start; | |
2769 | |
2770 if (ctx->flags & NGX_HTTP_V2_END_HEADERS_FLAG) { | |
2771 | |
2772 if (ctx->fragment_state) { | |
2773 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
2774 "upstream sent truncated http2 header"); | |
2775 return NGX_ERROR; | |
2776 } | |
2777 | |
2778 ctx->parsing_headers = 0; | |
2779 | |
2780 return NGX_HTTP_PARSE_HEADER_DONE; | |
2781 } | |
2782 | |
2783 return NGX_AGAIN; | |
2784 } | |
2785 | |
2786 /* unreachable */ | |
2787 | |
2788 return NGX_ERROR; | |
2789 } | |
2790 | |
2791 | |
2792 static ngx_int_t | |
2793 ngx_http_grpc_parse_fragment(ngx_http_request_t *r, ngx_http_grpc_ctx_t *ctx, | |
2794 ngx_buf_t *b) | |
2795 { | |
2796 u_char ch, *p, *last; | |
2797 size_t size; | |
2798 ngx_uint_t index, size_update; | |
2799 enum { | |
2800 sw_start = 0, | |
2801 sw_index, | |
2802 sw_name_length, | |
2803 sw_name_length_2, | |
2804 sw_name_length_3, | |
2805 sw_name_length_4, | |
2806 sw_name, | |
2807 sw_name_bytes, | |
2808 sw_value_length, | |
2809 sw_value_length_2, | |
2810 sw_value_length_3, | |
2811 sw_value_length_4, | |
2812 sw_value, | |
2813 sw_value_bytes | |
2814 } state; | |
2815 | |
2816 /* header block fragment */ | |
2817 | |
2818 #if 0 | |
2819 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
2820 "grpc header fragment %p:%p rest:%uz", | |
2821 b->pos, b->last, ctx->rest); | |
2822 #endif | |
2823 | |
2824 if (b->last - b->pos < (ssize_t) ctx->rest - ctx->padding) { | |
2825 last = b->last; | |
2826 | |
2827 } else { | |
2828 last = b->pos + ctx->rest - ctx->padding; | |
2829 } | |
2830 | |
2831 state = ctx->fragment_state; | |
2832 | |
2833 for (p = b->pos; p < last; p++) { | |
2834 ch = *p; | |
2835 | |
2836 #if 0 | |
2837 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
2838 "grpc header byte: %02Xd s:%d", ch, state); | |
2839 #endif | |
2840 | |
2841 switch (state) { | |
2842 | |
2843 case sw_start: | |
2844 ctx->index = 0; | |
2845 | |
2846 if ((ch & 0x80) == 0x80) { | |
2847 /* | |
2848 * indexed header: | |
2849 * | |
2850 * 0 1 2 3 4 5 6 7 | |
2851 * +---+---+---+---+---+---+---+---+ | |
2852 * | 1 | Index (7+) | | |
2853 * +---+---------------------------+ | |
2854 */ | |
2855 | |
2856 index = ch & ~0x80; | |
2857 | |
2858 if (index == 0 || index > 61) { | |
2859 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
2860 "upstream sent invalid http2 " | |
2861 "table index: %ui", index); | |
2862 return NGX_ERROR; | |
2863 } | |
2864 | |
2865 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
2866 "grpc indexed header: %ui", index); | |
2867 | |
2868 ctx->index = index; | |
2869 ctx->literal = 0; | |
2870 | |
2871 goto done; | |
2872 | |
2873 } else if ((ch & 0xc0) == 0x40) { | |
2874 /* | |
2875 * literal header with incremental indexing: | |
2876 * | |
2877 * 0 1 2 3 4 5 6 7 | |
2878 * +---+---+---+---+---+---+---+---+ | |
2879 * | 0 | 1 | Index (6+) | | |
2880 * +---+---+-----------------------+ | |
2881 * | H | Value Length (7+) | | |
2882 * +---+---------------------------+ | |
2883 * | Value String (Length octets) | | |
2884 * +-------------------------------+ | |
2885 * | |
2886 * 0 1 2 3 4 5 6 7 | |
2887 * +---+---+---+---+---+---+---+---+ | |
2888 * | 0 | 1 | 0 | | |
2889 * +---+---+-----------------------+ | |
2890 * | H | Name Length (7+) | | |
2891 * +---+---------------------------+ | |
2892 * | Name String (Length octets) | | |
2893 * +---+---------------------------+ | |
2894 * | H | Value Length (7+) | | |
2895 * +---+---------------------------+ | |
2896 * | Value String (Length octets) | | |
2897 * +-------------------------------+ | |
2898 */ | |
2899 | |
2900 index = ch & ~0xc0; | |
2901 | |
2902 if (index > 61) { | |
2903 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
2904 "upstream sent invalid http2 " | |
2905 "table index: %ui", index); | |
2906 return NGX_ERROR; | |
2907 } | |
2908 | |
2909 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
2910 "grpc literal header: %ui", index); | |
2911 | |
2912 if (index == 0) { | |
2913 state = sw_name_length; | |
2914 break; | |
2915 } | |
2916 | |
2917 ctx->index = index; | |
2918 ctx->literal = 1; | |
2919 | |
2920 state = sw_value_length; | |
2921 break; | |
2922 | |
2923 } else if ((ch & 0xe0) == 0x20) { | |
2924 /* | |
2925 * dynamic table size update: | |
2926 * | |
2927 * 0 1 2 3 4 5 6 7 | |
2928 * +---+---+---+---+---+---+---+---+ | |
2929 * | 0 | 0 | 1 | Max size (5+) | | |
2930 * +---+---------------------------+ | |
2931 */ | |
2932 | |
2933 size_update = ch & ~0xe0; | |
2934 | |
2935 if (size_update > 0) { | |
2936 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
2937 "upstream sent invalid http2 " | |
2938 "dynamic table size update: %ui", | |
2939 size_update); | |
2940 return NGX_ERROR; | |
2941 } | |
2942 | |
2943 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
2944 "grpc table size update: %ui", size_update); | |
2945 | |
2946 break; | |
2947 | |
2948 } else if ((ch & 0xf0) == 0x10) { | |
2949 /* | |
2950 * literal header field never indexed: | |
2951 * | |
2952 * 0 1 2 3 4 5 6 7 | |
2953 * +---+---+---+---+---+---+---+---+ | |
2954 * | 0 | 0 | 0 | 1 | Index (4+) | | |
2955 * +---+---+-----------------------+ | |
2956 * | H | Value Length (7+) | | |
2957 * +---+---------------------------+ | |
2958 * | Value String (Length octets) | | |
2959 * +-------------------------------+ | |
2960 * | |
2961 * 0 1 2 3 4 5 6 7 | |
2962 * +---+---+---+---+---+---+---+---+ | |
2963 * | 0 | 0 | 0 | 1 | 0 | | |
2964 * +---+---+-----------------------+ | |
2965 * | H | Name Length (7+) | | |
2966 * +---+---------------------------+ | |
2967 * | Name String (Length octets) | | |
2968 * +---+---------------------------+ | |
2969 * | H | Value Length (7+) | | |
2970 * +---+---------------------------+ | |
2971 * | Value String (Length octets) | | |
2972 * +-------------------------------+ | |
2973 */ | |
2974 | |
2975 index = ch & ~0xf0; | |
2976 | |
2977 if (index == 0x0f) { | |
2978 ctx->index = index; | |
2979 ctx->literal = 1; | |
2980 state = sw_index; | |
2981 break; | |
2982 } | |
2983 | |
2984 if (index == 0) { | |
2985 state = sw_name_length; | |
2986 break; | |
2987 } | |
2988 | |
2989 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
2990 "grpc literal header never indexed: %ui", | |
2991 index); | |
2992 | |
2993 ctx->index = index; | |
2994 ctx->literal = 1; | |
2995 | |
2996 state = sw_value_length; | |
2997 break; | |
2998 | |
2999 } else if ((ch & 0xf0) == 0x00) { | |
3000 /* | |
3001 * literal header field without indexing: | |
3002 * | |
3003 * 0 1 2 3 4 5 6 7 | |
3004 * +---+---+---+---+---+---+---+---+ | |
3005 * | 0 | 0 | 0 | 0 | Index (4+) | | |
3006 * +---+---+-----------------------+ | |
3007 * | H | Value Length (7+) | | |
3008 * +---+---------------------------+ | |
3009 * | Value String (Length octets) | | |
3010 * +-------------------------------+ | |
3011 * | |
3012 * 0 1 2 3 4 5 6 7 | |
3013 * +---+---+---+---+---+---+---+---+ | |
3014 * | 0 | 0 | 0 | 0 | 0 | | |
3015 * +---+---+-----------------------+ | |
3016 * | H | Name Length (7+) | | |
3017 * +---+---------------------------+ | |
3018 * | Name String (Length octets) | | |
3019 * +---+---------------------------+ | |
3020 * | H | Value Length (7+) | | |
3021 * +---+---------------------------+ | |
3022 * | Value String (Length octets) | | |
3023 * +-------------------------------+ | |
3024 */ | |
3025 | |
3026 index = ch & ~0xf0; | |
3027 | |
3028 if (index == 0x0f) { | |
3029 ctx->index = index; | |
3030 ctx->literal = 1; | |
3031 state = sw_index; | |
3032 break; | |
3033 } | |
3034 | |
3035 if (index == 0) { | |
3036 state = sw_name_length; | |
3037 break; | |
3038 } | |
3039 | |
3040 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
3041 "grpc literal header without indexing: %ui", | |
3042 index); | |
3043 | |
3044 ctx->index = index; | |
3045 ctx->literal = 1; | |
3046 | |
3047 state = sw_value_length; | |
3048 break; | |
3049 } | |
3050 | |
3051 /* not reached */ | |
3052 | |
3053 return NGX_ERROR; | |
3054 | |
3055 case sw_index: | |
3056 ctx->index = ctx->index + (ch & ~0x80); | |
3057 | |
3058 if (ch & 0x80) { | |
3059 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
3060 "upstream sent http2 table index " | |
3061 "with continuation flag"); | |
3062 return NGX_ERROR; | |
3063 } | |
3064 | |
3065 if (ctx->index > 61) { | |
3066 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
3067 "upstream sent invalid http2 " | |
3068 "table index: %ui", ctx->index); | |
3069 return NGX_ERROR; | |
3070 } | |
3071 | |
3072 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
3073 "grpc header index: %ui", ctx->index); | |
3074 | |
3075 state = sw_value_length; | |
3076 break; | |
3077 | |
3078 case sw_name_length: | |
3079 ctx->field_huffman = ch & 0x80 ? 1 : 0; | |
3080 ctx->field_length = ch & ~0x80; | |
3081 | |
3082 if (ctx->field_length == 0x7f) { | |
3083 state = sw_name_length_2; | |
3084 break; | |
3085 } | |
3086 | |
3087 if (ctx->field_length == 0) { | |
3088 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
3089 "upstream sent zero http2 " | |
3090 "header name length"); | |
3091 return NGX_ERROR; | |
3092 } | |
3093 | |
3094 state = sw_name; | |
3095 break; | |
3096 | |
3097 case sw_name_length_2: | |
3098 ctx->field_length += ch & ~0x80; | |
3099 | |
3100 if (ch & 0x80) { | |
3101 state = sw_name_length_3; | |
3102 break; | |
3103 } | |
3104 | |
3105 state = sw_name; | |
3106 break; | |
3107 | |
3108 case sw_name_length_3: | |
3109 ctx->field_length += (ch & ~0x80) << 7; | |
3110 | |
3111 if (ch & 0x80) { | |
3112 state = sw_name_length_4; | |
3113 break; | |
3114 } | |
3115 | |
3116 state = sw_name; | |
3117 break; | |
3118 | |
3119 case sw_name_length_4: | |
3120 ctx->field_length += (ch & ~0x80) << 14; | |
3121 | |
3122 if (ch & 0x80) { | |
3123 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
3124 "upstream sent too large http2 " | |
3125 "header name length"); | |
3126 return NGX_ERROR; | |
3127 } | |
3128 | |
3129 state = sw_name; | |
3130 break; | |
3131 | |
3132 case sw_name: | |
3133 ctx->name.len = ctx->field_huffman ? | |
3134 ctx->field_length * 8 / 5 : ctx->field_length; | |
3135 | |
3136 ctx->name.data = ngx_pnalloc(r->pool, ctx->name.len + 1); | |
3137 if (ctx->name.data == NULL) { | |
3138 return NGX_ERROR; | |
3139 } | |
3140 | |
3141 ctx->field_end = ctx->name.data; | |
3142 ctx->field_rest = ctx->field_length; | |
3143 ctx->field_state = 0; | |
3144 | |
3145 state = sw_name_bytes; | |
3146 | |
3147 /* fall through */ | |
3148 | |
3149 case sw_name_bytes: | |
3150 | |
3151 ngx_log_debug4(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
3152 "grpc name: len:%uz h:%d last:%uz, rest:%uz", | |
3153 ctx->field_length, | |
3154 ctx->field_huffman, | |
3155 last - p, | |
3156 ctx->rest - (p - b->pos)); | |
3157 | |
3158 size = ngx_min(last - p, (ssize_t) ctx->field_rest); | |
3159 ctx->field_rest -= size; | |
3160 | |
3161 if (ctx->field_huffman) { | |
3162 if (ngx_http_v2_huff_decode(&ctx->field_state, p, size, | |
3163 &ctx->field_end, | |
3164 ctx->field_rest == 0, | |
3165 r->connection->log) | |
3166 != NGX_OK) | |
3167 { | |
3168 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
3169 "upstream sent invalid encoded header"); | |
3170 return NGX_ERROR; | |
3171 } | |
3172 | |
3173 ctx->name.len = ctx->field_end - ctx->name.data; | |
3174 ctx->name.data[ctx->name.len] = '\0'; | |
3175 | |
3176 } else { | |
7240
413189f03c8d
gRPC: fixed parsing response headers split on CONTINUATION frames.
Sergey Kandaurov <pluknet@nginx.com>
parents:
7235
diff
changeset
|
3177 ctx->field_end = ngx_cpymem(ctx->field_end, p, size); |
7233 | 3178 ctx->name.data[ctx->name.len] = '\0'; |
3179 } | |
3180 | |
3181 p += size - 1; | |
3182 | |
3183 if (ctx->field_rest == 0) { | |
3184 state = sw_value_length; | |
3185 } | |
3186 | |
3187 break; | |
3188 | |
3189 case sw_value_length: | |
3190 ctx->field_huffman = ch & 0x80 ? 1 : 0; | |
3191 ctx->field_length = ch & ~0x80; | |
3192 | |
3193 if (ctx->field_length == 0x7f) { | |
3194 state = sw_value_length_2; | |
3195 break; | |
3196 } | |
3197 | |
3198 if (ctx->field_length == 0) { | |
3199 ngx_str_set(&ctx->value, ""); | |
3200 goto done; | |
3201 } | |
3202 | |
3203 state = sw_value; | |
3204 break; | |
3205 | |
3206 case sw_value_length_2: | |
3207 ctx->field_length += ch & ~0x80; | |
3208 | |
3209 if (ch & 0x80) { | |
3210 state = sw_value_length_3; | |
3211 break; | |
3212 } | |
3213 | |
3214 state = sw_value; | |
3215 break; | |
3216 | |
3217 case sw_value_length_3: | |
3218 ctx->field_length += (ch & ~0x80) << 7; | |
3219 | |
3220 if (ch & 0x80) { | |
3221 state = sw_value_length_4; | |
3222 break; | |
3223 } | |
3224 | |
3225 state = sw_value; | |
3226 break; | |
3227 | |
3228 case sw_value_length_4: | |
3229 ctx->field_length += (ch & ~0x80) << 14; | |
3230 | |
3231 if (ch & 0x80) { | |
3232 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
3233 "upstream sent too large http2 " | |
3234 "header value length"); | |
3235 return NGX_ERROR; | |
3236 } | |
3237 | |
3238 state = sw_value; | |
3239 break; | |
3240 | |
3241 case sw_value: | |
3242 ctx->value.len = ctx->field_huffman ? | |
3243 ctx->field_length * 8 / 5 : ctx->field_length; | |
3244 | |
3245 ctx->value.data = ngx_pnalloc(r->pool, ctx->value.len + 1); | |
3246 if (ctx->value.data == NULL) { | |
3247 return NGX_ERROR; | |
3248 } | |
3249 | |
3250 ctx->field_end = ctx->value.data; | |
3251 ctx->field_rest = ctx->field_length; | |
3252 ctx->field_state = 0; | |
3253 | |
3254 state = sw_value_bytes; | |
3255 | |
3256 /* fall through */ | |
3257 | |
3258 case sw_value_bytes: | |
3259 | |
3260 ngx_log_debug4(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
3261 "grpc value: len:%uz h:%d last:%uz, rest:%uz", | |
3262 ctx->field_length, | |
3263 ctx->field_huffman, | |
3264 last - p, | |
3265 ctx->rest - (p - b->pos)); | |
3266 | |
3267 size = ngx_min(last - p, (ssize_t) ctx->field_rest); | |
3268 ctx->field_rest -= size; | |
3269 | |
3270 if (ctx->field_huffman) { | |
3271 if (ngx_http_v2_huff_decode(&ctx->field_state, p, size, | |
3272 &ctx->field_end, | |
3273 ctx->field_rest == 0, | |
3274 r->connection->log) | |
3275 != NGX_OK) | |
3276 { | |
3277 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
3278 "upstream sent invalid encoded header"); | |
3279 return NGX_ERROR; | |
3280 } | |
3281 | |
3282 ctx->value.len = ctx->field_end - ctx->value.data; | |
3283 ctx->value.data[ctx->value.len] = '\0'; | |
3284 | |
3285 } else { | |
7240
413189f03c8d
gRPC: fixed parsing response headers split on CONTINUATION frames.
Sergey Kandaurov <pluknet@nginx.com>
parents:
7235
diff
changeset
|
3286 ctx->field_end = ngx_cpymem(ctx->field_end, p, size); |
7233 | 3287 ctx->value.data[ctx->value.len] = '\0'; |
3288 } | |
3289 | |
3290 p += size - 1; | |
3291 | |
3292 if (ctx->field_rest == 0) { | |
3293 goto done; | |
3294 } | |
3295 | |
3296 break; | |
3297 } | |
3298 | |
3299 continue; | |
3300 | |
3301 done: | |
3302 | |
3303 p++; | |
3304 ctx->rest -= p - b->pos; | |
3305 ctx->fragment_state = sw_start; | |
3306 b->pos = p; | |
3307 | |
3308 if (ctx->index) { | |
3309 ctx->name = *ngx_http_v2_get_static_name(ctx->index); | |
3310 } | |
3311 | |
3312 if (ctx->index && !ctx->literal) { | |
3313 ctx->value = *ngx_http_v2_get_static_value(ctx->index); | |
3314 } | |
3315 | |
3316 if (!ctx->index) { | |
3317 if (ngx_http_grpc_validate_header_name(r, &ctx->name) != NGX_OK) { | |
3318 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
3319 "upstream sent invalid header: \"%V: %V\"", | |
3320 &ctx->name, &ctx->value); | |
3321 return NGX_ERROR; | |
3322 } | |
3323 } | |
3324 | |
3325 if (!ctx->index || ctx->literal) { | |
3326 if (ngx_http_grpc_validate_header_value(r, &ctx->value) != NGX_OK) { | |
3327 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
3328 "upstream sent invalid header: \"%V: %V\"", | |
3329 &ctx->name, &ctx->value); | |
3330 return NGX_ERROR; | |
3331 } | |
3332 } | |
3333 | |
3334 return NGX_OK; | |
3335 } | |
3336 | |
3337 ctx->rest -= p - b->pos; | |
3338 ctx->fragment_state = state; | |
3339 b->pos = p; | |
3340 | |
3341 if (ctx->rest > ctx->padding) { | |
3342 return NGX_AGAIN; | |
3343 } | |
3344 | |
3345 return NGX_DONE; | |
3346 } | |
3347 | |
3348 | |
3349 static ngx_int_t | |
3350 ngx_http_grpc_validate_header_name(ngx_http_request_t *r, ngx_str_t *s) | |
3351 { | |
3352 u_char ch; | |
3353 ngx_uint_t i; | |
3354 | |
3355 for (i = 0; i < s->len; i++) { | |
3356 ch = s->data[i]; | |
3357 | |
3358 if (ch == ':' && i > 0) { | |
3359 return NGX_ERROR; | |
3360 } | |
3361 | |
3362 if (ch >= 'A' && ch <= 'Z') { | |
3363 return NGX_ERROR; | |
3364 } | |
3365 | |
3366 if (ch == '\0' || ch == CR || ch == LF) { | |
3367 return NGX_ERROR; | |
3368 } | |
3369 } | |
3370 | |
3371 return NGX_OK; | |
3372 } | |
3373 | |
3374 | |
3375 static ngx_int_t | |
3376 ngx_http_grpc_validate_header_value(ngx_http_request_t *r, ngx_str_t *s) | |
3377 { | |
3378 u_char ch; | |
3379 ngx_uint_t i; | |
3380 | |
3381 for (i = 0; i < s->len; i++) { | |
3382 ch = s->data[i]; | |
3383 | |
3384 if (ch == '\0' || ch == CR || ch == LF) { | |
3385 return NGX_ERROR; | |
3386 } | |
3387 } | |
3388 | |
3389 return NGX_OK; | |
3390 } | |
3391 | |
3392 | |
3393 static ngx_int_t | |
3394 ngx_http_grpc_parse_rst_stream(ngx_http_request_t *r, ngx_http_grpc_ctx_t *ctx, | |
3395 ngx_buf_t *b) | |
3396 { | |
3397 u_char ch, *p, *last; | |
3398 enum { | |
3399 sw_start = 0, | |
3400 sw_error_2, | |
3401 sw_error_3, | |
3402 sw_error_4 | |
3403 } state; | |
3404 | |
3405 if (b->last - b->pos < (ssize_t) ctx->rest) { | |
3406 last = b->last; | |
3407 | |
3408 } else { | |
3409 last = b->pos + ctx->rest; | |
3410 } | |
3411 | |
3412 state = ctx->frame_state; | |
3413 | |
3414 if (state == sw_start) { | |
3415 if (ctx->rest != 4) { | |
3416 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
3417 "upstream sent rst stream frame " | |
3418 "with invalid length: %uz", | |
3419 ctx->rest); | |
3420 return NGX_ERROR; | |
3421 } | |
3422 } | |
3423 | |
3424 for (p = b->pos; p < last; p++) { | |
3425 ch = *p; | |
3426 | |
3427 #if 0 | |
3428 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
3429 "grpc rst byte: %02Xd s:%d", ch, state); | |
3430 #endif | |
3431 | |
3432 switch (state) { | |
3433 | |
3434 case sw_start: | |
7249
070c972336c4
gRPC: fixed possible sign extension of error and setting_value.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7242
diff
changeset
|
3435 ctx->error = (ngx_uint_t) ch << 24; |
7233 | 3436 state = sw_error_2; |
3437 break; | |
3438 | |
3439 case sw_error_2: | |
3440 ctx->error |= ch << 16; | |
3441 state = sw_error_3; | |
3442 break; | |
3443 | |
3444 case sw_error_3: | |
3445 ctx->error |= ch << 8; | |
3446 state = sw_error_4; | |
3447 break; | |
3448 | |
3449 case sw_error_4: | |
3450 ctx->error |= ch; | |
3451 state = sw_start; | |
3452 | |
3453 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
3454 "grpc error: %ui", ctx->error); | |
3455 | |
3456 break; | |
3457 } | |
3458 } | |
3459 | |
3460 ctx->rest -= p - b->pos; | |
3461 ctx->frame_state = state; | |
3462 b->pos = p; | |
3463 | |
3464 if (ctx->rest > 0) { | |
3465 return NGX_AGAIN; | |
3466 } | |
3467 | |
3468 return NGX_OK; | |
3469 } | |
3470 | |
3471 | |
3472 static ngx_int_t | |
3473 ngx_http_grpc_parse_goaway(ngx_http_request_t *r, ngx_http_grpc_ctx_t *ctx, | |
3474 ngx_buf_t *b) | |
3475 { | |
3476 u_char ch, *p, *last; | |
3477 enum { | |
3478 sw_start = 0, | |
3479 sw_last_stream_id_2, | |
3480 sw_last_stream_id_3, | |
3481 sw_last_stream_id_4, | |
3482 sw_error, | |
3483 sw_error_2, | |
3484 sw_error_3, | |
3485 sw_error_4, | |
3486 sw_debug | |
3487 } state; | |
3488 | |
3489 if (b->last - b->pos < (ssize_t) ctx->rest) { | |
3490 last = b->last; | |
3491 | |
3492 } else { | |
3493 last = b->pos + ctx->rest; | |
3494 } | |
3495 | |
3496 state = ctx->frame_state; | |
3497 | |
3498 if (state == sw_start) { | |
3499 | |
3500 if (ctx->stream_id) { | |
3501 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
3502 "upstream sent goaway frame " | |
3503 "with non-zero stream id: %ui", | |
3504 ctx->stream_id); | |
3505 return NGX_ERROR; | |
3506 } | |
3507 | |
3508 if (ctx->rest < 8) { | |
3509 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
3510 "upstream sent goaway frame " | |
3511 "with invalid length: %uz", | |
3512 ctx->rest); | |
3513 return NGX_ERROR; | |
3514 } | |
3515 } | |
3516 | |
3517 for (p = b->pos; p < last; p++) { | |
3518 ch = *p; | |
3519 | |
3520 #if 0 | |
3521 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
3522 "grpc goaway byte: %02Xd s:%d", ch, state); | |
3523 #endif | |
3524 | |
3525 switch (state) { | |
3526 | |
3527 case sw_start: | |
3528 ctx->stream_id = (ch & 0x7f) << 24; | |
3529 state = sw_last_stream_id_2; | |
3530 break; | |
3531 | |
3532 case sw_last_stream_id_2: | |
3533 ctx->stream_id |= ch << 16; | |
3534 state = sw_last_stream_id_3; | |
3535 break; | |
3536 | |
3537 case sw_last_stream_id_3: | |
3538 ctx->stream_id |= ch << 8; | |
3539 state = sw_last_stream_id_4; | |
3540 break; | |
3541 | |
3542 case sw_last_stream_id_4: | |
3543 ctx->stream_id |= ch; | |
3544 state = sw_error; | |
3545 break; | |
3546 | |
3547 case sw_error: | |
7249
070c972336c4
gRPC: fixed possible sign extension of error and setting_value.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7242
diff
changeset
|
3548 ctx->error = (ngx_uint_t) ch << 24; |
7233 | 3549 state = sw_error_2; |
3550 break; | |
3551 | |
3552 case sw_error_2: | |
3553 ctx->error |= ch << 16; | |
3554 state = sw_error_3; | |
3555 break; | |
3556 | |
3557 case sw_error_3: | |
3558 ctx->error |= ch << 8; | |
3559 state = sw_error_4; | |
3560 break; | |
3561 | |
3562 case sw_error_4: | |
3563 ctx->error |= ch; | |
3564 state = sw_debug; | |
3565 break; | |
3566 | |
3567 case sw_debug: | |
3568 break; | |
3569 } | |
3570 } | |
3571 | |
3572 ctx->rest -= p - b->pos; | |
3573 ctx->frame_state = state; | |
3574 b->pos = p; | |
3575 | |
3576 if (ctx->rest > 0) { | |
3577 return NGX_AGAIN; | |
3578 } | |
3579 | |
3580 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
3581 "grpc goaway: %ui, stream %ui", | |
3582 ctx->error, ctx->stream_id); | |
3583 | |
3584 ctx->state = ngx_http_grpc_st_start; | |
3585 | |
3586 return NGX_OK; | |
3587 } | |
3588 | |
3589 | |
3590 static ngx_int_t | |
3591 ngx_http_grpc_parse_window_update(ngx_http_request_t *r, | |
3592 ngx_http_grpc_ctx_t *ctx, ngx_buf_t *b) | |
3593 { | |
3594 u_char ch, *p, *last; | |
3595 enum { | |
3596 sw_start = 0, | |
3597 sw_size_2, | |
3598 sw_size_3, | |
3599 sw_size_4 | |
3600 } state; | |
3601 | |
3602 if (b->last - b->pos < (ssize_t) ctx->rest) { | |
3603 last = b->last; | |
3604 | |
3605 } else { | |
3606 last = b->pos + ctx->rest; | |
3607 } | |
3608 | |
3609 state = ctx->frame_state; | |
3610 | |
3611 if (state == sw_start) { | |
3612 if (ctx->rest != 4) { | |
3613 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
3614 "upstream sent window update frame " | |
3615 "with invalid length: %uz", | |
3616 ctx->rest); | |
3617 return NGX_ERROR; | |
3618 } | |
3619 } | |
3620 | |
3621 for (p = b->pos; p < last; p++) { | |
3622 ch = *p; | |
3623 | |
3624 #if 0 | |
3625 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
3626 "grpc window update byte: %02Xd s:%d", ch, state); | |
3627 #endif | |
3628 | |
3629 switch (state) { | |
3630 | |
3631 case sw_start: | |
3632 ctx->window_update = (ch & 0x7f) << 24; | |
3633 state = sw_size_2; | |
3634 break; | |
3635 | |
3636 case sw_size_2: | |
3637 ctx->window_update |= ch << 16; | |
3638 state = sw_size_3; | |
3639 break; | |
3640 | |
3641 case sw_size_3: | |
3642 ctx->window_update |= ch << 8; | |
3643 state = sw_size_4; | |
3644 break; | |
3645 | |
3646 case sw_size_4: | |
3647 ctx->window_update |= ch; | |
3648 state = sw_start; | |
3649 break; | |
3650 } | |
3651 } | |
3652 | |
3653 ctx->rest -= p - b->pos; | |
3654 ctx->frame_state = state; | |
3655 b->pos = p; | |
3656 | |
3657 if (ctx->rest > 0) { | |
3658 return NGX_AGAIN; | |
3659 } | |
3660 | |
3661 ctx->state = ngx_http_grpc_st_start; | |
3662 | |
3663 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
3664 "grpc window update: %ui", ctx->window_update); | |
3665 | |
3666 if (ctx->stream_id) { | |
3667 | |
3668 if (ctx->window_update > (size_t) NGX_HTTP_V2_MAX_WINDOW | |
3669 - ctx->send_window) | |
3670 { | |
3671 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
3672 "upstream sent too large window update"); | |
3673 return NGX_ERROR; | |
3674 } | |
3675 | |
3676 ctx->send_window += ctx->window_update; | |
3677 | |
3678 } else { | |
3679 | |
3680 if (ctx->window_update > NGX_HTTP_V2_MAX_WINDOW | |
3681 - ctx->connection->send_window) | |
3682 { | |
3683 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
3684 "upstream sent too large window update"); | |
3685 return NGX_ERROR; | |
3686 } | |
3687 | |
3688 ctx->connection->send_window += ctx->window_update; | |
3689 } | |
3690 | |
3691 return NGX_OK; | |
3692 } | |
3693 | |
3694 | |
3695 static ngx_int_t | |
3696 ngx_http_grpc_parse_settings(ngx_http_request_t *r, ngx_http_grpc_ctx_t *ctx, | |
3697 ngx_buf_t *b) | |
3698 { | |
3699 u_char ch, *p, *last; | |
3700 ssize_t window_update; | |
3701 enum { | |
3702 sw_start = 0, | |
3703 sw_id, | |
3704 sw_id_2, | |
3705 sw_value, | |
3706 sw_value_2, | |
3707 sw_value_3, | |
3708 sw_value_4 | |
3709 } state; | |
3710 | |
3711 if (b->last - b->pos < (ssize_t) ctx->rest) { | |
3712 last = b->last; | |
3713 | |
3714 } else { | |
3715 last = b->pos + ctx->rest; | |
3716 } | |
3717 | |
3718 state = ctx->frame_state; | |
3719 | |
3720 if (state == sw_start) { | |
3721 | |
3722 if (ctx->stream_id) { | |
3723 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
3724 "upstream sent settings frame " | |
3725 "with non-zero stream id: %ui", | |
3726 ctx->stream_id); | |
3727 return NGX_ERROR; | |
3728 } | |
3729 | |
3730 if (ctx->flags & NGX_HTTP_V2_ACK_FLAG) { | |
3731 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
3732 "grpc settings ack"); | |
3733 | |
3734 if (ctx->rest != 0) { | |
3735 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
3736 "upstream sent settings frame " | |
3737 "with ack flag and non-zero length: %uz", | |
3738 ctx->rest); | |
3739 return NGX_ERROR; | |
3740 } | |
3741 | |
3742 ctx->state = ngx_http_grpc_st_start; | |
3743 | |
3744 return NGX_OK; | |
3745 } | |
3746 | |
3747 if (ctx->rest % 6 != 0) { | |
3748 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
3749 "upstream sent settings frame " | |
3750 "with invalid length: %uz", | |
3751 ctx->rest); | |
3752 return NGX_ERROR; | |
3753 } | |
7379
57463f4e2fcd
gRPC: limited allocations due to ping and settings frames.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7371
diff
changeset
|
3754 |
57463f4e2fcd
gRPC: limited allocations due to ping and settings frames.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7371
diff
changeset
|
3755 if (ctx->free == NULL && ctx->settings++ > 1000) { |
57463f4e2fcd
gRPC: limited allocations due to ping and settings frames.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7371
diff
changeset
|
3756 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, |
57463f4e2fcd
gRPC: limited allocations due to ping and settings frames.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7371
diff
changeset
|
3757 "upstream sent too many settings frames"); |
57463f4e2fcd
gRPC: limited allocations due to ping and settings frames.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7371
diff
changeset
|
3758 return NGX_ERROR; |
57463f4e2fcd
gRPC: limited allocations due to ping and settings frames.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7371
diff
changeset
|
3759 } |
7233 | 3760 } |
3761 | |
3762 for (p = b->pos; p < last; p++) { | |
3763 ch = *p; | |
3764 | |
3765 #if 0 | |
3766 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
3767 "grpc settings byte: %02Xd s:%d", ch, state); | |
3768 #endif | |
3769 | |
3770 switch (state) { | |
3771 | |
3772 case sw_start: | |
3773 case sw_id: | |
3774 ctx->setting_id = ch << 8; | |
3775 state = sw_id_2; | |
3776 break; | |
3777 | |
3778 case sw_id_2: | |
3779 ctx->setting_id |= ch; | |
3780 state = sw_value; | |
3781 break; | |
3782 | |
3783 case sw_value: | |
7249
070c972336c4
gRPC: fixed possible sign extension of error and setting_value.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7242
diff
changeset
|
3784 ctx->setting_value = (ngx_uint_t) ch << 24; |
7233 | 3785 state = sw_value_2; |
3786 break; | |
3787 | |
3788 case sw_value_2: | |
3789 ctx->setting_value |= ch << 16; | |
3790 state = sw_value_3; | |
3791 break; | |
3792 | |
3793 case sw_value_3: | |
3794 ctx->setting_value |= ch << 8; | |
3795 state = sw_value_4; | |
3796 break; | |
3797 | |
3798 case sw_value_4: | |
3799 ctx->setting_value |= ch; | |
3800 state = sw_id; | |
3801 | |
3802 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
3803 "grpc setting: %ui %ui", | |
3804 ctx->setting_id, ctx->setting_value); | |
3805 | |
3806 /* | |
3807 * The following settings are defined by the protocol: | |
3808 * | |
3809 * SETTINGS_HEADER_TABLE_SIZE, SETTINGS_ENABLE_PUSH, | |
3810 * SETTINGS_MAX_CONCURRENT_STREAMS, SETTINGS_INITIAL_WINDOW_SIZE, | |
3811 * SETTINGS_MAX_FRAME_SIZE, SETTINGS_MAX_HEADER_LIST_SIZE | |
3812 * | |
3813 * Only SETTINGS_INITIAL_WINDOW_SIZE seems to be needed in | |
3814 * a simple client. | |
3815 */ | |
3816 | |
3817 if (ctx->setting_id == 0x04) { | |
3818 /* SETTINGS_INITIAL_WINDOW_SIZE */ | |
3819 | |
3820 if (ctx->setting_value > NGX_HTTP_V2_MAX_WINDOW) { | |
3821 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
3822 "upstream sent settings frame " | |
3823 "with too large initial window size: %ui", | |
3824 ctx->setting_value); | |
3825 return NGX_ERROR; | |
3826 } | |
3827 | |
3828 window_update = ctx->setting_value | |
3829 - ctx->connection->init_window; | |
3830 ctx->connection->init_window = ctx->setting_value; | |
3831 | |
3832 if (ctx->send_window > 0 | |
3833 && window_update > (ssize_t) NGX_HTTP_V2_MAX_WINDOW | |
3834 - ctx->send_window) | |
3835 { | |
3836 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
3837 "upstream sent settings frame " | |
3838 "with too large initial window size: %ui", | |
3839 ctx->setting_value); | |
3840 return NGX_ERROR; | |
3841 } | |
3842 | |
3843 ctx->send_window += window_update; | |
3844 } | |
3845 | |
3846 break; | |
3847 } | |
3848 } | |
3849 | |
3850 ctx->rest -= p - b->pos; | |
3851 ctx->frame_state = state; | |
3852 b->pos = p; | |
3853 | |
3854 if (ctx->rest > 0) { | |
3855 return NGX_AGAIN; | |
3856 } | |
3857 | |
3858 ctx->state = ngx_http_grpc_st_start; | |
3859 | |
3860 return ngx_http_grpc_send_settings_ack(r, ctx); | |
3861 } | |
3862 | |
3863 | |
3864 static ngx_int_t | |
3865 ngx_http_grpc_parse_ping(ngx_http_request_t *r, | |
3866 ngx_http_grpc_ctx_t *ctx, ngx_buf_t *b) | |
3867 { | |
3868 u_char ch, *p, *last; | |
3869 enum { | |
3870 sw_start = 0, | |
3871 sw_data_2, | |
3872 sw_data_3, | |
3873 sw_data_4, | |
3874 sw_data_5, | |
3875 sw_data_6, | |
3876 sw_data_7, | |
3877 sw_data_8 | |
3878 } state; | |
3879 | |
3880 if (b->last - b->pos < (ssize_t) ctx->rest) { | |
3881 last = b->last; | |
3882 | |
3883 } else { | |
3884 last = b->pos + ctx->rest; | |
3885 } | |
3886 | |
3887 state = ctx->frame_state; | |
3888 | |
3889 if (state == sw_start) { | |
3890 | |
3891 if (ctx->stream_id) { | |
3892 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
3893 "upstream sent ping frame " | |
3894 "with non-zero stream id: %ui", | |
3895 ctx->stream_id); | |
3896 return NGX_ERROR; | |
3897 } | |
3898 | |
3899 if (ctx->rest != 8) { | |
3900 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
3901 "upstream sent ping frame " | |
3902 "with invalid length: %uz", | |
3903 ctx->rest); | |
3904 return NGX_ERROR; | |
3905 } | |
3906 | |
3907 if (ctx->flags & NGX_HTTP_V2_ACK_FLAG) { | |
3908 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
3909 "upstream sent ping frame with ack flag"); | |
3910 return NGX_ERROR; | |
3911 } | |
7379
57463f4e2fcd
gRPC: limited allocations due to ping and settings frames.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7371
diff
changeset
|
3912 |
57463f4e2fcd
gRPC: limited allocations due to ping and settings frames.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7371
diff
changeset
|
3913 if (ctx->free == NULL && ctx->pings++ > 1000) { |
57463f4e2fcd
gRPC: limited allocations due to ping and settings frames.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7371
diff
changeset
|
3914 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, |
57463f4e2fcd
gRPC: limited allocations due to ping and settings frames.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7371
diff
changeset
|
3915 "upstream sent too many ping frames"); |
57463f4e2fcd
gRPC: limited allocations due to ping and settings frames.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7371
diff
changeset
|
3916 return NGX_ERROR; |
57463f4e2fcd
gRPC: limited allocations due to ping and settings frames.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7371
diff
changeset
|
3917 } |
7233 | 3918 } |
3919 | |
3920 for (p = b->pos; p < last; p++) { | |
3921 ch = *p; | |
3922 | |
3923 #if 0 | |
3924 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
3925 "grpc ping byte: %02Xd s:%d", ch, state); | |
3926 #endif | |
3927 | |
3928 if (state < sw_data_8) { | |
3929 ctx->ping_data[state] = ch; | |
3930 state++; | |
3931 | |
3932 } else { | |
3933 ctx->ping_data[7] = ch; | |
3934 state = sw_start; | |
3935 | |
3936 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
3937 "grpc ping"); | |
3938 } | |
3939 } | |
3940 | |
3941 ctx->rest -= p - b->pos; | |
3942 ctx->frame_state = state; | |
3943 b->pos = p; | |
3944 | |
3945 if (ctx->rest > 0) { | |
3946 return NGX_AGAIN; | |
3947 } | |
3948 | |
3949 ctx->state = ngx_http_grpc_st_start; | |
3950 | |
3951 return ngx_http_grpc_send_ping_ack(r, ctx); | |
3952 } | |
3953 | |
3954 | |
3955 static ngx_int_t | |
3956 ngx_http_grpc_send_settings_ack(ngx_http_request_t *r, ngx_http_grpc_ctx_t *ctx) | |
3957 { | |
3958 ngx_chain_t *cl, **ll; | |
3959 ngx_http_grpc_frame_t *f; | |
3960 | |
3961 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
3962 "grpc send settings ack"); | |
3963 | |
3964 for (cl = ctx->out, ll = &ctx->out; cl; cl = cl->next) { | |
3965 ll = &cl->next; | |
3966 } | |
3967 | |
3968 cl = ngx_http_grpc_get_buf(r, ctx); | |
3969 if (cl == NULL) { | |
3970 return NGX_ERROR; | |
3971 } | |
3972 | |
3973 f = (ngx_http_grpc_frame_t *) cl->buf->last; | |
3974 cl->buf->last += sizeof(ngx_http_grpc_frame_t); | |
3975 | |
3976 f->length_0 = 0; | |
3977 f->length_1 = 0; | |
3978 f->length_2 = 0; | |
3979 f->type = NGX_HTTP_V2_SETTINGS_FRAME; | |
3980 f->flags = NGX_HTTP_V2_ACK_FLAG; | |
3981 f->stream_id_0 = 0; | |
3982 f->stream_id_1 = 0; | |
3983 f->stream_id_2 = 0; | |
3984 f->stream_id_3 = 0; | |
3985 | |
3986 *ll = cl; | |
3987 | |
3988 return NGX_OK; | |
3989 } | |
3990 | |
3991 | |
3992 static ngx_int_t | |
3993 ngx_http_grpc_send_ping_ack(ngx_http_request_t *r, ngx_http_grpc_ctx_t *ctx) | |
3994 { | |
3995 ngx_chain_t *cl, **ll; | |
3996 ngx_http_grpc_frame_t *f; | |
3997 | |
3998 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
3999 "grpc send ping ack"); | |
4000 | |
4001 for (cl = ctx->out, ll = &ctx->out; cl; cl = cl->next) { | |
4002 ll = &cl->next; | |
4003 } | |
4004 | |
4005 cl = ngx_http_grpc_get_buf(r, ctx); | |
4006 if (cl == NULL) { | |
4007 return NGX_ERROR; | |
4008 } | |
4009 | |
4010 f = (ngx_http_grpc_frame_t *) cl->buf->last; | |
4011 cl->buf->last += sizeof(ngx_http_grpc_frame_t); | |
4012 | |
4013 f->length_0 = 0; | |
4014 f->length_1 = 0; | |
4015 f->length_2 = 8; | |
4016 f->type = NGX_HTTP_V2_PING_FRAME; | |
4017 f->flags = NGX_HTTP_V2_ACK_FLAG; | |
4018 f->stream_id_0 = 0; | |
4019 f->stream_id_1 = 0; | |
4020 f->stream_id_2 = 0; | |
4021 f->stream_id_3 = 0; | |
4022 | |
4023 cl->buf->last = ngx_copy(cl->buf->last, ctx->ping_data, 8); | |
4024 | |
4025 *ll = cl; | |
4026 | |
4027 return NGX_OK; | |
4028 } | |
4029 | |
4030 | |
4031 static ngx_int_t | |
4032 ngx_http_grpc_send_window_update(ngx_http_request_t *r, | |
4033 ngx_http_grpc_ctx_t *ctx) | |
4034 { | |
4035 size_t n; | |
4036 ngx_chain_t *cl, **ll; | |
4037 ngx_http_grpc_frame_t *f; | |
4038 | |
4039 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
4040 "grpc send window update: %uz %uz", | |
4041 ctx->connection->recv_window, ctx->recv_window); | |
4042 | |
4043 for (cl = ctx->out, ll = &ctx->out; cl; cl = cl->next) { | |
4044 ll = &cl->next; | |
4045 } | |
4046 | |
4047 cl = ngx_http_grpc_get_buf(r, ctx); | |
4048 if (cl == NULL) { | |
4049 return NGX_ERROR; | |
4050 } | |
4051 | |
4052 f = (ngx_http_grpc_frame_t *) cl->buf->last; | |
4053 cl->buf->last += sizeof(ngx_http_grpc_frame_t); | |
4054 | |
4055 f->length_0 = 0; | |
4056 f->length_1 = 0; | |
4057 f->length_2 = 4; | |
4058 f->type = NGX_HTTP_V2_WINDOW_UPDATE_FRAME; | |
4059 f->flags = 0; | |
4060 f->stream_id_0 = 0; | |
4061 f->stream_id_1 = 0; | |
4062 f->stream_id_2 = 0; | |
4063 f->stream_id_3 = 0; | |
4064 | |
4065 n = NGX_HTTP_V2_MAX_WINDOW - ctx->connection->recv_window; | |
4066 ctx->connection->recv_window = NGX_HTTP_V2_MAX_WINDOW; | |
4067 | |
4068 *cl->buf->last++ = (u_char) ((n >> 24) & 0xff); | |
4069 *cl->buf->last++ = (u_char) ((n >> 16) & 0xff); | |
4070 *cl->buf->last++ = (u_char) ((n >> 8) & 0xff); | |
4071 *cl->buf->last++ = (u_char) (n & 0xff); | |
4072 | |
4073 f = (ngx_http_grpc_frame_t *) cl->buf->last; | |
4074 cl->buf->last += sizeof(ngx_http_grpc_frame_t); | |
4075 | |
4076 f->length_0 = 0; | |
4077 f->length_1 = 0; | |
4078 f->length_2 = 4; | |
4079 f->type = NGX_HTTP_V2_WINDOW_UPDATE_FRAME; | |
4080 f->flags = 0; | |
4081 f->stream_id_0 = (u_char) ((ctx->id >> 24) & 0xff); | |
4082 f->stream_id_1 = (u_char) ((ctx->id >> 16) & 0xff); | |
4083 f->stream_id_2 = (u_char) ((ctx->id >> 8) & 0xff); | |
4084 f->stream_id_3 = (u_char) (ctx->id & 0xff); | |
4085 | |
4086 n = NGX_HTTP_V2_MAX_WINDOW - ctx->recv_window; | |
4087 ctx->recv_window = NGX_HTTP_V2_MAX_WINDOW; | |
4088 | |
4089 *cl->buf->last++ = (u_char) ((n >> 24) & 0xff); | |
4090 *cl->buf->last++ = (u_char) ((n >> 16) & 0xff); | |
4091 *cl->buf->last++ = (u_char) ((n >> 8) & 0xff); | |
4092 *cl->buf->last++ = (u_char) (n & 0xff); | |
4093 | |
4094 *ll = cl; | |
4095 | |
4096 return NGX_OK; | |
4097 } | |
4098 | |
4099 | |
4100 static ngx_chain_t * | |
4101 ngx_http_grpc_get_buf(ngx_http_request_t *r, ngx_http_grpc_ctx_t *ctx) | |
4102 { | |
7305
6cfd45d4c754
gRPC: clearing buffers in ngx_http_grpc_get_buf().
Maxim Dounin <mdounin@mdounin.ru>
parents:
7271
diff
changeset
|
4103 u_char *start; |
7233 | 4104 ngx_buf_t *b; |
4105 ngx_chain_t *cl; | |
4106 | |
4107 cl = ngx_chain_get_free_buf(r->pool, &ctx->free); | |
4108 if (cl == NULL) { | |
4109 return NULL; | |
4110 } | |
4111 | |
4112 b = cl->buf; | |
7305
6cfd45d4c754
gRPC: clearing buffers in ngx_http_grpc_get_buf().
Maxim Dounin <mdounin@mdounin.ru>
parents:
7271
diff
changeset
|
4113 start = b->start; |
6cfd45d4c754
gRPC: clearing buffers in ngx_http_grpc_get_buf().
Maxim Dounin <mdounin@mdounin.ru>
parents:
7271
diff
changeset
|
4114 |
6cfd45d4c754
gRPC: clearing buffers in ngx_http_grpc_get_buf().
Maxim Dounin <mdounin@mdounin.ru>
parents:
7271
diff
changeset
|
4115 if (start == NULL) { |
7233 | 4116 |
4117 /* | |
4118 * each buffer is large enough to hold two window update | |
4119 * frames in a row | |
4120 */ | |
4121 | |
7305
6cfd45d4c754
gRPC: clearing buffers in ngx_http_grpc_get_buf().
Maxim Dounin <mdounin@mdounin.ru>
parents:
7271
diff
changeset
|
4122 start = ngx_palloc(r->pool, 2 * sizeof(ngx_http_grpc_frame_t) + 8); |
6cfd45d4c754
gRPC: clearing buffers in ngx_http_grpc_get_buf().
Maxim Dounin <mdounin@mdounin.ru>
parents:
7271
diff
changeset
|
4123 if (start == NULL) { |
7233 | 4124 return NULL; |
4125 } | |
4126 | |
4127 } | |
4128 | |
7305
6cfd45d4c754
gRPC: clearing buffers in ngx_http_grpc_get_buf().
Maxim Dounin <mdounin@mdounin.ru>
parents:
7271
diff
changeset
|
4129 ngx_memzero(b, sizeof(ngx_buf_t)); |
6cfd45d4c754
gRPC: clearing buffers in ngx_http_grpc_get_buf().
Maxim Dounin <mdounin@mdounin.ru>
parents:
7271
diff
changeset
|
4130 |
6cfd45d4c754
gRPC: clearing buffers in ngx_http_grpc_get_buf().
Maxim Dounin <mdounin@mdounin.ru>
parents:
7271
diff
changeset
|
4131 b->start = start; |
6cfd45d4c754
gRPC: clearing buffers in ngx_http_grpc_get_buf().
Maxim Dounin <mdounin@mdounin.ru>
parents:
7271
diff
changeset
|
4132 b->pos = start; |
6cfd45d4c754
gRPC: clearing buffers in ngx_http_grpc_get_buf().
Maxim Dounin <mdounin@mdounin.ru>
parents:
7271
diff
changeset
|
4133 b->last = start; |
6cfd45d4c754
gRPC: clearing buffers in ngx_http_grpc_get_buf().
Maxim Dounin <mdounin@mdounin.ru>
parents:
7271
diff
changeset
|
4134 b->end = start + 2 * sizeof(ngx_http_grpc_frame_t) + 8; |
6cfd45d4c754
gRPC: clearing buffers in ngx_http_grpc_get_buf().
Maxim Dounin <mdounin@mdounin.ru>
parents:
7271
diff
changeset
|
4135 |
6cfd45d4c754
gRPC: clearing buffers in ngx_http_grpc_get_buf().
Maxim Dounin <mdounin@mdounin.ru>
parents:
7271
diff
changeset
|
4136 b->tag = (ngx_buf_tag_t) &ngx_http_grpc_body_output_filter; |
6cfd45d4c754
gRPC: clearing buffers in ngx_http_grpc_get_buf().
Maxim Dounin <mdounin@mdounin.ru>
parents:
7271
diff
changeset
|
4137 b->temporary = 1; |
6cfd45d4c754
gRPC: clearing buffers in ngx_http_grpc_get_buf().
Maxim Dounin <mdounin@mdounin.ru>
parents:
7271
diff
changeset
|
4138 b->flush = 1; |
6cfd45d4c754
gRPC: clearing buffers in ngx_http_grpc_get_buf().
Maxim Dounin <mdounin@mdounin.ru>
parents:
7271
diff
changeset
|
4139 |
7233 | 4140 return cl; |
4141 } | |
4142 | |
4143 | |
4144 static ngx_http_grpc_ctx_t * | |
4145 ngx_http_grpc_get_ctx(ngx_http_request_t *r) | |
4146 { | |
4147 ngx_http_grpc_ctx_t *ctx; | |
4148 ngx_http_upstream_t *u; | |
4149 | |
4150 ctx = ngx_http_get_module_ctx(r, ngx_http_grpc_module); | |
4151 | |
4152 if (ctx->connection == NULL) { | |
4153 u = r->upstream; | |
4154 | |
4155 if (ngx_http_grpc_get_connection_data(r, ctx, &u->peer) != NGX_OK) { | |
4156 return NULL; | |
4157 } | |
4158 } | |
4159 | |
4160 return ctx; | |
4161 } | |
4162 | |
4163 | |
4164 static ngx_int_t | |
4165 ngx_http_grpc_get_connection_data(ngx_http_request_t *r, | |
4166 ngx_http_grpc_ctx_t *ctx, ngx_peer_connection_t *pc) | |
4167 { | |
4168 ngx_connection_t *c; | |
4169 ngx_pool_cleanup_t *cln; | |
4170 | |
4171 c = pc->connection; | |
4172 | |
4173 if (pc->cached) { | |
4174 | |
4175 /* | |
4176 * for cached connections, connection data can be found | |
4177 * in the cleanup handler | |
4178 */ | |
4179 | |
4180 for (cln = c->pool->cleanup; cln; cln = cln->next) { | |
4181 if (cln->handler == ngx_http_grpc_cleanup) { | |
4182 ctx->connection = cln->data; | |
4183 break; | |
4184 } | |
4185 } | |
4186 | |
4187 if (ctx->connection == NULL) { | |
4188 ngx_log_error(NGX_LOG_ERR, c->log, 0, | |
4189 "no connection data found for " | |
4190 "keepalive http2 connection"); | |
4191 return NGX_ERROR; | |
4192 } | |
4193 | |
4194 ctx->send_window = ctx->connection->init_window; | |
4195 ctx->recv_window = NGX_HTTP_V2_MAX_WINDOW; | |
4196 | |
4197 ctx->connection->last_stream_id += 2; | |
4198 ctx->id = ctx->connection->last_stream_id; | |
4199 | |
4200 return NGX_OK; | |
4201 } | |
4202 | |
4203 cln = ngx_pool_cleanup_add(c->pool, sizeof(ngx_http_grpc_conn_t)); | |
4204 if (cln == NULL) { | |
4205 return NGX_ERROR; | |
4206 } | |
4207 | |
4208 cln->handler = ngx_http_grpc_cleanup; | |
4209 ctx->connection = cln->data; | |
4210 | |
4211 ctx->connection->init_window = NGX_HTTP_V2_DEFAULT_WINDOW; | |
4212 ctx->connection->send_window = NGX_HTTP_V2_DEFAULT_WINDOW; | |
4213 ctx->connection->recv_window = NGX_HTTP_V2_MAX_WINDOW; | |
4214 | |
4215 ctx->send_window = NGX_HTTP_V2_DEFAULT_WINDOW; | |
4216 ctx->recv_window = NGX_HTTP_V2_MAX_WINDOW; | |
4217 | |
4218 ctx->id = 1; | |
4219 ctx->connection->last_stream_id = 1; | |
4220 | |
4221 return NGX_OK; | |
4222 } | |
4223 | |
4224 | |
4225 static void | |
4226 ngx_http_grpc_cleanup(void *data) | |
4227 { | |
4228 #if 0 | |
4229 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, | |
4230 "grpc cleanup"); | |
4231 #endif | |
4232 return; | |
4233 } | |
4234 | |
4235 | |
4236 static void | |
4237 ngx_http_grpc_abort_request(ngx_http_request_t *r) | |
4238 { | |
4239 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
4240 "abort grpc request"); | |
4241 return; | |
4242 } | |
4243 | |
4244 | |
4245 static void | |
4246 ngx_http_grpc_finalize_request(ngx_http_request_t *r, ngx_int_t rc) | |
4247 { | |
4248 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
4249 "finalize grpc request"); | |
4250 return; | |
4251 } | |
4252 | |
4253 | |
7234
c693daca57f7
gRPC: special handling of the TE request header.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7233
diff
changeset
|
4254 static ngx_int_t |
c693daca57f7
gRPC: special handling of the TE request header.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7233
diff
changeset
|
4255 ngx_http_grpc_internal_trailers_variable(ngx_http_request_t *r, |
c693daca57f7
gRPC: special handling of the TE request header.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7233
diff
changeset
|
4256 ngx_http_variable_value_t *v, uintptr_t data) |
c693daca57f7
gRPC: special handling of the TE request header.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7233
diff
changeset
|
4257 { |
c693daca57f7
gRPC: special handling of the TE request header.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7233
diff
changeset
|
4258 ngx_table_elt_t *te; |
c693daca57f7
gRPC: special handling of the TE request header.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7233
diff
changeset
|
4259 |
c693daca57f7
gRPC: special handling of the TE request header.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7233
diff
changeset
|
4260 te = r->headers_in.te; |
c693daca57f7
gRPC: special handling of the TE request header.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7233
diff
changeset
|
4261 |
c693daca57f7
gRPC: special handling of the TE request header.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7233
diff
changeset
|
4262 if (te == NULL) { |
c693daca57f7
gRPC: special handling of the TE request header.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7233
diff
changeset
|
4263 v->not_found = 1; |
c693daca57f7
gRPC: special handling of the TE request header.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7233
diff
changeset
|
4264 return NGX_OK; |
c693daca57f7
gRPC: special handling of the TE request header.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7233
diff
changeset
|
4265 } |
c693daca57f7
gRPC: special handling of the TE request header.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7233
diff
changeset
|
4266 |
c693daca57f7
gRPC: special handling of the TE request header.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7233
diff
changeset
|
4267 if (ngx_strlcasestrn(te->value.data, te->value.data + te->value.len, |
c693daca57f7
gRPC: special handling of the TE request header.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7233
diff
changeset
|
4268 (u_char *) "trailers", 8 - 1) |
c693daca57f7
gRPC: special handling of the TE request header.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7233
diff
changeset
|
4269 == NULL) |
c693daca57f7
gRPC: special handling of the TE request header.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7233
diff
changeset
|
4270 { |
c693daca57f7
gRPC: special handling of the TE request header.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7233
diff
changeset
|
4271 v->not_found = 1; |
c693daca57f7
gRPC: special handling of the TE request header.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7233
diff
changeset
|
4272 return NGX_OK; |
c693daca57f7
gRPC: special handling of the TE request header.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7233
diff
changeset
|
4273 } |
c693daca57f7
gRPC: special handling of the TE request header.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7233
diff
changeset
|
4274 |
c693daca57f7
gRPC: special handling of the TE request header.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7233
diff
changeset
|
4275 v->valid = 1; |
c693daca57f7
gRPC: special handling of the TE request header.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7233
diff
changeset
|
4276 v->no_cacheable = 0; |
c693daca57f7
gRPC: special handling of the TE request header.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7233
diff
changeset
|
4277 v->not_found = 0; |
c693daca57f7
gRPC: special handling of the TE request header.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7233
diff
changeset
|
4278 |
c693daca57f7
gRPC: special handling of the TE request header.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7233
diff
changeset
|
4279 v->data = (u_char *) "trailers"; |
c693daca57f7
gRPC: special handling of the TE request header.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7233
diff
changeset
|
4280 v->len = sizeof("trailers") - 1; |
c693daca57f7
gRPC: special handling of the TE request header.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7233
diff
changeset
|
4281 |
c693daca57f7
gRPC: special handling of the TE request header.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7233
diff
changeset
|
4282 return NGX_OK; |
c693daca57f7
gRPC: special handling of the TE request header.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7233
diff
changeset
|
4283 } |
c693daca57f7
gRPC: special handling of the TE request header.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7233
diff
changeset
|
4284 |
c693daca57f7
gRPC: special handling of the TE request header.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7233
diff
changeset
|
4285 |
c693daca57f7
gRPC: special handling of the TE request header.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7233
diff
changeset
|
4286 static ngx_int_t |
c693daca57f7
gRPC: special handling of the TE request header.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7233
diff
changeset
|
4287 ngx_http_grpc_add_variables(ngx_conf_t *cf) |
c693daca57f7
gRPC: special handling of the TE request header.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7233
diff
changeset
|
4288 { |
c693daca57f7
gRPC: special handling of the TE request header.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7233
diff
changeset
|
4289 ngx_http_variable_t *var, *v; |
c693daca57f7
gRPC: special handling of the TE request header.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7233
diff
changeset
|
4290 |
c693daca57f7
gRPC: special handling of the TE request header.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7233
diff
changeset
|
4291 for (v = ngx_http_grpc_vars; v->name.len; v++) { |
c693daca57f7
gRPC: special handling of the TE request header.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7233
diff
changeset
|
4292 var = ngx_http_add_variable(cf, &v->name, v->flags); |
c693daca57f7
gRPC: special handling of the TE request header.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7233
diff
changeset
|
4293 if (var == NULL) { |
c693daca57f7
gRPC: special handling of the TE request header.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7233
diff
changeset
|
4294 return NGX_ERROR; |
c693daca57f7
gRPC: special handling of the TE request header.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7233
diff
changeset
|
4295 } |
c693daca57f7
gRPC: special handling of the TE request header.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7233
diff
changeset
|
4296 |
c693daca57f7
gRPC: special handling of the TE request header.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7233
diff
changeset
|
4297 var->get_handler = v->get_handler; |
c693daca57f7
gRPC: special handling of the TE request header.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7233
diff
changeset
|
4298 var->data = v->data; |
c693daca57f7
gRPC: special handling of the TE request header.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7233
diff
changeset
|
4299 } |
c693daca57f7
gRPC: special handling of the TE request header.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7233
diff
changeset
|
4300 |
c693daca57f7
gRPC: special handling of the TE request header.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7233
diff
changeset
|
4301 return NGX_OK; |
c693daca57f7
gRPC: special handling of the TE request header.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7233
diff
changeset
|
4302 } |
c693daca57f7
gRPC: special handling of the TE request header.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7233
diff
changeset
|
4303 |
c693daca57f7
gRPC: special handling of the TE request header.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7233
diff
changeset
|
4304 |
7233 | 4305 static void * |
4306 ngx_http_grpc_create_loc_conf(ngx_conf_t *cf) | |
4307 { | |
4308 ngx_http_grpc_loc_conf_t *conf; | |
4309 | |
4310 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_grpc_loc_conf_t)); | |
4311 if (conf == NULL) { | |
4312 return NULL; | |
4313 } | |
4314 | |
4315 /* | |
4316 * set by ngx_pcalloc(): | |
4317 * | |
4318 * conf->upstream.ignore_headers = 0; | |
4319 * conf->upstream.next_upstream = 0; | |
4320 * conf->upstream.hide_headers_hash = { NULL, 0 }; | |
4321 * conf->upstream.ssl_name = NULL; | |
4322 * | |
4323 * conf->headers.lengths = NULL; | |
4324 * conf->headers.values = NULL; | |
4325 * conf->headers.hash = { NULL, 0 }; | |
4326 * conf->host = { 0, NULL }; | |
4327 * conf->host_set = 0; | |
4328 * conf->ssl = 0; | |
4329 * conf->ssl_protocols = 0; | |
4330 * conf->ssl_ciphers = { 0, NULL }; | |
4331 * conf->ssl_trusted_certificate = { 0, NULL }; | |
4332 * conf->ssl_crl = { 0, NULL }; | |
4333 * conf->ssl_certificate = { 0, NULL }; | |
4334 * conf->ssl_certificate_key = { 0, NULL }; | |
4335 */ | |
4336 | |
4337 conf->upstream.local = NGX_CONF_UNSET_PTR; | |
7371
8b68d50090e4
Upstream: proxy_socket_keepalive and friends.
Vladimir Homutov <vl@nginx.com>
parents:
7350
diff
changeset
|
4338 conf->upstream.socket_keepalive = NGX_CONF_UNSET; |
7233 | 4339 conf->upstream.next_upstream_tries = NGX_CONF_UNSET_UINT; |
4340 conf->upstream.connect_timeout = NGX_CONF_UNSET_MSEC; | |
4341 conf->upstream.send_timeout = NGX_CONF_UNSET_MSEC; | |
4342 conf->upstream.read_timeout = NGX_CONF_UNSET_MSEC; | |
4343 conf->upstream.next_upstream_timeout = NGX_CONF_UNSET_MSEC; | |
4344 | |
4345 conf->upstream.buffer_size = NGX_CONF_UNSET_SIZE; | |
4346 | |
4347 conf->upstream.hide_headers = NGX_CONF_UNSET_PTR; | |
4348 conf->upstream.pass_headers = NGX_CONF_UNSET_PTR; | |
4349 | |
4350 conf->upstream.intercept_errors = NGX_CONF_UNSET; | |
4351 | |
4352 #if (NGX_HTTP_SSL) | |
4353 conf->upstream.ssl_session_reuse = NGX_CONF_UNSET; | |
4354 conf->upstream.ssl_server_name = NGX_CONF_UNSET; | |
4355 conf->upstream.ssl_verify = NGX_CONF_UNSET; | |
4356 conf->ssl_verify_depth = NGX_CONF_UNSET_UINT; | |
4357 conf->ssl_passwords = NGX_CONF_UNSET_PTR; | |
8183
1a719ee45526
Upstream: proxy_ssl_conf_command and friends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
8181
diff
changeset
|
4358 conf->ssl_conf_commands = NGX_CONF_UNSET_PTR; |
7233 | 4359 #endif |
4360 | |
4361 /* the hardcoded values */ | |
4362 conf->upstream.cyclic_temp_file = 0; | |
4363 conf->upstream.buffering = 0; | |
4364 conf->upstream.ignore_client_abort = 0; | |
4365 conf->upstream.send_lowat = 0; | |
4366 conf->upstream.bufs.num = 0; | |
4367 conf->upstream.busy_buffers_size = 0; | |
4368 conf->upstream.max_temp_file_size = 0; | |
4369 conf->upstream.temp_file_write_size = 0; | |
4370 conf->upstream.pass_request_headers = 1; | |
4371 conf->upstream.pass_request_body = 1; | |
4372 conf->upstream.force_ranges = 0; | |
4373 conf->upstream.pass_trailers = 1; | |
4374 conf->upstream.preserve_output = 1; | |
4375 | |
8181
485dba3e2a01
Core: ngx_conf_set_keyval_slot() now accepts NGX_CONF_UNSET_PTR.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7983
diff
changeset
|
4376 conf->headers_source = NGX_CONF_UNSET_PTR; |
485dba3e2a01
Core: ngx_conf_set_keyval_slot() now accepts NGX_CONF_UNSET_PTR.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7983
diff
changeset
|
4377 |
7233 | 4378 ngx_str_set(&conf->upstream.module, "grpc"); |
4379 | |
4380 return conf; | |
4381 } | |
4382 | |
4383 | |
4384 static char * | |
4385 ngx_http_grpc_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child) | |
4386 { | |
4387 ngx_http_grpc_loc_conf_t *prev = parent; | |
4388 ngx_http_grpc_loc_conf_t *conf = child; | |
4389 | |
4390 ngx_int_t rc; | |
4391 ngx_hash_init_t hash; | |
4392 ngx_http_core_loc_conf_t *clcf; | |
4393 | |
4394 ngx_conf_merge_ptr_value(conf->upstream.local, | |
4395 prev->upstream.local, NULL); | |
4396 | |
7371
8b68d50090e4
Upstream: proxy_socket_keepalive and friends.
Vladimir Homutov <vl@nginx.com>
parents:
7350
diff
changeset
|
4397 ngx_conf_merge_value(conf->upstream.socket_keepalive, |
8b68d50090e4
Upstream: proxy_socket_keepalive and friends.
Vladimir Homutov <vl@nginx.com>
parents:
7350
diff
changeset
|
4398 prev->upstream.socket_keepalive, 0); |
8b68d50090e4
Upstream: proxy_socket_keepalive and friends.
Vladimir Homutov <vl@nginx.com>
parents:
7350
diff
changeset
|
4399 |
7233 | 4400 ngx_conf_merge_uint_value(conf->upstream.next_upstream_tries, |
4401 prev->upstream.next_upstream_tries, 0); | |
4402 | |
4403 ngx_conf_merge_msec_value(conf->upstream.connect_timeout, | |
4404 prev->upstream.connect_timeout, 60000); | |
4405 | |
4406 ngx_conf_merge_msec_value(conf->upstream.send_timeout, | |
4407 prev->upstream.send_timeout, 60000); | |
4408 | |
4409 ngx_conf_merge_msec_value(conf->upstream.read_timeout, | |
4410 prev->upstream.read_timeout, 60000); | |
4411 | |
4412 ngx_conf_merge_msec_value(conf->upstream.next_upstream_timeout, | |
4413 prev->upstream.next_upstream_timeout, 0); | |
4414 | |
4415 ngx_conf_merge_size_value(conf->upstream.buffer_size, | |
4416 prev->upstream.buffer_size, | |
4417 (size_t) ngx_pagesize); | |
4418 | |
4419 ngx_conf_merge_bitmask_value(conf->upstream.ignore_headers, | |
4420 prev->upstream.ignore_headers, | |
4421 NGX_CONF_BITMASK_SET); | |
4422 | |
4423 ngx_conf_merge_bitmask_value(conf->upstream.next_upstream, | |
4424 prev->upstream.next_upstream, | |
4425 (NGX_CONF_BITMASK_SET | |
4426 |NGX_HTTP_UPSTREAM_FT_ERROR | |
4427 |NGX_HTTP_UPSTREAM_FT_TIMEOUT)); | |
4428 | |
4429 if (conf->upstream.next_upstream & NGX_HTTP_UPSTREAM_FT_OFF) { | |
4430 conf->upstream.next_upstream = NGX_CONF_BITMASK_SET | |
4431 |NGX_HTTP_UPSTREAM_FT_OFF; | |
4432 } | |
4433 | |
4434 ngx_conf_merge_value(conf->upstream.intercept_errors, | |
4435 prev->upstream.intercept_errors, 0); | |
4436 | |
4437 #if (NGX_HTTP_SSL) | |
4438 | |
4439 ngx_conf_merge_value(conf->upstream.ssl_session_reuse, | |
4440 prev->upstream.ssl_session_reuse, 1); | |
4441 | |
4442 ngx_conf_merge_bitmask_value(conf->ssl_protocols, prev->ssl_protocols, | |
4443 (NGX_CONF_BITMASK_SET|NGX_SSL_TLSv1 | |
4444 |NGX_SSL_TLSv1_1|NGX_SSL_TLSv1_2)); | |
4445 | |
4446 ngx_conf_merge_str_value(conf->ssl_ciphers, prev->ssl_ciphers, | |
4447 "DEFAULT"); | |
4448 | |
4449 if (conf->upstream.ssl_name == NULL) { | |
4450 conf->upstream.ssl_name = prev->upstream.ssl_name; | |
4451 } | |
4452 | |
4453 ngx_conf_merge_value(conf->upstream.ssl_server_name, | |
4454 prev->upstream.ssl_server_name, 0); | |
4455 ngx_conf_merge_value(conf->upstream.ssl_verify, | |
4456 prev->upstream.ssl_verify, 0); | |
4457 ngx_conf_merge_uint_value(conf->ssl_verify_depth, | |
4458 prev->ssl_verify_depth, 1); | |
4459 ngx_conf_merge_str_value(conf->ssl_trusted_certificate, | |
4460 prev->ssl_trusted_certificate, ""); | |
4461 ngx_conf_merge_str_value(conf->ssl_crl, prev->ssl_crl, ""); | |
4462 | |
4463 ngx_conf_merge_str_value(conf->ssl_certificate, | |
4464 prev->ssl_certificate, ""); | |
4465 ngx_conf_merge_str_value(conf->ssl_certificate_key, | |
4466 prev->ssl_certificate_key, ""); | |
4467 ngx_conf_merge_ptr_value(conf->ssl_passwords, prev->ssl_passwords, NULL); | |
4468 | |
8183
1a719ee45526
Upstream: proxy_ssl_conf_command and friends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
8181
diff
changeset
|
4469 ngx_conf_merge_ptr_value(conf->ssl_conf_commands, |
1a719ee45526
Upstream: proxy_ssl_conf_command and friends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
8181
diff
changeset
|
4470 prev->ssl_conf_commands, NULL); |
1a719ee45526
Upstream: proxy_ssl_conf_command and friends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
8181
diff
changeset
|
4471 |
7233 | 4472 if (conf->ssl && ngx_http_grpc_set_ssl(cf, conf) != NGX_OK) { |
4473 return NGX_CONF_ERROR; | |
4474 } | |
4475 | |
4476 #endif | |
4477 | |
4478 hash.max_size = 512; | |
4479 hash.bucket_size = ngx_align(64, ngx_cacheline_size); | |
4480 hash.name = "grpc_headers_hash"; | |
4481 | |
4482 if (ngx_http_upstream_hide_headers_hash(cf, &conf->upstream, | |
4483 &prev->upstream, ngx_http_grpc_hide_headers, &hash) | |
4484 != NGX_OK) | |
4485 { | |
4486 return NGX_CONF_ERROR; | |
4487 } | |
4488 | |
4489 clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module); | |
4490 | |
7619
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
4491 if (clcf->noname |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
4492 && conf->upstream.upstream == NULL && conf->grpc_lengths == NULL) |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
4493 { |
7233 | 4494 conf->upstream.upstream = prev->upstream.upstream; |
4495 conf->host = prev->host; | |
7619
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
4496 |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
4497 conf->grpc_lengths = prev->grpc_lengths; |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
4498 conf->grpc_values = prev->grpc_values; |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
4499 |
7233 | 4500 #if (NGX_HTTP_SSL) |
4501 conf->upstream.ssl = prev->upstream.ssl; | |
4502 #endif | |
4503 } | |
4504 | |
7619
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
4505 if (clcf->lmt_excpt && clcf->handler == NULL |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
4506 && (conf->upstream.upstream || conf->grpc_lengths)) |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
4507 { |
7233 | 4508 clcf->handler = ngx_http_grpc_handler; |
4509 } | |
4510 | |
8181
485dba3e2a01
Core: ngx_conf_set_keyval_slot() now accepts NGX_CONF_UNSET_PTR.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7983
diff
changeset
|
4511 ngx_conf_merge_ptr_value(conf->headers_source, prev->headers_source, NULL); |
485dba3e2a01
Core: ngx_conf_set_keyval_slot() now accepts NGX_CONF_UNSET_PTR.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7983
diff
changeset
|
4512 |
485dba3e2a01
Core: ngx_conf_set_keyval_slot() now accepts NGX_CONF_UNSET_PTR.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7983
diff
changeset
|
4513 if (conf->headers_source == prev->headers_source) { |
7233 | 4514 conf->headers = prev->headers; |
4515 conf->host_set = prev->host_set; | |
4516 } | |
4517 | |
4518 rc = ngx_http_grpc_init_headers(cf, conf, &conf->headers, | |
4519 ngx_http_grpc_headers); | |
4520 if (rc != NGX_OK) { | |
4521 return NGX_CONF_ERROR; | |
4522 } | |
4523 | |
4524 /* | |
4525 * special handling to preserve conf->headers in the "http" section | |
4526 * to inherit it to all servers | |
4527 */ | |
4528 | |
4529 if (prev->headers.hash.buckets == NULL | |
4530 && conf->headers_source == prev->headers_source) | |
4531 { | |
4532 prev->headers = conf->headers; | |
4533 prev->host_set = conf->host_set; | |
4534 } | |
4535 | |
4536 return NGX_CONF_OK; | |
4537 } | |
4538 | |
4539 | |
4540 static ngx_int_t | |
4541 ngx_http_grpc_init_headers(ngx_conf_t *cf, ngx_http_grpc_loc_conf_t *conf, | |
4542 ngx_http_grpc_headers_t *headers, ngx_keyval_t *default_headers) | |
4543 { | |
4544 u_char *p; | |
4545 size_t size; | |
4546 uintptr_t *code; | |
4547 ngx_uint_t i; | |
4548 ngx_array_t headers_names, headers_merged; | |
4549 ngx_keyval_t *src, *s, *h; | |
4550 ngx_hash_key_t *hk; | |
4551 ngx_hash_init_t hash; | |
4552 ngx_http_script_compile_t sc; | |
4553 ngx_http_script_copy_code_t *copy; | |
4554 | |
4555 if (headers->hash.buckets) { | |
4556 return NGX_OK; | |
4557 } | |
4558 | |
4559 if (ngx_array_init(&headers_names, cf->temp_pool, 4, sizeof(ngx_hash_key_t)) | |
4560 != NGX_OK) | |
4561 { | |
4562 return NGX_ERROR; | |
4563 } | |
4564 | |
4565 if (ngx_array_init(&headers_merged, cf->temp_pool, 4, sizeof(ngx_keyval_t)) | |
4566 != NGX_OK) | |
4567 { | |
4568 return NGX_ERROR; | |
4569 } | |
4570 | |
4571 headers->lengths = ngx_array_create(cf->pool, 64, 1); | |
4572 if (headers->lengths == NULL) { | |
4573 return NGX_ERROR; | |
4574 } | |
4575 | |
4576 headers->values = ngx_array_create(cf->pool, 512, 1); | |
4577 if (headers->values == NULL) { | |
4578 return NGX_ERROR; | |
4579 } | |
4580 | |
4581 if (conf->headers_source) { | |
4582 | |
4583 src = conf->headers_source->elts; | |
4584 for (i = 0; i < conf->headers_source->nelts; i++) { | |
4585 | |
4586 if (src[i].key.len == 4 | |
4587 && ngx_strncasecmp(src[i].key.data, (u_char *) "Host", 4) == 0) | |
4588 { | |
4589 conf->host_set = 1; | |
4590 } | |
4591 | |
4592 s = ngx_array_push(&headers_merged); | |
4593 if (s == NULL) { | |
4594 return NGX_ERROR; | |
4595 } | |
4596 | |
4597 *s = src[i]; | |
4598 } | |
4599 } | |
4600 | |
4601 h = default_headers; | |
4602 | |
4603 while (h->key.len) { | |
4604 | |
4605 src = headers_merged.elts; | |
4606 for (i = 0; i < headers_merged.nelts; i++) { | |
4607 if (ngx_strcasecmp(h->key.data, src[i].key.data) == 0) { | |
4608 goto next; | |
4609 } | |
4610 } | |
4611 | |
4612 s = ngx_array_push(&headers_merged); | |
4613 if (s == NULL) { | |
4614 return NGX_ERROR; | |
4615 } | |
4616 | |
4617 *s = *h; | |
4618 | |
4619 next: | |
4620 | |
4621 h++; | |
4622 } | |
4623 | |
4624 | |
4625 src = headers_merged.elts; | |
4626 for (i = 0; i < headers_merged.nelts; i++) { | |
4627 | |
4628 hk = ngx_array_push(&headers_names); | |
4629 if (hk == NULL) { | |
4630 return NGX_ERROR; | |
4631 } | |
4632 | |
4633 hk->key = src[i].key; | |
4634 hk->key_hash = ngx_hash_key_lc(src[i].key.data, src[i].key.len); | |
4635 hk->value = (void *) 1; | |
4636 | |
4637 if (src[i].value.len == 0) { | |
4638 continue; | |
4639 } | |
4640 | |
4641 copy = ngx_array_push_n(headers->lengths, | |
4642 sizeof(ngx_http_script_copy_code_t)); | |
4643 if (copy == NULL) { | |
4644 return NGX_ERROR; | |
4645 } | |
4646 | |
7271
9e25a5380a21
Silenced -Wcast-function-type warnings (closes #1546).
Sergey Kandaurov <pluknet@nginx.com>
parents:
7249
diff
changeset
|
4647 copy->code = (ngx_http_script_code_pt) (void *) |
9e25a5380a21
Silenced -Wcast-function-type warnings (closes #1546).
Sergey Kandaurov <pluknet@nginx.com>
parents:
7249
diff
changeset
|
4648 ngx_http_script_copy_len_code; |
7233 | 4649 copy->len = src[i].key.len; |
4650 | |
4651 size = (sizeof(ngx_http_script_copy_code_t) | |
4652 + src[i].key.len + sizeof(uintptr_t) - 1) | |
4653 & ~(sizeof(uintptr_t) - 1); | |
4654 | |
4655 copy = ngx_array_push_n(headers->values, size); | |
4656 if (copy == NULL) { | |
4657 return NGX_ERROR; | |
4658 } | |
4659 | |
4660 copy->code = ngx_http_script_copy_code; | |
4661 copy->len = src[i].key.len; | |
4662 | |
4663 p = (u_char *) copy + sizeof(ngx_http_script_copy_code_t); | |
4664 ngx_memcpy(p, src[i].key.data, src[i].key.len); | |
4665 | |
4666 ngx_memzero(&sc, sizeof(ngx_http_script_compile_t)); | |
4667 | |
4668 sc.cf = cf; | |
4669 sc.source = &src[i].value; | |
4670 sc.flushes = &headers->flushes; | |
4671 sc.lengths = &headers->lengths; | |
4672 sc.values = &headers->values; | |
4673 | |
4674 if (ngx_http_script_compile(&sc) != NGX_OK) { | |
4675 return NGX_ERROR; | |
4676 } | |
4677 | |
4678 code = ngx_array_push_n(headers->lengths, sizeof(uintptr_t)); | |
4679 if (code == NULL) { | |
4680 return NGX_ERROR; | |
4681 } | |
4682 | |
4683 *code = (uintptr_t) NULL; | |
4684 | |
4685 code = ngx_array_push_n(headers->values, sizeof(uintptr_t)); | |
4686 if (code == NULL) { | |
4687 return NGX_ERROR; | |
4688 } | |
4689 | |
4690 *code = (uintptr_t) NULL; | |
4691 } | |
4692 | |
4693 code = ngx_array_push_n(headers->lengths, sizeof(uintptr_t)); | |
4694 if (code == NULL) { | |
4695 return NGX_ERROR; | |
4696 } | |
4697 | |
4698 *code = (uintptr_t) NULL; | |
4699 | |
4700 | |
4701 hash.hash = &headers->hash; | |
4702 hash.key = ngx_hash_key_lc; | |
4703 hash.max_size = 512; | |
4704 hash.bucket_size = 64; | |
4705 hash.name = "grpc_headers_hash"; | |
4706 hash.pool = cf->pool; | |
4707 hash.temp_pool = NULL; | |
4708 | |
4709 return ngx_hash_init(&hash, headers_names.elts, headers_names.nelts); | |
4710 } | |
4711 | |
4712 | |
4713 static char * | |
4714 ngx_http_grpc_pass(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
4715 { | |
4716 ngx_http_grpc_loc_conf_t *glcf = conf; | |
4717 | |
7619
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
4718 size_t add; |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
4719 ngx_str_t *value, *url; |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
4720 ngx_url_t u; |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
4721 ngx_uint_t n; |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
4722 ngx_http_core_loc_conf_t *clcf; |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
4723 ngx_http_script_compile_t sc; |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
4724 |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
4725 if (glcf->upstream.upstream || glcf->grpc_lengths) { |
7233 | 4726 return "is duplicate"; |
4727 } | |
4728 | |
7619
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
4729 clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module); |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
4730 |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
4731 clcf->handler = ngx_http_grpc_handler; |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
4732 |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
4733 if (clcf->name.len && clcf->name.data[clcf->name.len - 1] == '/') { |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
4734 clcf->auto_redirect = 1; |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
4735 } |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
4736 |
7233 | 4737 value = cf->args->elts; |
7619
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
4738 |
7233 | 4739 url = &value[1]; |
4740 | |
7619
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
4741 n = ngx_http_script_variables_count(url); |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
4742 |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
4743 if (n) { |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
4744 |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
4745 ngx_memzero(&sc, sizeof(ngx_http_script_compile_t)); |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
4746 |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
4747 sc.cf = cf; |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
4748 sc.source = url; |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
4749 sc.lengths = &glcf->grpc_lengths; |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
4750 sc.values = &glcf->grpc_values; |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
4751 sc.variables = n; |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
4752 sc.complete_lengths = 1; |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
4753 sc.complete_values = 1; |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
4754 |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
4755 if (ngx_http_script_compile(&sc) != NGX_OK) { |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
4756 return NGX_CONF_ERROR; |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
4757 } |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
4758 |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
4759 #if (NGX_HTTP_SSL) |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
4760 glcf->ssl = 1; |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
4761 #endif |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
4762 |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
4763 return NGX_CONF_OK; |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
4764 } |
6439ef81e37d
gRPC: variables support in the "grpc_pass" directive.
Vladimir Homutov <vl@nginx.com>
parents:
7473
diff
changeset
|
4765 |
7233 | 4766 if (ngx_strncasecmp(url->data, (u_char *) "grpc://", 7) == 0) { |
4767 add = 7; | |
4768 | |
4769 } else if (ngx_strncasecmp(url->data, (u_char *) "grpcs://", 8) == 0) { | |
4770 | |
4771 #if (NGX_HTTP_SSL) | |
4772 glcf->ssl = 1; | |
4773 | |
4774 add = 8; | |
4775 #else | |
4776 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
4777 "grpcs protocol requires SSL support"); | |
4778 return NGX_CONF_ERROR; | |
4779 #endif | |
4780 | |
4781 } else { | |
4782 add = 0; | |
4783 } | |
4784 | |
4785 ngx_memzero(&u, sizeof(ngx_url_t)); | |
4786 | |
4787 u.url.len = url->len - add; | |
4788 u.url.data = url->data + add; | |
4789 u.no_resolve = 1; | |
4790 | |
4791 glcf->upstream.upstream = ngx_http_upstream_add(cf, &u, 0); | |
4792 if (glcf->upstream.upstream == NULL) { | |
4793 return NGX_CONF_ERROR; | |
4794 } | |
4795 | |
4796 if (u.family != AF_UNIX) { | |
4797 | |
4798 if (u.no_port) { | |
4799 glcf->host = u.host; | |
4800 | |
4801 } else { | |
4802 glcf->host.len = u.host.len + 1 + u.port_text.len; | |
4803 glcf->host.data = u.host.data; | |
4804 } | |
4805 | |
4806 } else { | |
4807 ngx_str_set(&glcf->host, "localhost"); | |
4808 } | |
4809 | |
4810 return NGX_CONF_OK; | |
4811 } | |
4812 | |
4813 | |
4814 #if (NGX_HTTP_SSL) | |
4815 | |
4816 static char * | |
4817 ngx_http_grpc_ssl_password_file(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
4818 { | |
4819 ngx_http_grpc_loc_conf_t *glcf = conf; | |
4820 | |
4821 ngx_str_t *value; | |
4822 | |
4823 if (glcf->ssl_passwords != NGX_CONF_UNSET_PTR) { | |
4824 return "is duplicate"; | |
4825 } | |
4826 | |
4827 value = cf->args->elts; | |
4828 | |
4829 glcf->ssl_passwords = ngx_ssl_read_password_file(cf, &value[1]); | |
4830 | |
4831 if (glcf->ssl_passwords == NULL) { | |
4832 return NGX_CONF_ERROR; | |
4833 } | |
4834 | |
4835 return NGX_CONF_OK; | |
4836 } | |
4837 | |
4838 | |
8183
1a719ee45526
Upstream: proxy_ssl_conf_command and friends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
8181
diff
changeset
|
4839 static char * |
1a719ee45526
Upstream: proxy_ssl_conf_command and friends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
8181
diff
changeset
|
4840 ngx_http_grpc_ssl_conf_command_check(ngx_conf_t *cf, void *post, void *data) |
1a719ee45526
Upstream: proxy_ssl_conf_command and friends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
8181
diff
changeset
|
4841 { |
1a719ee45526
Upstream: proxy_ssl_conf_command and friends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
8181
diff
changeset
|
4842 #ifndef SSL_CONF_FLAG_FILE |
1a719ee45526
Upstream: proxy_ssl_conf_command and friends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
8181
diff
changeset
|
4843 return "is not supported on this platform"; |
1a719ee45526
Upstream: proxy_ssl_conf_command and friends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
8181
diff
changeset
|
4844 #endif |
1a719ee45526
Upstream: proxy_ssl_conf_command and friends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
8181
diff
changeset
|
4845 |
1a719ee45526
Upstream: proxy_ssl_conf_command and friends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
8181
diff
changeset
|
4846 return NGX_CONF_OK; |
1a719ee45526
Upstream: proxy_ssl_conf_command and friends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
8181
diff
changeset
|
4847 } |
1a719ee45526
Upstream: proxy_ssl_conf_command and friends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
8181
diff
changeset
|
4848 |
1a719ee45526
Upstream: proxy_ssl_conf_command and friends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
8181
diff
changeset
|
4849 |
7233 | 4850 static ngx_int_t |
4851 ngx_http_grpc_set_ssl(ngx_conf_t *cf, ngx_http_grpc_loc_conf_t *glcf) | |
4852 { | |
4853 ngx_pool_cleanup_t *cln; | |
4854 | |
4855 glcf->upstream.ssl = ngx_pcalloc(cf->pool, sizeof(ngx_ssl_t)); | |
4856 if (glcf->upstream.ssl == NULL) { | |
4857 return NGX_ERROR; | |
4858 } | |
4859 | |
4860 glcf->upstream.ssl->log = cf->log; | |
4861 | |
4862 if (ngx_ssl_create(glcf->upstream.ssl, glcf->ssl_protocols, NULL) | |
4863 != NGX_OK) | |
4864 { | |
4865 return NGX_ERROR; | |
4866 } | |
4867 | |
4868 cln = ngx_pool_cleanup_add(cf->pool, 0); | |
4869 if (cln == NULL) { | |
7473
8981dbb12254
SSL: fixed potential leak on memory allocation errors.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7379
diff
changeset
|
4870 ngx_ssl_cleanup_ctx(glcf->upstream.ssl); |
7233 | 4871 return NGX_ERROR; |
4872 } | |
4873 | |
4874 cln->handler = ngx_ssl_cleanup_ctx; | |
4875 cln->data = glcf->upstream.ssl; | |
4876 | |
4877 if (glcf->ssl_certificate.len) { | |
4878 | |
4879 if (glcf->ssl_certificate_key.len == 0) { | |
4880 ngx_log_error(NGX_LOG_EMERG, cf->log, 0, | |
4881 "no \"grpc_ssl_certificate_key\" is defined " | |
4882 "for certificate \"%V\"", &glcf->ssl_certificate); | |
4883 return NGX_ERROR; | |
4884 } | |
4885 | |
4886 if (ngx_ssl_certificate(cf, glcf->upstream.ssl, &glcf->ssl_certificate, | |
4887 &glcf->ssl_certificate_key, glcf->ssl_passwords) | |
4888 != NGX_OK) | |
4889 { | |
4890 return NGX_ERROR; | |
4891 } | |
4892 } | |
4893 | |
4894 if (ngx_ssl_ciphers(cf, glcf->upstream.ssl, &glcf->ssl_ciphers, 0) | |
4895 != NGX_OK) | |
4896 { | |
4897 return NGX_ERROR; | |
4898 } | |
4899 | |
4900 if (glcf->upstream.ssl_verify) { | |
4901 if (glcf->ssl_trusted_certificate.len == 0) { | |
4902 ngx_log_error(NGX_LOG_EMERG, cf->log, 0, | |
4903 "no grpc_ssl_trusted_certificate for grpc_ssl_verify"); | |
4904 return NGX_ERROR; | |
4905 } | |
4906 | |
4907 if (ngx_ssl_trusted_certificate(cf, glcf->upstream.ssl, | |
4908 &glcf->ssl_trusted_certificate, | |
4909 glcf->ssl_verify_depth) | |
4910 != NGX_OK) | |
4911 { | |
4912 return NGX_ERROR; | |
4913 } | |
4914 | |
4915 if (ngx_ssl_crl(cf, glcf->upstream.ssl, &glcf->ssl_crl) != NGX_OK) { | |
4916 return NGX_ERROR; | |
4917 } | |
4918 } | |
4919 | |
7320
696df3ac27ac
SSL: save sessions for upstream peers using a callback function.
Sergey Kandaurov <pluknet@nginx.com>
parents:
7305
diff
changeset
|
4920 if (ngx_ssl_client_session_cache(cf, glcf->upstream.ssl, |
696df3ac27ac
SSL: save sessions for upstream peers using a callback function.
Sergey Kandaurov <pluknet@nginx.com>
parents:
7305
diff
changeset
|
4921 glcf->upstream.ssl_session_reuse) |
696df3ac27ac
SSL: save sessions for upstream peers using a callback function.
Sergey Kandaurov <pluknet@nginx.com>
parents:
7305
diff
changeset
|
4922 != NGX_OK) |
696df3ac27ac
SSL: save sessions for upstream peers using a callback function.
Sergey Kandaurov <pluknet@nginx.com>
parents:
7305
diff
changeset
|
4923 { |
696df3ac27ac
SSL: save sessions for upstream peers using a callback function.
Sergey Kandaurov <pluknet@nginx.com>
parents:
7305
diff
changeset
|
4924 return NGX_ERROR; |
696df3ac27ac
SSL: save sessions for upstream peers using a callback function.
Sergey Kandaurov <pluknet@nginx.com>
parents:
7305
diff
changeset
|
4925 } |
696df3ac27ac
SSL: save sessions for upstream peers using a callback function.
Sergey Kandaurov <pluknet@nginx.com>
parents:
7305
diff
changeset
|
4926 |
7233 | 4927 #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
4928 | |
4929 if (SSL_CTX_set_alpn_protos(glcf->upstream.ssl->ctx, | |
4930 (u_char *) "\x02h2", 3) | |
4931 != 0) | |
4932 { | |
4933 ngx_ssl_error(NGX_LOG_EMERG, cf->log, 0, | |
4934 "SSL_CTX_set_alpn_protos() failed"); | |
4935 return NGX_ERROR; | |
4936 } | |
4937 | |
4938 #endif | |
4939 | |
8183
1a719ee45526
Upstream: proxy_ssl_conf_command and friends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
8181
diff
changeset
|
4940 if (ngx_ssl_conf_commands(cf, glcf->upstream.ssl, glcf->ssl_conf_commands) |
1a719ee45526
Upstream: proxy_ssl_conf_command and friends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
8181
diff
changeset
|
4941 != NGX_OK) |
1a719ee45526
Upstream: proxy_ssl_conf_command and friends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
8181
diff
changeset
|
4942 { |
1a719ee45526
Upstream: proxy_ssl_conf_command and friends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
8181
diff
changeset
|
4943 return NGX_ERROR; |
1a719ee45526
Upstream: proxy_ssl_conf_command and friends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
8181
diff
changeset
|
4944 } |
1a719ee45526
Upstream: proxy_ssl_conf_command and friends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
8181
diff
changeset
|
4945 |
7233 | 4946 return NGX_OK; |
4947 } | |
4948 | |
4949 #endif |