Skip to content

Commit

Permalink
Merge pull request danghvu#6 from zaccheob/original
Browse files Browse the repository at this point in the history
  • Loading branch information
danghvu committed Nov 17, 2013
2 parents 9802bc9 + 80470e5 commit a9102fe
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
17 changes: 14 additions & 3 deletions mod_dumpost.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,21 @@
#include "apr_strings.h"
#include "mod_dumpost.h"

#define DEBUG(request, format, ...) ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, request, format, __VA_ARGS__);

module AP_MODULE_DECLARE_DATA dumpost_module;

static void dumpit(request_rec *r, apr_bucket *b, char *buf, apr_size_t *current_size) {

dumpost_cfg_t *cfg =
(dumpost_cfg_t *) ap_get_module_config(r->per_dir_config, &dumpost_module);

if (!(APR_BUCKET_IS_METADATA(b))) {
if (*current_size < cfg->max_size && !(APR_BUCKET_IS_METADATA(b))) {
const char * ibuf;
apr_size_t nbytes;
if (apr_bucket_read(b, &ibuf, &nbytes, APR_BLOCK_READ) == APR_SUCCESS) {
if (nbytes) {
DEBUG(r, "%ld bytes read from bucket for request %s", nbytes, r->the_request);
nbytes = min(nbytes, cfg->max_size - *current_size);
strncpy(buf, ibuf, nbytes);
*current_size += nbytes;
Expand All @@ -47,6 +50,11 @@ static void dumpit(request_rec *r, apr_bucket *b, char *buf, apr_size_t *current
"mod_dumpost: error reading data");
}
}
else {
if (APR_BUCKET_IS_EOS(b)) {
DEBUG(r, "EOS bucket detected for request %s", r->the_request);
}
}
}

apr_status_t logit(ap_filter_t *f) {
Expand Down Expand Up @@ -108,7 +116,7 @@ apr_status_t dumpost_input_filter (ap_filter_t *f, apr_bucket_brigade *bb,
state->mp = mp;
state->log_size = 0;
state->header_printed = 0;
state->buffer = apr_palloc(state->mp, cfg->max_size);
state->buffer = apr_palloc(state->mp, cfg->max_size + 1); //1 byte more because string buffer is null terminated
state->fd = NULL;

if (cfg->file != 0) {
Expand All @@ -121,6 +129,7 @@ apr_status_t dumpost_input_filter (ap_filter_t *f, apr_bucket_brigade *bb,
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, f->r, "mod_dumpost: unable to open the log file: %s %s", cfg->file, buferr);
}
}
//This doesn't work for oldest apr versions but i can obtain same result in a cleaner way using macro APR_BUCKET_IS_EOS() in dumpit function to detect when data stream ends
apr_pool_pre_cleanup_register(state->mp, f, (apr_status_t (*)(void *))logit);
}

Expand All @@ -135,7 +144,7 @@ apr_status_t dumpost_input_filter (ap_filter_t *f, apr_bucket_brigade *bb,
const char *s = apr_table_get(f->r->headers_in, headers[i]);
if (s == NULL) continue;
int len = strlen(s);
len = min(len, cfg->max_size - len);
len = min(len, cfg->max_size - buf_len);
strncpy(buf + buf_len, s, len);
buf_len += len + 1;
buf[buf_len-1] = ' ';
Expand All @@ -148,9 +157,11 @@ apr_status_t dumpost_input_filter (ap_filter_t *f, apr_bucket_brigade *bb,
return ret;

/* dump body */
DEBUG(f->r, "Start brigade for request: %s", f->r->the_request)
for (b = APR_BRIGADE_FIRST(bb); b != APR_BRIGADE_SENTINEL(bb); b = APR_BUCKET_NEXT(b))
if (state->log_size != LOG_IS_FULL && buf_len < cfg->max_size)
dumpit(f->r, b, buf + buf_len, &buf_len);
DEBUG(f->r, "End brigade for request: %s, buffer: %ld bytes", f->r->the_request, buf_len)

if (buf_len && state->log_size != LOG_IS_FULL) {
buf_len = min(buf_len, cfg->max_size);
Expand Down
1 change: 0 additions & 1 deletion mod_dumpost.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
#define LOG_IS_FULL -1
#define DEFAULT_MAX_SIZE 1024*1024
#define min(a,b) (a)<(b)?(a):(b)
#define DEBUG(s,t) ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, f->c->base_server, s, t);
#define CREATEMODE ( APR_UREAD | APR_UWRITE | APR_GREAD )

typedef struct dumpost_cfg_t {
Expand Down

0 comments on commit a9102fe

Please sign in to comment.