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