Imported Upstream version 3.2.0
[platform/upstream/libwebsockets.git] / lib / roles / http / server / access-log.c
1 /*
2  * libwebsockets - server access log handling
3  *
4  * Copyright (C) 2010-2017 Andy Green <andy@warmcat.com>
5  *
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.
10  *
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.
15  *
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,
19  *  MA  02110-1301  USA
20  */
21
22 #include "core/private.h"
23
24 /*
25  * Produce Apache-compatible log string for wsi, like this:
26  *
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"
31  *
32  */
33
34 extern const char * const method_names[];
35
36 static const char * const hver[] = {
37         "HTTP/1.0", "HTTP/1.1", "HTTP/2"
38 };
39
40 void
41 lws_prepare_access_log_info(struct lws *wsi, char *uri_ptr, int uri_len, int meth)
42 {
43         char da[64], uri[256];
44         const char *pa, *me;
45         time_t t = time(NULL);
46         int l = 256, m;
47 #ifdef LWS_WITH_IPV6
48         char ads[INET6_ADDRSTRLEN];
49 #else
50         char ads[INET_ADDRSTRLEN];
51 #endif
52         struct tm *tmp;
53
54         if (!wsi->vhost)
55                 return;
56
57         /* only worry about preparing it if we store it */
58         if (wsi->vhost->log_fd == (int)LWS_INVALID_FILE)
59                 return;
60
61         if (wsi->access_log_pending)
62                 lws_access_log(wsi);
63
64         wsi->http.access_log.header_log = lws_malloc(l, "access log");
65         if (!wsi->http.access_log.header_log)
66                 return;
67
68         tmp = localtime(&t);
69         if (tmp)
70                 strftime(da, sizeof(da), "%d/%b/%Y:%H:%M:%S %z", tmp);
71         else
72                 strcpy(da, "01/Jan/1970:00:00:00 +0000");
73
74         pa = lws_get_peer_simple(wsi, ads, sizeof(ads));
75         if (!pa)
76                 pa = "(unknown)";
77
78         if (wsi->http2_substream)
79                 me = lws_hdr_simple_ptr(wsi, WSI_TOKEN_HTTP_COLON_METHOD);
80         else
81                 me = method_names[meth];
82         if (!me)
83                 me = "(null)";
84
85         m = uri_len;
86         if (m > (int)sizeof(uri) - 1)
87                 m = sizeof(uri) - 1;
88
89         strncpy(uri, uri_ptr, m);
90         uri[m] = '\0';
91
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]);
95
96         //lwsl_notice("%s\n", wsi->http.access_log.header_log);
97
98         l = lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_USER_AGENT);
99         if (l) {
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);
105                         return;
106                 }
107                 wsi->http.access_log.user_agent[0] = '\0';
108
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] = '\'';
114         }
115         l = lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_REFERER);
116         if (l) {
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);
122                         return;
123                 }
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)
127
128                         for (m = 0; m < l; m++)
129                                 if (wsi->http.access_log.referrer[m] == '\"')
130                                         wsi->http.access_log.referrer[m] = '\'';
131         }
132         wsi->access_log_pending = 1;
133 }
134
135
136 int
137 lws_access_log(struct lws *wsi)
138 {
139         char *p = wsi->http.access_log.user_agent, ass[512],
140              *p1 = wsi->http.access_log.referrer;
141         int l;
142
143         if (!wsi->vhost)
144                 return 0;
145
146         if (wsi->vhost->log_fd == (int)LWS_INVALID_FILE)
147                 return 0;
148
149         if (!wsi->access_log_pending)
150                 return 0;
151
152         if (!wsi->http.access_log.header_log)
153                 return 0;
154
155         if (!p)
156                 p = "";
157
158         if (!p1)
159                 p1 = "";
160
161         /*
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
165          */
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';
172                 l--;
173         }
174         l += lws_snprintf(ass + l, sizeof(ass) - 1 - l, "\" \"%s\"\n", p);
175
176         ass[sizeof(ass) - 1] = '\0';
177
178         if (write(wsi->vhost->log_fd, ass, l) != l)
179                 lwsl_err("Failed to write log\n");
180
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;
184         }
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;
188         }
189         if (wsi->http.access_log.referrer) {
190                 lws_free(wsi->http.access_log.referrer);
191                 wsi->http.access_log.referrer = NULL;
192         }
193         wsi->access_log_pending = 0;
194
195         return 0;
196 }
197