]> granicus.if.org Git - apache/blobdiff - server/protocol.c
WIN64: API changes to clean up Windows 64bit compile warnings
[apache] / server / protocol.c
index a0f2ad18afb779bc41a02f0e0d04328a99e8612b..445c72fe27e9906123fa3c427e1c8cf8bbac79e2 100644 (file)
@@ -1,59 +1,16 @@
-/* ====================================================================
- * The Apache Software License, Version 1.1
+/* Copyright 2001-2004 The Apache Software Foundation
  *
- * Copyright (c) 2000-2002 The Apache Software Foundation.  All rights
- * reserved.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
  *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
+ *     http://www.apache.org/licenses/LICENSE-2.0
  *
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in
- *    the documentation and/or other materials provided with the
- *    distribution.
- *
- * 3. The end-user documentation included with the redistribution,
- *    if any, must include the following acknowledgment:
- *       "This product includes software developed by the
- *        Apache Software Foundation (http://www.apache.org/)."
- *    Alternately, this acknowledgment may appear in the software itself,
- *    if and wherever such third-party acknowledgments normally appear.
- *
- * 4. The names "Apache" and "Apache Software Foundation" must
- *    not be used to endorse or promote products derived from this
- *    software without prior written permission. For written
- *    permission, please contact apache@apache.org.
- *
- * 5. Products derived from this software may not be called "Apache",
- *    nor may "Apache" appear in their name, without prior written
- *    permission of the Apache Software Foundation.
- *
- * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
- * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
- * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
- * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
- * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
- * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- * ====================================================================
- *
- * This software consists of voluntary contributions made by many
- * individuals on behalf of the Apache Software Foundation.  For more
- * information on the Apache Software Foundation, please see
- * <http://www.apache.org/>.
- *
- * Portions of this software are based upon public domain software
- * originally written at the National Center for Supercomputing Applications,
- * University of Illinois, Urbana-Champaign.
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
  */
 
 /*
@@ -90,6 +47,7 @@
 #include "mod_core.h"
 #include "util_charset.h"
 #include "util_ebcdic.h"
+#include "scoreboard.h"
 
 #if APR_HAVE_STDARG_H
 #include <stdarg.h>
@@ -251,146 +209,100 @@ AP_DECLARE(apr_status_t) ap_rgetline_core(char **s, apr_size_t n,
     char *pos, *last_char = *s;
     int do_alloc = (*s == NULL), saw_eos = 0;
 
-    apr_brigade_cleanup(bb);
-    rv = ap_get_brigade(r->input_filters, bb, AP_MODE_GETLINE,
-                        APR_BLOCK_READ, 0);
-
-    if (rv != APR_SUCCESS) {
-        return rv;
-    }
-
-    /* Something horribly wrong happened.  Someone didn't block! */
-    if (APR_BRIGADE_EMPTY(bb)) {
-        return APR_EGENERAL;
-    }
-
-    APR_BRIGADE_FOREACH(e, bb) {
-        const char *str;
-        apr_size_t len;
-
-        /* If we see an EOS, don't bother doing anything more. */
-        if (APR_BUCKET_IS_EOS(e)) {
-            saw_eos = 1;
-            break;
-        }
-
-        rv = apr_bucket_read(e, &str, &len, APR_BLOCK_READ);
-
+    for (;;) {
+        apr_brigade_cleanup(bb);
+        rv = ap_get_brigade(r->input_filters, bb, AP_MODE_GETLINE,
+                            APR_BLOCK_READ, 0);
         if (rv != APR_SUCCESS) {
             return rv;
         }
-
-        if (len == 0) {
-            /* no use attempting a zero-byte alloc (hurts when
-             * using --with-efence --enable-pool-debug) or
-             * doing any of the other logic either
-             */
-            continue;
-        }
-
-        /* Would this overrun our buffer?  If so, we'll die. */
-        if (n < bytes_handled + len) {
-            return APR_ENOSPC;
-        }
-
-        /* Do we have to handle the allocation ourselves? */
-        if (do_alloc) {
-            /* We'll assume the common case where one bucket is enough. */
-            if (!*s) {
-                current_alloc = len;
-                if (current_alloc < MIN_LINE_ALLOC) {
-                    current_alloc = MIN_LINE_ALLOC;
-                }
-                *s = apr_palloc(r->pool, current_alloc);
-            }
-            else if (bytes_handled + len > current_alloc) {
-                /* Increase the buffer size */
-                apr_size_t new_size = current_alloc * 2;
-                char *new_buffer;
-
-                if (bytes_handled + len > new_size) {
-                    new_size = (bytes_handled + len) * 2;
-                }
-
-                new_buffer = apr_palloc(r->pool, new_size);
-
-                /* Copy what we already had. */
-                memcpy(new_buffer, *s, bytes_handled);
-                current_alloc = new_size;
-                *s = new_buffer;
-            }
+        
+        /* Something horribly wrong happened.  Someone didn't block! */
+        if (APR_BRIGADE_EMPTY(bb)) {
+            return APR_EGENERAL;
         }
-
-        /* Just copy the rest of the data to the end of the old buffer. */
-        pos = *s + bytes_handled;
-        memcpy(pos, str, len);
-        last_char = pos + len - 1;
-
-        /* We've now processed that new data - update accordingly. */
-        bytes_handled += len;
-    }
-
-    /* We likely aborted early before reading anything or we read no
-     * data.  Technically, this might be success condition.  But,
-     * probably means something is horribly wrong.  For now, we'll
-     * treat this as APR_SUCCESS, but it may be worth re-examining.
-     */
-    if (bytes_handled == 0) {
-        *read = 0;
-        return APR_SUCCESS;
-    }
-
-    /* If we didn't get a full line of input, try again. */
-    if (*last_char != APR_ASCII_LF) {
-        /* Do we have enough space? We may be full now. */
-        if (bytes_handled < n) {
-            apr_size_t next_size, next_len;
-            char *tmp;
-
-            /* If we're doing the allocations for them, we have to
-             * give ourselves a NULL and copy it on return.
-             */
-            if (do_alloc) {
-                tmp = NULL;
-            } else {
-                /* We're not null terminated yet. */
-                tmp = last_char + 1;
+        
+        for (e = APR_BRIGADE_FIRST(bb);
+             e != APR_BRIGADE_SENTINEL(bb);
+             e = APR_BUCKET_NEXT(e))
+        {
+            const char *str;
+            apr_size_t len;
+            
+            /* If we see an EOS, don't bother doing anything more. */
+            if (APR_BUCKET_IS_EOS(e)) {
+                saw_eos = 1;
+                break;
             }
-
-            next_size = n - bytes_handled;
-
-            rv = ap_rgetline_core(&tmp, next_size, &next_len, r, fold, bb);
-
+            
+            rv = apr_bucket_read(e, &str, &len, APR_BLOCK_READ);
             if (rv != APR_SUCCESS) {
                 return rv;
             }
-
-            /* XXX this code appears to be dead because the filter chain
-             * seems to read until it sees a LF or an error.  If it ever
-             * comes back to life, we need to make sure that:
-             * - we really alloc enough space for the trailing null
-             * - we don't allow the tail trimming code to run more than
-             *   once
-             */
-            if (do_alloc && next_len > 0) {
-                char *new_buffer;
-                apr_size_t new_size = bytes_handled + next_len;
-
-                /* Again we need to alloc an extra two bytes for LF, null */
-                new_buffer = apr_palloc(r->pool, new_size);
-
-                /* Copy what we already had. */
-                memcpy(new_buffer, *s, bytes_handled);
-                memcpy(new_buffer + bytes_handled, tmp, next_len);
-                current_alloc = new_size;
-                *s = new_buffer;
+            
+            if (len == 0) {
+                /* no use attempting a zero-byte alloc (hurts when
+                 * using --with-efence --enable-pool-debug) or
+                 * doing any of the other logic either
+                 */
+                continue;
+            }
+            
+            /* Would this overrun our buffer?  If so, we'll die. */
+            if (n < bytes_handled + len) {
+                *read = bytes_handled;
+                if (*s) {
+                    /* ensure this string is NUL terminated */
+                    if (bytes_handled > 0) {
+                        (*s)[bytes_handled-1] = '\0';
+                    }
+                    else {
+                        (*s)[0] = '\0';
+                    }
+                }
+                return APR_ENOSPC;
+            }
+            
+            /* Do we have to handle the allocation ourselves? */
+            if (do_alloc) {
+                /* We'll assume the common case where one bucket is enough. */
+                if (!*s) {
+                    current_alloc = len;
+                    if (current_alloc < MIN_LINE_ALLOC) {
+                        current_alloc = MIN_LINE_ALLOC;
+                    }
+                    *s = apr_palloc(r->pool, current_alloc);
+                }
+                else if (bytes_handled + len > current_alloc) {
+                    /* Increase the buffer size */
+                    apr_size_t new_size = current_alloc * 2;
+                    char *new_buffer;
+                    
+                    if (bytes_handled + len > new_size) {
+                        new_size = (bytes_handled + len) * 2;
+                    }
+                    
+                    new_buffer = apr_palloc(r->pool, new_size);
+                    
+                    /* Copy what we already had. */
+                    memcpy(new_buffer, *s, bytes_handled);
+                    current_alloc = new_size;
+                    *s = new_buffer;
+                }
             }
 
-            bytes_handled += next_len;
-            last_char = *s + bytes_handled - 1;
+            /* Just copy the rest of the data to the end of the old buffer. */
+            pos = *s + bytes_handled;
+            memcpy(pos, str, len);
+            last_char = pos + len - 1;
+            
+            /* We've now processed that new data - update accordingly. */
+            bytes_handled += len;
         }
-        else {
-            return APR_ENOSPC;
+        
+        /* If we got a full line of input, stop reading */
+        if (last_char && (*last_char == APR_ASCII_LF)) {
+            break;
         }
     }
 
@@ -429,97 +341,100 @@ AP_DECLARE(apr_status_t) ap_rgetline_core(char **s, apr_size_t n,
      * Note that if an EOS was seen, we know we can't have another line.
      */
     if (fold && bytes_handled && !saw_eos) {
-        const char *str;
-        apr_size_t len;
-        char c;
-
-        /* Clear the temp brigade for this filter read. */
-        apr_brigade_cleanup(bb);
-
-        /* We only care about the first byte. */
-        rv = ap_get_brigade(r->input_filters, bb, AP_MODE_SPECULATIVE,
-                            APR_BLOCK_READ, 1);
-
-        if (rv != APR_SUCCESS) {
-            return rv;
-        }
-
-        if (APR_BRIGADE_EMPTY(bb)) {
-            *read = bytes_handled;
-            return APR_SUCCESS;
-        }
-
-        e = APR_BRIGADE_FIRST(bb);
-
-        /* If we see an EOS, don't bother doing anything more. */
-        if (APR_BUCKET_IS_EOS(e)) {
-            *read = bytes_handled;
-            return APR_SUCCESS;
-        }
-
-        rv = apr_bucket_read(e, &str, &len, APR_BLOCK_READ);
-
-        if (rv != APR_SUCCESS) {
-            apr_brigade_destroy(bb);
-            return rv;
-        }
-
-        /* When we call destroy, the buckets are deleted, so save that
-         * one character we need.  This simplifies our execution paths
-         * at the cost of one character read.
-         */
-        c = *str;
-
-        /* Found one, so call ourselves again to get the next line.
-         *
-         * FIXME: If the folding line is completely blank, should we
-         * stop folding?  Does that require also looking at the next
-         * char?
-         */
-        if (c == APR_ASCII_BLANK || c == APR_ASCII_TAB) {
-            /* Do we have enough space? We may be full now. */
-            if (bytes_handled < n) {
-                apr_size_t next_size, next_len;
-                char *tmp;
-
-                /* If we're doing the allocations for them, we have to
-                 * give ourselves a NULL and copy it on return.
-                 */
-                if (do_alloc) {
-                    tmp = NULL;
-                } else {
-                    /* We're null terminated. */
-                    tmp = last_char;
-                }
-
-                next_size = n - bytes_handled;
-
-                rv = ap_rgetline_core(&tmp, next_size, &next_len, r, fold, bb);
-
-                if (rv != APR_SUCCESS) {
-                    return rv;
+        for (;;) {
+            const char *str;
+            apr_size_t len;
+            char c;
+            
+            /* Clear the temp brigade for this filter read. */
+            apr_brigade_cleanup(bb);
+            
+            /* We only care about the first byte. */
+            rv = ap_get_brigade(r->input_filters, bb, AP_MODE_SPECULATIVE,
+                                APR_BLOCK_READ, 1);
+            if (rv != APR_SUCCESS) {
+                return rv;
+            }
+            
+            if (APR_BRIGADE_EMPTY(bb)) {
+                break;
+            }
+            
+            e = APR_BRIGADE_FIRST(bb);
+            
+            /* If we see an EOS, don't bother doing anything more. */
+            if (APR_BUCKET_IS_EOS(e)) {
+                break;
+            }
+            
+            rv = apr_bucket_read(e, &str, &len, APR_BLOCK_READ);
+            if (rv != APR_SUCCESS) {
+                apr_brigade_cleanup(bb);
+                return rv;
+            }
+            
+            /* Found one, so call ourselves again to get the next line.
+             *
+             * FIXME: If the folding line is completely blank, should we
+             * stop folding?  Does that require also looking at the next
+             * char?
+             */
+            /* When we call destroy, the buckets are deleted, so save that
+             * one character we need.  This simplifies our execution paths
+             * at the cost of one character read.
+             */
+            c = *str;
+            if (c == APR_ASCII_BLANK || c == APR_ASCII_TAB) {
+                /* Do we have enough space? We may be full now. */
+                if (bytes_handled >= n) {
+                    *read = n;
+                    /* ensure this string is terminated */
+                    (*s)[n-1] = '\0';
+                    return APR_ENOSPC;
                 }
+                else {
+                    apr_size_t next_size, next_len;
+                    char *tmp;
+                    
+                    /* If we're doing the allocations for them, we have to
+                     * give ourselves a NULL and copy it on return.
+                     */
+                    if (do_alloc) {
+                        tmp = NULL;
+                    } else {
+                        /* We're null terminated. */
+                        tmp = last_char;
+                    }
+                    
+                    next_size = n - bytes_handled;
+                    
+                    rv = ap_rgetline_core(&tmp, next_size,
+                                          &next_len, r, 0, bb);
+                    if (rv != APR_SUCCESS) {
+                        return rv;
+                    }
+                    
+                    if (do_alloc && next_len > 0) {
+                        char *new_buffer;
+                        apr_size_t new_size = bytes_handled + next_len + 1;
+                        
+                        /* we need to alloc an extra byte for a null */
+                        new_buffer = apr_palloc(r->pool, new_size);
+
+                        /* Copy what we already had. */
+                        memcpy(new_buffer, *s, bytes_handled);
+                        
+                        /* copy the new line, including the trailing null */
+                        memcpy(new_buffer + bytes_handled, tmp, next_len + 1);
+                        *s = new_buffer;
+                    }
 
-                if (do_alloc && next_len > 0) {
-                    char *new_buffer;
-                    apr_size_t new_size = bytes_handled + next_len + 1;
-
-                    /* we need to alloc an extra byte for a null */
-                    new_buffer = apr_palloc(r->pool, new_size);
-
-                    /* Copy what we already had. */
-                    memcpy(new_buffer, *s, bytes_handled);
-
-                    /* copy the new line, including the trailing null */
-                    memcpy(new_buffer + bytes_handled, tmp, next_len + 1);
-                    *s = new_buffer;
+                    last_char += next_len;
+                    bytes_handled += next_len;
                 }
-
-                *read = bytes_handled + next_len;
-                return APR_SUCCESS;
             }
-            else {
-                return APR_ENOSPC;
+            else { /* next character is not tab or space */
+                break;
             }
         }
     }
@@ -644,6 +559,12 @@ static int read_request_line(request_rec *r, apr_bucket_brigade *bb)
     int major = 1, minor = 0;   /* Assume HTTP/1.0 if non-"HTTP" protocol */
     char http[5];
     apr_size_t len;
+    int num_blank_lines = 0;
+    int max_blank_lines = r->server->limit_req_fields;
+
+    if (max_blank_lines <= 0) {
+        max_blank_lines = DEFAULT_LIMIT_REQUEST_FIELDS;
+    }
 
     /* Read past empty lines until we get a real request line,
      * a read error, the connection closes (EOF), or we timeout.
@@ -667,14 +588,25 @@ static int read_request_line(request_rec *r, apr_bucket_brigade *bb)
          * if there are empty lines
          */
         r->the_request = NULL;
-        rv = ap_rgetline(&(r->the_request), DEFAULT_LIMIT_REQUEST_LINE + 2,
+        rv = ap_rgetline(&(r->the_request), (apr_size_t)(r->server->limit_req_line + 2),
                          &len, r, 0, bb);
 
         if (rv != APR_SUCCESS) {
             r->request_time = apr_time_now();
+
+            /* ap_rgetline returns APR_ENOSPC if it fills up the
+             * buffer before finding the end-of-line.  This is only going to
+             * happen if it exceeds the configured limit for a request-line.
+             */
+            if (rv == APR_ENOSPC) {
+                r->status    = HTTP_REQUEST_URI_TOO_LARGE;
+                r->proto_num = HTTP_VERSION(1,0);
+                r->protocol  = apr_pstrdup(r->pool, "HTTP/1.0");
+            }
+
             return 0;
         }
-    } while (len <= 0);
+    } while ((len <= 0) && (++num_blank_lines < max_blank_lines));
 
     /* we've probably got something to do, ignore graceful restart requests */
 
@@ -701,18 +633,6 @@ static int read_request_line(request_rec *r, apr_bucket_brigade *bb)
 
     ap_parse_uri(r, uri);
 
-    /* ap_getline returns (size of max buffer - 1) if it fills up the
-     * buffer before finding the end-of-line.  This is only going to
-     * happen if it exceeds the configured limit for a request-line.
-     * The cast is safe, limit_req_line cannot be negative
-     */
-    if (len > (apr_size_t)r->server->limit_req_line) {
-        r->status    = HTTP_REQUEST_URI_TOO_LARGE;
-        r->proto_num = HTTP_VERSION(1,0);
-        r->protocol  = apr_pstrdup(r->pool, "HTTP/1.0");
-        return 0;
-    }
-
     if (ll[0]) {
         r->assbackwards = 0;
         pro = ll;
@@ -752,10 +672,7 @@ AP_DECLARE(void) ap_get_mime_headers_core(request_rec *r, apr_bucket_brigade *bb
     char *value;
     apr_size_t len;
     int fields_read = 0;
-    apr_table_t *tmp_headers;
-
-    /* We'll use apr_table_overlap later to merge these into r->headers_in. */
-    tmp_headers = apr_table_make(r->pool, 50);
+    char *tmp_field;
 
     /*
      * Read header lines until we get the empty separator line, a read error,
@@ -769,26 +686,24 @@ AP_DECLARE(void) ap_get_mime_headers_core(request_rec *r, apr_bucket_brigade *bb
         rv = ap_rgetline(&field, r->server->limit_req_fieldsize + 2,
                          &len, r, 0, bb);
 
-        /* ap_rgetline returns APR_ENOSPC if it fills up the buffer before
-         * finding the end-of-line.  This is only going to happen if it
-         * exceeds the configured limit for a field size.
-         */
-        if (rv == APR_ENOSPC && field) {
-            r->status = HTTP_BAD_REQUEST;
-            /* insure ap_escape_html will terminate correctly */
-            field[len - 1] = '\0';
-            apr_table_setn(r->notes, "error-notes",
-                           apr_pstrcat(r->pool,
-                                       "Size of a request header field "
-                                       "exceeds server limit.<br />\n"
-                                       "<pre>\n",
-                                       ap_escape_html(r->pool, field),
-                                       "</pre>\n", NULL));
-            return;
-        }
-
         if (rv != APR_SUCCESS) {
             r->status = HTTP_BAD_REQUEST;
+
+            /* ap_rgetline returns APR_ENOSPC if it fills up the buffer before
+             * finding the end-of-line.  This is only going to happen if it
+             * exceeds the configured limit for a field size.
+             */
+            if (rv == APR_ENOSPC && field) {
+                /* insure ap_escape_html will terminate correctly */
+                field[len - 1] = '\0';
+                apr_table_setn(r->notes, "error-notes",
+                               apr_pstrcat(r->pool,
+                                           "Size of a request header field "
+                                           "exceeds server limit.<br />\n"
+                                           "<pre>\n",
+                                           ap_escape_html(r->pool, field),
+                                           "</pre>\n", NULL));
+            }
             return;
         }
 
@@ -800,11 +715,29 @@ AP_DECLARE(void) ap_get_mime_headers_core(request_rec *r, apr_bucket_brigade *bb
                  * doing O(n) allocs and using O(n^2) space for
                  * continuations that span many many lines.
                  */
-                if (last_len + len > alloc_len) {
+                apr_size_t fold_len = last_len + len + 1; /* trailing null */
+
+                if (fold_len > r->server->limit_req_fieldsize + 1) {
+                    r->status = HTTP_BAD_REQUEST;
+                    /* report what we have accumulated so far before the
+                     * overflow (last_field) as the field with the problem
+                     */
+                    apr_table_setn(r->notes, "error-notes",
+                                   apr_pstrcat(r->pool,
+                                               "Size of a request header field " 
+                                               "after folding "
+                                               "exceeds server limit.<br />\n"
+                                               "<pre>\n",
+                                               ap_escape_html(r->pool, last_field),
+                                               "</pre>\n", NULL));
+                    return;
+                }
+
+                if (fold_len > alloc_len) {
                     char *fold_buf;
                     alloc_len += alloc_len;
-                    if (last_len + len > alloc_len) {
-                        alloc_len = last_len + len;
+                    if (fold_len > alloc_len) {
+                        alloc_len = fold_len;
                     }
                     fold_buf = (char *)apr_palloc(r->pool, alloc_len);
                     memcpy(fold_buf, last_field, last_len);
@@ -839,17 +772,35 @@ AP_DECLARE(void) ap_get_mime_headers_core(request_rec *r, apr_bucket_brigade *bb
                 }
 
                 *value = '\0';
+                tmp_field = value;  /* used to trim the whitespace between key
+                                     * token and separator
+                                     */
                 ++value;
                 while (*value == ' ' || *value == '\t') {
                     ++value;            /* Skip to start of value   */
                 }
 
-                apr_table_addn(tmp_headers, last_field, value);
+                /* This check is to avoid any invalid memory reference while
+                 * traversing backwards in the key. To avoid a case where
+                 * the header starts with ':' (or with just some white
+                 * space and the ':') followed by the value
+                 */
+                if (tmp_field > last_field) {
+                    --tmp_field;
+                    while ((tmp_field > last_field) &&
+                           (*tmp_field == ' ' || *tmp_field == '\t')) {
+                        --tmp_field;   /* Removing LWS between key and ':' */
+                    }
+                    ++tmp_field;
+                    *tmp_field = '\0';
+                }
+
+                apr_table_addn(r->headers_in, last_field, value);
 
                 /* reset the alloc_len so that we'll allocate a new
                  * buffer if we have to do any more folding: we can't
                  * use the previous buffer because its contents are
-                 * now part of tmp_headers
+                 * now part of r->headers_in
                  */
                 alloc_len = 0;
 
@@ -871,7 +822,7 @@ AP_DECLARE(void) ap_get_mime_headers_core(request_rec *r, apr_bucket_brigade *bb
         }
     }
 
-    apr_table_overlap(r->headers_in, tmp_headers, APR_OVERLAP_TABLES_MERGE);
+    apr_table_compress(r->headers_in, APR_OVERLAP_TABLES_MERGE);
 }
 
 AP_DECLARE(void) ap_get_mime_headers(request_rec *r)
@@ -891,6 +842,7 @@ request_rec *ap_read_request(conn_rec *conn)
     apr_bucket_brigade *tmp_bb;
 
     apr_pool_create(&p, conn->pool);
+    apr_pool_tag(p, "request");
     r = apr_pcalloc(p, sizeof(request_rec));
     r->pool            = p;
     r->connection      = conn;
@@ -931,8 +883,9 @@ request_rec *ap_read_request(conn_rec *conn)
     if (!read_request_line(r, tmp_bb)) {
         if (r->status == HTTP_REQUEST_URI_TOO_LARGE) {
             ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
-                          "request failed: URI too long");
+                          "request failed: URI too long (longer than %d)", r->server->limit_req_line);
             ap_send_error_response(r, 0);
+            ap_update_child_status(conn->sbh, SERVER_BUSY_LOG, r);
             ap_run_log_transaction(r);
             apr_brigade_destroy(tmp_bb);
             return r;
@@ -948,6 +901,7 @@ request_rec *ap_read_request(conn_rec *conn)
             ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
                           "request failed: error reading the headers");
             ap_send_error_response(r, 0);
+            ap_update_child_status(conn->sbh, SERVER_BUSY_LOG, r);
             ap_run_log_transaction(r);
             apr_brigade_destroy(tmp_bb);
             return r;
@@ -966,6 +920,7 @@ request_rec *ap_read_request(conn_rec *conn)
             r->header_only = 0;
             r->status = HTTP_BAD_REQUEST;
             ap_send_error_response(r, 0);
+            ap_update_child_status(conn->sbh, SERVER_BUSY_LOG, r);
             ap_run_log_transaction(r);
             apr_brigade_destroy(tmp_bb);
             return r;
@@ -1002,12 +957,14 @@ request_rec *ap_read_request(conn_rec *conn)
 
     if (r->status != HTTP_OK) {
         ap_send_error_response(r, 0);
+        ap_update_child_status(conn->sbh, SERVER_BUSY_LOG, r);
         ap_run_log_transaction(r);
         return r;
     }
 
     if ((access_status = ap_run_post_read_request(r))) {
         ap_die(access_status, r);
+        ap_update_child_status(conn->sbh, SERVER_BUSY_LOG, r);
         ap_run_log_transaction(r);
         return NULL;
     }
@@ -1029,6 +986,7 @@ request_rec *ap_read_request(conn_rec *conn)
                           "client sent an unrecognized expectation value of "
                           "Expect: %s", expect);
             ap_send_error_response(r, 0);
+            ap_update_child_status(conn->sbh, SERVER_BUSY_LOG, r);
             ap_run_log_transaction(r);
             return r;
         }
@@ -1047,7 +1005,8 @@ request_rec *ap_read_request(conn_rec *conn)
  * *someone* has to set the protocol-specific fields...
  */
 
-void ap_set_sub_req_protocol(request_rec *rnew, const request_rec *r)
+AP_DECLARE(void) ap_set_sub_req_protocol(request_rec *rnew,
+                                         const request_rec *r)
 {
     rnew->the_request     = r->the_request;  /* Keep original request-line */
 
@@ -1085,7 +1044,7 @@ static void end_output_stream(request_rec *r)
     ap_pass_brigade(r->output_filters, bb);
 }
 
-void ap_finalize_sub_req_protocol(request_rec *sub)
+AP_DECLARE(void) ap_finalize_sub_req_protocol(request_rec *sub)
 {
     /* tell the filter chain there is no more content coming */
     if (!sub->eos_sent) {
@@ -1148,9 +1107,9 @@ AP_DECLARE(void) ap_note_digest_auth_failure(request_rec *r)
     apr_table_setn(r->err_headers_out,
                    (PROXYREQ_PROXY == r->proxyreq) ? "Proxy-Authenticate"
                                                    : "WWW-Authenticate",
-                   /* need APR_TIME_T_FMT_HEX */
-                   apr_psprintf(r->pool, "Digest realm=\"%s\", nonce=\"%llx\"",
-                                ap_auth_name(r), r->request_time));
+                   apr_psprintf(r->pool, "Digest realm=\"%s\", nonce=\""
+                                "%" APR_UINT64_T_HEX_FMT "\"",
+                                ap_auth_name(r), (apr_uint64_t)r->request_time));
 }
 
 AP_DECLARE(int) ap_get_basic_auth_pw(request_rec *r, const char **pw)
@@ -1188,10 +1147,6 @@ AP_DECLARE(int) ap_get_basic_auth_pw(request_rec *r, const char **pw)
     }
 
     t = ap_pbase64decode(r->pool, auth_line);
-    /* Note that this allocation has to be made from r->connection->pool
-     * because it has the lifetime of the connection.  The other allocations
-     * are temporary and can be tossed away any time.
-     */
     r->user = ap_getword_nulls (r->pool, &t, ':');
     r->ap_auth_type = "Basic";
 
@@ -1289,8 +1244,11 @@ AP_CORE_DECLARE_NONSTD(apr_status_t) ap_content_length_filter(
      *
      * We can only set a C-L in the response header if we haven't already
      * sent any buckets on to the next output filter for this request.
+     *
+     * Also check against cases of zero bytes sent, to avoid a bogus
+     * C-L on HEAD requests, or no-body GETs like 204s.
      */
-    if (ctx->data_sent == 0 && eos) {
+    if (ctx->data_sent == 0 && eos && r->bytes_sent > 0 ) {
         ap_set_content_length(r, r->bytes_sent);
     }
 
@@ -1429,7 +1387,7 @@ AP_DECLARE(int) ap_rputc(int c, request_rec *r)
     return c;
 }
 
-AP_DECLARE(int) ap_rputs(const char *str, request_rec *r)
+AP_DECLARE(apr_ssize_t) ap_rputs(const char *str, request_rec *r)
 {
     apr_size_t len;
 
@@ -1483,9 +1441,9 @@ static apr_status_t r_flush(apr_vformatter_buff_t *buff)
     return APR_SUCCESS;
 }
 
-AP_DECLARE(int) ap_vrprintf(request_rec *r, const char *fmt, va_list va)
+AP_DECLARE(apr_ssize_t) ap_vrprintf(request_rec *r, const char *fmt, va_list va)
 {
-    apr_size_t written;
+    apr_ssize_t written;
     struct ap_vrprintf_data vd;
     char vrprintf_buf[AP_IOBUFSIZE];
 
@@ -1503,7 +1461,7 @@ AP_DECLARE(int) ap_vrprintf(request_rec *r, const char *fmt, va_list va)
     *(vd.vbuff.curpos) = '\0';
 
     if (written != -1) {
-        int n = vd.vbuff.curpos - vrprintf_buf;
+        apr_size_t n = vd.vbuff.curpos - vrprintf_buf;
 
         /* last call to buffer_output, to finish clearing the buffer */
         if (buffer_output(r, vrprintf_buf,n) != APR_SUCCESS)
@@ -1515,10 +1473,10 @@ AP_DECLARE(int) ap_vrprintf(request_rec *r, const char *fmt, va_list va)
     return written;
 }
 
-AP_DECLARE_NONSTD(int) ap_rprintf(request_rec *r, const char *fmt, ...)
+AP_DECLARE_NONSTD(apr_ssize_t) ap_rprintf(request_rec *r, const char *fmt, ...)
 {
     va_list va;
-    int n;
+    apr_ssize_t n;
 
     if (r->connection->aborted)
         return -1;
@@ -1530,7 +1488,7 @@ AP_DECLARE_NONSTD(int) ap_rprintf(request_rec *r, const char *fmt, ...)
     return n;
 }
 
-AP_DECLARE_NONSTD(int) ap_rvputs(request_rec *r, ...)
+AP_DECLARE_NONSTD(apr_ssize_t) ap_rvputs(request_rec *r, ...)
 {
     va_list va;
     const char *s;