src/server/safs/addlog.c

Sat, 26 Nov 2022 17:07:08 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sat, 26 Nov 2022 17:07:08 +0100
changeset 438
22eca559aded
parent 415
d938228c382e
permissions
-rw-r--r--

refactore http listener creation

45
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1 /*
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
3 *
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
4 * Copyright 2013 Olaf Wintermann. All rights reserved.
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
5 *
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
8 *
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
11 *
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
15 *
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
27 */
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
28
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
29 #include <stdio.h>
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
30 #include <stdlib.h>
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
31
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
32 #include "addlog.h"
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
33
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
34 #include "../daemon/request.h"
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
35 #include "../daemon/vserver.h"
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
36 #include "../daemon/log.h"
196
0459c94a60ed adds combined log format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
37 #include "../util/util.h"
45
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
38
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
39 int common_log(pblock *pb, Session *sn, Request *rq) {
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
40 NSAPIRequest *request = (NSAPIRequest*)rq;
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
41 VirtualServer *vs = request->vs;
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
42 AccessLog *log = vs->log;
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
43
196
0459c94a60ed adds combined log format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
44 char *combined_str = pblock_findval("combined", pb);
0459c94a60ed adds combined log format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
45 WSBool combined = FALSE;
0459c94a60ed adds combined log format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
46 if(combined_str) {
0459c94a60ed adds combined log format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
47 combined = util_getboolean(combined_str, FALSE);
0459c94a60ed adds combined log format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
48 }
0459c94a60ed adds combined log format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
49
45
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
50 if(log == NULL) {
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
51 return REQ_NOACTION;
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
52 }
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
53
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
54 char *ip = pblock_findval("ip", sn->client);
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
55 char *user = pblock_findval("auth-user", rq->vars);
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
56 time_t t = time(NULL);
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
57 char *time = ctime(&t);
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
58 char *req = pblock_findval("clf-request", rq->reqpb);
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
59
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
60 // hack to get the content length
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
61 // http_start_response should not modify the header names
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
62 char *len = pblock_findval("Content-length", rq->srvhdrs);
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
63
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
64 if(!ip) {
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
65 ip = "-";
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
66 }
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
67 if(!user) {
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
68 user = "-";
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
69 }
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
70 if(!len) {
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
71 len = "0";
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
72 }
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
73
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
74 // remove trailing line feed
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 196
diff changeset
75 cxstring tmstr = cx_str(time);
45
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
76 if(tmstr.ptr[tmstr.length-1] == '\n') {
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
77 tmstr.length--;
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
78 }
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
79
196
0459c94a60ed adds combined log format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
80 if(combined) {
0459c94a60ed adds combined log format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
81 char *referer = pblock_findval("referer", rq->headers);
0459c94a60ed adds combined log format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
82 char *user_agent = pblock_findval("user-agent", rq->headers);
0459c94a60ed adds combined log format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
83 int refq = 1;
0459c94a60ed adds combined log format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
84 int uaq = 1;
0459c94a60ed adds combined log format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
85 if(!referer) {
0459c94a60ed adds combined log format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
86 referer = "-";
0459c94a60ed adds combined log format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
87 refq = 0;
0459c94a60ed adds combined log format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
88 }
0459c94a60ed adds combined log format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
89 if(!user_agent) {
0459c94a60ed adds combined log format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
90 user_agent = "-";
0459c94a60ed adds combined log format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
91 uaq = 0;
0459c94a60ed adds combined log format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
92 }
0459c94a60ed adds combined log format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
93 fprintf(
0459c94a60ed adds combined log format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
94 log->log->file,
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 196
diff changeset
95 "%s - %s [%.*s] \"%s\" %d %s %.*s%s%.*s %.*s%s%.*s\n",
196
0459c94a60ed adds combined log format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
96 ip,
0459c94a60ed adds combined log format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
97 user,
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 196
diff changeset
98 (int)tmstr.length,
196
0459c94a60ed adds combined log format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
99 tmstr.ptr,
0459c94a60ed adds combined log format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
100 req,
0459c94a60ed adds combined log format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
101 rq->status_num,
0459c94a60ed adds combined log format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
102 len,
0459c94a60ed adds combined log format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
103 refq,
0459c94a60ed adds combined log format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
104 "\"",
0459c94a60ed adds combined log format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
105 referer,
0459c94a60ed adds combined log format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
106 refq,
0459c94a60ed adds combined log format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
107 "\"",
0459c94a60ed adds combined log format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
108 uaq,
0459c94a60ed adds combined log format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
109 "\"",
0459c94a60ed adds combined log format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
110 user_agent,
0459c94a60ed adds combined log format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
111 uaq,
0459c94a60ed adds combined log format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
112 "\""
0459c94a60ed adds combined log format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
113 );
0459c94a60ed adds combined log format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
114 } else {
0459c94a60ed adds combined log format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
115 fprintf(
0459c94a60ed adds combined log format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
116 log->log->file,
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 196
diff changeset
117 "%s - %s [%.*s] \"%s\" %d %s\n",
196
0459c94a60ed adds combined log format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
118 ip,
0459c94a60ed adds combined log format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
119 user,
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 196
diff changeset
120 (int)tmstr.length,
196
0459c94a60ed adds combined log format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
121 tmstr.ptr,
0459c94a60ed adds combined log format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
122 req,
0459c94a60ed adds combined log format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
123 rq->status_num,
0459c94a60ed adds combined log format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
124 len);
0459c94a60ed adds combined log format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
125 }
77
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 45
diff changeset
126 fflush(log->log->file);
45
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
127
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
128
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
129 return REQ_PROCEED;
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
130 }
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
131

mercurial