bottleneck testcase based on rubbos
[bottlenecks.git] / rubbos / app / httpd-2.0.64 / modules / experimental / mod_dumpio.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  * Originally written @ Covalent by Jim Jagielski
19  */
20
21 /*
22  * mod_dumpio.c:
23  *  Think of this as a filter sniffer for Apache 2.x. It logs
24  *  all filter data right before and after it goes out on the
25  *  wire (BUT right before SSL encoded or after SSL decoded).
26  *  It can produce a *huge* amount of data.
27  */
28
29
30 #include "httpd.h"
31 #include "http_connection.h"
32 #include "http_config.h"
33 #include "http_core.h"
34 #include "http_log.h"
35
36 module AP_MODULE_DECLARE_DATA dumpio_module ;
37
38 typedef struct dumpio_conf_t {
39     int enable_input;
40     int enable_output;
41 } dumpio_conf_t;
42
43 /*
44  * Workhorse function: simply log to the current error_log
45  * info about the data in the bucket as well as the data itself
46  */
47 static void dumpit(ap_filter_t *f, apr_bucket *b)
48 {
49     conn_rec *c = f->c;
50     
51     ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, c->base_server,
52         "mod_dumpio:  %s (%s-%s): %" APR_SIZE_T_FMT " bytes",
53                 f->frec->name,
54                 (APR_BUCKET_IS_METADATA(b)) ? "metadata" : "data",
55                 b->type->name,
56                 b->length) ;
57
58     if (!(APR_BUCKET_IS_METADATA(b))) {
59         const char *buf;
60         apr_size_t nbytes;
61         char *obuf;
62         if (apr_bucket_read(b, &buf, &nbytes, APR_BLOCK_READ) == APR_SUCCESS) {
63             if (nbytes) {
64                 obuf = malloc(nbytes+1);    /* use pool? */
65                 memcpy(obuf, buf, nbytes);
66                 obuf[nbytes] = '\0';
67                 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, c->base_server,
68                      "mod_dumpio:  %s (%s-%s): %s",
69                      f->frec->name,
70                      (APR_BUCKET_IS_METADATA(b)) ? "metadata" : "data",
71                      b->type->name,
72                      obuf);
73                 free(obuf);
74             }
75         } else {
76             ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, c->base_server,
77                  "mod_dumpio:  %s (%s-%s): %s",
78                  f->frec->name,
79                  (APR_BUCKET_IS_METADATA(b)) ? "metadata" : "data",
80                  b->type->name,
81                  "error reading data");
82         }
83     }
84 }
85
86 #define whichmode( mode ) \
87  ( (( mode ) == AP_MODE_READBYTES) ? "readbytes" : \
88    (( mode ) == AP_MODE_GETLINE) ? "getline" : \
89    (( mode ) == AP_MODE_EATCRLF) ? "eatcrlf" : \
90    (( mode ) == AP_MODE_SPECULATIVE) ? "speculative" : \
91    (( mode ) == AP_MODE_EXHAUSTIVE) ? "exhaustive" : \
92    (( mode ) == AP_MODE_INIT) ? "init" : "unknown" \
93  )
94        
95 static int dumpio_input_filter (ap_filter_t *f, apr_bucket_brigade *bb,
96     ap_input_mode_t mode, apr_read_type_e block, apr_off_t readbytes)
97 {
98
99     apr_bucket *b;
100     apr_status_t ret;
101     conn_rec *c = f->c;
102
103     ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, c->base_server,
104         "mod_dumpio: %s [%s-%s] %" APR_OFF_T_FMT " readbytes",
105          f->frec->name,
106          whichmode(mode),
107          ((block) == APR_BLOCK_READ) ? "blocking" : "nonblocking",
108          readbytes) ;
109
110     ret = ap_get_brigade(f->next, bb, mode, block, readbytes);
111
112     if (ret == APR_SUCCESS) {
113         for (b = APR_BRIGADE_FIRST(bb); b != APR_BRIGADE_SENTINEL(bb); b = APR_BUCKET_NEXT(b)) {
114           dumpit(f, b);
115         }
116     } else {
117         ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, c->base_server,
118         "mod_dumpio: %s - %d", f->frec->name, ret) ;
119     }
120
121     return APR_SUCCESS ;
122 }
123
124 static int dumpio_output_filter (ap_filter_t *f, apr_bucket_brigade *bb)
125 {
126     apr_bucket *b;
127     conn_rec *c = f->c;
128     
129     ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, c->base_server, "mod_dumpio: %s", f->frec->name) ;
130     
131     for (b = APR_BRIGADE_FIRST(bb); b != APR_BRIGADE_SENTINEL(bb); b = APR_BUCKET_NEXT(b)) {
132         /*
133          * If we ever see an EOS, make sure to FLUSH.
134          */
135         if (APR_BUCKET_IS_EOS(b)) {
136             apr_bucket *flush = apr_bucket_flush_create(f->c->bucket_alloc);
137             APR_BUCKET_INSERT_BEFORE(b, flush);
138         }
139         dumpit(f, b);
140     }
141     
142     return ap_pass_brigade(f->next, bb) ;
143 }
144
145 static int dumpio_pre_conn(conn_rec *c, void *csd)
146 {
147     dumpio_conf_t *ptr =
148     (dumpio_conf_t *) ap_get_module_config(c->base_server->module_config,
149                                            &dumpio_module);
150     
151     if (ptr->enable_input)
152         ap_add_input_filter("DUMPIO_IN", NULL, NULL, c);
153     if (ptr->enable_output)
154         ap_add_output_filter("DUMPIO_OUT", NULL, NULL, c);
155     return OK;
156 }
157
158 static void dumpio_register_hooks(apr_pool_t *p)
159 {
160 /*
161  * We know that SSL is CONNECTION + 5
162  */
163   ap_register_output_filter("DUMPIO_OUT", dumpio_output_filter,
164         NULL, AP_FTYPE_CONNECTION + 3) ;
165
166   ap_register_input_filter("DUMPIO_IN", dumpio_input_filter,
167         NULL, AP_FTYPE_CONNECTION + 3) ;
168
169   ap_hook_pre_connection(dumpio_pre_conn, NULL, NULL, APR_HOOK_MIDDLE);
170 }
171
172 static void *dumpio_create_sconfig(apr_pool_t *p, server_rec *s)
173 {
174     dumpio_conf_t *ptr = apr_pcalloc(p, sizeof *ptr);
175     ptr->enable_input = ptr->enable_output = 0;
176     return ptr;
177 }
178
179 static const char *dumpio_enable_input(cmd_parms *cmd, void *dummy, int arg)
180 {
181     dumpio_conf_t *ptr =
182     (dumpio_conf_t *) ap_get_module_config(cmd->server->module_config,
183                                            &dumpio_module);
184     
185     ptr->enable_input = arg;
186     return NULL;
187 }
188
189 static const char *dumpio_enable_output(cmd_parms *cmd, void *dummy, int arg)
190 {
191     dumpio_conf_t *ptr =
192     (dumpio_conf_t *) ap_get_module_config(cmd->server->module_config,
193                                            &dumpio_module);
194     
195     ptr->enable_output = arg;
196     return NULL;
197 }
198
199 static const command_rec dumpio_cmds[] = {
200     AP_INIT_FLAG("DumpIOInput", dumpio_enable_input, NULL,
201                  RSRC_CONF, "Enable I/O Dump on Input Data"),
202     AP_INIT_FLAG("DumpIOOutput", dumpio_enable_output, NULL,
203                  RSRC_CONF, "Enable I/O Dump on Output Data"),
204     { NULL }
205 };
206
207 module AP_MODULE_DECLARE_DATA dumpio_module = {
208         STANDARD20_MODULE_STUFF,
209         NULL,
210         NULL,
211         dumpio_create_sconfig,
212         NULL,
213         dumpio_cmds,
214         dumpio_register_hooks
215 };