]> git.ipfire.org Git - thirdparty/squid.git/blob - src/log/FormatHttpdCombined.cc
Source Format Enforcement (#532)
[thirdparty/squid.git] / src / log / FormatHttpdCombined.cc
1 /*
2 * Copyright (C) 1996-2020 The Squid Software Foundation and contributors
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
9 /* DEBUG: section 46 Access Log - Apache combined format */
10
11 #include "squid.h"
12 #include "AccessLogEntry.h"
13 #include "format/Quoting.h"
14 #include "format/Token.h"
15 #include "globals.h"
16 #include "HttpRequest.h"
17 #include "log/File.h"
18 #include "log/Formats.h"
19 #include "SquidConfig.h"
20 #include "SquidTime.h"
21
22 void
23 Log::Format::HttpdCombined(const AccessLogEntry::Pointer &al, Logfile * logfile)
24 {
25 const char *user_ident = ::Format::QuoteUrlEncodeUsername(al->getClientIdent());
26 const char *user_auth = NULL;
27 const char *referer = NULL;
28 const char *agent = NULL;
29
30 if (al->request) {
31 #if USE_AUTH
32 if (al->request->auth_user_request != NULL)
33 user_auth = ::Format::QuoteUrlEncodeUsername(al->request->auth_user_request->username());
34 #endif
35 referer = al->request->header.getStr(Http::HdrType::REFERER);
36 agent = al->request->header.getStr(Http::HdrType::USER_AGENT);
37 }
38
39 if (!referer || *referer == '\0')
40 referer = "-";
41
42 if (!agent || *agent == '\0')
43 agent = "-";
44
45 char clientip[MAX_IPSTRLEN];
46 al->getLogClientIp(clientip, MAX_IPSTRLEN);
47
48 const SBuf method(al->getLogMethod());
49
50 logfilePrintf(logfile, "%s %s %s [%s] \"" SQUIDSBUFPH " " SQUIDSBUFPH " %s/%d.%d\" %d %" PRId64 " \"%s\" \"%s\" %s:%s%s",
51 clientip,
52 user_ident ? user_ident : dash_str,
53 user_auth ? user_auth : dash_str,
54 Time::FormatHttpd(squid_curtime),
55 SQUIDSBUFPRINT(method),
56 SQUIDSBUFPRINT(al->url),
57 AnyP::ProtocolType_str[al->http.version.protocol],
58 al->http.version.major, al->http.version.minor,
59 al->http.code,
60 al->http.clientReplySz.messageTotal(),
61 referer,
62 agent,
63 al->cache.code.c_str(),
64 hier_code_str[al->hier.code],
65 (Config.onoff.log_mime_hdrs?"":"\n"));
66
67 safe_free(user_ident);
68 safe_free(user_auth);
69
70 if (Config.onoff.log_mime_hdrs) {
71 char *ereq = ::Format::QuoteMimeBlob(al->headers.request);
72 MemBuf mb;
73 mb.init();
74 al->packReplyHeaders(mb);
75 auto erep = ::Format::QuoteMimeBlob(mb.content());
76 logfilePrintf(logfile, " [%s] [%s]\n", ereq, erep);
77 safe_free(ereq);
78 safe_free(erep);
79 }
80 }
81