diff options
Diffstat (limited to 'src/mod_log_sql_logio.c')
-rw-r--r-- | src/mod_log_sql_logio.c | 146 |
1 files changed, 146 insertions, 0 deletions
diff --git a/src/mod_log_sql_logio.c b/src/mod_log_sql_logio.c new file mode 100644 index 0000000..ebdeddd --- /dev/null +++ b/src/mod_log_sql_logio.c | |||
@@ -0,0 +1,146 @@ | |||
1 | /* $Id: mod_log_sql_ssl.c 140 2004-05-14 03:50:47Z urkle@drip.ws $ */ | ||
2 | |||
3 | #if defined(WITH_APACHE20) | ||
4 | # include "apache20.h" | ||
5 | #else | ||
6 | # error Unsupported Apache version | ||
7 | #endif | ||
8 | |||
9 | #ifdef HAVE_CONFIG_H | ||
10 | /* Undefine these to prevent conflicts between Apache ap_config_auto.h and | ||
11 | * my config.h. Only really needed for Apache < 2.0.48, but it can't hurt. | ||
12 | */ | ||
13 | #undef PACKAGE_BUGREPORT | ||
14 | #undef PACKAGE_NAME | ||
15 | #undef PACKAGE_STRING | ||
16 | #undef PACKAGE_TARNAME | ||
17 | #undef PACKAGE_VERSION | ||
18 | |||
19 | #include "autoconfig.h" | ||
20 | #endif | ||
21 | |||
22 | #include "mod_log_sql.h" | ||
23 | |||
24 | #include "http_connection.h" | ||
25 | |||
26 | module AP_MODULE_DECLARE_DATA log_sql_logio_module; | ||
27 | |||
28 | // From apachge 2.2's mod_logio.c to provide logging ACTUAL incoming and outgoing bytes | ||
29 | static const char logio_filter_name[] = "LOG_SQL_INPUT_OUTPUT"; | ||
30 | |||
31 | typedef struct { | ||
32 | apr_off_t bytes_in; | ||
33 | apr_off_t bytes_out; | ||
34 | } logio_config_t; | ||
35 | |||
36 | static void ap_logio_add_bytes_out(conn_rec *c, apr_off_t bytes){ | ||
37 | logio_config_t *cf = ap_get_module_config(c->conn_config, &log_sql_logio_module); | ||
38 | |||
39 | cf->bytes_out += bytes; | ||
40 | } | ||
41 | |||
42 | static const char *log_bytes_in(request_rec *r, char *a) | ||
43 | { | ||
44 | logio_config_t *cf = ap_get_module_config(r->connection->conn_config, | ||
45 | &log_sql_logio_module); | ||
46 | |||
47 | return apr_off_t_toa(r->pool, cf->bytes_in); | ||
48 | } | ||
49 | |||
50 | static const char *log_bytes_out(request_rec *r, char *a) | ||
51 | { | ||
52 | logio_config_t *cf = ap_get_module_config(r->connection->conn_config, | ||
53 | &log_sql_logio_module); | ||
54 | |||
55 | return apr_off_t_toa(r->pool, cf->bytes_out); | ||
56 | } | ||
57 | |||
58 | static int logio_transaction(request_rec *r) | ||
59 | { | ||
60 | logio_config_t *cf = ap_get_module_config(r->connection->conn_config, | ||
61 | &log_sql_logio_module); | ||
62 | |||
63 | cf->bytes_in = cf->bytes_out = 0; | ||
64 | |||
65 | return OK; | ||
66 | } | ||
67 | |||
68 | static apr_status_t logio_in_filter(ap_filter_t *f, | ||
69 | apr_bucket_brigade *bb, | ||
70 | ap_input_mode_t mode, | ||
71 | apr_read_type_e block, | ||
72 | apr_off_t readbytes) { | ||
73 | apr_off_t length; | ||
74 | apr_status_t status; | ||
75 | logio_config_t *cf = ap_get_module_config(f->c->conn_config, &log_sql_logio_module); | ||
76 | |||
77 | status = ap_get_brigade(f->next, bb, mode, block, readbytes); | ||
78 | |||
79 | apr_brigade_length (bb, 0, &length); | ||
80 | |||
81 | if (length > 0) | ||
82 | cf->bytes_in += length; | ||
83 | |||
84 | return status; | ||
85 | } | ||
86 | |||
87 | static apr_status_t logio_out_filter(ap_filter_t *f, | ||
88 | apr_bucket_brigade *bb) { | ||
89 | apr_bucket *b = APR_BRIGADE_LAST(bb); | ||
90 | |||
91 | /* End of data, make sure we flush */ | ||
92 | if (APR_BUCKET_IS_EOS(b)) { | ||
93 | APR_BUCKET_INSERT_BEFORE(b, | ||
94 | apr_bucket_flush_create(f->c->bucket_alloc)); | ||
95 | } | ||
96 | |||
97 | return ap_pass_brigade(f->next, bb); | ||
98 | } | ||
99 | |||
100 | static int logio_pre_conn(conn_rec *c, void *csd) { | ||
101 | logio_config_t *cf = apr_pcalloc(c->pool, sizeof(logio_config_t)); | ||
102 | |||
103 | ap_set_module_config(c->conn_config, &log_sql_logio_module, cf); | ||
104 | |||
105 | ap_add_input_filter(logio_filter_name, NULL, NULL, c); | ||
106 | ap_add_output_filter(logio_filter_name, NULL, NULL, c); | ||
107 | |||
108 | return OK; | ||
109 | } | ||
110 | |||
111 | static int post_config(apr_pool_t *p, apr_pool_t *plog, apr_pool_t *ptemp, server_rec *s) | ||
112 | { | ||
113 | log_sql_register_function(p, "bytes_in", log_bytes_in, LOGSQL_FUNCTION_REQ_FINAL); | ||
114 | log_sql_register_function(p, "bytes_out", log_bytes_out, LOGSQL_FUNCTION_REQ_FINAL); | ||
115 | |||
116 | log_sql_register_alias(s,p,'i', "bytes_in"); | ||
117 | log_sql_register_alias(s,p,'o', "bytes_out"); | ||
118 | |||
119 | log_sql_register_field(p, "bytes_in", "bytes_in", NULL, | ||
120 | "bytes_in", LOGSQL_DATATYPE_INT, 0); | ||
121 | log_sql_register_field(p, "bytes_out", "bytes_out", NULL, | ||
122 | "bytes_out", LOGSQL_DATATYPE_INT, 0); | ||
123 | |||
124 | log_sql_register_finish(s); | ||
125 | return OK; | ||
126 | } | ||
127 | |||
128 | static void register_hooks(apr_pool_t *p) { | ||
129 | static const char *pre[] = { "mod_log_sql.c", NULL }; | ||
130 | |||
131 | ap_hook_pre_connection(logio_pre_conn, NULL, NULL, APR_HOOK_MIDDLE); | ||
132 | ap_hook_log_transaction(logio_transaction, pre, NULL, APR_HOOK_MIDDLE); | ||
133 | ap_hook_post_config(post_config, NULL, NULL, APR_HOOK_REALLY_FIRST); | ||
134 | |||
135 | ap_register_input_filter(logio_filter_name, logio_in_filter, NULL, | ||
136 | AP_FTYPE_NETWORK - 1); | ||
137 | ap_register_output_filter(logio_filter_name, logio_out_filter, NULL, | ||
138 | AP_FTYPE_NETWORK - 1); | ||
139 | |||
140 | APR_REGISTER_OPTIONAL_FN(ap_logio_add_bytes_out); | ||
141 | } | ||
142 | |||
143 | module AP_MODULE_DECLARE_DATA log_sql_logio_module = { | ||
144 | STANDARD20_MODULE_STUFF, | ||
145 | NULL, NULL, NULL, NULL, NULL, register_hooks | ||
146 | }; | ||