Skip to content

Commit

Permalink
nghttpx: Add accesslog variables to record request path without query
Browse files Browse the repository at this point in the history
This commit the following variables to construct request line without
including query component:

* $method
* $path
* $path_without_query
* $protocol_version
  • Loading branch information
tatsuhiro-t committed Sep 19, 2020
1 parent 7b4de40 commit 4e3c61e
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 0 deletions.
4 changes: 4 additions & 0 deletions gennghttpxfun.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,10 @@
"tls_client_serial",
"backend_host",
"backend_port",
"method",
"path",
"path_without_query",
"protocol_version",
]

if __name__ == '__main__':
Expand Down
8 changes: 8 additions & 0 deletions src/shrpx.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2607,6 +2607,14 @@ HTTP/2:
request. "-" if backend host is not available.
* $backend_port: backend port used to fulfill the
request. "-" if backend host is not available.
* $method: HTTP method
* $path: Request path including query. For CONNECT
request, authority is recorded.
* $path_without_query: $path up to the first '?'
character. For CONNECT request, authority is
recorded.
* $protocol_version: HTTP version (e.g., HTTP/1.1,
HTTP/2)
The variable can be enclosed by "{" and "}" for
disambiguation (e.g., ${remote_addr}).
Expand Down
24 changes: 24 additions & 0 deletions src/shrpx_config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,11 @@ LogFragmentType log_var_lookup_token(const char *name, size_t namelen) {
break;
case 4:
switch (name[3]) {
case 'h':
if (util::strieq_l("pat", name, 3)) {
return LogFragmentType::PATH;
}
break;
case 'n':
if (util::strieq_l("alp", name, 3)) {
return LogFragmentType::ALPN;
Expand All @@ -396,6 +401,11 @@ LogFragmentType log_var_lookup_token(const char *name, size_t namelen) {
break;
case 6:
switch (name[5]) {
case 'd':
if (util::strieq_l("metho", name, 5)) {
return LogFragmentType::METHOD;
}
break;
case 's':
if (util::strieq_l("statu", name, 5)) {
return LogFragmentType::STATUS;
Expand Down Expand Up @@ -502,6 +512,15 @@ LogFragmentType log_var_lookup_token(const char *name, size_t namelen) {
break;
}
break;
case 16:
switch (name[15]) {
case 'n':
if (util::strieq_l("protocol_versio", name, 15)) {
return LogFragmentType::PROTOCOL_VERSION;
}
break;
}
break;
case 17:
switch (name[16]) {
case 'l':
Expand All @@ -521,6 +540,11 @@ LogFragmentType log_var_lookup_token(const char *name, size_t namelen) {
return LogFragmentType::TLS_SESSION_REUSED;
}
break;
case 'y':
if (util::strieq_l("path_without_quer", name, 17)) {
return LogFragmentType::PATH_WITHOUT_QUERY;
}
break;
}
break;
case 22:
Expand Down
23 changes: 23 additions & 0 deletions src/shrpx_log.cc
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,11 @@ void upstream_accesslog(const std::vector<LogFragment> &lfv,
? StringRef::from_lit("*")
: StringRef::from_lit("-")
: req.path;
auto path_without_query =
req.method == HTTP_CONNECT
? path
: StringRef{std::begin(path),
std::find(std::begin(path), std::end(path), '?')};

auto p = std::begin(buf);
auto last = std::end(buf) - 2;
Expand Down Expand Up @@ -632,6 +637,24 @@ void upstream_accesslog(const std::vector<LogFragment> &lfv,
std::tie(p, last) = copy(req.http_minor, p, last);
}
break;
case LogFragmentType::METHOD:
std::tie(p, last) = copy(method, p, last);
std::tie(p, last) = copy(' ', p, last);
break;
case LogFragmentType::PATH:
std::tie(p, last) = copy_escape(path, p, last);
break;
case LogFragmentType::PATH_WITHOUT_QUERY:
std::tie(p, last) = copy_escape(path_without_query, p, last);
break;
case LogFragmentType::PROTOCOL_VERSION:
std::tie(p, last) = copy_l("HTTP/", p, last);
std::tie(p, last) = copy(req.http_major, p, last);
if (req.http_major < 2) {
std::tie(p, last) = copy('.', p, last);
std::tie(p, last) = copy(req.http_minor, p, last);
}
break;
case LogFragmentType::STATUS:
std::tie(p, last) = copy(resp.http_status, p, last);
break;
Expand Down
4 changes: 4 additions & 0 deletions src/shrpx_log.h
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,10 @@ enum class LogFragmentType {
TLS_CLIENT_SUBJECT_NAME,
BACKEND_HOST,
BACKEND_PORT,
METHOD,
PATH,
PATH_WITHOUT_QUERY,
PROTOCOL_VERSION,
};

struct LogFragment {
Expand Down

0 comments on commit 4e3c61e

Please sign in to comment.