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
This commit is contained in:
Andy Green 2017-08-27 20:18:48 +08:00
parent ca045d4a8e
commit debb7aa043
3 changed files with 30 additions and 4 deletions

View file

@ -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;

View file

@ -1545,6 +1545,7 @@ struct lws_rewrite;
struct lws_access_log {
char *header_log;
char *user_agent;
char *referrer;
unsigned long sent;
int response;
};

View file

@ -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;
}