From 944a3fef29cfbdce4d026a8183b2809286d6b400 Mon Sep 17 00:00:00 2001 From: reyk Date: Sun, 3 Aug 2014 10:26:43 +0000 Subject: [PATCH] Add another log mode "connection" for a relayd(8)-style log entry after each connection, not every request. The code was already there and enabled on debug, I just turned it into an alternative log format. --- usr.sbin/httpd/httpd.conf.5 | 22 ++++++++++--- usr.sbin/httpd/httpd.h | 7 ++-- usr.sbin/httpd/parse.y | 14 +++++--- usr.sbin/httpd/server.c | 62 +++++++++++++++++++++++------------- usr.sbin/httpd/server_http.c | 13 ++++++-- 5 files changed, 80 insertions(+), 38 deletions(-) diff --git a/usr.sbin/httpd/httpd.conf.5 b/usr.sbin/httpd/httpd.conf.5 index befeaa0f30a..6eb86990e26 100644 --- a/usr.sbin/httpd/httpd.conf.5 +++ b/usr.sbin/httpd/httpd.conf.5 @@ -1,4 +1,4 @@ -.\" $OpenBSD: httpd.conf.5,v 1.17 2014/08/02 21:21:47 doug Exp $ +.\" $OpenBSD: httpd.conf.5,v 1.18 2014/08/03 10:26:43 reyk Exp $ .\" .\" Copyright (c) 2014 Reyk Floeter .\" @@ -14,7 +14,7 @@ .\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF .\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. .\" -.Dd $Mdocdate: August 2 2014 $ +.Dd $Mdocdate: August 3 2014 $ .Dt HTTPD.CONF 5 .Os .Sh NAME @@ -150,10 +150,22 @@ Enable writing an access log to syslog. The .Ar style can be -.Ar common +.Ar common , +.Ar combined or -.Ar combined . -It is similar to the default Apache and nginx access log. +.Ar connection . +The styles +.Ar common +and +.Ar combined +write a log entry after each request similar to the standard Apache +and nginx access log formats. +The style +.Ar connection +writes a summarized log entry after each connection, +that can have multiple requests, +similar to the format that is used by +.Xr relayd 8 . If not specified, the default is .Ar common . .It Ic root Ar directory diff --git a/usr.sbin/httpd/httpd.h b/usr.sbin/httpd/httpd.h index 3b3a78e1d32..a47e4c99a40 100644 --- a/usr.sbin/httpd/httpd.h +++ b/usr.sbin/httpd/httpd.h @@ -1,4 +1,4 @@ -/* $OpenBSD: httpd.h,v 1.33 2014/08/02 21:21:47 doug Exp $ */ +/* $OpenBSD: httpd.h,v 1.34 2014/08/03 10:26:43 reyk Exp $ */ /* * Copyright (c) 2006 - 2014 Reyk Floeter @@ -316,7 +316,8 @@ SPLAY_HEAD(client_tree, client); enum log_format { LOG_FORMAT_COMMON, - LOG_FORMAT_COMBINED + LOG_FORMAT_COMBINED, + LOG_FORMAT_CONNECTION }; struct server_config { @@ -413,7 +414,7 @@ int server_socket_connect(struct sockaddr_storage *, 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_log(struct client *, const char *); 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/parse.y b/usr.sbin/httpd/parse.y index 72dd913608e..2ff7c505dae 100644 --- a/usr.sbin/httpd/parse.y +++ b/usr.sbin/httpd/parse.y @@ -1,4 +1,4 @@ -/* $OpenBSD: parse.y,v 1.16 2014/08/02 21:21:47 doug Exp $ */ +/* $OpenBSD: parse.y,v 1.17 2014/08/03 10:26:43 reyk Exp $ */ /* * Copyright (c) 2007 - 2014 Reyk Floeter @@ -126,9 +126,9 @@ typedef struct { %} -%token AUTO DIRECTORY FCGI INDEX LISTEN LOCATION LOG NO ON PORT -%token PREFORK ROOT SERVER SOCKET TYPES -%token ERROR INCLUDE COMMON COMBINED +%token AUTO COMMON COMBINED CONNECTION DIRECTORY FCGI INDEX LISTEN LOCATION +%token LOG NO ON PORT PREFORK ROOT SERVER SOCKET TYPES +%token ERROR INCLUDE %token STRING %token NUMBER %type port @@ -449,6 +449,11 @@ logformat : LOG COMMON { srv->srv_conf.flags |= SRVFLAG_LOG; srv->srv_conf.logformat = LOG_FORMAT_COMBINED; } + | LOG CONNECTION { + srv->srv_conf.flags &= ~SRVFLAG_NO_LOG; + srv->srv_conf.flags |= SRVFLAG_LOG; + srv->srv_conf.logformat = LOG_FORMAT_CONNECTION; + } | NO LOG { srv->srv_conf.flags &= ~SRVFLAG_LOG; srv->srv_conf.flags |= SRVFLAG_NO_LOG; @@ -585,6 +590,7 @@ lookup(char *s) { "auto", AUTO }, { "combined", COMBINED }, { "common", COMMON }, + { "connection", CONNECTION }, { "directory", DIRECTORY }, { "fastcgi", FCGI }, { "include", INCLUDE }, diff --git a/usr.sbin/httpd/server.c b/usr.sbin/httpd/server.c index 173f6325b5a..942917a890d 100644 --- a/usr.sbin/httpd/server.c +++ b/usr.sbin/httpd/server.c @@ -1,4 +1,4 @@ -/* $OpenBSD: server.c,v 1.22 2014/08/02 11:52:01 reyk Exp $ */ +/* $OpenBSD: server.c,v 1.23 2014/08/03 10:26:44 reyk Exp $ */ /* * Copyright (c) 2006 - 2014 Reyk Floeter @@ -606,26 +606,53 @@ server_inflight_dec(struct client *clt, const char *why) } void -server_log(struct client *clt) +server_log(struct client *clt, const char *msg) { - char *ptr = NULL; + char ibuf[MAXHOSTNAMELEN], obuf[MAXHOSTNAMELEN]; + struct server_config *srv_conf = clt->clt_srv_conf; + char *ptr = NULL; + void (*log_cb)(const char *, ...) = NULL; + extern int debug; - if (!EVBUFFER_LENGTH(clt->clt_log)) - return; + switch (srv_conf->logformat) { + case LOG_FORMAT_CONNECTION: + log_cb = log_info; + break; + default: + if (debug) + log_cb = log_debug; + if (EVBUFFER_LENGTH(clt->clt_log)) { + while ((ptr = + evbuffer_readline(clt->clt_log)) != NULL) { + log_info("%s", ptr); + free(ptr); + } + } + break; + } - while ((ptr = evbuffer_readline(clt->clt_log)) != NULL) { - log_info("%s", ptr); - free(ptr); + if (log_cb != NULL && 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, "\n") != -1) + ptr = evbuffer_readline(clt->clt_log); + log_cb("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); } } void server_close(struct client *clt, const char *msg) { - char ibuf[MAXHOSTNAMELEN], obuf[MAXHOSTNAMELEN]; 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); @@ -638,18 +665,7 @@ server_close(struct client *clt, const char *msg) if (clt->clt_srvbev != NULL) bufferevent_disable(clt->clt_srvbev, EV_READ|EV_WRITE); - 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)); - log_debug("server %s, " - "client %d (%d active), %s:%u -> %s, " - "%s", srv_conf->name, clt->clt_id, server_clients, - ibuf, ntohs(clt->clt_port), obuf, msg); - } + server_log(clt, msg); if (clt->clt_bev != NULL) bufferevent_free(clt->clt_bev); diff --git a/usr.sbin/httpd/server_http.c b/usr.sbin/httpd/server_http.c index 7bd053fe6b9..6da7cf146a9 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.29 2014/08/03 10:22:30 reyk Exp $ */ +/* $OpenBSD: server_http.c,v 1.30 2014/08/03 10:26:44 reyk Exp $ */ /* * Copyright (c) 2006 - 2014 Reyk Floeter @@ -518,7 +518,7 @@ server_reset_http(struct client *clt) clt->clt_bev->readcb = server_read_http; clt->clt_srv_conf = &srv->srv_conf; - server_log(clt); + server_log(clt, NULL); } void @@ -993,7 +993,8 @@ server_log_http(struct client *clt, u_int code, size_t len) if (strftime(tstamp, sizeof(tstamp), "%d/%b/%Y:%H:%M:%S %z", tm) == 0) return (-1); - print_host(&clt->clt_ss, ip, sizeof(ip)); + if (print_host(&clt->clt_ss, ip, sizeof(ip)) == NULL) + return (-1); /* * For details on common log format, see: @@ -1043,6 +1044,12 @@ server_log_http(struct client *clt, u_int code, size_t len) agent == NULL ? "" : agent->kv_value) == -1) return (-1); break; + + case LOG_FORMAT_CONNECTION: + if (evbuffer_add_printf(clt->clt_log, " [%s]", + desc->http_path == NULL ? "" : desc->http_path) == -1) + return (-1); + break; } return (0); -- 2.20.1