8335b1
diff --git a/include/http_core.h b/include/http_core.h
8335b1
index 3c47989..f6f4aa2 100644
8335b1
--- a/include/http_core.h
8335b1
+++ b/include/http_core.h
8335b1
@@ -663,6 +663,10 @@ typedef struct {
8335b1
 #define AP_TRACE_ENABLE    1
8335b1
 #define AP_TRACE_EXTENDED  2
8335b1
     int trace_enable;
8335b1
+#define AP_MERGE_TRAILERS_UNSET    0
8335b1
+#define AP_MERGE_TRAILERS_ENABLE   1
8335b1
+#define AP_MERGE_TRAILERS_DISABLE  2
8335b1
+    int merge_trailers;
8335b1
 
8335b1
 } core_server_config;
8335b1
 
8335b1
diff --git a/include/httpd.h b/include/httpd.h
8335b1
index 36cd58d..2e415f9 100644
8335b1
--- a/include/httpd.h
8335b1
+++ b/include/httpd.h
8335b1
@@ -1032,6 +1032,11 @@ struct request_rec {
8335b1
      */
8335b1
     apr_sockaddr_t *useragent_addr;
8335b1
     char *useragent_ip;
8335b1
+
8335b1
+    /** MIME trailer environment from the request */
8335b1
+    apr_table_t *trailers_in;
8335b1
+    /** MIME trailer environment from the response */
8335b1
+    apr_table_t *trailers_out;
8335b1
 };
8335b1
 
8335b1
 /**
8335b1
diff --git a/modules/http/http_filters.c b/modules/http/http_filters.c
8335b1
index 24a939a..2ae8f46 100644
8335b1
--- a/modules/http/http_filters.c
8335b1
+++ b/modules/http/http_filters.c
8335b1
@@ -214,6 +214,49 @@ static apr_status_t get_chunk_line(http_ctx_t *ctx, apr_bucket_brigade *b,
8335b1
 }
8335b1
 
8335b1
 
8335b1
+static apr_status_t read_chunked_trailers(http_ctx_t *ctx, ap_filter_t *f,
8335b1
+                                          apr_bucket_brigade *b, int merge)
8335b1
+{
8335b1
+    int rv;
8335b1
+    apr_bucket *e;
8335b1
+    request_rec *r = f->r;
8335b1
+    apr_table_t *saved_headers_in = r->headers_in;
8335b1
+    int saved_status = r->status;
8335b1
+
8335b1
+    r->status = HTTP_OK;
8335b1
+    r->headers_in = r->trailers_in;
8335b1
+    apr_table_clear(r->headers_in);
8335b1
+    ctx->state = BODY_NONE;
8335b1
+    ap_get_mime_headers(r);
8335b1
+
8335b1
+    if(r->status == HTTP_OK) {
8335b1
+        r->status = saved_status;
8335b1
+        e = apr_bucket_eos_create(f->c->bucket_alloc);
8335b1
+        APR_BRIGADE_INSERT_TAIL(b, e);
8335b1
+        ctx->eos_sent = 1;
8335b1
+        rv = APR_SUCCESS;
8335b1
+    }
8335b1
+    else {
8335b1
+        const char *error_notes = apr_table_get(r->notes,
8335b1
+                                                "error-notes");
8335b1
+        ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, 
8335b1
+                      "Error while reading HTTP trailer: %i%s%s",
8335b1
+                      r->status, error_notes ? ": " : "",
8335b1
+                      error_notes ? error_notes : "");
8335b1
+        rv = APR_EINVAL;
8335b1
+    }
8335b1
+
8335b1
+    if(!merge) {
8335b1
+        r->headers_in = saved_headers_in;
8335b1
+    }
8335b1
+    else {
8335b1
+        r->headers_in = apr_table_overlay(r->pool, saved_headers_in,
8335b1
+                r->trailers_in);
8335b1
+    }
8335b1
+
8335b1
+    return rv;
8335b1
+}
8335b1
+
8335b1
 /* This is the HTTP_INPUT filter for HTTP requests and responses from
8335b1
  * proxied servers (mod_proxy).  It handles chunked and content-length
8335b1
  * bodies.  This can only be inserted/used after the headers
8335b1
@@ -223,6 +266,7 @@ apr_status_t ap_http_filter(ap_filter_t *f, apr_bucket_brigade *b,
8335b1
                             ap_input_mode_t mode, apr_read_type_e block,
8335b1
                             apr_off_t readbytes)
8335b1
 {
8335b1
+    core_server_config *conf;
8335b1
     apr_bucket *e;
8335b1
     http_ctx_t *ctx = f->ctx;
8335b1
     apr_status_t rv;
8335b1
@@ -230,6 +274,9 @@ apr_status_t ap_http_filter(ap_filter_t *f, apr_bucket_brigade *b,
8335b1
     int http_error = HTTP_REQUEST_ENTITY_TOO_LARGE;
8335b1
     apr_bucket_brigade *bb;
8335b1
 
8335b1
+    conf = (core_server_config *)
8335b1
+        ap_get_module_config(f->r->server->module_config, &core_module);
8335b1
+
8335b1
     /* just get out of the way of things we don't want. */
