]> granicus.if.org Git - apache/blobdiff - server/protocol.c
Catch up to apr_dbm projects
[apache] / server / protocol.c
index 546df2f7f267e42b3b35e90ba67f90317b19bad0..fa7aecffb1b522a9081c8512e0663726456a1ede 100644 (file)
@@ -1,9 +1,9 @@
-/* Copyright 2001-2005 The Apache Software Foundation or its licensors, as
- * applicable.
- *
- * 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
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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
  *
  *     http://www.apache.org/licenses/LICENSE-2.0
  *
@@ -33,7 +33,6 @@
 #define APR_WANT_MEMFUNC
 #include "apr_want.h"
 
-#define CORE_PRIVATE
 #include "util_filter.h"
 #include "ap_config.h"
 #include "httpd.h"
@@ -95,7 +94,7 @@ AP_DECLARE(void) ap_setup_make_content_type(apr_pool_t *pool)
 /*
  * Builds the content-type that should be sent to the client from the
  * content-type specified.  The following rules are followed:
- *    - if type is NULL, type is set to ap_default_type(r)
+ *    - if type is NULL or "", return NULL (do not set content-type).
  *    - if charset adding is disabled, stop processing and return type.
  *    - then, if there are no parameters on type, add the default charset
  *    - return type
@@ -106,16 +105,23 @@ AP_DECLARE(const char *)ap_make_content_type(request_rec *r, const char *type)
     core_dir_config *conf =
         (core_dir_config *)ap_get_module_config(r->per_dir_config,
                                                 &core_module);
+    core_request_config *request_conf;
     apr_size_t type_len;
 
-    if (!type) {
-        type = ap_default_type(r);
+    if (!type || *type == '\0') {
+        return NULL;
     }
 
     if (conf->add_default_charset != ADD_DEFAULT_CHARSET_ON) {
         return type;
     }
 
+    request_conf =
+        ap_get_module_config(r->request_config, &core_module);
+    if (request_conf->suppress_charset) {
+        return type;
+    }
+
     type_len = strlen(type);
 
     if (apr_strmatch(charset_pattern, type, type_len) != NULL) {
@@ -210,6 +216,14 @@ 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;
 
+    /*
+     * Initialize last_char as otherwise a random value will be compared
+     * against APR_ASCII_LF at the end of the loop if bb only contains
+     * zero-length buckets.
+     */
+    if (last_char)
+        *last_char = '\0';
+
     for (;;) {
         apr_brigade_cleanup(bb);
         rv = ap_get_brigade(r->input_filters, bb, AP_MODE_GETLINE,
@@ -217,30 +231,30 @@ AP_DECLARE(apr_status_t) ap_rgetline_core(char **s, apr_size_t n,
         if (rv != APR_SUCCESS) {
             return rv;
         }
-        
+
         /* Something horribly wrong happened.  Someone didn't block! */
         if (APR_BRIGADE_EMPTY(bb)) {
             return APR_EGENERAL;
         }
-        
+
         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;
             }
-            
+
             rv = apr_bucket_read(e, &str, &len, APR_BLOCK_READ);
             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
@@ -248,7 +262,7 @@ AP_DECLARE(apr_status_t) ap_rgetline_core(char **s, apr_size_t n,
                  */
                 continue;
             }
-            
+
             /* Would this overrun our buffer?  If so, we'll die. */
             if (n < bytes_handled + len) {
                 *read = bytes_handled;
@@ -263,7 +277,7 @@ AP_DECLARE(apr_status_t) ap_rgetline_core(char **s, apr_size_t n,
                 }
                 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. */
@@ -278,13 +292,13 @@ AP_DECLARE(apr_status_t) ap_rgetline_core(char **s, apr_size_t n,
                     /* 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;
@@ -296,18 +310,18 @@ AP_DECLARE(apr_status_t) ap_rgetline_core(char **s, apr_size_t n,
             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;
         }
-        
+
         /* If we got a full line of input, stop reading */
         if (last_char && (*last_char == APR_ASCII_LF)) {
             break;
         }
     }
 
-    /* Now NUL-terminate the string at the end of the line; 
+    /* Now NUL-terminate the string at the end of the line;
      * if the last-but-one character is a CR, terminate there */
     if (last_char > *s && last_char[-1] == APR_ASCII_CR) {
         last_char--;
@@ -324,34 +338,34 @@ AP_DECLARE(apr_status_t) ap_rgetline_core(char **s, apr_size_t n,
             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
@@ -374,7 +388,7 @@ AP_DECLARE(apr_status_t) ap_rgetline_core(char **s, apr_size_t n,
                 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.
                      */
@@ -384,25 +398,25 @@ AP_DECLARE(apr_status_t) ap_rgetline_core(char **s, apr_size_t n,
                         /* 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;
@@ -515,8 +529,8 @@ AP_CORE_DECLARE(void) ap_parse_uri(request_rec *r, const char *uri)
         r->uri = r->parsed_uri.path ? r->parsed_uri.path
                  : apr_pstrdup(r->pool, "/");
 
-#if defined(OS2) || defined(WIN32)
-        /* Handle path translations for OS/2 and plug security hole.
+#if defined(WIN32)
+        /* Handle path translations and plug security hole.
          * This will prevent "http://www.wherever.com/..\..\/" from
          * returning a directory for the root drive.
          */
@@ -526,7 +540,7 @@ AP_CORE_DECLARE(void) ap_parse_uri(request_rec *r, const char *uri)
             for (x = r->uri; (x = strchr(x, '\\')) != NULL; )
                 *x = '/';
         }
-#endif /* OS2 || WIN32 */
+#endif /* WIN32 */
     }
     else {
         r->args = NULL;
@@ -592,10 +606,6 @@ static int read_request_line(request_rec *r, apr_bucket_brigade *bb)
                 r->proto_num = HTTP_VERSION(1,0);
                 r->protocol  = apr_pstrdup(r->pool, "HTTP/1.0");
             }
-            else if (r->connection->keepalive != AP_CONN_KEEPALIVE) {
-                ap_log_rerror(APLOG_MARK, APLOG_INFO, rv, r,
-                              "Error while reading request line. (client didn't send a request?)");
-            }
             return 0;
         }
     } while ((len <= 0) && (++num_blank_lines < max_blank_lines));
@@ -716,7 +726,7 @@ AP_DECLARE(void) ap_get_mime_headers_core(request_rec *r, apr_bucket_brigade *bb
                      */
                     apr_table_setn(r->notes, "error-notes",
                                    apr_pstrcat(r->pool,
-                                               "Size of a request header field " 
+                                               "Size of a request header field "
                                                "after folding "
                                                "exceeds server limit.<br />\n"
                                                "<pre>\n",
@@ -762,7 +772,7 @@ AP_DECLARE(void) ap_get_mime_headers_core(request_rec *r, apr_bucket_brigade *bb
                                                "</pre>\n", NULL));
                     return;
                 }
-                
+
                 tmp_field = value - 1; /* last character of field-name */
 
                 *value++ = '\0'; /* NUL-terminate at colon */
@@ -772,11 +782,11 @@ AP_DECLARE(void) ap_get_mime_headers_core(request_rec *r, apr_bucket_brigade *bb
                 }
 
                 /* Strip LWS after field-name: */
-                while (tmp_field > last_field 
+                while (tmp_field > last_field
                        && (*tmp_field == ' ' || *tmp_field == '\t')) {
                     *tmp_field-- = '\0';
                 }
-                
+
                 /* Strip LWS after field-value: */
                 tmp_field = last_field + last_len - 1;
                 while (tmp_field > value
@@ -829,10 +839,14 @@ request_rec *ap_read_request(conn_rec *conn)
     const char *expect;
     int access_status;
     apr_bucket_brigade *tmp_bb;
+    apr_socket_t *csd;
+    apr_interval_time_t cur_timeout;
+
 
     apr_pool_create(&p, conn->pool);
     apr_pool_tag(p, "request");
     r = apr_pcalloc(p, sizeof(request_rec));
+    AP_READ_REQUEST_ENTRY((intptr_t)r, (uintptr_t)conn);
     r->pool            = p;
     r->connection      = conn;
     r->server          = conn->base_server;
@@ -866,6 +880,11 @@ request_rec *ap_read_request(conn_rec *conn)
     r->status          = HTTP_REQUEST_TIME_OUT;  /* Until we get a request */
     r->the_request     = NULL;
 
+    /* Begin by presuming any module can make its own path_info assumptions,
+     * until some module interjects and changes the value.
+     */
+    r->used_path_info = AP_REQ_DEFAULT_PATH_INFO;
+
     tmp_bb = apr_brigade_create(r->pool, r->connection->bucket_alloc);
 
     /* Get the request... */
@@ -877,11 +896,23 @@ request_rec *ap_read_request(conn_rec *conn)
             ap_update_child_status(conn->sbh, SERVER_BUSY_LOG, r);
             ap_run_log_transaction(r);
             apr_brigade_destroy(tmp_bb);
-            return r;
+            goto traceout;
         }
 
         apr_brigade_destroy(tmp_bb);
-        return NULL;
+        r = NULL;
+        goto traceout;
+    }
+
+    /* We may have been in keep_alive_timeout mode, so toggle back
+     * to the normal timeout mode as we fetch the header lines,
+     * as necessary.
+     */
+    csd = ap_get_module_config(conn->conn_config, &core_module);
+    apr_socket_timeout_get(csd, &cur_timeout);
+    if (cur_timeout != conn->base_server->timeout) {
+        apr_socket_timeout_set(csd, conn->base_server->timeout);
+        cur_timeout = conn->base_server->timeout;
     }
 
     if (!r->assbackwards) {
@@ -893,7 +924,16 @@ request_rec *ap_read_request(conn_rec *conn)
             ap_update_child_status(conn->sbh, SERVER_BUSY_LOG, r);
             ap_run_log_transaction(r);
             apr_brigade_destroy(tmp_bb);
-            return r;
+            goto traceout;
+        }
+
+        if (apr_table_get(r->headers_in, "Transfer-Encoding")
+            && apr_table_get(r->headers_in, "Content-Length")) {
+            /* 2616 section 4.4, point 3: "if both Transfer-Encoding
+             * and Content-Length are received, the latter MUST be
+             * ignored"; so unset it here to prevent any confusion
+             * later. */
+            apr_table_unset(r->headers_in, "Content-Length");
         }
     }
     else {
@@ -912,7 +952,7 @@ request_rec *ap_read_request(conn_rec *conn)
             ap_update_child_status(conn->sbh, SERVER_BUSY_LOG, r);
             ap_run_log_transaction(r);
             apr_brigade_destroy(tmp_bb);
-            return r;
+            goto traceout;
         }
     }
 
@@ -925,6 +965,14 @@ request_rec *ap_read_request(conn_rec *conn)
      */
     ap_update_vhost_from_headers(r);
 
+    /* Toggle to the Host:-based vhost's timeout mode to fetch the
+     * request body and send the response body, if needed.
+     */
+    if (cur_timeout != r->server->timeout) {
+        apr_socket_timeout_set(csd, r->server->timeout);
+        cur_timeout = r->server->timeout;
+    }
+
     /* we may have switched to another server */
     r->per_dir_config = r->server->lookup_defaults;
 
@@ -944,18 +992,29 @@ request_rec *ap_read_request(conn_rec *conn)
                       "(see RFC2616 section 14.23): %s", r->uri);
     }
 
