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