Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pxyconn.c: refactor content log #325

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions log.c
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,9 @@ log_masterkey_writecb(UNUSED void *fh, UNUSED unsigned long ctl,
static void
log_masterkey_fini(void)
{
close(masterkey_fd);
if (masterkey_fd != -1)
close(masterkey_fd);
masterkey_fd = -1;
}


Expand Down Expand Up @@ -325,7 +327,9 @@ log_connect_writecb(UNUSED void *fh, UNUSED unsigned long ctl,
static void
log_connect_fini(void)
{
close(connect_fd);
if (connect_fd != -1)
close(connect_fd);
connect_fd = -1;
}


Expand Down
89 changes: 35 additions & 54 deletions pxyconn.c
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,32 @@ typedef struct pxy_conn_ctx {
#define WANT_CONTENT_LOG(ctx) (((ctx)->opts->contentlog||(ctx)->opts->pcaplog)&&!(ctx)->passthrough)
#endif /* WITHOUT_MIRROR */

static void
add_line_to_content_log(const char *line, logbuf_t **plb, logbuf_t **ptail) {
logbuf_t *tmp;
tmp = logbuf_new_printf(NULL, "%s\r\n", line);
if (tmp) {
if (*ptail) {
(*ptail)->next = tmp;
(*ptail) = (*ptail)->next;
} else {
*plb = *ptail = tmp;
}
}
}

static void
submit_content_logbuf_free(pxy_conn_ctx_t *ctx, logbuf_t *lb, int is_req) {
if (lb) {
if (log_content_submit(&ctx->logctx, lb,
is_req) == -1) {
logbuf_free(lb);
log_err_printf("Warning: Content log "
"submission failed\n");
}
}
}

static pxy_conn_ctx_t *
pxy_conn_ctx_new(proxyspec_t *spec, opts_t *opts,
pxy_thrmgr_ctx_t *thrmgr, evutil_socket_t fd)
Expand Down Expand Up @@ -1622,12 +1648,7 @@ pxy_ocsp_deny(pxy_conn_ctx_t *ctx)
lb = logbuf_new_alloc(evbuffer_get_length(inbuf), NULL);
if (lb &&
(evbuffer_copyout(inbuf, lb->buf, lb->sz) != -1)) {
if (log_content_submit(&ctx->logctx, lb,
1/*req*/) == -1) {
logbuf_free(lb);
log_err_printf("Warning: Content log "
"submission failed\n");
}
submit_content_logbuf_free(ctx, lb, 1/*req*/);
}
}
evbuffer_drain(inbuf, evbuffer_get_length(inbuf));
Expand All @@ -1640,14 +1661,7 @@ pxy_ocsp_deny(pxy_conn_ctx_t *ctx)
if (WANT_CONTENT_LOG(ctx)) {
logbuf_t *lb;
lb = logbuf_new_copy(ocspresp, sizeof(ocspresp) - 1, NULL);
if (lb) {
if (log_content_submit(&ctx->logctx, lb,
0/*resp*/) == -1) {
logbuf_free(lb);
log_err_printf("Warning: Content log "
"submission failed\n");
}
}
submit_content_logbuf_free(ctx, lb, 0/*resp*/);
}
}

Expand Down Expand Up @@ -1785,16 +1799,7 @@ pxy_bev_readcb(struct bufferevent *bev, void *arg)
EVBUFFER_EOL_CRLF))) {
char *replace;
if (WANT_CONTENT_LOG(ctx)) {
logbuf_t *tmp;
tmp = logbuf_new_printf(NULL, "%s\r\n", line);
if (tail) {
if (tmp) {
tail->next = tmp;
tail = tail->next;
}
} else {
lb = tail = tmp;
}
add_line_to_content_log(line, &lb, &tail);
}
replace = pxy_http_reqhdr_filter_line(line, ctx);
if (replace == line) {
Expand All @@ -1812,13 +1817,8 @@ pxy_bev_readcb(struct bufferevent *bev, void *arg)
break;
}
}
if (lb && WANT_CONTENT_LOG(ctx)) {
if (log_content_submit(&ctx->logctx, lb,
1/*req*/) == -1) {
logbuf_free(lb);
log_err_printf("Warning: Content log "
"submission failed\n");
}
if (WANT_CONTENT_LOG(ctx)) {
submit_content_logbuf_free(ctx, lb, 1/*req*/);
}
if (!ctx->seen_req_header)
return;
Expand All @@ -1832,16 +1832,7 @@ pxy_bev_readcb(struct bufferevent *bev, void *arg)
EVBUFFER_EOL_CRLF))) {
char *replace;
if (WANT_CONTENT_LOG(ctx)) {
logbuf_t *tmp;
tmp = logbuf_new_printf(NULL, "%s\r\n", line);
if (tail) {
if (tmp) {
tail->next = tmp;
tail = tail->next;
}
} else {
lb = tail = tmp;
}
add_line_to_content_log(line, &lb, &tail);
}
replace = pxy_http_resphdr_filter_line(line, ctx);
if (replace == line) {
Expand All @@ -1859,13 +1850,8 @@ pxy_bev_readcb(struct bufferevent *bev, void *arg)
break;
}
}
if (lb && WANT_CONTENT_LOG(ctx)) {
if (log_content_submit(&ctx->logctx, lb,
0/*resp*/) == -1) {
logbuf_free(lb);
log_err_printf("Warning: Content log "
"submission failed\n");
}
if (WANT_CONTENT_LOG(ctx)) {
submit_content_logbuf_free(ctx, lb, 0/*resp*/);
}
if (!ctx->seen_resp_header)
return;
Expand All @@ -1885,12 +1871,7 @@ pxy_bev_readcb(struct bufferevent *bev, void *arg)
logbuf_t *lb;
lb = logbuf_new_alloc(evbuffer_get_length(inbuf), NULL);
if (lb && (evbuffer_copyout(inbuf, lb->buf, lb->sz) != -1)) {
if (log_content_submit(&ctx->logctx, lb,
(bev == ctx->src.bev)) == -1) {
logbuf_free(lb);
log_err_printf("Warning: Content log "
"submission failed\n");
}
submit_content_logbuf_free(ctx, lb, (bev == ctx->src.bev));
}
}
evbuffer_add_buffer(outbuf, inbuf);
Expand Down