+    /*
+     * Add the HTTP_IN filter here to ensure that ap_discard_request_body
+     * called by ap_die and by ap_send_error_response works correctly on
+     * status codes that do not cause the connection to be dropped and
+     * in situations where the connection should be kept alive.
+     */
+
+    ap_add_input_filter_handle(ap_http_input_filter_handle,
+                               NULL, r, r->connection);
+
     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;
+        goto traceout;
     }
 
     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;
+        r = NULL;
+        goto traceout;
     }
 
     if (((expect = apr_table_get(r->headers_in, "Expect")) != NULL)
@@ -977,13 +1036,14 @@ request_rec *ap_read_request(conn_rec *conn)
             ap_send_error_response(r, 0);
             ap_update_child_status(conn->sbh, SERVER_BUSY_LOG, r);
             ap_run_log_transaction(r);
-            return r;
+            goto traceout;
         }
     }
 
-    ap_add_input_filter_handle(ap_http_input_filter_handle,
-                               NULL, r, r->connection);
-
+    AP_READ_REQUEST_SUCCESS((uintptr_t)r, (char *)r->method, (char *)r->uri, (char *)r->server->defn_name, r->status);
+    return r;
+    traceout:
+    AP_READ_REQUEST_FAILURE((uintptr_t)r);
     return r;
 }
 