8335b1
     if (mode != AP_MODE_READBYTES && mode != AP_MODE_GETLINE) {
8335b1
         return ap_get_brigade(f->next, b, mode, block, readbytes);
8335b1
@@ -403,13 +450,8 @@ apr_status_t ap_http_filter(ap_filter_t *f, apr_bucket_brigade *b,
8335b1
             }
8335b1
 
8335b1
             if (!ctx->remaining) {
8335b1
-                /* Handle trailers by calling ap_get_mime_headers again! */
8335b1
-                ctx->state = BODY_NONE;
8335b1
-                ap_get_mime_headers(f->r);
8335b1
-                e = apr_bucket_eos_create(f->c->bucket_alloc);
8335b1
-                APR_BRIGADE_INSERT_TAIL(b, e);
8335b1
-                ctx->eos_sent = 1;
8335b1
-                return APR_SUCCESS;
8335b1
+                return read_chunked_trailers(ctx, f, b,
8335b1
+                        conf->merge_trailers == AP_MERGE_TRAILERS_ENABLE);
8335b1
             }
8335b1
         }
8335b1
     }
8335b1
@@ -509,13 +551,8 @@ apr_status_t ap_http_filter(ap_filter_t *f, apr_bucket_brigade *b,
8335b1
                 }
8335b1
 
8335b1
                 if (!ctx->remaining) {
8335b1
-                    /* Handle trailers by calling ap_get_mime_headers again! */
8335b1
-                    ctx->state = BODY_NONE;
8335b1
-                    ap_get_mime_headers(f->r);
8335b1
-                    e = apr_bucket_eos_create(f->c->bucket_alloc);
8335b1
-                    APR_BRIGADE_INSERT_TAIL(b, e);
8335b1
-                    ctx->eos_sent = 1;
8335b1
-                    return APR_SUCCESS;
8335b1
+                    return read_chunked_trailers(ctx, f, b,
8335b1
+                            conf->merge_trailers == AP_MERGE_TRAILERS_ENABLE);
8335b1
                 }
8335b1
             }
8335b1
             break;
8335b1
diff --git a/modules/http/http_request.c b/modules/http/http_request.c
8335b1
index 796d506..cdfec8b 100644
8335b1
--- a/modules/http/http_request.c
8335b1
+++ b/modules/http/http_request.c
8335b1
@@ -463,6 +463,7 @@ static request_rec *internal_internal_redirect(const char *new_uri,
8335b1
     new->main            = r->main;
8335b1
 
8335b1
     new->headers_in      = r->headers_in;
8335b1
+    new->trailers_in     = r->trailers_in;
8335b1
     new->headers_out     = apr_table_make(r->pool, 12);
8335b1
     if (ap_is_HTTP_REDIRECT(new->status)) {
8335b1
         const char *location = apr_table_get(r->headers_out, "Location");
8335b1
@@ -470,6 +471,7 @@ static request_rec *internal_internal_redirect(const char *new_uri,
8335b1
             apr_table_setn(new->headers_out, "Location", location);
8335b1
     }
8335b1
     new->err_headers_out = r->err_headers_out;
8335b1
+    new->trailers_out    = apr_table_make(r->pool, 5);
8335b1
     new->subprocess_env  = rename_original_env(r->pool, r->subprocess_env);
8335b1
     new->notes           = apr_table_make(r->pool, 5);
8335b1
 
8335b1
@@ -583,6 +585,8 @@ AP_DECLARE(void) ap_internal_fast_redirect(request_rec *rr, request_rec *r)
8335b1
                                        r->headers_out);
8335b1
     r->err_headers_out = apr_table_overlay(r->pool, rr->err_headers_out,
8335b1
                                            r->err_headers_out);
8335b1
+    r->trailers_out = apr_table_overlay(r->pool, rr->trailers_out,
8335b1
+                                           r->trailers_out);
8335b1
     r->subprocess_env = apr_table_overlay(r->pool, rr->subprocess_env,
8335b1
                                           r->subprocess_env);
8335b1
 
8335b1
diff --git a/modules/loggers/mod_log_config.c b/modules/loggers/mod_log_config.c
8335b1
index 25f5030..b021dd3 100644
8335b1
--- a/modules/loggers/mod_log_config.c
8335b1
+++ b/modules/loggers/mod_log_config.c
8335b1
@@ -431,6 +431,12 @@ static const char *log_header_in(request_rec *r, char *a)
8335b1
     return ap_escape_logitem(r->pool, apr_table_get(r->headers_in, a));
8335b1
 }
