2 * libwebsockets - server access log handling
4 * Copyright (C) 2010-2017 Andy Green <andy@warmcat.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation:
9 * version 2.1 of the License.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
22 #include "core/private.h"
25 * Produce Apache-compatible log string for wsi, like this:
27 * 2.31.234.19 - - [27/Mar/2016:03:22:44 +0800]
28 * "GET /aep-screen.png HTTP/1.1"
29 * 200 152987 "https://libwebsockets.org/index.html"
30 * "Mozilla/5.0 (Macint... Chrome/49.0.2623.87 Safari/537.36"
34 extern const char * const method_names[];
36 static const char * const hver[] = {
37 "HTTP/1.0", "HTTP/1.1", "HTTP/2"
41 lws_prepare_access_log_info(struct lws *wsi, char *uri_ptr, int uri_len, int meth)
43 char da[64], uri[256];
45 time_t t = time(NULL);
48 char ads[INET6_ADDRSTRLEN];
50 char ads[INET_ADDRSTRLEN];
57 /* only worry about preparing it if we store it */
58 if (wsi->vhost->log_fd == (int)LWS_INVALID_FILE)
61 if (wsi->access_log_pending)
64 wsi->http.access_log.header_log = lws_malloc(l, "access log");
65 if (!wsi->http.access_log.header_log)
70 strftime(da, sizeof(da), "%d/%b/%Y:%H:%M:%S %z", tmp);
72 strcpy(da, "01/Jan/1970:00:00:00 +0000");
74 pa = lws_get_peer_simple(wsi, ads, sizeof(ads));
78 if (wsi->http2_substream)
79 me = lws_hdr_simple_ptr(wsi, WSI_TOKEN_HTTP_COLON_METHOD);
81 me = method_names[meth];
86 if (m > (int)sizeof(uri) - 1)
89 strncpy(uri, uri_ptr, m);
92 lws_snprintf(wsi->http.access_log.header_log, l,
93 "%s - - [%s] \"%s %s %s\"",
94 pa, da, me, uri, hver[wsi->http.request_version]);
96 //lwsl_notice("%s\n", wsi->http.access_log.header_log);
98 l = lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_USER_AGENT);
100 wsi->http.access_log.user_agent =
101 lws_malloc(l + 5, "access log");
102 if (!wsi->http.access_log.user_agent) {
103 lwsl_err("OOM getting user agent\n");
104 lws_free_set_NULL(wsi->http.access_log.header_log);
107 wsi->http.access_log.user_agent[0] = '\0';
109 if (lws_hdr_copy(wsi, wsi->http.access_log.user_agent, l + 4,
110 WSI_TOKEN_HTTP_USER_AGENT) >= 0)
111 for (m = 0; m < l; m++)
112 if (wsi->http.access_log.user_agent[m] == '\"')
113 wsi->http.access_log.user_agent[m] = '\'';
115 l = lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_REFERER);
117 wsi->http.access_log.referrer = lws_malloc(l + 5, "referrer");
118 if (!wsi->http.access_log.referrer) {
119 lwsl_err("OOM getting referrer\n");
120 lws_free_set_NULL(wsi->http.access_log.user_agent);
121 lws_free_set_NULL(wsi->http.access_log.header_log);
124 wsi->http.access_log.referrer[0] = '\0';
125 if (lws_hdr_copy(wsi, wsi->http.access_log.referrer,
126 l + 4, WSI_TOKEN_HTTP_REFERER) >= 0)
128 for (m = 0; m < l; m++)
129 if (wsi->http.access_log.referrer[m] == '\"')
130 wsi->http.access_log.referrer[m] = '\'';
132 wsi->access_log_pending = 1;
137 lws_access_log(struct lws *wsi)
139 char *p = wsi->http.access_log.user_agent, ass[512],
140 *p1 = wsi->http.access_log.referrer;
146 if (wsi->vhost->log_fd == (int)LWS_INVALID_FILE)
149 if (!wsi->access_log_pending)
152 if (!wsi->http.access_log.header_log)
162 * We do this in two parts to restrict an oversize referrer such that
163 * we will always have space left to append an empty useragent, while
164 * maintaining the structure of the log text
166 l = lws_snprintf(ass, sizeof(ass) - 7, "%s %d %lu \"%s",
167 wsi->http.access_log.header_log,
168 wsi->http.access_log.response,
169 wsi->http.access_log.sent, p1);
170 if (strlen(p) > sizeof(ass) - 6 - l) {
171 p[sizeof(ass) - 6 - l] = '\0';
174 l += lws_snprintf(ass + l, sizeof(ass) - 1 - l, "\" \"%s\"\n", p);
176 ass[sizeof(ass) - 1] = '\0';
178 if (write(wsi->vhost->log_fd, ass, l) != l)
179 lwsl_err("Failed to write log\n");
181 if (wsi->http.access_log.header_log) {
182 lws_free(wsi->http.access_log.header_log);
183 wsi->http.access_log.header_log = NULL;
185 if (wsi->http.access_log.user_agent) {
186 lws_free(wsi->http.access_log.user_agent);
187 wsi->http.access_log.user_agent = NULL;
189 if (wsi->http.access_log.referrer) {
190 lws_free(wsi->http.access_log.referrer);
191 wsi->http.access_log.referrer = NULL;
193 wsi->access_log_pending = 0;