@@ -1006,7 +1066,7 @@ static void clone_headers_no_body(request_rec *rnew,
     apr_table_unset(rnew->headers_in, "Expires");
     apr_table_unset(rnew->headers_in, "Last-Modified");
     apr_table_unset(rnew->headers_in, "Transfer-Encoding");
-}        
+}
 
 /*
  * A couple of other functions which initialize some of the fields of
@@ -1032,7 +1092,8 @@ AP_DECLARE(void) ap_set_sub_req_protocol(request_rec *rnew,
     /* did the original request have a body?  (e.g. POST w/SSI tags)
      * if so, make sure the subrequest doesn't inherit body headers
      */
-    if (r->read_length) {
+    if (!r->kept_body && (apr_table_get(r->headers_in, "Content-Length")
+        || apr_table_get(r->headers_in, "Transfer-Encoding"))) {
         clone_headers_no_body(rnew, r);
     } else {
         /* no body (common case).  clone headers the cheap way */
@@ -1263,7 +1324,19 @@ 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.
      */
-    if (ctx->data_sent == 0 && eos) {
+    if (ctx->data_sent == 0 && eos &&
+        /* don't whack the C-L if it has already been set for a HEAD
+         * by something like proxy.  the brigade only has an EOS bucket
+         * in this case, making r->bytes_sent zero.
+         *
+         * if r->bytes_sent > 0 we have a (temporary) body whose length may
+         * have been changed by a filter.  the C-L header might not have been
+         * updated so we do it here.  long term it would be cleaner to have
+         * such filters update or remove the C-L header, and just use it
+         * if present.
+         */
+        !(r->header_only && r->bytes_sent == 0 &&
+            apr_table_get(r->headers_out, "Content-Length"))) {
         ap_set_content_length(r, r->bytes_sent);
     }
 
@@ -1280,12 +1353,11 @@ AP_DECLARE(apr_status_t) ap_send_fd(apr_file_t *fd, request_rec *r,
 {
     conn_rec *c = r->connection;
     apr_bucket_brigade *bb = NULL;
-    apr_bucket *b;
     apr_status_t rv;
 
     bb = apr_brigade_create(r->pool, c->bucket_alloc);
-    b = apr_bucket_file_create(fd, offset, len, r->pool, c->bucket_alloc);
-    APR_BRIGADE_INSERT_TAIL(bb, b);
+    
+    apr_brigade_insert_file(bb, fd, 0, len, r->pool);
 
     rv = ap_pass_brigade(r->output_filters, bb);
     if (rv != APR_SUCCESS) {
@@ -1327,14 +1399,12 @@ AP_CORE_DECLARE_NONSTD(apr_status_t) ap_old_write_filter(
 
     AP_DEBUG_ASSERT(ctx);
 
-    if (ctx->bb != 0) {
+    if (ctx->bb != NULL) {
         /* whatever is coming down the pipe (we don't care), we
          * can simply insert our buffered data at the front and
          * pass the whole bundle down the chain.
          */
-        APR_BRIGADE_CONCAT(ctx->bb, bb);
-        bb = ctx->bb;
-        ctx->bb = NULL;
+        APR_BRIGADE_PREPEND(bb, ctx->bb);
     }
 
     return ap_pass_brigade(f->next, bb);
@@ -1472,9 +1542,6 @@ AP_DECLARE(int) ap_vrprintf(request_rec *r, const char *fmt, va_list va)
 
     written = apr_vformatter(r_flush, &vd.vbuff, fmt, va);
 
-    /* tack on null terminator on remaining string */
-    *(vd.vbuff.curpos) = '\0';
-
     if (written != -1) {
         int n = vd.vbuff.curpos - vrprintf_buf;
 
@@ -1566,6 +1633,56 @@ AP_DECLARE(void) ap_set_last_modified(request_rec *r)
     }
 }
 
+typedef struct hdr_ptr {
+    ap_filter_t *f;
+    apr_bucket_brigade *bb;
+} hdr_ptr;
+static int send_header(void *data, const char *key, const char *val)
+{
+    ap_fputstrs(((hdr_ptr*)data)->f, ((hdr_ptr*)data)->bb,
+                key, ": ", val, CRLF, NULL);
+    return 1;
+}
+AP_DECLARE(void) ap_send_interim_response(request_rec *r, int send_headers)
+{
+    hdr_ptr x;
+    char *status_line = NULL;
+
+    if (r->proto_num < 1001) {
+        /* don't send interim response to HTTP/1.0 Client */
+        return;
+    }
+    if (!ap_is_HTTP_INFO(r->status)) {
+        ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, NULL,
+                      "Status is %d - not sending interim response", r->status);
+        return;
+    }
+    if ((r->status == HTTP_CONTINUE) && !r->expecting_100) {
+        /*
+         * Don't send 100-Continue when there was no Expect: 100-continue
+         * in the request headers. For origin servers this is a SHOULD NOT
+         * for proxies it is a MUST NOT according to RFC 2616 8.2.3
+         */
+        return;
+    }
+
+    status_line = apr_pstrcat(r->pool, AP_SERVER_PROTOCOL, " ", r->status_line, CRLF, NULL);
+    ap_xlate_proto_to_ascii(status_line, strlen(status_line));
+
+    x.f = r->connection->output_filters;
+    x.bb = apr_brigade_create(r->pool, r->connection->bucket_alloc);
+
+    ap_fputs(x.f, x.bb, status_line);
+    if (send_headers) {
+        apr_table_do(send_header, &x, r->headers_out, NULL);
+        apr_table_clear(r->headers_out);
+    }
+    ap_fputs(x.f, x.bb, CRLF_ASCII);
+    ap_fflush(x.f, x.bb);
+    apr_brigade_destroy(x.bb);
+}
+
+
 AP_IMPLEMENT_HOOK_RUN_ALL(int,post_read_request,
                           (request_rec *r), (r), OK, DECLINED)
 AP_IMPLEMENT_HOOK_RUN_ALL(int,log_transaction,