From: reyk Date: Fri, 1 Aug 2014 22:24:05 +0000 (+0000) Subject: Use the log buffer to defer the logging until the connection is closed X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=27b71aedfa5de5b2e9ef31c8a9851e849affc4b8;p=openbsd Use the log buffer to defer the logging until the connection is closed or the request completed. Turn the old log message into a debug message. ok doug@ --- diff --git a/usr.sbin/httpd/httpd.h b/usr.sbin/httpd/httpd.h index c5a24c3aebf..30b312d3fbf 100644 --- a/usr.sbin/httpd/httpd.h +++ b/usr.sbin/httpd/httpd.h @@ -1,4 +1,4 @@ -/* $OpenBSD: httpd.h,v 1.29 2014/08/01 21:59:56 reyk Exp $ */ +/* $OpenBSD: httpd.h,v 1.30 2014/08/01 22:24:05 reyk Exp $ */ /* * Copyright (c) 2006 - 2014 Reyk Floeter @@ -408,6 +408,7 @@ in_port_t void server_write(struct bufferevent *, void *); void server_read(struct bufferevent *, void *); void server_error(struct bufferevent *, short, void *); +void server_log(struct client *); void server_close(struct client *, const char *); void server_dump(struct client *, const void *, size_t); int server_client_cmp(struct client *, struct client *); diff --git a/usr.sbin/httpd/server.c b/usr.sbin/httpd/server.c index ebbc07d2d17..0a2077e27ee 100644 --- a/usr.sbin/httpd/server.c +++ b/usr.sbin/httpd/server.c @@ -1,4 +1,4 @@ -/* $OpenBSD: server.c,v 1.20 2014/08/01 21:59:56 reyk Exp $ */ +/* $OpenBSD: server.c,v 1.21 2014/08/01 22:24:05 reyk Exp $ */ /* * Copyright (c) 2006 - 2014 Reyk Floeter @@ -584,13 +584,27 @@ server_inflight_dec(struct client *clt, const char *why) __func__, server_inflight, why); } +void +server_log(struct client *clt) +{ + char *ptr = NULL; + + if (!EVBUFFER_LENGTH(clt->clt_log)) + return; + + while ((ptr = evbuffer_readline(clt->clt_log)) != NULL) { + log_info("%s", ptr); + free(ptr); + } +} + void server_close(struct client *clt, const char *msg) { char ibuf[MAXHOSTNAMELEN], obuf[MAXHOSTNAMELEN]; - char *ptr = NULL; struct server *srv = clt->clt_srv; struct server_config *srv_conf = clt->clt_srv_conf; + extern int debug; SPLAY_REMOVE(client_tree, &srv->srv_clients, clt); @@ -603,21 +617,17 @@ server_close(struct client *clt, const char *msg) if (clt->clt_srvbev != NULL) bufferevent_disable(clt->clt_srvbev, EV_READ|EV_WRITE); - if (msg != NULL) { + server_log(clt); + + if (debug && msg != NULL) { memset(&ibuf, 0, sizeof(ibuf)); memset(&obuf, 0, sizeof(obuf)); (void)print_host(&clt->clt_ss, ibuf, sizeof(ibuf)); (void)server_http_host(&clt->clt_srv_ss, obuf, sizeof(obuf)); - if (EVBUFFER_LENGTH(clt->clt_log) && - evbuffer_add_printf(clt->clt_log, "\r\n") != -1) - ptr = evbuffer_readline(clt->clt_log); - log_info("server %s, " + log_debug("server %s, " "client %d (%d active), %s:%u -> %s, " - "%s%s%s", srv_conf->name, clt->clt_id, server_clients, - ibuf, ntohs(clt->clt_port), obuf, msg, - ptr == NULL ? "" : ",", ptr == NULL ? "" : ptr); - if (ptr != NULL) - free(ptr); + "%s", srv_conf->name, clt->clt_id, server_clients, + ibuf, ntohs(clt->clt_port), obuf, msg); } if (clt->clt_bev != NULL) diff --git a/usr.sbin/httpd/server_http.c b/usr.sbin/httpd/server_http.c index 719b9700d6b..45b7000df38 100644 --- a/usr.sbin/httpd/server_http.c +++ b/usr.sbin/httpd/server_http.c @@ -1,4 +1,4 @@ -/* $OpenBSD: server_http.c,v 1.26 2014/08/01 21:51:02 doug Exp $ */ +/* $OpenBSD: server_http.c,v 1.27 2014/08/01 22:24:05 reyk Exp $ */ /* * Copyright (c) 2006 - 2014 Reyk Floeter @@ -517,6 +517,8 @@ server_reset_http(struct client *clt) clt->clt_chunk = 0; clt->clt_bev->readcb = server_read_http; clt->clt_srv_conf = &srv->srv_conf; + + server_log(clt); } void @@ -1008,7 +1010,8 @@ server_log_http(struct client *clt, u_int code, size_t len) */ switch (srv_conf->logformat) { case LOG_FORMAT_COMMON: - log_info("%s %s - - [%s] \"%s %s%s%s%s%s\" %03d %zu", + if (evbuffer_add_printf(clt->clt_log, + "%s %s - - [%s] \"%s %s%s%s%s%s\" %03d %zu\n", srv_conf->name, ip, tstamp, server_httpmethod_byid(desc->http_method), desc->http_path == NULL ? "" : desc->http_path, @@ -1016,7 +1019,8 @@ server_log_http(struct client *clt, u_int code, size_t len) desc->http_query == NULL ? "" : desc->http_query, desc->http_version == NULL ? "" : " ", desc->http_version == NULL ? "" : desc->http_version, - code, len); + code, len) == -1) + return (-1); break; case LOG_FORMAT_COMBINED: @@ -1030,8 +1034,8 @@ server_log_http(struct client *clt, u_int code, size_t len) agent->kv_value == NULL) agent = NULL; - log_info("%s %s - - [%s] \"%s %s%s%s%s%s\" %03d %zu" - " \"%s\" \"%s\"", + if (evbuffer_add_printf(clt->clt_log, + "%s %s - - [%s] \"%s %s%s%s%s%s\" %03d %zu \"%s\" \"%s\"\n", srv_conf->name, ip, tstamp, server_httpmethod_byid(desc->http_method), desc->http_path == NULL ? "" : desc->http_path, @@ -1041,10 +1045,11 @@ server_log_http(struct client *clt, u_int code, size_t len) desc->http_version == NULL ? "" : desc->http_version, code, len, referrer == NULL ? "" : referrer->kv_value, - agent == NULL ? "" : agent->kv_value); + agent == NULL ? "" : agent->kv_value) == -1) + return (-1); break; - default: + case LOG_FORMAT_NONE: return (-1); }