Skip to content

Commit 2bf28ad

Browse files
committed
updated REAMDE.md & patch file: support nginx 1.18.0
1 parent 2468003 commit 2bf28ad

File tree

2 files changed

+307
-1
lines changed

2 files changed

+307
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ Select patch
178178
| 1.13.x ~ 1.14.x | YES | [proxy_connect_rewrite_1014.patch](patch/proxy_connect_rewrite_1014.patch) |
179179
| 1.15.2 | YES | [proxy_connect_rewrite_1015.patch](patch/proxy_connect_rewrite_1015.patch) |
180180
| 1.15.4 ~ 1.16.x | YES | [proxy_connect_rewrite_101504.patch](patch/proxy_connect_rewrite_101504.patch) |
181-
| 1.17.x | YES | [proxy_connect_rewrite_101504.patch](patch/proxy_connect_rewrite_101504.patch) |
181+
| 1.17.x ~ 1.18.0 | YES | [proxy_connect_rewrite_1018.patch](patch/proxy_connect_rewrite_1018.patch) |
182182

183183
| OpenResty version | enable REWRITE phase | patch |
184184
| --: | --: | --: |
Lines changed: 306 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,306 @@
1+
diff --git a/src/http/ngx_http_core_module.c b/src/http/ngx_http_core_module.c
2+
index 3671558..1d3ae77 100644
3+
--- a/src/http/ngx_http_core_module.c
4+
+++ b/src/http/ngx_http_core_module.c
5+
@@ -950,6 +950,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 cfc42f9..02a4877 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+
@@ -246,6 +254,13 @@ ngx_http_parse_request_line(ngx_http_request_t *r, ngx_buf_t *b)
40+
r->method = NGX_HTTP_OPTIONS;
41+
}
42+
43+
+#if (NGX_HTTP_PROXY_CONNECT)
44+
+ if (ngx_str7_cmp(m, 'C', 'O', 'N', 'N', 'E', 'C', 'T', ' '))
45+
+ {
46+
+ r->method = NGX_HTTP_CONNECT;
47+
+ }
48+
+#endif
49+
+
50+
break;
51+
52+
case 8:
53+
@@ -267,6 +282,13 @@ ngx_http_parse_request_line(ngx_http_request_t *r, ngx_buf_t *b)
54+
}
55+
56+
state = sw_spaces_before_uri;
57+
+
58+
+#if (NGX_HTTP_PROXY_CONNECT)
59+
+ if (r->method == NGX_HTTP_CONNECT) {
60+
+ state = sw_spaces_before_connect_host;
61+
+ }
62+
+#endif
63+
+
64+
break;
65+
}
66+
67+
@@ -276,6 +298,111 @@ ngx_http_parse_request_line(ngx_http_request_t *r, ngx_buf_t *b)
68+
69+
break;
70+
71+
+#if (NGX_HTTP_PROXY_CONNECT)
72+
+ case sw_spaces_before_connect_host:
73+
+
74+
+ if (ch == ' ') {
75+
+ break;
76+
+ }
77+
+
78+
+ /* fall through */
79+
+
80+
+ case sw_connect_host_start:
81+
+
82+
+ r->connect_host_start = p;
83+
+
84+
+ if (ch == '[') {
85+
+ state = sw_connect_host_ip_literal;
86+
+ break;
87+
+ }
88+
+
89+
+ state = sw_connect_host;
90+
+
91+
+ /* fall through */
92+
+
93+
+ case sw_connect_host:
94+
+
95+
+ c = (u_char) (ch | 0x20);
96+
+ if (c >= 'a' && c <= 'z') {
97+
+ break;
98+
+ }
99+
+
100+
+ if ((ch >= '0' && ch <= '9') || ch == '.' || ch == '-') {
101+
+ break;
102+
+ }
103+
+
104+
+ /* fall through */
105+
+
106+
+ case sw_connect_host_end:
107+
+
108+
+ r->connect_host_end = p;
109+
+
110+
+ switch (ch) {
111+
+ case ':':
112+
+ state = sw_connect_port;
113+
+ break;
114+
+ default:
115+
+ return NGX_HTTP_PARSE_INVALID_REQUEST;
116+
+ }
117+
+ break;
118+
+
119+
+ case sw_connect_host_ip_literal:
120+
+
121+
+ if (ch >= '0' && ch <= '9') {
122+
+ break;
123+
+ }
124+
+
125+
+ c = (u_char) (ch | 0x20);
126+
+ if (c >= 'a' && c <= 'z') {
127+
+ break;
128+
+ }
129+
+
130+
+ switch (ch) {
131+
+ case ':':
132+
+ break;
133+
+ case ']':
134+
+ state = sw_connect_host_end;
135+
+ break;
136+
+ case '-':
137+
+ case '.':
138+
+ case '_':
139+
+ case '~':
140+
+ /* unreserved */
141+
+ break;
142+
+ case '!':
143+
+ case '$':
144+
+ case '&':
145+
+ case '\'':
146+
+ case '(':
147+
+ case ')':
148+
+ case '*':
149+
+ case '+':
150+
+ case ',':
151+
+ case ';':
152+
+ case '=':
153+
+ /* sub-delims */
154+
+ break;
155+
+ default:
156+
+ return NGX_HTTP_PARSE_INVALID_REQUEST;
157+
+ }
158+
+ break;
159+
+
160+
+ case sw_connect_port:
161+
+ if (ch >= '0' && ch <= '9') {
162+
+ break;
163+
+ }
164+
+
165+
+ switch (ch) {
166+
+ case ' ':
167+
+ r->connect_port_end = p;
168+
+ state = sw_host_http_09;
169+
+ break;
170+
+ default:
171+
+ return NGX_HTTP_PARSE_INVALID_REQUEST;
172+
+ }
173+
+ break;
174+
+#endif
175+
+
176+
/* space* before URI */
177+
case sw_spaces_before_uri:
178+
179+
diff --git a/src/http/ngx_http_request.c b/src/http/ngx_http_request.c
180+
index eb53996..a7a5263 100644
181+
--- a/src/http/ngx_http_request.c
182+
+++ b/src/http/ngx_http_request.c
183+
@@ -1092,6 +1092,53 @@ ngx_http_process_request_line(ngx_event_t *rev)
184+
break;
185+
}
186+
187+
+#if (NGX_HTTP_PROXY_CONNECT)
188+
+
189+
+ if (r->connect_host_start && r->connect_host_end) {
190+
+
191+
+ host.len = r->connect_host_end - r->connect_host_start;
192+
+ host.data = r->connect_host_start;
193+
+ rc = ngx_http_validate_host(&host, r->pool, 0);
194+
+
195+
+ if (rc == NGX_DECLINED) {
196+
+ ngx_log_error(NGX_LOG_INFO, c->log, 0,
197+
+ "client sent invalid host in request line");
198+
+ ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
199+
+ return;
200+
+ }
201+
+
202+
+ if (rc == NGX_ERROR) {
203+
+ ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
204+
+ return;
205+
+ }
206+
+
207+
+ r->connect_host = host;
208+
+
209+
+ if (!r->connect_port_end) {
210+
+ ngx_log_error(NGX_LOG_INFO, c->log, 0,
211+
+ "client sent no port in request line");
212+
+ ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
213+
+ return;
214+
+ }
215+
+
216+
+ r->connect_port.data = r->connect_host_end + 1;
217+
+ r->connect_port.len = r->connect_port_end
218+
+ - r->connect_host_end - 1;
219+
+
220+
+ ngx_int_t port;
221+
+
222+
+ port = ngx_atoi(r->connect_port.data, r->connect_port.len);
223+
+ if (port == NGX_ERROR || port < 1 || port > 65535) {
224+
+ ngx_log_error(NGX_LOG_INFO, c->log, 0,
225+
+ "client sent invalid port in request line");
226+
+ ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
227+
+ return;
228+
+ }
229+
+
230+
+ r->connect_port_n = port;
231+
+ }
232+
+#endif
233+
+
234+
if (r->schema_end) {
235+
r->schema.len = r->schema_end - r->schema_start;
236+
r->schema.data = r->schema_start;
237+
@@ -1671,6 +1718,19 @@ ngx_http_alloc_large_header_buffer(ngx_http_request_t *r,
238+
r->schema_end = new + (r->schema_end - old);
239+
}
240+
241+
+#if (NGX_HTTP_PROXY_CONNECT)
242+
+ if (r->connect_host_start) {
243+
+ r->connect_host_start = new + (r->connect_host_start - old);
244+
+ if (r->connect_host_end) {
245+
+ r->connect_host_end = new + (r->connect_host_end - old);
246+
+ }
247+
+
248+
+ if (r->connect_port_end) {
249+
+ r->connect_port_end = new + (r->connect_port_end - old);
250+
+ }
251+
+ }
252+
+#endif
253+
+
254+
if (r->host_start) {
255+
r->host_start = new + (r->host_start - old);
256+
if (r->host_end) {
257+
diff --git a/src/http/ngx_http_request.h b/src/http/ngx_http_request.h
258+
index 70c2d42..90f0934 100644
259+
--- a/src/http/ngx_http_request.h
260+
+++ b/src/http/ngx_http_request.h
261+
@@ -42,6 +42,10 @@
262+
#define NGX_HTTP_PATCH 0x4000
263+
#define NGX_HTTP_TRACE 0x8000
264+
265+
+#if (NGX_HTTP_PROXY_CONNECT)
266+
+#define NGX_HTTP_CONNECT 0x10000
267+
+#endif
268+
+
269+
#define NGX_HTTP_CONNECTION_CLOSE 1
270+
#define NGX_HTTP_CONNECTION_KEEP_ALIVE 2
271+
272+
@@ -410,6 +414,15 @@ struct ngx_http_request_s {
273+
ngx_str_t exten;
274+
ngx_str_t unparsed_uri;
275+
276+
+#if (NGX_HTTP_PROXY_CONNECT)
277+
+ ngx_str_t connect_host;
278+
+ ngx_str_t connect_port;
279+
+ in_port_t connect_port_n;
280+
+ u_char *connect_host_start;
281+
+ u_char *connect_host_end;
282+
+ u_char *connect_port_end;
283+
+#endif
284+
+
285+
ngx_str_t method_name;
286+
ngx_str_t http_protocol;
287+
ngx_str_t schema;
288+
diff --git a/src/http/ngx_http_variables.c b/src/http/ngx_http_variables.c
289+
index e067cf0..4bb9260 100644
290+
--- a/src/http/ngx_http_variables.c
291+
+++ b/src/http/ngx_http_variables.c
292+
@@ -161,6 +161,14 @@ static ngx_int_t ngx_http_variable_time_local(ngx_http_request_t *r,
293+
294+
static ngx_http_variable_t ngx_http_core_variables[] = {
295+
296+
+#if (NGX_HTTP_PROXY_CONNECT)
297+
+ { ngx_string("connect_host"), NULL, ngx_http_variable_request,
298+
+ offsetof(ngx_http_request_t, connect_host), 0, 0 },
299+
+
300+
+ { ngx_string("connect_port"), NULL, ngx_http_variable_request,
301+
+ offsetof(ngx_http_request_t, connect_port), 0, 0 },
302+
+#endif
303+
+
304+
{ ngx_string("http_host"), NULL, ngx_http_variable_header,
305+
offsetof(ngx_http_request_t, headers_in.host), 0, 0 },
306+

0 commit comments

Comments
 (0)