/src/httpd/server/protocol.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* Licensed to the Apache Software Foundation (ASF) under one or more |
2 | | * contributor license agreements. See the NOTICE file distributed with |
3 | | * this work for additional information regarding copyright ownership. |
4 | | * The ASF licenses this file to You under the Apache License, Version 2.0 |
5 | | * (the "License"); you may not use this file except in compliance with |
6 | | * the License. You may obtain a copy of the License at |
7 | | * |
8 | | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | | * |
10 | | * Unless required by applicable law or agreed to in writing, software |
11 | | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | | * See the License for the specific language governing permissions and |
14 | | * limitations under the License. |
15 | | */ |
16 | | |
17 | | /* |
18 | | * protocol.c --- routines which directly communicate with the client. |
19 | | * |
20 | | * Code originally by Rob McCool; much redone by Robert S. Thau |
21 | | * and the Apache Software Foundation. |
22 | | */ |
23 | | |
24 | | #include "apr.h" |
25 | | #include "apr_strings.h" |
26 | | #include "apr_buckets.h" |
27 | | #include "apr_lib.h" |
28 | | #include "apr_signal.h" |
29 | | #include "apr_strmatch.h" |
30 | | |
31 | | #define APR_WANT_STDIO /* for sscanf */ |
32 | | #define APR_WANT_STRFUNC |
33 | | #define APR_WANT_MEMFUNC |
34 | | #include "apr_want.h" |
35 | | |
36 | | #include "util_filter.h" |
37 | | #include "ap_config.h" |
38 | | #include "httpd.h" |
39 | | #include "http_config.h" |
40 | | #include "http_core.h" |
41 | | #include "http_protocol.h" |
42 | | #include "http_main.h" |
43 | | #include "http_request.h" |
44 | | #include "http_vhost.h" |
45 | | #include "http_log.h" /* For errors detected in basic auth common |
46 | | * support code... */ |
47 | | #include "mod_core.h" |
48 | | #include "util_charset.h" |
49 | | #include "util_ebcdic.h" |
50 | | #include "util_time.h" |
51 | | #include "scoreboard.h" |
52 | | |
53 | | #if APR_HAVE_STDARG_H |
54 | | #include <stdarg.h> |
55 | | #endif |
56 | | #if APR_HAVE_UNISTD_H |
57 | | #include <unistd.h> |
58 | | #endif |
59 | | |
60 | | /* we know core's module_index is 0 */ |
61 | | #undef APLOG_MODULE_INDEX |
62 | | #define APLOG_MODULE_INDEX AP_CORE_MODULE_INDEX |
63 | | |
64 | | APR_HOOK_STRUCT( |
65 | | APR_HOOK_LINK(pre_read_request) |
66 | | APR_HOOK_LINK(post_read_request) |
67 | | APR_HOOK_LINK(log_transaction) |
68 | | APR_HOOK_LINK(http_scheme) |
69 | | APR_HOOK_LINK(default_port) |
70 | | APR_HOOK_LINK(note_auth_failure) |
71 | | APR_HOOK_LINK(protocol_propose) |
72 | | APR_HOOK_LINK(protocol_switch) |
73 | | APR_HOOK_LINK(protocol_get) |
74 | | ) |
75 | | |
76 | | AP_DECLARE_DATA ap_filter_rec_t *ap_old_write_func = NULL; |
77 | | |
78 | | |
79 | | /* Patterns to match in ap_make_content_type() */ |
80 | | static const char *needcset[] = { |
81 | | "text/plain", |
82 | | "text/html", |
83 | | NULL |
84 | | }; |
85 | | static const apr_strmatch_pattern **needcset_patterns; |
86 | | static const apr_strmatch_pattern *charset_pattern; |
87 | | |
88 | | AP_DECLARE(void) ap_setup_make_content_type(apr_pool_t *pool) |
89 | 0 | { |
90 | 0 | int i; |
91 | 0 | for (i = 0; needcset[i]; i++) { |
92 | 0 | continue; |
93 | 0 | } |
94 | 0 | needcset_patterns = (const apr_strmatch_pattern **) |
95 | 0 | apr_palloc(pool, (i + 1) * sizeof(apr_strmatch_pattern *)); |
96 | 0 | for (i = 0; needcset[i]; i++) { |
97 | 0 | needcset_patterns[i] = apr_strmatch_precompile(pool, needcset[i], 0); |
98 | 0 | } |
99 | 0 | needcset_patterns[i] = NULL; |
100 | 0 | charset_pattern = apr_strmatch_precompile(pool, "charset=", 0); |
101 | 0 | } |
102 | | |
103 | | /* |
104 | | * Builds the content-type that should be sent to the client from the |
105 | | * content-type specified. The following rules are followed: |
106 | | * - if type is NULL or "", return NULL (do not set content-type). |
107 | | * - if charset adding is disabled, stop processing and return type. |
108 | | * - then, if there are no parameters on type, add the default charset |
109 | | * - return type |
110 | | */ |
111 | | AP_DECLARE(const char *)ap_make_content_type(request_rec *r, const char *type) |
112 | 0 | { |
113 | 0 | const apr_strmatch_pattern **pcset; |
114 | 0 | core_dir_config *conf = |
115 | 0 | (core_dir_config *)ap_get_core_module_config(r->per_dir_config); |
116 | 0 | core_request_config *request_conf; |
117 | 0 | apr_size_t type_len; |
118 | |
|
119 | 0 | if (!type || *type == '\0') { |
120 | 0 | return NULL; |
121 | 0 | } |
122 | | |
123 | 0 | if (conf->add_default_charset != ADD_DEFAULT_CHARSET_ON) { |
124 | 0 | return type; |
125 | 0 | } |
126 | | |
127 | 0 | request_conf = ap_get_core_module_config(r->request_config); |
128 | 0 | if (request_conf->suppress_charset) { |
129 | 0 | return type; |
130 | 0 | } |
131 | | |
132 | 0 | type_len = strlen(type); |
133 | |
|
134 | 0 | if (apr_strmatch(charset_pattern, type, type_len) != NULL) { |
135 | | /* already has parameter, do nothing */ |
136 | | /* XXX we don't check the validity */ |
137 | 0 | ; |
138 | 0 | } |
139 | 0 | else { |
140 | | /* see if it makes sense to add the charset. At present, |
141 | | * we only add it if the Content-type is one of needcset[] |
142 | | */ |
143 | 0 | for (pcset = needcset_patterns; *pcset ; pcset++) { |
144 | 0 | if (apr_strmatch(*pcset, type, type_len) != NULL) { |
145 | 0 | struct iovec concat[3]; |
146 | 0 | concat[0].iov_base = (void *)type; |
147 | 0 | concat[0].iov_len = type_len; |
148 | 0 | concat[1].iov_base = (void *)"; charset="; |
149 | 0 | concat[1].iov_len = sizeof("; charset=") - 1; |
150 | 0 | concat[2].iov_base = (void *)(conf->add_default_charset_name); |
151 | 0 | concat[2].iov_len = strlen(conf->add_default_charset_name); |
152 | 0 | type = apr_pstrcatv(r->pool, concat, 3, NULL); |
153 | 0 | break; |
154 | 0 | } |
155 | 0 | } |
156 | 0 | } |
157 | |
|
158 | 0 | return type; |
159 | 0 | } |
160 | | |
161 | | AP_DECLARE(void) ap_set_content_length(request_rec *r, apr_off_t clength) |
162 | 0 | { |
163 | 0 | r->clength = clength; |
164 | 0 | apr_table_setn(r->headers_out, "Content-Length", |
165 | 0 | apr_off_t_toa(r->pool, clength)); |
166 | 0 | } |
167 | | |
168 | | /* |
169 | | * Return the latest rational time from a request/mtime (modification time) |
170 | | * pair. We return the mtime unless it's in the future, in which case we |
171 | | * return the current time. We use the request time as a reference in order |
172 | | * to limit the number of calls to time(). We don't check for futurosity |
173 | | * unless the mtime is at least as new as the reference. |
174 | | */ |
175 | | AP_DECLARE(apr_time_t) ap_rationalize_mtime(request_rec *r, apr_time_t mtime) |
176 | 0 | { |
177 | 0 | apr_time_t now; |
178 | | |
179 | | /* For all static responses, it's almost certain that the file was |
180 | | * last modified before the beginning of the request. So there's |
181 | | * no reason to call time(NULL) again. But if the response has been |
182 | | * created on demand, then it might be newer than the time the request |
183 | | * started. In this event we really have to call time(NULL) again |
184 | | * so that we can give the clients the most accurate Last-Modified. If we |
185 | | * were given a time in the future, we return the current time - the |
186 | | * Last-Modified can't be in the future. |
187 | | */ |
188 | 0 | now = (mtime < r->request_time) ? r->request_time : apr_time_now(); |
189 | 0 | return (mtime > now) ? now : mtime; |
190 | 0 | } |
191 | | |
192 | | /* Get a line from an input filter, including any continuation lines |
193 | | * caused by MIME folding (or broken clients) if fold != 0, and place it |
194 | | * in the buffer s, of size n bytes, without the ending newline. |
195 | | * |
196 | | * If s is NULL, ap_fgetline_core will allocate necessary memory from p. |
197 | | * |
198 | | * Returns APR_SUCCESS if there are no problems and sets *read to be |
199 | | * the full length of s. |
200 | | * |
201 | | * APR_ENOSPC is returned if there is not enough buffer space. |
202 | | * Other errors may be returned on other errors. |
203 | | * |
204 | | * The [CR]LF are *not* returned in the buffer. Therefore, a *read of 0 |
205 | | * indicates that an empty line was read. |
206 | | * |
207 | | * Notes: Because the buffer uses 1 char for NUL, the most we can return is |
208 | | * (n - 1) actual characters. |
209 | | * |
210 | | * If no LF is detected on the last line due to a dropped connection |
211 | | * or a full buffer, that's considered an error. |
212 | | */ |
213 | | static apr_status_t ap_fgetline_core(char **s, apr_size_t n, |
214 | | apr_size_t *read, ap_filter_t *f, |
215 | | int flags, apr_bucket_brigade *bb, |
216 | | apr_pool_t *p) |
217 | 0 | { |
218 | 0 | apr_status_t rv; |
219 | 0 | apr_bucket *e; |
220 | 0 | apr_size_t bytes_handled = 0, current_alloc = 0; |
221 | 0 | char *pos, *last_char = *s; |
222 | 0 | int do_alloc = (*s == NULL), saw_eos = 0; |
223 | 0 | int fold = flags & AP_GETLINE_FOLD; |
224 | 0 | int crlf = flags & AP_GETLINE_CRLF; |
225 | 0 | int nospc_eol = flags & AP_GETLINE_NOSPC_EOL; |
226 | 0 | int saw_eol = 0, saw_nospc = 0; |
227 | 0 | apr_read_type_e block; |
228 | |
|
229 | 0 | if (!n) { |
230 | | /* Needs room for NUL byte at least */ |
231 | 0 | *read = 0; |
232 | 0 | return APR_BADARG; |
233 | 0 | } |
234 | | |
235 | 0 | block = (flags & AP_GETLINE_NONBLOCK) ? APR_NONBLOCK_READ |
236 | 0 | : APR_BLOCK_READ; |
237 | | |
238 | | /* |
239 | | * Initialize last_char as otherwise a random value will be compared |
240 | | * against APR_ASCII_LF at the end of the loop if bb only contains |
241 | | * zero-length buckets. |
242 | | */ |
243 | 0 | if (last_char) |
244 | 0 | *last_char = '\0'; |
245 | |
|
246 | 0 | do { |
247 | 0 | apr_brigade_cleanup(bb); |
248 | 0 | rv = ap_get_brigade(f, bb, AP_MODE_GETLINE, block, 0); |
249 | 0 | if (rv != APR_SUCCESS) { |
250 | 0 | goto cleanup; |
251 | 0 | } |
252 | | |
253 | | /* Something horribly wrong happened. Someone didn't block! |
254 | | * (this also happens at the end of each keepalive connection) |
255 | | * (this also happens when non-blocking is asked too, not that wrong) |
256 | | */ |
257 | 0 | if (APR_BRIGADE_EMPTY(bb)) { |
258 | 0 | if (block != APR_NONBLOCK_READ) { |
259 | 0 | rv = APR_EGENERAL; |
260 | 0 | } |
261 | 0 | else { |
262 | 0 | rv = APR_EAGAIN; |
263 | 0 | } |
264 | 0 | goto cleanup; |
265 | 0 | } |
266 | | |
267 | 0 | for (e = APR_BRIGADE_FIRST(bb); |
268 | 0 | e != APR_BRIGADE_SENTINEL(bb); |
269 | 0 | e = APR_BUCKET_NEXT(e)) |
270 | 0 | { |
271 | 0 | const char *str; |
272 | 0 | apr_size_t len; |
273 | | |
274 | | /* If we see an EOS, don't bother doing anything more. */ |
275 | 0 | if (APR_BUCKET_IS_EOS(e)) { |
276 | 0 | saw_eos = 1; |
277 | 0 | break; |
278 | 0 | } |
279 | | |
280 | 0 | rv = apr_bucket_read(e, &str, &len, APR_BLOCK_READ); |
281 | 0 | if (rv != APR_SUCCESS) { |
282 | 0 | goto cleanup; |
283 | 0 | } |
284 | | |
285 | 0 | if (len == 0) { |
286 | | /* no use attempting a zero-byte alloc (hurts when |
287 | | * using --with-efence --enable-pool-debug) or |
288 | | * doing any of the other logic either |
289 | | */ |
290 | 0 | continue; |
291 | 0 | } |
292 | | |
293 | | /* Would this overrun our buffer? If so, we'll die. */ |
294 | 0 | if (n < bytes_handled + len) { |
295 | | /* Before we die, let's fill the buffer up to its limit (i.e. |
296 | | * fall through with the remaining length, if any), setting |
297 | | * saw_eol on LF to stop the outer loop appropriately; we may |
298 | | * come back here once the buffer is filled (no LF seen), and |
299 | | * either be done at that time or continue to wait for LF here |
300 | | * if nospc_eol is set. |
301 | | * |
302 | | * But there is also a corner cases which we want to address, |
303 | | * namely if the buffer is overrun by the final LF only (i.e. |
304 | | * the CR fits in); this is not really an overrun since we'll |
305 | | * strip the CR finally (and use it for NUL byte), but anyway |
306 | | * we have to handle the case so that it's not returned to the |
307 | | * caller as part of the truncated line (it's not!). This is |
308 | | * easier to consider that LF is out of counting and thus fall |
309 | | * through with no error (saw_eol is set to 2 so that we later |
310 | | * ignore LF handling already done here), while folding and |
311 | | * nospc_eol logics continue to work (or fail) appropriately. |
312 | | */ |
313 | 0 | saw_eol = (str[len - 1] == APR_ASCII_LF); |
314 | 0 | if (/* First time around */ |
315 | 0 | saw_eol && !saw_nospc |
316 | | /* Single LF completing the buffered CR, */ |
317 | 0 | && ((len == 1 && ((*s)[bytes_handled - 1] == APR_ASCII_CR)) |
318 | | /* or trailing CRLF overuns by LF only */ |
319 | 0 | || (len > 1 && str[len - 2] == APR_ASCII_CR |
320 | 0 | && n - bytes_handled + 1 == len))) { |
321 | | /* In both cases *last_char is (to be) the CR stripped by |
322 | | * later 'bytes_handled = last_char - *s'. |
323 | | */ |
324 | 0 | saw_eol = 2; |
325 | 0 | } |
326 | 0 | else { |
327 | | /* In any other case we'd lose data. */ |
328 | 0 | rv = APR_ENOSPC; |
329 | 0 | saw_nospc = 1; |
330 | 0 | } |
331 | 0 | len = n - bytes_handled; |
332 | 0 | if (!len) { |
333 | 0 | if (saw_eol) { |
334 | 0 | break; |
335 | 0 | } |
336 | 0 | if (nospc_eol) { |
337 | 0 | continue; |
338 | 0 | } |
339 | 0 | goto cleanup; |
340 | 0 | } |
341 | 0 | } |
342 | | |
343 | | /* Do we have to handle the allocation ourselves? */ |
344 | 0 | if (do_alloc) { |
345 | | /* We'll assume the common case where one bucket is enough. */ |
346 | 0 | if (!*s) { |
347 | 0 | current_alloc = len; |
348 | 0 | *s = apr_palloc(p, current_alloc + 1); |
349 | 0 | } |
350 | 0 | else if (bytes_handled + len > current_alloc) { |
351 | | /* Increase the buffer size */ |
352 | 0 | apr_size_t new_size = current_alloc * 2; |
353 | 0 | char *new_buffer; |
354 | |
|
355 | 0 | if (bytes_handled + len > new_size) { |
356 | 0 | new_size = (bytes_handled + len) * 2; |
357 | 0 | } |
358 | |
|
359 | 0 | new_buffer = apr_palloc(p, new_size + 1); |
360 | | |
361 | | /* Copy what we already had. */ |
362 | 0 | memcpy(new_buffer, *s, bytes_handled); |
363 | 0 | current_alloc = new_size; |
364 | 0 | *s = new_buffer; |
365 | 0 | } |
366 | 0 | } |
367 | | |
368 | | /* Just copy the rest of the data to the end of the old buffer. */ |
369 | 0 | pos = *s + bytes_handled; |
370 | 0 | memcpy(pos, str, len); |
371 | 0 | last_char = pos + len - 1; |
372 | | |
373 | | /* We've now processed that new data - update accordingly. */ |
374 | 0 | bytes_handled += len; |
375 | 0 | } |
376 | | |
377 | | /* If we got a full line of input, stop reading */ |
378 | 0 | if (last_char && (*last_char == APR_ASCII_LF)) { |
379 | 0 | saw_eol = 1; |
380 | 0 | } |
381 | 0 | } while (!saw_eol); |
382 | | |
383 | 0 | if (rv != APR_SUCCESS) { |
384 | | /* End of line after APR_ENOSPC above */ |
385 | 0 | goto cleanup; |
386 | 0 | } |
387 | | |
388 | | /* Now terminate the string at the end of the line; |
389 | | * if the last-but-one character is a CR, terminate there. |
390 | | * LF is handled above (not accounted) when saw_eol == 2, |
391 | | * the last char is CR to terminate at still. |
392 | | */ |
393 | 0 | if (saw_eol < 2) { |
394 | 0 | if (last_char > *s && last_char[-1] == APR_ASCII_CR) { |
395 | 0 | last_char--; |
396 | 0 | } |
397 | 0 | else if (crlf) { |
398 | 0 | rv = APR_EINVAL; |
399 | 0 | goto cleanup; |
400 | 0 | } |
401 | 0 | } |
402 | 0 | bytes_handled = last_char - *s; |
403 | | |
404 | | /* If we're folding, we have more work to do. |
405 | | * |
406 | | * Note that if an EOS was seen, we know we can't have another line. |
407 | | */ |
408 | 0 | if (fold && bytes_handled && !saw_eos) { |
409 | 0 | for (;;) { |
410 | 0 | const char *str; |
411 | 0 | apr_size_t len; |
412 | 0 | char c; |
413 | | |
414 | | /* Clear the temp brigade for this filter read. */ |
415 | 0 | apr_brigade_cleanup(bb); |
416 | | |
417 | | /* We only care about the first byte. */ |
418 | 0 | rv = ap_get_brigade(f, bb, AP_MODE_SPECULATIVE, block, 1); |
419 | 0 | if (rv != APR_SUCCESS) { |
420 | 0 | goto cleanup; |
421 | 0 | } |
422 | | |
423 | 0 | if (APR_BRIGADE_EMPTY(bb)) { |
424 | 0 | break; |
425 | 0 | } |
426 | | |
427 | 0 | e = APR_BRIGADE_FIRST(bb); |
428 | | |
429 | | /* If we see an EOS, don't bother doing anything more. */ |
430 | 0 | if (APR_BUCKET_IS_EOS(e)) { |
431 | 0 | break; |
432 | 0 | } |
433 | | |
434 | 0 | rv = apr_bucket_read(e, &str, &len, APR_BLOCK_READ); |
435 | 0 | if (rv != APR_SUCCESS) { |
436 | 0 | apr_brigade_cleanup(bb); |
437 | 0 | goto cleanup; |
438 | 0 | } |
439 | | |
440 | | /* Found one, so call ourselves again to get the next line. |
441 | | * |
442 | | * FIXME: If the folding line is completely blank, should we |
443 | | * stop folding? Does that require also looking at the next |
444 | | * char? |
445 | | */ |
446 | | /* When we call destroy, the buckets are deleted, so save that |
447 | | * one character we need. This simplifies our execution paths |
448 | | * at the cost of one character read. |
449 | | */ |
450 | 0 | c = *str; |
451 | 0 | if (c == APR_ASCII_BLANK || c == APR_ASCII_TAB) { |
452 | | /* Do we have enough space? We may be full now. */ |
453 | 0 | if (bytes_handled >= n) { |
454 | 0 | rv = APR_ENOSPC; |
455 | 0 | goto cleanup; |
456 | 0 | } |
457 | 0 | else { |
458 | 0 | apr_size_t next_size, next_len; |
459 | 0 | char *tmp; |
460 | | |
461 | | /* If we're doing the allocations for them, we have to |
462 | | * give ourselves a NULL and copy it on return. |
463 | | */ |
464 | 0 | if (do_alloc) { |
465 | 0 | tmp = NULL; |
466 | 0 | } |
467 | 0 | else { |
468 | 0 | tmp = last_char; |
469 | 0 | } |
470 | |
|
471 | 0 | next_size = n - bytes_handled; |
472 | |
|
473 | 0 | rv = ap_fgetline_core(&tmp, next_size, &next_len, f, |
474 | 0 | flags & ~AP_GETLINE_FOLD, bb, p); |
475 | 0 | if (rv != APR_SUCCESS) { |
476 | 0 | goto cleanup; |
477 | 0 | } |
478 | | |
479 | 0 | if (do_alloc && next_len > 0) { |
480 | 0 | char *new_buffer; |
481 | 0 | apr_size_t new_size = bytes_handled + next_len + 1; |
482 | | |
483 | | /* we need to alloc an extra byte for a null */ |
484 | 0 | new_buffer = apr_palloc(p, new_size); |
485 | | |
486 | | /* Copy what we already had. */ |
487 | 0 | memcpy(new_buffer, *s, bytes_handled); |
488 | | |
489 | | /* copy the new line, including the trailing null */ |
490 | 0 | memcpy(new_buffer + bytes_handled, tmp, next_len); |
491 | 0 | *s = new_buffer; |
492 | 0 | } |
493 | |
|
494 | 0 | last_char += next_len; |
495 | 0 | bytes_handled += next_len; |
496 | 0 | } |
497 | 0 | } |
498 | 0 | else { /* next character is not tab or space */ |
499 | 0 | break; |
500 | 0 | } |
501 | 0 | } |
502 | 0 | } |
503 | | |
504 | 0 | cleanup: |
505 | 0 | if (bytes_handled >= n) { |
506 | 0 | bytes_handled = n - 1; |
507 | 0 | } |
508 | |
|
509 | 0 | *read = bytes_handled; |
510 | 0 | if (*s) { |
511 | | /* ensure the string is NUL terminated */ |
512 | 0 | (*s)[*read] = '\0'; |
513 | | |
514 | | /* PR#43039: We shouldn't accept NULL bytes within the line */ |
515 | 0 | bytes_handled = strlen(*s); |
516 | 0 | if (bytes_handled < *read) { |
517 | 0 | ap_log_data(APLOG_MARK, APLOG_DEBUG, ap_server_conf, |
518 | 0 | "NULL bytes in header", *s, *read, 0); |
519 | 0 | *read = bytes_handled; |
520 | 0 | if (rv == APR_SUCCESS) { |
521 | 0 | rv = APR_EINVAL; |
522 | 0 | } |
523 | 0 | } |
524 | 0 | } |
525 | 0 | return rv; |
526 | 0 | } |
527 | | |
528 | | AP_DECLARE(apr_status_t) ap_fgetline(char **s, apr_size_t n, |
529 | | apr_size_t *read, ap_filter_t *f, |
530 | | int flags, apr_bucket_brigade *bb, |
531 | | apr_pool_t *p) |
532 | 0 | { |
533 | 0 | apr_status_t rv; |
534 | | |
535 | 0 | rv = ap_fgetline_core(s, n, read, f, flags, bb, p); |
536 | |
|
537 | | #if APR_CHARSET_EBCDIC |
538 | | /* On EBCDIC boxes, each complete http protocol input line needs to be |
539 | | * translated into the code page used by the compiler. Since |
540 | | * ap_fgetline_core uses recursion, we do the translation in a wrapper |
541 | | * function to ensure that each input character gets translated only once. |
542 | | */ |
543 | | if (*read) { |
544 | | ap_xlate_proto_from_ascii(*s, *read); |
545 | | } |
546 | | #endif |
547 | |
|
548 | 0 | return rv; |
549 | 0 | } |
550 | | |
551 | | /* Same as ap_fgetline(), working on r's pool and protocol input filters. |
552 | | * Pulls from r->proto_input_filters instead of r->input_filters for |
553 | | * stricter protocol adherence and better input filter behavior during |
554 | | * chunked trailer processing (for http). |
555 | | */ |
556 | | AP_DECLARE(apr_status_t) ap_rgetline(char **s, apr_size_t n, |
557 | | apr_size_t *read, request_rec *r, |
558 | | int flags, apr_bucket_brigade *bb) |
559 | 0 | { |
560 | 0 | apr_status_t rv; |
561 | |
|
562 | 0 | rv = ap_fgetline_core(s, n, read, r->proto_input_filters, flags, |
563 | 0 | bb, r->pool); |
564 | | #if APR_CHARSET_EBCDIC |
565 | | /* On EBCDIC boxes, each complete http protocol input line needs to be |
566 | | * translated into the code page used by the compiler. Since |
567 | | * ap_fgetline_core uses recursion, we do the translation in a wrapper |
568 | | * function to ensure that each input character gets translated only once. |
569 | | */ |
570 | | if (*read) { |
571 | | ap_xlate_proto_from_ascii(*s, *read); |
572 | | } |
573 | | #endif |
574 | |
|
575 | 0 | return rv; |
576 | 0 | } |
577 | | |
578 | | AP_DECLARE(int) ap_getline(char *s, int n, request_rec *r, int flags) |
579 | 0 | { |
580 | 0 | apr_status_t rv; |
581 | 0 | apr_size_t len; |
582 | 0 | apr_bucket_brigade *tmp_bb; |
583 | |
|
584 | 0 | if (n < 1) { |
585 | | /* Can't work since we always NUL terminate */ |
586 | 0 | return -1; |
587 | 0 | } |
588 | | |
589 | 0 | tmp_bb = apr_brigade_create(r->pool, r->connection->bucket_alloc); |
590 | 0 | rv = ap_rgetline(&s, n, &len, r, flags, tmp_bb); |
591 | 0 | apr_brigade_destroy(tmp_bb); |
592 | | |
593 | | /* Map the out-of-space condition to the old API. */ |
594 | 0 | if (rv == APR_ENOSPC) { |
595 | 0 | return n; |
596 | 0 | } |
597 | | |
598 | | /* Anything else is just bad. */ |
599 | 0 | if (rv != APR_SUCCESS) { |
600 | 0 | return -1; |
601 | 0 | } |
602 | | |
603 | 0 | return (int)len; |
604 | 0 | } |
605 | | |
606 | | /* parse_uri: break apart the uri |
607 | | * Side Effects: |
608 | | * - sets r->args to rest after '?' (or NULL if no '?') |
609 | | * - sets r->uri to request uri (without r->args part) |
610 | | * - sets r->hostname (if not set already) from request (scheme://host:port) |
611 | | */ |
612 | | AP_CORE_DECLARE(void) ap_parse_uri(request_rec *r, const char *uri) |
613 | 347 | { |
614 | 347 | int status = HTTP_OK; |
615 | | |
616 | 347 | r->unparsed_uri = apr_pstrdup(r->pool, uri); |
617 | | |
618 | | /* http://issues.apache.org/bugzilla/show_bug.cgi?id=31875 |
619 | | * http://issues.apache.org/bugzilla/show_bug.cgi?id=28450 |
620 | | * |
621 | | * This is not in fact a URI, it's a path. That matters in the |
622 | | * case of a leading double-slash. We need to resolve the issue |
623 | | * by normalizing that out before treating it as a URI. |
624 | | */ |
625 | 676 | while ((uri[0] == '/') && (uri[1] == '/')) { |
626 | 329 | ++uri ; |
627 | 329 | } |
628 | 347 | if (r->method_number == M_CONNECT) { |
629 | 30 | status = apr_uri_parse_hostinfo(r->pool, uri, &r->parsed_uri); |
630 | 30 | } |
631 | 317 | else { |
632 | 317 | status = apr_uri_parse(r->pool, uri, &r->parsed_uri); |
633 | 317 | } |
634 | | |
635 | 347 | if (status == APR_SUCCESS) { |
636 | | /* if it has a scheme we may need to do absoluteURI vhost stuff */ |
637 | 269 | if (r->parsed_uri.scheme |
638 | 269 | && !ap_cstr_casecmp(r->parsed_uri.scheme, ap_http_scheme(r))) { |
639 | 2 | r->hostname = r->parsed_uri.hostname; |
640 | 2 | } |
641 | 267 | else if (r->method_number == M_CONNECT) { |
642 | 2 | r->hostname = r->parsed_uri.hostname; |
643 | 2 | } |
644 | | |
645 | 269 | r->args = r->parsed_uri.query; |
646 | 269 | if (r->parsed_uri.path) { |
647 | 231 | r->uri = r->parsed_uri.path; |
648 | 231 | } |
649 | 38 | else if (r->method_number == M_OPTIONS) { |
650 | 1 | r->uri = apr_pstrdup(r->pool, "*"); |
651 | 1 | } |
652 | 37 | else { |
653 | 37 | r->uri = apr_pstrdup(r->pool, "/"); |
654 | 37 | } |
655 | | |
656 | | #if defined(OS2) || defined(WIN32) |
657 | | /* Handle path translations for OS/2 and plug security hole. |
658 | | * This will prevent "http://www.wherever.com/..\..\/" from |
659 | | * returning a directory for the root drive. |
660 | | */ |
661 | | { |
662 | | char *x; |
663 | | |
664 | | for (x = r->uri; (x = strchr(x, '\\')) != NULL; ) |
665 | | *x = '/'; |
666 | | } |
667 | | #endif /* OS2 || WIN32 */ |
668 | 269 | } |
669 | 78 | else { |
670 | 78 | r->args = NULL; |
671 | 78 | r->hostname = NULL; |
672 | 78 | r->status = HTTP_BAD_REQUEST; /* set error status */ |
673 | 78 | r->uri = apr_pstrdup(r->pool, uri); |
674 | 78 | } |
675 | 347 | } |
676 | | |
677 | | /* get the length of the field name for logging, but no more than 80 bytes */ |
678 | 0 | #define LOG_NAME_MAX_LEN 80 |
679 | | static int field_name_len(const char *field) |
680 | 0 | { |
681 | 0 | const char *end = ap_strchr_c(field, ':'); |
682 | 0 | if (end == NULL || end - field > LOG_NAME_MAX_LEN) |
683 | 0 | return LOG_NAME_MAX_LEN; |
684 | 0 | return end - field; |
685 | 0 | } |
686 | | |
687 | | AP_DECLARE(int) ap_parse_request_line(request_rec *r) |
688 | 495 | { |
689 | 495 | const char *method, *uri, *protocol; |
690 | | |
691 | 495 | return ap_h1_tokenize_request_line(r, r->the_request, |
692 | 495 | &method, &uri, &protocol) |
693 | 495 | && ap_assign_request_line(r, method, uri, protocol); |
694 | 495 | } |
695 | | |
696 | | AP_DECLARE(int) ap_check_request_header(request_rec *r) |
697 | 0 | { |
698 | 0 | core_server_config *conf; |
699 | 0 | int strict_host_check; |
700 | 0 | const char *expect; |
701 | 0 | int access_status; |
702 | |
|
703 | 0 | conf = ap_get_core_module_config(r->server->module_config); |
704 | | |
705 | | /* update what we think the virtual host is based on the headers we've |
706 | | * now read. may update status. |
707 | | */ |
708 | 0 | strict_host_check = (conf->strict_host_check == AP_CORE_CONFIG_ON); |
709 | 0 | access_status = ap_update_vhost_from_headers_ex(r, strict_host_check); |
710 | 0 | if (strict_host_check && access_status != HTTP_OK) { |
711 | 0 | if (r->server == ap_server_conf) { |
712 | 0 | ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, APLOGNO(10156) |
713 | 0 | "Requested hostname '%s' did not match any ServerName/ServerAlias " |
714 | 0 | "in the global server configuration ", r->hostname); |
715 | 0 | } |
716 | 0 | else { |
717 | 0 | ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, APLOGNO(10157) |
718 | 0 | "Requested hostname '%s' did not match any ServerName/ServerAlias " |
719 | 0 | "in the matching virtual host (default vhost for " |
720 | 0 | "current connection is %s:%u)", |
721 | 0 | r->hostname, r->server->defn_name, r->server->defn_line_number); |
722 | 0 | } |
723 | 0 | r->status = access_status; |
724 | 0 | } |
725 | 0 | if (r->status != HTTP_OK) { |
726 | 0 | return 0; |
727 | 0 | } |
728 | | |
729 | 0 | if ((!r->hostname && (r->proto_num >= HTTP_VERSION(1, 1))) |
730 | 0 | || ((r->proto_num == HTTP_VERSION(1, 1)) |
731 | 0 | && !apr_table_get(r->headers_in, "Host"))) { |
732 | | /* |
733 | | * Client sent us an HTTP/1.1 or later request without telling us the |
734 | | * hostname, either with a full URL or a Host: header. We therefore |
735 | | * need to (as per the 1.1 spec) send an error. As a special case, |
736 | | * HTTP/1.1 mentions twice (S9, S14.23) that a request MUST contain |
737 | | * a Host: header, and the server MUST respond with 400 if it doesn't. |
738 | | */ |
739 | 0 | ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(00569) |
740 | 0 | "client sent HTTP/1.1 request without hostname " |
741 | 0 | "(see RFC2616 section 14.23): %s", r->uri); |
742 | 0 | r->status = HTTP_BAD_REQUEST; |
743 | 0 | return 0; |
744 | 0 | } |
745 | | |
746 | | /* we may have switched to another server */ |
747 | 0 | conf = ap_get_core_module_config(r->server->module_config); |
748 | |
|
749 | 0 | if (((expect = apr_table_get(r->headers_in, "Expect")) != NULL) |
750 | 0 | && (expect[0] != '\0')) { |
751 | | /* |
752 | | * The Expect header field was added to HTTP/1.1 after RFC 2068 |
753 | | * as a means to signal when a 100 response is desired and, |
754 | | * unfortunately, to signal a poor man's mandatory extension that |
755 | | * the server must understand or return 417 Expectation Failed. |
756 | | */ |
757 | 0 | if (ap_cstr_casecmp(expect, "100-continue") == 0) { |
758 | 0 | r->expecting_100 = 1; |
759 | 0 | } |
760 | 0 | else if (conf->http_expect_strict == AP_HTTP_EXPECT_STRICT_DISABLE) { |
761 | 0 | ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(02595) |
762 | 0 | "client sent an unrecognized expectation value " |
763 | 0 | "of Expect (not fatal): %s", expect); |
764 | 0 | } |
765 | 0 | else { |
766 | 0 | ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(00570) |
767 | 0 | "client sent an unrecognized expectation value " |
768 | 0 | "of Expect: %s", expect); |
769 | 0 | r->status = HTTP_EXPECTATION_FAILED; |
770 | 0 | return 0; |
771 | 0 | } |
772 | 0 | } |
773 | | |
774 | 0 | return 1; |
775 | 0 | } |
776 | | |
777 | | static int table_do_fn_check_lengths(void *r_, const char *key, |
778 | | const char *value) |
779 | 0 | { |
780 | 0 | request_rec *r = r_; |
781 | 0 | if (value == NULL || r->server->limit_req_fieldsize >= strlen(value) ) |
782 | 0 | return 1; |
783 | | |
784 | 0 | r->status = HTTP_BAD_REQUEST; |
785 | 0 | apr_table_setn(r->notes, "error-notes", |
786 | 0 | "Size of a request header field exceeds server limit."); |
787 | 0 | ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(00560) "Request " |
788 | 0 | "header exceeds LimitRequestFieldSize after merging: %.*s", |
789 | 0 | field_name_len(key), key); |
790 | 0 | return 0; |
791 | 0 | } |
792 | | |
793 | | AP_DECLARE(void) ap_get_mime_headers_core(request_rec *r, apr_bucket_brigade *bb) |
794 | 0 | { |
795 | 0 | char *last_field = NULL; |
796 | 0 | apr_size_t last_len = 0; |
797 | 0 | apr_size_t alloc_len = 0; |
798 | 0 | char *field; |
799 | 0 | char *value; |
800 | 0 | apr_size_t len; |
801 | 0 | int fields_read = 0; |
802 | 0 | char *tmp_field; |
803 | 0 | core_server_config *conf = ap_get_core_module_config(r->server->module_config); |
804 | 0 | int strict = (conf->http_conformance != AP_HTTP_CONFORMANCE_UNSAFE); |
805 | | |
806 | | /* |
807 | | * Read header lines until we get the empty separator line, a read error, |
808 | | * the connection closes (EOF), reach the server limit, or we timeout. |
809 | | */ |
810 | 0 | while(1) { |
811 | 0 | apr_status_t rv; |
812 | |
|
813 | 0 | field = NULL; |
814 | 0 | rv = ap_rgetline(&field, r->server->limit_req_fieldsize + 2, |
815 | 0 | &len, r, strict ? AP_GETLINE_CRLF : 0, bb); |
816 | |
|
817 | 0 | if (rv != APR_SUCCESS) { |
818 | 0 | if (APR_STATUS_IS_TIMEUP(rv)) { |
819 | 0 | r->status = HTTP_REQUEST_TIME_OUT; |
820 | 0 | } |
821 | 0 | else { |
822 | 0 | r->status = HTTP_BAD_REQUEST; |
823 | 0 | } |
824 | | |
825 | | /* ap_rgetline returns APR_ENOSPC if it fills up the buffer before |
826 | | * finding the end-of-line. This is only going to happen if it |
827 | | * exceeds the configured limit for a field size. |
828 | | */ |
829 | 0 | if (rv == APR_ENOSPC) { |
830 | 0 | apr_table_setn(r->notes, "error-notes", |
831 | 0 | "Size of a request header field " |
832 | 0 | "exceeds server limit."); |
833 | 0 | ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(00561) |
834 | 0 | "Request header exceeds LimitRequestFieldSize%s" |
835 | 0 | "%.*s", |
836 | 0 | (field && *field) ? ": " : "", |
837 | 0 | (field) ? field_name_len(field) : 0, |
838 | 0 | (field) ? field : ""); |
839 | 0 | } |
840 | 0 | return; |
841 | 0 | } |
842 | | |
843 | | /* For all header values, and all obs-fold lines, the presence of |
844 | | * additional whitespace is a no-op, so collapse trailing whitespace |
845 | | * to save buffer allocation and optimize copy operations. |
846 | | * Do not remove the last single whitespace under any condition. |
847 | | */ |
848 | 0 | while (len > 1 && (field[len-1] == '\t' || field[len-1] == ' ')) { |
849 | 0 | field[--len] = '\0'; |
850 | 0 | } |
851 | |
|
852 | 0 | if (*field == '\t' || *field == ' ') { |
853 | | |
854 | | /* Append any newly-read obs-fold line onto the preceding |
855 | | * last_field line we are processing |
856 | | */ |
857 | 0 | apr_size_t fold_len; |
858 | |
|
859 | 0 | if (last_field == NULL) { |
860 | 0 | r->status = HTTP_BAD_REQUEST; |
861 | 0 | ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(03442) |
862 | 0 | "Line folding encountered before first" |
863 | 0 | " header line"); |
864 | 0 | return; |
865 | 0 | } |
866 | | |
867 | 0 | if (field[1] == '\0') { |
868 | 0 | r->status = HTTP_BAD_REQUEST; |
869 | 0 | ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(03443) |
870 | 0 | "Empty folded line encountered"); |
871 | 0 | return; |
872 | 0 | } |
873 | | |
874 | | /* Leading whitespace on an obs-fold line can be |
875 | | * similarly discarded */ |
876 | 0 | while (field[1] == '\t' || field[1] == ' ') { |
877 | 0 | ++field; --len; |
878 | 0 | } |
879 | | |
880 | | /* This line is a continuation of the preceding line(s), |
881 | | * so append it to the line that we've set aside. |
882 | | * Note: this uses a power-of-two allocator to avoid |
883 | | * doing O(n) allocs and using O(n^2) space for |
884 | | * continuations that span many many lines. |
885 | | */ |
886 | 0 | fold_len = last_len + len + 1; /* trailing null */ |
887 | |
|
888 | 0 | if (fold_len >= (apr_size_t)(r->server->limit_req_fieldsize)) { |
889 | 0 | r->status = HTTP_BAD_REQUEST; |
890 | | /* report what we have accumulated so far before the |
891 | | * overflow (last_field) as the field with the problem |
892 | | */ |
893 | 0 | apr_table_setn(r->notes, "error-notes", |
894 | 0 | "Size of a request header field " |
895 | 0 | "exceeds server limit."); |
896 | 0 | ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(00562) |
897 | 0 | "Request header exceeds LimitRequestFieldSize " |
898 | 0 | "after folding: %.*s", |
899 | 0 | field_name_len(last_field), last_field); |
900 | 0 | return; |
901 | 0 | } |
902 | | |
903 | 0 | if (fold_len > alloc_len) { |
904 | 0 | char *fold_buf; |
905 | 0 | alloc_len += alloc_len; |
906 | 0 | if (fold_len > alloc_len) { |
907 | 0 | alloc_len = fold_len; |
908 | 0 | } |
909 | 0 | fold_buf = (char *)apr_palloc(r->pool, alloc_len); |
910 | 0 | memcpy(fold_buf, last_field, last_len); |
911 | 0 | last_field = fold_buf; |
912 | 0 | } |
913 | 0 | memcpy(last_field + last_len, field, len +1); /* +1 for nul */ |
914 | | /* Replace obs-fold w/ SP per RFC 7230 3.2.4 */ |
915 | 0 | last_field[last_len] = ' '; |
916 | 0 | last_len += len; |
917 | | |
918 | | /* We've appended this obs-fold line to last_len, proceed to |
919 | | * read the next input line |
920 | | */ |
921 | 0 | continue; |
922 | 0 | } |
923 | 0 | else if (last_field != NULL) { |
924 | | |
925 | | /* Process the previous last_field header line with all obs-folded |
926 | | * segments already concatenated (this is not operating on the |
927 | | * most recently read input line). |
928 | | */ |
929 | |
|
930 | 0 | if (r->server->limit_req_fields |
931 | 0 | && (++fields_read > r->server->limit_req_fields)) { |
932 | 0 | r->status = HTTP_BAD_REQUEST; |
933 | 0 | apr_table_setn(r->notes, "error-notes", |
934 | 0 | "The number of request header fields " |
935 | 0 | "exceeds this server's limit."); |
936 | 0 | ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(00563) |
937 | 0 | "Number of request headers exceeds " |
938 | 0 | "LimitRequestFields"); |
939 | 0 | return; |
940 | 0 | } |
941 | | |
942 | 0 | if (!strict) |
943 | 0 | { |
944 | | /* Not Strict ('Unsafe' mode), using the legacy parser */ |
945 | |
|
946 | 0 | if (!(value = strchr(last_field, ':'))) { /* Find ':' or */ |
947 | 0 | r->status = HTTP_BAD_REQUEST; /* abort bad request */ |
948 | 0 | ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(00564) |
949 | 0 | "Request header field is missing ':' " |
950 | 0 | "separator: %.*s", (int)LOG_NAME_MAX_LEN, |
951 | 0 | last_field); |
952 | 0 | return; |
953 | 0 | } |
954 | | |
955 | 0 | if (value == last_field) { |
956 | 0 | r->status = HTTP_BAD_REQUEST; |
957 | 0 | ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(03453) |
958 | 0 | "Request header field name was empty"); |
959 | 0 | return; |
960 | 0 | } |
961 | | |
962 | 0 | *value++ = '\0'; /* NUL-terminate at colon */ |
963 | |
|
964 | 0 | if (strpbrk(last_field, "\t\n\v\f\r ")) { |
965 | 0 | r->status = HTTP_BAD_REQUEST; |
966 | 0 | ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(03452) |
967 | 0 | "Request header field name presented" |
968 | 0 | " invalid whitespace"); |
969 | 0 | return; |
970 | 0 | } |
971 | | |
972 | 0 | while (*value == ' ' || *value == '\t') { |
973 | 0 | ++value; /* Skip to start of value */ |
974 | 0 | } |
975 | |
|
976 | 0 | if (strpbrk(value, "\n\v\f\r")) { |
977 | 0 | r->status = HTTP_BAD_REQUEST; |
978 | 0 | ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(03451) |
979 | 0 | "Request header field value presented" |
980 | 0 | " bad whitespace"); |
981 | 0 | return; |
982 | 0 | } |
983 | 0 | } |
984 | 0 | else /* Using strict RFC7230 parsing */ |
985 | 0 | { |
986 | | /* Ensure valid token chars before ':' per RFC 7230 3.2.4 */ |
987 | 0 | value = (char *)ap_scan_http_token(last_field); |
988 | 0 | if ((value == last_field) || *value != ':') { |
989 | 0 | r->status = HTTP_BAD_REQUEST; |
990 | 0 | ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(02426) |
991 | 0 | "Request header field name is malformed: " |
992 | 0 | "%.*s", (int)LOG_NAME_MAX_LEN, last_field); |
993 | 0 | return; |
994 | 0 | } |
995 | | |
996 | 0 | *value++ = '\0'; /* NUL-terminate last_field name at ':' */ |
997 | |
|
998 | 0 | while (*value == ' ' || *value == '\t') { |
999 | 0 | ++value; /* Skip LWS of value */ |
1000 | 0 | } |
1001 | | |
1002 | | /* Find invalid, non-HT ctrl char, or the trailing NULL */ |
1003 | 0 | tmp_field = (char *)ap_scan_http_field_content(value); |
1004 | | |
1005 | | /* Reject value for all garbage input (CTRLs excluding HT) |
1006 | | * e.g. only VCHAR / SP / HT / obs-text are allowed per |
1007 | | * RFC7230 3.2.6 - leave all more explicit rule enforcement |
1008 | | * for specific header handler logic later in the cycle |
1009 | | */ |
1010 | 0 | if (*tmp_field != '\0') { |
1011 | 0 | r->status = HTTP_BAD_REQUEST; |
1012 | 0 | ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(02427) |
1013 | 0 | "Request header value is malformed: " |
1014 | 0 | "%.*s", (int)LOG_NAME_MAX_LEN, value); |
1015 | 0 | return; |
1016 | 0 | } |
1017 | 0 | } |
1018 | | |
1019 | 0 | apr_table_addn(r->headers_in, last_field, value); |
1020 | | |
1021 | | /* This last_field header is now stored in headers_in, |
1022 | | * resume processing of the current input line. |
1023 | | */ |
1024 | 0 | } |
1025 | | |
1026 | | /* Found the terminating empty end-of-headers line, stop. */ |
1027 | 0 | if (len == 0) { |
1028 | 0 | break; |
1029 | 0 | } |
1030 | | |
1031 | | /* Keep track of this new header line so that we can extend it across |
1032 | | * any obs-fold or parse it on the next loop iteration. We referenced |
1033 | | * our previously allocated buffer in r->headers_in, |
1034 | | * so allocate a fresh buffer if required. |
1035 | | */ |
1036 | 0 | alloc_len = 0; |
1037 | 0 | last_field = field; |
1038 | 0 | last_len = len; |
1039 | 0 | } |
1040 | | |
1041 | | /* Combine multiple message-header fields with the same |
1042 | | * field-name, following RFC 2616, 4.2. |
1043 | | */ |
1044 | 0 | apr_table_compress(r->headers_in, APR_OVERLAP_TABLES_MERGE); |
1045 | | |
1046 | | /* enforce LimitRequestFieldSize for merged headers */ |
1047 | 0 | apr_table_do(table_do_fn_check_lengths, r, r->headers_in, NULL); |
1048 | 0 | } |
1049 | | |
1050 | | AP_DECLARE(void) ap_get_mime_headers(request_rec *r) |
1051 | 0 | { |
1052 | 0 | apr_bucket_brigade *tmp_bb; |
1053 | 0 | tmp_bb = apr_brigade_create(r->pool, r->connection->bucket_alloc); |
1054 | 0 | ap_get_mime_headers_core(r, tmp_bb); |
1055 | 0 | apr_brigade_destroy(tmp_bb); |
1056 | 0 | } |
1057 | | |
1058 | | AP_DECLARE(request_rec *) ap_create_request(conn_rec *conn) |
1059 | 495 | { |
1060 | 495 | request_rec *r; |
1061 | 495 | apr_pool_t *p; |
1062 | | |
1063 | 495 | apr_pool_create(&p, conn->pool); |
1064 | 495 | apr_pool_tag(p, "request"); |
1065 | 495 | r = apr_pcalloc(p, sizeof(request_rec)); |
1066 | 495 | AP_READ_REQUEST_ENTRY((intptr_t)r, (uintptr_t)conn); |
1067 | 495 | r->pool = p; |
1068 | 495 | r->connection = conn; |
1069 | 495 | r->server = conn->base_server; |
1070 | | |
1071 | 495 | r->user = NULL; |
1072 | 495 | r->ap_auth_type = NULL; |
1073 | | |
1074 | 495 | r->allowed_methods = ap_make_method_list(p, 2); |
1075 | | |
1076 | 495 | r->headers_in = apr_table_make(r->pool, 25); |
1077 | 495 | r->trailers_in = apr_table_make(r->pool, 5); |
1078 | 495 | r->subprocess_env = apr_table_make(r->pool, 25); |
1079 | 495 | r->headers_out = apr_table_make(r->pool, 12); |
1080 | 495 | r->err_headers_out = apr_table_make(r->pool, 5); |
1081 | 495 | r->trailers_out = apr_table_make(r->pool, 5); |
1082 | 495 | r->notes = apr_table_make(r->pool, 5); |
1083 | | |
1084 | 495 | r->request_config = ap_create_request_config(r->pool); |
1085 | | /* Must be set before we run create request hook */ |
1086 | | |
1087 | 495 | r->proto_output_filters = conn->output_filters; |
1088 | 495 | r->output_filters = r->proto_output_filters; |
1089 | 495 | r->proto_input_filters = conn->input_filters; |
1090 | 495 | r->input_filters = r->proto_input_filters; |
1091 | 495 | ap_run_create_request(r); |
1092 | 495 | r->per_dir_config = r->server->lookup_defaults; |
1093 | | |
1094 | 495 | r->sent_bodyct = 0; /* bytect isn't for body */ |
1095 | | |
1096 | 495 | r->read_length = 0; |
1097 | 495 | r->read_body = REQUEST_NO_BODY; |
1098 | | |
1099 | 495 | r->status = HTTP_OK; /* Until further notice */ |
1100 | 495 | r->header_only = 0; |
1101 | 495 | r->the_request = NULL; |
1102 | | |
1103 | | /* Begin by presuming any module can make its own path_info assumptions, |
1104 | | * until some module interjects and changes the value. |
1105 | | */ |
1106 | 495 | r->used_path_info = AP_REQ_DEFAULT_PATH_INFO; |
1107 | | |
1108 | 495 | r->useragent_addr = conn->client_addr; |
1109 | 495 | r->useragent_ip = conn->client_ip; |
1110 | | |
1111 | 495 | return r; |
1112 | 495 | } |
1113 | | |
1114 | | /* Apply the server's timeout/config to the connection/request. */ |
1115 | | static void apply_server_config(request_rec *r) |
1116 | 0 | { |
1117 | 0 | apr_socket_t *csd; |
1118 | |
|
1119 | 0 | csd = ap_get_conn_socket(r->connection); |
1120 | 0 | apr_socket_timeout_set(csd, r->server->timeout); |
1121 | |
|
1122 | 0 | r->per_dir_config = r->server->lookup_defaults; |
1123 | 0 | } |
1124 | | |
1125 | | typedef enum { |
1126 | | http_error_none, |
1127 | | http_error_badprotocol, |
1128 | | http_error_reject09, |
1129 | | http_error_badmethod09, |
1130 | | } http_error; |
1131 | | |
1132 | | static http_error r_assign_protocol(request_rec *r, |
1133 | | const char *protocol, |
1134 | | int strict) |
1135 | 347 | { |
1136 | 347 | int proto_num; |
1137 | 347 | http_error error = http_error_none; |
1138 | | |
1139 | 347 | if (protocol[0] == 'H' && protocol[1] == 'T' |
1140 | 347 | && protocol[2] == 'T' && protocol[3] == 'P' |
1141 | 347 | && protocol[4] == '/' && apr_isdigit(protocol[5]) |
1142 | 347 | && protocol[6] == '.' && apr_isdigit(protocol[7]) |
1143 | 347 | && !protocol[8] && protocol[5] != '0') { |
1144 | 9 | r->assbackwards = 0; |
1145 | 9 | proto_num = HTTP_VERSION(protocol[5] - '0', protocol[7] - '0'); |
1146 | 9 | } |
1147 | 338 | else if ((protocol[0] == 'H' || protocol[0] == 'h') |
1148 | 338 | && (protocol[1] == 'T' || protocol[1] == 't') |
1149 | 338 | && (protocol[2] == 'T' || protocol[2] == 't') |
1150 | 338 | && (protocol[3] == 'P' || protocol[3] == 'p') |
1151 | 338 | && protocol[4] == '/' && apr_isdigit(protocol[5]) |
1152 | 338 | && protocol[6] == '.' && apr_isdigit(protocol[7]) |
1153 | 338 | && !protocol[8] && protocol[5] != '0') { |
1154 | 15 | r->assbackwards = 0; |
1155 | 15 | proto_num = HTTP_VERSION(protocol[5] - '0', protocol[7] - '0'); |
1156 | 15 | if (strict && error == http_error_none) |
1157 | 11 | error = http_error_badprotocol; |
1158 | 4 | else |
1159 | 4 | protocol = apr_psprintf(r->pool, "HTTP/%d.%d", HTTP_VERSION_MAJOR(proto_num), |
1160 | 4 | HTTP_VERSION_MINOR(proto_num)); |
1161 | 15 | } |
1162 | 323 | else if (protocol[0]) { |
1163 | 188 | proto_num = HTTP_VERSION(0, 9); |
1164 | 188 | if (error == http_error_none) |
1165 | 188 | error = http_error_badprotocol; |
1166 | 188 | } |
1167 | 135 | else { |
1168 | 135 | r->assbackwards = 1; |
1169 | 135 | protocol = "HTTP/0.9"; |
1170 | 135 | proto_num = HTTP_VERSION(0, 9); |
1171 | 135 | } |
1172 | 347 | r->protocol = protocol; |
1173 | 347 | r->proto_num = proto_num; |
1174 | 347 | return error; |
1175 | 347 | } |
1176 | | |
1177 | | AP_DECLARE(int) ap_assign_request_line(request_rec *r, |
1178 | | const char *method, const char *uri, |
1179 | | const char *protocol) |
1180 | 347 | { |
1181 | 347 | core_server_config *conf = ap_get_core_module_config(r->server->module_config); |
1182 | 347 | int strict = (conf->http_conformance != AP_HTTP_CONFORMANCE_UNSAFE); |
1183 | 347 | http_error error = r_assign_protocol(r, protocol, strict); |
1184 | | |
1185 | 347 | ap_log_rerror(APLOG_MARK, APLOG_TRACE2, 0, r, |
1186 | 347 | "assigned protocol: %s, error=%d", r->protocol, error); |
1187 | | |
1188 | | /* Determine the method_number and parse the uri prior to invoking error |
1189 | | * handling, such that these fields are available for substitution |
1190 | | */ |
1191 | 347 | r->method = method; |
1192 | 347 | r->method_number = ap_method_number_of(r->method); |
1193 | 347 | if (r->method_number == M_GET && r->method[0] == 'H') |
1194 | 2 | r->header_only = 1; |
1195 | | |
1196 | | /* For internal integrity and palloc efficiency, reconstruct the_request |
1197 | | * in one palloc, using only single SP characters, per spec. |
1198 | | */ |
1199 | 347 | r->the_request = apr_pstrcat(r->pool, r->method, *uri ? " " : NULL, uri, |
1200 | 347 | *r->protocol ? " " : NULL, r->protocol, NULL); |
1201 | 347 | ap_log_rerror(APLOG_MARK, APLOG_TRACE2, 0, r, |
1202 | 347 | "assigned request_line: %s, error=%d", r->the_request, error); |
1203 | | |
1204 | 347 | ap_parse_uri(r, uri); |
1205 | 347 | if (r->status == HTTP_OK |
1206 | 347 | && (r->parsed_uri.path != NULL) |
1207 | 347 | && (r->parsed_uri.path[0] != '/') |
1208 | 347 | && (r->method_number != M_OPTIONS |
1209 | 74 | || strcmp(r->parsed_uri.path, "*") != 0)) { |
1210 | | /* Invalid request-target per RFC 7230 section 5.3 */ |
1211 | 73 | r->status = HTTP_BAD_REQUEST; |
1212 | 73 | } |
1213 | | |
1214 | 347 | ap_log_rerror(APLOG_MARK, APLOG_TRACE2, 0, r, |
1215 | 347 | "parsed uri: r->status=%d, error=%d", r->status, error); |
1216 | | /* With the request understood, we can consider HTTP/0.9 specific errors */ |
1217 | 347 | if (r->proto_num == HTTP_VERSION(0, 9) && error == http_error_none) { |
1218 | 135 | if (conf->http09_enable == AP_HTTP09_DISABLE) |
1219 | 1 | error = http_error_reject09; |
1220 | 134 | else if (strict && (r->method_number != M_GET || r->header_only)) |
1221 | 118 | error = http_error_badmethod09; |
1222 | 135 | } |
1223 | | |
1224 | | /* Now that the method, uri and protocol are all processed, |
1225 | | * we can safely resume any deferred error reporting |
1226 | | */ |
1227 | 347 | if (error != http_error_none) { |
1228 | 318 | switch (error) { |
1229 | 199 | case http_error_badprotocol: |
1230 | 199 | ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(10388) |
1231 | 199 | "HTTP Request: Unrecognized protocol '%.*s' " |
1232 | 199 | "(perhaps whitespace was injected?)", |
1233 | 199 | field_name_len(r->protocol), r->protocol); |
1234 | 199 | break; |
1235 | 1 | case http_error_reject09: |
1236 | 1 | ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(02401) |
1237 | 1 | "HTTP Request: Rejected HTTP/0.9 request"); |
1238 | 1 | break; |
1239 | 118 | case http_error_badmethod09: |
1240 | 118 | ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(03444) |
1241 | 118 | "HTTP Request: Invalid method token: '%.*s'" |
1242 | 118 | " (only GET is allowed for HTTP/0.9 requests)", |
1243 | 118 | field_name_len(r->method), r->method); |
1244 | 118 | break; |
1245 | 0 | default: |
1246 | 0 | break; |
1247 | 318 | } |
1248 | 318 | r->status = HTTP_BAD_REQUEST; |
1249 | 318 | goto failed; |
1250 | 318 | } |
1251 | | |
1252 | 29 | if (conf->http_methods == AP_HTTP_METHODS_REGISTERED |
1253 | 29 | && r->method_number == M_INVALID) { |
1254 | 1 | ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(02423) |
1255 | 1 | "HTTP Request Line; Unrecognized HTTP method: '%.*s' " |
1256 | 1 | "(disallowed by RegisteredMethods)", |
1257 | 1 | field_name_len(r->method), r->method); |
1258 | 1 | r->status = HTTP_NOT_IMPLEMENTED; |
1259 | | /* This can't happen in an HTTP/0.9 request, we verified GET above */ |
1260 | 1 | goto failed; |
1261 | 1 | } |
1262 | | |
1263 | 28 | if (r->status != HTTP_OK) { |
1264 | 15 | ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(03450) |
1265 | 15 | "HTTP Request Line; Unable to parse URI: '%.*s'", |
1266 | 15 | field_name_len(r->uri), r->uri); |
1267 | 15 | goto failed; |
1268 | 15 | } |
1269 | | |
1270 | 13 | if (strict) { |
1271 | 9 | if (r->parsed_uri.fragment) { |
1272 | | /* RFC3986 3.5: no fragment */ |
1273 | 2 | ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(02421) |
1274 | 2 | "HTTP Request Line; URI must not contain a fragment"); |
1275 | 2 | r->status = HTTP_BAD_REQUEST; |
1276 | 2 | goto failed; |
1277 | 2 | } |
1278 | 7 | if (r->parsed_uri.user || r->parsed_uri.password) { |
1279 | 3 | ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(02422) |
1280 | 3 | "HTTP Request Line; URI must not contain a " |
1281 | 3 | "username/password"); |
1282 | 3 | r->status = HTTP_BAD_REQUEST; |
1283 | 3 | goto failed; |
1284 | 3 | } |
1285 | 7 | } |
1286 | 8 | return 1; |
1287 | | |
1288 | 339 | failed: |
1289 | 339 | if (error != http_error_none && r->proto_num == HTTP_VERSION(0, 9)) { |
1290 | | /* Send all parsing and protocol error response with 1.x behavior, |
1291 | | * and reserve 505 errors for actual HTTP protocols presented. |
1292 | | * As called out in RFC7230 3.5, any errors parsing the protocol |
1293 | | * from the request line are nearly always misencoded HTTP/1.x |
1294 | | * requests. Only a valid 0.9 request with no parsing errors |
1295 | | * at all may be treated as a simple request, if allowed. |
1296 | | */ |
1297 | 307 | r->assbackwards = 0; |
1298 | 307 | r->connection->keepalive = AP_CONN_CLOSE; |
1299 | 307 | r->proto_num = HTTP_VERSION(1, 0); |
1300 | 307 | r->protocol = "HTTP/1.0"; |
1301 | 307 | } |
1302 | 339 | return 0; |
1303 | | |
1304 | 13 | } |
1305 | | |
1306 | | AP_DECLARE(request_rec *) ap_read_request(conn_rec *conn) |
1307 | 0 | { |
1308 | 0 | int access_status; |
1309 | 0 | apr_bucket_brigade *tmp_bb; |
1310 | 0 | apr_bucket *e, *bdata = NULL, *berr = NULL; |
1311 | 0 | ap_bucket_request *breq = NULL; |
1312 | 0 | const char *method, *uri, *protocol; |
1313 | 0 | apr_table_t *headers; |
1314 | 0 | apr_status_t rv; |
1315 | |
|
1316 | 0 | request_rec *r = ap_create_request(conn); |
1317 | |
|
1318 | 0 | tmp_bb = apr_brigade_create(r->pool, r->connection->bucket_alloc); |
1319 | 0 | conn->keepalive = AP_CONN_UNKNOWN; |
1320 | |
|
1321 | 0 | ap_run_pre_read_request(r, conn); |
1322 | |
|
1323 | 0 | r->request_time = apr_time_now(); |
1324 | 0 | rv = ap_get_brigade(r->proto_input_filters, tmp_bb, AP_MODE_READBYTES, APR_BLOCK_READ, 0); |
1325 | 0 | if (rv != APR_SUCCESS || APR_BRIGADE_EMPTY(tmp_bb)) { |
1326 | | /* Not worth dying with. */ |
1327 | 0 | conn->keepalive = AP_CONN_CLOSE; |
1328 | 0 | apr_pool_destroy(r->pool); |
1329 | 0 | goto ignore; |
1330 | 0 | } |
1331 | | |
1332 | 0 | for (e = APR_BRIGADE_FIRST(tmp_bb); |
1333 | 0 | e != APR_BRIGADE_SENTINEL(tmp_bb); |
1334 | 0 | e = APR_BUCKET_NEXT(e)) |
1335 | 0 | { |
1336 | 0 | if (AP_BUCKET_IS_REQUEST(e)) { |
1337 | 0 | if (!breq) breq = e->data; |
1338 | 0 | } |
1339 | 0 | else if (AP_BUCKET_IS_ERROR(e)) { |
1340 | 0 | if (!berr) berr = e; |
1341 | 0 | } |
1342 | 0 | else if (!APR_BUCKET_IS_METADATA(e) && e->length != 0) { |
1343 | 0 | if (!bdata) bdata = e; |
1344 | 0 | break; |
1345 | 0 | } |
1346 | 0 | } |
1347 | |
|
1348 | 0 | if (!breq && !berr) { |
1349 | 0 | ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(10389) |
1350 | 0 | "request failed: neither request bucket nor error at start of input"); |
1351 | 0 | access_status = HTTP_INTERNAL_SERVER_ERROR; |
1352 | 0 | goto die_unusable_input; |
1353 | 0 | } |
1354 | | |
1355 | 0 | if (breq) { |
1356 | | /* If there is a request, we always process it, as it defines |
1357 | | * the context in which a potential error bucket is handled. */ |
1358 | 0 | if (apr_pool_is_ancestor(r->pool, breq->pool)) { |
1359 | 0 | method = breq->method; |
1360 | 0 | uri = breq->uri; |
1361 | 0 | protocol = breq->protocol; |
1362 | 0 | headers = breq->headers; |
1363 | 0 | } |
1364 | 0 | else { |
1365 | 0 | method = apr_pstrdup(r->pool, breq->method); |
1366 | 0 | uri = apr_pstrdup(r->pool, breq->uri); |
1367 | 0 | protocol = apr_pstrdup(r->pool, breq->protocol); |
1368 | 0 | headers = breq->headers? apr_table_clone(r->pool, breq->headers) : NULL; |
1369 | 0 | } |
1370 | |
|
1371 | 0 | if (!method || !uri || !protocol) { |
1372 | 0 | access_status = berr? ((ap_bucket_error *)(berr->data))->status : |
1373 | 0 | HTTP_INTERNAL_SERVER_ERROR; |
1374 | 0 | goto die_unusable_input; |
1375 | 0 | } |
1376 | | |
1377 | 0 | if (headers) { |
1378 | 0 | r->headers_in = headers; |
1379 | 0 | } |
1380 | |
|
1381 | 0 | ap_log_rerror(APLOG_MARK, APLOG_TRACE2, 0, r, |
1382 | 0 | "checking request: %s %s %s", |
1383 | 0 | method, uri, protocol); |
1384 | |
|
1385 | 0 | if (!ap_assign_request_line(r, method, uri, protocol)) { |
1386 | 0 | apr_brigade_cleanup(tmp_bb); |
1387 | 0 | switch (r->status) { |
1388 | 0 | case HTTP_REQUEST_URI_TOO_LARGE: |
1389 | 0 | case HTTP_BAD_REQUEST: |
1390 | 0 | case HTTP_VERSION_NOT_SUPPORTED: |
1391 | 0 | case HTTP_NOT_IMPLEMENTED: |
1392 | 0 | if (r->status == HTTP_REQUEST_URI_TOO_LARGE) { |
1393 | 0 | ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(00565) |
1394 | 0 | "request failed: client's request-line exceeds LimitRequestLine (longer than %d)", |
1395 | 0 | r->server->limit_req_line); |
1396 | 0 | } |
1397 | 0 | else if (!strcmp("-", r->method)) { |
1398 | 0 | ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(00566) |
1399 | 0 | "request failed: malformed request line"); |
1400 | 0 | } |
1401 | 0 | access_status = r->status; |
1402 | 0 | goto die_unusable_input; |
1403 | | |
1404 | 0 | case HTTP_REQUEST_TIME_OUT: |
1405 | | /* Just log, no further action on this connection. */ |
1406 | 0 | ap_update_child_status(conn->sbh, SERVER_BUSY_LOG, NULL); |
1407 | 0 | if (!r->connection->keepalives) |
1408 | 0 | ap_run_log_transaction(r); |
1409 | 0 | break; |
1410 | 0 | } |
1411 | | /* Not worth dying with. */ |
1412 | 0 | conn->keepalive = AP_CONN_CLOSE; |
1413 | 0 | apr_pool_destroy(r->pool); |
1414 | 0 | goto ignore; |
1415 | 0 | } |
1416 | 0 | } |
1417 | | |
1418 | 0 | if (berr) { |
1419 | 0 | access_status = ((ap_bucket_error *)(berr->data))->status; |
1420 | 0 | goto die_unusable_input; |
1421 | 0 | } |
1422 | 0 | else if (bdata) { |
1423 | | /* Since processing of a request body depends on knowing the request, we |
1424 | | * cannot handle any data here. For example, chunked-encoding filters are |
1425 | | * added after the request is read, so any data buckets here will not |
1426 | | * have been de-chunked. |
1427 | | */ |
1428 | 0 | ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(10391) |
1429 | 0 | "request failed: seeing DATA bucket(len=%d) of request " |
1430 | 0 | "body, too early to process", (int)bdata->length); |
1431 | 0 | access_status = HTTP_INTERNAL_SERVER_ERROR; |
1432 | 0 | goto die_unusable_input; |
1433 | 0 | } |
1434 | | |
1435 | 0 | apr_brigade_cleanup(tmp_bb); |
1436 | | |
1437 | | /* We may have been in keep_alive_timeout mode, so toggle back |
1438 | | * to the normal timeout mode as we fetch the header lines, |
1439 | | * as necessary. |
1440 | | */ |
1441 | 0 | apply_server_config(r); |
1442 | |
|
1443 | 0 | if (!r->assbackwards) { |
1444 | 0 | const char *clen = apr_table_get(r->headers_in, "Content-Length"); |
1445 | 0 | if (clen) { |
1446 | 0 | apr_off_t cl; |
1447 | |
|
1448 | 0 | if (!ap_parse_strict_length(&cl, clen)) { |
1449 | 0 | ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(10242) |
1450 | 0 | "client sent invalid Content-Length " |
1451 | 0 | "(%s): %s", clen, r->uri); |
1452 | 0 | access_status = HTTP_BAD_REQUEST; |
1453 | 0 | goto die_unusable_input; |
1454 | 0 | } |
1455 | 0 | } |
1456 | 0 | } |
1457 | | |
1458 | | /* |
1459 | | * Add HTTP_IN here to ensure that content and HEADERS buckets are processed |
1460 | | * accordingly, e.g. populating r->trailers_in for example. |
1461 | | */ |
1462 | 0 | ap_add_input_filter_handle(ap_http_input_filter_handle, |
1463 | 0 | NULL, r, r->connection); |
1464 | | |
1465 | | /* Validate Host/Expect headers and select vhost. */ |
1466 | 0 | if (!ap_check_request_header(r)) { |
1467 | | /* we may have switched to another server still */ |
1468 | 0 | apply_server_config(r); |
1469 | 0 | access_status = r->status; |
1470 | 0 | goto die_before_hooks; |
1471 | 0 | } |
1472 | | |
1473 | | /* we may have switched to another server */ |
1474 | 0 | apply_server_config(r); |
1475 | |
|
1476 | 0 | if ((access_status = ap_post_read_request(r))) { |
1477 | 0 | goto die; |
1478 | 0 | } |
1479 | | |
1480 | 0 | AP_READ_REQUEST_SUCCESS((uintptr_t)r, (char *)r->method, |
1481 | 0 | (char *)r->uri, (char *)r->server->defn_name, |
1482 | 0 | r->status); |
1483 | 0 | return r; |
1484 | | |
1485 | | /* Everything falls through on failure */ |
1486 | | |
1487 | 0 | die_unusable_input: |
1488 | | /* Input filters are in an undeterminate state, cleanup (including |
1489 | | * CORE_IN's socket) such that any further attempt to read is EOF. |
1490 | | */ |
1491 | 0 | { |
1492 | 0 | ap_filter_t *f = conn->input_filters; |
1493 | 0 | while (f) { |
1494 | 0 | ap_filter_reinstate_brigade(f, tmp_bb, NULL); |
1495 | 0 | apr_brigade_cleanup(tmp_bb); |
1496 | 0 | if (f->frec == ap_core_input_filter_handle) { |
1497 | 0 | break; |
1498 | 0 | } |
1499 | 0 | ap_remove_input_filter(f); |
1500 | 0 | f = f->next; |
1501 | 0 | } |
1502 | 0 | conn->input_filters = r->input_filters = f; |
1503 | 0 | conn->keepalive = AP_CONN_CLOSE; |
1504 | 0 | } |
1505 | |
|
1506 | 0 | die_before_hooks: |
1507 | | /* First call to ap_die() (non recursive) */ |
1508 | 0 | r->status = HTTP_OK; |
1509 | |
|
1510 | 0 | die: |
1511 | 0 | ap_die(access_status, r); |
1512 | | |
1513 | | /* ap_die() sent the response through the output filters, we must now |
1514 | | * end the request with an EOR bucket for stream/pipeline accounting. |
1515 | | */ |
1516 | 0 | { |
1517 | 0 | apr_bucket_brigade *eor_bb; |
1518 | 0 | eor_bb = ap_acquire_brigade(conn); |
1519 | 0 | APR_BRIGADE_INSERT_TAIL(eor_bb, |
1520 | 0 | ap_bucket_eor_create(conn->bucket_alloc, r)); |
1521 | 0 | ap_pass_brigade(conn->output_filters, eor_bb); |
1522 | 0 | ap_release_brigade(conn, eor_bb); |
1523 | 0 | } |
1524 | |
|
1525 | 0 | ignore: |
1526 | 0 | r = NULL; |
1527 | 0 | AP_READ_REQUEST_FAILURE((uintptr_t)r); |
1528 | 0 | return NULL; |
1529 | 0 | } |
1530 | | |
1531 | | AP_DECLARE(int) ap_post_read_request(request_rec *r) |
1532 | 0 | { |
1533 | 0 | int status; |
1534 | |
|
1535 | 0 | if ((status = ap_run_post_read_request(r))) { |
1536 | 0 | return status; |
1537 | 0 | } |
1538 | | |
1539 | | /* Enforce http(s) only scheme for non-forward-proxy requests */ |
1540 | 0 | if (!r->proxyreq |
1541 | 0 | && r->parsed_uri.scheme |
1542 | 0 | && (ap_cstr_casecmpn(r->parsed_uri.scheme, "http", 4) != 0 |
1543 | 0 | || (r->parsed_uri.scheme[4] != '\0' |
1544 | 0 | && (apr_tolower(r->parsed_uri.scheme[4]) != 's' |
1545 | 0 | || r->parsed_uri.scheme[5] != '\0')))) { |
1546 | 0 | return HTTP_BAD_REQUEST; |
1547 | 0 | } |
1548 | | |
1549 | 0 | return OK; |
1550 | 0 | } |
1551 | | |
1552 | | /* if a request with a body creates a subrequest, remove original request's |
1553 | | * input headers which pertain to the body which has already been read. |
1554 | | * out-of-line helper function for ap_set_sub_req_protocol. |
1555 | | */ |
1556 | | |
1557 | | static void strip_headers_request_body(request_rec *rnew) |
1558 | 0 | { |
1559 | 0 | apr_table_unset(rnew->headers_in, "Content-Encoding"); |
1560 | 0 | apr_table_unset(rnew->headers_in, "Content-Language"); |
1561 | 0 | apr_table_unset(rnew->headers_in, "Content-Length"); |
1562 | 0 | apr_table_unset(rnew->headers_in, "Content-Location"); |
1563 | 0 | apr_table_unset(rnew->headers_in, "Content-MD5"); |
1564 | 0 | apr_table_unset(rnew->headers_in, "Content-Range"); |
1565 | 0 | apr_table_unset(rnew->headers_in, "Content-Type"); |
1566 | 0 | apr_table_unset(rnew->headers_in, "Expires"); |
1567 | 0 | apr_table_unset(rnew->headers_in, "Last-Modified"); |
1568 | 0 | apr_table_unset(rnew->headers_in, "Transfer-Encoding"); |
1569 | 0 | } |
1570 | | |
1571 | | /* |
1572 | | * A couple of other functions which initialize some of the fields of |
1573 | | * a request structure, as appropriate for adjuncts of one kind or another |
1574 | | * to a request in progress. Best here, rather than elsewhere, since |
1575 | | * *someone* has to set the protocol-specific fields... |
1576 | | */ |
1577 | | |
1578 | | AP_DECLARE(void) ap_set_sub_req_protocol(request_rec *rnew, |
1579 | | const request_rec *r) |
1580 | 0 | { |
1581 | 0 | rnew->the_request = r->the_request; /* Keep original request-line */ |
1582 | |
|
1583 | 0 | rnew->assbackwards = 1; /* Don't send headers from this. */ |
1584 | 0 | rnew->no_local_copy = 1; /* Don't try to send HTTP_NOT_MODIFIED for a |
1585 | | * fragment. */ |
1586 | 0 | rnew->method = "GET"; |
1587 | 0 | rnew->method_number = M_GET; |
1588 | 0 | rnew->protocol = "INCLUDED"; |
1589 | |
|
1590 | 0 | rnew->status = HTTP_OK; |
1591 | |
|
1592 | 0 | rnew->headers_in = apr_table_copy(rnew->pool, r->headers_in); |
1593 | 0 | rnew->trailers_in = apr_table_copy(rnew->pool, r->trailers_in); |
1594 | | |
1595 | | /* did the original request have a body? (e.g. POST w/SSI tags) |
1596 | | * if so, make sure the subrequest doesn't inherit body headers |
1597 | | */ |
1598 | 0 | if (!r->kept_body && (apr_table_get(r->headers_in, "Content-Length") |
1599 | 0 | || apr_table_get(r->headers_in, "Transfer-Encoding"))) { |
1600 | 0 | strip_headers_request_body(rnew); |
1601 | 0 | } |
1602 | 0 | rnew->subprocess_env = apr_table_copy(rnew->pool, r->subprocess_env); |
1603 | 0 | rnew->headers_out = apr_table_make(rnew->pool, 5); |
1604 | 0 | rnew->err_headers_out = apr_table_make(rnew->pool, 5); |
1605 | 0 | rnew->trailers_out = apr_table_make(rnew->pool, 5); |
1606 | 0 | rnew->notes = apr_table_make(rnew->pool, 5); |
1607 | |
|
1608 | 0 | rnew->expecting_100 = r->expecting_100; |
1609 | 0 | rnew->read_length = r->read_length; |
1610 | 0 | rnew->read_body = REQUEST_NO_BODY; |
1611 | |
|
1612 | 0 | rnew->main = (request_rec *) r; |
1613 | 0 | } |
1614 | | |
1615 | | static void end_output_stream(request_rec *r, int status) |
1616 | 0 | { |
1617 | 0 | conn_rec *c = r->connection; |
1618 | 0 | apr_bucket_brigade *bb; |
1619 | 0 | apr_bucket *b; |
1620 | |
|
1621 | 0 | bb = apr_brigade_create(r->pool, c->bucket_alloc); |
1622 | 0 | if (status != OK) { |
1623 | 0 | b = ap_bucket_error_create(status, NULL, r->pool, c->bucket_alloc); |
1624 | 0 | APR_BRIGADE_INSERT_TAIL(bb, b); |
1625 | 0 | } |
1626 | 0 | b = apr_bucket_eos_create(c->bucket_alloc); |
1627 | 0 | APR_BRIGADE_INSERT_TAIL(bb, b); |
1628 | |
|
1629 | 0 | ap_pass_brigade(r->output_filters, bb); |
1630 | 0 | apr_brigade_cleanup(bb); |
1631 | 0 | } |
1632 | | |
1633 | | AP_DECLARE(void) ap_finalize_sub_req_protocol(request_rec *sub) |
1634 | 0 | { |
1635 | | /* tell the filter chain there is no more content coming */ |
1636 | 0 | if (!sub->eos_sent) { |
1637 | 0 | end_output_stream(sub, OK); |
1638 | 0 | } |
1639 | 0 | } |
1640 | | |
1641 | | /* finalize_request_protocol is called at completion of sending the |
1642 | | * response. Its sole purpose is to send the terminating protocol |
1643 | | * information for any wrappers around the response message body |
1644 | | * (i.e., transfer encodings). It should have been named finalize_response. |
1645 | | */ |
1646 | | AP_DECLARE(void) ap_finalize_request_protocol(request_rec *r) |
1647 | 0 | { |
1648 | 0 | int status = ap_discard_request_body(r); |
1649 | | |
1650 | | /* tell the filter chain there is no more content coming */ |
1651 | 0 | if (!r->eos_sent) { |
1652 | 0 | end_output_stream(r, status); |
1653 | 0 | } |
1654 | 0 | } |
1655 | | |
1656 | | /* |
1657 | | * Support for the Basic authentication protocol, and a bit for Digest. |
1658 | | */ |
1659 | | AP_DECLARE(void) ap_note_auth_failure(request_rec *r) |
1660 | 0 | { |
1661 | 0 | const char *type = ap_auth_type(r); |
1662 | 0 | if (type) { |
1663 | 0 | ap_run_note_auth_failure(r, type); |
1664 | 0 | } |
1665 | 0 | else { |
1666 | 0 | ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(00571) |
1667 | 0 | "need AuthType to note auth failure: %s", r->uri); |
1668 | 0 | } |
1669 | 0 | } |
1670 | | |
1671 | | AP_DECLARE(void) ap_note_basic_auth_failure(request_rec *r) |
1672 | 0 | { |
1673 | 0 | ap_note_auth_failure(r); |
1674 | 0 | } |
1675 | | |
1676 | | AP_DECLARE(void) ap_note_digest_auth_failure(request_rec *r) |
1677 | 0 | { |
1678 | 0 | ap_note_auth_failure(r); |
1679 | 0 | } |
1680 | | |
1681 | | AP_DECLARE(int) ap_get_basic_auth_pw(request_rec *r, const char **pw) |
1682 | 0 | { |
1683 | 0 | const char *t, *auth_line; |
1684 | |
|
1685 | 0 | if (!(t = ap_auth_type(r)) || ap_cstr_casecmp(t, "Basic")) |
1686 | 0 | return DECLINED; |
1687 | | |
1688 | 0 | if (!ap_auth_name(r)) { |
1689 | 0 | ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(00572) |
1690 | 0 | "need AuthName: %s", r->uri); |
1691 | 0 | return HTTP_INTERNAL_SERVER_ERROR; |
1692 | 0 | } |
1693 | | |
1694 | 0 | auth_line = apr_table_get(r->headers_in, |
1695 | 0 | (PROXYREQ_PROXY == r->proxyreq) |
1696 | 0 | ? "Proxy-Authorization" : "Authorization"); |
1697 | |
|
1698 | 0 | if (!auth_line) { |
1699 | 0 | ap_note_auth_failure(r); |
1700 | 0 | return HTTP_UNAUTHORIZED; |
1701 | 0 | } |
1702 | | |
1703 | 0 | if (ap_cstr_casecmp(ap_getword(r->pool, &auth_line, ' '), "Basic")) { |
1704 | | /* Client tried to authenticate using wrong auth scheme */ |
1705 | 0 | ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(00573) |
1706 | 0 | "client used wrong authentication scheme: %s", r->uri); |
1707 | 0 | ap_note_auth_failure(r); |
1708 | 0 | return HTTP_UNAUTHORIZED; |
1709 | 0 | } |
1710 | | |
1711 | 0 | while (*auth_line == ' ' || *auth_line == '\t') { |
1712 | 0 | auth_line++; |
1713 | 0 | } |
1714 | |
|
1715 | 0 | t = ap_pbase64decode(r->pool, auth_line); |
1716 | 0 | r->user = ap_getword_nulls (r->pool, &t, ':'); |
1717 | 0 | apr_table_setn(r->notes, AP_GET_BASIC_AUTH_PW_NOTE, "1"); |
1718 | 0 | r->ap_auth_type = "Basic"; |
1719 | |
|
1720 | 0 | *pw = t; |
1721 | |
|
1722 | 0 | return OK; |
1723 | 0 | } |
1724 | | |
1725 | | AP_DECLARE(apr_status_t) ap_get_basic_auth_components(const request_rec *r, |
1726 | | const char **username, |
1727 | | const char **password) |
1728 | 0 | { |
1729 | 0 | const char *auth_header; |
1730 | 0 | const char *credentials; |
1731 | 0 | const char *decoded; |
1732 | 0 | const char *user; |
1733 | |
|
1734 | 0 | auth_header = (PROXYREQ_PROXY == r->proxyreq) ? "Proxy-Authorization" |
1735 | 0 | : "Authorization"; |
1736 | 0 | credentials = apr_table_get(r->headers_in, auth_header); |
1737 | |
|
1738 | 0 | if (!credentials) { |
1739 | | /* No auth header. */ |
1740 | 0 | return APR_EINVAL; |
1741 | 0 | } |
1742 | | |
1743 | 0 | if (ap_cstr_casecmp(ap_getword(r->pool, &credentials, ' '), "Basic")) { |
1744 | | /* These aren't Basic credentials. */ |
1745 | 0 | return APR_EINVAL; |
1746 | 0 | } |
1747 | | |
1748 | 0 | while (*credentials == ' ' || *credentials == '\t') { |
1749 | 0 | credentials++; |
1750 | 0 | } |
1751 | | |
1752 | | /* XXX Our base64 decoding functions don't actually error out if the string |
1753 | | * we give it isn't base64; they'll just silently stop and hand us whatever |
1754 | | * they've parsed up to that point. |
1755 | | * |
1756 | | * Since this function is supposed to be a drop-in replacement for the |
1757 | | * deprecated ap_get_basic_auth_pw(), don't fix this for 2.4.x. |
1758 | | */ |
1759 | 0 | decoded = ap_pbase64decode(r->pool, credentials); |
1760 | 0 | user = ap_getword_nulls(r->pool, &decoded, ':'); |
1761 | |
|
1762 | 0 | if (username) { |
1763 | 0 | *username = user; |
1764 | 0 | } |
1765 | 0 | if (password) { |
1766 | 0 | *password = decoded; |
1767 | 0 | } |
1768 | |
|
1769 | 0 | return APR_SUCCESS; |
1770 | 0 | } |
1771 | | |
1772 | | struct content_length_ctx { |
1773 | | int data_sent; /* true if the C-L filter has already sent at |
1774 | | * least one bucket on to the next output filter |
1775 | | * for this request |
1776 | | */ |
1777 | | apr_bucket_brigade *tmpbb; |
1778 | | }; |
1779 | | |
1780 | | /* This filter computes the content length, but it also computes the number |
1781 | | * of bytes sent to the client. This means that this filter will always run |
1782 | | * through all of the buckets in all brigades |
1783 | | */ |
1784 | | AP_CORE_DECLARE_NONSTD(apr_status_t) ap_content_length_filter( |
1785 | | ap_filter_t *f, |
1786 | | apr_bucket_brigade *b) |
1787 | 0 | { |
1788 | 0 | request_rec *r = f->r; |
1789 | 0 | struct content_length_ctx *ctx; |
1790 | 0 | apr_bucket *e; |
1791 | 0 | int eos = 0; |
1792 | 0 | apr_read_type_e eblock = APR_NONBLOCK_READ; |
1793 | |
|
1794 | 0 | ctx = f->ctx; |
1795 | 0 | if (!ctx) { |
1796 | 0 | f->ctx = ctx = apr_palloc(r->pool, sizeof(*ctx)); |
1797 | 0 | ctx->data_sent = 0; |
1798 | 0 | ctx->tmpbb = apr_brigade_create(r->pool, r->connection->bucket_alloc); |
1799 | 0 | } |
1800 | | |
1801 | | /* Loop through the brigade to count the length. To avoid |
1802 | | * arbitrary memory consumption with morphing bucket types, this |
1803 | | * loop will stop and pass on the brigade when necessary. */ |
1804 | 0 | e = APR_BRIGADE_FIRST(b); |
1805 | 0 | while (e != APR_BRIGADE_SENTINEL(b)) { |
1806 | 0 | apr_status_t rv; |
1807 | |
|
1808 | 0 | if (APR_BUCKET_IS_EOS(e)) { |
1809 | 0 | eos = 1; |
1810 | 0 | break; |
1811 | 0 | } |
1812 | | /* For a flush bucket, fall through to pass the brigade and |
1813 | | * flush now. */ |
1814 | 0 | else if (APR_BUCKET_IS_FLUSH(e)) { |
1815 | 0 | e = APR_BUCKET_NEXT(e); |
1816 | 0 | } |
1817 | | /* For metadata bucket types other than FLUSH, loop. */ |
1818 | 0 | else if (APR_BUCKET_IS_METADATA(e)) { |
1819 | 0 | e = APR_BUCKET_NEXT(e); |
1820 | 0 | continue; |
1821 | 0 | } |
1822 | | /* For determinate length data buckets, count the length and |
1823 | | * continue. */ |
1824 | 0 | else if (e->length != (apr_size_t)-1) { |
1825 | 0 | r->bytes_sent += e->length; |
1826 | 0 | e = APR_BUCKET_NEXT(e); |
1827 | 0 | continue; |
1828 | 0 | } |
1829 | | /* For indeterminate length data buckets, perform one read. */ |
1830 | 0 | else /* e->length == (apr_size_t)-1 */ { |
1831 | 0 | apr_size_t len; |
1832 | 0 | const char *ignored; |
1833 | | |
1834 | 0 | rv = apr_bucket_read(e, &ignored, &len, eblock); |
1835 | 0 | if ((rv != APR_SUCCESS) && !APR_STATUS_IS_EAGAIN(rv)) { |
1836 | 0 | ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(00574) |
1837 | 0 | "ap_content_length_filter: " |
1838 | 0 | "apr_bucket_read() failed"); |
1839 | 0 | return rv; |
1840 | 0 | } |
1841 | 0 | if (rv == APR_SUCCESS) { |
1842 | 0 | eblock = APR_NONBLOCK_READ; |
1843 | 0 | e = APR_BUCKET_NEXT(e); |
1844 | 0 | r->bytes_sent += len; |
1845 | 0 | } |
1846 | 0 | else if (APR_STATUS_IS_EAGAIN(rv)) { |
1847 | 0 | apr_bucket *flush; |
1848 | | |
1849 | | /* Next read must block. */ |
1850 | 0 | eblock = APR_BLOCK_READ; |
1851 | | |
1852 | | /* Ensure the last bucket to pass down is a flush if |
1853 | | * the next read will block. */ |
1854 | 0 | flush = apr_bucket_flush_create(f->c->bucket_alloc); |
1855 | 0 | APR_BUCKET_INSERT_BEFORE(e, flush); |
1856 | 0 | } |
1857 | 0 | } |
1858 | | |
1859 | | /* Optimization: if the next bucket is EOS (directly after a |
1860 | | * bucket morphed to the heap, or a flush), short-cut to |
1861 | | * handle EOS straight away - allowing C-L to be determined |
1862 | | * for content which is already entirely in memory. */ |
1863 | 0 | if (e != APR_BRIGADE_SENTINEL(b) && APR_BUCKET_IS_EOS(e)) { |
1864 | 0 | continue; |
1865 | 0 | } |
1866 | | |
1867 | | /* On reaching here, pass on everything in the brigade up to |
1868 | | * this point. */ |
1869 | 0 | apr_brigade_split_ex(b, e, ctx->tmpbb); |
1870 | | |
1871 | 0 | rv = ap_pass_brigade(f->next, b); |
1872 | 0 | if (rv != APR_SUCCESS) { |
1873 | 0 | return rv; |
1874 | 0 | } |
1875 | 0 | else if (f->c->aborted) { |
1876 | 0 | return APR_ECONNABORTED; |
1877 | 0 | } |
1878 | 0 | apr_brigade_cleanup(b); |
1879 | 0 | APR_BRIGADE_CONCAT(b, ctx->tmpbb); |
1880 | 0 | e = APR_BRIGADE_FIRST(b); |
1881 | | |
1882 | 0 | ctx->data_sent = 1; |
1883 | 0 | } |
1884 | | |
1885 | | /* If we've now seen the entire response and it's otherwise |
1886 | | * okay to set the C-L in the response header, then do so now. |
1887 | | * |
1888 | | * We can only set a C-L in the response header if we haven't already |
1889 | | * sent any buckets on to the next output filter for this request. |
1890 | | */ |
1891 | 0 | if (ctx->data_sent == 0 && eos) { |
1892 | 0 | core_server_config *conf = |
1893 | 0 | ap_get_core_module_config(r->server->module_config); |
1894 | | |
1895 | | /* This is a hack, but I can't find anyway around it. The idea is that |
1896 | | * we don't want to send out 0 Content-Lengths if it is a HEAD request. |
1897 | | * [Unless the corresponding body (for a GET) would really be empty!] |
1898 | | * This happens when modules try to outsmart the server, and return |
1899 | | * if they see a HEAD request. Apache 1.3 handlers were supposed to |
1900 | | * just return in that situation, and the core handled the HEAD. From |
1901 | | * 2.0, if a handler returns, then the core sends an EOS bucket down |
1902 | | * the filter stack, and this content-length filter computes a length |
1903 | | * of zero and we would end up sending a zero C-L to the client. |
1904 | | * We can't just remove the this C-L filter, because well behaved 2.0+ |
1905 | | * handlers will send their data down the stack, and we will compute |
1906 | | * a real C-L for the head request. RBB |
1907 | | * |
1908 | | * Allow modification of this behavior through the |
1909 | | * HttpContentLengthHeadZero directive. |
1910 | | * |
1911 | | * The default (unset) behavior is to squelch the C-L in this case. |
1912 | | */ |
1913 | | |
1914 | | /* don't whack the C-L if it has already been set for a HEAD |
1915 | | * by something like proxy. the brigade only has an EOS bucket |
1916 | | * in this case, making r->bytes_sent zero, and either there is |
1917 | | * an existing C-L we want to preserve, or r->sent_bodyct is not |
1918 | | * zero (the empty body is being sent) thus we don't want to add |
1919 | | * a C-L of zero (the backend did not provide it, neither do we). |
1920 | | * |
1921 | | * if r->bytes_sent > 0 we have a (temporary) body whose length may |
1922 | | * have been changed by a filter. the C-L header might not have been |
1923 | | * updated so we do it here. long term it would be cleaner to have |
1924 | | * such filters update or remove the C-L header, and just use it |
1925 | | * if present. |
1926 | | */ |
1927 | 0 | if (!((r->header_only || AP_STATUS_IS_HEADER_ONLY(r->status)) |
1928 | 0 | && !r->bytes_sent |
1929 | 0 | && (r->sent_bodyct |
1930 | 0 | || conf->http_cl_head_zero != AP_HTTP_CL_HEAD_ZERO_ENABLE |
1931 | 0 | || apr_table_get(r->headers_out, "Content-Length")))) { |
1932 | 0 | ap_set_content_length(r, r->bytes_sent); |
1933 | 0 | } |
1934 | 0 | } |
1935 | |
|
1936 | 0 | ctx->data_sent = 1; |
1937 | 0 | return ap_pass_brigade(f->next, b); |
1938 | 0 | } |
1939 | | |
1940 | | /* |
1941 | | * Send the body of a response to the client. |
1942 | | */ |
1943 | | AP_DECLARE(apr_status_t) ap_send_fd(apr_file_t *fd, request_rec *r, |
1944 | | apr_off_t offset, apr_size_t len, |
1945 | | apr_size_t *nbytes) |
1946 | 0 | { |
1947 | 0 | conn_rec *c = r->connection; |
1948 | 0 | apr_bucket_brigade *bb = NULL; |
1949 | 0 | apr_status_t rv; |
1950 | |
|
1951 | 0 | bb = apr_brigade_create(r->pool, c->bucket_alloc); |
1952 | |
|
1953 | 0 | apr_brigade_insert_file(bb, fd, offset, len, r->pool); |
1954 | |
|
1955 | 0 | rv = ap_pass_brigade(r->output_filters, bb); |
1956 | 0 | if (rv != APR_SUCCESS) { |
1957 | 0 | *nbytes = 0; /* no way to tell how many were actually sent */ |
1958 | 0 | } |
1959 | 0 | else { |
1960 | 0 | *nbytes = len; |
1961 | 0 | } |
1962 | |
|
1963 | 0 | return rv; |
1964 | 0 | } |
1965 | | |
1966 | | #if APR_HAS_MMAP |
1967 | | /* send data from an in-memory buffer */ |
1968 | | AP_DECLARE(apr_size_t) ap_send_mmap(apr_mmap_t *mm, |
1969 | | request_rec *r, |
1970 | | apr_size_t offset, |
1971 | | apr_size_t length) |
1972 | 0 | { |
1973 | 0 | conn_rec *c = r->connection; |
1974 | 0 | apr_bucket_brigade *bb = NULL; |
1975 | 0 | apr_bucket *b; |
1976 | |
|
1977 | 0 | bb = apr_brigade_create(r->pool, c->bucket_alloc); |
1978 | 0 | b = apr_bucket_mmap_create(mm, offset, length, c->bucket_alloc); |
1979 | 0 | APR_BRIGADE_INSERT_TAIL(bb, b); |
1980 | 0 | ap_pass_brigade(r->output_filters, bb); |
1981 | |
|
1982 | 0 | return mm->size; /* XXX - change API to report apr_status_t? */ |
1983 | 0 | } |
1984 | | #endif /* APR_HAS_MMAP */ |
1985 | | |
1986 | | typedef struct { |
1987 | | apr_bucket_brigade *bb; |
1988 | | apr_bucket_brigade *tmpbb; |
1989 | | } old_write_filter_ctx; |
1990 | | |
1991 | | AP_CORE_DECLARE_NONSTD(apr_status_t) ap_old_write_filter( |
1992 | | ap_filter_t *f, apr_bucket_brigade *bb) |
1993 | 0 | { |
1994 | 0 | old_write_filter_ctx *ctx = f->ctx; |
1995 | |
|
1996 | 0 | AP_DEBUG_ASSERT(ctx); |
1997 | |
|
1998 | 0 | if (ctx->bb != NULL) { |
1999 | | /* whatever is coming down the pipe (we don't care), we |
2000 | | * can simply insert our buffered data at the front and |
2001 | | * pass the whole bundle down the chain. |
2002 | | */ |
2003 | 0 | APR_BRIGADE_PREPEND(bb, ctx->bb); |
2004 | 0 | } |
2005 | |
|
2006 | 0 | return ap_pass_brigade(f->next, bb); |
2007 | 0 | } |
2008 | | |
2009 | | static ap_filter_t *insert_old_write_filter(request_rec *r) |
2010 | 0 | { |
2011 | 0 | ap_filter_t *f; |
2012 | 0 | old_write_filter_ctx *ctx; |
2013 | | |
2014 | | /* future optimization: record some flags in the request_rec to |
2015 | | * say whether we've added our filter, and whether it is first. |
2016 | | */ |
2017 | | |
2018 | | /* this will typically exit on the first test */ |
2019 | 0 | for (f = r->output_filters; f != NULL; f = f->next) { |
2020 | 0 | if (ap_old_write_func == f->frec) |
2021 | 0 | break; |
2022 | 0 | } |
2023 | |
|
2024 | 0 | if (f == NULL) { |
2025 | | /* our filter hasn't been added yet */ |
2026 | 0 | ctx = apr_pcalloc(r->pool, sizeof(*ctx)); |
2027 | 0 | ctx->tmpbb = apr_brigade_create(r->pool, r->connection->bucket_alloc); |
2028 | |
|
2029 | 0 | ap_add_output_filter("OLD_WRITE", ctx, r, r->connection); |
2030 | 0 | f = r->output_filters; |
2031 | 0 | } |
2032 | |
|
2033 | 0 | return f; |
2034 | 0 | } |
2035 | | |
2036 | | static apr_status_t buffer_output(request_rec *r, |
2037 | | const char *str, apr_size_t len) |
2038 | 0 | { |
2039 | 0 | conn_rec *c = r->connection; |
2040 | 0 | ap_filter_t *f; |
2041 | 0 | old_write_filter_ctx *ctx; |
2042 | |
|
2043 | 0 | if (len == 0) |
2044 | 0 | return APR_SUCCESS; |
2045 | | |
2046 | 0 | f = insert_old_write_filter(r); |
2047 | 0 | ctx = f->ctx; |
2048 | | |
2049 | | /* if the first filter is not our buffering filter, then we have to |
2050 | | * deliver the content through the normal filter chain |
2051 | | */ |
2052 | 0 | if (f != r->output_filters) { |
2053 | 0 | apr_status_t rv; |
2054 | 0 | apr_bucket *b = apr_bucket_transient_create(str, len, c->bucket_alloc); |
2055 | 0 | APR_BRIGADE_INSERT_TAIL(ctx->tmpbb, b); |
2056 | |
|
2057 | 0 | rv = ap_pass_brigade(r->output_filters, ctx->tmpbb); |
2058 | 0 | apr_brigade_cleanup(ctx->tmpbb); |
2059 | 0 | return rv; |
2060 | 0 | } |
2061 | | |
2062 | 0 | if (ctx->bb == NULL) { |
2063 | 0 | ctx->bb = apr_brigade_create(r->pool, c->bucket_alloc); |
2064 | 0 | } |
2065 | |
|
2066 | 0 | return ap_fwrite(f->next, ctx->bb, str, len); |
2067 | 0 | } |
2068 | | |
2069 | | AP_DECLARE(int) ap_rputc(int c, request_rec *r) |
2070 | 0 | { |
2071 | 0 | char c2 = (char)c; |
2072 | |
|
2073 | 0 | if (r->connection->aborted) { |
2074 | 0 | return -1; |
2075 | 0 | } |
2076 | | |
2077 | 0 | if (buffer_output(r, &c2, 1) != APR_SUCCESS) |
2078 | 0 | return -1; |
2079 | | |
2080 | 0 | return c; |
2081 | 0 | } |
2082 | | |
2083 | | AP_DECLARE(int) ap_rwrite(const void *buf, int nbyte, request_rec *r) |
2084 | 0 | { |
2085 | 0 | if (nbyte < 0) |
2086 | 0 | return -1; |
2087 | | |
2088 | 0 | if (r->connection->aborted) |
2089 | 0 | return -1; |
2090 | | |
2091 | 0 | if (buffer_output(r, buf, nbyte) != APR_SUCCESS) |
2092 | 0 | return -1; |
2093 | | |
2094 | 0 | return nbyte; |
2095 | 0 | } |
2096 | | |
2097 | | struct ap_vrprintf_data { |
2098 | | apr_vformatter_buff_t vbuff; |
2099 | | request_rec *r; |
2100 | | char *buff; |
2101 | | }; |
2102 | | |
2103 | | /* Flush callback for apr_vformatter; returns -1 on error. */ |
2104 | | static int r_flush(apr_vformatter_buff_t *buff) |
2105 | 0 | { |
2106 | | /* callback function passed to ap_vformatter to be called when |
2107 | | * vformatter needs to write into buff and buff.curpos > buff.endpos */ |
2108 | | |
2109 | | /* ap_vrprintf_data passed as a apr_vformatter_buff_t, which is then |
2110 | | * "downcast" to an ap_vrprintf_data */ |
2111 | 0 | struct ap_vrprintf_data *vd = (struct ap_vrprintf_data*)buff; |
2112 | |
|
2113 | 0 | if (vd->r->connection->aborted) |
2114 | 0 | return -1; |
2115 | | |
2116 | | /* r_flush is called when vbuff is completely full */ |
2117 | 0 | if (buffer_output(vd->r, vd->buff, AP_IOBUFSIZE)) { |
2118 | 0 | return -1; |
2119 | 0 | } |
2120 | | |
2121 | | /* reset the buffer position */ |
2122 | 0 | vd->vbuff.curpos = vd->buff; |
2123 | 0 | vd->vbuff.endpos = vd->buff + AP_IOBUFSIZE; |
2124 | |
|
2125 | 0 | return 0; |
2126 | 0 | } |
2127 | | |
2128 | | AP_DECLARE(int) ap_vrprintf(request_rec *r, const char *fmt, va_list va) |
2129 | 0 | { |
2130 | 0 | int written; |
2131 | 0 | struct ap_vrprintf_data vd; |
2132 | 0 | char vrprintf_buf[AP_IOBUFSIZE]; |
2133 | |
|
2134 | 0 | vd.vbuff.curpos = vrprintf_buf; |
2135 | 0 | vd.vbuff.endpos = vrprintf_buf + AP_IOBUFSIZE; |
2136 | 0 | vd.r = r; |
2137 | 0 | vd.buff = vrprintf_buf; |
2138 | |
|
2139 | 0 | if (r->connection->aborted) |
2140 | 0 | return -1; |
2141 | | |
2142 | 0 | written = apr_vformatter(r_flush, &vd.vbuff, fmt, va); |
2143 | |
|
2144 | 0 | if (written != -1) { |
2145 | 0 | int n = vd.vbuff.curpos - vrprintf_buf; |
2146 | | |
2147 | | /* last call to buffer_output, to finish clearing the buffer */ |
2148 | 0 | if (buffer_output(r, vrprintf_buf, n) != APR_SUCCESS) |
2149 | 0 | return -1; |
2150 | | |
2151 | 0 | written += n; |
2152 | 0 | } |
2153 | | |
2154 | 0 | return written; |
2155 | 0 | } |
2156 | | |
2157 | | AP_DECLARE_NONSTD(int) ap_rprintf(request_rec *r, const char *fmt, ...) |
2158 | 0 | { |
2159 | 0 | va_list va; |
2160 | 0 | int n; |
2161 | |
|
2162 | 0 | if (r->connection->aborted) |
2163 | 0 | return -1; |
2164 | | |
2165 | 0 | va_start(va, fmt); |
2166 | 0 | n = ap_vrprintf(r, fmt, va); |
2167 | 0 | va_end(va); |
2168 | |
|
2169 | 0 | return n; |
2170 | 0 | } |
2171 | | |
2172 | | AP_DECLARE_NONSTD(int) ap_rvputs(request_rec *r, ...) |
2173 | 0 | { |
2174 | 0 | va_list va; |
2175 | 0 | const char *s; |
2176 | 0 | apr_size_t len; |
2177 | 0 | apr_size_t written = 0; |
2178 | |
|
2179 | 0 | if (r->connection->aborted) |
2180 | 0 | return -1; |
2181 | | |
2182 | | /* ### TODO: if the total output is large, put all the strings |
2183 | | * ### into a single brigade, rather than flushing each time we |
2184 | | * ### fill the buffer |
2185 | | */ |
2186 | 0 | va_start(va, r); |
2187 | 0 | while (1) { |
2188 | 0 | s = va_arg(va, const char *); |
2189 | 0 | if (s == NULL) |
2190 | 0 | break; |
2191 | | |
2192 | 0 | len = strlen(s); |
2193 | 0 | if (buffer_output(r, s, len) != APR_SUCCESS) { |
2194 | 0 | va_end(va); |
2195 | 0 | return -1; |
2196 | 0 | } |
2197 | | |
2198 | 0 | written += len; |
2199 | 0 | } |
2200 | 0 | va_end(va); |
2201 | |
|
2202 | 0 | return written; |
2203 | 0 | } |
2204 | | |
2205 | | AP_DECLARE(int) ap_rflush(request_rec *r) |
2206 | 0 | { |
2207 | 0 | conn_rec *c = r->connection; |
2208 | 0 | apr_bucket *b; |
2209 | 0 | ap_filter_t *f; |
2210 | 0 | old_write_filter_ctx *ctx; |
2211 | 0 | apr_status_t rv; |
2212 | |
|
2213 | 0 | f = insert_old_write_filter(r); |
2214 | 0 | ctx = f->ctx; |
2215 | |
|
2216 | 0 | b = apr_bucket_flush_create(c->bucket_alloc); |
2217 | 0 | APR_BRIGADE_INSERT_TAIL(ctx->tmpbb, b); |
2218 | |
|
2219 | 0 | rv = ap_pass_brigade(r->output_filters, ctx->tmpbb); |
2220 | 0 | apr_brigade_cleanup(ctx->tmpbb); |
2221 | 0 | if (rv != APR_SUCCESS) |
2222 | 0 | return -1; |
2223 | | |
2224 | 0 | return 0; |
2225 | 0 | } |
2226 | | |
2227 | | /* |
2228 | | * This function sets the Last-Modified output header field to the value |
2229 | | * of the mtime field in the request structure - rationalized to keep it from |
2230 | | * being in the future. |
2231 | | */ |
2232 | | AP_DECLARE(void) ap_set_last_modified(request_rec *r) |
2233 | 0 | { |
2234 | 0 | if (!r->assbackwards) { |
2235 | 0 | apr_time_t mod_time = ap_rationalize_mtime(r, r->mtime); |
2236 | 0 | char *datestr = apr_palloc(r->pool, APR_RFC822_DATE_LEN); |
2237 | |
|
2238 | 0 | apr_rfc822_date(datestr, mod_time); |
2239 | 0 | apr_table_setn(r->headers_out, "Last-Modified", datestr); |
2240 | 0 | } |
2241 | 0 | } |
2242 | | |
2243 | | typedef struct hdr_ptr { |
2244 | | ap_filter_t *f; |
2245 | | apr_bucket_brigade *bb; |
2246 | | } hdr_ptr; |
2247 | | |
2248 | | |
2249 | | AP_DECLARE(void) ap_set_std_response_headers(request_rec *r) |
2250 | 0 | { |
2251 | 0 | const char *server = NULL, *date; |
2252 | 0 | char *s; |
2253 | | |
2254 | | /* Before generating a response, we make sure that `Date` and `Server` |
2255 | | * headers are present. When proxying requests, we preserver existing |
2256 | | * values and replace them otherwise. |
2257 | | */ |
2258 | 0 | if (r->proxyreq != PROXYREQ_NONE) { |
2259 | 0 | date = apr_table_get(r->headers_out, "Date"); |
2260 | 0 | if (!date) { |
2261 | 0 | s = apr_palloc(r->pool, APR_RFC822_DATE_LEN); |
2262 | 0 | ap_recent_rfc822_date(s, r->request_time); |
2263 | 0 | date = s; |
2264 | 0 | } |
2265 | 0 | server = apr_table_get(r->headers_out, "Server"); |
2266 | 0 | } |
2267 | 0 | else { |
2268 | 0 | s = apr_palloc(r->pool, APR_RFC822_DATE_LEN); |
2269 | 0 | ap_recent_rfc822_date(s, r->request_time); |
2270 | 0 | date = s; |
2271 | 0 | } |
2272 | |
|
2273 | 0 | apr_table_setn(r->headers_out, "Date", date); |
2274 | |
|
2275 | 0 | if (!server) |
2276 | 0 | server = ap_get_server_banner(); |
2277 | 0 | if (server && *server) |
2278 | 0 | apr_table_setn(r->headers_out, "Server", server); |
2279 | 0 | } |
2280 | | |
2281 | | AP_DECLARE(void) ap_send_interim_response(request_rec *r, int send_headers) |
2282 | 0 | { |
2283 | 0 | request_rec *rr; |
2284 | 0 | apr_bucket *b; |
2285 | 0 | apr_bucket_brigade *bb; |
2286 | 0 | const char *reason = NULL; |
2287 | |
|
2288 | 0 | if (r->proto_num < HTTP_VERSION(1,1)) { |
2289 | | /* don't send interim response to HTTP/1.0 Client */ |
2290 | 0 | return; |
2291 | 0 | } |
2292 | 0 | if (!ap_is_HTTP_INFO(r->status)) { |
2293 | 0 | ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(00575) |
2294 | 0 | "Status is %d - not sending interim response", r->status); |
2295 | 0 | return; |
2296 | 0 | } |
2297 | 0 | if (r->status == HTTP_CONTINUE) { |
2298 | 0 | if (!r->expecting_100) { |
2299 | | /* |
2300 | | * Don't send 100-Continue when there was no Expect: 100-continue |
2301 | | * in the request headers. For origin servers this is a SHOULD NOT |
2302 | | * for proxies it is a MUST NOT according to RFC 2616 8.2.3 |
2303 | | */ |
2304 | 0 | return; |
2305 | 0 | } |
2306 | | |
2307 | | /* if we send an interim response, we're no longer in a state of |
2308 | | * expecting one. Also, this could feasibly be in a subrequest, |
2309 | | * so we need to propagate the fact that we responded. |
2310 | | */ |
2311 | 0 | for (rr = r; rr != NULL; rr = rr->main) { |
2312 | 0 | rr->expecting_100 = 0; |
2313 | 0 | } |
2314 | 0 | } |
2315 | | |
2316 | 0 | ap_log_rerror(APLOG_MARK, APLOG_TRACE2, 0, r, |
2317 | 0 | "ap_send_interim_response: send %d", r->status); |
2318 | 0 | bb = apr_brigade_create(r->pool, r->connection->bucket_alloc); |
2319 | 0 | if (send_headers) { |
2320 | 0 | ap_set_std_response_headers(r); |
2321 | 0 | } |
2322 | 0 | if (r->status_line && strlen(r->status_line) > 4) { |
2323 | 0 | reason = r->status_line + 4; |
2324 | 0 | } |
2325 | 0 | b = ap_bucket_response_create(r->status, reason, |
2326 | 0 | send_headers? r->headers_out : NULL, |
2327 | 0 | r->notes, r->pool, r->connection->bucket_alloc); |
2328 | 0 | APR_BRIGADE_INSERT_TAIL(bb, b); |
2329 | 0 | if (send_headers) { |
2330 | 0 | apr_table_clear(r->headers_out); |
2331 | 0 | } |
2332 | 0 | b = apr_bucket_flush_create(r->connection->bucket_alloc); |
2333 | 0 | APR_BRIGADE_INSERT_TAIL(bb, b); |
2334 | 0 | ap_pass_brigade(r->proto_output_filters, bb); |
2335 | 0 | apr_brigade_destroy(bb); |
2336 | 0 | } |
2337 | | |
2338 | | /* |
2339 | | * Compare two protocol identifier. Result is similar to strcmp(): |
2340 | | * 0 gives same precedence, >0 means proto1 is preferred. |
2341 | | */ |
2342 | | static int protocol_cmp(const apr_array_header_t *preferences, |
2343 | | const char *proto1, |
2344 | | const char *proto2) |
2345 | 0 | { |
2346 | 0 | if (preferences && preferences->nelts > 0) { |
2347 | 0 | int index1 = ap_array_str_index(preferences, proto1, 0); |
2348 | 0 | int index2 = ap_array_str_index(preferences, proto2, 0); |
2349 | 0 | if (index2 > index1) { |
2350 | 0 | return (index1 >= 0) ? 1 : -1; |
2351 | 0 | } |
2352 | 0 | else if (index1 > index2) { |
2353 | 0 | return (index2 >= 0) ? -1 : 1; |
2354 | 0 | } |
2355 | 0 | } |
2356 | | /* both have the same index (maybe -1 or no pref configured) and we compare |
2357 | | * the names so that spdy3 gets precedence over spdy2. That makes |
2358 | | * the outcome at least deterministic. */ |
2359 | 0 | return strcmp(proto1, proto2); |
2360 | 0 | } |
2361 | | |
2362 | | AP_DECLARE(const char *) ap_get_protocol(conn_rec *c) |
2363 | 0 | { |
2364 | 0 | const char *protocol = ap_run_protocol_get(c); |
2365 | 0 | return protocol? protocol : AP_PROTOCOL_HTTP1; |
2366 | 0 | } |
2367 | | |
2368 | | AP_DECLARE(apr_status_t) ap_get_protocol_upgrades(conn_rec *c, request_rec *r, |
2369 | | server_rec *s, int report_all, |
2370 | | const apr_array_header_t **pupgrades) |
2371 | 0 | { |
2372 | 0 | apr_pool_t *pool = r? r->pool : c->pool; |
2373 | 0 | core_server_config *conf; |
2374 | 0 | const char *existing; |
2375 | 0 | apr_array_header_t *upgrades = NULL; |
2376 | |
|
2377 | 0 | if (!s) { |
2378 | 0 | s = (r? r->server : c->base_server); |
2379 | 0 | } |
2380 | 0 | conf = ap_get_core_module_config(s->module_config); |
2381 | | |
2382 | 0 | if (conf->protocols->nelts > 0) { |
2383 | 0 | existing = ap_get_protocol(c); |
2384 | 0 | if (conf->protocols->nelts > 1 |
2385 | 0 | || !ap_array_str_contains(conf->protocols, existing)) { |
2386 | 0 | int i; |
2387 | | |
2388 | | /* possibly more than one choice or one, but not the |
2389 | | * existing. (TODO: maybe 426 and Upgrade then?) */ |
2390 | 0 | upgrades = apr_array_make(pool, conf->protocols->nelts + 1, |
2391 | 0 | sizeof(char *)); |
2392 | 0 | for (i = 0; i < conf->protocols->nelts; i++) { |
2393 | 0 | const char *p = APR_ARRAY_IDX(conf->protocols, i, char *); |
2394 | 0 | if (strcmp(existing, p)) { |
2395 | | /* not the one we have and possible, add in this order */ |
2396 | 0 | APR_ARRAY_PUSH(upgrades, const char*) = p; |
2397 | 0 | } |
2398 | 0 | else if (!report_all) { |
2399 | 0 | break; |
2400 | 0 | } |
2401 | 0 | } |
2402 | 0 | } |
2403 | 0 | } |
2404 | | |
2405 | 0 | *pupgrades = upgrades; |
2406 | 0 | return APR_SUCCESS; |
2407 | 0 | } |
2408 | | |
2409 | | AP_DECLARE(const char *) ap_select_protocol(conn_rec *c, request_rec *r, |
2410 | | server_rec *s, |
2411 | | const apr_array_header_t *choices) |
2412 | 0 | { |
2413 | 0 | apr_pool_t *pool = r? r->pool : c->pool; |
2414 | 0 | core_server_config *conf; |
2415 | 0 | const char *protocol = NULL, *existing; |
2416 | 0 | apr_array_header_t *proposals; |
2417 | |
|
2418 | 0 | if (!s) { |
2419 | 0 | s = (r? r->server : c->base_server); |
2420 | 0 | } |
2421 | 0 | conf = ap_get_core_module_config(s->module_config); |
2422 | | |
2423 | 0 | if (APLOGcdebug(c)) { |
2424 | 0 | const char *p = apr_array_pstrcat(pool, conf->protocols, ','); |
2425 | 0 | ap_log_cerror(APLOG_MARK, APLOG_DEBUG, 0, c, APLOGNO(03155) |
2426 | 0 | "select protocol from %s, choices=%s for server %s", |
2427 | 0 | p, apr_array_pstrcat(pool, choices, ','), |
2428 | 0 | s->server_hostname); |
2429 | 0 | } |
2430 | |
|
2431 | 0 | if (conf->protocols->nelts <= 0) { |
2432 | | /* nothing configured, by default, we only allow http/1.1 here. |
2433 | | * For now... |
2434 | | */ |
2435 | 0 | if (ap_array_str_contains(choices, AP_PROTOCOL_HTTP1)) { |
2436 | 0 | return AP_PROTOCOL_HTTP1; |
2437 | 0 | } |
2438 | 0 | else { |
2439 | 0 | return NULL; |
2440 | 0 | } |
2441 | 0 | } |
2442 | | |
2443 | 0 | proposals = apr_array_make(pool, choices->nelts + 1, sizeof(char *)); |
2444 | 0 | ap_run_protocol_propose(c, r, s, choices, proposals); |
2445 | | |
2446 | | /* If the existing protocol has not been proposed, but is a choice, |
2447 | | * add it to the proposals implicitly. |
2448 | | */ |
2449 | 0 | existing = ap_get_protocol(c); |
2450 | 0 | if (!ap_array_str_contains(proposals, existing) |
2451 | 0 | && ap_array_str_contains(choices, existing)) { |
2452 | 0 | APR_ARRAY_PUSH(proposals, const char*) = existing; |
2453 | 0 | } |
2454 | |
|
2455 | 0 | if (proposals->nelts > 0) { |
2456 | 0 | int i; |
2457 | 0 | const apr_array_header_t *prefs = NULL; |
2458 | | |
2459 | | /* Default for protocols_honor_order is 'on' or != 0 */ |
2460 | 0 | if (conf->protocols_honor_order == 0 && choices->nelts > 0) { |
2461 | 0 | prefs = choices; |
2462 | 0 | } |
2463 | 0 | else { |
2464 | 0 | prefs = conf->protocols; |
2465 | 0 | } |
2466 | | |
2467 | | /* Select the most preferred protocol */ |
2468 | 0 | if (APLOGcdebug(c)) { |
2469 | 0 | ap_log_cerror(APLOG_MARK, APLOG_DEBUG, 0, c, APLOGNO(03156) |
2470 | 0 | "select protocol, proposals=%s preferences=%s configured=%s", |
2471 | 0 | apr_array_pstrcat(pool, proposals, ','), |
2472 | 0 | apr_array_pstrcat(pool, prefs, ','), |
2473 | 0 | apr_array_pstrcat(pool, conf->protocols, ',')); |
2474 | 0 | } |
2475 | 0 | for (i = 0; i < proposals->nelts; ++i) { |
2476 | 0 | const char *p = APR_ARRAY_IDX(proposals, i, const char *); |
2477 | 0 | if (!ap_array_str_contains(conf->protocols, p)) { |
2478 | | /* not a configured protocol here */ |
2479 | 0 | continue; |
2480 | 0 | } |
2481 | 0 | else if (!protocol |
2482 | 0 | || (protocol_cmp(prefs, protocol, p) < 0)) { |
2483 | | /* none selected yet or this one has preference */ |
2484 | 0 | protocol = p; |
2485 | 0 | } |
2486 | 0 | } |
2487 | 0 | } |
2488 | 0 | if (APLOGcdebug(c)) { |
2489 | 0 | ap_log_cerror(APLOG_MARK, APLOG_DEBUG, 0, c, APLOGNO(03157) |
2490 | 0 | "selected protocol=%s", |
2491 | 0 | protocol? protocol : "(none)"); |
2492 | 0 | } |
2493 | |
|
2494 | 0 | return protocol; |
2495 | 0 | } |
2496 | | |
2497 | | AP_DECLARE(apr_status_t) ap_switch_protocol(conn_rec *c, request_rec *r, |
2498 | | server_rec *s, |
2499 | | const char *protocol) |
2500 | 0 | { |
2501 | 0 | const char *current = ap_get_protocol(c); |
2502 | 0 | int rc; |
2503 | | |
2504 | 0 | if (!strcmp(current, protocol)) { |
2505 | 0 | ap_log_cerror(APLOG_MARK, APLOG_WARNING, 0, c, APLOGNO(02906) |
2506 | 0 | "already at it, protocol_switch to %s", |
2507 | 0 | protocol); |
2508 | 0 | return APR_SUCCESS; |
2509 | 0 | } |
2510 | | |
2511 | 0 | rc = ap_run_protocol_switch(c, r, s, protocol); |
2512 | 0 | switch (rc) { |
2513 | 0 | case DECLINED: |
2514 | 0 | ap_log_cerror(APLOG_MARK, APLOG_ERR, 0, c, APLOGNO(02907) |
2515 | 0 | "no implementation for protocol_switch to %s", |
2516 | 0 | protocol); |
2517 | 0 | return APR_ENOTIMPL; |
2518 | 0 | case OK: |
2519 | 0 | case DONE: |
2520 | 0 | return APR_SUCCESS; |
2521 | 0 | default: |
2522 | 0 | ap_log_cerror(APLOG_MARK, APLOG_ERR, 0, c, APLOGNO(02905) |
2523 | 0 | "unexpected return code %d from protocol_switch to %s" |
2524 | 0 | , rc, protocol); |
2525 | 0 | return APR_EOF; |
2526 | 0 | } |
2527 | 0 | } |
2528 | | |
2529 | | AP_DECLARE(int) ap_is_allowed_protocol(conn_rec *c, request_rec *r, |
2530 | | server_rec *s, const char *protocol) |
2531 | 0 | { |
2532 | 0 | core_server_config *conf; |
2533 | |
|
2534 | 0 | if (!s) { |
2535 | 0 | s = (r? r->server : c->base_server); |
2536 | 0 | } |
2537 | 0 | conf = ap_get_core_module_config(s->module_config); |
2538 | | |
2539 | 0 | if (conf->protocols->nelts > 0) { |
2540 | 0 | return ap_array_str_contains(conf->protocols, protocol); |
2541 | 0 | } |
2542 | 0 | return !strcmp(AP_PROTOCOL_HTTP1, protocol); |
2543 | 0 | } |
2544 | | |
2545 | | |
2546 | | AP_IMPLEMENT_HOOK_VOID(pre_read_request, |
2547 | | (request_rec *r, conn_rec *c), |
2548 | | (r, c)) |
2549 | | AP_IMPLEMENT_HOOK_RUN_ALL(int,post_read_request, |
2550 | | (request_rec *r), (r), OK, DECLINED) |
2551 | | AP_IMPLEMENT_HOOK_RUN_ALL(int,log_transaction, |
2552 | | (request_rec *r), (r), OK, DECLINED) |
2553 | | AP_IMPLEMENT_HOOK_RUN_FIRST(const char *,http_scheme, |
2554 | | (const request_rec *r), (r), NULL) |
2555 | | AP_IMPLEMENT_HOOK_RUN_FIRST(unsigned short,default_port, |
2556 | | (const request_rec *r), (r), 0) |
2557 | | AP_IMPLEMENT_HOOK_RUN_FIRST(int, note_auth_failure, |
2558 | | (request_rec *r, const char *auth_type), |
2559 | | (r, auth_type), DECLINED) |
2560 | | AP_IMPLEMENT_HOOK_RUN_ALL(int,protocol_propose, |
2561 | | (conn_rec *c, request_rec *r, server_rec *s, |
2562 | | const apr_array_header_t *offers, |
2563 | | apr_array_header_t *proposals), |
2564 | | (c, r, s, offers, proposals), OK, DECLINED) |
2565 | | AP_IMPLEMENT_HOOK_RUN_FIRST(int,protocol_switch, |
2566 | | (conn_rec *c, request_rec *r, server_rec *s, |
2567 | | const char *protocol), |
2568 | | (c, r, s, protocol), DECLINED) |
2569 | | AP_IMPLEMENT_HOOK_RUN_FIRST(const char *,protocol_get, |
2570 | | (const conn_rec *c), (c), NULL) |