]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: log: Add log-format variable %HQ, to log HTTP query strings
authorAndrew Hayworth <andrew.hayworth@getbraintree.com>
Fri, 31 Jul 2015 16:14:16 +0000 (16:14 +0000)
committerWilly Tarreau <w@1wt.eu>
Sun, 9 Aug 2015 08:16:49 +0000 (10:16 +0200)
Since sample fetches are not always available in the response phase,
this patch implements %HQ such that:

  GET /foo?bar=baz HTTP/1.0

...would be logged as:

  ?bar=baz

doc/configuration.txt
include/types/log.h
src/log.c

index c71f95eebdd041867446f2e4803874675ec9a9e0..f30ab42fd9fac5a3be08beb5ff592adfaff1fb50 100644 (file)
@@ -14072,6 +14072,7 @@ Please refer to the table below for currently defined variables :
   |   | %H   | hostname                                      | string      |
   | H | %HM  | HTTP method (ex: POST)                        | string      |
   | H | %HP  | HTTP request URI without query string (path)  | string      |
+  | H | %HQ  | HTTP request URI query string (ex: ?bar=baz)  | string      |
   | H | %HU  | HTTP request URI (ex: /foo?bar=baz)           | string      |
   | H | %HV  | HTTP version (ex: HTTP/1.0)                   | string      |
   |   | %ID  | unique-id                                     | string      |
index bbfe0209fd04d4ff677776cd8c1444ec913f7a6c..d0fb966828ccc31743e1bac9c2cdeeecfdb22b1c 100644 (file)
@@ -96,6 +96,7 @@ enum {
        LOG_FMT_HTTP_METHOD,
        LOG_FMT_HTTP_URI,
        LOG_FMT_HTTP_PATH,
+       LOG_FMT_HTTP_QUERY,
        LOG_FMT_HTTP_VERSION,
        LOG_FMT_HOSTNAME,
        LOG_FMT_UNIQUEID,
index ffd8f10dae4e7b0580ade5fda2ff1a7c7fd504dd..f80de2e75d0a9f74209f41332cf2481879756e55 100644 (file)
--- a/src/log.c
+++ b/src/log.c
@@ -111,6 +111,7 @@ static const struct logformat_type logformat_keywords[] = {
        { "hsl", LOG_FMT_HDRRESPONSLIST, PR_MODE_TCP, LW_RSPHDR, NULL },  /* header response list */
        { "HM", LOG_FMT_HTTP_METHOD, PR_MODE_HTTP, LW_REQ, NULL },  /* HTTP method */
        { "HP", LOG_FMT_HTTP_PATH, PR_MODE_HTTP, LW_REQ, NULL },  /* HTTP path */
+       { "HQ", LOG_FMT_HTTP_QUERY, PR_MODE_HTTP, LW_REQ, NULL },  /* HTTP query */
        { "HU", LOG_FMT_HTTP_URI, PR_MODE_HTTP, LW_REQ, NULL },  /* HTTP full URI */
        { "HV", LOG_FMT_HTTP_VERSION, PR_MODE_HTTP, LW_REQ, NULL },  /* HTTP version */
        { "lc", LOG_FMT_LOGCNT, PR_MODE_TCP, LW_INIT, NULL }, /* log counter */
@@ -937,6 +938,7 @@ int build_logline(struct stream *s, char *dst, size_t maxsize, struct list *list
        struct chunk chunk;
        char *uri;
        char *spc;
+       char *qmark;
        char *end;
        struct tm tm;
        int t_request;
@@ -1578,6 +1580,40 @@ int build_logline(struct stream *s, char *dst, size_t maxsize, struct list *list
                                last_isspace = 0;
                                break;
 
+                       case LOG_FMT_HTTP_QUERY: // %HQ
+                               if (tmp->options & LOG_OPT_QUOTE)
+                                       LOGCHAR('"');
+
+                               if (!txn->uri) {
+                                       chunk.str = "<BADREQ>";
+                                       chunk.len = strlen("<BADREQ>");
+                               } else {
+                                       uri = txn->uri;
+                                       end = uri + strlen(uri);
+                                       // look for the first question mark
+                                       while (uri < end && *uri != '?')
+                                               uri++;
+
+                                       qmark = uri;
+                                       // look for first space or question mark after url
+                                       while (uri < end && !HTTP_IS_SPHT(*uri))
+                                               uri++;
+
+                                       chunk.str = qmark;
+                                       chunk.len = uri - qmark;
+                               }
+
+                               ret = encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk);
+                               if (ret == NULL || *ret != '\0')
+                                       goto out;
+
+                               tmplog = ret;
+                               if (tmp->options & LOG_OPT_QUOTE)
+                                       LOGCHAR('"');
+
+                               last_isspace = 0;
+                               break;
+
                        case LOG_FMT_HTTP_URI: // %HU
                                uri = txn->uri ? txn->uri : "<BADREQ>";