8335b1
 
8335b1
+static const char *log_trailer_in(request_rec *r, char *a)
8335b1
+{
8335b1
+    return ap_escape_logitem(r->pool, apr_table_get(r->trailers_in, a));
8335b1
+}
8335b1
+
8335b1
+
8335b1
 static APR_INLINE char *find_multiple_headers(apr_pool_t *pool,
8335b1
                                               const apr_table_t *table,
8335b1
                                               const char *key)
8335b1
@@ -514,6 +520,11 @@ static const char *log_header_out(request_rec *r, char *a)
8335b1
     return ap_escape_logitem(r->pool, cp);
8335b1
 }
8335b1
 
8335b1
+static const char *log_trailer_out(request_rec *r, char *a)
8335b1
+{
8335b1
+    return ap_escape_logitem(r->pool, apr_table_get(r->trailers_out, a));
8335b1
+}
8335b1
+
8335b1
 static const char *log_note(request_rec *r, char *a)
8335b1
 {
8335b1
     return ap_escape_logitem(r->pool, apr_table_get(r->notes, a));
8335b1
@@ -916,7 +927,7 @@ static char *parse_log_misc_string(apr_pool_t *p, log_format_item *it,
8335b1
 static char *parse_log_item(apr_pool_t *p, log_format_item *it, const char **sa)
8335b1
 {
8335b1
     const char *s = *sa;
8335b1
-    ap_log_handler *handler;
8335b1
+    ap_log_handler *handler = NULL;
8335b1
 
8335b1
     if (*s != '%') {
8335b1
         return parse_log_misc_string(p, it, sa);
8335b1
@@ -986,7 +997,16 @@ static char *parse_log_item(apr_pool_t *p, log_format_item *it, const char **sa)
8335b1
             break;
8335b1
 
8335b1
         default:
8335b1
-            handler = (ap_log_handler *)apr_hash_get(log_hash, s++, 1);
8335b1
+            /* check for '^' + two character format first */
8335b1
+            if (*s == '^' && *(s+1) && *(s+2)) { 
8335b1
+                handler = (ap_log_handler *)apr_hash_get(log_hash, s, 3); 
8335b1
+                if (handler) { 
8335b1
+                   s += 3;
8335b1
+                }
8335b1
+            }
8335b1
+            if (!handler) {  
8335b1
+                handler = (ap_log_handler *)apr_hash_get(log_hash, s++, 1);  
8335b1
+            }
8335b1
             if (!handler) {
8335b1
                 char dummy[2];
8335b1
 
8335b1
@@ -1516,7 +1536,7 @@ static void ap_register_log_handler(apr_pool_t *p, char *tag,
8335b1
     log_struct->func = handler;
8335b1
     log_struct->want_orig_default = def;
8335b1
 
8335b1
-    apr_hash_set(log_hash, tag, 1, (const void *)log_struct);
8335b1
+    apr_hash_set(log_hash, tag, strlen(tag), (const void *)log_struct);
8335b1
 }
8335b1
 static ap_log_writer_init* ap_log_set_writer_init(ap_log_writer_init *handle)
8335b1
 {
8335b1
@@ -1686,6 +1706,9 @@ static int log_pre_config(apr_pool_t *p, apr_pool_t *plog, apr_pool_t *ptemp)
8335b1
         log_pfn_register(p, "U", log_request_uri, 1);
8335b1
         log_pfn_register(p, "s", log_status, 1);
8335b1
         log_pfn_register(p, "R", log_handler, 1);
8335b1
+
8335b1
+        log_pfn_register(p, "^ti", log_trailer_in, 0);
8335b1
+        log_pfn_register(p, "^to", log_trailer_out, 0);
8335b1
     }
8335b1
 
8335b1
     /* reset to default conditions */
8335b1
diff --git a/modules/proxy/mod_proxy_http.c b/modules/proxy/mod_proxy_http.c
8335b1
index 7ae0fa4..05f33b4 100644
8335b1
--- a/modules/proxy/mod_proxy_http.c
8335b1
+++ b/modules/proxy/mod_proxy_http.c
8335b1
@@ -994,8 +994,11 @@ static request_rec *make_fake_req(conn_rec *c, request_rec *r)
8335b1
     rp->status          = HTTP_OK;
8335b1
 
8335b1
     rp->headers_in      = apr_table_make(pool, 50);
8335b1
+    rp->trailers_in     = apr_table_make(pool, 5);
8335b1
+
8335b1
     rp->subprocess_env  = apr_table_make(pool, 50);
8335b1
     rp->headers_out     = apr_table_make(pool, 12);
8335b1
+    rp->trailers_out    = apr_table_make(pool, 5);
8335b1
     rp->err_headers_out = apr_table_make(pool, 5);
8335b1
     rp->notes           = apr_table_make(pool, 5);
8335b1
 
8335b1
@@ -1076,6 +1079,7 @@ static void ap_proxy_read_headers(request_rec *r, request_rec *rr,
8335b1
     psc = (proxy_server_conf *) ap_get_module_config(sconf, &proxy_module);
8335b1
 
8335b1
     r->headers_out = apr_table_make(r->pool, 20);
8335b1
+    r->trailers_out = apr_table_make(r->pool, 5);
8335b1
     *pread_len = 0;
8335b1
 
8335b1
     /*
8335b1
@@ -1206,6 +1210,14 @@ apr_status_t ap_proxygetline(apr_bucket_brigade *bb, char *s, int n, request_rec
8335b1
 #define AP_MAX_INTERIM_RESPONSES 10
8335b1
 #endif
8335b1
 
8335b1
+static int add_trailers(void *data, const char *key, const char *val)
8335b1
+{
8335b1
+    if (val) {
8335b1
+        apr_table_add((apr_table_t*)data, key, val);
8335b1
+    }
8335b1
+    return 1;
8335b1
+}
8335b1
+
8335b1
 static
8335b1
 apr_status_t ap_proxy_http_process_response(apr_pool_t * p, request_rec *r,
8335b1
                                             proxy_conn_rec **backend_ptr,
8335b1
@@ -1717,6 +1729,12 @@ apr_status_t ap_proxy_http_process_response(apr_pool_t * p, request_rec *r,
8335b1
                     /* next time try a non-blocking read */
8335b1
                     mode = APR_NONBLOCK_READ;
8335b1
 
8335b1
+                    if (!apr_is_empty_table(backend->r->trailers_in)) {
8335b1
+                        apr_table_do(add_trailers, r->trailers_out,
8335b1
+                                backend->r->trailers_in, NULL);
8335b1
+                        apr_table_clear(backend->r->trailers_in);
8335b1
+                    }
8335b1
+
8335b1
                     apr_brigade_length(bb, 0, &readbytes);
8335b1
                     backend->worker->s->read += readbytes;
8335b1
 #if DEBUGGING
8335b1
diff --git a/server/core.c b/server/core.c
8335b1
index 024bab6..7cfde63 100644
8335b1
--- a/server/core.c
8335b1
+++ b/server/core.c
8335b1
@@ -523,6 +523,10 @@ static void *merge_core_server_configs(apr_pool_t *p, void *basev, void *virtv)
8335b1
     if (virt->error_log_req)
8335b1
         conf->error_log_req = virt->error_log_req;
8335b1
 
8335b1
+    conf->merge_trailers = (virt->merge_trailers != AP_MERGE_TRAILERS_UNSET)
8335b1
+                           ? virt->merge_trailers
8335b1
+                           : base->merge_trailers;
8335b1
+
8335b1
     return conf;
8335b1
 }
8335b1
 
8335b1
@@ -3877,6 +3881,16 @@ AP_DECLARE(void) ap_register_errorlog_handler(apr_pool_t *p, char *tag,
8335b1
 }
8335b1
 
8335b1
 
8335b1
+static const char *set_merge_trailers(cmd_parms *cmd, void *dummy, int arg)
8335b1
+{
8335b1
+    core_server_config *conf = ap_get_module_config(cmd->server->module_config,
8335b1
+                                                    &core_module);
8335b1
+    conf->merge_trailers = (arg ? AP_MERGE_TRAILERS_ENABLE :
8335b1
+            AP_MERGE_TRAILERS_DISABLE);
8335b1
+
8335b1
+    return NULL;
8335b1
+}
8335b1
+
8335b1
 /* Note --- ErrorDocument will now work from .htaccess files.
8335b1
  * The AllowOverride of Fileinfo allows webmasters to turn it off
8335b1
  */
8335b1
@@ -4124,6 +4138,8 @@ AP_INIT_TAKE1("EnableExceptionHook", ap_mpm_set_exception_hook, NULL, RSRC_CONF,
8335b1
 #endif
8335b1
 AP_INIT_TAKE1("TraceEnable", set_trace_enable, NULL, RSRC_CONF,
8335b1
               "'on' (default), 'off' or 'extended' to trace request body content"),
8335b1
+AP_INIT_FLAG("MergeTrailers", set_merge_trailers, NULL, RSRC_CONF,
8335b1
+              "merge request trailers into request headers or not"),
8335b1
 { NULL }
8335b1
 };
8335b1
 
8335b1
@@ -4206,7 +4222,6 @@ static int core_map_to_storage(request_rec *r)
8335b1
 
8335b1
 static int do_nothing(request_rec *r) { return OK; }
8335b1
 
8335b1
-
8335b1
 static int core_override_type(request_rec *r)
8335b1
 {
8335b1
     core_dir_config *conf =
8335b1
diff --git a/server/protocol.c b/server/protocol.c
8335b1
index 14329eb..46fc034 100644
8335b1
--- a/server/protocol.c
8335b1
+++ b/server/protocol.c
8335b1
@@ -718,6 +718,8 @@ AP_DECLARE(void) ap_get_mime_headers_core(request_rec *r, apr_bucket_brigade *bb
8335b1
                 r->status = HTTP_REQUEST_TIME_OUT;
8335b1
             }
8335b1
             else {
8335b1
+                ap_log_rerror(APLOG_MARK, APLOG_DEBUG, rv, r, 
8335b1
+                              "Failed to read request header line %s", field);
8335b1
                 r->status = HTTP_BAD_REQUEST;
8335b1
             }
8335b1
 
8335b1
@@ -917,9 +919,11 @@ request_rec *ap_read_request(conn_rec *conn)
8335b1
     r->allowed_methods = ap_make_method_list(p, 2);
8335b1
 
8335b1
     r->headers_in      = apr_table_make(r->pool, 25);
8335b1
+    r->trailers_in     = apr_table_make(r->pool, 5);
8335b1
     r->subprocess_env  = apr_table_make(r->pool, 25);
8335b1
     r->headers_out     = apr_table_make(r->pool, 12);
8335b1
     r->err_headers_out = apr_table_make(r->pool, 5);
8335b1
+    r->trailers_out    = apr_table_make(r->pool, 5);
8335b1
     r->notes           = apr_table_make(r->pool, 5);
8335b1
 
8335b1
     r->request_config  = ap_create_request_config(r->pool);
8335b1
@@ -1162,6 +1166,7 @@ AP_DECLARE(void) ap_set_sub_req_protocol(request_rec *rnew,
8335b1
     rnew->status          = HTTP_OK;
8335b1
 
8335b1
     rnew->headers_in      = apr_table_copy(rnew->pool, r->headers_in);
8335b1
+    rnew->trailers_in     = apr_table_copy(rnew->pool, r->trailers_in);
8335b1
 
8335b1
     /* did the original request have a body?  (e.g. POST w/SSI tags)
8335b1
      * if so, make sure the subrequest doesn't inherit body headers
8335b1
@@ -1173,6 +1178,7 @@ AP_DECLARE(void) ap_set_sub_req_protocol(request_rec *rnew,
8335b1
     rnew->subprocess_env  = apr_table_copy(rnew->pool, r->subprocess_env);
8335b1
     rnew->headers_out     = apr_table_make(rnew->pool, 5);
8335b1
     rnew->err_headers_out = apr_table_make(rnew->pool, 5);
8335b1
+    rnew->trailers_out    = apr_table_make(rnew->pool, 5);
8335b1
     rnew->notes           = apr_table_make(rnew->pool, 5);
8335b1
 
8335b1
     rnew->expecting_100   = r->expecting_100;