Skip to content

Commit 92f09d5

Browse files
committed
support nginx-1.21.1+
1 parent cb4dcd5 commit 92f09d5

File tree

1 file changed

+281
-0
lines changed

1 file changed

+281
-0
lines changed
Lines changed: 281 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,281 @@
1+
diff --git a/src/http/ngx_http_core_module.c b/src/http/ngx_http_core_module.c
2+
index bad43ea5..b2cebb38 100644
3+
--- a/src/http/ngx_http_core_module.c
4+
+++ b/src/http/ngx_http_core_module.c
5+
@@ -957,6 +957,14 @@ ngx_http_core_find_config_phase(ngx_http_request_t *r,
6+
r->content_handler = NULL;
7+
r->uri_changed = 0;
8+
9+
+#if (NGX_HTTP_PROXY_CONNECT)
10+
+ if (r->method == NGX_HTTP_CONNECT) {
11+
+ ngx_http_update_location_config(r);
12+
+ r->phase_handler++;
13+
+ return NGX_AGAIN;
14+
+ }
15+
+#endif
16+
+
17+
rc = ngx_http_core_find_location(r);
18+
19+
if (rc == NGX_ERROR) {
20+
diff --git a/src/http/ngx_http_parse.c b/src/http/ngx_http_parse.c
21+
index 6460da29..7b209fa8 100644
22+
--- a/src/http/ngx_http_parse.c
23+
+++ b/src/http/ngx_http_parse.c
24+
@@ -107,6 +107,14 @@ ngx_http_parse_request_line(ngx_http_request_t *r, ngx_buf_t *b)
25+
enum {
26+
sw_start = 0,
27+
sw_method,
28+
+#if (NGX_HTTP_PROXY_CONNECT)
29+
+ sw_spaces_before_connect_host,
30+
+ sw_connect_host_start,
31+
+ sw_connect_host,
32+
+ sw_connect_host_end,
33+
+ sw_connect_host_ip_literal,
34+
+ sw_connect_port,
35+
+#endif
36+
sw_spaces_before_uri,
37+
sw_schema,
38+
sw_schema_slash,
39+
@@ -270,6 +278,13 @@ ngx_http_parse_request_line(ngx_http_request_t *r, ngx_buf_t *b)
40+
}
41+
42+
state = sw_spaces_before_uri;
43+
+
44+
+#if (NGX_HTTP_PROXY_CONNECT)
45+
+ if (r->method == NGX_HTTP_CONNECT) {
46+
+ state = sw_spaces_before_connect_host;
47+
+ }
48+
+#endif
49+
+
50+
break;
51+
}
52+
53+
@@ -279,6 +294,111 @@ ngx_http_parse_request_line(ngx_http_request_t *r, ngx_buf_t *b)
54+
55+
break;
56+
57+
+#if (NGX_HTTP_PROXY_CONNECT)
58+
+ case sw_spaces_before_connect_host:
59+
+
60+
+ if (ch == ' ') {
61+
+ break;
62+
+ }
63+
+
64+
+ /* fall through */
65+
+
66+
+ case sw_connect_host_start:
67+
+
68+
+ r->connect_host_start = p;
69+
+
70+
+ if (ch == '[') {
71+
+ state = sw_connect_host_ip_literal;
72+
+ break;
73+
+ }
74+
+
75+
+ state = sw_connect_host;
76+
+
77+
+ /* fall through */
78+
+
79+
+ case sw_connect_host:
80+
+
81+
+ c = (u_char) (ch | 0x20);
82+
+ if (c >= 'a' && c <= 'z') {
83+
+ break;
84+
+ }
85+
+
86+
+ if ((ch >= '0' && ch <= '9') || ch == '.' || ch == '-') {
87+
+ break;
88+
+ }
89+
+
90+
+ /* fall through */
91+
+
92+
+ case sw_connect_host_end:
93+
+
94+
+ r->connect_host_end = p;
95+
+
96+
+ switch (ch) {
97+
+ case ':':
98+
+ state = sw_connect_port;
99+
+ break;
100+
+ default:
101+
+ return NGX_HTTP_PARSE_INVALID_REQUEST;
102+
+ }
103+
+ break;
104+
+
105+
+ case sw_connect_host_ip_literal:
106+
+
107+
+ if (ch >= '0' && ch <= '9') {
108+
+ break;
109+
+ }
110+
+
111+
+ c = (u_char) (ch | 0x20);
112+
+ if (c >= 'a' && c <= 'z') {
113+
+ break;
114+
+ }
115+
+
116+
+ switch (ch) {
117+
+ case ':':
118+
+ break;
119+
+ case ']':
120+
+ state = sw_connect_host_end;
121+
+ break;
122+
+ case '-':
123+
+ case '.':
124+
+ case '_':
125+
+ case '~':
126+
+ /* unreserved */
127+
+ break;
128+
+ case '!':
129+
+ case '$':
130+
+ case '&':
131+
+ case '\'':
132+
+ case '(':
133+
+ case ')':
134+
+ case '*':
135+
+ case '+':
136+
+ case ',':
137+
+ case ';':
138+
+ case '=':
139+
+ /* sub-delims */
140+
+ break;
141+
+ default:
142+
+ return NGX_HTTP_PARSE_INVALID_REQUEST;
143+
+ }
144+
+ break;
145+
+
146+
+ case sw_connect_port:
147+
+ if (ch >= '0' && ch <= '9') {
148+
+ break;
149+
+ }
150+
+
151+
+ switch (ch) {
152+
+ case ' ':
153+
+ r->connect_port_end = p;
154+
+ state = sw_http_09;
155+
+ break;
156+
+ default:
157+
+ return NGX_HTTP_PARSE_INVALID_REQUEST;
158+
+ }
159+
+ break;
160+
+#endif
161+
+
162+
/* space* before URI */
163+
case sw_spaces_before_uri:
164+
165+
diff --git a/src/http/ngx_http_request.c b/src/http/ngx_http_request.c
166+
index 2d1845d0..c65dd9f1 100644
167+
--- a/src/http/ngx_http_request.c
168+
+++ b/src/http/ngx_http_request.c
169+
@@ -1118,6 +1118,53 @@ ngx_http_process_request_line(ngx_event_t *rev)
170+
break;
171+
}
172+
173+
+#if (NGX_HTTP_PROXY_CONNECT)
174+
+
175+
+ if (r->connect_host_start && r->connect_host_end) {
176+
+
177+
+ host.len = r->connect_host_end - r->connect_host_start;
178+
+ host.data = r->connect_host_start;
179+
+ rc = ngx_http_validate_host(&host, r->pool, 0);
180+
+
181+
+ if (rc == NGX_DECLINED) {
182+
+ ngx_log_error(NGX_LOG_INFO, c->log, 0,
183+
+ "client sent invalid host in request line");
184+
+ ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
185+
+ return;
186+
+ }
187+
+
188+
+ if (rc == NGX_ERROR) {
189+
+ ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
190+
+ return;
191+
+ }
192+
+
193+
+ r->connect_host = host;
194+
+
195+
+ if (!r->connect_port_end) {
196+
+ ngx_log_error(NGX_LOG_INFO, c->log, 0,
197+
+ "client sent no port in request line");
198+
+ ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
199+
+ return;
200+
+ }
201+
+
202+
+ r->connect_port.data = r->connect_host_end + 1;
203+
+ r->connect_port.len = r->connect_port_end
204+
+ - r->connect_host_end - 1;
205+
+
206+
+ ngx_int_t port;
207+
+
208+
+ port = ngx_atoi(r->connect_port.data, r->connect_port.len);
209+
+ if (port == NGX_ERROR || port < 1 || port > 65535) {
210+
+ ngx_log_error(NGX_LOG_INFO, c->log, 0,
211+
+ "client sent invalid port in request line");
212+
+ ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
213+
+ return;
214+
+ }
215+
+
216+
+ r->connect_port_n = port;
217+
+ }
218+
+#endif
219+
+
220+
if (r->schema_end) {
221+
r->schema.len = r->schema_end - r->schema_start;
222+
r->schema.data = r->schema_start;
223+
@@ -1709,6 +1756,19 @@ ngx_http_alloc_large_header_buffer(ngx_http_request_t *r,
224+
r->schema_end = new + (r->schema_end - old);
225+
}
226+
227+
+#if (NGX_HTTP_PROXY_CONNECT)
228+
+ if (r->connect_host_start) {
229+
+ r->connect_host_start = new + (r->connect_host_start - old);
230+
+ if (r->connect_host_end) {
231+
+ r->connect_host_end = new + (r->connect_host_end - old);
232+
+ }
233+
+
234+
+ if (r->connect_port_end) {
235+
+ r->connect_port_end = new + (r->connect_port_end - old);
236+
+ }
237+
+ }
238+
+#endif
239+
+
240+
if (r->host_start) {
241+
r->host_start = new + (r->host_start - old);
242+
if (r->host_end) {
243+
diff --git a/src/http/ngx_http_request.h b/src/http/ngx_http_request.h
244+
index 63576274..cac53b68 100644
245+
--- a/src/http/ngx_http_request.h
246+
+++ b/src/http/ngx_http_request.h
247+
@@ -411,6 +411,15 @@ struct ngx_http_request_s {
248+
ngx_str_t exten;
249+
ngx_str_t unparsed_uri;
250+
251+
+#if (NGX_HTTP_PROXY_CONNECT)
252+
+ ngx_str_t connect_host;
253+
+ ngx_str_t connect_port;
254+
+ in_port_t connect_port_n;
255+
+ u_char *connect_host_start;
256+
+ u_char *connect_host_end;
257+
+ u_char *connect_port_end;
258+
+#endif
259+
+
260+
ngx_str_t method_name;
261+
ngx_str_t http_protocol;
262+
ngx_str_t schema;
263+
diff --git a/src/http/ngx_http_variables.c b/src/http/ngx_http_variables.c
264+
index c25d80cc..8bb40d70 100644
265+
--- a/src/http/ngx_http_variables.c
266+
+++ b/src/http/ngx_http_variables.c
267+
@@ -163,6 +163,14 @@ static ngx_int_t ngx_http_variable_time_local(ngx_http_request_t *r,
268+
269+
static ngx_http_variable_t ngx_http_core_variables[] = {
270+
271+
+#if (NGX_HTTP_PROXY_CONNECT)
272+
+ { ngx_string("connect_host"), NULL, ngx_http_variable_request,
273+
+ offsetof(ngx_http_request_t, connect_host), 0, 0 },
274+
+
275+
+ { ngx_string("connect_port"), NULL, ngx_http_variable_request,
276+
+ offsetof(ngx_http_request_t, connect_port), 0, 0 },
277+
+#endif
278+
+
279+
{ ngx_string("http_host"), NULL, ngx_http_variable_header,
280+
offsetof(ngx_http_request_t, headers_in.host), 0, 0 },
281+

0 commit comments

Comments
 (0)