Merge lp:ubuntu/raring-security/nginx into lp:ubuntu/raring/nginx
- Raring (13.04)
- raring-security
- Merge into raring
Status: | Needs review |
---|---|
Proposed branch: | lp:ubuntu/raring-security/nginx |
Merge into: | lp:ubuntu/raring/nginx |
Diff against target: |
6045 lines (+5938/-7) 11 files modified
.pc/applied-patches (+2/-0) .pc/cve-2013-2070.patch/src/http/modules/ngx_http_proxy_module.c (+4038/-0) .pc/cve-2013-4547.patch/src/http/ngx_http_parse.c (+1820/-0) debian/changelog (+24/-0) debian/patches/cve-2013-2070.patch (+18/-0) debian/patches/cve-2013-4547.patch (+21/-0) debian/patches/series (+2/-0) debian/patches/ubuntu-branding.patch (+5/-5) src/core/nginx.h (+2/-2) src/http/modules/ngx_http_proxy_module.c (+4/-0) src/http/ngx_http_parse.c (+2/-0) |
To merge this branch: | bzr merge lp:ubuntu/raring-security/nginx |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Roger Mbiama Assogo (community) | Approve | ||
Review via email: mp+167366@code.launchpad.net |
Commit message
Description of the change
RUN: /usr/share/
Forking launchpad-buildd slave process...
Kernel version: 2.6.24-32-xen #1 SMP Mon Dec 3 16:12:25 UTC 2012 x86_64
Buildd toolchain package versions: launchpad-
Syncing the system clock with the buildd NTP service...
4 Jun 18:14:27 ntpdate[3371]: adjust time server 10.211.37.1 offset -0.007500 sec
RUN: /usr/share/
Uncompressing the tarball...
Unpacking chroot for build ef036a27999f1de
RUN: /usr/share/
Mounting chroot for build ef036a27999f1de
RUN: /usr/share/
Overriding sources.list in build-ef036a279
RUN: /usr/share/
Updating debian chroot for build ef036a27999f1de
Get:1 http://
Get:2 http://
Get:3 http://
Get:4 http://
Get:5 http://
Get:6 http://
Get:7 http://
Get:8 http://
Get:9 http://
Get:10 http://
Get:11 http://
Get:12 http://
Fetched 11.2 MB in 6s (1719 kB/s)
Reading package lists...
Reading package lists...
Building dependency tree...
Reading state information...
The following packages will be upgraded:
libgnutls26 libudev1 linux-libc-dev
3 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
Need to get 1401 kB of archives.
After this operation, 9216 B disk space will be freed.
WARNING: The following packages cannot be authenticated!
libudev1 libgnutls26 linux-libc-dev
Authentication warning overridden.
Get:1 http://
Get:2 http://
Get:3 http://
debconf: delaying package configuration, since apt-utils is not installed
Fetched 1401 kB in 0s (11.3 MB/s)
(Reading database ... 12143 files and directories currently installed.)
Preparing to replace libudev1:i386 198-0ubuntu10 (using .../libudev1_
Unpacking replacement libudev1:i386 ...
Preparing to replace libgnutls26:i386 2.12.23-1ubuntu1 (using .../libgnutls26
Unpacking replacement libgnutls26:i386 ...
Preparing to replace linux-libc-dev:i386 3.8.0-19.29 (using .../linux-
Unpacking replacement linux-libc-dev:i386 ...
Setting up libudev1:i386 (198-0ubuntu11) ...
Setting up libgnutls26:i386 (2.12.23-
Setting up linux-libc-dev:i386 (3.8.0-23.34) ...
Processing triggers for libc-bin ...
ldconfig deferred processing now taking place
RUN: /usr/share/
Bazaar versions:
Bazaar (bzr) 2.4.0
Python interpreter: /usr/bin/python 2.5.2
Python standard library: /usr/lib/python2.5
Platform: Linux-2.
bzrlib: /usr/lib/
Bazaar configuration: /home/buildd/
Bazaar log file: /home/buildd/
Copyright 2005-2011 Canonical Ltd.
http://
bzr comes with ABSOLUTELY NO WARRANTY. bzr is free software, and
you may use, modify and redistribute it under the terms of the GNU
General Public License version 2 or later.
Bazaar is part of the GNU Project to produce a free operating system.
bash_completion 2.4.0
Generate a shell function for bash command line completion.
builder 0.7.2dev
The bzr-builder plugin allows you to construct a branch from a 'recipe'.
changelog_merge 2.4.0
Merge hook for GNU-format ChangeLog files
launchpad 2.4.0
Launchpad.net integration plugin for Bazaar.
netrc_credentia
Use ~/.netrc as a credential store for authentication.
news_merge 2.4.0
Merge hook for bzr's NEWS file.
weave_fmt 2.4.0
Weave formats.
Building recipe:
# bzr-builder format 0.3 deb-version {debupstream}
lp:~mbiama/mailman/trunk
RUN ['bzr', '-Derror', 'dailydeb', '--safe', '--no-build', '/home/
You have not informed bzr of your Launchpad ID, and you must do this to
write to Launchpad or access private data. See "bzr help launchpad-login".
bzr: ERROR: bzrlib.
Traceback (most recent call last):
File "/usr/lib/
return the_callable(*args, **kwargs)
File "/usr/lib/
ret = run(*run_argv)
File "/usr/lib/
return self.run(
File "/usr/lib/
return self._operation
File "/usr/lib/
self.cleanups, self.func, *args, **kwargs)
File "/usr/lib/
result = func(*args, **kwargs)
File "/usr/lib/
possible_
File "/usr/lib/
changed = resolve_
File "/usr/lib/
if_
File "/usr/lib/
new_
File "/usr/lib/
possible_
File "/usr/lib/
return BzrDir.
File "/usr/lib/
redirected)
File "/usr/lib/
return obj(*args, **kwargs)
File "/usr/lib/
return action(transport)
File "/usr/lib/
transport, _server_
File "/usr/lib/
raise errors.
NotBranchError: Not a branch: "http://
(0.076004000000
RUN: /usr/share/
Scanning for processes to kill in build /home/buildd/
RUN: /usr/share/
Unmounting chroot for build ef036a27999f1de
RUN: /usr/share/
Removing build ef036a27999f1de
- 64. By Thomas Ward
-
* SECURITY UPDATE: ACL bypass via space character (LP: #1253691)
- debian/patches/ cve-2013- 4547.patch: modify src/http/ ngx_http_ parse.c
to account for a space character, fixing an issue which could result in
security restrictions being bypassed
- CVE-2013-4547
Unmerged revisions
- 64. By Thomas Ward
-
* SECURITY UPDATE: ACL bypass via space character (LP: #1253691)
- debian/patches/ cve-2013- 4547.patch: modify src/http/ ngx_http_ parse.c
to account for a space character, fixing an issue which could result in
security restrictions being bypassed
- CVE-2013-4547 - 63. By Thomas Ward
-
* Security update (closes LP: #1182586):
* Patch to fix a buffer overflow vulnerability (CVE-2013-2070)
Preview Diff
1 | === modified file '.pc/applied-patches' | |||
2 | --- .pc/applied-patches 2013-04-09 10:23:26 +0000 | |||
3 | +++ .pc/applied-patches 2014-01-15 15:02:58 +0000 | |||
4 | @@ -1,2 +1,4 @@ | |||
5 | 1 | perl-use-dpkg-buildflags.patch | 1 | perl-use-dpkg-buildflags.patch |
6 | 2 | ubuntu-branding.patch | 2 | ubuntu-branding.patch |
7 | 3 | cve-2013-2070.patch | ||
8 | 4 | cve-2013-4547.patch | ||
9 | 3 | 5 | ||
10 | === added directory '.pc/cve-2013-2070.patch' | |||
11 | === added directory '.pc/cve-2013-2070.patch/src' | |||
12 | === added directory '.pc/cve-2013-2070.patch/src/http' | |||
13 | === added directory '.pc/cve-2013-2070.patch/src/http/modules' | |||
14 | === added file '.pc/cve-2013-2070.patch/src/http/modules/ngx_http_proxy_module.c' | |||
15 | --- .pc/cve-2013-2070.patch/src/http/modules/ngx_http_proxy_module.c 1970-01-01 00:00:00 +0000 | |||
16 | +++ .pc/cve-2013-2070.patch/src/http/modules/ngx_http_proxy_module.c 2014-01-15 15:02:58 +0000 | |||
17 | @@ -0,0 +1,4038 @@ | |||
18 | 1 | |||
19 | 2 | /* | ||
20 | 3 | * Copyright (C) Igor Sysoev | ||
21 | 4 | * Copyright (C) Nginx, Inc. | ||
22 | 5 | */ | ||
23 | 6 | |||
24 | 7 | |||
25 | 8 | #include <ngx_config.h> | ||
26 | 9 | #include <ngx_core.h> | ||
27 | 10 | #include <ngx_http.h> | ||
28 | 11 | |||
29 | 12 | |||
30 | 13 | typedef struct ngx_http_proxy_rewrite_s ngx_http_proxy_rewrite_t; | ||
31 | 14 | |||
32 | 15 | typedef ngx_int_t (*ngx_http_proxy_rewrite_pt)(ngx_http_request_t *r, | ||
33 | 16 | ngx_table_elt_t *h, size_t prefix, size_t len, | ||
34 | 17 | ngx_http_proxy_rewrite_t *pr); | ||
35 | 18 | |||
36 | 19 | struct ngx_http_proxy_rewrite_s { | ||
37 | 20 | ngx_http_proxy_rewrite_pt handler; | ||
38 | 21 | |||
39 | 22 | union { | ||
40 | 23 | ngx_http_complex_value_t complex; | ||
41 | 24 | #if (NGX_PCRE) | ||
42 | 25 | ngx_http_regex_t *regex; | ||
43 | 26 | #endif | ||
44 | 27 | } pattern; | ||
45 | 28 | |||
46 | 29 | ngx_http_complex_value_t replacement; | ||
47 | 30 | }; | ||
48 | 31 | |||
49 | 32 | |||
50 | 33 | typedef struct { | ||
51 | 34 | ngx_str_t key_start; | ||
52 | 35 | ngx_str_t schema; | ||
53 | 36 | ngx_str_t host_header; | ||
54 | 37 | ngx_str_t port; | ||
55 | 38 | ngx_str_t uri; | ||
56 | 39 | } ngx_http_proxy_vars_t; | ||
57 | 40 | |||
58 | 41 | |||
59 | 42 | typedef struct { | ||
60 | 43 | ngx_http_upstream_conf_t upstream; | ||
61 | 44 | |||
62 | 45 | ngx_array_t *flushes; | ||
63 | 46 | ngx_array_t *body_set_len; | ||
64 | 47 | ngx_array_t *body_set; | ||
65 | 48 | ngx_array_t *headers_set_len; | ||
66 | 49 | ngx_array_t *headers_set; | ||
67 | 50 | ngx_hash_t headers_set_hash; | ||
68 | 51 | |||
69 | 52 | ngx_array_t *headers_source; | ||
70 | 53 | |||
71 | 54 | ngx_array_t *proxy_lengths; | ||
72 | 55 | ngx_array_t *proxy_values; | ||
73 | 56 | |||
74 | 57 | ngx_array_t *redirects; | ||
75 | 58 | ngx_array_t *cookie_domains; | ||
76 | 59 | ngx_array_t *cookie_paths; | ||
77 | 60 | |||
78 | 61 | ngx_str_t body_source; | ||
79 | 62 | |||
80 | 63 | ngx_str_t method; | ||
81 | 64 | ngx_str_t location; | ||
82 | 65 | ngx_str_t url; | ||
83 | 66 | |||
84 | 67 | #if (NGX_HTTP_CACHE) | ||
85 | 68 | ngx_http_complex_value_t cache_key; | ||
86 | 69 | #endif | ||
87 | 70 | |||
88 | 71 | ngx_http_proxy_vars_t vars; | ||
89 | 72 | |||
90 | 73 | ngx_flag_t redirect; | ||
91 | 74 | |||
92 | 75 | ngx_uint_t http_version; | ||
93 | 76 | |||
94 | 77 | ngx_uint_t headers_hash_max_size; | ||
95 | 78 | ngx_uint_t headers_hash_bucket_size; | ||
96 | 79 | } ngx_http_proxy_loc_conf_t; | ||
97 | 80 | |||
98 | 81 | |||
99 | 82 | typedef struct { | ||
100 | 83 | ngx_http_status_t status; | ||
101 | 84 | ngx_http_proxy_vars_t vars; | ||
102 | 85 | size_t internal_body_length; | ||
103 | 86 | |||
104 | 87 | ngx_uint_t state; | ||
105 | 88 | off_t size; | ||
106 | 89 | off_t length; | ||
107 | 90 | |||
108 | 91 | ngx_uint_t head; /* unsigned head:1 */ | ||
109 | 92 | } ngx_http_proxy_ctx_t; | ||
110 | 93 | |||
111 | 94 | |||
112 | 95 | static ngx_int_t ngx_http_proxy_eval(ngx_http_request_t *r, | ||
113 | 96 | ngx_http_proxy_ctx_t *ctx, ngx_http_proxy_loc_conf_t *plcf); | ||
114 | 97 | #if (NGX_HTTP_CACHE) | ||
115 | 98 | static ngx_int_t ngx_http_proxy_create_key(ngx_http_request_t *r); | ||
116 | 99 | #endif | ||
117 | 100 | static ngx_int_t ngx_http_proxy_create_request(ngx_http_request_t *r); | ||
118 | 101 | static ngx_int_t ngx_http_proxy_reinit_request(ngx_http_request_t *r); | ||
119 | 102 | static ngx_int_t ngx_http_proxy_process_status_line(ngx_http_request_t *r); | ||
120 | 103 | static ngx_int_t ngx_http_proxy_process_header(ngx_http_request_t *r); | ||
121 | 104 | static ngx_int_t ngx_http_proxy_input_filter_init(void *data); | ||
122 | 105 | static ngx_int_t ngx_http_proxy_copy_filter(ngx_event_pipe_t *p, | ||
123 | 106 | ngx_buf_t *buf); | ||
124 | 107 | static ngx_int_t ngx_http_proxy_chunked_filter(ngx_event_pipe_t *p, | ||
125 | 108 | ngx_buf_t *buf); | ||
126 | 109 | static ngx_int_t ngx_http_proxy_non_buffered_copy_filter(void *data, | ||
127 | 110 | ssize_t bytes); | ||
128 | 111 | static ngx_int_t ngx_http_proxy_non_buffered_chunked_filter(void *data, | ||
129 | 112 | ssize_t bytes); | ||
130 | 113 | static void ngx_http_proxy_abort_request(ngx_http_request_t *r); | ||
131 | 114 | static void ngx_http_proxy_finalize_request(ngx_http_request_t *r, | ||
132 | 115 | ngx_int_t rc); | ||
133 | 116 | |||
134 | 117 | static ngx_int_t ngx_http_proxy_host_variable(ngx_http_request_t *r, | ||
135 | 118 | ngx_http_variable_value_t *v, uintptr_t data); | ||
136 | 119 | static ngx_int_t ngx_http_proxy_port_variable(ngx_http_request_t *r, | ||
137 | 120 | ngx_http_variable_value_t *v, uintptr_t data); | ||
138 | 121 | static ngx_int_t | ||
139 | 122 | ngx_http_proxy_add_x_forwarded_for_variable(ngx_http_request_t *r, | ||
140 | 123 | ngx_http_variable_value_t *v, uintptr_t data); | ||
141 | 124 | static ngx_int_t | ||
142 | 125 | ngx_http_proxy_internal_body_length_variable(ngx_http_request_t *r, | ||
143 | 126 | ngx_http_variable_value_t *v, uintptr_t data); | ||
144 | 127 | static ngx_int_t ngx_http_proxy_rewrite_redirect(ngx_http_request_t *r, | ||
145 | 128 | ngx_table_elt_t *h, size_t prefix); | ||
146 | 129 | static ngx_int_t ngx_http_proxy_rewrite_cookie(ngx_http_request_t *r, | ||
147 | 130 | ngx_table_elt_t *h); | ||
148 | 131 | static ngx_int_t ngx_http_proxy_rewrite_cookie_value(ngx_http_request_t *r, | ||
149 | 132 | ngx_table_elt_t *h, u_char *value, ngx_array_t *rewrites); | ||
150 | 133 | static ngx_int_t ngx_http_proxy_rewrite(ngx_http_request_t *r, | ||
151 | 134 | ngx_table_elt_t *h, size_t prefix, size_t len, ngx_str_t *replacement); | ||
152 | 135 | |||
153 | 136 | static ngx_int_t ngx_http_proxy_add_variables(ngx_conf_t *cf); | ||
154 | 137 | static void *ngx_http_proxy_create_loc_conf(ngx_conf_t *cf); | ||
155 | 138 | static char *ngx_http_proxy_merge_loc_conf(ngx_conf_t *cf, | ||
156 | 139 | void *parent, void *child); | ||
157 | 140 | static ngx_int_t ngx_http_proxy_merge_headers(ngx_conf_t *cf, | ||
158 | 141 | ngx_http_proxy_loc_conf_t *conf, ngx_http_proxy_loc_conf_t *prev); | ||
159 | 142 | |||
160 | 143 | static char *ngx_http_proxy_pass(ngx_conf_t *cf, ngx_command_t *cmd, | ||
161 | 144 | void *conf); | ||
162 | 145 | static char *ngx_http_proxy_redirect(ngx_conf_t *cf, ngx_command_t *cmd, | ||
163 | 146 | void *conf); | ||
164 | 147 | static char *ngx_http_proxy_cookie_domain(ngx_conf_t *cf, ngx_command_t *cmd, | ||
165 | 148 | void *conf); | ||
166 | 149 | static char *ngx_http_proxy_cookie_path(ngx_conf_t *cf, ngx_command_t *cmd, | ||
167 | 150 | void *conf); | ||
168 | 151 | static char *ngx_http_proxy_store(ngx_conf_t *cf, ngx_command_t *cmd, | ||
169 | 152 | void *conf); | ||
170 | 153 | #if (NGX_HTTP_CACHE) | ||
171 | 154 | static char *ngx_http_proxy_cache(ngx_conf_t *cf, ngx_command_t *cmd, | ||
172 | 155 | void *conf); | ||
173 | 156 | static char *ngx_http_proxy_cache_key(ngx_conf_t *cf, ngx_command_t *cmd, | ||
174 | 157 | void *conf); | ||
175 | 158 | #endif | ||
176 | 159 | |||
177 | 160 | static char *ngx_http_proxy_lowat_check(ngx_conf_t *cf, void *post, void *data); | ||
178 | 161 | |||
179 | 162 | static ngx_int_t ngx_http_proxy_rewrite_regex(ngx_conf_t *cf, | ||
180 | 163 | ngx_http_proxy_rewrite_t *pr, ngx_str_t *regex, ngx_uint_t caseless); | ||
181 | 164 | |||
182 | 165 | #if (NGX_HTTP_SSL) | ||
183 | 166 | static ngx_int_t ngx_http_proxy_set_ssl(ngx_conf_t *cf, | ||
184 | 167 | ngx_http_proxy_loc_conf_t *plcf); | ||
185 | 168 | #endif | ||
186 | 169 | static void ngx_http_proxy_set_vars(ngx_url_t *u, ngx_http_proxy_vars_t *v); | ||
187 | 170 | |||
188 | 171 | |||
189 | 172 | static ngx_conf_post_t ngx_http_proxy_lowat_post = | ||
190 | 173 | { ngx_http_proxy_lowat_check }; | ||
191 | 174 | |||
192 | 175 | |||
193 | 176 | static ngx_conf_bitmask_t ngx_http_proxy_next_upstream_masks[] = { | ||
194 | 177 | { ngx_string("error"), NGX_HTTP_UPSTREAM_FT_ERROR }, | ||
195 | 178 | { ngx_string("timeout"), NGX_HTTP_UPSTREAM_FT_TIMEOUT }, | ||
196 | 179 | { ngx_string("invalid_header"), NGX_HTTP_UPSTREAM_FT_INVALID_HEADER }, | ||
197 | 180 | { ngx_string("http_500"), NGX_HTTP_UPSTREAM_FT_HTTP_500 }, | ||
198 | 181 | { ngx_string("http_502"), NGX_HTTP_UPSTREAM_FT_HTTP_502 }, | ||
199 | 182 | { ngx_string("http_503"), NGX_HTTP_UPSTREAM_FT_HTTP_503 }, | ||
200 | 183 | { ngx_string("http_504"), NGX_HTTP_UPSTREAM_FT_HTTP_504 }, | ||
201 | 184 | { ngx_string("http_404"), NGX_HTTP_UPSTREAM_FT_HTTP_404 }, | ||
202 | 185 | { ngx_string("updating"), NGX_HTTP_UPSTREAM_FT_UPDATING }, | ||
203 | 186 | { ngx_string("off"), NGX_HTTP_UPSTREAM_FT_OFF }, | ||
204 | 187 | { ngx_null_string, 0 } | ||
205 | 188 | }; | ||
206 | 189 | |||
207 | 190 | |||
208 | 191 | static ngx_conf_enum_t ngx_http_proxy_http_version[] = { | ||
209 | 192 | { ngx_string("1.0"), NGX_HTTP_VERSION_10 }, | ||
210 | 193 | { ngx_string("1.1"), NGX_HTTP_VERSION_11 }, | ||
211 | 194 | { ngx_null_string, 0 } | ||
212 | 195 | }; | ||
213 | 196 | |||
214 | 197 | |||
215 | 198 | ngx_module_t ngx_http_proxy_module; | ||
216 | 199 | |||
217 | 200 | |||
218 | 201 | static ngx_command_t ngx_http_proxy_commands[] = { | ||
219 | 202 | |||
220 | 203 | { ngx_string("proxy_pass"), | ||
221 | 204 | NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_HTTP_LMT_CONF|NGX_CONF_TAKE1, | ||
222 | 205 | ngx_http_proxy_pass, | ||
223 | 206 | NGX_HTTP_LOC_CONF_OFFSET, | ||
224 | 207 | 0, | ||
225 | 208 | NULL }, | ||
226 | 209 | |||
227 | 210 | { ngx_string("proxy_redirect"), | ||
228 | 211 | NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE12, | ||
229 | 212 | ngx_http_proxy_redirect, | ||
230 | 213 | NGX_HTTP_LOC_CONF_OFFSET, | ||
231 | 214 | 0, | ||
232 | 215 | NULL }, | ||
233 | 216 | |||
234 | 217 | { ngx_string("proxy_cookie_domain"), | ||
235 | 218 | NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE12, | ||
236 | 219 | ngx_http_proxy_cookie_domain, | ||
237 | 220 | NGX_HTTP_LOC_CONF_OFFSET, | ||
238 | 221 | 0, | ||
239 | 222 | NULL }, | ||
240 | 223 | |||
241 | 224 | { ngx_string("proxy_cookie_path"), | ||
242 | 225 | NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE12, | ||
243 | 226 | ngx_http_proxy_cookie_path, | ||
244 | 227 | NGX_HTTP_LOC_CONF_OFFSET, | ||
245 | 228 | 0, | ||
246 | 229 | NULL }, | ||
247 | 230 | |||
248 | 231 | { ngx_string("proxy_store"), | ||
249 | 232 | NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | ||
250 | 233 | ngx_http_proxy_store, | ||
251 | 234 | NGX_HTTP_LOC_CONF_OFFSET, | ||
252 | 235 | 0, | ||
253 | 236 | NULL }, | ||
254 | 237 | |||
255 | 238 | { ngx_string("proxy_store_access"), | ||
256 | 239 | NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE123, | ||
257 | 240 | ngx_conf_set_access_slot, | ||
258 | 241 | NGX_HTTP_LOC_CONF_OFFSET, | ||
259 | 242 | offsetof(ngx_http_proxy_loc_conf_t, upstream.store_access), | ||
260 | 243 | NULL }, | ||
261 | 244 | |||
262 | 245 | { ngx_string("proxy_buffering"), | ||
263 | 246 | NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG, | ||
264 | 247 | ngx_conf_set_flag_slot, | ||
265 | 248 | NGX_HTTP_LOC_CONF_OFFSET, | ||
266 | 249 | offsetof(ngx_http_proxy_loc_conf_t, upstream.buffering), | ||
267 | 250 | NULL }, | ||
268 | 251 | |||
269 | 252 | { ngx_string("proxy_ignore_client_abort"), | ||
270 | 253 | NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG, | ||
271 | 254 | ngx_conf_set_flag_slot, | ||
272 | 255 | NGX_HTTP_LOC_CONF_OFFSET, | ||
273 | 256 | offsetof(ngx_http_proxy_loc_conf_t, upstream.ignore_client_abort), | ||
274 | 257 | NULL }, | ||
275 | 258 | |||
276 | 259 | { ngx_string("proxy_bind"), | ||
277 | 260 | NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | ||
278 | 261 | ngx_http_upstream_bind_set_slot, | ||
279 | 262 | NGX_HTTP_LOC_CONF_OFFSET, | ||
280 | 263 | offsetof(ngx_http_proxy_loc_conf_t, upstream.local), | ||
281 | 264 | NULL }, | ||
282 | 265 | |||
283 | 266 | { ngx_string("proxy_connect_timeout"), | ||
284 | 267 | NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | ||
285 | 268 | ngx_conf_set_msec_slot, | ||
286 | 269 | NGX_HTTP_LOC_CONF_OFFSET, | ||
287 | 270 | offsetof(ngx_http_proxy_loc_conf_t, upstream.connect_timeout), | ||
288 | 271 | NULL }, | ||
289 | 272 | |||
290 | 273 | { ngx_string("proxy_send_timeout"), | ||
291 | 274 | NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | ||
292 | 275 | ngx_conf_set_msec_slot, | ||
293 | 276 | NGX_HTTP_LOC_CONF_OFFSET, | ||
294 | 277 | offsetof(ngx_http_proxy_loc_conf_t, upstream.send_timeout), | ||
295 | 278 | NULL }, | ||
296 | 279 | |||
297 | 280 | { ngx_string("proxy_send_lowat"), | ||
298 | 281 | NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | ||
299 | 282 | ngx_conf_set_size_slot, | ||
300 | 283 | NGX_HTTP_LOC_CONF_OFFSET, | ||
301 | 284 | offsetof(ngx_http_proxy_loc_conf_t, upstream.send_lowat), | ||
302 | 285 | &ngx_http_proxy_lowat_post }, | ||
303 | 286 | |||
304 | 287 | { ngx_string("proxy_intercept_errors"), | ||
305 | 288 | NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG, | ||
306 | 289 | ngx_conf_set_flag_slot, | ||
307 | 290 | NGX_HTTP_LOC_CONF_OFFSET, | ||
308 | 291 | offsetof(ngx_http_proxy_loc_conf_t, upstream.intercept_errors), | ||
309 | 292 | NULL }, | ||
310 | 293 | |||
311 | 294 | { ngx_string("proxy_set_header"), | ||
312 | 295 | NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE2, | ||
313 | 296 | ngx_conf_set_keyval_slot, | ||
314 | 297 | NGX_HTTP_LOC_CONF_OFFSET, | ||
315 | 298 | offsetof(ngx_http_proxy_loc_conf_t, headers_source), | ||
316 | 299 | NULL }, | ||
317 | 300 | |||
318 | 301 | { ngx_string("proxy_headers_hash_max_size"), | ||
319 | 302 | NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | ||
320 | 303 | ngx_conf_set_num_slot, | ||
321 | 304 | NGX_HTTP_LOC_CONF_OFFSET, | ||
322 | 305 | offsetof(ngx_http_proxy_loc_conf_t, headers_hash_max_size), | ||
323 | 306 | NULL }, | ||
324 | 307 | |||
325 | 308 | { ngx_string("proxy_headers_hash_bucket_size"), | ||
326 | 309 | NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | ||
327 | 310 | ngx_conf_set_num_slot, | ||
328 | 311 | NGX_HTTP_LOC_CONF_OFFSET, | ||
329 | 312 | offsetof(ngx_http_proxy_loc_conf_t, headers_hash_bucket_size), | ||
330 | 313 | NULL }, | ||
331 | 314 | |||
332 | 315 | { ngx_string("proxy_set_body"), | ||
333 | 316 | NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | ||
334 | 317 | ngx_conf_set_str_slot, | ||
335 | 318 | NGX_HTTP_LOC_CONF_OFFSET, | ||
336 | 319 | offsetof(ngx_http_proxy_loc_conf_t, body_source), | ||
337 | 320 | NULL }, | ||
338 | 321 | |||
339 | 322 | { ngx_string("proxy_method"), | ||
340 | 323 | NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | ||
341 | 324 | ngx_conf_set_str_slot, | ||
342 | 325 | NGX_HTTP_LOC_CONF_OFFSET, | ||
343 | 326 | offsetof(ngx_http_proxy_loc_conf_t, method), | ||
344 | 327 | NULL }, | ||
345 | 328 | |||
346 | 329 | { ngx_string("proxy_pass_request_headers"), | ||
347 | 330 | NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG, | ||
348 | 331 | ngx_conf_set_flag_slot, | ||
349 | 332 | NGX_HTTP_LOC_CONF_OFFSET, | ||
350 | 333 | offsetof(ngx_http_proxy_loc_conf_t, upstream.pass_request_headers), | ||
351 | 334 | NULL }, | ||
352 | 335 | |||
353 | 336 | { ngx_string("proxy_pass_request_body"), | ||
354 | 337 | NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG, | ||
355 | 338 | ngx_conf_set_flag_slot, | ||
356 | 339 | NGX_HTTP_LOC_CONF_OFFSET, | ||
357 | 340 | offsetof(ngx_http_proxy_loc_conf_t, upstream.pass_request_body), | ||
358 | 341 | NULL }, | ||
359 | 342 | |||
360 | 343 | { ngx_string("proxy_buffer_size"), | ||
361 | 344 | NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | ||
362 | 345 | ngx_conf_set_size_slot, | ||
363 | 346 | NGX_HTTP_LOC_CONF_OFFSET, | ||
364 | 347 | offsetof(ngx_http_proxy_loc_conf_t, upstream.buffer_size), | ||
365 | 348 | NULL }, | ||
366 | 349 | |||
367 | 350 | { ngx_string("proxy_read_timeout"), | ||
368 | 351 | NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | ||
369 | 352 | ngx_conf_set_msec_slot, | ||
370 | 353 | NGX_HTTP_LOC_CONF_OFFSET, | ||
371 | 354 | offsetof(ngx_http_proxy_loc_conf_t, upstream.read_timeout), | ||
372 | 355 | NULL }, | ||
373 | 356 | |||
374 | 357 | { ngx_string("proxy_buffers"), | ||
375 | 358 | NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE2, | ||
376 | 359 | ngx_conf_set_bufs_slot, | ||
377 | 360 | NGX_HTTP_LOC_CONF_OFFSET, | ||
378 | 361 | offsetof(ngx_http_proxy_loc_conf_t, upstream.bufs), | ||
379 | 362 | NULL }, | ||
380 | 363 | |||
381 | 364 | { ngx_string("proxy_busy_buffers_size"), | ||
382 | 365 | NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | ||
383 | 366 | ngx_conf_set_size_slot, | ||
384 | 367 | NGX_HTTP_LOC_CONF_OFFSET, | ||
385 | 368 | offsetof(ngx_http_proxy_loc_conf_t, upstream.busy_buffers_size_conf), | ||
386 | 369 | NULL }, | ||
387 | 370 | |||
388 | 371 | #if (NGX_HTTP_CACHE) | ||
389 | 372 | |||
390 | 373 | { ngx_string("proxy_cache"), | ||
391 | 374 | NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | ||
392 | 375 | ngx_http_proxy_cache, | ||
393 | 376 | NGX_HTTP_LOC_CONF_OFFSET, | ||
394 | 377 | 0, | ||
395 | 378 | NULL }, | ||
396 | 379 | |||
397 | 380 | { ngx_string("proxy_cache_key"), | ||
398 | 381 | NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | ||
399 | 382 | ngx_http_proxy_cache_key, | ||
400 | 383 | NGX_HTTP_LOC_CONF_OFFSET, | ||
401 | 384 | 0, | ||
402 | 385 | NULL }, | ||
403 | 386 | |||
404 | 387 | { ngx_string("proxy_cache_path"), | ||
405 | 388 | NGX_HTTP_MAIN_CONF|NGX_CONF_2MORE, | ||
406 | 389 | ngx_http_file_cache_set_slot, | ||
407 | 390 | 0, | ||
408 | 391 | 0, | ||
409 | 392 | &ngx_http_proxy_module }, | ||
410 | 393 | |||
411 | 394 | { ngx_string("proxy_cache_bypass"), | ||
412 | 395 | NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE, | ||
413 | 396 | ngx_http_set_predicate_slot, | ||
414 | 397 | NGX_HTTP_LOC_CONF_OFFSET, | ||
415 | 398 | offsetof(ngx_http_proxy_loc_conf_t, upstream.cache_bypass), | ||
416 | 399 | NULL }, | ||
417 | 400 | |||
418 | 401 | { ngx_string("proxy_no_cache"), | ||
419 | 402 | NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE, | ||
420 | 403 | ngx_http_set_predicate_slot, | ||
421 | 404 | NGX_HTTP_LOC_CONF_OFFSET, | ||
422 | 405 | offsetof(ngx_http_proxy_loc_conf_t, upstream.no_cache), | ||
423 | 406 | NULL }, | ||
424 | 407 | |||
425 | 408 | { ngx_string("proxy_cache_valid"), | ||
426 | 409 | NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE, | ||
427 | 410 | ngx_http_file_cache_valid_set_slot, | ||
428 | 411 | NGX_HTTP_LOC_CONF_OFFSET, | ||
429 | 412 | offsetof(ngx_http_proxy_loc_conf_t, upstream.cache_valid), | ||
430 | 413 | NULL }, | ||
431 | 414 | |||
432 | 415 | { ngx_string("proxy_cache_min_uses"), | ||
433 | 416 | NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | ||
434 | 417 | ngx_conf_set_num_slot, | ||
435 | 418 | NGX_HTTP_LOC_CONF_OFFSET, | ||
436 | 419 | offsetof(ngx_http_proxy_loc_conf_t, upstream.cache_min_uses), | ||
437 | 420 | NULL }, | ||
438 | 421 | |||
439 | 422 | { ngx_string("proxy_cache_use_stale"), | ||
440 | 423 | NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE, | ||
441 | 424 | ngx_conf_set_bitmask_slot, | ||
442 | 425 | NGX_HTTP_LOC_CONF_OFFSET, | ||
443 | 426 | offsetof(ngx_http_proxy_loc_conf_t, upstream.cache_use_stale), | ||
444 | 427 | &ngx_http_proxy_next_upstream_masks }, | ||
445 | 428 | |||
446 | 429 | { ngx_string("proxy_cache_methods"), | ||
447 | 430 | NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE, | ||
448 | 431 | ngx_conf_set_bitmask_slot, | ||
449 | 432 | NGX_HTTP_LOC_CONF_OFFSET, | ||
450 | 433 | offsetof(ngx_http_proxy_loc_conf_t, upstream.cache_methods), | ||
451 | 434 | &ngx_http_upstream_cache_method_mask }, | ||
452 | 435 | |||
453 | 436 | { ngx_string("proxy_cache_lock"), | ||
454 | 437 | NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG, | ||
455 | 438 | ngx_conf_set_flag_slot, | ||
456 | 439 | NGX_HTTP_LOC_CONF_OFFSET, | ||
457 | 440 | offsetof(ngx_http_proxy_loc_conf_t, upstream.cache_lock), | ||
458 | 441 | NULL }, | ||
459 | 442 | |||
460 | 443 | { ngx_string("proxy_cache_lock_timeout"), | ||
461 | 444 | NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | ||
462 | 445 | ngx_conf_set_msec_slot, | ||
463 | 446 | NGX_HTTP_LOC_CONF_OFFSET, | ||
464 | 447 | offsetof(ngx_http_proxy_loc_conf_t, upstream.cache_lock_timeout), | ||
465 | 448 | NULL }, | ||
466 | 449 | |||
467 | 450 | #endif | ||
468 | 451 | |||
469 | 452 | { ngx_string("proxy_temp_path"), | ||
470 | 453 | NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1234, | ||
471 | 454 | ngx_conf_set_path_slot, | ||
472 | 455 | NGX_HTTP_LOC_CONF_OFFSET, | ||
473 | 456 | offsetof(ngx_http_proxy_loc_conf_t, upstream.temp_path), | ||
474 | 457 | NULL }, | ||
475 | 458 | |||
476 | 459 | { ngx_string("proxy_max_temp_file_size"), | ||
477 | 460 | NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | ||
478 | 461 | ngx_conf_set_size_slot, | ||
479 | 462 | NGX_HTTP_LOC_CONF_OFFSET, | ||
480 | 463 | offsetof(ngx_http_proxy_loc_conf_t, upstream.max_temp_file_size_conf), | ||
481 | 464 | NULL }, | ||
482 | 465 | |||
483 | 466 | { ngx_string("proxy_temp_file_write_size"), | ||
484 | 467 | NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | ||
485 | 468 | ngx_conf_set_size_slot, | ||
486 | 469 | NGX_HTTP_LOC_CONF_OFFSET, | ||
487 | 470 | offsetof(ngx_http_proxy_loc_conf_t, upstream.temp_file_write_size_conf), | ||
488 | 471 | NULL }, | ||
489 | 472 | |||
490 | 473 | { ngx_string("proxy_next_upstream"), | ||
491 | 474 | NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE, | ||
492 | 475 | ngx_conf_set_bitmask_slot, | ||
493 | 476 | NGX_HTTP_LOC_CONF_OFFSET, | ||
494 | 477 | offsetof(ngx_http_proxy_loc_conf_t, upstream.next_upstream), | ||
495 | 478 | &ngx_http_proxy_next_upstream_masks }, | ||
496 | 479 | |||
497 | 480 | { ngx_string("proxy_pass_header"), | ||
498 | 481 | NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | ||
499 | 482 | ngx_conf_set_str_array_slot, | ||
500 | 483 | NGX_HTTP_LOC_CONF_OFFSET, | ||
501 | 484 | offsetof(ngx_http_proxy_loc_conf_t, upstream.pass_headers), | ||
502 | 485 | NULL }, | ||
503 | 486 | |||
504 | 487 | { ngx_string("proxy_hide_header"), | ||
505 | 488 | NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | ||
506 | 489 | ngx_conf_set_str_array_slot, | ||
507 | 490 | NGX_HTTP_LOC_CONF_OFFSET, | ||
508 | 491 | offsetof(ngx_http_proxy_loc_conf_t, upstream.hide_headers), | ||
509 | 492 | NULL }, | ||
510 | 493 | |||
511 | 494 | { ngx_string("proxy_ignore_headers"), | ||
512 | 495 | NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE, | ||
513 | 496 | ngx_conf_set_bitmask_slot, | ||
514 | 497 | NGX_HTTP_LOC_CONF_OFFSET, | ||
515 | 498 | offsetof(ngx_http_proxy_loc_conf_t, upstream.ignore_headers), | ||
516 | 499 | &ngx_http_upstream_ignore_headers_masks }, | ||
517 | 500 | |||
518 | 501 | { ngx_string("proxy_http_version"), | ||
519 | 502 | NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | ||
520 | 503 | ngx_conf_set_enum_slot, | ||
521 | 504 | NGX_HTTP_LOC_CONF_OFFSET, | ||
522 | 505 | offsetof(ngx_http_proxy_loc_conf_t, http_version), | ||
523 | 506 | &ngx_http_proxy_http_version }, | ||
524 | 507 | |||
525 | 508 | #if (NGX_HTTP_SSL) | ||
526 | 509 | |||
527 | 510 | { ngx_string("proxy_ssl_session_reuse"), | ||
528 | 511 | NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG, | ||
529 | 512 | ngx_conf_set_flag_slot, | ||
530 | 513 | NGX_HTTP_LOC_CONF_OFFSET, | ||
531 | 514 | offsetof(ngx_http_proxy_loc_conf_t, upstream.ssl_session_reuse), | ||
532 | 515 | NULL }, | ||
533 | 516 | |||
534 | 517 | #endif | ||
535 | 518 | |||
536 | 519 | ngx_null_command | ||
537 | 520 | }; | ||
538 | 521 | |||
539 | 522 | |||
540 | 523 | static ngx_http_module_t ngx_http_proxy_module_ctx = { | ||
541 | 524 | ngx_http_proxy_add_variables, /* preconfiguration */ | ||
542 | 525 | NULL, /* postconfiguration */ | ||
543 | 526 | |||
544 | 527 | NULL, /* create main configuration */ | ||
545 | 528 | NULL, /* init main configuration */ | ||
546 | 529 | |||
547 | 530 | NULL, /* create server configuration */ | ||
548 | 531 | NULL, /* merge server configuration */ | ||
549 | 532 | |||
550 | 533 | ngx_http_proxy_create_loc_conf, /* create location configuration */ | ||
551 | 534 | ngx_http_proxy_merge_loc_conf /* merge location configuration */ | ||
552 | 535 | }; | ||
553 | 536 | |||
554 | 537 | |||
555 | 538 | ngx_module_t ngx_http_proxy_module = { | ||
556 | 539 | NGX_MODULE_V1, | ||
557 | 540 | &ngx_http_proxy_module_ctx, /* module context */ | ||
558 | 541 | ngx_http_proxy_commands, /* module directives */ | ||
559 | 542 | NGX_HTTP_MODULE, /* module type */ | ||
560 | 543 | NULL, /* init master */ | ||
561 | 544 | NULL, /* init module */ | ||
562 | 545 | NULL, /* init process */ | ||
563 | 546 | NULL, /* init thread */ | ||
564 | 547 | NULL, /* exit thread */ | ||
565 | 548 | NULL, /* exit process */ | ||
566 | 549 | NULL, /* exit master */ | ||
567 | 550 | NGX_MODULE_V1_PADDING | ||
568 | 551 | }; | ||
569 | 552 | |||
570 | 553 | |||
571 | 554 | static char ngx_http_proxy_version[] = " HTTP/1.0" CRLF; | ||
572 | 555 | static char ngx_http_proxy_version_11[] = " HTTP/1.1" CRLF; | ||
573 | 556 | |||
574 | 557 | |||
575 | 558 | static ngx_keyval_t ngx_http_proxy_headers[] = { | ||
576 | 559 | { ngx_string("Host"), ngx_string("$proxy_host") }, | ||
577 | 560 | { ngx_string("Connection"), ngx_string("close") }, | ||
578 | 561 | { ngx_string("Keep-Alive"), ngx_string("") }, | ||
579 | 562 | { ngx_string("Expect"), ngx_string("") }, | ||
580 | 563 | { ngx_string("Upgrade"), ngx_string("") }, | ||
581 | 564 | { ngx_null_string, ngx_null_string } | ||
582 | 565 | }; | ||
583 | 566 | |||
584 | 567 | |||
585 | 568 | static ngx_str_t ngx_http_proxy_hide_headers[] = { | ||
586 | 569 | ngx_string("Date"), | ||
587 | 570 | ngx_string("Server"), | ||
588 | 571 | ngx_string("X-Pad"), | ||
589 | 572 | ngx_string("X-Accel-Expires"), | ||
590 | 573 | ngx_string("X-Accel-Redirect"), | ||
591 | 574 | ngx_string("X-Accel-Limit-Rate"), | ||
592 | 575 | ngx_string("X-Accel-Buffering"), | ||
593 | 576 | ngx_string("X-Accel-Charset"), | ||
594 | 577 | ngx_null_string | ||
595 | 578 | }; | ||
596 | 579 | |||
597 | 580 | |||
598 | 581 | #if (NGX_HTTP_CACHE) | ||
599 | 582 | |||
600 | 583 | static ngx_keyval_t ngx_http_proxy_cache_headers[] = { | ||
601 | 584 | { ngx_string("Host"), ngx_string("$proxy_host") }, | ||
602 | 585 | { ngx_string("Connection"), ngx_string("close") }, | ||
603 | 586 | { ngx_string("Keep-Alive"), ngx_string("") }, | ||
604 | 587 | { ngx_string("Expect"), ngx_string("") }, | ||
605 | 588 | { ngx_string("Upgrade"), ngx_string("") }, | ||
606 | 589 | { ngx_string("If-Modified-Since"), ngx_string("") }, | ||
607 | 590 | { ngx_string("If-Unmodified-Since"), ngx_string("") }, | ||
608 | 591 | { ngx_string("If-None-Match"), ngx_string("") }, | ||
609 | 592 | { ngx_string("If-Match"), ngx_string("") }, | ||
610 | 593 | { ngx_string("Range"), ngx_string("") }, | ||
611 | 594 | { ngx_string("If-Range"), ngx_string("") }, | ||
612 | 595 | { ngx_null_string, ngx_null_string } | ||
613 | 596 | }; | ||
614 | 597 | |||
615 | 598 | #endif | ||
616 | 599 | |||
617 | 600 | |||
618 | 601 | static ngx_http_variable_t ngx_http_proxy_vars[] = { | ||
619 | 602 | |||
620 | 603 | { ngx_string("proxy_host"), NULL, ngx_http_proxy_host_variable, 0, | ||
621 | 604 | NGX_HTTP_VAR_CHANGEABLE|NGX_HTTP_VAR_NOCACHEABLE|NGX_HTTP_VAR_NOHASH, 0 }, | ||
622 | 605 | |||
623 | 606 | { ngx_string("proxy_port"), NULL, ngx_http_proxy_port_variable, 0, | ||
624 | 607 | NGX_HTTP_VAR_CHANGEABLE|NGX_HTTP_VAR_NOCACHEABLE|NGX_HTTP_VAR_NOHASH, 0 }, | ||
625 | 608 | |||
626 | 609 | { ngx_string("proxy_add_x_forwarded_for"), NULL, | ||
627 | 610 | ngx_http_proxy_add_x_forwarded_for_variable, 0, NGX_HTTP_VAR_NOHASH, 0 }, | ||
628 | 611 | |||
629 | 612 | #if 0 | ||
630 | 613 | { ngx_string("proxy_add_via"), NULL, NULL, 0, NGX_HTTP_VAR_NOHASH, 0 }, | ||
631 | 614 | #endif | ||
632 | 615 | |||
633 | 616 | { ngx_string("proxy_internal_body_length"), NULL, | ||
634 | 617 | ngx_http_proxy_internal_body_length_variable, 0, NGX_HTTP_VAR_NOHASH, 0 }, | ||
635 | 618 | |||
636 | 619 | { ngx_null_string, NULL, NULL, 0, 0, 0 } | ||
637 | 620 | }; | ||
638 | 621 | |||
639 | 622 | |||
640 | 623 | static ngx_path_init_t ngx_http_proxy_temp_path = { | ||
641 | 624 | ngx_string(NGX_HTTP_PROXY_TEMP_PATH), { 1, 2, 0 } | ||
642 | 625 | }; | ||
643 | 626 | |||
644 | 627 | |||
645 | 628 | static ngx_int_t | ||
646 | 629 | ngx_http_proxy_handler(ngx_http_request_t *r) | ||
647 | 630 | { | ||
648 | 631 | ngx_int_t rc; | ||
649 | 632 | ngx_http_upstream_t *u; | ||
650 | 633 | ngx_http_proxy_ctx_t *ctx; | ||
651 | 634 | ngx_http_proxy_loc_conf_t *plcf; | ||
652 | 635 | |||
653 | 636 | if (ngx_http_upstream_create(r) != NGX_OK) { | ||
654 | 637 | return NGX_HTTP_INTERNAL_SERVER_ERROR; | ||
655 | 638 | } | ||
656 | 639 | |||
657 | 640 | ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_proxy_ctx_t)); | ||
658 | 641 | if (ctx == NULL) { | ||
659 | 642 | return NGX_ERROR; | ||
660 | 643 | } | ||
661 | 644 | |||
662 | 645 | ngx_http_set_ctx(r, ctx, ngx_http_proxy_module); | ||
663 | 646 | |||
664 | 647 | plcf = ngx_http_get_module_loc_conf(r, ngx_http_proxy_module); | ||
665 | 648 | |||
666 | 649 | u = r->upstream; | ||
667 | 650 | |||
668 | 651 | if (plcf->proxy_lengths == NULL) { | ||
669 | 652 | ctx->vars = plcf->vars; | ||
670 | 653 | u->schema = plcf->vars.schema; | ||
671 | 654 | #if (NGX_HTTP_SSL) | ||
672 | 655 | u->ssl = (plcf->upstream.ssl != NULL); | ||
673 | 656 | #endif | ||
674 | 657 | |||
675 | 658 | } else { | ||
676 | 659 | if (ngx_http_proxy_eval(r, ctx, plcf) != NGX_OK) { | ||
677 | 660 | return NGX_HTTP_INTERNAL_SERVER_ERROR; | ||
678 | 661 | } | ||
679 | 662 | } | ||
680 | 663 | |||
681 | 664 | u->output.tag = (ngx_buf_tag_t) &ngx_http_proxy_module; | ||
682 | 665 | |||
683 | 666 | u->conf = &plcf->upstream; | ||
684 | 667 | |||
685 | 668 | #if (NGX_HTTP_CACHE) | ||
686 | 669 | u->create_key = ngx_http_proxy_create_key; | ||
687 | 670 | #endif | ||
688 | 671 | u->create_request = ngx_http_proxy_create_request; | ||
689 | 672 | u->reinit_request = ngx_http_proxy_reinit_request; | ||
690 | 673 | u->process_header = ngx_http_proxy_process_status_line; | ||
691 | 674 | u->abort_request = ngx_http_proxy_abort_request; | ||
692 | 675 | u->finalize_request = ngx_http_proxy_finalize_request; | ||
693 | 676 | r->state = 0; | ||
694 | 677 | |||
695 | 678 | if (plcf->redirects) { | ||
696 | 679 | u->rewrite_redirect = ngx_http_proxy_rewrite_redirect; | ||
697 | 680 | } | ||
698 | 681 | |||
699 | 682 | if (plcf->cookie_domains || plcf->cookie_paths) { | ||
700 | 683 | u->rewrite_cookie = ngx_http_proxy_rewrite_cookie; | ||
701 | 684 | } | ||
702 | 685 | |||
703 | 686 | u->buffering = plcf->upstream.buffering; | ||
704 | 687 | |||
705 | 688 | u->pipe = ngx_pcalloc(r->pool, sizeof(ngx_event_pipe_t)); | ||
706 | 689 | if (u->pipe == NULL) { | ||
707 | 690 | return NGX_HTTP_INTERNAL_SERVER_ERROR; | ||
708 | 691 | } | ||
709 | 692 | |||
710 | 693 | u->pipe->input_filter = ngx_http_proxy_copy_filter; | ||
711 | 694 | u->pipe->input_ctx = r; | ||
712 | 695 | |||
713 | 696 | u->input_filter_init = ngx_http_proxy_input_filter_init; | ||
714 | 697 | u->input_filter = ngx_http_proxy_non_buffered_copy_filter; | ||
715 | 698 | u->input_filter_ctx = r; | ||
716 | 699 | |||
717 | 700 | u->accel = 1; | ||
718 | 701 | |||
719 | 702 | rc = ngx_http_read_client_request_body(r, ngx_http_upstream_init); | ||
720 | 703 | |||
721 | 704 | if (rc >= NGX_HTTP_SPECIAL_RESPONSE) { | ||
722 | 705 | return rc; | ||
723 | 706 | } | ||
724 | 707 | |||
725 | 708 | return NGX_DONE; | ||
726 | 709 | } | ||
727 | 710 | |||
728 | 711 | |||
729 | 712 | static ngx_int_t | ||
730 | 713 | ngx_http_proxy_eval(ngx_http_request_t *r, ngx_http_proxy_ctx_t *ctx, | ||
731 | 714 | ngx_http_proxy_loc_conf_t *plcf) | ||
732 | 715 | { | ||
733 | 716 | u_char *p; | ||
734 | 717 | size_t add; | ||
735 | 718 | u_short port; | ||
736 | 719 | ngx_str_t proxy; | ||
737 | 720 | ngx_url_t url; | ||
738 | 721 | ngx_http_upstream_t *u; | ||
739 | 722 | |||
740 | 723 | if (ngx_http_script_run(r, &proxy, plcf->proxy_lengths->elts, 0, | ||
741 | 724 | plcf->proxy_values->elts) | ||
742 | 725 | == NULL) | ||
743 | 726 | { | ||
744 | 727 | return NGX_ERROR; | ||
745 | 728 | } | ||
746 | 729 | |||
747 | 730 | if (proxy.len > 7 | ||
748 | 731 | && ngx_strncasecmp(proxy.data, (u_char *) "http://", 7) == 0) | ||
749 | 732 | { | ||
750 | 733 | add = 7; | ||
751 | 734 | port = 80; | ||
752 | 735 | |||
753 | 736 | #if (NGX_HTTP_SSL) | ||
754 | 737 | |||
755 | 738 | } else if (proxy.len > 8 | ||
756 | 739 | && ngx_strncasecmp(proxy.data, (u_char *) "https://", 8) == 0) | ||
757 | 740 | { | ||
758 | 741 | add = 8; | ||
759 | 742 | port = 443; | ||
760 | 743 | r->upstream->ssl = 1; | ||
761 | 744 | |||
762 | 745 | #endif | ||
763 | 746 | |||
764 | 747 | } else { | ||
765 | 748 | ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | ||
766 | 749 | "invalid URL prefix in \"%V\"", &proxy); | ||
767 | 750 | return NGX_ERROR; | ||
768 | 751 | } | ||
769 | 752 | |||
770 | 753 | u = r->upstream; | ||
771 | 754 | |||
772 | 755 | u->schema.len = add; | ||
773 | 756 | u->schema.data = proxy.data; | ||
774 | 757 | |||
775 | 758 | ngx_memzero(&url, sizeof(ngx_url_t)); | ||
776 | 759 | |||
777 | 760 | url.url.len = proxy.len - add; | ||
778 | 761 | url.url.data = proxy.data + add; | ||
779 | 762 | url.default_port = port; | ||
780 | 763 | url.uri_part = 1; | ||
781 | 764 | url.no_resolve = 1; | ||
782 | 765 | |||
783 | 766 | if (ngx_parse_url(r->pool, &url) != NGX_OK) { | ||
784 | 767 | if (url.err) { | ||
785 | 768 | ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | ||
786 | 769 | "%s in upstream \"%V\"", url.err, &url.url); | ||
787 | 770 | } | ||
788 | 771 | |||
789 | 772 | return NGX_ERROR; | ||
790 | 773 | } | ||
791 | 774 | |||
792 | 775 | if (url.uri.len) { | ||
793 | 776 | if (url.uri.data[0] == '?') { | ||
794 | 777 | p = ngx_pnalloc(r->pool, url.uri.len + 1); | ||
795 | 778 | if (p == NULL) { | ||
796 | 779 | return NGX_ERROR; | ||
797 | 780 | } | ||
798 | 781 | |||
799 | 782 | *p++ = '/'; | ||
800 | 783 | ngx_memcpy(p, url.uri.data, url.uri.len); | ||
801 | 784 | |||
802 | 785 | url.uri.len++; | ||
803 | 786 | url.uri.data = p - 1; | ||
804 | 787 | } | ||
805 | 788 | } | ||
806 | 789 | |||
807 | 790 | ctx->vars.key_start = u->schema; | ||
808 | 791 | |||
809 | 792 | ngx_http_proxy_set_vars(&url, &ctx->vars); | ||
810 | 793 | |||
811 | 794 | u->resolved = ngx_pcalloc(r->pool, sizeof(ngx_http_upstream_resolved_t)); | ||
812 | 795 | if (u->resolved == NULL) { | ||
813 | 796 | return NGX_ERROR; | ||
814 | 797 | } | ||
815 | 798 | |||
816 | 799 | if (url.addrs && url.addrs[0].sockaddr) { | ||
817 | 800 | u->resolved->sockaddr = url.addrs[0].sockaddr; | ||
818 | 801 | u->resolved->socklen = url.addrs[0].socklen; | ||
819 | 802 | u->resolved->naddrs = 1; | ||
820 | 803 | u->resolved->host = url.addrs[0].name; | ||
821 | 804 | |||
822 | 805 | } else { | ||
823 | 806 | u->resolved->host = url.host; | ||
824 | 807 | u->resolved->port = (in_port_t) (url.no_port ? port : url.port); | ||
825 | 808 | u->resolved->no_port = url.no_port; | ||
826 | 809 | } | ||
827 | 810 | |||
828 | 811 | return NGX_OK; | ||
829 | 812 | } | ||
830 | 813 | |||
831 | 814 | |||
832 | 815 | #if (NGX_HTTP_CACHE) | ||
833 | 816 | |||
834 | 817 | static ngx_int_t | ||
835 | 818 | ngx_http_proxy_create_key(ngx_http_request_t *r) | ||
836 | 819 | { | ||
837 | 820 | size_t len, loc_len; | ||
838 | 821 | u_char *p; | ||
839 | 822 | uintptr_t escape; | ||
840 | 823 | ngx_str_t *key; | ||
841 | 824 | ngx_http_upstream_t *u; | ||
842 | 825 | ngx_http_proxy_ctx_t *ctx; | ||
843 | 826 | ngx_http_proxy_loc_conf_t *plcf; | ||
844 | 827 | |||
845 | 828 | u = r->upstream; | ||
846 | 829 | |||
847 | 830 | plcf = ngx_http_get_module_loc_conf(r, ngx_http_proxy_module); | ||
848 | 831 | |||
849 | 832 | ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module); | ||
850 | 833 | |||
851 | 834 | key = ngx_array_push(&r->cache->keys); | ||
852 | 835 | if (key == NULL) { | ||
853 | 836 | return NGX_ERROR; | ||
854 | 837 | } | ||
855 | 838 | |||
856 | 839 | if (plcf->cache_key.value.len) { | ||
857 | 840 | |||
858 | 841 | if (ngx_http_complex_value(r, &plcf->cache_key, key) != NGX_OK) { | ||
859 | 842 | return NGX_ERROR; | ||
860 | 843 | } | ||
861 | 844 | |||
862 | 845 | return NGX_OK; | ||
863 | 846 | } | ||
864 | 847 | |||
865 | 848 | *key = ctx->vars.key_start; | ||
866 | 849 | |||
867 | 850 | key = ngx_array_push(&r->cache->keys); | ||
868 | 851 | if (key == NULL) { | ||
869 | 852 | return NGX_ERROR; | ||
870 | 853 | } | ||
871 | 854 | |||
872 | 855 | if (plcf->proxy_lengths && ctx->vars.uri.len) { | ||
873 | 856 | |||
874 | 857 | *key = ctx->vars.uri; | ||
875 | 858 | u->uri = ctx->vars.uri; | ||
876 | 859 | |||
877 | 860 | return NGX_OK; | ||
878 | 861 | |||
879 | 862 | } else if (ctx->vars.uri.len == 0 && r->valid_unparsed_uri && r == r->main) | ||
880 | 863 | { | ||
881 | 864 | *key = r->unparsed_uri; | ||
882 | 865 | u->uri = r->unparsed_uri; | ||
883 | 866 | |||
884 | 867 | return NGX_OK; | ||
885 | 868 | } | ||
886 | 869 | |||
887 | 870 | loc_len = (r->valid_location && ctx->vars.uri.len) ? plcf->location.len : 0; | ||
888 | 871 | |||
889 | 872 | if (r->quoted_uri || r->internal) { | ||
890 | 873 | escape = 2 * ngx_escape_uri(NULL, r->uri.data + loc_len, | ||
891 | 874 | r->uri.len - loc_len, NGX_ESCAPE_URI); | ||
892 | 875 | } else { | ||
893 | 876 | escape = 0; | ||
894 | 877 | } | ||
895 | 878 | |||
896 | 879 | len = ctx->vars.uri.len + r->uri.len - loc_len + escape | ||
897 | 880 | + sizeof("?") - 1 + r->args.len; | ||
898 | 881 | |||
899 | 882 | p = ngx_pnalloc(r->pool, len); | ||
900 | 883 | if (p == NULL) { | ||
901 | 884 | return NGX_ERROR; | ||
902 | 885 | } | ||
903 | 886 | |||
904 | 887 | key->data = p; | ||
905 | 888 | |||
906 | 889 | if (r->valid_location) { | ||
907 | 890 | p = ngx_copy(p, ctx->vars.uri.data, ctx->vars.uri.len); | ||
908 | 891 | } | ||
909 | 892 | |||
910 | 893 | if (escape) { | ||
911 | 894 | ngx_escape_uri(p, r->uri.data + loc_len, | ||
912 | 895 | r->uri.len - loc_len, NGX_ESCAPE_URI); | ||
913 | 896 | p += r->uri.len - loc_len + escape; | ||
914 | 897 | |||
915 | 898 | } else { | ||
916 | 899 | p = ngx_copy(p, r->uri.data + loc_len, r->uri.len - loc_len); | ||
917 | 900 | } | ||
918 | 901 | |||
919 | 902 | if (r->args.len > 0) { | ||
920 | 903 | *p++ = '?'; | ||
921 | 904 | p = ngx_copy(p, r->args.data, r->args.len); | ||
922 | 905 | } | ||
923 | 906 | |||
924 | 907 | key->len = p - key->data; | ||
925 | 908 | u->uri = *key; | ||
926 | 909 | |||
927 | 910 | return NGX_OK; | ||
928 | 911 | } | ||
929 | 912 | |||
930 | 913 | #endif | ||
931 | 914 | |||
932 | 915 | |||
933 | 916 | static ngx_int_t | ||
934 | 917 | ngx_http_proxy_create_request(ngx_http_request_t *r) | ||
935 | 918 | { | ||
936 | 919 | size_t len, uri_len, loc_len, body_len; | ||
937 | 920 | uintptr_t escape; | ||
938 | 921 | ngx_buf_t *b; | ||
939 | 922 | ngx_str_t method; | ||
940 | 923 | ngx_uint_t i, unparsed_uri; | ||
941 | 924 | ngx_chain_t *cl, *body; | ||
942 | 925 | ngx_list_part_t *part; | ||
943 | 926 | ngx_table_elt_t *header; | ||
944 | 927 | ngx_http_upstream_t *u; | ||
945 | 928 | ngx_http_proxy_ctx_t *ctx; | ||
946 | 929 | ngx_http_script_code_pt code; | ||
947 | 930 | ngx_http_script_engine_t e, le; | ||
948 | 931 | ngx_http_proxy_loc_conf_t *plcf; | ||
949 | 932 | ngx_http_script_len_code_pt lcode; | ||
950 | 933 | |||
951 | 934 | u = r->upstream; | ||
952 | 935 | |||
953 | 936 | plcf = ngx_http_get_module_loc_conf(r, ngx_http_proxy_module); | ||
954 | 937 | |||
955 | 938 | if (u->method.len) { | ||
956 | 939 | /* HEAD was changed to GET to cache response */ | ||
957 | 940 | method = u->method; | ||
958 | 941 | method.len++; | ||
959 | 942 | |||
960 | 943 | } else if (plcf->method.len) { | ||
961 | 944 | method = plcf->method; | ||
962 | 945 | |||
963 | 946 | } else { | ||
964 | 947 | method = r->method_name; | ||
965 | 948 | method.len++; | ||
966 | 949 | } | ||
967 | 950 | |||
968 | 951 | ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module); | ||
969 | 952 | |||
970 | 953 | if (method.len == 5 | ||
971 | 954 | && ngx_strncasecmp(method.data, (u_char *) "HEAD ", 5) == 0) | ||
972 | 955 | { | ||
973 | 956 | ctx->head = 1; | ||
974 | 957 | } | ||
975 | 958 | |||
976 | 959 | len = method.len + sizeof(ngx_http_proxy_version) - 1 + sizeof(CRLF) - 1; | ||
977 | 960 | |||
978 | 961 | escape = 0; | ||
979 | 962 | loc_len = 0; | ||
980 | 963 | unparsed_uri = 0; | ||
981 | 964 | |||
982 | 965 | if (plcf->proxy_lengths && ctx->vars.uri.len) { | ||
983 | 966 | uri_len = ctx->vars.uri.len; | ||
984 | 967 | |||
985 | 968 | } else if (ctx->vars.uri.len == 0 && r->valid_unparsed_uri && r == r->main) | ||
986 | 969 | { | ||
987 | 970 | unparsed_uri = 1; | ||
988 | 971 | uri_len = r->unparsed_uri.len; | ||
989 | 972 | |||
990 | 973 | } else { | ||
991 | 974 | loc_len = (r->valid_location && ctx->vars.uri.len) ? | ||
992 | 975 | plcf->location.len : 0; | ||
993 | 976 | |||
994 | 977 | if (r->quoted_uri || r->space_in_uri || r->internal) { | ||
995 | 978 | escape = 2 * ngx_escape_uri(NULL, r->uri.data + loc_len, | ||
996 | 979 | r->uri.len - loc_len, NGX_ESCAPE_URI); | ||
997 | 980 | } | ||
998 | 981 | |||
999 | 982 | uri_len = ctx->vars.uri.len + r->uri.len - loc_len + escape | ||
1000 | 983 | + sizeof("?") - 1 + r->args.len; | ||
1001 | 984 | } | ||
1002 | 985 | |||
1003 | 986 | if (uri_len == 0) { | ||
1004 | 987 | ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | ||
1005 | 988 | "zero length URI to proxy"); | ||
1006 | 989 | return NGX_ERROR; | ||
1007 | 990 | } | ||
1008 | 991 | |||
1009 | 992 | len += uri_len; | ||
1010 | 993 | |||
1011 | 994 | ngx_http_script_flush_no_cacheable_variables(r, plcf->flushes); | ||
1012 | 995 | |||
1013 | 996 | if (plcf->body_set_len) { | ||
1014 | 997 | le.ip = plcf->body_set_len->elts; | ||
1015 | 998 | le.request = r; | ||
1016 | 999 | le.flushed = 1; | ||
1017 | 1000 | body_len = 0; | ||
1018 | 1001 | |||
1019 | 1002 | while (*(uintptr_t *) le.ip) { | ||
1020 | 1003 | lcode = *(ngx_http_script_len_code_pt *) le.ip; | ||
1021 | 1004 | body_len += lcode(&le); | ||
1022 | 1005 | } | ||
1023 | 1006 | |||
1024 | 1007 | ctx->internal_body_length = body_len; | ||
1025 | 1008 | len += body_len; | ||
1026 | 1009 | } | ||
1027 | 1010 | |||
1028 | 1011 | le.ip = plcf->headers_set_len->elts; | ||
1029 | 1012 | le.request = r; | ||
1030 | 1013 | le.flushed = 1; | ||
1031 | 1014 | |||
1032 | 1015 | while (*(uintptr_t *) le.ip) { | ||
1033 | 1016 | while (*(uintptr_t *) le.ip) { | ||
1034 | 1017 | lcode = *(ngx_http_script_len_code_pt *) le.ip; | ||
1035 | 1018 | len += lcode(&le); | ||
1036 | 1019 | } | ||
1037 | 1020 | le.ip += sizeof(uintptr_t); | ||
1038 | 1021 | } | ||
1039 | 1022 | |||
1040 | 1023 | |||
1041 | 1024 | if (plcf->upstream.pass_request_headers) { | ||
1042 | 1025 | part = &r->headers_in.headers.part; | ||
1043 | 1026 | header = part->elts; | ||
1044 | 1027 | |||
1045 | 1028 | for (i = 0; /* void */; i++) { | ||
1046 | 1029 | |||
1047 | 1030 | if (i >= part->nelts) { | ||
1048 | 1031 | if (part->next == NULL) { | ||
1049 | 1032 | break; | ||
1050 | 1033 | } | ||
1051 | 1034 | |||
1052 | 1035 | part = part->next; | ||
1053 | 1036 | header = part->elts; | ||
1054 | 1037 | i = 0; | ||
1055 | 1038 | } | ||
1056 | 1039 | |||
1057 | 1040 | if (ngx_hash_find(&plcf->headers_set_hash, header[i].hash, | ||
1058 | 1041 | header[i].lowcase_key, header[i].key.len)) | ||
1059 | 1042 | { | ||
1060 | 1043 | continue; | ||
1061 | 1044 | } | ||
1062 | 1045 | |||
1063 | 1046 | len += header[i].key.len + sizeof(": ") - 1 | ||
1064 | 1047 | + header[i].value.len + sizeof(CRLF) - 1; | ||
1065 | 1048 | } | ||
1066 | 1049 | } | ||
1067 | 1050 | |||
1068 | 1051 | |||
1069 | 1052 | b = ngx_create_temp_buf(r->pool, len); | ||
1070 | 1053 | if (b == NULL) { | ||
1071 | 1054 | return NGX_ERROR; | ||
1072 | 1055 | } | ||
1073 | 1056 | |||
1074 | 1057 | cl = ngx_alloc_chain_link(r->pool); | ||
1075 | 1058 | if (cl == NULL) { | ||
1076 | 1059 | return NGX_ERROR; | ||
1077 | 1060 | } | ||
1078 | 1061 | |||
1079 | 1062 | cl->buf = b; | ||
1080 | 1063 | |||
1081 | 1064 | |||
1082 | 1065 | /* the request line */ | ||
1083 | 1066 | |||
1084 | 1067 | b->last = ngx_copy(b->last, method.data, method.len); | ||
1085 | 1068 | |||
1086 | 1069 | u->uri.data = b->last; | ||
1087 | 1070 | |||
1088 | 1071 | if (plcf->proxy_lengths && ctx->vars.uri.len) { | ||
1089 | 1072 | b->last = ngx_copy(b->last, ctx->vars.uri.data, ctx->vars.uri.len); | ||
1090 | 1073 | |||
1091 | 1074 | } else if (unparsed_uri) { | ||
1092 | 1075 | b->last = ngx_copy(b->last, r->unparsed_uri.data, r->unparsed_uri.len); | ||
1093 | 1076 | |||
1094 | 1077 | } else { | ||
1095 | 1078 | if (r->valid_location) { | ||
1096 | 1079 | b->last = ngx_copy(b->last, ctx->vars.uri.data, ctx->vars.uri.len); | ||
1097 | 1080 | } | ||
1098 | 1081 | |||
1099 | 1082 | if (escape) { | ||
1100 | 1083 | ngx_escape_uri(b->last, r->uri.data + loc_len, | ||
1101 | 1084 | r->uri.len - loc_len, NGX_ESCAPE_URI); | ||
1102 | 1085 | b->last += r->uri.len - loc_len + escape; | ||
1103 | 1086 | |||
1104 | 1087 | } else { | ||
1105 | 1088 | b->last = ngx_copy(b->last, r->uri.data + loc_len, | ||
1106 | 1089 | r->uri.len - loc_len); | ||
1107 | 1090 | } | ||
1108 | 1091 | |||
1109 | 1092 | if (r->args.len > 0) { | ||
1110 | 1093 | *b->last++ = '?'; | ||
1111 | 1094 | b->last = ngx_copy(b->last, r->args.data, r->args.len); | ||
1112 | 1095 | } | ||
1113 | 1096 | } | ||
1114 | 1097 | |||
1115 | 1098 | u->uri.len = b->last - u->uri.data; | ||
1116 | 1099 | |||
1117 | 1100 | if (plcf->http_version == NGX_HTTP_VERSION_11) { | ||
1118 | 1101 | b->last = ngx_cpymem(b->last, ngx_http_proxy_version_11, | ||
1119 | 1102 | sizeof(ngx_http_proxy_version_11) - 1); | ||
1120 | 1103 | |||
1121 | 1104 | } else { | ||
1122 | 1105 | b->last = ngx_cpymem(b->last, ngx_http_proxy_version, | ||
1123 | 1106 | sizeof(ngx_http_proxy_version) - 1); | ||
1124 | 1107 | } | ||
1125 | 1108 | |||
1126 | 1109 | ngx_memzero(&e, sizeof(ngx_http_script_engine_t)); | ||
1127 | 1110 | |||
1128 | 1111 | e.ip = plcf->headers_set->elts; | ||
1129 | 1112 | e.pos = b->last; | ||
1130 | 1113 | e.request = r; | ||
1131 | 1114 | e.flushed = 1; | ||
1132 | 1115 | |||
1133 | 1116 | le.ip = plcf->headers_set_len->elts; | ||
1134 | 1117 | |||
1135 | 1118 | while (*(uintptr_t *) le.ip) { | ||
1136 | 1119 | lcode = *(ngx_http_script_len_code_pt *) le.ip; | ||
1137 | 1120 | |||
1138 | 1121 | /* skip the header line name length */ | ||
1139 | 1122 | (void) lcode(&le); | ||
1140 | 1123 | |||
1141 | 1124 | if (*(ngx_http_script_len_code_pt *) le.ip) { | ||
1142 | 1125 | |||
1143 | 1126 | for (len = 0; *(uintptr_t *) le.ip; len += lcode(&le)) { | ||
1144 | 1127 | lcode = *(ngx_http_script_len_code_pt *) le.ip; | ||
1145 | 1128 | } | ||
1146 | 1129 | |||
1147 | 1130 | e.skip = (len == sizeof(CRLF) - 1) ? 1 : 0; | ||
1148 | 1131 | |||
1149 | 1132 | } else { | ||
1150 | 1133 | e.skip = 0; | ||
1151 | 1134 | } | ||
1152 | 1135 | |||
1153 | 1136 | le.ip += sizeof(uintptr_t); | ||
1154 | 1137 | |||
1155 | 1138 | while (*(uintptr_t *) e.ip) { | ||
1156 | 1139 | code = *(ngx_http_script_code_pt *) e.ip; | ||
1157 | 1140 | code((ngx_http_script_engine_t *) &e); | ||
1158 | 1141 | } | ||
1159 | 1142 | e.ip += sizeof(uintptr_t); | ||
1160 | 1143 | } | ||
1161 | 1144 | |||
1162 | 1145 | b->last = e.pos; | ||
1163 | 1146 | |||
1164 | 1147 | |||
1165 | 1148 | if (plcf->upstream.pass_request_headers) { | ||
1166 | 1149 | part = &r->headers_in.headers.part; | ||
1167 | 1150 | header = part->elts; | ||
1168 | 1151 | |||
1169 | 1152 | for (i = 0; /* void */; i++) { | ||
1170 | 1153 | |||
1171 | 1154 | if (i >= part->nelts) { | ||
1172 | 1155 | if (part->next == NULL) { | ||
1173 | 1156 | break; | ||
1174 | 1157 | } | ||
1175 | 1158 | |||
1176 | 1159 | part = part->next; | ||
1177 | 1160 | header = part->elts; | ||
1178 | 1161 | i = 0; | ||
1179 | 1162 | } | ||
1180 | 1163 | |||
1181 | 1164 | if (ngx_hash_find(&plcf->headers_set_hash, header[i].hash, | ||
1182 | 1165 | header[i].lowcase_key, header[i].key.len)) | ||
1183 | 1166 | { | ||
1184 | 1167 | continue; | ||
1185 | 1168 | } | ||
1186 | 1169 | |||
1187 | 1170 | b->last = ngx_copy(b->last, header[i].key.data, header[i].key.len); | ||
1188 | 1171 | |||
1189 | 1172 | *b->last++ = ':'; *b->last++ = ' '; | ||
1190 | 1173 | |||
1191 | 1174 | b->last = ngx_copy(b->last, header[i].value.data, | ||
1192 | 1175 | header[i].value.len); | ||
1193 | 1176 | |||
1194 | 1177 | *b->last++ = CR; *b->last++ = LF; | ||
1195 | 1178 | |||
1196 | 1179 | ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | ||
1197 | 1180 | "http proxy header: \"%V: %V\"", | ||
1198 | 1181 | &header[i].key, &header[i].value); | ||
1199 | 1182 | } | ||
1200 | 1183 | } | ||
1201 | 1184 | |||
1202 | 1185 | |||
1203 | 1186 | /* add "\r\n" at the header end */ | ||
1204 | 1187 | *b->last++ = CR; *b->last++ = LF; | ||
1205 | 1188 | |||
1206 | 1189 | if (plcf->body_set) { | ||
1207 | 1190 | e.ip = plcf->body_set->elts; | ||
1208 | 1191 | e.pos = b->last; | ||
1209 | 1192 | |||
1210 | 1193 | while (*(uintptr_t *) e.ip) { | ||
1211 | 1194 | code = *(ngx_http_script_code_pt *) e.ip; | ||
1212 | 1195 | code((ngx_http_script_engine_t *) &e); | ||
1213 | 1196 | } | ||
1214 | 1197 | |||
1215 | 1198 | b->last = e.pos; | ||
1216 | 1199 | } | ||
1217 | 1200 | |||
1218 | 1201 | ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | ||
1219 | 1202 | "http proxy header:\n\"%*s\"", | ||
1220 | 1203 | (size_t) (b->last - b->pos), b->pos); | ||
1221 | 1204 | |||
1222 | 1205 | if (plcf->body_set == NULL && plcf->upstream.pass_request_body) { | ||
1223 | 1206 | |||
1224 | 1207 | body = u->request_bufs; | ||
1225 | 1208 | u->request_bufs = cl; | ||
1226 | 1209 | |||
1227 | 1210 | while (body) { | ||
1228 | 1211 | b = ngx_alloc_buf(r->pool); | ||
1229 | 1212 | if (b == NULL) { | ||
1230 | 1213 | return NGX_ERROR; | ||
1231 | 1214 | } | ||
1232 | 1215 | |||
1233 | 1216 | ngx_memcpy(b, body->buf, sizeof(ngx_buf_t)); | ||
1234 | 1217 | |||
1235 | 1218 | cl->next = ngx_alloc_chain_link(r->pool); | ||
1236 | 1219 | if (cl->next == NULL) { | ||
1237 | 1220 | return NGX_ERROR; | ||
1238 | 1221 | } | ||
1239 | 1222 | |||
1240 | 1223 | cl = cl->next; | ||
1241 | 1224 | cl->buf = b; | ||
1242 | 1225 | |||
1243 | 1226 | body = body->next; | ||
1244 | 1227 | } | ||
1245 | 1228 | |||
1246 | 1229 | } else { | ||
1247 | 1230 | u->request_bufs = cl; | ||
1248 | 1231 | } | ||
1249 | 1232 | |||
1250 | 1233 | b->flush = 1; | ||
1251 | 1234 | cl->next = NULL; | ||
1252 | 1235 | |||
1253 | 1236 | return NGX_OK; | ||
1254 | 1237 | } | ||
1255 | 1238 | |||
1256 | 1239 | |||
1257 | 1240 | static ngx_int_t | ||
1258 | 1241 | ngx_http_proxy_reinit_request(ngx_http_request_t *r) | ||
1259 | 1242 | { | ||
1260 | 1243 | ngx_http_proxy_ctx_t *ctx; | ||
1261 | 1244 | |||
1262 | 1245 | ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module); | ||
1263 | 1246 | |||
1264 | 1247 | if (ctx == NULL) { | ||
1265 | 1248 | return NGX_OK; | ||
1266 | 1249 | } | ||
1267 | 1250 | |||
1268 | 1251 | ctx->status.code = 0; | ||
1269 | 1252 | ctx->status.count = 0; | ||
1270 | 1253 | ctx->status.start = NULL; | ||
1271 | 1254 | ctx->status.end = NULL; | ||
1272 | 1255 | ctx->state = 0; | ||
1273 | 1256 | |||
1274 | 1257 | r->upstream->process_header = ngx_http_proxy_process_status_line; | ||
1275 | 1258 | r->upstream->pipe->input_filter = ngx_http_proxy_copy_filter; | ||
1276 | 1259 | r->upstream->input_filter = ngx_http_proxy_non_buffered_copy_filter; | ||
1277 | 1260 | r->state = 0; | ||
1278 | 1261 | |||
1279 | 1262 | return NGX_OK; | ||
1280 | 1263 | } | ||
1281 | 1264 | |||
1282 | 1265 | |||
1283 | 1266 | static ngx_int_t | ||
1284 | 1267 | ngx_http_proxy_process_status_line(ngx_http_request_t *r) | ||
1285 | 1268 | { | ||
1286 | 1269 | size_t len; | ||
1287 | 1270 | ngx_int_t rc; | ||
1288 | 1271 | ngx_http_upstream_t *u; | ||
1289 | 1272 | ngx_http_proxy_ctx_t *ctx; | ||
1290 | 1273 | |||
1291 | 1274 | ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module); | ||
1292 | 1275 | |||
1293 | 1276 | if (ctx == NULL) { | ||
1294 | 1277 | return NGX_ERROR; | ||
1295 | 1278 | } | ||
1296 | 1279 | |||
1297 | 1280 | u = r->upstream; | ||
1298 | 1281 | |||
1299 | 1282 | rc = ngx_http_parse_status_line(r, &u->buffer, &ctx->status); | ||
1300 | 1283 | |||
1301 | 1284 | if (rc == NGX_AGAIN) { | ||
1302 | 1285 | return rc; | ||
1303 | 1286 | } | ||
1304 | 1287 | |||
1305 | 1288 | if (rc == NGX_ERROR) { | ||
1306 | 1289 | |||
1307 | 1290 | #if (NGX_HTTP_CACHE) | ||
1308 | 1291 | |||
1309 | 1292 | if (r->cache) { | ||
1310 | 1293 | r->http_version = NGX_HTTP_VERSION_9; | ||
1311 | 1294 | return NGX_OK; | ||
1312 | 1295 | } | ||
1313 | 1296 | |||
1314 | 1297 | #endif | ||
1315 | 1298 | |||
1316 | 1299 | ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | ||
1317 | 1300 | "upstream sent no valid HTTP/1.0 header"); | ||
1318 | 1301 | |||
1319 | 1302 | #if 0 | ||
1320 | 1303 | if (u->accel) { | ||
1321 | 1304 | return NGX_HTTP_UPSTREAM_INVALID_HEADER; | ||
1322 | 1305 | } | ||
1323 | 1306 | #endif | ||
1324 | 1307 | |||
1325 | 1308 | r->http_version = NGX_HTTP_VERSION_9; | ||
1326 | 1309 | u->state->status = NGX_HTTP_OK; | ||
1327 | 1310 | u->headers_in.connection_close = 1; | ||
1328 | 1311 | |||
1329 | 1312 | return NGX_OK; | ||
1330 | 1313 | } | ||
1331 | 1314 | |||
1332 | 1315 | if (u->state) { | ||
1333 | 1316 | u->state->status = ctx->status.code; | ||
1334 | 1317 | } | ||
1335 | 1318 | |||
1336 | 1319 | u->headers_in.status_n = ctx->status.code; | ||
1337 | 1320 | |||
1338 | 1321 | len = ctx->status.end - ctx->status.start; | ||
1339 | 1322 | u->headers_in.status_line.len = len; | ||
1340 | 1323 | |||
1341 | 1324 | u->headers_in.status_line.data = ngx_pnalloc(r->pool, len); | ||
1342 | 1325 | if (u->headers_in.status_line.data == NULL) { | ||
1343 | 1326 | return NGX_ERROR; | ||
1344 | 1327 | } | ||
1345 | 1328 | |||
1346 | 1329 | ngx_memcpy(u->headers_in.status_line.data, ctx->status.start, len); | ||
1347 | 1330 | |||
1348 | 1331 | ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | ||
1349 | 1332 | "http proxy status %ui \"%V\"", | ||
1350 | 1333 | u->headers_in.status_n, &u->headers_in.status_line); | ||
1351 | 1334 | |||
1352 | 1335 | if (ctx->status.http_version < NGX_HTTP_VERSION_11) { | ||
1353 | 1336 | u->headers_in.connection_close = 1; | ||
1354 | 1337 | } | ||
1355 | 1338 | |||
1356 | 1339 | u->process_header = ngx_http_proxy_process_header; | ||
1357 | 1340 | |||
1358 | 1341 | return ngx_http_proxy_process_header(r); | ||
1359 | 1342 | } | ||
1360 | 1343 | |||
1361 | 1344 | |||
1362 | 1345 | static ngx_int_t | ||
1363 | 1346 | ngx_http_proxy_process_header(ngx_http_request_t *r) | ||
1364 | 1347 | { | ||
1365 | 1348 | ngx_int_t rc; | ||
1366 | 1349 | ngx_table_elt_t *h; | ||
1367 | 1350 | ngx_http_upstream_t *u; | ||
1368 | 1351 | ngx_http_proxy_ctx_t *ctx; | ||
1369 | 1352 | ngx_http_upstream_header_t *hh; | ||
1370 | 1353 | ngx_http_upstream_main_conf_t *umcf; | ||
1371 | 1354 | |||
1372 | 1355 | umcf = ngx_http_get_module_main_conf(r, ngx_http_upstream_module); | ||
1373 | 1356 | |||
1374 | 1357 | for ( ;; ) { | ||
1375 | 1358 | |||
1376 | 1359 | rc = ngx_http_parse_header_line(r, &r->upstream->buffer, 1); | ||
1377 | 1360 | |||
1378 | 1361 | if (rc == NGX_OK) { | ||
1379 | 1362 | |||
1380 | 1363 | /* a header line has been parsed successfully */ | ||
1381 | 1364 | |||
1382 | 1365 | h = ngx_list_push(&r->upstream->headers_in.headers); | ||
1383 | 1366 | if (h == NULL) { | ||
1384 | 1367 | return NGX_ERROR; | ||
1385 | 1368 | } | ||
1386 | 1369 | |||
1387 | 1370 | h->hash = r->header_hash; | ||
1388 | 1371 | |||
1389 | 1372 | h->key.len = r->header_name_end - r->header_name_start; | ||
1390 | 1373 | h->value.len = r->header_end - r->header_start; | ||
1391 | 1374 | |||
1392 | 1375 | h->key.data = ngx_pnalloc(r->pool, | ||
1393 | 1376 | h->key.len + 1 + h->value.len + 1 + h->key.len); | ||
1394 | 1377 | if (h->key.data == NULL) { | ||
1395 | 1378 | return NGX_ERROR; | ||
1396 | 1379 | } | ||
1397 | 1380 | |||
1398 | 1381 | h->value.data = h->key.data + h->key.len + 1; | ||
1399 | 1382 | h->lowcase_key = h->key.data + h->key.len + 1 + h->value.len + 1; | ||
1400 | 1383 | |||
1401 | 1384 | ngx_memcpy(h->key.data, r->header_name_start, h->key.len); | ||
1402 | 1385 | h->key.data[h->key.len] = '\0'; | ||
1403 | 1386 | ngx_memcpy(h->value.data, r->header_start, h->value.len); | ||
1404 | 1387 | h->value.data[h->value.len] = '\0'; | ||
1405 | 1388 | |||
1406 | 1389 | if (h->key.len == r->lowcase_index) { | ||
1407 | 1390 | ngx_memcpy(h->lowcase_key, r->lowcase_header, h->key.len); | ||
1408 | 1391 | |||
1409 | 1392 | } else { | ||
1410 | 1393 | ngx_strlow(h->lowcase_key, h->key.data, h->key.len); | ||
1411 | 1394 | } | ||
1412 | 1395 | |||
1413 | 1396 | hh = ngx_hash_find(&umcf->headers_in_hash, h->hash, | ||
1414 | 1397 | h->lowcase_key, h->key.len); | ||
1415 | 1398 | |||
1416 | 1399 | if (hh && hh->handler(r, h, hh->offset) != NGX_OK) { | ||
1417 | 1400 | return NGX_ERROR; | ||
1418 | 1401 | } | ||
1419 | 1402 | |||
1420 | 1403 | ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | ||
1421 | 1404 | "http proxy header: \"%V: %V\"", | ||
1422 | 1405 | &h->key, &h->value); | ||
1423 | 1406 | |||
1424 | 1407 | continue; | ||
1425 | 1408 | } | ||
1426 | 1409 | |||
1427 | 1410 | if (rc == NGX_HTTP_PARSE_HEADER_DONE) { | ||
1428 | 1411 | |||
1429 | 1412 | /* a whole header has been parsed successfully */ | ||
1430 | 1413 | |||
1431 | 1414 | ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | ||
1432 | 1415 | "http proxy header done"); | ||
1433 | 1416 | |||
1434 | 1417 | /* | ||
1435 | 1418 | * if no "Server" and "Date" in header line, | ||
1436 | 1419 | * then add the special empty headers | ||
1437 | 1420 | */ | ||
1438 | 1421 | |||
1439 | 1422 | if (r->upstream->headers_in.server == NULL) { | ||
1440 | 1423 | h = ngx_list_push(&r->upstream->headers_in.headers); | ||
1441 | 1424 | if (h == NULL) { | ||
1442 | 1425 | return NGX_ERROR; | ||
1443 | 1426 | } | ||
1444 | 1427 | |||
1445 | 1428 | h->hash = ngx_hash(ngx_hash(ngx_hash(ngx_hash( | ||
1446 | 1429 | ngx_hash('s', 'e'), 'r'), 'v'), 'e'), 'r'); | ||
1447 | 1430 | |||
1448 | 1431 | ngx_str_set(&h->key, "Server"); | ||
1449 | 1432 | ngx_str_null(&h->value); | ||
1450 | 1433 | h->lowcase_key = (u_char *) "server"; | ||
1451 | 1434 | } | ||
1452 | 1435 | |||
1453 | 1436 | if (r->upstream->headers_in.date == NULL) { | ||
1454 | 1437 | h = ngx_list_push(&r->upstream->headers_in.headers); | ||
1455 | 1438 | if (h == NULL) { | ||
1456 | 1439 | return NGX_ERROR; | ||
1457 | 1440 | } | ||
1458 | 1441 | |||
1459 | 1442 | h->hash = ngx_hash(ngx_hash(ngx_hash('d', 'a'), 't'), 'e'); | ||
1460 | 1443 | |||
1461 | 1444 | ngx_str_set(&h->key, "Date"); | ||
1462 | 1445 | ngx_str_null(&h->value); | ||
1463 | 1446 | h->lowcase_key = (u_char *) "date"; | ||
1464 | 1447 | } | ||
1465 | 1448 | |||
1466 | 1449 | /* clear content length if response is chunked */ | ||
1467 | 1450 | |||
1468 | 1451 | u = r->upstream; | ||
1469 | 1452 | |||
1470 | 1453 | if (u->headers_in.chunked) { | ||
1471 | 1454 | u->headers_in.content_length_n = -1; | ||
1472 | 1455 | } | ||
1473 | 1456 | |||
1474 | 1457 | /* | ||
1475 | 1458 | * set u->keepalive if response has no body; this allows to keep | ||
1476 | 1459 | * connections alive in case of r->header_only or X-Accel-Redirect | ||
1477 | 1460 | */ | ||
1478 | 1461 | |||
1479 | 1462 | ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module); | ||
1480 | 1463 | |||
1481 | 1464 | if (u->headers_in.status_n == NGX_HTTP_NO_CONTENT | ||
1482 | 1465 | || u->headers_in.status_n == NGX_HTTP_NOT_MODIFIED | ||
1483 | 1466 | || ctx->head | ||
1484 | 1467 | || (!u->headers_in.chunked | ||
1485 | 1468 | && u->headers_in.content_length_n == 0)) | ||
1486 | 1469 | { | ||
1487 | 1470 | u->keepalive = !u->headers_in.connection_close; | ||
1488 | 1471 | } | ||
1489 | 1472 | |||
1490 | 1473 | return NGX_OK; | ||
1491 | 1474 | } | ||
1492 | 1475 | |||
1493 | 1476 | if (rc == NGX_AGAIN) { | ||
1494 | 1477 | return NGX_AGAIN; | ||
1495 | 1478 | } | ||
1496 | 1479 | |||
1497 | 1480 | /* there was error while a header line parsing */ | ||
1498 | 1481 | |||
1499 | 1482 | ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | ||
1500 | 1483 | "upstream sent invalid header"); | ||
1501 | 1484 | |||
1502 | 1485 | return NGX_HTTP_UPSTREAM_INVALID_HEADER; | ||
1503 | 1486 | } | ||
1504 | 1487 | } | ||
1505 | 1488 | |||
1506 | 1489 | |||
1507 | 1490 | static ngx_int_t | ||
1508 | 1491 | ngx_http_proxy_input_filter_init(void *data) | ||
1509 | 1492 | { | ||
1510 | 1493 | ngx_http_request_t *r = data; | ||
1511 | 1494 | ngx_http_upstream_t *u; | ||
1512 | 1495 | ngx_http_proxy_ctx_t *ctx; | ||
1513 | 1496 | |||
1514 | 1497 | u = r->upstream; | ||
1515 | 1498 | ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module); | ||
1516 | 1499 | |||
1517 | 1500 | if (ctx == NULL) { | ||
1518 | 1501 | return NGX_ERROR; | ||
1519 | 1502 | } | ||
1520 | 1503 | |||
1521 | 1504 | ngx_log_debug4(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | ||
1522 | 1505 | "http proxy filter init s:%d h:%d c:%d l:%O", | ||
1523 | 1506 | u->headers_in.status_n, ctx->head, u->headers_in.chunked, | ||
1524 | 1507 | u->headers_in.content_length_n); | ||
1525 | 1508 | |||
1526 | 1509 | /* as per RFC2616, 4.4 Message Length */ | ||
1527 | 1510 | |||
1528 | 1511 | if (u->headers_in.status_n == NGX_HTTP_NO_CONTENT | ||
1529 | 1512 | || u->headers_in.status_n == NGX_HTTP_NOT_MODIFIED | ||
1530 | 1513 | || ctx->head) | ||
1531 | 1514 | { | ||
1532 | 1515 | /* 1xx, 204, and 304 and replies to HEAD requests */ | ||
1533 | 1516 | /* no 1xx since we don't send Expect and Upgrade */ | ||
1534 | 1517 | |||
1535 | 1518 | u->pipe->length = 0; | ||
1536 | 1519 | u->length = 0; | ||
1537 | 1520 | u->keepalive = !u->headers_in.connection_close; | ||
1538 | 1521 | |||
1539 | 1522 | } else if (u->headers_in.chunked) { | ||
1540 | 1523 | /* chunked */ | ||
1541 | 1524 | |||
1542 | 1525 | u->pipe->input_filter = ngx_http_proxy_chunked_filter; | ||
1543 | 1526 | u->pipe->length = 3; /* "0" LF LF */ | ||
1544 | 1527 | |||
1545 | 1528 | u->input_filter = ngx_http_proxy_non_buffered_chunked_filter; | ||
1546 | 1529 | u->length = -1; | ||
1547 | 1530 | |||
1548 | 1531 | } else if (u->headers_in.content_length_n == 0) { | ||
1549 | 1532 | /* empty body: special case as filter won't be called */ | ||
1550 | 1533 | |||
1551 | 1534 | u->pipe->length = 0; | ||
1552 | 1535 | u->length = 0; | ||
1553 | 1536 | u->keepalive = !u->headers_in.connection_close; | ||
1554 | 1537 | |||
1555 | 1538 | } else { | ||
1556 | 1539 | /* content length or connection close */ | ||
1557 | 1540 | |||
1558 | 1541 | u->pipe->length = u->headers_in.content_length_n; | ||
1559 | 1542 | u->length = u->headers_in.content_length_n; | ||
1560 | 1543 | } | ||
1561 | 1544 | |||
1562 | 1545 | return NGX_OK; | ||
1563 | 1546 | } | ||
1564 | 1547 | |||
1565 | 1548 | |||
1566 | 1549 | static ngx_int_t | ||
1567 | 1550 | ngx_http_proxy_copy_filter(ngx_event_pipe_t *p, ngx_buf_t *buf) | ||
1568 | 1551 | { | ||
1569 | 1552 | ngx_buf_t *b; | ||
1570 | 1553 | ngx_chain_t *cl; | ||
1571 | 1554 | ngx_http_request_t *r; | ||
1572 | 1555 | |||
1573 | 1556 | if (buf->pos == buf->last) { | ||
1574 | 1557 | return NGX_OK; | ||
1575 | 1558 | } | ||
1576 | 1559 | |||
1577 | 1560 | if (p->free) { | ||
1578 | 1561 | cl = p->free; | ||
1579 | 1562 | b = cl->buf; | ||
1580 | 1563 | p->free = cl->next; | ||
1581 | 1564 | ngx_free_chain(p->pool, cl); | ||
1582 | 1565 | |||
1583 | 1566 | } else { | ||
1584 | 1567 | b = ngx_alloc_buf(p->pool); | ||
1585 | 1568 | if (b == NULL) { | ||
1586 | 1569 | return NGX_ERROR; | ||
1587 | 1570 | } | ||
1588 | 1571 | } | ||
1589 | 1572 | |||
1590 | 1573 | ngx_memcpy(b, buf, sizeof(ngx_buf_t)); | ||
1591 | 1574 | b->shadow = buf; | ||
1592 | 1575 | b->tag = p->tag; | ||
1593 | 1576 | b->last_shadow = 1; | ||
1594 | 1577 | b->recycled = 1; | ||
1595 | 1578 | buf->shadow = b; | ||
1596 | 1579 | |||
1597 | 1580 | cl = ngx_alloc_chain_link(p->pool); | ||
1598 | 1581 | if (cl == NULL) { | ||
1599 | 1582 | return NGX_ERROR; | ||
1600 | 1583 | } | ||
1601 | 1584 | |||
1602 | 1585 | cl->buf = b; | ||
1603 | 1586 | cl->next = NULL; | ||
1604 | 1587 | |||
1605 | 1588 | ngx_log_debug1(NGX_LOG_DEBUG_EVENT, p->log, 0, "input buf #%d", b->num); | ||
1606 | 1589 | |||
1607 | 1590 | if (p->in) { | ||
1608 | 1591 | *p->last_in = cl; | ||
1609 | 1592 | } else { | ||
1610 | 1593 | p->in = cl; | ||
1611 | 1594 | } | ||
1612 | 1595 | p->last_in = &cl->next; | ||
1613 | 1596 | |||
1614 | 1597 | if (p->length == -1) { | ||
1615 | 1598 | return NGX_OK; | ||
1616 | 1599 | } | ||
1617 | 1600 | |||
1618 | 1601 | p->length -= b->last - b->pos; | ||
1619 | 1602 | |||
1620 | 1603 | if (p->length == 0) { | ||
1621 | 1604 | r = p->input_ctx; | ||
1622 | 1605 | p->upstream_done = 1; | ||
1623 | 1606 | r->upstream->keepalive = !r->upstream->headers_in.connection_close; | ||
1624 | 1607 | |||
1625 | 1608 | } else if (p->length < 0) { | ||
1626 | 1609 | r = p->input_ctx; | ||
1627 | 1610 | p->upstream_done = 1; | ||
1628 | 1611 | |||
1629 | 1612 | ngx_log_error(NGX_LOG_WARN, r->connection->log, 0, | ||
1630 | 1613 | "upstream sent too much data"); | ||
1631 | 1614 | } | ||
1632 | 1615 | |||
1633 | 1616 | return NGX_OK; | ||
1634 | 1617 | } | ||
1635 | 1618 | |||
1636 | 1619 | |||
1637 | 1620 | static ngx_inline ngx_int_t | ||
1638 | 1621 | ngx_http_proxy_parse_chunked(ngx_http_request_t *r, ngx_buf_t *buf) | ||
1639 | 1622 | { | ||
1640 | 1623 | u_char *pos, ch, c; | ||
1641 | 1624 | ngx_int_t rc; | ||
1642 | 1625 | ngx_http_proxy_ctx_t *ctx; | ||
1643 | 1626 | enum { | ||
1644 | 1627 | sw_chunk_start = 0, | ||
1645 | 1628 | sw_chunk_size, | ||
1646 | 1629 | sw_chunk_extension, | ||
1647 | 1630 | sw_chunk_extension_almost_done, | ||
1648 | 1631 | sw_chunk_data, | ||
1649 | 1632 | sw_after_data, | ||
1650 | 1633 | sw_after_data_almost_done, | ||
1651 | 1634 | sw_last_chunk_extension, | ||
1652 | 1635 | sw_last_chunk_extension_almost_done, | ||
1653 | 1636 | sw_trailer, | ||
1654 | 1637 | sw_trailer_almost_done, | ||
1655 | 1638 | sw_trailer_header, | ||
1656 | 1639 | sw_trailer_header_almost_done | ||
1657 | 1640 | } state; | ||
1658 | 1641 | |||
1659 | 1642 | ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module); | ||
1660 | 1643 | |||
1661 | 1644 | if (ctx == NULL) { | ||
1662 | 1645 | return NGX_ERROR; | ||
1663 | 1646 | } | ||
1664 | 1647 | |||
1665 | 1648 | state = ctx->state; | ||
1666 | 1649 | |||
1667 | 1650 | if (state == sw_chunk_data && ctx->size == 0) { | ||
1668 | 1651 | state = sw_after_data; | ||
1669 | 1652 | } | ||
1670 | 1653 | |||
1671 | 1654 | rc = NGX_AGAIN; | ||
1672 | 1655 | |||
1673 | 1656 | for (pos = buf->pos; pos < buf->last; pos++) { | ||
1674 | 1657 | |||
1675 | 1658 | ch = *pos; | ||
1676 | 1659 | |||
1677 | 1660 | ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | ||
1678 | 1661 | "http proxy chunked byte: %02Xd s:%d", ch, state); | ||
1679 | 1662 | |||
1680 | 1663 | switch (state) { | ||
1681 | 1664 | |||
1682 | 1665 | case sw_chunk_start: | ||
1683 | 1666 | if (ch >= '0' && ch <= '9') { | ||
1684 | 1667 | state = sw_chunk_size; | ||
1685 | 1668 | ctx->size = ch - '0'; | ||
1686 | 1669 | break; | ||
1687 | 1670 | } | ||
1688 | 1671 | |||
1689 | 1672 | c = (u_char) (ch | 0x20); | ||
1690 | 1673 | |||
1691 | 1674 | if (c >= 'a' && c <= 'f') { | ||
1692 | 1675 | state = sw_chunk_size; | ||
1693 | 1676 | ctx->size = c - 'a' + 10; | ||
1694 | 1677 | break; | ||
1695 | 1678 | } | ||
1696 | 1679 | |||
1697 | 1680 | goto invalid; | ||
1698 | 1681 | |||
1699 | 1682 | case sw_chunk_size: | ||
1700 | 1683 | if (ch >= '0' && ch <= '9') { | ||
1701 | 1684 | ctx->size = ctx->size * 16 + (ch - '0'); | ||
1702 | 1685 | break; | ||
1703 | 1686 | } | ||
1704 | 1687 | |||
1705 | 1688 | c = (u_char) (ch | 0x20); | ||
1706 | 1689 | |||
1707 | 1690 | if (c >= 'a' && c <= 'f') { | ||
1708 | 1691 | ctx->size = ctx->size * 16 + (c - 'a' + 10); | ||
1709 | 1692 | break; | ||
1710 | 1693 | } | ||
1711 | 1694 | |||
1712 | 1695 | if (ctx->size == 0) { | ||
1713 | 1696 | |||
1714 | 1697 | switch (ch) { | ||
1715 | 1698 | case CR: | ||
1716 | 1699 | state = sw_last_chunk_extension_almost_done; | ||
1717 | 1700 | break; | ||
1718 | 1701 | case LF: | ||
1719 | 1702 | state = sw_trailer; | ||
1720 | 1703 | break; | ||
1721 | 1704 | case ';': | ||
1722 | 1705 | case ' ': | ||
1723 | 1706 | case '\t': | ||
1724 | 1707 | state = sw_last_chunk_extension; | ||
1725 | 1708 | break; | ||
1726 | 1709 | default: | ||
1727 | 1710 | goto invalid; | ||
1728 | 1711 | } | ||
1729 | 1712 | |||
1730 | 1713 | break; | ||
1731 | 1714 | } | ||
1732 | 1715 | |||
1733 | 1716 | switch (ch) { | ||
1734 | 1717 | case CR: | ||
1735 | 1718 | state = sw_chunk_extension_almost_done; | ||
1736 | 1719 | break; | ||
1737 | 1720 | case LF: | ||
1738 | 1721 | state = sw_chunk_data; | ||
1739 | 1722 | break; | ||
1740 | 1723 | case ';': | ||
1741 | 1724 | case ' ': | ||
1742 | 1725 | case '\t': | ||
1743 | 1726 | state = sw_chunk_extension; | ||
1744 | 1727 | break; | ||
1745 | 1728 | default: | ||
1746 | 1729 | goto invalid; | ||
1747 | 1730 | } | ||
1748 | 1731 | |||
1749 | 1732 | break; | ||
1750 | 1733 | |||
1751 | 1734 | case sw_chunk_extension: | ||
1752 | 1735 | switch (ch) { | ||
1753 | 1736 | case CR: | ||
1754 | 1737 | state = sw_chunk_extension_almost_done; | ||
1755 | 1738 | break; | ||
1756 | 1739 | case LF: | ||
1757 | 1740 | state = sw_chunk_data; | ||
1758 | 1741 | } | ||
1759 | 1742 | break; | ||
1760 | 1743 | |||
1761 | 1744 | case sw_chunk_extension_almost_done: | ||
1762 | 1745 | if (ch == LF) { | ||
1763 | 1746 | state = sw_chunk_data; | ||
1764 | 1747 | break; | ||
1765 | 1748 | } | ||
1766 | 1749 | goto invalid; | ||
1767 | 1750 | |||
1768 | 1751 | case sw_chunk_data: | ||
1769 | 1752 | rc = NGX_OK; | ||
1770 | 1753 | goto data; | ||
1771 | 1754 | |||
1772 | 1755 | case sw_after_data: | ||
1773 | 1756 | switch (ch) { | ||
1774 | 1757 | case CR: | ||
1775 | 1758 | state = sw_after_data_almost_done; | ||
1776 | 1759 | break; | ||
1777 | 1760 | case LF: | ||
1778 | 1761 | state = sw_chunk_start; | ||
1779 | 1762 | } | ||
1780 | 1763 | break; | ||
1781 | 1764 | |||
1782 | 1765 | case sw_after_data_almost_done: | ||
1783 | 1766 | if (ch == LF) { | ||
1784 | 1767 | state = sw_chunk_start; | ||
1785 | 1768 | break; | ||
1786 | 1769 | } | ||
1787 | 1770 | goto invalid; | ||
1788 | 1771 | |||
1789 | 1772 | case sw_last_chunk_extension: | ||
1790 | 1773 | switch (ch) { | ||
1791 | 1774 | case CR: | ||
1792 | 1775 | state = sw_last_chunk_extension_almost_done; | ||
1793 | 1776 | break; | ||
1794 | 1777 | case LF: | ||
1795 | 1778 | state = sw_trailer; | ||
1796 | 1779 | } | ||
1797 | 1780 | break; | ||
1798 | 1781 | |||
1799 | 1782 | case sw_last_chunk_extension_almost_done: | ||
1800 | 1783 | if (ch == LF) { | ||
1801 | 1784 | state = sw_trailer; | ||
1802 | 1785 | break; | ||
1803 | 1786 | } | ||
1804 | 1787 | goto invalid; | ||
1805 | 1788 | |||
1806 | 1789 | case sw_trailer: | ||
1807 | 1790 | switch (ch) { | ||
1808 | 1791 | case CR: | ||
1809 | 1792 | state = sw_trailer_almost_done; | ||
1810 | 1793 | break; | ||
1811 | 1794 | case LF: | ||
1812 | 1795 | goto done; | ||
1813 | 1796 | default: | ||
1814 | 1797 | state = sw_trailer_header; | ||
1815 | 1798 | } | ||
1816 | 1799 | break; | ||
1817 | 1800 | |||
1818 | 1801 | case sw_trailer_almost_done: | ||
1819 | 1802 | if (ch == LF) { | ||
1820 | 1803 | goto done; | ||
1821 | 1804 | } | ||
1822 | 1805 | goto invalid; | ||
1823 | 1806 | |||
1824 | 1807 | case sw_trailer_header: | ||
1825 | 1808 | switch (ch) { | ||
1826 | 1809 | case CR: | ||
1827 | 1810 | state = sw_trailer_header_almost_done; | ||
1828 | 1811 | break; | ||
1829 | 1812 | case LF: | ||
1830 | 1813 | state = sw_trailer; | ||
1831 | 1814 | } | ||
1832 | 1815 | break; | ||
1833 | 1816 | |||
1834 | 1817 | case sw_trailer_header_almost_done: | ||
1835 | 1818 | if (ch == LF) { | ||
1836 | 1819 | state = sw_trailer; | ||
1837 | 1820 | break; | ||
1838 | 1821 | } | ||
1839 | 1822 | goto invalid; | ||
1840 | 1823 | |||
1841 | 1824 | } | ||
1842 | 1825 | } | ||
1843 | 1826 | |||
1844 | 1827 | data: | ||
1845 | 1828 | |||
1846 | 1829 | ctx->state = state; | ||
1847 | 1830 | buf->pos = pos; | ||
1848 | 1831 | |||
1849 | 1832 | switch (state) { | ||
1850 | 1833 | |||
1851 | 1834 | case sw_chunk_start: | ||
1852 | 1835 | ctx->length = 3 /* "0" LF LF */; | ||
1853 | 1836 | break; | ||
1854 | 1837 | case sw_chunk_size: | ||
1855 | 1838 | ctx->length = 2 /* LF LF */ | ||
1856 | 1839 | + (ctx->size ? ctx->size + 4 /* LF "0" LF LF */ : 0); | ||
1857 | 1840 | break; | ||
1858 | 1841 | case sw_chunk_extension: | ||
1859 | 1842 | case sw_chunk_extension_almost_done: | ||
1860 | 1843 | ctx->length = 1 /* LF */ + ctx->size + 4 /* LF "0" LF LF */; | ||
1861 | 1844 | break; | ||
1862 | 1845 | case sw_chunk_data: | ||
1863 | 1846 | ctx->length = ctx->size + 4 /* LF "0" LF LF */; | ||
1864 | 1847 | break; | ||
1865 | 1848 | case sw_after_data: | ||
1866 | 1849 | case sw_after_data_almost_done: | ||
1867 | 1850 | ctx->length = 4 /* LF "0" LF LF */; | ||
1868 | 1851 | break; | ||
1869 | 1852 | case sw_last_chunk_extension: | ||
1870 | 1853 | case sw_last_chunk_extension_almost_done: | ||
1871 | 1854 | ctx->length = 2 /* LF LF */; | ||
1872 | 1855 | break; | ||
1873 | 1856 | case sw_trailer: | ||
1874 | 1857 | case sw_trailer_almost_done: | ||
1875 | 1858 | ctx->length = 1 /* LF */; | ||
1876 | 1859 | break; | ||
1877 | 1860 | case sw_trailer_header: | ||
1878 | 1861 | case sw_trailer_header_almost_done: | ||
1879 | 1862 | ctx->length = 2 /* LF LF */; | ||
1880 | 1863 | break; | ||
1881 | 1864 | |||
1882 | 1865 | } | ||
1883 | 1866 | |||
1884 | 1867 | return rc; | ||
1885 | 1868 | |||
1886 | 1869 | done: | ||
1887 | 1870 | |||
1888 | 1871 | return NGX_DONE; | ||
1889 | 1872 | |||
1890 | 1873 | invalid: | ||
1891 | 1874 | |||
1892 | 1875 | return NGX_ERROR; | ||
1893 | 1876 | } | ||
1894 | 1877 | |||
1895 | 1878 | |||
1896 | 1879 | static ngx_int_t | ||
1897 | 1880 | ngx_http_proxy_chunked_filter(ngx_event_pipe_t *p, ngx_buf_t *buf) | ||
1898 | 1881 | { | ||
1899 | 1882 | ngx_int_t rc; | ||
1900 | 1883 | ngx_buf_t *b, **prev; | ||
1901 | 1884 | ngx_chain_t *cl; | ||
1902 | 1885 | ngx_http_request_t *r; | ||
1903 | 1886 | ngx_http_proxy_ctx_t *ctx; | ||
1904 | 1887 | |||
1905 | 1888 | if (buf->pos == buf->last) { | ||
1906 | 1889 | return NGX_OK; | ||
1907 | 1890 | } | ||
1908 | 1891 | |||
1909 | 1892 | r = p->input_ctx; | ||
1910 | 1893 | ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module); | ||
1911 | 1894 | |||
1912 | 1895 | if (ctx == NULL) { | ||
1913 | 1896 | return NGX_ERROR; | ||
1914 | 1897 | } | ||
1915 | 1898 | |||
1916 | 1899 | b = NULL; | ||
1917 | 1900 | prev = &buf->shadow; | ||
1918 | 1901 | |||
1919 | 1902 | for ( ;; ) { | ||
1920 | 1903 | |||
1921 | 1904 | rc = ngx_http_proxy_parse_chunked(r, buf); | ||
1922 | 1905 | |||
1923 | 1906 | if (rc == NGX_OK) { | ||
1924 | 1907 | |||
1925 | 1908 | /* a chunk has been parsed successfully */ | ||
1926 | 1909 | |||
1927 | 1910 | if (p->free) { | ||
1928 | 1911 | cl = p->free; | ||
1929 | 1912 | b = cl->buf; | ||
1930 | 1913 | p->free = cl->next; | ||
1931 | 1914 | ngx_free_chain(p->pool, cl); | ||
1932 | 1915 | |||
1933 | 1916 | } else { | ||
1934 | 1917 | b = ngx_alloc_buf(p->pool); | ||
1935 | 1918 | if (b == NULL) { | ||
1936 | 1919 | return NGX_ERROR; | ||
1937 | 1920 | } | ||
1938 | 1921 | } | ||
1939 | 1922 | |||
1940 | 1923 | ngx_memzero(b, sizeof(ngx_buf_t)); | ||
1941 | 1924 | |||
1942 | 1925 | b->pos = buf->pos; | ||
1943 | 1926 | b->start = buf->start; | ||
1944 | 1927 | b->end = buf->end; | ||
1945 | 1928 | b->tag = p->tag; | ||
1946 | 1929 | b->temporary = 1; | ||
1947 | 1930 | b->recycled = 1; | ||
1948 | 1931 | |||
1949 | 1932 | *prev = b; | ||
1950 | 1933 | prev = &b->shadow; | ||
1951 | 1934 | |||
1952 | 1935 | cl = ngx_alloc_chain_link(p->pool); | ||
1953 | 1936 | if (cl == NULL) { | ||
1954 | 1937 | return NGX_ERROR; | ||
1955 | 1938 | } | ||
1956 | 1939 | |||
1957 | 1940 | cl->buf = b; | ||
1958 | 1941 | cl->next = NULL; | ||
1959 | 1942 | |||
1960 | 1943 | if (p->in) { | ||
1961 | 1944 | *p->last_in = cl; | ||
1962 | 1945 | } else { | ||
1963 | 1946 | p->in = cl; | ||
1964 | 1947 | } | ||
1965 | 1948 | p->last_in = &cl->next; | ||
1966 | 1949 | |||
1967 | 1950 | /* STUB */ b->num = buf->num; | ||
1968 | 1951 | |||
1969 | 1952 | ngx_log_debug2(NGX_LOG_DEBUG_EVENT, p->log, 0, | ||
1970 | 1953 | "input buf #%d %p", b->num, b->pos); | ||
1971 | 1954 | |||
1972 | 1955 | if (buf->last - buf->pos >= ctx->size) { | ||
1973 | 1956 | |||
1974 | 1957 | buf->pos += ctx->size; | ||
1975 | 1958 | b->last = buf->pos; | ||
1976 | 1959 | ctx->size = 0; | ||
1977 | 1960 | |||
1978 | 1961 | continue; | ||
1979 | 1962 | } | ||
1980 | 1963 | |||
1981 | 1964 | ctx->size -= buf->last - buf->pos; | ||
1982 | 1965 | buf->pos = buf->last; | ||
1983 | 1966 | b->last = buf->last; | ||
1984 | 1967 | |||
1985 | 1968 | continue; | ||
1986 | 1969 | } | ||
1987 | 1970 | |||
1988 | 1971 | if (rc == NGX_DONE) { | ||
1989 | 1972 | |||
1990 | 1973 | /* a whole response has been parsed successfully */ | ||
1991 | 1974 | |||
1992 | 1975 | p->upstream_done = 1; | ||
1993 | 1976 | r->upstream->keepalive = !r->upstream->headers_in.connection_close; | ||
1994 | 1977 | |||
1995 | 1978 | break; | ||
1996 | 1979 | } | ||
1997 | 1980 | |||
1998 | 1981 | if (rc == NGX_AGAIN) { | ||
1999 | 1982 | |||
2000 | 1983 | /* set p->length, minimal amount of data we want to see */ | ||
2001 | 1984 | |||
2002 | 1985 | p->length = ctx->length; | ||
2003 | 1986 | |||
2004 | 1987 | break; | ||
2005 | 1988 | } | ||
2006 | 1989 | |||
2007 | 1990 | /* invalid response */ | ||
2008 | 1991 | |||
2009 | 1992 | ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | ||
2010 | 1993 | "upstream sent invalid chunked response"); | ||
2011 | 1994 | |||
2012 | 1995 | return NGX_ERROR; | ||
2013 | 1996 | } | ||
2014 | 1997 | |||
2015 | 1998 | ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | ||
2016 | 1999 | "http proxy chunked state %d, length %d", | ||
2017 | 2000 | ctx->state, p->length); | ||
2018 | 2001 | |||
2019 | 2002 | if (b) { | ||
2020 | 2003 | b->shadow = buf; | ||
2021 | 2004 | b->last_shadow = 1; | ||
2022 | 2005 | |||
2023 | 2006 | ngx_log_debug2(NGX_LOG_DEBUG_EVENT, p->log, 0, | ||
2024 | 2007 | "input buf %p %z", b->pos, b->last - b->pos); | ||
2025 | 2008 | |||
2026 | 2009 | return NGX_OK; | ||
2027 | 2010 | } | ||
2028 | 2011 | |||
2029 | 2012 | /* there is no data record in the buf, add it to free chain */ | ||
2030 | 2013 | |||
2031 | 2014 | if (ngx_event_pipe_add_free_buf(p, buf) != NGX_OK) { | ||
2032 | 2015 | return NGX_ERROR; | ||
2033 | 2016 | } | ||
2034 | 2017 | |||
2035 | 2018 | return NGX_OK; | ||
2036 | 2019 | } | ||
2037 | 2020 | |||
2038 | 2021 | |||
2039 | 2022 | static ngx_int_t | ||
2040 | 2023 | ngx_http_proxy_non_buffered_copy_filter(void *data, ssize_t bytes) | ||
2041 | 2024 | { | ||
2042 | 2025 | ngx_http_request_t *r = data; | ||
2043 | 2026 | |||
2044 | 2027 | ngx_buf_t *b; | ||
2045 | 2028 | ngx_chain_t *cl, **ll; | ||
2046 | 2029 | ngx_http_upstream_t *u; | ||
2047 | 2030 | |||
2048 | 2031 | u = r->upstream; | ||
2049 | 2032 | |||
2050 | 2033 | for (cl = u->out_bufs, ll = &u->out_bufs; cl; cl = cl->next) { | ||
2051 | 2034 | ll = &cl->next; | ||
2052 | 2035 | } | ||
2053 | 2036 | |||
2054 | 2037 | cl = ngx_chain_get_free_buf(r->pool, &u->free_bufs); | ||
2055 | 2038 | if (cl == NULL) { | ||
2056 | 2039 | return NGX_ERROR; | ||
2057 | 2040 | } | ||
2058 | 2041 | |||
2059 | 2042 | *ll = cl; | ||
2060 | 2043 | |||
2061 | 2044 | cl->buf->flush = 1; | ||
2062 | 2045 | cl->buf->memory = 1; | ||
2063 | 2046 | |||
2064 | 2047 | b = &u->buffer; | ||
2065 | 2048 | |||
2066 | 2049 | cl->buf->pos = b->last; | ||
2067 | 2050 | b->last += bytes; | ||
2068 | 2051 | cl->buf->last = b->last; | ||
2069 | 2052 | cl->buf->tag = u->output.tag; | ||
2070 | 2053 | |||
2071 | 2054 | if (u->length == -1) { | ||
2072 | 2055 | return NGX_OK; | ||
2073 | 2056 | } | ||
2074 | 2057 | |||
2075 | 2058 | u->length -= bytes; | ||
2076 | 2059 | |||
2077 | 2060 | if (u->length == 0) { | ||
2078 | 2061 | u->keepalive = !u->headers_in.connection_close; | ||
2079 | 2062 | } | ||
2080 | 2063 | |||
2081 | 2064 | return NGX_OK; | ||
2082 | 2065 | } | ||
2083 | 2066 | |||
2084 | 2067 | |||
2085 | 2068 | static ngx_int_t | ||
2086 | 2069 | ngx_http_proxy_non_buffered_chunked_filter(void *data, ssize_t bytes) | ||
2087 | 2070 | { | ||
2088 | 2071 | ngx_http_request_t *r = data; | ||
2089 | 2072 | |||
2090 | 2073 | ngx_int_t rc; | ||
2091 | 2074 | ngx_buf_t *b, *buf; | ||
2092 | 2075 | ngx_chain_t *cl, **ll; | ||
2093 | 2076 | ngx_http_upstream_t *u; | ||
2094 | 2077 | ngx_http_proxy_ctx_t *ctx; | ||
2095 | 2078 | |||
2096 | 2079 | ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module); | ||
2097 | 2080 | |||
2098 | 2081 | if (ctx == NULL) { | ||
2099 | 2082 | return NGX_ERROR; | ||
2100 | 2083 | } | ||
2101 | 2084 | |||
2102 | 2085 | u = r->upstream; | ||
2103 | 2086 | buf = &u->buffer; | ||
2104 | 2087 | |||
2105 | 2088 | buf->pos = buf->last; | ||
2106 | 2089 | buf->last += bytes; | ||
2107 | 2090 | |||
2108 | 2091 | for (cl = u->out_bufs, ll = &u->out_bufs; cl; cl = cl->next) { | ||
2109 | 2092 | ll = &cl->next; | ||
2110 | 2093 | } | ||
2111 | 2094 | |||
2112 | 2095 | for ( ;; ) { | ||
2113 | 2096 | |||
2114 | 2097 | rc = ngx_http_proxy_parse_chunked(r, buf); | ||
2115 | 2098 | |||
2116 | 2099 | if (rc == NGX_OK) { | ||
2117 | 2100 | |||
2118 | 2101 | /* a chunk has been parsed successfully */ | ||
2119 | 2102 | |||
2120 | 2103 | cl = ngx_chain_get_free_buf(r->pool, &u->free_bufs); | ||
2121 | 2104 | if (cl == NULL) { | ||
2122 | 2105 | return NGX_ERROR; | ||
2123 | 2106 | } | ||
2124 | 2107 | |||
2125 | 2108 | *ll = cl; | ||
2126 | 2109 | ll = &cl->next; | ||
2127 | 2110 | |||
2128 | 2111 | b = cl->buf; | ||
2129 | 2112 | |||
2130 | 2113 | b->flush = 1; | ||
2131 | 2114 | b->memory = 1; | ||
2132 | 2115 | |||
2133 | 2116 | b->pos = buf->pos; | ||
2134 | 2117 | b->tag = u->output.tag; | ||
2135 | 2118 | |||
2136 | 2119 | if (buf->last - buf->pos >= ctx->size) { | ||
2137 | 2120 | buf->pos += ctx->size; | ||
2138 | 2121 | b->last = buf->pos; | ||
2139 | 2122 | ctx->size = 0; | ||
2140 | 2123 | |||
2141 | 2124 | } else { | ||
2142 | 2125 | ctx->size -= buf->last - buf->pos; | ||
2143 | 2126 | buf->pos = buf->last; | ||
2144 | 2127 | b->last = buf->last; | ||
2145 | 2128 | } | ||
2146 | 2129 | |||
2147 | 2130 | ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | ||
2148 | 2131 | "http proxy out buf %p %z", | ||
2149 | 2132 | b->pos, b->last - b->pos); | ||
2150 | 2133 | |||
2151 | 2134 | continue; | ||
2152 | 2135 | } | ||
2153 | 2136 | |||
2154 | 2137 | if (rc == NGX_DONE) { | ||
2155 | 2138 | |||
2156 | 2139 | /* a whole response has been parsed successfully */ | ||
2157 | 2140 | |||
2158 | 2141 | u->keepalive = !u->headers_in.connection_close; | ||
2159 | 2142 | u->length = 0; | ||
2160 | 2143 | |||
2161 | 2144 | break; | ||
2162 | 2145 | } | ||
2163 | 2146 | |||
2164 | 2147 | if (rc == NGX_AGAIN) { | ||
2165 | 2148 | break; | ||
2166 | 2149 | } | ||
2167 | 2150 | |||
2168 | 2151 | /* invalid response */ | ||
2169 | 2152 | |||
2170 | 2153 | ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | ||
2171 | 2154 | "upstream sent invalid chunked response"); | ||
2172 | 2155 | |||
2173 | 2156 | return NGX_ERROR; | ||
2174 | 2157 | } | ||
2175 | 2158 | |||
2176 | 2159 | /* provide continuous buffer for subrequests in memory */ | ||
2177 | 2160 | |||
2178 | 2161 | if (r->subrequest_in_memory) { | ||
2179 | 2162 | |||
2180 | 2163 | cl = u->out_bufs; | ||
2181 | 2164 | |||
2182 | 2165 | if (cl) { | ||
2183 | 2166 | buf->pos = cl->buf->pos; | ||
2184 | 2167 | } | ||
2185 | 2168 | |||
2186 | 2169 | buf->last = buf->pos; | ||
2187 | 2170 | |||
2188 | 2171 | for (cl = u->out_bufs; cl; cl = cl->next) { | ||
2189 | 2172 | ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | ||
2190 | 2173 | "http proxy in memory %p-%p %uz", | ||
2191 | 2174 | cl->buf->pos, cl->buf->last, ngx_buf_size(cl->buf)); | ||
2192 | 2175 | |||
2193 | 2176 | if (buf->last == cl->buf->pos) { | ||
2194 | 2177 | buf->last = cl->buf->last; | ||
2195 | 2178 | continue; | ||
2196 | 2179 | } | ||
2197 | 2180 | |||
2198 | 2181 | buf->last = ngx_movemem(buf->last, cl->buf->pos, | ||
2199 | 2182 | cl->buf->last - cl->buf->pos); | ||
2200 | 2183 | |||
2201 | 2184 | cl->buf->pos = buf->last - (cl->buf->last - cl->buf->pos); | ||
2202 | 2185 | cl->buf->last = buf->last; | ||
2203 | 2186 | } | ||
2204 | 2187 | } | ||
2205 | 2188 | |||
2206 | 2189 | return NGX_OK; | ||
2207 | 2190 | } | ||
2208 | 2191 | |||
2209 | 2192 | |||
2210 | 2193 | static void | ||
2211 | 2194 | ngx_http_proxy_abort_request(ngx_http_request_t *r) | ||
2212 | 2195 | { | ||
2213 | 2196 | ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | ||
2214 | 2197 | "abort http proxy request"); | ||
2215 | 2198 | |||
2216 | 2199 | return; | ||
2217 | 2200 | } | ||
2218 | 2201 | |||
2219 | 2202 | |||
2220 | 2203 | static void | ||
2221 | 2204 | ngx_http_proxy_finalize_request(ngx_http_request_t *r, ngx_int_t rc) | ||
2222 | 2205 | { | ||
2223 | 2206 | ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | ||
2224 | 2207 | "finalize http proxy request"); | ||
2225 | 2208 | |||
2226 | 2209 | return; | ||
2227 | 2210 | } | ||
2228 | 2211 | |||
2229 | 2212 | |||
2230 | 2213 | static ngx_int_t | ||
2231 | 2214 | ngx_http_proxy_host_variable(ngx_http_request_t *r, | ||
2232 | 2215 | ngx_http_variable_value_t *v, uintptr_t data) | ||
2233 | 2216 | { | ||
2234 | 2217 | ngx_http_proxy_ctx_t *ctx; | ||
2235 | 2218 | |||
2236 | 2219 | ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module); | ||
2237 | 2220 | |||
2238 | 2221 | if (ctx == NULL) { | ||
2239 | 2222 | v->not_found = 1; | ||
2240 | 2223 | return NGX_OK; | ||
2241 | 2224 | } | ||
2242 | 2225 | |||
2243 | 2226 | v->len = ctx->vars.host_header.len; | ||
2244 | 2227 | v->valid = 1; | ||
2245 | 2228 | v->no_cacheable = 0; | ||
2246 | 2229 | v->not_found = 0; | ||
2247 | 2230 | v->data = ctx->vars.host_header.data; | ||
2248 | 2231 | |||
2249 | 2232 | return NGX_OK; | ||
2250 | 2233 | } | ||
2251 | 2234 | |||
2252 | 2235 | |||
2253 | 2236 | static ngx_int_t | ||
2254 | 2237 | ngx_http_proxy_port_variable(ngx_http_request_t *r, | ||
2255 | 2238 | ngx_http_variable_value_t *v, uintptr_t data) | ||
2256 | 2239 | { | ||
2257 | 2240 | ngx_http_proxy_ctx_t *ctx; | ||
2258 | 2241 | |||
2259 | 2242 | ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module); | ||
2260 | 2243 | |||
2261 | 2244 | if (ctx == NULL) { | ||
2262 | 2245 | v->not_found = 1; | ||
2263 | 2246 | return NGX_OK; | ||
2264 | 2247 | } | ||
2265 | 2248 | |||
2266 | 2249 | v->len = ctx->vars.port.len; | ||
2267 | 2250 | v->valid = 1; | ||
2268 | 2251 | v->no_cacheable = 0; | ||
2269 | 2252 | v->not_found = 0; | ||
2270 | 2253 | v->data = ctx->vars.port.data; | ||
2271 | 2254 | |||
2272 | 2255 | return NGX_OK; | ||
2273 | 2256 | } | ||
2274 | 2257 | |||
2275 | 2258 | |||
2276 | 2259 | static ngx_int_t | ||
2277 | 2260 | ngx_http_proxy_add_x_forwarded_for_variable(ngx_http_request_t *r, | ||
2278 | 2261 | ngx_http_variable_value_t *v, uintptr_t data) | ||
2279 | 2262 | { | ||
2280 | 2263 | u_char *p; | ||
2281 | 2264 | |||
2282 | 2265 | v->valid = 1; | ||
2283 | 2266 | v->no_cacheable = 0; | ||
2284 | 2267 | v->not_found = 0; | ||
2285 | 2268 | |||
2286 | 2269 | if (r->headers_in.x_forwarded_for == NULL) { | ||
2287 | 2270 | v->len = r->connection->addr_text.len; | ||
2288 | 2271 | v->data = r->connection->addr_text.data; | ||
2289 | 2272 | return NGX_OK; | ||
2290 | 2273 | } | ||
2291 | 2274 | |||
2292 | 2275 | v->len = r->headers_in.x_forwarded_for->value.len | ||
2293 | 2276 | + sizeof(", ") - 1 + r->connection->addr_text.len; | ||
2294 | 2277 | |||
2295 | 2278 | p = ngx_pnalloc(r->pool, v->len); | ||
2296 | 2279 | if (p == NULL) { | ||
2297 | 2280 | return NGX_ERROR; | ||
2298 | 2281 | } | ||
2299 | 2282 | |||
2300 | 2283 | v->data = p; | ||
2301 | 2284 | |||
2302 | 2285 | p = ngx_copy(p, r->headers_in.x_forwarded_for->value.data, | ||
2303 | 2286 | r->headers_in.x_forwarded_for->value.len); | ||
2304 | 2287 | |||
2305 | 2288 | *p++ = ','; *p++ = ' '; | ||
2306 | 2289 | |||
2307 | 2290 | ngx_memcpy(p, r->connection->addr_text.data, r->connection->addr_text.len); | ||
2308 | 2291 | |||
2309 | 2292 | return NGX_OK; | ||
2310 | 2293 | } | ||
2311 | 2294 | |||
2312 | 2295 | |||
2313 | 2296 | static ngx_int_t | ||
2314 | 2297 | ngx_http_proxy_internal_body_length_variable(ngx_http_request_t *r, | ||
2315 | 2298 | ngx_http_variable_value_t *v, uintptr_t data) | ||
2316 | 2299 | { | ||
2317 | 2300 | ngx_http_proxy_ctx_t *ctx; | ||
2318 | 2301 | |||
2319 | 2302 | ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module); | ||
2320 | 2303 | |||
2321 | 2304 | if (ctx == NULL) { | ||
2322 | 2305 | v->not_found = 1; | ||
2323 | 2306 | return NGX_OK; | ||
2324 | 2307 | } | ||
2325 | 2308 | |||
2326 | 2309 | v->valid = 1; | ||
2327 | 2310 | v->no_cacheable = 0; | ||
2328 | 2311 | v->not_found = 0; | ||
2329 | 2312 | |||
2330 | 2313 | v->data = ngx_pnalloc(r->connection->pool, NGX_SIZE_T_LEN); | ||
2331 | 2314 | |||
2332 | 2315 | if (v->data == NULL) { | ||
2333 | 2316 | return NGX_ERROR; | ||
2334 | 2317 | } | ||
2335 | 2318 | |||
2336 | 2319 | v->len = ngx_sprintf(v->data, "%uz", ctx->internal_body_length) - v->data; | ||
2337 | 2320 | |||
2338 | 2321 | return NGX_OK; | ||
2339 | 2322 | } | ||
2340 | 2323 | |||
2341 | 2324 | |||
2342 | 2325 | static ngx_int_t | ||
2343 | 2326 | ngx_http_proxy_rewrite_redirect(ngx_http_request_t *r, ngx_table_elt_t *h, | ||
2344 | 2327 | size_t prefix) | ||
2345 | 2328 | { | ||
2346 | 2329 | size_t len; | ||
2347 | 2330 | ngx_int_t rc; | ||
2348 | 2331 | ngx_uint_t i; | ||
2349 | 2332 | ngx_http_proxy_rewrite_t *pr; | ||
2350 | 2333 | ngx_http_proxy_loc_conf_t *plcf; | ||
2351 | 2334 | |||
2352 | 2335 | plcf = ngx_http_get_module_loc_conf(r, ngx_http_proxy_module); | ||
2353 | 2336 | |||
2354 | 2337 | pr = plcf->redirects->elts; | ||
2355 | 2338 | |||
2356 | 2339 | if (pr == NULL) { | ||
2357 | 2340 | return NGX_DECLINED; | ||
2358 | 2341 | } | ||
2359 | 2342 | |||
2360 | 2343 | len = h->value.len - prefix; | ||
2361 | 2344 | |||
2362 | 2345 | for (i = 0; i < plcf->redirects->nelts; i++) { | ||
2363 | 2346 | rc = pr[i].handler(r, h, prefix, len, &pr[i]); | ||
2364 | 2347 | |||
2365 | 2348 | if (rc != NGX_DECLINED) { | ||
2366 | 2349 | return rc; | ||
2367 | 2350 | } | ||
2368 | 2351 | } | ||
2369 | 2352 | |||
2370 | 2353 | return NGX_DECLINED; | ||
2371 | 2354 | } | ||
2372 | 2355 | |||
2373 | 2356 | |||
2374 | 2357 | static ngx_int_t | ||
2375 | 2358 | ngx_http_proxy_rewrite_cookie(ngx_http_request_t *r, ngx_table_elt_t *h) | ||
2376 | 2359 | { | ||
2377 | 2360 | size_t prefix; | ||
2378 | 2361 | u_char *p; | ||
2379 | 2362 | ngx_int_t rc, rv; | ||
2380 | 2363 | ngx_http_proxy_loc_conf_t *plcf; | ||
2381 | 2364 | |||
2382 | 2365 | p = (u_char *) ngx_strchr(h->value.data, ';'); | ||
2383 | 2366 | if (p == NULL) { | ||
2384 | 2367 | return NGX_DECLINED; | ||
2385 | 2368 | } | ||
2386 | 2369 | |||
2387 | 2370 | prefix = p + 1 - h->value.data; | ||
2388 | 2371 | |||
2389 | 2372 | rv = NGX_DECLINED; | ||
2390 | 2373 | |||
2391 | 2374 | plcf = ngx_http_get_module_loc_conf(r, ngx_http_proxy_module); | ||
2392 | 2375 | |||
2393 | 2376 | if (plcf->cookie_domains) { | ||
2394 | 2377 | p = ngx_strcasestrn(h->value.data + prefix, "domain=", 7 - 1); | ||
2395 | 2378 | |||
2396 | 2379 | if (p) { | ||
2397 | 2380 | rc = ngx_http_proxy_rewrite_cookie_value(r, h, p + 7, | ||
2398 | 2381 | plcf->cookie_domains); | ||
2399 | 2382 | if (rc == NGX_ERROR) { | ||
2400 | 2383 | return NGX_ERROR; | ||
2401 | 2384 | } | ||
2402 | 2385 | |||
2403 | 2386 | if (rc != NGX_DECLINED) { | ||
2404 | 2387 | rv = rc; | ||
2405 | 2388 | } | ||
2406 | 2389 | } | ||
2407 | 2390 | } | ||
2408 | 2391 | |||
2409 | 2392 | if (plcf->cookie_paths) { | ||
2410 | 2393 | p = ngx_strcasestrn(h->value.data + prefix, "path=", 5 - 1); | ||
2411 | 2394 | |||
2412 | 2395 | if (p) { | ||
2413 | 2396 | rc = ngx_http_proxy_rewrite_cookie_value(r, h, p + 5, | ||
2414 | 2397 | plcf->cookie_paths); | ||
2415 | 2398 | if (rc == NGX_ERROR) { | ||
2416 | 2399 | return NGX_ERROR; | ||
2417 | 2400 | } | ||
2418 | 2401 | |||
2419 | 2402 | if (rc != NGX_DECLINED) { | ||
2420 | 2403 | rv = rc; | ||
2421 | 2404 | } | ||
2422 | 2405 | } | ||
2423 | 2406 | } | ||
2424 | 2407 | |||
2425 | 2408 | return rv; | ||
2426 | 2409 | } | ||
2427 | 2410 | |||
2428 | 2411 | |||
2429 | 2412 | static ngx_int_t | ||
2430 | 2413 | ngx_http_proxy_rewrite_cookie_value(ngx_http_request_t *r, ngx_table_elt_t *h, | ||
2431 | 2414 | u_char *value, ngx_array_t *rewrites) | ||
2432 | 2415 | { | ||
2433 | 2416 | size_t len, prefix; | ||
2434 | 2417 | u_char *p; | ||
2435 | 2418 | ngx_int_t rc; | ||
2436 | 2419 | ngx_uint_t i; | ||
2437 | 2420 | ngx_http_proxy_rewrite_t *pr; | ||
2438 | 2421 | |||
2439 | 2422 | prefix = value - h->value.data; | ||
2440 | 2423 | |||
2441 | 2424 | p = (u_char *) ngx_strchr(value, ';'); | ||
2442 | 2425 | |||
2443 | 2426 | len = p ? (size_t) (p - value) : (h->value.len - prefix); | ||
2444 | 2427 | |||
2445 | 2428 | pr = rewrites->elts; | ||
2446 | 2429 | |||
2447 | 2430 | for (i = 0; i < rewrites->nelts; i++) { | ||
2448 | 2431 | rc = pr[i].handler(r, h, prefix, len, &pr[i]); | ||
2449 | 2432 | |||
2450 | 2433 | if (rc != NGX_DECLINED) { | ||
2451 | 2434 | return rc; | ||
2452 | 2435 | } | ||
2453 | 2436 | } | ||
2454 | 2437 | |||
2455 | 2438 | return NGX_DECLINED; | ||
2456 | 2439 | } | ||
2457 | 2440 | |||
2458 | 2441 | |||
2459 | 2442 | static ngx_int_t | ||
2460 | 2443 | ngx_http_proxy_rewrite_complex_handler(ngx_http_request_t *r, | ||
2461 | 2444 | ngx_table_elt_t *h, size_t prefix, size_t len, ngx_http_proxy_rewrite_t *pr) | ||
2462 | 2445 | { | ||
2463 | 2446 | ngx_str_t pattern, replacement; | ||
2464 | 2447 | |||
2465 | 2448 | if (ngx_http_complex_value(r, &pr->pattern.complex, &pattern) != NGX_OK) { | ||
2466 | 2449 | return NGX_ERROR; | ||
2467 | 2450 | } | ||
2468 | 2451 | |||
2469 | 2452 | if (pattern.len > len | ||
2470 | 2453 | || ngx_rstrncmp(h->value.data + prefix, pattern.data, | ||
2471 | 2454 | pattern.len) != 0) | ||
2472 | 2455 | { | ||
2473 | 2456 | return NGX_DECLINED; | ||
2474 | 2457 | } | ||
2475 | 2458 | |||
2476 | 2459 | if (ngx_http_complex_value(r, &pr->replacement, &replacement) != NGX_OK) { | ||
2477 | 2460 | return NGX_ERROR; | ||
2478 | 2461 | } | ||
2479 | 2462 | |||
2480 | 2463 | return ngx_http_proxy_rewrite(r, h, prefix, pattern.len, &replacement); | ||
2481 | 2464 | } | ||
2482 | 2465 | |||
2483 | 2466 | |||
2484 | 2467 | #if (NGX_PCRE) | ||
2485 | 2468 | |||
2486 | 2469 | static ngx_int_t | ||
2487 | 2470 | ngx_http_proxy_rewrite_regex_handler(ngx_http_request_t *r, ngx_table_elt_t *h, | ||
2488 | 2471 | size_t prefix, size_t len, ngx_http_proxy_rewrite_t *pr) | ||
2489 | 2472 | { | ||
2490 | 2473 | ngx_str_t pattern, replacement; | ||
2491 | 2474 | |||
2492 | 2475 | pattern.len = len; | ||
2493 | 2476 | pattern.data = h->value.data + prefix; | ||
2494 | 2477 | |||
2495 | 2478 | if (ngx_http_regex_exec(r, pr->pattern.regex, &pattern) != NGX_OK) { | ||
2496 | 2479 | return NGX_DECLINED; | ||
2497 | 2480 | } | ||
2498 | 2481 | |||
2499 | 2482 | if (ngx_http_complex_value(r, &pr->replacement, &replacement) != NGX_OK) { | ||
2500 | 2483 | return NGX_ERROR; | ||
2501 | 2484 | } | ||
2502 | 2485 | |||
2503 | 2486 | if (prefix == 0 && h->value.len == len) { | ||
2504 | 2487 | h->value = replacement; | ||
2505 | 2488 | return NGX_OK; | ||
2506 | 2489 | } | ||
2507 | 2490 | |||
2508 | 2491 | return ngx_http_proxy_rewrite(r, h, prefix, len, &replacement); | ||
2509 | 2492 | } | ||
2510 | 2493 | |||
2511 | 2494 | #endif | ||
2512 | 2495 | |||
2513 | 2496 | |||
2514 | 2497 | static ngx_int_t | ||
2515 | 2498 | ngx_http_proxy_rewrite_domain_handler(ngx_http_request_t *r, | ||
2516 | 2499 | ngx_table_elt_t *h, size_t prefix, size_t len, ngx_http_proxy_rewrite_t *pr) | ||
2517 | 2500 | { | ||
2518 | 2501 | u_char *p; | ||
2519 | 2502 | ngx_str_t pattern, replacement; | ||
2520 | 2503 | |||
2521 | 2504 | if (ngx_http_complex_value(r, &pr->pattern.complex, &pattern) != NGX_OK) { | ||
2522 | 2505 | return NGX_ERROR; | ||
2523 | 2506 | } | ||
2524 | 2507 | |||
2525 | 2508 | p = h->value.data + prefix; | ||
2526 | 2509 | |||
2527 | 2510 | if (p[0] == '.') { | ||
2528 | 2511 | p++; | ||
2529 | 2512 | prefix++; | ||
2530 | 2513 | len--; | ||
2531 | 2514 | } | ||
2532 | 2515 | |||
2533 | 2516 | if (pattern.len != len || ngx_rstrncasecmp(pattern.data, p, len) != 0) { | ||
2534 | 2517 | return NGX_DECLINED; | ||
2535 | 2518 | } | ||
2536 | 2519 | |||
2537 | 2520 | if (ngx_http_complex_value(r, &pr->replacement, &replacement) != NGX_OK) { | ||
2538 | 2521 | return NGX_ERROR; | ||
2539 | 2522 | } | ||
2540 | 2523 | |||
2541 | 2524 | return ngx_http_proxy_rewrite(r, h, prefix, len, &replacement); | ||
2542 | 2525 | } | ||
2543 | 2526 | |||
2544 | 2527 | |||
2545 | 2528 | static ngx_int_t | ||
2546 | 2529 | ngx_http_proxy_rewrite(ngx_http_request_t *r, ngx_table_elt_t *h, size_t prefix, | ||
2547 | 2530 | size_t len, ngx_str_t *replacement) | ||
2548 | 2531 | { | ||
2549 | 2532 | u_char *p, *data; | ||
2550 | 2533 | size_t new_len; | ||
2551 | 2534 | |||
2552 | 2535 | new_len = replacement->len + h->value.len - len; | ||
2553 | 2536 | |||
2554 | 2537 | if (replacement->len > len) { | ||
2555 | 2538 | |||
2556 | 2539 | data = ngx_pnalloc(r->pool, new_len); | ||
2557 | 2540 | if (data == NULL) { | ||
2558 | 2541 | return NGX_ERROR; | ||
2559 | 2542 | } | ||
2560 | 2543 | |||
2561 | 2544 | p = ngx_copy(data, h->value.data, prefix); | ||
2562 | 2545 | p = ngx_copy(p, replacement->data, replacement->len); | ||
2563 | 2546 | |||
2564 | 2547 | ngx_memcpy(p, h->value.data + prefix + len, | ||
2565 | 2548 | h->value.len - len - prefix); | ||
2566 | 2549 | |||
2567 | 2550 | h->value.data = data; | ||
2568 | 2551 | |||
2569 | 2552 | } else { | ||
2570 | 2553 | p = ngx_copy(h->value.data + prefix, replacement->data, | ||
2571 | 2554 | replacement->len); | ||
2572 | 2555 | |||
2573 | 2556 | ngx_memmove(p, h->value.data + prefix + len, | ||
2574 | 2557 | h->value.len - len - prefix); | ||
2575 | 2558 | } | ||
2576 | 2559 | |||
2577 | 2560 | h->value.len = new_len; | ||
2578 | 2561 | |||
2579 | 2562 | return NGX_OK; | ||
2580 | 2563 | } | ||
2581 | 2564 | |||
2582 | 2565 | |||
2583 | 2566 | static ngx_int_t | ||
2584 | 2567 | ngx_http_proxy_add_variables(ngx_conf_t *cf) | ||
2585 | 2568 | { | ||
2586 | 2569 | ngx_http_variable_t *var, *v; | ||
2587 | 2570 | |||
2588 | 2571 | for (v = ngx_http_proxy_vars; v->name.len; v++) { | ||
2589 | 2572 | var = ngx_http_add_variable(cf, &v->name, v->flags); | ||
2590 | 2573 | if (var == NULL) { | ||
2591 | 2574 | return NGX_ERROR; | ||
2592 | 2575 | } | ||
2593 | 2576 | |||
2594 | 2577 | var->get_handler = v->get_handler; | ||
2595 | 2578 | var->data = v->data; | ||
2596 | 2579 | } | ||
2597 | 2580 | |||
2598 | 2581 | return NGX_OK; | ||
2599 | 2582 | } | ||
2600 | 2583 | |||
2601 | 2584 | |||
2602 | 2585 | static void * | ||
2603 | 2586 | ngx_http_proxy_create_loc_conf(ngx_conf_t *cf) | ||
2604 | 2587 | { | ||
2605 | 2588 | ngx_http_proxy_loc_conf_t *conf; | ||
2606 | 2589 | |||
2607 | 2590 | conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_proxy_loc_conf_t)); | ||
2608 | 2591 | if (conf == NULL) { | ||
2609 | 2592 | return NULL; | ||
2610 | 2593 | } | ||
2611 | 2594 | |||
2612 | 2595 | /* | ||
2613 | 2596 | * set by ngx_pcalloc(): | ||
2614 | 2597 | * | ||
2615 | 2598 | * conf->upstream.bufs.num = 0; | ||
2616 | 2599 | * conf->upstream.ignore_headers = 0; | ||
2617 | 2600 | * conf->upstream.next_upstream = 0; | ||
2618 | 2601 | * conf->upstream.cache_use_stale = 0; | ||
2619 | 2602 | * conf->upstream.cache_methods = 0; | ||
2620 | 2603 | * conf->upstream.temp_path = NULL; | ||
2621 | 2604 | * conf->upstream.hide_headers_hash = { NULL, 0 }; | ||
2622 | 2605 | * conf->upstream.uri = { 0, NULL }; | ||
2623 | 2606 | * conf->upstream.location = NULL; | ||
2624 | 2607 | * conf->upstream.store_lengths = NULL; | ||
2625 | 2608 | * conf->upstream.store_values = NULL; | ||
2626 | 2609 | * | ||
2627 | 2610 | * conf->method = NULL; | ||
2628 | 2611 | * conf->headers_source = NULL; | ||
2629 | 2612 | * conf->headers_set_len = NULL; | ||
2630 | 2613 | * conf->headers_set = NULL; | ||
2631 | 2614 | * conf->headers_set_hash = NULL; | ||
2632 | 2615 | * conf->body_set_len = NULL; | ||
2633 | 2616 | * conf->body_set = NULL; | ||
2634 | 2617 | * conf->body_source = { 0, NULL }; | ||
2635 | 2618 | * conf->redirects = NULL; | ||
2636 | 2619 | */ | ||
2637 | 2620 | |||
2638 | 2621 | conf->upstream.store = NGX_CONF_UNSET; | ||
2639 | 2622 | conf->upstream.store_access = NGX_CONF_UNSET_UINT; | ||
2640 | 2623 | conf->upstream.buffering = NGX_CONF_UNSET; | ||
2641 | 2624 | conf->upstream.ignore_client_abort = NGX_CONF_UNSET; | ||
2642 | 2625 | |||
2643 | 2626 | conf->upstream.connect_timeout = NGX_CONF_UNSET_MSEC; | ||
2644 | 2627 | conf->upstream.send_timeout = NGX_CONF_UNSET_MSEC; | ||
2645 | 2628 | conf->upstream.read_timeout = NGX_CONF_UNSET_MSEC; | ||
2646 | 2629 | |||
2647 | 2630 | conf->upstream.send_lowat = NGX_CONF_UNSET_SIZE; | ||
2648 | 2631 | conf->upstream.buffer_size = NGX_CONF_UNSET_SIZE; | ||
2649 | 2632 | |||
2650 | 2633 | conf->upstream.busy_buffers_size_conf = NGX_CONF_UNSET_SIZE; | ||
2651 | 2634 | conf->upstream.max_temp_file_size_conf = NGX_CONF_UNSET_SIZE; | ||
2652 | 2635 | conf->upstream.temp_file_write_size_conf = NGX_CONF_UNSET_SIZE; | ||
2653 | 2636 | |||
2654 | 2637 | conf->upstream.pass_request_headers = NGX_CONF_UNSET; | ||
2655 | 2638 | conf->upstream.pass_request_body = NGX_CONF_UNSET; | ||
2656 | 2639 | |||
2657 | 2640 | #if (NGX_HTTP_CACHE) | ||
2658 | 2641 | conf->upstream.cache = NGX_CONF_UNSET_PTR; | ||
2659 | 2642 | conf->upstream.cache_min_uses = NGX_CONF_UNSET_UINT; | ||
2660 | 2643 | conf->upstream.cache_bypass = NGX_CONF_UNSET_PTR; | ||
2661 | 2644 | conf->upstream.no_cache = NGX_CONF_UNSET_PTR; | ||
2662 | 2645 | conf->upstream.cache_valid = NGX_CONF_UNSET_PTR; | ||
2663 | 2646 | conf->upstream.cache_lock = NGX_CONF_UNSET; | ||
2664 | 2647 | conf->upstream.cache_lock_timeout = NGX_CONF_UNSET_MSEC; | ||
2665 | 2648 | #endif | ||
2666 | 2649 | |||
2667 | 2650 | conf->upstream.hide_headers = NGX_CONF_UNSET_PTR; | ||
2668 | 2651 | conf->upstream.pass_headers = NGX_CONF_UNSET_PTR; | ||
2669 | 2652 | |||
2670 | 2653 | conf->upstream.intercept_errors = NGX_CONF_UNSET; | ||
2671 | 2654 | #if (NGX_HTTP_SSL) | ||
2672 | 2655 | conf->upstream.ssl_session_reuse = NGX_CONF_UNSET; | ||
2673 | 2656 | #endif | ||
2674 | 2657 | |||
2675 | 2658 | /* "proxy_cyclic_temp_file" is disabled */ | ||
2676 | 2659 | conf->upstream.cyclic_temp_file = 0; | ||
2677 | 2660 | |||
2678 | 2661 | conf->redirect = NGX_CONF_UNSET; | ||
2679 | 2662 | conf->upstream.change_buffering = 1; | ||
2680 | 2663 | |||
2681 | 2664 | conf->cookie_domains = NGX_CONF_UNSET_PTR; | ||
2682 | 2665 | conf->cookie_paths = NGX_CONF_UNSET_PTR; | ||
2683 | 2666 | |||
2684 | 2667 | conf->http_version = NGX_CONF_UNSET_UINT; | ||
2685 | 2668 | |||
2686 | 2669 | conf->headers_hash_max_size = NGX_CONF_UNSET_UINT; | ||
2687 | 2670 | conf->headers_hash_bucket_size = NGX_CONF_UNSET_UINT; | ||
2688 | 2671 | |||
2689 | 2672 | ngx_str_set(&conf->upstream.module, "proxy"); | ||
2690 | 2673 | |||
2691 | 2674 | return conf; | ||
2692 | 2675 | } | ||
2693 | 2676 | |||
2694 | 2677 | |||
2695 | 2678 | static char * | ||
2696 | 2679 | ngx_http_proxy_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child) | ||
2697 | 2680 | { | ||
2698 | 2681 | ngx_http_proxy_loc_conf_t *prev = parent; | ||
2699 | 2682 | ngx_http_proxy_loc_conf_t *conf = child; | ||
2700 | 2683 | |||
2701 | 2684 | u_char *p; | ||
2702 | 2685 | size_t size; | ||
2703 | 2686 | ngx_hash_init_t hash; | ||
2704 | 2687 | ngx_http_core_loc_conf_t *clcf; | ||
2705 | 2688 | ngx_http_proxy_rewrite_t *pr; | ||
2706 | 2689 | ngx_http_script_compile_t sc; | ||
2707 | 2690 | |||
2708 | 2691 | if (conf->upstream.store != 0) { | ||
2709 | 2692 | ngx_conf_merge_value(conf->upstream.store, | ||
2710 | 2693 | prev->upstream.store, 0); | ||
2711 | 2694 | |||
2712 | 2695 | if (conf->upstream.store_lengths == NULL) { | ||
2713 | 2696 | conf->upstream.store_lengths = prev->upstream.store_lengths; | ||
2714 | 2697 | conf->upstream.store_values = prev->upstream.store_values; | ||
2715 | 2698 | } | ||
2716 | 2699 | } | ||
2717 | 2700 | |||
2718 | 2701 | ngx_conf_merge_uint_value(conf->upstream.store_access, | ||
2719 | 2702 | prev->upstream.store_access, 0600); | ||
2720 | 2703 | |||
2721 | 2704 | ngx_conf_merge_value(conf->upstream.buffering, | ||
2722 | 2705 | prev->upstream.buffering, 1); | ||
2723 | 2706 | |||
2724 | 2707 | ngx_conf_merge_value(conf->upstream.ignore_client_abort, | ||
2725 | 2708 | prev->upstream.ignore_client_abort, 0); | ||
2726 | 2709 | |||
2727 | 2710 | ngx_conf_merge_msec_value(conf->upstream.connect_timeout, | ||
2728 | 2711 | prev->upstream.connect_timeout, 60000); | ||
2729 | 2712 | |||
2730 | 2713 | ngx_conf_merge_msec_value(conf->upstream.send_timeout, | ||
2731 | 2714 | prev->upstream.send_timeout, 60000); | ||
2732 | 2715 | |||
2733 | 2716 | ngx_conf_merge_msec_value(conf->upstream.read_timeout, | ||
2734 | 2717 | prev->upstream.read_timeout, 60000); | ||
2735 | 2718 | |||
2736 | 2719 | ngx_conf_merge_size_value(conf->upstream.send_lowat, | ||
2737 | 2720 | prev->upstream.send_lowat, 0); | ||
2738 | 2721 | |||
2739 | 2722 | ngx_conf_merge_size_value(conf->upstream.buffer_size, | ||
2740 | 2723 | prev->upstream.buffer_size, | ||
2741 | 2724 | (size_t) ngx_pagesize); | ||
2742 | 2725 | |||
2743 | 2726 | ngx_conf_merge_bufs_value(conf->upstream.bufs, prev->upstream.bufs, | ||
2744 | 2727 | 8, ngx_pagesize); | ||
2745 | 2728 | |||
2746 | 2729 | if (conf->upstream.bufs.num < 2) { | ||
2747 | 2730 | ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | ||
2748 | 2731 | "there must be at least 2 \"proxy_buffers\""); | ||
2749 | 2732 | return NGX_CONF_ERROR; | ||
2750 | 2733 | } | ||
2751 | 2734 | |||
2752 | 2735 | |||
2753 | 2736 | size = conf->upstream.buffer_size; | ||
2754 | 2737 | if (size < conf->upstream.bufs.size) { | ||
2755 | 2738 | size = conf->upstream.bufs.size; | ||
2756 | 2739 | } | ||
2757 | 2740 | |||
2758 | 2741 | |||
2759 | 2742 | ngx_conf_merge_size_value(conf->upstream.busy_buffers_size_conf, | ||
2760 | 2743 | prev->upstream.busy_buffers_size_conf, | ||
2761 | 2744 | NGX_CONF_UNSET_SIZE); | ||
2762 | 2745 | |||
2763 | 2746 | if (conf->upstream.busy_buffers_size_conf == NGX_CONF_UNSET_SIZE) { | ||
2764 | 2747 | conf->upstream.busy_buffers_size = 2 * size; | ||
2765 | 2748 | } else { | ||
2766 | 2749 | conf->upstream.busy_buffers_size = | ||
2767 | 2750 | conf->upstream.busy_buffers_size_conf; | ||
2768 | 2751 | } | ||
2769 | 2752 | |||
2770 | 2753 | if (conf->upstream.busy_buffers_size < size) { | ||
2771 | 2754 | ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | ||
2772 | 2755 | "\"proxy_busy_buffers_size\" must be equal to or greater than " | ||
2773 | 2756 | "the maximum of the value of \"proxy_buffer_size\" and " | ||
2774 | 2757 | "one of the \"proxy_buffers\""); | ||
2775 | 2758 | |||
2776 | 2759 | return NGX_CONF_ERROR; | ||
2777 | 2760 | } | ||
2778 | 2761 | |||
2779 | 2762 | if (conf->upstream.busy_buffers_size | ||
2780 | 2763 | > (conf->upstream.bufs.num - 1) * conf->upstream.bufs.size) | ||
2781 | 2764 | { | ||
2782 | 2765 | ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | ||
2783 | 2766 | "\"proxy_busy_buffers_size\" must be less than " | ||
2784 | 2767 | "the size of all \"proxy_buffers\" minus one buffer"); | ||
2785 | 2768 | |||
2786 | 2769 | return NGX_CONF_ERROR; | ||
2787 | 2770 | } | ||
2788 | 2771 | |||
2789 | 2772 | |||
2790 | 2773 | ngx_conf_merge_size_value(conf->upstream.temp_file_write_size_conf, | ||
2791 | 2774 | prev->upstream.temp_file_write_size_conf, | ||
2792 | 2775 | NGX_CONF_UNSET_SIZE); | ||
2793 | 2776 | |||
2794 | 2777 | if (conf->upstream.temp_file_write_size_conf == NGX_CONF_UNSET_SIZE) { | ||
2795 | 2778 | conf->upstream.temp_file_write_size = 2 * size; | ||
2796 | 2779 | } else { | ||
2797 | 2780 | conf->upstream.temp_file_write_size = | ||
2798 | 2781 | conf->upstream.temp_file_write_size_conf; | ||
2799 | 2782 | } | ||
2800 | 2783 | |||
2801 | 2784 | if (conf->upstream.temp_file_write_size < size) { | ||
2802 | 2785 | ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | ||
2803 | 2786 | "\"proxy_temp_file_write_size\" must be equal to or greater " | ||
2804 | 2787 | "than the maximum of the value of \"proxy_buffer_size\" and " | ||
2805 | 2788 | "one of the \"proxy_buffers\""); | ||
2806 | 2789 | |||
2807 | 2790 | return NGX_CONF_ERROR; | ||
2808 | 2791 | } | ||
2809 | 2792 | |||
2810 | 2793 | ngx_conf_merge_size_value(conf->upstream.max_temp_file_size_conf, | ||
2811 | 2794 | prev->upstream.max_temp_file_size_conf, | ||
2812 | 2795 | NGX_CONF_UNSET_SIZE); | ||
2813 | 2796 | |||
2814 | 2797 | if (conf->upstream.max_temp_file_size_conf == NGX_CONF_UNSET_SIZE) { | ||
2815 | 2798 | conf->upstream.max_temp_file_size = 1024 * 1024 * 1024; | ||
2816 | 2799 | } else { | ||
2817 | 2800 | conf->upstream.max_temp_file_size = | ||
2818 | 2801 | conf->upstream.max_temp_file_size_conf; | ||
2819 | 2802 | } | ||
2820 | 2803 | |||
2821 | 2804 | if (conf->upstream.max_temp_file_size != 0 | ||
2822 | 2805 | && conf->upstream.max_temp_file_size < size) | ||
2823 | 2806 | { | ||
2824 | 2807 | ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | ||
2825 | 2808 | "\"proxy_max_temp_file_size\" must be equal to zero to disable " | ||
2826 | 2809 | "temporary files usage or must be equal to or greater than " | ||
2827 | 2810 | "the maximum of the value of \"proxy_buffer_size\" and " | ||
2828 | 2811 | "one of the \"proxy_buffers\""); | ||
2829 | 2812 | |||
2830 | 2813 | return NGX_CONF_ERROR; | ||
2831 | 2814 | } | ||
2832 | 2815 | |||
2833 | 2816 | |||
2834 | 2817 | ngx_conf_merge_bitmask_value(conf->upstream.ignore_headers, | ||
2835 | 2818 | prev->upstream.ignore_headers, | ||
2836 | 2819 | NGX_CONF_BITMASK_SET); | ||
2837 | 2820 | |||
2838 | 2821 | |||
2839 | 2822 | ngx_conf_merge_bitmask_value(conf->upstream.next_upstream, | ||
2840 | 2823 | prev->upstream.next_upstream, | ||
2841 | 2824 | (NGX_CONF_BITMASK_SET | ||
2842 | 2825 | |NGX_HTTP_UPSTREAM_FT_ERROR | ||
2843 | 2826 | |NGX_HTTP_UPSTREAM_FT_TIMEOUT)); | ||
2844 | 2827 | |||
2845 | 2828 | if (conf->upstream.next_upstream & NGX_HTTP_UPSTREAM_FT_OFF) { | ||
2846 | 2829 | conf->upstream.next_upstream = NGX_CONF_BITMASK_SET | ||
2847 | 2830 | |NGX_HTTP_UPSTREAM_FT_OFF; | ||
2848 | 2831 | } | ||
2849 | 2832 | |||
2850 | 2833 | if (ngx_conf_merge_path_value(cf, &conf->upstream.temp_path, | ||
2851 | 2834 | prev->upstream.temp_path, | ||
2852 | 2835 | &ngx_http_proxy_temp_path) | ||
2853 | 2836 | != NGX_OK) | ||
2854 | 2837 | { | ||
2855 | 2838 | return NGX_CONF_ERROR; | ||
2856 | 2839 | } | ||
2857 | 2840 | |||
2858 | 2841 | |||
2859 | 2842 | #if (NGX_HTTP_CACHE) | ||
2860 | 2843 | |||
2861 | 2844 | ngx_conf_merge_ptr_value(conf->upstream.cache, | ||
2862 | 2845 | prev->upstream.cache, NULL); | ||
2863 | 2846 | |||
2864 | 2847 | if (conf->upstream.cache && conf->upstream.cache->data == NULL) { | ||
2865 | 2848 | ngx_shm_zone_t *shm_zone; | ||
2866 | 2849 | |||
2867 | 2850 | shm_zone = conf->upstream.cache; | ||
2868 | 2851 | |||
2869 | 2852 | ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | ||
2870 | 2853 | "\"proxy_cache\" zone \"%V\" is unknown", | ||
2871 | 2854 | &shm_zone->shm.name); | ||
2872 | 2855 | |||
2873 | 2856 | return NGX_CONF_ERROR; | ||
2874 | 2857 | } | ||
2875 | 2858 | |||
2876 | 2859 | ngx_conf_merge_uint_value(conf->upstream.cache_min_uses, | ||
2877 | 2860 | prev->upstream.cache_min_uses, 1); | ||
2878 | 2861 | |||
2879 | 2862 | ngx_conf_merge_bitmask_value(conf->upstream.cache_use_stale, | ||
2880 | 2863 | prev->upstream.cache_use_stale, | ||
2881 | 2864 | (NGX_CONF_BITMASK_SET | ||
2882 | 2865 | |NGX_HTTP_UPSTREAM_FT_OFF)); | ||
2883 | 2866 | |||
2884 | 2867 | if (conf->upstream.cache_use_stale & NGX_HTTP_UPSTREAM_FT_OFF) { | ||
2885 | 2868 | conf->upstream.cache_use_stale = NGX_CONF_BITMASK_SET | ||
2886 | 2869 | |NGX_HTTP_UPSTREAM_FT_OFF; | ||
2887 | 2870 | } | ||
2888 | 2871 | |||
2889 | 2872 | if (conf->upstream.cache_use_stale & NGX_HTTP_UPSTREAM_FT_ERROR) { | ||
2890 | 2873 | conf->upstream.cache_use_stale |= NGX_HTTP_UPSTREAM_FT_NOLIVE; | ||
2891 | 2874 | } | ||
2892 | 2875 | |||
2893 | 2876 | if (conf->upstream.cache_methods == 0) { | ||
2894 | 2877 | conf->upstream.cache_methods = prev->upstream.cache_methods; | ||
2895 | 2878 | } | ||
2896 | 2879 | |||
2897 | 2880 | conf->upstream.cache_methods |= NGX_HTTP_GET|NGX_HTTP_HEAD; | ||
2898 | 2881 | |||
2899 | 2882 | ngx_conf_merge_ptr_value(conf->upstream.cache_bypass, | ||
2900 | 2883 | prev->upstream.cache_bypass, NULL); | ||
2901 | 2884 | |||
2902 | 2885 | ngx_conf_merge_ptr_value(conf->upstream.no_cache, | ||
2903 | 2886 | prev->upstream.no_cache, NULL); | ||
2904 | 2887 | |||
2905 | 2888 | if (conf->upstream.no_cache && conf->upstream.cache_bypass == NULL) { | ||
2906 | 2889 | ngx_log_error(NGX_LOG_WARN, cf->log, 0, | ||
2907 | 2890 | "\"proxy_no_cache\" functionality has been changed in 0.8.46, " | ||
2908 | 2891 | "now it should be used together with \"proxy_cache_bypass\""); | ||
2909 | 2892 | } | ||
2910 | 2893 | |||
2911 | 2894 | ngx_conf_merge_ptr_value(conf->upstream.cache_valid, | ||
2912 | 2895 | prev->upstream.cache_valid, NULL); | ||
2913 | 2896 | |||
2914 | 2897 | if (conf->cache_key.value.data == NULL) { | ||
2915 | 2898 | conf->cache_key = prev->cache_key; | ||
2916 | 2899 | } | ||
2917 | 2900 | |||
2918 | 2901 | ngx_conf_merge_value(conf->upstream.cache_lock, | ||
2919 | 2902 | prev->upstream.cache_lock, 0); | ||
2920 | 2903 | |||
2921 | 2904 | ngx_conf_merge_msec_value(conf->upstream.cache_lock_timeout, | ||
2922 | 2905 | prev->upstream.cache_lock_timeout, 5000); | ||
2923 | 2906 | |||
2924 | 2907 | #endif | ||
2925 | 2908 | |||
2926 | 2909 | if (conf->method.len == 0) { | ||
2927 | 2910 | conf->method = prev->method; | ||
2928 | 2911 | |||
2929 | 2912 | } else { | ||
2930 | 2913 | conf->method.data[conf->method.len] = ' '; | ||
2931 | 2914 | conf->method.len++; | ||
2932 | 2915 | } | ||
2933 | 2916 | |||
2934 | 2917 | ngx_conf_merge_value(conf->upstream.pass_request_headers, | ||
2935 | 2918 | prev->upstream.pass_request_headers, 1); | ||
2936 | 2919 | ngx_conf_merge_value(conf->upstream.pass_request_body, | ||
2937 | 2920 | prev->upstream.pass_request_body, 1); | ||
2938 | 2921 | |||
2939 | 2922 | ngx_conf_merge_value(conf->upstream.intercept_errors, | ||
2940 | 2923 | prev->upstream.intercept_errors, 0); | ||
2941 | 2924 | |||
2942 | 2925 | #if (NGX_HTTP_SSL) | ||
2943 | 2926 | ngx_conf_merge_value(conf->upstream.ssl_session_reuse, | ||
2944 | 2927 | prev->upstream.ssl_session_reuse, 1); | ||
2945 | 2928 | #endif | ||
2946 | 2929 | |||
2947 | 2930 | ngx_conf_merge_value(conf->redirect, prev->redirect, 1); | ||
2948 | 2931 | |||
2949 | 2932 | if (conf->redirect) { | ||
2950 | 2933 | |||
2951 | 2934 | if (conf->redirects == NULL) { | ||
2952 | 2935 | conf->redirects = prev->redirects; | ||
2953 | 2936 | } | ||
2954 | 2937 | |||
2955 | 2938 | if (conf->redirects == NULL && conf->url.data) { | ||
2956 | 2939 | |||
2957 | 2940 | conf->redirects = ngx_array_create(cf->pool, 1, | ||
2958 | 2941 | sizeof(ngx_http_proxy_rewrite_t)); | ||
2959 | 2942 | if (conf->redirects == NULL) { | ||
2960 | 2943 | return NGX_CONF_ERROR; | ||
2961 | 2944 | } | ||
2962 | 2945 | |||
2963 | 2946 | pr = ngx_array_push(conf->redirects); | ||
2964 | 2947 | if (pr == NULL) { | ||
2965 | 2948 | return NGX_CONF_ERROR; | ||
2966 | 2949 | } | ||
2967 | 2950 | |||
2968 | 2951 | ngx_memzero(&pr->pattern.complex, | ||
2969 | 2952 | sizeof(ngx_http_complex_value_t)); | ||
2970 | 2953 | |||
2971 | 2954 | ngx_memzero(&pr->replacement, sizeof(ngx_http_complex_value_t)); | ||
2972 | 2955 | |||
2973 | 2956 | pr->handler = ngx_http_proxy_rewrite_complex_handler; | ||
2974 | 2957 | |||
2975 | 2958 | if (conf->vars.uri.len) { | ||
2976 | 2959 | pr->pattern.complex.value = conf->url; | ||
2977 | 2960 | pr->replacement.value = conf->location; | ||
2978 | 2961 | |||
2979 | 2962 | } else { | ||
2980 | 2963 | pr->pattern.complex.value.len = conf->url.len | ||
2981 | 2964 | + sizeof("/") - 1; | ||
2982 | 2965 | |||
2983 | 2966 | p = ngx_pnalloc(cf->pool, pr->pattern.complex.value.len); | ||
2984 | 2967 | if (p == NULL) { | ||
2985 | 2968 | return NGX_CONF_ERROR; | ||
2986 | 2969 | } | ||
2987 | 2970 | |||
2988 | 2971 | pr->pattern.complex.value.data = p; | ||
2989 | 2972 | |||
2990 | 2973 | p = ngx_cpymem(p, conf->url.data, conf->url.len); | ||
2991 | 2974 | *p = '/'; | ||
2992 | 2975 | |||
2993 | 2976 | ngx_str_set(&pr->replacement.value, "/"); | ||
2994 | 2977 | } | ||
2995 | 2978 | } | ||
2996 | 2979 | } | ||
2997 | 2980 | |||
2998 | 2981 | ngx_conf_merge_ptr_value(conf->cookie_domains, prev->cookie_domains, NULL); | ||
2999 | 2982 | |||
3000 | 2983 | ngx_conf_merge_ptr_value(conf->cookie_paths, prev->cookie_paths, NULL); | ||
3001 | 2984 | |||
3002 | 2985 | #if (NGX_HTTP_SSL) | ||
3003 | 2986 | if (conf->upstream.ssl == NULL) { | ||
3004 | 2987 | conf->upstream.ssl = prev->upstream.ssl; | ||
3005 | 2988 | } | ||
3006 | 2989 | #endif | ||
3007 | 2990 | |||
3008 | 2991 | ngx_conf_merge_uint_value(conf->http_version, prev->http_version, | ||
3009 | 2992 | NGX_HTTP_VERSION_10); | ||
3010 | 2993 | |||
3011 | 2994 | ngx_conf_merge_uint_value(conf->headers_hash_max_size, | ||
3012 | 2995 | prev->headers_hash_max_size, 512); | ||
3013 | 2996 | |||
3014 | 2997 | ngx_conf_merge_uint_value(conf->headers_hash_bucket_size, | ||
3015 | 2998 | prev->headers_hash_bucket_size, 64); | ||
3016 | 2999 | |||
3017 | 3000 | conf->headers_hash_bucket_size = ngx_align(conf->headers_hash_bucket_size, | ||
3018 | 3001 | ngx_cacheline_size); | ||
3019 | 3002 | |||
3020 | 3003 | hash.max_size = conf->headers_hash_max_size; | ||
3021 | 3004 | hash.bucket_size = conf->headers_hash_bucket_size; | ||
3022 | 3005 | hash.name = "proxy_headers_hash"; | ||
3023 | 3006 | |||
3024 | 3007 | if (ngx_http_upstream_hide_headers_hash(cf, &conf->upstream, | ||
3025 | 3008 | &prev->upstream, ngx_http_proxy_hide_headers, &hash) | ||
3026 | 3009 | != NGX_OK) | ||
3027 | 3010 | { | ||
3028 | 3011 | return NGX_CONF_ERROR; | ||
3029 | 3012 | } | ||
3030 | 3013 | |||
3031 | 3014 | if (conf->upstream.upstream == NULL) { | ||
3032 | 3015 | conf->upstream.upstream = prev->upstream.upstream; | ||
3033 | 3016 | conf->vars = prev->vars; | ||
3034 | 3017 | } | ||
3035 | 3018 | |||
3036 | 3019 | if (conf->proxy_lengths == NULL) { | ||
3037 | 3020 | conf->proxy_lengths = prev->proxy_lengths; | ||
3038 | 3021 | conf->proxy_values = prev->proxy_values; | ||
3039 | 3022 | } | ||
3040 | 3023 | |||
3041 | 3024 | if (conf->upstream.upstream || conf->proxy_lengths) { | ||
3042 | 3025 | clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module); | ||
3043 | 3026 | if (clcf->handler == NULL && clcf->lmt_excpt) { | ||
3044 | 3027 | clcf->handler = ngx_http_proxy_handler; | ||
3045 | 3028 | conf->location = prev->location; | ||
3046 | 3029 | } | ||
3047 | 3030 | } | ||
3048 | 3031 | |||
3049 | 3032 | if (conf->body_source.data == NULL) { | ||
3050 | 3033 | conf->body_source = prev->body_source; | ||
3051 | 3034 | conf->body_set_len = prev->body_set_len; | ||
3052 | 3035 | conf->body_set = prev->body_set; | ||
3053 | 3036 | } | ||
3054 | 3037 | |||
3055 | 3038 | if (conf->body_source.data && conf->body_set_len == NULL) { | ||
3056 | 3039 | |||
3057 | 3040 | ngx_memzero(&sc, sizeof(ngx_http_script_compile_t)); | ||
3058 | 3041 | |||
3059 | 3042 | sc.cf = cf; | ||
3060 | 3043 | sc.source = &conf->body_source; | ||
3061 | 3044 | sc.flushes = &conf->flushes; | ||
3062 | 3045 | sc.lengths = &conf->body_set_len; | ||
3063 | 3046 | sc.values = &conf->body_set; | ||
3064 | 3047 | sc.complete_lengths = 1; | ||
3065 | 3048 | sc.complete_values = 1; | ||
3066 | 3049 | |||
3067 | 3050 | if (ngx_http_script_compile(&sc) != NGX_OK) { | ||
3068 | 3051 | return NGX_CONF_ERROR; | ||
3069 | 3052 | } | ||
3070 | 3053 | } | ||
3071 | 3054 | |||
3072 | 3055 | if (ngx_http_proxy_merge_headers(cf, conf, prev) != NGX_OK) { | ||
3073 | 3056 | return NGX_CONF_ERROR; | ||
3074 | 3057 | } | ||
3075 | 3058 | |||
3076 | 3059 | return NGX_CONF_OK; | ||
3077 | 3060 | } | ||
3078 | 3061 | |||
3079 | 3062 | |||
3080 | 3063 | static ngx_int_t | ||
3081 | 3064 | ngx_http_proxy_merge_headers(ngx_conf_t *cf, ngx_http_proxy_loc_conf_t *conf, | ||
3082 | 3065 | ngx_http_proxy_loc_conf_t *prev) | ||
3083 | 3066 | { | ||
3084 | 3067 | u_char *p; | ||
3085 | 3068 | size_t size; | ||
3086 | 3069 | uintptr_t *code; | ||
3087 | 3070 | ngx_uint_t i; | ||
3088 | 3071 | ngx_array_t headers_names, headers_merged; | ||
3089 | 3072 | ngx_keyval_t *src, *s, *h; | ||
3090 | 3073 | ngx_hash_key_t *hk; | ||
3091 | 3074 | ngx_hash_init_t hash; | ||
3092 | 3075 | ngx_http_script_compile_t sc; | ||
3093 | 3076 | ngx_http_script_copy_code_t *copy; | ||
3094 | 3077 | |||
3095 | 3078 | if (conf->headers_source == NULL) { | ||
3096 | 3079 | conf->flushes = prev->flushes; | ||
3097 | 3080 | conf->headers_set_len = prev->headers_set_len; | ||
3098 | 3081 | conf->headers_set = prev->headers_set; | ||
3099 | 3082 | conf->headers_set_hash = prev->headers_set_hash; | ||
3100 | 3083 | conf->headers_source = prev->headers_source; | ||
3101 | 3084 | } | ||
3102 | 3085 | |||
3103 | 3086 | if (conf->headers_set_hash.buckets | ||
3104 | 3087 | && ((conf->body_source.data == NULL) | ||
3105 | 3088 | == (prev->body_source.data == NULL)) | ||
3106 | 3089 | #if (NGX_HTTP_CACHE) | ||
3107 | 3090 | && ((conf->upstream.cache == NULL) == (prev->upstream.cache == NULL)) | ||
3108 | 3091 | #endif | ||
3109 | 3092 | ) | ||
3110 | 3093 | { | ||
3111 | 3094 | return NGX_OK; | ||
3112 | 3095 | } | ||
3113 | 3096 | |||
3114 | 3097 | |||
3115 | 3098 | if (ngx_array_init(&headers_names, cf->temp_pool, 4, sizeof(ngx_hash_key_t)) | ||
3116 | 3099 | != NGX_OK) | ||
3117 | 3100 | { | ||
3118 | 3101 | return NGX_ERROR; | ||
3119 | 3102 | } | ||
3120 | 3103 | |||
3121 | 3104 | if (ngx_array_init(&headers_merged, cf->temp_pool, 4, sizeof(ngx_keyval_t)) | ||
3122 | 3105 | != NGX_OK) | ||
3123 | 3106 | { | ||
3124 | 3107 | return NGX_ERROR; | ||
3125 | 3108 | } | ||
3126 | 3109 | |||
3127 | 3110 | if (conf->headers_source == NULL) { | ||
3128 | 3111 | conf->headers_source = ngx_array_create(cf->pool, 4, | ||
3129 | 3112 | sizeof(ngx_keyval_t)); | ||
3130 | 3113 | if (conf->headers_source == NULL) { | ||
3131 | 3114 | return NGX_ERROR; | ||
3132 | 3115 | } | ||
3133 | 3116 | } | ||
3134 | 3117 | |||
3135 | 3118 | conf->headers_set_len = ngx_array_create(cf->pool, 64, 1); | ||
3136 | 3119 | if (conf->headers_set_len == NULL) { | ||
3137 | 3120 | return NGX_ERROR; | ||
3138 | 3121 | } | ||
3139 | 3122 | |||
3140 | 3123 | conf->headers_set = ngx_array_create(cf->pool, 512, 1); | ||
3141 | 3124 | if (conf->headers_set == NULL) { | ||
3142 | 3125 | return NGX_ERROR; | ||
3143 | 3126 | } | ||
3144 | 3127 | |||
3145 | 3128 | |||
3146 | 3129 | #if (NGX_HTTP_CACHE) | ||
3147 | 3130 | |||
3148 | 3131 | h = conf->upstream.cache ? ngx_http_proxy_cache_headers: | ||
3149 | 3132 | ngx_http_proxy_headers; | ||
3150 | 3133 | #else | ||
3151 | 3134 | |||
3152 | 3135 | h = ngx_http_proxy_headers; | ||
3153 | 3136 | |||
3154 | 3137 | #endif | ||
3155 | 3138 | |||
3156 | 3139 | src = conf->headers_source->elts; | ||
3157 | 3140 | for (i = 0; i < conf->headers_source->nelts; i++) { | ||
3158 | 3141 | |||
3159 | 3142 | s = ngx_array_push(&headers_merged); | ||
3160 | 3143 | if (s == NULL) { | ||
3161 | 3144 | return NGX_ERROR; | ||
3162 | 3145 | } | ||
3163 | 3146 | |||
3164 | 3147 | *s = src[i]; | ||
3165 | 3148 | } | ||
3166 | 3149 | |||
3167 | 3150 | while (h->key.len) { | ||
3168 | 3151 | |||
3169 | 3152 | src = headers_merged.elts; | ||
3170 | 3153 | for (i = 0; i < headers_merged.nelts; i++) { | ||
3171 | 3154 | if (ngx_strcasecmp(h->key.data, src[i].key.data) == 0) { | ||
3172 | 3155 | goto next; | ||
3173 | 3156 | } | ||
3174 | 3157 | } | ||
3175 | 3158 | |||
3176 | 3159 | s = ngx_array_push(&headers_merged); | ||
3177 | 3160 | if (s == NULL) { | ||
3178 | 3161 | return NGX_ERROR; | ||
3179 | 3162 | } | ||
3180 | 3163 | |||
3181 | 3164 | *s = *h; | ||
3182 | 3165 | |||
3183 | 3166 | next: | ||
3184 | 3167 | |||
3185 | 3168 | h++; | ||
3186 | 3169 | } | ||
3187 | 3170 | |||
3188 | 3171 | if (conf->body_source.data) { | ||
3189 | 3172 | s = ngx_array_push(&headers_merged); | ||
3190 | 3173 | if (s == NULL) { | ||
3191 | 3174 | return NGX_ERROR; | ||
3192 | 3175 | } | ||
3193 | 3176 | |||
3194 | 3177 | ngx_str_set(&s->key, "Content-Length"); | ||
3195 | 3178 | ngx_str_set(&s->value, "$proxy_internal_body_length"); | ||
3196 | 3179 | } | ||
3197 | 3180 | |||
3198 | 3181 | |||
3199 | 3182 | src = headers_merged.elts; | ||
3200 | 3183 | for (i = 0; i < headers_merged.nelts; i++) { | ||
3201 | 3184 | |||
3202 | 3185 | hk = ngx_array_push(&headers_names); | ||
3203 | 3186 | if (hk == NULL) { | ||
3204 | 3187 | return NGX_ERROR; | ||
3205 | 3188 | } | ||
3206 | 3189 | |||
3207 | 3190 | hk->key = src[i].key; | ||
3208 | 3191 | hk->key_hash = ngx_hash_key_lc(src[i].key.data, src[i].key.len); | ||
3209 | 3192 | hk->value = (void *) 1; | ||
3210 | 3193 | |||
3211 | 3194 | if (src[i].value.len == 0) { | ||
3212 | 3195 | continue; | ||
3213 | 3196 | } | ||
3214 | 3197 | |||
3215 | 3198 | if (ngx_http_script_variables_count(&src[i].value) == 0) { | ||
3216 | 3199 | copy = ngx_array_push_n(conf->headers_set_len, | ||
3217 | 3200 | sizeof(ngx_http_script_copy_code_t)); | ||
3218 | 3201 | if (copy == NULL) { | ||
3219 | 3202 | return NGX_ERROR; | ||
3220 | 3203 | } | ||
3221 | 3204 | |||
3222 | 3205 | copy->code = (ngx_http_script_code_pt) | ||
3223 | 3206 | ngx_http_script_copy_len_code; | ||
3224 | 3207 | copy->len = src[i].key.len + sizeof(": ") - 1 | ||
3225 | 3208 | + src[i].value.len + sizeof(CRLF) - 1; | ||
3226 | 3209 | |||
3227 | 3210 | |||
3228 | 3211 | size = (sizeof(ngx_http_script_copy_code_t) | ||
3229 | 3212 | + src[i].key.len + sizeof(": ") - 1 | ||
3230 | 3213 | + src[i].value.len + sizeof(CRLF) - 1 | ||
3231 | 3214 | + sizeof(uintptr_t) - 1) | ||
3232 | 3215 | & ~(sizeof(uintptr_t) - 1); | ||
3233 | 3216 | |||
3234 | 3217 | copy = ngx_array_push_n(conf->headers_set, size); | ||
3235 | 3218 | if (copy == NULL) { | ||
3236 | 3219 | return NGX_ERROR; | ||
3237 | 3220 | } | ||
3238 | 3221 | |||
3239 | 3222 | copy->code = ngx_http_script_copy_code; | ||
3240 | 3223 | copy->len = src[i].key.len + sizeof(": ") - 1 | ||
3241 | 3224 | + src[i].value.len + sizeof(CRLF) - 1; | ||
3242 | 3225 | |||
3243 | 3226 | p = (u_char *) copy + sizeof(ngx_http_script_copy_code_t); | ||
3244 | 3227 | |||
3245 | 3228 | p = ngx_cpymem(p, src[i].key.data, src[i].key.len); | ||
3246 | 3229 | *p++ = ':'; *p++ = ' '; | ||
3247 | 3230 | p = ngx_cpymem(p, src[i].value.data, src[i].value.len); | ||
3248 | 3231 | *p++ = CR; *p = LF; | ||
3249 | 3232 | |||
3250 | 3233 | } else { | ||
3251 | 3234 | copy = ngx_array_push_n(conf->headers_set_len, | ||
3252 | 3235 | sizeof(ngx_http_script_copy_code_t)); | ||
3253 | 3236 | if (copy == NULL) { | ||
3254 | 3237 | return NGX_ERROR; | ||
3255 | 3238 | } | ||
3256 | 3239 | |||
3257 | 3240 | copy->code = (ngx_http_script_code_pt) | ||
3258 | 3241 | ngx_http_script_copy_len_code; | ||
3259 | 3242 | copy->len = src[i].key.len + sizeof(": ") - 1; | ||
3260 | 3243 | |||
3261 | 3244 | |||
3262 | 3245 | size = (sizeof(ngx_http_script_copy_code_t) | ||
3263 | 3246 | + src[i].key.len + sizeof(": ") - 1 + sizeof(uintptr_t) - 1) | ||
3264 | 3247 | & ~(sizeof(uintptr_t) - 1); | ||
3265 | 3248 | |||
3266 | 3249 | copy = ngx_array_push_n(conf->headers_set, size); | ||
3267 | 3250 | if (copy == NULL) { | ||
3268 | 3251 | return NGX_ERROR; | ||
3269 | 3252 | } | ||
3270 | 3253 | |||
3271 | 3254 | copy->code = ngx_http_script_copy_code; | ||
3272 | 3255 | copy->len = src[i].key.len + sizeof(": ") - 1; | ||
3273 | 3256 | |||
3274 | 3257 | p = (u_char *) copy + sizeof(ngx_http_script_copy_code_t); | ||
3275 | 3258 | p = ngx_cpymem(p, src[i].key.data, src[i].key.len); | ||
3276 | 3259 | *p++ = ':'; *p = ' '; | ||
3277 | 3260 | |||
3278 | 3261 | |||
3279 | 3262 | ngx_memzero(&sc, sizeof(ngx_http_script_compile_t)); | ||
3280 | 3263 | |||
3281 | 3264 | sc.cf = cf; | ||
3282 | 3265 | sc.source = &src[i].value; | ||
3283 | 3266 | sc.flushes = &conf->flushes; | ||
3284 | 3267 | sc.lengths = &conf->headers_set_len; | ||
3285 | 3268 | sc.values = &conf->headers_set; | ||
3286 | 3269 | |||
3287 | 3270 | if (ngx_http_script_compile(&sc) != NGX_OK) { | ||
3288 | 3271 | return NGX_ERROR; | ||
3289 | 3272 | } | ||
3290 | 3273 | |||
3291 | 3274 | |||
3292 | 3275 | copy = ngx_array_push_n(conf->headers_set_len, | ||
3293 | 3276 | sizeof(ngx_http_script_copy_code_t)); | ||
3294 | 3277 | if (copy == NULL) { | ||
3295 | 3278 | return NGX_ERROR; | ||
3296 | 3279 | } | ||
3297 | 3280 | |||
3298 | 3281 | copy->code = (ngx_http_script_code_pt) | ||
3299 | 3282 | ngx_http_script_copy_len_code; | ||
3300 | 3283 | copy->len = sizeof(CRLF) - 1; | ||
3301 | 3284 | |||
3302 | 3285 | |||
3303 | 3286 | size = (sizeof(ngx_http_script_copy_code_t) | ||
3304 | 3287 | + sizeof(CRLF) - 1 + sizeof(uintptr_t) - 1) | ||
3305 | 3288 | & ~(sizeof(uintptr_t) - 1); | ||
3306 | 3289 | |||
3307 | 3290 | copy = ngx_array_push_n(conf->headers_set, size); | ||
3308 | 3291 | if (copy == NULL) { | ||
3309 | 3292 | return NGX_ERROR; | ||
3310 | 3293 | } | ||
3311 | 3294 | |||
3312 | 3295 | copy->code = ngx_http_script_copy_code; | ||
3313 | 3296 | copy->len = sizeof(CRLF) - 1; | ||
3314 | 3297 | |||
3315 | 3298 | p = (u_char *) copy + sizeof(ngx_http_script_copy_code_t); | ||
3316 | 3299 | *p++ = CR; *p = LF; | ||
3317 | 3300 | } | ||
3318 | 3301 | |||
3319 | 3302 | code = ngx_array_push_n(conf->headers_set_len, sizeof(uintptr_t)); | ||
3320 | 3303 | if (code == NULL) { | ||
3321 | 3304 | return NGX_ERROR; | ||
3322 | 3305 | } | ||
3323 | 3306 | |||
3324 | 3307 | *code = (uintptr_t) NULL; | ||
3325 | 3308 | |||
3326 | 3309 | code = ngx_array_push_n(conf->headers_set, sizeof(uintptr_t)); | ||
3327 | 3310 | if (code == NULL) { | ||
3328 | 3311 | return NGX_ERROR; | ||
3329 | 3312 | } | ||
3330 | 3313 | |||
3331 | 3314 | *code = (uintptr_t) NULL; | ||
3332 | 3315 | } | ||
3333 | 3316 | |||
3334 | 3317 | code = ngx_array_push_n(conf->headers_set_len, sizeof(uintptr_t)); | ||
3335 | 3318 | if (code == NULL) { | ||
3336 | 3319 | return NGX_ERROR; | ||
3337 | 3320 | } | ||
3338 | 3321 | |||
3339 | 3322 | *code = (uintptr_t) NULL; | ||
3340 | 3323 | |||
3341 | 3324 | |||
3342 | 3325 | hash.hash = &conf->headers_set_hash; | ||
3343 | 3326 | hash.key = ngx_hash_key_lc; | ||
3344 | 3327 | hash.max_size = conf->headers_hash_max_size; | ||
3345 | 3328 | hash.bucket_size = conf->headers_hash_bucket_size; | ||
3346 | 3329 | hash.name = "proxy_headers_hash"; | ||
3347 | 3330 | hash.pool = cf->pool; | ||
3348 | 3331 | hash.temp_pool = NULL; | ||
3349 | 3332 | |||
3350 | 3333 | return ngx_hash_init(&hash, headers_names.elts, headers_names.nelts); | ||
3351 | 3334 | } | ||
3352 | 3335 | |||
3353 | 3336 | |||
3354 | 3337 | static char * | ||
3355 | 3338 | ngx_http_proxy_pass(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | ||
3356 | 3339 | { | ||
3357 | 3340 | ngx_http_proxy_loc_conf_t *plcf = conf; | ||
3358 | 3341 | |||
3359 | 3342 | size_t add; | ||
3360 | 3343 | u_short port; | ||
3361 | 3344 | ngx_str_t *value, *url; | ||
3362 | 3345 | ngx_url_t u; | ||
3363 | 3346 | ngx_uint_t n; | ||
3364 | 3347 | ngx_http_core_loc_conf_t *clcf; | ||
3365 | 3348 | ngx_http_script_compile_t sc; | ||
3366 | 3349 | |||
3367 | 3350 | if (plcf->upstream.upstream || plcf->proxy_lengths) { | ||
3368 | 3351 | return "is duplicate"; | ||
3369 | 3352 | } | ||
3370 | 3353 | |||
3371 | 3354 | clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module); | ||
3372 | 3355 | |||
3373 | 3356 | clcf->handler = ngx_http_proxy_handler; | ||
3374 | 3357 | |||
3375 | 3358 | if (clcf->name.data[clcf->name.len - 1] == '/') { | ||
3376 | 3359 | clcf->auto_redirect = 1; | ||
3377 | 3360 | } | ||
3378 | 3361 | |||
3379 | 3362 | value = cf->args->elts; | ||
3380 | 3363 | |||
3381 | 3364 | url = &value[1]; | ||
3382 | 3365 | |||
3383 | 3366 | n = ngx_http_script_variables_count(url); | ||
3384 | 3367 | |||
3385 | 3368 | if (n) { | ||
3386 | 3369 | |||
3387 | 3370 | ngx_memzero(&sc, sizeof(ngx_http_script_compile_t)); | ||
3388 | 3371 | |||
3389 | 3372 | sc.cf = cf; | ||
3390 | 3373 | sc.source = url; | ||
3391 | 3374 | sc.lengths = &plcf->proxy_lengths; | ||
3392 | 3375 | sc.values = &plcf->proxy_values; | ||
3393 | 3376 | sc.variables = n; | ||
3394 | 3377 | sc.complete_lengths = 1; | ||
3395 | 3378 | sc.complete_values = 1; | ||
3396 | 3379 | |||
3397 | 3380 | if (ngx_http_script_compile(&sc) != NGX_OK) { | ||
3398 | 3381 | return NGX_CONF_ERROR; | ||
3399 | 3382 | } | ||
3400 | 3383 | |||
3401 | 3384 | #if (NGX_HTTP_SSL) | ||
3402 | 3385 | if (ngx_http_proxy_set_ssl(cf, plcf) != NGX_OK) { | ||
3403 | 3386 | return NGX_CONF_ERROR; | ||
3404 | 3387 | } | ||
3405 | 3388 | #endif | ||
3406 | 3389 | |||
3407 | 3390 | return NGX_CONF_OK; | ||
3408 | 3391 | } | ||
3409 | 3392 | |||
3410 | 3393 | if (ngx_strncasecmp(url->data, (u_char *) "http://", 7) == 0) { | ||
3411 | 3394 | add = 7; | ||
3412 | 3395 | port = 80; | ||
3413 | 3396 | |||
3414 | 3397 | } else if (ngx_strncasecmp(url->data, (u_char *) "https://", 8) == 0) { | ||
3415 | 3398 | |||
3416 | 3399 | #if (NGX_HTTP_SSL) | ||
3417 | 3400 | if (ngx_http_proxy_set_ssl(cf, plcf) != NGX_OK) { | ||
3418 | 3401 | return NGX_CONF_ERROR; | ||
3419 | 3402 | } | ||
3420 | 3403 | |||
3421 | 3404 | add = 8; | ||
3422 | 3405 | port = 443; | ||
3423 | 3406 | #else | ||
3424 | 3407 | ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | ||
3425 | 3408 | "https protocol requires SSL support"); | ||
3426 | 3409 | return NGX_CONF_ERROR; | ||
3427 | 3410 | #endif | ||
3428 | 3411 | |||
3429 | 3412 | } else { | ||
3430 | 3413 | ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid URL prefix"); | ||
3431 | 3414 | return NGX_CONF_ERROR; | ||
3432 | 3415 | } | ||
3433 | 3416 | |||
3434 | 3417 | ngx_memzero(&u, sizeof(ngx_url_t)); | ||
3435 | 3418 | |||
3436 | 3419 | u.url.len = url->len - add; | ||
3437 | 3420 | u.url.data = url->data + add; | ||
3438 | 3421 | u.default_port = port; | ||
3439 | 3422 | u.uri_part = 1; | ||
3440 | 3423 | u.no_resolve = 1; | ||
3441 | 3424 | |||
3442 | 3425 | plcf->upstream.upstream = ngx_http_upstream_add(cf, &u, 0); | ||
3443 | 3426 | if (plcf->upstream.upstream == NULL) { | ||
3444 | 3427 | return NGX_CONF_ERROR; | ||
3445 | 3428 | } | ||
3446 | 3429 | |||
3447 | 3430 | plcf->vars.schema.len = add; | ||
3448 | 3431 | plcf->vars.schema.data = url->data; | ||
3449 | 3432 | plcf->vars.key_start = plcf->vars.schema; | ||
3450 | 3433 | |||
3451 | 3434 | ngx_http_proxy_set_vars(&u, &plcf->vars); | ||
3452 | 3435 | |||
3453 | 3436 | plcf->location = clcf->name; | ||
3454 | 3437 | |||
3455 | 3438 | if (clcf->named | ||
3456 | 3439 | #if (NGX_PCRE) | ||
3457 | 3440 | || clcf->regex | ||
3458 | 3441 | #endif | ||
3459 | 3442 | || clcf->noname) | ||
3460 | 3443 | { | ||
3461 | 3444 | if (plcf->vars.uri.len) { | ||
3462 | 3445 | ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | ||
3463 | 3446 | "\"proxy_pass\" cannot have URI part in " | ||
3464 | 3447 | "location given by regular expression, " | ||
3465 | 3448 | "or inside named location, " | ||
3466 | 3449 | "or inside \"if\" statement, " | ||
3467 | 3450 | "or inside \"limit_except\" block"); | ||
3468 | 3451 | return NGX_CONF_ERROR; | ||
3469 | 3452 | } | ||
3470 | 3453 | |||
3471 | 3454 | plcf->location.len = 0; | ||
3472 | 3455 | } | ||
3473 | 3456 | |||
3474 | 3457 | plcf->url = *url; | ||
3475 | 3458 | |||
3476 | 3459 | return NGX_CONF_OK; | ||
3477 | 3460 | } | ||
3478 | 3461 | |||
3479 | 3462 | |||
3480 | 3463 | static char * | ||
3481 | 3464 | ngx_http_proxy_redirect(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | ||
3482 | 3465 | { | ||
3483 | 3466 | ngx_http_proxy_loc_conf_t *plcf = conf; | ||
3484 | 3467 | |||
3485 | 3468 | u_char *p; | ||
3486 | 3469 | ngx_str_t *value; | ||
3487 | 3470 | ngx_http_proxy_rewrite_t *pr; | ||
3488 | 3471 | ngx_http_compile_complex_value_t ccv; | ||
3489 | 3472 | |||
3490 | 3473 | if (plcf->redirect == 0) { | ||
3491 | 3474 | return NGX_CONF_OK; | ||
3492 | 3475 | } | ||
3493 | 3476 | |||
3494 | 3477 | plcf->redirect = 1; | ||
3495 | 3478 | |||
3496 | 3479 | value = cf->args->elts; | ||
3497 | 3480 | |||
3498 | 3481 | if (cf->args->nelts == 2) { | ||
3499 | 3482 | if (ngx_strcmp(value[1].data, "off") == 0) { | ||
3500 | 3483 | plcf->redirect = 0; | ||
3501 | 3484 | plcf->redirects = NULL; | ||
3502 | 3485 | return NGX_CONF_OK; | ||
3503 | 3486 | } | ||
3504 | 3487 | |||
3505 | 3488 | if (ngx_strcmp(value[1].data, "false") == 0) { | ||
3506 | 3489 | ngx_conf_log_error(NGX_LOG_ERR, cf, 0, | ||
3507 | 3490 | "invalid parameter \"false\", use \"off\" instead"); | ||
3508 | 3491 | plcf->redirect = 0; | ||
3509 | 3492 | plcf->redirects = NULL; | ||
3510 | 3493 | return NGX_CONF_OK; | ||
3511 | 3494 | } | ||
3512 | 3495 | |||
3513 | 3496 | if (ngx_strcmp(value[1].data, "default") != 0) { | ||
3514 | 3497 | ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | ||
3515 | 3498 | "invalid parameter \"%V\"", &value[1]); | ||
3516 | 3499 | return NGX_CONF_ERROR; | ||
3517 | 3500 | } | ||
3518 | 3501 | } | ||
3519 | 3502 | |||
3520 | 3503 | if (plcf->redirects == NULL) { | ||
3521 | 3504 | plcf->redirects = ngx_array_create(cf->pool, 1, | ||
3522 | 3505 | sizeof(ngx_http_proxy_rewrite_t)); | ||
3523 | 3506 | if (plcf->redirects == NULL) { | ||
3524 | 3507 | return NGX_CONF_ERROR; | ||
3525 | 3508 | } | ||
3526 | 3509 | } | ||
3527 | 3510 | |||
3528 | 3511 | pr = ngx_array_push(plcf->redirects); | ||
3529 | 3512 | if (pr == NULL) { | ||
3530 | 3513 | return NGX_CONF_ERROR; | ||
3531 | 3514 | } | ||
3532 | 3515 | |||
3533 | 3516 | if (ngx_strcmp(value[1].data, "default") == 0) { | ||
3534 | 3517 | if (plcf->proxy_lengths) { | ||
3535 | 3518 | ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | ||
3536 | 3519 | "\"proxy_redirect default\" cannot be used " | ||
3537 | 3520 | "with \"proxy_pass\" directive with variables"); | ||
3538 | 3521 | return NGX_CONF_ERROR; | ||
3539 | 3522 | } | ||
3540 | 3523 | |||
3541 | 3524 | if (plcf->url.data == NULL) { | ||
3542 | 3525 | ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | ||
3543 | 3526 | "\"proxy_redirect default\" should be placed " | ||
3544 | 3527 | "after the \"proxy_pass\" directive"); | ||
3545 | 3528 | return NGX_CONF_ERROR; | ||
3546 | 3529 | } | ||
3547 | 3530 | |||
3548 | 3531 | pr->handler = ngx_http_proxy_rewrite_complex_handler; | ||
3549 | 3532 | |||
3550 | 3533 | ngx_memzero(&pr->pattern.complex, sizeof(ngx_http_complex_value_t)); | ||
3551 | 3534 | |||
3552 | 3535 | ngx_memzero(&pr->replacement, sizeof(ngx_http_complex_value_t)); | ||
3553 | 3536 | |||
3554 | 3537 | if (plcf->vars.uri.len) { | ||
3555 | 3538 | pr->pattern.complex.value = plcf->url; | ||
3556 | 3539 | pr->replacement.value = plcf->location; | ||
3557 | 3540 | |||
3558 | 3541 | } else { | ||
3559 | 3542 | pr->pattern.complex.value.len = plcf->url.len + sizeof("/") - 1; | ||
3560 | 3543 | |||
3561 | 3544 | p = ngx_pnalloc(cf->pool, pr->pattern.complex.value.len); | ||
3562 | 3545 | if (p == NULL) { | ||
3563 | 3546 | return NGX_CONF_ERROR; | ||
3564 | 3547 | } | ||
3565 | 3548 | |||
3566 | 3549 | pr->pattern.complex.value.data = p; | ||
3567 | 3550 | |||
3568 | 3551 | p = ngx_cpymem(p, plcf->url.data, plcf->url.len); | ||
3569 | 3552 | *p = '/'; | ||
3570 | 3553 | |||
3571 | 3554 | ngx_str_set(&pr->replacement.value, "/"); | ||
3572 | 3555 | } | ||
3573 | 3556 | |||
3574 | 3557 | return NGX_CONF_OK; | ||
3575 | 3558 | } | ||
3576 | 3559 | |||
3577 | 3560 | |||
3578 | 3561 | if (value[1].data[0] == '~') { | ||
3579 | 3562 | value[1].len--; | ||
3580 | 3563 | value[1].data++; | ||
3581 | 3564 | |||
3582 | 3565 | if (value[1].data[0] == '*') { | ||
3583 | 3566 | value[1].len--; | ||
3584 | 3567 | value[1].data++; | ||
3585 | 3568 | |||
3586 | 3569 | if (ngx_http_proxy_rewrite_regex(cf, pr, &value[1], 1) != NGX_OK) { | ||
3587 | 3570 | return NGX_CONF_ERROR; | ||
3588 | 3571 | } | ||
3589 | 3572 | |||
3590 | 3573 | } else { | ||
3591 | 3574 | if (ngx_http_proxy_rewrite_regex(cf, pr, &value[1], 0) != NGX_OK) { | ||
3592 | 3575 | return NGX_CONF_ERROR; | ||
3593 | 3576 | } | ||
3594 | 3577 | } | ||
3595 | 3578 | |||
3596 | 3579 | } else { | ||
3597 | 3580 | |||
3598 | 3581 | ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t)); | ||
3599 | 3582 | |||
3600 | 3583 | ccv.cf = cf; | ||
3601 | 3584 | ccv.value = &value[1]; | ||
3602 | 3585 | ccv.complex_value = &pr->pattern.complex; | ||
3603 | 3586 | |||
3604 | 3587 | if (ngx_http_compile_complex_value(&ccv) != NGX_OK) { | ||
3605 | 3588 | return NGX_CONF_ERROR; | ||
3606 | 3589 | } | ||
3607 | 3590 | |||
3608 | 3591 | pr->handler = ngx_http_proxy_rewrite_complex_handler; | ||
3609 | 3592 | } | ||
3610 | 3593 | |||
3611 | 3594 | |||
3612 | 3595 | ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t)); | ||
3613 | 3596 | |||
3614 | 3597 | ccv.cf = cf; | ||
3615 | 3598 | ccv.value = &value[2]; | ||
3616 | 3599 | ccv.complex_value = &pr->replacement; | ||
3617 | 3600 | |||
3618 | 3601 | if (ngx_http_compile_complex_value(&ccv) != NGX_OK) { | ||
3619 | 3602 | return NGX_CONF_ERROR; | ||
3620 | 3603 | } | ||
3621 | 3604 | |||
3622 | 3605 | return NGX_CONF_OK; | ||
3623 | 3606 | } | ||
3624 | 3607 | |||
3625 | 3608 | |||
3626 | 3609 | static char * | ||
3627 | 3610 | ngx_http_proxy_cookie_domain(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | ||
3628 | 3611 | { | ||
3629 | 3612 | ngx_http_proxy_loc_conf_t *plcf = conf; | ||
3630 | 3613 | |||
3631 | 3614 | ngx_str_t *value; | ||
3632 | 3615 | ngx_http_proxy_rewrite_t *pr; | ||
3633 | 3616 | ngx_http_compile_complex_value_t ccv; | ||
3634 | 3617 | |||
3635 | 3618 | if (plcf->cookie_domains == NULL) { | ||
3636 | 3619 | return NGX_CONF_OK; | ||
3637 | 3620 | } | ||
3638 | 3621 | |||
3639 | 3622 | value = cf->args->elts; | ||
3640 | 3623 | |||
3641 | 3624 | if (cf->args->nelts == 2) { | ||
3642 | 3625 | |||
3643 | 3626 | if (ngx_strcmp(value[1].data, "off") == 0) { | ||
3644 | 3627 | plcf->cookie_domains = NULL; | ||
3645 | 3628 | return NGX_CONF_OK; | ||
3646 | 3629 | } | ||
3647 | 3630 | |||
3648 | 3631 | ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | ||
3649 | 3632 | "invalid parameter \"%V\"", &value[1]); | ||
3650 | 3633 | return NGX_CONF_ERROR; | ||
3651 | 3634 | } | ||
3652 | 3635 | |||
3653 | 3636 | if (plcf->cookie_domains == NGX_CONF_UNSET_PTR) { | ||
3654 | 3637 | plcf->cookie_domains = ngx_array_create(cf->pool, 1, | ||
3655 | 3638 | sizeof(ngx_http_proxy_rewrite_t)); | ||
3656 | 3639 | if (plcf->cookie_domains == NULL) { | ||
3657 | 3640 | return NGX_CONF_ERROR; | ||
3658 | 3641 | } | ||
3659 | 3642 | } | ||
3660 | 3643 | |||
3661 | 3644 | pr = ngx_array_push(plcf->cookie_domains); | ||
3662 | 3645 | if (pr == NULL) { | ||
3663 | 3646 | return NGX_CONF_ERROR; | ||
3664 | 3647 | } | ||
3665 | 3648 | |||
3666 | 3649 | if (value[1].data[0] == '~') { | ||
3667 | 3650 | value[1].len--; | ||
3668 | 3651 | value[1].data++; | ||
3669 | 3652 | |||
3670 | 3653 | if (ngx_http_proxy_rewrite_regex(cf, pr, &value[1], 1) != NGX_OK) { | ||
3671 | 3654 | return NGX_CONF_ERROR; | ||
3672 | 3655 | } | ||
3673 | 3656 | |||
3674 | 3657 | } else { | ||
3675 | 3658 | |||
3676 | 3659 | if (value[1].data[0] == '.') { | ||
3677 | 3660 | value[1].len--; | ||
3678 | 3661 | value[1].data++; | ||
3679 | 3662 | } | ||
3680 | 3663 | |||
3681 | 3664 | ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t)); | ||
3682 | 3665 | |||
3683 | 3666 | ccv.cf = cf; | ||
3684 | 3667 | ccv.value = &value[1]; | ||
3685 | 3668 | ccv.complex_value = &pr->pattern.complex; | ||
3686 | 3669 | |||
3687 | 3670 | if (ngx_http_compile_complex_value(&ccv) != NGX_OK) { | ||
3688 | 3671 | return NGX_CONF_ERROR; | ||
3689 | 3672 | } | ||
3690 | 3673 | |||
3691 | 3674 | pr->handler = ngx_http_proxy_rewrite_domain_handler; | ||
3692 | 3675 | |||
3693 | 3676 | if (value[2].data[0] == '.') { | ||
3694 | 3677 | value[2].len--; | ||
3695 | 3678 | value[2].data++; | ||
3696 | 3679 | } | ||
3697 | 3680 | } | ||
3698 | 3681 | |||
3699 | 3682 | ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t)); | ||
3700 | 3683 | |||
3701 | 3684 | ccv.cf = cf; | ||
3702 | 3685 | ccv.value = &value[2]; | ||
3703 | 3686 | ccv.complex_value = &pr->replacement; | ||
3704 | 3687 | |||
3705 | 3688 | if (ngx_http_compile_complex_value(&ccv) != NGX_OK) { | ||
3706 | 3689 | return NGX_CONF_ERROR; | ||
3707 | 3690 | } | ||
3708 | 3691 | |||
3709 | 3692 | return NGX_CONF_OK; | ||
3710 | 3693 | } | ||
3711 | 3694 | |||
3712 | 3695 | |||
3713 | 3696 | static char * | ||
3714 | 3697 | ngx_http_proxy_cookie_path(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | ||
3715 | 3698 | { | ||
3716 | 3699 | ngx_http_proxy_loc_conf_t *plcf = conf; | ||
3717 | 3700 | |||
3718 | 3701 | ngx_str_t *value; | ||
3719 | 3702 | ngx_http_proxy_rewrite_t *pr; | ||
3720 | 3703 | ngx_http_compile_complex_value_t ccv; | ||
3721 | 3704 | |||
3722 | 3705 | if (plcf->cookie_paths == NULL) { | ||
3723 | 3706 | return NGX_CONF_OK; | ||
3724 | 3707 | } | ||
3725 | 3708 | |||
3726 | 3709 | value = cf->args->elts; | ||
3727 | 3710 | |||
3728 | 3711 | if (cf->args->nelts == 2) { | ||
3729 | 3712 | |||
3730 | 3713 | if (ngx_strcmp(value[1].data, "off") == 0) { | ||
3731 | 3714 | plcf->cookie_paths = NULL; | ||
3732 | 3715 | return NGX_CONF_OK; | ||
3733 | 3716 | } | ||
3734 | 3717 | |||
3735 | 3718 | ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | ||
3736 | 3719 | "invalid parameter \"%V\"", &value[1]); | ||
3737 | 3720 | return NGX_CONF_ERROR; | ||
3738 | 3721 | } | ||
3739 | 3722 | |||
3740 | 3723 | if (plcf->cookie_paths == NGX_CONF_UNSET_PTR) { | ||
3741 | 3724 | plcf->cookie_paths = ngx_array_create(cf->pool, 1, | ||
3742 | 3725 | sizeof(ngx_http_proxy_rewrite_t)); | ||
3743 | 3726 | if (plcf->cookie_paths == NULL) { | ||
3744 | 3727 | return NGX_CONF_ERROR; | ||
3745 | 3728 | } | ||
3746 | 3729 | } | ||
3747 | 3730 | |||
3748 | 3731 | pr = ngx_array_push(plcf->cookie_paths); | ||
3749 | 3732 | if (pr == NULL) { | ||
3750 | 3733 | return NGX_CONF_ERROR; | ||
3751 | 3734 | } | ||
3752 | 3735 | |||
3753 | 3736 | if (value[1].data[0] == '~') { | ||
3754 | 3737 | value[1].len--; | ||
3755 | 3738 | value[1].data++; | ||
3756 | 3739 | |||
3757 | 3740 | if (value[1].data[0] == '*') { | ||
3758 | 3741 | value[1].len--; | ||
3759 | 3742 | value[1].data++; | ||
3760 | 3743 | |||
3761 | 3744 | if (ngx_http_proxy_rewrite_regex(cf, pr, &value[1], 1) != NGX_OK) { | ||
3762 | 3745 | return NGX_CONF_ERROR; | ||
3763 | 3746 | } | ||
3764 | 3747 | |||
3765 | 3748 | } else { | ||
3766 | 3749 | if (ngx_http_proxy_rewrite_regex(cf, pr, &value[1], 0) != NGX_OK) { | ||
3767 | 3750 | return NGX_CONF_ERROR; | ||
3768 | 3751 | } | ||
3769 | 3752 | } | ||
3770 | 3753 | |||
3771 | 3754 | } else { | ||
3772 | 3755 | |||
3773 | 3756 | ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t)); | ||
3774 | 3757 | |||
3775 | 3758 | ccv.cf = cf; | ||
3776 | 3759 | ccv.value = &value[1]; | ||
3777 | 3760 | ccv.complex_value = &pr->pattern.complex; | ||
3778 | 3761 | |||
3779 | 3762 | if (ngx_http_compile_complex_value(&ccv) != NGX_OK) { | ||
3780 | 3763 | return NGX_CONF_ERROR; | ||
3781 | 3764 | } | ||
3782 | 3765 | |||
3783 | 3766 | pr->handler = ngx_http_proxy_rewrite_complex_handler; | ||
3784 | 3767 | } | ||
3785 | 3768 | |||
3786 | 3769 | ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t)); | ||
3787 | 3770 | |||
3788 | 3771 | ccv.cf = cf; | ||
3789 | 3772 | ccv.value = &value[2]; | ||
3790 | 3773 | ccv.complex_value = &pr->replacement; | ||
3791 | 3774 | |||
3792 | 3775 | if (ngx_http_compile_complex_value(&ccv) != NGX_OK) { | ||
3793 | 3776 | return NGX_CONF_ERROR; | ||
3794 | 3777 | } | ||
3795 | 3778 | |||
3796 | 3779 | return NGX_CONF_OK; | ||
3797 | 3780 | } | ||
3798 | 3781 | |||
3799 | 3782 | |||
3800 | 3783 | static ngx_int_t | ||
3801 | 3784 | ngx_http_proxy_rewrite_regex(ngx_conf_t *cf, ngx_http_proxy_rewrite_t *pr, | ||
3802 | 3785 | ngx_str_t *regex, ngx_uint_t caseless) | ||
3803 | 3786 | { | ||
3804 | 3787 | #if (NGX_PCRE) | ||
3805 | 3788 | u_char errstr[NGX_MAX_CONF_ERRSTR]; | ||
3806 | 3789 | ngx_regex_compile_t rc; | ||
3807 | 3790 | |||
3808 | 3791 | ngx_memzero(&rc, sizeof(ngx_regex_compile_t)); | ||
3809 | 3792 | |||
3810 | 3793 | rc.pattern = *regex; | ||
3811 | 3794 | rc.err.len = NGX_MAX_CONF_ERRSTR; | ||
3812 | 3795 | rc.err.data = errstr; | ||
3813 | 3796 | |||
3814 | 3797 | if (caseless) { | ||
3815 | 3798 | rc.options = NGX_REGEX_CASELESS; | ||
3816 | 3799 | } | ||
3817 | 3800 | |||
3818 | 3801 | pr->pattern.regex = ngx_http_regex_compile(cf, &rc); | ||
3819 | 3802 | if (pr->pattern.regex == NULL) { | ||
3820 | 3803 | return NGX_ERROR; | ||
3821 | 3804 | } | ||
3822 | 3805 | |||
3823 | 3806 | pr->handler = ngx_http_proxy_rewrite_regex_handler; | ||
3824 | 3807 | |||
3825 | 3808 | return NGX_OK; | ||
3826 | 3809 | |||
3827 | 3810 | #else | ||
3828 | 3811 | |||
3829 | 3812 | ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | ||
3830 | 3813 | "using regex \"%V\" requires PCRE library", regex); | ||
3831 | 3814 | return NGX_ERROR; | ||
3832 | 3815 | |||
3833 | 3816 | #endif | ||
3834 | 3817 | } | ||
3835 | 3818 | |||
3836 | 3819 | |||
3837 | 3820 | static char * | ||
3838 | 3821 | ngx_http_proxy_store(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | ||
3839 | 3822 | { | ||
3840 | 3823 | ngx_http_proxy_loc_conf_t *plcf = conf; | ||
3841 | 3824 | |||
3842 | 3825 | ngx_str_t *value; | ||
3843 | 3826 | ngx_http_script_compile_t sc; | ||
3844 | 3827 | |||
3845 | 3828 | if (plcf->upstream.store != NGX_CONF_UNSET | ||
3846 | 3829 | || plcf->upstream.store_lengths) | ||
3847 | 3830 | { | ||
3848 | 3831 | return "is duplicate"; | ||
3849 | 3832 | } | ||
3850 | 3833 | |||
3851 | 3834 | value = cf->args->elts; | ||
3852 | 3835 | |||
3853 | 3836 | if (ngx_strcmp(value[1].data, "off") == 0) { | ||
3854 | 3837 | plcf->upstream.store = 0; | ||
3855 | 3838 | return NGX_CONF_OK; | ||
3856 | 3839 | } | ||
3857 | 3840 | |||
3858 | 3841 | #if (NGX_HTTP_CACHE) | ||
3859 | 3842 | |||
3860 | 3843 | if (plcf->upstream.cache != NGX_CONF_UNSET_PTR | ||
3861 | 3844 | && plcf->upstream.cache != NULL) | ||
3862 | 3845 | { | ||
3863 | 3846 | return "is incompatible with \"proxy_cache\""; | ||
3864 | 3847 | } | ||
3865 | 3848 | |||
3866 | 3849 | #endif | ||
3867 | 3850 | |||
3868 | 3851 | if (ngx_strcmp(value[1].data, "on") == 0) { | ||
3869 | 3852 | plcf->upstream.store = 1; | ||
3870 | 3853 | return NGX_CONF_OK; | ||
3871 | 3854 | } | ||
3872 | 3855 | |||
3873 | 3856 | /* include the terminating '\0' into script */ | ||
3874 | 3857 | value[1].len++; | ||
3875 | 3858 | |||
3876 | 3859 | ngx_memzero(&sc, sizeof(ngx_http_script_compile_t)); | ||
3877 | 3860 | |||
3878 | 3861 | sc.cf = cf; | ||
3879 | 3862 | sc.source = &value[1]; | ||
3880 | 3863 | sc.lengths = &plcf->upstream.store_lengths; | ||
3881 | 3864 | sc.values = &plcf->upstream.store_values; | ||
3882 | 3865 | sc.variables = ngx_http_script_variables_count(&value[1]); | ||
3883 | 3866 | sc.complete_lengths = 1; | ||
3884 | 3867 | sc.complete_values = 1; | ||
3885 | 3868 | |||
3886 | 3869 | if (ngx_http_script_compile(&sc) != NGX_OK) { | ||
3887 | 3870 | return NGX_CONF_ERROR; | ||
3888 | 3871 | } | ||
3889 | 3872 | |||
3890 | 3873 | return NGX_CONF_OK; | ||
3891 | 3874 | } | ||
3892 | 3875 | |||
3893 | 3876 | |||
3894 | 3877 | #if (NGX_HTTP_CACHE) | ||
3895 | 3878 | |||
3896 | 3879 | static char * | ||
3897 | 3880 | ngx_http_proxy_cache(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | ||
3898 | 3881 | { | ||
3899 | 3882 | ngx_http_proxy_loc_conf_t *plcf = conf; | ||
3900 | 3883 | |||
3901 | 3884 | ngx_str_t *value; | ||
3902 | 3885 | |||
3903 | 3886 | value = cf->args->elts; | ||
3904 | 3887 | |||
3905 | 3888 | if (plcf->upstream.cache != NGX_CONF_UNSET_PTR) { | ||
3906 | 3889 | return "is duplicate"; | ||
3907 | 3890 | } | ||
3908 | 3891 | |||
3909 | 3892 | if (ngx_strcmp(value[1].data, "off") == 0) { | ||
3910 | 3893 | plcf->upstream.cache = NULL; | ||
3911 | 3894 | return NGX_CONF_OK; | ||
3912 | 3895 | } | ||
3913 | 3896 | |||
3914 | 3897 | if (plcf->upstream.store > 0 || plcf->upstream.store_lengths) { | ||
3915 | 3898 | return "is incompatible with \"proxy_store\""; | ||
3916 | 3899 | } | ||
3917 | 3900 | |||
3918 | 3901 | plcf->upstream.cache = ngx_shared_memory_add(cf, &value[1], 0, | ||
3919 | 3902 | &ngx_http_proxy_module); | ||
3920 | 3903 | if (plcf->upstream.cache == NULL) { | ||
3921 | 3904 | return NGX_CONF_ERROR; | ||
3922 | 3905 | } | ||
3923 | 3906 | |||
3924 | 3907 | return NGX_CONF_OK; | ||
3925 | 3908 | } | ||
3926 | 3909 | |||
3927 | 3910 | |||
3928 | 3911 | static char * | ||
3929 | 3912 | ngx_http_proxy_cache_key(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | ||
3930 | 3913 | { | ||
3931 | 3914 | ngx_http_proxy_loc_conf_t *plcf = conf; | ||
3932 | 3915 | |||
3933 | 3916 | ngx_str_t *value; | ||
3934 | 3917 | ngx_http_compile_complex_value_t ccv; | ||
3935 | 3918 | |||
3936 | 3919 | value = cf->args->elts; | ||
3937 | 3920 | |||
3938 | 3921 | if (plcf->cache_key.value.len) { | ||
3939 | 3922 | return "is duplicate"; | ||
3940 | 3923 | } | ||
3941 | 3924 | |||
3942 | 3925 | ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t)); | ||
3943 | 3926 | |||
3944 | 3927 | ccv.cf = cf; | ||
3945 | 3928 | ccv.value = &value[1]; | ||
3946 | 3929 | ccv.complex_value = &plcf->cache_key; | ||
3947 | 3930 | |||
3948 | 3931 | if (ngx_http_compile_complex_value(&ccv) != NGX_OK) { | ||
3949 | 3932 | return NGX_CONF_ERROR; | ||
3950 | 3933 | } | ||
3951 | 3934 | |||
3952 | 3935 | return NGX_CONF_OK; | ||
3953 | 3936 | } | ||
3954 | 3937 | |||
3955 | 3938 | #endif | ||
3956 | 3939 | |||
3957 | 3940 | |||
3958 | 3941 | static char * | ||
3959 | 3942 | ngx_http_proxy_lowat_check(ngx_conf_t *cf, void *post, void *data) | ||
3960 | 3943 | { | ||
3961 | 3944 | #if (NGX_FREEBSD) | ||
3962 | 3945 | ssize_t *np = data; | ||
3963 | 3946 | |||
3964 | 3947 | if ((u_long) *np >= ngx_freebsd_net_inet_tcp_sendspace) { | ||
3965 | 3948 | ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | ||
3966 | 3949 | "\"proxy_send_lowat\" must be less than %d " | ||
3967 | 3950 | "(sysctl net.inet.tcp.sendspace)", | ||
3968 | 3951 | ngx_freebsd_net_inet_tcp_sendspace); | ||
3969 | 3952 | |||
3970 | 3953 | return NGX_CONF_ERROR; | ||
3971 | 3954 | } | ||
3972 | 3955 | |||
3973 | 3956 | #elif !(NGX_HAVE_SO_SNDLOWAT) | ||
3974 | 3957 | ssize_t *np = data; | ||
3975 | 3958 | |||
3976 | 3959 | ngx_conf_log_error(NGX_LOG_WARN, cf, 0, | ||
3977 | 3960 | "\"proxy_send_lowat\" is not supported, ignored"); | ||
3978 | 3961 | |||
3979 | 3962 | *np = 0; | ||
3980 | 3963 | |||
3981 | 3964 | #endif | ||
3982 | 3965 | |||
3983 | 3966 | return NGX_CONF_OK; | ||
3984 | 3967 | } | ||
3985 | 3968 | |||
3986 | 3969 | |||
3987 | 3970 | #if (NGX_HTTP_SSL) | ||
3988 | 3971 | |||
3989 | 3972 | static ngx_int_t | ||
3990 | 3973 | ngx_http_proxy_set_ssl(ngx_conf_t *cf, ngx_http_proxy_loc_conf_t *plcf) | ||
3991 | 3974 | { | ||
3992 | 3975 | ngx_pool_cleanup_t *cln; | ||
3993 | 3976 | |||
3994 | 3977 | plcf->upstream.ssl = ngx_pcalloc(cf->pool, sizeof(ngx_ssl_t)); | ||
3995 | 3978 | if (plcf->upstream.ssl == NULL) { | ||
3996 | 3979 | return NGX_ERROR; | ||
3997 | 3980 | } | ||
3998 | 3981 | |||
3999 | 3982 | plcf->upstream.ssl->log = cf->log; | ||
4000 | 3983 | |||
4001 | 3984 | if (ngx_ssl_create(plcf->upstream.ssl, | ||
4002 | 3985 | NGX_SSL_SSLv2|NGX_SSL_SSLv3|NGX_SSL_TLSv1 | ||
4003 | 3986 | |NGX_SSL_TLSv1_1|NGX_SSL_TLSv1_2, | ||
4004 | 3987 | NULL) | ||
4005 | 3988 | != NGX_OK) | ||
4006 | 3989 | { | ||
4007 | 3990 | return NGX_ERROR; | ||
4008 | 3991 | } | ||
4009 | 3992 | |||
4010 | 3993 | cln = ngx_pool_cleanup_add(cf->pool, 0); | ||
4011 | 3994 | if (cln == NULL) { | ||
4012 | 3995 | return NGX_ERROR; | ||
4013 | 3996 | } | ||
4014 | 3997 | |||
4015 | 3998 | cln->handler = ngx_ssl_cleanup_ctx; | ||
4016 | 3999 | cln->data = plcf->upstream.ssl; | ||
4017 | 4000 | |||
4018 | 4001 | return NGX_OK; | ||
4019 | 4002 | } | ||
4020 | 4003 | |||
4021 | 4004 | #endif | ||
4022 | 4005 | |||
4023 | 4006 | |||
4024 | 4007 | static void | ||
4025 | 4008 | ngx_http_proxy_set_vars(ngx_url_t *u, ngx_http_proxy_vars_t *v) | ||
4026 | 4009 | { | ||
4027 | 4010 | if (u->family != AF_UNIX) { | ||
4028 | 4011 | |||
4029 | 4012 | if (u->no_port || u->port == u->default_port) { | ||
4030 | 4013 | |||
4031 | 4014 | v->host_header = u->host; | ||
4032 | 4015 | |||
4033 | 4016 | if (u->default_port == 80) { | ||
4034 | 4017 | ngx_str_set(&v->port, "80"); | ||
4035 | 4018 | |||
4036 | 4019 | } else { | ||
4037 | 4020 | ngx_str_set(&v->port, "443"); | ||
4038 | 4021 | } | ||
4039 | 4022 | |||
4040 | 4023 | } else { | ||
4041 | 4024 | v->host_header.len = u->host.len + 1 + u->port_text.len; | ||
4042 | 4025 | v->host_header.data = u->host.data; | ||
4043 | 4026 | v->port = u->port_text; | ||
4044 | 4027 | } | ||
4045 | 4028 | |||
4046 | 4029 | v->key_start.len += v->host_header.len; | ||
4047 | 4030 | |||
4048 | 4031 | } else { | ||
4049 | 4032 | ngx_str_set(&v->host_header, "localhost"); | ||
4050 | 4033 | ngx_str_null(&v->port); | ||
4051 | 4034 | v->key_start.len += sizeof("unix:") - 1 + u->host.len + 1; | ||
4052 | 4035 | } | ||
4053 | 4036 | |||
4054 | 4037 | v->uri = u->uri; | ||
4055 | 4038 | } | ||
4056 | 0 | 4039 | ||
4057 | === added directory '.pc/cve-2013-4547.patch' | |||
4058 | === added directory '.pc/cve-2013-4547.patch/src' | |||
4059 | === added directory '.pc/cve-2013-4547.patch/src/http' | |||
4060 | === added file '.pc/cve-2013-4547.patch/src/http/ngx_http_parse.c' | |||
4061 | --- .pc/cve-2013-4547.patch/src/http/ngx_http_parse.c 1970-01-01 00:00:00 +0000 | |||
4062 | +++ .pc/cve-2013-4547.patch/src/http/ngx_http_parse.c 2014-01-15 15:02:58 +0000 | |||
4063 | @@ -0,0 +1,1820 @@ | |||
4064 | 1 | |||
4065 | 2 | /* | ||
4066 | 3 | * Copyright (C) Igor Sysoev | ||
4067 | 4 | * Copyright (C) Nginx, Inc. | ||
4068 | 5 | */ | ||
4069 | 6 | |||
4070 | 7 | |||
4071 | 8 | #include <ngx_config.h> | ||
4072 | 9 | #include <ngx_core.h> | ||
4073 | 10 | #include <ngx_http.h> | ||
4074 | 11 | |||
4075 | 12 | |||
4076 | 13 | static uint32_t usual[] = { | ||
4077 | 14 | 0xffffdbfe, /* 1111 1111 1111 1111 1101 1011 1111 1110 */ | ||
4078 | 15 | |||
4079 | 16 | /* ?>=< ;:98 7654 3210 /.-, +*)( '&%$ #"! */ | ||
4080 | 17 | 0x7fff37d6, /* 0111 1111 1111 1111 0011 0111 1101 0110 */ | ||
4081 | 18 | |||
4082 | 19 | /* _^]\ [ZYX WVUT SRQP ONML KJIH GFED CBA@ */ | ||
4083 | 20 | #if (NGX_WIN32) | ||
4084 | 21 | 0xefffffff, /* 1110 1111 1111 1111 1111 1111 1111 1111 */ | ||
4085 | 22 | #else | ||
4086 | 23 | 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */ | ||
4087 | 24 | #endif | ||
4088 | 25 | |||
4089 | 26 | /* ~}| {zyx wvut srqp onml kjih gfed cba` */ | ||
4090 | 27 | 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */ | ||
4091 | 28 | |||
4092 | 29 | 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */ | ||
4093 | 30 | 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */ | ||
4094 | 31 | 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */ | ||
4095 | 32 | 0xffffffff /* 1111 1111 1111 1111 1111 1111 1111 1111 */ | ||
4096 | 33 | }; | ||
4097 | 34 | |||
4098 | 35 | |||
4099 | 36 | #if (NGX_HAVE_LITTLE_ENDIAN && NGX_HAVE_NONALIGNED) | ||
4100 | 37 | |||
4101 | 38 | #define ngx_str3_cmp(m, c0, c1, c2, c3) \ | ||
4102 | 39 | *(uint32_t *) m == ((c3 << 24) | (c2 << 16) | (c1 << 8) | c0) | ||
4103 | 40 | |||
4104 | 41 | #define ngx_str3Ocmp(m, c0, c1, c2, c3) \ | ||
4105 | 42 | *(uint32_t *) m == ((c3 << 24) | (c2 << 16) | (c1 << 8) | c0) | ||
4106 | 43 | |||
4107 | 44 | #define ngx_str4cmp(m, c0, c1, c2, c3) \ | ||
4108 | 45 | *(uint32_t *) m == ((c3 << 24) | (c2 << 16) | (c1 << 8) | c0) | ||
4109 | 46 | |||
4110 | 47 | #define ngx_str5cmp(m, c0, c1, c2, c3, c4) \ | ||
4111 | 48 | *(uint32_t *) m == ((c3 << 24) | (c2 << 16) | (c1 << 8) | c0) \ | ||
4112 | 49 | && m[4] == c4 | ||
4113 | 50 | |||
4114 | 51 | #define ngx_str6cmp(m, c0, c1, c2, c3, c4, c5) \ | ||
4115 | 52 | *(uint32_t *) m == ((c3 << 24) | (c2 << 16) | (c1 << 8) | c0) \ | ||
4116 | 53 | && (((uint32_t *) m)[1] & 0xffff) == ((c5 << 8) | c4) | ||
4117 | 54 | |||
4118 | 55 | #define ngx_str7_cmp(m, c0, c1, c2, c3, c4, c5, c6, c7) \ | ||
4119 | 56 | *(uint32_t *) m == ((c3 << 24) | (c2 << 16) | (c1 << 8) | c0) \ | ||
4120 | 57 | && ((uint32_t *) m)[1] == ((c7 << 24) | (c6 << 16) | (c5 << 8) | c4) | ||
4121 | 58 | |||
4122 | 59 | #define ngx_str8cmp(m, c0, c1, c2, c3, c4, c5, c6, c7) \ | ||
4123 | 60 | *(uint32_t *) m == ((c3 << 24) | (c2 << 16) | (c1 << 8) | c0) \ | ||
4124 | 61 | && ((uint32_t *) m)[1] == ((c7 << 24) | (c6 << 16) | (c5 << 8) | c4) | ||
4125 | 62 | |||
4126 | 63 | #define ngx_str9cmp(m, c0, c1, c2, c3, c4, c5, c6, c7, c8) \ | ||
4127 | 64 | *(uint32_t *) m == ((c3 << 24) | (c2 << 16) | (c1 << 8) | c0) \ | ||
4128 | 65 | && ((uint32_t *) m)[1] == ((c7 << 24) | (c6 << 16) | (c5 << 8) | c4) \ | ||
4129 | 66 | && m[8] == c8 | ||
4130 | 67 | |||
4131 | 68 | #else /* !(NGX_HAVE_LITTLE_ENDIAN && NGX_HAVE_NONALIGNED) */ | ||
4132 | 69 | |||
4133 | 70 | #define ngx_str3_cmp(m, c0, c1, c2, c3) \ | ||
4134 | 71 | m[0] == c0 && m[1] == c1 && m[2] == c2 | ||
4135 | 72 | |||
4136 | 73 | #define ngx_str3Ocmp(m, c0, c1, c2, c3) \ | ||
4137 | 74 | m[0] == c0 && m[2] == c2 && m[3] == c3 | ||
4138 | 75 | |||
4139 | 76 | #define ngx_str4cmp(m, c0, c1, c2, c3) \ | ||
4140 | 77 | m[0] == c0 && m[1] == c1 && m[2] == c2 && m[3] == c3 | ||
4141 | 78 | |||
4142 | 79 | #define ngx_str5cmp(m, c0, c1, c2, c3, c4) \ | ||
4143 | 80 | m[0] == c0 && m[1] == c1 && m[2] == c2 && m[3] == c3 && m[4] == c4 | ||
4144 | 81 | |||
4145 | 82 | #define ngx_str6cmp(m, c0, c1, c2, c3, c4, c5) \ | ||
4146 | 83 | m[0] == c0 && m[1] == c1 && m[2] == c2 && m[3] == c3 \ | ||
4147 | 84 | && m[4] == c4 && m[5] == c5 | ||
4148 | 85 | |||
4149 | 86 | #define ngx_str7_cmp(m, c0, c1, c2, c3, c4, c5, c6, c7) \ | ||
4150 | 87 | m[0] == c0 && m[1] == c1 && m[2] == c2 && m[3] == c3 \ | ||
4151 | 88 | && m[4] == c4 && m[5] == c5 && m[6] == c6 | ||
4152 | 89 | |||
4153 | 90 | #define ngx_str8cmp(m, c0, c1, c2, c3, c4, c5, c6, c7) \ | ||
4154 | 91 | m[0] == c0 && m[1] == c1 && m[2] == c2 && m[3] == c3 \ | ||
4155 | 92 | && m[4] == c4 && m[5] == c5 && m[6] == c6 && m[7] == c7 | ||
4156 | 93 | |||
4157 | 94 | #define ngx_str9cmp(m, c0, c1, c2, c3, c4, c5, c6, c7, c8) \ | ||
4158 | 95 | m[0] == c0 && m[1] == c1 && m[2] == c2 && m[3] == c3 \ | ||
4159 | 96 | && m[4] == c4 && m[5] == c5 && m[6] == c6 && m[7] == c7 && m[8] == c8 | ||
4160 | 97 | |||
4161 | 98 | #endif | ||
4162 | 99 | |||
4163 | 100 | |||
4164 | 101 | /* gcc, icc, msvc and others compile these switches as an jump table */ | ||
4165 | 102 | |||
4166 | 103 | ngx_int_t | ||
4167 | 104 | ngx_http_parse_request_line(ngx_http_request_t *r, ngx_buf_t *b) | ||
4168 | 105 | { | ||
4169 | 106 | u_char c, ch, *p, *m; | ||
4170 | 107 | enum { | ||
4171 | 108 | sw_start = 0, | ||
4172 | 109 | sw_method, | ||
4173 | 110 | sw_spaces_before_uri, | ||
4174 | 111 | sw_schema, | ||
4175 | 112 | sw_schema_slash, | ||
4176 | 113 | sw_schema_slash_slash, | ||
4177 | 114 | sw_host_start, | ||
4178 | 115 | sw_host, | ||
4179 | 116 | sw_host_end, | ||
4180 | 117 | sw_host_ip_literal, | ||
4181 | 118 | sw_port, | ||
4182 | 119 | sw_host_http_09, | ||
4183 | 120 | sw_after_slash_in_uri, | ||
4184 | 121 | sw_check_uri, | ||
4185 | 122 | sw_check_uri_http_09, | ||
4186 | 123 | sw_uri, | ||
4187 | 124 | sw_http_09, | ||
4188 | 125 | sw_http_H, | ||
4189 | 126 | sw_http_HT, | ||
4190 | 127 | sw_http_HTT, | ||
4191 | 128 | sw_http_HTTP, | ||
4192 | 129 | sw_first_major_digit, | ||
4193 | 130 | sw_major_digit, | ||
4194 | 131 | sw_first_minor_digit, | ||
4195 | 132 | sw_minor_digit, | ||
4196 | 133 | sw_spaces_after_digit, | ||
4197 | 134 | sw_almost_done | ||
4198 | 135 | } state; | ||
4199 | 136 | |||
4200 | 137 | state = r->state; | ||
4201 | 138 | |||
4202 | 139 | for (p = b->pos; p < b->last; p++) { | ||
4203 | 140 | ch = *p; | ||
4204 | 141 | |||
4205 | 142 | switch (state) { | ||
4206 | 143 | |||
4207 | 144 | /* HTTP methods: GET, HEAD, POST */ | ||
4208 | 145 | case sw_start: | ||
4209 | 146 | r->request_start = p; | ||
4210 | 147 | |||
4211 | 148 | if (ch == CR || ch == LF) { | ||
4212 | 149 | break; | ||
4213 | 150 | } | ||
4214 | 151 | |||
4215 | 152 | if ((ch < 'A' || ch > 'Z') && ch != '_') { | ||
4216 | 153 | return NGX_HTTP_PARSE_INVALID_METHOD; | ||
4217 | 154 | } | ||
4218 | 155 | |||
4219 | 156 | state = sw_method; | ||
4220 | 157 | break; | ||
4221 | 158 | |||
4222 | 159 | case sw_method: | ||
4223 | 160 | if (ch == ' ') { | ||
4224 | 161 | r->method_end = p - 1; | ||
4225 | 162 | m = r->request_start; | ||
4226 | 163 | |||
4227 | 164 | switch (p - m) { | ||
4228 | 165 | |||
4229 | 166 | case 3: | ||
4230 | 167 | if (ngx_str3_cmp(m, 'G', 'E', 'T', ' ')) { | ||
4231 | 168 | r->method = NGX_HTTP_GET; | ||
4232 | 169 | break; | ||
4233 | 170 | } | ||
4234 | 171 | |||
4235 | 172 | if (ngx_str3_cmp(m, 'P', 'U', 'T', ' ')) { | ||
4236 | 173 | r->method = NGX_HTTP_PUT; | ||
4237 | 174 | break; | ||
4238 | 175 | } | ||
4239 | 176 | |||
4240 | 177 | break; | ||
4241 | 178 | |||
4242 | 179 | case 4: | ||
4243 | 180 | if (m[1] == 'O') { | ||
4244 | 181 | |||
4245 | 182 | if (ngx_str3Ocmp(m, 'P', 'O', 'S', 'T')) { | ||
4246 | 183 | r->method = NGX_HTTP_POST; | ||
4247 | 184 | break; | ||
4248 | 185 | } | ||
4249 | 186 | |||
4250 | 187 | if (ngx_str3Ocmp(m, 'C', 'O', 'P', 'Y')) { | ||
4251 | 188 | r->method = NGX_HTTP_COPY; | ||
4252 | 189 | break; | ||
4253 | 190 | } | ||
4254 | 191 | |||
4255 | 192 | if (ngx_str3Ocmp(m, 'M', 'O', 'V', 'E')) { | ||
4256 | 193 | r->method = NGX_HTTP_MOVE; | ||
4257 | 194 | break; | ||
4258 | 195 | } | ||
4259 | 196 | |||
4260 | 197 | if (ngx_str3Ocmp(m, 'L', 'O', 'C', 'K')) { | ||
4261 | 198 | r->method = NGX_HTTP_LOCK; | ||
4262 | 199 | break; | ||
4263 | 200 | } | ||
4264 | 201 | |||
4265 | 202 | } else { | ||
4266 | 203 | |||
4267 | 204 | if (ngx_str4cmp(m, 'H', 'E', 'A', 'D')) { | ||
4268 | 205 | r->method = NGX_HTTP_HEAD; | ||
4269 | 206 | break; | ||
4270 | 207 | } | ||
4271 | 208 | } | ||
4272 | 209 | |||
4273 | 210 | break; | ||
4274 | 211 | |||
4275 | 212 | case 5: | ||
4276 | 213 | if (ngx_str5cmp(m, 'M', 'K', 'C', 'O', 'L')) { | ||
4277 | 214 | r->method = NGX_HTTP_MKCOL; | ||
4278 | 215 | } | ||
4279 | 216 | |||
4280 | 217 | if (ngx_str5cmp(m, 'P', 'A', 'T', 'C', 'H')) { | ||
4281 | 218 | r->method = NGX_HTTP_PATCH; | ||
4282 | 219 | } | ||
4283 | 220 | |||
4284 | 221 | if (ngx_str5cmp(m, 'T', 'R', 'A', 'C', 'E')) { | ||
4285 | 222 | r->method = NGX_HTTP_TRACE; | ||
4286 | 223 | } | ||
4287 | 224 | |||
4288 | 225 | break; | ||
4289 | 226 | |||
4290 | 227 | case 6: | ||
4291 | 228 | if (ngx_str6cmp(m, 'D', 'E', 'L', 'E', 'T', 'E')) { | ||
4292 | 229 | r->method = NGX_HTTP_DELETE; | ||
4293 | 230 | break; | ||
4294 | 231 | } | ||
4295 | 232 | |||
4296 | 233 | if (ngx_str6cmp(m, 'U', 'N', 'L', 'O', 'C', 'K')) { | ||
4297 | 234 | r->method = NGX_HTTP_UNLOCK; | ||
4298 | 235 | break; | ||
4299 | 236 | } | ||
4300 | 237 | |||
4301 | 238 | break; | ||
4302 | 239 | |||
4303 | 240 | case 7: | ||
4304 | 241 | if (ngx_str7_cmp(m, 'O', 'P', 'T', 'I', 'O', 'N', 'S', ' ')) | ||
4305 | 242 | { | ||
4306 | 243 | r->method = NGX_HTTP_OPTIONS; | ||
4307 | 244 | } | ||
4308 | 245 | |||
4309 | 246 | break; | ||
4310 | 247 | |||
4311 | 248 | case 8: | ||
4312 | 249 | if (ngx_str8cmp(m, 'P', 'R', 'O', 'P', 'F', 'I', 'N', 'D')) | ||
4313 | 250 | { | ||
4314 | 251 | r->method = NGX_HTTP_PROPFIND; | ||
4315 | 252 | } | ||
4316 | 253 | |||
4317 | 254 | break; | ||
4318 | 255 | |||
4319 | 256 | case 9: | ||
4320 | 257 | if (ngx_str9cmp(m, | ||
4321 | 258 | 'P', 'R', 'O', 'P', 'P', 'A', 'T', 'C', 'H')) | ||
4322 | 259 | { | ||
4323 | 260 | r->method = NGX_HTTP_PROPPATCH; | ||
4324 | 261 | } | ||
4325 | 262 | |||
4326 | 263 | break; | ||
4327 | 264 | } | ||
4328 | 265 | |||
4329 | 266 | state = sw_spaces_before_uri; | ||
4330 | 267 | break; | ||
4331 | 268 | } | ||
4332 | 269 | |||
4333 | 270 | if ((ch < 'A' || ch > 'Z') && ch != '_') { | ||
4334 | 271 | return NGX_HTTP_PARSE_INVALID_METHOD; | ||
4335 | 272 | } | ||
4336 | 273 | |||
4337 | 274 | break; | ||
4338 | 275 | |||
4339 | 276 | /* space* before URI */ | ||
4340 | 277 | case sw_spaces_before_uri: | ||
4341 | 278 | |||
4342 | 279 | if (ch == '/') { | ||
4343 | 280 | r->uri_start = p; | ||
4344 | 281 | state = sw_after_slash_in_uri; | ||
4345 | 282 | break; | ||
4346 | 283 | } | ||
4347 | 284 | |||
4348 | 285 | c = (u_char) (ch | 0x20); | ||
4349 | 286 | if (c >= 'a' && c <= 'z') { | ||
4350 | 287 | r->schema_start = p; | ||
4351 | 288 | state = sw_schema; | ||
4352 | 289 | break; | ||
4353 | 290 | } | ||
4354 | 291 | |||
4355 | 292 | switch (ch) { | ||
4356 | 293 | case ' ': | ||
4357 | 294 | break; | ||
4358 | 295 | default: | ||
4359 | 296 | return NGX_HTTP_PARSE_INVALID_REQUEST; | ||
4360 | 297 | } | ||
4361 | 298 | break; | ||
4362 | 299 | |||
4363 | 300 | case sw_schema: | ||
4364 | 301 | |||
4365 | 302 | c = (u_char) (ch | 0x20); | ||
4366 | 303 | if (c >= 'a' && c <= 'z') { | ||
4367 | 304 | break; | ||
4368 | 305 | } | ||
4369 | 306 | |||
4370 | 307 | switch (ch) { | ||
4371 | 308 | case ':': | ||
4372 | 309 | r->schema_end = p; | ||
4373 | 310 | state = sw_schema_slash; | ||
4374 | 311 | break; | ||
4375 | 312 | default: | ||
4376 | 313 | return NGX_HTTP_PARSE_INVALID_REQUEST; | ||
4377 | 314 | } | ||
4378 | 315 | break; | ||
4379 | 316 | |||
4380 | 317 | case sw_schema_slash: | ||
4381 | 318 | switch (ch) { | ||
4382 | 319 | case '/': | ||
4383 | 320 | state = sw_schema_slash_slash; | ||
4384 | 321 | break; | ||
4385 | 322 | default: | ||
4386 | 323 | return NGX_HTTP_PARSE_INVALID_REQUEST; | ||
4387 | 324 | } | ||
4388 | 325 | break; | ||
4389 | 326 | |||
4390 | 327 | case sw_schema_slash_slash: | ||
4391 | 328 | switch (ch) { | ||
4392 | 329 | case '/': | ||
4393 | 330 | state = sw_host_start; | ||
4394 | 331 | break; | ||
4395 | 332 | default: | ||
4396 | 333 | return NGX_HTTP_PARSE_INVALID_REQUEST; | ||
4397 | 334 | } | ||
4398 | 335 | break; | ||
4399 | 336 | |||
4400 | 337 | case sw_host_start: | ||
4401 | 338 | |||
4402 | 339 | r->host_start = p; | ||
4403 | 340 | |||
4404 | 341 | if (ch == '[') { | ||
4405 | 342 | state = sw_host_ip_literal; | ||
4406 | 343 | break; | ||
4407 | 344 | } | ||
4408 | 345 | |||
4409 | 346 | state = sw_host; | ||
4410 | 347 | |||
4411 | 348 | /* fall through */ | ||
4412 | 349 | |||
4413 | 350 | case sw_host: | ||
4414 | 351 | |||
4415 | 352 | c = (u_char) (ch | 0x20); | ||
4416 | 353 | if (c >= 'a' && c <= 'z') { | ||
4417 | 354 | break; | ||
4418 | 355 | } | ||
4419 | 356 | |||
4420 | 357 | if ((ch >= '0' && ch <= '9') || ch == '.' || ch == '-') { | ||
4421 | 358 | break; | ||
4422 | 359 | } | ||
4423 | 360 | |||
4424 | 361 | /* fall through */ | ||
4425 | 362 | |||
4426 | 363 | case sw_host_end: | ||
4427 | 364 | |||
4428 | 365 | r->host_end = p; | ||
4429 | 366 | |||
4430 | 367 | switch (ch) { | ||
4431 | 368 | case ':': | ||
4432 | 369 | state = sw_port; | ||
4433 | 370 | break; | ||
4434 | 371 | case '/': | ||
4435 | 372 | r->uri_start = p; | ||
4436 | 373 | state = sw_after_slash_in_uri; | ||
4437 | 374 | break; | ||
4438 | 375 | case ' ': | ||
4439 | 376 | /* | ||
4440 | 377 | * use single "/" from request line to preserve pointers, | ||
4441 | 378 | * if request line will be copied to large client buffer | ||
4442 | 379 | */ | ||
4443 | 380 | r->uri_start = r->schema_end + 1; | ||
4444 | 381 | r->uri_end = r->schema_end + 2; | ||
4445 | 382 | state = sw_host_http_09; | ||
4446 | 383 | break; | ||
4447 | 384 | default: | ||
4448 | 385 | return NGX_HTTP_PARSE_INVALID_REQUEST; | ||
4449 | 386 | } | ||
4450 | 387 | break; | ||
4451 | 388 | |||
4452 | 389 | case sw_host_ip_literal: | ||
4453 | 390 | |||
4454 | 391 | if (ch >= '0' && ch <= '9') { | ||
4455 | 392 | break; | ||
4456 | 393 | } | ||
4457 | 394 | |||
4458 | 395 | c = (u_char) (ch | 0x20); | ||
4459 | 396 | if (c >= 'a' && c <= 'z') { | ||
4460 | 397 | break; | ||
4461 | 398 | } | ||
4462 | 399 | |||
4463 | 400 | switch (ch) { | ||
4464 | 401 | case ':': | ||
4465 | 402 | break; | ||
4466 | 403 | case ']': | ||
4467 | 404 | state = sw_host_end; | ||
4468 | 405 | break; | ||
4469 | 406 | case '-': | ||
4470 | 407 | case '.': | ||
4471 | 408 | case '_': | ||
4472 | 409 | case '~': | ||
4473 | 410 | /* unreserved */ | ||
4474 | 411 | break; | ||
4475 | 412 | case '!': | ||
4476 | 413 | case '$': | ||
4477 | 414 | case '&': | ||
4478 | 415 | case '\'': | ||
4479 | 416 | case '(': | ||
4480 | 417 | case ')': | ||
4481 | 418 | case '*': | ||
4482 | 419 | case '+': | ||
4483 | 420 | case ',': | ||
4484 | 421 | case ';': | ||
4485 | 422 | case '=': | ||
4486 | 423 | /* sub-delims */ | ||
4487 | 424 | break; | ||
4488 | 425 | default: | ||
4489 | 426 | return NGX_HTTP_PARSE_INVALID_REQUEST; | ||
4490 | 427 | } | ||
4491 | 428 | break; | ||
4492 | 429 | |||
4493 | 430 | case sw_port: | ||
4494 | 431 | if (ch >= '0' && ch <= '9') { | ||
4495 | 432 | break; | ||
4496 | 433 | } | ||
4497 | 434 | |||
4498 | 435 | switch (ch) { | ||
4499 | 436 | case '/': | ||
4500 | 437 | r->port_end = p; | ||
4501 | 438 | r->uri_start = p; | ||
4502 | 439 | state = sw_after_slash_in_uri; | ||
4503 | 440 | break; | ||
4504 | 441 | case ' ': | ||
4505 | 442 | r->port_end = p; | ||
4506 | 443 | /* | ||
4507 | 444 | * use single "/" from request line to preserve pointers, | ||
4508 | 445 | * if request line will be copied to large client buffer | ||
4509 | 446 | */ | ||
4510 | 447 | r->uri_start = r->schema_end + 1; | ||
4511 | 448 | r->uri_end = r->schema_end + 2; | ||
4512 | 449 | state = sw_host_http_09; | ||
4513 | 450 | break; | ||
4514 | 451 | default: | ||
4515 | 452 | return NGX_HTTP_PARSE_INVALID_REQUEST; | ||
4516 | 453 | } | ||
4517 | 454 | break; | ||
4518 | 455 | |||
4519 | 456 | /* space+ after "http://host[:port] " */ | ||
4520 | 457 | case sw_host_http_09: | ||
4521 | 458 | switch (ch) { | ||
4522 | 459 | case ' ': | ||
4523 | 460 | break; | ||
4524 | 461 | case CR: | ||
4525 | 462 | r->http_minor = 9; | ||
4526 | 463 | state = sw_almost_done; | ||
4527 | 464 | break; | ||
4528 | 465 | case LF: | ||
4529 | 466 | r->http_minor = 9; | ||
4530 | 467 | goto done; | ||
4531 | 468 | case 'H': | ||
4532 | 469 | r->http_protocol.data = p; | ||
4533 | 470 | state = sw_http_H; | ||
4534 | 471 | break; | ||
4535 | 472 | default: | ||
4536 | 473 | return NGX_HTTP_PARSE_INVALID_REQUEST; | ||
4537 | 474 | } | ||
4538 | 475 | break; | ||
4539 | 476 | |||
4540 | 477 | |||
4541 | 478 | /* check "/.", "//", "%", and "\" (Win32) in URI */ | ||
4542 | 479 | case sw_after_slash_in_uri: | ||
4543 | 480 | |||
4544 | 481 | if (usual[ch >> 5] & (1 << (ch & 0x1f))) { | ||
4545 | 482 | state = sw_check_uri; | ||
4546 | 483 | break; | ||
4547 | 484 | } | ||
4548 | 485 | |||
4549 | 486 | switch (ch) { | ||
4550 | 487 | case ' ': | ||
4551 | 488 | r->uri_end = p; | ||
4552 | 489 | state = sw_check_uri_http_09; | ||
4553 | 490 | break; | ||
4554 | 491 | case CR: | ||
4555 | 492 | r->uri_end = p; | ||
4556 | 493 | r->http_minor = 9; | ||
4557 | 494 | state = sw_almost_done; | ||
4558 | 495 | break; | ||
4559 | 496 | case LF: | ||
4560 | 497 | r->uri_end = p; | ||
4561 | 498 | r->http_minor = 9; | ||
4562 | 499 | goto done; | ||
4563 | 500 | case '.': | ||
4564 | 501 | r->complex_uri = 1; | ||
4565 | 502 | state = sw_uri; | ||
4566 | 503 | break; | ||
4567 | 504 | case '%': | ||
4568 | 505 | r->quoted_uri = 1; | ||
4569 | 506 | state = sw_uri; | ||
4570 | 507 | break; | ||
4571 | 508 | case '/': | ||
4572 | 509 | r->complex_uri = 1; | ||
4573 | 510 | state = sw_uri; | ||
4574 | 511 | break; | ||
4575 | 512 | #if (NGX_WIN32) | ||
4576 | 513 | case '\\': | ||
4577 | 514 | r->complex_uri = 1; | ||
4578 | 515 | state = sw_uri; | ||
4579 | 516 | break; | ||
4580 | 517 | #endif | ||
4581 | 518 | case '?': | ||
4582 | 519 | r->args_start = p + 1; | ||
4583 | 520 | state = sw_uri; | ||
4584 | 521 | break; | ||
4585 | 522 | case '#': | ||
4586 | 523 | r->complex_uri = 1; | ||
4587 | 524 | state = sw_uri; | ||
4588 | 525 | break; | ||
4589 | 526 | case '+': | ||
4590 | 527 | r->plus_in_uri = 1; | ||
4591 | 528 | break; | ||
4592 | 529 | case '\0': | ||
4593 | 530 | return NGX_HTTP_PARSE_INVALID_REQUEST; | ||
4594 | 531 | default: | ||
4595 | 532 | state = sw_check_uri; | ||
4596 | 533 | break; | ||
4597 | 534 | } | ||
4598 | 535 | break; | ||
4599 | 536 | |||
4600 | 537 | /* check "/", "%" and "\" (Win32) in URI */ | ||
4601 | 538 | case sw_check_uri: | ||
4602 | 539 | |||
4603 | 540 | if (usual[ch >> 5] & (1 << (ch & 0x1f))) { | ||
4604 | 541 | break; | ||
4605 | 542 | } | ||
4606 | 543 | |||
4607 | 544 | switch (ch) { | ||
4608 | 545 | case '/': | ||
4609 | 546 | #if (NGX_WIN32) | ||
4610 | 547 | if (r->uri_ext == p) { | ||
4611 | 548 | r->complex_uri = 1; | ||
4612 | 549 | state = sw_uri; | ||
4613 | 550 | break; | ||
4614 | 551 | } | ||
4615 | 552 | #endif | ||
4616 | 553 | r->uri_ext = NULL; | ||
4617 | 554 | state = sw_after_slash_in_uri; | ||
4618 | 555 | break; | ||
4619 | 556 | case '.': | ||
4620 | 557 | r->uri_ext = p + 1; | ||
4621 | 558 | break; | ||
4622 | 559 | case ' ': | ||
4623 | 560 | r->uri_end = p; | ||
4624 | 561 | state = sw_check_uri_http_09; | ||
4625 | 562 | break; | ||
4626 | 563 | case CR: | ||
4627 | 564 | r->uri_end = p; | ||
4628 | 565 | r->http_minor = 9; | ||
4629 | 566 | state = sw_almost_done; | ||
4630 | 567 | break; | ||
4631 | 568 | case LF: | ||
4632 | 569 | r->uri_end = p; | ||
4633 | 570 | r->http_minor = 9; | ||
4634 | 571 | goto done; | ||
4635 | 572 | #if (NGX_WIN32) | ||
4636 | 573 | case '\\': | ||
4637 | 574 | r->complex_uri = 1; | ||
4638 | 575 | state = sw_after_slash_in_uri; | ||
4639 | 576 | break; | ||
4640 | 577 | #endif | ||
4641 | 578 | case '%': | ||
4642 | 579 | r->quoted_uri = 1; | ||
4643 | 580 | state = sw_uri; | ||
4644 | 581 | break; | ||
4645 | 582 | case '?': | ||
4646 | 583 | r->args_start = p + 1; | ||
4647 | 584 | state = sw_uri; | ||
4648 | 585 | break; | ||
4649 | 586 | case '#': | ||
4650 | 587 | r->complex_uri = 1; | ||
4651 | 588 | state = sw_uri; | ||
4652 | 589 | break; | ||
4653 | 590 | case '+': | ||
4654 | 591 | r->plus_in_uri = 1; | ||
4655 | 592 | break; | ||
4656 | 593 | case '\0': | ||
4657 | 594 | return NGX_HTTP_PARSE_INVALID_REQUEST; | ||
4658 | 595 | } | ||
4659 | 596 | break; | ||
4660 | 597 | |||
4661 | 598 | /* space+ after URI */ | ||
4662 | 599 | case sw_check_uri_http_09: | ||
4663 | 600 | switch (ch) { | ||
4664 | 601 | case ' ': | ||
4665 | 602 | break; | ||
4666 | 603 | case CR: | ||
4667 | 604 | r->http_minor = 9; | ||
4668 | 605 | state = sw_almost_done; | ||
4669 | 606 | break; | ||
4670 | 607 | case LF: | ||
4671 | 608 | r->http_minor = 9; | ||
4672 | 609 | goto done; | ||
4673 | 610 | case 'H': | ||
4674 | 611 | r->http_protocol.data = p; | ||
4675 | 612 | state = sw_http_H; | ||
4676 | 613 | break; | ||
4677 | 614 | default: | ||
4678 | 615 | r->space_in_uri = 1; | ||
4679 | 616 | state = sw_check_uri; | ||
4680 | 617 | break; | ||
4681 | 618 | } | ||
4682 | 619 | break; | ||
4683 | 620 | |||
4684 | 621 | |||
4685 | 622 | /* URI */ | ||
4686 | 623 | case sw_uri: | ||
4687 | 624 | |||
4688 | 625 | if (usual[ch >> 5] & (1 << (ch & 0x1f))) { | ||
4689 | 626 | break; | ||
4690 | 627 | } | ||
4691 | 628 | |||
4692 | 629 | switch (ch) { | ||
4693 | 630 | case ' ': | ||
4694 | 631 | r->uri_end = p; | ||
4695 | 632 | state = sw_http_09; | ||
4696 | 633 | break; | ||
4697 | 634 | case CR: | ||
4698 | 635 | r->uri_end = p; | ||
4699 | 636 | r->http_minor = 9; | ||
4700 | 637 | state = sw_almost_done; | ||
4701 | 638 | break; | ||
4702 | 639 | case LF: | ||
4703 | 640 | r->uri_end = p; | ||
4704 | 641 | r->http_minor = 9; | ||
4705 | 642 | goto done; | ||
4706 | 643 | case '#': | ||
4707 | 644 | r->complex_uri = 1; | ||
4708 | 645 | break; | ||
4709 | 646 | case '\0': | ||
4710 | 647 | return NGX_HTTP_PARSE_INVALID_REQUEST; | ||
4711 | 648 | } | ||
4712 | 649 | break; | ||
4713 | 650 | |||
4714 | 651 | /* space+ after URI */ | ||
4715 | 652 | case sw_http_09: | ||
4716 | 653 | switch (ch) { | ||
4717 | 654 | case ' ': | ||
4718 | 655 | break; | ||
4719 | 656 | case CR: | ||
4720 | 657 | r->http_minor = 9; | ||
4721 | 658 | state = sw_almost_done; | ||
4722 | 659 | break; | ||
4723 | 660 | case LF: | ||
4724 | 661 | r->http_minor = 9; | ||
4725 | 662 | goto done; | ||
4726 | 663 | case 'H': | ||
4727 | 664 | r->http_protocol.data = p; | ||
4728 | 665 | state = sw_http_H; | ||
4729 | 666 | break; | ||
4730 | 667 | default: | ||
4731 | 668 | r->space_in_uri = 1; | ||
4732 | 669 | state = sw_uri; | ||
4733 | 670 | break; | ||
4734 | 671 | } | ||
4735 | 672 | break; | ||
4736 | 673 | |||
4737 | 674 | case sw_http_H: | ||
4738 | 675 | switch (ch) { | ||
4739 | 676 | case 'T': | ||
4740 | 677 | state = sw_http_HT; | ||
4741 | 678 | break; | ||
4742 | 679 | default: | ||
4743 | 680 | return NGX_HTTP_PARSE_INVALID_REQUEST; | ||
4744 | 681 | } | ||
4745 | 682 | break; | ||
4746 | 683 | |||
4747 | 684 | case sw_http_HT: | ||
4748 | 685 | switch (ch) { | ||
4749 | 686 | case 'T': | ||
4750 | 687 | state = sw_http_HTT; | ||
4751 | 688 | break; | ||
4752 | 689 | default: | ||
4753 | 690 | return NGX_HTTP_PARSE_INVALID_REQUEST; | ||
4754 | 691 | } | ||
4755 | 692 | break; | ||
4756 | 693 | |||
4757 | 694 | case sw_http_HTT: | ||
4758 | 695 | switch (ch) { | ||
4759 | 696 | case 'P': | ||
4760 | 697 | state = sw_http_HTTP; | ||
4761 | 698 | break; | ||
4762 | 699 | default: | ||
4763 | 700 | return NGX_HTTP_PARSE_INVALID_REQUEST; | ||
4764 | 701 | } | ||
4765 | 702 | break; | ||
4766 | 703 | |||
4767 | 704 | case sw_http_HTTP: | ||
4768 | 705 | switch (ch) { | ||
4769 | 706 | case '/': | ||
4770 | 707 | state = sw_first_major_digit; | ||
4771 | 708 | break; | ||
4772 | 709 | default: | ||
4773 | 710 | return NGX_HTTP_PARSE_INVALID_REQUEST; | ||
4774 | 711 | } | ||
4775 | 712 | break; | ||
4776 | 713 | |||
4777 | 714 | /* first digit of major HTTP version */ | ||
4778 | 715 | case sw_first_major_digit: | ||
4779 | 716 | if (ch < '1' || ch > '9') { | ||
4780 | 717 | return NGX_HTTP_PARSE_INVALID_REQUEST; | ||
4781 | 718 | } | ||
4782 | 719 | |||
4783 | 720 | r->http_major = ch - '0'; | ||
4784 | 721 | state = sw_major_digit; | ||
4785 | 722 | break; | ||
4786 | 723 | |||
4787 | 724 | /* major HTTP version or dot */ | ||
4788 | 725 | case sw_major_digit: | ||
4789 | 726 | if (ch == '.') { | ||
4790 | 727 | state = sw_first_minor_digit; | ||
4791 | 728 | break; | ||
4792 | 729 | } | ||
4793 | 730 | |||
4794 | 731 | if (ch < '0' || ch > '9') { | ||
4795 | 732 | return NGX_HTTP_PARSE_INVALID_REQUEST; | ||
4796 | 733 | } | ||
4797 | 734 | |||
4798 | 735 | r->http_major = r->http_major * 10 + ch - '0'; | ||
4799 | 736 | break; | ||
4800 | 737 | |||
4801 | 738 | /* first digit of minor HTTP version */ | ||
4802 | 739 | case sw_first_minor_digit: | ||
4803 | 740 | if (ch < '0' || ch > '9') { | ||
4804 | 741 | return NGX_HTTP_PARSE_INVALID_REQUEST; | ||
4805 | 742 | } | ||
4806 | 743 | |||
4807 | 744 | r->http_minor = ch - '0'; | ||
4808 | 745 | state = sw_minor_digit; | ||
4809 | 746 | break; | ||
4810 | 747 | |||
4811 | 748 | /* minor HTTP version or end of request line */ | ||
4812 | 749 | case sw_minor_digit: | ||
4813 | 750 | if (ch == CR) { | ||
4814 | 751 | state = sw_almost_done; | ||
4815 | 752 | break; | ||
4816 | 753 | } | ||
4817 | 754 | |||
4818 | 755 | if (ch == LF) { | ||
4819 | 756 | goto done; | ||
4820 | 757 | } | ||
4821 | 758 | |||
4822 | 759 | if (ch == ' ') { | ||
4823 | 760 | state = sw_spaces_after_digit; | ||
4824 | 761 | break; | ||
4825 | 762 | } | ||
4826 | 763 | |||
4827 | 764 | if (ch < '0' || ch > '9') { | ||
4828 | 765 | return NGX_HTTP_PARSE_INVALID_REQUEST; | ||
4829 | 766 | } | ||
4830 | 767 | |||
4831 | 768 | r->http_minor = r->http_minor * 10 + ch - '0'; | ||
4832 | 769 | break; | ||
4833 | 770 | |||
4834 | 771 | case sw_spaces_after_digit: | ||
4835 | 772 | switch (ch) { | ||
4836 | 773 | case ' ': | ||
4837 | 774 | break; | ||
4838 | 775 | case CR: | ||
4839 | 776 | state = sw_almost_done; | ||
4840 | 777 | break; | ||
4841 | 778 | case LF: | ||
4842 | 779 | goto done; | ||
4843 | 780 | default: | ||
4844 | 781 | return NGX_HTTP_PARSE_INVALID_REQUEST; | ||
4845 | 782 | } | ||
4846 | 783 | break; | ||
4847 | 784 | |||
4848 | 785 | /* end of request line */ | ||
4849 | 786 | case sw_almost_done: | ||
4850 | 787 | r->request_end = p - 1; | ||
4851 | 788 | switch (ch) { | ||
4852 | 789 | case LF: | ||
4853 | 790 | goto done; | ||
4854 | 791 | default: | ||
4855 | 792 | return NGX_HTTP_PARSE_INVALID_REQUEST; | ||
4856 | 793 | } | ||
4857 | 794 | } | ||
4858 | 795 | } | ||
4859 | 796 | |||
4860 | 797 | b->pos = p; | ||
4861 | 798 | r->state = state; | ||
4862 | 799 | |||
4863 | 800 | return NGX_AGAIN; | ||
4864 | 801 | |||
4865 | 802 | done: | ||
4866 | 803 | |||
4867 | 804 | b->pos = p + 1; | ||
4868 | 805 | |||
4869 | 806 | if (r->request_end == NULL) { | ||
4870 | 807 | r->request_end = p; | ||
4871 | 808 | } | ||
4872 | 809 | |||
4873 | 810 | r->http_version = r->http_major * 1000 + r->http_minor; | ||
4874 | 811 | r->state = sw_start; | ||
4875 | 812 | |||
4876 | 813 | if (r->http_version == 9 && r->method != NGX_HTTP_GET) { | ||
4877 | 814 | return NGX_HTTP_PARSE_INVALID_09_METHOD; | ||
4878 | 815 | } | ||
4879 | 816 | |||
4880 | 817 | return NGX_OK; | ||
4881 | 818 | } | ||
4882 | 819 | |||
4883 | 820 | |||
4884 | 821 | ngx_int_t | ||
4885 | 822 | ngx_http_parse_header_line(ngx_http_request_t *r, ngx_buf_t *b, | ||
4886 | 823 | ngx_uint_t allow_underscores) | ||
4887 | 824 | { | ||
4888 | 825 | u_char c, ch, *p; | ||
4889 | 826 | ngx_uint_t hash, i; | ||
4890 | 827 | enum { | ||
4891 | 828 | sw_start = 0, | ||
4892 | 829 | sw_name, | ||
4893 | 830 | sw_space_before_value, | ||
4894 | 831 | sw_value, | ||
4895 | 832 | sw_space_after_value, | ||
4896 | 833 | sw_ignore_line, | ||
4897 | 834 | sw_almost_done, | ||
4898 | 835 | sw_header_almost_done | ||
4899 | 836 | } state; | ||
4900 | 837 | |||
4901 | 838 | /* the last '\0' is not needed because string is zero terminated */ | ||
4902 | 839 | |||
4903 | 840 | static u_char lowcase[] = | ||
4904 | 841 | "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0" | ||
4905 | 842 | "\0\0\0\0\0\0\0\0\0\0\0\0\0-\0\0" "0123456789\0\0\0\0\0\0" | ||
4906 | 843 | "\0abcdefghijklmnopqrstuvwxyz\0\0\0\0\0" | ||
4907 | 844 | "\0abcdefghijklmnopqrstuvwxyz\0\0\0\0\0" | ||
4908 | 845 | "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0" | ||
4909 | 846 | "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0" | ||
4910 | 847 | "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0" | ||
4911 | 848 | "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"; | ||
4912 | 849 | |||
4913 | 850 | state = r->state; | ||
4914 | 851 | hash = r->header_hash; | ||
4915 | 852 | i = r->lowcase_index; | ||
4916 | 853 | |||
4917 | 854 | for (p = b->pos; p < b->last; p++) { | ||
4918 | 855 | ch = *p; | ||
4919 | 856 | |||
4920 | 857 | switch (state) { | ||
4921 | 858 | |||
4922 | 859 | /* first char */ | ||
4923 | 860 | case sw_start: | ||
4924 | 861 | r->header_name_start = p; | ||
4925 | 862 | r->invalid_header = 0; | ||
4926 | 863 | |||
4927 | 864 | switch (ch) { | ||
4928 | 865 | case CR: | ||
4929 | 866 | r->header_end = p; | ||
4930 | 867 | state = sw_header_almost_done; | ||
4931 | 868 | break; | ||
4932 | 869 | case LF: | ||
4933 | 870 | r->header_end = p; | ||
4934 | 871 | goto header_done; | ||
4935 | 872 | default: | ||
4936 | 873 | state = sw_name; | ||
4937 | 874 | |||
4938 | 875 | c = lowcase[ch]; | ||
4939 | 876 | |||
4940 | 877 | if (c) { | ||
4941 | 878 | hash = ngx_hash(0, c); | ||
4942 | 879 | r->lowcase_header[0] = c; | ||
4943 | 880 | i = 1; | ||
4944 | 881 | break; | ||
4945 | 882 | } | ||
4946 | 883 | |||
4947 | 884 | if (ch == '\0') { | ||
4948 | 885 | return NGX_HTTP_PARSE_INVALID_HEADER; | ||
4949 | 886 | } | ||
4950 | 887 | |||
4951 | 888 | r->invalid_header = 1; | ||
4952 | 889 | |||
4953 | 890 | break; | ||
4954 | 891 | |||
4955 | 892 | } | ||
4956 | 893 | break; | ||
4957 | 894 | |||
4958 | 895 | /* header name */ | ||
4959 | 896 | case sw_name: | ||
4960 | 897 | c = lowcase[ch]; | ||
4961 | 898 | |||
4962 | 899 | if (c) { | ||
4963 | 900 | hash = ngx_hash(hash, c); | ||
4964 | 901 | r->lowcase_header[i++] = c; | ||
4965 | 902 | i &= (NGX_HTTP_LC_HEADER_LEN - 1); | ||
4966 | 903 | break; | ||
4967 | 904 | } | ||
4968 | 905 | |||
4969 | 906 | if (ch == '_') { | ||
4970 | 907 | if (allow_underscores) { | ||
4971 | 908 | hash = ngx_hash(hash, ch); | ||
4972 | 909 | r->lowcase_header[i++] = ch; | ||
4973 | 910 | i &= (NGX_HTTP_LC_HEADER_LEN - 1); | ||
4974 | 911 | |||
4975 | 912 | } else { | ||
4976 | 913 | r->invalid_header = 1; | ||
4977 | 914 | } | ||
4978 | 915 | |||
4979 | 916 | break; | ||
4980 | 917 | } | ||
4981 | 918 | |||
4982 | 919 | if (ch == ':') { | ||
4983 | 920 | r->header_name_end = p; | ||
4984 | 921 | state = sw_space_before_value; | ||
4985 | 922 | break; | ||
4986 | 923 | } | ||
4987 | 924 | |||
4988 | 925 | if (ch == CR) { | ||
4989 | 926 | r->header_name_end = p; | ||
4990 | 927 | r->header_start = p; | ||
4991 | 928 | r->header_end = p; | ||
4992 | 929 | state = sw_almost_done; | ||
4993 | 930 | break; | ||
4994 | 931 | } | ||
4995 | 932 | |||
4996 | 933 | if (ch == LF) { | ||
4997 | 934 | r->header_name_end = p; | ||
4998 | 935 | r->header_start = p; | ||
4999 | 936 | r->header_end = p; | ||
5000 | 937 | goto done; |