• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * libwebsockets - small server side websockets and web server implementation
3  *
4  * Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com>
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to
8  * deal in the Software without restriction, including without limitation the
9  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10  * sell copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22  * IN THE SOFTWARE.
23  */
24 
25 #include "private-lib-core.h"
26 
27 /*
28  * Produce Apache-compatible log string for wsi, like this:
29  *
30  * 2.31.234.19 - - [27/Mar/2016:03:22:44 +0800]
31  * "GET /aep-screen.png HTTP/1.1"
32  * 200 152987 "https://libwebsockets.org/index.html"
33  * "Mozilla/5.0 (Macint... Chrome/49.0.2623.87 Safari/537.36"
34  *
35  */
36 
37 extern const char * const method_names[];
38 
39 static const char * const hver[] = {
40 	"HTTP/1.0", "HTTP/1.1", "HTTP/2"
41 };
42 
43 void
lws_prepare_access_log_info(struct lws * wsi,char * uri_ptr,int uri_len,int meth)44 lws_prepare_access_log_info(struct lws *wsi, char *uri_ptr, int uri_len, int meth)
45 {
46 	char da[64], uri[256], ta[64];
47 	time_t t = time(NULL);
48 	struct lws *nwsi;
49 	const char *me;
50 	int l = 256, m;
51 	struct tm *ptm = NULL;
52 #if defined(LWS_HAVE_LOCALTIME_R)
53 	struct tm tm;
54 #endif
55 
56 	if (!wsi->a.vhost)
57 		return;
58 
59 	/* only worry about preparing it if we store it */
60 	if (wsi->a.vhost->log_fd == (int)LWS_INVALID_FILE)
61 		return;
62 
63 	if (wsi->access_log_pending)
64 		lws_access_log(wsi);
65 
66 	wsi->http.access_log.header_log = lws_malloc((unsigned int)l, "access log");
67 	if (!wsi->http.access_log.header_log)
68 		return;
69 
70 #if defined(LWS_HAVE_LOCALTIME_R)
71 	ptm = localtime_r(&t, &tm);
72 #else
73 	ptm = localtime(&t);
74 #endif
75 	if (ptm)
76 		strftime(da, sizeof(da), "%d/%b/%Y:%H:%M:%S %z", ptm);
77 	else
78 		strcpy(da, "01/Jan/1970:00:00:00 +0000");
79 
80 #if defined(LWS_ROLE_H2)
81 	if (wsi->mux_substream)
82 		me = lws_hdr_simple_ptr(wsi, WSI_TOKEN_HTTP_COLON_METHOD);
83 	else
84 #endif
85 		me = method_names[meth];
86 
87 	if (!me)
88 		me = "(null)";
89 
90 	m = uri_len;
91 	if (m > (int)sizeof(uri) - 1)
92 		m = sizeof(uri) - 1;
93 
94 	strncpy(uri, uri_ptr, (unsigned int)m);
95 	uri[m] = '\0';
96 
97 	nwsi = lws_get_network_wsi(wsi);
98 
99 	if (nwsi->sa46_peer.sa4.sin_family)
100 		lws_sa46_write_numeric_address(&nwsi->sa46_peer, ta, sizeof(ta));
101 	else
102 		strncpy(ta, "unknown", sizeof(ta));
103 
104 	lws_snprintf(wsi->http.access_log.header_log, (size_t)l,
105 		     "%s - - [%s] \"%s %s %s\"",
106 		     ta, da, me, uri, hver[wsi->http.request_version]);
107 
108 	//lwsl_notice("%s\n", wsi->http.access_log.header_log);
109 
110 	l = lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_USER_AGENT);
111 	if (l) {
112 		wsi->http.access_log.user_agent =
113 				lws_malloc((unsigned int)l + 5, "access log");
114 		if (!wsi->http.access_log.user_agent) {
115 			lwsl_err("OOM getting user agent\n");
116 			lws_free_set_NULL(wsi->http.access_log.header_log);
117 			return;
118 		}
119 		wsi->http.access_log.user_agent[0] = '\0';
120 
121 		if (lws_hdr_copy(wsi, wsi->http.access_log.user_agent, l + 4,
122 				 WSI_TOKEN_HTTP_USER_AGENT) >= 0)
123 			for (m = 0; m < l; m++)
124 				if (wsi->http.access_log.user_agent[m] == '\"')
125 					wsi->http.access_log.user_agent[m] = '\'';
126 	}
127 	l = lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_REFERER);
128 	if (l) {
129 		wsi->http.access_log.referrer = lws_malloc((unsigned int)l + 5, "referrer");
130 		if (!wsi->http.access_log.referrer) {
131 			lwsl_err("OOM getting referrer\n");
132 			lws_free_set_NULL(wsi->http.access_log.user_agent);
133 			lws_free_set_NULL(wsi->http.access_log.header_log);
134 			return;
135 		}
136 		wsi->http.access_log.referrer[0] = '\0';
137 		if (lws_hdr_copy(wsi, wsi->http.access_log.referrer,
138 				l + 4, WSI_TOKEN_HTTP_REFERER) >= 0)
139 
140 			for (m = 0; m < l; m++)
141 				if (wsi->http.access_log.referrer[m] == '\"')
142 					wsi->http.access_log.referrer[m] = '\'';
143 	}
144 	wsi->access_log_pending = 1;
145 }
146 
147 
148 int
lws_access_log(struct lws * wsi)149 lws_access_log(struct lws *wsi)
150 {
151 	char *p = wsi->http.access_log.user_agent, ass[512],
152 	     *p1 = wsi->http.access_log.referrer;
153 	int l;
154 
155 	if (!wsi->a.vhost)
156 		return 0;
157 
158 	if (wsi->a.vhost->log_fd == (int)LWS_INVALID_FILE)
159 		return 0;
160 
161 	if (!wsi->access_log_pending)
162 		return 0;
163 
164 	if (!wsi->http.access_log.header_log)
165 		return 0;
166 
167 	if (!p)
168 		p = "";
169 
170 	if (!p1)
171 		p1 = "";
172 
173 	/*
174 	 * We do this in two parts to restrict an oversize referrer such that
175 	 * we will always have space left to append an empty useragent, while
176 	 * maintaining the structure of the log text
177 	 */
178 	l = lws_snprintf(ass, sizeof(ass) - 7, "%s %d %lu \"%s",
179 			 wsi->http.access_log.header_log,
180 			 wsi->http.access_log.response,
181 			 wsi->http.access_log.sent, p1);
182 	if (strlen(p) > sizeof(ass) - 6 - (unsigned int)l) {
183 		p[sizeof(ass) - 6 - (unsigned int)l] = '\0';
184 		l--;
185 	}
186 	l += lws_snprintf(ass + (unsigned int)l, sizeof(ass) - 1 - (unsigned int)l, "\" \"%s\"\n", p);
187 
188 	ass[sizeof(ass) - 1] = '\0';
189 
190 	if ((int)write(wsi->a.vhost->log_fd, ass, (size_t)l) != l)
191 		lwsl_err("Failed to write log\n");
192 
193 	if (wsi->http.access_log.header_log) {
194 		lws_free(wsi->http.access_log.header_log);
195 		wsi->http.access_log.header_log = NULL;
196 	}
197 	if (wsi->http.access_log.user_agent) {
198 		lws_free(wsi->http.access_log.user_agent);
199 		wsi->http.access_log.user_agent = NULL;
200 	}
201 	if (wsi->http.access_log.referrer) {
202 		lws_free(wsi->http.access_log.referrer);
203 		wsi->http.access_log.referrer = NULL;
204 	}
205 	wsi->access_log_pending = 0;
206 
207 	return 0;
208 }
209 
210