From debb7aa04347a6b019b3971409f01c2eaab774b5 Mon Sep 17 00:00:00 2001 From: Andy Green Date: Sun, 27 Aug 2017 20:18:48 +0800 Subject: [PATCH] LWS_WITH_ACCESS_LOG: add referrer to log format and quotes around user agent This also forces any double-quotes in the UA or referrer to be single-quotes. This aligns to log to the "combined log format" described here https://httpd.apache.org/docs/1.3/logs.html#combined --- lib/libwebsockets.c | 14 +++++++++++--- lib/private-libwebsockets.h | 1 + lib/server.c | 19 ++++++++++++++++++- 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/lib/libwebsockets.c b/lib/libwebsockets.c index a6243d01..780d220f 100755 --- a/lib/libwebsockets.c +++ b/lib/libwebsockets.c @@ -3248,7 +3248,8 @@ lws_set_extension_option(struct lws *wsi, const char *ext_name, int lws_access_log(struct lws *wsi) { - char *p = wsi->access_log.user_agent, ass[512]; + char *p = wsi->access_log.user_agent, ass[512], + *p1 = wsi->access_log.referrer; int l; if (!wsi->access_log_pending) @@ -3260,9 +3261,12 @@ lws_access_log(struct lws *wsi) if (!p) p = ""; - l = lws_snprintf(ass, sizeof(ass) - 1, "%s %d %lu %s\n", + if (!p1) + p1 = ""; + + l = lws_snprintf(ass, sizeof(ass) - 1, "%s %d %lu \"%s\" \"%s\"\n", wsi->access_log.header_log, - wsi->access_log.response, wsi->access_log.sent, p); + wsi->access_log.response, wsi->access_log.sent, p1, p); if (wsi->vhost->log_fd != (int)LWS_INVALID_FILE) { if (write(wsi->vhost->log_fd, ass, l) != l) @@ -3278,6 +3282,10 @@ lws_access_log(struct lws *wsi) lws_free(wsi->access_log.user_agent); wsi->access_log.user_agent = NULL; } + if (wsi->access_log.referrer) { + lws_free(wsi->access_log.referrer); + wsi->access_log.referrer = NULL; + } wsi->access_log_pending = 0; return 0; diff --git a/lib/private-libwebsockets.h b/lib/private-libwebsockets.h index 5b5b25df..0aa5410c 100644 --- a/lib/private-libwebsockets.h +++ b/lib/private-libwebsockets.h @@ -1545,6 +1545,7 @@ struct lws_rewrite; struct lws_access_log { char *header_log; char *user_agent; + char *referrer; unsigned long sent; int response; }; diff --git a/lib/server.c b/lib/server.c index 6e7c8b0c..7a34111d 100644 --- a/lib/server.c +++ b/lib/server.c @@ -906,7 +906,7 @@ lws_http_action(struct lws *wsi) const char *pa, *me; struct tm *tmp; time_t t = time(NULL); - int l = 256; + int l = 256, m; if (wsi->access_log_pending) lws_access_log(wsi); @@ -939,6 +939,23 @@ lws_http_action(struct lws *wsi) l + 1, WSI_TOKEN_HTTP_USER_AGENT); else lwsl_err("OOM getting user agent\n"); + + for (m = 0; m < l; m++) + if (wsi->access_log.user_agent[m] == '\"') + wsi->access_log.user_agent[m] = '\''; + } + l = lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_REFERER); + if (l) { + wsi->access_log.referrer = lws_malloc(l + 2); + if (wsi->access_log.referrer) + lws_hdr_copy(wsi, wsi->access_log.referrer, + l + 1, WSI_TOKEN_HTTP_REFERER); + else + lwsl_err("OOM getting user agent\n"); + + for (m = 0; m < l; m++) + if (wsi->access_log.referrer[m] == '\"') + wsi->access_log.referrer[m] = '\''; } wsi->access_log_pending = 1; }