• 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];
47 	time_t t = time(NULL);
48 	struct lws *nwsi;
49 	const char *me;
50 	int l = 256, m;
51 	struct tm *tmp;
52 
53 	if (!wsi->vhost)
54 		return;
55 
56 	/* only worry about preparing it if we store it */
57 	if (wsi->vhost->log_fd == (int)LWS_INVALID_FILE)
58 		return;
59 
60 	if (wsi->access_log_pending)
61 		lws_access_log(wsi);
62 
63 	wsi->http.access_log.header_log = lws_malloc(l, "access log");
64 	if (!wsi->http.access_log.header_log)
65 		return;
66 
67 	tmp = localtime(&t);
68 	if (tmp)
69 		strftime(da, sizeof(da), "%d/%b/%Y:%H:%M:%S %z", tmp);
70 	else
71 		strcpy(da, "01/Jan/1970:00:00:00 +0000");
72 
73 	if (wsi->mux_substream)
74 		me = lws_hdr_simple_ptr(wsi, WSI_TOKEN_HTTP_COLON_METHOD);
75 	else
76 		me = method_names[meth];
77 	if (!me)
78 		me = "(null)";
79 
80 	m = uri_len;
81 	if (m > (int)sizeof(uri) - 1)
82 		m = sizeof(uri) - 1;
83 
84 	strncpy(uri, uri_ptr, m);
85 	uri[m] = '\0';
86 
87 	nwsi = lws_get_network_wsi(wsi);
88 
89 	lws_snprintf(wsi->http.access_log.header_log, l,
90 		     "%s - - [%s] \"%s %s %s\"",
91 		     nwsi->simple_ip[0] ? nwsi->simple_ip : "unknown", da, me, uri,
92 			hver[wsi->http.request_version]);
93 
94 	//lwsl_notice("%s\n", wsi->http.access_log.header_log);
95 
96 	l = lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_USER_AGENT);
97 	if (l) {
98 		wsi->http.access_log.user_agent =
99 				lws_malloc(l + 5, "access log");
100 		if (!wsi->http.access_log.user_agent) {
101 			lwsl_err("OOM getting user agent\n");
102 			lws_free_set_NULL(wsi->http.access_log.header_log);
103 			return;
104 		}
105 		wsi->http.access_log.user_agent[0] = '\0';
106 
107 		if (lws_hdr_copy(wsi, wsi->http.access_log.user_agent, l + 4,
108 				 WSI_TOKEN_HTTP_USER_AGENT) >= 0)
109 			for (m = 0; m < l; m++)
110 				if (wsi->http.access_log.user_agent[m] == '\"')
111 					wsi->http.access_log.user_agent[m] = '\'';
112 	}
113 	l = lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_REFERER);
114 	if (l) {
115 		wsi->http.access_log.referrer = lws_malloc(l + 5, "referrer");
116 		if (!wsi->http.access_log.referrer) {
117 			lwsl_err("OOM getting referrer\n");
118 			lws_free_set_NULL(wsi->http.access_log.user_agent);
119 			lws_free_set_NULL(wsi->http.access_log.header_log);
120 			return;
121 		}
122 		wsi->http.access_log.referrer[0] = '\0';
123 		if (lws_hdr_copy(wsi, wsi->http.access_log.referrer,
124 				l + 4, WSI_TOKEN_HTTP_REFERER) >= 0)
125 
126 			for (m = 0; m < l; m++)
127 				if (wsi->http.access_log.referrer[m] == '\"')
128 					wsi->http.access_log.referrer[m] = '\'';
129 	}
130 	wsi->access_log_pending = 1;
131 }
132 
133 
134 int
lws_access_log(struct lws * wsi)135 lws_access_log(struct lws *wsi)
136 {
137 	char *p = wsi->http.access_log.user_agent, ass[512],
138 	     *p1 = wsi->http.access_log.referrer;
139 	int l;
140 
141 	if (!wsi->vhost)
142 		return 0;
143 
144 	if (wsi->vhost->log_fd == (int)LWS_INVALID_FILE)
145 		return 0;
146 
147 	if (!wsi->access_log_pending)
148 		return 0;
149 
150 	if (!wsi->http.access_log.header_log)
151 		return 0;
152 
153 	if (!p)
154 		p = "";
155 
156 	if (!p1)
157 		p1 = "";
158 
159 	/*
160 	 * We do this in two parts to restrict an oversize referrer such that
161 	 * we will always have space left to append an empty useragent, while
162 	 * maintaining the structure of the log text
163 	 */
164 	l = lws_snprintf(ass, sizeof(ass) - 7, "%s %d %lu \"%s",
165 			 wsi->http.access_log.header_log,
166 			 wsi->http.access_log.response,
167 			 wsi->http.access_log.sent, p1);
168 	if (strlen(p) > sizeof(ass) - 6 - l) {
169 		p[sizeof(ass) - 6 - l] = '\0';
170 		l--;
171 	}
172 	l += lws_snprintf(ass + l, sizeof(ass) - 1 - l, "\" \"%s\"\n", p);
173 
174 	ass[sizeof(ass) - 1] = '\0';
175 
176 	if (write(wsi->vhost->log_fd, ass, l) != l)
177 		lwsl_err("Failed to write log\n");
178 
179 	if (wsi->http.access_log.header_log) {
180 		lws_free(wsi->http.access_log.header_log);
181 		wsi->http.access_log.header_log = NULL;
182 	}
183 	if (wsi->http.access_log.user_agent) {
184 		lws_free(wsi->http.access_log.user_agent);
185 		wsi->http.access_log.user_agent = NULL;
186 	}
187 	if (wsi->http.access_log.referrer) {
188 		lws_free(wsi->http.access_log.referrer);
189 		wsi->http.access_log.referrer = NULL;
190 	}
191 	wsi->access_log_pending = 0;
192 
193 	return 0;
194 }
195 
196