Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * HTTP protocol analyzer |
| 3 | * |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 4 | * Copyright 2000-2011 Willy Tarreau <w@1wt.eu> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
| 13 | #include <ctype.h> |
| 14 | #include <errno.h> |
| 15 | #include <fcntl.h> |
| 16 | #include <stdio.h> |
| 17 | #include <stdlib.h> |
| 18 | #include <string.h> |
| 19 | #include <syslog.h> |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 20 | #include <time.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 21 | |
| 22 | #include <sys/socket.h> |
| 23 | #include <sys/stat.h> |
| 24 | #include <sys/types.h> |
| 25 | |
Willy Tarreau | b05405a | 2012-01-23 15:35:52 +0100 | [diff] [blame] | 26 | #include <netinet/tcp.h> |
| 27 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 28 | #include <common/base64.h> |
Frédéric Lécaille | a41d531 | 2018-01-29 12:05:07 +0100 | [diff] [blame] | 29 | #include <common/cfgparse.h> |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 30 | #include <common/chunk.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 31 | #include <common/compat.h> |
| 32 | #include <common/config.h> |
Willy Tarreau | a4cd1f5 | 2006-12-16 19:57:26 +0100 | [diff] [blame] | 33 | #include <common/debug.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 34 | #include <common/memory.h> |
| 35 | #include <common/mini-clist.h> |
| 36 | #include <common/standard.h> |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 37 | #include <common/ticks.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 38 | #include <common/time.h> |
| 39 | #include <common/uri_auth.h> |
| 40 | #include <common/version.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 41 | |
| 42 | #include <types/capture.h> |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 43 | #include <types/cli.h> |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 44 | #include <types/filters.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 45 | #include <types/global.h> |
William Lallemand | 71bd11a | 2017-11-20 19:13:14 +0100 | [diff] [blame] | 46 | #include <types/cache.h> |
William Lallemand | 9ed6203 | 2016-11-21 17:49:11 +0100 | [diff] [blame] | 47 | #include <types/stats.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 48 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 49 | #include <proto/acl.h> |
Thierry FOURNIER | 322a124 | 2015-08-19 09:07:47 +0200 | [diff] [blame] | 50 | #include <proto/action.h> |
Willy Tarreau | 61612d4 | 2012-04-19 18:42:05 +0200 | [diff] [blame] | 51 | #include <proto/arg.h> |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 52 | #include <proto/auth.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 53 | #include <proto/backend.h> |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 54 | #include <proto/channel.h> |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 55 | #include <proto/checks.h> |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 56 | #include <proto/cli.h> |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 57 | #include <proto/compression.h> |
William Lallemand | 9ed6203 | 2016-11-21 17:49:11 +0100 | [diff] [blame] | 58 | #include <proto/stats.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 59 | #include <proto/fd.h> |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 60 | #include <proto/filters.h> |
Willy Tarreau | 03fa5df | 2010-05-24 21:02:37 +0200 | [diff] [blame] | 61 | #include <proto/frontend.h> |
Willy Tarreau | 0da5b3b | 2017-09-21 09:30:46 +0200 | [diff] [blame] | 62 | #include <proto/h1.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 63 | #include <proto/log.h> |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 64 | #include <proto/hdr_idx.h> |
Patrick Hemmer | e3faf02 | 2018-08-22 10:02:00 -0400 | [diff] [blame] | 65 | #include <proto/hlua.h> |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 66 | #include <proto/pattern.h> |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 67 | #include <proto/proto_tcp.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 68 | #include <proto/proto_http.h> |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 69 | #include <proto/proxy.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 70 | #include <proto/queue.h> |
Willy Tarreau | cd3b094 | 2012-04-27 21:52:18 +0200 | [diff] [blame] | 71 | #include <proto/sample.h> |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 72 | #include <proto/server.h> |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 73 | #include <proto/stream.h> |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 74 | #include <proto/stream_interface.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 75 | #include <proto/task.h> |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 76 | #include <proto/pattern.h> |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 77 | #include <proto/vars.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 78 | |
Cyril Bonté | 19979e1 | 2012-04-04 12:57:21 +0200 | [diff] [blame] | 79 | /* status codes available for the stats admin page (strictly 4 chars length) */ |
| 80 | const char *stat_status_codes[STAT_STATUS_SIZE] = { |
| 81 | [STAT_STATUS_DENY] = "DENY", |
| 82 | [STAT_STATUS_DONE] = "DONE", |
| 83 | [STAT_STATUS_ERRP] = "ERRP", |
| 84 | [STAT_STATUS_EXCD] = "EXCD", |
| 85 | [STAT_STATUS_NONE] = "NONE", |
| 86 | [STAT_STATUS_PART] = "PART", |
| 87 | [STAT_STATUS_UNKN] = "UNKN", |
| 88 | }; |
| 89 | |
| 90 | |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 91 | /* List head of all known action keywords for "http-request" */ |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 92 | struct action_kw_list http_req_keywords = { |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 93 | .list = LIST_HEAD_INIT(http_req_keywords.list) |
| 94 | }; |
| 95 | |
| 96 | /* List head of all known action keywords for "http-response" */ |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 97 | struct action_kw_list http_res_keywords = { |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 98 | .list = LIST_HEAD_INIT(http_res_keywords.list) |
| 99 | }; |
| 100 | |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 101 | /* this struct is used between calls to smp_fetch_hdr() or smp_fetch_cookie() */ |
Christopher Faulet | 1bc04c7 | 2017-10-29 20:14:08 +0100 | [diff] [blame] | 102 | static THREAD_LOCAL struct hdr_ctx static_hdr_ctx; |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 103 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 104 | #define FD_SETS_ARE_BITFIELDS |
| 105 | #ifdef FD_SETS_ARE_BITFIELDS |
| 106 | /* |
| 107 | * This map is used with all the FD_* macros to check whether a particular bit |
| 108 | * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes |
| 109 | * which should be encoded. When FD_ISSET() returns non-zero, it means that the |
| 110 | * byte should be encoded. Be careful to always pass bytes from 0 to 255 |
| 111 | * exclusively to the macros. |
| 112 | */ |
| 113 | fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| 114 | fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 115 | fd_set http_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 116 | |
| 117 | #else |
| 118 | #error "Check if your OS uses bitfields for fd_sets" |
| 119 | #endif |
| 120 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 121 | static int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn); |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 122 | |
David Carlier | 7365f7d | 2016-04-04 11:54:42 +0100 | [diff] [blame] | 123 | static inline int http_msg_forward_body(struct stream *s, struct http_msg *msg); |
| 124 | static inline int http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 125 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 126 | void init_proto_http() |
| 127 | { |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 128 | int i; |
| 129 | char *tmp; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 130 | |
| 131 | /* initialize the log header encoding map : '{|}"#' should be encoded with |
| 132 | * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ). |
| 133 | * URL encoding only requires '"', '#' to be encoded as well as non- |
| 134 | * printable characters above. |
| 135 | */ |
| 136 | memset(hdr_encode_map, 0, sizeof(hdr_encode_map)); |
| 137 | memset(url_encode_map, 0, sizeof(url_encode_map)); |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 138 | memset(http_encode_map, 0, sizeof(url_encode_map)); |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 139 | for (i = 0; i < 32; i++) { |
| 140 | FD_SET(i, hdr_encode_map); |
| 141 | FD_SET(i, url_encode_map); |
| 142 | } |
| 143 | for (i = 127; i < 256; i++) { |
| 144 | FD_SET(i, hdr_encode_map); |
| 145 | FD_SET(i, url_encode_map); |
| 146 | } |
| 147 | |
| 148 | tmp = "\"#{|}"; |
| 149 | while (*tmp) { |
| 150 | FD_SET(*tmp, hdr_encode_map); |
| 151 | tmp++; |
| 152 | } |
| 153 | |
| 154 | tmp = "\"#"; |
| 155 | while (*tmp) { |
| 156 | FD_SET(*tmp, url_encode_map); |
| 157 | tmp++; |
| 158 | } |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 159 | |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 160 | /* initialize the http header encoding map. The draft httpbis define the |
| 161 | * header content as: |
| 162 | * |
| 163 | * HTTP-message = start-line |
| 164 | * *( header-field CRLF ) |
| 165 | * CRLF |
| 166 | * [ message-body ] |
| 167 | * header-field = field-name ":" OWS field-value OWS |
| 168 | * field-value = *( field-content / obs-fold ) |
| 169 | * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ] |
| 170 | * obs-fold = CRLF 1*( SP / HTAB ) |
| 171 | * field-vchar = VCHAR / obs-text |
| 172 | * VCHAR = %x21-7E |
| 173 | * obs-text = %x80-FF |
| 174 | * |
| 175 | * All the chars are encoded except "VCHAR", "obs-text", SP and HTAB. |
| 176 | * The encoded chars are form 0x00 to 0x08, 0x0a to 0x1f and 0x7f. The |
| 177 | * "obs-fold" is volontary forgotten because haproxy remove this. |
| 178 | */ |
| 179 | memset(http_encode_map, 0, sizeof(http_encode_map)); |
| 180 | for (i = 0x00; i <= 0x08; i++) |
| 181 | FD_SET(i, http_encode_map); |
| 182 | for (i = 0x0a; i <= 0x1f; i++) |
| 183 | FD_SET(i, http_encode_map); |
| 184 | FD_SET(0x7f, http_encode_map); |
| 185 | |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 186 | /* memory allocations */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 187 | pool_head_http_txn = create_pool("http_txn", sizeof(struct http_txn), MEM_F_SHARED); |
| 188 | pool_head_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED); |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 189 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 190 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 191 | /* |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 192 | * Adds a header and its CRLF at the tail of the message's buffer, just before |
Willy Tarreau | 4d893d4 | 2018-07-12 15:43:32 +0200 | [diff] [blame] | 193 | * the last CRLF. |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 194 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 195 | * of headers is automatically adjusted. The number of bytes added is returned |
| 196 | * on success, otherwise <0 is returned indicating an error. |
| 197 | */ |
Willy Tarreau | 4d893d4 | 2018-07-12 15:43:32 +0200 | [diff] [blame] | 198 | static inline int http_header_add_tail(struct http_msg *msg, struct hdr_idx *hdr_idx, const char *text) |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 199 | { |
Willy Tarreau | 4d893d4 | 2018-07-12 15:43:32 +0200 | [diff] [blame] | 200 | return http_header_add_tail2(msg, hdr_idx, text, strlen(text)); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | /* |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 204 | * Adds a header and its CRLF at the tail of the message's buffer, just before |
Willy Tarreau | 4d893d4 | 2018-07-12 15:43:32 +0200 | [diff] [blame] | 205 | * the last CRLF. <len> bytes are copied, not counting the CRLF. |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 206 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 207 | * of headers is automatically adjusted. The number of bytes added is returned |
| 208 | * on success, otherwise <0 is returned indicating an error. |
| 209 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 210 | int http_header_add_tail2(struct http_msg *msg, |
| 211 | struct hdr_idx *hdr_idx, const char *text, int len) |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 212 | { |
| 213 | int bytes; |
| 214 | |
Willy Tarreau | 4d893d4 | 2018-07-12 15:43:32 +0200 | [diff] [blame] | 215 | bytes = ci_insert_line2(msg->chn, msg->eoh, text, len); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 216 | if (!bytes) |
| 217 | return -1; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 218 | http_msg_move_end(msg, bytes); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 219 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 220 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 221 | |
| 222 | /* |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 223 | * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon. |
| 224 | * If so, returns the position of the first non-space character relative to |
| 225 | * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries |
| 226 | * to return a pointer to the place after the first space. Returns 0 if the |
| 227 | * header name does not match. Checks are case-insensitive. |
| 228 | */ |
| 229 | int http_header_match2(const char *hdr, const char *end, |
| 230 | const char *name, int len) |
| 231 | { |
| 232 | const char *val; |
| 233 | |
| 234 | if (hdr + len >= end) |
| 235 | return 0; |
| 236 | if (hdr[len] != ':') |
| 237 | return 0; |
| 238 | if (strncasecmp(hdr, name, len) != 0) |
| 239 | return 0; |
| 240 | val = hdr + len + 1; |
| 241 | while (val < end && HTTP_IS_SPHT(*val)) |
| 242 | val++; |
| 243 | if ((val >= end) && (len + 2 <= end - hdr)) |
| 244 | return len + 2; /* we may replace starting from second space */ |
| 245 | return val - hdr; |
| 246 | } |
| 247 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 248 | /* Find the first or next occurrence of header <name> in message buffer <sol> |
| 249 | * using headers index <idx>, and return it in the <ctx> structure. This |
| 250 | * structure holds everything necessary to use the header and find next |
| 251 | * occurrence. If its <idx> member is 0, the header is searched from the |
| 252 | * beginning. Otherwise, the next occurrence is returned. The function returns |
| 253 | * 1 when it finds a value, and 0 when there is no more. It is very similar to |
| 254 | * http_find_header2() except that it is designed to work with full-line headers |
| 255 | * whose comma is not a delimiter but is part of the syntax. As a special case, |
| 256 | * if ctx->val is NULL when searching for a new values of a header, the current |
| 257 | * header is rescanned. This allows rescanning after a header deletion. |
| 258 | */ |
| 259 | int http_find_full_header2(const char *name, int len, |
| 260 | char *sol, struct hdr_idx *idx, |
| 261 | struct hdr_ctx *ctx) |
| 262 | { |
| 263 | char *eol, *sov; |
| 264 | int cur_idx, old_idx; |
| 265 | |
| 266 | cur_idx = ctx->idx; |
| 267 | if (cur_idx) { |
| 268 | /* We have previously returned a header, let's search another one */ |
| 269 | sol = ctx->line; |
| 270 | eol = sol + idx->v[cur_idx].len; |
| 271 | goto next_hdr; |
| 272 | } |
| 273 | |
| 274 | /* first request for this header */ |
| 275 | sol += hdr_idx_first_pos(idx); |
| 276 | old_idx = 0; |
| 277 | cur_idx = hdr_idx_first_idx(idx); |
| 278 | while (cur_idx) { |
| 279 | eol = sol + idx->v[cur_idx].len; |
| 280 | |
| 281 | if (len == 0) { |
| 282 | /* No argument was passed, we want any header. |
| 283 | * To achieve this, we simply build a fake request. */ |
| 284 | while (sol + len < eol && sol[len] != ':') |
| 285 | len++; |
| 286 | name = sol; |
| 287 | } |
| 288 | |
| 289 | if ((len < eol - sol) && |
| 290 | (sol[len] == ':') && |
| 291 | (strncasecmp(sol, name, len) == 0)) { |
| 292 | ctx->del = len; |
| 293 | sov = sol + len + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 294 | while (sov < eol && HTTP_IS_LWS(*sov)) |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 295 | sov++; |
| 296 | |
| 297 | ctx->line = sol; |
| 298 | ctx->prev = old_idx; |
| 299 | ctx->idx = cur_idx; |
| 300 | ctx->val = sov - sol; |
| 301 | ctx->tws = 0; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 302 | while (eol > sov && HTTP_IS_LWS(*(eol - 1))) { |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 303 | eol--; |
| 304 | ctx->tws++; |
| 305 | } |
| 306 | ctx->vlen = eol - sov; |
| 307 | return 1; |
| 308 | } |
| 309 | next_hdr: |
| 310 | sol = eol + idx->v[cur_idx].cr + 1; |
| 311 | old_idx = cur_idx; |
| 312 | cur_idx = idx->v[cur_idx].next; |
| 313 | } |
| 314 | return 0; |
| 315 | } |
| 316 | |
Willy Tarreau | c90dc23 | 2015-02-20 13:51:36 +0100 | [diff] [blame] | 317 | /* Find the first or next header field in message buffer <sol> using headers |
| 318 | * index <idx>, and return it in the <ctx> structure. This structure holds |
| 319 | * everything necessary to use the header and find next occurrence. If its |
| 320 | * <idx> member is 0, the first header is retrieved. Otherwise, the next |
| 321 | * occurrence is returned. The function returns 1 when it finds a value, and |
| 322 | * 0 when there is no more. It is equivalent to http_find_full_header2() with |
| 323 | * no header name. |
| 324 | */ |
| 325 | int http_find_next_header(char *sol, struct hdr_idx *idx, struct hdr_ctx *ctx) |
| 326 | { |
| 327 | char *eol, *sov; |
| 328 | int cur_idx, old_idx; |
| 329 | int len; |
| 330 | |
| 331 | cur_idx = ctx->idx; |
| 332 | if (cur_idx) { |
| 333 | /* We have previously returned a header, let's search another one */ |
| 334 | sol = ctx->line; |
| 335 | eol = sol + idx->v[cur_idx].len; |
| 336 | goto next_hdr; |
| 337 | } |
| 338 | |
| 339 | /* first request for this header */ |
| 340 | sol += hdr_idx_first_pos(idx); |
| 341 | old_idx = 0; |
| 342 | cur_idx = hdr_idx_first_idx(idx); |
| 343 | while (cur_idx) { |
| 344 | eol = sol + idx->v[cur_idx].len; |
| 345 | |
| 346 | len = 0; |
| 347 | while (1) { |
| 348 | if (len >= eol - sol) |
| 349 | goto next_hdr; |
| 350 | if (sol[len] == ':') |
| 351 | break; |
| 352 | len++; |
| 353 | } |
| 354 | |
| 355 | ctx->del = len; |
| 356 | sov = sol + len + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 357 | while (sov < eol && HTTP_IS_LWS(*sov)) |
Willy Tarreau | c90dc23 | 2015-02-20 13:51:36 +0100 | [diff] [blame] | 358 | sov++; |
| 359 | |
| 360 | ctx->line = sol; |
| 361 | ctx->prev = old_idx; |
| 362 | ctx->idx = cur_idx; |
| 363 | ctx->val = sov - sol; |
| 364 | ctx->tws = 0; |
| 365 | |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 366 | while (eol > sov && HTTP_IS_LWS(*(eol - 1))) { |
Willy Tarreau | c90dc23 | 2015-02-20 13:51:36 +0100 | [diff] [blame] | 367 | eol--; |
| 368 | ctx->tws++; |
| 369 | } |
| 370 | ctx->vlen = eol - sov; |
| 371 | return 1; |
| 372 | |
| 373 | next_hdr: |
| 374 | sol = eol + idx->v[cur_idx].cr + 1; |
| 375 | old_idx = cur_idx; |
| 376 | cur_idx = idx->v[cur_idx].next; |
| 377 | } |
| 378 | return 0; |
| 379 | } |
| 380 | |
Lukas Tribus | 2395368 | 2017-04-28 13:24:30 +0000 | [diff] [blame] | 381 | /* Find the end of the header value contained between <s> and <e>. See RFC7230, |
| 382 | * par 3.2 for more information. Note that it requires a valid header to return |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 383 | * a valid result. This works for headers defined as comma-separated lists. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 384 | */ |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 385 | char *find_hdr_value_end(char *s, const char *e) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 386 | { |
| 387 | int quoted, qdpair; |
| 388 | |
| 389 | quoted = qdpair = 0; |
Willy Tarreau | e6d9c21 | 2016-11-05 18:23:38 +0100 | [diff] [blame] | 390 | |
| 391 | #if defined(__x86_64__) || \ |
| 392 | defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) || \ |
| 393 | defined(__ARM_ARCH_7A__) |
| 394 | /* speedup: skip everything not a comma nor a double quote */ |
| 395 | for (; s <= e - sizeof(int); s += sizeof(int)) { |
| 396 | unsigned int c = *(int *)s; // comma |
| 397 | unsigned int q = c; // quote |
| 398 | |
| 399 | c ^= 0x2c2c2c2c; // contains one zero on a comma |
| 400 | q ^= 0x22222222; // contains one zero on a quote |
| 401 | |
| 402 | c = (c - 0x01010101) & ~c; // contains 0x80 below a comma |
| 403 | q = (q - 0x01010101) & ~q; // contains 0x80 below a quote |
| 404 | |
| 405 | if ((c | q) & 0x80808080) |
| 406 | break; // found a comma or a quote |
| 407 | } |
| 408 | #endif |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 409 | for (; s < e; s++) { |
| 410 | if (qdpair) qdpair = 0; |
Willy Tarreau | 0f7f51f | 2010-08-30 11:06:34 +0200 | [diff] [blame] | 411 | else if (quoted) { |
| 412 | if (*s == '\\') qdpair = 1; |
| 413 | else if (*s == '"') quoted = 0; |
| 414 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 415 | else if (*s == '"') quoted = 1; |
| 416 | else if (*s == ',') return s; |
| 417 | } |
| 418 | return s; |
| 419 | } |
| 420 | |
| 421 | /* Find the first or next occurrence of header <name> in message buffer <sol> |
| 422 | * using headers index <idx>, and return it in the <ctx> structure. This |
| 423 | * structure holds everything necessary to use the header and find next |
| 424 | * occurrence. If its <idx> member is 0, the header is searched from the |
| 425 | * beginning. Otherwise, the next occurrence is returned. The function returns |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 426 | * 1 when it finds a value, and 0 when there is no more. It is designed to work |
| 427 | * with headers defined as comma-separated lists. As a special case, if ctx->val |
| 428 | * is NULL when searching for a new values of a header, the current header is |
| 429 | * rescanned. This allows rescanning after a header deletion. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 430 | */ |
| 431 | int http_find_header2(const char *name, int len, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 432 | char *sol, struct hdr_idx *idx, |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 433 | struct hdr_ctx *ctx) |
| 434 | { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 435 | char *eol, *sov; |
| 436 | int cur_idx, old_idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 437 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 438 | cur_idx = ctx->idx; |
| 439 | if (cur_idx) { |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 440 | /* We have previously returned a value, let's search |
| 441 | * another one on the same line. |
| 442 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 443 | sol = ctx->line; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 444 | ctx->del = ctx->val + ctx->vlen + ctx->tws; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 445 | sov = sol + ctx->del; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 446 | eol = sol + idx->v[cur_idx].len; |
| 447 | |
| 448 | if (sov >= eol) |
| 449 | /* no more values in this header */ |
| 450 | goto next_hdr; |
| 451 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 452 | /* values remaining for this header, skip the comma but save it |
| 453 | * for later use (eg: for header deletion). |
| 454 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 455 | sov++; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 456 | while (sov < eol && HTTP_IS_LWS((*sov))) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 457 | sov++; |
| 458 | |
| 459 | goto return_hdr; |
| 460 | } |
| 461 | |
| 462 | /* first request for this header */ |
| 463 | sol += hdr_idx_first_pos(idx); |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 464 | old_idx = 0; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 465 | cur_idx = hdr_idx_first_idx(idx); |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 466 | while (cur_idx) { |
| 467 | eol = sol + idx->v[cur_idx].len; |
| 468 | |
Willy Tarreau | 1ad7c6d | 2007-06-10 21:42:55 +0200 | [diff] [blame] | 469 | if (len == 0) { |
| 470 | /* No argument was passed, we want any header. |
| 471 | * To achieve this, we simply build a fake request. */ |
| 472 | while (sol + len < eol && sol[len] != ':') |
| 473 | len++; |
| 474 | name = sol; |
| 475 | } |
| 476 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 477 | if ((len < eol - sol) && |
| 478 | (sol[len] == ':') && |
| 479 | (strncasecmp(sol, name, len) == 0)) { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 480 | ctx->del = len; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 481 | sov = sol + len + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 482 | while (sov < eol && HTTP_IS_LWS(*sov)) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 483 | sov++; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 484 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 485 | ctx->line = sol; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 486 | ctx->prev = old_idx; |
| 487 | return_hdr: |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 488 | ctx->idx = cur_idx; |
| 489 | ctx->val = sov - sol; |
| 490 | |
| 491 | eol = find_hdr_value_end(sov, eol); |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 492 | ctx->tws = 0; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 493 | while (eol > sov && HTTP_IS_LWS(*(eol - 1))) { |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 494 | eol--; |
| 495 | ctx->tws++; |
| 496 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 497 | ctx->vlen = eol - sov; |
| 498 | return 1; |
| 499 | } |
| 500 | next_hdr: |
| 501 | sol = eol + idx->v[cur_idx].cr + 1; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 502 | old_idx = cur_idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 503 | cur_idx = idx->v[cur_idx].next; |
| 504 | } |
| 505 | return 0; |
| 506 | } |
| 507 | |
| 508 | int http_find_header(const char *name, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 509 | char *sol, struct hdr_idx *idx, |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 510 | struct hdr_ctx *ctx) |
| 511 | { |
| 512 | return http_find_header2(name, strlen(name), sol, idx, ctx); |
| 513 | } |
| 514 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 515 | /* Remove one value of a header. This only works on a <ctx> returned by one of |
| 516 | * the http_find_header functions. The value is removed, as well as surrounding |
| 517 | * commas if any. If the removed value was alone, the whole header is removed. |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 518 | * The ctx is always updated accordingly, as well as the buffer and HTTP |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 519 | * message <msg>. The new index is returned. If it is zero, it means there is |
| 520 | * no more header, so any processing may stop. The ctx is always left in a form |
| 521 | * that can be handled by http_find_header2() to find next occurrence. |
| 522 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 523 | int http_remove_header2(struct http_msg *msg, struct hdr_idx *idx, struct hdr_ctx *ctx) |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 524 | { |
| 525 | int cur_idx = ctx->idx; |
| 526 | char *sol = ctx->line; |
| 527 | struct hdr_idx_elem *hdr; |
| 528 | int delta, skip_comma; |
| 529 | |
| 530 | if (!cur_idx) |
| 531 | return 0; |
| 532 | |
| 533 | hdr = &idx->v[cur_idx]; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 534 | if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 535 | /* This was the only value of the header, we must now remove it entirely. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 536 | delta = b_rep_blk(&msg->chn->buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0); |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 537 | http_msg_move_end(msg, delta); |
| 538 | idx->used--; |
| 539 | hdr->len = 0; /* unused entry */ |
| 540 | idx->v[ctx->prev].next = idx->v[ctx->idx].next; |
Willy Tarreau | 5c4784f | 2011-02-12 13:07:35 +0100 | [diff] [blame] | 541 | if (idx->tail == ctx->idx) |
| 542 | idx->tail = ctx->prev; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 543 | ctx->idx = ctx->prev; /* walk back to the end of previous header */ |
Willy Tarreau | 7c1c217 | 2015-01-07 17:23:50 +0100 | [diff] [blame] | 544 | ctx->line -= idx->v[ctx->idx].len + idx->v[ctx->idx].cr + 1; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 545 | ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */ |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 546 | ctx->tws = ctx->vlen = 0; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 547 | return ctx->idx; |
| 548 | } |
| 549 | |
| 550 | /* This was not the only value of this header. We have to remove between |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 551 | * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the |
| 552 | * last entry of the list, we remove the last separator. |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 553 | */ |
| 554 | |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 555 | skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1; |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 556 | delta = b_rep_blk(&msg->chn->buf, sol + ctx->del + skip_comma, |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 557 | sol + ctx->val + ctx->vlen + ctx->tws + skip_comma, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 558 | NULL, 0); |
| 559 | hdr->len += delta; |
| 560 | http_msg_move_end(msg, delta); |
| 561 | ctx->val = ctx->del; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 562 | ctx->tws = ctx->vlen = 0; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 563 | return ctx->idx; |
| 564 | } |
| 565 | |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 566 | /* This function handles a server error at the stream interface level. The |
| 567 | * stream interface is assumed to be already in a closed state. An optional |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 568 | * message is copied into the input buffer. |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 569 | * The error flags are set to the values in arguments. Any pending request |
Willy Tarreau | 6f0aa47 | 2009-03-08 20:33:29 +0100 | [diff] [blame] | 570 | * in this buffer will be lost. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 571 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 572 | static void http_server_error(struct stream *s, struct stream_interface *si, |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 573 | int err, int finst, const struct buffer *msg) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 574 | { |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 575 | FLT_STRM_CB(s, flt_http_reply(s, s->txn->status, msg)); |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 576 | channel_auto_read(si_oc(si)); |
| 577 | channel_abort(si_oc(si)); |
| 578 | channel_auto_close(si_oc(si)); |
| 579 | channel_erase(si_oc(si)); |
| 580 | channel_auto_close(si_ic(si)); |
| 581 | channel_auto_read(si_ic(si)); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 582 | if (msg) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 583 | co_inject(si_ic(si), msg->area, msg->data); |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 584 | if (!(s->flags & SF_ERR_MASK)) |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 585 | s->flags |= err; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 586 | if (!(s->flags & SF_FINST_MASK)) |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 587 | s->flags |= finst; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 588 | } |
| 589 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 590 | /* This function returns the appropriate error location for the given stream |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 591 | * and message. |
| 592 | */ |
| 593 | |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 594 | struct buffer *http_error_message(struct stream *s) |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 595 | { |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 596 | const int msgnum = http_get_status_idx(s->txn->status); |
| 597 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 598 | if (s->be->errmsg[msgnum].area) |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 599 | return &s->be->errmsg[msgnum]; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 600 | else if (strm_fe(s)->errmsg[msgnum].area) |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 601 | return &strm_fe(s)->errmsg[msgnum]; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 602 | else |
| 603 | return &http_err_chunks[msgnum]; |
| 604 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 605 | |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 606 | void |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 607 | http_reply_and_close(struct stream *s, short status, struct buffer *msg) |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 608 | { |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 609 | s->txn->flags &= ~TX_WAIT_NEXT_RQ; |
Christopher Faulet | 3e34429 | 2015-11-24 16:24:13 +0100 | [diff] [blame] | 610 | FLT_STRM_CB(s, flt_http_reply(s, status, msg)); |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 611 | stream_int_retnclose(&s->si[0], msg); |
| 612 | } |
| 613 | |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 614 | /* Parse the URI from the given transaction (which is assumed to be in request |
| 615 | * phase) and look for the "/" beginning the PATH. If not found, return NULL. |
| 616 | * It is returned otherwise. |
| 617 | */ |
Willy Tarreau | 6b952c8 | 2018-09-10 17:45:34 +0200 | [diff] [blame] | 618 | char *http_txn_get_path(const struct http_txn *txn) |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 619 | { |
Willy Tarreau | 6b952c8 | 2018-09-10 17:45:34 +0200 | [diff] [blame] | 620 | struct ist ret; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 621 | |
Willy Tarreau | 6b952c8 | 2018-09-10 17:45:34 +0200 | [diff] [blame] | 622 | ret = http_get_path(ist2(ci_head(txn->req.chn) + txn->req.sl.rq.u, txn->req.sl.rq.u_l)); |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 623 | |
Willy Tarreau | 6b952c8 | 2018-09-10 17:45:34 +0200 | [diff] [blame] | 624 | return ret.ptr; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 625 | } |
| 626 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 627 | /* Returns a 302 for a redirectable request that reaches a server working in |
| 628 | * in redirect mode. This may only be called just after the stream interface |
| 629 | * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will |
| 630 | * follow normal proxy processing. NOTE: this function is designed to support |
| 631 | * being called once data are scheduled for forwarding. |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 632 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 633 | void http_perform_server_redirect(struct stream *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 634 | { |
| 635 | struct http_txn *txn; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 636 | struct server *srv; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 637 | char *path; |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 638 | int len, rewind; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 639 | |
| 640 | /* 1: create the response header */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 641 | trash.data = strlen(HTTP_302); |
| 642 | memcpy(trash.area, HTTP_302, trash.data); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 643 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 644 | srv = objt_server(s->target); |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 645 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 646 | /* 2: add the server's prefix */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 647 | if (trash.data + srv->rdr_len > trash.size) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 648 | return; |
| 649 | |
Willy Tarreau | dcb75c4 | 2010-01-10 00:24:22 +0100 | [diff] [blame] | 650 | /* special prefix "/" means don't change URL */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 651 | if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 652 | memcpy(trash.area + trash.data, srv->rdr_pfx, srv->rdr_len); |
| 653 | trash.data += srv->rdr_len; |
Willy Tarreau | dcb75c4 | 2010-01-10 00:24:22 +0100 | [diff] [blame] | 654 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 655 | |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 656 | /* 3: add the request URI. Since it was already forwarded, we need |
| 657 | * to temporarily rewind the buffer. |
| 658 | */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 659 | txn = s->txn; |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 660 | c_rew(&s->req, rewind = http_hdr_rewind(&txn->req)); |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 661 | |
Willy Tarreau | 6b952c8 | 2018-09-10 17:45:34 +0200 | [diff] [blame] | 662 | path = http_txn_get_path(txn); |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 663 | len = b_dist(&s->req.buf, path, c_ptr(&s->req, txn->req.sl.rq.u + txn->req.sl.rq.u_l)); |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 664 | |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 665 | c_adv(&s->req, rewind); |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 666 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 667 | if (!path) |
| 668 | return; |
| 669 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 670 | if (trash.data + len > trash.size - 4) /* 4 for CRLF-CRLF */ |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 671 | return; |
| 672 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 673 | memcpy(trash.area + trash.data, path, len); |
| 674 | trash.data += len; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 675 | |
| 676 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 677 | memcpy(trash.area + trash.data, |
| 678 | "\r\nProxy-Connection: close\r\n\r\n", 29); |
| 679 | trash.data += 29; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 680 | } else { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 681 | memcpy(trash.area + trash.data, |
| 682 | "\r\nConnection: close\r\n\r\n", 23); |
| 683 | trash.data += 23; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 684 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 685 | |
| 686 | /* prepare to return without error. */ |
Willy Tarreau | 73b013b | 2012-05-21 16:31:45 +0200 | [diff] [blame] | 687 | si_shutr(si); |
| 688 | si_shutw(si); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 689 | si->err_type = SI_ET_NONE; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 690 | si->state = SI_ST_CLO; |
| 691 | |
| 692 | /* send the message */ |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 693 | txn->status = 302; |
| 694 | http_server_error(s, si, SF_ERR_LOCAL, SF_FINST_C, &trash); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 695 | |
| 696 | /* FIXME: we should increase a counter of redirects per server and per backend. */ |
Willy Tarreau | 4521ba6 | 2013-01-24 01:25:25 +0100 | [diff] [blame] | 697 | srv_inc_sess_ctr(srv); |
Bhaskar Maddala | a20cb85 | 2014-02-03 16:26:46 -0500 | [diff] [blame] | 698 | srv_set_sess_last(srv); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 699 | } |
| 700 | |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 701 | /* Return the error message corresponding to si->err_type. It is assumed |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 702 | * that the server side is closed. Note that err_type is actually a |
| 703 | * bitmask, where almost only aborts may be cumulated with other |
| 704 | * values. We consider that aborted operations are more important |
| 705 | * than timeouts or errors due to the fact that nobody else in the |
| 706 | * logs might explain incomplete retries. All others should avoid |
| 707 | * being cumulated. It should normally not be possible to have multiple |
| 708 | * aborts at once, but just in case, the first one in sequence is reported. |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 709 | * Note that connection errors appearing on the second request of a keep-alive |
| 710 | * connection are not reported since this allows the client to retry. |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 711 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 712 | void http_return_srv_error(struct stream *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 713 | { |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 714 | int err_type = si->err_type; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 715 | |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 716 | /* set s->txn->status for http_error_message(s) */ |
| 717 | s->txn->status = 503; |
| 718 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 719 | if (err_type & SI_ET_QUEUE_ABRT) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 720 | http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q, |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 721 | http_error_message(s)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 722 | else if (err_type & SI_ET_CONN_ABRT) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 723 | http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C, |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 724 | (s->txn->flags & TX_NOT_FIRST) ? NULL : |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 725 | http_error_message(s)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 726 | else if (err_type & SI_ET_QUEUE_TO) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 727 | http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q, |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 728 | http_error_message(s)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 729 | else if (err_type & SI_ET_QUEUE_ERR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 730 | http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q, |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 731 | http_error_message(s)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 732 | else if (err_type & SI_ET_CONN_TO) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 733 | http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C, |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 734 | (s->txn->flags & TX_NOT_FIRST) ? NULL : |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 735 | http_error_message(s)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 736 | else if (err_type & SI_ET_CONN_ERR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 737 | http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C, |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 738 | (s->flags & SF_SRV_REUSED) ? NULL : |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 739 | http_error_message(s)); |
Willy Tarreau | 2d400bb | 2012-05-14 12:11:47 +0200 | [diff] [blame] | 740 | else if (err_type & SI_ET_CONN_RES) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 741 | http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C, |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 742 | (s->txn->flags & TX_NOT_FIRST) ? NULL : |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 743 | http_error_message(s)); |
| 744 | else { /* SI_ET_CONN_OTHER and others */ |
| 745 | s->txn->status = 500; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 746 | http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C, |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 747 | http_error_message(s)); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 748 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 749 | } |
| 750 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 751 | extern const char sess_term_cond[8]; |
| 752 | extern const char sess_fin_state[8]; |
| 753 | extern const char *monthname[12]; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 754 | struct pool_head *pool_head_http_txn; |
| 755 | struct pool_head *pool_head_requri; |
| 756 | struct pool_head *pool_head_capture = NULL; |
| 757 | struct pool_head *pool_head_uniqueid; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 758 | |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 759 | /* |
| 760 | * Capture headers from message starting at <som> according to header list |
Willy Tarreau | 54da8db | 2014-06-13 16:11:48 +0200 | [diff] [blame] | 761 | * <cap_hdr>, and fill the <cap> pointers appropriately. |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 762 | */ |
| 763 | void capture_headers(char *som, struct hdr_idx *idx, |
| 764 | char **cap, struct cap_hdr *cap_hdr) |
| 765 | { |
| 766 | char *eol, *sol, *col, *sov; |
| 767 | int cur_idx; |
| 768 | struct cap_hdr *h; |
| 769 | int len; |
| 770 | |
| 771 | sol = som + hdr_idx_first_pos(idx); |
| 772 | cur_idx = hdr_idx_first_idx(idx); |
| 773 | |
| 774 | while (cur_idx) { |
| 775 | eol = sol + idx->v[cur_idx].len; |
| 776 | |
| 777 | col = sol; |
| 778 | while (col < eol && *col != ':') |
| 779 | col++; |
| 780 | |
| 781 | sov = col + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 782 | while (sov < eol && HTTP_IS_LWS(*sov)) |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 783 | sov++; |
| 784 | |
| 785 | for (h = cap_hdr; h; h = h->next) { |
Willy Tarreau | 54da8db | 2014-06-13 16:11:48 +0200 | [diff] [blame] | 786 | if (h->namelen && (h->namelen == col - sol) && |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 787 | (strncasecmp(sol, h->name, h->namelen) == 0)) { |
| 788 | if (cap[h->index] == NULL) |
| 789 | cap[h->index] = |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 790 | pool_alloc(h->pool); |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 791 | |
| 792 | if (cap[h->index] == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 793 | ha_alert("HTTP capture : out of memory.\n"); |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 794 | continue; |
| 795 | } |
| 796 | |
| 797 | len = eol - sov; |
| 798 | if (len > h->len) |
| 799 | len = h->len; |
| 800 | |
| 801 | memcpy(cap[h->index], sov, len); |
| 802 | cap[h->index][len]=0; |
| 803 | } |
| 804 | } |
| 805 | sol = eol + idx->v[cur_idx].cr + 1; |
| 806 | cur_idx = idx->v[cur_idx].next; |
| 807 | } |
| 808 | } |
| 809 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 810 | /* |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 811 | * Returns the data from Authorization header. Function may be called more |
| 812 | * than once so data is stored in txn->auth_data. When no header is found |
| 813 | * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid |
Thierry FOURNIER | 98d9695 | 2014-01-23 12:13:02 +0100 | [diff] [blame] | 814 | * searching again for something we are unable to find anyway. However, if |
| 815 | * the result if valid, the cache is not reused because we would risk to |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 816 | * have the credentials overwritten by another stream in parallel. |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 817 | */ |
| 818 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 819 | int |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 820 | get_http_auth(struct stream *s) |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 821 | { |
| 822 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 823 | struct http_txn *txn = s->txn; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 824 | struct buffer auth_method; |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 825 | struct hdr_ctx ctx; |
| 826 | char *h, *p; |
| 827 | int len; |
| 828 | |
| 829 | #ifdef DEBUG_AUTH |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 830 | printf("Auth for stream %p: %d\n", s, txn->auth.method); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 831 | #endif |
| 832 | |
| 833 | if (txn->auth.method == HTTP_AUTH_WRONG) |
| 834 | return 0; |
| 835 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 836 | txn->auth.method = HTTP_AUTH_WRONG; |
| 837 | |
| 838 | ctx.idx = 0; |
Willy Tarreau | 844a7e7 | 2010-01-31 21:46:18 +0100 | [diff] [blame] | 839 | |
| 840 | if (txn->flags & TX_USE_PX_CONN) { |
| 841 | h = "Proxy-Authorization"; |
| 842 | len = strlen(h); |
| 843 | } else { |
| 844 | h = "Authorization"; |
| 845 | len = strlen(h); |
| 846 | } |
| 847 | |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 848 | if (!http_find_header2(h, len, ci_head(&s->req), &txn->hdr_idx, &ctx)) |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 849 | return 0; |
| 850 | |
| 851 | h = ctx.line + ctx.val; |
| 852 | |
| 853 | p = memchr(h, ' ', ctx.vlen); |
Willy Tarreau | 5c557d1 | 2016-03-13 08:17:02 +0100 | [diff] [blame] | 854 | len = p - h; |
| 855 | if (!p || len <= 0) |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 856 | return 0; |
| 857 | |
David Carlier | 7365f7d | 2016-04-04 11:54:42 +0100 | [diff] [blame] | 858 | if (chunk_initlen(&auth_method, h, 0, len) != 1) |
| 859 | return 0; |
| 860 | |
Willy Tarreau | 5c557d1 | 2016-03-13 08:17:02 +0100 | [diff] [blame] | 861 | chunk_initlen(&txn->auth.method_data, p + 1, 0, ctx.vlen - len - 1); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 862 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 863 | if (!strncasecmp("Basic", auth_method.area, auth_method.data)) { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 864 | struct buffer *http_auth = get_trash_chunk(); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 865 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 866 | len = base64dec(txn->auth.method_data.area, |
| 867 | txn->auth.method_data.data, |
| 868 | http_auth->area, global.tune.bufsize - 1); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 869 | |
| 870 | if (len < 0) |
| 871 | return 0; |
| 872 | |
| 873 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 874 | http_auth->area[len] = '\0'; |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 875 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 876 | p = strchr(http_auth->area, ':'); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 877 | |
| 878 | if (!p) |
| 879 | return 0; |
| 880 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 881 | txn->auth.user = http_auth->area; |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 882 | *p = '\0'; |
| 883 | txn->auth.pass = p+1; |
| 884 | |
| 885 | txn->auth.method = HTTP_AUTH_BASIC; |
| 886 | return 1; |
| 887 | } |
| 888 | |
| 889 | return 0; |
| 890 | } |
| 891 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 892 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 893 | /* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the |
| 894 | * conversion succeeded, 0 in case of error. If the request was already 1.X, |
| 895 | * nothing is done and 1 is returned. |
| 896 | */ |
Willy Tarreau | 418bfcc | 2012-03-09 13:56:20 +0100 | [diff] [blame] | 897 | static int http_upgrade_v09_to_v10(struct http_txn *txn) |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 898 | { |
| 899 | int delta; |
| 900 | char *cur_end; |
Willy Tarreau | 418bfcc | 2012-03-09 13:56:20 +0100 | [diff] [blame] | 901 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 902 | |
| 903 | if (msg->sl.rq.v_l != 0) |
| 904 | return 1; |
| 905 | |
Apollon Oikonomopoulos | 25a1522 | 2014-04-06 02:46:00 +0300 | [diff] [blame] | 906 | /* RFC 1945 allows only GET for HTTP/0.9 requests */ |
| 907 | if (txn->meth != HTTP_METH_GET) |
| 908 | return 0; |
| 909 | |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 910 | cur_end = ci_head(msg->chn) + msg->sl.rq.l; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 911 | |
| 912 | if (msg->sl.rq.u_l == 0) { |
Apollon Oikonomopoulos | 25a1522 | 2014-04-06 02:46:00 +0300 | [diff] [blame] | 913 | /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */ |
| 914 | return 0; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 915 | } |
| 916 | /* add HTTP version */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 917 | delta = b_rep_blk(&msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11); |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 918 | http_msg_move_end(msg, delta); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 919 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 920 | cur_end = (char *)http_parse_reqline(msg, |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 921 | HTTP_MSG_RQMETH, |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 922 | ci_head(msg->chn), cur_end + 1, |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 923 | NULL, NULL); |
| 924 | if (unlikely(!cur_end)) |
| 925 | return 0; |
| 926 | |
| 927 | /* we have a full HTTP/1.0 request now and we know that |
| 928 | * we have either a CR or an LF at <ptr>. |
| 929 | */ |
| 930 | hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r'); |
| 931 | return 1; |
| 932 | } |
| 933 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 934 | /* Parse the Connection: header of an HTTP request, looking for both "close" |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 935 | * and "keep-alive" values. If we already know that some headers may safely |
| 936 | * be removed, we remove them now. The <to_del> flags are used for that : |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 937 | * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses) |
| 938 | * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1). |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 939 | * Presence of the "Upgrade" token is also checked and reported. |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 940 | * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was |
| 941 | * found, and TX_CON_*_SET is adjusted depending on what is left so only |
| 942 | * harmless combinations may be removed. Do not call that after changes have |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 943 | * been processed. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 944 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 945 | void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 946 | { |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 947 | struct hdr_ctx ctx; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 948 | const char *hdr_val = "Connection"; |
| 949 | int hdr_len = 10; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 950 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 951 | if (txn->flags & TX_HDR_CONN_PRS) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 952 | return; |
| 953 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 954 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 955 | hdr_val = "Proxy-Connection"; |
| 956 | hdr_len = 16; |
| 957 | } |
| 958 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 959 | ctx.idx = 0; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 960 | txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET); |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 961 | while (http_find_header2(hdr_val, hdr_len, ci_head(msg->chn), &txn->hdr_idx, &ctx)) { |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 962 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 963 | txn->flags |= TX_HDR_CONN_KAL; |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 964 | if (to_del & 2) |
| 965 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 966 | else |
| 967 | txn->flags |= TX_CON_KAL_SET; |
| 968 | } |
| 969 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 970 | txn->flags |= TX_HDR_CONN_CLO; |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 971 | if (to_del & 1) |
| 972 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 973 | else |
| 974 | txn->flags |= TX_CON_CLO_SET; |
| 975 | } |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 976 | else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) { |
| 977 | txn->flags |= TX_HDR_CONN_UPG; |
| 978 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 979 | } |
| 980 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 981 | txn->flags |= TX_HDR_CONN_PRS; |
| 982 | return; |
| 983 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 984 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 985 | /* Apply desired changes on the Connection: header. Values may be removed and/or |
| 986 | * added depending on the <wanted> flags, which are exclusively composed of |
| 987 | * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The |
| 988 | * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left. |
| 989 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 990 | void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 991 | { |
| 992 | struct hdr_ctx ctx; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 993 | const char *hdr_val = "Connection"; |
| 994 | int hdr_len = 10; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 995 | |
| 996 | ctx.idx = 0; |
| 997 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 998 | |
| 999 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1000 | hdr_val = "Proxy-Connection"; |
| 1001 | hdr_len = 16; |
| 1002 | } |
| 1003 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1004 | txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET); |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 1005 | while (http_find_header2(hdr_val, hdr_len, ci_head(msg->chn), &txn->hdr_idx, &ctx)) { |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1006 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 1007 | if (wanted & TX_CON_KAL_SET) |
| 1008 | txn->flags |= TX_CON_KAL_SET; |
| 1009 | else |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1010 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1011 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1012 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 1013 | if (wanted & TX_CON_CLO_SET) |
| 1014 | txn->flags |= TX_CON_CLO_SET; |
| 1015 | else |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1016 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 1017 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1018 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1019 | |
| 1020 | if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 1021 | return; |
| 1022 | |
| 1023 | if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) { |
| 1024 | txn->flags |= TX_CON_CLO_SET; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1025 | hdr_val = "Connection: close"; |
| 1026 | hdr_len = 17; |
| 1027 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1028 | hdr_val = "Proxy-Connection: close"; |
| 1029 | hdr_len = 23; |
| 1030 | } |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1031 | http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1032 | } |
| 1033 | |
| 1034 | if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) { |
| 1035 | txn->flags |= TX_CON_KAL_SET; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1036 | hdr_val = "Connection: keep-alive"; |
| 1037 | hdr_len = 22; |
| 1038 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1039 | hdr_val = "Proxy-Connection: keep-alive"; |
| 1040 | hdr_len = 28; |
| 1041 | } |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1042 | http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1043 | } |
| 1044 | return; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1045 | } |
| 1046 | |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 1047 | /* Parses a qvalue and returns it multipled by 1000, from 0 to 1000. If the |
| 1048 | * value is larger than 1000, it is bound to 1000. The parser consumes up to |
| 1049 | * 1 digit, one dot and 3 digits and stops on the first invalid character. |
| 1050 | * Unparsable qvalues return 1000 as "q=1.000". |
| 1051 | */ |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 1052 | int parse_qvalue(const char *qvalue, const char **end) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 1053 | { |
| 1054 | int q = 1000; |
| 1055 | |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 1056 | if (!isdigit((unsigned char)*qvalue)) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 1057 | goto out; |
| 1058 | q = (*qvalue++ - '0') * 1000; |
| 1059 | |
| 1060 | if (*qvalue++ != '.') |
| 1061 | goto out; |
| 1062 | |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 1063 | if (!isdigit((unsigned char)*qvalue)) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 1064 | goto out; |
| 1065 | q += (*qvalue++ - '0') * 100; |
| 1066 | |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 1067 | if (!isdigit((unsigned char)*qvalue)) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 1068 | goto out; |
| 1069 | q += (*qvalue++ - '0') * 10; |
| 1070 | |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 1071 | if (!isdigit((unsigned char)*qvalue)) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 1072 | goto out; |
| 1073 | q += (*qvalue++ - '0') * 1; |
| 1074 | out: |
| 1075 | if (q > 1000) |
| 1076 | q = 1000; |
Willy Tarreau | 38b3aa5 | 2014-04-22 23:32:05 +0200 | [diff] [blame] | 1077 | if (end) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 1078 | *end = qvalue; |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 1079 | return q; |
| 1080 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 1081 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1082 | void http_adjust_conn_mode(struct stream *s, struct http_txn *txn, struct http_msg *msg) |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 1083 | { |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 1084 | struct proxy *fe = strm_fe(s); |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 1085 | int tmp = TX_CON_WANT_KAL; |
| 1086 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1087 | if (!((fe->options2|s->be->options2) & PR_O2_FAKE_KA)) { |
| 1088 | if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN || |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 1089 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN) |
| 1090 | tmp = TX_CON_WANT_TUN; |
| 1091 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1092 | if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 1093 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL) |
| 1094 | tmp = TX_CON_WANT_TUN; |
| 1095 | } |
| 1096 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1097 | if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL || |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 1098 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) { |
| 1099 | /* option httpclose + server_close => forceclose */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1100 | if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 1101 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL) |
| 1102 | tmp = TX_CON_WANT_CLO; |
| 1103 | else |
| 1104 | tmp = TX_CON_WANT_SCL; |
| 1105 | } |
| 1106 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1107 | if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL || |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 1108 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL) |
| 1109 | tmp = TX_CON_WANT_CLO; |
| 1110 | |
| 1111 | if ((txn->flags & TX_CON_WANT_MSK) < tmp) |
| 1112 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp; |
| 1113 | |
| 1114 | if (!(txn->flags & TX_HDR_CONN_PRS) && |
| 1115 | (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) { |
| 1116 | /* parse the Connection header and possibly clean it */ |
| 1117 | int to_del = 0; |
| 1118 | if ((msg->flags & HTTP_MSGF_VER_11) || |
| 1119 | ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1120 | !((fe->options2|s->be->options2) & PR_O2_FAKE_KA))) |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 1121 | to_del |= 2; /* remove "keep-alive" */ |
| 1122 | if (!(msg->flags & HTTP_MSGF_VER_11)) |
| 1123 | to_del |= 1; /* remove "close" */ |
| 1124 | http_parse_connection_header(txn, msg, to_del); |
| 1125 | } |
| 1126 | |
| 1127 | /* check if client or config asks for explicit close in KAL/SCL */ |
| 1128 | if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 1129 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) && |
| 1130 | ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */ |
| 1131 | (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */ |
| 1132 | !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1133 | fe->state == PR_STSTOPPED)) /* frontend is stopping */ |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 1134 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 1135 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 1136 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 1137 | /* This stream analyser waits for a complete HTTP request. It returns 1 if the |
| 1138 | * processing can continue on next analysers, or zero if it either needs more |
| 1139 | * data or wants to immediately abort the request (eg: timeout, error, ...). It |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1140 | * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 1141 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 1142 | * abort. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1143 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1144 | int http_wait_for_request(struct stream *s, struct channel *req, int an_bit) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1145 | { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1146 | /* |
| 1147 | * We will parse the partial (or complete) lines. |
| 1148 | * We will check the request syntax, and also join multi-line |
| 1149 | * headers. An index of all the lines will be elaborated while |
| 1150 | * parsing. |
| 1151 | * |
| 1152 | * For the parsing, we use a 28 states FSM. |
| 1153 | * |
| 1154 | * Here is the information we currently have : |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 1155 | * ci_head(req) = beginning of request |
| 1156 | * ci_head(req) + msg->eoh = end of processed headers / start of current one |
| 1157 | * ci_tail(req) = end of input data |
| 1158 | * msg->eol = end of current header or line (LF or CRLF) |
| 1159 | * msg->next = first non-visited byte |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 1160 | * |
| 1161 | * At end of parsing, we may perform a capture of the error (if any), and |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1162 | * we will set a few fields (txn->meth, sn->flags/SF_REDIRECTABLE). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 1163 | * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and |
| 1164 | * finally headers capture. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1165 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1166 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1167 | int cur_idx; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 1168 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 1169 | struct http_txn *txn = s->txn; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1170 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 1171 | struct hdr_ctx ctx; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1172 | |
Christopher Faulet | 4507351 | 2018-07-20 10:16:29 +0200 | [diff] [blame] | 1173 | DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%lu analysers=%02x\n", |
Willy Tarreau | 6bf1736 | 2009-02-24 10:48:35 +0100 | [diff] [blame] | 1174 | now_ms, __FUNCTION__, |
| 1175 | s, |
| 1176 | req, |
| 1177 | req->rex, req->wex, |
| 1178 | req->flags, |
Christopher Faulet | 4507351 | 2018-07-20 10:16:29 +0200 | [diff] [blame] | 1179 | ci_data(req), |
Willy Tarreau | 6bf1736 | 2009-02-24 10:48:35 +0100 | [diff] [blame] | 1180 | req->analysers); |
| 1181 | |
Willy Tarreau | 52a0c60 | 2009-08-16 22:45:38 +0200 | [diff] [blame] | 1182 | /* we're speaking HTTP here, so let's speak HTTP to the client */ |
| 1183 | s->srv_error = http_return_srv_error; |
| 1184 | |
Rian McGuire | 89fcb7d | 2018-04-24 11:19:21 -0300 | [diff] [blame] | 1185 | /* If there is data available for analysis, log the end of the idle time. */ |
Willy Tarreau | d760eec | 2018-07-10 09:50:25 +0200 | [diff] [blame] | 1186 | if (c_data(req) && s->logs.t_idle == -1) |
Rian McGuire | 89fcb7d | 2018-04-24 11:19:21 -0300 | [diff] [blame] | 1187 | s->logs.t_idle = tv_ms_elapsed(&s->logs.tv_accept, &now) - s->logs.t_handshake; |
| 1188 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 1189 | /* There's a protected area at the end of the buffer for rewriting |
| 1190 | * purposes. We don't want to start to parse the request if the |
| 1191 | * protected area is affected, because we may have to move processed |
| 1192 | * data later, which is much more complicated. |
| 1193 | */ |
Willy Tarreau | d760eec | 2018-07-10 09:50:25 +0200 | [diff] [blame] | 1194 | if (c_data(req) && msg->msg_state < HTTP_MSG_ERROR) { |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 1195 | if (txn->flags & TX_NOT_FIRST) { |
Willy Tarreau | ba0902e | 2015-01-13 14:39:16 +0100 | [diff] [blame] | 1196 | if (unlikely(!channel_is_rewritable(req))) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 1197 | if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) |
Willy Tarreau | 6464841 | 2010-03-05 10:41:54 +0100 | [diff] [blame] | 1198 | goto failed_keep_alive; |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 1199 | /* some data has still not left the buffer, wake us once that's done */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 1200 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 1201 | req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | d7ad9f5 | 2013-12-31 17:26:25 +0100 | [diff] [blame] | 1202 | req->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 1203 | return 0; |
| 1204 | } |
Willy Tarreau | 188e230 | 2018-06-15 11:11:53 +0200 | [diff] [blame] | 1205 | if (unlikely(ci_tail(req) < c_ptr(req, msg->next) || |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1206 | ci_tail(req) > b_wrap(&req->buf) - global.tune.maxrewrite)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1207 | channel_slow_realign(req, trash.area); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 1208 | } |
| 1209 | |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 1210 | if (likely(msg->next < ci_data(req))) /* some unparsed data are available */ |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 1211 | http_msg_analyzer(msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 1212 | } |
| 1213 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1214 | /* 1: we might have to print this header in debug mode */ |
| 1215 | if (unlikely((global.mode & MODE_DEBUG) && |
| 1216 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | 7d59e90 | 2014-10-21 19:36:09 +0200 | [diff] [blame] | 1217 | msg->msg_state >= HTTP_MSG_BODY)) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1218 | char *eol, *sol; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1219 | |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 1220 | sol = ci_head(req); |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 1221 | /* this is a bit complex : in case of error on the request line, |
| 1222 | * we know that rq.l is still zero, so we display only the part |
| 1223 | * up to the end of the line (truncated by debug_hdr). |
| 1224 | */ |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 1225 | eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : ci_data(req)); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1226 | debug_hdr("clireq", s, sol, eol); |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 1227 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1228 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 1229 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1230 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1231 | while (cur_idx) { |
| 1232 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 1233 | debug_hdr("clihdr", s, sol, eol); |
| 1234 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 1235 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1236 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1237 | } |
| 1238 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1239 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1240 | /* |
| 1241 | * Now we quickly check if we have found a full valid request. |
| 1242 | * If not so, we check the FD and buffer states before leaving. |
| 1243 | * A full request is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 1244 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 1245 | * requests are checked first. When waiting for a second request |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1246 | * on a keep-alive stream, if we encounter and error, close, t/o, |
| 1247 | * we note the error in the stream flags but don't set any state. |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 1248 | * Since the error will be noted there, it will not be counted by |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1249 | * process_stream() as a frontend error. |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 1250 | * Last, we may increase some tracked counters' http request errors on |
| 1251 | * the cases that are deliberately the client's fault. For instance, |
| 1252 | * a timeout or connection reset is not counted as an error. However |
| 1253 | * a bad request is. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1254 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1255 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 1256 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1257 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1258 | * First, let's catch bad requests. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1259 | */ |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 1260 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1261 | stream_inc_http_req_ctr(s); |
| 1262 | stream_inc_http_err_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1263 | proxy_inc_fe_req_ctr(sess->fe); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1264 | goto return_bad_req; |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 1265 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1266 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1267 | /* 1: Since we are in header mode, if there's no space |
| 1268 | * left for headers, we won't be able to free more |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1269 | * later, so the stream will never terminate. We |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1270 | * must terminate it now. |
| 1271 | */ |
Willy Tarreau | 2375233 | 2018-06-15 14:54:53 +0200 | [diff] [blame] | 1272 | if (unlikely(channel_full(req, global.tune.maxrewrite))) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1273 | /* FIXME: check if URI is set and return Status |
| 1274 | * 414 Request URI too long instead. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1275 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1276 | stream_inc_http_req_ctr(s); |
| 1277 | stream_inc_http_err_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1278 | proxy_inc_fe_req_ctr(sess->fe); |
Willy Tarreau | fec4d89 | 2011-09-02 20:04:57 +0200 | [diff] [blame] | 1279 | if (msg->err_pos < 0) |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 1280 | msg->err_pos = ci_data(req); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1281 | goto return_bad_req; |
| 1282 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1283 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1284 | /* 2: have we encountered a read error ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 1285 | else if (req->flags & CF_READ_ERROR) { |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1286 | if (!(s->flags & SF_ERR_MASK)) |
| 1287 | s->flags |= SF_ERR_CLICL; |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 1288 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 1289 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 1290 | goto failed_keep_alive; |
| 1291 | |
Willy Tarreau | 0f228a0 | 2015-05-01 15:37:53 +0200 | [diff] [blame] | 1292 | if (sess->fe->options & PR_O_IGNORE_PRB) |
| 1293 | goto failed_keep_alive; |
| 1294 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1295 | /* we cannot return any message on error */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 1296 | if (msg->err_pos >= 0) { |
Willy Tarreau | fd9419d | 2018-09-07 18:01:03 +0200 | [diff] [blame] | 1297 | http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1298 | stream_inc_http_err_ctr(s); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 1299 | } |
| 1300 | |
Willy Tarreau | dc979f2 | 2012-12-04 10:39:01 +0100 | [diff] [blame] | 1301 | txn->status = 400; |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 1302 | msg->err_state = msg->msg_state; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1303 | msg->msg_state = HTTP_MSG_ERROR; |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 1304 | http_reply_and_close(s, txn->status, NULL); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 1305 | req->analysers &= AN_REQ_FLT_END; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1306 | stream_inc_http_req_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1307 | proxy_inc_fe_req_ctr(sess->fe); |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 1308 | HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 1309 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 1310 | HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 1311 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1312 | if (!(s->flags & SF_FINST_MASK)) |
| 1313 | s->flags |= SF_FINST_R; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1314 | return 0; |
| 1315 | } |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 1316 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1317 | /* 3: has the read timeout expired ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 1318 | else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) { |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1319 | if (!(s->flags & SF_ERR_MASK)) |
| 1320 | s->flags |= SF_ERR_CLITO; |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 1321 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 1322 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 1323 | goto failed_keep_alive; |
| 1324 | |
Willy Tarreau | 0f228a0 | 2015-05-01 15:37:53 +0200 | [diff] [blame] | 1325 | if (sess->fe->options & PR_O_IGNORE_PRB) |
| 1326 | goto failed_keep_alive; |
| 1327 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1328 | /* read timeout : give up with an error message. */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 1329 | if (msg->err_pos >= 0) { |
Willy Tarreau | fd9419d | 2018-09-07 18:01:03 +0200 | [diff] [blame] | 1330 | http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1331 | stream_inc_http_err_ctr(s); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 1332 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1333 | txn->status = 408; |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 1334 | msg->err_state = msg->msg_state; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1335 | msg->msg_state = HTTP_MSG_ERROR; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 1336 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 1337 | req->analysers &= AN_REQ_FLT_END; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 1338 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1339 | stream_inc_http_req_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1340 | proxy_inc_fe_req_ctr(sess->fe); |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 1341 | HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 1342 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 1343 | HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 1344 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1345 | if (!(s->flags & SF_FINST_MASK)) |
| 1346 | s->flags |= SF_FINST_R; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1347 | return 0; |
| 1348 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1349 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1350 | /* 4: have we encountered a close ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 1351 | else if (req->flags & CF_SHUTR) { |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1352 | if (!(s->flags & SF_ERR_MASK)) |
| 1353 | s->flags |= SF_ERR_CLICL; |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 1354 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 1355 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 1356 | goto failed_keep_alive; |
| 1357 | |
Willy Tarreau | 0f228a0 | 2015-05-01 15:37:53 +0200 | [diff] [blame] | 1358 | if (sess->fe->options & PR_O_IGNORE_PRB) |
| 1359 | goto failed_keep_alive; |
| 1360 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 1361 | if (msg->err_pos >= 0) |
Willy Tarreau | fd9419d | 2018-09-07 18:01:03 +0200 | [diff] [blame] | 1362 | http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1363 | txn->status = 400; |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 1364 | msg->err_state = msg->msg_state; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1365 | msg->msg_state = HTTP_MSG_ERROR; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 1366 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 1367 | req->analysers &= AN_REQ_FLT_END; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1368 | stream_inc_http_err_ctr(s); |
| 1369 | stream_inc_http_req_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1370 | proxy_inc_fe_req_ctr(sess->fe); |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 1371 | HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 1372 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 1373 | HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 1374 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1375 | if (!(s->flags & SF_FINST_MASK)) |
| 1376 | s->flags |= SF_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 1377 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1378 | } |
| 1379 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 1380 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 1381 | req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1382 | s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 1383 | #ifdef TCP_QUICKACK |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 1384 | if (sess->listener->options & LI_O_NOQUICKACK && ci_data(req) && |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 1385 | objt_conn(sess->origin) && conn_ctrl_ready(__objt_conn(sess->origin))) { |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 1386 | /* We need more data, we have to re-enable quick-ack in case we |
| 1387 | * previously disabled it, otherwise we might cause the client |
| 1388 | * to delay next data. |
| 1389 | */ |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 1390 | setsockopt(__objt_conn(sess->origin)->handle.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one)); |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 1391 | } |
| 1392 | #endif |
Willy Tarreau | 1b194fe | 2009-03-21 21:10:04 +0100 | [diff] [blame] | 1393 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 1394 | if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) { |
| 1395 | /* If the client starts to talk, let's fall back to |
| 1396 | * request timeout processing. |
| 1397 | */ |
| 1398 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 1399 | req->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 1400 | } |
| 1401 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1402 | /* just set the request timeout once at the beginning of the request */ |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 1403 | if (!tick_isset(req->analyse_exp)) { |
| 1404 | if ((msg->msg_state == HTTP_MSG_RQBEFORE) && |
| 1405 | (txn->flags & TX_WAIT_NEXT_RQ) && |
| 1406 | tick_isset(s->be->timeout.httpka)) |
| 1407 | req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka); |
| 1408 | else |
| 1409 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
| 1410 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1411 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1412 | /* we're not ready yet */ |
| 1413 | return 0; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 1414 | |
| 1415 | failed_keep_alive: |
| 1416 | /* Here we process low-level errors for keep-alive requests. In |
| 1417 | * short, if the request is not the first one and it experiences |
| 1418 | * a timeout, read error or shutdown, we just silently close so |
| 1419 | * that the client can try again. |
| 1420 | */ |
| 1421 | txn->status = 0; |
| 1422 | msg->msg_state = HTTP_MSG_RQBEFORE; |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 1423 | req->analysers &= AN_REQ_FLT_END; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 1424 | s->logs.logwait = 0; |
Willy Tarreau | abcd514 | 2013-06-11 17:18:02 +0200 | [diff] [blame] | 1425 | s->logs.level = 0; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1426 | s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 1427 | http_reply_and_close(s, txn->status, NULL); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 1428 | return 0; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1429 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1430 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 1431 | /* OK now we have a complete HTTP request with indexed headers. Let's |
| 1432 | * complete the request parsing by setting a few fields we will need |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1433 | * later. At this point, we have the last CRLF at req->buf.data + msg->eoh. |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 1434 | * If the request is in HTTP/0.9 form, the rule is still true, and eoh |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1435 | * points to the CRLF of the request line. msg->next points to the first |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1436 | * byte after the last LF. msg->sov points to the first byte of data. |
| 1437 | * msg->eol cannot be trusted because it may have been left uninitialized |
| 1438 | * (for instance in the absence of headers). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 1439 | */ |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1440 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1441 | stream_inc_http_req_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1442 | proxy_inc_fe_req_ctr(sess->fe); /* one more valid request for this FE */ |
Willy Tarreau | d9b587f | 2010-02-26 10:05:55 +0100 | [diff] [blame] | 1443 | |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 1444 | if (txn->flags & TX_WAIT_NEXT_RQ) { |
| 1445 | /* kill the pending keep-alive timeout */ |
| 1446 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
| 1447 | req->analyse_exp = TICK_ETERNITY; |
| 1448 | } |
| 1449 | |
| 1450 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 1451 | /* Maybe we found in invalid header name while we were configured not |
| 1452 | * to block on that, so we have to capture it now. |
| 1453 | */ |
| 1454 | if (unlikely(msg->err_pos >= 0)) |
Willy Tarreau | fd9419d | 2018-09-07 18:01:03 +0200 | [diff] [blame] | 1455 | http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 1456 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1457 | /* |
| 1458 | * 1: identify the method |
| 1459 | */ |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 1460 | txn->meth = find_http_meth(ci_head(req), msg->sl.rq.m_l); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1461 | |
| 1462 | /* we can make use of server redirect on GET and HEAD */ |
| 1463 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1464 | s->flags |= SF_REDIRECTABLE; |
Willy Tarreau | 9165979 | 2017-11-10 19:38:10 +0100 | [diff] [blame] | 1465 | else if (txn->meth == HTTP_METH_OTHER && |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 1466 | msg->sl.rq.m_l == 3 && memcmp(ci_head(req), "PRI", 3) == 0) { |
Willy Tarreau | 9165979 | 2017-11-10 19:38:10 +0100 | [diff] [blame] | 1467 | /* PRI is reserved for the HTTP/2 preface */ |
| 1468 | msg->err_pos = 0; |
| 1469 | goto return_bad_req; |
| 1470 | } |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 1471 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1472 | /* |
| 1473 | * 2: check if the URI matches the monitor_uri. |
| 1474 | * We have to do this for every request which gets in, because |
| 1475 | * the monitor-uri is defined by the frontend. |
| 1476 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1477 | if (unlikely((sess->fe->monitor_uri_len != 0) && |
| 1478 | (sess->fe->monitor_uri_len == msg->sl.rq.u_l) && |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 1479 | !memcmp(ci_head(req) + msg->sl.rq.u, |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1480 | sess->fe->monitor_uri, |
| 1481 | sess->fe->monitor_uri_len))) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1482 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1483 | * We have found the monitor URI |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1484 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1485 | struct acl_cond *cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 1486 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1487 | s->flags |= SF_MONITOR; |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 1488 | HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1); |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 1489 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1490 | /* Check if we want to fail this monitor request or not */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1491 | list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 1492 | int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 1493 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1494 | ret = acl_pass(ret); |
| 1495 | if (cond->pol == ACL_COND_UNLESS) |
| 1496 | ret = !ret; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 1497 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1498 | if (ret) { |
| 1499 | /* we fail this request, let's return 503 service unavail */ |
| 1500 | txn->status = 503; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 1501 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1502 | if (!(s->flags & SF_ERR_MASK)) |
| 1503 | s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1504 | goto return_prx_cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 1505 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1506 | } |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 1507 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1508 | /* nothing to fail, let's reply normaly */ |
| 1509 | txn->status = 200; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 1510 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1511 | if (!(s->flags & SF_ERR_MASK)) |
| 1512 | s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1513 | goto return_prx_cond; |
| 1514 | } |
| 1515 | |
| 1516 | /* |
| 1517 | * 3: Maybe we have to copy the original REQURI for the logs ? |
| 1518 | * Note: we cannot log anymore if the request has been |
| 1519 | * classified as invalid. |
| 1520 | */ |
| 1521 | if (unlikely(s->logs.logwait & LW_REQ)) { |
| 1522 | /* we have a complete HTTP request that we must log */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 1523 | if ((txn->uri = pool_alloc(pool_head_requri)) != NULL) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1524 | int urilen = msg->sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1525 | |
Stéphane Cottin | 23e9e93 | 2017-05-18 08:58:41 +0200 | [diff] [blame] | 1526 | if (urilen >= global.tune.requri_len ) |
| 1527 | urilen = global.tune.requri_len - 1; |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 1528 | memcpy(txn->uri, ci_head(req), urilen); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1529 | txn->uri[urilen] = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1530 | |
Willy Tarreau | d79a3b2 | 2012-12-28 09:40:16 +0100 | [diff] [blame] | 1531 | if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT))) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1532 | s->do_log(s); |
| 1533 | } else { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1534 | ha_alert("HTTP logging : out of memory.\n"); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1535 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1536 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1537 | |
Willy Tarreau | 91852eb | 2015-05-01 13:26:00 +0200 | [diff] [blame] | 1538 | /* RFC7230#2.6 has enforced the format of the HTTP version string to be |
| 1539 | * exactly one digit "." one digit. This check may be disabled using |
| 1540 | * option accept-invalid-http-request. |
| 1541 | */ |
| 1542 | if (!(sess->fe->options2 & PR_O2_REQBUG_OK)) { |
| 1543 | if (msg->sl.rq.v_l != 8) { |
| 1544 | msg->err_pos = msg->sl.rq.v; |
| 1545 | goto return_bad_req; |
| 1546 | } |
| 1547 | |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 1548 | if (ci_head(req)[msg->sl.rq.v + 4] != '/' || |
| 1549 | !isdigit((unsigned char)ci_head(req)[msg->sl.rq.v + 5]) || |
| 1550 | ci_head(req)[msg->sl.rq.v + 6] != '.' || |
| 1551 | !isdigit((unsigned char)ci_head(req)[msg->sl.rq.v + 7])) { |
Willy Tarreau | 91852eb | 2015-05-01 13:26:00 +0200 | [diff] [blame] | 1552 | msg->err_pos = msg->sl.rq.v + 4; |
| 1553 | goto return_bad_req; |
| 1554 | } |
| 1555 | } |
Willy Tarreau | 1331766 | 2015-05-01 13:47:08 +0200 | [diff] [blame] | 1556 | else { |
| 1557 | /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */ |
| 1558 | if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn)) |
| 1559 | goto return_bad_req; |
| 1560 | } |
Willy Tarreau | 91852eb | 2015-05-01 13:26:00 +0200 | [diff] [blame] | 1561 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1562 | /* ... and check if the request is HTTP/1.1 or above */ |
| 1563 | if ((msg->sl.rq.v_l == 8) && |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 1564 | ((ci_head(req)[msg->sl.rq.v + 5] > '1') || |
| 1565 | ((ci_head(req)[msg->sl.rq.v + 5] == '1') && |
| 1566 | (ci_head(req)[msg->sl.rq.v + 7] >= '1')))) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 1567 | msg->flags |= HTTP_MSGF_VER_11; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1568 | |
| 1569 | /* "connection" has not been parsed yet */ |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 1570 | txn->flags &= ~(TX_HDR_CONN_PRS | TX_HDR_CONN_CLO | TX_HDR_CONN_KAL | TX_HDR_CONN_UPG); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1571 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1572 | /* if the frontend has "option http-use-proxy-header", we'll check if |
| 1573 | * we have what looks like a proxied connection instead of a connection, |
| 1574 | * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection. |
| 1575 | * Note that this is *not* RFC-compliant, however browsers and proxies |
| 1576 | * happen to do that despite being non-standard :-( |
| 1577 | * We consider that a request not beginning with either '/' or '*' is |
| 1578 | * a proxied connection, which covers both "scheme://location" and |
| 1579 | * CONNECT ip:port. |
| 1580 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1581 | if ((sess->fe->options2 & PR_O2_USE_PXHDR) && |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 1582 | ci_head(req)[msg->sl.rq.u] != '/' && ci_head(req)[msg->sl.rq.u] != '*') |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1583 | txn->flags |= TX_USE_PX_CONN; |
| 1584 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 1585 | /* transfer length unknown*/ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 1586 | msg->flags &= ~HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 1587 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1588 | /* 5: we may need to capture headers */ |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 1589 | if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap)) |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 1590 | capture_headers(ci_head(req), &txn->hdr_idx, |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 1591 | s->req_cap, sess->fe->req_cap); |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 1592 | |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 1593 | /* 6: determine the transfer-length according to RFC2616 #4.4, updated |
| 1594 | * by RFC7230#3.3.3 : |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 1595 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 1596 | * The length of a message body is determined by one of the following |
| 1597 | * (in order of precedence): |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 1598 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 1599 | * 1. Any response to a HEAD request and any response with a 1xx |
| 1600 | * (Informational), 204 (No Content), or 304 (Not Modified) status |
| 1601 | * code is always terminated by the first empty line after the |
| 1602 | * header fields, regardless of the header fields present in the |
| 1603 | * message, and thus cannot contain a message body. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 1604 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 1605 | * 2. Any 2xx (Successful) response to a CONNECT request implies that |
| 1606 | * the connection will become a tunnel immediately after the empty |
| 1607 | * line that concludes the header fields. A client MUST ignore any |
| 1608 | * Content-Length or Transfer-Encoding header fields received in |
| 1609 | * such a message. |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 1610 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 1611 | * 3. If a Transfer-Encoding header field is present and the chunked |
| 1612 | * transfer coding (Section 4.1) is the final encoding, the message |
| 1613 | * body length is determined by reading and decoding the chunked |
| 1614 | * data until the transfer coding indicates the data is complete. |
| 1615 | * |
| 1616 | * If a Transfer-Encoding header field is present in a response and |
| 1617 | * the chunked transfer coding is not the final encoding, the |
| 1618 | * message body length is determined by reading the connection until |
| 1619 | * it is closed by the server. If a Transfer-Encoding header field |
| 1620 | * is present in a request and the chunked transfer coding is not |
| 1621 | * the final encoding, the message body length cannot be determined |
| 1622 | * reliably; the server MUST respond with the 400 (Bad Request) |
| 1623 | * status code and then close the connection. |
| 1624 | * |
| 1625 | * If a message is received with both a Transfer-Encoding and a |
| 1626 | * Content-Length header field, the Transfer-Encoding overrides the |
| 1627 | * Content-Length. Such a message might indicate an attempt to |
| 1628 | * perform request smuggling (Section 9.5) or response splitting |
| 1629 | * (Section 9.4) and ought to be handled as an error. A sender MUST |
| 1630 | * remove the received Content-Length field prior to forwarding such |
| 1631 | * a message downstream. |
| 1632 | * |
| 1633 | * 4. If a message is received without Transfer-Encoding and with |
| 1634 | * either multiple Content-Length header fields having differing |
| 1635 | * field-values or a single Content-Length header field having an |
| 1636 | * invalid value, then the message framing is invalid and the |
| 1637 | * recipient MUST treat it as an unrecoverable error. If this is a |
| 1638 | * request message, the server MUST respond with a 400 (Bad Request) |
| 1639 | * status code and then close the connection. If this is a response |
| 1640 | * message received by a proxy, the proxy MUST close the connection |
| 1641 | * to the server, discard the received response, and send a 502 (Bad |
| 1642 | * Gateway) response to the client. If this is a response message |
| 1643 | * received by a user agent, the user agent MUST close the |
| 1644 | * connection to the server and discard the received response. |
| 1645 | * |
| 1646 | * 5. If a valid Content-Length header field is present without |
| 1647 | * Transfer-Encoding, its decimal value defines the expected message |
| 1648 | * body length in octets. If the sender closes the connection or |
| 1649 | * the recipient times out before the indicated number of octets are |
| 1650 | * received, the recipient MUST consider the message to be |
| 1651 | * incomplete and close the connection. |
| 1652 | * |
| 1653 | * 6. If this is a request message and none of the above are true, then |
| 1654 | * the message body length is zero (no message body is present). |
| 1655 | * |
| 1656 | * 7. Otherwise, this is a response message without a declared message |
| 1657 | * body length, so the message body length is determined by the |
| 1658 | * number of octets received prior to the server closing the |
| 1659 | * connection. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 1660 | */ |
| 1661 | |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 1662 | ctx.idx = 0; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 1663 | /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */ |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 1664 | while (http_find_header2("Transfer-Encoding", 17, ci_head(req), &txn->hdr_idx, &ctx)) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 1665 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
Christopher Faulet | be821b9 | 2017-03-30 11:21:53 +0200 | [diff] [blame] | 1666 | msg->flags |= HTTP_MSGF_TE_CHNK; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 1667 | else if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | 34dfc60 | 2015-05-01 10:09:49 +0200 | [diff] [blame] | 1668 | /* chunked not last, return badreq */ |
| 1669 | goto return_bad_req; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 1670 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 1671 | } |
| 1672 | |
Willy Tarreau | 1c91391 | 2015-04-30 10:57:51 +0200 | [diff] [blame] | 1673 | /* Chunked requests must have their content-length removed */ |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 1674 | ctx.idx = 0; |
Willy Tarreau | 1c91391 | 2015-04-30 10:57:51 +0200 | [diff] [blame] | 1675 | if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 1676 | while (http_find_header2("Content-Length", 14, ci_head(req), &txn->hdr_idx, &ctx)) |
Willy Tarreau | 1c91391 | 2015-04-30 10:57:51 +0200 | [diff] [blame] | 1677 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 1678 | } |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 1679 | else while (http_find_header2("Content-Length", 14, ci_head(req), &txn->hdr_idx, &ctx)) { |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 1680 | signed long long cl; |
| 1681 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 1682 | if (!ctx.vlen) { |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 1683 | msg->err_pos = ctx.line + ctx.val - ci_head(req); |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 1684 | goto return_bad_req; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 1685 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 1686 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 1687 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) { |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 1688 | msg->err_pos = ctx.line + ctx.val - ci_head(req); |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 1689 | goto return_bad_req; /* parse failure */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 1690 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 1691 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 1692 | if (cl < 0) { |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 1693 | msg->err_pos = ctx.line + ctx.val - ci_head(req); |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 1694 | goto return_bad_req; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 1695 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 1696 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 1697 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) { |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 1698 | msg->err_pos = ctx.line + ctx.val - ci_head(req); |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 1699 | goto return_bad_req; /* already specified, was different */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 1700 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 1701 | |
Christopher Faulet | be821b9 | 2017-03-30 11:21:53 +0200 | [diff] [blame] | 1702 | msg->flags |= HTTP_MSGF_CNT_LEN; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 1703 | msg->body_len = msg->chunk_len = cl; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 1704 | } |
| 1705 | |
Willy Tarreau | 34dfc60 | 2015-05-01 10:09:49 +0200 | [diff] [blame] | 1706 | /* even bodyless requests have a known length */ |
| 1707 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 1708 | |
Willy Tarreau | 179085c | 2014-04-28 16:48:56 +0200 | [diff] [blame] | 1709 | /* Until set to anything else, the connection mode is set as Keep-Alive. It will |
| 1710 | * only change if both the request and the config reference something else. |
| 1711 | * Option httpclose by itself sets tunnel mode where headers are mangled. |
| 1712 | * However, if another mode is set, it will affect it (eg: server-close/ |
| 1713 | * keep-alive + httpclose = close). Note that we avoid to redo the same work |
| 1714 | * if FE and BE have the same settings (common). The method consists in |
| 1715 | * checking if options changed between the two calls (implying that either |
| 1716 | * one is non-null, or one of them is non-null and we are there for the first |
| 1717 | * time. |
| 1718 | */ |
| 1719 | if (!(txn->flags & TX_HDR_CONN_PRS) || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1720 | ((sess->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE))) |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 1721 | http_adjust_conn_mode(s, txn, msg); |
Willy Tarreau | 179085c | 2014-04-28 16:48:56 +0200 | [diff] [blame] | 1722 | |
Willy Tarreau | 9fbe18e | 2015-05-01 22:42:08 +0200 | [diff] [blame] | 1723 | /* we may have to wait for the request's body */ |
| 1724 | if ((s->be->options & PR_O_WREQ_BODY) && |
| 1725 | (msg->body_len || (msg->flags & HTTP_MSGF_TE_CHNK))) |
| 1726 | req->analysers |= AN_REQ_HTTP_BODY; |
| 1727 | |
Willy Tarreau | 83ece46 | 2017-12-21 15:13:09 +0100 | [diff] [blame] | 1728 | /* |
| 1729 | * RFC7234#4: |
| 1730 | * A cache MUST write through requests with methods |
| 1731 | * that are unsafe (Section 4.2.1 of [RFC7231]) to |
| 1732 | * the origin server; i.e., a cache is not allowed |
| 1733 | * to generate a reply to such a request before |
| 1734 | * having forwarded the request and having received |
| 1735 | * a corresponding response. |
| 1736 | * |
| 1737 | * RFC7231#4.2.1: |
| 1738 | * Of the request methods defined by this |
| 1739 | * specification, the GET, HEAD, OPTIONS, and TRACE |
| 1740 | * methods are defined to be safe. |
| 1741 | */ |
| 1742 | if (likely(txn->meth == HTTP_METH_GET || |
| 1743 | txn->meth == HTTP_METH_HEAD || |
| 1744 | txn->meth == HTTP_METH_OPTIONS || |
| 1745 | txn->meth == HTTP_METH_TRACE)) |
| 1746 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
| 1747 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 1748 | /* end of job, return OK */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 1749 | req->analysers &= ~an_bit; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 1750 | req->analyse_exp = TICK_ETERNITY; |
| 1751 | return 1; |
| 1752 | |
| 1753 | return_bad_req: |
| 1754 | /* We centralize bad requests processing here */ |
| 1755 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 1756 | /* we detected a parsing error. We want to archive this request |
| 1757 | * in the dedicated proxy area for later troubleshooting. |
| 1758 | */ |
Willy Tarreau | fd9419d | 2018-09-07 18:01:03 +0200 | [diff] [blame] | 1759 | http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe); |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 1760 | } |
| 1761 | |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 1762 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 1763 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 1764 | txn->status = 400; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 1765 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 1766 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 1767 | HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 1768 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 1769 | HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1); |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 1770 | |
| 1771 | return_prx_cond: |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1772 | if (!(s->flags & SF_ERR_MASK)) |
| 1773 | s->flags |= SF_ERR_PRXCOND; |
| 1774 | if (!(s->flags & SF_FINST_MASK)) |
| 1775 | s->flags |= SF_FINST_R; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 1776 | |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 1777 | req->analysers &= AN_REQ_FLT_END; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 1778 | req->analyse_exp = TICK_ETERNITY; |
| 1779 | return 0; |
| 1780 | } |
| 1781 | |
Willy Tarreau | 4f8a83c | 2012-06-04 00:26:23 +0200 | [diff] [blame] | 1782 | |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 1783 | /* This function prepares an applet to handle the stats. It can deal with the |
| 1784 | * "100-continue" expectation, check that admin rules are met for POST requests, |
| 1785 | * and program a response message if something was unexpected. It cannot fail |
| 1786 | * and always relies on the stats applet to complete the job. It does not touch |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 1787 | * analysers nor counters, which are left to the caller. It does not touch |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1788 | * s->target which is supposed to already point to the stats applet. The caller |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1789 | * is expected to have already assigned an appctx to the stream. |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 1790 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1791 | int http_handle_stats(struct stream *s, struct channel *req) |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 1792 | { |
| 1793 | struct stats_admin_rule *stats_admin_rule; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 1794 | struct stream_interface *si = &s->si[1]; |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 1795 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 1796 | struct http_txn *txn = s->txn; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 1797 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 1798 | struct uri_auth *uri_auth = s->be->uri_auth; |
| 1799 | const char *uri, *h, *lookup; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1800 | struct appctx *appctx; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 1801 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1802 | appctx = si_appctx(si); |
| 1803 | memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats)); |
| 1804 | appctx->st1 = appctx->st2 = 0; |
| 1805 | appctx->ctx.stats.st_code = STAT_STATUS_INIT; |
| 1806 | appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 1807 | if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn->meth != HTTP_METH_HEAD)) |
Willy Tarreau | af3cf70 | 2014-04-22 22:19:53 +0200 | [diff] [blame] | 1808 | appctx->ctx.stats.flags |= STAT_CHUNKED; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 1809 | |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 1810 | uri = ci_head(msg->chn) + msg->sl.rq.u; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 1811 | lookup = uri + uri_auth->uri_len; |
| 1812 | |
| 1813 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) { |
| 1814 | if (memcmp(h, ";up", 3) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1815 | appctx->ctx.stats.flags |= STAT_HIDE_DOWN; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 1816 | break; |
| 1817 | } |
| 1818 | } |
| 1819 | |
| 1820 | if (uri_auth->refresh) { |
| 1821 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) { |
| 1822 | if (memcmp(h, ";norefresh", 10) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1823 | appctx->ctx.stats.flags |= STAT_NO_REFRESH; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 1824 | break; |
| 1825 | } |
| 1826 | } |
| 1827 | } |
| 1828 | |
| 1829 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) { |
| 1830 | if (memcmp(h, ";csv", 4) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1831 | appctx->ctx.stats.flags &= ~STAT_FMT_HTML; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 1832 | break; |
| 1833 | } |
| 1834 | } |
| 1835 | |
Willy Tarreau | 1e62df9 | 2016-01-11 18:57:53 +0100 | [diff] [blame] | 1836 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 6; h++) { |
| 1837 | if (memcmp(h, ";typed", 6) == 0) { |
| 1838 | appctx->ctx.stats.flags &= ~STAT_FMT_HTML; |
| 1839 | appctx->ctx.stats.flags |= STAT_FMT_TYPED; |
| 1840 | break; |
| 1841 | } |
| 1842 | } |
| 1843 | |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 1844 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) { |
| 1845 | if (memcmp(h, ";st=", 4) == 0) { |
| 1846 | int i; |
| 1847 | h += 4; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1848 | appctx->ctx.stats.st_code = STAT_STATUS_UNKN; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 1849 | for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) { |
| 1850 | if (strncmp(stat_status_codes[i], h, 4) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1851 | appctx->ctx.stats.st_code = i; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 1852 | break; |
| 1853 | } |
| 1854 | } |
| 1855 | break; |
| 1856 | } |
| 1857 | } |
| 1858 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1859 | appctx->ctx.stats.scope_str = 0; |
| 1860 | appctx->ctx.stats.scope_len = 0; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 1861 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) { |
| 1862 | if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) { |
| 1863 | int itx = 0; |
| 1864 | const char *h2; |
| 1865 | char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1]; |
| 1866 | const char *err; |
| 1867 | |
| 1868 | h += strlen(STAT_SCOPE_INPUT_NAME) + 1; |
| 1869 | h2 = h; |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 1870 | appctx->ctx.stats.scope_str = h2 - ci_head(msg->chn); |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 1871 | while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') { |
| 1872 | itx++; |
| 1873 | h++; |
| 1874 | } |
| 1875 | |
| 1876 | if (itx > STAT_SCOPE_TXT_MAXLEN) |
| 1877 | itx = STAT_SCOPE_TXT_MAXLEN; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1878 | appctx->ctx.stats.scope_len = itx; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 1879 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1880 | /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */ |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 1881 | memcpy(scope_txt, h2, itx); |
| 1882 | scope_txt[itx] = '\0'; |
| 1883 | err = invalid_char(scope_txt); |
| 1884 | if (err) { |
| 1885 | /* bad char in search text => clear scope */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1886 | appctx->ctx.stats.scope_str = 0; |
| 1887 | appctx->ctx.stats.scope_len = 0; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 1888 | } |
| 1889 | break; |
| 1890 | } |
| 1891 | } |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 1892 | |
| 1893 | /* now check whether we have some admin rules for this request */ |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 1894 | list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) { |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 1895 | int ret = 1; |
| 1896 | |
| 1897 | if (stats_admin_rule->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 1898 | ret = acl_exec_cond(stats_admin_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 1899 | ret = acl_pass(ret); |
| 1900 | if (stats_admin_rule->cond->pol == ACL_COND_UNLESS) |
| 1901 | ret = !ret; |
| 1902 | } |
| 1903 | |
| 1904 | if (ret) { |
| 1905 | /* no rule, or the rule matches */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1906 | appctx->ctx.stats.flags |= STAT_ADMIN; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 1907 | break; |
| 1908 | } |
| 1909 | } |
| 1910 | |
| 1911 | /* Was the status page requested with a POST ? */ |
Willy Tarreau | b8cdf52 | 2015-05-29 01:09:15 +0200 | [diff] [blame] | 1912 | if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1913 | if (appctx->ctx.stats.flags & STAT_ADMIN) { |
Willy Tarreau | cfe7fdd | 2014-04-26 22:08:32 +0200 | [diff] [blame] | 1914 | /* we'll need the request body, possibly after sending 100-continue */ |
Willy Tarreau | b8cdf52 | 2015-05-29 01:09:15 +0200 | [diff] [blame] | 1915 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) |
| 1916 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1917 | appctx->st0 = STAT_HTTP_POST; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 1918 | } |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 1919 | else { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1920 | appctx->ctx.stats.st_code = STAT_STATUS_DENY; |
| 1921 | appctx->st0 = STAT_HTTP_LAST; |
de Lafond Guillaume | 88c278f | 2013-04-15 19:27:10 +0200 | [diff] [blame] | 1922 | } |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 1923 | } |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 1924 | else { |
| 1925 | /* So it was another method (GET/HEAD) */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1926 | appctx->st0 = STAT_HTTP_HEAD; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 1927 | } |
| 1928 | |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 1929 | s->task->nice = -32; /* small boost for HTTP statistics */ |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 1930 | return 1; |
| 1931 | } |
| 1932 | |
Lukas Tribus | 67db8df | 2013-06-23 17:37:13 +0200 | [diff] [blame] | 1933 | /* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets |
| 1934 | * (as per RFC3260 #4 and BCP37 #4.2 and #5.2). |
| 1935 | */ |
Vincent Bernat | 6e61589 | 2016-05-18 16:17:44 +0200 | [diff] [blame] | 1936 | void inet_set_tos(int fd, const struct sockaddr_storage *from, int tos) |
Lukas Tribus | 67db8df | 2013-06-23 17:37:13 +0200 | [diff] [blame] | 1937 | { |
| 1938 | #ifdef IP_TOS |
Vincent Bernat | 6e61589 | 2016-05-18 16:17:44 +0200 | [diff] [blame] | 1939 | if (from->ss_family == AF_INET) |
Lukas Tribus | 67db8df | 2013-06-23 17:37:13 +0200 | [diff] [blame] | 1940 | setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)); |
| 1941 | #endif |
| 1942 | #ifdef IPV6_TCLASS |
Vincent Bernat | 6e61589 | 2016-05-18 16:17:44 +0200 | [diff] [blame] | 1943 | if (from->ss_family == AF_INET6) { |
| 1944 | if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)from)->sin6_addr)) |
Lukas Tribus | 67db8df | 2013-06-23 17:37:13 +0200 | [diff] [blame] | 1945 | /* v4-mapped addresses need IP_TOS */ |
| 1946 | setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)); |
| 1947 | else |
| 1948 | setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos)); |
| 1949 | } |
| 1950 | #endif |
| 1951 | } |
| 1952 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1953 | int http_transform_header_str(struct stream* s, struct http_msg *msg, |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 1954 | const char* name, unsigned int name_len, |
| 1955 | const char *str, struct my_regex *re, |
| 1956 | int action) |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 1957 | { |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 1958 | struct hdr_ctx ctx; |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 1959 | char *buf = ci_head(msg->chn); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 1960 | struct hdr_idx *idx = &s->txn->hdr_idx; |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 1961 | int (*http_find_hdr_func)(const char *name, int len, char *sol, |
| 1962 | struct hdr_idx *idx, struct hdr_ctx *ctx); |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1963 | struct buffer *output = get_trash_chunk(); |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 1964 | |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 1965 | ctx.idx = 0; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 1966 | |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 1967 | /* Choose the header browsing function. */ |
| 1968 | switch (action) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 1969 | case ACT_HTTP_REPLACE_VAL: |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 1970 | http_find_hdr_func = http_find_header2; |
| 1971 | break; |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 1972 | case ACT_HTTP_REPLACE_HDR: |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 1973 | http_find_hdr_func = http_find_full_header2; |
| 1974 | break; |
| 1975 | default: /* impossible */ |
| 1976 | return -1; |
| 1977 | } |
| 1978 | |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 1979 | while (http_find_hdr_func(name, name_len, buf, idx, &ctx)) { |
| 1980 | struct hdr_idx_elem *hdr = idx->v + ctx.idx; |
Willy Tarreau | 6e27be1 | 2018-08-22 04:46:47 +0200 | [diff] [blame] | 1981 | int delta, len; |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 1982 | char *val = ctx.line + ctx.val; |
| 1983 | char* val_end = val + ctx.vlen; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 1984 | |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 1985 | if (!regex_exec_match2(re, val, val_end-val, MAX_MATCH, pmatch, 0)) |
| 1986 | continue; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 1987 | |
Willy Tarreau | 6e27be1 | 2018-08-22 04:46:47 +0200 | [diff] [blame] | 1988 | len = exp_replace(output->area, output->size, val, str, pmatch); |
| 1989 | if (len == -1) |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 1990 | return -1; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 1991 | |
Willy Tarreau | 6e27be1 | 2018-08-22 04:46:47 +0200 | [diff] [blame] | 1992 | delta = b_rep_blk(&msg->chn->buf, val, val_end, output->area, len); |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 1993 | |
| 1994 | hdr->len += delta; |
| 1995 | http_msg_move_end(msg, delta); |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 1996 | |
| 1997 | /* Adjust the length of the current value of the index. */ |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 1998 | ctx.vlen += delta; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 1999 | } |
| 2000 | |
| 2001 | return 0; |
| 2002 | } |
| 2003 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2004 | static int http_transform_header(struct stream* s, struct http_msg *msg, |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 2005 | const char* name, unsigned int name_len, |
| 2006 | struct list *fmt, struct my_regex *re, |
| 2007 | int action) |
| 2008 | { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 2009 | struct buffer *replace; |
Christopher Faulet | 07a0fec | 2017-02-08 12:17:07 +0100 | [diff] [blame] | 2010 | int ret = -1; |
| 2011 | |
| 2012 | replace = alloc_trash_chunk(); |
| 2013 | if (!replace) |
| 2014 | goto leave; |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 2015 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2016 | replace->data = build_logline(s, replace->area, replace->size, fmt); |
| 2017 | if (replace->data >= replace->size - 1) |
Christopher Faulet | 07a0fec | 2017-02-08 12:17:07 +0100 | [diff] [blame] | 2018 | goto leave; |
| 2019 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2020 | ret = http_transform_header_str(s, msg, name, name_len, replace->area, |
| 2021 | re, action); |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 2022 | |
Christopher Faulet | 07a0fec | 2017-02-08 12:17:07 +0100 | [diff] [blame] | 2023 | leave: |
| 2024 | free_trash_chunk(replace); |
| 2025 | return ret; |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 2026 | } |
| 2027 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2028 | /* Executes the http-request rules <rules> for stream <s>, proxy <px> and |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 2029 | * transaction <txn>. Returns the verdict of the first rule that prevents |
| 2030 | * further processing of the request (auth, deny, ...), and defaults to |
| 2031 | * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or |
| 2032 | * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 2033 | * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL |
| 2034 | * and a deny/tarpit rule is matched, it will be filled with this rule's deny |
| 2035 | * status. |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2036 | */ |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 2037 | enum rule_result |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 2038 | http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s, int *deny_status) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2039 | { |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 2040 | struct session *sess = strm_sess(s); |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 2041 | struct http_txn *txn = s->txn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2042 | struct connection *cli_conn; |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 2043 | struct act_rule *rule; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 2044 | struct hdr_ctx ctx; |
Willy Tarreau | ae3c010 | 2014-04-28 23:22:08 +0200 | [diff] [blame] | 2045 | const char *auth_realm; |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 2046 | int act_flags = 0; |
Thierry Fournier | 4b788f7 | 2016-06-01 13:35:36 +0200 | [diff] [blame] | 2047 | int len; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2048 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 2049 | /* If "the current_rule_list" match the executed rule list, we are in |
| 2050 | * resume condition. If a resume is needed it is always in the action |
| 2051 | * and never in the ACL or converters. In this case, we initialise the |
| 2052 | * current rule, and go to the action execution point. |
| 2053 | */ |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 2054 | if (s->current_rule) { |
| 2055 | rule = s->current_rule; |
| 2056 | s->current_rule = NULL; |
| 2057 | if (s->current_rule_list == rules) |
| 2058 | goto resume_execution; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 2059 | } |
| 2060 | s->current_rule_list = rules; |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 2061 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2062 | list_for_each_entry(rule, rules, list) { |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2063 | |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 2064 | /* check optional condition */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2065 | if (rule->cond) { |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 2066 | int ret; |
| 2067 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 2068 | ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2069 | ret = acl_pass(ret); |
| 2070 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2071 | if (rule->cond->pol == ACL_COND_UNLESS) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2072 | ret = !ret; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 2073 | |
| 2074 | if (!ret) /* condition not matched */ |
| 2075 | continue; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2076 | } |
| 2077 | |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 2078 | act_flags |= ACT_FLAG_FIRST; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 2079 | resume_execution: |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 2080 | switch (rule->action) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2081 | case ACT_ACTION_ALLOW: |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 2082 | return HTTP_RULE_RES_STOP; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 2083 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2084 | case ACT_ACTION_DENY: |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 2085 | if (deny_status) |
| 2086 | *deny_status = rule->deny_status; |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 2087 | return HTTP_RULE_RES_DENY; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 2088 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2089 | case ACT_HTTP_REQ_TARPIT: |
Willy Tarreau | ccbcc37 | 2012-12-27 12:37:57 +0100 | [diff] [blame] | 2090 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 2091 | if (deny_status) |
| 2092 | *deny_status = rule->deny_status; |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 2093 | return HTTP_RULE_RES_DENY; |
Willy Tarreau | ccbcc37 | 2012-12-27 12:37:57 +0100 | [diff] [blame] | 2094 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2095 | case ACT_HTTP_REQ_AUTH: |
Willy Tarreau | ae3c010 | 2014-04-28 23:22:08 +0200 | [diff] [blame] | 2096 | /* Auth might be performed on regular http-req rules as well as on stats */ |
| 2097 | auth_realm = rule->arg.auth.realm; |
| 2098 | if (!auth_realm) { |
| 2099 | if (px->uri_auth && rules == &px->uri_auth->http_req_rules) |
| 2100 | auth_realm = STATS_DEFAULT_REALM; |
| 2101 | else |
| 2102 | auth_realm = px->id; |
| 2103 | } |
| 2104 | /* send 401/407 depending on whether we use a proxy or not. We still |
| 2105 | * count one error, because normal browsing won't significantly |
| 2106 | * increase the counter but brute force attempts will. |
| 2107 | */ |
| 2108 | chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm); |
| 2109 | txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401; |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 2110 | http_reply_and_close(s, txn->status, &trash); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2111 | stream_inc_http_err_ctr(s); |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 2112 | return HTTP_RULE_RES_ABRT; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 2113 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2114 | case ACT_HTTP_REDIR: |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 2115 | if (!http_apply_redirect_rule(rule->arg.redir, s, txn)) |
| 2116 | return HTTP_RULE_RES_BADREQ; |
| 2117 | return HTTP_RULE_RES_DONE; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 2118 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2119 | case ACT_HTTP_SET_NICE: |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 2120 | s->task->nice = rule->arg.nice; |
| 2121 | break; |
| 2122 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2123 | case ACT_HTTP_SET_TOS: |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 2124 | if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 2125 | inet_set_tos(cli_conn->handle.fd, &cli_conn->addr.from, rule->arg.tos); |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 2126 | break; |
| 2127 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2128 | case ACT_HTTP_SET_MARK: |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 2129 | #ifdef SO_MARK |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 2130 | if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 2131 | setsockopt(cli_conn->handle.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark)); |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 2132 | #endif |
| 2133 | break; |
| 2134 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2135 | case ACT_HTTP_SET_LOGL: |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 2136 | s->logs.level = rule->arg.loglevel; |
| 2137 | break; |
| 2138 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2139 | case ACT_HTTP_REPLACE_HDR: |
| 2140 | case ACT_HTTP_REPLACE_VAL: |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 2141 | if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name, |
| 2142 | rule->arg.hdr_add.name_len, |
| 2143 | &rule->arg.hdr_add.fmt, |
| 2144 | &rule->arg.hdr_add.re, rule->action)) |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 2145 | return HTTP_RULE_RES_BADREQ; |
| 2146 | break; |
| 2147 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2148 | case ACT_HTTP_DEL_HDR: |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 2149 | ctx.idx = 0; |
| 2150 | /* remove all occurrences of the header */ |
| 2151 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 2152 | ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) { |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 2153 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 2154 | } |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 2155 | break; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 2156 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2157 | case ACT_HTTP_SET_HDR: |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2158 | case ACT_HTTP_ADD_HDR: { |
Thierry Fournier | 4b788f7 | 2016-06-01 13:35:36 +0200 | [diff] [blame] | 2159 | /* The scope of the trash buffer must be limited to this function. The |
| 2160 | * build_logline() function can execute a lot of other function which |
| 2161 | * can use the trash buffer. So for limiting the scope of this global |
| 2162 | * buffer, we build first the header value using build_logline, and |
| 2163 | * after we store the header name. |
| 2164 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 2165 | struct buffer *replace; |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2166 | |
| 2167 | replace = alloc_trash_chunk(); |
| 2168 | if (!replace) |
| 2169 | return HTTP_RULE_RES_BADREQ; |
| 2170 | |
Thierry Fournier | 4b788f7 | 2016-06-01 13:35:36 +0200 | [diff] [blame] | 2171 | len = rule->arg.hdr_add.name_len + 2, |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2172 | len += build_logline(s, replace->area + len, |
| 2173 | replace->size - len, |
| 2174 | &rule->arg.hdr_add.fmt); |
| 2175 | memcpy(replace->area, rule->arg.hdr_add.name, |
| 2176 | rule->arg.hdr_add.name_len); |
| 2177 | replace->area[rule->arg.hdr_add.name_len] = ':'; |
| 2178 | replace->area[rule->arg.hdr_add.name_len + 1] = ' '; |
| 2179 | replace->data = len; |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 2180 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2181 | if (rule->action == ACT_HTTP_SET_HDR) { |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 2182 | /* remove all occurrences of the header */ |
| 2183 | ctx.idx = 0; |
| 2184 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 2185 | ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) { |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 2186 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
| 2187 | } |
| 2188 | } |
| 2189 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2190 | if (http_header_add_tail2(&txn->req, &txn->hdr_idx, replace->area, replace->data) < 0) { |
Tim Duesterhus | 3fd1973 | 2018-05-27 20:35:08 +0200 | [diff] [blame] | 2191 | static unsigned char rate_limit = 0; |
| 2192 | |
| 2193 | if ((rate_limit++ & 255) == 0) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2194 | replace->area[rule->arg.hdr_add.name_len] = 0; |
| 2195 | send_log(px, LOG_WARNING, "Proxy %s failed to add or set the request header '%s' for request #%u. You might need to increase tune.maxrewrite.", px->id, |
| 2196 | replace->area, s->uniq_id); |
Tim Duesterhus | 3fd1973 | 2018-05-27 20:35:08 +0200 | [diff] [blame] | 2197 | } |
| 2198 | |
| 2199 | HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1); |
| 2200 | if (sess->fe != s->be) |
| 2201 | HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1); |
| 2202 | if (sess->listener->counters) |
| 2203 | HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1); |
| 2204 | } |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2205 | |
| 2206 | free_trash_chunk(replace); |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 2207 | break; |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2208 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2209 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2210 | case ACT_HTTP_DEL_ACL: |
| 2211 | case ACT_HTTP_DEL_MAP: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2212 | struct pat_ref *ref; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 2213 | struct buffer *key; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2214 | |
| 2215 | /* collect reference */ |
| 2216 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 2217 | if (!ref) |
| 2218 | continue; |
| 2219 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2220 | /* allocate key */ |
| 2221 | key = alloc_trash_chunk(); |
| 2222 | if (!key) |
| 2223 | return HTTP_RULE_RES_BADREQ; |
| 2224 | |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2225 | /* collect key */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2226 | key->data = build_logline(s, key->area, key->size, |
| 2227 | &rule->arg.map.key); |
| 2228 | key->area[key->data] = '\0'; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2229 | |
| 2230 | /* perform update */ |
| 2231 | /* returned code: 1=ok, 0=ko */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2232 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2233 | pat_ref_delete(ref, key->area); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2234 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2235 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2236 | free_trash_chunk(key); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2237 | break; |
| 2238 | } |
| 2239 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2240 | case ACT_HTTP_ADD_ACL: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2241 | struct pat_ref *ref; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 2242 | struct buffer *key; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2243 | |
| 2244 | /* collect reference */ |
| 2245 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 2246 | if (!ref) |
| 2247 | continue; |
| 2248 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2249 | /* allocate key */ |
| 2250 | key = alloc_trash_chunk(); |
| 2251 | if (!key) |
| 2252 | return HTTP_RULE_RES_BADREQ; |
| 2253 | |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2254 | /* collect key */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2255 | key->data = build_logline(s, key->area, key->size, |
| 2256 | &rule->arg.map.key); |
| 2257 | key->area[key->data] = '\0'; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2258 | |
| 2259 | /* perform update */ |
| 2260 | /* add entry only if it does not already exist */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2261 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2262 | if (pat_ref_find_elt(ref, key->area) == NULL) |
| 2263 | pat_ref_add(ref, key->area, NULL, NULL); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2264 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2265 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2266 | free_trash_chunk(key); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2267 | break; |
| 2268 | } |
| 2269 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2270 | case ACT_HTTP_SET_MAP: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2271 | struct pat_ref *ref; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 2272 | struct buffer *key, *value; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2273 | |
| 2274 | /* collect reference */ |
| 2275 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 2276 | if (!ref) |
| 2277 | continue; |
| 2278 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2279 | /* allocate key */ |
| 2280 | key = alloc_trash_chunk(); |
| 2281 | if (!key) |
| 2282 | return HTTP_RULE_RES_BADREQ; |
| 2283 | |
| 2284 | /* allocate value */ |
| 2285 | value = alloc_trash_chunk(); |
| 2286 | if (!value) { |
| 2287 | free_trash_chunk(key); |
| 2288 | return HTTP_RULE_RES_BADREQ; |
| 2289 | } |
| 2290 | |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2291 | /* collect key */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2292 | key->data = build_logline(s, key->area, key->size, |
| 2293 | &rule->arg.map.key); |
| 2294 | key->area[key->data] = '\0'; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2295 | |
| 2296 | /* collect value */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2297 | value->data = build_logline(s, value->area, |
| 2298 | value->size, |
| 2299 | &rule->arg.map.value); |
| 2300 | value->area[value->data] = '\0'; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2301 | |
| 2302 | /* perform update */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2303 | if (pat_ref_find_elt(ref, key->area) != NULL) |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2304 | /* update entry if it exists */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2305 | pat_ref_set(ref, key->area, value->area, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2306 | else |
| 2307 | /* insert a new entry */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2308 | pat_ref_add(ref, key->area, value->area, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2309 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2310 | free_trash_chunk(key); |
| 2311 | free_trash_chunk(value); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2312 | break; |
| 2313 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 2314 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 2315 | case ACT_CUSTOM: |
Willy Tarreau | 7aa15b0 | 2017-12-20 16:56:50 +0100 | [diff] [blame] | 2316 | if ((s->req.flags & CF_READ_ERROR) || |
| 2317 | ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) && |
| 2318 | !(s->si[0].flags & SI_FL_CLEAN_ABRT) && |
| 2319 | (px->options & PR_O_ABRT_CLOSE))) |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 2320 | act_flags |= ACT_FLAG_FINAL; |
Willy Tarreau | 3945868 | 2015-09-27 10:33:15 +0200 | [diff] [blame] | 2321 | |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 2322 | switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) { |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 2323 | case ACT_RET_ERR: |
| 2324 | case ACT_RET_CONT: |
| 2325 | break; |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 2326 | case ACT_RET_STOP: |
| 2327 | return HTTP_RULE_RES_DONE; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 2328 | case ACT_RET_YIELD: |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 2329 | s->current_rule = rule; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 2330 | return HTTP_RULE_RES_YIELD; |
| 2331 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 2332 | break; |
| 2333 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2334 | case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX: |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 2335 | /* Note: only the first valid tracking parameter of each |
| 2336 | * applies. |
| 2337 | */ |
| 2338 | |
Christopher Faulet | 4fce0d8 | 2017-09-18 11:57:31 +0200 | [diff] [blame] | 2339 | if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) { |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 2340 | struct stktable *t; |
| 2341 | struct stksess *ts; |
| 2342 | struct stktable_key *key; |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 2343 | void *ptr1, *ptr2; |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 2344 | |
Thierry FOURNIER | 5ec63e0 | 2015-08-04 09:09:48 +0200 | [diff] [blame] | 2345 | t = rule->arg.trk_ctr.table.t; |
| 2346 | key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_REQ | SMP_OPT_FINAL, rule->arg.trk_ctr.expr, NULL); |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 2347 | |
| 2348 | if (key && (ts = stktable_get_entry(t, key))) { |
Christopher Faulet | 4fce0d8 | 2017-09-18 11:57:31 +0200 | [diff] [blame] | 2349 | stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts); |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 2350 | |
| 2351 | /* let's count a new HTTP request as it's the first time we do it */ |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 2352 | ptr1 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT); |
| 2353 | ptr2 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE); |
| 2354 | if (ptr1 || ptr2) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2355 | HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock); |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 2356 | |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 2357 | if (ptr1) |
| 2358 | stktable_data_cast(ptr1, http_req_cnt)++; |
| 2359 | |
| 2360 | if (ptr2) |
| 2361 | update_freq_ctr_period(&stktable_data_cast(ptr2, http_req_rate), |
| 2362 | t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1); |
| 2363 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2364 | HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock); |
Emeric Brun | 0fed0b0 | 2017-11-29 16:15:07 +0100 | [diff] [blame] | 2365 | |
| 2366 | /* If data was modified, we need to touch to re-schedule sync */ |
| 2367 | stktable_touch_local(t, ts, 0); |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 2368 | } |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 2369 | |
Christopher Faulet | 4fce0d8 | 2017-09-18 11:57:31 +0200 | [diff] [blame] | 2370 | stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT); |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 2371 | if (sess->fe != s->be) |
Christopher Faulet | 4fce0d8 | 2017-09-18 11:57:31 +0200 | [diff] [blame] | 2372 | stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND); |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 2373 | } |
| 2374 | } |
Adis Nezirovic | 2fbcafc | 2015-07-06 15:44:30 +0200 | [diff] [blame] | 2375 | break; |
| 2376 | |
Thierry FOURNIER | 22e4901 | 2015-08-05 19:13:48 +0200 | [diff] [blame] | 2377 | /* other flags exists, but normaly, they never be matched. */ |
| 2378 | default: |
| 2379 | break; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2380 | } |
| 2381 | } |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 2382 | |
| 2383 | /* we reached the end of the rules, nothing to report */ |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 2384 | return HTTP_RULE_RES_CONT; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2385 | } |
| 2386 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2387 | |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 2388 | /* Executes the http-response rules <rules> for stream <s> and proxy <px>. It |
| 2389 | * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP, |
| 2390 | * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT |
| 2391 | * is returned, the process can continue the evaluation of next rule list. If |
| 2392 | * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ |
| 2393 | * is returned, it means the operation could not be processed and a server error |
| 2394 | * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a |
| 2395 | * deny rule. If *YIELD is returned, the caller must call again the function |
| 2396 | * with the same context. |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2397 | */ |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 2398 | static enum rule_result |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 2399 | http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s) |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2400 | { |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 2401 | struct session *sess = strm_sess(s); |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 2402 | struct http_txn *txn = s->txn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2403 | struct connection *cli_conn; |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 2404 | struct act_rule *rule; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2405 | struct hdr_ctx ctx; |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 2406 | int act_flags = 0; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2407 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 2408 | /* If "the current_rule_list" match the executed rule list, we are in |
| 2409 | * resume condition. If a resume is needed it is always in the action |
| 2410 | * and never in the ACL or converters. In this case, we initialise the |
| 2411 | * current rule, and go to the action execution point. |
| 2412 | */ |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 2413 | if (s->current_rule) { |
| 2414 | rule = s->current_rule; |
| 2415 | s->current_rule = NULL; |
| 2416 | if (s->current_rule_list == rules) |
| 2417 | goto resume_execution; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 2418 | } |
| 2419 | s->current_rule_list = rules; |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 2420 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2421 | list_for_each_entry(rule, rules, list) { |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2422 | |
| 2423 | /* check optional condition */ |
| 2424 | if (rule->cond) { |
| 2425 | int ret; |
| 2426 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 2427 | ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL); |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2428 | ret = acl_pass(ret); |
| 2429 | |
| 2430 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 2431 | ret = !ret; |
| 2432 | |
| 2433 | if (!ret) /* condition not matched */ |
| 2434 | continue; |
| 2435 | } |
| 2436 | |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 2437 | act_flags |= ACT_FLAG_FIRST; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 2438 | resume_execution: |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2439 | switch (rule->action) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2440 | case ACT_ACTION_ALLOW: |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 2441 | return HTTP_RULE_RES_STOP; /* "allow" rules are OK */ |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2442 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2443 | case ACT_ACTION_DENY: |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2444 | txn->flags |= TX_SVDENY; |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 2445 | return HTTP_RULE_RES_STOP; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2446 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2447 | case ACT_HTTP_SET_NICE: |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 2448 | s->task->nice = rule->arg.nice; |
| 2449 | break; |
| 2450 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2451 | case ACT_HTTP_SET_TOS: |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 2452 | if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 2453 | inet_set_tos(cli_conn->handle.fd, &cli_conn->addr.from, rule->arg.tos); |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 2454 | break; |
| 2455 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2456 | case ACT_HTTP_SET_MARK: |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 2457 | #ifdef SO_MARK |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 2458 | if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 2459 | setsockopt(cli_conn->handle.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark)); |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 2460 | #endif |
| 2461 | break; |
| 2462 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2463 | case ACT_HTTP_SET_LOGL: |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 2464 | s->logs.level = rule->arg.loglevel; |
| 2465 | break; |
| 2466 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2467 | case ACT_HTTP_REPLACE_HDR: |
| 2468 | case ACT_HTTP_REPLACE_VAL: |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 2469 | if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name, |
| 2470 | rule->arg.hdr_add.name_len, |
| 2471 | &rule->arg.hdr_add.fmt, |
| 2472 | &rule->arg.hdr_add.re, rule->action)) |
Christopher Faulet | cdade94 | 2017-02-08 12:41:31 +0100 | [diff] [blame] | 2473 | return HTTP_RULE_RES_BADREQ; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 2474 | break; |
| 2475 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2476 | case ACT_HTTP_DEL_HDR: |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2477 | ctx.idx = 0; |
| 2478 | /* remove all occurrences of the header */ |
| 2479 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 2480 | ci_head(txn->rsp.chn), &txn->hdr_idx, &ctx)) { |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2481 | http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx); |
| 2482 | } |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 2483 | break; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2484 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2485 | case ACT_HTTP_SET_HDR: |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2486 | case ACT_HTTP_ADD_HDR: { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 2487 | struct buffer *replace; |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2488 | |
| 2489 | replace = alloc_trash_chunk(); |
| 2490 | if (!replace) |
| 2491 | return HTTP_RULE_RES_BADREQ; |
| 2492 | |
| 2493 | chunk_printf(replace, "%s: ", rule->arg.hdr_add.name); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2494 | memcpy(replace->area, rule->arg.hdr_add.name, |
| 2495 | rule->arg.hdr_add.name_len); |
| 2496 | replace->data = rule->arg.hdr_add.name_len; |
| 2497 | replace->area[replace->data++] = ':'; |
| 2498 | replace->area[replace->data++] = ' '; |
| 2499 | replace->data += build_logline(s, |
| 2500 | replace->area + replace->data, |
| 2501 | replace->size - replace->data, |
| 2502 | &rule->arg.hdr_add.fmt); |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 2503 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2504 | if (rule->action == ACT_HTTP_SET_HDR) { |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 2505 | /* remove all occurrences of the header */ |
| 2506 | ctx.idx = 0; |
| 2507 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 2508 | ci_head(txn->rsp.chn), &txn->hdr_idx, &ctx)) { |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 2509 | http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx); |
| 2510 | } |
| 2511 | } |
Tim Duesterhus | 3fd1973 | 2018-05-27 20:35:08 +0200 | [diff] [blame] | 2512 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2513 | if (http_header_add_tail2(&txn->rsp, &txn->hdr_idx, replace->area, replace->data) < 0) { |
Tim Duesterhus | 3fd1973 | 2018-05-27 20:35:08 +0200 | [diff] [blame] | 2514 | static unsigned char rate_limit = 0; |
| 2515 | |
| 2516 | if ((rate_limit++ & 255) == 0) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2517 | replace->area[rule->arg.hdr_add.name_len] = 0; |
| 2518 | send_log(px, LOG_WARNING, "Proxy %s failed to add or set the response header '%s' for request #%u. You might need to increase tune.maxrewrite.", px->id, |
| 2519 | replace->area, s->uniq_id); |
Tim Duesterhus | 3fd1973 | 2018-05-27 20:35:08 +0200 | [diff] [blame] | 2520 | } |
| 2521 | |
| 2522 | HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1); |
| 2523 | if (sess->fe != s->be) |
| 2524 | HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1); |
| 2525 | if (sess->listener->counters) |
| 2526 | HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1); |
| 2527 | if (objt_server(s->target)) |
| 2528 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_rewrites, 1); |
| 2529 | } |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2530 | |
| 2531 | free_trash_chunk(replace); |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2532 | break; |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2533 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2534 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2535 | case ACT_HTTP_DEL_ACL: |
| 2536 | case ACT_HTTP_DEL_MAP: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2537 | struct pat_ref *ref; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 2538 | struct buffer *key; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2539 | |
| 2540 | /* collect reference */ |
| 2541 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 2542 | if (!ref) |
| 2543 | continue; |
| 2544 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2545 | /* allocate key */ |
| 2546 | key = alloc_trash_chunk(); |
| 2547 | if (!key) |
| 2548 | return HTTP_RULE_RES_BADREQ; |
| 2549 | |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2550 | /* collect key */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2551 | key->data = build_logline(s, key->area, key->size, |
| 2552 | &rule->arg.map.key); |
| 2553 | key->area[key->data] = '\0'; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2554 | |
| 2555 | /* perform update */ |
| 2556 | /* returned code: 1=ok, 0=ko */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2557 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2558 | pat_ref_delete(ref, key->area); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2559 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2560 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2561 | free_trash_chunk(key); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2562 | break; |
| 2563 | } |
| 2564 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2565 | case ACT_HTTP_ADD_ACL: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2566 | struct pat_ref *ref; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 2567 | struct buffer *key; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2568 | |
| 2569 | /* collect reference */ |
| 2570 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 2571 | if (!ref) |
| 2572 | continue; |
| 2573 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2574 | /* allocate key */ |
| 2575 | key = alloc_trash_chunk(); |
| 2576 | if (!key) |
| 2577 | return HTTP_RULE_RES_BADREQ; |
| 2578 | |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2579 | /* collect key */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2580 | key->data = build_logline(s, key->area, key->size, |
| 2581 | &rule->arg.map.key); |
| 2582 | key->area[key->data] = '\0'; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2583 | |
| 2584 | /* perform update */ |
| 2585 | /* check if the entry already exists */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2586 | if (pat_ref_find_elt(ref, key->area) == NULL) |
| 2587 | pat_ref_add(ref, key->area, NULL, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2588 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2589 | free_trash_chunk(key); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2590 | break; |
| 2591 | } |
| 2592 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2593 | case ACT_HTTP_SET_MAP: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2594 | struct pat_ref *ref; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 2595 | struct buffer *key, *value; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2596 | |
| 2597 | /* collect reference */ |
| 2598 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 2599 | if (!ref) |
| 2600 | continue; |
| 2601 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2602 | /* allocate key */ |
| 2603 | key = alloc_trash_chunk(); |
| 2604 | if (!key) |
| 2605 | return HTTP_RULE_RES_BADREQ; |
| 2606 | |
| 2607 | /* allocate value */ |
| 2608 | value = alloc_trash_chunk(); |
| 2609 | if (!value) { |
| 2610 | free_trash_chunk(key); |
| 2611 | return HTTP_RULE_RES_BADREQ; |
| 2612 | } |
| 2613 | |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2614 | /* collect key */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2615 | key->data = build_logline(s, key->area, key->size, |
| 2616 | &rule->arg.map.key); |
| 2617 | key->area[key->data] = '\0'; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2618 | |
| 2619 | /* collect value */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2620 | value->data = build_logline(s, value->area, |
| 2621 | value->size, |
| 2622 | &rule->arg.map.value); |
| 2623 | value->area[value->data] = '\0'; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2624 | |
| 2625 | /* perform update */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2626 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2627 | if (pat_ref_find_elt(ref, key->area) != NULL) |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2628 | /* update entry if it exists */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2629 | pat_ref_set(ref, key->area, value->area, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2630 | else |
| 2631 | /* insert a new entry */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2632 | pat_ref_add(ref, key->area, value->area, NULL); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2633 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2634 | free_trash_chunk(key); |
| 2635 | free_trash_chunk(value); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2636 | break; |
| 2637 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 2638 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2639 | case ACT_HTTP_REDIR: |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 2640 | if (!http_apply_redirect_rule(rule->arg.redir, s, txn)) |
| 2641 | return HTTP_RULE_RES_BADREQ; |
| 2642 | return HTTP_RULE_RES_DONE; |
| 2643 | |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 2644 | case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX: |
| 2645 | /* Note: only the first valid tracking parameter of each |
| 2646 | * applies. |
| 2647 | */ |
| 2648 | |
Christopher Faulet | 4fce0d8 | 2017-09-18 11:57:31 +0200 | [diff] [blame] | 2649 | if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) { |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 2650 | struct stktable *t; |
| 2651 | struct stksess *ts; |
| 2652 | struct stktable_key *key; |
| 2653 | void *ptr; |
| 2654 | |
| 2655 | t = rule->arg.trk_ctr.table.t; |
| 2656 | key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_RES | SMP_OPT_FINAL, rule->arg.trk_ctr.expr, NULL); |
| 2657 | |
| 2658 | if (key && (ts = stktable_get_entry(t, key))) { |
Christopher Faulet | 4fce0d8 | 2017-09-18 11:57:31 +0200 | [diff] [blame] | 2659 | stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts); |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 2660 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2661 | HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock); |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 2662 | |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 2663 | /* let's count a new HTTP request as it's the first time we do it */ |
| 2664 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT); |
| 2665 | if (ptr) |
| 2666 | stktable_data_cast(ptr, http_req_cnt)++; |
| 2667 | |
| 2668 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE); |
| 2669 | if (ptr) |
| 2670 | update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate), |
| 2671 | t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1); |
| 2672 | |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 2673 | /* When the client triggers a 4xx from the server, it's most often due |
| 2674 | * to a missing object or permission. These events should be tracked |
| 2675 | * because if they happen often, it may indicate a brute force or a |
| 2676 | * vulnerability scan. Normally this is done when receiving the response |
| 2677 | * but here we're tracking after this ought to have been done so we have |
| 2678 | * to do it on purpose. |
| 2679 | */ |
Willy Tarreau | 3146a4c | 2016-07-26 15:22:33 +0200 | [diff] [blame] | 2680 | if ((unsigned)(txn->status - 400) < 100) { |
| 2681 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT); |
| 2682 | if (ptr) |
| 2683 | stktable_data_cast(ptr, http_err_cnt)++; |
| 2684 | |
| 2685 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE); |
| 2686 | if (ptr) |
| 2687 | update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate), |
| 2688 | t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1); |
| 2689 | } |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 2690 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2691 | HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock); |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 2692 | |
Emeric Brun | 0fed0b0 | 2017-11-29 16:15:07 +0100 | [diff] [blame] | 2693 | /* If data was modified, we need to touch to re-schedule sync */ |
| 2694 | stktable_touch_local(t, ts, 0); |
| 2695 | |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 2696 | stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT); |
| 2697 | if (sess->fe != s->be) |
| 2698 | stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND); |
| 2699 | |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 2700 | } |
| 2701 | } |
| 2702 | break; |
| 2703 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 2704 | case ACT_CUSTOM: |
Willy Tarreau | 7aa15b0 | 2017-12-20 16:56:50 +0100 | [diff] [blame] | 2705 | if ((s->req.flags & CF_READ_ERROR) || |
| 2706 | ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) && |
| 2707 | !(s->si[0].flags & SI_FL_CLEAN_ABRT) && |
| 2708 | (px->options & PR_O_ABRT_CLOSE))) |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 2709 | act_flags |= ACT_FLAG_FINAL; |
Willy Tarreau | 3945868 | 2015-09-27 10:33:15 +0200 | [diff] [blame] | 2710 | |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 2711 | switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) { |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 2712 | case ACT_RET_ERR: |
| 2713 | case ACT_RET_CONT: |
| 2714 | break; |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 2715 | case ACT_RET_STOP: |
| 2716 | return HTTP_RULE_RES_STOP; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 2717 | case ACT_RET_YIELD: |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 2718 | s->current_rule = rule; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 2719 | return HTTP_RULE_RES_YIELD; |
| 2720 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 2721 | break; |
| 2722 | |
Thierry FOURNIER | 22e4901 | 2015-08-05 19:13:48 +0200 | [diff] [blame] | 2723 | /* other flags exists, but normaly, they never be matched. */ |
| 2724 | default: |
| 2725 | break; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2726 | } |
| 2727 | } |
| 2728 | |
| 2729 | /* we reached the end of the rules, nothing to report */ |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 2730 | return HTTP_RULE_RES_CONT; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2731 | } |
| 2732 | |
| 2733 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2734 | /* Perform an HTTP redirect based on the information in <rule>. The function |
| 2735 | * returns non-zero on success, or zero in case of a, irrecoverable error such |
| 2736 | * as too large a request to build a valid response. |
| 2737 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2738 | static int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn) |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2739 | { |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 2740 | struct http_msg *req = &txn->req; |
| 2741 | struct http_msg *res = &txn->rsp; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2742 | const char *msg_fmt; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 2743 | struct buffer *chunk; |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 2744 | int ret = 0; |
| 2745 | |
| 2746 | chunk = alloc_trash_chunk(); |
| 2747 | if (!chunk) |
| 2748 | goto leave; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2749 | |
| 2750 | /* build redirect message */ |
| 2751 | switch(rule->code) { |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 2752 | case 308: |
| 2753 | msg_fmt = HTTP_308; |
| 2754 | break; |
| 2755 | case 307: |
| 2756 | msg_fmt = HTTP_307; |
| 2757 | break; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2758 | case 303: |
| 2759 | msg_fmt = HTTP_303; |
| 2760 | break; |
| 2761 | case 301: |
| 2762 | msg_fmt = HTTP_301; |
| 2763 | break; |
| 2764 | case 302: |
| 2765 | default: |
| 2766 | msg_fmt = HTTP_302; |
| 2767 | break; |
| 2768 | } |
| 2769 | |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 2770 | if (unlikely(!chunk_strcpy(chunk, msg_fmt))) |
| 2771 | goto leave; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2772 | |
| 2773 | switch(rule->type) { |
| 2774 | case REDIRECT_TYPE_SCHEME: { |
| 2775 | const char *path; |
| 2776 | const char *host; |
| 2777 | struct hdr_ctx ctx; |
| 2778 | int pathlen; |
| 2779 | int hostlen; |
| 2780 | |
| 2781 | host = ""; |
| 2782 | hostlen = 0; |
| 2783 | ctx.idx = 0; |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 2784 | if (http_find_header2("Host", 4, ci_head(req->chn), &txn->hdr_idx, &ctx)) { |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2785 | host = ctx.line + ctx.val; |
| 2786 | hostlen = ctx.vlen; |
| 2787 | } |
| 2788 | |
Willy Tarreau | 6b952c8 | 2018-09-10 17:45:34 +0200 | [diff] [blame] | 2789 | path = http_txn_get_path(txn); |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2790 | /* build message using path */ |
| 2791 | if (path) { |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 2792 | pathlen = req->sl.rq.u_l + (ci_head(req->chn) + req->sl.rq.u) - path; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2793 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 2794 | int qs = 0; |
| 2795 | while (qs < pathlen) { |
| 2796 | if (path[qs] == '?') { |
| 2797 | pathlen = qs; |
| 2798 | break; |
| 2799 | } |
| 2800 | qs++; |
| 2801 | } |
| 2802 | } |
| 2803 | } else { |
| 2804 | path = "/"; |
| 2805 | pathlen = 1; |
| 2806 | } |
| 2807 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 2808 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
| 2809 | /* check if we can add scheme + "://" + host + path */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2810 | if (chunk->data + rule->rdr_len + 3 + hostlen + pathlen > chunk->size - 4) |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 2811 | goto leave; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2812 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 2813 | /* add scheme */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2814 | memcpy(chunk->area + chunk->data, rule->rdr_str, |
| 2815 | rule->rdr_len); |
| 2816 | chunk->data += rule->rdr_len; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 2817 | } |
| 2818 | else { |
| 2819 | /* add scheme with executing log format */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2820 | chunk->data += build_logline(s, |
| 2821 | chunk->area + chunk->data, |
| 2822 | chunk->size - chunk->data, |
| 2823 | &rule->rdr_fmt); |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2824 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 2825 | /* check if we can add scheme + "://" + host + path */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2826 | if (chunk->data + 3 + hostlen + pathlen > chunk->size - 4) |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 2827 | goto leave; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 2828 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2829 | /* add "://" */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2830 | memcpy(chunk->area + chunk->data, "://", 3); |
| 2831 | chunk->data += 3; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2832 | |
| 2833 | /* add host */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2834 | memcpy(chunk->area + chunk->data, host, hostlen); |
| 2835 | chunk->data += hostlen; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2836 | |
| 2837 | /* add path */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2838 | memcpy(chunk->area + chunk->data, path, pathlen); |
| 2839 | chunk->data += pathlen; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2840 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 2841 | /* append a slash at the end of the location if needed and missing */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2842 | if (chunk->data && chunk->area[chunk->data - 1] != '/' && |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2843 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2844 | if (chunk->data > chunk->size - 5) |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 2845 | goto leave; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2846 | chunk->area[chunk->data] = '/'; |
| 2847 | chunk->data++; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2848 | } |
| 2849 | |
| 2850 | break; |
| 2851 | } |
| 2852 | case REDIRECT_TYPE_PREFIX: { |
| 2853 | const char *path; |
| 2854 | int pathlen; |
| 2855 | |
Willy Tarreau | 6b952c8 | 2018-09-10 17:45:34 +0200 | [diff] [blame] | 2856 | path = http_txn_get_path(txn); |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2857 | /* build message using path */ |
| 2858 | if (path) { |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 2859 | pathlen = req->sl.rq.u_l + (ci_head(req->chn) + req->sl.rq.u) - path; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2860 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 2861 | int qs = 0; |
| 2862 | while (qs < pathlen) { |
| 2863 | if (path[qs] == '?') { |
| 2864 | pathlen = qs; |
| 2865 | break; |
| 2866 | } |
| 2867 | qs++; |
| 2868 | } |
| 2869 | } |
| 2870 | } else { |
| 2871 | path = "/"; |
| 2872 | pathlen = 1; |
| 2873 | } |
| 2874 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 2875 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2876 | if (chunk->data + rule->rdr_len + pathlen > chunk->size - 4) |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 2877 | goto leave; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2878 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 2879 | /* add prefix. Note that if prefix == "/", we don't want to |
| 2880 | * add anything, otherwise it makes it hard for the user to |
| 2881 | * configure a self-redirection. |
| 2882 | */ |
| 2883 | if (rule->rdr_len != 1 || *rule->rdr_str != '/') { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2884 | memcpy(chunk->area + chunk->data, |
| 2885 | rule->rdr_str, rule->rdr_len); |
| 2886 | chunk->data += rule->rdr_len; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 2887 | } |
| 2888 | } |
| 2889 | else { |
| 2890 | /* add prefix with executing log format */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2891 | chunk->data += build_logline(s, |
| 2892 | chunk->area + chunk->data, |
| 2893 | chunk->size - chunk->data, |
| 2894 | &rule->rdr_fmt); |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 2895 | |
| 2896 | /* Check length */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2897 | if (chunk->data + pathlen > chunk->size - 4) |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 2898 | goto leave; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2899 | } |
| 2900 | |
| 2901 | /* add path */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2902 | memcpy(chunk->area + chunk->data, path, pathlen); |
| 2903 | chunk->data += pathlen; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2904 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 2905 | /* append a slash at the end of the location if needed and missing */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2906 | if (chunk->data && chunk->area[chunk->data - 1] != '/' && |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2907 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2908 | if (chunk->data > chunk->size - 5) |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 2909 | goto leave; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2910 | chunk->area[chunk->data] = '/'; |
| 2911 | chunk->data++; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2912 | } |
| 2913 | |
| 2914 | break; |
| 2915 | } |
| 2916 | case REDIRECT_TYPE_LOCATION: |
| 2917 | default: |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 2918 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2919 | if (chunk->data + rule->rdr_len > chunk->size - 4) |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 2920 | goto leave; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 2921 | |
| 2922 | /* add location */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2923 | memcpy(chunk->area + chunk->data, rule->rdr_str, |
| 2924 | rule->rdr_len); |
| 2925 | chunk->data += rule->rdr_len; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 2926 | } |
| 2927 | else { |
| 2928 | /* add location with executing log format */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2929 | chunk->data += build_logline(s, |
| 2930 | chunk->area + chunk->data, |
| 2931 | chunk->size - chunk->data, |
| 2932 | &rule->rdr_fmt); |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2933 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 2934 | /* Check left length */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2935 | if (chunk->data > chunk->size - 4) |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 2936 | goto leave; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 2937 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2938 | break; |
| 2939 | } |
| 2940 | |
| 2941 | if (rule->cookie_len) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2942 | memcpy(chunk->area + chunk->data, "\r\nSet-Cookie: ", 14); |
| 2943 | chunk->data += 14; |
| 2944 | memcpy(chunk->area + chunk->data, rule->cookie_str, |
| 2945 | rule->cookie_len); |
| 2946 | chunk->data += rule->cookie_len; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2947 | } |
| 2948 | |
Willy Tarreau | 19b1412 | 2017-02-28 09:48:11 +0100 | [diff] [blame] | 2949 | /* add end of headers and the keep-alive/close status. */ |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2950 | txn->status = rule->code; |
| 2951 | /* let's log the request time */ |
| 2952 | s->logs.tv_request = now; |
| 2953 | |
Christopher Faulet | be821b9 | 2017-03-30 11:21:53 +0200 | [diff] [blame] | 2954 | if (((!(req->flags & HTTP_MSGF_TE_CHNK) && !req->body_len) || (req->msg_state == HTTP_MSG_DONE)) && |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2955 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL || |
| 2956 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) { |
| 2957 | /* keep-alive possible */ |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 2958 | if (!(req->flags & HTTP_MSGF_VER_11)) { |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2959 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2960 | memcpy(chunk->area + chunk->data, |
| 2961 | "\r\nProxy-Connection: keep-alive", 30); |
| 2962 | chunk->data += 30; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2963 | } else { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2964 | memcpy(chunk->area + chunk->data, |
| 2965 | "\r\nConnection: keep-alive", 24); |
| 2966 | chunk->data += 24; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2967 | } |
| 2968 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2969 | memcpy(chunk->area + chunk->data, "\r\n\r\n", 4); |
| 2970 | chunk->data += 4; |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 2971 | FLT_STRM_CB(s, flt_http_reply(s, txn->status, chunk)); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2972 | co_inject(res->chn, chunk->area, chunk->data); |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2973 | /* "eat" the request */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2974 | b_del(&req->chn->buf, req->sov); |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 2975 | req->next -= req->sov; |
| 2976 | req->sov = 0; |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 2977 | s->req.analysers = AN_REQ_HTTP_XFER_BODY | (s->req.analysers & AN_REQ_FLT_END); |
Christopher Faulet | 014e39c | 2017-03-10 13:52:30 +0100 | [diff] [blame] | 2978 | s->res.analysers = AN_RES_HTTP_XFER_BODY | (s->res.analysers & AN_RES_FLT_END); |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 2979 | req->msg_state = HTTP_MSG_CLOSED; |
| 2980 | res->msg_state = HTTP_MSG_DONE; |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 2981 | /* Trim any possible response */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2982 | b_set_data(&res->chn->buf, co_data(res->chn)); |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 2983 | res->next = res->sov = 0; |
Christopher Faulet | 5d468ca | 2017-09-11 09:27:29 +0200 | [diff] [blame] | 2984 | /* let the server side turn to SI_ST_CLO */ |
| 2985 | channel_shutw_now(req->chn); |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2986 | } else { |
| 2987 | /* keep-alive not possible */ |
| 2988 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2989 | memcpy(chunk->area + chunk->data, |
| 2990 | "\r\nProxy-Connection: close\r\n\r\n", 29); |
| 2991 | chunk->data += 29; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2992 | } else { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2993 | memcpy(chunk->area + chunk->data, |
| 2994 | "\r\nConnection: close\r\n\r\n", 23); |
| 2995 | chunk->data += 23; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2996 | } |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 2997 | http_reply_and_close(s, txn->status, chunk); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 2998 | req->chn->analysers &= AN_REQ_FLT_END; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2999 | } |
| 3000 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3001 | if (!(s->flags & SF_ERR_MASK)) |
| 3002 | s->flags |= SF_ERR_LOCAL; |
| 3003 | if (!(s->flags & SF_FINST_MASK)) |
| 3004 | s->flags |= SF_FINST_R; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3005 | |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3006 | ret = 1; |
| 3007 | leave: |
| 3008 | free_trash_chunk(chunk); |
| 3009 | return ret; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3010 | } |
| 3011 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3012 | /* This stream analyser runs all HTTP request processing which is common to |
| 3013 | * frontends and backends, which means blocking ACLs, filters, connection-close, |
| 3014 | * reqadd, stats and redirects. This is performed for the designated proxy. |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3015 | * It returns 1 if the processing can continue on next analysers, or zero if it |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3016 | * either needs more data or wants to immediately abort the request (eg: deny, |
| 3017 | * error, ...). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3018 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3019 | int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px) |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3020 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 3021 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3022 | struct http_txn *txn = s->txn; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3023 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3024 | struct redirect_rule *rule; |
Willy Tarreau | f4f0412 | 2010-01-28 18:10:50 +0100 | [diff] [blame] | 3025 | struct cond_wordlist *wl; |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3026 | enum rule_result verdict; |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 3027 | int deny_status = HTTP_ERR_403; |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 3028 | struct connection *conn = objt_conn(sess->origin); |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3029 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 3030 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3031 | /* we need more data */ |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3032 | goto return_prx_yield; |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3033 | } |
| 3034 | |
Christopher Faulet | 4507351 | 2018-07-20 10:16:29 +0200 | [diff] [blame] | 3035 | DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%lu analysers=%02x\n", |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3036 | now_ms, __FUNCTION__, |
| 3037 | s, |
| 3038 | req, |
| 3039 | req->rex, req->wex, |
| 3040 | req->flags, |
Christopher Faulet | 4507351 | 2018-07-20 10:16:29 +0200 | [diff] [blame] | 3041 | ci_data(req), |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3042 | req->analysers); |
| 3043 | |
Willy Tarreau | 6541083 | 2014-04-28 21:25:43 +0200 | [diff] [blame] | 3044 | /* just in case we have some per-backend tracking */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3045 | stream_inc_be_http_req_ctr(s); |
Willy Tarreau | 6541083 | 2014-04-28 21:25:43 +0200 | [diff] [blame] | 3046 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3047 | /* evaluate http-request rules */ |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3048 | if (!LIST_ISEMPTY(&px->http_req_rules)) { |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 3049 | verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s, &deny_status); |
Willy Tarreau | 5142594 | 2010-02-01 10:40:19 +0100 | [diff] [blame] | 3050 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3051 | switch (verdict) { |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3052 | case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */ |
| 3053 | goto return_prx_yield; |
| 3054 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3055 | case HTTP_RULE_RES_CONT: |
| 3056 | case HTTP_RULE_RES_STOP: /* nothing to do */ |
| 3057 | break; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 3058 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3059 | case HTTP_RULE_RES_DENY: /* deny or tarpit */ |
| 3060 | if (txn->flags & TX_CLTARPIT) |
| 3061 | goto tarpit; |
| 3062 | goto deny; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 3063 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3064 | case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */ |
| 3065 | goto return_prx_cond; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 3066 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3067 | case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */ |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 3068 | goto done; |
| 3069 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3070 | case HTTP_RULE_RES_BADREQ: /* failed with a bad request */ |
| 3071 | goto return_bad_req; |
| 3072 | } |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3073 | } |
| 3074 | |
Olivier Houchard | 25ae45a | 2017-11-29 19:51:19 +0100 | [diff] [blame] | 3075 | if (conn && (conn->flags & CO_FL_EARLY_DATA) && |
| 3076 | (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE))) { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 3077 | struct hdr_ctx ctx; |
| 3078 | |
| 3079 | ctx.idx = 0; |
| 3080 | if (!http_find_header2("Early-Data", strlen("Early-Data"), |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 3081 | ci_head(&s->req), &txn->hdr_idx, &ctx)) { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 3082 | if (unlikely(http_header_add_tail2(&txn->req, |
| 3083 | &txn->hdr_idx, "Early-Data: 1", |
Christopher Faulet | 005e79e | 2018-07-20 09:54:26 +0200 | [diff] [blame] | 3084 | strlen("Early-Data: 1")) < 0)) { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 3085 | goto return_bad_req; |
| 3086 | } |
| 3087 | } |
| 3088 | |
| 3089 | } |
| 3090 | |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 3091 | /* OK at this stage, we know that the request was accepted according to |
| 3092 | * the http-request rules, we can check for the stats. Note that the |
| 3093 | * URI is detected *before* the req* rules in order not to be affected |
| 3094 | * by a possible reqrep, while they are processed *after* so that a |
| 3095 | * reqdeny can still block them. This clearly needs to change in 1.6! |
| 3096 | */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 3097 | if (stats_check_uri(&s->si[1], txn, px)) { |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 3098 | s->target = &http_stats_applet.obj_type; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 3099 | if (unlikely(!stream_int_register_handler(&s->si[1], objt_applet(s->target)))) { |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 3100 | txn->status = 500; |
| 3101 | s->logs.tv_request = now; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 3102 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3103 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3104 | if (!(s->flags & SF_ERR_MASK)) |
| 3105 | s->flags |= SF_ERR_RESOURCE; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 3106 | goto return_prx_cond; |
| 3107 | } |
| 3108 | |
| 3109 | /* parse the whole stats request and extract the relevant information */ |
| 3110 | http_handle_stats(s, req); |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 3111 | verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, &deny_status); |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3112 | /* not all actions implemented: deny, allow, auth */ |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3113 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3114 | if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */ |
| 3115 | goto deny; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 3116 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3117 | if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */ |
| 3118 | goto return_prx_cond; |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 3119 | } |
| 3120 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3121 | /* evaluate the req* rules except reqadd */ |
| 3122 | if (px->req_exp != NULL) { |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 3123 | if (apply_filters_to_request(s, req, px) < 0) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3124 | goto return_bad_req; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3125 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3126 | if (txn->flags & TX_CLDENY) |
| 3127 | goto deny; |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 3128 | |
Jarno Huuskonen | 800d176 | 2017-03-06 14:56:36 +0200 | [diff] [blame] | 3129 | if (txn->flags & TX_CLTARPIT) { |
| 3130 | deny_status = HTTP_ERR_500; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3131 | goto tarpit; |
Jarno Huuskonen | 800d176 | 2017-03-06 14:56:36 +0200 | [diff] [blame] | 3132 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3133 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3134 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3135 | /* add request headers from the rule sets in the same order */ |
| 3136 | list_for_each_entry(wl, &px->req_add, list) { |
| 3137 | if (wl->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 3138 | int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3139 | ret = acl_pass(ret); |
| 3140 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 3141 | ret = !ret; |
| 3142 | if (!ret) |
| 3143 | continue; |
| 3144 | } |
| 3145 | |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 3146 | if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0)) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3147 | goto return_bad_req; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 3148 | } |
| 3149 | |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 3150 | |
| 3151 | /* Proceed with the stats now. */ |
William Lallemand | 71bd11a | 2017-11-20 19:13:14 +0100 | [diff] [blame] | 3152 | if (unlikely(objt_applet(s->target) == &http_stats_applet) || |
| 3153 | unlikely(objt_applet(s->target) == &http_cache_applet)) { |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3154 | /* process the stats request now */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3155 | if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */ |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 3156 | HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1); |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 3157 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3158 | if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is |
| 3159 | s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy |
| 3160 | if (!(s->flags & SF_FINST_MASK)) |
| 3161 | s->flags |= SF_FINST_R; |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 3162 | |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 3163 | /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */ |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 3164 | req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END); |
| 3165 | req->analysers &= ~AN_REQ_FLT_XFER_DATA; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 3166 | req->analysers |= AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3167 | goto done; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3168 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 3169 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3170 | /* check whether we have some ACLs set to redirect this request */ |
| 3171 | list_for_each_entry(rule, &px->redirect_rules, list) { |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 3172 | if (rule->cond) { |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3173 | int ret; |
| 3174 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 3175 | ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 3176 | ret = acl_pass(ret); |
| 3177 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 3178 | ret = !ret; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3179 | if (!ret) |
| 3180 | continue; |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 3181 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3182 | if (!http_apply_redirect_rule(rule, s, txn)) |
| 3183 | goto return_bad_req; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3184 | goto done; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3185 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 3186 | |
Willy Tarreau | 2be3939 | 2010-01-03 17:24:51 +0100 | [diff] [blame] | 3187 | /* POST requests may be accompanied with an "Expect: 100-Continue" header. |
| 3188 | * If this happens, then the data will not come immediately, so we must |
| 3189 | * send all what we have without waiting. Note that due to the small gain |
| 3190 | * in waiting for the body of the request, it's easier to simply put the |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 3191 | * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove |
Willy Tarreau | 2be3939 | 2010-01-03 17:24:51 +0100 | [diff] [blame] | 3192 | * itself once used. |
| 3193 | */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 3194 | req->flags |= CF_SEND_DONTWAIT; |
Willy Tarreau | 2be3939 | 2010-01-03 17:24:51 +0100 | [diff] [blame] | 3195 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3196 | done: /* done with this analyser, continue with next ones that the calling |
| 3197 | * points will have set, if any. |
| 3198 | */ |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3199 | req->analyse_exp = TICK_ETERNITY; |
Thierry FOURNIER | 7566e30 | 2014-08-22 06:55:26 +0200 | [diff] [blame] | 3200 | done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */ |
| 3201 | req->analysers &= ~an_bit; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3202 | return 1; |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 3203 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3204 | tarpit: |
Willy Tarreau | 6a0bca9 | 2017-06-11 17:56:27 +0200 | [diff] [blame] | 3205 | /* Allow cookie logging |
| 3206 | */ |
| 3207 | if (s->be->cookie_name || sess->fe->capture_name) |
| 3208 | manage_client_side_cookies(s, req); |
| 3209 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3210 | /* When a connection is tarpitted, we use the tarpit timeout, |
| 3211 | * which may be the same as the connect timeout if unspecified. |
| 3212 | * If unset, then set it to zero because we really want it to |
| 3213 | * eventually expire. We build the tarpit as an analyser. |
| 3214 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 3215 | channel_erase(&s->req); |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3216 | |
| 3217 | /* wipe the request out so that we can drop the connection early |
| 3218 | * if the client closes first. |
| 3219 | */ |
| 3220 | channel_dont_connect(req); |
Bertrand Paquet | 83a2c3d | 2016-04-06 11:58:31 +0200 | [diff] [blame] | 3221 | |
Jarno Huuskonen | 800d176 | 2017-03-06 14:56:36 +0200 | [diff] [blame] | 3222 | txn->status = http_err_codes[deny_status]; |
| 3223 | |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 3224 | req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */ |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3225 | req->analysers |= AN_REQ_HTTP_TARPIT; |
| 3226 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit); |
| 3227 | if (!req->analyse_exp) |
| 3228 | req->analyse_exp = tick_add(now_ms, 0); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3229 | stream_inc_http_err_ctr(s); |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 3230 | HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3231 | if (sess->fe != s->be) |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 3232 | HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 3233 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 3234 | HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1); |
Thierry FOURNIER | 7566e30 | 2014-08-22 06:55:26 +0200 | [diff] [blame] | 3235 | goto done_without_exp; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3236 | |
| 3237 | deny: /* this request was blocked (denied) */ |
Bertrand Paquet | 83a2c3d | 2016-04-06 11:58:31 +0200 | [diff] [blame] | 3238 | |
| 3239 | /* Allow cookie logging |
| 3240 | */ |
| 3241 | if (s->be->cookie_name || sess->fe->capture_name) |
| 3242 | manage_client_side_cookies(s, req); |
| 3243 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3244 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 3245 | txn->status = http_err_codes[deny_status]; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3246 | s->logs.tv_request = now; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 3247 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3248 | stream_inc_http_err_ctr(s); |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 3249 | HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3250 | if (sess->fe != s->be) |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 3251 | HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 3252 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 3253 | HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1); |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3254 | goto return_prx_cond; |
| 3255 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3256 | return_bad_req: |
| 3257 | /* We centralize bad requests processing here */ |
| 3258 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 3259 | /* we detected a parsing error. We want to archive this request |
| 3260 | * in the dedicated proxy area for later troubleshooting. |
| 3261 | */ |
Willy Tarreau | fd9419d | 2018-09-07 18:01:03 +0200 | [diff] [blame] | 3262 | http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe); |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3263 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 3264 | |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 3265 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3266 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3267 | txn->status = 400; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 3268 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3269 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 3270 | HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 3271 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 3272 | HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1); |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 3273 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3274 | return_prx_cond: |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3275 | if (!(s->flags & SF_ERR_MASK)) |
| 3276 | s->flags |= SF_ERR_PRXCOND; |
| 3277 | if (!(s->flags & SF_FINST_MASK)) |
| 3278 | s->flags |= SF_FINST_R; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 3279 | |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 3280 | req->analysers &= AN_REQ_FLT_END; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3281 | req->analyse_exp = TICK_ETERNITY; |
| 3282 | return 0; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3283 | |
| 3284 | return_prx_yield: |
| 3285 | channel_dont_connect(req); |
| 3286 | return 0; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3287 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3288 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3289 | /* This function performs all the processing enabled for the current request. |
| 3290 | * It returns 1 if the processing can continue on next analysers, or zero if it |
| 3291 | * needs more data, encounters an error, or wants to immediately abort the |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 3292 | * request. It relies on buffers flags, and updates s->req.analysers. |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3293 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3294 | int http_process_request(struct stream *s, struct channel *req, int an_bit) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3295 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 3296 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3297 | struct http_txn *txn = s->txn; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3298 | struct http_msg *msg = &txn->req; |
Willy Tarreau | ee335e6 | 2015-04-21 18:15:13 +0200 | [diff] [blame] | 3299 | struct connection *cli_conn = objt_conn(strm_sess(s)->origin); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3300 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 3301 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3302 | /* we need more data */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 3303 | channel_dont_connect(req); |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3304 | return 0; |
| 3305 | } |
| 3306 | |
Christopher Faulet | 4507351 | 2018-07-20 10:16:29 +0200 | [diff] [blame] | 3307 | DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%lu analysers=%02x\n", |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3308 | now_ms, __FUNCTION__, |
| 3309 | s, |
| 3310 | req, |
| 3311 | req->rex, req->wex, |
| 3312 | req->flags, |
Christopher Faulet | 4507351 | 2018-07-20 10:16:29 +0200 | [diff] [blame] | 3313 | ci_data(req), |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3314 | req->analysers); |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3315 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3316 | /* |
| 3317 | * Right now, we know that we have processed the entire headers |
| 3318 | * and that unwanted requests have been filtered out. We can do |
| 3319 | * whatever we want with the remaining request. Also, now we |
| 3320 | * may have separate values for ->fe, ->be. |
| 3321 | */ |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3322 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3323 | /* |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3324 | * If HTTP PROXY is set we simply get remote server address parsing |
| 3325 | * incoming request. Note that this requires that a connection is |
| 3326 | * allocated on the server side. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3327 | */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3328 | if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) { |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 3329 | struct connection *conn; |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 3330 | char *path; |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 3331 | |
Willy Tarreau | 9471b8c | 2013-12-15 13:31:35 +0100 | [diff] [blame] | 3332 | /* Note that for now we don't reuse existing proxy connections */ |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 3333 | if (unlikely((conn = cs_conn(si_alloc_cs(&s->si[1], NULL))) == NULL)) { |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 3334 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 3335 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3336 | txn->status = 500; |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 3337 | req->analysers &= AN_REQ_FLT_END; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 3338 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 3339 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3340 | if (!(s->flags & SF_ERR_MASK)) |
| 3341 | s->flags |= SF_ERR_RESOURCE; |
| 3342 | if (!(s->flags & SF_FINST_MASK)) |
| 3343 | s->flags |= SF_FINST_R; |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 3344 | |
| 3345 | return 0; |
| 3346 | } |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 3347 | |
Willy Tarreau | 6b952c8 | 2018-09-10 17:45:34 +0200 | [diff] [blame] | 3348 | path = http_txn_get_path(txn); |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 3349 | if (url2sa(ci_head(req) + msg->sl.rq.u, |
| 3350 | path ? path - (ci_head(req) + msg->sl.rq.u) : msg->sl.rq.u_l, |
Christopher Faulet | 11ebb20 | 2018-04-13 15:53:12 +0200 | [diff] [blame] | 3351 | &conn->addr.to, NULL) == -1) |
| 3352 | goto return_bad_req; |
| 3353 | |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 3354 | /* if the path was found, we have to remove everything between |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 3355 | * ci_head(req) + msg->sl.rq.u and path (excluded). If it was not |
| 3356 | * found, we need to replace from ci_head(req) + msg->sl.rq.u for |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 3357 | * u_l characters by a single "/". |
| 3358 | */ |
| 3359 | if (path) { |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 3360 | char *cur_ptr = ci_head(req); |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 3361 | char *cur_end = cur_ptr + txn->req.sl.rq.l; |
| 3362 | int delta; |
| 3363 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3364 | delta = b_rep_blk(&req->buf, cur_ptr + msg->sl.rq.u, path, NULL, 0); |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 3365 | http_msg_move_end(&txn->req, delta); |
| 3366 | cur_end += delta; |
| 3367 | if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL) |
| 3368 | goto return_bad_req; |
| 3369 | } |
| 3370 | else { |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 3371 | char *cur_ptr = ci_head(req); |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 3372 | char *cur_end = cur_ptr + txn->req.sl.rq.l; |
| 3373 | int delta; |
| 3374 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3375 | delta = b_rep_blk(&req->buf, cur_ptr + msg->sl.rq.u, |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 3376 | cur_ptr + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1); |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 3377 | http_msg_move_end(&txn->req, delta); |
| 3378 | cur_end += delta; |
| 3379 | if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL) |
| 3380 | goto return_bad_req; |
| 3381 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3382 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3383 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3384 | /* |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 3385 | * 7: Now we can work with the cookies. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3386 | * Note that doing so might move headers in the request, but |
| 3387 | * the fields will stay coherent and the URI will not move. |
| 3388 | * This should only be performed in the backend. |
| 3389 | */ |
Bertrand Paquet | 83a2c3d | 2016-04-06 11:58:31 +0200 | [diff] [blame] | 3390 | if (s->be->cookie_name || sess->fe->capture_name) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3391 | manage_client_side_cookies(s, req); |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 3392 | |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 3393 | /* add unique-id if "header-unique-id" is specified */ |
| 3394 | |
Thierry Fournier | f4011dd | 2016-03-29 17:23:51 +0200 | [diff] [blame] | 3395 | if (!LIST_ISEMPTY(&sess->fe->format_unique_id) && !s->unique_id) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 3396 | if ((s->unique_id = pool_alloc(pool_head_uniqueid)) == NULL) |
William Lallemand | 5b7ea3a | 2013-08-28 15:44:19 +0200 | [diff] [blame] | 3397 | goto return_bad_req; |
| 3398 | s->unique_id[0] = '\0'; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3399 | build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id); |
William Lallemand | 5b7ea3a | 2013-08-28 15:44:19 +0200 | [diff] [blame] | 3400 | } |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 3401 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3402 | if (sess->fe->header_unique_id && s->unique_id) { |
Willy Tarreau | 5f6333c | 2018-08-22 05:14:37 +0200 | [diff] [blame] | 3403 | if (chunk_printf(&trash, "%s: %s", sess->fe->header_unique_id, s->unique_id) < 0) |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 3404 | goto return_bad_req; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3405 | if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, trash.data) < 0)) |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 3406 | goto return_bad_req; |
| 3407 | } |
| 3408 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 3409 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3410 | * 9: add X-Forwarded-For if either the frontend or the backend |
| 3411 | * asks for it. |
| 3412 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3413 | if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) { |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 3414 | struct hdr_ctx ctx = { .idx = 0 }; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3415 | if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) && |
| 3416 | http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name, |
| 3417 | s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len, |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 3418 | ci_head(req), &txn->hdr_idx, &ctx)) { |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 3419 | /* The header is set to be added only if none is present |
| 3420 | * and we found it, so don't do anything. |
| 3421 | */ |
| 3422 | } |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3423 | else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3424 | /* Add an X-Forwarded-For header unless the source IP is |
| 3425 | * in the 'except' network range. |
| 3426 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3427 | if ((!sess->fe->except_mask.s_addr || |
| 3428 | (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr) |
| 3429 | != sess->fe->except_net.s_addr) && |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3430 | (!s->be->except_mask.s_addr || |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3431 | (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3432 | != s->be->except_net.s_addr)) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 3433 | int len; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3434 | unsigned char *pn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3435 | pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr; |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 3436 | |
| 3437 | /* Note: we rely on the backend to get the header name to be used for |
| 3438 | * x-forwarded-for, because the header is really meant for the backends. |
| 3439 | * However, if the backend did not specify any option, we have to rely |
| 3440 | * on the frontend's header name. |
| 3441 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3442 | if (s->be->fwdfor_hdr_len) { |
| 3443 | len = s->be->fwdfor_hdr_len; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3444 | memcpy(trash.area, |
| 3445 | s->be->fwdfor_hdr_name, len); |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 3446 | } else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3447 | len = sess->fe->fwdfor_hdr_len; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3448 | memcpy(trash.area, |
| 3449 | sess->fe->fwdfor_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 3450 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3451 | len += snprintf(trash.area + len, |
| 3452 | trash.size - len, |
| 3453 | ": %d.%d.%d.%d", pn[0], pn[1], |
| 3454 | pn[2], pn[3]); |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 3455 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3456 | if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, len) < 0)) |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3457 | goto return_bad_req; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 3458 | } |
| 3459 | } |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3460 | else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3461 | /* FIXME: for the sake of completeness, we should also support |
| 3462 | * 'except' here, although it is mostly useless in this case. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3463 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3464 | int len; |
| 3465 | char pn[INET6_ADDRSTRLEN]; |
| 3466 | inet_ntop(AF_INET6, |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3467 | (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3468 | pn, sizeof(pn)); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3469 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3470 | /* Note: we rely on the backend to get the header name to be used for |
| 3471 | * x-forwarded-for, because the header is really meant for the backends. |
| 3472 | * However, if the backend did not specify any option, we have to rely |
| 3473 | * on the frontend's header name. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3474 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3475 | if (s->be->fwdfor_hdr_len) { |
| 3476 | len = s->be->fwdfor_hdr_len; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3477 | memcpy(trash.area, s->be->fwdfor_hdr_name, |
| 3478 | len); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3479 | } else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3480 | len = sess->fe->fwdfor_hdr_len; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3481 | memcpy(trash.area, sess->fe->fwdfor_hdr_name, |
| 3482 | len); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3483 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3484 | len += snprintf(trash.area + len, trash.size - len, |
| 3485 | ": %s", pn); |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3486 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3487 | if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, len) < 0)) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3488 | goto return_bad_req; |
| 3489 | } |
| 3490 | } |
| 3491 | |
| 3492 | /* |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3493 | * 10: add X-Original-To if either the frontend or the backend |
| 3494 | * asks for it. |
| 3495 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3496 | if ((sess->fe->options | s->be->options) & PR_O_ORGTO) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3497 | |
| 3498 | /* FIXME: don't know if IPv6 can handle that case too. */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3499 | if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3500 | /* Add an X-Original-To header unless the destination IP is |
| 3501 | * in the 'except' network range. |
| 3502 | */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3503 | conn_get_to_addr(cli_conn); |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3504 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3505 | if (cli_conn->addr.to.ss_family == AF_INET && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3506 | ((!sess->fe->except_mask_to.s_addr || |
| 3507 | (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr) |
| 3508 | != sess->fe->except_to.s_addr) && |
Emeric Brun | 5bd86a8 | 2010-10-22 17:23:04 +0200 | [diff] [blame] | 3509 | (!s->be->except_mask_to.s_addr || |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3510 | (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & s->be->except_mask_to.s_addr) |
Emeric Brun | 5bd86a8 | 2010-10-22 17:23:04 +0200 | [diff] [blame] | 3511 | != s->be->except_to.s_addr))) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3512 | int len; |
| 3513 | unsigned char *pn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3514 | pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr; |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3515 | |
| 3516 | /* Note: we rely on the backend to get the header name to be used for |
| 3517 | * x-original-to, because the header is really meant for the backends. |
| 3518 | * However, if the backend did not specify any option, we have to rely |
| 3519 | * on the frontend's header name. |
| 3520 | */ |
| 3521 | if (s->be->orgto_hdr_len) { |
| 3522 | len = s->be->orgto_hdr_len; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3523 | memcpy(trash.area, |
| 3524 | s->be->orgto_hdr_name, len); |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3525 | } else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3526 | len = sess->fe->orgto_hdr_len; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3527 | memcpy(trash.area, |
| 3528 | sess->fe->orgto_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 3529 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3530 | len += snprintf(trash.area + len, |
| 3531 | trash.size - len, |
| 3532 | ": %d.%d.%d.%d", pn[0], pn[1], |
| 3533 | pn[2], pn[3]); |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3534 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3535 | if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, len) < 0)) |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3536 | goto return_bad_req; |
| 3537 | } |
| 3538 | } |
| 3539 | } |
| 3540 | |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 3541 | /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set. |
| 3542 | * If an "Upgrade" token is found, the header is left untouched in order not to have |
| 3543 | * to deal with some servers bugs : some of them fail an Upgrade if anything but |
| 3544 | * "Upgrade" is present in the Connection header. |
| 3545 | */ |
| 3546 | if (!(txn->flags & TX_HDR_CONN_UPG) && |
| 3547 | (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3548 | ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 3549 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) { |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3550 | unsigned int want_flags = 0; |
| 3551 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3552 | if (msg->flags & HTTP_MSGF_VER_11) { |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 3553 | if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3554 | ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 3555 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3556 | !((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3557 | want_flags |= TX_CON_CLO_SET; |
| 3558 | } else { |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 3559 | if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3560 | ((sess->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL && |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 3561 | (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3562 | ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3563 | want_flags |= TX_CON_KAL_SET; |
| 3564 | } |
| 3565 | |
| 3566 | if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 3567 | http_change_connection_header(txn, msg, want_flags); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3568 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3569 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3570 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3571 | /* If we have no server assigned yet and we're balancing on url_param |
| 3572 | * with a POST request, we may be interested in checking the body for |
| 3573 | * that parameter. This will be done in another analyser. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3574 | */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3575 | if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) && |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3576 | s->txn->meth == HTTP_METH_POST && s->be->url_param_name != NULL && |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3577 | (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) { |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 3578 | channel_dont_connect(req); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3579 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3580 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3581 | |
Christopher Faulet | be821b9 | 2017-03-30 11:21:53 +0200 | [diff] [blame] | 3582 | req->analysers &= ~AN_REQ_FLT_XFER_DATA; |
| 3583 | req->analysers |= AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 3584 | #ifdef TCP_QUICKACK |
Christopher Faulet | be821b9 | 2017-03-30 11:21:53 +0200 | [diff] [blame] | 3585 | /* We expect some data from the client. Unless we know for sure |
| 3586 | * we already have a full request, we have to re-enable quick-ack |
| 3587 | * in case we previously disabled it, otherwise we might cause |
| 3588 | * the client to delay further data. |
| 3589 | */ |
| 3590 | if ((sess->listener->options & LI_O_NOQUICKACK) && |
| 3591 | cli_conn && conn_ctrl_ready(cli_conn) && |
| 3592 | ((msg->flags & HTTP_MSGF_TE_CHNK) || |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 3593 | (msg->body_len > ci_data(req) - txn->req.eoh - 2))) |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 3594 | setsockopt(cli_conn->handle.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one)); |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 3595 | #endif |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 3596 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3597 | /************************************************************* |
| 3598 | * OK, that's finished for the headers. We have done what we * |
| 3599 | * could. Let's switch to the DATA state. * |
| 3600 | ************************************************************/ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3601 | req->analyse_exp = TICK_ETERNITY; |
| 3602 | req->analysers &= ~an_bit; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3603 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3604 | s->logs.tv_request = now; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3605 | /* OK let's go on with the BODY now */ |
| 3606 | return 1; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3607 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3608 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 3609 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
Willy Tarreau | f073a83 | 2009-03-01 23:21:47 +0100 | [diff] [blame] | 3610 | /* we detected a parsing error. We want to archive this request |
| 3611 | * in the dedicated proxy area for later troubleshooting. |
| 3612 | */ |
Willy Tarreau | fd9419d | 2018-09-07 18:01:03 +0200 | [diff] [blame] | 3613 | http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe); |
Willy Tarreau | f073a83 | 2009-03-01 23:21:47 +0100 | [diff] [blame] | 3614 | } |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 3615 | |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 3616 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3617 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3618 | txn->status = 400; |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 3619 | req->analysers &= AN_REQ_FLT_END; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 3620 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3621 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 3622 | HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 3623 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 3624 | HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1); |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3625 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3626 | if (!(s->flags & SF_ERR_MASK)) |
| 3627 | s->flags |= SF_ERR_PRXCOND; |
| 3628 | if (!(s->flags & SF_FINST_MASK)) |
| 3629 | s->flags |= SF_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3630 | return 0; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 3631 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3632 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3633 | /* This function is an analyser which processes the HTTP tarpit. It always |
| 3634 | * returns zero, at the beginning because it prevents any other processing |
| 3635 | * from occurring, and at the end because it terminates the request. |
| 3636 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3637 | int http_process_tarpit(struct stream *s, struct channel *req, int an_bit) |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3638 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3639 | struct http_txn *txn = s->txn; |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3640 | |
| 3641 | /* This connection is being tarpitted. The CLIENT side has |
| 3642 | * already set the connect expiration date to the right |
| 3643 | * timeout. We just have to check that the client is still |
| 3644 | * there and that the timeout has not expired. |
| 3645 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 3646 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 3647 | if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 && |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3648 | !tick_is_expired(req->analyse_exp, now_ms)) |
| 3649 | return 0; |
| 3650 | |
| 3651 | /* We will set the queue timer to the time spent, just for |
| 3652 | * logging purposes. We fake a 500 server error, so that the |
| 3653 | * attacker will not suspect his connection has been tarpitted. |
| 3654 | * It will not cause trouble to the logs because we can exclude |
| 3655 | * the tarpitted connections by filtering on the 'PT' status flags. |
| 3656 | */ |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3657 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 3658 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 3659 | if (!(req->flags & CF_READ_ERROR)) |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 3660 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3661 | |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 3662 | req->analysers &= AN_REQ_FLT_END; |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3663 | req->analyse_exp = TICK_ETERNITY; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3664 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3665 | if (!(s->flags & SF_ERR_MASK)) |
| 3666 | s->flags |= SF_ERR_PRXCOND; |
| 3667 | if (!(s->flags & SF_FINST_MASK)) |
| 3668 | s->flags |= SF_FINST_T; |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3669 | return 0; |
| 3670 | } |
| 3671 | |
Willy Tarreau | 5a8f947 | 2014-04-10 11:16:06 +0200 | [diff] [blame] | 3672 | /* This function is an analyser which waits for the HTTP request body. It waits |
| 3673 | * for either the buffer to be full, or the full advertised contents to have |
| 3674 | * reached the buffer. It must only be called after the standard HTTP request |
| 3675 | * processing has occurred, because it expects the request to be parsed and will |
| 3676 | * look for the Expect header. It may send a 100-Continue interim response. It |
| 3677 | * takes in input any state starting from HTTP_MSG_BODY and leaves with one of |
| 3678 | * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it |
| 3679 | * needs to read more data, or 1 once it has completed its analysis. |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3680 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3681 | int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit) |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3682 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 3683 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3684 | struct http_txn *txn = s->txn; |
| 3685 | struct http_msg *msg = &s->txn->req; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3686 | |
| 3687 | /* We have to parse the HTTP request body to find any required data. |
| 3688 | * "balance url_param check_post" should have been the only way to get |
| 3689 | * into this. We were brought here after HTTP header analysis, so all |
| 3690 | * related structures are ready. |
| 3691 | */ |
| 3692 | |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 3693 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
| 3694 | /* This is the first call */ |
| 3695 | if (msg->msg_state < HTTP_MSG_BODY) |
| 3696 | goto missing_data; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3697 | |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 3698 | if (msg->msg_state < HTTP_MSG_100_SENT) { |
| 3699 | /* If we have HTTP/1.1 and Expect: 100-continue, then we must |
| 3700 | * send an HTTP/1.1 100 Continue intermediate response. |
| 3701 | */ |
| 3702 | if (msg->flags & HTTP_MSGF_VER_11) { |
| 3703 | struct hdr_ctx ctx; |
| 3704 | ctx.idx = 0; |
| 3705 | /* Expect is allowed in 1.1, look for it */ |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 3706 | if (http_find_header2("Expect", 6, ci_head(req), &txn->hdr_idx, &ctx) && |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 3707 | unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) { |
Willy Tarreau | 04f1e2d | 2018-09-10 18:04:24 +0200 | [diff] [blame^] | 3708 | co_inject(&s->res, HTTP_100.ptr, HTTP_100.len); |
Thierry FOURNIER / OZON.IO | 43ad11d | 2016-12-12 15:19:58 +0100 | [diff] [blame] | 3709 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 3710 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3711 | } |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 3712 | msg->msg_state = HTTP_MSG_100_SENT; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3713 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3714 | |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 3715 | /* we have msg->sov which points to the first byte of message body. |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 3716 | * ci_head(req) still points to the beginning of the message. We |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 3717 | * must save the body in msg->next because it survives buffer |
| 3718 | * re-alignments. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3719 | */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 3720 | msg->next = msg->sov; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 3721 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3722 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3723 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 3724 | else |
| 3725 | msg->msg_state = HTTP_MSG_DATA; |
| 3726 | } |
| 3727 | |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 3728 | if (!(msg->flags & HTTP_MSGF_TE_CHNK)) { |
| 3729 | /* We're in content-length mode, we just have to wait for enough data. */ |
Willy Tarreau | e115b49 | 2015-05-01 23:05:14 +0200 | [diff] [blame] | 3730 | if (http_body_bytes(msg) < msg->body_len) |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 3731 | goto missing_data; |
| 3732 | |
| 3733 | /* OK we have everything we need now */ |
| 3734 | goto http_end; |
| 3735 | } |
| 3736 | |
| 3737 | /* OK here we're parsing a chunked-encoded message */ |
| 3738 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3739 | if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 3740 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 3741 | * set ->sov and ->next to point to the body and switch to DATA or |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3742 | * TRAILERS state. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3743 | */ |
Willy Tarreau | e56cdd3 | 2017-09-21 08:36:33 +0200 | [diff] [blame] | 3744 | unsigned int chunk; |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3745 | int ret = h1_parse_chunk_size(&req->buf, co_data(req) + msg->next, c_data(req), &chunk); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3746 | |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3747 | if (!ret) |
| 3748 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3749 | else if (ret < 0) { |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 3750 | msg->err_pos = ci_data(req) + ret; |
Willy Tarreau | e56cdd3 | 2017-09-21 08:36:33 +0200 | [diff] [blame] | 3751 | if (msg->err_pos < 0) |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3752 | msg->err_pos += req->buf.size; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3753 | stream_inc_http_err_ctr(s); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3754 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3755 | } |
Willy Tarreau | e56cdd3 | 2017-09-21 08:36:33 +0200 | [diff] [blame] | 3756 | |
| 3757 | msg->chunk_len = chunk; |
| 3758 | msg->body_len += chunk; |
| 3759 | |
| 3760 | msg->sol = ret; |
Christopher Faulet | 113f7de | 2015-12-14 14:52:13 +0100 | [diff] [blame] | 3761 | msg->next += ret; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 3762 | msg->msg_state = msg->chunk_len ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3763 | } |
| 3764 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3765 | /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state. |
Willy Tarreau | e115b49 | 2015-05-01 23:05:14 +0200 | [diff] [blame] | 3766 | * We have the first data byte is in msg->sov + msg->sol. We're waiting |
| 3767 | * for at least a whole chunk or the whole content length bytes after |
| 3768 | * msg->sov + msg->sol. |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3769 | */ |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 3770 | if (msg->msg_state == HTTP_MSG_TRAILERS) |
| 3771 | goto http_end; |
| 3772 | |
Willy Tarreau | e115b49 | 2015-05-01 23:05:14 +0200 | [diff] [blame] | 3773 | if (http_body_bytes(msg) >= msg->body_len) /* we have enough bytes now */ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3774 | goto http_end; |
| 3775 | |
| 3776 | missing_data: |
Willy Tarreau | 31a1995 | 2014-04-10 11:50:37 +0200 | [diff] [blame] | 3777 | /* we get here if we need to wait for more data. If the buffer is full, |
| 3778 | * we have the maximum we can expect. |
| 3779 | */ |
Willy Tarreau | 2375233 | 2018-06-15 14:54:53 +0200 | [diff] [blame] | 3780 | if (channel_full(req, global.tune.maxrewrite)) |
Willy Tarreau | 31a1995 | 2014-04-10 11:50:37 +0200 | [diff] [blame] | 3781 | goto http_end; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3782 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 3783 | if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) { |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3784 | txn->status = 408; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 3785 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 3786 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3787 | if (!(s->flags & SF_ERR_MASK)) |
| 3788 | s->flags |= SF_ERR_CLITO; |
| 3789 | if (!(s->flags & SF_FINST_MASK)) |
| 3790 | s->flags |= SF_FINST_D; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3791 | goto return_err_msg; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3792 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3793 | |
| 3794 | /* we get here if we need to wait for more data */ |
Willy Tarreau | 31a1995 | 2014-04-10 11:50:37 +0200 | [diff] [blame] | 3795 | if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) { |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3796 | /* Not enough data. We'll re-use the http-request |
| 3797 | * timeout here. Ideally, we should set the timeout |
| 3798 | * relative to the accept() date. We just set the |
| 3799 | * request timeout once at the beginning of the |
| 3800 | * request. |
| 3801 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 3802 | channel_dont_connect(req); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3803 | if (!tick_isset(req->analyse_exp)) |
Willy Tarreau | cd7afc0 | 2009-07-12 10:03:17 +0200 | [diff] [blame] | 3804 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3805 | return 0; |
| 3806 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3807 | |
| 3808 | http_end: |
| 3809 | /* The situation will not evolve, so let's give up on the analysis. */ |
| 3810 | s->logs.tv_request = now; /* update the request timer to reflect full request */ |
| 3811 | req->analysers &= ~an_bit; |
| 3812 | req->analyse_exp = TICK_ETERNITY; |
| 3813 | return 1; |
| 3814 | |
| 3815 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 3816 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3817 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3818 | txn->status = 400; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 3819 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3820 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3821 | if (!(s->flags & SF_ERR_MASK)) |
| 3822 | s->flags |= SF_ERR_PRXCOND; |
| 3823 | if (!(s->flags & SF_FINST_MASK)) |
| 3824 | s->flags |= SF_FINST_R; |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 3825 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3826 | return_err_msg: |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 3827 | req->analysers &= AN_REQ_FLT_END; |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 3828 | HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 3829 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 3830 | HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3831 | return 0; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3832 | } |
| 3833 | |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 3834 | /* send a server's name with an outgoing request over an established connection. |
| 3835 | * Note: this function is designed to be called once the request has been scheduled |
| 3836 | * for being forwarded. This is the reason why it rewinds the buffer before |
| 3837 | * proceeding. |
| 3838 | */ |
Willy Tarreau | 45c0d98 | 2012-03-09 12:11:57 +0100 | [diff] [blame] | 3839 | int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) { |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 3840 | |
| 3841 | struct hdr_ctx ctx; |
| 3842 | |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 3843 | char *hdr_name = be->server_id_hdr_name; |
| 3844 | int hdr_name_len = be->server_id_hdr_len; |
Willy Tarreau | 394db37 | 2012-10-12 22:40:39 +0200 | [diff] [blame] | 3845 | struct channel *chn = txn->req.chn; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 3846 | char *hdr_val; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 3847 | unsigned int old_o, old_i; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 3848 | |
William Lallemand | d9e9066 | 2012-01-30 17:27:17 +0100 | [diff] [blame] | 3849 | ctx.idx = 0; |
| 3850 | |
Willy Tarreau | 211cdec | 2014-04-17 20:18:08 +0200 | [diff] [blame] | 3851 | old_o = http_hdr_rewind(&txn->req); |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 3852 | if (old_o) { |
| 3853 | /* The request was already skipped, let's restore it */ |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 3854 | c_rew(chn, old_o); |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 3855 | txn->req.next += old_o; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 3856 | txn->req.sov += old_o; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 3857 | } |
| 3858 | |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 3859 | old_i = ci_data(chn); |
| 3860 | while (http_find_header2(hdr_name, hdr_name_len, ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) { |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 3861 | /* remove any existing values from the header */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 3862 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 3863 | } |
| 3864 | |
| 3865 | /* Add the new header requested with the server value */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3866 | hdr_val = trash.area; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 3867 | memcpy(hdr_val, hdr_name, hdr_name_len); |
| 3868 | hdr_val += hdr_name_len; |
| 3869 | *hdr_val++ = ':'; |
| 3870 | *hdr_val++ = ' '; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3871 | hdr_val += strlcpy2(hdr_val, srv_name, |
| 3872 | trash.area + trash.size - hdr_val); |
| 3873 | http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, |
| 3874 | hdr_val - trash.area); |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 3875 | |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 3876 | if (old_o) { |
| 3877 | /* If this was a forwarded request, we must readjust the amount of |
| 3878 | * data to be forwarded in order to take into account the size |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 3879 | * variations. Note that the current state is >= HTTP_MSG_BODY, |
| 3880 | * so we don't have to adjust ->sol. |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 3881 | */ |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 3882 | old_o += ci_data(chn) - old_i; |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 3883 | c_adv(chn, old_o); |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 3884 | txn->req.next -= old_o; |
| 3885 | txn->req.sov -= old_o; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 3886 | } |
| 3887 | |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 3888 | return 0; |
| 3889 | } |
| 3890 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3891 | /* Terminate current transaction and prepare a new one. This is very tricky |
| 3892 | * right now but it works. |
| 3893 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3894 | void http_end_txn_clean_session(struct stream *s) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3895 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3896 | int prev_status = s->txn->status; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 3897 | struct proxy *fe = strm_fe(s); |
Willy Tarreau | 858b103 | 2015-12-07 17:04:59 +0100 | [diff] [blame] | 3898 | struct proxy *be = s->be; |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 3899 | struct conn_stream *cs; |
Willy Tarreau | 323a2d9 | 2015-08-04 19:00:17 +0200 | [diff] [blame] | 3900 | struct connection *srv_conn; |
| 3901 | struct server *srv; |
Willy Tarreau | 449d74a | 2015-08-05 17:16:33 +0200 | [diff] [blame] | 3902 | unsigned int prev_flags = s->txn->flags; |
Willy Tarreau | 068621e | 2013-12-23 15:11:25 +0100 | [diff] [blame] | 3903 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3904 | /* FIXME: We need a more portable way of releasing a backend's and a |
| 3905 | * server's connections. We need a safer way to reinitialize buffer |
| 3906 | * flags. We also need a more accurate method for computing per-request |
| 3907 | * data. |
| 3908 | */ |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 3909 | /* |
| 3910 | * XXX cognet: This is probably wrong, this is killing a whole |
| 3911 | * connection, in the new world order, we probably want to just kill |
| 3912 | * the stream, this is to be revisited the day we handle multiple |
| 3913 | * streams in one server connection. |
| 3914 | */ |
| 3915 | cs = objt_cs(s->si[1].end); |
| 3916 | srv_conn = cs_conn(cs); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3917 | |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 3918 | /* unless we're doing keep-alive, we want to quickly close the connection |
| 3919 | * to the server. |
| 3920 | */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3921 | if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) || |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 3922 | !si_conn_ready(&s->si[1])) { |
| 3923 | s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF; |
| 3924 | si_shutr(&s->si[1]); |
| 3925 | si_shutw(&s->si[1]); |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 3926 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3927 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3928 | if (s->flags & SF_BE_ASSIGNED) { |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 3929 | HA_ATOMIC_SUB(&be->beconn, 1); |
Willy Tarreau | 2d5cd47 | 2012-03-01 23:34:37 +0100 | [diff] [blame] | 3930 | if (unlikely(s->srv_conn)) |
| 3931 | sess_change_server(s, NULL); |
| 3932 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3933 | |
| 3934 | s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3935 | stream_process_counters(s); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3936 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3937 | if (s->txn->status) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3938 | int n; |
| 3939 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3940 | n = s->txn->status / 100; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3941 | if (n < 1 || n > 5) |
| 3942 | n = 0; |
| 3943 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3944 | if (fe->mode == PR_MODE_HTTP) { |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 3945 | HA_ATOMIC_ADD(&fe->fe_counters.p.http.rsp[n], 1); |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 3946 | } |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3947 | if ((s->flags & SF_BE_ASSIGNED) && |
Willy Tarreau | 858b103 | 2015-12-07 17:04:59 +0100 | [diff] [blame] | 3948 | (be->mode == PR_MODE_HTTP)) { |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 3949 | HA_ATOMIC_ADD(&be->be_counters.p.http.rsp[n], 1); |
| 3950 | HA_ATOMIC_ADD(&be->be_counters.p.http.cum_req, 1); |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 3951 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3952 | } |
| 3953 | |
| 3954 | /* don't count other requests' data */ |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 3955 | s->logs.bytes_in -= ci_data(&s->req); |
| 3956 | s->logs.bytes_out -= ci_data(&s->res); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3957 | |
Willy Tarreau | 66425e3 | 2018-07-25 06:55:12 +0200 | [diff] [blame] | 3958 | /* we may need to know the position in the queue */ |
| 3959 | pendconn_free(s); |
| 3960 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3961 | /* let's do a final log if we need it */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3962 | if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait && |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3963 | !(s->flags & SF_MONITOR) && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3964 | (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3965 | s->do_log(s); |
| 3966 | } |
| 3967 | |
Willy Tarreau | d713bcc | 2014-06-25 15:36:04 +0200 | [diff] [blame] | 3968 | /* stop tracking content-based counters */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3969 | stream_stop_content_counters(s); |
| 3970 | stream_update_time_stats(s); |
Willy Tarreau | 4bfc580 | 2014-06-17 12:19:18 +0200 | [diff] [blame] | 3971 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3972 | s->logs.accept_date = date; /* user-visible date for logging */ |
| 3973 | s->logs.tv_accept = now; /* corrected date for internal use */ |
Thierry FOURNIER / OZON.IO | 4cac359 | 2016-07-28 17:19:45 +0200 | [diff] [blame] | 3974 | s->logs.t_handshake = 0; /* There are no handshake in keep alive connection. */ |
| 3975 | s->logs.t_idle = -1; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3976 | tv_zero(&s->logs.tv_request); |
| 3977 | s->logs.t_queue = -1; |
| 3978 | s->logs.t_connect = -1; |
| 3979 | s->logs.t_data = -1; |
| 3980 | s->logs.t_close = 0; |
Patrick Hemmer | ffe5e8c | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 3981 | s->logs.prx_queue_pos = 0; /* we get the number of pending conns before us */ |
| 3982 | s->logs.srv_queue_pos = 0; /* we will get this number soon */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3983 | |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 3984 | s->logs.bytes_in = s->req.total = ci_data(&s->req); |
| 3985 | s->logs.bytes_out = s->res.total = ci_data(&s->res); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3986 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 3987 | if (objt_server(s->target)) { |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3988 | if (s->flags & SF_CURR_SESS) { |
| 3989 | s->flags &= ~SF_CURR_SESS; |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 3990 | HA_ATOMIC_SUB(&objt_server(s->target)->cur_sess, 1); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3991 | } |
Willy Tarreau | 858b103 | 2015-12-07 17:04:59 +0100 | [diff] [blame] | 3992 | if (may_dequeue_tasks(objt_server(s->target), be)) |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 3993 | process_srv_queue(objt_server(s->target)); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3994 | } |
| 3995 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 3996 | s->target = NULL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3997 | |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 3998 | /* only release our endpoint if we don't intend to reuse the |
| 3999 | * connection. |
| 4000 | */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4001 | if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) || |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4002 | !si_conn_ready(&s->si[1])) { |
| 4003 | si_release_endpoint(&s->si[1]); |
Willy Tarreau | 323a2d9 | 2015-08-04 19:00:17 +0200 | [diff] [blame] | 4004 | srv_conn = NULL; |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4005 | } |
| 4006 | |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4007 | s->si[1].state = s->si[1].prev_state = SI_ST_INI; |
| 4008 | s->si[1].err_type = SI_ET_NONE; |
| 4009 | s->si[1].conn_retries = 0; /* used for logging too */ |
| 4010 | s->si[1].exp = TICK_ETERNITY; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4011 | s->si[1].flags &= SI_FL_ISBACK | SI_FL_DONT_WAKE; /* we're in the context of process_stream */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4012 | s->req.flags &= ~(CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CONNECT|CF_WRITE_ERROR|CF_STREAMER|CF_STREAMER_FAST|CF_NEVER_WAIT|CF_WAKE_CONNECT|CF_WROTE_DATA); |
Christopher Faulet | c5a9d5b | 2017-11-09 09:36:43 +0100 | [diff] [blame] | 4013 | s->res.flags &= ~(CF_SHUTR|CF_SHUTR_NOW|CF_READ_ATTACHED|CF_READ_ERROR|CF_READ_NOEXP|CF_STREAMER|CF_STREAMER_FAST|CF_WRITE_PARTIAL|CF_NEVER_WAIT|CF_WROTE_DATA|CF_WRITE_EVENT); |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4014 | s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST); |
| 4015 | s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED); |
| 4016 | s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP); |
Willy Tarreau | 543db62 | 2012-11-15 16:41:22 +0100 | [diff] [blame] | 4017 | |
Patrick Hemmer | e3faf02 | 2018-08-22 10:02:00 -0400 | [diff] [blame] | 4018 | hlua_ctx_destroy(s->hlua); |
| 4019 | s->hlua = NULL; |
| 4020 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4021 | s->txn->meth = 0; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4022 | http_reset_txn(s); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4023 | s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ; |
Willy Tarreau | 068621e | 2013-12-23 15:11:25 +0100 | [diff] [blame] | 4024 | |
| 4025 | if (prev_status == 401 || prev_status == 407) { |
| 4026 | /* In HTTP keep-alive mode, if we receive a 401, we still have |
| 4027 | * a chance of being able to send the visitor again to the same |
| 4028 | * server over the same connection. This is required by some |
| 4029 | * broken protocols such as NTLM, and anyway whenever there is |
| 4030 | * an opportunity for sending the challenge to the proper place, |
| 4031 | * it's better to do it (at least it helps with debugging). |
| 4032 | */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4033 | s->txn->flags |= TX_PREFER_LAST; |
Willy Tarreau | bd99d58 | 2015-09-02 10:40:43 +0200 | [diff] [blame] | 4034 | if (srv_conn) |
| 4035 | srv_conn->flags |= CO_FL_PRIVATE; |
Willy Tarreau | 068621e | 2013-12-23 15:11:25 +0100 | [diff] [blame] | 4036 | } |
| 4037 | |
Willy Tarreau | 53f9685 | 2016-02-02 18:50:47 +0100 | [diff] [blame] | 4038 | /* Never ever allow to reuse a connection from a non-reuse backend */ |
| 4039 | if (srv_conn && (be->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR) |
| 4040 | srv_conn->flags |= CO_FL_PRIVATE; |
| 4041 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4042 | if (fe->options2 & PR_O2_INDEPSTR) |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4043 | s->si[1].flags |= SI_FL_INDEP_STR; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4044 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4045 | if (fe->options2 & PR_O2_NODELAY) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4046 | s->req.flags |= CF_NEVER_WAIT; |
| 4047 | s->res.flags |= CF_NEVER_WAIT; |
Willy Tarreau | 96e3121 | 2011-05-30 18:10:30 +0200 | [diff] [blame] | 4048 | } |
| 4049 | |
Willy Tarreau | 714ea78 | 2015-11-25 20:11:11 +0100 | [diff] [blame] | 4050 | /* we're removing the analysers, we MUST re-enable events detection. |
| 4051 | * We don't enable close on the response channel since it's either |
| 4052 | * already closed, or in keep-alive with an idle connection handler. |
| 4053 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4054 | channel_auto_read(&s->req); |
| 4055 | channel_auto_close(&s->req); |
| 4056 | channel_auto_read(&s->res); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4057 | |
Willy Tarreau | 1c59bd5 | 2015-11-02 20:20:11 +0100 | [diff] [blame] | 4058 | /* we're in keep-alive with an idle connection, monitor it if not already done */ |
| 4059 | if (srv_conn && LIST_ISEMPTY(&srv_conn->list)) { |
Willy Tarreau | 323a2d9 | 2015-08-04 19:00:17 +0200 | [diff] [blame] | 4060 | srv = objt_server(srv_conn->target); |
Willy Tarreau | 8dff998 | 2015-08-04 20:45:52 +0200 | [diff] [blame] | 4061 | if (!srv) |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 4062 | si_idle_cs(&s->si[1], NULL); |
Willy Tarreau | 53f9685 | 2016-02-02 18:50:47 +0100 | [diff] [blame] | 4063 | else if (srv_conn->flags & CO_FL_PRIVATE) |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 4064 | si_idle_cs(&s->si[1], (srv->priv_conns ? &srv->priv_conns[tid] : NULL)); |
Willy Tarreau | 449d74a | 2015-08-05 17:16:33 +0200 | [diff] [blame] | 4065 | else if (prev_flags & TX_NOT_FIRST) |
| 4066 | /* note: we check the request, not the connection, but |
| 4067 | * this is valid for strategies SAFE and AGGR, and in |
| 4068 | * case of ALWS, we don't care anyway. |
| 4069 | */ |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 4070 | si_idle_cs(&s->si[1], (srv->safe_conns ? &srv->safe_conns[tid] : NULL)); |
Willy Tarreau | 8dff998 | 2015-08-04 20:45:52 +0200 | [diff] [blame] | 4071 | else |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 4072 | si_idle_cs(&s->si[1], (srv->idle_conns ? &srv->idle_conns[tid] : NULL)); |
Willy Tarreau | 4320eaa | 2015-08-05 11:08:30 +0200 | [diff] [blame] | 4073 | } |
Christopher Faulet | e600624 | 2017-03-10 11:52:44 +0100 | [diff] [blame] | 4074 | s->req.analysers = strm_li(s) ? strm_li(s)->analysers : 0; |
| 4075 | s->res.analysers = 0; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4076 | } |
| 4077 | |
| 4078 | |
| 4079 | /* This function updates the request state machine according to the response |
| 4080 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 4081 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 4082 | * it is only used to find when a request/response couple is complete. Both |
| 4083 | * this function and its equivalent should loop until both return zero. It |
| 4084 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 4085 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4086 | int http_sync_req_state(struct stream *s) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4087 | { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4088 | struct channel *chn = &s->req; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4089 | struct http_txn *txn = s->txn; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4090 | unsigned int old_flags = chn->flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4091 | unsigned int old_state = txn->req.msg_state; |
| 4092 | |
Christopher Faulet | 4be9803 | 2017-07-18 10:48:24 +0200 | [diff] [blame] | 4093 | if (unlikely(txn->req.msg_state < HTTP_MSG_DONE)) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4094 | return 0; |
| 4095 | |
| 4096 | if (txn->req.msg_state == HTTP_MSG_DONE) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4097 | /* No need to read anymore, the request was completely parsed. |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 4098 | * We can shut the read side unless we want to abort_on_close, |
| 4099 | * or we have a POST request. The issue with POST requests is |
| 4100 | * that some browsers still send a CRLF after the request, and |
| 4101 | * this CRLF must be read so that it does not remain in the kernel |
| 4102 | * buffers, otherwise a close could cause an RST on some systems |
| 4103 | * (eg: Linux). |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 4104 | * Note that if we're using keep-alive on the client side, we'd |
| 4105 | * rather poll now and keep the polling enabled for the whole |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4106 | * stream's life than enabling/disabling it between each |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 4107 | * response and next request. |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4108 | */ |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 4109 | if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) && |
| 4110 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) && |
Willy Tarreau | 7aa15b0 | 2017-12-20 16:56:50 +0100 | [diff] [blame] | 4111 | (!(s->be->options & PR_O_ABRT_CLOSE) || |
| 4112 | (s->si[0].flags & SI_FL_CLEAN_ABRT)) && |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 4113 | txn->meth != HTTP_METH_POST) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4114 | channel_dont_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4115 | |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 4116 | /* if the server closes the connection, we want to immediately react |
| 4117 | * and close the socket to save packets and syscalls. |
| 4118 | */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4119 | s->si[1].flags |= SI_FL_NOHALF; |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 4120 | |
Willy Tarreau | 7f876a1 | 2015-11-18 11:59:55 +0100 | [diff] [blame] | 4121 | /* In any case we've finished parsing the request so we must |
| 4122 | * disable Nagle when sending data because 1) we're not going |
| 4123 | * to shut this side, and 2) the server is waiting for us to |
| 4124 | * send pending data. |
| 4125 | */ |
| 4126 | chn->flags |= CF_NEVER_WAIT; |
| 4127 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4128 | if (txn->rsp.msg_state == HTTP_MSG_ERROR) |
| 4129 | goto wait_other_side; |
| 4130 | |
| 4131 | if (txn->rsp.msg_state < HTTP_MSG_DONE) { |
| 4132 | /* The server has not finished to respond, so we |
| 4133 | * don't want to move in order not to upset it. |
| 4134 | */ |
| 4135 | goto wait_other_side; |
| 4136 | } |
| 4137 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4138 | /* When we get here, it means that both the request and the |
| 4139 | * response have finished receiving. Depending on the connection |
| 4140 | * mode, we'll have to wait for the last bytes to leave in either |
| 4141 | * direction, and sometimes for a close to be effective. |
| 4142 | */ |
| 4143 | |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4144 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 4145 | /* Server-close mode : queue a connection close to the server */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4146 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) |
| 4147 | channel_shutw_now(chn); |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4148 | } |
| 4149 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 4150 | /* Option forceclose is set, or either side wants to close, |
| 4151 | * let's enforce it now that we're not expecting any new |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4152 | * data to come. The caller knows the stream is complete |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4153 | * once both states are CLOSED. |
Christopher Faulet | 1486b0a | 2017-07-18 11:42:08 +0200 | [diff] [blame] | 4154 | * |
| 4155 | * However, there is an exception if the response |
| 4156 | * length is undefined. In this case, we need to wait |
| 4157 | * the close from the server. The response will be |
| 4158 | * switched in TUNNEL mode until the end. |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4159 | */ |
Christopher Faulet | 1486b0a | 2017-07-18 11:42:08 +0200 | [diff] [blame] | 4160 | if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) && |
| 4161 | txn->rsp.msg_state != HTTP_MSG_CLOSED) |
| 4162 | goto check_channel_flags; |
| 4163 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4164 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) { |
| 4165 | channel_shutr_now(chn); |
| 4166 | channel_shutw_now(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4167 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4168 | } |
| 4169 | else { |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4170 | /* The last possible modes are keep-alive and tunnel. Tunnel mode |
| 4171 | * will not have any analyser so it needs to poll for reads. |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4172 | */ |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4173 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) { |
| 4174 | channel_auto_read(chn); |
| 4175 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4176 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4177 | } |
| 4178 | |
Christopher Faulet | 4be9803 | 2017-07-18 10:48:24 +0200 | [diff] [blame] | 4179 | goto check_channel_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4180 | } |
| 4181 | |
| 4182 | if (txn->req.msg_state == HTTP_MSG_CLOSING) { |
| 4183 | http_msg_closing: |
| 4184 | /* nothing else to forward, just waiting for the output buffer |
| 4185 | * to be empty and for the shutw_now to take effect. |
| 4186 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4187 | if (channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4188 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 4189 | goto http_msg_closed; |
| 4190 | } |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4191 | else if (chn->flags & CF_SHUTW) { |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 4192 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4193 | txn->req.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4194 | } |
Christopher Faulet | 56d2609 | 2017-07-20 11:05:10 +0200 | [diff] [blame] | 4195 | goto wait_other_side; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4196 | } |
| 4197 | |
| 4198 | if (txn->req.msg_state == HTTP_MSG_CLOSED) { |
| 4199 | http_msg_closed: |
Willy Tarreau | 8059351 | 2017-12-14 10:43:31 +0100 | [diff] [blame] | 4200 | /* if we don't know whether the server will close, we need to hard close */ |
| 4201 | if (txn->rsp.flags & HTTP_MSGF_XFER_LEN) |
| 4202 | s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */ |
| 4203 | |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 4204 | /* see above in MSG_DONE why we only do this in these states */ |
| 4205 | if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) && |
| 4206 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) && |
Willy Tarreau | 7aa15b0 | 2017-12-20 16:56:50 +0100 | [diff] [blame] | 4207 | (!(s->be->options & PR_O_ABRT_CLOSE) || |
| 4208 | (s->si[0].flags & SI_FL_CLEAN_ABRT))) |
Willy Tarreau | 2e7a165 | 2013-12-15 15:32:10 +0100 | [diff] [blame] | 4209 | channel_dont_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4210 | goto wait_other_side; |
| 4211 | } |
| 4212 | |
Christopher Faulet | 4be9803 | 2017-07-18 10:48:24 +0200 | [diff] [blame] | 4213 | check_channel_flags: |
| 4214 | /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */ |
| 4215 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) { |
| 4216 | /* if we've just closed an output, let's switch */ |
Christopher Faulet | 4be9803 | 2017-07-18 10:48:24 +0200 | [diff] [blame] | 4217 | txn->req.msg_state = HTTP_MSG_CLOSING; |
| 4218 | goto http_msg_closing; |
| 4219 | } |
| 4220 | |
| 4221 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4222 | wait_other_side: |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4223 | return txn->req.msg_state != old_state || chn->flags != old_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4224 | } |
| 4225 | |
| 4226 | |
| 4227 | /* This function updates the response state machine according to the request |
| 4228 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 4229 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 4230 | * it is only used to find when a request/response couple is complete. Both |
| 4231 | * this function and its equivalent should loop until both return zero. It |
| 4232 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 4233 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4234 | int http_sync_res_state(struct stream *s) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4235 | { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4236 | struct channel *chn = &s->res; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4237 | struct http_txn *txn = s->txn; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4238 | unsigned int old_flags = chn->flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4239 | unsigned int old_state = txn->rsp.msg_state; |
| 4240 | |
Christopher Faulet | 4be9803 | 2017-07-18 10:48:24 +0200 | [diff] [blame] | 4241 | if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE)) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4242 | return 0; |
| 4243 | |
| 4244 | if (txn->rsp.msg_state == HTTP_MSG_DONE) { |
| 4245 | /* In theory, we don't need to read anymore, but we must |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4246 | * still monitor the server connection for a possible close |
| 4247 | * while the request is being uploaded, so we don't disable |
| 4248 | * reading. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4249 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4250 | /* channel_dont_read(chn); */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4251 | |
| 4252 | if (txn->req.msg_state == HTTP_MSG_ERROR) |
| 4253 | goto wait_other_side; |
| 4254 | |
| 4255 | if (txn->req.msg_state < HTTP_MSG_DONE) { |
| 4256 | /* The client seems to still be sending data, probably |
| 4257 | * because we got an error response during an upload. |
| 4258 | * We have the choice of either breaking the connection |
| 4259 | * or letting it pass through. Let's do the later. |
| 4260 | */ |
| 4261 | goto wait_other_side; |
| 4262 | } |
| 4263 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4264 | /* When we get here, it means that both the request and the |
| 4265 | * response have finished receiving. Depending on the connection |
| 4266 | * mode, we'll have to wait for the last bytes to leave in either |
| 4267 | * direction, and sometimes for a close to be effective. |
| 4268 | */ |
| 4269 | |
| 4270 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 4271 | /* Server-close mode : shut read and wait for the request |
| 4272 | * side to close its output buffer. The caller will detect |
| 4273 | * when we're in DONE and the other is in CLOSED and will |
| 4274 | * catch that for the final cleanup. |
| 4275 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4276 | if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW))) |
| 4277 | channel_shutr_now(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4278 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4279 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 4280 | /* Option forceclose is set, or either side wants to close, |
| 4281 | * let's enforce it now that we're not expecting any new |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4282 | * data to come. The caller knows the stream is complete |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4283 | * once both states are CLOSED. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4284 | */ |
Christopher Faulet | fd04fcf | 2018-02-02 15:54:15 +0100 | [diff] [blame] | 4285 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4286 | channel_shutr_now(chn); |
| 4287 | channel_shutw_now(chn); |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4288 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4289 | } |
| 4290 | else { |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4291 | /* The last possible modes are keep-alive and tunnel. Tunnel will |
| 4292 | * need to forward remaining data. Keep-alive will need to monitor |
| 4293 | * for connection closing. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4294 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4295 | channel_auto_read(chn); |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 4296 | chn->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4297 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) |
| 4298 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4299 | } |
| 4300 | |
Christopher Faulet | 4be9803 | 2017-07-18 10:48:24 +0200 | [diff] [blame] | 4301 | goto check_channel_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4302 | } |
| 4303 | |
| 4304 | if (txn->rsp.msg_state == HTTP_MSG_CLOSING) { |
| 4305 | http_msg_closing: |
| 4306 | /* nothing else to forward, just waiting for the output buffer |
| 4307 | * to be empty and for the shutw_now to take effect. |
| 4308 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4309 | if (channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4310 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 4311 | goto http_msg_closed; |
| 4312 | } |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4313 | else if (chn->flags & CF_SHUTW) { |
Christopher Faulet | a3992e0 | 2017-07-18 10:35:55 +0200 | [diff] [blame] | 4314 | txn->rsp.err_state = txn->rsp.msg_state; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4315 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 4316 | HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4317 | if (objt_server(s->target)) |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 4318 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4319 | } |
Christopher Faulet | 56d2609 | 2017-07-20 11:05:10 +0200 | [diff] [blame] | 4320 | goto wait_other_side; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4321 | } |
| 4322 | |
| 4323 | if (txn->rsp.msg_state == HTTP_MSG_CLOSED) { |
| 4324 | http_msg_closed: |
| 4325 | /* drop any pending data */ |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 4326 | channel_truncate(chn); |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4327 | channel_auto_close(chn); |
| 4328 | channel_auto_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4329 | goto wait_other_side; |
| 4330 | } |
| 4331 | |
Christopher Faulet | 4be9803 | 2017-07-18 10:48:24 +0200 | [diff] [blame] | 4332 | check_channel_flags: |
| 4333 | /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */ |
| 4334 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) { |
| 4335 | /* if we've just closed an output, let's switch */ |
| 4336 | txn->rsp.msg_state = HTTP_MSG_CLOSING; |
| 4337 | goto http_msg_closing; |
| 4338 | } |
| 4339 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4340 | wait_other_side: |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 4341 | /* We force the response to leave immediately if we're waiting for the |
| 4342 | * other side, since there is no pending shutdown to push it out. |
| 4343 | */ |
| 4344 | if (!channel_is_empty(chn)) |
| 4345 | chn->flags |= CF_SEND_DONTWAIT; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4346 | return txn->rsp.msg_state != old_state || chn->flags != old_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4347 | } |
| 4348 | |
| 4349 | |
Christopher Faulet | 894da4c | 2017-07-18 11:29:07 +0200 | [diff] [blame] | 4350 | /* Resync the request and response state machines. */ |
| 4351 | void http_resync_states(struct stream *s) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4352 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4353 | struct http_txn *txn = s->txn; |
Christopher Faulet | 894da4c | 2017-07-18 11:29:07 +0200 | [diff] [blame] | 4354 | #ifdef DEBUG_FULL |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4355 | int old_req_state = txn->req.msg_state; |
| 4356 | int old_res_state = txn->rsp.msg_state; |
Christopher Faulet | 894da4c | 2017-07-18 11:29:07 +0200 | [diff] [blame] | 4357 | #endif |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4358 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4359 | http_sync_req_state(s); |
| 4360 | while (1) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4361 | if (!http_sync_res_state(s)) |
| 4362 | break; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4363 | if (!http_sync_req_state(s)) |
| 4364 | break; |
| 4365 | } |
Willy Tarreau | 3ce10ff | 2014-04-22 08:24:38 +0200 | [diff] [blame] | 4366 | |
Christopher Faulet | 894da4c | 2017-07-18 11:29:07 +0200 | [diff] [blame] | 4367 | DPRINTF(stderr,"[%u] %s: stream=%p old=%s,%s cur=%s,%s " |
| 4368 | "req->analysers=0x%08x res->analysers=0x%08x\n", |
| 4369 | now_ms, __FUNCTION__, s, |
Willy Tarreau | 0da5b3b | 2017-09-21 09:30:46 +0200 | [diff] [blame] | 4370 | h1_msg_state_str(old_req_state), h1_msg_state_str(old_res_state), |
| 4371 | h1_msg_state_str(txn->req.msg_state), h1_msg_state_str(txn->rsp.msg_state), |
Christopher Faulet | 894da4c | 2017-07-18 11:29:07 +0200 | [diff] [blame] | 4372 | s->req.analysers, s->res.analysers); |
Christopher Faulet | 814d270 | 2017-03-30 11:33:44 +0200 | [diff] [blame] | 4373 | |
| 4374 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4375 | /* OK, both state machines agree on a compatible state. |
| 4376 | * There are a few cases we're interested in : |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4377 | * - HTTP_MSG_CLOSED on both sides means we've reached the end in both |
| 4378 | * directions, so let's simply disable both analysers. |
Christopher Faulet | f77bb53 | 2017-07-18 11:18:46 +0200 | [diff] [blame] | 4379 | * - HTTP_MSG_CLOSED on the response only or HTTP_MSG_ERROR on either |
| 4380 | * means we must abort the request. |
| 4381 | * - HTTP_MSG_TUNNEL on either means we have to disable analyser on |
| 4382 | * corresponding channel. |
| 4383 | * - HTTP_MSG_DONE or HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE |
| 4384 | * on the response with server-close mode means we've completed one |
| 4385 | * request and we must re-initialize the server connection. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4386 | */ |
Christopher Faulet | f77bb53 | 2017-07-18 11:18:46 +0200 | [diff] [blame] | 4387 | if (txn->req.msg_state == HTTP_MSG_CLOSED && |
| 4388 | txn->rsp.msg_state == HTTP_MSG_CLOSED) { |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4389 | s->req.analysers &= AN_REQ_FLT_END; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4390 | channel_auto_close(&s->req); |
| 4391 | channel_auto_read(&s->req); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4392 | s->res.analysers &= AN_RES_FLT_END; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4393 | channel_auto_close(&s->res); |
| 4394 | channel_auto_read(&s->res); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4395 | } |
Christopher Faulet | f77bb53 | 2017-07-18 11:18:46 +0200 | [diff] [blame] | 4396 | else if (txn->rsp.msg_state == HTTP_MSG_CLOSED || |
| 4397 | txn->rsp.msg_state == HTTP_MSG_ERROR || |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 4398 | txn->req.msg_state == HTTP_MSG_ERROR) { |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4399 | s->res.analysers &= AN_RES_FLT_END; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4400 | channel_auto_close(&s->res); |
| 4401 | channel_auto_read(&s->res); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4402 | s->req.analysers &= AN_REQ_FLT_END; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4403 | channel_abort(&s->req); |
| 4404 | channel_auto_close(&s->req); |
| 4405 | channel_auto_read(&s->req); |
| 4406 | channel_truncate(&s->req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4407 | } |
Christopher Faulet | f77bb53 | 2017-07-18 11:18:46 +0200 | [diff] [blame] | 4408 | else if (txn->req.msg_state == HTTP_MSG_TUNNEL || |
| 4409 | txn->rsp.msg_state == HTTP_MSG_TUNNEL) { |
| 4410 | if (txn->req.msg_state == HTTP_MSG_TUNNEL) { |
| 4411 | s->req.analysers &= AN_REQ_FLT_END; |
| 4412 | if (HAS_REQ_DATA_FILTERS(s)) |
| 4413 | s->req.analysers |= AN_REQ_FLT_XFER_DATA; |
| 4414 | } |
| 4415 | if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) { |
| 4416 | s->res.analysers &= AN_RES_FLT_END; |
| 4417 | if (HAS_RSP_DATA_FILTERS(s)) |
| 4418 | s->res.analysers |= AN_RES_FLT_XFER_DATA; |
| 4419 | } |
| 4420 | channel_auto_close(&s->req); |
| 4421 | channel_auto_read(&s->req); |
| 4422 | channel_auto_close(&s->res); |
| 4423 | channel_auto_read(&s->res); |
| 4424 | } |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4425 | else if ((txn->req.msg_state == HTTP_MSG_DONE || |
| 4426 | txn->req.msg_state == HTTP_MSG_CLOSED) && |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4427 | txn->rsp.msg_state == HTTP_MSG_DONE && |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4428 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL || |
| 4429 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) { |
| 4430 | /* server-close/keep-alive: terminate this transaction, |
| 4431 | * possibly killing the server connection and reinitialize |
Willy Tarreau | 3de5bd6 | 2016-05-02 16:07:07 +0200 | [diff] [blame] | 4432 | * a fresh-new transaction, but only once we're sure there's |
| 4433 | * enough room in the request and response buffer to process |
Christopher Faulet | c0c672a | 2017-03-28 11:51:33 +0200 | [diff] [blame] | 4434 | * another request. They must not hold any pending output data |
| 4435 | * and the response buffer must realigned |
| 4436 | * (realign is done is http_end_txn_clean_session). |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4437 | */ |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 4438 | if (co_data(&s->req)) |
Willy Tarreau | 3de5bd6 | 2016-05-02 16:07:07 +0200 | [diff] [blame] | 4439 | s->req.flags |= CF_WAKE_WRITE; |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 4440 | else if (co_data(&s->res)) |
Willy Tarreau | 3de5bd6 | 2016-05-02 16:07:07 +0200 | [diff] [blame] | 4441 | s->res.flags |= CF_WAKE_WRITE; |
Christopher Faulet | a81ff60 | 2017-07-18 22:01:05 +0200 | [diff] [blame] | 4442 | else { |
| 4443 | s->req.analysers = AN_REQ_FLT_END; |
| 4444 | s->res.analysers = AN_RES_FLT_END; |
| 4445 | txn->flags |= TX_WAIT_CLEANUP; |
| 4446 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4447 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4448 | } |
| 4449 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4450 | /* This function is an analyser which forwards request body (including chunk |
| 4451 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 4452 | * zero byte. The only situation where it must not be called is when we're in |
| 4453 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 4454 | * remaining data and to resync after end of body. It expects the msg_state to |
| 4455 | * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4456 | * read more data, or 1 once we can go on with next request or end the stream. |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 4457 | * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 4458 | * bytes of pending data + the headers if not already done. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4459 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4460 | int http_request_forward_body(struct stream *s, struct channel *req, int an_bit) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4461 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4462 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4463 | struct http_txn *txn = s->txn; |
| 4464 | struct http_msg *msg = &s->txn->req; |
Christopher Faulet | 3e34429 | 2015-11-24 16:24:13 +0100 | [diff] [blame] | 4465 | int ret; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4466 | |
Christopher Faulet | 4507351 | 2018-07-20 10:16:29 +0200 | [diff] [blame] | 4467 | DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%lu analysers=%02x\n", |
Christopher Faulet | 814d270 | 2017-03-30 11:33:44 +0200 | [diff] [blame] | 4468 | now_ms, __FUNCTION__, |
| 4469 | s, |
| 4470 | req, |
| 4471 | req->rex, req->wex, |
| 4472 | req->flags, |
Christopher Faulet | 4507351 | 2018-07-20 10:16:29 +0200 | [diff] [blame] | 4473 | ci_data(req), |
Christopher Faulet | 814d270 | 2017-03-30 11:33:44 +0200 | [diff] [blame] | 4474 | req->analysers); |
| 4475 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4476 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4477 | return 0; |
| 4478 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4479 | if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) || |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 4480 | ((req->flags & CF_SHUTW) && (req->to_forward || co_data(req)))) { |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 4481 | /* Output closed while we were sending data. We must abort and |
| 4482 | * wake the other side up. |
| 4483 | */ |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 4484 | msg->err_state = msg->msg_state; |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 4485 | msg->msg_state = HTTP_MSG_ERROR; |
| 4486 | http_resync_states(s); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 4487 | return 1; |
| 4488 | } |
| 4489 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4490 | /* Note that we don't have to send 100-continue back because we don't |
| 4491 | * need the data to complete our job, and it's up to the server to |
| 4492 | * decide whether to return 100, 417 or anything else in return of |
| 4493 | * an "Expect: 100-continue" header. |
| 4494 | */ |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 4495 | if (msg->msg_state == HTTP_MSG_BODY) { |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4496 | msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK) |
| 4497 | ? HTTP_MSG_CHUNK_SIZE |
| 4498 | : HTTP_MSG_DATA); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 4499 | |
| 4500 | /* TODO/filters: when http-buffer-request option is set or if a |
| 4501 | * rule on url_param exists, the first chunk size could be |
| 4502 | * already parsed. In that case, msg->next is after the chunk |
| 4503 | * size (including the CRLF after the size). So this case should |
| 4504 | * be handled to */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4505 | } |
| 4506 | |
Willy Tarreau | 7ba2354 | 2014-04-17 21:50:00 +0200 | [diff] [blame] | 4507 | /* Some post-connect processing might want us to refrain from starting to |
| 4508 | * forward data. Currently, the only reason for this is "balance url_param" |
| 4509 | * whichs need to parse/process the request after we've enabled forwarding. |
| 4510 | */ |
| 4511 | if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4512 | if (!(s->res.flags & CF_READ_ATTACHED)) { |
Willy Tarreau | 7ba2354 | 2014-04-17 21:50:00 +0200 | [diff] [blame] | 4513 | channel_auto_connect(req); |
Willy Tarreau | 644c101 | 2014-04-30 18:11:11 +0200 | [diff] [blame] | 4514 | req->flags |= CF_WAKE_CONNECT; |
Willy Tarreau | ba20dfc | 2018-05-16 11:35:05 +0200 | [diff] [blame] | 4515 | channel_dont_close(req); /* don't fail on early shutr */ |
| 4516 | goto waiting; |
Willy Tarreau | 7ba2354 | 2014-04-17 21:50:00 +0200 | [diff] [blame] | 4517 | } |
| 4518 | msg->flags &= ~HTTP_MSGF_WAIT_CONN; |
| 4519 | } |
| 4520 | |
Willy Tarreau | 80a92c0 | 2014-03-12 10:41:13 +0100 | [diff] [blame] | 4521 | /* in most states, we should abort in case of early close */ |
| 4522 | channel_auto_close(req); |
| 4523 | |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 4524 | if (req->to_forward) { |
| 4525 | /* We can't process the buffer's contents yet */ |
| 4526 | req->flags |= CF_WAKE_WRITE; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4527 | goto missing_data_or_waiting; |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 4528 | } |
| 4529 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4530 | if (msg->msg_state < HTTP_MSG_DONE) { |
| 4531 | ret = ((msg->flags & HTTP_MSGF_TE_CHNK) |
| 4532 | ? http_msg_forward_chunked_body(s, msg) |
| 4533 | : http_msg_forward_body(s, msg)); |
| 4534 | if (!ret) |
| 4535 | goto missing_data_or_waiting; |
| 4536 | if (ret < 0) |
| 4537 | goto return_bad_req; |
| 4538 | } |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 4539 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4540 | /* other states, DONE...TUNNEL */ |
| 4541 | /* we don't want to forward closes on DONE except in tunnel mode. */ |
| 4542 | if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) |
| 4543 | channel_dont_close(req); |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 4544 | |
Christopher Faulet | 894da4c | 2017-07-18 11:29:07 +0200 | [diff] [blame] | 4545 | http_resync_states(s); |
| 4546 | if (!(req->analysers & an_bit)) { |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4547 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 4548 | if (req->flags & CF_SHUTW) { |
| 4549 | /* request errors are most likely due to the |
| 4550 | * server aborting the transfer. */ |
| 4551 | goto aborted_xfer; |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 4552 | } |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4553 | if (msg->err_pos >= 0) |
Willy Tarreau | fd9419d | 2018-09-07 18:01:03 +0200 | [diff] [blame] | 4554 | http_capture_bad_message(sess->fe, s, msg, msg->err_state, s->be); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4555 | goto return_bad_req; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4556 | } |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4557 | return 1; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4558 | } |
| 4559 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4560 | /* If "option abortonclose" is set on the backend, we want to monitor |
| 4561 | * the client's connection and forward any shutdown notification to the |
| 4562 | * server, which will decide whether to close or to go on processing the |
| 4563 | * request. We only do that in tunnel mode, and not in other modes since |
| 4564 | * it can be abused to exhaust source ports. */ |
Willy Tarreau | 7aa15b0 | 2017-12-20 16:56:50 +0100 | [diff] [blame] | 4565 | if ((s->be->options & PR_O_ABRT_CLOSE) && !(s->si[0].flags & SI_FL_CLEAN_ABRT)) { |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4566 | channel_auto_read(req); |
| 4567 | if ((req->flags & (CF_SHUTR|CF_READ_NULL)) && |
| 4568 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)) |
| 4569 | s->si[1].flags |= SI_FL_NOLINGER; |
| 4570 | channel_auto_close(req); |
| 4571 | } |
| 4572 | else if (s->txn->meth == HTTP_METH_POST) { |
| 4573 | /* POST requests may require to read extra CRLF sent by broken |
| 4574 | * browsers and which could cause an RST to be sent upon close |
| 4575 | * on some systems (eg: Linux). */ |
| 4576 | channel_auto_read(req); |
| 4577 | } |
| 4578 | return 0; |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 4579 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4580 | missing_data_or_waiting: |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4581 | /* stop waiting for data if the input is closed before the end */ |
Christopher Faulet | a33510b | 2017-03-31 15:37:29 +0200 | [diff] [blame] | 4582 | if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR) { |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4583 | if (!(s->flags & SF_ERR_MASK)) |
| 4584 | s->flags |= SF_ERR_CLICL; |
| 4585 | if (!(s->flags & SF_FINST_MASK)) { |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4586 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4587 | s->flags |= SF_FINST_H; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4588 | else |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4589 | s->flags |= SF_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4590 | } |
| 4591 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 4592 | HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1); |
| 4593 | HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4594 | if (objt_server(s->target)) |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 4595 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4596 | |
| 4597 | goto return_bad_req_stats_ok; |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 4598 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4599 | |
Willy Tarreau | ba20dfc | 2018-05-16 11:35:05 +0200 | [diff] [blame] | 4600 | waiting: |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4601 | /* waiting for the last bits to leave the buffer */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4602 | if (req->flags & CF_SHUTW) |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4603 | goto aborted_xfer; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4604 | |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 4605 | /* When TE: chunked is used, we need to get there again to parse remaining |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4606 | * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE. |
Willy Tarreau | ff47b3f | 2017-12-29 16:30:31 +0100 | [diff] [blame] | 4607 | * And when content-length is used, we never want to let the possible |
| 4608 | * shutdown be forwarded to the other side, as the state machine will |
| 4609 | * take care of it once the client responds. It's also important to |
| 4610 | * prevent TIME_WAITs from accumulating on the backend side, and for |
| 4611 | * HTTP/2 where the last frame comes with a shutdown. |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 4612 | */ |
Willy Tarreau | ff47b3f | 2017-12-29 16:30:31 +0100 | [diff] [blame] | 4613 | if (msg->flags & (HTTP_MSGF_TE_CHNK|HTTP_MSGF_CNT_LEN)) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4614 | channel_dont_close(req); |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 4615 | |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 4616 | /* We know that more data are expected, but we couldn't send more that |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4617 | * what we did. So we always set the CF_EXPECT_MORE flag so that the |
Willy Tarreau | 0729303 | 2011-05-30 18:29:28 +0200 | [diff] [blame] | 4618 | * system knows it must not set a PUSH on this first part. Interactive |
Willy Tarreau | 869fc1e | 2012-03-05 08:29:20 +0100 | [diff] [blame] | 4619 | * modes are already handled by the stream sock layer. We must not do |
| 4620 | * this in content-length mode because it could present the MSG_MORE |
| 4621 | * flag with the last block of forwarded data, which would cause an |
| 4622 | * additional delay to be observed by the receiver. |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 4623 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4624 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4625 | req->flags |= CF_EXPECT_MORE; |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 4626 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4627 | return 0; |
| 4628 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4629 | return_bad_req: /* let's centralize all bad requests */ |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 4630 | HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4631 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 4632 | HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1); |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 4633 | |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4634 | return_bad_req_stats_ok: |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 4635 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4636 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4637 | if (txn->status) { |
| 4638 | /* Note: we don't send any error if some data were already sent */ |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 4639 | http_reply_and_close(s, txn->status, NULL); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4640 | } else { |
| 4641 | txn->status = 400; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 4642 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4643 | } |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4644 | req->analysers &= AN_REQ_FLT_END; |
| 4645 | s->res.analysers &= AN_RES_FLT_END; /* we're in data phase, we want to abort both directions */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4646 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4647 | if (!(s->flags & SF_ERR_MASK)) |
| 4648 | s->flags |= SF_ERR_PRXCOND; |
| 4649 | if (!(s->flags & SF_FINST_MASK)) { |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4650 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4651 | s->flags |= SF_FINST_H; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4652 | else |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4653 | s->flags |= SF_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4654 | } |
| 4655 | return 0; |
| 4656 | |
| 4657 | aborted_xfer: |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 4658 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4659 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4660 | if (txn->status) { |
| 4661 | /* Note: we don't send any error if some data were already sent */ |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 4662 | http_reply_and_close(s, txn->status, NULL); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4663 | } else { |
| 4664 | txn->status = 502; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 4665 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4666 | } |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4667 | req->analysers &= AN_REQ_FLT_END; |
| 4668 | s->res.analysers &= AN_RES_FLT_END; /* we're in data phase, we want to abort both directions */ |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4669 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 4670 | HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1); |
| 4671 | HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4672 | if (objt_server(s->target)) |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 4673 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4674 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4675 | if (!(s->flags & SF_ERR_MASK)) |
| 4676 | s->flags |= SF_ERR_SRVCL; |
| 4677 | if (!(s->flags & SF_FINST_MASK)) { |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4678 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4679 | s->flags |= SF_FINST_H; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4680 | else |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4681 | s->flags |= SF_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4682 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4683 | return 0; |
| 4684 | } |
| 4685 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4686 | /* This stream analyser waits for a complete HTTP response. It returns 1 if the |
| 4687 | * processing can continue on next analysers, or zero if it either needs more |
| 4688 | * data or wants to immediately abort the response (eg: timeout, error, ...). It |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4689 | * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4690 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 4691 | * abort. |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 4692 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4693 | int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit) |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 4694 | { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4695 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4696 | struct http_txn *txn = s->txn; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4697 | struct http_msg *msg = &txn->rsp; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4698 | struct hdr_ctx ctx; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4699 | int use_close_only; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4700 | int cur_idx; |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 4701 | int n; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 4702 | |
Christopher Faulet | 4507351 | 2018-07-20 10:16:29 +0200 | [diff] [blame] | 4703 | DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%lu analysers=%02x\n", |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 4704 | now_ms, __FUNCTION__, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4705 | s, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 4706 | rep, |
| 4707 | rep->rex, rep->wex, |
| 4708 | rep->flags, |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 4709 | ci_data(rep), |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 4710 | rep->analysers); |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 4711 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4712 | /* |
| 4713 | * Now parse the partial (or complete) lines. |
| 4714 | * We will check the response syntax, and also join multi-line |
| 4715 | * headers. An index of all the lines will be elaborated while |
| 4716 | * parsing. |
| 4717 | * |
| 4718 | * For the parsing, we use a 28 states FSM. |
| 4719 | * |
| 4720 | * Here is the information we currently have : |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 4721 | * ci_head(rep) = beginning of response |
| 4722 | * ci_head(rep) + msg->eoh = end of processed headers / start of current one |
| 4723 | * ci_tail(rep) = end of input data |
| 4724 | * msg->eol = end of current header or line (LF or CRLF) |
| 4725 | * msg->next = first non-visited byte |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4726 | */ |
| 4727 | |
Willy Tarreau | 628c40c | 2014-04-24 19:11:26 +0200 | [diff] [blame] | 4728 | next_one: |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 4729 | /* There's a protected area at the end of the buffer for rewriting |
| 4730 | * purposes. We don't want to start to parse the request if the |
| 4731 | * protected area is affected, because we may have to move processed |
| 4732 | * data later, which is much more complicated. |
| 4733 | */ |
Willy Tarreau | d760eec | 2018-07-10 09:50:25 +0200 | [diff] [blame] | 4734 | if (c_data(rep) && msg->msg_state < HTTP_MSG_ERROR) { |
Willy Tarreau | ba0902e | 2015-01-13 14:39:16 +0100 | [diff] [blame] | 4735 | if (unlikely(!channel_is_rewritable(rep))) { |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 4736 | /* some data has still not left the buffer, wake us once that's done */ |
| 4737 | if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) |
| 4738 | goto abort_response; |
| 4739 | channel_dont_close(rep); |
| 4740 | rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | d7ad9f5 | 2013-12-31 17:26:25 +0100 | [diff] [blame] | 4741 | rep->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 4742 | return 0; |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 4743 | } |
| 4744 | |
Willy Tarreau | 188e230 | 2018-06-15 11:11:53 +0200 | [diff] [blame] | 4745 | if (unlikely(ci_tail(rep) < c_ptr(rep, msg->next) || |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 4746 | ci_tail(rep) > b_wrap(&rep->buf) - global.tune.maxrewrite)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4747 | channel_slow_realign(rep, trash.area); |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 4748 | |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 4749 | if (likely(msg->next < ci_data(rep))) |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 4750 | http_msg_analyzer(msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 4751 | } |
| 4752 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4753 | /* 1: we might have to print this header in debug mode */ |
| 4754 | if (unlikely((global.mode & MODE_DEBUG) && |
| 4755 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | 7d59e90 | 2014-10-21 19:36:09 +0200 | [diff] [blame] | 4756 | msg->msg_state >= HTTP_MSG_BODY)) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4757 | char *eol, *sol; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4758 | |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 4759 | sol = ci_head(rep); |
| 4760 | eol = sol + (msg->sl.st.l ? msg->sl.st.l : ci_data(rep)); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4761 | debug_hdr("srvrep", s, sol, eol); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4762 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4763 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 4764 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4765 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4766 | while (cur_idx) { |
| 4767 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 4768 | debug_hdr("srvhdr", s, sol, eol); |
| 4769 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 4770 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
| 4771 | } |
| 4772 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4773 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4774 | /* |
| 4775 | * Now we quickly check if we have found a full valid response. |
| 4776 | * If not so, we check the FD and buffer states before leaving. |
| 4777 | * A full response is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4778 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4779 | * responses are checked first. |
| 4780 | * |
| 4781 | * Depending on whether the client is still there or not, we |
| 4782 | * may send an error response back or not. Note that normally |
| 4783 | * we should only check for HTTP status there, and check I/O |
| 4784 | * errors somewhere else. |
| 4785 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4786 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4787 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4788 | /* Invalid response */ |
| 4789 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 4790 | /* we detected a parsing error. We want to archive this response |
| 4791 | * in the dedicated proxy area for later troubleshooting. |
| 4792 | */ |
| 4793 | hdr_response_bad: |
| 4794 | if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0) |
Willy Tarreau | fd9419d | 2018-09-07 18:01:03 +0200 | [diff] [blame] | 4795 | http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4796 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 4797 | HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4798 | if (objt_server(s->target)) { |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 4799 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4800 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4801 | } |
Willy Tarreau | 6464841 | 2010-03-05 10:41:54 +0100 | [diff] [blame] | 4802 | abort_response: |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4803 | channel_auto_close(rep); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4804 | rep->analysers &= AN_RES_FLT_END; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4805 | txn->status = 502; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4806 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 4807 | channel_truncate(rep); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 4808 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4809 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4810 | if (!(s->flags & SF_ERR_MASK)) |
| 4811 | s->flags |= SF_ERR_PRXCOND; |
| 4812 | if (!(s->flags & SF_FINST_MASK)) |
| 4813 | s->flags |= SF_FINST_H; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4814 | |
| 4815 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4816 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4817 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4818 | /* too large response does not fit in buffer. */ |
Willy Tarreau | 2375233 | 2018-06-15 14:54:53 +0200 | [diff] [blame] | 4819 | else if (channel_full(rep, global.tune.maxrewrite)) { |
Willy Tarreau | fec4d89 | 2011-09-02 20:04:57 +0200 | [diff] [blame] | 4820 | if (msg->err_pos < 0) |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 4821 | msg->err_pos = ci_data(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4822 | goto hdr_response_bad; |
| 4823 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4824 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4825 | /* read error */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4826 | else if (rep->flags & CF_READ_ERROR) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4827 | if (msg->err_pos >= 0) |
Willy Tarreau | fd9419d | 2018-09-07 18:01:03 +0200 | [diff] [blame] | 4828 | http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe); |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 4829 | else if (txn->flags & TX_NOT_FIRST) |
| 4830 | goto abort_keep_alive; |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 4831 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 4832 | HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4833 | if (objt_server(s->target)) { |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 4834 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4835 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4836 | } |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 4837 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4838 | channel_auto_close(rep); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4839 | rep->analysers &= AN_RES_FLT_END; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4840 | txn->status = 502; |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 4841 | |
| 4842 | /* Check to see if the server refused the early data. |
| 4843 | * If so, just send a 425 |
| 4844 | */ |
| 4845 | if (objt_cs(s->si[1].end)) { |
| 4846 | struct connection *conn = objt_cs(s->si[1].end)->conn; |
| 4847 | |
| 4848 | if (conn->err_code == CO_ER_SSL_EARLY_FAILED) |
| 4849 | txn->status = 425; |
| 4850 | } |
| 4851 | |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4852 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 4853 | channel_truncate(rep); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 4854 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 4855 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4856 | if (!(s->flags & SF_ERR_MASK)) |
| 4857 | s->flags |= SF_ERR_SRVCL; |
| 4858 | if (!(s->flags & SF_FINST_MASK)) |
| 4859 | s->flags |= SF_FINST_H; |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 4860 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4861 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4862 | |
Willy Tarreau | 6f0a7ba | 2014-06-23 15:22:31 +0200 | [diff] [blame] | 4863 | /* read timeout : return a 504 to the client. */ |
| 4864 | else if (rep->flags & CF_READ_TIMEOUT) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4865 | if (msg->err_pos >= 0) |
Willy Tarreau | fd9419d | 2018-09-07 18:01:03 +0200 | [diff] [blame] | 4866 | http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4867 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 4868 | HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4869 | if (objt_server(s->target)) { |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 4870 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4871 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4872 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4873 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4874 | channel_auto_close(rep); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4875 | rep->analysers &= AN_RES_FLT_END; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4876 | txn->status = 504; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4877 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 4878 | channel_truncate(rep); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 4879 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 4880 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4881 | if (!(s->flags & SF_ERR_MASK)) |
| 4882 | s->flags |= SF_ERR_SRVTO; |
| 4883 | if (!(s->flags & SF_FINST_MASK)) |
| 4884 | s->flags |= SF_FINST_H; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4885 | return 0; |
| 4886 | } |
Willy Tarreau | a7c5276 | 2008-08-16 18:40:18 +0200 | [diff] [blame] | 4887 | |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 4888 | /* client abort with an abortonclose */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4889 | else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) { |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 4890 | HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1); |
| 4891 | HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1); |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 4892 | if (objt_server(s->target)) |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 4893 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1); |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 4894 | |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4895 | rep->analysers &= AN_RES_FLT_END; |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 4896 | channel_auto_close(rep); |
| 4897 | |
| 4898 | txn->status = 400; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 4899 | channel_truncate(rep); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 4900 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 4901 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4902 | if (!(s->flags & SF_ERR_MASK)) |
| 4903 | s->flags |= SF_ERR_CLICL; |
| 4904 | if (!(s->flags & SF_FINST_MASK)) |
| 4905 | s->flags |= SF_FINST_H; |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 4906 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4907 | /* process_stream() will take care of the error */ |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 4908 | return 0; |
| 4909 | } |
| 4910 | |
Willy Tarreau | 3b8c08a | 2011-09-02 20:16:24 +0200 | [diff] [blame] | 4911 | /* close from server, capture the response if the server has started to respond */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4912 | else if (rep->flags & CF_SHUTR) { |
Willy Tarreau | 3b8c08a | 2011-09-02 20:16:24 +0200 | [diff] [blame] | 4913 | if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0) |
Willy Tarreau | fd9419d | 2018-09-07 18:01:03 +0200 | [diff] [blame] | 4914 | http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe); |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 4915 | else if (txn->flags & TX_NOT_FIRST) |
| 4916 | goto abort_keep_alive; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4917 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 4918 | HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4919 | if (objt_server(s->target)) { |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 4920 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4921 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4922 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4923 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4924 | channel_auto_close(rep); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4925 | rep->analysers &= AN_RES_FLT_END; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4926 | txn->status = 502; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4927 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 4928 | channel_truncate(rep); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 4929 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4930 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4931 | if (!(s->flags & SF_ERR_MASK)) |
| 4932 | s->flags |= SF_ERR_SRVCL; |
| 4933 | if (!(s->flags & SF_FINST_MASK)) |
| 4934 | s->flags |= SF_FINST_H; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4935 | return 0; |
| 4936 | } |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 4937 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4938 | /* write error to client (we don't send any message then) */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4939 | else if (rep->flags & CF_WRITE_ERROR) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4940 | if (msg->err_pos >= 0) |
Willy Tarreau | fd9419d | 2018-09-07 18:01:03 +0200 | [diff] [blame] | 4941 | http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe); |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 4942 | else if (txn->flags & TX_NOT_FIRST) |
| 4943 | goto abort_keep_alive; |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 4944 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 4945 | HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4946 | rep->analysers &= AN_RES_FLT_END; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4947 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4948 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4949 | if (!(s->flags & SF_ERR_MASK)) |
| 4950 | s->flags |= SF_ERR_CLICL; |
| 4951 | if (!(s->flags & SF_FINST_MASK)) |
| 4952 | s->flags |= SF_FINST_H; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4953 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4954 | /* process_stream() will take care of the error */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4955 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4956 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4957 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4958 | channel_dont_close(rep); |
Willy Tarreau | 3f3997e | 2013-12-15 15:21:32 +0100 | [diff] [blame] | 4959 | rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4960 | return 0; |
| 4961 | } |
| 4962 | |
| 4963 | /* More interesting part now : we know that we have a complete |
| 4964 | * response which at least looks like HTTP. We have an indicator |
| 4965 | * of each header's length, so we can parse them quickly. |
| 4966 | */ |
| 4967 | |
| 4968 | if (unlikely(msg->err_pos >= 0)) |
Willy Tarreau | fd9419d | 2018-09-07 18:01:03 +0200 | [diff] [blame] | 4969 | http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4970 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4971 | /* |
| 4972 | * 1: get the status code |
| 4973 | */ |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 4974 | n = ci_head(rep)[msg->sl.st.c] - '0'; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4975 | if (n < 1 || n > 5) |
| 4976 | n = 0; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4977 | /* when the client triggers a 4xx from the server, it's most often due |
| 4978 | * to a missing object or permission. These events should be tracked |
| 4979 | * because if they happen often, it may indicate a brute force or a |
| 4980 | * vulnerability scan. |
| 4981 | */ |
| 4982 | if (n == 4) |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4983 | stream_inc_http_err_ctr(s); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4984 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4985 | if (objt_server(s->target)) |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 4986 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.p.http.rsp[n], 1); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4987 | |
Willy Tarreau | 91852eb | 2015-05-01 13:26:00 +0200 | [diff] [blame] | 4988 | /* RFC7230#2.6 has enforced the format of the HTTP version string to be |
| 4989 | * exactly one digit "." one digit. This check may be disabled using |
| 4990 | * option accept-invalid-http-response. |
| 4991 | */ |
| 4992 | if (!(s->be->options2 & PR_O2_RSPBUG_OK)) { |
| 4993 | if (msg->sl.st.v_l != 8) { |
| 4994 | msg->err_pos = 0; |
| 4995 | goto hdr_response_bad; |
| 4996 | } |
| 4997 | |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 4998 | if (ci_head(rep)[4] != '/' || |
| 4999 | !isdigit((unsigned char)ci_head(rep)[5]) || |
| 5000 | ci_head(rep)[6] != '.' || |
| 5001 | !isdigit((unsigned char)ci_head(rep)[7])) { |
Willy Tarreau | 91852eb | 2015-05-01 13:26:00 +0200 | [diff] [blame] | 5002 | msg->err_pos = 4; |
| 5003 | goto hdr_response_bad; |
| 5004 | } |
| 5005 | } |
| 5006 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5007 | /* check if the response is HTTP/1.1 or above */ |
| 5008 | if ((msg->sl.st.v_l == 8) && |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 5009 | ((ci_head(rep)[5] > '1') || |
| 5010 | ((ci_head(rep)[5] == '1') && (ci_head(rep)[7] >= '1')))) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5011 | msg->flags |= HTTP_MSGF_VER_11; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5012 | |
| 5013 | /* "connection" has not been parsed yet */ |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 5014 | txn->flags &= ~(TX_HDR_CONN_PRS|TX_HDR_CONN_CLO|TX_HDR_CONN_KAL|TX_HDR_CONN_UPG|TX_CON_CLO_SET|TX_CON_KAL_SET); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5015 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5016 | /* transfer length unknown*/ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5017 | msg->flags &= ~HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5018 | |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 5019 | txn->status = strl2ui(ci_head(rep) + msg->sl.st.c, msg->sl.st.c_l); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5020 | |
Willy Tarreau | 3965040 | 2010-03-15 19:44:39 +0100 | [diff] [blame] | 5021 | /* Adjust server's health based on status code. Note: status codes 501 |
| 5022 | * and 505 are triggered on demand by client request, so we must not |
| 5023 | * count them as server failures. |
| 5024 | */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5025 | if (objt_server(s->target)) { |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 5026 | if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505)) |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5027 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK); |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 5028 | else |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5029 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS); |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 5030 | } |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5031 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5032 | /* |
Willy Tarreau | a14ad72 | 2017-07-07 11:36:32 +0200 | [diff] [blame] | 5033 | * We may be facing a 100-continue response, or any other informational |
| 5034 | * 1xx response which is non-final, in which case this is not the right |
| 5035 | * response, and we're waiting for the next one. Let's allow this response |
| 5036 | * to go to the client and wait for the next one. There's an exception for |
| 5037 | * 101 which is used later in the code to switch protocols. |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5038 | */ |
Willy Tarreau | a14ad72 | 2017-07-07 11:36:32 +0200 | [diff] [blame] | 5039 | if (txn->status < 200 && |
| 5040 | (txn->status == 100 || txn->status >= 102)) { |
Willy Tarreau | 628c40c | 2014-04-24 19:11:26 +0200 | [diff] [blame] | 5041 | hdr_idx_init(&txn->hdr_idx); |
| 5042 | msg->next -= channel_forward(rep, msg->next); |
| 5043 | msg->msg_state = HTTP_MSG_RPBEFORE; |
| 5044 | txn->status = 0; |
| 5045 | s->logs.t_data = -1; /* was not a response yet */ |
Christopher Faulet | 3e34429 | 2015-11-24 16:24:13 +0100 | [diff] [blame] | 5046 | FLT_STRM_CB(s, flt_http_reset(s, msg)); |
Willy Tarreau | 628c40c | 2014-04-24 19:11:26 +0200 | [diff] [blame] | 5047 | goto next_one; |
Willy Tarreau | a14ad72 | 2017-07-07 11:36:32 +0200 | [diff] [blame] | 5048 | } |
Willy Tarreau | 628c40c | 2014-04-24 19:11:26 +0200 | [diff] [blame] | 5049 | |
Willy Tarreau | a14ad72 | 2017-07-07 11:36:32 +0200 | [diff] [blame] | 5050 | /* |
| 5051 | * 2: check for cacheability. |
| 5052 | */ |
| 5053 | |
| 5054 | switch (txn->status) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5055 | case 200: |
| 5056 | case 203: |
Willy Tarreau | c55ddce | 2017-12-21 11:41:38 +0100 | [diff] [blame] | 5057 | case 204: |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5058 | case 206: |
| 5059 | case 300: |
| 5060 | case 301: |
Willy Tarreau | c55ddce | 2017-12-21 11:41:38 +0100 | [diff] [blame] | 5061 | case 404: |
| 5062 | case 405: |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5063 | case 410: |
Willy Tarreau | c55ddce | 2017-12-21 11:41:38 +0100 | [diff] [blame] | 5064 | case 414: |
| 5065 | case 501: |
Willy Tarreau | 83ece46 | 2017-12-21 15:13:09 +0100 | [diff] [blame] | 5066 | break; |
| 5067 | default: |
Willy Tarreau | c55ddce | 2017-12-21 11:41:38 +0100 | [diff] [blame] | 5068 | /* RFC7231#6.1: |
| 5069 | * Responses with status codes that are defined as |
| 5070 | * cacheable by default (e.g., 200, 203, 204, 206, |
| 5071 | * 300, 301, 404, 405, 410, 414, and 501 in this |
| 5072 | * specification) can be reused by a cache with |
| 5073 | * heuristic expiration unless otherwise indicated |
| 5074 | * by the method definition or explicit cache |
| 5075 | * controls [RFC7234]; all other status codes are |
| 5076 | * not cacheable by default. |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5077 | */ |
Willy Tarreau | 83ece46 | 2017-12-21 15:13:09 +0100 | [diff] [blame] | 5078 | txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5079 | break; |
| 5080 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5081 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5082 | /* |
| 5083 | * 3: we may need to capture headers |
| 5084 | */ |
| 5085 | s->logs.logwait &= ~LW_RESP; |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 5086 | if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap)) |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 5087 | capture_headers(ci_head(rep), &txn->hdr_idx, |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 5088 | s->res_cap, sess->fe->rsp_cap); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5089 | |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 5090 | /* 4: determine the transfer-length according to RFC2616 #4.4, updated |
| 5091 | * by RFC7230#3.3.3 : |
| 5092 | * |
| 5093 | * The length of a message body is determined by one of the following |
| 5094 | * (in order of precedence): |
| 5095 | * |
Christopher Faulet | d1cd209 | 2016-11-28 10:14:03 +0100 | [diff] [blame] | 5096 | * 1. Any 2xx (Successful) response to a CONNECT request implies that |
| 5097 | * the connection will become a tunnel immediately after the empty |
| 5098 | * line that concludes the header fields. A client MUST ignore |
| 5099 | * any Content-Length or Transfer-Encoding header fields received |
| 5100 | * in such a message. Any 101 response (Switching Protocols) is |
| 5101 | * managed in the same manner. |
| 5102 | * |
| 5103 | * 2. Any response to a HEAD request and any response with a 1xx |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 5104 | * (Informational), 204 (No Content), or 304 (Not Modified) status |
| 5105 | * code is always terminated by the first empty line after the |
| 5106 | * header fields, regardless of the header fields present in the |
| 5107 | * message, and thus cannot contain a message body. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5108 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 5109 | * 3. If a Transfer-Encoding header field is present and the chunked |
| 5110 | * transfer coding (Section 4.1) is the final encoding, the message |
| 5111 | * body length is determined by reading and decoding the chunked |
| 5112 | * data until the transfer coding indicates the data is complete. |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5113 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 5114 | * If a Transfer-Encoding header field is present in a response and |
| 5115 | * the chunked transfer coding is not the final encoding, the |
| 5116 | * message body length is determined by reading the connection until |
| 5117 | * it is closed by the server. If a Transfer-Encoding header field |
| 5118 | * is present in a request and the chunked transfer coding is not |
| 5119 | * the final encoding, the message body length cannot be determined |
| 5120 | * reliably; the server MUST respond with the 400 (Bad Request) |
| 5121 | * status code and then close the connection. |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5122 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 5123 | * If a message is received with both a Transfer-Encoding and a |
| 5124 | * Content-Length header field, the Transfer-Encoding overrides the |
| 5125 | * Content-Length. Such a message might indicate an attempt to |
| 5126 | * perform request smuggling (Section 9.5) or response splitting |
| 5127 | * (Section 9.4) and ought to be handled as an error. A sender MUST |
| 5128 | * remove the received Content-Length field prior to forwarding such |
| 5129 | * a message downstream. |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5130 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 5131 | * 4. If a message is received without Transfer-Encoding and with |
| 5132 | * either multiple Content-Length header fields having differing |
| 5133 | * field-values or a single Content-Length header field having an |
| 5134 | * invalid value, then the message framing is invalid and the |
| 5135 | * recipient MUST treat it as an unrecoverable error. If this is a |
| 5136 | * request message, the server MUST respond with a 400 (Bad Request) |
| 5137 | * status code and then close the connection. If this is a response |
| 5138 | * message received by a proxy, the proxy MUST close the connection |
| 5139 | * to the server, discard the received response, and send a 502 (Bad |
| 5140 | * Gateway) response to the client. If this is a response message |
| 5141 | * received by a user agent, the user agent MUST close the |
| 5142 | * connection to the server and discard the received response. |
| 5143 | * |
| 5144 | * 5. If a valid Content-Length header field is present without |
| 5145 | * Transfer-Encoding, its decimal value defines the expected message |
| 5146 | * body length in octets. If the sender closes the connection or |
| 5147 | * the recipient times out before the indicated number of octets are |
| 5148 | * received, the recipient MUST consider the message to be |
| 5149 | * incomplete and close the connection. |
| 5150 | * |
| 5151 | * 6. If this is a request message and none of the above are true, then |
| 5152 | * the message body length is zero (no message body is present). |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5153 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 5154 | * 7. Otherwise, this is a response message without a declared message |
| 5155 | * body length, so the message body length is determined by the |
| 5156 | * number of octets received prior to the server closing the |
| 5157 | * connection. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5158 | */ |
| 5159 | |
| 5160 | /* Skip parsing if no content length is possible. The response flags |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 5161 | * remain 0 as well as the chunk_len, which may or may not mirror |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5162 | * the real header value, and we note that we know the response's length. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5163 | * FIXME: should we parse anyway and return an error on chunked encoding ? |
| 5164 | */ |
Christopher Faulet | d1cd209 | 2016-11-28 10:14:03 +0100 | [diff] [blame] | 5165 | if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || |
| 5166 | txn->status == 101)) { |
| 5167 | /* Either we've established an explicit tunnel, or we're |
| 5168 | * switching the protocol. In both cases, we're very unlikely |
| 5169 | * to understand the next protocols. We have to switch to tunnel |
| 5170 | * mode, so that we transfer the request and responses then let |
| 5171 | * this protocol pass unmodified. When we later implement specific |
| 5172 | * parsers for such protocols, we'll want to check the Upgrade |
| 5173 | * header which contains information about that protocol for |
| 5174 | * responses with status 101 (eg: see RFC2817 about TLS). |
| 5175 | */ |
| 5176 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN; |
| 5177 | msg->flags |= HTTP_MSGF_XFER_LEN; |
| 5178 | goto end; |
| 5179 | } |
| 5180 | |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5181 | if (txn->meth == HTTP_METH_HEAD || |
| 5182 | (txn->status >= 100 && txn->status < 200) || |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5183 | txn->status == 204 || txn->status == 304) { |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5184 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5185 | goto skip_content_length; |
| 5186 | } |
| 5187 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5188 | use_close_only = 0; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5189 | ctx.idx = 0; |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 5190 | while (http_find_header2("Transfer-Encoding", 17, ci_head(rep), &txn->hdr_idx, &ctx)) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5191 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5192 | msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
| 5193 | else if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5194 | /* bad transfer-encoding (chunked followed by something else) */ |
| 5195 | use_close_only = 1; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5196 | msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5197 | break; |
| 5198 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5199 | } |
| 5200 | |
Willy Tarreau | 1c91391 | 2015-04-30 10:57:51 +0200 | [diff] [blame] | 5201 | /* Chunked responses must have their content-length removed */ |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5202 | ctx.idx = 0; |
Willy Tarreau | b4d0c03 | 2015-05-01 10:25:45 +0200 | [diff] [blame] | 5203 | if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) { |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 5204 | while (http_find_header2("Content-Length", 14, ci_head(rep), &txn->hdr_idx, &ctx)) |
Willy Tarreau | 1c91391 | 2015-04-30 10:57:51 +0200 | [diff] [blame] | 5205 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 5206 | } |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 5207 | else while (http_find_header2("Content-Length", 14, ci_head(rep), &txn->hdr_idx, &ctx)) { |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5208 | signed long long cl; |
| 5209 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5210 | if (!ctx.vlen) { |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 5211 | msg->err_pos = ctx.line + ctx.val - ci_head(rep); |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5212 | goto hdr_response_bad; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5213 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5214 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5215 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) { |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 5216 | msg->err_pos = ctx.line + ctx.val - ci_head(rep); |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5217 | goto hdr_response_bad; /* parse failure */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5218 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5219 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5220 | if (cl < 0) { |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 5221 | msg->err_pos = ctx.line + ctx.val - ci_head(rep); |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5222 | goto hdr_response_bad; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5223 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5224 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5225 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) { |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 5226 | msg->err_pos = ctx.line + ctx.val - ci_head(rep); |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5227 | goto hdr_response_bad; /* already specified, was different */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5228 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5229 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5230 | msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 5231 | msg->body_len = msg->chunk_len = cl; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5232 | } |
| 5233 | |
Christopher Faulet | d1cd209 | 2016-11-28 10:14:03 +0100 | [diff] [blame] | 5234 | skip_content_length: |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5235 | /* Now we have to check if we need to modify the Connection header. |
| 5236 | * This is more difficult on the response than it is on the request, |
| 5237 | * because we can have two different HTTP versions and we don't know |
| 5238 | * how the client will interprete a response. For instance, let's say |
| 5239 | * that the client sends a keep-alive request in HTTP/1.0 and gets an |
| 5240 | * HTTP/1.1 response without any header. Maybe it will bound itself to |
| 5241 | * HTTP/1.0 because it only knows about it, and will consider the lack |
| 5242 | * of header as a close, or maybe it knows HTTP/1.1 and can consider |
| 5243 | * the lack of header as a keep-alive. Thus we will use two flags |
| 5244 | * indicating how a request MAY be understood by the client. In case |
| 5245 | * of multiple possibilities, we'll fix the header to be explicit. If |
| 5246 | * ambiguous cases such as both close and keepalive are seen, then we |
| 5247 | * will fall back to explicit close. Note that we won't take risks with |
| 5248 | * HTTP/1.0 clients which may not necessarily understand keep-alive. |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5249 | * See doc/internals/connection-header.txt for the complete matrix. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5250 | */ |
Christopher Faulet | d1cd209 | 2016-11-28 10:14:03 +0100 | [diff] [blame] | 5251 | if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) && |
| 5252 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN || |
| 5253 | ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
| 5254 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) { |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5255 | int to_del = 0; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5256 | |
Willy Tarreau | 70dffda | 2014-01-30 03:07:23 +0100 | [diff] [blame] | 5257 | /* this situation happens when combining pretend-keepalive with httpclose. */ |
| 5258 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5259 | ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 5260 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) |
Willy Tarreau | 70dffda | 2014-01-30 03:07:23 +0100 | [diff] [blame] | 5261 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 5262 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5263 | /* on unknown transfer length, we must close */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5264 | if (!(msg->flags & HTTP_MSGF_XFER_LEN) && |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5265 | (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) |
| 5266 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5267 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5268 | /* now adjust header transformations depending on current state */ |
| 5269 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN || |
| 5270 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 5271 | to_del |= 2; /* remove "keep-alive" on any response */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5272 | if (!(msg->flags & HTTP_MSGF_VER_11)) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5273 | to_del |= 1; /* remove "close" for HTTP/1.0 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5274 | } |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5275 | else { /* SCL / KAL */ |
| 5276 | to_del |= 1; /* remove "close" on any response */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5277 | if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5278 | to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5279 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5280 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5281 | /* Parse and remove some headers from the connection header */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 5282 | http_parse_connection_header(txn, msg, to_del); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5283 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5284 | /* Some keep-alive responses are converted to Server-close if |
| 5285 | * the server wants to close. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5286 | */ |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5287 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) { |
| 5288 | if ((txn->flags & TX_HDR_CONN_CLO) || |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5289 | (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11))) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5290 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 5291 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5292 | } |
| 5293 | |
Christopher Faulet | d1cd209 | 2016-11-28 10:14:03 +0100 | [diff] [blame] | 5294 | end: |
Willy Tarreau | 7959a55 | 2013-09-23 16:44:27 +0200 | [diff] [blame] | 5295 | /* we want to have the response time before we start processing it */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 5296 | s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now); |
Willy Tarreau | 7959a55 | 2013-09-23 16:44:27 +0200 | [diff] [blame] | 5297 | |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 5298 | /* end of job, return OK */ |
| 5299 | rep->analysers &= ~an_bit; |
| 5300 | rep->analyse_exp = TICK_ETERNITY; |
| 5301 | channel_auto_close(rep); |
| 5302 | return 1; |
| 5303 | |
| 5304 | abort_keep_alive: |
| 5305 | /* A keep-alive request to the server failed on a network error. |
| 5306 | * The client is required to retry. We need to close without returning |
| 5307 | * any other information so that the client retries. |
| 5308 | */ |
| 5309 | txn->status = 0; |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 5310 | rep->analysers &= AN_RES_FLT_END; |
| 5311 | s->req.analysers &= AN_REQ_FLT_END; |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 5312 | channel_auto_close(rep); |
| 5313 | s->logs.logwait = 0; |
| 5314 | s->logs.level = 0; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5315 | s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5316 | channel_truncate(rep); |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 5317 | http_reply_and_close(s, txn->status, NULL); |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 5318 | return 0; |
| 5319 | } |
| 5320 | |
| 5321 | /* This function performs all the processing enabled for the current response. |
| 5322 | * It normally returns 1 unless it wants to break. It relies on buffers flags, |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5323 | * and updates s->res.analysers. It might make sense to explode it into several |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 5324 | * other functions. It works like process_request (see indications above). |
| 5325 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5326 | int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px) |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 5327 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 5328 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5329 | struct http_txn *txn = s->txn; |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 5330 | struct http_msg *msg = &txn->rsp; |
| 5331 | struct proxy *cur_proxy; |
| 5332 | struct cond_wordlist *wl; |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 5333 | enum rule_result ret = HTTP_RULE_RES_CONT; |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 5334 | |
Christopher Faulet | 4507351 | 2018-07-20 10:16:29 +0200 | [diff] [blame] | 5335 | DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%lu analysers=%02x\n", |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 5336 | now_ms, __FUNCTION__, |
| 5337 | s, |
| 5338 | rep, |
| 5339 | rep->rex, rep->wex, |
| 5340 | rep->flags, |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 5341 | ci_data(rep), |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 5342 | rep->analysers); |
| 5343 | |
| 5344 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */ |
| 5345 | return 0; |
| 5346 | |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 5347 | /* The stats applet needs to adjust the Connection header but we don't |
| 5348 | * apply any filter there. |
| 5349 | */ |
Willy Tarreau | 612adb8 | 2015-03-10 15:25:54 +0100 | [diff] [blame] | 5350 | if (unlikely(objt_applet(s->target) == &http_stats_applet)) { |
| 5351 | rep->analysers &= ~an_bit; |
| 5352 | rep->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 5353 | goto skip_filters; |
Willy Tarreau | 612adb8 | 2015-03-10 15:25:54 +0100 | [diff] [blame] | 5354 | } |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 5355 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5356 | /* |
| 5357 | * We will have to evaluate the filters. |
| 5358 | * As opposed to version 1.2, now they will be evaluated in the |
| 5359 | * filters order and not in the header order. This means that |
| 5360 | * each filter has to be validated among all headers. |
| 5361 | * |
| 5362 | * Filters are tried with ->be first, then with ->fe if it is |
| 5363 | * different from ->be. |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 5364 | * |
| 5365 | * Maybe we are in resume condiion. In this case I choose the |
| 5366 | * "struct proxy" which contains the rule list matching the resume |
| 5367 | * pointer. If none of theses "struct proxy" match, I initialise |
| 5368 | * the process with the first one. |
| 5369 | * |
| 5370 | * In fact, I check only correspondance betwwen the current list |
| 5371 | * pointer and the ->fe rule list. If it doesn't match, I initialize |
| 5372 | * the loop with the ->be. |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5373 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5374 | if (s->current_rule_list == &sess->fe->http_res_rules) |
| 5375 | cur_proxy = sess->fe; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 5376 | else |
| 5377 | cur_proxy = s->be; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5378 | while (1) { |
| 5379 | struct proxy *rule_set = cur_proxy; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5380 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5381 | /* evaluate http-response rules */ |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 5382 | if (ret == HTTP_RULE_RES_CONT) { |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 5383 | ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s); |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 5384 | |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 5385 | if (ret == HTTP_RULE_RES_BADREQ) |
| 5386 | goto return_srv_prx_502; |
| 5387 | |
| 5388 | if (ret == HTTP_RULE_RES_DONE) { |
| 5389 | rep->analysers &= ~an_bit; |
| 5390 | rep->analyse_exp = TICK_ETERNITY; |
| 5391 | return 1; |
| 5392 | } |
| 5393 | } |
| 5394 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 5395 | /* we need to be called again. */ |
| 5396 | if (ret == HTTP_RULE_RES_YIELD) { |
| 5397 | channel_dont_close(rep); |
| 5398 | return 0; |
| 5399 | } |
| 5400 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5401 | /* try headers filters */ |
| 5402 | if (rule_set->rsp_exp != NULL) { |
| 5403 | if (apply_filters_to_response(s, rep, rule_set) < 0) { |
| 5404 | return_bad_resp: |
| 5405 | if (objt_server(s->target)) { |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 5406 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1); |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5407 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5408 | } |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 5409 | HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1); |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5410 | return_srv_prx_502: |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 5411 | rep->analysers &= AN_RES_FLT_END; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5412 | txn->status = 502; |
| 5413 | s->logs.t_data = -1; /* was not a valid response */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5414 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5415 | channel_truncate(rep); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 5416 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5417 | if (!(s->flags & SF_ERR_MASK)) |
| 5418 | s->flags |= SF_ERR_PRXCOND; |
| 5419 | if (!(s->flags & SF_FINST_MASK)) |
| 5420 | s->flags |= SF_FINST_H; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5421 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5422 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5423 | } |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5424 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5425 | /* has the response been denied ? */ |
| 5426 | if (txn->flags & TX_SVDENY) { |
| 5427 | if (objt_server(s->target)) |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 5428 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5429 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 5430 | HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1); |
| 5431 | HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 5432 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 5433 | HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5434 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5435 | goto return_srv_prx_502; |
| 5436 | } |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 5437 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5438 | /* add response headers from the rule sets in the same order */ |
| 5439 | list_for_each_entry(wl, &rule_set->rsp_add, list) { |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 5440 | if (txn->status < 200 && txn->status != 101) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5441 | break; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5442 | if (wl->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 5443 | int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL); |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5444 | ret = acl_pass(ret); |
| 5445 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 5446 | ret = !ret; |
| 5447 | if (!ret) |
| 5448 | continue; |
| 5449 | } |
| 5450 | if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0)) |
| 5451 | goto return_bad_resp; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5452 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5453 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5454 | /* check whether we're already working on the frontend */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5455 | if (cur_proxy == sess->fe) |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5456 | break; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5457 | cur_proxy = sess->fe; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5458 | } |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5459 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 5460 | /* After this point, this anayzer can't return yield, so we can |
| 5461 | * remove the bit corresponding to this analyzer from the list. |
| 5462 | * |
| 5463 | * Note that the intermediate returns and goto found previously |
| 5464 | * reset the analyzers. |
| 5465 | */ |
| 5466 | rep->analysers &= ~an_bit; |
| 5467 | rep->analyse_exp = TICK_ETERNITY; |
| 5468 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5469 | /* OK that's all we can do for 1xx responses */ |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 5470 | if (unlikely(txn->status < 200 && txn->status != 101)) |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5471 | goto skip_header_mangling; |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5472 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5473 | /* |
| 5474 | * Now check for a server cookie. |
| 5475 | */ |
Willy Tarreau | 53a09d5 | 2015-08-10 18:59:40 +0200 | [diff] [blame] | 5476 | if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE)) |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5477 | manage_server_side_cookies(s, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5478 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5479 | /* |
| 5480 | * Check for cache-control or pragma headers if required. |
| 5481 | */ |
Willy Tarreau | 12b32f2 | 2017-12-21 16:08:09 +0100 | [diff] [blame] | 5482 | if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)) |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5483 | check_response_for_cacheability(s, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5484 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5485 | /* |
| 5486 | * Add server cookie in the response if needed |
| 5487 | */ |
| 5488 | if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) && |
| 5489 | !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) && |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5490 | (!(s->flags & SF_DIRECT) || |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5491 | ((s->be->cookie_maxidle || txn->cookie_last_date) && |
| 5492 | (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) || |
| 5493 | (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date |
| 5494 | (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date |
| 5495 | (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) && |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5496 | !(s->flags & SF_IGNORE_PRST)) { |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5497 | /* the server is known, it's not the one the client requested, or the |
| 5498 | * cookie's last seen date needs to be refreshed. We have to |
| 5499 | * insert a set-cookie here, except if we want to insert only on POST |
| 5500 | * requests and this one isn't. Note that servers which don't have cookies |
| 5501 | * (eg: some backup servers) will return a full cookie removal request. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5502 | */ |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5503 | if (!objt_server(s->target)->cookie) { |
| 5504 | chunk_printf(&trash, |
| 5505 | "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/", |
| 5506 | s->be->cookie_name); |
| 5507 | } |
| 5508 | else { |
| 5509 | chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5510 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5511 | if (s->be->cookie_maxidle || s->be->cookie_maxlife) { |
| 5512 | /* emit last_date, which is mandatory */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5513 | trash.area[trash.data++] = COOKIE_DELIM_DATE; |
| 5514 | s30tob64((date.tv_sec+3) >> 2, |
| 5515 | trash.area + trash.data); |
| 5516 | trash.data += 5; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5517 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5518 | if (s->be->cookie_maxlife) { |
| 5519 | /* emit first_date, which is either the original one or |
| 5520 | * the current date. |
| 5521 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5522 | trash.area[trash.data++] = COOKIE_DELIM_DATE; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5523 | s30tob64(txn->cookie_first_date ? |
| 5524 | txn->cookie_first_date >> 2 : |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5525 | (date.tv_sec+3) >> 2, |
| 5526 | trash.area + trash.data); |
| 5527 | trash.data += 5; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5528 | } |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5529 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5530 | chunk_appendf(&trash, "; path=/"); |
| 5531 | } |
Willy Tarreau | 4992dd2 | 2012-05-31 21:02:17 +0200 | [diff] [blame] | 5532 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5533 | if (s->be->cookie_domain) |
| 5534 | chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain); |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5535 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5536 | if (s->be->ck_opts & PR_CK_HTTPONLY) |
| 5537 | chunk_appendf(&trash, "; HttpOnly"); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5538 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5539 | if (s->be->ck_opts & PR_CK_SECURE) |
| 5540 | chunk_appendf(&trash, "; Secure"); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5541 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5542 | if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.area, trash.data) < 0)) |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5543 | goto return_bad_resp; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5544 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5545 | txn->flags &= ~TX_SCK_MASK; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5546 | if (objt_server(s->target)->cookie && (s->flags & SF_DIRECT)) |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5547 | /* the server did not change, only the date was updated */ |
| 5548 | txn->flags |= TX_SCK_UPDATED; |
| 5549 | else |
| 5550 | txn->flags |= TX_SCK_INSERTED; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5551 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5552 | /* Here, we will tell an eventual cache on the client side that we don't |
| 5553 | * want it to cache this reply because HTTP/1.0 caches also cache cookies ! |
| 5554 | * Some caches understand the correct form: 'no-cache="set-cookie"', but |
| 5555 | * others don't (eg: apache <= 1.3.26). So we use 'private' instead. |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5556 | */ |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5557 | if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5558 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5559 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5560 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5561 | if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, |
| 5562 | "Cache-control: private", 22) < 0)) |
| 5563 | goto return_bad_resp; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5564 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5565 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5566 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5567 | /* |
| 5568 | * Check if result will be cacheable with a cookie. |
| 5569 | * We'll block the response if security checks have caught |
| 5570 | * nasty things such as a cacheable cookie. |
| 5571 | */ |
| 5572 | if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) == |
| 5573 | (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) && |
| 5574 | (s->be->options & PR_O_CHK_CACHE)) { |
| 5575 | /* we're in presence of a cacheable response containing |
| 5576 | * a set-cookie header. We'll block it as requested by |
| 5577 | * the 'checkcache' option, and send an alert. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5578 | */ |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5579 | if (objt_server(s->target)) |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 5580 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1); |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5581 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 5582 | HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1); |
| 5583 | HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 5584 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 5585 | HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5586 | |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5587 | ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 5588 | s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>"); |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5589 | send_log(s->be, LOG_ALERT, |
| 5590 | "Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 5591 | s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>"); |
| 5592 | goto return_srv_prx_502; |
| 5593 | } |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 5594 | |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 5595 | skip_filters: |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5596 | /* |
| 5597 | * Adjust "Connection: close" or "Connection: keep-alive" if needed. |
| 5598 | * If an "Upgrade" token is found, the header is left untouched in order |
| 5599 | * not to have to deal with some client bugs : some of them fail an upgrade |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 5600 | * if anything but "Upgrade" is present in the Connection header. We don't |
| 5601 | * want to touch any 101 response either since it's switching to another |
| 5602 | * protocol. |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5603 | */ |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 5604 | if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) && |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5605 | (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5606 | ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5607 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) { |
| 5608 | unsigned int want_flags = 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5609 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5610 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 5611 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 5612 | /* we want a keep-alive response here. Keep-alive header |
| 5613 | * required if either side is not 1.1. |
| 5614 | */ |
| 5615 | if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11)) |
| 5616 | want_flags |= TX_CON_KAL_SET; |
| 5617 | } |
| 5618 | else { |
| 5619 | /* we want a close response here. Close header required if |
| 5620 | * the server is 1.1, regardless of the client. |
| 5621 | */ |
| 5622 | if (msg->flags & HTTP_MSGF_VER_11) |
| 5623 | want_flags |= TX_CON_CLO_SET; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5624 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5625 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5626 | if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 5627 | http_change_connection_header(txn, msg, want_flags); |
| 5628 | } |
| 5629 | |
| 5630 | skip_header_mangling: |
Christopher Faulet | 69744d9 | 2017-03-30 10:54:35 +0200 | [diff] [blame] | 5631 | /* Always enter in the body analyzer */ |
| 5632 | rep->analysers &= ~AN_RES_FLT_XFER_DATA; |
| 5633 | rep->analysers |= AN_RES_HTTP_XFER_BODY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5634 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5635 | /* if the user wants to log as soon as possible, without counting |
| 5636 | * bytes from the server, then this is the right moment. We have |
| 5637 | * to temporarily assign bytes_out to log what we currently have. |
| 5638 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5639 | if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) { |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5640 | s->logs.t_close = s->logs.t_data; /* to get a valid end date */ |
| 5641 | s->logs.bytes_out = txn->rsp.eoh; |
| 5642 | s->do_log(s); |
| 5643 | s->logs.bytes_out = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5644 | } |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 5645 | return 1; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5646 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5647 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5648 | /* This function is an analyser which forwards response body (including chunk |
| 5649 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 5650 | * zero byte. The only situation where it must not be called is when we're in |
| 5651 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 5652 | * remaining data and to resync after end of body. It expects the msg_state to |
| 5653 | * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5654 | * read more data, or 1 once we can go on with next request or end the stream. |
Willy Tarreau | d351021 | 2014-04-21 11:24:13 +0200 | [diff] [blame] | 5655 | * |
| 5656 | * It is capable of compressing response data both in content-length mode and |
| 5657 | * in chunked mode. The state machines follows different flows depending on |
| 5658 | * whether content-length and chunked modes are used, since there are no |
| 5659 | * trailers in content-length : |
| 5660 | * |
| 5661 | * chk-mode cl-mode |
| 5662 | * ,----- BODY -----. |
| 5663 | * / \ |
| 5664 | * V size > 0 V chk-mode |
| 5665 | * .--> SIZE -------------> DATA -------------> CRLF |
| 5666 | * | | size == 0 | last byte | |
| 5667 | * | v final crlf v inspected | |
| 5668 | * | TRAILERS -----------> DONE | |
| 5669 | * | | |
| 5670 | * `----------------------------------------------' |
| 5671 | * |
| 5672 | * Compression only happens in the DATA state, and must be flushed in final |
| 5673 | * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding |
| 5674 | * is performed at once on final states for all bytes parsed, or when leaving |
| 5675 | * on missing data. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5676 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5677 | int http_response_forward_body(struct stream *s, struct channel *res, int an_bit) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5678 | { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5679 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5680 | struct http_txn *txn = s->txn; |
| 5681 | struct http_msg *msg = &s->txn->rsp; |
Christopher Faulet | 3e34429 | 2015-11-24 16:24:13 +0100 | [diff] [blame] | 5682 | int ret; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5683 | |
Christopher Faulet | 4507351 | 2018-07-20 10:16:29 +0200 | [diff] [blame] | 5684 | DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%lu analysers=%02x\n", |
Christopher Faulet | 814d270 | 2017-03-30 11:33:44 +0200 | [diff] [blame] | 5685 | now_ms, __FUNCTION__, |
| 5686 | s, |
| 5687 | res, |
| 5688 | res->rex, res->wex, |
| 5689 | res->flags, |
Christopher Faulet | 4507351 | 2018-07-20 10:16:29 +0200 | [diff] [blame] | 5690 | ci_data(res), |
Christopher Faulet | 814d270 | 2017-03-30 11:33:44 +0200 | [diff] [blame] | 5691 | res->analysers); |
| 5692 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5693 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 5694 | return 0; |
| 5695 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5696 | if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) || |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 5697 | ((res->flags & CF_SHUTW) && (res->to_forward || co_data(res))) || |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 5698 | !s->req.analysers) { |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 5699 | /* Output closed while we were sending data. We must abort and |
| 5700 | * wake the other side up. |
| 5701 | */ |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 5702 | msg->err_state = msg->msg_state; |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 5703 | msg->msg_state = HTTP_MSG_ERROR; |
| 5704 | http_resync_states(s); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 5705 | return 1; |
| 5706 | } |
| 5707 | |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 5708 | /* in most states, we should abort in case of early close */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5709 | channel_auto_close(res); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 5710 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 5711 | if (msg->msg_state == HTTP_MSG_BODY) { |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5712 | msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK) |
| 5713 | ? HTTP_MSG_CHUNK_SIZE |
| 5714 | : HTTP_MSG_DATA); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5715 | } |
| 5716 | |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 5717 | if (res->to_forward) { |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5718 | /* We can't process the buffer's contents yet */ |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 5719 | res->flags |= CF_WAKE_WRITE; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5720 | goto missing_data_or_waiting; |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 5721 | } |
| 5722 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5723 | if (msg->msg_state < HTTP_MSG_DONE) { |
| 5724 | ret = ((msg->flags & HTTP_MSGF_TE_CHNK) |
| 5725 | ? http_msg_forward_chunked_body(s, msg) |
| 5726 | : http_msg_forward_body(s, msg)); |
| 5727 | if (!ret) |
| 5728 | goto missing_data_or_waiting; |
| 5729 | if (ret < 0) |
| 5730 | goto return_bad_res; |
| 5731 | } |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 5732 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5733 | /* other states, DONE...TUNNEL */ |
| 5734 | /* for keep-alive we don't want to forward closes on DONE */ |
| 5735 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 5736 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
| 5737 | channel_dont_close(res); |
Willy Tarreau | 3ce10ff | 2014-04-22 08:24:38 +0200 | [diff] [blame] | 5738 | |
Christopher Faulet | 894da4c | 2017-07-18 11:29:07 +0200 | [diff] [blame] | 5739 | http_resync_states(s); |
| 5740 | if (!(res->analysers & an_bit)) { |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5741 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 5742 | if (res->flags & CF_SHUTW) { |
| 5743 | /* response errors are most likely due to the |
| 5744 | * client aborting the transfer. */ |
| 5745 | goto aborted_xfer; |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 5746 | } |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5747 | if (msg->err_pos >= 0) |
Willy Tarreau | fd9419d | 2018-09-07 18:01:03 +0200 | [diff] [blame] | 5748 | http_capture_bad_message(s->be, s, msg, msg->err_state, strm_fe(s)); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5749 | goto return_bad_res; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5750 | } |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5751 | return 1; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5752 | } |
Willy Tarreau | f51d03c | 2016-05-02 15:25:15 +0200 | [diff] [blame] | 5753 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5754 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5755 | missing_data_or_waiting: |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 5756 | if (res->flags & CF_SHUTW) |
| 5757 | goto aborted_xfer; |
| 5758 | |
| 5759 | /* stop waiting for data if the input is closed before the end. If the |
| 5760 | * client side was already closed, it means that the client has aborted, |
| 5761 | * so we don't want to count this as a server abort. Otherwise it's a |
| 5762 | * server abort. |
| 5763 | */ |
Christopher Faulet | a33510b | 2017-03-31 15:37:29 +0200 | [diff] [blame] | 5764 | if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5765 | if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW)) |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 5766 | goto aborted_xfer; |
Christopher Faulet | a46bbd8 | 2015-06-19 09:00:58 +0200 | [diff] [blame] | 5767 | /* If we have some pending data, we continue the processing */ |
Willy Tarreau | 5ba6552 | 2018-06-15 15:14:53 +0200 | [diff] [blame] | 5768 | if (!ci_data(res)) { |
Christopher Faulet | a46bbd8 | 2015-06-19 09:00:58 +0200 | [diff] [blame] | 5769 | if (!(s->flags & SF_ERR_MASK)) |
| 5770 | s->flags |= SF_ERR_SRVCL; |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 5771 | HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1); |
Christopher Faulet | a46bbd8 | 2015-06-19 09:00:58 +0200 | [diff] [blame] | 5772 | if (objt_server(s->target)) |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 5773 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1); |
Christopher Faulet | a46bbd8 | 2015-06-19 09:00:58 +0200 | [diff] [blame] | 5774 | goto return_bad_res_stats_ok; |
| 5775 | } |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 5776 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5777 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 5778 | /* we need to obey the req analyser, so if it leaves, we must too */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5779 | if (!s->req.analysers) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5780 | goto return_bad_res; |
| 5781 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5782 | /* When TE: chunked is used, we need to get there again to parse |
| 5783 | * remaining chunks even if the server has closed, so we don't want to |
Christopher Faulet | 69744d9 | 2017-03-30 10:54:35 +0200 | [diff] [blame] | 5784 | * set CF_DONTCLOSE. Similarly, if keep-alive is set on the client side |
| 5785 | * or if there are filters registered on the stream, we don't want to |
| 5786 | * forward a close |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5787 | */ |
Christopher Faulet | 69744d9 | 2017-03-30 10:54:35 +0200 | [diff] [blame] | 5788 | if ((msg->flags & HTTP_MSGF_TE_CHNK) || |
Christopher Faulet | f1cc5d0 | 2017-02-08 09:45:13 +0100 | [diff] [blame] | 5789 | HAS_DATA_FILTERS(s, res) || |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5790 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 5791 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5792 | channel_dont_close(res); |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5793 | |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5794 | /* We know that more data are expected, but we couldn't send more that |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5795 | * what we did. So we always set the CF_EXPECT_MORE flag so that the |
Willy Tarreau | 0729303 | 2011-05-30 18:29:28 +0200 | [diff] [blame] | 5796 | * system knows it must not set a PUSH on this first part. Interactive |
Willy Tarreau | 869fc1e | 2012-03-05 08:29:20 +0100 | [diff] [blame] | 5797 | * modes are already handled by the stream sock layer. We must not do |
| 5798 | * this in content-length mode because it could present the MSG_MORE |
| 5799 | * flag with the last block of forwarded data, which would cause an |
| 5800 | * additional delay to be observed by the receiver. |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5801 | */ |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 5802 | if ((msg->flags & HTTP_MSGF_TE_CHNK) || (msg->flags & HTTP_MSGF_COMPRESSING)) |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5803 | res->flags |= CF_EXPECT_MORE; |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5804 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5805 | /* the stream handler will take care of timeouts and errors */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5806 | return 0; |
| 5807 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 5808 | return_bad_res: /* let's centralize all bad responses */ |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 5809 | HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5810 | if (objt_server(s->target)) |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 5811 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5812 | |
| 5813 | return_bad_res_stats_ok: |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 5814 | txn->rsp.err_state = txn->rsp.msg_state; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5815 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 5816 | /* don't send any error message as we're in the body */ |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 5817 | http_reply_and_close(s, txn->status, NULL); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 5818 | res->analysers &= AN_RES_FLT_END; |
| 5819 | s->req.analysers &= AN_REQ_FLT_END; /* we're in data phase, we want to abort both directions */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5820 | if (objt_server(s->target)) |
| 5821 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5822 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5823 | if (!(s->flags & SF_ERR_MASK)) |
| 5824 | s->flags |= SF_ERR_PRXCOND; |
| 5825 | if (!(s->flags & SF_FINST_MASK)) |
| 5826 | s->flags |= SF_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5827 | return 0; |
| 5828 | |
| 5829 | aborted_xfer: |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 5830 | txn->rsp.err_state = txn->rsp.msg_state; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5831 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
| 5832 | /* don't send any error message as we're in the body */ |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 5833 | http_reply_and_close(s, txn->status, NULL); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 5834 | res->analysers &= AN_RES_FLT_END; |
| 5835 | s->req.analysers &= AN_REQ_FLT_END; /* we're in data phase, we want to abort both directions */ |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5836 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 5837 | HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1); |
| 5838 | HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5839 | if (objt_server(s->target)) |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 5840 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5841 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5842 | if (!(s->flags & SF_ERR_MASK)) |
| 5843 | s->flags |= SF_ERR_CLICL; |
| 5844 | if (!(s->flags & SF_FINST_MASK)) |
| 5845 | s->flags |= SF_FINST_D; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5846 | return 0; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5847 | } |
| 5848 | |
| 5849 | |
| 5850 | static inline int |
| 5851 | http_msg_forward_body(struct stream *s, struct http_msg *msg) |
| 5852 | { |
| 5853 | struct channel *chn = msg->chn; |
| 5854 | int ret; |
| 5855 | |
| 5856 | /* Here we have the guarantee to be in HTTP_MSG_DATA or HTTP_MSG_ENDING state */ |
| 5857 | |
| 5858 | if (msg->msg_state == HTTP_MSG_ENDING) |
| 5859 | goto ending; |
| 5860 | |
| 5861 | /* Neither content-length, nor transfer-encoding was found, so we must |
| 5862 | * read the body until the server connection is closed. In that case, we |
| 5863 | * eat data as they come. Of course, this happens for response only. */ |
| 5864 | if (!(msg->flags & HTTP_MSGF_XFER_LEN)) { |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 5865 | unsigned long long len = ci_data(chn) - msg->next; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5866 | msg->chunk_len += len; |
| 5867 | msg->body_len += len; |
| 5868 | } |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 5869 | ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg), |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 5870 | /* default_ret */ MIN(msg->chunk_len, ci_data(chn) - msg->next), |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 5871 | /* on_error */ goto error); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5872 | msg->next += ret; |
| 5873 | msg->chunk_len -= ret; |
| 5874 | if (msg->chunk_len) { |
| 5875 | /* input empty or output full */ |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 5876 | if (ci_data(chn) > msg->next) |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5877 | chn->flags |= CF_WAKE_WRITE; |
| 5878 | goto missing_data_or_waiting; |
| 5879 | } |
| 5880 | |
Christopher Faulet | 1486b0a | 2017-07-18 11:42:08 +0200 | [diff] [blame] | 5881 | /* This check can only be true for a response. HTTP_MSGF_XFER_LEN is |
| 5882 | * always set for a request. */ |
| 5883 | if (!(msg->flags & HTTP_MSGF_XFER_LEN)) { |
| 5884 | /* The server still sending data that should be filtered */ |
| 5885 | if (!(chn->flags & CF_SHUTR) && HAS_DATA_FILTERS(s, chn)) |
| 5886 | goto missing_data_or_waiting; |
Christopher Faulet | fd04fcf | 2018-02-02 15:54:15 +0100 | [diff] [blame] | 5887 | msg->msg_state = HTTP_MSG_TUNNEL; |
| 5888 | goto ending; |
Christopher Faulet | 1486b0a | 2017-07-18 11:42:08 +0200 | [diff] [blame] | 5889 | } |
Christopher Faulet | f1cc5d0 | 2017-02-08 09:45:13 +0100 | [diff] [blame] | 5890 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5891 | msg->msg_state = HTTP_MSG_ENDING; |
| 5892 | |
| 5893 | ending: |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 5894 | /* we may have some pending data starting at res->buf.p such as a last |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5895 | * chunk of data or trailers. */ |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 5896 | ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next), |
| 5897 | /* default_ret */ msg->next, |
| 5898 | /* on_error */ goto error); |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 5899 | c_adv(chn, ret); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5900 | msg->next -= ret; |
Willy Tarreau | 9962f8f | 2016-06-28 11:52:08 +0200 | [diff] [blame] | 5901 | if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)) |
| 5902 | msg->sov -= ret; |
Christopher Faulet | a9300a3 | 2016-06-28 15:54:44 +0200 | [diff] [blame] | 5903 | if (msg->next) |
| 5904 | goto waiting; |
Willy Tarreau | 9962f8f | 2016-06-28 11:52:08 +0200 | [diff] [blame] | 5905 | |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 5906 | FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg), |
| 5907 | /* default_ret */ 1, |
| 5908 | /* on_error */ goto error, |
| 5909 | /* on_wait */ goto waiting); |
Christopher Faulet | fd04fcf | 2018-02-02 15:54:15 +0100 | [diff] [blame] | 5910 | if (msg->msg_state == HTTP_MSG_ENDING) |
| 5911 | msg->msg_state = HTTP_MSG_DONE; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5912 | return 1; |
| 5913 | |
| 5914 | missing_data_or_waiting: |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 5915 | /* we may have some pending data starting at chn->buf.p */ |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 5916 | ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next), |
| 5917 | /* default_ret */ msg->next, |
| 5918 | /* on_error */ goto error); |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 5919 | c_adv(chn, ret); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5920 | msg->next -= ret; |
| 5921 | if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0) |
| 5922 | msg->sov -= ret; |
Christopher Faulet | 75e2eb6 | 2015-12-15 10:41:47 +0100 | [diff] [blame] | 5923 | if (!HAS_DATA_FILTERS(s, chn)) |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5924 | msg->chunk_len -= channel_forward(chn, msg->chunk_len); |
Christopher Faulet | a9300a3 | 2016-06-28 15:54:44 +0200 | [diff] [blame] | 5925 | waiting: |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5926 | return 0; |
| 5927 | error: |
| 5928 | return -1; |
| 5929 | } |
| 5930 | |
| 5931 | static inline int |
| 5932 | http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg) |
| 5933 | { |
| 5934 | struct channel *chn = msg->chn; |
Willy Tarreau | e56cdd3 | 2017-09-21 08:36:33 +0200 | [diff] [blame] | 5935 | unsigned int chunk; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5936 | int ret; |
| 5937 | |
| 5938 | /* Here we have the guarantee to be in one of the following state: |
| 5939 | * HTTP_MSG_DATA, HTTP_MSG_CHUNK_SIZE, HTTP_MSG_CHUNK_CRLF, |
| 5940 | * HTTP_MSG_TRAILERS or HTTP_MSG_ENDING. */ |
| 5941 | |
| 5942 | switch_states: |
| 5943 | switch (msg->msg_state) { |
| 5944 | case HTTP_MSG_DATA: |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 5945 | ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg), |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 5946 | /* default_ret */ MIN(msg->chunk_len, ci_data(chn) - msg->next), |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 5947 | /* on_error */ goto error); |
| 5948 | msg->next += ret; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5949 | msg->chunk_len -= ret; |
| 5950 | if (msg->chunk_len) { |
| 5951 | /* input empty or output full */ |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 5952 | if (ci_data(chn) > msg->next) |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5953 | chn->flags |= CF_WAKE_WRITE; |
| 5954 | goto missing_data_or_waiting; |
| 5955 | } |
| 5956 | |
| 5957 | /* nothing left to forward for this chunk*/ |
| 5958 | msg->msg_state = HTTP_MSG_CHUNK_CRLF; |
| 5959 | /* fall through for HTTP_MSG_CHUNK_CRLF */ |
| 5960 | |
| 5961 | case HTTP_MSG_CHUNK_CRLF: |
| 5962 | /* we want the CRLF after the data */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 5963 | ret = h1_skip_chunk_crlf(&chn->buf, co_data(chn) + msg->next, c_data(chn)); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5964 | if (ret == 0) |
| 5965 | goto missing_data_or_waiting; |
Willy Tarreau | b289256 | 2017-09-21 11:33:54 +0200 | [diff] [blame] | 5966 | if (ret < 0) { |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 5967 | msg->err_pos = ci_data(chn) + ret; |
Willy Tarreau | b289256 | 2017-09-21 11:33:54 +0200 | [diff] [blame] | 5968 | if (msg->err_pos < 0) |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 5969 | msg->err_pos += chn->buf.size; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5970 | goto chunk_parsing_error; |
Willy Tarreau | b289256 | 2017-09-21 11:33:54 +0200 | [diff] [blame] | 5971 | } |
Christopher Faulet | 113f7de | 2015-12-14 14:52:13 +0100 | [diff] [blame] | 5972 | msg->next += ret; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5973 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 5974 | /* fall through for HTTP_MSG_CHUNK_SIZE */ |
| 5975 | |
| 5976 | case HTTP_MSG_CHUNK_SIZE: |
| 5977 | /* read the chunk size and assign it to ->chunk_len, |
| 5978 | * then set ->next to point to the body and switch to |
| 5979 | * DATA or TRAILERS state. |
| 5980 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 5981 | ret = h1_parse_chunk_size(&chn->buf, co_data(chn) + msg->next, c_data(chn), &chunk); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5982 | if (ret == 0) |
| 5983 | goto missing_data_or_waiting; |
Willy Tarreau | e56cdd3 | 2017-09-21 08:36:33 +0200 | [diff] [blame] | 5984 | if (ret < 0) { |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 5985 | msg->err_pos = ci_data(chn) + ret; |
Willy Tarreau | e56cdd3 | 2017-09-21 08:36:33 +0200 | [diff] [blame] | 5986 | if (msg->err_pos < 0) |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 5987 | msg->err_pos += chn->buf.size; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5988 | goto chunk_parsing_error; |
Willy Tarreau | e56cdd3 | 2017-09-21 08:36:33 +0200 | [diff] [blame] | 5989 | } |
| 5990 | |
| 5991 | msg->sol = ret; |
Christopher Faulet | 113f7de | 2015-12-14 14:52:13 +0100 | [diff] [blame] | 5992 | msg->next += ret; |
Willy Tarreau | e56cdd3 | 2017-09-21 08:36:33 +0200 | [diff] [blame] | 5993 | msg->chunk_len = chunk; |
| 5994 | msg->body_len += chunk; |
| 5995 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5996 | if (msg->chunk_len) { |
| 5997 | msg->msg_state = HTTP_MSG_DATA; |
| 5998 | goto switch_states; |
| 5999 | } |
| 6000 | msg->msg_state = HTTP_MSG_TRAILERS; |
| 6001 | /* fall through for HTTP_MSG_TRAILERS */ |
| 6002 | |
| 6003 | case HTTP_MSG_TRAILERS: |
| 6004 | ret = http_forward_trailers(msg); |
| 6005 | if (ret < 0) |
| 6006 | goto chunk_parsing_error; |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 6007 | FLT_STRM_DATA_CB(s, chn, flt_http_chunk_trailers(s, msg), |
| 6008 | /* default_ret */ 1, |
| 6009 | /* on_error */ goto error); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6010 | msg->next += msg->sol; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6011 | if (!ret) |
| 6012 | goto missing_data_or_waiting; |
| 6013 | break; |
| 6014 | |
| 6015 | case HTTP_MSG_ENDING: |
| 6016 | goto ending; |
| 6017 | |
| 6018 | default: |
| 6019 | /* This should no happen in this function */ |
| 6020 | goto error; |
| 6021 | } |
| 6022 | |
| 6023 | msg->msg_state = HTTP_MSG_ENDING; |
| 6024 | ending: |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 6025 | /* we may have some pending data starting at res->buf.p such as a last |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6026 | * chunk of data or trailers. */ |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 6027 | ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next), |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6028 | /* default_ret */ msg->next, |
| 6029 | /* on_error */ goto error); |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 6030 | c_adv(chn, ret); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6031 | msg->next -= ret; |
Willy Tarreau | 9962f8f | 2016-06-28 11:52:08 +0200 | [diff] [blame] | 6032 | if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)) |
| 6033 | msg->sov -= ret; |
Christopher Faulet | a9300a3 | 2016-06-28 15:54:44 +0200 | [diff] [blame] | 6034 | if (msg->next) |
| 6035 | goto waiting; |
Willy Tarreau | 9962f8f | 2016-06-28 11:52:08 +0200 | [diff] [blame] | 6036 | |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 6037 | FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg), |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6038 | /* default_ret */ 1, |
| 6039 | /* on_error */ goto error, |
| 6040 | /* on_wait */ goto waiting); |
| 6041 | msg->msg_state = HTTP_MSG_DONE; |
| 6042 | return 1; |
| 6043 | |
| 6044 | missing_data_or_waiting: |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 6045 | /* we may have some pending data starting at chn->buf.p */ |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 6046 | ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next), |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6047 | /* default_ret */ msg->next, |
| 6048 | /* on_error */ goto error); |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 6049 | c_adv(chn, ret); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6050 | msg->next -= ret; |
| 6051 | if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0) |
| 6052 | msg->sov -= ret; |
Christopher Faulet | 75e2eb6 | 2015-12-15 10:41:47 +0100 | [diff] [blame] | 6053 | if (!HAS_DATA_FILTERS(s, chn)) |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6054 | msg->chunk_len -= channel_forward(chn, msg->chunk_len); |
Christopher Faulet | a9300a3 | 2016-06-28 15:54:44 +0200 | [diff] [blame] | 6055 | waiting: |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6056 | return 0; |
| 6057 | |
| 6058 | chunk_parsing_error: |
| 6059 | if (msg->err_pos >= 0) { |
| 6060 | if (chn->flags & CF_ISRESP) |
Willy Tarreau | fd9419d | 2018-09-07 18:01:03 +0200 | [diff] [blame] | 6061 | http_capture_bad_message(s->be, s, msg, |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6062 | msg->msg_state, strm_fe(s)); |
| 6063 | else |
Willy Tarreau | fd9419d | 2018-09-07 18:01:03 +0200 | [diff] [blame] | 6064 | http_capture_bad_message(strm_fe(s), s, |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6065 | msg, msg->msg_state, s->be); |
| 6066 | } |
| 6067 | error: |
| 6068 | return -1; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6069 | } |
| 6070 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6071 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6072 | /* Iterate the same filter through all request headers. |
| 6073 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6074 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 6075 | * DENY stats. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6076 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6077 | int apply_filter_to_req_headers(struct stream *s, struct channel *req, struct hdr_exp *exp) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6078 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6079 | char *cur_ptr, *cur_end, *cur_next; |
| 6080 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 6081 | struct http_txn *txn = s->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6082 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 6e27be1 | 2018-08-22 04:46:47 +0200 | [diff] [blame] | 6083 | int delta, len; |
Willy Tarreau | 0f7562b | 2007-01-07 15:46:13 +0100 | [diff] [blame] | 6084 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6085 | last_hdr = 0; |
| 6086 | |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 6087 | cur_next = ci_head(req) + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6088 | old_idx = 0; |
| 6089 | |
| 6090 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6091 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6092 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6093 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6094 | (exp->action == ACT_ALLOW || |
| 6095 | exp->action == ACT_DENY || |
| 6096 | exp->action == ACT_TARPIT)) |
| 6097 | return 0; |
| 6098 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6099 | cur_idx = txn->hdr_idx.v[old_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6100 | if (!cur_idx) |
| 6101 | break; |
| 6102 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6103 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6104 | cur_ptr = cur_next; |
| 6105 | cur_end = cur_ptr + cur_hdr->len; |
| 6106 | cur_next = cur_end + cur_hdr->cr + 1; |
| 6107 | |
| 6108 | /* Now we have one header between cur_ptr and cur_end, |
| 6109 | * and the next header starts at cur_next. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6110 | */ |
| 6111 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 6112 | if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6113 | switch (exp->action) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6114 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6115 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6116 | last_hdr = 1; |
| 6117 | break; |
| 6118 | |
| 6119 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6120 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6121 | last_hdr = 1; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6122 | break; |
| 6123 | |
| 6124 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6125 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6126 | last_hdr = 1; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6127 | break; |
| 6128 | |
| 6129 | case ACT_REPLACE: |
Willy Tarreau | 6e27be1 | 2018-08-22 04:46:47 +0200 | [diff] [blame] | 6130 | len = exp_replace(trash.area, |
| 6131 | trash.size, cur_ptr, |
| 6132 | exp->replace, pmatch); |
| 6133 | if (len < 0) |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 6134 | return -1; |
| 6135 | |
Willy Tarreau | 6e27be1 | 2018-08-22 04:46:47 +0200 | [diff] [blame] | 6136 | delta = b_rep_blk(&req->buf, cur_ptr, cur_end, trash.area, len); |
| 6137 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6138 | /* FIXME: if the user adds a newline in the replacement, the |
| 6139 | * index will not be recalculated for now, and the new line |
| 6140 | * will not be counted as a new header. |
| 6141 | */ |
| 6142 | |
| 6143 | cur_end += delta; |
| 6144 | cur_next += delta; |
| 6145 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6146 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6147 | break; |
| 6148 | |
| 6149 | case ACT_REMOVE: |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 6150 | delta = b_rep_blk(&req->buf, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6151 | cur_next += delta; |
| 6152 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6153 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6154 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 6155 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6156 | cur_hdr->len = 0; |
| 6157 | cur_end = NULL; /* null-term has been rewritten */ |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 6158 | cur_idx = old_idx; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6159 | break; |
| 6160 | |
| 6161 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6162 | } |
| 6163 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6164 | /* keep the link from this header to next one in case of later |
| 6165 | * removal of next header. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6166 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6167 | old_idx = cur_idx; |
| 6168 | } |
| 6169 | return 0; |
| 6170 | } |
| 6171 | |
| 6172 | |
| 6173 | /* Apply the filter to the request line. |
| 6174 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 6175 | * or -1 if a replacement resulted in an invalid request line. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6176 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 6177 | * DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6178 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6179 | int apply_filter_to_req_line(struct stream *s, struct channel *req, struct hdr_exp *exp) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6180 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6181 | char *cur_ptr, *cur_end; |
| 6182 | int done; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 6183 | struct http_txn *txn = s->txn; |
Willy Tarreau | 6e27be1 | 2018-08-22 04:46:47 +0200 | [diff] [blame] | 6184 | int delta, len; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6185 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6186 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6187 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6188 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6189 | (exp->action == ACT_ALLOW || |
| 6190 | exp->action == ACT_DENY || |
| 6191 | exp->action == ACT_TARPIT)) |
| 6192 | return 0; |
| 6193 | else if (exp->action == ACT_REMOVE) |
| 6194 | return 0; |
| 6195 | |
| 6196 | done = 0; |
| 6197 | |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 6198 | cur_ptr = ci_head(req); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6199 | cur_end = cur_ptr + txn->req.sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6200 | |
| 6201 | /* Now we have the request line between cur_ptr and cur_end */ |
| 6202 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 6203 | if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6204 | switch (exp->action) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6205 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6206 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6207 | done = 1; |
| 6208 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 6209 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6210 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6211 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6212 | done = 1; |
| 6213 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 6214 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6215 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6216 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6217 | done = 1; |
| 6218 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 6219 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6220 | case ACT_REPLACE: |
Willy Tarreau | 6e27be1 | 2018-08-22 04:46:47 +0200 | [diff] [blame] | 6221 | len = exp_replace(trash.area, trash.size, |
| 6222 | cur_ptr, exp->replace, pmatch); |
| 6223 | if (len < 0) |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 6224 | return -1; |
| 6225 | |
Willy Tarreau | 6e27be1 | 2018-08-22 04:46:47 +0200 | [diff] [blame] | 6226 | delta = b_rep_blk(&req->buf, cur_ptr, cur_end, trash.area, len); |
| 6227 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6228 | /* FIXME: if the user adds a newline in the replacement, the |
| 6229 | * index will not be recalculated for now, and the new line |
| 6230 | * will not be counted as a new header. |
| 6231 | */ |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 6232 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6233 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6234 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 6235 | cur_end = (char *)http_parse_reqline(&txn->req, |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6236 | HTTP_MSG_RQMETH, |
| 6237 | cur_ptr, cur_end + 1, |
| 6238 | NULL, NULL); |
| 6239 | if (unlikely(!cur_end)) |
| 6240 | return -1; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 6241 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6242 | /* we have a full request and we know that we have either a CR |
| 6243 | * or an LF at <ptr>. |
| 6244 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6245 | txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l); |
| 6246 | hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r'); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6247 | /* there is no point trying this regex on headers */ |
| 6248 | return 1; |
| 6249 | } |
| 6250 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6251 | return done; |
| 6252 | } |
Willy Tarreau | 97de624 | 2006-12-27 17:18:38 +0100 | [diff] [blame] | 6253 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6254 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6255 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6256 | /* |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6257 | * Apply all the req filters of proxy <px> to all headers in buffer <req> of stream <s>. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6258 | * Returns 0 if everything is alright, or -1 in case a replacement lead to an |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6259 | * unparsable request. Since it can manage the switch to another backend, it |
| 6260 | * updates the per-proxy DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6261 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6262 | int apply_filters_to_request(struct stream *s, struct channel *req, struct proxy *px) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6263 | { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 6264 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 6265 | struct http_txn *txn = s->txn; |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6266 | struct hdr_exp *exp; |
| 6267 | |
| 6268 | for (exp = px->req_exp; exp; exp = exp->next) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6269 | int ret; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6270 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6271 | /* |
| 6272 | * The interleaving of transformations and verdicts |
| 6273 | * makes it difficult to decide to continue or stop |
| 6274 | * the evaluation. |
| 6275 | */ |
| 6276 | |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6277 | if (txn->flags & (TX_CLDENY|TX_CLTARPIT)) |
| 6278 | break; |
| 6279 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6280 | if ((txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6281 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6282 | exp->action == ACT_TARPIT || exp->action == ACT_PASS)) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6283 | continue; |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6284 | |
| 6285 | /* if this filter had a condition, evaluate it now and skip to |
| 6286 | * next filter if the condition does not match. |
| 6287 | */ |
| 6288 | if (exp->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 6289 | ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6290 | ret = acl_pass(ret); |
| 6291 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 6292 | ret = !ret; |
| 6293 | |
| 6294 | if (!ret) |
| 6295 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6296 | } |
| 6297 | |
| 6298 | /* Apply the filter to the request line. */ |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6299 | ret = apply_filter_to_req_line(s, req, exp); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6300 | if (unlikely(ret < 0)) |
| 6301 | return -1; |
| 6302 | |
| 6303 | if (likely(ret == 0)) { |
| 6304 | /* The filter did not match the request, it can be |
| 6305 | * iterated through all headers. |
| 6306 | */ |
Willy Tarreau | 34d4c3c | 2015-01-30 20:58:58 +0100 | [diff] [blame] | 6307 | if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0)) |
| 6308 | return -1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6309 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6310 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6311 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6312 | } |
| 6313 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6314 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6315 | /* Find the end of a cookie value contained between <s> and <e>. It works the |
| 6316 | * same way as with headers above except that the semi-colon also ends a token. |
| 6317 | * See RFC2965 for more information. Note that it requires a valid header to |
| 6318 | * return a valid result. |
| 6319 | */ |
| 6320 | char *find_cookie_value_end(char *s, const char *e) |
| 6321 | { |
| 6322 | int quoted, qdpair; |
| 6323 | |
| 6324 | quoted = qdpair = 0; |
| 6325 | for (; s < e; s++) { |
| 6326 | if (qdpair) qdpair = 0; |
| 6327 | else if (quoted) { |
| 6328 | if (*s == '\\') qdpair = 1; |
| 6329 | else if (*s == '"') quoted = 0; |
| 6330 | } |
| 6331 | else if (*s == '"') quoted = 1; |
| 6332 | else if (*s == ',' || *s == ';') return s; |
| 6333 | } |
| 6334 | return s; |
| 6335 | } |
| 6336 | |
| 6337 | /* Delete a value in a header between delimiters <from> and <next> in buffer |
| 6338 | * <buf>. The number of characters displaced is returned, and the pointer to |
| 6339 | * the first delimiter is updated if required. The function tries as much as |
| 6340 | * possible to respect the following principles : |
| 6341 | * - replace <from> delimiter by the <next> one unless <from> points to a |
| 6342 | * colon, in which case <next> is simply removed |
| 6343 | * - set exactly one space character after the new first delimiter, unless |
| 6344 | * there are not enough characters in the block being moved to do so. |
| 6345 | * - remove unneeded spaces before the previous delimiter and after the new |
| 6346 | * one. |
| 6347 | * |
| 6348 | * It is the caller's responsibility to ensure that : |
| 6349 | * - <from> points to a valid delimiter or the colon ; |
| 6350 | * - <next> points to a valid delimiter or the final CR/LF ; |
| 6351 | * - there are non-space chars before <from> ; |
| 6352 | * - there is a CR/LF at or after <next>. |
| 6353 | */ |
Willy Tarreau | af81935 | 2012-08-27 22:08:00 +0200 | [diff] [blame] | 6354 | int del_hdr_value(struct buffer *buf, char **from, char *next) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6355 | { |
| 6356 | char *prev = *from; |
| 6357 | |
| 6358 | if (*prev == ':') { |
| 6359 | /* We're removing the first value, preserve the colon and add a |
| 6360 | * space if possible. |
| 6361 | */ |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 6362 | if (!HTTP_IS_CRLF(*next)) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6363 | next++; |
| 6364 | prev++; |
| 6365 | if (prev < next) |
| 6366 | *prev++ = ' '; |
| 6367 | |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 6368 | while (HTTP_IS_SPHT(*next)) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6369 | next++; |
| 6370 | } else { |
| 6371 | /* Remove useless spaces before the old delimiter. */ |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 6372 | while (HTTP_IS_SPHT(*(prev-1))) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6373 | prev--; |
| 6374 | *from = prev; |
| 6375 | |
| 6376 | /* copy the delimiter and if possible a space if we're |
| 6377 | * not at the end of the line. |
| 6378 | */ |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 6379 | if (!HTTP_IS_CRLF(*next)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6380 | *prev++ = *next++; |
| 6381 | if (prev + 1 < next) |
| 6382 | *prev++ = ' '; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 6383 | while (HTTP_IS_SPHT(*next)) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6384 | next++; |
| 6385 | } |
| 6386 | } |
Willy Tarreau | e312802 | 2018-07-12 15:55:34 +0200 | [diff] [blame] | 6387 | return b_rep_blk(buf, prev, next, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6388 | } |
| 6389 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6390 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 6391 | * Manage client-side cookie. It can impact performance by about 2% so it is |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6392 | * desirable to call it only when needed. This code is quite complex because |
| 6393 | * of the multiple very crappy and ambiguous syntaxes we have to support. it |
| 6394 | * highly recommended not to touch this part without a good reason ! |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6395 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6396 | void manage_client_side_cookies(struct stream *s, struct channel *req) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6397 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 6398 | struct http_txn *txn = s->txn; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6399 | struct session *sess = s->sess; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6400 | int preserve_hdr; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6401 | int cur_idx, old_idx; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6402 | char *hdr_beg, *hdr_end, *hdr_next, *del_from; |
| 6403 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6404 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6405 | /* Iterate through the headers, we start with the start line. */ |
Willy Tarreau | 83969f4 | 2007-01-22 08:55:47 +0100 | [diff] [blame] | 6406 | old_idx = 0; |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 6407 | hdr_next = ci_head(req) + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6408 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6409 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6410 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6411 | int val; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6412 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6413 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6414 | hdr_beg = hdr_next; |
| 6415 | hdr_end = hdr_beg + cur_hdr->len; |
| 6416 | hdr_next = hdr_end + cur_hdr->cr + 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6417 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6418 | /* We have one full header between hdr_beg and hdr_end, and the |
| 6419 | * next header starts at hdr_next. We're only interested in |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6420 | * "Cookie:" headers. |
| 6421 | */ |
| 6422 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6423 | val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6); |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6424 | if (!val) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6425 | old_idx = cur_idx; |
| 6426 | continue; |
| 6427 | } |
| 6428 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6429 | del_from = NULL; /* nothing to be deleted */ |
| 6430 | preserve_hdr = 0; /* assume we may kill the whole header */ |
| 6431 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6432 | /* Now look for cookies. Conforming to RFC2109, we have to support |
| 6433 | * attributes whose name begin with a '$', and associate them with |
| 6434 | * the right cookie, if we want to delete this cookie. |
| 6435 | * So there are 3 cases for each cookie read : |
| 6436 | * 1) it's a special attribute, beginning with a '$' : ignore it. |
| 6437 | * 2) it's a server id cookie that we *MAY* want to delete : save |
| 6438 | * some pointers on it (last semi-colon, beginning of cookie...) |
| 6439 | * 3) it's an application cookie : we *MAY* have to delete a previous |
| 6440 | * "special" cookie. |
| 6441 | * At the end of loop, if a "special" cookie remains, we may have to |
| 6442 | * remove it. If no application cookie persists in the header, we |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6443 | * *MUST* delete it. |
| 6444 | * |
| 6445 | * Note: RFC2965 is unclear about the processing of spaces around |
| 6446 | * the equal sign in the ATTR=VALUE form. A careful inspection of |
| 6447 | * the RFC explicitly allows spaces before it, and not within the |
| 6448 | * tokens (attrs or values). An inspection of RFC2109 allows that |
| 6449 | * too but section 10.1.3 lets one think that spaces may be allowed |
| 6450 | * after the equal sign too, resulting in some (rare) buggy |
| 6451 | * implementations trying to do that. So let's do what servers do. |
| 6452 | * Latest ietf draft forbids spaces all around. Also, earlier RFCs |
| 6453 | * allowed quoted strings in values, with any possible character |
| 6454 | * after a backslash, including control chars and delimitors, which |
| 6455 | * causes parsing to become ambiguous. Browsers also allow spaces |
| 6456 | * within values even without quotes. |
| 6457 | * |
| 6458 | * We have to keep multiple pointers in order to support cookie |
| 6459 | * removal at the beginning, middle or end of header without |
| 6460 | * corrupting the header. All of these headers are valid : |
| 6461 | * |
| 6462 | * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n |
| 6463 | * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n |
| 6464 | * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n |
| 6465 | * | | | | | | | | | |
| 6466 | * | | | | | | | | hdr_end <--+ |
| 6467 | * | | | | | | | +--> next |
| 6468 | * | | | | | | +----> val_end |
| 6469 | * | | | | | +-----------> val_beg |
| 6470 | * | | | | +--------------> equal |
| 6471 | * | | | +----------------> att_end |
| 6472 | * | | +---------------------> att_beg |
| 6473 | * | +--------------------------> prev |
| 6474 | * +--------------------------------> hdr_beg |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6475 | */ |
| 6476 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6477 | for (prev = hdr_beg + 6; prev < hdr_end; prev = next) { |
| 6478 | /* Iterate through all cookies on this line */ |
| 6479 | |
| 6480 | /* find att_beg */ |
| 6481 | att_beg = prev + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 6482 | while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg)) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6483 | att_beg++; |
| 6484 | |
| 6485 | /* find att_end : this is the first character after the last non |
| 6486 | * space before the equal. It may be equal to hdr_end. |
| 6487 | */ |
| 6488 | equal = att_end = att_beg; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6489 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6490 | while (equal < hdr_end) { |
| 6491 | if (*equal == '=' || *equal == ',' || *equal == ';') |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6492 | break; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 6493 | if (HTTP_IS_SPHT(*equal++)) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6494 | continue; |
| 6495 | att_end = equal; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6496 | } |
| 6497 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6498 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 6499 | * is between <att_beg> and <equal>, both may be identical. |
| 6500 | */ |
| 6501 | |
| 6502 | /* look for end of cookie if there is an equal sign */ |
| 6503 | if (equal < hdr_end && *equal == '=') { |
| 6504 | /* look for the beginning of the value */ |
| 6505 | val_beg = equal + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 6506 | while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg)) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6507 | val_beg++; |
| 6508 | |
| 6509 | /* find the end of the value, respecting quotes */ |
| 6510 | next = find_cookie_value_end(val_beg, hdr_end); |
| 6511 | |
| 6512 | /* make val_end point to the first white space or delimitor after the value */ |
| 6513 | val_end = next; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 6514 | while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1))) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6515 | val_end--; |
| 6516 | } else { |
| 6517 | val_beg = val_end = next = equal; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 6518 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6519 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6520 | /* We have nothing to do with attributes beginning with '$'. However, |
| 6521 | * they will automatically be removed if a header before them is removed, |
| 6522 | * since they're supposed to be linked together. |
| 6523 | */ |
| 6524 | if (*att_beg == '$') |
| 6525 | continue; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6526 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6527 | /* Ignore cookies with no equal sign */ |
| 6528 | if (equal == next) { |
| 6529 | /* This is not our cookie, so we must preserve it. But if we already |
| 6530 | * scheduled another cookie for removal, we cannot remove the |
| 6531 | * complete header, but we can remove the previous block itself. |
| 6532 | */ |
| 6533 | preserve_hdr = 1; |
| 6534 | if (del_from != NULL) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 6535 | int delta = del_hdr_value(&req->buf, &del_from, prev); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6536 | val_end += delta; |
| 6537 | next += delta; |
| 6538 | hdr_end += delta; |
| 6539 | hdr_next += delta; |
| 6540 | cur_hdr->len += delta; |
| 6541 | http_msg_move_end(&txn->req, delta); |
| 6542 | prev = del_from; |
| 6543 | del_from = NULL; |
| 6544 | } |
| 6545 | continue; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 6546 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6547 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6548 | /* if there are spaces around the equal sign, we need to |
| 6549 | * strip them otherwise we'll get trouble for cookie captures, |
| 6550 | * or even for rewrites. Since this happens extremely rarely, |
| 6551 | * it does not hurt performance. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6552 | */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6553 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 6554 | int stripped_before = 0; |
| 6555 | int stripped_after = 0; |
| 6556 | |
| 6557 | if (att_end != equal) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 6558 | stripped_before = b_rep_blk(&req->buf, att_end, equal, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6559 | equal += stripped_before; |
| 6560 | val_beg += stripped_before; |
| 6561 | } |
| 6562 | |
| 6563 | if (val_beg > equal + 1) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 6564 | stripped_after = b_rep_blk(&req->buf, equal + 1, val_beg, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6565 | val_beg += stripped_after; |
| 6566 | stripped_before += stripped_after; |
| 6567 | } |
| 6568 | |
| 6569 | val_end += stripped_before; |
| 6570 | next += stripped_before; |
| 6571 | hdr_end += stripped_before; |
| 6572 | hdr_next += stripped_before; |
| 6573 | cur_hdr->len += stripped_before; |
| 6574 | http_msg_move_end(&txn->req, stripped_before); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6575 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6576 | /* now everything is as on the diagram above */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6577 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6578 | /* First, let's see if we want to capture this cookie. We check |
| 6579 | * that we don't already have a client side cookie, because we |
| 6580 | * can only capture one. Also as an optimisation, we ignore |
| 6581 | * cookies shorter than the declared name. |
| 6582 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6583 | if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL && |
| 6584 | (val_end - att_beg >= sess->fe->capture_namelen) && |
| 6585 | memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6586 | int log_len = val_end - att_beg; |
| 6587 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6588 | if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 6589 | ha_alert("HTTP logging : out of memory.\n"); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6590 | } else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6591 | if (log_len > sess->fe->capture_len) |
| 6592 | log_len = sess->fe->capture_len; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6593 | memcpy(txn->cli_cookie, att_beg, log_len); |
| 6594 | txn->cli_cookie[log_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6595 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6596 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6597 | |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6598 | /* Persistence cookies in passive, rewrite or insert mode have the |
| 6599 | * following form : |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6600 | * |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6601 | * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]] |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6602 | * |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6603 | * For cookies in prefix mode, the form is : |
| 6604 | * |
| 6605 | * Cookie: NAME=SRV~VALUE |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6606 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6607 | if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) && |
| 6608 | (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) { |
| 6609 | struct server *srv = s->be->srv; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6610 | char *delim; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6611 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6612 | /* if we're in cookie prefix mode, we'll search the delimitor so that we |
| 6613 | * have the server ID between val_beg and delim, and the original cookie between |
| 6614 | * delim+1 and val_end. Otherwise, delim==val_end : |
| 6615 | * |
| 6616 | * Cookie: NAME=SRV; # in all but prefix modes |
| 6617 | * Cookie: NAME=SRV~OPAQUE ; # in prefix mode |
| 6618 | * | || || | |+-> next |
| 6619 | * | || || | +--> val_end |
| 6620 | * | || || +---------> delim |
| 6621 | * | || |+------------> val_beg |
| 6622 | * | || +-------------> att_end = equal |
| 6623 | * | |+-----------------> att_beg |
| 6624 | * | +------------------> prev |
| 6625 | * +-------------------------> hdr_beg |
| 6626 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6627 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6628 | if (s->be->ck_opts & PR_CK_PFX) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6629 | for (delim = val_beg; delim < val_end; delim++) |
| 6630 | if (*delim == COOKIE_DELIM) |
| 6631 | break; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6632 | } else { |
| 6633 | char *vbar1; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6634 | delim = val_end; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6635 | /* Now check if the cookie contains a date field, which would |
| 6636 | * appear after a vertical bar ('|') just after the server name |
| 6637 | * and before the delimiter. |
| 6638 | */ |
| 6639 | vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg); |
| 6640 | if (vbar1) { |
| 6641 | /* OK, so left of the bar is the server's cookie and |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6642 | * right is the last seen date. It is a base64 encoded |
| 6643 | * 30-bit value representing the UNIX date since the |
| 6644 | * epoch in 4-second quantities. |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6645 | */ |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6646 | int val; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6647 | delim = vbar1++; |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6648 | if (val_end - vbar1 >= 5) { |
| 6649 | val = b64tos30(vbar1); |
| 6650 | if (val > 0) |
| 6651 | txn->cookie_last_date = val << 2; |
| 6652 | } |
| 6653 | /* look for a second vertical bar */ |
| 6654 | vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1); |
| 6655 | if (vbar1 && (val_end - vbar1 > 5)) { |
| 6656 | val = b64tos30(vbar1 + 1); |
| 6657 | if (val > 0) |
| 6658 | txn->cookie_first_date = val << 2; |
| 6659 | } |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6660 | } |
| 6661 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6662 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6663 | /* if the cookie has an expiration date and the proxy wants to check |
| 6664 | * it, then we do that now. We first check if the cookie is too old, |
| 6665 | * then only if it has expired. We detect strict overflow because the |
| 6666 | * time resolution here is not great (4 seconds). Cookies with dates |
| 6667 | * in the future are ignored if their offset is beyond one day. This |
| 6668 | * allows an admin to fix timezone issues without expiring everyone |
| 6669 | * and at the same time avoids keeping unwanted side effects for too |
| 6670 | * long. |
| 6671 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6672 | if (txn->cookie_first_date && s->be->cookie_maxlife && |
| 6673 | (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) || |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6674 | ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) { |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6675 | txn->flags &= ~TX_CK_MASK; |
| 6676 | txn->flags |= TX_CK_OLD; |
| 6677 | delim = val_beg; // let's pretend we have not found the cookie |
| 6678 | txn->cookie_first_date = 0; |
| 6679 | txn->cookie_last_date = 0; |
| 6680 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6681 | else if (txn->cookie_last_date && s->be->cookie_maxidle && |
| 6682 | (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) || |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6683 | ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) { |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6684 | txn->flags &= ~TX_CK_MASK; |
| 6685 | txn->flags |= TX_CK_EXPIRED; |
| 6686 | delim = val_beg; // let's pretend we have not found the cookie |
| 6687 | txn->cookie_first_date = 0; |
| 6688 | txn->cookie_last_date = 0; |
| 6689 | } |
| 6690 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6691 | /* Here, we'll look for the first running server which supports the cookie. |
| 6692 | * This allows to share a same cookie between several servers, for example |
| 6693 | * to dedicate backup servers to specific servers only. |
| 6694 | * However, to prevent clients from sticking to cookie-less backup server |
| 6695 | * when they have incidentely learned an empty cookie, we simply ignore |
| 6696 | * empty cookies and mark them as invalid. |
| 6697 | * The same behaviour is applied when persistence must be ignored. |
| 6698 | */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6699 | if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED))) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6700 | srv = NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6701 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6702 | while (srv) { |
| 6703 | if (srv->cookie && (srv->cklen == delim - val_beg) && |
| 6704 | !memcmp(val_beg, srv->cookie, delim - val_beg)) { |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 6705 | if ((srv->cur_state != SRV_ST_STOPPED) || |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6706 | (s->be->options & PR_O_PERSIST) || |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6707 | (s->flags & SF_FORCE_PRST)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6708 | /* we found the server and we can use it */ |
| 6709 | txn->flags &= ~TX_CK_MASK; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 6710 | txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6711 | s->flags |= SF_DIRECT | SF_ASSIGNED; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6712 | s->target = &srv->obj_type; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6713 | break; |
| 6714 | } else { |
| 6715 | /* we found a server, but it's down, |
| 6716 | * mark it as such and go on in case |
| 6717 | * another one is available. |
| 6718 | */ |
| 6719 | txn->flags &= ~TX_CK_MASK; |
| 6720 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6721 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6722 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6723 | srv = srv->next; |
| 6724 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6725 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6726 | if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) { |
Willy Tarreau | c89ccb6 | 2012-04-05 21:18:22 +0200 | [diff] [blame] | 6727 | /* no server matched this cookie or we deliberately skipped it */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6728 | txn->flags &= ~TX_CK_MASK; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6729 | if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED))) |
Willy Tarreau | c89ccb6 | 2012-04-05 21:18:22 +0200 | [diff] [blame] | 6730 | txn->flags |= TX_CK_UNUSED; |
| 6731 | else |
| 6732 | txn->flags |= TX_CK_INVALID; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6733 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6734 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6735 | /* depending on the cookie mode, we may have to either : |
| 6736 | * - delete the complete cookie if we're in insert+indirect mode, so that |
| 6737 | * the server never sees it ; |
| 6738 | * - remove the server id from the cookie value, and tag the cookie as an |
| 6739 | * application cookie so that it does not get accidentely removed later, |
| 6740 | * if we're in cookie prefix mode |
| 6741 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6742 | if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6743 | int delta; /* negative */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6744 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 6745 | delta = b_rep_blk(&req->buf, val_beg, delim + 1, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6746 | val_end += delta; |
| 6747 | next += delta; |
| 6748 | hdr_end += delta; |
| 6749 | hdr_next += delta; |
| 6750 | cur_hdr->len += delta; |
| 6751 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6752 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6753 | del_from = NULL; |
| 6754 | preserve_hdr = 1; /* we want to keep this cookie */ |
| 6755 | } |
| 6756 | else if (del_from == NULL && |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6757 | (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6758 | del_from = prev; |
| 6759 | } |
| 6760 | } else { |
| 6761 | /* This is not our cookie, so we must preserve it. But if we already |
| 6762 | * scheduled another cookie for removal, we cannot remove the |
| 6763 | * complete header, but we can remove the previous block itself. |
| 6764 | */ |
| 6765 | preserve_hdr = 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6766 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6767 | if (del_from != NULL) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 6768 | int delta = del_hdr_value(&req->buf, &del_from, prev); |
Willy Tarreau | b810554 | 2010-11-24 18:31:28 +0100 | [diff] [blame] | 6769 | if (att_beg >= del_from) |
| 6770 | att_beg += delta; |
| 6771 | if (att_end >= del_from) |
| 6772 | att_end += delta; |
| 6773 | val_beg += delta; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6774 | val_end += delta; |
| 6775 | next += delta; |
| 6776 | hdr_end += delta; |
| 6777 | hdr_next += delta; |
| 6778 | cur_hdr->len += delta; |
| 6779 | http_msg_move_end(&txn->req, delta); |
| 6780 | prev = del_from; |
| 6781 | del_from = NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6782 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6783 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6784 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6785 | /* continue with next cookie on this header line */ |
| 6786 | att_beg = next; |
| 6787 | } /* for each cookie */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6788 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6789 | /* There are no more cookies on this line. |
| 6790 | * We may still have one (or several) marked for deletion at the |
| 6791 | * end of the line. We must do this now in two ways : |
| 6792 | * - if some cookies must be preserved, we only delete from the |
| 6793 | * mark to the end of line ; |
| 6794 | * - if nothing needs to be preserved, simply delete the whole header |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6795 | */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6796 | if (del_from) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6797 | int delta; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6798 | if (preserve_hdr) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 6799 | delta = del_hdr_value(&req->buf, &del_from, hdr_end); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6800 | hdr_end = del_from; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6801 | cur_hdr->len += delta; |
| 6802 | } else { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 6803 | delta = b_rep_blk(&req->buf, hdr_beg, hdr_next, NULL, 0); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6804 | |
| 6805 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6806 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 6807 | txn->hdr_idx.used--; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6808 | cur_hdr->len = 0; |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 6809 | cur_idx = old_idx; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6810 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6811 | hdr_next += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6812 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6813 | } |
| 6814 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6815 | /* check next header */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6816 | old_idx = cur_idx; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6817 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6818 | } |
| 6819 | |
| 6820 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6821 | /* Iterate the same filter through all response headers contained in <rtr>. |
| 6822 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
| 6823 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6824 | int apply_filter_to_resp_headers(struct stream *s, struct channel *rtr, struct hdr_exp *exp) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6825 | { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6826 | char *cur_ptr, *cur_end, *cur_next; |
| 6827 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 6828 | struct http_txn *txn = s->txn; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6829 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 6e27be1 | 2018-08-22 04:46:47 +0200 | [diff] [blame] | 6830 | int delta, len; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6831 | |
| 6832 | last_hdr = 0; |
| 6833 | |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 6834 | cur_next = ci_head(rtr) + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6835 | old_idx = 0; |
| 6836 | |
| 6837 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6838 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6839 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6840 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6841 | (exp->action == ACT_ALLOW || |
| 6842 | exp->action == ACT_DENY)) |
| 6843 | return 0; |
| 6844 | |
| 6845 | cur_idx = txn->hdr_idx.v[old_idx].next; |
| 6846 | if (!cur_idx) |
| 6847 | break; |
| 6848 | |
| 6849 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 6850 | cur_ptr = cur_next; |
| 6851 | cur_end = cur_ptr + cur_hdr->len; |
| 6852 | cur_next = cur_end + cur_hdr->cr + 1; |
| 6853 | |
| 6854 | /* Now we have one header between cur_ptr and cur_end, |
| 6855 | * and the next header starts at cur_next. |
| 6856 | */ |
| 6857 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 6858 | if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6859 | switch (exp->action) { |
| 6860 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6861 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6862 | last_hdr = 1; |
| 6863 | break; |
| 6864 | |
| 6865 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6866 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6867 | last_hdr = 1; |
| 6868 | break; |
| 6869 | |
| 6870 | case ACT_REPLACE: |
Willy Tarreau | 6e27be1 | 2018-08-22 04:46:47 +0200 | [diff] [blame] | 6871 | len = exp_replace(trash.area, |
| 6872 | trash.size, cur_ptr, |
| 6873 | exp->replace, pmatch); |
| 6874 | if (len < 0) |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 6875 | return -1; |
| 6876 | |
Willy Tarreau | 6e27be1 | 2018-08-22 04:46:47 +0200 | [diff] [blame] | 6877 | delta = b_rep_blk(&rtr->buf, cur_ptr, cur_end, trash.area, len); |
| 6878 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6879 | /* FIXME: if the user adds a newline in the replacement, the |
| 6880 | * index will not be recalculated for now, and the new line |
| 6881 | * will not be counted as a new header. |
| 6882 | */ |
| 6883 | |
| 6884 | cur_end += delta; |
| 6885 | cur_next += delta; |
| 6886 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6887 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6888 | break; |
| 6889 | |
| 6890 | case ACT_REMOVE: |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 6891 | delta = b_rep_blk(&rtr->buf, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6892 | cur_next += delta; |
| 6893 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6894 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6895 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 6896 | txn->hdr_idx.used--; |
| 6897 | cur_hdr->len = 0; |
| 6898 | cur_end = NULL; /* null-term has been rewritten */ |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 6899 | cur_idx = old_idx; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6900 | break; |
| 6901 | |
| 6902 | } |
| 6903 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6904 | |
| 6905 | /* keep the link from this header to next one in case of later |
| 6906 | * removal of next header. |
| 6907 | */ |
| 6908 | old_idx = cur_idx; |
| 6909 | } |
| 6910 | return 0; |
| 6911 | } |
| 6912 | |
| 6913 | |
| 6914 | /* Apply the filter to the status line in the response buffer <rtr>. |
| 6915 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 6916 | * or -1 if a replacement resulted in an invalid status line. |
| 6917 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6918 | int apply_filter_to_sts_line(struct stream *s, struct channel *rtr, struct hdr_exp *exp) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6919 | { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6920 | char *cur_ptr, *cur_end; |
| 6921 | int done; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 6922 | struct http_txn *txn = s->txn; |
Willy Tarreau | 6e27be1 | 2018-08-22 04:46:47 +0200 | [diff] [blame] | 6923 | int delta, len; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6924 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6925 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6926 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6927 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6928 | (exp->action == ACT_ALLOW || |
| 6929 | exp->action == ACT_DENY)) |
| 6930 | return 0; |
| 6931 | else if (exp->action == ACT_REMOVE) |
| 6932 | return 0; |
| 6933 | |
| 6934 | done = 0; |
| 6935 | |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 6936 | cur_ptr = ci_head(rtr); |
Willy Tarreau | 1ba0e5f | 2010-06-07 13:57:32 +0200 | [diff] [blame] | 6937 | cur_end = cur_ptr + txn->rsp.sl.st.l; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6938 | |
| 6939 | /* Now we have the status line between cur_ptr and cur_end */ |
| 6940 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 6941 | if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6942 | switch (exp->action) { |
| 6943 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6944 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6945 | done = 1; |
| 6946 | break; |
| 6947 | |
| 6948 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6949 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6950 | done = 1; |
| 6951 | break; |
| 6952 | |
| 6953 | case ACT_REPLACE: |
Willy Tarreau | 6e27be1 | 2018-08-22 04:46:47 +0200 | [diff] [blame] | 6954 | len = exp_replace(trash.area, trash.size, |
| 6955 | cur_ptr, exp->replace, pmatch); |
| 6956 | if (len < 0) |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 6957 | return -1; |
| 6958 | |
Willy Tarreau | 6e27be1 | 2018-08-22 04:46:47 +0200 | [diff] [blame] | 6959 | delta = b_rep_blk(&rtr->buf, cur_ptr, cur_end, trash.area, len); |
| 6960 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6961 | /* FIXME: if the user adds a newline in the replacement, the |
| 6962 | * index will not be recalculated for now, and the new line |
| 6963 | * will not be counted as a new header. |
| 6964 | */ |
| 6965 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6966 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6967 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 6968 | cur_end = (char *)http_parse_stsline(&txn->rsp, |
Willy Tarreau | 0278576 | 2007-04-03 14:45:44 +0200 | [diff] [blame] | 6969 | HTTP_MSG_RPVER, |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6970 | cur_ptr, cur_end + 1, |
| 6971 | NULL, NULL); |
| 6972 | if (unlikely(!cur_end)) |
| 6973 | return -1; |
| 6974 | |
| 6975 | /* we have a full respnse and we know that we have either a CR |
| 6976 | * or an LF at <ptr>. |
| 6977 | */ |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 6978 | txn->status = strl2ui(ci_head(rtr) + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l); |
Willy Tarreau | 1ba0e5f | 2010-06-07 13:57:32 +0200 | [diff] [blame] | 6979 | hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r'); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6980 | /* there is no point trying this regex on headers */ |
| 6981 | return 1; |
| 6982 | } |
| 6983 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6984 | return done; |
| 6985 | } |
| 6986 | |
| 6987 | |
| 6988 | |
| 6989 | /* |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6990 | * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of stream <s>. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6991 | * Returns 0 if everything is alright, or -1 in case a replacement lead to an |
| 6992 | * unparsable response. |
| 6993 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6994 | int apply_filters_to_response(struct stream *s, struct channel *rtr, struct proxy *px) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6995 | { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 6996 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 6997 | struct http_txn *txn = s->txn; |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6998 | struct hdr_exp *exp; |
| 6999 | |
| 7000 | for (exp = px->rsp_exp; exp; exp = exp->next) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7001 | int ret; |
| 7002 | |
| 7003 | /* |
| 7004 | * The interleaving of transformations and verdicts |
| 7005 | * makes it difficult to decide to continue or stop |
| 7006 | * the evaluation. |
| 7007 | */ |
| 7008 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7009 | if (txn->flags & TX_SVDENY) |
| 7010 | break; |
| 7011 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7012 | if ((txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7013 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 7014 | exp->action == ACT_PASS)) { |
| 7015 | exp = exp->next; |
| 7016 | continue; |
| 7017 | } |
| 7018 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7019 | /* if this filter had a condition, evaluate it now and skip to |
| 7020 | * next filter if the condition does not match. |
| 7021 | */ |
| 7022 | if (exp->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 7023 | ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL); |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7024 | ret = acl_pass(ret); |
| 7025 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 7026 | ret = !ret; |
| 7027 | if (!ret) |
| 7028 | continue; |
| 7029 | } |
| 7030 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7031 | /* Apply the filter to the status line. */ |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7032 | ret = apply_filter_to_sts_line(s, rtr, exp); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7033 | if (unlikely(ret < 0)) |
| 7034 | return -1; |
| 7035 | |
| 7036 | if (likely(ret == 0)) { |
| 7037 | /* The filter did not match the response, it can be |
| 7038 | * iterated through all headers. |
| 7039 | */ |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 7040 | if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0)) |
| 7041 | return -1; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7042 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7043 | } |
| 7044 | return 0; |
| 7045 | } |
| 7046 | |
| 7047 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7048 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 7049 | * Manage server-side cookies. It can impact performance by about 2% so it is |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7050 | * desirable to call it only when needed. This function is also used when we |
| 7051 | * just need to know if there is a cookie (eg: for check-cache). |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7052 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7053 | void manage_server_side_cookies(struct stream *s, struct channel *res) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7054 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7055 | struct http_txn *txn = s->txn; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7056 | struct session *sess = s->sess; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 7057 | struct server *srv; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7058 | int is_cookie2; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7059 | int cur_idx, old_idx, delta; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7060 | char *hdr_beg, *hdr_end, *hdr_next; |
| 7061 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7062 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7063 | /* Iterate through the headers. |
| 7064 | * we start with the start line. |
| 7065 | */ |
| 7066 | old_idx = 0; |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 7067 | hdr_next = ci_head(res) + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7068 | |
| 7069 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 7070 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7071 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7072 | |
| 7073 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7074 | hdr_beg = hdr_next; |
| 7075 | hdr_end = hdr_beg + cur_hdr->len; |
| 7076 | hdr_next = hdr_end + cur_hdr->cr + 1; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7077 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7078 | /* We have one full header between hdr_beg and hdr_end, and the |
| 7079 | * next header starts at hdr_next. We're only interested in |
| 7080 | * "Set-Cookie" and "Set-Cookie2" headers. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7081 | */ |
| 7082 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7083 | is_cookie2 = 0; |
| 7084 | prev = hdr_beg + 10; |
| 7085 | val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10); |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7086 | if (!val) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7087 | val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11); |
| 7088 | if (!val) { |
| 7089 | old_idx = cur_idx; |
| 7090 | continue; |
| 7091 | } |
| 7092 | is_cookie2 = 1; |
| 7093 | prev = hdr_beg + 11; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7094 | } |
| 7095 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7096 | /* OK, right now we know we have a Set-Cookie* at hdr_beg, and |
| 7097 | * <prev> points to the colon. |
| 7098 | */ |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7099 | txn->flags |= TX_SCK_PRESENT; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7100 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7101 | /* Maybe we only wanted to see if there was a Set-Cookie (eg: |
| 7102 | * check-cache is enabled) and we are not interested in checking |
| 7103 | * them. Warning, the cookie capture is declared in the frontend. |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 7104 | */ |
Willy Tarreau | 53a09d5 | 2015-08-10 18:59:40 +0200 | [diff] [blame] | 7105 | if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7106 | return; |
| 7107 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7108 | /* OK so now we know we have to process this response cookie. |
| 7109 | * The format of the Set-Cookie header is slightly different |
| 7110 | * from the format of the Cookie header in that it does not |
| 7111 | * support the comma as a cookie delimiter (thus the header |
| 7112 | * cannot be folded) because the Expires attribute described in |
| 7113 | * the original Netscape's spec may contain an unquoted date |
| 7114 | * with a comma inside. We have to live with this because |
| 7115 | * many browsers don't support Max-Age and some browsers don't |
| 7116 | * support quoted strings. However the Set-Cookie2 header is |
| 7117 | * clean. |
| 7118 | * |
| 7119 | * We have to keep multiple pointers in order to support cookie |
| 7120 | * removal at the beginning, middle or end of header without |
| 7121 | * corrupting the header (in case of set-cookie2). A special |
| 7122 | * pointer, <scav> points to the beginning of the set-cookie-av |
| 7123 | * fields after the first semi-colon. The <next> pointer points |
| 7124 | * either to the end of line (set-cookie) or next unquoted comma |
| 7125 | * (set-cookie2). All of these headers are valid : |
| 7126 | * |
| 7127 | * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n |
| 7128 | * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n |
| 7129 | * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n |
| 7130 | * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n |
| 7131 | * | | | | | | | | | | |
| 7132 | * | | | | | | | | +-> next hdr_end <--+ |
| 7133 | * | | | | | | | +------------> scav |
| 7134 | * | | | | | | +--------------> val_end |
| 7135 | * | | | | | +--------------------> val_beg |
| 7136 | * | | | | +----------------------> equal |
| 7137 | * | | | +------------------------> att_end |
| 7138 | * | | +----------------------------> att_beg |
| 7139 | * | +------------------------------> prev |
| 7140 | * +-----------------------------------------> hdr_beg |
| 7141 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7142 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7143 | for (; prev < hdr_end; prev = next) { |
| 7144 | /* Iterate through all cookies on this line */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7145 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7146 | /* find att_beg */ |
| 7147 | att_beg = prev + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 7148 | while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg)) |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7149 | att_beg++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7150 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7151 | /* find att_end : this is the first character after the last non |
| 7152 | * space before the equal. It may be equal to hdr_end. |
| 7153 | */ |
| 7154 | equal = att_end = att_beg; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7155 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7156 | while (equal < hdr_end) { |
| 7157 | if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ',')) |
| 7158 | break; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 7159 | if (HTTP_IS_SPHT(*equal++)) |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7160 | continue; |
| 7161 | att_end = equal; |
| 7162 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7163 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7164 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 7165 | * is between <att_beg> and <equal>, both may be identical. |
| 7166 | */ |
| 7167 | |
| 7168 | /* look for end of cookie if there is an equal sign */ |
| 7169 | if (equal < hdr_end && *equal == '=') { |
| 7170 | /* look for the beginning of the value */ |
| 7171 | val_beg = equal + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 7172 | while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg)) |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7173 | val_beg++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7174 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7175 | /* find the end of the value, respecting quotes */ |
| 7176 | next = find_cookie_value_end(val_beg, hdr_end); |
| 7177 | |
| 7178 | /* make val_end point to the first white space or delimitor after the value */ |
| 7179 | val_end = next; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 7180 | while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1))) |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7181 | val_end--; |
| 7182 | } else { |
| 7183 | /* <equal> points to next comma, semi-colon or EOL */ |
| 7184 | val_beg = val_end = next = equal; |
| 7185 | } |
| 7186 | |
| 7187 | if (next < hdr_end) { |
| 7188 | /* Set-Cookie2 supports multiple cookies, and <next> points to |
| 7189 | * a colon or semi-colon before the end. So skip all attr-value |
| 7190 | * pairs and look for the next comma. For Set-Cookie, since |
| 7191 | * commas are permitted in values, skip to the end. |
| 7192 | */ |
| 7193 | if (is_cookie2) |
| 7194 | next = find_hdr_value_end(next, hdr_end); |
| 7195 | else |
| 7196 | next = hdr_end; |
| 7197 | } |
| 7198 | |
| 7199 | /* Now everything is as on the diagram above */ |
| 7200 | |
| 7201 | /* Ignore cookies with no equal sign */ |
| 7202 | if (equal == val_end) |
| 7203 | continue; |
| 7204 | |
| 7205 | /* If there are spaces around the equal sign, we need to |
| 7206 | * strip them otherwise we'll get trouble for cookie captures, |
| 7207 | * or even for rewrites. Since this happens extremely rarely, |
| 7208 | * it does not hurt performance. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7209 | */ |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7210 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 7211 | int stripped_before = 0; |
| 7212 | int stripped_after = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7213 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7214 | if (att_end != equal) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 7215 | stripped_before = b_rep_blk(&res->buf, att_end, equal, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7216 | equal += stripped_before; |
| 7217 | val_beg += stripped_before; |
| 7218 | } |
| 7219 | |
| 7220 | if (val_beg > equal + 1) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 7221 | stripped_after = b_rep_blk(&res->buf, equal + 1, val_beg, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7222 | val_beg += stripped_after; |
| 7223 | stripped_before += stripped_after; |
| 7224 | } |
| 7225 | |
| 7226 | val_end += stripped_before; |
| 7227 | next += stripped_before; |
| 7228 | hdr_end += stripped_before; |
| 7229 | hdr_next += stripped_before; |
| 7230 | cur_hdr->len += stripped_before; |
Willy Tarreau | 1fc1f45 | 2011-04-07 22:35:37 +0200 | [diff] [blame] | 7231 | http_msg_move_end(&txn->rsp, stripped_before); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7232 | } |
| 7233 | |
| 7234 | /* First, let's see if we want to capture this cookie. We check |
| 7235 | * that we don't already have a server side cookie, because we |
| 7236 | * can only capture one. Also as an optimisation, we ignore |
| 7237 | * cookies shorter than the declared name. |
| 7238 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7239 | if (sess->fe->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 7240 | txn->srv_cookie == NULL && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7241 | (val_end - att_beg >= sess->fe->capture_namelen) && |
| 7242 | memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7243 | int log_len = val_end - att_beg; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7244 | if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 7245 | ha_alert("HTTP logging : out of memory.\n"); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7246 | } |
Willy Tarreau | f70fc75 | 2010-11-19 11:27:18 +0100 | [diff] [blame] | 7247 | else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7248 | if (log_len > sess->fe->capture_len) |
| 7249 | log_len = sess->fe->capture_len; |
Willy Tarreau | f70fc75 | 2010-11-19 11:27:18 +0100 | [diff] [blame] | 7250 | memcpy(txn->srv_cookie, att_beg, log_len); |
| 7251 | txn->srv_cookie[log_len] = 0; |
| 7252 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7253 | } |
| 7254 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7255 | srv = objt_server(s->target); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7256 | /* now check if we need to process it for persistence */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 7257 | if (!(s->flags & SF_IGNORE_PRST) && |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7258 | (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) && |
| 7259 | (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) { |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7260 | /* assume passive cookie by default */ |
| 7261 | txn->flags &= ~TX_SCK_MASK; |
| 7262 | txn->flags |= TX_SCK_FOUND; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7263 | |
| 7264 | /* If the cookie is in insert mode on a known server, we'll delete |
| 7265 | * this occurrence because we'll insert another one later. |
| 7266 | * We'll delete it too if the "indirect" option is set and we're in |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7267 | * a direct access. |
| 7268 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7269 | if (s->be->ck_opts & PR_CK_PSV) { |
Willy Tarreau | ba4c5be | 2010-10-23 12:46:42 +0200 | [diff] [blame] | 7270 | /* The "preserve" flag was set, we don't want to touch the |
| 7271 | * server's cookie. |
| 7272 | */ |
| 7273 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7274 | else if ((srv && (s->be->ck_opts & PR_CK_INS)) || |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 7275 | ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7276 | /* this cookie must be deleted */ |
| 7277 | if (*prev == ':' && next == hdr_end) { |
| 7278 | /* whole header */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 7279 | delta = b_rep_blk(&res->buf, hdr_beg, hdr_next, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7280 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 7281 | txn->hdr_idx.used--; |
| 7282 | cur_hdr->len = 0; |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 7283 | cur_idx = old_idx; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7284 | hdr_next += delta; |
| 7285 | http_msg_move_end(&txn->rsp, delta); |
| 7286 | /* note: while both invalid now, <next> and <hdr_end> |
| 7287 | * are still equal, so the for() will stop as expected. |
| 7288 | */ |
| 7289 | } else { |
| 7290 | /* just remove the value */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 7291 | int delta = del_hdr_value(&res->buf, &prev, next); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7292 | next = prev; |
| 7293 | hdr_end += delta; |
| 7294 | hdr_next += delta; |
| 7295 | cur_hdr->len += delta; |
| 7296 | http_msg_move_end(&txn->rsp, delta); |
| 7297 | } |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7298 | txn->flags &= ~TX_SCK_MASK; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7299 | txn->flags |= TX_SCK_DELETED; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7300 | /* and go on with next cookie */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7301 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7302 | else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7303 | /* replace bytes val_beg->val_end with the cookie name associated |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7304 | * with this server since we know it. |
| 7305 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 7306 | delta = b_rep_blk(&res->buf, val_beg, val_end, srv->cookie, srv->cklen); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7307 | next += delta; |
| 7308 | hdr_end += delta; |
| 7309 | hdr_next += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7310 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7311 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7312 | |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7313 | txn->flags &= ~TX_SCK_MASK; |
| 7314 | txn->flags |= TX_SCK_REPLACED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7315 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7316 | else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7317 | /* insert the cookie name associated with this server |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7318 | * before existing cookie, and insert a delimiter between them.. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7319 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 7320 | delta = b_rep_blk(&res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7321 | next += delta; |
| 7322 | hdr_end += delta; |
| 7323 | hdr_next += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7324 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7325 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7326 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 7327 | val_beg[srv->cklen] = COOKIE_DELIM; |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7328 | txn->flags &= ~TX_SCK_MASK; |
| 7329 | txn->flags |= TX_SCK_REPLACED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7330 | } |
| 7331 | } |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7332 | /* that's done for this cookie, check the next one on the same |
| 7333 | * line when next != hdr_end (only if is_cookie2). |
| 7334 | */ |
| 7335 | } |
| 7336 | /* check next header */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7337 | old_idx = cur_idx; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7338 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7339 | } |
| 7340 | |
| 7341 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7342 | /* |
Willy Tarreau | 0ad8e0d | 2017-12-22 15:03:36 +0100 | [diff] [blame] | 7343 | * Parses the Cache-Control and Pragma request header fields to determine if |
| 7344 | * the request may be served from the cache and/or if it is cacheable. Updates |
| 7345 | * s->txn->flags. |
| 7346 | */ |
| 7347 | void check_request_for_cacheability(struct stream *s, struct channel *chn) |
| 7348 | { |
| 7349 | struct http_txn *txn = s->txn; |
| 7350 | char *p1, *p2; |
| 7351 | char *cur_ptr, *cur_end, *cur_next; |
| 7352 | int pragma_found; |
| 7353 | int cc_found; |
| 7354 | int cur_idx; |
| 7355 | |
| 7356 | if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE) |
| 7357 | return; /* nothing more to do here */ |
| 7358 | |
| 7359 | cur_idx = 0; |
| 7360 | pragma_found = cc_found = 0; |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 7361 | cur_next = ci_head(chn) + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 0ad8e0d | 2017-12-22 15:03:36 +0100 | [diff] [blame] | 7362 | |
| 7363 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 7364 | struct hdr_idx_elem *cur_hdr; |
| 7365 | int val; |
| 7366 | |
| 7367 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 7368 | cur_ptr = cur_next; |
| 7369 | cur_end = cur_ptr + cur_hdr->len; |
| 7370 | cur_next = cur_end + cur_hdr->cr + 1; |
| 7371 | |
| 7372 | /* We have one full header between cur_ptr and cur_end, and the |
| 7373 | * next header starts at cur_next. |
| 7374 | */ |
| 7375 | |
| 7376 | val = http_header_match2(cur_ptr, cur_end, "Pragma", 6); |
| 7377 | if (val) { |
| 7378 | if ((cur_end - (cur_ptr + val) >= 8) && |
| 7379 | strncasecmp(cur_ptr + val, "no-cache", 8) == 0) { |
| 7380 | pragma_found = 1; |
| 7381 | continue; |
| 7382 | } |
| 7383 | } |
| 7384 | |
William Lallemand | 8a16fe0 | 2018-05-22 11:04:33 +0200 | [diff] [blame] | 7385 | /* Don't use the cache and don't try to store if we found the |
| 7386 | * Authorization header */ |
| 7387 | val = http_header_match2(cur_ptr, cur_end, "Authorization", 13); |
| 7388 | if (val) { |
| 7389 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 7390 | txn->flags |= TX_CACHE_IGNORE; |
| 7391 | continue; |
| 7392 | } |
| 7393 | |
Willy Tarreau | 0ad8e0d | 2017-12-22 15:03:36 +0100 | [diff] [blame] | 7394 | val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13); |
| 7395 | if (!val) |
| 7396 | continue; |
| 7397 | |
| 7398 | /* OK, right now we know we have a cache-control header at cur_ptr */ |
| 7399 | cc_found = 1; |
| 7400 | p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */ |
| 7401 | |
| 7402 | if (p1 >= cur_end) /* no more info */ |
| 7403 | continue; |
| 7404 | |
| 7405 | /* p1 is at the beginning of the value */ |
| 7406 | p2 = p1; |
| 7407 | while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2)) |
| 7408 | p2++; |
| 7409 | |
| 7410 | /* we have a complete value between p1 and p2. We don't check the |
| 7411 | * values after max-age, max-stale nor min-fresh, we simply don't |
| 7412 | * use the cache when they're specified. |
| 7413 | */ |
| 7414 | if (((p2 - p1 == 7) && strncasecmp(p1, "max-age", 7) == 0) || |
| 7415 | ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) || |
| 7416 | ((p2 - p1 == 9) && strncasecmp(p1, "max-stale", 9) == 0) || |
| 7417 | ((p2 - p1 == 9) && strncasecmp(p1, "min-fresh", 9) == 0)) { |
| 7418 | txn->flags |= TX_CACHE_IGNORE; |
| 7419 | continue; |
| 7420 | } |
| 7421 | |
| 7422 | if ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) { |
| 7423 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 7424 | continue; |
| 7425 | } |
| 7426 | } |
| 7427 | |
| 7428 | /* RFC7234#5.4: |
| 7429 | * When the Cache-Control header field is also present and |
| 7430 | * understood in a request, Pragma is ignored. |
| 7431 | * When the Cache-Control header field is not present in a |
| 7432 | * request, caches MUST consider the no-cache request |
| 7433 | * pragma-directive as having the same effect as if |
| 7434 | * "Cache-Control: no-cache" were present. |
| 7435 | */ |
| 7436 | if (!cc_found && pragma_found) |
| 7437 | txn->flags |= TX_CACHE_IGNORE; |
| 7438 | } |
| 7439 | |
| 7440 | /* |
Willy Tarreau | d3900cc | 2017-12-22 15:35:11 +0100 | [diff] [blame] | 7441 | * Check if response is cacheable or not. Updates s->txn->flags. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7442 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7443 | void check_response_for_cacheability(struct stream *s, struct channel *rtr) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7444 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7445 | struct http_txn *txn = s->txn; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7446 | char *p1, *p2; |
| 7447 | |
| 7448 | char *cur_ptr, *cur_end, *cur_next; |
| 7449 | int cur_idx; |
| 7450 | |
Willy Tarreau | 12b32f2 | 2017-12-21 16:08:09 +0100 | [diff] [blame] | 7451 | if (txn->status < 200) { |
| 7452 | /* do not try to cache interim responses! */ |
| 7453 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7454 | return; |
Willy Tarreau | 12b32f2 | 2017-12-21 16:08:09 +0100 | [diff] [blame] | 7455 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7456 | |
| 7457 | /* Iterate through the headers. |
| 7458 | * we start with the start line. |
| 7459 | */ |
| 7460 | cur_idx = 0; |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 7461 | cur_next = ci_head(rtr) + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7462 | |
| 7463 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 7464 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7465 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7466 | |
| 7467 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 7468 | cur_ptr = cur_next; |
| 7469 | cur_end = cur_ptr + cur_hdr->len; |
| 7470 | cur_next = cur_end + cur_hdr->cr + 1; |
| 7471 | |
| 7472 | /* We have one full header between cur_ptr and cur_end, and the |
Willy Tarreau | d3900cc | 2017-12-22 15:35:11 +0100 | [diff] [blame] | 7473 | * next header starts at cur_next. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7474 | */ |
| 7475 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7476 | val = http_header_match2(cur_ptr, cur_end, "Pragma", 6); |
| 7477 | if (val) { |
| 7478 | if ((cur_end - (cur_ptr + val) >= 8) && |
| 7479 | strncasecmp(cur_ptr + val, "no-cache", 8) == 0) { |
| 7480 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 7481 | return; |
| 7482 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7483 | } |
| 7484 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7485 | val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13); |
| 7486 | if (!val) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7487 | continue; |
| 7488 | |
| 7489 | /* OK, right now we know we have a cache-control header at cur_ptr */ |
| 7490 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7491 | p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7492 | |
| 7493 | if (p1 >= cur_end) /* no more info */ |
| 7494 | continue; |
| 7495 | |
| 7496 | /* p1 is at the beginning of the value */ |
| 7497 | p2 = p1; |
| 7498 | |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 7499 | while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7500 | p2++; |
| 7501 | |
| 7502 | /* we have a complete value between p1 and p2 */ |
| 7503 | if (p2 < cur_end && *p2 == '=') { |
Willy Tarreau | d3900cc | 2017-12-22 15:35:11 +0100 | [diff] [blame] | 7504 | if (((cur_end - p2) > 1 && (p2 - p1 == 7) && strncasecmp(p1, "max-age=0", 9) == 0) || |
| 7505 | ((cur_end - p2) > 1 && (p2 - p1 == 8) && strncasecmp(p1, "s-maxage=0", 10) == 0)) { |
| 7506 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 7507 | continue; |
| 7508 | } |
| 7509 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7510 | /* we have something of the form no-cache="set-cookie" */ |
| 7511 | if ((cur_end - p1 >= 21) && |
| 7512 | strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0 |
| 7513 | && (p1[20] == '"' || p1[20] == ',')) |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7514 | txn->flags &= ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7515 | continue; |
| 7516 | } |
| 7517 | |
| 7518 | /* OK, so we know that either p2 points to the end of string or to a comma */ |
| 7519 | if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) || |
Willy Tarreau | 5b15f90 | 2013-07-04 12:46:56 +0200 | [diff] [blame] | 7520 | ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) || |
Willy Tarreau | d3900cc | 2017-12-22 15:35:11 +0100 | [diff] [blame] | 7521 | ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0)) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7522 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7523 | return; |
| 7524 | } |
| 7525 | |
| 7526 | if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7527 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7528 | continue; |
| 7529 | } |
| 7530 | } |
| 7531 | } |
| 7532 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7533 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7534 | /* |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7535 | * In a GET, HEAD or POST request, check if the requested URI matches the stats uri |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 7536 | * for the current backend. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7537 | * |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7538 | * It is assumed that the request is either a HEAD, GET, or POST and that the |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7539 | * uri_auth field is valid. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7540 | * |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 7541 | * Returns 1 if stats should be provided, otherwise 0. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7542 | */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7543 | int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7544 | { |
| 7545 | struct uri_auth *uri_auth = backend->uri_auth; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7546 | struct http_msg *msg = &txn->req; |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 7547 | const char *uri = ci_head(msg->chn)+ msg->sl.rq.u; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7548 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 7549 | if (!uri_auth) |
| 7550 | return 0; |
| 7551 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7552 | if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST) |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 7553 | return 0; |
| 7554 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7555 | /* check URI size */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7556 | if (uri_auth->uri_len > msg->sl.rq.u_l) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7557 | return 0; |
| 7558 | |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 7559 | if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7560 | return 0; |
| 7561 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7562 | return 1; |
| 7563 | } |
| 7564 | |
Willy Tarreau | 7ccdd8d | 2018-09-07 14:01:39 +0200 | [diff] [blame] | 7565 | /* Append the description of what is present in error snapshot <es> into <out>. |
| 7566 | * The description must be small enough to always fit in a trash. The output |
| 7567 | * buffer may be the trash so the trash must not be used inside this function. |
| 7568 | */ |
| 7569 | void http_show_error_snapshot(struct buffer *out, const struct error_snapshot *es) |
| 7570 | { |
| 7571 | chunk_appendf(&trash, |
| 7572 | " stream #%d, stream flags 0x%08x, tx flags 0x%08x\n" |
| 7573 | " HTTP msg state %s(%d), msg flags 0x%08x\n" |
| 7574 | " HTTP chunk len %lld bytes, HTTP body len %lld bytes, channel flags 0x%08x :\n", |
| 7575 | es->ctx.http.sid, es->ctx.http.s_flags, es->ctx.http.t_flags, |
| 7576 | h1_msg_state_str(es->ctx.http.state), es->ctx.http.state, |
| 7577 | es->ctx.http.m_flags, es->ctx.http.m_clen, |
| 7578 | es->ctx.http.m_blen, es->ctx.http.b_flags); |
| 7579 | } |
| 7580 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7581 | /* |
| 7582 | * Capture a bad request or response and archive it in the proxy's structure. |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 7583 | * By default it tries to report the error position as msg->err_pos. However if |
| 7584 | * this one is not set, it will then report msg->next, which is the last known |
| 7585 | * parsing point. The function is able to deal with wrapping buffers. It always |
Willy Tarreau | fd9419d | 2018-09-07 18:01:03 +0200 | [diff] [blame] | 7586 | * displays buffers as a contiguous area starting at buf->p. The direction is |
| 7587 | * determined thanks to the channel's flags. |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7588 | */ |
Willy Tarreau | fd9419d | 2018-09-07 18:01:03 +0200 | [diff] [blame] | 7589 | void http_capture_bad_message(struct proxy *proxy, struct stream *s, |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 7590 | struct http_msg *msg, |
Willy Tarreau | 0da5b3b | 2017-09-21 09:30:46 +0200 | [diff] [blame] | 7591 | enum h1_state state, struct proxy *other_end) |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7592 | { |
Willy Tarreau | ef3ca73 | 2018-09-07 15:47:35 +0200 | [diff] [blame] | 7593 | union error_snapshot_ctx ctx; |
| 7594 | long ofs; |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 7595 | |
Willy Tarreau | ef3ca73 | 2018-09-07 15:47:35 +0200 | [diff] [blame] | 7596 | /* http-specific part now */ |
| 7597 | ctx.http.sid = s->uniq_id; |
| 7598 | ctx.http.state = state; |
| 7599 | ctx.http.b_flags = msg->chn->flags; |
| 7600 | ctx.http.s_flags = s->flags; |
| 7601 | ctx.http.t_flags = s->txn->flags; |
| 7602 | ctx.http.m_flags = msg->flags; |
| 7603 | ctx.http.m_clen = msg->chunk_len; |
| 7604 | ctx.http.m_blen = msg->body_len; |
Willy Tarreau | 7480f32 | 2018-09-06 19:41:22 +0200 | [diff] [blame] | 7605 | |
Willy Tarreau | ef3ca73 | 2018-09-07 15:47:35 +0200 | [diff] [blame] | 7606 | ofs = msg->chn->total - ci_data(msg->chn); |
| 7607 | if (ofs < 0) |
| 7608 | ofs = 0; |
Willy Tarreau | 0b5b480 | 2018-09-07 13:49:44 +0200 | [diff] [blame] | 7609 | |
Willy Tarreau | fd9419d | 2018-09-07 18:01:03 +0200 | [diff] [blame] | 7610 | proxy_capture_error(proxy, !!(msg->chn->flags & CF_ISRESP), |
Willy Tarreau | ef3ca73 | 2018-09-07 15:47:35 +0200 | [diff] [blame] | 7611 | other_end, s->target, |
| 7612 | strm_sess(s), &msg->chn->buf, |
| 7613 | ofs, co_data(msg->chn), |
| 7614 | (msg->err_pos >= 0) ? msg->err_pos : msg->next, |
| 7615 | &ctx, http_show_error_snapshot); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7616 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7617 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7618 | /* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of |
| 7619 | * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is |
| 7620 | * performed over the whole headers. Otherwise it must contain a valid header |
| 7621 | * context, initialised with ctx->idx=0 for the first lookup in a series. If |
| 7622 | * <occ> is positive or null, occurrence #occ from the beginning (or last ctx) |
| 7623 | * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less |
| 7624 | * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 7625 | * -1. The value fetch stops at commas, so this function is suited for use with |
| 7626 | * list headers. |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7627 | * The return value is 0 if nothing was found, or non-zero otherwise. |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7628 | */ |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 7629 | unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen, |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7630 | struct hdr_idx *idx, int occ, |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7631 | struct hdr_ctx *ctx, char **vptr, size_t *vlen) |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7632 | { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7633 | struct hdr_ctx local_ctx; |
| 7634 | char *ptr_hist[MAX_HDR_HISTORY]; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7635 | unsigned int len_hist[MAX_HDR_HISTORY]; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7636 | unsigned int hist_ptr; |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7637 | int found; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7638 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7639 | if (!ctx) { |
| 7640 | local_ctx.idx = 0; |
| 7641 | ctx = &local_ctx; |
| 7642 | } |
| 7643 | |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7644 | if (occ >= 0) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7645 | /* search from the beginning */ |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 7646 | while (http_find_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) { |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7647 | occ--; |
| 7648 | if (occ <= 0) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7649 | *vptr = ctx->line + ctx->val; |
| 7650 | *vlen = ctx->vlen; |
| 7651 | return 1; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7652 | } |
| 7653 | } |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7654 | return 0; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7655 | } |
| 7656 | |
| 7657 | /* negative occurrence, we scan all the list then walk back */ |
| 7658 | if (-occ > MAX_HDR_HISTORY) |
| 7659 | return 0; |
| 7660 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7661 | found = hist_ptr = 0; |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 7662 | while (http_find_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7663 | ptr_hist[hist_ptr] = ctx->line + ctx->val; |
| 7664 | len_hist[hist_ptr] = ctx->vlen; |
| 7665 | if (++hist_ptr >= MAX_HDR_HISTORY) |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7666 | hist_ptr = 0; |
| 7667 | found++; |
| 7668 | } |
| 7669 | if (-occ > found) |
| 7670 | return 0; |
| 7671 | /* OK now we have the last occurrence in [hist_ptr-1], and we need to |
Willy Tarreau | 67dad27 | 2013-06-12 22:27:44 +0200 | [diff] [blame] | 7672 | * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have |
| 7673 | * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ] |
| 7674 | * to remain in the 0..9 range. |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7675 | */ |
Willy Tarreau | 67dad27 | 2013-06-12 22:27:44 +0200 | [diff] [blame] | 7676 | hist_ptr += occ + MAX_HDR_HISTORY; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7677 | if (hist_ptr >= MAX_HDR_HISTORY) |
| 7678 | hist_ptr -= MAX_HDR_HISTORY; |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7679 | *vptr = ptr_hist[hist_ptr]; |
| 7680 | *vlen = len_hist[hist_ptr]; |
| 7681 | return 1; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7682 | } |
| 7683 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 7684 | /* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of |
| 7685 | * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is |
| 7686 | * performed over the whole headers. Otherwise it must contain a valid header |
| 7687 | * context, initialised with ctx->idx=0 for the first lookup in a series. If |
| 7688 | * <occ> is positive or null, occurrence #occ from the beginning (or last ctx) |
| 7689 | * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less |
| 7690 | * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is |
| 7691 | * -1. This function differs from http_get_hdr() in that it only returns full |
| 7692 | * line header values and does not stop at commas. |
| 7693 | * The return value is 0 if nothing was found, or non-zero otherwise. |
| 7694 | */ |
| 7695 | unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen, |
| 7696 | struct hdr_idx *idx, int occ, |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7697 | struct hdr_ctx *ctx, char **vptr, size_t *vlen) |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 7698 | { |
| 7699 | struct hdr_ctx local_ctx; |
| 7700 | char *ptr_hist[MAX_HDR_HISTORY]; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7701 | unsigned int len_hist[MAX_HDR_HISTORY]; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 7702 | unsigned int hist_ptr; |
| 7703 | int found; |
| 7704 | |
| 7705 | if (!ctx) { |
| 7706 | local_ctx.idx = 0; |
| 7707 | ctx = &local_ctx; |
| 7708 | } |
| 7709 | |
| 7710 | if (occ >= 0) { |
| 7711 | /* search from the beginning */ |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 7712 | while (http_find_full_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) { |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 7713 | occ--; |
| 7714 | if (occ <= 0) { |
| 7715 | *vptr = ctx->line + ctx->val; |
| 7716 | *vlen = ctx->vlen; |
| 7717 | return 1; |
| 7718 | } |
| 7719 | } |
| 7720 | return 0; |
| 7721 | } |
| 7722 | |
| 7723 | /* negative occurrence, we scan all the list then walk back */ |
| 7724 | if (-occ > MAX_HDR_HISTORY) |
| 7725 | return 0; |
| 7726 | |
| 7727 | found = hist_ptr = 0; |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 7728 | while (http_find_full_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) { |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 7729 | ptr_hist[hist_ptr] = ctx->line + ctx->val; |
| 7730 | len_hist[hist_ptr] = ctx->vlen; |
| 7731 | if (++hist_ptr >= MAX_HDR_HISTORY) |
| 7732 | hist_ptr = 0; |
| 7733 | found++; |
| 7734 | } |
| 7735 | if (-occ > found) |
| 7736 | return 0; |
Nenad Merdanovic | 69ad4b9 | 2016-03-29 13:14:30 +0200 | [diff] [blame] | 7737 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 7738 | /* OK now we have the last occurrence in [hist_ptr-1], and we need to |
Nenad Merdanovic | 69ad4b9 | 2016-03-29 13:14:30 +0200 | [diff] [blame] | 7739 | * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have |
| 7740 | * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ] |
| 7741 | * to remain in the 0..9 range. |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 7742 | */ |
Nenad Merdanovic | 69ad4b9 | 2016-03-29 13:14:30 +0200 | [diff] [blame] | 7743 | hist_ptr += occ + MAX_HDR_HISTORY; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 7744 | if (hist_ptr >= MAX_HDR_HISTORY) |
| 7745 | hist_ptr -= MAX_HDR_HISTORY; |
| 7746 | *vptr = ptr_hist[hist_ptr]; |
| 7747 | *vlen = len_hist[hist_ptr]; |
| 7748 | return 1; |
| 7749 | } |
| 7750 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 7751 | /* |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 7752 | * Print a debug line with a header. Always stop at the first CR or LF char, |
| 7753 | * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an |
| 7754 | * arrow is printed after the line which contains the pointer. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7755 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7756 | void debug_hdr(const char *dir, struct stream *s, const char *start, const char *end) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7757 | { |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 7758 | struct session *sess = strm_sess(s); |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7759 | int max; |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 7760 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7761 | chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id, |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7762 | dir, |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 7763 | objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1, |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 7764 | objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1); |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 7765 | |
| 7766 | for (max = 0; start + max < end; max++) |
| 7767 | if (start[max] == '\r' || start[max] == '\n') |
| 7768 | break; |
| 7769 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7770 | UBOUND(max, trash.size - trash.data - 3); |
| 7771 | trash.data += strlcpy2(trash.area + trash.data, start, max + 1); |
| 7772 | trash.area[trash.data++] = '\n'; |
| 7773 | shut_your_big_mouth_gcc(write(1, trash.area, trash.data)); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7774 | } |
| 7775 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7776 | |
| 7777 | /* Allocate a new HTTP transaction for stream <s> unless there is one already. |
| 7778 | * The hdr_idx is allocated as well. In case of allocation failure, everything |
| 7779 | * allocated is freed and NULL is returned. Otherwise the new transaction is |
| 7780 | * assigned to the stream and returned. |
| 7781 | */ |
| 7782 | struct http_txn *http_alloc_txn(struct stream *s) |
| 7783 | { |
| 7784 | struct http_txn *txn = s->txn; |
| 7785 | |
| 7786 | if (txn) |
| 7787 | return txn; |
| 7788 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7789 | txn = pool_alloc(pool_head_http_txn); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7790 | if (!txn) |
| 7791 | return txn; |
| 7792 | |
| 7793 | txn->hdr_idx.size = global.tune.max_http_hdr; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7794 | txn->hdr_idx.v = pool_alloc(pool_head_hdr_idx); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7795 | if (!txn->hdr_idx.v) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7796 | pool_free(pool_head_http_txn, txn); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7797 | return NULL; |
| 7798 | } |
| 7799 | |
| 7800 | s->txn = txn; |
| 7801 | return txn; |
| 7802 | } |
| 7803 | |
Thierry FOURNIER | fd50f0b | 2015-09-25 18:53:18 +0200 | [diff] [blame] | 7804 | void http_txn_reset_req(struct http_txn *txn) |
| 7805 | { |
| 7806 | txn->req.flags = 0; |
| 7807 | txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */ |
| 7808 | txn->req.next = 0; |
| 7809 | txn->req.chunk_len = 0LL; |
| 7810 | txn->req.body_len = 0LL; |
| 7811 | txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */ |
| 7812 | } |
| 7813 | |
| 7814 | void http_txn_reset_res(struct http_txn *txn) |
| 7815 | { |
| 7816 | txn->rsp.flags = 0; |
| 7817 | txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */ |
| 7818 | txn->rsp.next = 0; |
| 7819 | txn->rsp.chunk_len = 0LL; |
| 7820 | txn->rsp.body_len = 0LL; |
| 7821 | txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */ |
| 7822 | } |
| 7823 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7824 | /* |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7825 | * Initialize a new HTTP transaction for stream <s>. It is assumed that all |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7826 | * the required fields are properly allocated and that we only need to (re)init |
| 7827 | * them. This should be used before processing any new request. |
| 7828 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7829 | void http_init_txn(struct stream *s) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7830 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7831 | struct http_txn *txn = s->txn; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 7832 | struct proxy *fe = strm_fe(s); |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7833 | |
| 7834 | txn->flags = 0; |
| 7835 | txn->status = -1; |
| 7836 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7837 | txn->cookie_first_date = 0; |
| 7838 | txn->cookie_last_date = 0; |
| 7839 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7840 | txn->srv_cookie = NULL; |
| 7841 | txn->cli_cookie = NULL; |
| 7842 | txn->uri = NULL; |
| 7843 | |
Thierry FOURNIER | fd50f0b | 2015-09-25 18:53:18 +0200 | [diff] [blame] | 7844 | http_txn_reset_req(txn); |
| 7845 | http_txn_reset_res(txn); |
| 7846 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 7847 | txn->req.chn = &s->req; |
| 7848 | txn->rsp.chn = &s->res; |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 7849 | |
| 7850 | txn->auth.method = HTTP_AUTH_UNKNOWN; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7851 | |
| 7852 | txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */ |
| 7853 | if (fe->options2 & PR_O2_REQBUG_OK) |
| 7854 | txn->req.err_pos = -1; /* let buggy requests pass */ |
| 7855 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7856 | if (txn->hdr_idx.v) |
| 7857 | hdr_idx_init(&txn->hdr_idx); |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 7858 | |
| 7859 | vars_init(&s->vars_txn, SCOPE_TXN); |
| 7860 | vars_init(&s->vars_reqres, SCOPE_REQ); |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7861 | } |
| 7862 | |
| 7863 | /* to be used at the end of a transaction */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7864 | void http_end_txn(struct stream *s) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7865 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7866 | struct http_txn *txn = s->txn; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 7867 | struct proxy *fe = strm_fe(s); |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7868 | |
| 7869 | /* these ones will have been dynamically allocated */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7870 | pool_free(pool_head_requri, txn->uri); |
| 7871 | pool_free(pool_head_capture, txn->cli_cookie); |
| 7872 | pool_free(pool_head_capture, txn->srv_cookie); |
| 7873 | pool_free(pool_head_uniqueid, s->unique_id); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 7874 | |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 7875 | s->unique_id = NULL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7876 | txn->uri = NULL; |
| 7877 | txn->srv_cookie = NULL; |
| 7878 | txn->cli_cookie = NULL; |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 7879 | |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 7880 | if (s->req_cap) { |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 7881 | struct cap_hdr *h; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7882 | for (h = fe->req_cap; h; h = h->next) |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7883 | pool_free(h->pool, s->req_cap[h->index]); |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 7884 | memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *)); |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 7885 | } |
| 7886 | |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 7887 | if (s->res_cap) { |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 7888 | struct cap_hdr *h; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7889 | for (h = fe->rsp_cap; h; h = h->next) |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7890 | pool_free(h->pool, s->res_cap[h->index]); |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 7891 | memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *)); |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 7892 | } |
| 7893 | |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 7894 | vars_prune(&s->vars_txn, s->sess, s); |
| 7895 | vars_prune(&s->vars_reqres, s->sess, s); |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7896 | } |
| 7897 | |
| 7898 | /* to be used at the end of a transaction to prepare a new one */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7899 | void http_reset_txn(struct stream *s) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7900 | { |
| 7901 | http_end_txn(s); |
| 7902 | http_init_txn(s); |
| 7903 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 7904 | /* reinitialise the current rule list pointer to NULL. We are sure that |
| 7905 | * any rulelist match the NULL pointer. |
| 7906 | */ |
| 7907 | s->current_rule_list = NULL; |
| 7908 | |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 7909 | s->be = strm_fe(s); |
| 7910 | s->logs.logwait = strm_fe(s)->to_log; |
Willy Tarreau | abcd514 | 2013-06-11 17:18:02 +0200 | [diff] [blame] | 7911 | s->logs.level = 0; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7912 | stream_del_srv_conn(s); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 7913 | s->target = NULL; |
Emeric Brun | b982a3d | 2010-01-04 15:45:53 +0100 | [diff] [blame] | 7914 | /* re-init store persistence */ |
| 7915 | s->store_count = 0; |
Willy Tarreau | 90a7c03 | 2018-09-05 16:21:29 +0200 | [diff] [blame] | 7916 | s->uniq_id = HA_ATOMIC_XADD(&global.req_count, 1); |
Emeric Brun | b982a3d | 2010-01-04 15:45:53 +0100 | [diff] [blame] | 7917 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 7918 | s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */ |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7919 | |
Willy Tarreau | 739cfba | 2010-01-25 23:11:14 +0100 | [diff] [blame] | 7920 | /* We must trim any excess data from the response buffer, because we |
| 7921 | * may have blocked an invalid response from a server that we don't |
| 7922 | * want to accidentely forward once we disable the analysers, nor do |
| 7923 | * we want those data to come along with next response. A typical |
| 7924 | * example of such data would be from a buggy server responding to |
| 7925 | * a HEAD with some data, or sending more than the advertised |
| 7926 | * content-length. |
| 7927 | */ |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 7928 | if (unlikely(ci_data(&s->res))) |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 7929 | b_set_data(&s->res.buf, co_data(&s->res)); |
Willy Tarreau | 739cfba | 2010-01-25 23:11:14 +0100 | [diff] [blame] | 7930 | |
Christopher Faulet | c0c672a | 2017-03-28 11:51:33 +0200 | [diff] [blame] | 7931 | /* Now we can realign the response buffer */ |
Willy Tarreau | d5b343b | 2018-06-06 06:42:46 +0200 | [diff] [blame] | 7932 | c_realign_if_empty(&s->res); |
Christopher Faulet | c0c672a | 2017-03-28 11:51:33 +0200 | [diff] [blame] | 7933 | |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 7934 | s->req.rto = strm_fe(s)->timeout.client; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 7935 | s->req.wto = TICK_ETERNITY; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7936 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 7937 | s->res.rto = TICK_ETERNITY; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 7938 | s->res.wto = strm_fe(s)->timeout.client; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7939 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 7940 | s->req.rex = TICK_ETERNITY; |
| 7941 | s->req.wex = TICK_ETERNITY; |
| 7942 | s->req.analyse_exp = TICK_ETERNITY; |
| 7943 | s->res.rex = TICK_ETERNITY; |
| 7944 | s->res.wex = TICK_ETERNITY; |
| 7945 | s->res.analyse_exp = TICK_ETERNITY; |
Hongbo Long | e39683c | 2017-03-10 18:41:51 +0100 | [diff] [blame] | 7946 | s->si[1].hcto = TICK_ETERNITY; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7947 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7948 | |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 7949 | void free_http_res_rules(struct list *r) |
| 7950 | { |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 7951 | struct act_rule *tr, *pr; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 7952 | |
| 7953 | list_for_each_entry_safe(pr, tr, r, list) { |
| 7954 | LIST_DEL(&pr->list); |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 7955 | regex_free(&pr->arg.hdr_add.re); |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 7956 | free(pr); |
| 7957 | } |
| 7958 | } |
| 7959 | |
| 7960 | void free_http_req_rules(struct list *r) |
| 7961 | { |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 7962 | struct act_rule *tr, *pr; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 7963 | |
| 7964 | list_for_each_entry_safe(pr, tr, r, list) { |
| 7965 | LIST_DEL(&pr->list); |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 7966 | if (pr->action == ACT_HTTP_REQ_AUTH) |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 7967 | free(pr->arg.auth.realm); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 7968 | |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 7969 | regex_free(&pr->arg.hdr_add.re); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 7970 | free(pr); |
| 7971 | } |
| 7972 | } |
| 7973 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 7974 | /* parse an "http-request" rule */ |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 7975 | struct act_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy) |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 7976 | { |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 7977 | struct act_rule *rule; |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 7978 | struct action_kw *custom = NULL; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 7979 | int cur_arg; |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 7980 | char *error; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 7981 | |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 7982 | rule = calloc(1, sizeof(*rule)); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 7983 | if (!rule) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 7984 | ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 7985 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 7986 | } |
| 7987 | |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 7988 | if (!strcmp(args[0], "allow")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 7989 | rule->action = ACT_ACTION_ALLOW; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 7990 | cur_arg = 1; |
Jarno Huuskonen | 800d176 | 2017-03-06 14:56:36 +0200 | [diff] [blame] | 7991 | } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block") || !strcmp(args[0], "tarpit")) { |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 7992 | int code; |
| 7993 | int hc; |
| 7994 | |
Jarno Huuskonen | 800d176 | 2017-03-06 14:56:36 +0200 | [diff] [blame] | 7995 | if (!strcmp(args[0], "tarpit")) { |
| 7996 | rule->action = ACT_HTTP_REQ_TARPIT; |
| 7997 | rule->deny_status = HTTP_ERR_500; |
| 7998 | } |
| 7999 | else { |
| 8000 | rule->action = ACT_ACTION_DENY; |
| 8001 | rule->deny_status = HTTP_ERR_403; |
| 8002 | } |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8003 | cur_arg = 1; |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 8004 | if (strcmp(args[cur_arg], "deny_status") == 0) { |
| 8005 | cur_arg++; |
| 8006 | if (!args[cur_arg]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8007 | ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing status code.\n", |
| 8008 | file, linenum, proxy_type_str(proxy), proxy->id, args[0]); |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 8009 | goto out_err; |
| 8010 | } |
| 8011 | |
| 8012 | code = atol(args[cur_arg]); |
| 8013 | cur_arg++; |
| 8014 | for (hc = 0; hc < HTTP_ERR_SIZE; hc++) { |
| 8015 | if (http_err_codes[hc] == code) { |
| 8016 | rule->deny_status = hc; |
| 8017 | break; |
| 8018 | } |
| 8019 | } |
| 8020 | |
| 8021 | if (hc >= HTTP_ERR_SIZE) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8022 | ha_warning("parsing [%s:%d] : status code %d not handled, using default code %d.\n", |
| 8023 | file, linenum, code, http_err_codes[rule->deny_status]); |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 8024 | } |
| 8025 | } |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8026 | } else if (!strcmp(args[0], "auth")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8027 | rule->action = ACT_HTTP_REQ_AUTH; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8028 | cur_arg = 1; |
| 8029 | |
| 8030 | while(*args[cur_arg]) { |
| 8031 | if (!strcmp(args[cur_arg], "realm")) { |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 8032 | rule->arg.auth.realm = strdup(args[cur_arg + 1]); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8033 | cur_arg+=2; |
| 8034 | continue; |
| 8035 | } else |
| 8036 | break; |
| 8037 | } |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 8038 | } else if (!strcmp(args[0], "set-nice")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8039 | rule->action = ACT_HTTP_SET_NICE; |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 8040 | cur_arg = 1; |
| 8041 | |
| 8042 | if (!*args[cur_arg] || |
| 8043 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8044 | ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n", |
| 8045 | file, linenum, args[0]); |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 8046 | goto out_err; |
| 8047 | } |
| 8048 | rule->arg.nice = atoi(args[cur_arg]); |
| 8049 | if (rule->arg.nice < -1024) |
| 8050 | rule->arg.nice = -1024; |
| 8051 | else if (rule->arg.nice > 1024) |
| 8052 | rule->arg.nice = 1024; |
| 8053 | cur_arg++; |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 8054 | } else if (!strcmp(args[0], "set-tos")) { |
| 8055 | #ifdef IP_TOS |
| 8056 | char *err; |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8057 | rule->action = ACT_HTTP_SET_TOS; |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 8058 | cur_arg = 1; |
| 8059 | |
| 8060 | if (!*args[cur_arg] || |
| 8061 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8062 | ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n", |
| 8063 | file, linenum, args[0]); |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 8064 | goto out_err; |
| 8065 | } |
| 8066 | |
| 8067 | rule->arg.tos = strtol(args[cur_arg], &err, 0); |
| 8068 | if (err && *err != '\0') { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8069 | ha_alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n", |
| 8070 | file, linenum, err, args[0]); |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 8071 | goto out_err; |
| 8072 | } |
| 8073 | cur_arg++; |
| 8074 | #else |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8075 | ha_alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]); |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 8076 | goto out_err; |
| 8077 | #endif |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 8078 | } else if (!strcmp(args[0], "set-mark")) { |
| 8079 | #ifdef SO_MARK |
| 8080 | char *err; |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8081 | rule->action = ACT_HTTP_SET_MARK; |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 8082 | cur_arg = 1; |
| 8083 | |
| 8084 | if (!*args[cur_arg] || |
| 8085 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8086 | ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n", |
| 8087 | file, linenum, args[0]); |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 8088 | goto out_err; |
| 8089 | } |
| 8090 | |
| 8091 | rule->arg.mark = strtoul(args[cur_arg], &err, 0); |
| 8092 | if (err && *err != '\0') { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8093 | ha_alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n", |
| 8094 | file, linenum, err, args[0]); |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 8095 | goto out_err; |
| 8096 | } |
| 8097 | cur_arg++; |
| 8098 | global.last_checks |= LSTCHK_NETADM; |
| 8099 | #else |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8100 | ha_alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]); |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 8101 | goto out_err; |
| 8102 | #endif |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 8103 | } else if (!strcmp(args[0], "set-log-level")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8104 | rule->action = ACT_HTTP_SET_LOGL; |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 8105 | cur_arg = 1; |
| 8106 | |
| 8107 | if (!*args[cur_arg] || |
| 8108 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 8109 | bad_log_level: |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8110 | ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n", |
| 8111 | file, linenum, args[0]); |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 8112 | goto out_err; |
| 8113 | } |
| 8114 | if (strcmp(args[cur_arg], "silent") == 0) |
| 8115 | rule->arg.loglevel = -1; |
| 8116 | else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0) |
| 8117 | goto bad_log_level; |
| 8118 | cur_arg++; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 8119 | } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8120 | rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 8121 | cur_arg = 1; |
| 8122 | |
Willy Tarreau | 8d1c516 | 2013-04-03 14:13:58 +0200 | [diff] [blame] | 8123 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 8124 | (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8125 | ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n", |
| 8126 | file, linenum, args[0]); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8127 | goto out_err; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 8128 | } |
| 8129 | |
| 8130 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 8131 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 8132 | LIST_INIT(&rule->arg.hdr_add.fmt); |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 8133 | |
Thierry FOURNIER | 1c0054f | 2013-11-20 15:09:52 +0100 | [diff] [blame] | 8134 | proxy->conf.args.ctx = ARGC_HRQ; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8135 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8136 | if (!parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP, |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8137 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8138 | ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n", |
| 8139 | file, linenum, args[0], error); |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8140 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8141 | goto out_err; |
| 8142 | } |
Willy Tarreau | 59ad1a2 | 2014-01-29 14:39:58 +0100 | [diff] [blame] | 8143 | free(proxy->conf.lfs_file); |
| 8144 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 8145 | proxy->conf.lfs_line = proxy->conf.args.line; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 8146 | cur_arg += 2; |
Willy Tarreau | b854392 | 2014-06-17 18:58:26 +0200 | [diff] [blame] | 8147 | } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8148 | rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 8149 | cur_arg = 1; |
| 8150 | |
| 8151 | if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] || |
Baptiste Assmann | 92df370 | 2014-06-24 11:10:00 +0200 | [diff] [blame] | 8152 | (*args[cur_arg+3] && strcmp(args[cur_arg+3], "if") != 0 && strcmp(args[cur_arg+3], "unless") != 0)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8153 | ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n", |
| 8154 | file, linenum, args[0]); |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 8155 | goto out_err; |
| 8156 | } |
| 8157 | |
| 8158 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 8159 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 8160 | LIST_INIT(&rule->arg.hdr_add.fmt); |
| 8161 | |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 8162 | error = NULL; |
| 8163 | if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8164 | ha_alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum, |
| 8165 | args[cur_arg + 1], error); |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 8166 | free(error); |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 8167 | goto out_err; |
| 8168 | } |
| 8169 | |
| 8170 | proxy->conf.args.ctx = ARGC_HRQ; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8171 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8172 | if (!parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP, |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8173 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8174 | ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n", |
| 8175 | file, linenum, args[0], error); |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8176 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8177 | goto out_err; |
| 8178 | } |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 8179 | |
| 8180 | free(proxy->conf.lfs_file); |
| 8181 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 8182 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 8183 | cur_arg += 3; |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 8184 | } else if (strcmp(args[0], "del-header") == 0) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8185 | rule->action = ACT_HTTP_DEL_HDR; |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 8186 | cur_arg = 1; |
| 8187 | |
| 8188 | if (!*args[cur_arg] || |
| 8189 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8190 | ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 8191 | file, linenum, args[0]); |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 8192 | goto out_err; |
| 8193 | } |
| 8194 | |
| 8195 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 8196 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 8197 | |
| 8198 | proxy->conf.args.ctx = ARGC_HRQ; |
| 8199 | free(proxy->conf.lfs_file); |
| 8200 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 8201 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 8202 | cur_arg += 1; |
Frédéric Lécaille | a41d531 | 2018-01-29 12:05:07 +0100 | [diff] [blame] | 8203 | } else if (strncmp(args[0], "track-sc", 8) == 0) { |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 8204 | struct sample_expr *expr; |
| 8205 | unsigned int where; |
| 8206 | char *err = NULL; |
Frédéric Lécaille | a41d531 | 2018-01-29 12:05:07 +0100 | [diff] [blame] | 8207 | unsigned int tsc_num; |
| 8208 | const char *tsc_num_str; |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 8209 | |
| 8210 | cur_arg = 1; |
| 8211 | proxy->conf.args.ctx = ARGC_TRK; |
| 8212 | |
Frédéric Lécaille | a41d531 | 2018-01-29 12:05:07 +0100 | [diff] [blame] | 8213 | tsc_num_str = &args[0][8]; |
| 8214 | if (cfg_parse_track_sc_num(&tsc_num, tsc_num_str, tsc_num_str + strlen(tsc_num_str), &err) == -1) { |
| 8215 | ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n", |
| 8216 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], err); |
| 8217 | free(err); |
| 8218 | goto out_err; |
| 8219 | } |
| 8220 | |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 8221 | expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args); |
| 8222 | if (!expr) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8223 | ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n", |
| 8224 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], err); |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 8225 | free(err); |
| 8226 | goto out_err; |
| 8227 | } |
| 8228 | |
| 8229 | where = 0; |
| 8230 | if (proxy->cap & PR_CAP_FE) |
| 8231 | where |= SMP_VAL_FE_HRQ_HDR; |
| 8232 | if (proxy->cap & PR_CAP_BE) |
| 8233 | where |= SMP_VAL_BE_HRQ_HDR; |
| 8234 | |
| 8235 | if (!(expr->fetch->val & where)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8236 | ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :" |
| 8237 | " fetch method '%s' extracts information from '%s', none of which is available here.\n", |
| 8238 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], |
| 8239 | args[cur_arg-1], sample_src_names(expr->fetch->use)); |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 8240 | free(expr); |
| 8241 | goto out_err; |
| 8242 | } |
| 8243 | |
| 8244 | if (strcmp(args[cur_arg], "table") == 0) { |
| 8245 | cur_arg++; |
| 8246 | if (!args[cur_arg]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8247 | ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing table name.\n", |
| 8248 | file, linenum, proxy_type_str(proxy), proxy->id, args[0]); |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 8249 | free(expr); |
| 8250 | goto out_err; |
| 8251 | } |
| 8252 | /* we copy the table name for now, it will be resolved later */ |
Thierry FOURNIER | 5ec63e0 | 2015-08-04 09:09:48 +0200 | [diff] [blame] | 8253 | rule->arg.trk_ctr.table.n = strdup(args[cur_arg]); |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 8254 | cur_arg++; |
| 8255 | } |
Thierry FOURNIER | 5ec63e0 | 2015-08-04 09:09:48 +0200 | [diff] [blame] | 8256 | rule->arg.trk_ctr.expr = expr; |
Frédéric Lécaille | a41d531 | 2018-01-29 12:05:07 +0100 | [diff] [blame] | 8257 | rule->action = ACT_ACTION_TRK_SC0 + tsc_num; |
Christopher Faulet | 78880fb | 2017-09-18 14:43:55 +0200 | [diff] [blame] | 8258 | rule->check_ptr = check_trk_action; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8259 | } else if (strcmp(args[0], "redirect") == 0) { |
| 8260 | struct redirect_rule *redir; |
Willy Tarreau | 6d4890c | 2013-11-18 18:04:25 +0100 | [diff] [blame] | 8261 | char *errmsg = NULL; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8262 | |
Willy Tarreau | be4653b | 2015-05-28 15:26:58 +0200 | [diff] [blame] | 8263 | if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 0)) == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8264 | ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n", |
| 8265 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8266 | goto out_err; |
| 8267 | } |
| 8268 | |
| 8269 | /* this redirect rule might already contain a parsed condition which |
| 8270 | * we'll pass to the http-request rule. |
| 8271 | */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8272 | rule->action = ACT_HTTP_REDIR; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8273 | rule->arg.redir = redir; |
| 8274 | rule->cond = redir->cond; |
| 8275 | redir->cond = NULL; |
| 8276 | cur_arg = 2; |
| 8277 | return rule; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8278 | } else if (strncmp(args[0], "add-acl", 7) == 0) { |
| 8279 | /* http-request add-acl(<reference (acl name)>) <key pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8280 | rule->action = ACT_HTTP_ADD_ACL; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8281 | /* |
| 8282 | * '+ 8' for 'add-acl(' |
| 8283 | * '- 9' for 'add-acl(' + trailing ')' |
| 8284 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 8285 | rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8286 | |
| 8287 | cur_arg = 1; |
| 8288 | |
| 8289 | if (!*args[cur_arg] || |
| 8290 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8291 | ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 8292 | file, linenum, args[0]); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8293 | goto out_err; |
| 8294 | } |
| 8295 | |
| 8296 | LIST_INIT(&rule->arg.map.key); |
| 8297 | proxy->conf.args.ctx = ARGC_HRQ; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8298 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8299 | if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8300 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8301 | ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n", |
| 8302 | file, linenum, args[0], error); |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8303 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8304 | goto out_err; |
| 8305 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8306 | free(proxy->conf.lfs_file); |
| 8307 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 8308 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 8309 | cur_arg += 1; |
| 8310 | } else if (strncmp(args[0], "del-acl", 7) == 0) { |
| 8311 | /* http-request del-acl(<reference (acl name)>) <key pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8312 | rule->action = ACT_HTTP_DEL_ACL; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8313 | /* |
| 8314 | * '+ 8' for 'del-acl(' |
| 8315 | * '- 9' for 'del-acl(' + trailing ')' |
| 8316 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 8317 | rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8318 | |
| 8319 | cur_arg = 1; |
| 8320 | |
| 8321 | if (!*args[cur_arg] || |
| 8322 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8323 | ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 8324 | file, linenum, args[0]); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8325 | goto out_err; |
| 8326 | } |
| 8327 | |
| 8328 | LIST_INIT(&rule->arg.map.key); |
| 8329 | proxy->conf.args.ctx = ARGC_HRQ; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8330 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8331 | if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8332 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8333 | ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n", |
| 8334 | file, linenum, args[0], error); |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8335 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8336 | goto out_err; |
| 8337 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8338 | free(proxy->conf.lfs_file); |
| 8339 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 8340 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 8341 | cur_arg += 1; |
| 8342 | } else if (strncmp(args[0], "del-map", 7) == 0) { |
| 8343 | /* http-request del-map(<reference (map name)>) <key pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8344 | rule->action = ACT_HTTP_DEL_MAP; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8345 | /* |
| 8346 | * '+ 8' for 'del-map(' |
| 8347 | * '- 9' for 'del-map(' + trailing ')' |
| 8348 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 8349 | rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8350 | |
| 8351 | cur_arg = 1; |
| 8352 | |
| 8353 | if (!*args[cur_arg] || |
| 8354 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8355 | ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 8356 | file, linenum, args[0]); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8357 | goto out_err; |
| 8358 | } |
| 8359 | |
| 8360 | LIST_INIT(&rule->arg.map.key); |
| 8361 | proxy->conf.args.ctx = ARGC_HRQ; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8362 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8363 | if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8364 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8365 | ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n", |
| 8366 | file, linenum, args[0], error); |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8367 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8368 | goto out_err; |
| 8369 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8370 | free(proxy->conf.lfs_file); |
| 8371 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 8372 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 8373 | cur_arg += 1; |
| 8374 | } else if (strncmp(args[0], "set-map", 7) == 0) { |
| 8375 | /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8376 | rule->action = ACT_HTTP_SET_MAP; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8377 | /* |
| 8378 | * '+ 8' for 'set-map(' |
| 8379 | * '- 9' for 'set-map(' + trailing ')' |
| 8380 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 8381 | rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8382 | |
| 8383 | cur_arg = 1; |
| 8384 | |
| 8385 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 8386 | (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8387 | ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n", |
| 8388 | file, linenum, args[0]); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8389 | goto out_err; |
| 8390 | } |
| 8391 | |
| 8392 | LIST_INIT(&rule->arg.map.key); |
| 8393 | LIST_INIT(&rule->arg.map.value); |
| 8394 | proxy->conf.args.ctx = ARGC_HRQ; |
| 8395 | |
| 8396 | /* key pattern */ |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8397 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8398 | if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8399 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8400 | ha_alert("parsing [%s:%d]: 'http-request %s' key: %s.\n", |
| 8401 | file, linenum, args[0], error); |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8402 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8403 | goto out_err; |
| 8404 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8405 | |
| 8406 | /* value pattern */ |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8407 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8408 | if (!parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP, |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8409 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8410 | ha_alert("parsing [%s:%d]: 'http-request %s' pattern: %s.\n", |
| 8411 | file, linenum, args[0], error); |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8412 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8413 | goto out_err; |
| 8414 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8415 | free(proxy->conf.lfs_file); |
| 8416 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 8417 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 8418 | |
| 8419 | cur_arg += 2; |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 8420 | } else if (((custom = action_http_req_custom(args[0])) != NULL)) { |
| 8421 | char *errmsg = NULL; |
| 8422 | cur_arg = 1; |
| 8423 | /* try in the module list */ |
Thierry FOURNIER | 5563e4b | 2015-08-14 19:20:07 +0200 | [diff] [blame] | 8424 | rule->from = ACT_F_HTTP_REQ; |
Thierry FOURNIER | 85c6c97 | 2015-09-22 19:14:35 +0200 | [diff] [blame] | 8425 | rule->kw = custom; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 8426 | if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8427 | ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n", |
| 8428 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 8429 | free(errmsg); |
| 8430 | goto out_err; |
| 8431 | } |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8432 | } else { |
Thierry FOURNIER | ab95e65 | 2015-10-02 08:24:51 +0200 | [diff] [blame] | 8433 | action_build_list(&http_req_keywords.list, &trash); |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8434 | ha_alert("parsing [%s:%d]: 'http-request' expects 'allow', 'deny', 'auth', 'redirect', " |
| 8435 | "'tarpit', 'add-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', " |
| 8436 | "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'track-sc*'" |
| 8437 | "%s%s, but got '%s'%s.\n", |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8438 | file, linenum, *trash.area ? ", " : "", trash.area, |
| 8439 | args[0], *args[0] ? "" : " (missing argument)"); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8440 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8441 | } |
| 8442 | |
| 8443 | if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) { |
| 8444 | struct acl_cond *cond; |
Willy Tarreau | b7451bb | 2012-04-27 12:38:15 +0200 | [diff] [blame] | 8445 | char *errmsg = NULL; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8446 | |
Christopher Faulet | 1b421ea | 2017-09-22 14:38:56 +0200 | [diff] [blame] | 8447 | if ((cond = build_acl_cond(file, linenum, &proxy->acl, proxy, args+cur_arg, &errmsg)) == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8448 | ha_alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n", |
| 8449 | file, linenum, args[0], errmsg); |
Willy Tarreau | b7451bb | 2012-04-27 12:38:15 +0200 | [diff] [blame] | 8450 | free(errmsg); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8451 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8452 | } |
| 8453 | rule->cond = cond; |
| 8454 | } |
| 8455 | else if (*args[cur_arg]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8456 | ha_alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or" |
| 8457 | " either 'if' or 'unless' followed by a condition but found '%s'.\n", |
| 8458 | file, linenum, args[0], args[cur_arg]); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8459 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8460 | } |
| 8461 | |
| 8462 | return rule; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8463 | out_err: |
| 8464 | free(rule); |
| 8465 | return NULL; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8466 | } |
| 8467 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8468 | /* parse an "http-respose" rule */ |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 8469 | struct act_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy) |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8470 | { |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 8471 | struct act_rule *rule; |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 8472 | struct action_kw *custom = NULL; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8473 | int cur_arg; |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 8474 | char *error; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8475 | |
| 8476 | rule = calloc(1, sizeof(*rule)); |
| 8477 | if (!rule) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8478 | ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum); |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8479 | goto out_err; |
| 8480 | } |
| 8481 | |
| 8482 | if (!strcmp(args[0], "allow")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8483 | rule->action = ACT_ACTION_ALLOW; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8484 | cur_arg = 1; |
| 8485 | } else if (!strcmp(args[0], "deny")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8486 | rule->action = ACT_ACTION_DENY; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8487 | cur_arg = 1; |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 8488 | } else if (!strcmp(args[0], "set-nice")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8489 | rule->action = ACT_HTTP_SET_NICE; |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 8490 | cur_arg = 1; |
| 8491 | |
| 8492 | if (!*args[cur_arg] || |
| 8493 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8494 | ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n", |
| 8495 | file, linenum, args[0]); |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 8496 | goto out_err; |
| 8497 | } |
| 8498 | rule->arg.nice = atoi(args[cur_arg]); |
| 8499 | if (rule->arg.nice < -1024) |
| 8500 | rule->arg.nice = -1024; |
| 8501 | else if (rule->arg.nice > 1024) |
| 8502 | rule->arg.nice = 1024; |
| 8503 | cur_arg++; |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 8504 | } else if (!strcmp(args[0], "set-tos")) { |
| 8505 | #ifdef IP_TOS |
| 8506 | char *err; |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8507 | rule->action = ACT_HTTP_SET_TOS; |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 8508 | cur_arg = 1; |
| 8509 | |
| 8510 | if (!*args[cur_arg] || |
| 8511 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8512 | ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n", |
| 8513 | file, linenum, args[0]); |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 8514 | goto out_err; |
| 8515 | } |
| 8516 | |
| 8517 | rule->arg.tos = strtol(args[cur_arg], &err, 0); |
| 8518 | if (err && *err != '\0') { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8519 | ha_alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n", |
| 8520 | file, linenum, err, args[0]); |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 8521 | goto out_err; |
| 8522 | } |
| 8523 | cur_arg++; |
| 8524 | #else |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8525 | ha_alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]); |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 8526 | goto out_err; |
| 8527 | #endif |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 8528 | } else if (!strcmp(args[0], "set-mark")) { |
| 8529 | #ifdef SO_MARK |
| 8530 | char *err; |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8531 | rule->action = ACT_HTTP_SET_MARK; |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 8532 | cur_arg = 1; |
| 8533 | |
| 8534 | if (!*args[cur_arg] || |
| 8535 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8536 | ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n", |
| 8537 | file, linenum, args[0]); |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 8538 | goto out_err; |
| 8539 | } |
| 8540 | |
| 8541 | rule->arg.mark = strtoul(args[cur_arg], &err, 0); |
| 8542 | if (err && *err != '\0') { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8543 | ha_alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n", |
| 8544 | file, linenum, err, args[0]); |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 8545 | goto out_err; |
| 8546 | } |
| 8547 | cur_arg++; |
| 8548 | global.last_checks |= LSTCHK_NETADM; |
| 8549 | #else |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8550 | ha_alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]); |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 8551 | goto out_err; |
| 8552 | #endif |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 8553 | } else if (!strcmp(args[0], "set-log-level")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8554 | rule->action = ACT_HTTP_SET_LOGL; |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 8555 | cur_arg = 1; |
| 8556 | |
| 8557 | if (!*args[cur_arg] || |
| 8558 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 8559 | bad_log_level: |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8560 | ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n", |
| 8561 | file, linenum, args[0]); |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 8562 | goto out_err; |
| 8563 | } |
| 8564 | if (strcmp(args[cur_arg], "silent") == 0) |
| 8565 | rule->arg.loglevel = -1; |
Ruoshan Huang | dd01678 | 2016-06-15 22:16:03 +0800 | [diff] [blame] | 8566 | else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0) |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 8567 | goto bad_log_level; |
| 8568 | cur_arg++; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8569 | } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8570 | rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8571 | cur_arg = 1; |
| 8572 | |
| 8573 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 8574 | (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8575 | ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n", |
| 8576 | file, linenum, args[0]); |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8577 | goto out_err; |
| 8578 | } |
| 8579 | |
| 8580 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 8581 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 8582 | LIST_INIT(&rule->arg.hdr_add.fmt); |
| 8583 | |
Thierry FOURNIER | 1c0054f | 2013-11-20 15:09:52 +0100 | [diff] [blame] | 8584 | proxy->conf.args.ctx = ARGC_HRS; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8585 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8586 | if (!parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP, |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8587 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8588 | ha_alert("parsing [%s:%d]: 'http-response %s': %s.\n", |
| 8589 | file, linenum, args[0], error); |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8590 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8591 | goto out_err; |
| 8592 | } |
Willy Tarreau | 59ad1a2 | 2014-01-29 14:39:58 +0100 | [diff] [blame] | 8593 | free(proxy->conf.lfs_file); |
| 8594 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 8595 | proxy->conf.lfs_line = proxy->conf.args.line; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8596 | cur_arg += 2; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 8597 | } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8598 | rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 8599 | cur_arg = 1; |
| 8600 | |
| 8601 | if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] || |
Baptiste Assmann | 12cb00b | 2014-08-08 17:29:06 +0200 | [diff] [blame] | 8602 | (*args[cur_arg+3] && strcmp(args[cur_arg+3], "if") != 0 && strcmp(args[cur_arg+3], "unless") != 0)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8603 | ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n", |
| 8604 | file, linenum, args[0]); |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 8605 | goto out_err; |
| 8606 | } |
| 8607 | |
| 8608 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 8609 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 8610 | LIST_INIT(&rule->arg.hdr_add.fmt); |
| 8611 | |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 8612 | error = NULL; |
| 8613 | if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8614 | ha_alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum, |
| 8615 | args[cur_arg + 1], error); |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 8616 | free(error); |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 8617 | goto out_err; |
| 8618 | } |
| 8619 | |
| 8620 | proxy->conf.args.ctx = ARGC_HRQ; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8621 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8622 | if (!parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP, |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8623 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8624 | ha_alert("parsing [%s:%d]: 'http-response %s': %s.\n", |
| 8625 | file, linenum, args[0], error); |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8626 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8627 | goto out_err; |
| 8628 | } |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 8629 | |
| 8630 | free(proxy->conf.lfs_file); |
| 8631 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 8632 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 8633 | cur_arg += 3; |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 8634 | } else if (strcmp(args[0], "del-header") == 0) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8635 | rule->action = ACT_HTTP_DEL_HDR; |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 8636 | cur_arg = 1; |
| 8637 | |
| 8638 | if (!*args[cur_arg] || |
| 8639 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8640 | ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 8641 | file, linenum, args[0]); |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 8642 | goto out_err; |
| 8643 | } |
| 8644 | |
| 8645 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 8646 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 8647 | |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8648 | proxy->conf.args.ctx = ARGC_HRS; |
| 8649 | free(proxy->conf.lfs_file); |
| 8650 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 8651 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 8652 | cur_arg += 1; |
| 8653 | } else if (strncmp(args[0], "add-acl", 7) == 0) { |
| 8654 | /* http-request add-acl(<reference (acl name)>) <key pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8655 | rule->action = ACT_HTTP_ADD_ACL; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8656 | /* |
| 8657 | * '+ 8' for 'add-acl(' |
| 8658 | * '- 9' for 'add-acl(' + trailing ')' |
| 8659 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 8660 | rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8661 | |
| 8662 | cur_arg = 1; |
| 8663 | |
| 8664 | if (!*args[cur_arg] || |
| 8665 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8666 | ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 8667 | file, linenum, args[0]); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8668 | goto out_err; |
| 8669 | } |
| 8670 | |
| 8671 | LIST_INIT(&rule->arg.map.key); |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 8672 | proxy->conf.args.ctx = ARGC_HRS; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8673 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8674 | if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8675 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8676 | ha_alert("parsing [%s:%d]: 'http-response %s': %s.\n", |
| 8677 | file, linenum, args[0], error); |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8678 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8679 | goto out_err; |
| 8680 | } |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 8681 | free(proxy->conf.lfs_file); |
| 8682 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 8683 | proxy->conf.lfs_line = proxy->conf.args.line; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8684 | |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 8685 | cur_arg += 1; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8686 | } else if (strncmp(args[0], "del-acl", 7) == 0) { |
| 8687 | /* http-response del-acl(<reference (acl name)>) <key pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8688 | rule->action = ACT_HTTP_DEL_ACL; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8689 | /* |
| 8690 | * '+ 8' for 'del-acl(' |
| 8691 | * '- 9' for 'del-acl(' + trailing ')' |
| 8692 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 8693 | rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8694 | |
| 8695 | cur_arg = 1; |
| 8696 | |
| 8697 | if (!*args[cur_arg] || |
| 8698 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8699 | ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 8700 | file, linenum, args[0]); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8701 | goto out_err; |
| 8702 | } |
| 8703 | |
| 8704 | LIST_INIT(&rule->arg.map.key); |
| 8705 | proxy->conf.args.ctx = ARGC_HRS; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8706 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8707 | if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8708 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8709 | ha_alert("parsing [%s:%d]: 'http-response %s': %s.\n", |
| 8710 | file, linenum, args[0], error); |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8711 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8712 | goto out_err; |
| 8713 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8714 | free(proxy->conf.lfs_file); |
| 8715 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 8716 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 8717 | cur_arg += 1; |
| 8718 | } else if (strncmp(args[0], "del-map", 7) == 0) { |
| 8719 | /* http-response del-map(<reference (map name)>) <key pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8720 | rule->action = ACT_HTTP_DEL_MAP; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8721 | /* |
| 8722 | * '+ 8' for 'del-map(' |
| 8723 | * '- 9' for 'del-map(' + trailing ')' |
| 8724 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 8725 | rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8726 | |
| 8727 | cur_arg = 1; |
| 8728 | |
| 8729 | if (!*args[cur_arg] || |
| 8730 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8731 | ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 8732 | file, linenum, args[0]); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8733 | goto out_err; |
| 8734 | } |
| 8735 | |
| 8736 | LIST_INIT(&rule->arg.map.key); |
| 8737 | proxy->conf.args.ctx = ARGC_HRS; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8738 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8739 | if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8740 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8741 | ha_alert("parsing [%s:%d]: 'http-response %s' %s.\n", |
| 8742 | file, linenum, args[0], error); |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8743 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8744 | goto out_err; |
| 8745 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8746 | free(proxy->conf.lfs_file); |
| 8747 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 8748 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 8749 | cur_arg += 1; |
| 8750 | } else if (strncmp(args[0], "set-map", 7) == 0) { |
| 8751 | /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8752 | rule->action = ACT_HTTP_SET_MAP; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8753 | /* |
| 8754 | * '+ 8' for 'set-map(' |
| 8755 | * '- 9' for 'set-map(' + trailing ')' |
| 8756 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 8757 | rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8758 | |
| 8759 | cur_arg = 1; |
| 8760 | |
| 8761 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 8762 | (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8763 | ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n", |
| 8764 | file, linenum, args[0]); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8765 | goto out_err; |
| 8766 | } |
| 8767 | |
| 8768 | LIST_INIT(&rule->arg.map.key); |
| 8769 | LIST_INIT(&rule->arg.map.value); |
| 8770 | |
| 8771 | proxy->conf.args.ctx = ARGC_HRS; |
| 8772 | |
| 8773 | /* key pattern */ |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8774 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8775 | if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8776 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8777 | ha_alert("parsing [%s:%d]: 'http-response %s' name: %s.\n", |
| 8778 | file, linenum, args[0], error); |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8779 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8780 | goto out_err; |
| 8781 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8782 | |
| 8783 | /* value pattern */ |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8784 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8785 | if (!parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP, |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8786 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8787 | ha_alert("parsing [%s:%d]: 'http-response %s' value: %s.\n", |
| 8788 | file, linenum, args[0], error); |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8789 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8790 | goto out_err; |
| 8791 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8792 | |
| 8793 | free(proxy->conf.lfs_file); |
| 8794 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 8795 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 8796 | |
| 8797 | cur_arg += 2; |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 8798 | } else if (strcmp(args[0], "redirect") == 0) { |
| 8799 | struct redirect_rule *redir; |
| 8800 | char *errmsg = NULL; |
| 8801 | |
| 8802 | if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 1)) == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8803 | ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n", |
| 8804 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 8805 | goto out_err; |
| 8806 | } |
| 8807 | |
| 8808 | /* this redirect rule might already contain a parsed condition which |
| 8809 | * we'll pass to the http-request rule. |
| 8810 | */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8811 | rule->action = ACT_HTTP_REDIR; |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 8812 | rule->arg.redir = redir; |
| 8813 | rule->cond = redir->cond; |
| 8814 | redir->cond = NULL; |
| 8815 | cur_arg = 2; |
| 8816 | return rule; |
Frédéric Lécaille | a41d531 | 2018-01-29 12:05:07 +0100 | [diff] [blame] | 8817 | } else if (strncmp(args[0], "track-sc", 8) == 0) { |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 8818 | struct sample_expr *expr; |
| 8819 | unsigned int where; |
| 8820 | char *err = NULL; |
Frédéric Lécaille | a41d531 | 2018-01-29 12:05:07 +0100 | [diff] [blame] | 8821 | unsigned int tsc_num; |
| 8822 | const char *tsc_num_str; |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 8823 | |
| 8824 | cur_arg = 1; |
| 8825 | proxy->conf.args.ctx = ARGC_TRK; |
| 8826 | |
Frédéric Lécaille | a41d531 | 2018-01-29 12:05:07 +0100 | [diff] [blame] | 8827 | tsc_num_str = &args[0][8]; |
| 8828 | if (cfg_parse_track_sc_num(&tsc_num, tsc_num_str, tsc_num_str + strlen(tsc_num_str), &err) == -1) { |
| 8829 | ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n", |
| 8830 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], err); |
| 8831 | free(err); |
| 8832 | goto out_err; |
| 8833 | } |
| 8834 | |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 8835 | expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args); |
| 8836 | if (!expr) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8837 | ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n", |
| 8838 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], err); |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 8839 | free(err); |
| 8840 | goto out_err; |
| 8841 | } |
| 8842 | |
| 8843 | where = 0; |
| 8844 | if (proxy->cap & PR_CAP_FE) |
| 8845 | where |= SMP_VAL_FE_HRS_HDR; |
| 8846 | if (proxy->cap & PR_CAP_BE) |
| 8847 | where |= SMP_VAL_BE_HRS_HDR; |
| 8848 | |
| 8849 | if (!(expr->fetch->val & where)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8850 | ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule :" |
| 8851 | " fetch method '%s' extracts information from '%s', none of which is available here.\n", |
| 8852 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], |
| 8853 | args[cur_arg-1], sample_src_names(expr->fetch->use)); |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 8854 | free(expr); |
| 8855 | goto out_err; |
| 8856 | } |
| 8857 | |
| 8858 | if (strcmp(args[cur_arg], "table") == 0) { |
| 8859 | cur_arg++; |
| 8860 | if (!args[cur_arg]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8861 | ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : missing table name.\n", |
| 8862 | file, linenum, proxy_type_str(proxy), proxy->id, args[0]); |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 8863 | free(expr); |
| 8864 | goto out_err; |
| 8865 | } |
| 8866 | /* we copy the table name for now, it will be resolved later */ |
| 8867 | rule->arg.trk_ctr.table.n = strdup(args[cur_arg]); |
| 8868 | cur_arg++; |
| 8869 | } |
| 8870 | rule->arg.trk_ctr.expr = expr; |
Frédéric Lécaille | a41d531 | 2018-01-29 12:05:07 +0100 | [diff] [blame] | 8871 | rule->action = ACT_ACTION_TRK_SC0 + tsc_num; |
Christopher Faulet | 78880fb | 2017-09-18 14:43:55 +0200 | [diff] [blame] | 8872 | rule->check_ptr = check_trk_action; |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 8873 | } else if (((custom = action_http_res_custom(args[0])) != NULL)) { |
| 8874 | char *errmsg = NULL; |
| 8875 | cur_arg = 1; |
| 8876 | /* try in the module list */ |
Thierry FOURNIER | 5563e4b | 2015-08-14 19:20:07 +0200 | [diff] [blame] | 8877 | rule->from = ACT_F_HTTP_RES; |
Thierry FOURNIER | 85c6c97 | 2015-09-22 19:14:35 +0200 | [diff] [blame] | 8878 | rule->kw = custom; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 8879 | if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8880 | ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n", |
| 8881 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 8882 | free(errmsg); |
| 8883 | goto out_err; |
| 8884 | } |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8885 | } else { |
Thierry FOURNIER | ab95e65 | 2015-10-02 08:24:51 +0200 | [diff] [blame] | 8886 | action_build_list(&http_res_keywords.list, &trash); |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8887 | ha_alert("parsing [%s:%d]: 'http-response' expects 'allow', 'deny', 'redirect', " |
| 8888 | "'add-header', 'del-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', " |
| 8889 | "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'track-sc*'" |
| 8890 | "%s%s, but got '%s'%s.\n", |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8891 | file, linenum, *trash.area ? ", " : "", trash.area, |
| 8892 | args[0], *args[0] ? "" : " (missing argument)"); |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8893 | goto out_err; |
| 8894 | } |
| 8895 | |
| 8896 | if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) { |
| 8897 | struct acl_cond *cond; |
| 8898 | char *errmsg = NULL; |
| 8899 | |
Christopher Faulet | 1b421ea | 2017-09-22 14:38:56 +0200 | [diff] [blame] | 8900 | if ((cond = build_acl_cond(file, linenum, &proxy->acl, proxy, args+cur_arg, &errmsg)) == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8901 | ha_alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n", |
| 8902 | file, linenum, args[0], errmsg); |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8903 | free(errmsg); |
| 8904 | goto out_err; |
| 8905 | } |
| 8906 | rule->cond = cond; |
| 8907 | } |
| 8908 | else if (*args[cur_arg]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8909 | ha_alert("parsing [%s:%d]: 'http-response %s' expects" |
| 8910 | " either 'if' or 'unless' followed by a condition but found '%s'.\n", |
| 8911 | file, linenum, args[0], args[cur_arg]); |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8912 | goto out_err; |
| 8913 | } |
| 8914 | |
| 8915 | return rule; |
| 8916 | out_err: |
| 8917 | free(rule); |
| 8918 | return NULL; |
| 8919 | } |
| 8920 | |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 8921 | /* Parses a redirect rule. Returns the redirect rule on success or NULL on error, |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 8922 | * with <err> filled with the error message. If <use_fmt> is not null, builds a |
Willy Tarreau | be4653b | 2015-05-28 15:26:58 +0200 | [diff] [blame] | 8923 | * dynamic log-format rule instead of a static string. Parameter <dir> indicates |
| 8924 | * the direction of the rule, and equals 0 for request, non-zero for responses. |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 8925 | */ |
| 8926 | struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy, |
Willy Tarreau | be4653b | 2015-05-28 15:26:58 +0200 | [diff] [blame] | 8927 | const char **args, char **errmsg, int use_fmt, int dir) |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 8928 | { |
| 8929 | struct redirect_rule *rule; |
| 8930 | int cur_arg; |
| 8931 | int type = REDIRECT_TYPE_NONE; |
| 8932 | int code = 302; |
| 8933 | const char *destination = NULL; |
| 8934 | const char *cookie = NULL; |
| 8935 | int cookie_set = 0; |
| 8936 | unsigned int flags = REDIRECT_FLAG_NONE; |
| 8937 | struct acl_cond *cond = NULL; |
| 8938 | |
| 8939 | cur_arg = 0; |
| 8940 | while (*(args[cur_arg])) { |
| 8941 | if (strcmp(args[cur_arg], "location") == 0) { |
| 8942 | if (!*args[cur_arg + 1]) |
| 8943 | goto missing_arg; |
| 8944 | |
| 8945 | type = REDIRECT_TYPE_LOCATION; |
| 8946 | cur_arg++; |
| 8947 | destination = args[cur_arg]; |
| 8948 | } |
| 8949 | else if (strcmp(args[cur_arg], "prefix") == 0) { |
| 8950 | if (!*args[cur_arg + 1]) |
| 8951 | goto missing_arg; |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 8952 | type = REDIRECT_TYPE_PREFIX; |
| 8953 | cur_arg++; |
| 8954 | destination = args[cur_arg]; |
| 8955 | } |
| 8956 | else if (strcmp(args[cur_arg], "scheme") == 0) { |
| 8957 | if (!*args[cur_arg + 1]) |
| 8958 | goto missing_arg; |
| 8959 | |
| 8960 | type = REDIRECT_TYPE_SCHEME; |
| 8961 | cur_arg++; |
| 8962 | destination = args[cur_arg]; |
| 8963 | } |
| 8964 | else if (strcmp(args[cur_arg], "set-cookie") == 0) { |
| 8965 | if (!*args[cur_arg + 1]) |
| 8966 | goto missing_arg; |
| 8967 | |
| 8968 | cur_arg++; |
| 8969 | cookie = args[cur_arg]; |
| 8970 | cookie_set = 1; |
| 8971 | } |
| 8972 | else if (strcmp(args[cur_arg], "clear-cookie") == 0) { |
| 8973 | if (!*args[cur_arg + 1]) |
| 8974 | goto missing_arg; |
| 8975 | |
| 8976 | cur_arg++; |
| 8977 | cookie = args[cur_arg]; |
| 8978 | cookie_set = 0; |
| 8979 | } |
| 8980 | else if (strcmp(args[cur_arg], "code") == 0) { |
| 8981 | if (!*args[cur_arg + 1]) |
| 8982 | goto missing_arg; |
| 8983 | |
| 8984 | cur_arg++; |
| 8985 | code = atol(args[cur_arg]); |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 8986 | if (code < 301 || code > 308 || (code > 303 && code < 307)) { |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 8987 | memprintf(errmsg, |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 8988 | "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)", |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 8989 | args[cur_arg - 1], args[cur_arg]); |
| 8990 | return NULL; |
| 8991 | } |
| 8992 | } |
| 8993 | else if (!strcmp(args[cur_arg],"drop-query")) { |
| 8994 | flags |= REDIRECT_FLAG_DROP_QS; |
| 8995 | } |
| 8996 | else if (!strcmp(args[cur_arg],"append-slash")) { |
| 8997 | flags |= REDIRECT_FLAG_APPEND_SLASH; |
| 8998 | } |
| 8999 | else if (strcmp(args[cur_arg], "if") == 0 || |
| 9000 | strcmp(args[cur_arg], "unless") == 0) { |
Olivier Houchard | fbc74e8 | 2017-11-24 16:54:05 +0100 | [diff] [blame] | 9001 | cond = build_acl_cond(file, linenum, &curproxy->acl, curproxy, (const char **)args + cur_arg, errmsg); |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9002 | if (!cond) { |
| 9003 | memprintf(errmsg, "error in condition: %s", *errmsg); |
| 9004 | return NULL; |
| 9005 | } |
| 9006 | break; |
| 9007 | } |
| 9008 | else { |
| 9009 | memprintf(errmsg, |
| 9010 | "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')", |
| 9011 | args[cur_arg]); |
| 9012 | return NULL; |
| 9013 | } |
| 9014 | cur_arg++; |
| 9015 | } |
| 9016 | |
| 9017 | if (type == REDIRECT_TYPE_NONE) { |
| 9018 | memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')"); |
| 9019 | return NULL; |
| 9020 | } |
| 9021 | |
Willy Tarreau | be4653b | 2015-05-28 15:26:58 +0200 | [diff] [blame] | 9022 | if (dir && type != REDIRECT_TYPE_LOCATION) { |
| 9023 | memprintf(errmsg, "response only supports redirect type 'location'"); |
| 9024 | return NULL; |
| 9025 | } |
| 9026 | |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 9027 | rule = calloc(1, sizeof(*rule)); |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9028 | rule->cond = cond; |
Willy Tarreau | 60e0838 | 2013-12-03 00:48:45 +0100 | [diff] [blame] | 9029 | LIST_INIT(&rule->rdr_fmt); |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 9030 | |
| 9031 | if (!use_fmt) { |
| 9032 | /* old-style static redirect rule */ |
| 9033 | rule->rdr_str = strdup(destination); |
| 9034 | rule->rdr_len = strlen(destination); |
| 9035 | } |
| 9036 | else { |
| 9037 | /* log-format based redirect rule */ |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 9038 | |
| 9039 | /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case, |
| 9040 | * if prefix == "/", we don't want to add anything, otherwise it |
| 9041 | * makes it hard for the user to configure a self-redirection. |
| 9042 | */ |
Godbach | d972203 | 2014-12-18 15:44:58 +0800 | [diff] [blame] | 9043 | curproxy->conf.args.ctx = ARGC_RDR; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 9044 | if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) { |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9045 | if (!parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP, |
| 9046 | dir ? (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRS_HDR : SMP_VAL_BE_HRS_HDR |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9047 | : (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9048 | errmsg)) { |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9049 | return NULL; |
| 9050 | } |
Willy Tarreau | 59ad1a2 | 2014-01-29 14:39:58 +0100 | [diff] [blame] | 9051 | free(curproxy->conf.lfs_file); |
| 9052 | curproxy->conf.lfs_file = strdup(curproxy->conf.args.file); |
| 9053 | curproxy->conf.lfs_line = curproxy->conf.args.line; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 9054 | } |
| 9055 | } |
| 9056 | |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9057 | if (cookie) { |
| 9058 | /* depending on cookie_set, either we want to set the cookie, or to clear it. |
| 9059 | * a clear consists in appending "; path=/; Max-Age=0;" at the end. |
| 9060 | */ |
| 9061 | rule->cookie_len = strlen(cookie); |
| 9062 | if (cookie_set) { |
| 9063 | rule->cookie_str = malloc(rule->cookie_len + 10); |
| 9064 | memcpy(rule->cookie_str, cookie, rule->cookie_len); |
| 9065 | memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10); |
| 9066 | rule->cookie_len += 9; |
| 9067 | } else { |
| 9068 | rule->cookie_str = malloc(rule->cookie_len + 21); |
| 9069 | memcpy(rule->cookie_str, cookie, rule->cookie_len); |
| 9070 | memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21); |
| 9071 | rule->cookie_len += 20; |
| 9072 | } |
| 9073 | } |
| 9074 | rule->type = type; |
| 9075 | rule->code = code; |
| 9076 | rule->flags = flags; |
| 9077 | LIST_INIT(&rule->list); |
| 9078 | return rule; |
| 9079 | |
| 9080 | missing_arg: |
| 9081 | memprintf(errmsg, "missing argument for '%s'", args[cur_arg]); |
| 9082 | return NULL; |
| 9083 | } |
| 9084 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9085 | /************************************************************************/ |
| 9086 | /* The code below is dedicated to ACL parsing and matching */ |
| 9087 | /************************************************************************/ |
| 9088 | |
| 9089 | |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9090 | /* This function ensures that the prerequisites for an L7 fetch are ready, |
| 9091 | * which means that a request or response is ready. If some data is missing, |
| 9092 | * a parsing attempt is made. This is useful in TCP-based ACLs which are able |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 9093 | * to extract data from L7. If <req_vol> is non-null during a request prefetch, |
| 9094 | * another test is made to ensure the required information is not gone. |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9095 | * |
| 9096 | * The function returns : |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9097 | * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to |
| 9098 | * decide whether or not an HTTP message is present ; |
| 9099 | * 0 if the requested data cannot be fetched or if it is certain that |
| 9100 | * we'll never have any HTTP message there ; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9101 | * 1 if an HTTP message is ready |
| 9102 | */ |
James Rosewell | 91a41cb | 2015-09-18 17:11:16 +0100 | [diff] [blame] | 9103 | int smp_prefetch_http(struct proxy *px, struct stream *s, unsigned int opt, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 9104 | const struct arg *args, struct sample *smp, int req_vol) |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9105 | { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 9106 | struct http_txn *txn; |
| 9107 | struct http_msg *msg; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9108 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 9109 | /* Note: it is possible that <s> is NULL when called before stream |
| 9110 | * initialization (eg: tcp-request connection), so this function is the |
| 9111 | * one responsible for guarding against this case for all HTTP users. |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9112 | */ |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 9113 | if (!s) |
| 9114 | return 0; |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 9115 | |
Thierry FOURNIER | ed08d6a | 2015-09-24 08:40:18 +0200 | [diff] [blame] | 9116 | if (!s->txn) { |
| 9117 | if (unlikely(!http_alloc_txn(s))) |
| 9118 | return 0; /* not enough memory */ |
| 9119 | http_init_txn(s); |
| 9120 | } |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 9121 | txn = s->txn; |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 9122 | msg = &txn->req; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9123 | |
| 9124 | /* Check for a dependency on a request */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9125 | smp->data.type = SMP_T_BOOL; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9126 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 9127 | if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 9128 | /* If the buffer does not leave enough free space at the end, |
| 9129 | * we must first realign it. |
| 9130 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 9131 | if (ci_head(&s->req) > b_orig(&s->req.buf) && |
| 9132 | ci_head(&s->req) + ci_data(&s->req) > b_wrap(&s->req.buf) - global.tune.maxrewrite) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 9133 | channel_slow_realign(&s->req, trash.area); |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 9134 | |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9135 | if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 472b1ee | 2013-10-14 22:41:30 +0200 | [diff] [blame] | 9136 | if (msg->msg_state == HTTP_MSG_ERROR) |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9137 | return 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9138 | |
| 9139 | /* Try to decode HTTP request */ |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 9140 | if (likely(msg->next < ci_data(&s->req))) |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9141 | http_msg_analyzer(msg, &txn->hdr_idx); |
| 9142 | |
| 9143 | /* Still no valid request ? */ |
| 9144 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 3bf1b2b | 2012-08-27 20:46:07 +0200 | [diff] [blame] | 9145 | if ((msg->msg_state == HTTP_MSG_ERROR) || |
Willy Tarreau | 2375233 | 2018-06-15 14:54:53 +0200 | [diff] [blame] | 9146 | channel_full(&s->req, global.tune.maxrewrite)) { |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9147 | return 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9148 | } |
| 9149 | /* wait for final state */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9150 | smp->flags |= SMP_F_MAY_CHANGE; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9151 | return 0; |
| 9152 | } |
| 9153 | |
| 9154 | /* OK we just got a valid HTTP request. We have some minor |
| 9155 | * preparation to perform so that further checks can rely |
| 9156 | * on HTTP tests. |
| 9157 | */ |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 9158 | |
| 9159 | /* If the request was parsed but was too large, we must absolutely |
| 9160 | * return an error so that it is not processed. At the moment this |
| 9161 | * cannot happen, but if the parsers are to change in the future, |
| 9162 | * we want this check to be maintained. |
| 9163 | */ |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 9164 | if (unlikely(ci_head(&s->req) + ci_data(&s->req) > |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 9165 | b_wrap(&s->req.buf) - global.tune.maxrewrite)) { |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 9166 | msg->err_state = msg->msg_state; |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 9167 | msg->msg_state = HTTP_MSG_ERROR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9168 | smp->data.u.sint = 1; |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 9169 | return 1; |
| 9170 | } |
| 9171 | |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 9172 | txn->meth = find_http_meth(ci_head(msg->chn), msg->sl.rq.m_l); |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9173 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 9174 | s->flags |= SF_REDIRECTABLE; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9175 | |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9176 | if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn)) |
| 9177 | return 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9178 | } |
| 9179 | |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9180 | if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) { |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9181 | return 0; /* data might have moved and indexes changed */ |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9182 | } |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9183 | |
| 9184 | /* otherwise everything's ready for the request */ |
| 9185 | } |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 9186 | else { |
| 9187 | /* Check for a dependency on a response */ |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9188 | if (txn->rsp.msg_state < HTTP_MSG_BODY) { |
| 9189 | smp->flags |= SMP_F_MAY_CHANGE; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9190 | return 0; |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9191 | } |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9192 | } |
| 9193 | |
| 9194 | /* everything's OK */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9195 | smp->data.u.sint = 1; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9196 | return 1; |
| 9197 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9198 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9199 | /* 1. Check on METHOD |
| 9200 | * We use the pre-parsed method if it is known, and store its number as an |
| 9201 | * integer. If it is unknown, we use the pointer and the length. |
| 9202 | */ |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 9203 | static int pat_parse_meth(const char *text, struct pattern *pattern, int mflags, char **err) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9204 | { |
| 9205 | int len, meth; |
| 9206 | |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 9207 | len = strlen(text); |
| 9208 | meth = find_http_meth(text, len); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9209 | |
| 9210 | pattern->val.i = meth; |
| 9211 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | 912c119 | 2014-08-29 15:15:50 +0200 | [diff] [blame] | 9212 | pattern->ptr.str = (char *)text; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9213 | pattern->len = len; |
| 9214 | } |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 9215 | else { |
| 9216 | pattern->ptr.str = NULL; |
| 9217 | pattern->len = 0; |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 9218 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9219 | return 1; |
| 9220 | } |
| 9221 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 9222 | /* This function fetches the method of current HTTP request and stores |
| 9223 | * it in the global pattern struct as a chunk. There are two possibilities : |
| 9224 | * - if the method is known (not HTTP_METH_OTHER), its identifier is stored |
| 9225 | * in <len> and <ptr> is NULL ; |
| 9226 | * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and |
| 9227 | * <len> to its length. |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 9228 | * This is intended to be used with pat_match_meth() only. |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 9229 | */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 9230 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9231 | smp_fetch_meth(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9232 | { |
| 9233 | int meth; |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 9234 | struct http_txn *txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9235 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 9236 | CHECK_HTTP_MESSAGE_FIRST_PERM(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9237 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 9238 | txn = smp->strm->txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9239 | meth = txn->meth; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9240 | smp->data.type = SMP_T_METH; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9241 | smp->data.u.meth.meth = meth; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9242 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9243 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 9244 | /* ensure the indexes are not affected */ |
| 9245 | return 0; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9246 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 9247 | smp->data.u.meth.str.data = txn->req.sl.rq.m_l; |
| 9248 | smp->data.u.meth.str.area = ci_head(txn->req.chn); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9249 | } |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9250 | smp->flags |= SMP_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9251 | return 1; |
| 9252 | } |
| 9253 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 9254 | /* See above how the method is stored in the global pattern */ |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 9255 | static struct pattern *pat_match_meth(struct sample *smp, struct pattern_expr *expr, int fill) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9256 | { |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 9257 | int icase; |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 9258 | struct pattern_list *lst; |
| 9259 | struct pattern *pattern; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 9260 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 9261 | list_for_each_entry(lst, &expr->patterns, list) { |
| 9262 | pattern = &lst->pat; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9263 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 9264 | /* well-known method */ |
| 9265 | if (pattern->val.i != HTTP_METH_OTHER) { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9266 | if (smp->data.u.meth.meth == pattern->val.i) |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 9267 | return pattern; |
| 9268 | else |
| 9269 | continue; |
| 9270 | } |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 9271 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 9272 | /* Other method, we must compare the strings */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 9273 | if (pattern->len != smp->data.u.meth.str.data) |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 9274 | continue; |
| 9275 | |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 9276 | icase = expr->mflags & PAT_MF_IGNORE_CASE; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 9277 | if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.meth.str.area, smp->data.u.meth.str.data) == 0) || |
| 9278 | (!icase && strncmp(pattern->ptr.str, smp->data.u.meth.str.area, smp->data.u.meth.str.data) == 0)) |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 9279 | return pattern; |
| 9280 | } |
| 9281 | return NULL; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9282 | } |
| 9283 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 9284 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9285 | smp_fetch_rqver(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9286 | { |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 9287 | struct http_txn *txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9288 | char *ptr; |
| 9289 | int len; |
| 9290 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9291 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9292 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 9293 | txn = smp->strm->txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9294 | len = txn->req.sl.rq.v_l; |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 9295 | ptr = ci_head(txn->req.chn) + txn->req.sl.rq.v; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9296 | |
| 9297 | while ((len-- > 0) && (*ptr++ != '/')); |
| 9298 | if (len <= 0) |
| 9299 | return 0; |
| 9300 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9301 | smp->data.type = SMP_T_STR; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 9302 | smp->data.u.str.area = ptr; |
| 9303 | smp->data.u.str.data = len; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9304 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9305 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9306 | return 1; |
| 9307 | } |
| 9308 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 9309 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9310 | smp_fetch_stver(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9311 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 9312 | struct http_txn *txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9313 | char *ptr; |
| 9314 | int len; |
| 9315 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9316 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9317 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 9318 | txn = smp->strm->txn; |
Willy Tarreau | f26b252 | 2012-12-14 08:33:14 +0100 | [diff] [blame] | 9319 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
| 9320 | return 0; |
| 9321 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9322 | len = txn->rsp.sl.st.v_l; |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 9323 | ptr = ci_head(txn->rsp.chn); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9324 | |
| 9325 | while ((len-- > 0) && (*ptr++ != '/')); |
| 9326 | if (len <= 0) |
| 9327 | return 0; |
| 9328 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9329 | smp->data.type = SMP_T_STR; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 9330 | smp->data.u.str.area = ptr; |
| 9331 | smp->data.u.str.data = len; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9332 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9333 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9334 | return 1; |
| 9335 | } |
| 9336 | |
| 9337 | /* 3. Check on Status Code. We manipulate integers here. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 9338 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9339 | smp_fetch_stcode(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9340 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 9341 | struct http_txn *txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9342 | char *ptr; |
| 9343 | int len; |
| 9344 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9345 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9346 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 9347 | txn = smp->strm->txn; |
Willy Tarreau | f26b252 | 2012-12-14 08:33:14 +0100 | [diff] [blame] | 9348 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
| 9349 | return 0; |
| 9350 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9351 | len = txn->rsp.sl.st.c_l; |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 9352 | ptr = ci_head(txn->rsp.chn) + txn->rsp.sl.st.c; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9353 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9354 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9355 | smp->data.u.sint = __strl2ui(ptr, len); |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9356 | smp->flags = SMP_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9357 | return 1; |
| 9358 | } |
| 9359 | |
Thierry Fournier | f4011dd | 2016-03-29 17:23:51 +0200 | [diff] [blame] | 9360 | static int |
| 9361 | smp_fetch_uniqueid(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 9362 | { |
| 9363 | if (LIST_ISEMPTY(&smp->sess->fe->format_unique_id)) |
| 9364 | return 0; |
| 9365 | |
| 9366 | if (!smp->strm->unique_id) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 9367 | if ((smp->strm->unique_id = pool_alloc(pool_head_uniqueid)) == NULL) |
Thierry Fournier | f4011dd | 2016-03-29 17:23:51 +0200 | [diff] [blame] | 9368 | return 0; |
| 9369 | smp->strm->unique_id[0] = '\0'; |
| 9370 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 9371 | smp->data.u.str.data = build_logline(smp->strm, smp->strm->unique_id, |
Thierry Fournier | f4011dd | 2016-03-29 17:23:51 +0200 | [diff] [blame] | 9372 | UNIQUEID_LEN, &smp->sess->fe->format_unique_id); |
| 9373 | |
| 9374 | smp->data.type = SMP_T_STR; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 9375 | smp->data.u.str.area = smp->strm->unique_id; |
Thierry Fournier | f4011dd | 2016-03-29 17:23:51 +0200 | [diff] [blame] | 9376 | smp->flags = SMP_F_CONST; |
| 9377 | return 1; |
| 9378 | } |
| 9379 | |
Thierry FOURNIER | d7d8881 | 2017-04-19 15:15:14 +0200 | [diff] [blame] | 9380 | /* Returns a string block containing all headers including the |
| 9381 | * empty line wich separes headers from the body. This is useful |
| 9382 | * form some headers analysis. |
| 9383 | */ |
| 9384 | static int |
| 9385 | smp_fetch_hdrs(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 9386 | { |
| 9387 | struct http_msg *msg; |
| 9388 | struct hdr_idx *idx; |
| 9389 | struct http_txn *txn; |
| 9390 | |
| 9391 | CHECK_HTTP_MESSAGE_FIRST(); |
| 9392 | |
| 9393 | txn = smp->strm->txn; |
| 9394 | idx = &txn->hdr_idx; |
| 9395 | msg = &txn->req; |
| 9396 | |
| 9397 | smp->data.type = SMP_T_STR; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 9398 | smp->data.u.str.area = ci_head(msg->chn) + hdr_idx_first_pos(idx); |
| 9399 | smp->data.u.str.data = msg->eoh - hdr_idx_first_pos(idx) + 1 + |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 9400 | (ci_head(msg->chn)[msg->eoh] == '\r'); |
Thierry FOURNIER | d7d8881 | 2017-04-19 15:15:14 +0200 | [diff] [blame] | 9401 | |
| 9402 | return 1; |
| 9403 | } |
| 9404 | |
Thierry FOURNIER | 5617dce | 2017-04-09 05:38:19 +0200 | [diff] [blame] | 9405 | /* Returns the header request in a length/value encoded format. |
| 9406 | * This is useful for exchanges with the SPOE. |
| 9407 | * |
| 9408 | * A "length value" is a multibyte code encoding numbers. It uses the |
| 9409 | * SPOE format. The encoding is the following: |
| 9410 | * |
| 9411 | * Each couple "header name" / "header value" is composed |
| 9412 | * like this: |
| 9413 | * "length value" "header name bytes" |
| 9414 | * "length value" "header value bytes" |
| 9415 | * When the last header is reached, the header name and the header |
| 9416 | * value are empty. Their length are 0 |
| 9417 | */ |
| 9418 | static int |
| 9419 | smp_fetch_hdrs_bin(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 9420 | { |
| 9421 | struct http_msg *msg; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 9422 | struct buffer *temp; |
Thierry FOURNIER | 5617dce | 2017-04-09 05:38:19 +0200 | [diff] [blame] | 9423 | struct hdr_idx *idx; |
| 9424 | const char *cur_ptr, *cur_next, *p; |
| 9425 | int old_idx, cur_idx; |
| 9426 | struct hdr_idx_elem *cur_hdr; |
| 9427 | const char *hn, *hv; |
| 9428 | int hnl, hvl; |
| 9429 | int ret; |
| 9430 | struct http_txn *txn; |
| 9431 | char *buf; |
| 9432 | char *end; |
| 9433 | |
| 9434 | CHECK_HTTP_MESSAGE_FIRST(); |
| 9435 | |
| 9436 | temp = get_trash_chunk(); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 9437 | buf = temp->area; |
| 9438 | end = temp->area + temp->size; |
Thierry FOURNIER | 5617dce | 2017-04-09 05:38:19 +0200 | [diff] [blame] | 9439 | |
| 9440 | txn = smp->strm->txn; |
| 9441 | idx = &txn->hdr_idx; |
| 9442 | msg = &txn->req; |
| 9443 | |
| 9444 | /* Build array of headers. */ |
| 9445 | old_idx = 0; |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 9446 | cur_next = ci_head(msg->chn) + hdr_idx_first_pos(idx); |
Thierry FOURNIER | 5617dce | 2017-04-09 05:38:19 +0200 | [diff] [blame] | 9447 | while (1) { |
| 9448 | cur_idx = idx->v[old_idx].next; |
| 9449 | if (!cur_idx) |
| 9450 | break; |
| 9451 | old_idx = cur_idx; |
| 9452 | |
| 9453 | cur_hdr = &idx->v[cur_idx]; |
| 9454 | cur_ptr = cur_next; |
| 9455 | cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1; |
| 9456 | |
| 9457 | /* Now we have one full header at cur_ptr of len cur_hdr->len, |
| 9458 | * and the next header starts at cur_next. We'll check |
| 9459 | * this header in the list as well as against the default |
| 9460 | * rule. |
| 9461 | */ |
| 9462 | |
| 9463 | /* look for ': *'. */ |
| 9464 | hn = cur_ptr; |
| 9465 | for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++); |
| 9466 | if (p >= cur_ptr+cur_hdr->len) |
| 9467 | continue; |
| 9468 | hnl = p - hn; |
| 9469 | p++; |
| 9470 | while (p < cur_ptr + cur_hdr->len && (*p == ' ' || *p == '\t')) |
| 9471 | p++; |
| 9472 | if (p >= cur_ptr + cur_hdr->len) |
| 9473 | continue; |
| 9474 | hv = p; |
| 9475 | hvl = cur_ptr + cur_hdr->len-p; |
| 9476 | |
| 9477 | /* encode the header name. */ |
| 9478 | ret = encode_varint(hnl, &buf, end); |
| 9479 | if (ret == -1) |
| 9480 | return 0; |
| 9481 | if (buf + hnl > end) |
| 9482 | return 0; |
| 9483 | memcpy(buf, hn, hnl); |
| 9484 | buf += hnl; |
| 9485 | |
| 9486 | /* encode and copy the value. */ |
| 9487 | ret = encode_varint(hvl, &buf, end); |
| 9488 | if (ret == -1) |
| 9489 | return 0; |
| 9490 | if (buf + hvl > end) |
| 9491 | return 0; |
| 9492 | memcpy(buf, hv, hvl); |
| 9493 | buf += hvl; |
| 9494 | } |
| 9495 | |
| 9496 | /* encode the end of the header list with empty |
| 9497 | * header name and header value. |
| 9498 | */ |
| 9499 | ret = encode_varint(0, &buf, end); |
| 9500 | if (ret == -1) |
| 9501 | return 0; |
| 9502 | ret = encode_varint(0, &buf, end); |
| 9503 | if (ret == -1) |
| 9504 | return 0; |
| 9505 | |
| 9506 | /* Initialise sample data which will be filled. */ |
| 9507 | smp->data.type = SMP_T_BIN; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 9508 | smp->data.u.str.area = temp->area; |
| 9509 | smp->data.u.str.data = buf - temp->area; |
Thierry FOURNIER | 5617dce | 2017-04-09 05:38:19 +0200 | [diff] [blame] | 9510 | smp->data.u.str.size = temp->size; |
| 9511 | |
| 9512 | return 1; |
| 9513 | } |
| 9514 | |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9515 | /* returns the longest available part of the body. This requires that the body |
| 9516 | * has been waited for using http-buffer-request. |
| 9517 | */ |
| 9518 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9519 | smp_fetch_body(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9520 | { |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9521 | struct http_msg *msg; |
| 9522 | unsigned long len; |
| 9523 | unsigned long block1; |
| 9524 | char *body; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 9525 | struct buffer *temp; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9526 | |
| 9527 | CHECK_HTTP_MESSAGE_FIRST(); |
| 9528 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9529 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 9530 | msg = &smp->strm->txn->req; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9531 | else |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 9532 | msg = &smp->strm->txn->rsp; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9533 | |
| 9534 | len = http_body_bytes(msg); |
Willy Tarreau | 188e230 | 2018-06-15 11:11:53 +0200 | [diff] [blame] | 9535 | body = c_ptr(msg->chn, -http_data_rewind(msg)); |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9536 | |
| 9537 | block1 = len; |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 9538 | if (block1 > b_wrap(&msg->chn->buf) - body) |
| 9539 | block1 = b_wrap(&msg->chn->buf) - body; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9540 | |
| 9541 | if (block1 == len) { |
| 9542 | /* buffer is not wrapped (or empty) */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9543 | smp->data.type = SMP_T_BIN; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 9544 | smp->data.u.str.area = body; |
| 9545 | smp->data.u.str.data = len; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9546 | smp->flags = SMP_F_VOL_TEST | SMP_F_CONST; |
| 9547 | } |
| 9548 | else { |
| 9549 | /* buffer is wrapped, we need to defragment it */ |
| 9550 | temp = get_trash_chunk(); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 9551 | memcpy(temp->area, body, block1); |
| 9552 | memcpy(temp->area + block1, b_orig(&msg->chn->buf), |
| 9553 | len - block1); |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9554 | smp->data.type = SMP_T_BIN; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 9555 | smp->data.u.str.area = temp->area; |
| 9556 | smp->data.u.str.data = len; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9557 | smp->flags = SMP_F_VOL_TEST; |
| 9558 | } |
| 9559 | return 1; |
| 9560 | } |
| 9561 | |
| 9562 | |
| 9563 | /* returns the available length of the body. This requires that the body |
| 9564 | * has been waited for using http-buffer-request. |
| 9565 | */ |
| 9566 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9567 | smp_fetch_body_len(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9568 | { |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9569 | struct http_msg *msg; |
| 9570 | |
| 9571 | CHECK_HTTP_MESSAGE_FIRST(); |
| 9572 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9573 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 9574 | msg = &smp->strm->txn->req; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9575 | else |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 9576 | msg = &smp->strm->txn->rsp; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9577 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9578 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9579 | smp->data.u.sint = http_body_bytes(msg); |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9580 | |
| 9581 | smp->flags = SMP_F_VOL_TEST; |
| 9582 | return 1; |
| 9583 | } |
| 9584 | |
| 9585 | |
| 9586 | /* returns the advertised length of the body, or the advertised size of the |
| 9587 | * chunks available in the buffer. This requires that the body has been waited |
| 9588 | * for using http-buffer-request. |
| 9589 | */ |
| 9590 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9591 | smp_fetch_body_size(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9592 | { |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9593 | struct http_msg *msg; |
| 9594 | |
| 9595 | CHECK_HTTP_MESSAGE_FIRST(); |
| 9596 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9597 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 9598 | msg = &smp->strm->txn->req; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9599 | else |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 9600 | msg = &smp->strm->txn->rsp; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9601 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9602 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9603 | smp->data.u.sint = msg->body_len; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9604 | |
| 9605 | smp->flags = SMP_F_VOL_TEST; |
| 9606 | return 1; |
| 9607 | } |
| 9608 | |
| 9609 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9610 | /* 4. Check on URL/URI. A pointer to the URI is stored. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 9611 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9612 | smp_fetch_url(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9613 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 9614 | struct http_txn *txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9615 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9616 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9617 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 9618 | txn = smp->strm->txn; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9619 | smp->data.type = SMP_T_STR; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 9620 | smp->data.u.str.data = txn->req.sl.rq.u_l; |
| 9621 | smp->data.u.str.area = ci_head(txn->req.chn) + txn->req.sl.rq.u; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9622 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9623 | return 1; |
| 9624 | } |
| 9625 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9626 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9627 | smp_fetch_url_ip(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9628 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 9629 | struct http_txn *txn; |
Willy Tarreau | 4c804ec | 2013-09-30 14:37:14 +0200 | [diff] [blame] | 9630 | struct sockaddr_storage addr; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9631 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9632 | CHECK_HTTP_MESSAGE_FIRST(); |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9633 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 9634 | txn = smp->strm->txn; |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 9635 | url2sa(ci_head(txn->req.chn) + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr, NULL); |
Willy Tarreau | 4c804ec | 2013-09-30 14:37:14 +0200 | [diff] [blame] | 9636 | if (((struct sockaddr_in *)&addr)->sin_family != AF_INET) |
Willy Tarreau | f4362b3 | 2011-12-16 17:49:52 +0100 | [diff] [blame] | 9637 | return 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9638 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9639 | smp->data.type = SMP_T_IPV4; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9640 | smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9641 | smp->flags = 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9642 | return 1; |
| 9643 | } |
| 9644 | |
| 9645 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9646 | smp_fetch_url_port(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9647 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 9648 | struct http_txn *txn; |
Willy Tarreau | 4c804ec | 2013-09-30 14:37:14 +0200 | [diff] [blame] | 9649 | struct sockaddr_storage addr; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9650 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9651 | CHECK_HTTP_MESSAGE_FIRST(); |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9652 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 9653 | txn = smp->strm->txn; |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 9654 | url2sa(ci_head(txn->req.chn) + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr, NULL); |
Willy Tarreau | 4c804ec | 2013-09-30 14:37:14 +0200 | [diff] [blame] | 9655 | if (((struct sockaddr_in *)&addr)->sin_family != AF_INET) |
| 9656 | return 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9657 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9658 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9659 | smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port); |
Willy Tarreau | 21e5b0e | 2012-04-23 19:25:44 +0200 | [diff] [blame] | 9660 | smp->flags = 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9661 | return 1; |
| 9662 | } |
| 9663 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9664 | /* Fetch an HTTP header. A pointer to the beginning of the value is returned. |
| 9665 | * Accepts an optional argument of type string containing the header field name, |
| 9666 | * and an optional argument of type signed or unsigned integer to request an |
| 9667 | * explicit occurrence of the header. Note that in the event of a missing name, |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 9668 | * headers are considered from the first one. It does not stop on commas and |
| 9669 | * returns full lines instead (useful for User-Agent or Date for example). |
| 9670 | */ |
| 9671 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9672 | smp_fetch_fhdr(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 9673 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 9674 | struct hdr_idx *idx; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 9675 | struct hdr_ctx *ctx = smp->ctx.a[0]; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 9676 | const struct http_msg *msg; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 9677 | int occ = 0; |
| 9678 | const char *name_str = NULL; |
| 9679 | int name_len = 0; |
| 9680 | |
| 9681 | if (!ctx) { |
| 9682 | /* first call */ |
| 9683 | ctx = &static_hdr_ctx; |
| 9684 | ctx->idx = 0; |
| 9685 | smp->ctx.a[0] = ctx; |
| 9686 | } |
| 9687 | |
| 9688 | if (args) { |
| 9689 | if (args[0].type != ARGT_STR) |
| 9690 | return 0; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 9691 | name_str = args[0].data.str.area; |
| 9692 | name_len = args[0].data.str.data; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 9693 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 9694 | if (args[1].type == ARGT_SINT) |
| 9695 | occ = args[1].data.sint; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 9696 | } |
| 9697 | |
| 9698 | CHECK_HTTP_MESSAGE_FIRST(); |
| 9699 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 9700 | idx = &smp->strm->txn->hdr_idx; |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9701 | msg = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &smp->strm->txn->req : &smp->strm->txn->rsp; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 9702 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 9703 | if (ctx && !(smp->flags & SMP_F_NOT_LAST)) |
| 9704 | /* search for header from the beginning */ |
| 9705 | ctx->idx = 0; |
| 9706 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9707 | if (!occ && !(smp->opt & SMP_OPT_ITERATE)) |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 9708 | /* no explicit occurrence and single fetch => last header by default */ |
| 9709 | occ = -1; |
| 9710 | |
| 9711 | if (!occ) |
| 9712 | /* prepare to report multiple occurrences for ACL fetches */ |
| 9713 | smp->flags |= SMP_F_NOT_LAST; |
| 9714 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9715 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9716 | smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 9717 | if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data)) |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 9718 | return 1; |
| 9719 | |
| 9720 | smp->flags &= ~SMP_F_NOT_LAST; |
| 9721 | return 0; |
| 9722 | } |
| 9723 | |
| 9724 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
| 9725 | * Accepts exactly 1 argument of type string. It does not stop on commas and |
| 9726 | * returns full lines instead (useful for User-Agent or Date for example). |
| 9727 | */ |
| 9728 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9729 | smp_fetch_fhdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 9730 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 9731 | struct hdr_idx *idx; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 9732 | struct hdr_ctx ctx; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 9733 | const struct http_msg *msg; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 9734 | int cnt; |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 9735 | const char *name = NULL; |
| 9736 | int len = 0; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 9737 | |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 9738 | if (args && args->type == ARGT_STR) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 9739 | name = args->data.str.area; |
| 9740 | len = args->data.str.data; |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 9741 | } |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 9742 | |
| 9743 | CHECK_HTTP_MESSAGE_FIRST(); |
| 9744 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 9745 | idx = &smp->strm->txn->hdr_idx; |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9746 | msg = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &smp->strm->txn->req : &smp->strm->txn->rsp; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 9747 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 9748 | ctx.idx = 0; |
| 9749 | cnt = 0; |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 9750 | while (http_find_full_header2(name, len, ci_head(msg->chn), idx, &ctx)) |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 9751 | cnt++; |
| 9752 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9753 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9754 | smp->data.u.sint = cnt; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 9755 | smp->flags = SMP_F_VOL_HDR; |
| 9756 | return 1; |
| 9757 | } |
| 9758 | |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 9759 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9760 | smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 9761 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 9762 | struct hdr_idx *idx; |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 9763 | struct hdr_ctx ctx; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 9764 | const struct http_msg *msg; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 9765 | struct buffer *temp; |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 9766 | char del = ','; |
| 9767 | |
| 9768 | if (args && args->type == ARGT_STR) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 9769 | del = *args[0].data.str.area; |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 9770 | |
| 9771 | CHECK_HTTP_MESSAGE_FIRST(); |
| 9772 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 9773 | idx = &smp->strm->txn->hdr_idx; |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9774 | msg = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &smp->strm->txn->req : &smp->strm->txn->rsp; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 9775 | |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 9776 | temp = get_trash_chunk(); |
| 9777 | |
| 9778 | ctx.idx = 0; |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 9779 | while (http_find_next_header(ci_head(msg->chn), idx, &ctx)) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 9780 | if (temp->data) |
| 9781 | temp->area[temp->data++] = del; |
| 9782 | memcpy(temp->area + temp->data, ctx.line, ctx.del); |
| 9783 | temp->data += ctx.del; |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 9784 | } |
| 9785 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9786 | smp->data.type = SMP_T_STR; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 9787 | smp->data.u.str.area = temp->area; |
| 9788 | smp->data.u.str.data = temp->data; |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 9789 | smp->flags = SMP_F_VOL_HDR; |
| 9790 | return 1; |
| 9791 | } |
| 9792 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 9793 | /* Fetch an HTTP header. A pointer to the beginning of the value is returned. |
| 9794 | * Accepts an optional argument of type string containing the header field name, |
| 9795 | * and an optional argument of type signed or unsigned integer to request an |
| 9796 | * explicit occurrence of the header. Note that in the event of a missing name, |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9797 | * headers are considered from the first one. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9798 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9799 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9800 | smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9801 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 9802 | struct hdr_idx *idx; |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 9803 | struct hdr_ctx *ctx = smp->ctx.a[0]; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 9804 | const struct http_msg *msg; |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9805 | int occ = 0; |
| 9806 | const char *name_str = NULL; |
| 9807 | int name_len = 0; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 9808 | |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 9809 | if (!ctx) { |
| 9810 | /* first call */ |
| 9811 | ctx = &static_hdr_ctx; |
| 9812 | ctx->idx = 0; |
Willy Tarreau | ffb6f08 | 2013-04-02 23:16:53 +0200 | [diff] [blame] | 9813 | smp->ctx.a[0] = ctx; |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 9814 | } |
| 9815 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9816 | if (args) { |
| 9817 | if (args[0].type != ARGT_STR) |
| 9818 | return 0; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 9819 | name_str = args[0].data.str.area; |
| 9820 | name_len = args[0].data.str.data; |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9821 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 9822 | if (args[1].type == ARGT_SINT) |
| 9823 | occ = args[1].data.sint; |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9824 | } |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 9825 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 9826 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9827 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 9828 | idx = &smp->strm->txn->hdr_idx; |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9829 | msg = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &smp->strm->txn->req : &smp->strm->txn->rsp; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 9830 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9831 | if (ctx && !(smp->flags & SMP_F_NOT_LAST)) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9832 | /* search for header from the beginning */ |
| 9833 | ctx->idx = 0; |
| 9834 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9835 | if (!occ && !(smp->opt & SMP_OPT_ITERATE)) |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9836 | /* no explicit occurrence and single fetch => last header by default */ |
| 9837 | occ = -1; |
| 9838 | |
| 9839 | if (!occ) |
| 9840 | /* prepare to report multiple occurrences for ACL fetches */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9841 | smp->flags |= SMP_F_NOT_LAST; |
Willy Tarreau | 664092c | 2011-12-16 19:11:42 +0100 | [diff] [blame] | 9842 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9843 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9844 | smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 9845 | if (http_get_hdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data)) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9846 | return 1; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9847 | |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9848 | smp->flags &= ~SMP_F_NOT_LAST; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9849 | return 0; |
| 9850 | } |
| 9851 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9852 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 9853 | * Accepts exactly 1 argument of type string. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9854 | */ |
| 9855 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9856 | smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9857 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 9858 | struct hdr_idx *idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9859 | struct hdr_ctx ctx; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 9860 | const struct http_msg *msg; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9861 | int cnt; |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 9862 | const char *name = NULL; |
| 9863 | int len = 0; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9864 | |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 9865 | if (args && args->type == ARGT_STR) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 9866 | name = args->data.str.area; |
| 9867 | len = args->data.str.data; |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 9868 | } |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 9869 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 9870 | CHECK_HTTP_MESSAGE_FIRST(); |
| 9871 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 9872 | idx = &smp->strm->txn->hdr_idx; |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9873 | msg = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &smp->strm->txn->req : &smp->strm->txn->rsp; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 9874 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9875 | ctx.idx = 0; |
| 9876 | cnt = 0; |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 9877 | while (http_find_header2(name, len, ci_head(msg->chn), idx, &ctx)) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9878 | cnt++; |
| 9879 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9880 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9881 | smp->data.u.sint = cnt; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9882 | smp->flags = SMP_F_VOL_HDR; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9883 | return 1; |
| 9884 | } |
| 9885 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9886 | /* Fetch an HTTP header's integer value. The integer value is returned. It |
| 9887 | * takes a mandatory argument of type string and an optional one of type int |
| 9888 | * to designate a specific occurrence. It returns an unsigned integer, which |
| 9889 | * may or may not be appropriate for everything. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9890 | */ |
| 9891 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9892 | smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9893 | { |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9894 | int ret = smp_fetch_hdr(args, smp, kw, private); |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 9895 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9896 | if (ret > 0) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9897 | smp->data.type = SMP_T_SINT; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 9898 | smp->data.u.sint = strl2ic(smp->data.u.str.area, |
| 9899 | smp->data.u.str.data); |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9900 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9901 | |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 9902 | return ret; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9903 | } |
| 9904 | |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 9905 | /* Fetch an HTTP header's IP value. takes a mandatory argument of type string |
| 9906 | * and an optional one of type int to designate a specific occurrence. |
| 9907 | * It returns an IPv4 or IPv6 address. |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 9908 | */ |
| 9909 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9910 | smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 9911 | { |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 9912 | int ret; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 9913 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9914 | while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 9915 | if (url2ipv4((char *) smp->data.u.str.area, &smp->data.u.ipv4)) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9916 | smp->data.type = SMP_T_IPV4; |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 9917 | break; |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 9918 | } else { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 9919 | struct buffer *temp = get_trash_chunk(); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 9920 | if (smp->data.u.str.data < temp->size - 1) { |
| 9921 | memcpy(temp->area, smp->data.u.str.area, |
| 9922 | smp->data.u.str.data); |
| 9923 | temp->area[smp->data.u.str.data] = '\0'; |
| 9924 | if (inet_pton(AF_INET6, temp->area, &smp->data.u.ipv6)) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9925 | smp->data.type = SMP_T_IPV6; |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 9926 | break; |
| 9927 | } |
| 9928 | } |
| 9929 | } |
| 9930 | |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 9931 | /* if the header doesn't match an IP address, fetch next one */ |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9932 | if (!(smp->flags & SMP_F_NOT_LAST)) |
| 9933 | return 0; |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 9934 | } |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 9935 | return ret; |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 9936 | } |
| 9937 | |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 9938 | /* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at |
| 9939 | * the first '/' after the possible hostname, and ends before the possible '?'. |
| 9940 | */ |
| 9941 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9942 | smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 9943 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 9944 | struct http_txn *txn; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 9945 | char *ptr, *end; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9946 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9947 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9948 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 9949 | txn = smp->strm->txn; |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 9950 | end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l; |
Willy Tarreau | 6b952c8 | 2018-09-10 17:45:34 +0200 | [diff] [blame] | 9951 | ptr = http_txn_get_path(txn); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 9952 | if (!ptr) |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 9953 | return 0; |
| 9954 | |
| 9955 | /* OK, we got the '/' ! */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9956 | smp->data.type = SMP_T_STR; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 9957 | smp->data.u.str.area = ptr; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 9958 | |
| 9959 | while (ptr < end && *ptr != '?') |
| 9960 | ptr++; |
| 9961 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 9962 | smp->data.u.str.data = ptr - smp->data.u.str.area; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9963 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 9964 | return 1; |
| 9965 | } |
| 9966 | |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 9967 | /* This produces a concatenation of the first occurrence of the Host header |
| 9968 | * followed by the path component if it begins with a slash ('/'). This means |
| 9969 | * that '*' will not be added, resulting in exactly the first Host entry. |
| 9970 | * If no Host header is found, then the path is returned as-is. The returned |
| 9971 | * value is stored in the trash so it does not need to be marked constant. |
Willy Tarreau | b169eba | 2013-12-16 15:14:43 +0100 | [diff] [blame] | 9972 | * The returned sample is of type string. |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 9973 | */ |
| 9974 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9975 | smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 9976 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 9977 | struct http_txn *txn; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 9978 | char *ptr, *end, *beg; |
| 9979 | struct hdr_ctx ctx; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 9980 | struct buffer *temp; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 9981 | |
| 9982 | CHECK_HTTP_MESSAGE_FIRST(); |
| 9983 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 9984 | txn = smp->strm->txn; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 9985 | ctx.idx = 0; |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 9986 | if (!http_find_header2("Host", 4, ci_head(txn->req.chn), &txn->hdr_idx, &ctx) || !ctx.vlen) |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9987 | return smp_fetch_path(args, smp, kw, private); |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 9988 | |
| 9989 | /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */ |
Willy Tarreau | 3caf2af | 2014-06-24 17:27:02 +0200 | [diff] [blame] | 9990 | temp = get_trash_chunk(); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 9991 | memcpy(temp->area, ctx.line + ctx.val, ctx.vlen); |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9992 | smp->data.type = SMP_T_STR; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 9993 | smp->data.u.str.area = temp->area; |
| 9994 | smp->data.u.str.data = ctx.vlen; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 9995 | |
| 9996 | /* now retrieve the path */ |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 9997 | end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l; |
Willy Tarreau | 6b952c8 | 2018-09-10 17:45:34 +0200 | [diff] [blame] | 9998 | beg = http_txn_get_path(txn); |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 9999 | if (!beg) |
| 10000 | beg = end; |
| 10001 | |
| 10002 | for (ptr = beg; ptr < end && *ptr != '?'; ptr++); |
| 10003 | |
| 10004 | if (beg < ptr && *beg == '/') { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 10005 | memcpy(smp->data.u.str.area + smp->data.u.str.data, beg, |
| 10006 | ptr - beg); |
| 10007 | smp->data.u.str.data += ptr - beg; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10008 | } |
| 10009 | |
| 10010 | smp->flags = SMP_F_VOL_1ST; |
| 10011 | return 1; |
| 10012 | } |
| 10013 | |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 10014 | /* This produces a 32-bit hash of the concatenation of the first occurrence of |
| 10015 | * the Host header followed by the path component if it begins with a slash ('/'). |
| 10016 | * This means that '*' will not be added, resulting in exactly the first Host |
| 10017 | * entry. If no Host header is found, then the path is used. The resulting value |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 10018 | * is hashed using the path hash followed by a full avalanche hash and provides a |
| 10019 | * 32-bit integer value. This fetch is useful for tracking per-path activity on |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 10020 | * high-traffic sites without having to store whole paths. |
| 10021 | */ |
Thierry FOURNIER | 055b9d5 | 2014-07-15 16:11:07 +0200 | [diff] [blame] | 10022 | int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10023 | smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 10024 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10025 | struct http_txn *txn; |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 10026 | struct hdr_ctx ctx; |
| 10027 | unsigned int hash = 0; |
| 10028 | char *ptr, *beg, *end; |
| 10029 | int len; |
| 10030 | |
| 10031 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10032 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10033 | txn = smp->strm->txn; |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 10034 | ctx.idx = 0; |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 10035 | if (http_find_header2("Host", 4, ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) { |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 10036 | /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */ |
| 10037 | ptr = ctx.line + ctx.val; |
| 10038 | len = ctx.vlen; |
| 10039 | while (len--) |
| 10040 | hash = *(ptr++) + (hash << 6) + (hash << 16) - hash; |
| 10041 | } |
| 10042 | |
| 10043 | /* now retrieve the path */ |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 10044 | end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l; |
Willy Tarreau | 6b952c8 | 2018-09-10 17:45:34 +0200 | [diff] [blame] | 10045 | beg = http_txn_get_path(txn); |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 10046 | if (!beg) |
| 10047 | beg = end; |
| 10048 | |
| 10049 | for (ptr = beg; ptr < end && *ptr != '?'; ptr++); |
| 10050 | |
| 10051 | if (beg < ptr && *beg == '/') { |
| 10052 | while (beg < ptr) |
| 10053 | hash = *(beg++) + (hash << 6) + (hash << 16) - hash; |
| 10054 | } |
| 10055 | hash = full_hash(hash); |
| 10056 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10057 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10058 | smp->data.u.sint = hash; |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 10059 | smp->flags = SMP_F_VOL_1ST; |
| 10060 | return 1; |
| 10061 | } |
| 10062 | |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10063 | /* This concatenates the source address with the 32-bit hash of the Host and |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 10064 | * path as returned by smp_fetch_base32(). The idea is to have per-source and |
| 10065 | * per-path counters. The result is a binary block from 8 to 20 bytes depending |
| 10066 | * on the source address length. The path hash is stored before the address so |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10067 | * that in environments where IPv6 is insignificant, truncating the output to |
| 10068 | * 8 bytes would still work. |
| 10069 | */ |
| 10070 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10071 | smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10072 | { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 10073 | struct buffer *temp; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10074 | struct connection *cli_conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10075 | |
| 10076 | if (!cli_conn) |
| 10077 | return 0; |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10078 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10079 | if (!smp_fetch_base32(args, smp, kw, private)) |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10080 | return 0; |
| 10081 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 10082 | temp = get_trash_chunk(); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 10083 | *(unsigned int *) temp->area = htonl(smp->data.u.sint); |
| 10084 | temp->data += sizeof(unsigned int); |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10085 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10086 | switch (cli_conn->addr.from.ss_family) { |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10087 | case AF_INET: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 10088 | memcpy(temp->area + temp->data, |
| 10089 | &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, |
| 10090 | 4); |
| 10091 | temp->data += 4; |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10092 | break; |
| 10093 | case AF_INET6: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 10094 | memcpy(temp->area + temp->data, |
| 10095 | &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, |
| 10096 | 16); |
| 10097 | temp->data += 16; |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10098 | break; |
| 10099 | default: |
| 10100 | return 0; |
| 10101 | } |
| 10102 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10103 | smp->data.u.str = *temp; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10104 | smp->data.type = SMP_T_BIN; |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10105 | return 1; |
| 10106 | } |
| 10107 | |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 10108 | /* Extracts the query string, which comes after the question mark '?'. If no |
| 10109 | * question mark is found, nothing is returned. Otherwise it returns a sample |
| 10110 | * of type string carrying the whole query string. |
| 10111 | */ |
| 10112 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10113 | smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 10114 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10115 | struct http_txn *txn; |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 10116 | char *ptr, *end; |
| 10117 | |
| 10118 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10119 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10120 | txn = smp->strm->txn; |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 10121 | ptr = ci_head(txn->req.chn) + txn->req.sl.rq.u; |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 10122 | end = ptr + txn->req.sl.rq.u_l; |
| 10123 | |
| 10124 | /* look up the '?' */ |
| 10125 | do { |
| 10126 | if (ptr == end) |
| 10127 | return 0; |
| 10128 | } while (*ptr++ != '?'); |
| 10129 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10130 | smp->data.type = SMP_T_STR; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 10131 | smp->data.u.str.area = ptr; |
| 10132 | smp->data.u.str.data = end - ptr; |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 10133 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
| 10134 | return 1; |
| 10135 | } |
| 10136 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10137 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10138 | smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10139 | { |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10140 | /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged |
| 10141 | * as a layer7 ACL, which involves automatic allocation of hdr_idx. |
| 10142 | */ |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10143 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10144 | CHECK_HTTP_MESSAGE_FIRST_PERM(); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10145 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10146 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10147 | smp->data.u.sint = 1; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10148 | return 1; |
| 10149 | } |
| 10150 | |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 10151 | /* return a valid test if the current request is the first one on the connection */ |
| 10152 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10153 | smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 10154 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10155 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10156 | smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST); |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 10157 | return 1; |
| 10158 | } |
| 10159 | |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10160 | /* Accepts exactly 1 argument of type userlist */ |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 10161 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10162 | smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 10163 | { |
| 10164 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10165 | if (!args || args->type != ARGT_USR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10166 | return 0; |
| 10167 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10168 | CHECK_HTTP_MESSAGE_FIRST(); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 10169 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10170 | if (!get_http_auth(smp->strm)) |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 10171 | return 0; |
| 10172 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10173 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10174 | smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user, |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10175 | smp->strm->txn->auth.pass); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 10176 | return 1; |
| 10177 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10178 | |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10179 | /* Accepts exactly 1 argument of type userlist */ |
| 10180 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10181 | smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10182 | { |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10183 | if (!args || args->type != ARGT_USR) |
| 10184 | return 0; |
| 10185 | |
| 10186 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10187 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10188 | if (!get_http_auth(smp->strm)) |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10189 | return 0; |
| 10190 | |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10191 | /* if the user does not belong to the userlist or has a wrong password, |
| 10192 | * report that it unconditionally does not match. Otherwise we return |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 10193 | * a string containing the username. |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10194 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10195 | if (!check_user(args->data.usr, smp->strm->txn->auth.user, |
| 10196 | smp->strm->txn->auth.pass)) |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 10197 | return 0; |
| 10198 | |
| 10199 | /* pat_match_auth() will need the user list */ |
| 10200 | smp->ctx.a[0] = args->data.usr; |
| 10201 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10202 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10203 | smp->flags = SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 10204 | smp->data.u.str.area = smp->strm->txn->auth.user; |
| 10205 | smp->data.u.str.data = strlen(smp->strm->txn->auth.user); |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10206 | |
| 10207 | return 1; |
| 10208 | } |
| 10209 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10210 | /* Try to find the next occurrence of a cookie name in a cookie header value. |
| 10211 | * The lookup begins at <hdr>. The pointer and size of the next occurrence of |
| 10212 | * the cookie value is returned into *value and *value_l, and the function |
| 10213 | * returns a pointer to the next pointer to search from if the value was found. |
| 10214 | * Otherwise if the cookie was not found, NULL is returned and neither value |
| 10215 | * nor value_l are touched. The input <hdr> string should first point to the |
| 10216 | * header's value, and the <hdr_end> pointer must point to the first character |
| 10217 | * not part of the value. <list> must be non-zero if value may represent a list |
| 10218 | * of values (cookie headers). This makes it faster to abort parsing when no |
| 10219 | * list is expected. |
| 10220 | */ |
David Carlier | 4686f79 | 2015-09-25 14:10:50 +0100 | [diff] [blame] | 10221 | char * |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10222 | extract_cookie_value(char *hdr, const char *hdr_end, |
| 10223 | char *cookie_name, size_t cookie_name_l, int list, |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 10224 | char **value, size_t *value_l) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10225 | { |
| 10226 | char *equal, *att_end, *att_beg, *val_beg, *val_end; |
| 10227 | char *next; |
| 10228 | |
| 10229 | /* we search at least a cookie name followed by an equal, and more |
| 10230 | * generally something like this : |
| 10231 | * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n |
| 10232 | */ |
| 10233 | for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) { |
| 10234 | /* Iterate through all cookies on this line */ |
| 10235 | |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 10236 | while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg)) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10237 | att_beg++; |
| 10238 | |
| 10239 | /* find att_end : this is the first character after the last non |
| 10240 | * space before the equal. It may be equal to hdr_end. |
| 10241 | */ |
| 10242 | equal = att_end = att_beg; |
| 10243 | |
| 10244 | while (equal < hdr_end) { |
| 10245 | if (*equal == '=' || *equal == ';' || (list && *equal == ',')) |
| 10246 | break; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 10247 | if (HTTP_IS_SPHT(*equal++)) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10248 | continue; |
| 10249 | att_end = equal; |
| 10250 | } |
| 10251 | |
| 10252 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 10253 | * is between <att_beg> and <equal>, both may be identical. |
| 10254 | */ |
| 10255 | |
| 10256 | /* look for end of cookie if there is an equal sign */ |
| 10257 | if (equal < hdr_end && *equal == '=') { |
| 10258 | /* look for the beginning of the value */ |
| 10259 | val_beg = equal + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 10260 | while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg)) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10261 | val_beg++; |
| 10262 | |
| 10263 | /* find the end of the value, respecting quotes */ |
| 10264 | next = find_cookie_value_end(val_beg, hdr_end); |
| 10265 | |
| 10266 | /* make val_end point to the first white space or delimitor after the value */ |
| 10267 | val_end = next; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 10268 | while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1))) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10269 | val_end--; |
| 10270 | } else { |
| 10271 | val_beg = val_end = next = equal; |
| 10272 | } |
| 10273 | |
| 10274 | /* We have nothing to do with attributes beginning with '$'. However, |
| 10275 | * they will automatically be removed if a header before them is removed, |
| 10276 | * since they're supposed to be linked together. |
| 10277 | */ |
| 10278 | if (*att_beg == '$') |
| 10279 | continue; |
| 10280 | |
| 10281 | /* Ignore cookies with no equal sign */ |
| 10282 | if (equal == next) |
| 10283 | continue; |
| 10284 | |
| 10285 | /* Now we have the cookie name between att_beg and att_end, and |
| 10286 | * its value between val_beg and val_end. |
| 10287 | */ |
| 10288 | |
| 10289 | if (att_end - att_beg == cookie_name_l && |
| 10290 | memcmp(att_beg, cookie_name, cookie_name_l) == 0) { |
| 10291 | /* let's return this value and indicate where to go on from */ |
| 10292 | *value = val_beg; |
| 10293 | *value_l = val_end - val_beg; |
| 10294 | return next + 1; |
| 10295 | } |
| 10296 | |
| 10297 | /* Set-Cookie headers only have the name in the first attr=value part */ |
| 10298 | if (!list) |
| 10299 | break; |
| 10300 | } |
| 10301 | |
| 10302 | return NULL; |
| 10303 | } |
| 10304 | |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10305 | /* Fetch a captured HTTP request header. The index is the position of |
| 10306 | * the "capture" option in the configuration file |
| 10307 | */ |
| 10308 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10309 | smp_fetch_capture_header_req(const struct arg *args, struct sample *smp, const char *kw, void *private) |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10310 | { |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10311 | struct proxy *fe = strm_fe(smp->strm); |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10312 | int idx; |
| 10313 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 10314 | if (!args || args->type != ARGT_SINT) |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10315 | return 0; |
| 10316 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 10317 | idx = args->data.sint; |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10318 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10319 | if (idx > (fe->nb_req_cap - 1) || smp->strm->req_cap == NULL || smp->strm->req_cap[idx] == NULL) |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10320 | return 0; |
| 10321 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10322 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10323 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 10324 | smp->data.u.str.area = smp->strm->req_cap[idx]; |
| 10325 | smp->data.u.str.data = strlen(smp->strm->req_cap[idx]); |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10326 | |
| 10327 | return 1; |
| 10328 | } |
| 10329 | |
| 10330 | /* Fetch a captured HTTP response header. The index is the position of |
| 10331 | * the "capture" option in the configuration file |
| 10332 | */ |
| 10333 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10334 | smp_fetch_capture_header_res(const struct arg *args, struct sample *smp, const char *kw, void *private) |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10335 | { |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10336 | struct proxy *fe = strm_fe(smp->strm); |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10337 | int idx; |
| 10338 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 10339 | if (!args || args->type != ARGT_SINT) |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10340 | return 0; |
| 10341 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 10342 | idx = args->data.sint; |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10343 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10344 | if (idx > (fe->nb_rsp_cap - 1) || smp->strm->res_cap == NULL || smp->strm->res_cap[idx] == NULL) |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10345 | return 0; |
| 10346 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10347 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10348 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 10349 | smp->data.u.str.area = smp->strm->res_cap[idx]; |
| 10350 | smp->data.u.str.data = strlen(smp->strm->res_cap[idx]); |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10351 | |
| 10352 | return 1; |
| 10353 | } |
| 10354 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10355 | /* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */ |
| 10356 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10357 | smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private) |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10358 | { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 10359 | struct buffer *temp; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10360 | struct http_txn *txn = smp->strm->txn; |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10361 | char *ptr; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10362 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10363 | if (!txn || !txn->uri) |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10364 | return 0; |
| 10365 | |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10366 | ptr = txn->uri; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10367 | |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10368 | while (*ptr != ' ' && *ptr != '\0') /* find first space */ |
| 10369 | ptr++; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10370 | |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10371 | temp = get_trash_chunk(); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 10372 | temp->area = txn->uri; |
| 10373 | temp->data = ptr - txn->uri; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10374 | smp->data.u.str = *temp; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10375 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10376 | smp->flags = SMP_F_CONST; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10377 | |
| 10378 | return 1; |
| 10379 | |
| 10380 | } |
| 10381 | |
| 10382 | /* Extracts the path in the HTTP request, the txn->uri should be filled before the call */ |
| 10383 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10384 | smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private) |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10385 | { |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10386 | struct http_txn *txn = smp->strm->txn; |
Willy Tarreau | 6b952c8 | 2018-09-10 17:45:34 +0200 | [diff] [blame] | 10387 | struct ist path; |
| 10388 | const char *ptr; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10389 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10390 | if (!txn || !txn->uri) |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10391 | return 0; |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10392 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10393 | ptr = txn->uri; |
| 10394 | |
| 10395 | while (*ptr != ' ' && *ptr != '\0') /* find first space */ |
| 10396 | ptr++; |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10397 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10398 | if (!*ptr) |
| 10399 | return 0; |
| 10400 | |
| 10401 | ptr++; /* skip the space */ |
| 10402 | |
Willy Tarreau | 6b952c8 | 2018-09-10 17:45:34 +0200 | [diff] [blame] | 10403 | path = http_get_path(ist(ptr)); |
| 10404 | if (!path.ptr) |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10405 | return 0; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10406 | |
Willy Tarreau | 6b952c8 | 2018-09-10 17:45:34 +0200 | [diff] [blame] | 10407 | smp->data.u.str.area = path.ptr; |
| 10408 | smp->data.u.str.data = path.len; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10409 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10410 | smp->flags = SMP_F_CONST; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10411 | |
| 10412 | return 1; |
| 10413 | } |
| 10414 | |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 10415 | /* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it |
| 10416 | * as a string (either "HTTP/1.0" or "HTTP/1.1"). |
| 10417 | */ |
| 10418 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10419 | smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 10420 | { |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10421 | struct http_txn *txn = smp->strm->txn; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 10422 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10423 | if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST) |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 10424 | return 0; |
| 10425 | |
| 10426 | if (txn->req.flags & HTTP_MSGF_VER_11) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 10427 | smp->data.u.str.area = "HTTP/1.1"; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 10428 | else |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 10429 | smp->data.u.str.area = "HTTP/1.0"; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 10430 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 10431 | smp->data.u.str.data = 8; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10432 | smp->data.type = SMP_T_STR; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 10433 | smp->flags = SMP_F_CONST; |
| 10434 | return 1; |
| 10435 | |
| 10436 | } |
| 10437 | |
| 10438 | /* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it |
| 10439 | * as a string (either "HTTP/1.0" or "HTTP/1.1"). |
| 10440 | */ |
| 10441 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10442 | smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 10443 | { |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10444 | struct http_txn *txn = smp->strm->txn; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 10445 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10446 | if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST) |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 10447 | return 0; |
| 10448 | |
| 10449 | if (txn->rsp.flags & HTTP_MSGF_VER_11) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 10450 | smp->data.u.str.area = "HTTP/1.1"; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 10451 | else |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 10452 | smp->data.u.str.area = "HTTP/1.0"; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 10453 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 10454 | smp->data.u.str.data = 8; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10455 | smp->data.type = SMP_T_STR; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 10456 | smp->flags = SMP_F_CONST; |
| 10457 | return 1; |
| 10458 | |
| 10459 | } |
| 10460 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10461 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10462 | /* Iterate over all cookies present in a message. The context is stored in |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10463 | * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 10464 | * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10465 | * the direction, multiple cookies may be parsed on the same line or not. |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10466 | * The cookie name is in args and the name length in args->data.str.len. |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10467 | * Accepts exactly 1 argument of type string. If the input options indicate |
| 10468 | * that no iterating is desired, then only last value is fetched if any. |
William Lallemand | 07c8b24 | 2014-05-02 17:11:07 +0200 | [diff] [blame] | 10469 | * The returned sample is of type CSTR. Can be used to parse cookies in other |
| 10470 | * files. |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10471 | */ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10472 | int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10473 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10474 | struct http_txn *txn; |
| 10475 | struct hdr_idx *idx; |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 10476 | struct hdr_ctx *ctx = smp->ctx.a[2]; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10477 | const struct http_msg *msg; |
| 10478 | const char *hdr_name; |
| 10479 | int hdr_name_len; |
| 10480 | char *sol; |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10481 | int occ = 0; |
| 10482 | int found = 0; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10483 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10484 | if (!args || args->type != ARGT_STR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10485 | return 0; |
| 10486 | |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 10487 | if (!ctx) { |
| 10488 | /* first call */ |
| 10489 | ctx = &static_hdr_ctx; |
| 10490 | ctx->idx = 0; |
| 10491 | smp->ctx.a[2] = ctx; |
| 10492 | } |
| 10493 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10494 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10495 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10496 | txn = smp->strm->txn; |
| 10497 | idx = &smp->strm->txn->hdr_idx; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10498 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10499 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10500 | msg = &txn->req; |
| 10501 | hdr_name = "Cookie"; |
| 10502 | hdr_name_len = 6; |
| 10503 | } else { |
| 10504 | msg = &txn->rsp; |
| 10505 | hdr_name = "Set-Cookie"; |
| 10506 | hdr_name_len = 10; |
| 10507 | } |
| 10508 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10509 | if (!occ && !(smp->opt & SMP_OPT_ITERATE)) |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10510 | /* no explicit occurrence and single fetch => last cookie by default */ |
| 10511 | occ = -1; |
| 10512 | |
| 10513 | /* OK so basically here, either we want only one value and it's the |
| 10514 | * last one, or we want to iterate over all of them and we fetch the |
| 10515 | * next one. |
| 10516 | */ |
| 10517 | |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 10518 | sol = ci_head(msg->chn); |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10519 | if (!(smp->flags & SMP_F_NOT_LAST)) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10520 | /* search for the header from the beginning, we must first initialize |
| 10521 | * the search parameters. |
| 10522 | */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10523 | smp->ctx.a[0] = NULL; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10524 | ctx->idx = 0; |
| 10525 | } |
| 10526 | |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10527 | smp->flags |= SMP_F_VOL_HDR; |
| 10528 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10529 | while (1) { |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10530 | /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */ |
| 10531 | if (!smp->ctx.a[0]) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10532 | if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx)) |
| 10533 | goto out; |
| 10534 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 10535 | if (ctx->vlen < args->data.str.data + 1) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10536 | continue; |
| 10537 | |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10538 | smp->ctx.a[0] = ctx->line + ctx->val; |
| 10539 | smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10540 | } |
| 10541 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10542 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10543 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10544 | smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1], |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 10545 | args->data.str.area, |
| 10546 | args->data.str.data, |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10547 | (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ, |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 10548 | &smp->data.u.str.area, |
| 10549 | &smp->data.u.str.data); |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10550 | if (smp->ctx.a[0]) { |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10551 | found = 1; |
| 10552 | if (occ >= 0) { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10553 | /* one value was returned into smp->data.u.str.{str,len} */ |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10554 | smp->flags |= SMP_F_NOT_LAST; |
| 10555 | return 1; |
| 10556 | } |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10557 | } |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10558 | /* if we're looking for last occurrence, let's loop */ |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10559 | } |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10560 | /* all cookie headers and values were scanned. If we're looking for the |
| 10561 | * last occurrence, we may return it now. |
| 10562 | */ |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10563 | out: |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10564 | smp->flags &= ~SMP_F_NOT_LAST; |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10565 | return found; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10566 | } |
| 10567 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10568 | /* Iterate over all cookies present in a request to count how many occurrences |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10569 | * match the name in args and args->data.str.len. If <multi> is non-null, then |
Willy Tarreau | b169eba | 2013-12-16 15:14:43 +0100 | [diff] [blame] | 10570 | * multiple cookies may be parsed on the same line. The returned sample is of |
| 10571 | * type UINT. Accepts exactly 1 argument of type string. |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10572 | */ |
| 10573 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10574 | smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10575 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10576 | struct http_txn *txn; |
| 10577 | struct hdr_idx *idx; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10578 | struct hdr_ctx ctx; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10579 | const struct http_msg *msg; |
| 10580 | const char *hdr_name; |
| 10581 | int hdr_name_len; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10582 | int cnt; |
| 10583 | char *val_beg, *val_end; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10584 | char *sol; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10585 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10586 | if (!args || args->type != ARGT_STR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10587 | return 0; |
| 10588 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10589 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10590 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10591 | txn = smp->strm->txn; |
| 10592 | idx = &smp->strm->txn->hdr_idx; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10593 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10594 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10595 | msg = &txn->req; |
| 10596 | hdr_name = "Cookie"; |
| 10597 | hdr_name_len = 6; |
| 10598 | } else { |
| 10599 | msg = &txn->rsp; |
| 10600 | hdr_name = "Set-Cookie"; |
| 10601 | hdr_name_len = 10; |
| 10602 | } |
| 10603 | |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 10604 | sol = ci_head(msg->chn); |
Willy Tarreau | 46787ed | 2012-04-11 17:28:40 +0200 | [diff] [blame] | 10605 | val_end = val_beg = NULL; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10606 | ctx.idx = 0; |
| 10607 | cnt = 0; |
| 10608 | |
| 10609 | while (1) { |
| 10610 | /* Note: val_beg == NULL every time we need to fetch a new header */ |
| 10611 | if (!val_beg) { |
| 10612 | if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx)) |
| 10613 | break; |
| 10614 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 10615 | if (ctx.vlen < args->data.str.data + 1) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10616 | continue; |
| 10617 | |
| 10618 | val_beg = ctx.line + ctx.val; |
| 10619 | val_end = val_beg + ctx.vlen; |
| 10620 | } |
| 10621 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10622 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10623 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10624 | while ((val_beg = extract_cookie_value(val_beg, val_end, |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 10625 | args->data.str.area, args->data.str.data, |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10626 | (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ, |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 10627 | &smp->data.u.str.area, |
| 10628 | &smp->data.u.str.data))) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10629 | cnt++; |
| 10630 | } |
| 10631 | } |
| 10632 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10633 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10634 | smp->data.u.sint = cnt; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10635 | smp->flags |= SMP_F_VOL_HDR; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10636 | return 1; |
| 10637 | } |
| 10638 | |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 10639 | /* Fetch an cookie's integer value. The integer value is returned. It |
| 10640 | * takes a mandatory argument of type string. It relies on smp_fetch_cookie(). |
| 10641 | */ |
| 10642 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10643 | smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 10644 | { |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10645 | int ret = smp_fetch_cookie(args, smp, kw, private); |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 10646 | |
| 10647 | if (ret > 0) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10648 | smp->data.type = SMP_T_SINT; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 10649 | smp->data.u.sint = strl2ic(smp->data.u.str.area, |
| 10650 | smp->data.u.str.data); |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 10651 | } |
| 10652 | |
| 10653 | return ret; |
| 10654 | } |
| 10655 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10656 | /************************************************************************/ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 10657 | /* The code below is dedicated to sample fetches */ |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 10658 | /************************************************************************/ |
| 10659 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10660 | /* |
| 10661 | * Given a path string and its length, find the position of beginning of the |
| 10662 | * query string. Returns NULL if no query string is found in the path. |
| 10663 | * |
| 10664 | * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22: |
| 10665 | * |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 10666 | * find_query_string(path, n, '?') points to "yo=mama;ye=daddy" string. |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10667 | */ |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 10668 | static inline char *find_param_list(char *path, size_t path_l, char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10669 | { |
| 10670 | char *p; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 10671 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 10672 | p = memchr(path, delim, path_l); |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10673 | return p ? p + 1 : NULL; |
| 10674 | } |
| 10675 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 10676 | static inline int is_param_delimiter(char c, char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10677 | { |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 10678 | return c == '&' || c == ';' || c == delim; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10679 | } |
| 10680 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10681 | /* after increasing a pointer value, it can exceed the first buffer |
| 10682 | * size. This function transform the value of <ptr> according with |
| 10683 | * the expected position. <chunks> is an array of the one or two |
| 10684 | * avalaible chunks. The first value is the start of the first chunk, |
| 10685 | * the second value if the end+1 of the first chunks. The third value |
| 10686 | * is NULL or the start of the second chunk and the fourth value is |
| 10687 | * the end+1 of the second chunk. The function returns 1 if does a |
| 10688 | * wrap, else returns 0. |
| 10689 | */ |
| 10690 | static inline int fix_pointer_if_wrap(const char **chunks, const char **ptr) |
| 10691 | { |
| 10692 | if (*ptr < chunks[1]) |
| 10693 | return 0; |
| 10694 | if (!chunks[2]) |
| 10695 | return 0; |
| 10696 | *ptr = chunks[2] + ( *ptr - chunks[1] ); |
| 10697 | return 1; |
| 10698 | } |
| 10699 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10700 | /* |
| 10701 | * Given a url parameter, find the starting position of the first occurence, |
| 10702 | * or NULL if the parameter is not found. |
| 10703 | * |
| 10704 | * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye", |
| 10705 | * the function will return query_string+8. |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10706 | * |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 10707 | * Warning: this function returns a pointer that can point to the first chunk |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10708 | * or the second chunk. The caller must be check the position before using the |
| 10709 | * result. |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10710 | */ |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10711 | static const char * |
| 10712 | find_url_param_pos(const char **chunks, |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 10713 | const char* url_param_name, size_t url_param_name_l, |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 10714 | char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10715 | { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10716 | const char *pos, *last, *equal; |
| 10717 | const char **bufs = chunks; |
| 10718 | int l1, l2; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10719 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10720 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10721 | pos = bufs[0]; |
| 10722 | last = bufs[1]; |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 10723 | while (pos < last) { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10724 | /* Check the equal. */ |
| 10725 | equal = pos + url_param_name_l; |
| 10726 | if (fix_pointer_if_wrap(chunks, &equal)) { |
| 10727 | if (equal >= chunks[3]) |
| 10728 | return NULL; |
| 10729 | } else { |
| 10730 | if (equal >= chunks[1]) |
| 10731 | return NULL; |
| 10732 | } |
| 10733 | if (*equal == '=') { |
| 10734 | if (pos + url_param_name_l > last) { |
| 10735 | /* process wrap case, we detect a wrap. In this case, the |
| 10736 | * comparison is performed in two parts. |
| 10737 | */ |
| 10738 | |
| 10739 | /* This is the end, we dont have any other chunk. */ |
| 10740 | if (bufs != chunks || !bufs[2]) |
| 10741 | return NULL; |
| 10742 | |
| 10743 | /* Compute the length of each part of the comparison. */ |
| 10744 | l1 = last - pos; |
| 10745 | l2 = url_param_name_l - l1; |
| 10746 | |
| 10747 | /* The second buffer is too short to contain the compared string. */ |
| 10748 | if (bufs[2] + l2 > bufs[3]) |
| 10749 | return NULL; |
| 10750 | |
| 10751 | if (memcmp(pos, url_param_name, l1) == 0 && |
| 10752 | memcmp(bufs[2], url_param_name+l1, l2) == 0) |
| 10753 | return pos; |
| 10754 | |
| 10755 | /* Perform wrapping and jump the string who fail the comparison. */ |
| 10756 | bufs += 2; |
| 10757 | pos = bufs[0] + l2; |
| 10758 | last = bufs[1]; |
| 10759 | |
| 10760 | } else { |
| 10761 | /* process a simple comparison. */ |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 10762 | if (memcmp(pos, url_param_name, url_param_name_l) == 0) |
| 10763 | return pos; |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10764 | pos += url_param_name_l + 1; |
| 10765 | if (fix_pointer_if_wrap(chunks, &pos)) |
| 10766 | last = bufs[2]; |
| 10767 | } |
| 10768 | } |
| 10769 | |
| 10770 | while (1) { |
| 10771 | /* Look for the next delimiter. */ |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 10772 | while (pos < last && !is_param_delimiter(*pos, delim)) |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10773 | pos++; |
| 10774 | if (pos < last) |
| 10775 | break; |
| 10776 | /* process buffer wrapping. */ |
| 10777 | if (bufs != chunks || !bufs[2]) |
| 10778 | return NULL; |
| 10779 | bufs += 2; |
| 10780 | pos = bufs[0]; |
| 10781 | last = bufs[1]; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10782 | } |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10783 | pos++; |
| 10784 | } |
| 10785 | return NULL; |
| 10786 | } |
| 10787 | |
| 10788 | /* |
Cyril Bonté | ce1ef4d | 2015-11-26 21:39:56 +0100 | [diff] [blame] | 10789 | * Given a url parameter name and a query string, find the next value. |
| 10790 | * An empty url_param_name matches the first available parameter. |
| 10791 | * If the parameter is found, 1 is returned and *vstart / *vend are updated to |
| 10792 | * respectively provide a pointer to the value and its end. |
| 10793 | * Otherwise, 0 is returned and vstart/vend are not modified. |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10794 | */ |
| 10795 | static int |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10796 | find_next_url_param(const char **chunks, |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 10797 | const char* url_param_name, size_t url_param_name_l, |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10798 | const char **vstart, const char **vend, char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10799 | { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10800 | const char *arg_start, *qs_end; |
| 10801 | const char *value_start, *value_end; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10802 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10803 | arg_start = chunks[0]; |
| 10804 | qs_end = chunks[1]; |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 10805 | if (url_param_name_l) { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10806 | /* Looks for an argument name. */ |
| 10807 | arg_start = find_url_param_pos(chunks, |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 10808 | url_param_name, url_param_name_l, |
| 10809 | delim); |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10810 | /* Check for wrapping. */ |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 10811 | if (arg_start >= qs_end) |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10812 | qs_end = chunks[3]; |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 10813 | } |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10814 | if (!arg_start) |
| 10815 | return 0; |
| 10816 | |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 10817 | if (!url_param_name_l) { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10818 | while (1) { |
| 10819 | /* looks for the first argument. */ |
| 10820 | value_start = memchr(arg_start, '=', qs_end - arg_start); |
| 10821 | if (!value_start) { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10822 | /* Check for wrapping. */ |
| 10823 | if (arg_start >= chunks[0] && |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 10824 | arg_start < chunks[1] && |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10825 | chunks[2]) { |
| 10826 | arg_start = chunks[2]; |
| 10827 | qs_end = chunks[3]; |
| 10828 | continue; |
| 10829 | } |
| 10830 | return 0; |
| 10831 | } |
| 10832 | break; |
| 10833 | } |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 10834 | value_start++; |
| 10835 | } |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10836 | else { |
| 10837 | /* Jump the argument length. */ |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 10838 | value_start = arg_start + url_param_name_l + 1; |
| 10839 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10840 | /* Check for pointer wrapping. */ |
| 10841 | if (fix_pointer_if_wrap(chunks, &value_start)) { |
| 10842 | /* Update the end pointer. */ |
| 10843 | qs_end = chunks[3]; |
| 10844 | |
| 10845 | /* Check for overflow. */ |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 10846 | if (value_start >= qs_end) |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10847 | return 0; |
| 10848 | } |
| 10849 | } |
| 10850 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10851 | value_end = value_start; |
| 10852 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10853 | while (1) { |
| 10854 | while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim)) |
| 10855 | value_end++; |
| 10856 | if (value_end < qs_end) |
| 10857 | break; |
| 10858 | /* process buffer wrapping. */ |
| 10859 | if (value_end >= chunks[0] && |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 10860 | value_end < chunks[1] && |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10861 | chunks[2]) { |
| 10862 | value_end = chunks[2]; |
| 10863 | qs_end = chunks[3]; |
| 10864 | continue; |
| 10865 | } |
| 10866 | break; |
| 10867 | } |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10868 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10869 | *vstart = value_start; |
| 10870 | *vend = value_end; |
Cyril Bonté | ce1ef4d | 2015-11-26 21:39:56 +0100 | [diff] [blame] | 10871 | return 1; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10872 | } |
| 10873 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10874 | /* This scans a URL-encoded query string. It takes an optionally wrapping |
| 10875 | * string whose first contigous chunk has its beginning in ctx->a[0] and end |
| 10876 | * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The |
| 10877 | * pointers are updated for next iteration before leaving. |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 10878 | */ |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10879 | static int |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 10880 | smp_fetch_param(char delim, const char *name, int name_len, const struct arg *args, struct sample *smp, const char *kw, void *private) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10881 | { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10882 | const char *vstart, *vend; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 10883 | struct buffer *temp; |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10884 | const char **chunks = (const char **)smp->ctx.a; |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 10885 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10886 | if (!find_next_url_param(chunks, |
Thierry FOURNIER | 0948d41 | 2015-05-20 15:22:37 +0200 | [diff] [blame] | 10887 | name, name_len, |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10888 | &vstart, &vend, |
Thierry FOURNIER | 0948d41 | 2015-05-20 15:22:37 +0200 | [diff] [blame] | 10889 | delim)) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10890 | return 0; |
| 10891 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10892 | /* Create sample. If the value is contiguous, return the pointer as CONST, |
| 10893 | * if the value is wrapped, copy-it in a buffer. |
| 10894 | */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10895 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10896 | if (chunks[2] && |
| 10897 | vstart >= chunks[0] && vstart <= chunks[1] && |
| 10898 | vend >= chunks[2] && vend <= chunks[3]) { |
| 10899 | /* Wrapped case. */ |
| 10900 | temp = get_trash_chunk(); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 10901 | memcpy(temp->area, vstart, chunks[1] - vstart); |
| 10902 | memcpy(temp->area + ( chunks[1] - vstart ), chunks[2], |
| 10903 | vend - chunks[2]); |
| 10904 | smp->data.u.str.area = temp->area; |
| 10905 | smp->data.u.str.data = ( chunks[1] - vstart ) + ( vend - chunks[2] ); |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10906 | } else { |
| 10907 | /* Contiguous case. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 10908 | smp->data.u.str.area = (char *)vstart; |
| 10909 | smp->data.u.str.data = vend - vstart; |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10910 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
| 10911 | } |
| 10912 | |
| 10913 | /* Update context, check wrapping. */ |
| 10914 | chunks[0] = vend; |
| 10915 | if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) { |
| 10916 | chunks[1] = chunks[3]; |
| 10917 | chunks[2] = NULL; |
| 10918 | } |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 10919 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10920 | if (chunks[0] < chunks[1]) |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 10921 | smp->flags |= SMP_F_NOT_LAST; |
| 10922 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10923 | return 1; |
| 10924 | } |
| 10925 | |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 10926 | /* This function iterates over each parameter of the query string. It uses |
| 10927 | * ctx->a[0] and ctx->a[1] to store the beginning and end of the current |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10928 | * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL. |
| 10929 | * An optional parameter name is passed in args[0], otherwise any parameter is |
| 10930 | * considered. It supports an optional delimiter argument for the beginning of |
| 10931 | * the string in args[1], which defaults to "?". |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 10932 | */ |
| 10933 | static int |
| 10934 | smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 10935 | { |
| 10936 | struct http_msg *msg; |
| 10937 | char delim = '?'; |
| 10938 | const char *name; |
| 10939 | int name_len; |
| 10940 | |
Dragan Dosen | 26f77e5 | 2015-05-25 10:02:11 +0200 | [diff] [blame] | 10941 | if (!args || |
| 10942 | (args[0].type && args[0].type != ARGT_STR) || |
| 10943 | (args[1].type && args[1].type != ARGT_STR)) |
| 10944 | return 0; |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 10945 | |
Dragan Dosen | 26f77e5 | 2015-05-25 10:02:11 +0200 | [diff] [blame] | 10946 | name = ""; |
| 10947 | name_len = 0; |
| 10948 | if (args->type == ARGT_STR) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 10949 | name = args->data.str.area; |
| 10950 | name_len = args->data.str.data; |
Dragan Dosen | 26f77e5 | 2015-05-25 10:02:11 +0200 | [diff] [blame] | 10951 | } |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 10952 | |
Dragan Dosen | 26f77e5 | 2015-05-25 10:02:11 +0200 | [diff] [blame] | 10953 | if (args[1].type) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 10954 | delim = *args[1].data.str.area; |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 10955 | |
Dragan Dosen | 26f77e5 | 2015-05-25 10:02:11 +0200 | [diff] [blame] | 10956 | if (!smp->ctx.a[0]) { // first call, find the query string |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 10957 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10958 | |
| 10959 | msg = &smp->strm->txn->req; |
| 10960 | |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 10961 | smp->ctx.a[0] = find_param_list(ci_head(msg->chn) + msg->sl.rq.u, |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 10962 | msg->sl.rq.u_l, delim); |
| 10963 | if (!smp->ctx.a[0]) |
| 10964 | return 0; |
| 10965 | |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 10966 | smp->ctx.a[1] = ci_head(msg->chn) + msg->sl.rq.u + msg->sl.rq.u_l; |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10967 | |
| 10968 | /* Assume that the context is filled with NULL pointer |
| 10969 | * before the first call. |
| 10970 | * smp->ctx.a[2] = NULL; |
| 10971 | * smp->ctx.a[3] = NULL; |
| 10972 | */ |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 10973 | } |
| 10974 | |
| 10975 | return smp_fetch_param(delim, name, name_len, args, smp, kw, private); |
| 10976 | } |
| 10977 | |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 10978 | /* This function iterates over each parameter of the body. This requires |
| 10979 | * that the body has been waited for using http-buffer-request. It uses |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10980 | * ctx->a[0] and ctx->a[1] to store the beginning and end of the first |
| 10981 | * contigous part of the body, and optionally ctx->a[2..3] to reference the |
| 10982 | * optional second part if the body wraps at the end of the buffer. An optional |
| 10983 | * parameter name is passed in args[0], otherwise any parameter is considered. |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 10984 | */ |
| 10985 | static int |
| 10986 | smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 10987 | { |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 10988 | struct http_msg *msg; |
| 10989 | unsigned long len; |
| 10990 | unsigned long block1; |
| 10991 | char *body; |
| 10992 | const char *name; |
| 10993 | int name_len; |
| 10994 | |
| 10995 | if (!args || (args[0].type && args[0].type != ARGT_STR)) |
| 10996 | return 0; |
| 10997 | |
| 10998 | name = ""; |
| 10999 | name_len = 0; |
| 11000 | if (args[0].type == ARGT_STR) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 11001 | name = args[0].data.str.area; |
| 11002 | name_len = args[0].data.str.data; |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 11003 | } |
| 11004 | |
| 11005 | if (!smp->ctx.a[0]) { // first call, find the query string |
| 11006 | CHECK_HTTP_MESSAGE_FIRST(); |
| 11007 | |
| 11008 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 11009 | msg = &smp->strm->txn->req; |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 11010 | else |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 11011 | msg = &smp->strm->txn->rsp; |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 11012 | |
| 11013 | len = http_body_bytes(msg); |
Willy Tarreau | 188e230 | 2018-06-15 11:11:53 +0200 | [diff] [blame] | 11014 | body = c_ptr(msg->chn, -http_data_rewind(msg)); |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 11015 | |
| 11016 | block1 = len; |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 11017 | if (block1 > b_wrap(&msg->chn->buf) - body) |
| 11018 | block1 = b_wrap(&msg->chn->buf) - body; |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 11019 | |
| 11020 | if (block1 == len) { |
| 11021 | /* buffer is not wrapped (or empty) */ |
| 11022 | smp->ctx.a[0] = body; |
| 11023 | smp->ctx.a[1] = body + len; |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11024 | |
| 11025 | /* Assume that the context is filled with NULL pointer |
| 11026 | * before the first call. |
| 11027 | * smp->ctx.a[2] = NULL; |
| 11028 | * smp->ctx.a[3] = NULL; |
| 11029 | */ |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 11030 | } |
| 11031 | else { |
| 11032 | /* buffer is wrapped, we need to defragment it */ |
| 11033 | smp->ctx.a[0] = body; |
| 11034 | smp->ctx.a[1] = body + block1; |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 11035 | smp->ctx.a[2] = b_orig(&msg->chn->buf); |
| 11036 | smp->ctx.a[3] = b_orig(&msg->chn->buf) + ( len - block1 ); |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 11037 | } |
| 11038 | } |
| 11039 | return smp_fetch_param('&', name, name_len, args, smp, kw, private); |
| 11040 | } |
| 11041 | |
Willy Tarreau | a9fddca | 2012-07-31 07:51:48 +0200 | [diff] [blame] | 11042 | /* Return the signed integer value for the specified url parameter (see url_param |
| 11043 | * above). |
| 11044 | */ |
| 11045 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11046 | smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | a9fddca | 2012-07-31 07:51:48 +0200 | [diff] [blame] | 11047 | { |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11048 | int ret = smp_fetch_url_param(args, smp, kw, private); |
Willy Tarreau | a9fddca | 2012-07-31 07:51:48 +0200 | [diff] [blame] | 11049 | |
| 11050 | if (ret > 0) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11051 | smp->data.type = SMP_T_SINT; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 11052 | smp->data.u.sint = strl2ic(smp->data.u.str.area, |
| 11053 | smp->data.u.str.data); |
Willy Tarreau | a9fddca | 2012-07-31 07:51:48 +0200 | [diff] [blame] | 11054 | } |
| 11055 | |
| 11056 | return ret; |
| 11057 | } |
| 11058 | |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11059 | /* This produces a 32-bit hash of the concatenation of the first occurrence of |
| 11060 | * the Host header followed by the path component if it begins with a slash ('/'). |
| 11061 | * This means that '*' will not be added, resulting in exactly the first Host |
| 11062 | * entry. If no Host header is found, then the path is used. The resulting value |
| 11063 | * is hashed using the url hash followed by a full avalanche hash and provides a |
| 11064 | * 32-bit integer value. This fetch is useful for tracking per-URL activity on |
| 11065 | * high-traffic sites without having to store whole paths. |
| 11066 | * this differs from the base32 functions in that it includes the url parameters |
| 11067 | * as well as the path |
| 11068 | */ |
| 11069 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11070 | smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11071 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11072 | struct http_txn *txn; |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11073 | struct hdr_ctx ctx; |
| 11074 | unsigned int hash = 0; |
| 11075 | char *ptr, *beg, *end; |
| 11076 | int len; |
| 11077 | |
| 11078 | CHECK_HTTP_MESSAGE_FIRST(); |
| 11079 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11080 | txn = smp->strm->txn; |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11081 | ctx.idx = 0; |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 11082 | if (http_find_header2("Host", 4, ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) { |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11083 | /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */ |
| 11084 | ptr = ctx.line + ctx.val; |
| 11085 | len = ctx.vlen; |
| 11086 | while (len--) |
| 11087 | hash = *(ptr++) + (hash << 6) + (hash << 16) - hash; |
| 11088 | } |
| 11089 | |
| 11090 | /* now retrieve the path */ |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 11091 | end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l; |
Willy Tarreau | 6b952c8 | 2018-09-10 17:45:34 +0200 | [diff] [blame] | 11092 | beg = http_txn_get_path(txn); |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11093 | if (!beg) |
| 11094 | beg = end; |
| 11095 | |
| 11096 | for (ptr = beg; ptr < end ; ptr++); |
| 11097 | |
| 11098 | if (beg < ptr && *beg == '/') { |
| 11099 | while (beg < ptr) |
| 11100 | hash = *(beg++) + (hash << 6) + (hash << 16) - hash; |
| 11101 | } |
| 11102 | hash = full_hash(hash); |
| 11103 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11104 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11105 | smp->data.u.sint = hash; |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11106 | smp->flags = SMP_F_VOL_1ST; |
| 11107 | return 1; |
| 11108 | } |
| 11109 | |
| 11110 | /* This concatenates the source address with the 32-bit hash of the Host and |
| 11111 | * URL as returned by smp_fetch_base32(). The idea is to have per-source and |
| 11112 | * per-url counters. The result is a binary block from 8 to 20 bytes depending |
| 11113 | * on the source address length. The URL hash is stored before the address so |
| 11114 | * that in environments where IPv6 is insignificant, truncating the output to |
| 11115 | * 8 bytes would still work. |
| 11116 | */ |
| 11117 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11118 | smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11119 | { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 11120 | struct buffer *temp; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11121 | struct connection *cli_conn = objt_conn(smp->sess->origin); |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11122 | |
Dragan Dosen | db5af61 | 2016-06-16 11:23:01 +0200 | [diff] [blame] | 11123 | if (!cli_conn) |
| 11124 | return 0; |
| 11125 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11126 | if (!smp_fetch_url32(args, smp, kw, private)) |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11127 | return 0; |
| 11128 | |
| 11129 | temp = get_trash_chunk(); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 11130 | *(unsigned int *) temp->area = htonl(smp->data.u.sint); |
| 11131 | temp->data += sizeof(unsigned int); |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11132 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 11133 | switch (cli_conn->addr.from.ss_family) { |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11134 | case AF_INET: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 11135 | memcpy(temp->area + temp->data, |
| 11136 | &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, |
| 11137 | 4); |
| 11138 | temp->data += 4; |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11139 | break; |
| 11140 | case AF_INET6: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 11141 | memcpy(temp->area + temp->data, |
| 11142 | &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, |
| 11143 | 16); |
| 11144 | temp->data += 16; |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11145 | break; |
| 11146 | default: |
| 11147 | return 0; |
| 11148 | } |
| 11149 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11150 | smp->data.u.str = *temp; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11151 | smp->data.type = SMP_T_BIN; |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11152 | return 1; |
| 11153 | } |
| 11154 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 11155 | /* This function is used to validate the arguments passed to any "hdr" fetch |
| 11156 | * keyword. These keywords support an optional positive or negative occurrence |
| 11157 | * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It |
| 11158 | * is assumed that the types are already the correct ones. Returns 0 on error, |
| 11159 | * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an |
| 11160 | * error message in case of error, that the caller is responsible for freeing. |
| 11161 | * The initial location must either be freeable or NULL. |
| 11162 | */ |
Thierry FOURNIER | 49f45af | 2014-12-08 19:50:43 +0100 | [diff] [blame] | 11163 | int val_hdr(struct arg *arg, char **err_msg) |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 11164 | { |
| 11165 | if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 11166 | memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY); |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 11167 | return 0; |
| 11168 | } |
| 11169 | return 1; |
| 11170 | } |
| 11171 | |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 11172 | /* takes an UINT value on input supposed to represent the time since EPOCH, |
| 11173 | * adds an optional offset found in args[0] and emits a string representing |
| 11174 | * the date in RFC-1123/5322 format. |
| 11175 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11176 | static int sample_conv_http_date(const struct arg *args, struct sample *smp, void *private) |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 11177 | { |
Cyril Bonté | f78d896 | 2016-01-22 19:40:28 +0100 | [diff] [blame] | 11178 | const char day[7][4] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 11179 | const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 11180 | struct buffer *temp; |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 11181 | struct tm *tm; |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 11182 | /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11183 | time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL; |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 11184 | |
| 11185 | /* add offset */ |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 11186 | if (args && (args[0].type == ARGT_SINT)) |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 11187 | curr_date += args[0].data.sint; |
| 11188 | |
| 11189 | tm = gmtime(&curr_date); |
Thierry FOURNIER | fac9ccf | 2015-07-08 00:15:20 +0200 | [diff] [blame] | 11190 | if (!tm) |
| 11191 | return 0; |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 11192 | |
| 11193 | temp = get_trash_chunk(); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 11194 | temp->data = snprintf(temp->area, temp->size - temp->data, |
| 11195 | "%s, %02d %s %04d %02d:%02d:%02d GMT", |
| 11196 | day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], |
| 11197 | 1900+tm->tm_year, |
| 11198 | tm->tm_hour, tm->tm_min, tm->tm_sec); |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 11199 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11200 | smp->data.u.str = *temp; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11201 | smp->data.type = SMP_T_STR; |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 11202 | return 1; |
| 11203 | } |
| 11204 | |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11205 | /* Match language range with language tag. RFC2616 14.4: |
| 11206 | * |
| 11207 | * A language-range matches a language-tag if it exactly equals |
| 11208 | * the tag, or if it exactly equals a prefix of the tag such |
| 11209 | * that the first tag character following the prefix is "-". |
| 11210 | * |
| 11211 | * Return 1 if the strings match, else return 0. |
| 11212 | */ |
| 11213 | static inline int language_range_match(const char *range, int range_len, |
| 11214 | const char *tag, int tag_len) |
| 11215 | { |
| 11216 | const char *end = range + range_len; |
| 11217 | const char *tend = tag + tag_len; |
| 11218 | while (range < end) { |
| 11219 | if (*range == '-' && tag == tend) |
| 11220 | return 1; |
| 11221 | if (*range != *tag || tag == tend) |
| 11222 | return 0; |
| 11223 | range++; |
| 11224 | tag++; |
| 11225 | } |
| 11226 | /* Return true only if the last char of the tag is matched. */ |
| 11227 | return tag == tend; |
| 11228 | } |
| 11229 | |
| 11230 | /* Arguments: The list of expected value, the number of parts returned and the separator */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11231 | static int sample_conv_q_prefered(const struct arg *args, struct sample *smp, void *private) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11232 | { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 11233 | const char *al = smp->data.u.str.area; |
| 11234 | const char *end = al + smp->data.u.str.data; |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11235 | const char *token; |
| 11236 | int toklen; |
| 11237 | int qvalue; |
| 11238 | const char *str; |
| 11239 | const char *w; |
| 11240 | int best_q = 0; |
| 11241 | |
| 11242 | /* Set the constant to the sample, because the output of the |
| 11243 | * function will be peek in the constant configuration string. |
| 11244 | */ |
| 11245 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11246 | smp->data.u.str.size = 0; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 11247 | smp->data.u.str.area = ""; |
| 11248 | smp->data.u.str.data = 0; |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11249 | |
| 11250 | /* Parse the accept language */ |
| 11251 | while (1) { |
| 11252 | |
| 11253 | /* Jump spaces, quit if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 11254 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11255 | al++; |
| 11256 | if (al >= end) |
| 11257 | break; |
| 11258 | |
| 11259 | /* Start of the fisrt word. */ |
| 11260 | token = al; |
| 11261 | |
| 11262 | /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 11263 | while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11264 | al++; |
| 11265 | if (al == token) |
| 11266 | goto expect_comma; |
| 11267 | |
| 11268 | /* Length of the token. */ |
| 11269 | toklen = al - token; |
| 11270 | qvalue = 1000; |
| 11271 | |
| 11272 | /* Check if the token exists in the list. If the token not exists, |
| 11273 | * jump to the next token. |
| 11274 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 11275 | str = args[0].data.str.area; |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11276 | w = str; |
| 11277 | while (1) { |
| 11278 | if (*str == ';' || *str == '\0') { |
| 11279 | if (language_range_match(token, toklen, w, str-w)) |
| 11280 | goto look_for_q; |
| 11281 | if (*str == '\0') |
| 11282 | goto expect_comma; |
| 11283 | w = str + 1; |
| 11284 | } |
| 11285 | str++; |
| 11286 | } |
| 11287 | goto expect_comma; |
| 11288 | |
| 11289 | look_for_q: |
| 11290 | |
| 11291 | /* Jump spaces, quit if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 11292 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11293 | al++; |
| 11294 | if (al >= end) |
| 11295 | goto process_value; |
| 11296 | |
| 11297 | /* If ',' is found, process the result */ |
| 11298 | if (*al == ',') |
| 11299 | goto process_value; |
| 11300 | |
| 11301 | /* If the character is different from ';', look |
| 11302 | * for the end of the header part in best effort. |
| 11303 | */ |
| 11304 | if (*al != ';') |
| 11305 | goto expect_comma; |
| 11306 | |
| 11307 | /* Assumes that the char is ';', now expect "q=". */ |
| 11308 | al++; |
| 11309 | |
| 11310 | /* Jump spaces, process value if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 11311 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11312 | al++; |
| 11313 | if (al >= end) |
| 11314 | goto process_value; |
| 11315 | |
| 11316 | /* Expect 'q'. If no 'q', continue in best effort */ |
| 11317 | if (*al != 'q') |
| 11318 | goto process_value; |
| 11319 | al++; |
| 11320 | |
| 11321 | /* Jump spaces, process value if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 11322 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11323 | al++; |
| 11324 | if (al >= end) |
| 11325 | goto process_value; |
| 11326 | |
| 11327 | /* Expect '='. If no '=', continue in best effort */ |
| 11328 | if (*al != '=') |
| 11329 | goto process_value; |
| 11330 | al++; |
| 11331 | |
| 11332 | /* Jump spaces, process value if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 11333 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11334 | al++; |
| 11335 | if (al >= end) |
| 11336 | goto process_value; |
| 11337 | |
| 11338 | /* Parse the q value. */ |
| 11339 | qvalue = parse_qvalue(al, &al); |
| 11340 | |
| 11341 | process_value: |
| 11342 | |
| 11343 | /* If the new q value is the best q value, then store the associated |
| 11344 | * language in the response. If qvalue is the biggest value (1000), |
| 11345 | * break the process. |
| 11346 | */ |
| 11347 | if (qvalue > best_q) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 11348 | smp->data.u.str.area = (char *)w; |
| 11349 | smp->data.u.str.data = str - w; |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11350 | if (qvalue >= 1000) |
| 11351 | break; |
| 11352 | best_q = qvalue; |
| 11353 | } |
| 11354 | |
| 11355 | expect_comma: |
| 11356 | |
| 11357 | /* Expect comma or end. If the end is detected, quit the loop. */ |
| 11358 | while (al < end && *al != ',') |
| 11359 | al++; |
| 11360 | if (al >= end) |
| 11361 | break; |
| 11362 | |
| 11363 | /* Comma is found, jump it and restart the analyzer. */ |
| 11364 | al++; |
| 11365 | } |
| 11366 | |
| 11367 | /* Set default value if required. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 11368 | if (smp->data.u.str.data == 0 && args[1].type == ARGT_STR) { |
| 11369 | smp->data.u.str.area = args[1].data.str.area; |
| 11370 | smp->data.u.str.data = args[1].data.str.data; |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11371 | } |
| 11372 | |
| 11373 | /* Return true only if a matching language was found. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 11374 | return smp->data.u.str.data != 0; |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11375 | } |
| 11376 | |
Thierry FOURNIER | 82ff3c9 | 2015-05-07 15:46:20 +0200 | [diff] [blame] | 11377 | /* This fetch url-decode any input string. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11378 | static int sample_conv_url_dec(const struct arg *args, struct sample *smp, void *private) |
Thierry FOURNIER | 82ff3c9 | 2015-05-07 15:46:20 +0200 | [diff] [blame] | 11379 | { |
Willy Tarreau | 9c768fd | 2018-08-22 05:08:57 +0200 | [diff] [blame] | 11380 | int len; |
| 11381 | |
Thierry FOURNIER | 82ff3c9 | 2015-05-07 15:46:20 +0200 | [diff] [blame] | 11382 | /* If the constant flag is set or if not size is avalaible at |
| 11383 | * the end of the buffer, copy the string in other buffer |
| 11384 | * before decoding. |
| 11385 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 11386 | if (smp->flags & SMP_F_CONST || smp->data.u.str.size <= smp->data.u.str.data) { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 11387 | struct buffer *str = get_trash_chunk(); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 11388 | memcpy(str->area, smp->data.u.str.area, smp->data.u.str.data); |
| 11389 | smp->data.u.str.area = str->area; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11390 | smp->data.u.str.size = str->size; |
Thierry FOURNIER | 82ff3c9 | 2015-05-07 15:46:20 +0200 | [diff] [blame] | 11391 | smp->flags &= ~SMP_F_CONST; |
| 11392 | } |
| 11393 | |
| 11394 | /* Add final \0 required by url_decode(), and convert the input string. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 11395 | smp->data.u.str.area[smp->data.u.str.data] = '\0'; |
Willy Tarreau | 9c768fd | 2018-08-22 05:08:57 +0200 | [diff] [blame] | 11396 | len = url_decode(smp->data.u.str.area); |
| 11397 | if (len < 0) |
| 11398 | return 0; |
| 11399 | smp->data.u.str.data = len; |
| 11400 | return 1; |
Thierry FOURNIER | 82ff3c9 | 2015-05-07 15:46:20 +0200 | [diff] [blame] | 11401 | } |
| 11402 | |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 11403 | static int smp_conv_req_capture(const struct arg *args, struct sample *smp, void *private) |
| 11404 | { |
| 11405 | struct proxy *fe = strm_fe(smp->strm); |
| 11406 | int idx, i; |
| 11407 | struct cap_hdr *hdr; |
| 11408 | int len; |
| 11409 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 11410 | if (!args || args->type != ARGT_SINT) |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 11411 | return 0; |
| 11412 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 11413 | idx = args->data.sint; |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 11414 | |
| 11415 | /* Check the availibity of the capture id. */ |
| 11416 | if (idx > fe->nb_req_cap - 1) |
| 11417 | return 0; |
| 11418 | |
| 11419 | /* Look for the original configuration. */ |
| 11420 | for (hdr = fe->req_cap, i = fe->nb_req_cap - 1; |
| 11421 | hdr != NULL && i != idx ; |
| 11422 | i--, hdr = hdr->next); |
| 11423 | if (!hdr) |
| 11424 | return 0; |
| 11425 | |
| 11426 | /* check for the memory allocation */ |
| 11427 | if (smp->strm->req_cap[hdr->index] == NULL) |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 11428 | smp->strm->req_cap[hdr->index] = pool_alloc(hdr->pool); |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 11429 | if (smp->strm->req_cap[hdr->index] == NULL) |
| 11430 | return 0; |
| 11431 | |
| 11432 | /* Check length. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 11433 | len = smp->data.u.str.data; |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 11434 | if (len > hdr->len) |
| 11435 | len = hdr->len; |
| 11436 | |
| 11437 | /* Capture input data. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 11438 | memcpy(smp->strm->req_cap[idx], smp->data.u.str.area, len); |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 11439 | smp->strm->req_cap[idx][len] = '\0'; |
| 11440 | |
| 11441 | return 1; |
| 11442 | } |
| 11443 | |
| 11444 | static int smp_conv_res_capture(const struct arg *args, struct sample *smp, void *private) |
| 11445 | { |
| 11446 | struct proxy *fe = strm_fe(smp->strm); |
| 11447 | int idx, i; |
| 11448 | struct cap_hdr *hdr; |
| 11449 | int len; |
| 11450 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 11451 | if (!args || args->type != ARGT_SINT) |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 11452 | return 0; |
| 11453 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 11454 | idx = args->data.sint; |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 11455 | |
| 11456 | /* Check the availibity of the capture id. */ |
| 11457 | if (idx > fe->nb_rsp_cap - 1) |
| 11458 | return 0; |
| 11459 | |
| 11460 | /* Look for the original configuration. */ |
| 11461 | for (hdr = fe->rsp_cap, i = fe->nb_rsp_cap - 1; |
| 11462 | hdr != NULL && i != idx ; |
| 11463 | i--, hdr = hdr->next); |
| 11464 | if (!hdr) |
| 11465 | return 0; |
| 11466 | |
| 11467 | /* check for the memory allocation */ |
| 11468 | if (smp->strm->res_cap[hdr->index] == NULL) |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 11469 | smp->strm->res_cap[hdr->index] = pool_alloc(hdr->pool); |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 11470 | if (smp->strm->res_cap[hdr->index] == NULL) |
| 11471 | return 0; |
| 11472 | |
| 11473 | /* Check length. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 11474 | len = smp->data.u.str.data; |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 11475 | if (len > hdr->len) |
| 11476 | len = hdr->len; |
| 11477 | |
| 11478 | /* Capture input data. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 11479 | memcpy(smp->strm->res_cap[idx], smp->data.u.str.area, len); |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 11480 | smp->strm->res_cap[idx][len] = '\0'; |
| 11481 | |
| 11482 | return 1; |
| 11483 | } |
| 11484 | |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11485 | /* This function executes one of the set-{method,path,query,uri} actions. It |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11486 | * takes the string from the variable 'replace' with length 'len', then modifies |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11487 | * the relevant part of the request line accordingly. Then it updates various |
| 11488 | * pointers to the next elements which were moved, and the total buffer length. |
| 11489 | * It finds the action to be performed in p[2], previously filled by function |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11490 | * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal |
| 11491 | * error, though this can be revisited when this code is finally exploited. |
| 11492 | * |
| 11493 | * 'action' can be '0' to replace method, '1' to replace path, '2' to replace |
| 11494 | * query string and 3 to replace uri. |
| 11495 | * |
| 11496 | * In query string case, the mark question '?' must be set at the start of the |
| 11497 | * string by the caller, event if the replacement query string is empty. |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11498 | */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11499 | int http_replace_req_line(int action, const char *replace, int len, |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 11500 | struct proxy *px, struct stream *s) |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11501 | { |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 11502 | struct http_txn *txn = s->txn; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11503 | char *cur_ptr, *cur_end; |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11504 | int offset = 0; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11505 | int delta; |
| 11506 | |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11507 | switch (action) { |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11508 | case 0: // method |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 11509 | cur_ptr = ci_head(&s->req); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11510 | cur_end = cur_ptr + txn->req.sl.rq.m_l; |
| 11511 | |
| 11512 | /* adjust req line offsets and lengths */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11513 | delta = len - offset - (cur_end - cur_ptr); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11514 | txn->req.sl.rq.m_l += delta; |
| 11515 | txn->req.sl.rq.u += delta; |
| 11516 | txn->req.sl.rq.v += delta; |
| 11517 | break; |
| 11518 | |
| 11519 | case 1: // path |
Willy Tarreau | 6b952c8 | 2018-09-10 17:45:34 +0200 | [diff] [blame] | 11520 | cur_ptr = http_txn_get_path(txn); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11521 | if (!cur_ptr) |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 11522 | cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11523 | |
| 11524 | cur_end = cur_ptr; |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 11525 | while (cur_end < ci_head(&s->req) + txn->req.sl.rq.u + txn->req.sl.rq.u_l && *cur_end != '?') |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11526 | cur_end++; |
| 11527 | |
| 11528 | /* adjust req line offsets and lengths */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11529 | delta = len - offset - (cur_end - cur_ptr); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11530 | txn->req.sl.rq.u_l += delta; |
| 11531 | txn->req.sl.rq.v += delta; |
| 11532 | break; |
| 11533 | |
| 11534 | case 2: // query |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11535 | offset = 1; |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 11536 | cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11537 | cur_end = cur_ptr + txn->req.sl.rq.u_l; |
| 11538 | while (cur_ptr < cur_end && *cur_ptr != '?') |
| 11539 | cur_ptr++; |
| 11540 | |
| 11541 | /* skip the question mark or indicate that we must insert it |
| 11542 | * (but only if the format string is not empty then). |
| 11543 | */ |
| 11544 | if (cur_ptr < cur_end) |
| 11545 | cur_ptr++; |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11546 | else if (len > 1) |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11547 | offset = 0; |
| 11548 | |
| 11549 | /* adjust req line offsets and lengths */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11550 | delta = len - offset - (cur_end - cur_ptr); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11551 | txn->req.sl.rq.u_l += delta; |
| 11552 | txn->req.sl.rq.v += delta; |
| 11553 | break; |
| 11554 | |
| 11555 | case 3: // uri |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 11556 | cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11557 | cur_end = cur_ptr + txn->req.sl.rq.u_l; |
| 11558 | |
| 11559 | /* adjust req line offsets and lengths */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11560 | delta = len - offset - (cur_end - cur_ptr); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11561 | txn->req.sl.rq.u_l += delta; |
| 11562 | txn->req.sl.rq.v += delta; |
| 11563 | break; |
| 11564 | |
| 11565 | default: |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11566 | return -1; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11567 | } |
| 11568 | |
| 11569 | /* commit changes and adjust end of message */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 11570 | delta = b_rep_blk(&s->req.buf, cur_ptr, cur_end, replace + offset, len - offset); |
Thierry FOURNIER | 7f6192c | 2015-04-26 18:01:40 +0200 | [diff] [blame] | 11571 | txn->req.sl.rq.l += delta; |
| 11572 | txn->hdr_idx.v[0].len += delta; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11573 | http_msg_move_end(&txn->req, delta); |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11574 | return 0; |
| 11575 | } |
| 11576 | |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 11577 | /* This function replace the HTTP status code and the associated message. The |
| 11578 | * variable <status> contains the new status code. This function never fails. |
| 11579 | */ |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 11580 | void http_set_status(unsigned int status, const char *reason, struct stream *s) |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 11581 | { |
| 11582 | struct http_txn *txn = s->txn; |
| 11583 | char *cur_ptr, *cur_end; |
| 11584 | int delta; |
| 11585 | char *res; |
| 11586 | int c_l; |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 11587 | const char *msg = reason; |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 11588 | int msg_len; |
| 11589 | |
| 11590 | chunk_reset(&trash); |
| 11591 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 11592 | res = ultoa_o(status, trash.area, trash.size); |
| 11593 | c_l = res - trash.area; |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 11594 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 11595 | trash.area[c_l] = ' '; |
| 11596 | trash.data = c_l + 1; |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 11597 | |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 11598 | /* Do we have a custom reason format string? */ |
| 11599 | if (msg == NULL) |
Willy Tarreau | 04f1e2d | 2018-09-10 18:04:24 +0200 | [diff] [blame^] | 11600 | msg = http_get_reason(status); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 11601 | msg_len = strlen(msg); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 11602 | strncpy(&trash.area[trash.data], msg, trash.size - trash.data); |
| 11603 | trash.data += msg_len; |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 11604 | |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 11605 | cur_ptr = ci_head(&s->res) + txn->rsp.sl.st.c; |
| 11606 | cur_end = ci_head(&s->res) + txn->rsp.sl.st.r + txn->rsp.sl.st.r_l; |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 11607 | |
| 11608 | /* commit changes and adjust message */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 11609 | delta = b_rep_blk(&s->res.buf, cur_ptr, cur_end, trash.area, |
| 11610 | trash.data); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 11611 | |
| 11612 | /* adjust res line offsets and lengths */ |
| 11613 | txn->rsp.sl.st.r += c_l - txn->rsp.sl.st.c_l; |
| 11614 | txn->rsp.sl.st.c_l = c_l; |
| 11615 | txn->rsp.sl.st.r_l = msg_len; |
| 11616 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 11617 | delta = trash.data - (cur_end - cur_ptr); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 11618 | txn->rsp.sl.st.l += delta; |
| 11619 | txn->hdr_idx.v[0].len += delta; |
| 11620 | http_msg_move_end(&txn->rsp, delta); |
| 11621 | } |
| 11622 | |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11623 | /* This function executes one of the set-{method,path,query,uri} actions. It |
| 11624 | * builds a string in the trash from the specified format string. It finds |
Thierry FOURNIER | 3f4bc65 | 2015-08-26 16:23:34 +0200 | [diff] [blame] | 11625 | * the action to be performed in <http.action>, previously filled by function |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11626 | * parse_set_req_line(). The replacement action is excuted by the function |
Thierry FOURNIER | 3f4bc65 | 2015-08-26 16:23:34 +0200 | [diff] [blame] | 11627 | * http_action_set_req_line(). It always returns ACT_RET_CONT. If an error |
| 11628 | * occurs the action is canceled, but the rule processing continue. |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11629 | */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 11630 | enum act_return http_action_set_req_line(struct act_rule *rule, struct proxy *px, |
Willy Tarreau | 658b85b | 2015-09-27 10:00:49 +0200 | [diff] [blame] | 11631 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11632 | { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 11633 | struct buffer *replace; |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 11634 | enum act_return ret = ACT_RET_ERR; |
| 11635 | |
| 11636 | replace = alloc_trash_chunk(); |
| 11637 | if (!replace) |
| 11638 | goto leave; |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11639 | |
| 11640 | /* If we have to create a query string, prepare a '?'. */ |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 11641 | if (rule->arg.http.action == 2) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 11642 | replace->area[replace->data++] = '?'; |
| 11643 | replace->data += build_logline(s, replace->area + replace->data, |
| 11644 | replace->size - replace->data, |
| 11645 | &rule->arg.http.logfmt); |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11646 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 11647 | http_replace_req_line(rule->arg.http.action, replace->area, |
| 11648 | replace->data, px, s); |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 11649 | |
| 11650 | ret = ACT_RET_CONT; |
| 11651 | |
| 11652 | leave: |
| 11653 | free_trash_chunk(replace); |
| 11654 | return ret; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11655 | } |
| 11656 | |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 11657 | /* This function is just a compliant action wrapper for "set-status". */ |
| 11658 | enum act_return action_http_set_status(struct act_rule *rule, struct proxy *px, |
Willy Tarreau | 658b85b | 2015-09-27 10:00:49 +0200 | [diff] [blame] | 11659 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 11660 | { |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 11661 | http_set_status(rule->arg.status.code, rule->arg.status.reason, s); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 11662 | return ACT_RET_CONT; |
| 11663 | } |
| 11664 | |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11665 | /* parse an http-request action among : |
| 11666 | * set-method |
| 11667 | * set-path |
| 11668 | * set-query |
| 11669 | * set-uri |
| 11670 | * |
| 11671 | * All of them accept a single argument of type string representing a log-format. |
| 11672 | * The resulting rule makes use of arg->act.p[0..1] to store the log-format list |
| 11673 | * head, and p[2] to store the action as an int (0=method, 1=path, 2=query, 3=uri). |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 11674 | * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error. |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11675 | */ |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 11676 | enum act_parse_ret parse_set_req_line(const char **args, int *orig_arg, struct proxy *px, |
| 11677 | struct act_rule *rule, char **err) |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11678 | { |
| 11679 | int cur_arg = *orig_arg; |
| 11680 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 11681 | rule->action = ACT_CUSTOM; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11682 | |
| 11683 | switch (args[0][4]) { |
| 11684 | case 'm' : |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 11685 | rule->arg.http.action = 0; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11686 | rule->action_ptr = http_action_set_req_line; |
| 11687 | break; |
| 11688 | case 'p' : |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 11689 | rule->arg.http.action = 1; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11690 | rule->action_ptr = http_action_set_req_line; |
| 11691 | break; |
| 11692 | case 'q' : |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 11693 | rule->arg.http.action = 2; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11694 | rule->action_ptr = http_action_set_req_line; |
| 11695 | break; |
| 11696 | case 'u' : |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 11697 | rule->arg.http.action = 3; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11698 | rule->action_ptr = http_action_set_req_line; |
| 11699 | break; |
| 11700 | default: |
| 11701 | memprintf(err, "internal error: unhandled action '%s'", args[0]); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 11702 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11703 | } |
| 11704 | |
| 11705 | if (!*args[cur_arg] || |
| 11706 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 11707 | memprintf(err, "expects exactly 1 argument <format>"); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 11708 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11709 | } |
| 11710 | |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 11711 | LIST_INIT(&rule->arg.http.logfmt); |
Olivier Houchard | fbc74e8 | 2017-11-24 16:54:05 +0100 | [diff] [blame] | 11712 | px->conf.args.ctx = ARGC_HRQ; |
| 11713 | if (!parse_logformat_string(args[cur_arg], px, &rule->arg.http.logfmt, LOG_OPT_HTTP, |
| 11714 | (px->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, err)) { |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 11715 | return ACT_RET_PRS_ERR; |
| 11716 | } |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11717 | |
| 11718 | (*orig_arg)++; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 11719 | return ACT_RET_PRS_OK; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11720 | } |
| 11721 | |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 11722 | /* parse set-status action: |
| 11723 | * This action accepts a single argument of type int representing |
| 11724 | * an http status code. It returns ACT_RET_PRS_OK on success, |
| 11725 | * ACT_RET_PRS_ERR on error. |
| 11726 | */ |
| 11727 | enum act_parse_ret parse_http_set_status(const char **args, int *orig_arg, struct proxy *px, |
| 11728 | struct act_rule *rule, char **err) |
| 11729 | { |
| 11730 | char *error; |
| 11731 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 11732 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 11733 | rule->action_ptr = action_http_set_status; |
| 11734 | |
| 11735 | /* Check if an argument is available */ |
| 11736 | if (!*args[*orig_arg]) { |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 11737 | memprintf(err, "expects 1 argument: <status>; or 3 arguments: <status> reason <fmt>"); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 11738 | return ACT_RET_PRS_ERR; |
| 11739 | } |
| 11740 | |
| 11741 | /* convert status code as integer */ |
| 11742 | rule->arg.status.code = strtol(args[*orig_arg], &error, 10); |
| 11743 | if (*error != '\0' || rule->arg.status.code < 100 || rule->arg.status.code > 999) { |
| 11744 | memprintf(err, "expects an integer status code between 100 and 999"); |
| 11745 | return ACT_RET_PRS_ERR; |
| 11746 | } |
| 11747 | |
| 11748 | (*orig_arg)++; |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 11749 | |
| 11750 | /* set custom reason string */ |
| 11751 | rule->arg.status.reason = NULL; // If null, we use the default reason for the status code. |
| 11752 | if (*args[*orig_arg] && strcmp(args[*orig_arg], "reason") == 0 && |
| 11753 | (*args[*orig_arg + 1] && strcmp(args[*orig_arg + 1], "if") != 0 && strcmp(args[*orig_arg + 1], "unless") != 0)) { |
| 11754 | (*orig_arg)++; |
| 11755 | rule->arg.status.reason = strdup(args[*orig_arg]); |
| 11756 | (*orig_arg)++; |
| 11757 | } |
| 11758 | |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 11759 | return ACT_RET_PRS_OK; |
| 11760 | } |
| 11761 | |
Willy Tarreau | 53275e8 | 2017-11-24 07:52:01 +0100 | [diff] [blame] | 11762 | /* This function executes the "reject" HTTP action. It clears the request and |
| 11763 | * response buffer without sending any response. It can be useful as an HTTP |
| 11764 | * alternative to the silent-drop action to defend against DoS attacks, and may |
| 11765 | * also be used with HTTP/2 to close a connection instead of just a stream. |
| 11766 | * The txn status is unchanged, indicating no response was sent. The termination |
| 11767 | * flags will indicate "PR". It always returns ACT_RET_STOP. |
| 11768 | */ |
| 11769 | enum act_return http_action_reject(struct act_rule *rule, struct proxy *px, |
| 11770 | struct session *sess, struct stream *s, int flags) |
| 11771 | { |
| 11772 | channel_abort(&s->req); |
| 11773 | channel_abort(&s->res); |
| 11774 | s->req.analysers = 0; |
| 11775 | s->res.analysers = 0; |
| 11776 | |
| 11777 | HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1); |
| 11778 | HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1); |
| 11779 | if (sess->listener && sess->listener->counters) |
| 11780 | HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1); |
| 11781 | |
| 11782 | if (!(s->flags & SF_ERR_MASK)) |
| 11783 | s->flags |= SF_ERR_PRXCOND; |
| 11784 | if (!(s->flags & SF_FINST_MASK)) |
| 11785 | s->flags |= SF_FINST_R; |
| 11786 | |
| 11787 | return ACT_RET_CONT; |
| 11788 | } |
| 11789 | |
| 11790 | /* parse the "reject" action: |
| 11791 | * This action takes no argument and returns ACT_RET_PRS_OK on success, |
| 11792 | * ACT_RET_PRS_ERR on error. |
| 11793 | */ |
| 11794 | enum act_parse_ret parse_http_action_reject(const char **args, int *orig_arg, struct proxy *px, |
| 11795 | struct act_rule *rule, char **err) |
| 11796 | { |
| 11797 | rule->action = ACT_CUSTOM; |
| 11798 | rule->action_ptr = http_action_reject; |
| 11799 | return ACT_RET_PRS_OK; |
| 11800 | } |
| 11801 | |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 11802 | /* This function executes the "capture" action. It executes a fetch expression, |
| 11803 | * turns the result into a string and puts it in a capture slot. It always |
| 11804 | * returns 1. If an error occurs the action is cancelled, but the rule |
| 11805 | * processing continues. |
| 11806 | */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 11807 | enum act_return http_action_req_capture(struct act_rule *rule, struct proxy *px, |
Willy Tarreau | 658b85b | 2015-09-27 10:00:49 +0200 | [diff] [blame] | 11808 | struct session *sess, struct stream *s, int flags) |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 11809 | { |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 11810 | struct sample *key; |
Thierry FOURNIER | 32b1500 | 2015-07-31 08:56:16 +0200 | [diff] [blame] | 11811 | struct cap_hdr *h = rule->arg.cap.hdr; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 11812 | char **cap = s->req_cap; |
| 11813 | int len; |
| 11814 | |
Thierry FOURNIER | 32b1500 | 2015-07-31 08:56:16 +0200 | [diff] [blame] | 11815 | key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.cap.expr, SMP_T_STR); |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 11816 | if (!key) |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 11817 | return ACT_RET_CONT; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 11818 | |
| 11819 | if (cap[h->index] == NULL) |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 11820 | cap[h->index] = pool_alloc(h->pool); |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 11821 | |
| 11822 | if (cap[h->index] == NULL) /* no more capture memory */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 11823 | return ACT_RET_CONT; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 11824 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 11825 | len = key->data.u.str.data; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 11826 | if (len > h->len) |
| 11827 | len = h->len; |
| 11828 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 11829 | memcpy(cap[h->index], key->data.u.str.area, len); |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 11830 | cap[h->index][len] = 0; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 11831 | return ACT_RET_CONT; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 11832 | } |
| 11833 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 11834 | /* This function executes the "capture" action and store the result in a |
| 11835 | * capture slot if exists. It executes a fetch expression, turns the result |
| 11836 | * into a string and puts it in a capture slot. It always returns 1. If an |
| 11837 | * error occurs the action is cancelled, but the rule processing continues. |
| 11838 | */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 11839 | enum act_return http_action_req_capture_by_id(struct act_rule *rule, struct proxy *px, |
Willy Tarreau | 658b85b | 2015-09-27 10:00:49 +0200 | [diff] [blame] | 11840 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 11841 | { |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 11842 | struct sample *key; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 11843 | struct cap_hdr *h; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 11844 | char **cap = s->req_cap; |
| 11845 | struct proxy *fe = strm_fe(s); |
| 11846 | int len; |
| 11847 | int i; |
| 11848 | |
| 11849 | /* Look for the original configuration. */ |
| 11850 | for (h = fe->req_cap, i = fe->nb_req_cap - 1; |
Thierry FOURNIER | e209797 | 2015-07-31 08:56:35 +0200 | [diff] [blame] | 11851 | h != NULL && i != rule->arg.capid.idx ; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 11852 | i--, h = h->next); |
| 11853 | if (!h) |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 11854 | return ACT_RET_CONT; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 11855 | |
Thierry FOURNIER | e209797 | 2015-07-31 08:56:35 +0200 | [diff] [blame] | 11856 | key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.capid.expr, SMP_T_STR); |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 11857 | if (!key) |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 11858 | return ACT_RET_CONT; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 11859 | |
| 11860 | if (cap[h->index] == NULL) |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 11861 | cap[h->index] = pool_alloc(h->pool); |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 11862 | |
| 11863 | if (cap[h->index] == NULL) /* no more capture memory */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 11864 | return ACT_RET_CONT; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 11865 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 11866 | len = key->data.u.str.data; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 11867 | if (len > h->len) |
| 11868 | len = h->len; |
| 11869 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 11870 | memcpy(cap[h->index], key->data.u.str.area, len); |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 11871 | cap[h->index][len] = 0; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 11872 | return ACT_RET_CONT; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 11873 | } |
| 11874 | |
Christopher Faulet | 29730ba | 2017-09-18 15:26:32 +0200 | [diff] [blame] | 11875 | /* Check an "http-request capture" action. |
| 11876 | * |
| 11877 | * The function returns 1 in success case, otherwise, it returns 0 and err is |
| 11878 | * filled. |
| 11879 | */ |
| 11880 | int check_http_req_capture(struct act_rule *rule, struct proxy *px, char **err) |
| 11881 | { |
Christopher Faulet | fd608dd | 2017-12-04 09:45:15 +0100 | [diff] [blame] | 11882 | if (rule->action_ptr != http_action_req_capture_by_id) |
| 11883 | return 1; |
| 11884 | |
Christopher Faulet | 29730ba | 2017-09-18 15:26:32 +0200 | [diff] [blame] | 11885 | if (rule->arg.capid.idx >= px->nb_req_cap) { |
| 11886 | memprintf(err, "unable to find capture id '%d' referenced by http-request capture rule", |
| 11887 | rule->arg.capid.idx); |
| 11888 | return 0; |
| 11889 | } |
| 11890 | |
| 11891 | return 1; |
| 11892 | } |
| 11893 | |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 11894 | /* parse an "http-request capture" action. It takes a single argument which is |
| 11895 | * a sample fetch expression. It stores the expression into arg->act.p[0] and |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 11896 | * the allocated hdr_cap struct or the preallocated "id" into arg->act.p[1]. |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 11897 | * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error. |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 11898 | */ |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 11899 | enum act_parse_ret parse_http_req_capture(const char **args, int *orig_arg, struct proxy *px, |
| 11900 | struct act_rule *rule, char **err) |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 11901 | { |
| 11902 | struct sample_expr *expr; |
| 11903 | struct cap_hdr *hdr; |
| 11904 | int cur_arg; |
Willy Tarreau | 3986ac1 | 2015-05-08 16:13:42 +0200 | [diff] [blame] | 11905 | int len = 0; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 11906 | |
| 11907 | for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++) |
| 11908 | if (strcmp(args[cur_arg], "if") == 0 || |
| 11909 | strcmp(args[cur_arg], "unless") == 0) |
| 11910 | break; |
| 11911 | |
| 11912 | if (cur_arg < *orig_arg + 3) { |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 11913 | memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]"); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 11914 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 11915 | } |
| 11916 | |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 11917 | cur_arg = *orig_arg; |
| 11918 | expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args); |
| 11919 | if (!expr) |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 11920 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 11921 | |
| 11922 | if (!(expr->fetch->val & SMP_VAL_FE_HRQ_HDR)) { |
| 11923 | memprintf(err, |
| 11924 | "fetch method '%s' extracts information from '%s', none of which is available here", |
| 11925 | args[cur_arg-1], sample_src_names(expr->fetch->use)); |
| 11926 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 11927 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 11928 | } |
| 11929 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 11930 | if (!args[cur_arg] || !*args[cur_arg]) { |
| 11931 | memprintf(err, "expects 'len or 'id'"); |
| 11932 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 11933 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 11934 | } |
| 11935 | |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 11936 | if (strcmp(args[cur_arg], "len") == 0) { |
| 11937 | cur_arg++; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 11938 | |
| 11939 | if (!(px->cap & PR_CAP_FE)) { |
| 11940 | memprintf(err, "proxy '%s' has no frontend capability", px->id); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 11941 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 11942 | } |
| 11943 | |
Olivier Houchard | fbc74e8 | 2017-11-24 16:54:05 +0100 | [diff] [blame] | 11944 | px->conf.args.ctx = ARGC_CAP; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 11945 | |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 11946 | if (!args[cur_arg]) { |
| 11947 | memprintf(err, "missing length value"); |
| 11948 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 11949 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 11950 | } |
| 11951 | /* we copy the table name for now, it will be resolved later */ |
| 11952 | len = atoi(args[cur_arg]); |
| 11953 | if (len <= 0) { |
| 11954 | memprintf(err, "length must be > 0"); |
| 11955 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 11956 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 11957 | } |
| 11958 | cur_arg++; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 11959 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 11960 | if (!len) { |
| 11961 | memprintf(err, "a positive 'len' argument is mandatory"); |
| 11962 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 11963 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 11964 | } |
| 11965 | |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 11966 | hdr = calloc(1, sizeof(*hdr)); |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 11967 | hdr->next = px->req_cap; |
| 11968 | hdr->name = NULL; /* not a header capture */ |
| 11969 | hdr->namelen = 0; |
| 11970 | hdr->len = len; |
| 11971 | hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED); |
| 11972 | hdr->index = px->nb_req_cap++; |
| 11973 | |
| 11974 | px->req_cap = hdr; |
| 11975 | px->to_log |= LW_REQHDR; |
| 11976 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 11977 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 11978 | rule->action_ptr = http_action_req_capture; |
Thierry FOURNIER | 32b1500 | 2015-07-31 08:56:16 +0200 | [diff] [blame] | 11979 | rule->arg.cap.expr = expr; |
| 11980 | rule->arg.cap.hdr = hdr; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 11981 | } |
| 11982 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 11983 | else if (strcmp(args[cur_arg], "id") == 0) { |
| 11984 | int id; |
| 11985 | char *error; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 11986 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 11987 | cur_arg++; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 11988 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 11989 | if (!args[cur_arg]) { |
| 11990 | memprintf(err, "missing id value"); |
| 11991 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 11992 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 11993 | } |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 11994 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 11995 | id = strtol(args[cur_arg], &error, 10); |
| 11996 | if (*error != '\0') { |
| 11997 | memprintf(err, "cannot parse id '%s'", args[cur_arg]); |
| 11998 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 11999 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12000 | } |
| 12001 | cur_arg++; |
| 12002 | |
Olivier Houchard | fbc74e8 | 2017-11-24 16:54:05 +0100 | [diff] [blame] | 12003 | px->conf.args.ctx = ARGC_CAP; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12004 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 12005 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12006 | rule->action_ptr = http_action_req_capture_by_id; |
Christopher Faulet | 29730ba | 2017-09-18 15:26:32 +0200 | [diff] [blame] | 12007 | rule->check_ptr = check_http_req_capture; |
Thierry FOURNIER | e209797 | 2015-07-31 08:56:35 +0200 | [diff] [blame] | 12008 | rule->arg.capid.expr = expr; |
| 12009 | rule->arg.capid.idx = id; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12010 | } |
| 12011 | |
| 12012 | else { |
| 12013 | memprintf(err, "expects 'len' or 'id', found '%s'", args[cur_arg]); |
| 12014 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12015 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12016 | } |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12017 | |
| 12018 | *orig_arg = cur_arg; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12019 | return ACT_RET_PRS_OK; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12020 | } |
| 12021 | |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12022 | /* This function executes the "capture" action and store the result in a |
| 12023 | * capture slot if exists. It executes a fetch expression, turns the result |
| 12024 | * into a string and puts it in a capture slot. It always returns 1. If an |
| 12025 | * error occurs the action is cancelled, but the rule processing continues. |
| 12026 | */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12027 | enum act_return http_action_res_capture_by_id(struct act_rule *rule, struct proxy *px, |
Willy Tarreau | 658b85b | 2015-09-27 10:00:49 +0200 | [diff] [blame] | 12028 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12029 | { |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12030 | struct sample *key; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12031 | struct cap_hdr *h; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12032 | char **cap = s->res_cap; |
| 12033 | struct proxy *fe = strm_fe(s); |
| 12034 | int len; |
| 12035 | int i; |
| 12036 | |
| 12037 | /* Look for the original configuration. */ |
| 12038 | for (h = fe->rsp_cap, i = fe->nb_rsp_cap - 1; |
Thierry FOURNIER | e209797 | 2015-07-31 08:56:35 +0200 | [diff] [blame] | 12039 | h != NULL && i != rule->arg.capid.idx ; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12040 | i--, h = h->next); |
| 12041 | if (!h) |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12042 | return ACT_RET_CONT; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12043 | |
Thierry FOURNIER | e209797 | 2015-07-31 08:56:35 +0200 | [diff] [blame] | 12044 | key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL, rule->arg.capid.expr, SMP_T_STR); |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12045 | if (!key) |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12046 | return ACT_RET_CONT; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12047 | |
| 12048 | if (cap[h->index] == NULL) |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 12049 | cap[h->index] = pool_alloc(h->pool); |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12050 | |
| 12051 | if (cap[h->index] == NULL) /* no more capture memory */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12052 | return ACT_RET_CONT; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12053 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 12054 | len = key->data.u.str.data; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12055 | if (len > h->len) |
| 12056 | len = h->len; |
| 12057 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 12058 | memcpy(cap[h->index], key->data.u.str.area, len); |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12059 | cap[h->index][len] = 0; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12060 | return ACT_RET_CONT; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12061 | } |
| 12062 | |
Christopher Faulet | 29730ba | 2017-09-18 15:26:32 +0200 | [diff] [blame] | 12063 | /* Check an "http-response capture" action. |
| 12064 | * |
| 12065 | * The function returns 1 in success case, otherwise, it returns 0 and err is |
| 12066 | * filled. |
| 12067 | */ |
| 12068 | int check_http_res_capture(struct act_rule *rule, struct proxy *px, char **err) |
| 12069 | { |
Christopher Faulet | fd608dd | 2017-12-04 09:45:15 +0100 | [diff] [blame] | 12070 | if (rule->action_ptr != http_action_res_capture_by_id) |
| 12071 | return 1; |
| 12072 | |
Christopher Faulet | 29730ba | 2017-09-18 15:26:32 +0200 | [diff] [blame] | 12073 | if (rule->arg.capid.idx >= px->nb_rsp_cap) { |
| 12074 | memprintf(err, "unable to find capture id '%d' referenced by http-response capture rule", |
| 12075 | rule->arg.capid.idx); |
| 12076 | return 0; |
| 12077 | } |
| 12078 | |
| 12079 | return 1; |
| 12080 | } |
| 12081 | |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12082 | /* parse an "http-response capture" action. It takes a single argument which is |
| 12083 | * a sample fetch expression. It stores the expression into arg->act.p[0] and |
| 12084 | * the allocated hdr_cap struct od the preallocated id into arg->act.p[1]. |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12085 | * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error. |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12086 | */ |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12087 | enum act_parse_ret parse_http_res_capture(const char **args, int *orig_arg, struct proxy *px, |
| 12088 | struct act_rule *rule, char **err) |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12089 | { |
| 12090 | struct sample_expr *expr; |
| 12091 | int cur_arg; |
| 12092 | int id; |
| 12093 | char *error; |
| 12094 | |
| 12095 | for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++) |
| 12096 | if (strcmp(args[cur_arg], "if") == 0 || |
| 12097 | strcmp(args[cur_arg], "unless") == 0) |
| 12098 | break; |
| 12099 | |
| 12100 | if (cur_arg < *orig_arg + 3) { |
Willy Tarreau | 29bdb1c | 2016-06-24 15:36:34 +0200 | [diff] [blame] | 12101 | memprintf(err, "expects <expression> id <idx>"); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12102 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12103 | } |
| 12104 | |
| 12105 | cur_arg = *orig_arg; |
| 12106 | expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args); |
| 12107 | if (!expr) |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12108 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12109 | |
| 12110 | if (!(expr->fetch->val & SMP_VAL_FE_HRS_HDR)) { |
| 12111 | memprintf(err, |
| 12112 | "fetch method '%s' extracts information from '%s', none of which is available here", |
| 12113 | args[cur_arg-1], sample_src_names(expr->fetch->use)); |
| 12114 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12115 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12116 | } |
| 12117 | |
| 12118 | if (!args[cur_arg] || !*args[cur_arg]) { |
Willy Tarreau | 29bdb1c | 2016-06-24 15:36:34 +0200 | [diff] [blame] | 12119 | memprintf(err, "expects 'id'"); |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12120 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12121 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12122 | } |
| 12123 | |
| 12124 | if (strcmp(args[cur_arg], "id") != 0) { |
| 12125 | memprintf(err, "expects 'id', found '%s'", args[cur_arg]); |
| 12126 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12127 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12128 | } |
| 12129 | |
| 12130 | cur_arg++; |
| 12131 | |
| 12132 | if (!args[cur_arg]) { |
| 12133 | memprintf(err, "missing id value"); |
| 12134 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12135 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12136 | } |
| 12137 | |
| 12138 | id = strtol(args[cur_arg], &error, 10); |
| 12139 | if (*error != '\0') { |
| 12140 | memprintf(err, "cannot parse id '%s'", args[cur_arg]); |
| 12141 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12142 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12143 | } |
| 12144 | cur_arg++; |
| 12145 | |
Olivier Houchard | fbc74e8 | 2017-11-24 16:54:05 +0100 | [diff] [blame] | 12146 | px->conf.args.ctx = ARGC_CAP; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12147 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 12148 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12149 | rule->action_ptr = http_action_res_capture_by_id; |
Christopher Faulet | 29730ba | 2017-09-18 15:26:32 +0200 | [diff] [blame] | 12150 | rule->check_ptr = check_http_res_capture; |
Thierry FOURNIER | e209797 | 2015-07-31 08:56:35 +0200 | [diff] [blame] | 12151 | rule->arg.capid.expr = expr; |
| 12152 | rule->arg.capid.idx = id; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12153 | |
| 12154 | *orig_arg = cur_arg; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12155 | return ACT_RET_PRS_OK; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12156 | } |
| 12157 | |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 12158 | /* |
| 12159 | * Return the struct http_req_action_kw associated to a keyword. |
| 12160 | */ |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 12161 | struct action_kw *action_http_req_custom(const char *kw) |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 12162 | { |
Thierry FOURNIER | 322a124 | 2015-08-19 09:07:47 +0200 | [diff] [blame] | 12163 | return action_lookup(&http_req_keywords.list, kw); |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 12164 | } |
| 12165 | |
| 12166 | /* |
| 12167 | * Return the struct http_res_action_kw associated to a keyword. |
| 12168 | */ |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 12169 | struct action_kw *action_http_res_custom(const char *kw) |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 12170 | { |
Thierry FOURNIER | 322a124 | 2015-08-19 09:07:47 +0200 | [diff] [blame] | 12171 | return action_lookup(&http_res_keywords.list, kw); |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 12172 | } |
| 12173 | |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12174 | |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 12175 | /************************************************************************/ |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12176 | /* All supported ACL keywords must be declared here. */ |
| 12177 | /************************************************************************/ |
| 12178 | |
| 12179 | /* Note: must not be declared <const> as its list will be overwritten. |
| 12180 | * Please take care of keeping this list alphabetically sorted. |
| 12181 | */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 12182 | static struct acl_kw_list acl_kws = {ILH, { |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12183 | { "base", "base", PAT_MATCH_STR }, |
| 12184 | { "base_beg", "base", PAT_MATCH_BEG }, |
| 12185 | { "base_dir", "base", PAT_MATCH_DIR }, |
| 12186 | { "base_dom", "base", PAT_MATCH_DOM }, |
| 12187 | { "base_end", "base", PAT_MATCH_END }, |
| 12188 | { "base_len", "base", PAT_MATCH_LEN }, |
| 12189 | { "base_reg", "base", PAT_MATCH_REG }, |
| 12190 | { "base_sub", "base", PAT_MATCH_SUB }, |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 12191 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12192 | { "cook", "req.cook", PAT_MATCH_STR }, |
| 12193 | { "cook_beg", "req.cook", PAT_MATCH_BEG }, |
| 12194 | { "cook_dir", "req.cook", PAT_MATCH_DIR }, |
| 12195 | { "cook_dom", "req.cook", PAT_MATCH_DOM }, |
| 12196 | { "cook_end", "req.cook", PAT_MATCH_END }, |
| 12197 | { "cook_len", "req.cook", PAT_MATCH_LEN }, |
| 12198 | { "cook_reg", "req.cook", PAT_MATCH_REG }, |
| 12199 | { "cook_sub", "req.cook", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12200 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12201 | { "hdr", "req.hdr", PAT_MATCH_STR }, |
| 12202 | { "hdr_beg", "req.hdr", PAT_MATCH_BEG }, |
| 12203 | { "hdr_dir", "req.hdr", PAT_MATCH_DIR }, |
| 12204 | { "hdr_dom", "req.hdr", PAT_MATCH_DOM }, |
| 12205 | { "hdr_end", "req.hdr", PAT_MATCH_END }, |
| 12206 | { "hdr_len", "req.hdr", PAT_MATCH_LEN }, |
| 12207 | { "hdr_reg", "req.hdr", PAT_MATCH_REG }, |
| 12208 | { "hdr_sub", "req.hdr", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12209 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12210 | /* these two declarations uses strings with list storage (in place |
| 12211 | * of tree storage). The basic match is PAT_MATCH_STR, but the indexation |
| 12212 | * and delete functions are relative to the list management. The parse |
| 12213 | * and match method are related to the corresponding fetch methods. This |
| 12214 | * is very particular ACL declaration mode. |
| 12215 | */ |
| 12216 | { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth }, |
| 12217 | { "method", NULL, PAT_MATCH_STR, pat_parse_meth, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_meth }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12218 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12219 | { "path", "path", PAT_MATCH_STR }, |
| 12220 | { "path_beg", "path", PAT_MATCH_BEG }, |
| 12221 | { "path_dir", "path", PAT_MATCH_DIR }, |
| 12222 | { "path_dom", "path", PAT_MATCH_DOM }, |
| 12223 | { "path_end", "path", PAT_MATCH_END }, |
| 12224 | { "path_len", "path", PAT_MATCH_LEN }, |
| 12225 | { "path_reg", "path", PAT_MATCH_REG }, |
| 12226 | { "path_sub", "path", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12227 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12228 | { "req_ver", "req.ver", PAT_MATCH_STR }, |
| 12229 | { "resp_ver", "res.ver", PAT_MATCH_STR }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12230 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12231 | { "scook", "res.cook", PAT_MATCH_STR }, |
| 12232 | { "scook_beg", "res.cook", PAT_MATCH_BEG }, |
| 12233 | { "scook_dir", "res.cook", PAT_MATCH_DIR }, |
| 12234 | { "scook_dom", "res.cook", PAT_MATCH_DOM }, |
| 12235 | { "scook_end", "res.cook", PAT_MATCH_END }, |
| 12236 | { "scook_len", "res.cook", PAT_MATCH_LEN }, |
| 12237 | { "scook_reg", "res.cook", PAT_MATCH_REG }, |
| 12238 | { "scook_sub", "res.cook", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12239 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12240 | { "shdr", "res.hdr", PAT_MATCH_STR }, |
| 12241 | { "shdr_beg", "res.hdr", PAT_MATCH_BEG }, |
| 12242 | { "shdr_dir", "res.hdr", PAT_MATCH_DIR }, |
| 12243 | { "shdr_dom", "res.hdr", PAT_MATCH_DOM }, |
| 12244 | { "shdr_end", "res.hdr", PAT_MATCH_END }, |
| 12245 | { "shdr_len", "res.hdr", PAT_MATCH_LEN }, |
| 12246 | { "shdr_reg", "res.hdr", PAT_MATCH_REG }, |
| 12247 | { "shdr_sub", "res.hdr", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12248 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12249 | { "url", "url", PAT_MATCH_STR }, |
| 12250 | { "url_beg", "url", PAT_MATCH_BEG }, |
| 12251 | { "url_dir", "url", PAT_MATCH_DIR }, |
| 12252 | { "url_dom", "url", PAT_MATCH_DOM }, |
| 12253 | { "url_end", "url", PAT_MATCH_END }, |
| 12254 | { "url_len", "url", PAT_MATCH_LEN }, |
| 12255 | { "url_reg", "url", PAT_MATCH_REG }, |
| 12256 | { "url_sub", "url", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12257 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12258 | { "urlp", "urlp", PAT_MATCH_STR }, |
| 12259 | { "urlp_beg", "urlp", PAT_MATCH_BEG }, |
| 12260 | { "urlp_dir", "urlp", PAT_MATCH_DIR }, |
| 12261 | { "urlp_dom", "urlp", PAT_MATCH_DOM }, |
| 12262 | { "urlp_end", "urlp", PAT_MATCH_END }, |
| 12263 | { "urlp_len", "urlp", PAT_MATCH_LEN }, |
| 12264 | { "urlp_reg", "urlp", PAT_MATCH_REG }, |
| 12265 | { "urlp_sub", "urlp", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12266 | |
Willy Tarreau | 8ed669b | 2013-01-11 15:49:37 +0100 | [diff] [blame] | 12267 | { /* END */ }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12268 | }}; |
| 12269 | |
| 12270 | /************************************************************************/ |
| 12271 | /* All supported pattern keywords must be declared here. */ |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 12272 | /************************************************************************/ |
| 12273 | /* Note: must not be declared <const> as its list will be overwritten */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 12274 | static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, { |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12275 | { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 12276 | { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 12277 | { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV }, |
| 12278 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 12279 | /* capture are allocated and are permanent in the stream */ |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 12280 | { "capture.req.hdr", smp_fetch_capture_header_req, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRQHP }, |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 12281 | |
| 12282 | /* retrieve these captures from the HTTP logs */ |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 12283 | { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP }, |
| 12284 | { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP }, |
| 12285 | { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP }, |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 12286 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 12287 | { "capture.res.hdr", smp_fetch_capture_header_res, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP }, |
| 12288 | { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP }, |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 12289 | |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 12290 | /* cookie is valid in both directions (eg: for "stick ...") but cook* |
| 12291 | * are only here to match the ACL's name, are request-only and are used |
| 12292 | * for ACL compatibility only. |
| 12293 | */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12294 | { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 12295 | { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 12296 | { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 12297 | { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 12298 | |
| 12299 | /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are |
| 12300 | * only here to match the ACL's name, are request-only and are used for |
| 12301 | * ACL compatibility only. |
| 12302 | */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12303 | { "hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 12304 | { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 12305 | { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 12306 | { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV }, |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 12307 | |
Willy Tarreau | 0a0daec | 2013-04-02 22:44:58 +0200 | [diff] [blame] | 12308 | { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV }, |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12309 | { "http_auth_group", smp_fetch_http_auth_grp, ARG1(1,USR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 12310 | { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP }, |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 12311 | { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP }, |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12312 | { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 12313 | { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 12314 | |
| 12315 | /* HTTP protocol on the request path */ |
| 12316 | { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP }, |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 12317 | { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP }, |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 12318 | |
| 12319 | /* HTTP version on the request path */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12320 | { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 12321 | { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 12322 | |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 12323 | { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 12324 | { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 12325 | { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 12326 | { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV }, |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 12327 | |
Thierry FOURNIER | d7d8881 | 2017-04-19 15:15:14 +0200 | [diff] [blame] | 12328 | { "req.hdrs", smp_fetch_hdrs, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV }, |
Thierry FOURNIER | 5617dce | 2017-04-09 05:38:19 +0200 | [diff] [blame] | 12329 | { "req.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV }, |
| 12330 | |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 12331 | /* HTTP version on the response path */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12332 | { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV }, |
| 12333 | { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV }, |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 12334 | |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 12335 | /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12336 | { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 12337 | { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 12338 | { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 12339 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12340 | { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 12341 | { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12342 | { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 12343 | { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 12344 | { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV }, |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 12345 | { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 12346 | { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV }, |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 12347 | |
| 12348 | /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12349 | { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 12350 | { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV }, |
| 12351 | { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV }, |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 12352 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12353 | { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 12354 | { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV }, |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12355 | { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 12356 | { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV }, |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 12357 | { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV }, |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 12358 | { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 12359 | { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV }, |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 12360 | |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 12361 | /* scook is valid only on the response and is used for ACL compatibility */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12362 | { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 12363 | { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV }, |
| 12364 | { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV }, |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12365 | { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */ |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 12366 | |
| 12367 | /* shdr is valid only on the response and is used for ACL compatibility */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12368 | { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 12369 | { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV }, |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 12370 | { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 12371 | { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV }, |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 12372 | |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 12373 | { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP }, |
Thierry Fournier | 0e00dca | 2016-04-07 15:47:40 +0200 | [diff] [blame] | 12374 | { "unique-id", smp_fetch_uniqueid, 0, NULL, SMP_T_STR, SMP_SRC_L4SRV }, |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12375 | { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 12376 | { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 12377 | { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV }, |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 12378 | { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 12379 | { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 12380 | { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 12381 | { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 12382 | { "urlp_val", smp_fetch_url_param_val, ARG2(0,STR,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 12383 | { /* END */ }, |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 12384 | }}; |
| 12385 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 12386 | |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12387 | /************************************************************************/ |
| 12388 | /* All supported converter keywords must be declared here. */ |
| 12389 | /************************************************************************/ |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 12390 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 12391 | static struct sample_conv_kw_list sample_conv_kws = {ILH, { |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 12392 | { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_T_STR}, |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 12393 | { "language", sample_conv_q_prefered, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR}, |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 12394 | { "capture-req", smp_conv_req_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR}, |
| 12395 | { "capture-res", smp_conv_res_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR}, |
Thierry FOURNIER | 82ff3c9 | 2015-05-07 15:46:20 +0200 | [diff] [blame] | 12396 | { "url_dec", sample_conv_url_dec, 0, NULL, SMP_T_STR, SMP_T_STR}, |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 12397 | { NULL, NULL, 0, 0, 0 }, |
| 12398 | }}; |
| 12399 | |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 12400 | |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12401 | /************************************************************************/ |
| 12402 | /* All supported http-request action keywords must be declared here. */ |
| 12403 | /************************************************************************/ |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 12404 | struct action_kw_list http_req_actions = { |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12405 | .kw = { |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12406 | { "capture", parse_http_req_capture }, |
Willy Tarreau | 53275e8 | 2017-11-24 07:52:01 +0100 | [diff] [blame] | 12407 | { "reject", parse_http_action_reject }, |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12408 | { "set-method", parse_set_req_line }, |
| 12409 | { "set-path", parse_set_req_line }, |
| 12410 | { "set-query", parse_set_req_line }, |
| 12411 | { "set-uri", parse_set_req_line }, |
Willy Tarreau | cb703b0 | 2015-04-03 09:52:01 +0200 | [diff] [blame] | 12412 | { NULL, NULL } |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12413 | } |
| 12414 | }; |
| 12415 | |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 12416 | struct action_kw_list http_res_actions = { |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12417 | .kw = { |
| 12418 | { "capture", parse_http_res_capture }, |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 12419 | { "set-status", parse_http_set_status }, |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12420 | { NULL, NULL } |
| 12421 | } |
| 12422 | }; |
| 12423 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 12424 | __attribute__((constructor)) |
| 12425 | static void __http_protocol_init(void) |
| 12426 | { |
| 12427 | acl_register_keywords(&acl_kws); |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 12428 | sample_register_fetches(&sample_fetch_keywords); |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 12429 | sample_register_convs(&sample_conv_kws); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12430 | http_req_keywords_register(&http_req_actions); |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12431 | http_res_keywords_register(&http_res_actions); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 12432 | } |
| 12433 | |
| 12434 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 12435 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 12436 | * Local variables: |
| 12437 | * c-indent-level: 8 |
| 12438 | * c-basic-offset: 8 |
| 12439 | * End: |
| 12440 | */ |