|
Packit |
90a5c9 |
/* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
Packit |
90a5c9 |
* contributor license agreements. See the NOTICE file distributed with
|
|
Packit |
90a5c9 |
* this work for additional information regarding copyright ownership.
|
|
Packit |
90a5c9 |
* The ASF licenses this file to You under the Apache License, Version 2.0
|
|
Packit |
90a5c9 |
* (the "License"); you may not use this file except in compliance with
|
|
Packit |
90a5c9 |
* the License. You may obtain a copy of the License at
|
|
Packit |
90a5c9 |
*
|
|
Packit |
90a5c9 |
* http://www.apache.org/licenses/LICENSE-2.0
|
|
Packit |
90a5c9 |
*
|
|
Packit |
90a5c9 |
* Unless required by applicable law or agreed to in writing, software
|
|
Packit |
90a5c9 |
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
Packit |
90a5c9 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
Packit |
90a5c9 |
* See the License for the specific language governing permissions and
|
|
Packit |
90a5c9 |
* limitations under the License.
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/*
|
|
Packit |
90a5c9 |
* Written by Bojan Smojver <bojan rexursive.com>.
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
#include "apr_strings.h"
|
|
Packit |
90a5c9 |
#include "apr_lib.h"
|
|
Packit |
90a5c9 |
#include "apr_hash.h"
|
|
Packit |
90a5c9 |
#include "apr_optional.h"
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
#define APR_WANT_STRFUNC
|
|
Packit |
90a5c9 |
#include "apr_want.h"
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
#include "ap_config.h"
|
|
Packit |
90a5c9 |
#include "mod_log_config.h"
|
|
Packit |
90a5c9 |
#include "httpd.h"
|
|
Packit |
90a5c9 |
#include "http_core.h"
|
|
Packit |
90a5c9 |
#include "http_config.h"
|
|
Packit |
90a5c9 |
#include "http_connection.h"
|
|
Packit |
90a5c9 |
#include "http_protocol.h"
|
|
Packit |
90a5c9 |
#include "http_request.h"
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
module AP_MODULE_DECLARE_DATA logio_module;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static const char logio_filter_name[] = "LOG_INPUT_OUTPUT";
|
|
Packit |
90a5c9 |
static const char logio_ttfb_filter_name[] = "LOGIO_TTFB_OUT";
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/*
|
|
Packit |
90a5c9 |
* Logging of input and output config...
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
typedef struct logio_config_t {
|
|
Packit |
90a5c9 |
apr_off_t bytes_in;
|
|
Packit |
90a5c9 |
apr_off_t bytes_out;
|
|
Packit |
90a5c9 |
apr_off_t bytes_last_request;
|
|
Packit |
90a5c9 |
} logio_config_t;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
typedef struct logio_dirconf_t {
|
|
Packit |
90a5c9 |
unsigned int track_ttfb:1;
|
|
Packit |
90a5c9 |
} logio_dirconf_t;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
typedef struct logio_req_t {
|
|
Packit |
90a5c9 |
apr_time_t ttfb;
|
|
Packit |
90a5c9 |
} logio_req_t;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/*
|
|
Packit |
90a5c9 |
* Optional function for the core to add to bytes_out
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static void ap_logio_add_bytes_out(conn_rec *c, apr_off_t bytes)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
logio_config_t *cf = ap_get_module_config(c->conn_config, &logio_module);
|
|
Packit |
90a5c9 |
cf->bytes_out += bytes;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/*
|
|
Packit |
90a5c9 |
* Optional function for modules to adjust bytes_in
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static void ap_logio_add_bytes_in(conn_rec *c, apr_off_t bytes)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
logio_config_t *cf = ap_get_module_config(c->conn_config, &logio_module);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
cf->bytes_in += bytes;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/*
|
|
Packit |
90a5c9 |
* Optional function to get total byte count of last request for
|
|
Packit |
90a5c9 |
* ap_increment_counts.
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static apr_off_t ap_logio_get_last_bytes(conn_rec *c)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
logio_config_t *cf = ap_get_module_config(c->conn_config, &logio_module);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
return cf->bytes_last_request;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/*
|
|
Packit |
90a5c9 |
* Format items...
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static const char *log_bytes_in(request_rec *r, char *a)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
logio_config_t *cf = ap_get_module_config(r->connection->conn_config,
|
|
Packit |
90a5c9 |
&logio_module);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
return apr_off_t_toa(r->pool, cf->bytes_in);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static const char *log_bytes_out(request_rec *r, char *a)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
logio_config_t *cf = ap_get_module_config(r->connection->conn_config,
|
|
Packit |
90a5c9 |
&logio_module);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
return apr_off_t_toa(r->pool, cf->bytes_out);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static const char *log_bytes_combined(request_rec *r, char *a)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
logio_config_t *cf = ap_get_module_config(r->connection->conn_config,
|
|
Packit |
90a5c9 |
&logio_module);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
return apr_off_t_toa(r->pool, cf->bytes_out + cf->bytes_in);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static const char *log_ttfb(request_rec *r, char *a)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
logio_req_t *rconf = ap_get_module_config(r->request_config,
|
|
Packit |
90a5c9 |
&logio_module);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (!rconf || !rconf->ttfb) {
|
|
Packit |
90a5c9 |
return "-";
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
return apr_psprintf(r->pool, "%" APR_TIME_T_FMT, rconf->ttfb);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
/*
|
|
Packit |
90a5c9 |
* Reset counters after logging...
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static int logio_transaction(request_rec *r)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
logio_config_t *cf = ap_get_module_config(r->connection->conn_config,
|
|
Packit |
90a5c9 |
&logio_module);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/* need to save byte count of last request for ap_increment_counts */
|
|
Packit |
90a5c9 |
cf->bytes_last_request = cf->bytes_in + cf->bytes_out;
|
|
Packit |
90a5c9 |
cf->bytes_in = cf->bytes_out = 0;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
return OK;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/*
|
|
Packit |
90a5c9 |
* Logging of input filter...
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static apr_status_t logio_in_filter(ap_filter_t *f,
|
|
Packit |
90a5c9 |
apr_bucket_brigade *bb,
|
|
Packit |
90a5c9 |
ap_input_mode_t mode,
|
|
Packit |
90a5c9 |
apr_read_type_e block,
|
|
Packit |
90a5c9 |
apr_off_t readbytes)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
apr_off_t length;
|
|
Packit |
90a5c9 |
apr_status_t status;
|
|
Packit |
90a5c9 |
logio_config_t *cf = ap_get_module_config(f->c->conn_config, &logio_module);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
status = ap_get_brigade(f->next, bb, mode, block, readbytes);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
apr_brigade_length (bb, 0, &length);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (length > 0)
|
|
Packit |
90a5c9 |
cf->bytes_in += length;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
return status;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/*
|
|
Packit |
90a5c9 |
* The hooks...
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static int logio_pre_conn(conn_rec *c, void *csd)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
logio_config_t *cf = apr_pcalloc(c->pool, sizeof(*cf));
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
ap_set_module_config(c->conn_config, &logio_module, cf);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
ap_add_input_filter(logio_filter_name, NULL, NULL, c);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
return OK;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static int logio_pre_config(apr_pool_t *p, apr_pool_t *plog, apr_pool_t *ptemp)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
APR_OPTIONAL_FN_TYPE(ap_register_log_handler) *log_pfn_register;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
log_pfn_register = APR_RETRIEVE_OPTIONAL_FN(ap_register_log_handler);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (log_pfn_register) {
|
|
Packit |
90a5c9 |
log_pfn_register(p, "I", log_bytes_in, 0);
|
|
Packit |
90a5c9 |
log_pfn_register(p, "O", log_bytes_out, 0);
|
|
Packit |
90a5c9 |
log_pfn_register(p, "S", log_bytes_combined, 0);
|
|
Packit |
90a5c9 |
log_pfn_register(p, "^FB", log_ttfb, 0);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
return OK;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static apr_status_t logio_ttfb_filter(ap_filter_t *f, apr_bucket_brigade *b)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
request_rec *r = f->r;
|
|
Packit |
90a5c9 |
logio_dirconf_t *conf = ap_get_module_config(r->per_dir_config,
|
|
Packit |
90a5c9 |
&logio_module);
|
|
Packit |
90a5c9 |
if (conf && conf->track_ttfb) {
|
|
Packit |
90a5c9 |
logio_req_t *rconf = ap_get_module_config(r->request_config,
|
|
Packit |
90a5c9 |
&logio_module);
|
|
Packit |
90a5c9 |
if (rconf == NULL) {
|
|
Packit |
90a5c9 |
rconf = apr_pcalloc(r->pool, sizeof(logio_req_t));
|
|
Packit |
90a5c9 |
rconf->ttfb = apr_time_now() - r->request_time;
|
|
Packit |
90a5c9 |
ap_set_module_config(r->request_config, &logio_module, rconf);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
ap_remove_output_filter(f);
|
|
Packit |
90a5c9 |
return ap_pass_brigade(f->next, b);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static void logio_insert_filter(request_rec * r)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
logio_dirconf_t *conf = ap_get_module_config(r->per_dir_config,
|
|
Packit |
90a5c9 |
&logio_module);
|
|
Packit |
90a5c9 |
if (conf->track_ttfb) {
|
|
Packit |
90a5c9 |
ap_add_output_filter(logio_ttfb_filter_name, NULL, r, r->connection);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static const char *logio_track_ttfb(cmd_parms *cmd, void *in_dir_config, int arg)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
logio_dirconf_t *dir_config = in_dir_config;
|
|
Packit |
90a5c9 |
dir_config->track_ttfb = arg;
|
|
Packit |
90a5c9 |
return NULL;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static void *create_logio_dirconf (apr_pool_t *p, char *dummy)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
logio_dirconf_t *new =
|
|
Packit |
90a5c9 |
(logio_dirconf_t *) apr_pcalloc(p, sizeof(logio_dirconf_t));
|
|
Packit |
90a5c9 |
return (void *) new;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static const command_rec logio_cmds[] = {
|
|
Packit |
90a5c9 |
AP_INIT_FLAG ("LogIOTrackTTFB", logio_track_ttfb, NULL, OR_ALL,
|
|
Packit |
90a5c9 |
"Set to 'ON' to enable tracking time to first byte"),
|
|
Packit |
90a5c9 |
{NULL}
|
|
Packit |
90a5c9 |
};
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static void register_hooks(apr_pool_t *p)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
static const char *pre[] = { "mod_log_config.c", NULL };
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
ap_hook_pre_connection(logio_pre_conn, NULL, NULL, APR_HOOK_MIDDLE);
|
|
Packit |
90a5c9 |
ap_hook_pre_config(logio_pre_config, NULL, NULL, APR_HOOK_REALLY_FIRST);
|
|
Packit |
90a5c9 |
ap_hook_log_transaction(logio_transaction, pre, NULL, APR_HOOK_MIDDLE);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
ap_register_input_filter(logio_filter_name, logio_in_filter, NULL,
|
|
Packit |
90a5c9 |
AP_FTYPE_NETWORK - 1);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
ap_hook_insert_filter(logio_insert_filter, NULL, NULL, APR_HOOK_LAST);
|
|
Packit |
90a5c9 |
ap_register_output_filter(logio_ttfb_filter_name, logio_ttfb_filter, NULL,
|
|
Packit |
90a5c9 |
AP_FTYPE_RESOURCE);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
APR_REGISTER_OPTIONAL_FN(ap_logio_add_bytes_out);
|
|
Packit |
90a5c9 |
APR_REGISTER_OPTIONAL_FN(ap_logio_add_bytes_in);
|
|
Packit |
90a5c9 |
APR_REGISTER_OPTIONAL_FN(ap_logio_get_last_bytes);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
AP_DECLARE_MODULE(logio) =
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
STANDARD20_MODULE_STUFF,
|
|
Packit |
90a5c9 |
create_logio_dirconf, /* create per-dir config */
|
|
Packit |
90a5c9 |
NULL, /* merge per-dir config */
|
|
Packit |
90a5c9 |
NULL, /* server config */
|
|
Packit |
90a5c9 |
NULL, /* merge server config */
|
|
Packit |
90a5c9 |
logio_cmds, /* command apr_table_t */
|
|
Packit |
90a5c9 |
register_hooks /* register hooks */
|
|
Packit |
90a5c9 |
};
|