]> granicus.if.org Git - apache/blob - server/protocol.c
fixing h2 supress to inhibit other annnouncements in upgrade: header
[apache] / server / protocol.c
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 "scoreboard.h"
51
52 #if APR_HAVE_STDARG_H
53 #include <stdarg.h>
54 #endif
55 #if APR_HAVE_UNISTD_H
56 #include <unistd.h>
57 #endif
58
59 /* we know core's module_index is 0 */
60 #undef APLOG_MODULE_INDEX
61 #define APLOG_MODULE_INDEX AP_CORE_MODULE_INDEX
62
63 APR_HOOK_STRUCT(
64     APR_HOOK_LINK(pre_read_request)
65     APR_HOOK_LINK(post_read_request)
66     APR_HOOK_LINK(log_transaction)
67     APR_HOOK_LINK(http_scheme)
68     APR_HOOK_LINK(default_port)
69     APR_HOOK_LINK(note_auth_failure)
70     APR_HOOK_LINK(protocol_propose)
71     APR_HOOK_LINK(protocol_switch)
72     APR_HOOK_LINK(protocol_get)
73 )
74
75 AP_DECLARE_DATA ap_filter_rec_t *ap_old_write_func = NULL;
76
77
78 /* Patterns to match in ap_make_content_type() */
79 static const char *needcset[] = {
80     "text/plain",
81     "text/html",
82     NULL
83 };
84 static const apr_strmatch_pattern **needcset_patterns;
85 static const apr_strmatch_pattern *charset_pattern;
86
87 AP_DECLARE(void) ap_setup_make_content_type(apr_pool_t *pool)
88 {
89     int i;
90     for (i = 0; needcset[i]; i++) {
91         continue;
92     }
93     needcset_patterns = (const apr_strmatch_pattern **)
94         apr_palloc(pool, (i + 1) * sizeof(apr_strmatch_pattern *));
95     for (i = 0; needcset[i]; i++) {
96         needcset_patterns[i] = apr_strmatch_precompile(pool, needcset[i], 0);
97     }
98     needcset_patterns[i] = NULL;
99     charset_pattern = apr_strmatch_precompile(pool, "charset=", 0);
100 }
101
102 /*
103  * Builds the content-type that should be sent to the client from the
104  * content-type specified.  The following rules are followed:
105  *    - if type is NULL or "", return NULL (do not set content-type).
106  *    - if charset adding is disabled, stop processing and return type.
107  *    - then, if there are no parameters on type, add the default charset
108  *    - return type
109  */
110 AP_DECLARE(const char *)ap_make_content_type(request_rec *r, const char *type)
111 {
112     const apr_strmatch_pattern **pcset;
113     core_dir_config *conf =
114         (core_dir_config *)ap_get_core_module_config(r->per_dir_config);
115     core_request_config *request_conf;
116     apr_size_t type_len;
117
118     if (!type || *type == '\0') {
119         return NULL;
120     }
121
122     if (conf->add_default_charset != ADD_DEFAULT_CHARSET_ON) {
123         return type;
124     }
125
126     request_conf = ap_get_core_module_config(r->request_config);
127     if (request_conf->suppress_charset) {
128         return type;
129     }
130
131     type_len = strlen(type);
132
133     if (apr_strmatch(charset_pattern, type, type_len) != NULL) {
134         /* already has parameter, do nothing */
135         /* XXX we don't check the validity */
136         ;
137     }
138     else {
139         /* see if it makes sense to add the charset. At present,
140          * we only add it if the Content-type is one of needcset[]
141          */
142         for (pcset = needcset_patterns; *pcset ; pcset++) {
143             if (apr_strmatch(*pcset, type, type_len) != NULL) {
144                 struct iovec concat[3];
145                 concat[0].iov_base = (void *)type;
146                 concat[0].iov_len = type_len;
147                 concat[1].iov_base = (void *)"; charset=";
148                 concat[1].iov_len = sizeof("; charset=") - 1;
149                 concat[2].iov_base = (void *)(conf->add_default_charset_name);
150                 concat[2].iov_len = strlen(conf->add_default_charset_name);
151                 type = apr_pstrcatv(r->pool, concat, 3, NULL);
152                 break;
153             }
154         }
155     }
156
157     return type;
158 }
159
160 AP_DECLARE(void) ap_set_content_length(request_rec *r, apr_off_t clength)
161 {
162     r->clength = clength;
163     apr_table_setn(r->headers_out, "Content-Length",
164                    apr_off_t_toa(r->pool, clength));
165 }
166
167 /*
168  * Return the latest rational time from a request/mtime (modification time)
169  * pair.  We return the mtime unless it's in the future, in which case we
170  * return the current time.  We use the request time as a reference in order
171  * to limit the number of calls to time().  We don't check for futurosity
172  * unless the mtime is at least as new as the reference.
173  */
174 AP_DECLARE(apr_time_t) ap_rationalize_mtime(request_rec *r, apr_time_t mtime)
175 {
176     apr_time_t now;
177
178     /* For all static responses, it's almost certain that the file was
179      * last modified before the beginning of the request.  So there's
180      * no reason to call time(NULL) again.  But if the response has been
181      * created on demand, then it might be newer than the time the request
182      * started.  In this event we really have to call time(NULL) again
183      * so that we can give the clients the most accurate Last-Modified.  If we
184      * were given a time in the future, we return the current time - the
185      * Last-Modified can't be in the future.
186      */
187     now = (mtime < r->request_time) ? r->request_time : apr_time_now();
188     return (mtime > now) ? now : mtime;
189 }
190
191 /* Get a line of protocol input, including any continuation lines
192  * caused by MIME folding (or broken clients) if fold != 0, and place it
193  * in the buffer s, of size n bytes, without the ending newline.
194  * 
195  * Pulls from r->proto_input_filters instead of r->input_filters for
196  * stricter protocol adherence and better input filter behavior during
197  * chunked trailer processing (for http).
198  *
199  * If s is NULL, ap_rgetline_core will allocate necessary memory from r->pool.
200  *
201  * Returns APR_SUCCESS if there are no problems and sets *read to be
202  * the full length of s.
203  *
204  * APR_ENOSPC is returned if there is not enough buffer space.
205  * Other errors may be returned on other errors.
206  *
207  * The LF is *not* returned in the buffer.  Therefore, a *read of 0
208  * indicates that an empty line was read.
209  *
210  * Notes: Because the buffer uses 1 char for NUL, the most we can return is
211  *        (n - 1) actual characters.
212  *
213  *        If no LF is detected on the last line due to a dropped connection
214  *        or a full buffer, that's considered an error.
215  */
216 AP_DECLARE(apr_status_t) ap_rgetline_core(char **s, apr_size_t n,
217                                           apr_size_t *read, request_rec *r,
218                                           int fold, apr_bucket_brigade *bb)
219 {
220     apr_status_t rv;
221     apr_bucket *e;
222     apr_size_t bytes_handled = 0, current_alloc = 0;
223     char *pos, *last_char = *s;
224     int do_alloc = (*s == NULL), saw_eos = 0;
225
226     /*
227      * Initialize last_char as otherwise a random value will be compared
228      * against APR_ASCII_LF at the end of the loop if bb only contains
229      * zero-length buckets.
230      */
231     if (last_char)
232         *last_char = '\0';
233
234     for (;;) {
235         apr_brigade_cleanup(bb);
236         rv = ap_get_brigade(r->proto_input_filters, bb, AP_MODE_GETLINE,
237                             APR_BLOCK_READ, 0);
238         if (rv != APR_SUCCESS) {
239             return rv;
240         }
241
242         /* Something horribly wrong happened.  Someone didn't block! 
243          * (this also happens at the end of each keepalive connection)
244          */
245         if (APR_BRIGADE_EMPTY(bb)) {
246             return APR_EGENERAL;
247         }
248
249         for (e = APR_BRIGADE_FIRST(bb);
250              e != APR_BRIGADE_SENTINEL(bb);
251              e = APR_BUCKET_NEXT(e))
252         {
253             const char *str;
254             apr_size_t len;
255
256             /* If we see an EOS, don't bother doing anything more. */
257             if (APR_BUCKET_IS_EOS(e)) {
258                 saw_eos = 1;
259                 break;
260             }
261
262             rv = apr_bucket_read(e, &str, &len, APR_BLOCK_READ);
263             if (rv != APR_SUCCESS) {
264                 return rv;
265             }
266
267             if (len == 0) {
268                 /* no use attempting a zero-byte alloc (hurts when
269                  * using --with-efence --enable-pool-debug) or
270                  * doing any of the other logic either
271                  */
272                 continue;
273             }
274
275             /* Would this overrun our buffer?  If so, we'll die. */
276             if (n < bytes_handled + len) {
277                 *read = bytes_handled;
278                 if (*s) {
279                     /* ensure this string is NUL terminated */
280                     if (bytes_handled > 0) {
281                         (*s)[bytes_handled-1] = '\0';
282                     }
283                     else {
284                         (*s)[0] = '\0';
285                     }
286                 }
287                 return APR_ENOSPC;
288             }
289
290             /* Do we have to handle the allocation ourselves? */
291             if (do_alloc) {
292                 /* We'll assume the common case where one bucket is enough. */
293                 if (!*s) {
294                     current_alloc = len;
295                     *s = apr_palloc(r->pool, current_alloc);
296                 }
297                 else if (bytes_handled + len > current_alloc) {
298                     /* Increase the buffer size */
299                     apr_size_t new_size = current_alloc * 2;
300                     char *new_buffer;
301
302                     if (bytes_handled + len > new_size) {
303                         new_size = (bytes_handled + len) * 2;
304                     }
305
306                     new_buffer = apr_palloc(r->pool, new_size);
307
308                     /* Copy what we already had. */
309                     memcpy(new_buffer, *s, bytes_handled);
310                     current_alloc = new_size;
311                     *s = new_buffer;
312                 }
313             }
314
315             /* Just copy the rest of the data to the end of the old buffer. */
316             pos = *s + bytes_handled;
317             memcpy(pos, str, len);
318             last_char = pos + len - 1;
319
320             /* We've now processed that new data - update accordingly. */
321             bytes_handled += len;
322         }
323
324         /* If we got a full line of input, stop reading */
325         if (last_char && (*last_char == APR_ASCII_LF)) {
326             break;
327         }
328     }
329
330     /* Now NUL-terminate the string at the end of the line;
331      * if the last-but-one character is a CR, terminate there */
332     if (last_char > *s && last_char[-1] == APR_ASCII_CR) {
333         last_char--;
334     }
335     *last_char = '\0';
336     bytes_handled = last_char - *s;
337
338     /* If we're folding, we have more work to do.
339      *
340      * Note that if an EOS was seen, we know we can't have another line.
341      */
342     if (fold && bytes_handled && !saw_eos) {
343         for (;;) {
344             const char *str;
345             apr_size_t len;
346             char c;
347
348             /* Clear the temp brigade for this filter read. */
349             apr_brigade_cleanup(bb);
350
351             /* We only care about the first byte. */
352             rv = ap_get_brigade(r->proto_input_filters, bb, AP_MODE_SPECULATIVE,
353                                 APR_BLOCK_READ, 1);
354             if (rv != APR_SUCCESS) {
355                 return rv;
356             }
357
358             if (APR_BRIGADE_EMPTY(bb)) {
359                 break;
360             }
361
362             e = APR_BRIGADE_FIRST(bb);
363
364             /* If we see an EOS, don't bother doing anything more. */
365             if (APR_BUCKET_IS_EOS(e)) {
366                 break;
367             }
368
369             rv = apr_bucket_read(e, &str, &len, APR_BLOCK_READ);
370             if (rv != APR_SUCCESS) {
371                 apr_brigade_cleanup(bb);
372                 return rv;
373             }
374
375             /* Found one, so call ourselves again to get the next line.
376              *
377              * FIXME: If the folding line is completely blank, should we
378              * stop folding?  Does that require also looking at the next
379              * char?
380              */
381             /* When we call destroy, the buckets are deleted, so save that
382              * one character we need.  This simplifies our execution paths
383              * at the cost of one character read.
384              */
385             c = *str;
386             if (c == APR_ASCII_BLANK || c == APR_ASCII_TAB) {
387                 /* Do we have enough space? We may be full now. */
388                 if (bytes_handled >= n) {
389                     *read = n;
390                     /* ensure this string is terminated */
391                     (*s)[n-1] = '\0';
392                     return APR_ENOSPC;
393                 }
394                 else {
395                     apr_size_t next_size, next_len;
396                     char *tmp;
397
398                     /* If we're doing the allocations for them, we have to
399                      * give ourselves a NULL and copy it on return.
400                      */
401                     if (do_alloc) {
402                         tmp = NULL;
403                     } else {
404                         /* We're null terminated. */
405                         tmp = last_char;
406                     }
407
408                     next_size = n - bytes_handled;
409
410                     rv = ap_rgetline_core(&tmp, next_size,
411                                           &next_len, r, 0, bb);
412                     if (rv != APR_SUCCESS) {
413                         return rv;
414                     }
415
416                     if (do_alloc && next_len > 0) {
417                         char *new_buffer;
418                         apr_size_t new_size = bytes_handled + next_len + 1;
419
420                         /* we need to alloc an extra byte for a null */
421                         new_buffer = apr_palloc(r->pool, new_size);
422
423                         /* Copy what we already had. */
424                         memcpy(new_buffer, *s, bytes_handled);
425
426                         /* copy the new line, including the trailing null */
427                         memcpy(new_buffer + bytes_handled, tmp, next_len + 1);
428                         *s = new_buffer;
429                     }
430
431                     last_char += next_len;
432                     bytes_handled += next_len;
433                 }
434             }
435             else { /* next character is not tab or space */
436                 break;
437             }
438         }
439     }
440     *read = bytes_handled;
441
442     /* PR#43039: We shouldn't accept NULL bytes within the line */
443     if (strlen(*s) < bytes_handled) {
444         return APR_EINVAL;
445     }
446
447     return APR_SUCCESS;
448 }
449
450 #if APR_CHARSET_EBCDIC
451 AP_DECLARE(apr_status_t) ap_rgetline(char **s, apr_size_t n,
452                                      apr_size_t *read, request_rec *r,
453                                      int fold, apr_bucket_brigade *bb)
454 {
455     /* on ASCII boxes, ap_rgetline is a macro which simply invokes
456      * ap_rgetline_core with the same parms
457      *
458      * on EBCDIC boxes, each complete http protocol input line needs to be
459      * translated into the code page used by the compiler.  Since
460      * ap_rgetline_core uses recursion, we do the translation in a wrapper
461      * function to ensure that each input character gets translated only once.
462      */
463     apr_status_t rv;
464
465     rv = ap_rgetline_core(s, n, read, r, fold, bb);
466     if (rv == APR_SUCCESS) {
467         ap_xlate_proto_from_ascii(*s, *read);
468     }
469     return rv;
470 }
471 #endif
472
473 AP_DECLARE(int) ap_getline(char *s, int n, request_rec *r, int fold)
474 {
475     char *tmp_s = s;
476     apr_status_t rv;
477     apr_size_t len;
478     apr_bucket_brigade *tmp_bb;
479
480     tmp_bb = apr_brigade_create(r->pool, r->connection->bucket_alloc);
481     rv = ap_rgetline(&tmp_s, n, &len, r, fold, tmp_bb);
482     apr_brigade_destroy(tmp_bb);
483
484     /* Map the out-of-space condition to the old API. */
485     if (rv == APR_ENOSPC) {
486         return n;
487     }
488
489     /* Anything else is just bad. */
490     if (rv != APR_SUCCESS) {
491         return -1;
492     }
493
494     return (int)len;
495 }
496
497 /* parse_uri: break apart the uri
498  * Side Effects:
499  * - sets r->args to rest after '?' (or NULL if no '?')
500  * - sets r->uri to request uri (without r->args part)
501  * - sets r->hostname (if not set already) from request (scheme://host:port)
502  */
503 AP_CORE_DECLARE(void) ap_parse_uri(request_rec *r, const char *uri)
504 {
505     int status = HTTP_OK;
506
507     r->unparsed_uri = apr_pstrdup(r->pool, uri);
508
509     /* http://issues.apache.org/bugzilla/show_bug.cgi?id=31875
510      * http://issues.apache.org/bugzilla/show_bug.cgi?id=28450
511      *
512      * This is not in fact a URI, it's a path.  That matters in the
513      * case of a leading double-slash.  We need to resolve the issue
514      * by normalising that out before treating it as a URI.
515      */
516     while ((uri[0] == '/') && (uri[1] == '/')) {
517         ++uri ;
518     }
519     if (r->method_number == M_CONNECT) {
520         status = apr_uri_parse_hostinfo(r->pool, uri, &r->parsed_uri);
521     }
522     else {
523         status = apr_uri_parse(r->pool, uri, &r->parsed_uri);
524     }
525
526     if (status == APR_SUCCESS) {
527         /* if it has a scheme we may need to do absoluteURI vhost stuff */
528         if (r->parsed_uri.scheme
529             && !ap_casecmpstr(r->parsed_uri.scheme, ap_http_scheme(r))) {
530             r->hostname = r->parsed_uri.hostname;
531         }
532         else if (r->method_number == M_CONNECT) {
533             r->hostname = r->parsed_uri.hostname;
534         }
535
536         r->args = r->parsed_uri.query;
537         r->uri = r->parsed_uri.path ? r->parsed_uri.path
538                  : apr_pstrdup(r->pool, "/");
539
540 #if defined(OS2) || defined(WIN32)
541         /* Handle path translations for OS/2 and plug security hole.
542          * This will prevent "http://www.wherever.com/..\..\/" from
543          * returning a directory for the root drive.
544          */
545         {
546             char *x;
547
548             for (x = r->uri; (x = strchr(x, '\\')) != NULL; )
549                 *x = '/';
550         }
551 #endif /* OS2 || WIN32 */
552     }
553     else {
554         r->args = NULL;
555         r->hostname = NULL;
556         r->status = HTTP_BAD_REQUEST;             /* set error status */
557         r->uri = apr_pstrdup(r->pool, uri);
558     }
559 }
560
561 static int read_request_line(request_rec *r, apr_bucket_brigade *bb)
562 {
563     const char *ll;
564     const char *uri;
565     const char *pro;
566
567     unsigned int major = 1, minor = 0;   /* Assume HTTP/1.0 if non-"HTTP" protocol */
568     char http[5];
569     apr_size_t len;
570     int num_blank_lines = DEFAULT_LIMIT_BLANK_LINES;
571     core_server_config *conf = ap_get_core_module_config(r->server->module_config);
572     int strict = conf->http_conformance & AP_HTTP_CONFORMANCE_STRICT;
573     int enforce_strict = !(conf->http_conformance & AP_HTTP_CONFORMANCE_LOGONLY);
574
575     /* Read past empty lines until we get a real request line,
576      * a read error, the connection closes (EOF), or we timeout.
577      *
578      * We skip empty lines because browsers have to tack a CRLF on to the end
579      * of POSTs to support old CERN webservers.  But note that we may not
580      * have flushed any previous response completely to the client yet.
581      * We delay the flush as long as possible so that we can improve
582      * performance for clients that are pipelining requests.  If a request
583      * is pipelined then we won't block during the (implicit) read() below.
584      * If the requests aren't pipelined, then the client is still waiting
585      * for the final buffer flush from us, and we will block in the implicit
586      * read().  B_SAFEREAD ensures that the BUFF layer flushes if it will
587      * have to block during a read.
588      */
589
590     do {
591         apr_status_t rv;
592
593         /* ensure ap_rgetline allocates memory each time thru the loop
594          * if there are empty lines
595          */
596         r->the_request = NULL;
597         rv = ap_rgetline(&(r->the_request), (apr_size_t)(r->server->limit_req_line + 2),
598                          &len, r, 0, bb);
599
600         if (rv != APR_SUCCESS) {
601             r->request_time = apr_time_now();
602
603             /* ap_rgetline returns APR_ENOSPC if it fills up the
604              * buffer before finding the end-of-line.  This is only going to
605              * happen if it exceeds the configured limit for a request-line.
606              */
607             if (APR_STATUS_IS_ENOSPC(rv)) {
608                 r->status    = HTTP_REQUEST_URI_TOO_LARGE;
609             }
610             else if (APR_STATUS_IS_TIMEUP(rv)) {
611                 r->status = HTTP_REQUEST_TIME_OUT;
612             }
613             else if (APR_STATUS_IS_EINVAL(rv)) {
614                 r->status = HTTP_BAD_REQUEST;
615             }
616             r->proto_num = HTTP_VERSION(1,0);
617             r->protocol  = "HTTP/1.0";
618             return 0;
619         }
620     } while ((len <= 0) && (--num_blank_lines >= 0));
621
622     if (APLOGrtrace5(r)) {
623         ap_log_rerror(APLOG_MARK, APLOG_TRACE5, 0, r,
624                       "Request received from client: %s",
625                       ap_escape_logitem(r->pool, r->the_request));
626     }
627
628     r->request_time = apr_time_now();
629     ll = r->the_request;
630     r->method = ap_getword_white(r->pool, &ll);
631
632     uri = ap_getword_white(r->pool, &ll);
633
634     if (!*r->method || !*uri) {
635         r->status    = HTTP_BAD_REQUEST;
636         r->proto_num = HTTP_VERSION(1,0);
637         r->protocol  = "HTTP/1.0";
638         return 0;
639     }
640
641     /* Provide quick information about the request method as soon as known */
642
643     r->method_number = ap_method_number_of(r->method);
644     if (r->method_number == M_GET && r->method[0] == 'H') {
645         r->header_only = 1;
646     }
647
648     ap_parse_uri(r, uri);
649     if (r->status != HTTP_OK) {
650         r->proto_num = HTTP_VERSION(1,0);
651         r->protocol  = "HTTP/1.0";
652         return 0;
653     }
654
655     if (ll[0]) {
656         r->assbackwards = 0;
657         pro = ll;
658         len = strlen(ll);
659     } else {
660         r->assbackwards = 1;
661         pro = "HTTP/0.9";
662         len = 8;
663         if (conf->http09_enable == AP_HTTP09_DISABLE) {
664                 r->status = HTTP_VERSION_NOT_SUPPORTED;
665                 r->protocol = apr_pstrmemdup(r->pool, pro, len);
666                 /* If we deny 0.9, send error message with 1.x */
667                 r->assbackwards = 0;
668                 r->proto_num = HTTP_VERSION(0, 9);
669                 r->connection->keepalive = AP_CONN_CLOSE;
670                 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(02401)
671                               "HTTP/0.9 denied by server configuration");
672                 return 0;
673         }
674     }
675     r->protocol = apr_pstrmemdup(r->pool, pro, len);
676
677     /* Avoid sscanf in the common case */
678     if (len == 8
679         && pro[0] == 'H' && pro[1] == 'T' && pro[2] == 'T' && pro[3] == 'P'
680         && pro[4] == '/' && apr_isdigit(pro[5]) && pro[6] == '.'
681         && apr_isdigit(pro[7])) {
682         r->proto_num = HTTP_VERSION(pro[5] - '0', pro[7] - '0');
683     }
684     else {
685         if (strict) {
686             ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(02418)
687                           "Invalid protocol '%s'", r->protocol);
688             if (enforce_strict) {
689                 r->proto_num = HTTP_VERSION(1,0);
690                 r->protocol  = "HTTP/1.0";
691                 r->connection->keepalive = AP_CONN_CLOSE;
692                 r->status = HTTP_BAD_REQUEST;
693                 return 0;
694             }
695         }
696         if (3 == sscanf(r->protocol, "%4s/%u.%u", http, &major, &minor)
697             && (ap_casecmpstr("http", http) == 0)
698             && (minor < HTTP_VERSION(1, 0)) ) { /* don't allow HTTP/0.1000 */
699             r->proto_num = HTTP_VERSION(major, minor);
700         }
701         else {
702             r->proto_num = HTTP_VERSION(1, 0);
703         }
704     }
705
706     if (strict) {
707         int err = 0;
708         if (ap_has_cntrl(r->the_request)) {
709             ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(02420)
710                           "Request line must not contain control characters");
711             err = HTTP_BAD_REQUEST;
712         }
713         if (r->parsed_uri.fragment) {
714             /* RFC3986 3.5: no fragment */
715             ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(02421)
716                           "URI must not contain a fragment");
717             err = HTTP_BAD_REQUEST;
718         }
719         else if (r->parsed_uri.user || r->parsed_uri.password) {
720             ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(02422)
721                           "URI must not contain a username/password");
722             err = HTTP_BAD_REQUEST;
723         }
724         else if (r->method_number == M_INVALID) {
725             ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(02423)
726                           "Invalid HTTP method string: %s", r->method);
727             err = HTTP_NOT_IMPLEMENTED;
728         }
729         else if (r->assbackwards == 0 && r->proto_num < HTTP_VERSION(1, 0)) {
730             ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(02424)
731                           "HTTP/0.x does not take a protocol");
732             err = HTTP_BAD_REQUEST;
733         }
734
735         if (err && enforce_strict) {
736             r->status = err;
737             return 0;
738         }
739     }
740
741     return 1;
742 }
743
744 static int table_do_fn_check_lengths(void *r_, const char *key,
745                                      const char *value)
746 {
747     request_rec *r = r_;
748     if (value == NULL || r->server->limit_req_fieldsize >= strlen(value) )
749         return 1;
750
751     r->status = HTTP_BAD_REQUEST;
752     apr_table_setn(r->notes, "error-notes",
753                    apr_pstrcat(r->pool, "Size of a request header field "
754                                "after merging exceeds server limit.<br />"
755                                "\n<pre>\n",
756                                ap_escape_html(r->pool, key),
757                                "</pre>\n", NULL));
758     ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(00560) "Request header "
759                   "exceeds LimitRequestFieldSize after merging: %s", key);
760     return 0;
761 }
762
763 /* get the length of the field name for logging, but no more than 80 bytes */
764 #define LOG_NAME_MAX_LEN 80
765 static int field_name_len(const char *field)
766 {
767     const char *end = ap_strchr_c(field, ':');
768     if (end == NULL || end - field > LOG_NAME_MAX_LEN)
769         return LOG_NAME_MAX_LEN;
770     return end - field;
771 }
772
773 AP_DECLARE(void) ap_get_mime_headers_core(request_rec *r, apr_bucket_brigade *bb)
774 {
775     char *last_field = NULL;
776     apr_size_t last_len = 0;
777     apr_size_t alloc_len = 0;
778     char *field;
779     char *value;
780     apr_size_t len;
781     int fields_read = 0;
782     char *tmp_field;
783     core_server_config *conf = ap_get_core_module_config(r->server->module_config);
784
785     /*
786      * Read header lines until we get the empty separator line, a read error,
787      * the connection closes (EOF), reach the server limit, or we timeout.
788      */
789     while(1) {
790         apr_status_t rv;
791         int folded = 0;
792
793         field = NULL;
794         rv = ap_rgetline(&field, r->server->limit_req_fieldsize + 2,
795                          &len, r, 0, bb);
796
797         if (rv != APR_SUCCESS) {
798             if (APR_STATUS_IS_TIMEUP(rv)) {
799                 r->status = HTTP_REQUEST_TIME_OUT;
800             }
801             else {
802                 r->status = HTTP_BAD_REQUEST;
803             }
804
805             /* ap_rgetline returns APR_ENOSPC if it fills up the buffer before
806              * finding the end-of-line.  This is only going to happen if it
807              * exceeds the configured limit for a field size.
808              */
809             if (rv == APR_ENOSPC) {
810                 const char *field_escaped;
811                 if (field && len) {
812                     /* ensure ap_escape_html will terminate correctly */
813                     field[len - 1] = '\0';
814                     field_escaped = ap_escape_html(r->pool, field);
815                 }
816                 else {
817                     field_escaped = field = "";
818                 }
819
820                 apr_table_setn(r->notes, "error-notes",
821                                apr_psprintf(r->pool,
822                                            "Size of a request header field "
823                                            "exceeds server limit.<br />\n"
824                                            "<pre>\n%.*s\n</pre>\n", 
825                                            field_name_len(field_escaped),
826                                            field_escaped));
827                 ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(00561)
828                               "Request header exceeds LimitRequestFieldSize%s"
829                               "%.*s",
830                               *field ? ": " : "",
831                               field_name_len(field), field);
832             }
833             return;
834         }
835
836         if (last_field != NULL) {
837             if ((len > 0) && ((*field == '\t') || *field == ' ')) {
838                 /* This line is a continuation of the preceding line(s),
839                  * so append it to the line that we've set aside.
840                  * Note: this uses a power-of-two allocator to avoid
841                  * doing O(n) allocs and using O(n^2) space for
842                  * continuations that span many many lines.
843                  */
844                 apr_size_t fold_len = last_len + len + 1; /* trailing null */
845
846                 if (fold_len >= (apr_size_t)(r->server->limit_req_fieldsize)) {
847                     const char *field_escaped;
848
849                     r->status = HTTP_BAD_REQUEST;
850                     /* report what we have accumulated so far before the
851                      * overflow (last_field) as the field with the problem
852                      */
853                     field_escaped = ap_escape_html(r->pool, last_field);
854                     apr_table_setn(r->notes, "error-notes",
855                                    apr_psprintf(r->pool,
856                                                "Size of a request header field "
857                                                "after folding "
858                                                "exceeds server limit.<br />\n"
859                                                "<pre>\n%.*s\n</pre>\n", 
860                                                field_name_len(field_escaped), 
861                                                field_escaped));
862                     ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(00562)
863                                   "Request header exceeds LimitRequestFieldSize "
864                                   "after folding: %.*s",
865                                   field_name_len(last_field), last_field);
866                     return;
867                 }
868
869                 if (fold_len > alloc_len) {
870                     char *fold_buf;
871                     alloc_len += alloc_len;
872                     if (fold_len > alloc_len) {
873                         alloc_len = fold_len;
874                     }
875                     fold_buf = (char *)apr_palloc(r->pool, alloc_len);
876                     memcpy(fold_buf, last_field, last_len);
877                     last_field = fold_buf;
878                 }
879                 memcpy(last_field + last_len, field, len +1); /* +1 for nul */
880                 last_len += len;
881                 folded = 1;
882             }
883             else /* not a continuation line */ {
884
885                 if (r->server->limit_req_fields
886                     && (++fields_read > r->server->limit_req_fields)) {
887                     r->status = HTTP_BAD_REQUEST;
888                     apr_table_setn(r->notes, "error-notes",
889                                    "The number of request header fields "
890                                    "exceeds this server's limit.");
891                     ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(00563)
892                                   "Number of request headers exceeds "
893                                   "LimitRequestFields");
894                     return;
895                 }
896
897                 if (!(value = strchr(last_field, ':'))) { /* Find ':' or    */
898                     r->status = HTTP_BAD_REQUEST;      /* abort bad request */
899                     apr_table_setn(r->notes, "error-notes",
900                                    apr_psprintf(r->pool,
901                                                "Request header field is "
902                                                "missing ':' separator.<br />\n"
903                                                "<pre>\n%.*s</pre>\n", 
904                                                (int)LOG_NAME_MAX_LEN,
905                                                ap_escape_html(r->pool,
906                                                               last_field)));
907                     ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(00564)
908                                   "Request header field is missing ':' "
909                                   "separator: %.*s", (int)LOG_NAME_MAX_LEN,
910                                   last_field);
911                     return;
912                 }
913
914                 tmp_field = value - 1; /* last character of field-name */
915
916                 *value++ = '\0'; /* NUL-terminate at colon */
917
918                 while (*value == ' ' || *value == '\t') {
919                     ++value;            /* Skip to start of value   */
920                 }
921
922                 /* Strip LWS after field-name: */
923                 while (tmp_field > last_field
924                        && (*tmp_field == ' ' || *tmp_field == '\t')) {
925                     *tmp_field-- = '\0';
926                 }
927
928                 /* Strip LWS after field-value: */
929                 tmp_field = last_field + last_len - 1;
930                 while (tmp_field > value
931                        && (*tmp_field == ' ' || *tmp_field == '\t')) {
932                     *tmp_field-- = '\0';
933                 }
934
935                 if (conf->http_conformance & AP_HTTP_CONFORMANCE_STRICT) {
936                     int err = 0;
937
938                     if (*last_field == '\0') {
939                         err = HTTP_BAD_REQUEST;
940                         ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(02425)
941                                       "Empty request header field name not allowed");
942                     }
943                     else if (ap_has_cntrl(last_field)) {
944                         err = HTTP_BAD_REQUEST;
945                         ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(02426)
946                                       "[HTTP strict] Request header field name contains "
947                                       "control character: %.*s",
948                                       (int)LOG_NAME_MAX_LEN, last_field);
949                     }
950                     else if (ap_has_cntrl(value)) {
951                         err = HTTP_BAD_REQUEST;
952                         ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(02427)
953                                       "Request header field '%.*s' contains "
954                                       "control character", (int)LOG_NAME_MAX_LEN,
955                                       last_field);
956                     }
957                     if (err && !(conf->http_conformance & AP_HTTP_CONFORMANCE_LOGONLY)) {
958                         r->status = err;
959                         return;
960                     }
961                 }
962                 apr_table_addn(r->headers_in, last_field, value);
963
964                 /* reset the alloc_len so that we'll allocate a new
965                  * buffer if we have to do any more folding: we can't
966                  * use the previous buffer because its contents are
967                  * now part of r->headers_in
968                  */
969                 alloc_len = 0;
970
971             } /* end if current line is not a continuation starting with tab */
972         }
973
974         /* Found a blank line, stop. */
975         if (len == 0) {
976             break;
977         }
978
979         /* Keep track of this line so that we can parse it on
980          * the next loop iteration.  (In the folded case, last_field
981          * has been updated already.)
982          */
983         if (!folded) {
984             last_field = field;
985             last_len = len;
986         }
987     }
988
989     /* Combine multiple message-header fields with the same
990      * field-name, following RFC 2616, 4.2.
991      */
992     apr_table_compress(r->headers_in, APR_OVERLAP_TABLES_MERGE);
993
994     /* enforce LimitRequestFieldSize for merged headers */
995     apr_table_do(table_do_fn_check_lengths, r, r->headers_in, NULL);
996 }
997
998 AP_DECLARE(void) ap_get_mime_headers(request_rec *r)
999 {
1000     apr_bucket_brigade *tmp_bb;
1001     tmp_bb = apr_brigade_create(r->pool, r->connection->bucket_alloc);
1002     ap_get_mime_headers_core(r, tmp_bb);
1003     apr_brigade_destroy(tmp_bb);
1004 }
1005
1006 AP_DECLARE(request_rec *) ap_create_request(conn_rec *conn)
1007 {
1008     request_rec *r;
1009     apr_pool_t *p;
1010
1011     apr_pool_create(&p, conn->pool);
1012     apr_pool_tag(p, "request");
1013     r = apr_pcalloc(p, sizeof(request_rec));
1014     AP_READ_REQUEST_ENTRY((intptr_t)r, (uintptr_t)conn);
1015     r->pool            = p;
1016     r->connection      = conn;
1017     r->server          = conn->base_server;
1018
1019     r->user            = NULL;
1020     r->ap_auth_type    = NULL;
1021
1022     r->allowed_methods = ap_make_method_list(p, 2);
1023
1024     r->headers_in      = apr_table_make(r->pool, 25);
1025     r->trailers_in     = apr_table_make(r->pool, 5);
1026     r->subprocess_env  = apr_table_make(r->pool, 25);
1027     r->headers_out     = apr_table_make(r->pool, 12);
1028     r->err_headers_out = apr_table_make(r->pool, 5);
1029     r->trailers_out    = apr_table_make(r->pool, 5);
1030     r->notes           = apr_table_make(r->pool, 5);
1031
1032     r->request_config  = ap_create_request_config(r->pool);
1033     /* Must be set before we run create request hook */
1034
1035     r->proto_output_filters = conn->output_filters;
1036     r->output_filters  = r->proto_output_filters;
1037     r->proto_input_filters = conn->input_filters;
1038     r->input_filters   = r->proto_input_filters;
1039     ap_run_create_request(r);
1040     r->per_dir_config  = r->server->lookup_defaults;
1041
1042     r->sent_bodyct     = 0;                      /* bytect isn't for body */
1043
1044     r->read_length     = 0;
1045     r->read_body       = REQUEST_NO_BODY;
1046
1047     r->status          = HTTP_OK;  /* Until further notice */
1048     r->header_only     = 0;
1049     r->the_request     = NULL;
1050
1051     /* Begin by presuming any module can make its own path_info assumptions,
1052      * until some module interjects and changes the value.
1053      */
1054     r->used_path_info = AP_REQ_DEFAULT_PATH_INFO;
1055
1056     r->useragent_addr = conn->client_addr;
1057     r->useragent_ip = conn->client_ip;
1058
1059     return r;
1060 }
1061
1062 request_rec *ap_read_request(conn_rec *conn)
1063 {
1064     const char *expect;
1065     int access_status;
1066     apr_bucket_brigade *tmp_bb;
1067     apr_socket_t *csd;
1068     apr_interval_time_t cur_timeout;
1069
1070
1071     request_rec *r = ap_create_request(conn);
1072
1073     tmp_bb = apr_brigade_create(r->pool, r->connection->bucket_alloc);
1074
1075     ap_run_pre_read_request(r, conn);
1076
1077     /* Get the request... */
1078     if (!read_request_line(r, tmp_bb)) {
1079         switch (r->status) {
1080         case HTTP_REQUEST_URI_TOO_LARGE:
1081         case HTTP_BAD_REQUEST:
1082         case HTTP_VERSION_NOT_SUPPORTED:
1083         case HTTP_NOT_IMPLEMENTED:
1084             if (r->status == HTTP_REQUEST_URI_TOO_LARGE) {
1085                 ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(00565)
1086                               "request failed: client's request-line exceeds LimitRequestLine (longer than %d)",
1087                               r->server->limit_req_line);
1088             }
1089             else if (r->method == NULL) {
1090                 ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(00566)
1091                               "request failed: invalid characters in URI");
1092             }
1093             access_status = r->status;
1094             r->status = HTTP_OK;
1095             ap_die(access_status, r);
1096             ap_update_child_status(conn->sbh, SERVER_BUSY_LOG, r);
1097             ap_run_log_transaction(r);
1098             r = NULL;
1099             apr_brigade_destroy(tmp_bb);
1100             goto traceout;
1101         case HTTP_REQUEST_TIME_OUT:
1102             ap_update_child_status(conn->sbh, SERVER_BUSY_LOG, r);
1103             if (!r->connection->keepalives)
1104                 ap_run_log_transaction(r);
1105             apr_brigade_destroy(tmp_bb);
1106             goto traceout;
1107         default:
1108             apr_brigade_destroy(tmp_bb);
1109             r = NULL;
1110             goto traceout;
1111         }
1112     }
1113
1114     /* We may have been in keep_alive_timeout mode, so toggle back
1115      * to the normal timeout mode as we fetch the header lines,
1116      * as necessary.
1117      */
1118     csd = ap_get_conn_socket(conn);
1119     apr_socket_timeout_get(csd, &cur_timeout);
1120     if (cur_timeout != conn->base_server->timeout) {
1121         apr_socket_timeout_set(csd, conn->base_server->timeout);
1122         cur_timeout = conn->base_server->timeout;
1123     }
1124
1125     if (!r->assbackwards) {
1126         const char *tenc;
1127
1128         ap_get_mime_headers_core(r, tmp_bb);
1129         if (r->status != HTTP_OK) {
1130             ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(00567)
1131                           "request failed: error reading the headers");
1132             ap_send_error_response(r, 0);
1133             ap_update_child_status(conn->sbh, SERVER_BUSY_LOG, r);
1134             ap_run_log_transaction(r);
1135             apr_brigade_destroy(tmp_bb);
1136             goto traceout;
1137         }
1138
1139         tenc = apr_table_get(r->headers_in, "Transfer-Encoding");
1140         if (tenc) {
1141             /* http://tools.ietf.org/html/draft-ietf-httpbis-p1-messaging-23
1142              * Section 3.3.3.3: "If a Transfer-Encoding header field is
1143              * present in a request and the chunked transfer coding is not
1144              * the final encoding ...; the server MUST respond with the 400
1145              * (Bad Request) status code and then close the connection".
1146              */
1147             if (!(ap_casecmpstr(tenc, "chunked") == 0 /* fast path */
1148                     || ap_find_last_token(r->pool, tenc, "chunked"))) {
1149                 ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(02539)
1150                               "client sent unknown Transfer-Encoding "
1151                               "(%s): %s", tenc, r->uri);
1152                 r->status = HTTP_BAD_REQUEST;
1153                 conn->keepalive = AP_CONN_CLOSE;
1154                 ap_send_error_response(r, 0);
1155                 ap_update_child_status(conn->sbh, SERVER_BUSY_LOG, r);
1156                 ap_run_log_transaction(r);
1157                 apr_brigade_destroy(tmp_bb);
1158                 goto traceout;
1159             }
1160
1161             /* http://tools.ietf.org/html/draft-ietf-httpbis-p1-messaging-23
1162              * Section 3.3.3.3: "If a message is received with both a
1163              * Transfer-Encoding and a Content-Length header field, the
1164              * Transfer-Encoding overrides the Content-Length. ... A sender
1165              * MUST remove the received Content-Length field".
1166              */
1167             apr_table_unset(r->headers_in, "Content-Length");
1168         }
1169     }
1170     else {
1171         if (r->header_only) {
1172             /*
1173              * Client asked for headers only with HTTP/0.9, which doesn't send
1174              * headers! Have to dink things just to make sure the error message
1175              * comes through...
1176              */
1177             ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(00568)
1178                           "client sent invalid HTTP/0.9 request: HEAD %s",
1179                           r->uri);
1180             r->header_only = 0;
1181             r->status = HTTP_BAD_REQUEST;
1182             ap_send_error_response(r, 0);
1183             ap_update_child_status(conn->sbh, SERVER_BUSY_LOG, r);
1184             ap_run_log_transaction(r);
1185             apr_brigade_destroy(tmp_bb);
1186             goto traceout;
1187         }
1188     }
1189
1190     apr_brigade_destroy(tmp_bb);
1191
1192     /* update what we think the virtual host is based on the headers we've
1193      * now read. may update status.
1194      */
1195     ap_update_vhost_from_headers(r);
1196     access_status = r->status;
1197
1198     /* Toggle to the Host:-based vhost's timeout mode to fetch the
1199      * request body and send the response body, if needed.
1200      */
1201     if (cur_timeout != r->server->timeout) {
1202         apr_socket_timeout_set(csd, r->server->timeout);
1203         cur_timeout = r->server->timeout;
1204     }
1205
1206     /* we may have switched to another server */
1207     r->per_dir_config = r->server->lookup_defaults;
1208
1209     if ((!r->hostname && (r->proto_num >= HTTP_VERSION(1, 1)))
1210         || ((r->proto_num == HTTP_VERSION(1, 1))
1211             && !apr_table_get(r->headers_in, "Host"))) {
1212         /*
1213          * Client sent us an HTTP/1.1 or later request without telling us the
1214          * hostname, either with a full URL or a Host: header. We therefore
1215          * need to (as per the 1.1 spec) send an error.  As a special case,
1216          * HTTP/1.1 mentions twice (S9, S14.23) that a request MUST contain
1217          * a Host: header, and the server MUST respond with 400 if it doesn't.
1218          */
1219         access_status = HTTP_BAD_REQUEST;
1220         ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(00569)
1221                       "client sent HTTP/1.1 request without hostname "
1222                       "(see RFC2616 section 14.23): %s", r->uri);
1223     }
1224
1225     /*
1226      * Add the HTTP_IN filter here to ensure that ap_discard_request_body
1227      * called by ap_die and by ap_send_error_response works correctly on
1228      * status codes that do not cause the connection to be dropped and
1229      * in situations where the connection should be kept alive.
1230      */
1231
1232     ap_add_input_filter_handle(ap_http_input_filter_handle,
1233                                NULL, r, r->connection);
1234
1235     if (access_status != HTTP_OK
1236         || (access_status = ap_run_post_read_request(r))) {
1237         ap_die(access_status, r);
1238         ap_update_child_status(conn->sbh, SERVER_BUSY_LOG, r);
1239         ap_run_log_transaction(r);
1240         r = NULL;
1241         goto traceout;
1242     }
1243
1244     if (((expect = apr_table_get(r->headers_in, "Expect")) != NULL)
1245         && (expect[0] != '\0')) {
1246         /*
1247          * The Expect header field was added to HTTP/1.1 after RFC 2068
1248          * as a means to signal when a 100 response is desired and,
1249          * unfortunately, to signal a poor man's mandatory extension that
1250          * the server must understand or return 417 Expectation Failed.
1251          */
1252         if (ap_casecmpstr(expect, "100-continue") == 0) {
1253             r->expecting_100 = 1;
1254         }
1255         else {
1256             core_server_config *conf;
1257
1258             conf = ap_get_core_module_config(r->server->module_config);
1259             if (conf->http_expect_strict != AP_HTTP_EXPECT_STRICT_DISABLE) {
1260                 r->status = HTTP_EXPECTATION_FAILED;
1261                 ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(00570)
1262                               "client sent an unrecognized expectation value "
1263                               "of Expect: %s", expect);
1264                 ap_send_error_response(r, 0);
1265                 ap_update_child_status(conn->sbh, SERVER_BUSY_LOG, r);
1266                 ap_run_log_transaction(r);
1267                 goto traceout;
1268             } else {
1269                 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(02595)
1270                               "client sent an unrecognized expectation value "
1271                               "of Expect (not fatal): %s", expect);
1272             }
1273         }
1274     }
1275
1276     AP_READ_REQUEST_SUCCESS((uintptr_t)r, (char *)r->method, (char *)r->uri, (char *)r->server->defn_name, r->status);
1277     return r;
1278     traceout:
1279     AP_READ_REQUEST_FAILURE((uintptr_t)r);
1280     return r;
1281 }
1282
1283 /* if a request with a body creates a subrequest, remove original request's
1284  * input headers which pertain to the body which has already been read.
1285  * out-of-line helper function for ap_set_sub_req_protocol.
1286  */
1287
1288 static void strip_headers_request_body(request_rec *rnew)
1289 {
1290     apr_table_unset(rnew->headers_in, "Content-Encoding");
1291     apr_table_unset(rnew->headers_in, "Content-Language");
1292     apr_table_unset(rnew->headers_in, "Content-Length");
1293     apr_table_unset(rnew->headers_in, "Content-Location");
1294     apr_table_unset(rnew->headers_in, "Content-MD5");
1295     apr_table_unset(rnew->headers_in, "Content-Range");
1296     apr_table_unset(rnew->headers_in, "Content-Type");
1297     apr_table_unset(rnew->headers_in, "Expires");
1298     apr_table_unset(rnew->headers_in, "Last-Modified");
1299     apr_table_unset(rnew->headers_in, "Transfer-Encoding");
1300 }
1301
1302 /*
1303  * A couple of other functions which initialize some of the fields of
1304  * a request structure, as appropriate for adjuncts of one kind or another
1305  * to a request in progress.  Best here, rather than elsewhere, since
1306  * *someone* has to set the protocol-specific fields...
1307  */
1308
1309 AP_DECLARE(void) ap_set_sub_req_protocol(request_rec *rnew,
1310                                          const request_rec *r)
1311 {
1312     rnew->the_request     = r->the_request;  /* Keep original request-line */
1313
1314     rnew->assbackwards    = 1;   /* Don't send headers from this. */
1315     rnew->no_local_copy   = 1;   /* Don't try to send HTTP_NOT_MODIFIED for a
1316                                   * fragment. */
1317     rnew->method          = "GET";
1318     rnew->method_number   = M_GET;
1319     rnew->protocol        = "INCLUDED";
1320
1321     rnew->status          = HTTP_OK;
1322
1323     rnew->headers_in      = apr_table_copy(rnew->pool, r->headers_in);
1324     rnew->trailers_in     = apr_table_copy(rnew->pool, r->trailers_in);
1325
1326     /* did the original request have a body?  (e.g. POST w/SSI tags)
1327      * if so, make sure the subrequest doesn't inherit body headers
1328      */
1329     if (!r->kept_body && (apr_table_get(r->headers_in, "Content-Length")
1330         || apr_table_get(r->headers_in, "Transfer-Encoding"))) {
1331         strip_headers_request_body(rnew);
1332     }
1333     rnew->subprocess_env  = apr_table_copy(rnew->pool, r->subprocess_env);
1334     rnew->headers_out     = apr_table_make(rnew->pool, 5);
1335     rnew->err_headers_out = apr_table_make(rnew->pool, 5);
1336     rnew->trailers_out    = apr_table_make(rnew->pool, 5);
1337     rnew->notes           = apr_table_make(rnew->pool, 5);
1338
1339     rnew->expecting_100   = r->expecting_100;
1340     rnew->read_length     = r->read_length;
1341     rnew->read_body       = REQUEST_NO_BODY;
1342
1343     rnew->main = (request_rec *) r;
1344 }
1345
1346 static void error_output_stream(request_rec *r, int status)
1347 {
1348     conn_rec *c = r->connection;
1349     apr_bucket_brigade *bb;
1350     apr_bucket *b;
1351
1352     bb = apr_brigade_create(r->pool, c->bucket_alloc);
1353     b = ap_bucket_error_create(status, NULL, r->pool,
1354             r->connection->bucket_alloc);
1355     APR_BRIGADE_INSERT_TAIL(bb, b);
1356     b = apr_bucket_eos_create(c->bucket_alloc);
1357     APR_BRIGADE_INSERT_TAIL(bb, b);
1358     ap_pass_brigade(r->output_filters, bb);
1359 }
1360
1361 static void end_output_stream(request_rec *r)
1362 {
1363     conn_rec *c = r->connection;
1364     apr_bucket_brigade *bb;
1365     apr_bucket *b;
1366
1367     bb = apr_brigade_create(r->pool, c->bucket_alloc);
1368     b = apr_bucket_eos_create(c->bucket_alloc);
1369     APR_BRIGADE_INSERT_TAIL(bb, b);
1370     ap_pass_brigade(r->output_filters, bb);
1371 }
1372
1373 AP_DECLARE(void) ap_finalize_sub_req_protocol(request_rec *sub)
1374 {
1375     /* tell the filter chain there is no more content coming */
1376     if (!sub->eos_sent) {
1377         end_output_stream(sub);
1378     }
1379 }
1380
1381 /* finalize_request_protocol is called at completion of sending the
1382  * response.  Its sole purpose is to send the terminating protocol
1383  * information for any wrappers around the response message body
1384  * (i.e., transfer encodings).  It should have been named finalize_response.
1385  */
1386 AP_DECLARE(void) ap_finalize_request_protocol(request_rec *r)
1387 {
1388     int status = ap_discard_request_body(r);
1389
1390     /* tell the filter chain there is no more content coming */
1391     if (status) {
1392         error_output_stream(r, status);
1393     }
1394     if (!r->eos_sent) {
1395         end_output_stream(r);
1396     }
1397 }
1398
1399 /*
1400  * Support for the Basic authentication protocol, and a bit for Digest.
1401  */
1402 AP_DECLARE(void) ap_note_auth_failure(request_rec *r)
1403 {
1404     const char *type = ap_auth_type(r);
1405     if (type) {
1406         ap_run_note_auth_failure(r, type);
1407     }
1408     else {
1409         ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(00571)
1410                       "need AuthType to note auth failure: %s", r->uri);
1411     }
1412 }
1413
1414 AP_DECLARE(void) ap_note_basic_auth_failure(request_rec *r)
1415 {
1416     ap_note_auth_failure(r);
1417 }
1418
1419 AP_DECLARE(void) ap_note_digest_auth_failure(request_rec *r)
1420 {
1421     ap_note_auth_failure(r);
1422 }
1423
1424 AP_DECLARE(int) ap_get_basic_auth_pw(request_rec *r, const char **pw)
1425 {
1426     const char *auth_line = apr_table_get(r->headers_in,
1427                                           (PROXYREQ_PROXY == r->proxyreq)
1428                                               ? "Proxy-Authorization"
1429                                               : "Authorization");
1430     const char *t;
1431
1432     if (!(t = ap_auth_type(r)) || ap_casecmpstr(t, "Basic"))
1433         return DECLINED;
1434
1435     if (!ap_auth_name(r)) {
1436         ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(00572) 
1437                       "need AuthName: %s", r->uri);
1438         return HTTP_INTERNAL_SERVER_ERROR;
1439     }
1440
1441     if (!auth_line) {
1442         ap_note_auth_failure(r);
1443         return HTTP_UNAUTHORIZED;
1444     }
1445
1446     if (ap_casecmpstr(ap_getword(r->pool, &auth_line, ' '), "Basic")) {
1447         /* Client tried to authenticate using wrong auth scheme */
1448         ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(00573)
1449                       "client used wrong authentication scheme: %s", r->uri);
1450         ap_note_auth_failure(r);
1451         return HTTP_UNAUTHORIZED;
1452     }
1453
1454     while (*auth_line == ' ' || *auth_line == '\t') {
1455         auth_line++;
1456     }
1457
1458     t = ap_pbase64decode(r->pool, auth_line);
1459     r->user = ap_getword_nulls (r->pool, &t, ':');
1460     r->ap_auth_type = "Basic";
1461
1462     *pw = t;
1463
1464     return OK;
1465 }
1466
1467 struct content_length_ctx {
1468     int data_sent;  /* true if the C-L filter has already sent at
1469                      * least one bucket on to the next output filter
1470                      * for this request
1471                      */
1472     apr_bucket_brigade *tmpbb;
1473 };
1474
1475 /* This filter computes the content length, but it also computes the number
1476  * of bytes sent to the client.  This means that this filter will always run
1477  * through all of the buckets in all brigades
1478  */
1479 AP_CORE_DECLARE_NONSTD(apr_status_t) ap_content_length_filter(
1480     ap_filter_t *f,
1481     apr_bucket_brigade *b)
1482 {
1483     request_rec *r = f->r;
1484     struct content_length_ctx *ctx;
1485     apr_bucket *e;
1486     int eos = 0;
1487     apr_read_type_e eblock = APR_NONBLOCK_READ;
1488
1489     ctx = f->ctx;
1490     if (!ctx) {
1491         f->ctx = ctx = apr_palloc(r->pool, sizeof(*ctx));
1492         ctx->data_sent = 0;
1493         ctx->tmpbb = apr_brigade_create(r->pool, r->connection->bucket_alloc);
1494     }
1495
1496     /* Loop through this set of buckets to compute their length
1497      */
1498     e = APR_BRIGADE_FIRST(b);
1499     while (e != APR_BRIGADE_SENTINEL(b)) {
1500         if (APR_BUCKET_IS_EOS(e)) {
1501             eos = 1;
1502             break;
1503         }
1504         if (e->length == (apr_size_t)-1) {
1505             apr_size_t len;
1506             const char *ignored;
1507             apr_status_t rv;
1508
1509             /* This is probably a pipe bucket.  Send everything
1510              * prior to this, and then read the data for this bucket.
1511              */
1512             rv = apr_bucket_read(e, &ignored, &len, eblock);
1513             if (rv == APR_SUCCESS) {
1514                 /* Attempt a nonblocking read next time through */
1515                 eblock = APR_NONBLOCK_READ;
1516                 r->bytes_sent += len;
1517             }
1518             else if (APR_STATUS_IS_EAGAIN(rv)) {
1519                 /* Output everything prior to this bucket, and then
1520                  * do a blocking read on the next batch.
1521                  */
1522                 if (e != APR_BRIGADE_FIRST(b)) {
1523                     apr_bucket *flush;
1524                     apr_brigade_split_ex(b, e, ctx->tmpbb);
1525                     flush = apr_bucket_flush_create(r->connection->bucket_alloc);
1526
1527                     APR_BRIGADE_INSERT_TAIL(b, flush);
1528                     rv = ap_pass_brigade(f->next, b);
1529                     if (rv != APR_SUCCESS || f->c->aborted) {
1530                         return rv;
1531                     }
1532                     apr_brigade_cleanup(b);
1533                     APR_BRIGADE_CONCAT(b, ctx->tmpbb);
1534                     e = APR_BRIGADE_FIRST(b);
1535
1536                     ctx->data_sent = 1;
1537                 }
1538                 eblock = APR_BLOCK_READ;
1539                 continue;
1540             }
1541             else {
1542                 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(00574)
1543                               "ap_content_length_filter: "
1544                               "apr_bucket_read() failed");
1545                 return rv;
1546             }
1547         }
1548         else {
1549             r->bytes_sent += e->length;
1550         }
1551         e = APR_BUCKET_NEXT(e);
1552     }
1553
1554     /* If we've now seen the entire response and it's otherwise
1555      * okay to set the C-L in the response header, then do so now.
1556      *
1557      * We can only set a C-L in the response header if we haven't already
1558      * sent any buckets on to the next output filter for this request.
1559      */
1560     if (ctx->data_sent == 0 && eos) {
1561         core_server_config *conf =
1562             ap_get_core_module_config(r->server->module_config);
1563
1564         /* This is a hack, but I can't find anyway around it.  The idea is that
1565          * we don't want to send out 0 Content-Lengths if it is a HEAD request.
1566          * [Unless the corresponding body (for a GET) would really be empty!]
1567          * This happens when modules try to outsmart the server, and return
1568          * if they see a HEAD request.  Apache 1.3 handlers were supposed to
1569          * just return in that situation, and the core handled the HEAD.  From
1570          * 2.0, if a handler returns, then the core sends an EOS bucket down
1571          * the filter stack, and this content-length filter computes a length
1572          * of zero and we would end up sending a zero C-L to the client.
1573          * We can't just remove the this C-L filter, because well behaved 2.0+
1574          * handlers will send their data down the stack, and we will compute
1575          * a real C-L for the head request. RBB
1576          *
1577          * Allow modification of this behavior through the
1578          * HttpContentLengthHeadZero directive.
1579          *
1580          * The default (unset) behavior is to squelch the C-L in this case.
1581          */
1582
1583         /* don't whack the C-L if it has already been set for a HEAD
1584          * by something like proxy.  the brigade only has an EOS bucket
1585          * in this case, making r->bytes_sent zero, and either there is
1586          * an existing C-L we want to preserve, or r->sent_bodyct is not
1587          * zero (the empty body is being sent) thus we don't want to add
1588          * a C-L of zero (the backend did not provide it, neither do we).
1589          *
1590          * if r->bytes_sent > 0 we have a (temporary) body whose length may
1591          * have been changed by a filter.  the C-L header might not have been
1592          * updated so we do it here.  long term it would be cleaner to have
1593          * such filters update or remove the C-L header, and just use it
1594          * if present.
1595          */
1596         if (!(r->header_only
1597               && !r->bytes_sent
1598               && (r->sent_bodyct
1599                   || conf->http_cl_head_zero != AP_HTTP_CL_HEAD_ZERO_ENABLE
1600                   || apr_table_get(r->headers_out, "Content-Length")))) {
1601             ap_set_content_length(r, r->bytes_sent);
1602         }
1603     }
1604
1605     ctx->data_sent = 1;
1606     return ap_pass_brigade(f->next, b);
1607 }
1608
1609 /*
1610  * Send the body of a response to the client.
1611  */
1612 AP_DECLARE(apr_status_t) ap_send_fd(apr_file_t *fd, request_rec *r,
1613                                     apr_off_t offset, apr_size_t len,
1614                                     apr_size_t *nbytes)
1615 {
1616     conn_rec *c = r->connection;
1617     apr_bucket_brigade *bb = NULL;
1618     apr_status_t rv;
1619
1620     bb = apr_brigade_create(r->pool, c->bucket_alloc);
1621
1622     apr_brigade_insert_file(bb, fd, offset, len, r->pool);
1623
1624     rv = ap_pass_brigade(r->output_filters, bb);
1625     if (rv != APR_SUCCESS) {
1626         *nbytes = 0; /* no way to tell how many were actually sent */
1627     }
1628     else {
1629         *nbytes = len;
1630     }
1631
1632     return rv;
1633 }
1634
1635 #if APR_HAS_MMAP
1636 /* send data from an in-memory buffer */
1637 AP_DECLARE(apr_size_t) ap_send_mmap(apr_mmap_t *mm,
1638                                     request_rec *r,
1639                                     apr_size_t offset,
1640                                     apr_size_t length)
1641 {
1642     conn_rec *c = r->connection;
1643     apr_bucket_brigade *bb = NULL;
1644     apr_bucket *b;
1645
1646     bb = apr_brigade_create(r->pool, c->bucket_alloc);
1647     b = apr_bucket_mmap_create(mm, offset, length, c->bucket_alloc);
1648     APR_BRIGADE_INSERT_TAIL(bb, b);
1649     ap_pass_brigade(r->output_filters, bb);
1650
1651     return mm->size; /* XXX - change API to report apr_status_t? */
1652 }
1653 #endif /* APR_HAS_MMAP */
1654
1655 typedef struct {
1656     apr_bucket_brigade *bb;
1657     apr_bucket_brigade *tmpbb;
1658 } old_write_filter_ctx;
1659
1660 AP_CORE_DECLARE_NONSTD(apr_status_t) ap_old_write_filter(
1661     ap_filter_t *f, apr_bucket_brigade *bb)
1662 {
1663     old_write_filter_ctx *ctx = f->ctx;
1664
1665     AP_DEBUG_ASSERT(ctx);
1666
1667     if (ctx->bb != NULL) {
1668         /* whatever is coming down the pipe (we don't care), we
1669          * can simply insert our buffered data at the front and
1670          * pass the whole bundle down the chain.
1671          */
1672         APR_BRIGADE_PREPEND(bb, ctx->bb);
1673     }
1674
1675     return ap_pass_brigade(f->next, bb);
1676 }
1677
1678 static ap_filter_t *insert_old_write_filter(request_rec *r)
1679 {
1680     ap_filter_t *f;
1681     old_write_filter_ctx *ctx;
1682
1683     /* future optimization: record some flags in the request_rec to
1684      * say whether we've added our filter, and whether it is first.
1685      */
1686
1687     /* this will typically exit on the first test */
1688     for (f = r->output_filters; f != NULL; f = f->next) {
1689         if (ap_old_write_func == f->frec)
1690             break;
1691     }
1692
1693     if (f == NULL) {
1694         /* our filter hasn't been added yet */
1695         ctx = apr_pcalloc(r->pool, sizeof(*ctx));
1696         ctx->tmpbb = apr_brigade_create(r->pool, r->connection->bucket_alloc);
1697
1698         ap_add_output_filter("OLD_WRITE", ctx, r, r->connection);
1699         f = r->output_filters;
1700     }
1701
1702     return f;
1703 }
1704
1705 static apr_status_t buffer_output(request_rec *r,
1706                                   const char *str, apr_size_t len)
1707 {
1708     conn_rec *c = r->connection;
1709     ap_filter_t *f;
1710     old_write_filter_ctx *ctx;
1711
1712     if (len == 0)
1713         return APR_SUCCESS;
1714
1715     f = insert_old_write_filter(r);
1716     ctx = f->ctx;
1717
1718     /* if the first filter is not our buffering filter, then we have to
1719      * deliver the content through the normal filter chain
1720      */
1721     if (f != r->output_filters) {
1722         apr_status_t rv;
1723         apr_bucket *b = apr_bucket_transient_create(str, len, c->bucket_alloc);
1724         APR_BRIGADE_INSERT_TAIL(ctx->tmpbb, b);
1725
1726         rv = ap_pass_brigade(r->output_filters, ctx->tmpbb);
1727         apr_brigade_cleanup(ctx->tmpbb);
1728         return rv;
1729     }
1730
1731     if (ctx->bb == NULL) {
1732         ctx->bb = apr_brigade_create(r->pool, c->bucket_alloc);
1733     }
1734
1735     return ap_fwrite(f->next, ctx->bb, str, len);
1736 }
1737
1738 AP_DECLARE(int) ap_rputc(int c, request_rec *r)
1739 {
1740     char c2 = (char)c;
1741
1742     if (r->connection->aborted) {
1743         return -1;
1744     }
1745
1746     if (buffer_output(r, &c2, 1) != APR_SUCCESS)
1747         return -1;
1748
1749     return c;
1750 }
1751
1752 AP_DECLARE(int) ap_rwrite(const void *buf, int nbyte, request_rec *r)
1753 {
1754     if (r->connection->aborted)
1755         return -1;
1756
1757     if (buffer_output(r, buf, nbyte) != APR_SUCCESS)
1758         return -1;
1759
1760     return nbyte;
1761 }
1762
1763 struct ap_vrprintf_data {
1764     apr_vformatter_buff_t vbuff;
1765     request_rec *r;
1766     char *buff;
1767 };
1768
1769 /* Flush callback for apr_vformatter; returns -1 on error. */
1770 static int r_flush(apr_vformatter_buff_t *buff)
1771 {
1772     /* callback function passed to ap_vformatter to be called when
1773      * vformatter needs to write into buff and buff.curpos > buff.endpos */
1774
1775     /* ap_vrprintf_data passed as a apr_vformatter_buff_t, which is then
1776      * "downcast" to an ap_vrprintf_data */
1777     struct ap_vrprintf_data *vd = (struct ap_vrprintf_data*)buff;
1778
1779     if (vd->r->connection->aborted)
1780         return -1;
1781
1782     /* r_flush is called when vbuff is completely full */
1783     if (buffer_output(vd->r, vd->buff, AP_IOBUFSIZE)) {
1784         return -1;
1785     }
1786
1787     /* reset the buffer position */
1788     vd->vbuff.curpos = vd->buff;
1789     vd->vbuff.endpos = vd->buff + AP_IOBUFSIZE;
1790
1791     return 0;
1792 }
1793
1794 AP_DECLARE(int) ap_vrprintf(request_rec *r, const char *fmt, va_list va)
1795 {
1796     apr_size_t written;
1797     struct ap_vrprintf_data vd;
1798     char vrprintf_buf[AP_IOBUFSIZE];
1799
1800     vd.vbuff.curpos = vrprintf_buf;
1801     vd.vbuff.endpos = vrprintf_buf + AP_IOBUFSIZE;
1802     vd.r = r;
1803     vd.buff = vrprintf_buf;
1804
1805     if (r->connection->aborted)
1806         return -1;
1807
1808     written = apr_vformatter(r_flush, &vd.vbuff, fmt, va);
1809
1810     if (written != -1) {
1811         int n = vd.vbuff.curpos - vrprintf_buf;
1812
1813         /* last call to buffer_output, to finish clearing the buffer */
1814         if (buffer_output(r, vrprintf_buf,n) != APR_SUCCESS)
1815             return -1;
1816
1817         written += n;
1818     }
1819
1820     return written;
1821 }
1822
1823 AP_DECLARE_NONSTD(int) ap_rprintf(request_rec *r, const char *fmt, ...)
1824 {
1825     va_list va;
1826     int n;
1827
1828     if (r->connection->aborted)
1829         return -1;
1830
1831     va_start(va, fmt);
1832     n = ap_vrprintf(r, fmt, va);
1833     va_end(va);
1834
1835     return n;
1836 }
1837
1838 AP_DECLARE_NONSTD(int) ap_rvputs(request_rec *r, ...)
1839 {
1840     va_list va;
1841     const char *s;
1842     apr_size_t len;
1843     apr_size_t written = 0;
1844
1845     if (r->connection->aborted)
1846         return -1;
1847
1848     /* ### TODO: if the total output is large, put all the strings
1849      * ### into a single brigade, rather than flushing each time we
1850      * ### fill the buffer
1851      */
1852     va_start(va, r);
1853     while (1) {
1854         s = va_arg(va, const char *);
1855         if (s == NULL)
1856             break;
1857
1858         len = strlen(s);
1859         if (buffer_output(r, s, len) != APR_SUCCESS) {
1860             return -1;
1861         }
1862
1863         written += len;
1864     }
1865     va_end(va);
1866
1867     return written;
1868 }
1869
1870 AP_DECLARE(int) ap_rflush(request_rec *r)
1871 {
1872     conn_rec *c = r->connection;
1873     apr_bucket *b;
1874     ap_filter_t *f;
1875     old_write_filter_ctx *ctx;
1876     apr_status_t rv;
1877
1878     f = insert_old_write_filter(r);
1879     ctx = f->ctx;
1880
1881     b = apr_bucket_flush_create(c->bucket_alloc);
1882     APR_BRIGADE_INSERT_TAIL(ctx->tmpbb, b);
1883
1884     rv = ap_pass_brigade(r->output_filters, ctx->tmpbb);
1885     apr_brigade_cleanup(ctx->tmpbb);
1886     if (rv != APR_SUCCESS)
1887         return -1;
1888
1889     return 0;
1890 }
1891
1892 /*
1893  * This function sets the Last-Modified output header field to the value
1894  * of the mtime field in the request structure - rationalized to keep it from
1895  * being in the future.
1896  */
1897 AP_DECLARE(void) ap_set_last_modified(request_rec *r)
1898 {
1899     if (!r->assbackwards) {
1900         apr_time_t mod_time = ap_rationalize_mtime(r, r->mtime);
1901         char *datestr = apr_palloc(r->pool, APR_RFC822_DATE_LEN);
1902
1903         apr_rfc822_date(datestr, mod_time);
1904         apr_table_setn(r->headers_out, "Last-Modified", datestr);
1905     }
1906 }
1907
1908 typedef struct hdr_ptr {
1909     ap_filter_t *f;
1910     apr_bucket_brigade *bb;
1911 } hdr_ptr;
1912 static int send_header(void *data, const char *key, const char *val)
1913 {
1914     ap_fputstrs(((hdr_ptr*)data)->f, ((hdr_ptr*)data)->bb,
1915                 key, ": ", val, CRLF, NULL);
1916     return 1;
1917 }
1918 AP_DECLARE(void) ap_send_interim_response(request_rec *r, int send_headers)
1919 {
1920     hdr_ptr x;
1921     char *status_line = NULL;
1922     request_rec *rr;
1923
1924     if (r->proto_num < HTTP_VERSION(1,1)) {
1925         /* don't send interim response to HTTP/1.0 Client */
1926         return;
1927     }
1928     if (!ap_is_HTTP_INFO(r->status)) {
1929         ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(00575)
1930                       "Status is %d - not sending interim response", r->status);
1931         return;
1932     }
1933     if ((r->status == HTTP_CONTINUE) && !r->expecting_100) {
1934         /*
1935          * Don't send 100-Continue when there was no Expect: 100-continue
1936          * in the request headers. For origin servers this is a SHOULD NOT
1937          * for proxies it is a MUST NOT according to RFC 2616 8.2.3
1938          */
1939         return;
1940     }
1941
1942     /* if we send an interim response, we're no longer in a state of
1943      * expecting one.  Also, this could feasibly be in a subrequest,
1944      * so we need to propagate the fact that we responded.
1945      */
1946     for (rr = r; rr != NULL; rr = rr->main) {
1947         rr->expecting_100 = 0;
1948     }
1949
1950     status_line = apr_pstrcat(r->pool, AP_SERVER_PROTOCOL, " ", r->status_line, CRLF, NULL);
1951     ap_xlate_proto_to_ascii(status_line, strlen(status_line));
1952
1953     x.f = r->connection->output_filters;
1954     x.bb = apr_brigade_create(r->pool, r->connection->bucket_alloc);
1955
1956     ap_fputs(x.f, x.bb, status_line);
1957     if (send_headers) {
1958         apr_table_do(send_header, &x, r->headers_out, NULL);
1959         apr_table_clear(r->headers_out);
1960     }
1961     ap_fputs(x.f, x.bb, CRLF_ASCII);
1962     ap_fflush(x.f, x.bb);
1963     apr_brigade_destroy(x.bb);
1964 }
1965
1966 /*
1967  * Compare two protocol identifier. Result is similar to strcmp():
1968  * 0 gives same precedence, >0 means proto1 is preferred.
1969  */
1970 static int protocol_cmp(const apr_array_header_t *preferences,
1971                         const char *proto1,
1972                         const char *proto2)
1973 {
1974     if (preferences && preferences->nelts > 0) {
1975         int index1 = ap_array_str_index(preferences, proto1, 0);
1976         int index2 = ap_array_str_index(preferences, proto2, 0);
1977         if (index2 > index1) {
1978             return (index1 >= 0) ? 1 : -1;
1979         }
1980         else if (index1 > index2) {
1981             return (index2 >= 0) ? -1 : 1;
1982         }
1983     }
1984     /* both have the same index (mabye -1 or no pref configured) and we compare
1985      * the names so that spdy3 gets precedence over spdy2. That makes
1986      * the outcome at least deterministic. */
1987     return strcmp(proto1, proto2);
1988 }
1989
1990 AP_DECLARE(const char *) ap_get_protocol(conn_rec *c)
1991 {
1992     const char *protocol = ap_run_protocol_get(c);
1993     return protocol? protocol : AP_PROTOCOL_HTTP1;
1994 }
1995
1996 AP_DECLARE(apr_status_t) ap_get_protocol_upgrades(conn_rec *c, request_rec *r, 
1997                                                   server_rec *s, int report_all, 
1998                                                   const apr_array_header_t **pupgrades)
1999 {
2000     apr_pool_t *pool = r? r->pool : c->pool;
2001     core_server_config *conf;
2002     const char *existing;
2003     apr_array_header_t *upgrades = NULL;
2004
2005     if (!s) {
2006         s = (r? r->server : c->base_server);
2007     }
2008     conf = ap_get_core_module_config(s->module_config);
2009     
2010     if (conf->protocols->nelts > 0) {
2011         existing = ap_get_protocol(c);
2012         if (conf->protocols->nelts > 1 
2013             || !ap_array_str_contains(conf->protocols, existing)) {
2014             int i;
2015             
2016             /* possibly more than one choice or one, but not the
2017              * existing. (TODO: maybe 426 and Upgrade then?) */
2018             upgrades = apr_array_make(pool, conf->protocols->nelts + 1, 
2019                                       sizeof(char *));
2020             for (i = 0; i < conf->protocols->nelts; i++) {
2021                 const char *p = APR_ARRAY_IDX(conf->protocols, i, char *);
2022                 /* special quirk for HTTP/2 which does not allow 'h2' to
2023                  * be part of an Upgrade: header */
2024                 if (!strcmp("h2", p)) {
2025                     continue;
2026                 }
2027                 else if (strcmp(existing, p)) {
2028                     /* not the one we have and possible, add in this order */
2029                     APR_ARRAY_PUSH(upgrades, const char*) = p;
2030                 }
2031                 else if (!report_all) {
2032                     break;
2033                 }
2034             }
2035         }
2036     }
2037     
2038     *pupgrades = upgrades;
2039     return APR_SUCCESS;
2040 }
2041
2042 AP_DECLARE(const char *) ap_select_protocol(conn_rec *c, request_rec *r, 
2043                                             server_rec *s,
2044                                             const apr_array_header_t *choices)
2045 {
2046     apr_pool_t *pool = r? r->pool : c->pool;
2047     core_server_config *conf;
2048     const char *protocol = NULL, *existing;
2049     apr_array_header_t *proposals;
2050
2051     if (!s) {
2052         s = (r? r->server : c->base_server);
2053     }
2054     conf = ap_get_core_module_config(s->module_config);
2055     
2056     if (APLOGcdebug(c)) {
2057         const char *p = apr_array_pstrcat(pool, conf->protocols, ',');
2058         ap_log_cerror(APLOG_MARK, APLOG_DEBUG, 0, c, APLOGNO(03155) 
2059                       "select protocol from %s, choices=%s for server %s", 
2060                       p, apr_array_pstrcat(pool, choices, ','),
2061                       s->server_hostname);
2062     }
2063
2064     if (conf->protocols->nelts <= 0) {
2065         /* nothing configured, by default, we only allow http/1.1 here.
2066          * For now...
2067          */
2068         if (ap_array_str_contains(choices, AP_PROTOCOL_HTTP1)) {
2069             return AP_PROTOCOL_HTTP1;
2070         }
2071         else {
2072             return NULL;
2073         }
2074     }
2075
2076     proposals = apr_array_make(pool, choices->nelts + 1, sizeof(char *));
2077     ap_run_protocol_propose(c, r, s, choices, proposals);
2078
2079     /* If the existing protocol has not been proposed, but is a choice,
2080      * add it to the proposals implicitly.
2081      */
2082     existing = ap_get_protocol(c);
2083     if (!ap_array_str_contains(proposals, existing)
2084         && ap_array_str_contains(choices, existing)) {
2085         APR_ARRAY_PUSH(proposals, const char*) = existing;
2086     }
2087
2088     if (proposals->nelts > 0) {
2089         int i;
2090         const apr_array_header_t *prefs = NULL;
2091
2092         /* Default for protocols_honor_order is 'on' or != 0 */
2093         if (conf->protocols_honor_order == 0 && choices->nelts > 0) {
2094             prefs = choices;
2095         }
2096         else {
2097             prefs = conf->protocols;
2098         }
2099
2100         /* Select the most preferred protocol */
2101         if (APLOGcdebug(c)) {
2102             ap_log_cerror(APLOG_MARK, APLOG_DEBUG, 0, c, APLOGNO(03156) 
2103                           "select protocol, proposals=%s preferences=%s configured=%s", 
2104                           apr_array_pstrcat(pool, proposals, ','),
2105                           apr_array_pstrcat(pool, prefs, ','),
2106                           apr_array_pstrcat(pool, conf->protocols, ','));
2107         }
2108         for (i = 0; i < proposals->nelts; ++i) {
2109             const char *p = APR_ARRAY_IDX(proposals, i, const char *);
2110             if (!ap_array_str_contains(conf->protocols, p)) {
2111                 /* not a configured protocol here */
2112                 continue;
2113             }
2114             else if (!protocol 
2115                      || (protocol_cmp(prefs, protocol, p) < 0)) {
2116                 /* none selected yet or this one has preference */
2117                 protocol = p;
2118             }
2119         }
2120     }
2121     if (APLOGcdebug(c)) {
2122         ap_log_cerror(APLOG_MARK, APLOG_DEBUG, 0, c, APLOGNO(03157)
2123                       "selected protocol=%s", 
2124                       protocol? protocol : "(none)");
2125     }
2126
2127     return protocol;
2128 }
2129
2130 AP_DECLARE(apr_status_t) ap_switch_protocol(conn_rec *c, request_rec *r, 
2131                                             server_rec *s,
2132                                             const char *protocol)
2133 {
2134     const char *current = ap_get_protocol(c);
2135     int rc;
2136     
2137     if (!strcmp(current, protocol)) {
2138         ap_log_cerror(APLOG_MARK, APLOG_WARNING, 0, c, APLOGNO(02906)
2139                       "already at it, protocol_switch to %s", 
2140                       protocol);
2141         return APR_SUCCESS;
2142     }
2143     
2144     rc = ap_run_protocol_switch(c, r, s, protocol);
2145     switch (rc) {
2146         case DECLINED:
2147             ap_log_cerror(APLOG_MARK, APLOG_ERR, 0, c, APLOGNO(02907)
2148                           "no implementation for protocol_switch to %s", 
2149                           protocol);
2150             return APR_ENOTIMPL;
2151         case OK:
2152         case DONE:
2153             return APR_SUCCESS;
2154         default:
2155             ap_log_cerror(APLOG_MARK, APLOG_ERR, 0, c, APLOGNO(02905)
2156                           "unexpected return code %d from protocol_switch to %s"
2157                           , rc, protocol);
2158             return APR_EOF;
2159     }    
2160 }
2161
2162 AP_DECLARE(int) ap_is_allowed_protocol(conn_rec *c, request_rec *r,
2163                                        server_rec *s, const char *protocol)
2164 {
2165     core_server_config *conf;
2166
2167     if (!s) {
2168         s = (r? r->server : c->base_server);
2169     }
2170     conf = ap_get_core_module_config(s->module_config);
2171     
2172     if (conf->protocols->nelts > 0) {
2173         return ap_array_str_contains(conf->protocols, protocol);
2174     }
2175     return !strcmp(AP_PROTOCOL_HTTP1, protocol);
2176 }
2177
2178
2179 AP_IMPLEMENT_HOOK_VOID(pre_read_request,
2180                        (request_rec *r, conn_rec *c),
2181                        (r, c))
2182 AP_IMPLEMENT_HOOK_RUN_ALL(int,post_read_request,
2183                           (request_rec *r), (r), OK, DECLINED)
2184 AP_IMPLEMENT_HOOK_RUN_ALL(int,log_transaction,
2185                           (request_rec *r), (r), OK, DECLINED)
2186 AP_IMPLEMENT_HOOK_RUN_FIRST(const char *,http_scheme,
2187                             (const request_rec *r), (r), NULL)
2188 AP_IMPLEMENT_HOOK_RUN_FIRST(unsigned short,default_port,
2189                             (const request_rec *r), (r), 0)
2190 AP_IMPLEMENT_HOOK_RUN_FIRST(int, note_auth_failure,
2191                             (request_rec *r, const char *auth_type),
2192                             (r, auth_type), DECLINED)
2193 AP_IMPLEMENT_HOOK_RUN_ALL(int,protocol_propose,
2194                           (conn_rec *c, request_rec *r, server_rec *s,
2195                            const apr_array_header_t *offers,
2196                            apr_array_header_t *proposals), 
2197                           (c, r, s, offers, proposals), OK, DECLINED)
2198 AP_IMPLEMENT_HOOK_RUN_FIRST(int,protocol_switch,
2199                             (conn_rec *c, request_rec *r, server_rec *s,
2200                              const char *protocol), 
2201                             (c, r, s, protocol), DECLINED)
2202 AP_IMPLEMENT_HOOK_RUN_FIRST(const char *,protocol_get,
2203                             (const conn_rec *c), (c), NULL)