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> |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 65 | #include <proto/pattern.h> |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 66 | #include <proto/proto_tcp.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 67 | #include <proto/proto_http.h> |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 68 | #include <proto/proxy.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 69 | #include <proto/queue.h> |
Willy Tarreau | cd3b094 | 2012-04-27 21:52:18 +0200 | [diff] [blame] | 70 | #include <proto/sample.h> |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 71 | #include <proto/server.h> |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 72 | #include <proto/stream.h> |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 73 | #include <proto/stream_interface.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 74 | #include <proto/task.h> |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 75 | #include <proto/pattern.h> |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 76 | #include <proto/vars.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 77 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 78 | const char HTTP_100[] = |
| 79 | "HTTP/1.1 100 Continue\r\n\r\n"; |
| 80 | |
| 81 | const struct chunk http_100_chunk = { |
| 82 | .str = (char *)&HTTP_100, |
| 83 | .len = sizeof(HTTP_100)-1 |
| 84 | }; |
| 85 | |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 86 | /* Warning: no "connection" header is provided with the 3xx messages below */ |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 87 | const char *HTTP_301 = |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 88 | "HTTP/1.1 301 Moved Permanently\r\n" |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 89 | "Content-length: 0\r\n" |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 90 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 91 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 92 | const char *HTTP_302 = |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 93 | "HTTP/1.1 302 Found\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 94 | "Cache-Control: no-cache\r\n" |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 95 | "Content-length: 0\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 96 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 97 | |
| 98 | /* same as 302 except that the browser MUST retry with the GET method */ |
| 99 | const char *HTTP_303 = |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 100 | "HTTP/1.1 303 See Other\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 101 | "Cache-Control: no-cache\r\n" |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 102 | "Content-length: 0\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 103 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 104 | |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 105 | |
| 106 | /* same as 302 except that the browser MUST retry with the same method */ |
| 107 | const char *HTTP_307 = |
| 108 | "HTTP/1.1 307 Temporary Redirect\r\n" |
| 109 | "Cache-Control: no-cache\r\n" |
| 110 | "Content-length: 0\r\n" |
| 111 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 112 | |
| 113 | /* same as 301 except that the browser MUST retry with the same method */ |
| 114 | const char *HTTP_308 = |
| 115 | "HTTP/1.1 308 Permanent Redirect\r\n" |
| 116 | "Content-length: 0\r\n" |
| 117 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 118 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 119 | /* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */ |
| 120 | const char *HTTP_401_fmt = |
| 121 | "HTTP/1.0 401 Unauthorized\r\n" |
| 122 | "Cache-Control: no-cache\r\n" |
| 123 | "Connection: close\r\n" |
Willy Tarreau | 791d66d | 2006-07-08 16:53:38 +0200 | [diff] [blame] | 124 | "Content-Type: text/html\r\n" |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 125 | "WWW-Authenticate: Basic realm=\"%s\"\r\n" |
| 126 | "\r\n" |
| 127 | "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n"; |
| 128 | |
Willy Tarreau | 844a7e7 | 2010-01-31 21:46:18 +0100 | [diff] [blame] | 129 | const char *HTTP_407_fmt = |
| 130 | "HTTP/1.0 407 Unauthorized\r\n" |
| 131 | "Cache-Control: no-cache\r\n" |
| 132 | "Connection: close\r\n" |
| 133 | "Content-Type: text/html\r\n" |
| 134 | "Proxy-Authenticate: Basic realm=\"%s\"\r\n" |
| 135 | "\r\n" |
Godbach | 1f1fae6 | 2014-12-17 16:32:05 +0800 | [diff] [blame] | 136 | "<html><body><h1>407 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n"; |
Willy Tarreau | 844a7e7 | 2010-01-31 21:46:18 +0100 | [diff] [blame] | 137 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 138 | |
| 139 | const int http_err_codes[HTTP_ERR_SIZE] = { |
Willy Tarreau | ae94d4d | 2011-05-11 16:28:49 +0200 | [diff] [blame] | 140 | [HTTP_ERR_200] = 200, /* used by "monitor-uri" */ |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 141 | [HTTP_ERR_400] = 400, |
| 142 | [HTTP_ERR_403] = 403, |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 143 | [HTTP_ERR_405] = 405, |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 144 | [HTTP_ERR_408] = 408, |
Tim Duesterhus | e2b10bf | 2018-04-27 21:18:46 +0200 | [diff] [blame] | 145 | [HTTP_ERR_421] = 421, |
Olivier Houchard | 51a76d8 | 2017-10-02 16:12:07 +0200 | [diff] [blame] | 146 | [HTTP_ERR_425] = 425, |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 147 | [HTTP_ERR_429] = 429, |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 148 | [HTTP_ERR_500] = 500, |
| 149 | [HTTP_ERR_502] = 502, |
| 150 | [HTTP_ERR_503] = 503, |
| 151 | [HTTP_ERR_504] = 504, |
| 152 | }; |
| 153 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 154 | static const char *http_err_msgs[HTTP_ERR_SIZE] = { |
Willy Tarreau | ae94d4d | 2011-05-11 16:28:49 +0200 | [diff] [blame] | 155 | [HTTP_ERR_200] = |
| 156 | "HTTP/1.0 200 OK\r\n" |
| 157 | "Cache-Control: no-cache\r\n" |
| 158 | "Connection: close\r\n" |
| 159 | "Content-Type: text/html\r\n" |
| 160 | "\r\n" |
| 161 | "<html><body><h1>200 OK</h1>\nService ready.\n</body></html>\n", |
| 162 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 163 | [HTTP_ERR_400] = |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 164 | "HTTP/1.0 400 Bad request\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 165 | "Cache-Control: no-cache\r\n" |
| 166 | "Connection: close\r\n" |
| 167 | "Content-Type: text/html\r\n" |
| 168 | "\r\n" |
| 169 | "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n", |
| 170 | |
| 171 | [HTTP_ERR_403] = |
| 172 | "HTTP/1.0 403 Forbidden\r\n" |
| 173 | "Cache-Control: no-cache\r\n" |
| 174 | "Connection: close\r\n" |
| 175 | "Content-Type: text/html\r\n" |
| 176 | "\r\n" |
| 177 | "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n", |
| 178 | |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 179 | [HTTP_ERR_405] = |
| 180 | "HTTP/1.0 405 Method Not Allowed\r\n" |
| 181 | "Cache-Control: no-cache\r\n" |
| 182 | "Connection: close\r\n" |
| 183 | "Content-Type: text/html\r\n" |
| 184 | "\r\n" |
| 185 | "<html><body><h1>405 Method Not Allowed</h1>\nA request was made of a resource using a request method not supported by that resource\n</body></html>\n", |
| 186 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 187 | [HTTP_ERR_408] = |
| 188 | "HTTP/1.0 408 Request Time-out\r\n" |
| 189 | "Cache-Control: no-cache\r\n" |
| 190 | "Connection: close\r\n" |
| 191 | "Content-Type: text/html\r\n" |
| 192 | "\r\n" |
| 193 | "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n", |
| 194 | |
Tim Duesterhus | e2b10bf | 2018-04-27 21:18:46 +0200 | [diff] [blame] | 195 | [HTTP_ERR_421] = |
| 196 | "HTTP/1.0 421 Misdirected Request\r\n" |
| 197 | "Cache-Control: no-cache\r\n" |
| 198 | "Connection: close\r\n" |
| 199 | "Content-Type: text/html\r\n" |
| 200 | "\r\n" |
| 201 | "<html><body><h1>421 Misdirected Request</h1>\nRequest sent to a non-authoritative server.\n</body></html>\n", |
| 202 | |
Olivier Houchard | 51a76d8 | 2017-10-02 16:12:07 +0200 | [diff] [blame] | 203 | [HTTP_ERR_425] = |
| 204 | "HTTP/1.0 425 Too Early\r\n" |
| 205 | "Cache-Control: no-cache\r\n" |
| 206 | "Connection: close\r\n" |
| 207 | "Content-Type: text/html\r\n" |
| 208 | "\r\n" |
| 209 | "<html><body><h1>425 Too Early</h1>\nYour browser sent early data.\n</body></html>\n", |
| 210 | |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 211 | [HTTP_ERR_429] = |
| 212 | "HTTP/1.0 429 Too Many Requests\r\n" |
| 213 | "Cache-Control: no-cache\r\n" |
| 214 | "Connection: close\r\n" |
| 215 | "Content-Type: text/html\r\n" |
| 216 | "\r\n" |
| 217 | "<html><body><h1>429 Too Many Requests</h1>\nYou have sent too many requests in a given amount of time.\n</body></html>\n", |
| 218 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 219 | [HTTP_ERR_500] = |
Jarno Huuskonen | 16ad94a | 2017-01-09 14:17:10 +0200 | [diff] [blame] | 220 | "HTTP/1.0 500 Internal Server Error\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 221 | "Cache-Control: no-cache\r\n" |
| 222 | "Connection: close\r\n" |
| 223 | "Content-Type: text/html\r\n" |
| 224 | "\r\n" |
Jarno Huuskonen | 16ad94a | 2017-01-09 14:17:10 +0200 | [diff] [blame] | 225 | "<html><body><h1>500 Internal Server Error</h1>\nAn internal server error occured.\n</body></html>\n", |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 226 | |
| 227 | [HTTP_ERR_502] = |
| 228 | "HTTP/1.0 502 Bad Gateway\r\n" |
| 229 | "Cache-Control: no-cache\r\n" |
| 230 | "Connection: close\r\n" |
| 231 | "Content-Type: text/html\r\n" |
| 232 | "\r\n" |
| 233 | "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n", |
| 234 | |
| 235 | [HTTP_ERR_503] = |
| 236 | "HTTP/1.0 503 Service Unavailable\r\n" |
| 237 | "Cache-Control: no-cache\r\n" |
| 238 | "Connection: close\r\n" |
| 239 | "Content-Type: text/html\r\n" |
| 240 | "\r\n" |
| 241 | "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n", |
| 242 | |
| 243 | [HTTP_ERR_504] = |
| 244 | "HTTP/1.0 504 Gateway Time-out\r\n" |
| 245 | "Cache-Control: no-cache\r\n" |
| 246 | "Connection: close\r\n" |
| 247 | "Content-Type: text/html\r\n" |
| 248 | "\r\n" |
| 249 | "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n", |
| 250 | |
| 251 | }; |
| 252 | |
Cyril Bonté | 19979e1 | 2012-04-04 12:57:21 +0200 | [diff] [blame] | 253 | /* status codes available for the stats admin page (strictly 4 chars length) */ |
| 254 | const char *stat_status_codes[STAT_STATUS_SIZE] = { |
| 255 | [STAT_STATUS_DENY] = "DENY", |
| 256 | [STAT_STATUS_DONE] = "DONE", |
| 257 | [STAT_STATUS_ERRP] = "ERRP", |
| 258 | [STAT_STATUS_EXCD] = "EXCD", |
| 259 | [STAT_STATUS_NONE] = "NONE", |
| 260 | [STAT_STATUS_PART] = "PART", |
| 261 | [STAT_STATUS_UNKN] = "UNKN", |
| 262 | }; |
| 263 | |
| 264 | |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 265 | /* List head of all known action keywords for "http-request" */ |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 266 | struct action_kw_list http_req_keywords = { |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 267 | .list = LIST_HEAD_INIT(http_req_keywords.list) |
| 268 | }; |
| 269 | |
| 270 | /* List head of all known action keywords for "http-response" */ |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 271 | struct action_kw_list http_res_keywords = { |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 272 | .list = LIST_HEAD_INIT(http_res_keywords.list) |
| 273 | }; |
| 274 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 275 | /* We must put the messages here since GCC cannot initialize consts depending |
| 276 | * on strlen(). |
| 277 | */ |
| 278 | struct chunk http_err_chunks[HTTP_ERR_SIZE]; |
| 279 | |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 280 | /* 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] | 281 | static THREAD_LOCAL struct hdr_ctx static_hdr_ctx; |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 282 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 283 | #define FD_SETS_ARE_BITFIELDS |
| 284 | #ifdef FD_SETS_ARE_BITFIELDS |
| 285 | /* |
| 286 | * This map is used with all the FD_* macros to check whether a particular bit |
| 287 | * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes |
| 288 | * which should be encoded. When FD_ISSET() returns non-zero, it means that the |
| 289 | * byte should be encoded. Be careful to always pass bytes from 0 to 255 |
| 290 | * exclusively to the macros. |
| 291 | */ |
| 292 | fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| 293 | 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] | 294 | 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] | 295 | |
| 296 | #else |
| 297 | #error "Check if your OS uses bitfields for fd_sets" |
| 298 | #endif |
| 299 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 300 | 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] | 301 | |
David Carlier | 7365f7d | 2016-04-04 11:54:42 +0100 | [diff] [blame] | 302 | static inline int http_msg_forward_body(struct stream *s, struct http_msg *msg); |
| 303 | 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] | 304 | |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 305 | /* This function returns a reason associated with the HTTP status. |
| 306 | * This function never fails, a message is always returned. |
| 307 | */ |
| 308 | const char *get_reason(unsigned int status) |
| 309 | { |
| 310 | switch (status) { |
| 311 | case 100: return "Continue"; |
| 312 | case 101: return "Switching Protocols"; |
| 313 | case 102: return "Processing"; |
| 314 | case 200: return "OK"; |
| 315 | case 201: return "Created"; |
| 316 | case 202: return "Accepted"; |
| 317 | case 203: return "Non-Authoritative Information"; |
| 318 | case 204: return "No Content"; |
| 319 | case 205: return "Reset Content"; |
| 320 | case 206: return "Partial Content"; |
| 321 | case 207: return "Multi-Status"; |
| 322 | case 210: return "Content Different"; |
| 323 | case 226: return "IM Used"; |
| 324 | case 300: return "Multiple Choices"; |
| 325 | case 301: return "Moved Permanently"; |
| 326 | case 302: return "Moved Temporarily"; |
| 327 | case 303: return "See Other"; |
| 328 | case 304: return "Not Modified"; |
| 329 | case 305: return "Use Proxy"; |
| 330 | case 307: return "Temporary Redirect"; |
| 331 | case 308: return "Permanent Redirect"; |
| 332 | case 310: return "Too many Redirects"; |
| 333 | case 400: return "Bad Request"; |
| 334 | case 401: return "Unauthorized"; |
| 335 | case 402: return "Payment Required"; |
| 336 | case 403: return "Forbidden"; |
| 337 | case 404: return "Not Found"; |
| 338 | case 405: return "Method Not Allowed"; |
| 339 | case 406: return "Not Acceptable"; |
| 340 | case 407: return "Proxy Authentication Required"; |
| 341 | case 408: return "Request Time-out"; |
| 342 | case 409: return "Conflict"; |
| 343 | case 410: return "Gone"; |
| 344 | case 411: return "Length Required"; |
| 345 | case 412: return "Precondition Failed"; |
| 346 | case 413: return "Request Entity Too Large"; |
| 347 | case 414: return "Request-URI Too Long"; |
| 348 | case 415: return "Unsupported Media Type"; |
| 349 | case 416: return "Requested range unsatisfiable"; |
| 350 | case 417: return "Expectation failed"; |
| 351 | case 418: return "I'm a teapot"; |
Tim Duesterhus | e2b10bf | 2018-04-27 21:18:46 +0200 | [diff] [blame] | 352 | case 421: return "Misdirected Request"; |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 353 | case 422: return "Unprocessable entity"; |
| 354 | case 423: return "Locked"; |
| 355 | case 424: return "Method failure"; |
Olivier Houchard | 51a76d8 | 2017-10-02 16:12:07 +0200 | [diff] [blame] | 356 | case 425: return "Too Early"; |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 357 | case 426: return "Upgrade Required"; |
| 358 | case 428: return "Precondition Required"; |
| 359 | case 429: return "Too Many Requests"; |
| 360 | case 431: return "Request Header Fields Too Large"; |
| 361 | case 449: return "Retry With"; |
| 362 | case 450: return "Blocked by Windows Parental Controls"; |
| 363 | case 451: return "Unavailable For Legal Reasons"; |
| 364 | case 456: return "Unrecoverable Error"; |
| 365 | case 499: return "client has closed connection"; |
| 366 | case 500: return "Internal Server Error"; |
| 367 | case 501: return "Not Implemented"; |
Jarno Huuskonen | 59af2df | 2016-12-28 10:49:01 +0200 | [diff] [blame] | 368 | case 502: return "Bad Gateway or Proxy Error"; |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 369 | case 503: return "Service Unavailable"; |
| 370 | case 504: return "Gateway Time-out"; |
| 371 | case 505: return "HTTP Version not supported"; |
| 372 | case 506: return "Variant also negociate"; |
| 373 | case 507: return "Insufficient storage"; |
| 374 | case 508: return "Loop detected"; |
| 375 | case 509: return "Bandwidth Limit Exceeded"; |
| 376 | case 510: return "Not extended"; |
| 377 | case 511: return "Network authentication required"; |
| 378 | case 520: return "Web server is returning an unknown error"; |
| 379 | default: |
| 380 | switch (status) { |
| 381 | case 100 ... 199: return "Informational"; |
| 382 | case 200 ... 299: return "Success"; |
| 383 | case 300 ... 399: return "Redirection"; |
| 384 | case 400 ... 499: return "Client Error"; |
| 385 | case 500 ... 599: return "Server Error"; |
| 386 | default: return "Other"; |
| 387 | } |
| 388 | } |
| 389 | } |
| 390 | |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 391 | /* This function returns HTTP_ERR_<num> (enum) matching http status code. |
| 392 | * Returned value should match codes from http_err_codes. |
| 393 | */ |
| 394 | static const int http_get_status_idx(unsigned int status) |
| 395 | { |
| 396 | switch (status) { |
| 397 | case 200: return HTTP_ERR_200; |
| 398 | case 400: return HTTP_ERR_400; |
| 399 | case 403: return HTTP_ERR_403; |
| 400 | case 405: return HTTP_ERR_405; |
| 401 | case 408: return HTTP_ERR_408; |
Tim Duesterhus | e2b10bf | 2018-04-27 21:18:46 +0200 | [diff] [blame] | 402 | case 421: return HTTP_ERR_421; |
Olivier Houchard | 51a76d8 | 2017-10-02 16:12:07 +0200 | [diff] [blame] | 403 | case 425: return HTTP_ERR_425; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 404 | case 429: return HTTP_ERR_429; |
| 405 | case 500: return HTTP_ERR_500; |
| 406 | case 502: return HTTP_ERR_502; |
| 407 | case 503: return HTTP_ERR_503; |
| 408 | case 504: return HTTP_ERR_504; |
| 409 | default: return HTTP_ERR_500; |
| 410 | } |
| 411 | } |
| 412 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 413 | void init_proto_http() |
| 414 | { |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 415 | int i; |
| 416 | char *tmp; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 417 | int msg; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 418 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 419 | for (msg = 0; msg < HTTP_ERR_SIZE; msg++) { |
| 420 | if (!http_err_msgs[msg]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 421 | ha_alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg); |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 422 | abort(); |
| 423 | } |
| 424 | |
| 425 | http_err_chunks[msg].str = (char *)http_err_msgs[msg]; |
| 426 | http_err_chunks[msg].len = strlen(http_err_msgs[msg]); |
| 427 | } |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 428 | |
| 429 | /* initialize the log header encoding map : '{|}"#' should be encoded with |
| 430 | * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ). |
| 431 | * URL encoding only requires '"', '#' to be encoded as well as non- |
| 432 | * printable characters above. |
| 433 | */ |
| 434 | memset(hdr_encode_map, 0, sizeof(hdr_encode_map)); |
| 435 | memset(url_encode_map, 0, sizeof(url_encode_map)); |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 436 | memset(http_encode_map, 0, sizeof(url_encode_map)); |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 437 | for (i = 0; i < 32; i++) { |
| 438 | FD_SET(i, hdr_encode_map); |
| 439 | FD_SET(i, url_encode_map); |
| 440 | } |
| 441 | for (i = 127; i < 256; i++) { |
| 442 | FD_SET(i, hdr_encode_map); |
| 443 | FD_SET(i, url_encode_map); |
| 444 | } |
| 445 | |
| 446 | tmp = "\"#{|}"; |
| 447 | while (*tmp) { |
| 448 | FD_SET(*tmp, hdr_encode_map); |
| 449 | tmp++; |
| 450 | } |
| 451 | |
| 452 | tmp = "\"#"; |
| 453 | while (*tmp) { |
| 454 | FD_SET(*tmp, url_encode_map); |
| 455 | tmp++; |
| 456 | } |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 457 | |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 458 | /* initialize the http header encoding map. The draft httpbis define the |
| 459 | * header content as: |
| 460 | * |
| 461 | * HTTP-message = start-line |
| 462 | * *( header-field CRLF ) |
| 463 | * CRLF |
| 464 | * [ message-body ] |
| 465 | * header-field = field-name ":" OWS field-value OWS |
| 466 | * field-value = *( field-content / obs-fold ) |
| 467 | * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ] |
| 468 | * obs-fold = CRLF 1*( SP / HTAB ) |
| 469 | * field-vchar = VCHAR / obs-text |
| 470 | * VCHAR = %x21-7E |
| 471 | * obs-text = %x80-FF |
| 472 | * |
| 473 | * All the chars are encoded except "VCHAR", "obs-text", SP and HTAB. |
| 474 | * The encoded chars are form 0x00 to 0x08, 0x0a to 0x1f and 0x7f. The |
| 475 | * "obs-fold" is volontary forgotten because haproxy remove this. |
| 476 | */ |
| 477 | memset(http_encode_map, 0, sizeof(http_encode_map)); |
| 478 | for (i = 0x00; i <= 0x08; i++) |
| 479 | FD_SET(i, http_encode_map); |
| 480 | for (i = 0x0a; i <= 0x1f; i++) |
| 481 | FD_SET(i, http_encode_map); |
| 482 | FD_SET(0x7f, http_encode_map); |
| 483 | |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 484 | /* memory allocations */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 485 | pool_head_http_txn = create_pool("http_txn", sizeof(struct http_txn), MEM_F_SHARED); |
| 486 | pool_head_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED); |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 487 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 488 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 489 | /* |
| 490 | * We have 26 list of methods (1 per first letter), each of which can have |
| 491 | * up to 3 entries (2 valid, 1 null). |
| 492 | */ |
| 493 | struct http_method_desc { |
Willy Tarreau | c8987b3 | 2013-12-06 23:43:17 +0100 | [diff] [blame] | 494 | enum http_meth_t meth; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 495 | int len; |
| 496 | const char text[8]; |
| 497 | }; |
| 498 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 499 | const struct http_method_desc http_methods[26][3] = { |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 500 | ['C' - 'A'] = { |
| 501 | [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" }, |
| 502 | }, |
| 503 | ['D' - 'A'] = { |
| 504 | [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" }, |
| 505 | }, |
| 506 | ['G' - 'A'] = { |
| 507 | [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" }, |
| 508 | }, |
| 509 | ['H' - 'A'] = { |
| 510 | [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" }, |
| 511 | }, |
Christopher Faulet | d57ad64 | 2015-07-31 14:26:57 +0200 | [diff] [blame] | 512 | ['O' - 'A'] = { |
| 513 | [0] = { .meth = HTTP_METH_OPTIONS , .len=7, .text="OPTIONS" }, |
| 514 | }, |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 515 | ['P' - 'A'] = { |
| 516 | [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" }, |
| 517 | [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" }, |
| 518 | }, |
| 519 | ['T' - 'A'] = { |
| 520 | [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" }, |
| 521 | }, |
| 522 | /* rest is empty like this : |
Willy Tarreau | b7ce424 | 2015-09-03 17:15:21 +0200 | [diff] [blame] | 523 | * [0] = { .meth = HTTP_METH_OTHER , .len=0, .text="" }, |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 524 | */ |
| 525 | }; |
| 526 | |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 527 | const struct http_method_name http_known_methods[HTTP_METH_OTHER] = { |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 528 | [HTTP_METH_OPTIONS] = { "OPTIONS", 7 }, |
| 529 | [HTTP_METH_GET] = { "GET", 3 }, |
| 530 | [HTTP_METH_HEAD] = { "HEAD", 4 }, |
| 531 | [HTTP_METH_POST] = { "POST", 4 }, |
| 532 | [HTTP_METH_PUT] = { "PUT", 3 }, |
| 533 | [HTTP_METH_DELETE] = { "DELETE", 6 }, |
| 534 | [HTTP_METH_TRACE] = { "TRACE", 5 }, |
| 535 | [HTTP_METH_CONNECT] = { "CONNECT", 7 }, |
| 536 | }; |
| 537 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 538 | /* |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 539 | * Adds a header and its CRLF at the tail of the message's buffer, just before |
| 540 | * the last CRLF. Text length is measured first, so it cannot be NULL. |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 541 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 542 | * of headers is automatically adjusted. The number of bytes added is returned |
| 543 | * on success, otherwise <0 is returned indicating an error. |
| 544 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 545 | 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] | 546 | { |
| 547 | int bytes, len; |
| 548 | |
| 549 | len = strlen(text); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 550 | bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 551 | if (!bytes) |
| 552 | return -1; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 553 | http_msg_move_end(msg, bytes); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 554 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 555 | } |
| 556 | |
| 557 | /* |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 558 | * Adds a header and its CRLF at the tail of the message's buffer, just before |
| 559 | * the last CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 560 | * the buffer is only opened and the space reserved, but nothing is copied. |
| 561 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 562 | * of headers is automatically adjusted. The number of bytes added is returned |
| 563 | * on success, otherwise <0 is returned indicating an error. |
| 564 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 565 | int http_header_add_tail2(struct http_msg *msg, |
| 566 | struct hdr_idx *hdr_idx, const char *text, int len) |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 567 | { |
| 568 | int bytes; |
| 569 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 570 | bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 571 | if (!bytes) |
| 572 | return -1; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 573 | http_msg_move_end(msg, bytes); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 574 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 575 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 576 | |
| 577 | /* |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 578 | * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon. |
| 579 | * If so, returns the position of the first non-space character relative to |
| 580 | * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries |
| 581 | * to return a pointer to the place after the first space. Returns 0 if the |
| 582 | * header name does not match. Checks are case-insensitive. |
| 583 | */ |
| 584 | int http_header_match2(const char *hdr, const char *end, |
| 585 | const char *name, int len) |
| 586 | { |
| 587 | const char *val; |
| 588 | |
| 589 | if (hdr + len >= end) |
| 590 | return 0; |
| 591 | if (hdr[len] != ':') |
| 592 | return 0; |
| 593 | if (strncasecmp(hdr, name, len) != 0) |
| 594 | return 0; |
| 595 | val = hdr + len + 1; |
| 596 | while (val < end && HTTP_IS_SPHT(*val)) |
| 597 | val++; |
| 598 | if ((val >= end) && (len + 2 <= end - hdr)) |
| 599 | return len + 2; /* we may replace starting from second space */ |
| 600 | return val - hdr; |
| 601 | } |
| 602 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 603 | /* Find the first or next occurrence of header <name> in message buffer <sol> |
| 604 | * using headers index <idx>, and return it in the <ctx> structure. This |
| 605 | * structure holds everything necessary to use the header and find next |
| 606 | * occurrence. If its <idx> member is 0, the header is searched from the |
| 607 | * beginning. Otherwise, the next occurrence is returned. The function returns |
| 608 | * 1 when it finds a value, and 0 when there is no more. It is very similar to |
| 609 | * http_find_header2() except that it is designed to work with full-line headers |
| 610 | * whose comma is not a delimiter but is part of the syntax. As a special case, |
| 611 | * if ctx->val is NULL when searching for a new values of a header, the current |
| 612 | * header is rescanned. This allows rescanning after a header deletion. |
| 613 | */ |
| 614 | int http_find_full_header2(const char *name, int len, |
| 615 | char *sol, struct hdr_idx *idx, |
| 616 | struct hdr_ctx *ctx) |
| 617 | { |
| 618 | char *eol, *sov; |
| 619 | int cur_idx, old_idx; |
| 620 | |
| 621 | cur_idx = ctx->idx; |
| 622 | if (cur_idx) { |
| 623 | /* We have previously returned a header, let's search another one */ |
| 624 | sol = ctx->line; |
| 625 | eol = sol + idx->v[cur_idx].len; |
| 626 | goto next_hdr; |
| 627 | } |
| 628 | |
| 629 | /* first request for this header */ |
| 630 | sol += hdr_idx_first_pos(idx); |
| 631 | old_idx = 0; |
| 632 | cur_idx = hdr_idx_first_idx(idx); |
| 633 | while (cur_idx) { |
| 634 | eol = sol + idx->v[cur_idx].len; |
| 635 | |
| 636 | if (len == 0) { |
| 637 | /* No argument was passed, we want any header. |
| 638 | * To achieve this, we simply build a fake request. */ |
| 639 | while (sol + len < eol && sol[len] != ':') |
| 640 | len++; |
| 641 | name = sol; |
| 642 | } |
| 643 | |
| 644 | if ((len < eol - sol) && |
| 645 | (sol[len] == ':') && |
| 646 | (strncasecmp(sol, name, len) == 0)) { |
| 647 | ctx->del = len; |
| 648 | sov = sol + len + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 649 | while (sov < eol && HTTP_IS_LWS(*sov)) |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 650 | sov++; |
| 651 | |
| 652 | ctx->line = sol; |
| 653 | ctx->prev = old_idx; |
| 654 | ctx->idx = cur_idx; |
| 655 | ctx->val = sov - sol; |
| 656 | ctx->tws = 0; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 657 | while (eol > sov && HTTP_IS_LWS(*(eol - 1))) { |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 658 | eol--; |
| 659 | ctx->tws++; |
| 660 | } |
| 661 | ctx->vlen = eol - sov; |
| 662 | return 1; |
| 663 | } |
| 664 | next_hdr: |
| 665 | sol = eol + idx->v[cur_idx].cr + 1; |
| 666 | old_idx = cur_idx; |
| 667 | cur_idx = idx->v[cur_idx].next; |
| 668 | } |
| 669 | return 0; |
| 670 | } |
| 671 | |
Willy Tarreau | c90dc23 | 2015-02-20 13:51:36 +0100 | [diff] [blame] | 672 | /* Find the first or next header field in message buffer <sol> using headers |
| 673 | * index <idx>, and return it in the <ctx> structure. This structure holds |
| 674 | * everything necessary to use the header and find next occurrence. If its |
| 675 | * <idx> member is 0, the first header is retrieved. Otherwise, the next |
| 676 | * occurrence is returned. The function returns 1 when it finds a value, and |
| 677 | * 0 when there is no more. It is equivalent to http_find_full_header2() with |
| 678 | * no header name. |
| 679 | */ |
| 680 | int http_find_next_header(char *sol, struct hdr_idx *idx, struct hdr_ctx *ctx) |
| 681 | { |
| 682 | char *eol, *sov; |
| 683 | int cur_idx, old_idx; |
| 684 | int len; |
| 685 | |
| 686 | cur_idx = ctx->idx; |
| 687 | if (cur_idx) { |
| 688 | /* We have previously returned a header, let's search another one */ |
| 689 | sol = ctx->line; |
| 690 | eol = sol + idx->v[cur_idx].len; |
| 691 | goto next_hdr; |
| 692 | } |
| 693 | |
| 694 | /* first request for this header */ |
| 695 | sol += hdr_idx_first_pos(idx); |
| 696 | old_idx = 0; |
| 697 | cur_idx = hdr_idx_first_idx(idx); |
| 698 | while (cur_idx) { |
| 699 | eol = sol + idx->v[cur_idx].len; |
| 700 | |
| 701 | len = 0; |
| 702 | while (1) { |
| 703 | if (len >= eol - sol) |
| 704 | goto next_hdr; |
| 705 | if (sol[len] == ':') |
| 706 | break; |
| 707 | len++; |
| 708 | } |
| 709 | |
| 710 | ctx->del = len; |
| 711 | sov = sol + len + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 712 | while (sov < eol && HTTP_IS_LWS(*sov)) |
Willy Tarreau | c90dc23 | 2015-02-20 13:51:36 +0100 | [diff] [blame] | 713 | sov++; |
| 714 | |
| 715 | ctx->line = sol; |
| 716 | ctx->prev = old_idx; |
| 717 | ctx->idx = cur_idx; |
| 718 | ctx->val = sov - sol; |
| 719 | ctx->tws = 0; |
| 720 | |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 721 | while (eol > sov && HTTP_IS_LWS(*(eol - 1))) { |
Willy Tarreau | c90dc23 | 2015-02-20 13:51:36 +0100 | [diff] [blame] | 722 | eol--; |
| 723 | ctx->tws++; |
| 724 | } |
| 725 | ctx->vlen = eol - sov; |
| 726 | return 1; |
| 727 | |
| 728 | next_hdr: |
| 729 | sol = eol + idx->v[cur_idx].cr + 1; |
| 730 | old_idx = cur_idx; |
| 731 | cur_idx = idx->v[cur_idx].next; |
| 732 | } |
| 733 | return 0; |
| 734 | } |
| 735 | |
Lukas Tribus | 2395368 | 2017-04-28 13:24:30 +0000 | [diff] [blame] | 736 | /* Find the end of the header value contained between <s> and <e>. See RFC7230, |
| 737 | * 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] | 738 | * a valid result. This works for headers defined as comma-separated lists. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 739 | */ |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 740 | char *find_hdr_value_end(char *s, const char *e) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 741 | { |
| 742 | int quoted, qdpair; |
| 743 | |
| 744 | quoted = qdpair = 0; |
Willy Tarreau | e6d9c21 | 2016-11-05 18:23:38 +0100 | [diff] [blame] | 745 | |
| 746 | #if defined(__x86_64__) || \ |
| 747 | defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) || \ |
| 748 | defined(__ARM_ARCH_7A__) |
| 749 | /* speedup: skip everything not a comma nor a double quote */ |
| 750 | for (; s <= e - sizeof(int); s += sizeof(int)) { |
| 751 | unsigned int c = *(int *)s; // comma |
| 752 | unsigned int q = c; // quote |
| 753 | |
| 754 | c ^= 0x2c2c2c2c; // contains one zero on a comma |
| 755 | q ^= 0x22222222; // contains one zero on a quote |
| 756 | |
| 757 | c = (c - 0x01010101) & ~c; // contains 0x80 below a comma |
| 758 | q = (q - 0x01010101) & ~q; // contains 0x80 below a quote |
| 759 | |
| 760 | if ((c | q) & 0x80808080) |
| 761 | break; // found a comma or a quote |
| 762 | } |
| 763 | #endif |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 764 | for (; s < e; s++) { |
| 765 | if (qdpair) qdpair = 0; |
Willy Tarreau | 0f7f51f | 2010-08-30 11:06:34 +0200 | [diff] [blame] | 766 | else if (quoted) { |
| 767 | if (*s == '\\') qdpair = 1; |
| 768 | else if (*s == '"') quoted = 0; |
| 769 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 770 | else if (*s == '"') quoted = 1; |
| 771 | else if (*s == ',') return s; |
| 772 | } |
| 773 | return s; |
| 774 | } |
| 775 | |
| 776 | /* Find the first or next occurrence of header <name> in message buffer <sol> |
| 777 | * using headers index <idx>, and return it in the <ctx> structure. This |
| 778 | * structure holds everything necessary to use the header and find next |
| 779 | * occurrence. If its <idx> member is 0, the header is searched from the |
| 780 | * beginning. Otherwise, the next occurrence is returned. The function returns |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 781 | * 1 when it finds a value, and 0 when there is no more. It is designed to work |
| 782 | * with headers defined as comma-separated lists. As a special case, if ctx->val |
| 783 | * is NULL when searching for a new values of a header, the current header is |
| 784 | * rescanned. This allows rescanning after a header deletion. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 785 | */ |
| 786 | int http_find_header2(const char *name, int len, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 787 | char *sol, struct hdr_idx *idx, |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 788 | struct hdr_ctx *ctx) |
| 789 | { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 790 | char *eol, *sov; |
| 791 | int cur_idx, old_idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 792 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 793 | cur_idx = ctx->idx; |
| 794 | if (cur_idx) { |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 795 | /* We have previously returned a value, let's search |
| 796 | * another one on the same line. |
| 797 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 798 | sol = ctx->line; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 799 | ctx->del = ctx->val + ctx->vlen + ctx->tws; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 800 | sov = sol + ctx->del; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 801 | eol = sol + idx->v[cur_idx].len; |
| 802 | |
| 803 | if (sov >= eol) |
| 804 | /* no more values in this header */ |
| 805 | goto next_hdr; |
| 806 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 807 | /* values remaining for this header, skip the comma but save it |
| 808 | * for later use (eg: for header deletion). |
| 809 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 810 | sov++; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 811 | while (sov < eol && HTTP_IS_LWS((*sov))) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 812 | sov++; |
| 813 | |
| 814 | goto return_hdr; |
| 815 | } |
| 816 | |
| 817 | /* first request for this header */ |
| 818 | sol += hdr_idx_first_pos(idx); |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 819 | old_idx = 0; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 820 | cur_idx = hdr_idx_first_idx(idx); |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 821 | while (cur_idx) { |
| 822 | eol = sol + idx->v[cur_idx].len; |
| 823 | |
Willy Tarreau | 1ad7c6d | 2007-06-10 21:42:55 +0200 | [diff] [blame] | 824 | if (len == 0) { |
| 825 | /* No argument was passed, we want any header. |
| 826 | * To achieve this, we simply build a fake request. */ |
| 827 | while (sol + len < eol && sol[len] != ':') |
| 828 | len++; |
| 829 | name = sol; |
| 830 | } |
| 831 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 832 | if ((len < eol - sol) && |
| 833 | (sol[len] == ':') && |
| 834 | (strncasecmp(sol, name, len) == 0)) { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 835 | ctx->del = len; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 836 | sov = sol + len + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 837 | while (sov < eol && HTTP_IS_LWS(*sov)) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 838 | sov++; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 839 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 840 | ctx->line = sol; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 841 | ctx->prev = old_idx; |
| 842 | return_hdr: |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 843 | ctx->idx = cur_idx; |
| 844 | ctx->val = sov - sol; |
| 845 | |
| 846 | eol = find_hdr_value_end(sov, eol); |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 847 | ctx->tws = 0; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 848 | while (eol > sov && HTTP_IS_LWS(*(eol - 1))) { |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 849 | eol--; |
| 850 | ctx->tws++; |
| 851 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 852 | ctx->vlen = eol - sov; |
| 853 | return 1; |
| 854 | } |
| 855 | next_hdr: |
| 856 | sol = eol + idx->v[cur_idx].cr + 1; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 857 | old_idx = cur_idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 858 | cur_idx = idx->v[cur_idx].next; |
| 859 | } |
| 860 | return 0; |
| 861 | } |
| 862 | |
| 863 | int http_find_header(const char *name, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 864 | char *sol, struct hdr_idx *idx, |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 865 | struct hdr_ctx *ctx) |
| 866 | { |
| 867 | return http_find_header2(name, strlen(name), sol, idx, ctx); |
| 868 | } |
| 869 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 870 | /* Remove one value of a header. This only works on a <ctx> returned by one of |
| 871 | * the http_find_header functions. The value is removed, as well as surrounding |
| 872 | * 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] | 873 | * 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] | 874 | * message <msg>. The new index is returned. If it is zero, it means there is |
| 875 | * no more header, so any processing may stop. The ctx is always left in a form |
| 876 | * that can be handled by http_find_header2() to find next occurrence. |
| 877 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 878 | 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] | 879 | { |
| 880 | int cur_idx = ctx->idx; |
| 881 | char *sol = ctx->line; |
| 882 | struct hdr_idx_elem *hdr; |
| 883 | int delta, skip_comma; |
| 884 | |
| 885 | if (!cur_idx) |
| 886 | return 0; |
| 887 | |
| 888 | hdr = &idx->v[cur_idx]; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 889 | if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 890 | /* This was the only value of the header, we must now remove it entirely. */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 891 | delta = buffer_replace2(msg->chn->buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0); |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 892 | http_msg_move_end(msg, delta); |
| 893 | idx->used--; |
| 894 | hdr->len = 0; /* unused entry */ |
| 895 | idx->v[ctx->prev].next = idx->v[ctx->idx].next; |
Willy Tarreau | 5c4784f | 2011-02-12 13:07:35 +0100 | [diff] [blame] | 896 | if (idx->tail == ctx->idx) |
| 897 | idx->tail = ctx->prev; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 898 | ctx->idx = ctx->prev; /* walk back to the end of previous header */ |
Willy Tarreau | 7c1c217 | 2015-01-07 17:23:50 +0100 | [diff] [blame] | 899 | 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] | 900 | 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] | 901 | ctx->tws = ctx->vlen = 0; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 902 | return ctx->idx; |
| 903 | } |
| 904 | |
| 905 | /* 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] | 906 | * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the |
| 907 | * last entry of the list, we remove the last separator. |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 908 | */ |
| 909 | |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 910 | skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 911 | delta = buffer_replace2(msg->chn->buf, sol + ctx->del + skip_comma, |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 912 | sol + ctx->val + ctx->vlen + ctx->tws + skip_comma, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 913 | NULL, 0); |
| 914 | hdr->len += delta; |
| 915 | http_msg_move_end(msg, delta); |
| 916 | ctx->val = ctx->del; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 917 | ctx->tws = ctx->vlen = 0; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 918 | return ctx->idx; |
| 919 | } |
| 920 | |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 921 | /* This function handles a server error at the stream interface level. The |
| 922 | * 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] | 923 | * message is copied into the input buffer. |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 924 | * 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] | 925 | * in this buffer will be lost. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 926 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 927 | static void http_server_error(struct stream *s, struct stream_interface *si, |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 928 | int err, int finst, const struct chunk *msg) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 929 | { |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 930 | FLT_STRM_CB(s, flt_http_reply(s, s->txn->status, msg)); |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 931 | channel_auto_read(si_oc(si)); |
| 932 | channel_abort(si_oc(si)); |
| 933 | channel_auto_close(si_oc(si)); |
| 934 | channel_erase(si_oc(si)); |
| 935 | channel_auto_close(si_ic(si)); |
| 936 | channel_auto_read(si_ic(si)); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 937 | if (msg) |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 938 | co_inject(si_ic(si), msg->str, msg->len); |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 939 | if (!(s->flags & SF_ERR_MASK)) |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 940 | s->flags |= err; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 941 | if (!(s->flags & SF_FINST_MASK)) |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 942 | s->flags |= finst; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 943 | } |
| 944 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 945 | /* This function returns the appropriate error location for the given stream |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 946 | * and message. |
| 947 | */ |
| 948 | |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 949 | struct chunk *http_error_message(struct stream *s) |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 950 | { |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 951 | const int msgnum = http_get_status_idx(s->txn->status); |
| 952 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 953 | if (s->be->errmsg[msgnum].str) |
| 954 | return &s->be->errmsg[msgnum]; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 955 | else if (strm_fe(s)->errmsg[msgnum].str) |
| 956 | return &strm_fe(s)->errmsg[msgnum]; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 957 | else |
| 958 | return &http_err_chunks[msgnum]; |
| 959 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 960 | |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 961 | void |
| 962 | http_reply_and_close(struct stream *s, short status, struct chunk *msg) |
| 963 | { |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 964 | s->txn->flags &= ~TX_WAIT_NEXT_RQ; |
Christopher Faulet | 3e34429 | 2015-11-24 16:24:13 +0100 | [diff] [blame] | 965 | FLT_STRM_CB(s, flt_http_reply(s, status, msg)); |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 966 | stream_int_retnclose(&s->si[0], msg); |
| 967 | } |
| 968 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 969 | /* |
Willy Tarreau | b7ce424 | 2015-09-03 17:15:21 +0200 | [diff] [blame] | 970 | * returns a known method among HTTP_METH_* or HTTP_METH_OTHER for all unknown |
| 971 | * ones. |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 972 | */ |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 973 | enum http_meth_t find_http_meth(const char *str, const int len) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 974 | { |
| 975 | unsigned char m; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 976 | const struct http_method_desc *h; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 977 | |
| 978 | m = ((unsigned)*str - 'A'); |
| 979 | |
| 980 | if (m < 26) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 981 | for (h = http_methods[m]; h->len > 0; h++) { |
| 982 | if (unlikely(h->len != len)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 983 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 984 | if (likely(memcmp(str, h->text, h->len) == 0)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 985 | return h->meth; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 986 | }; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 987 | } |
Willy Tarreau | b7ce424 | 2015-09-03 17:15:21 +0200 | [diff] [blame] | 988 | return HTTP_METH_OTHER; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 989 | } |
| 990 | |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 991 | /* Parse the URI from the given transaction (which is assumed to be in request |
| 992 | * phase) and look for the "/" beginning the PATH. If not found, return NULL. |
| 993 | * It is returned otherwise. |
| 994 | */ |
Thierry FOURNIER | 3c33178 | 2015-09-17 19:33:35 +0200 | [diff] [blame] | 995 | char *http_get_path(struct http_txn *txn) |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 996 | { |
| 997 | char *ptr, *end; |
| 998 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 999 | ptr = txn->req.chn->buf->p + txn->req.sl.rq.u; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 1000 | end = ptr + txn->req.sl.rq.u_l; |
| 1001 | |
| 1002 | if (ptr >= end) |
| 1003 | return NULL; |
| 1004 | |
Lukas Tribus | 2395368 | 2017-04-28 13:24:30 +0000 | [diff] [blame] | 1005 | /* RFC7230, par. 2.7 : |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 1006 | * Request-URI = "*" | absuri | abspath | authority |
| 1007 | */ |
| 1008 | |
| 1009 | if (*ptr == '*') |
| 1010 | return NULL; |
| 1011 | |
| 1012 | if (isalpha((unsigned char)*ptr)) { |
| 1013 | /* this is a scheme as described by RFC3986, par. 3.1 */ |
| 1014 | ptr++; |
| 1015 | while (ptr < end && |
| 1016 | (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')) |
| 1017 | ptr++; |
| 1018 | /* skip '://' */ |
| 1019 | if (ptr == end || *ptr++ != ':') |
| 1020 | return NULL; |
| 1021 | if (ptr == end || *ptr++ != '/') |
| 1022 | return NULL; |
| 1023 | if (ptr == end || *ptr++ != '/') |
| 1024 | return NULL; |
| 1025 | } |
| 1026 | /* skip [user[:passwd]@]host[:[port]] */ |
| 1027 | |
| 1028 | while (ptr < end && *ptr != '/') |
| 1029 | ptr++; |
| 1030 | |
| 1031 | if (ptr == end) |
| 1032 | return NULL; |
| 1033 | |
| 1034 | /* OK, we got the '/' ! */ |
| 1035 | return ptr; |
| 1036 | } |
| 1037 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 1038 | /* Parse the URI from the given string and look for the "/" beginning the PATH. |
| 1039 | * If not found, return NULL. It is returned otherwise. |
| 1040 | */ |
| 1041 | static char * |
| 1042 | http_get_path_from_string(char *str) |
| 1043 | { |
| 1044 | char *ptr = str; |
| 1045 | |
| 1046 | /* RFC2616, par. 5.1.2 : |
| 1047 | * Request-URI = "*" | absuri | abspath | authority |
| 1048 | */ |
| 1049 | |
| 1050 | if (*ptr == '*') |
| 1051 | return NULL; |
| 1052 | |
| 1053 | if (isalpha((unsigned char)*ptr)) { |
| 1054 | /* this is a scheme as described by RFC3986, par. 3.1 */ |
| 1055 | ptr++; |
| 1056 | while (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.') |
| 1057 | ptr++; |
| 1058 | /* skip '://' */ |
| 1059 | if (*ptr == '\0' || *ptr++ != ':') |
| 1060 | return NULL; |
| 1061 | if (*ptr == '\0' || *ptr++ != '/') |
| 1062 | return NULL; |
| 1063 | if (*ptr == '\0' || *ptr++ != '/') |
| 1064 | return NULL; |
| 1065 | } |
| 1066 | /* skip [user[:passwd]@]host[:[port]] */ |
| 1067 | |
| 1068 | while (*ptr != '\0' && *ptr != ' ' && *ptr != '/') |
| 1069 | ptr++; |
| 1070 | |
| 1071 | if (*ptr == '\0' || *ptr == ' ') |
| 1072 | return NULL; |
| 1073 | |
| 1074 | /* OK, we got the '/' ! */ |
| 1075 | return ptr; |
| 1076 | } |
| 1077 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 1078 | /* Returns a 302 for a redirectable request that reaches a server working in |
| 1079 | * in redirect mode. This may only be called just after the stream interface |
| 1080 | * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will |
| 1081 | * follow normal proxy processing. NOTE: this function is designed to support |
| 1082 | * being called once data are scheduled for forwarding. |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1083 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1084 | void http_perform_server_redirect(struct stream *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1085 | { |
| 1086 | struct http_txn *txn; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 1087 | struct server *srv; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1088 | char *path; |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 1089 | int len, rewind; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1090 | |
| 1091 | /* 1: create the response header */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1092 | trash.len = strlen(HTTP_302); |
| 1093 | memcpy(trash.str, HTTP_302, trash.len); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1094 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 1095 | srv = objt_server(s->target); |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 1096 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1097 | /* 2: add the server's prefix */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1098 | if (trash.len + srv->rdr_len > trash.size) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1099 | return; |
| 1100 | |
Willy Tarreau | dcb75c4 | 2010-01-10 00:24:22 +0100 | [diff] [blame] | 1101 | /* special prefix "/" means don't change URL */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 1102 | if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1103 | memcpy(trash.str + trash.len, srv->rdr_pfx, srv->rdr_len); |
| 1104 | trash.len += srv->rdr_len; |
Willy Tarreau | dcb75c4 | 2010-01-10 00:24:22 +0100 | [diff] [blame] | 1105 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1106 | |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 1107 | /* 3: add the request URI. Since it was already forwarded, we need |
| 1108 | * to temporarily rewind the buffer. |
| 1109 | */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 1110 | txn = s->txn; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1111 | b_rew(s->req.buf, rewind = http_hdr_rewind(&txn->req)); |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 1112 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1113 | path = http_get_path(txn); |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1114 | len = buffer_count(s->req.buf, path, b_ptr(s->req.buf, txn->req.sl.rq.u + txn->req.sl.rq.u_l)); |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 1115 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1116 | b_adv(s->req.buf, rewind); |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 1117 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1118 | if (!path) |
| 1119 | return; |
| 1120 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1121 | if (trash.len + len > trash.size - 4) /* 4 for CRLF-CRLF */ |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1122 | return; |
| 1123 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1124 | memcpy(trash.str + trash.len, path, len); |
| 1125 | trash.len += len; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1126 | |
| 1127 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1128 | memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29); |
| 1129 | trash.len += 29; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1130 | } else { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1131 | memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23); |
| 1132 | trash.len += 23; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1133 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1134 | |
| 1135 | /* prepare to return without error. */ |
Willy Tarreau | 73b013b | 2012-05-21 16:31:45 +0200 | [diff] [blame] | 1136 | si_shutr(si); |
| 1137 | si_shutw(si); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1138 | si->err_type = SI_ET_NONE; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1139 | si->state = SI_ST_CLO; |
| 1140 | |
| 1141 | /* send the message */ |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 1142 | txn->status = 302; |
| 1143 | http_server_error(s, si, SF_ERR_LOCAL, SF_FINST_C, &trash); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1144 | |
| 1145 | /* 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] | 1146 | srv_inc_sess_ctr(srv); |
Bhaskar Maddala | a20cb85 | 2014-02-03 16:26:46 -0500 | [diff] [blame] | 1147 | srv_set_sess_last(srv); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1148 | } |
| 1149 | |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 1150 | /* Return the error message corresponding to si->err_type. It is assumed |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1151 | * that the server side is closed. Note that err_type is actually a |
| 1152 | * bitmask, where almost only aborts may be cumulated with other |
| 1153 | * values. We consider that aborted operations are more important |
| 1154 | * than timeouts or errors due to the fact that nobody else in the |
| 1155 | * logs might explain incomplete retries. All others should avoid |
| 1156 | * being cumulated. It should normally not be possible to have multiple |
| 1157 | * 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] | 1158 | * Note that connection errors appearing on the second request of a keep-alive |
| 1159 | * connection are not reported since this allows the client to retry. |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1160 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1161 | void http_return_srv_error(struct stream *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1162 | { |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 1163 | int err_type = si->err_type; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1164 | |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 1165 | /* set s->txn->status for http_error_message(s) */ |
| 1166 | s->txn->status = 503; |
| 1167 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1168 | if (err_type & SI_ET_QUEUE_ABRT) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1169 | http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q, |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 1170 | http_error_message(s)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1171 | else if (err_type & SI_ET_CONN_ABRT) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1172 | http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C, |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 1173 | (s->txn->flags & TX_NOT_FIRST) ? NULL : |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 1174 | http_error_message(s)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1175 | else if (err_type & SI_ET_QUEUE_TO) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1176 | http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q, |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 1177 | http_error_message(s)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1178 | else if (err_type & SI_ET_QUEUE_ERR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1179 | http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q, |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 1180 | http_error_message(s)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1181 | else if (err_type & SI_ET_CONN_TO) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1182 | http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C, |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 1183 | (s->txn->flags & TX_NOT_FIRST) ? NULL : |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 1184 | http_error_message(s)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1185 | else if (err_type & SI_ET_CONN_ERR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1186 | http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C, |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 1187 | (s->flags & SF_SRV_REUSED) ? NULL : |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 1188 | http_error_message(s)); |
Willy Tarreau | 2d400bb | 2012-05-14 12:11:47 +0200 | [diff] [blame] | 1189 | else if (err_type & SI_ET_CONN_RES) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1190 | http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C, |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 1191 | (s->txn->flags & TX_NOT_FIRST) ? NULL : |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 1192 | http_error_message(s)); |
| 1193 | else { /* SI_ET_CONN_OTHER and others */ |
| 1194 | s->txn->status = 500; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1195 | http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C, |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 1196 | http_error_message(s)); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 1197 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1198 | } |
| 1199 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1200 | extern const char sess_term_cond[8]; |
| 1201 | extern const char sess_fin_state[8]; |
| 1202 | extern const char *monthname[12]; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 1203 | struct pool_head *pool_head_http_txn; |
| 1204 | struct pool_head *pool_head_requri; |
| 1205 | struct pool_head *pool_head_capture = NULL; |
| 1206 | struct pool_head *pool_head_uniqueid; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1207 | |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1208 | /* |
| 1209 | * Capture headers from message starting at <som> according to header list |
Willy Tarreau | 54da8db | 2014-06-13 16:11:48 +0200 | [diff] [blame] | 1210 | * <cap_hdr>, and fill the <cap> pointers appropriately. |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1211 | */ |
| 1212 | void capture_headers(char *som, struct hdr_idx *idx, |
| 1213 | char **cap, struct cap_hdr *cap_hdr) |
| 1214 | { |
| 1215 | char *eol, *sol, *col, *sov; |
| 1216 | int cur_idx; |
| 1217 | struct cap_hdr *h; |
| 1218 | int len; |
| 1219 | |
| 1220 | sol = som + hdr_idx_first_pos(idx); |
| 1221 | cur_idx = hdr_idx_first_idx(idx); |
| 1222 | |
| 1223 | while (cur_idx) { |
| 1224 | eol = sol + idx->v[cur_idx].len; |
| 1225 | |
| 1226 | col = sol; |
| 1227 | while (col < eol && *col != ':') |
| 1228 | col++; |
| 1229 | |
| 1230 | sov = col + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 1231 | while (sov < eol && HTTP_IS_LWS(*sov)) |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1232 | sov++; |
| 1233 | |
| 1234 | for (h = cap_hdr; h; h = h->next) { |
Willy Tarreau | 54da8db | 2014-06-13 16:11:48 +0200 | [diff] [blame] | 1235 | if (h->namelen && (h->namelen == col - sol) && |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1236 | (strncasecmp(sol, h->name, h->namelen) == 0)) { |
| 1237 | if (cap[h->index] == NULL) |
| 1238 | cap[h->index] = |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 1239 | pool_alloc(h->pool); |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1240 | |
| 1241 | if (cap[h->index] == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1242 | ha_alert("HTTP capture : out of memory.\n"); |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1243 | continue; |
| 1244 | } |
| 1245 | |
| 1246 | len = eol - sov; |
| 1247 | if (len > h->len) |
| 1248 | len = h->len; |
| 1249 | |
| 1250 | memcpy(cap[h->index], sov, len); |
| 1251 | cap[h->index][len]=0; |
| 1252 | } |
| 1253 | } |
| 1254 | sol = eol + idx->v[cur_idx].cr + 1; |
| 1255 | cur_idx = idx->v[cur_idx].next; |
| 1256 | } |
| 1257 | } |
| 1258 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1259 | /* |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1260 | * Returns the data from Authorization header. Function may be called more |
| 1261 | * than once so data is stored in txn->auth_data. When no header is found |
| 1262 | * 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] | 1263 | * searching again for something we are unable to find anyway. However, if |
| 1264 | * 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] | 1265 | * have the credentials overwritten by another stream in parallel. |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1266 | */ |
| 1267 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1268 | int |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1269 | get_http_auth(struct stream *s) |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1270 | { |
| 1271 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 1272 | struct http_txn *txn = s->txn; |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1273 | struct chunk auth_method; |
| 1274 | struct hdr_ctx ctx; |
| 1275 | char *h, *p; |
| 1276 | int len; |
| 1277 | |
| 1278 | #ifdef DEBUG_AUTH |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1279 | printf("Auth for stream %p: %d\n", s, txn->auth.method); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1280 | #endif |
| 1281 | |
| 1282 | if (txn->auth.method == HTTP_AUTH_WRONG) |
| 1283 | return 0; |
| 1284 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1285 | txn->auth.method = HTTP_AUTH_WRONG; |
| 1286 | |
| 1287 | ctx.idx = 0; |
Willy Tarreau | 844a7e7 | 2010-01-31 21:46:18 +0100 | [diff] [blame] | 1288 | |
| 1289 | if (txn->flags & TX_USE_PX_CONN) { |
| 1290 | h = "Proxy-Authorization"; |
| 1291 | len = strlen(h); |
| 1292 | } else { |
| 1293 | h = "Authorization"; |
| 1294 | len = strlen(h); |
| 1295 | } |
| 1296 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1297 | if (!http_find_header2(h, len, s->req.buf->p, &txn->hdr_idx, &ctx)) |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1298 | return 0; |
| 1299 | |
| 1300 | h = ctx.line + ctx.val; |
| 1301 | |
| 1302 | p = memchr(h, ' ', ctx.vlen); |
Willy Tarreau | 5c557d1 | 2016-03-13 08:17:02 +0100 | [diff] [blame] | 1303 | len = p - h; |
| 1304 | if (!p || len <= 0) |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1305 | return 0; |
| 1306 | |
David Carlier | 7365f7d | 2016-04-04 11:54:42 +0100 | [diff] [blame] | 1307 | if (chunk_initlen(&auth_method, h, 0, len) != 1) |
| 1308 | return 0; |
| 1309 | |
Willy Tarreau | 5c557d1 | 2016-03-13 08:17:02 +0100 | [diff] [blame] | 1310 | 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] | 1311 | |
| 1312 | if (!strncasecmp("Basic", auth_method.str, auth_method.len)) { |
Christopher Faulet | 6988f67 | 2017-07-27 15:18:52 +0200 | [diff] [blame] | 1313 | struct chunk *http_auth = get_trash_chunk(); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1314 | |
| 1315 | len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len, |
Christopher Faulet | 6988f67 | 2017-07-27 15:18:52 +0200 | [diff] [blame] | 1316 | http_auth->str, global.tune.bufsize - 1); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1317 | |
| 1318 | if (len < 0) |
| 1319 | return 0; |
| 1320 | |
| 1321 | |
Christopher Faulet | 6988f67 | 2017-07-27 15:18:52 +0200 | [diff] [blame] | 1322 | http_auth->str[len] = '\0'; |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1323 | |
Christopher Faulet | 6988f67 | 2017-07-27 15:18:52 +0200 | [diff] [blame] | 1324 | p = strchr(http_auth->str, ':'); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1325 | |
| 1326 | if (!p) |
| 1327 | return 0; |
| 1328 | |
Christopher Faulet | 6988f67 | 2017-07-27 15:18:52 +0200 | [diff] [blame] | 1329 | txn->auth.user = http_auth->str; |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1330 | *p = '\0'; |
| 1331 | txn->auth.pass = p+1; |
| 1332 | |
| 1333 | txn->auth.method = HTTP_AUTH_BASIC; |
| 1334 | return 1; |
| 1335 | } |
| 1336 | |
| 1337 | return 0; |
| 1338 | } |
| 1339 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1340 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1341 | /* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the |
| 1342 | * conversion succeeded, 0 in case of error. If the request was already 1.X, |
| 1343 | * nothing is done and 1 is returned. |
| 1344 | */ |
Willy Tarreau | 418bfcc | 2012-03-09 13:56:20 +0100 | [diff] [blame] | 1345 | static int http_upgrade_v09_to_v10(struct http_txn *txn) |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1346 | { |
| 1347 | int delta; |
| 1348 | char *cur_end; |
Willy Tarreau | 418bfcc | 2012-03-09 13:56:20 +0100 | [diff] [blame] | 1349 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1350 | |
| 1351 | if (msg->sl.rq.v_l != 0) |
| 1352 | return 1; |
| 1353 | |
Apollon Oikonomopoulos | 25a1522 | 2014-04-06 02:46:00 +0300 | [diff] [blame] | 1354 | /* RFC 1945 allows only GET for HTTP/0.9 requests */ |
| 1355 | if (txn->meth != HTTP_METH_GET) |
| 1356 | return 0; |
| 1357 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1358 | cur_end = msg->chn->buf->p + msg->sl.rq.l; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1359 | |
| 1360 | if (msg->sl.rq.u_l == 0) { |
Apollon Oikonomopoulos | 25a1522 | 2014-04-06 02:46:00 +0300 | [diff] [blame] | 1361 | /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */ |
| 1362 | return 0; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1363 | } |
| 1364 | /* add HTTP version */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1365 | delta = buffer_replace2(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] | 1366 | http_msg_move_end(msg, delta); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1367 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1368 | cur_end = (char *)http_parse_reqline(msg, |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1369 | HTTP_MSG_RQMETH, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1370 | msg->chn->buf->p, cur_end + 1, |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1371 | NULL, NULL); |
| 1372 | if (unlikely(!cur_end)) |
| 1373 | return 0; |
| 1374 | |
| 1375 | /* we have a full HTTP/1.0 request now and we know that |
| 1376 | * we have either a CR or an LF at <ptr>. |
| 1377 | */ |
| 1378 | hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r'); |
| 1379 | return 1; |
| 1380 | } |
| 1381 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1382 | /* Parse the Connection: header of an HTTP request, looking for both "close" |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1383 | * and "keep-alive" values. If we already know that some headers may safely |
| 1384 | * 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] | 1385 | * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses) |
| 1386 | * - 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] | 1387 | * Presence of the "Upgrade" token is also checked and reported. |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1388 | * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was |
| 1389 | * found, and TX_CON_*_SET is adjusted depending on what is left so only |
| 1390 | * harmless combinations may be removed. Do not call that after changes have |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1391 | * been processed. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1392 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1393 | 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] | 1394 | { |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1395 | struct hdr_ctx ctx; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1396 | const char *hdr_val = "Connection"; |
| 1397 | int hdr_len = 10; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1398 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1399 | if (txn->flags & TX_HDR_CONN_PRS) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1400 | return; |
| 1401 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1402 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1403 | hdr_val = "Proxy-Connection"; |
| 1404 | hdr_len = 16; |
| 1405 | } |
| 1406 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1407 | ctx.idx = 0; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1408 | txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1409 | while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1410 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 1411 | txn->flags |= TX_HDR_CONN_KAL; |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1412 | if (to_del & 2) |
| 1413 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1414 | else |
| 1415 | txn->flags |= TX_CON_KAL_SET; |
| 1416 | } |
| 1417 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 1418 | txn->flags |= TX_HDR_CONN_CLO; |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1419 | if (to_del & 1) |
| 1420 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1421 | else |
| 1422 | txn->flags |= TX_CON_CLO_SET; |
| 1423 | } |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 1424 | else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) { |
| 1425 | txn->flags |= TX_HDR_CONN_UPG; |
| 1426 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1427 | } |
| 1428 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1429 | txn->flags |= TX_HDR_CONN_PRS; |
| 1430 | return; |
| 1431 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1432 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1433 | /* Apply desired changes on the Connection: header. Values may be removed and/or |
| 1434 | * added depending on the <wanted> flags, which are exclusively composed of |
| 1435 | * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The |
| 1436 | * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left. |
| 1437 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1438 | 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] | 1439 | { |
| 1440 | struct hdr_ctx ctx; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1441 | const char *hdr_val = "Connection"; |
| 1442 | int hdr_len = 10; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1443 | |
| 1444 | ctx.idx = 0; |
| 1445 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1446 | |
| 1447 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1448 | hdr_val = "Proxy-Connection"; |
| 1449 | hdr_len = 16; |
| 1450 | } |
| 1451 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1452 | txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1453 | while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1454 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 1455 | if (wanted & TX_CON_KAL_SET) |
| 1456 | txn->flags |= TX_CON_KAL_SET; |
| 1457 | else |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1458 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1459 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1460 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 1461 | if (wanted & TX_CON_CLO_SET) |
| 1462 | txn->flags |= TX_CON_CLO_SET; |
| 1463 | else |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1464 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 1465 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1466 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1467 | |
| 1468 | if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 1469 | return; |
| 1470 | |
| 1471 | if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) { |
| 1472 | txn->flags |= TX_CON_CLO_SET; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1473 | hdr_val = "Connection: close"; |
| 1474 | hdr_len = 17; |
| 1475 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1476 | hdr_val = "Proxy-Connection: close"; |
| 1477 | hdr_len = 23; |
| 1478 | } |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1479 | http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1480 | } |
| 1481 | |
| 1482 | if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) { |
| 1483 | txn->flags |= TX_CON_KAL_SET; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1484 | hdr_val = "Connection: keep-alive"; |
| 1485 | hdr_len = 22; |
| 1486 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1487 | hdr_val = "Proxy-Connection: keep-alive"; |
| 1488 | hdr_len = 28; |
| 1489 | } |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1490 | http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1491 | } |
| 1492 | return; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1493 | } |
| 1494 | |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 1495 | /* Parses a qvalue and returns it multipled by 1000, from 0 to 1000. If the |
| 1496 | * value is larger than 1000, it is bound to 1000. The parser consumes up to |
| 1497 | * 1 digit, one dot and 3 digits and stops on the first invalid character. |
| 1498 | * Unparsable qvalues return 1000 as "q=1.000". |
| 1499 | */ |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 1500 | int parse_qvalue(const char *qvalue, const char **end) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 1501 | { |
| 1502 | int q = 1000; |
| 1503 | |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 1504 | if (!isdigit((unsigned char)*qvalue)) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 1505 | goto out; |
| 1506 | q = (*qvalue++ - '0') * 1000; |
| 1507 | |
| 1508 | if (*qvalue++ != '.') |
| 1509 | goto out; |
| 1510 | |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 1511 | if (!isdigit((unsigned char)*qvalue)) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 1512 | goto out; |
| 1513 | q += (*qvalue++ - '0') * 100; |
| 1514 | |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 1515 | if (!isdigit((unsigned char)*qvalue)) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 1516 | goto out; |
| 1517 | q += (*qvalue++ - '0') * 10; |
| 1518 | |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 1519 | if (!isdigit((unsigned char)*qvalue)) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 1520 | goto out; |
| 1521 | q += (*qvalue++ - '0') * 1; |
| 1522 | out: |
| 1523 | if (q > 1000) |
| 1524 | q = 1000; |
Willy Tarreau | 38b3aa5 | 2014-04-22 23:32:05 +0200 | [diff] [blame] | 1525 | if (end) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 1526 | *end = qvalue; |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 1527 | return q; |
| 1528 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 1529 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1530 | 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] | 1531 | { |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 1532 | struct proxy *fe = strm_fe(s); |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 1533 | int tmp = TX_CON_WANT_KAL; |
| 1534 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1535 | if (!((fe->options2|s->be->options2) & PR_O2_FAKE_KA)) { |
| 1536 | if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN || |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 1537 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN) |
| 1538 | tmp = TX_CON_WANT_TUN; |
| 1539 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1540 | if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 1541 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL) |
| 1542 | tmp = TX_CON_WANT_TUN; |
| 1543 | } |
| 1544 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1545 | if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL || |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 1546 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) { |
| 1547 | /* option httpclose + server_close => forceclose */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1548 | if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 1549 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL) |
| 1550 | tmp = TX_CON_WANT_CLO; |
| 1551 | else |
| 1552 | tmp = TX_CON_WANT_SCL; |
| 1553 | } |
| 1554 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1555 | if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL || |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 1556 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL) |
| 1557 | tmp = TX_CON_WANT_CLO; |
| 1558 | |
| 1559 | if ((txn->flags & TX_CON_WANT_MSK) < tmp) |
| 1560 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp; |
| 1561 | |
| 1562 | if (!(txn->flags & TX_HDR_CONN_PRS) && |
| 1563 | (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) { |
| 1564 | /* parse the Connection header and possibly clean it */ |
| 1565 | int to_del = 0; |
| 1566 | if ((msg->flags & HTTP_MSGF_VER_11) || |
| 1567 | ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1568 | !((fe->options2|s->be->options2) & PR_O2_FAKE_KA))) |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 1569 | to_del |= 2; /* remove "keep-alive" */ |
| 1570 | if (!(msg->flags & HTTP_MSGF_VER_11)) |
| 1571 | to_del |= 1; /* remove "close" */ |
| 1572 | http_parse_connection_header(txn, msg, to_del); |
| 1573 | } |
| 1574 | |
| 1575 | /* check if client or config asks for explicit close in KAL/SCL */ |
| 1576 | if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 1577 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) && |
| 1578 | ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */ |
| 1579 | (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */ |
| 1580 | !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1581 | fe->state == PR_STSTOPPED)) /* frontend is stopping */ |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 1582 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 1583 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 1584 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 1585 | /* This stream analyser waits for a complete HTTP request. It returns 1 if the |
| 1586 | * processing can continue on next analysers, or zero if it either needs more |
| 1587 | * data or wants to immediately abort the request (eg: timeout, error, ...). It |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1588 | * 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] | 1589 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 1590 | * abort. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1591 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1592 | 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] | 1593 | { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1594 | /* |
| 1595 | * We will parse the partial (or complete) lines. |
| 1596 | * We will check the request syntax, and also join multi-line |
| 1597 | * headers. An index of all the lines will be elaborated while |
| 1598 | * parsing. |
| 1599 | * |
| 1600 | * For the parsing, we use a 28 states FSM. |
| 1601 | * |
| 1602 | * Here is the information we currently have : |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1603 | * req->buf->p = beginning of request |
| 1604 | * req->buf->p + msg->eoh = end of processed headers / start of current one |
| 1605 | * req->buf->p + req->buf->i = end of input data |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 1606 | * msg->eol = end of current header or line (LF or CRLF) |
| 1607 | * msg->next = first non-visited byte |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 1608 | * |
| 1609 | * 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] | 1610 | * we will set a few fields (txn->meth, sn->flags/SF_REDIRECTABLE). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 1611 | * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and |
| 1612 | * finally headers capture. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1613 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1614 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1615 | int cur_idx; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 1616 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 1617 | struct http_txn *txn = s->txn; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1618 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 1619 | struct hdr_ctx ctx; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1620 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1621 | DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n", |
Willy Tarreau | 6bf1736 | 2009-02-24 10:48:35 +0100 | [diff] [blame] | 1622 | now_ms, __FUNCTION__, |
| 1623 | s, |
| 1624 | req, |
| 1625 | req->rex, req->wex, |
| 1626 | req->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1627 | req->buf->i, |
Willy Tarreau | 6bf1736 | 2009-02-24 10:48:35 +0100 | [diff] [blame] | 1628 | req->analysers); |
| 1629 | |
Willy Tarreau | 52a0c60 | 2009-08-16 22:45:38 +0200 | [diff] [blame] | 1630 | /* we're speaking HTTP here, so let's speak HTTP to the client */ |
| 1631 | s->srv_error = http_return_srv_error; |
| 1632 | |
Rian McGuire | 89fcb7d | 2018-04-24 11:19:21 -0300 | [diff] [blame] | 1633 | /* If there is data available for analysis, log the end of the idle time. */ |
| 1634 | if (buffer_not_empty(req->buf) && s->logs.t_idle == -1) |
| 1635 | s->logs.t_idle = tv_ms_elapsed(&s->logs.tv_accept, &now) - s->logs.t_handshake; |
| 1636 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 1637 | /* There's a protected area at the end of the buffer for rewriting |
| 1638 | * purposes. We don't want to start to parse the request if the |
| 1639 | * protected area is affected, because we may have to move processed |
| 1640 | * data later, which is much more complicated. |
| 1641 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1642 | if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) { |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 1643 | if (txn->flags & TX_NOT_FIRST) { |
Willy Tarreau | ba0902e | 2015-01-13 14:39:16 +0100 | [diff] [blame] | 1644 | if (unlikely(!channel_is_rewritable(req))) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 1645 | 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] | 1646 | goto failed_keep_alive; |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 1647 | /* 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] | 1648 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 1649 | req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | d7ad9f5 | 2013-12-31 17:26:25 +0100 | [diff] [blame] | 1650 | req->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 1651 | return 0; |
| 1652 | } |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 1653 | if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) || |
| 1654 | bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite)) |
| 1655 | buffer_slow_realign(req->buf); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 1656 | } |
| 1657 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1658 | if (likely(msg->next < req->buf->i)) /* some unparsed data are available */ |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 1659 | http_msg_analyzer(msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 1660 | } |
| 1661 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1662 | /* 1: we might have to print this header in debug mode */ |
| 1663 | if (unlikely((global.mode & MODE_DEBUG) && |
| 1664 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | 7d59e90 | 2014-10-21 19:36:09 +0200 | [diff] [blame] | 1665 | msg->msg_state >= HTTP_MSG_BODY)) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1666 | char *eol, *sol; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1667 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1668 | sol = req->buf->p; |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 1669 | /* this is a bit complex : in case of error on the request line, |
| 1670 | * we know that rq.l is still zero, so we display only the part |
| 1671 | * up to the end of the line (truncated by debug_hdr). |
| 1672 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1673 | eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : req->buf->i); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1674 | debug_hdr("clireq", s, sol, eol); |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 1675 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1676 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 1677 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1678 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1679 | while (cur_idx) { |
| 1680 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 1681 | debug_hdr("clihdr", s, sol, eol); |
| 1682 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 1683 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1684 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1685 | } |
| 1686 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1687 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1688 | /* |
| 1689 | * Now we quickly check if we have found a full valid request. |
| 1690 | * If not so, we check the FD and buffer states before leaving. |
| 1691 | * A full request is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 1692 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 1693 | * requests are checked first. When waiting for a second request |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1694 | * on a keep-alive stream, if we encounter and error, close, t/o, |
| 1695 | * 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] | 1696 | * 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] | 1697 | * process_stream() as a frontend error. |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 1698 | * Last, we may increase some tracked counters' http request errors on |
| 1699 | * the cases that are deliberately the client's fault. For instance, |
| 1700 | * a timeout or connection reset is not counted as an error. However |
| 1701 | * a bad request is. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1702 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1703 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 1704 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1705 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1706 | * First, let's catch bad requests. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1707 | */ |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 1708 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1709 | stream_inc_http_req_ctr(s); |
| 1710 | stream_inc_http_err_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1711 | proxy_inc_fe_req_ctr(sess->fe); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1712 | goto return_bad_req; |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 1713 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1714 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1715 | /* 1: Since we are in header mode, if there's no space |
| 1716 | * left for headers, we won't be able to free more |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1717 | * later, so the stream will never terminate. We |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1718 | * must terminate it now. |
| 1719 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1720 | if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1721 | /* FIXME: check if URI is set and return Status |
| 1722 | * 414 Request URI too long instead. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1723 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1724 | stream_inc_http_req_ctr(s); |
| 1725 | stream_inc_http_err_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1726 | proxy_inc_fe_req_ctr(sess->fe); |
Willy Tarreau | fec4d89 | 2011-09-02 20:04:57 +0200 | [diff] [blame] | 1727 | if (msg->err_pos < 0) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1728 | msg->err_pos = req->buf->i; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1729 | goto return_bad_req; |
| 1730 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1731 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1732 | /* 2: have we encountered a read error ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 1733 | else if (req->flags & CF_READ_ERROR) { |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1734 | if (!(s->flags & SF_ERR_MASK)) |
| 1735 | s->flags |= SF_ERR_CLICL; |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 1736 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 1737 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 1738 | goto failed_keep_alive; |
| 1739 | |
Willy Tarreau | 0f228a0 | 2015-05-01 15:37:53 +0200 | [diff] [blame] | 1740 | if (sess->fe->options & PR_O_IGNORE_PRB) |
| 1741 | goto failed_keep_alive; |
| 1742 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1743 | /* we cannot return any message on error */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 1744 | if (msg->err_pos >= 0) { |
Emeric Brun | 8c1aaa2 | 2017-06-15 11:30:06 +0200 | [diff] [blame] | 1745 | http_capture_bad_message(sess->fe, &sess->fe->invalid_req, s, msg, msg->err_state, sess->fe); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1746 | stream_inc_http_err_ctr(s); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 1747 | } |
| 1748 | |
Willy Tarreau | dc979f2 | 2012-12-04 10:39:01 +0100 | [diff] [blame] | 1749 | txn->status = 400; |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 1750 | msg->err_state = msg->msg_state; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1751 | msg->msg_state = HTTP_MSG_ERROR; |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 1752 | http_reply_and_close(s, txn->status, NULL); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 1753 | req->analysers &= AN_REQ_FLT_END; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1754 | stream_inc_http_req_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1755 | proxy_inc_fe_req_ctr(sess->fe); |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 1756 | HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 1757 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 1758 | HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 1759 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1760 | if (!(s->flags & SF_FINST_MASK)) |
| 1761 | s->flags |= SF_FINST_R; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1762 | return 0; |
| 1763 | } |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 1764 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1765 | /* 3: has the read timeout expired ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 1766 | 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] | 1767 | if (!(s->flags & SF_ERR_MASK)) |
| 1768 | s->flags |= SF_ERR_CLITO; |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 1769 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 1770 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 1771 | goto failed_keep_alive; |
| 1772 | |
Willy Tarreau | 0f228a0 | 2015-05-01 15:37:53 +0200 | [diff] [blame] | 1773 | if (sess->fe->options & PR_O_IGNORE_PRB) |
| 1774 | goto failed_keep_alive; |
| 1775 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1776 | /* read timeout : give up with an error message. */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 1777 | if (msg->err_pos >= 0) { |
Emeric Brun | 8c1aaa2 | 2017-06-15 11:30:06 +0200 | [diff] [blame] | 1778 | http_capture_bad_message(sess->fe, &sess->fe->invalid_req, s, msg, msg->err_state, sess->fe); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1779 | stream_inc_http_err_ctr(s); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 1780 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1781 | txn->status = 408; |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 1782 | msg->err_state = msg->msg_state; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1783 | msg->msg_state = HTTP_MSG_ERROR; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 1784 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 1785 | req->analysers &= AN_REQ_FLT_END; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 1786 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1787 | stream_inc_http_req_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1788 | proxy_inc_fe_req_ctr(sess->fe); |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 1789 | HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 1790 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 1791 | HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 1792 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1793 | if (!(s->flags & SF_FINST_MASK)) |
| 1794 | s->flags |= SF_FINST_R; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1795 | return 0; |
| 1796 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1797 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1798 | /* 4: have we encountered a close ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 1799 | else if (req->flags & CF_SHUTR) { |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1800 | if (!(s->flags & SF_ERR_MASK)) |
| 1801 | s->flags |= SF_ERR_CLICL; |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 1802 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 1803 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 1804 | goto failed_keep_alive; |
| 1805 | |
Willy Tarreau | 0f228a0 | 2015-05-01 15:37:53 +0200 | [diff] [blame] | 1806 | if (sess->fe->options & PR_O_IGNORE_PRB) |
| 1807 | goto failed_keep_alive; |
| 1808 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 1809 | if (msg->err_pos >= 0) |
Emeric Brun | 8c1aaa2 | 2017-06-15 11:30:06 +0200 | [diff] [blame] | 1810 | http_capture_bad_message(sess->fe, &sess->fe->invalid_req, s, msg, msg->err_state, sess->fe); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1811 | txn->status = 400; |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 1812 | msg->err_state = msg->msg_state; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1813 | msg->msg_state = HTTP_MSG_ERROR; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 1814 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 1815 | req->analysers &= AN_REQ_FLT_END; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1816 | stream_inc_http_err_ctr(s); |
| 1817 | stream_inc_http_req_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1818 | proxy_inc_fe_req_ctr(sess->fe); |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 1819 | HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 1820 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 1821 | HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 1822 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1823 | if (!(s->flags & SF_FINST_MASK)) |
| 1824 | s->flags |= SF_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 1825 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1826 | } |
| 1827 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 1828 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 1829 | req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1830 | s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 1831 | #ifdef TCP_QUICKACK |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 1832 | if (sess->listener->options & LI_O_NOQUICKACK && req->buf->i && |
| 1833 | objt_conn(sess->origin) && conn_ctrl_ready(__objt_conn(sess->origin))) { |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 1834 | /* We need more data, we have to re-enable quick-ack in case we |
| 1835 | * previously disabled it, otherwise we might cause the client |
| 1836 | * to delay next data. |
| 1837 | */ |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 1838 | 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] | 1839 | } |
| 1840 | #endif |
Willy Tarreau | 1b194fe | 2009-03-21 21:10:04 +0100 | [diff] [blame] | 1841 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 1842 | if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) { |
| 1843 | /* If the client starts to talk, let's fall back to |
| 1844 | * request timeout processing. |
| 1845 | */ |
| 1846 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 1847 | req->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 1848 | } |
| 1849 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1850 | /* just set the request timeout once at the beginning of the request */ |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 1851 | if (!tick_isset(req->analyse_exp)) { |
| 1852 | if ((msg->msg_state == HTTP_MSG_RQBEFORE) && |
| 1853 | (txn->flags & TX_WAIT_NEXT_RQ) && |
| 1854 | tick_isset(s->be->timeout.httpka)) |
| 1855 | req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka); |
| 1856 | else |
| 1857 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
| 1858 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1859 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1860 | /* we're not ready yet */ |
| 1861 | return 0; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 1862 | |
| 1863 | failed_keep_alive: |
| 1864 | /* Here we process low-level errors for keep-alive requests. In |
| 1865 | * short, if the request is not the first one and it experiences |
| 1866 | * a timeout, read error or shutdown, we just silently close so |
| 1867 | * that the client can try again. |
| 1868 | */ |
| 1869 | txn->status = 0; |
| 1870 | msg->msg_state = HTTP_MSG_RQBEFORE; |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 1871 | req->analysers &= AN_REQ_FLT_END; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 1872 | s->logs.logwait = 0; |
Willy Tarreau | abcd514 | 2013-06-11 17:18:02 +0200 | [diff] [blame] | 1873 | s->logs.level = 0; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1874 | s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 1875 | http_reply_and_close(s, txn->status, NULL); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 1876 | return 0; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1877 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1878 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 1879 | /* OK now we have a complete HTTP request with indexed headers. Let's |
| 1880 | * complete the request parsing by setting a few fields we will need |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1881 | * 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] | 1882 | * 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] | 1883 | * 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] | 1884 | * byte after the last LF. msg->sov points to the first byte of data. |
| 1885 | * msg->eol cannot be trusted because it may have been left uninitialized |
| 1886 | * (for instance in the absence of headers). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 1887 | */ |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1888 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1889 | stream_inc_http_req_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1890 | 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] | 1891 | |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 1892 | if (txn->flags & TX_WAIT_NEXT_RQ) { |
| 1893 | /* kill the pending keep-alive timeout */ |
| 1894 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
| 1895 | req->analyse_exp = TICK_ETERNITY; |
| 1896 | } |
| 1897 | |
| 1898 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 1899 | /* Maybe we found in invalid header name while we were configured not |
| 1900 | * to block on that, so we have to capture it now. |
| 1901 | */ |
| 1902 | if (unlikely(msg->err_pos >= 0)) |
Emeric Brun | 8c1aaa2 | 2017-06-15 11:30:06 +0200 | [diff] [blame] | 1903 | http_capture_bad_message(sess->fe, &sess->fe->invalid_req, s, msg, msg->err_state, sess->fe); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 1904 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1905 | /* |
| 1906 | * 1: identify the method |
| 1907 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1908 | txn->meth = find_http_meth(req->buf->p, msg->sl.rq.m_l); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1909 | |
| 1910 | /* we can make use of server redirect on GET and HEAD */ |
| 1911 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1912 | s->flags |= SF_REDIRECTABLE; |
Willy Tarreau | 9165979 | 2017-11-10 19:38:10 +0100 | [diff] [blame] | 1913 | else if (txn->meth == HTTP_METH_OTHER && |
| 1914 | msg->sl.rq.m_l == 3 && memcmp(req->buf->p, "PRI", 3) == 0) { |
| 1915 | /* PRI is reserved for the HTTP/2 preface */ |
| 1916 | msg->err_pos = 0; |
| 1917 | goto return_bad_req; |
| 1918 | } |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 1919 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1920 | /* |
| 1921 | * 2: check if the URI matches the monitor_uri. |
| 1922 | * We have to do this for every request which gets in, because |
| 1923 | * the monitor-uri is defined by the frontend. |
| 1924 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1925 | if (unlikely((sess->fe->monitor_uri_len != 0) && |
| 1926 | (sess->fe->monitor_uri_len == msg->sl.rq.u_l) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1927 | !memcmp(req->buf->p + msg->sl.rq.u, |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1928 | sess->fe->monitor_uri, |
| 1929 | sess->fe->monitor_uri_len))) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1930 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1931 | * We have found the monitor URI |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1932 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1933 | struct acl_cond *cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 1934 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1935 | s->flags |= SF_MONITOR; |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 1936 | HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1); |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 1937 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1938 | /* Check if we want to fail this monitor request or not */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1939 | list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 1940 | 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] | 1941 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1942 | ret = acl_pass(ret); |
| 1943 | if (cond->pol == ACL_COND_UNLESS) |
| 1944 | ret = !ret; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 1945 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1946 | if (ret) { |
| 1947 | /* we fail this request, let's return 503 service unavail */ |
| 1948 | txn->status = 503; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 1949 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1950 | if (!(s->flags & SF_ERR_MASK)) |
| 1951 | 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] | 1952 | goto return_prx_cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 1953 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1954 | } |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 1955 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1956 | /* nothing to fail, let's reply normaly */ |
| 1957 | txn->status = 200; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 1958 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1959 | if (!(s->flags & SF_ERR_MASK)) |
| 1960 | 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] | 1961 | goto return_prx_cond; |
| 1962 | } |
| 1963 | |
| 1964 | /* |
| 1965 | * 3: Maybe we have to copy the original REQURI for the logs ? |
| 1966 | * Note: we cannot log anymore if the request has been |
| 1967 | * classified as invalid. |
| 1968 | */ |
| 1969 | if (unlikely(s->logs.logwait & LW_REQ)) { |
| 1970 | /* we have a complete HTTP request that we must log */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 1971 | if ((txn->uri = pool_alloc(pool_head_requri)) != NULL) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1972 | int urilen = msg->sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1973 | |
Stéphane Cottin | 23e9e93 | 2017-05-18 08:58:41 +0200 | [diff] [blame] | 1974 | if (urilen >= global.tune.requri_len ) |
| 1975 | urilen = global.tune.requri_len - 1; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1976 | memcpy(txn->uri, req->buf->p, urilen); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1977 | txn->uri[urilen] = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1978 | |
Willy Tarreau | d79a3b2 | 2012-12-28 09:40:16 +0100 | [diff] [blame] | 1979 | if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT))) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1980 | s->do_log(s); |
| 1981 | } else { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1982 | ha_alert("HTTP logging : out of memory.\n"); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1983 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1984 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1985 | |
Willy Tarreau | 91852eb | 2015-05-01 13:26:00 +0200 | [diff] [blame] | 1986 | /* RFC7230#2.6 has enforced the format of the HTTP version string to be |
| 1987 | * exactly one digit "." one digit. This check may be disabled using |
| 1988 | * option accept-invalid-http-request. |
| 1989 | */ |
| 1990 | if (!(sess->fe->options2 & PR_O2_REQBUG_OK)) { |
| 1991 | if (msg->sl.rq.v_l != 8) { |
| 1992 | msg->err_pos = msg->sl.rq.v; |
| 1993 | goto return_bad_req; |
| 1994 | } |
| 1995 | |
| 1996 | if (req->buf->p[msg->sl.rq.v + 4] != '/' || |
| 1997 | !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 5]) || |
| 1998 | req->buf->p[msg->sl.rq.v + 6] != '.' || |
| 1999 | !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 7])) { |
| 2000 | msg->err_pos = msg->sl.rq.v + 4; |
| 2001 | goto return_bad_req; |
| 2002 | } |
| 2003 | } |
Willy Tarreau | 1331766 | 2015-05-01 13:47:08 +0200 | [diff] [blame] | 2004 | else { |
| 2005 | /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */ |
| 2006 | if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn)) |
| 2007 | goto return_bad_req; |
| 2008 | } |
Willy Tarreau | 91852eb | 2015-05-01 13:26:00 +0200 | [diff] [blame] | 2009 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2010 | /* ... and check if the request is HTTP/1.1 or above */ |
| 2011 | if ((msg->sl.rq.v_l == 8) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2012 | ((req->buf->p[msg->sl.rq.v + 5] > '1') || |
| 2013 | ((req->buf->p[msg->sl.rq.v + 5] == '1') && |
| 2014 | (req->buf->p[msg->sl.rq.v + 7] >= '1')))) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2015 | msg->flags |= HTTP_MSGF_VER_11; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2016 | |
| 2017 | /* "connection" has not been parsed yet */ |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 2018 | 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] | 2019 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2020 | /* if the frontend has "option http-use-proxy-header", we'll check if |
| 2021 | * we have what looks like a proxied connection instead of a connection, |
| 2022 | * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection. |
| 2023 | * Note that this is *not* RFC-compliant, however browsers and proxies |
| 2024 | * happen to do that despite being non-standard :-( |
| 2025 | * We consider that a request not beginning with either '/' or '*' is |
| 2026 | * a proxied connection, which covers both "scheme://location" and |
| 2027 | * CONNECT ip:port. |
| 2028 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2029 | if ((sess->fe->options2 & PR_O2_USE_PXHDR) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2030 | req->buf->p[msg->sl.rq.u] != '/' && req->buf->p[msg->sl.rq.u] != '*') |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2031 | txn->flags |= TX_USE_PX_CONN; |
| 2032 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2033 | /* transfer length unknown*/ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2034 | msg->flags &= ~HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2035 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2036 | /* 5: we may need to capture headers */ |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 2037 | if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap)) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2038 | capture_headers(req->buf->p, &txn->hdr_idx, |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 2039 | s->req_cap, sess->fe->req_cap); |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 2040 | |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 2041 | /* 6: determine the transfer-length according to RFC2616 #4.4, updated |
| 2042 | * by RFC7230#3.3.3 : |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2043 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 2044 | * The length of a message body is determined by one of the following |
| 2045 | * (in order of precedence): |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2046 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 2047 | * 1. Any response to a HEAD request and any response with a 1xx |
| 2048 | * (Informational), 204 (No Content), or 304 (Not Modified) status |
| 2049 | * code is always terminated by the first empty line after the |
| 2050 | * header fields, regardless of the header fields present in the |
| 2051 | * message, and thus cannot contain a message body. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2052 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 2053 | * 2. Any 2xx (Successful) response to a CONNECT request implies that |
| 2054 | * the connection will become a tunnel immediately after the empty |
| 2055 | * line that concludes the header fields. A client MUST ignore any |
| 2056 | * Content-Length or Transfer-Encoding header fields received in |
| 2057 | * such a message. |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2058 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 2059 | * 3. If a Transfer-Encoding header field is present and the chunked |
| 2060 | * transfer coding (Section 4.1) is the final encoding, the message |
| 2061 | * body length is determined by reading and decoding the chunked |
| 2062 | * data until the transfer coding indicates the data is complete. |
| 2063 | * |
| 2064 | * If a Transfer-Encoding header field is present in a response and |
| 2065 | * the chunked transfer coding is not the final encoding, the |
| 2066 | * message body length is determined by reading the connection until |
| 2067 | * it is closed by the server. If a Transfer-Encoding header field |
| 2068 | * is present in a request and the chunked transfer coding is not |
| 2069 | * the final encoding, the message body length cannot be determined |
| 2070 | * reliably; the server MUST respond with the 400 (Bad Request) |
| 2071 | * status code and then close the connection. |
| 2072 | * |
| 2073 | * If a message is received with both a Transfer-Encoding and a |
| 2074 | * Content-Length header field, the Transfer-Encoding overrides the |
| 2075 | * Content-Length. Such a message might indicate an attempt to |
| 2076 | * perform request smuggling (Section 9.5) or response splitting |
| 2077 | * (Section 9.4) and ought to be handled as an error. A sender MUST |
| 2078 | * remove the received Content-Length field prior to forwarding such |
| 2079 | * a message downstream. |
| 2080 | * |
| 2081 | * 4. If a message is received without Transfer-Encoding and with |
| 2082 | * either multiple Content-Length header fields having differing |
| 2083 | * field-values or a single Content-Length header field having an |
| 2084 | * invalid value, then the message framing is invalid and the |
| 2085 | * recipient MUST treat it as an unrecoverable error. If this is a |
| 2086 | * request message, the server MUST respond with a 400 (Bad Request) |
| 2087 | * status code and then close the connection. If this is a response |
| 2088 | * message received by a proxy, the proxy MUST close the connection |
| 2089 | * to the server, discard the received response, and send a 502 (Bad |
| 2090 | * Gateway) response to the client. If this is a response message |
| 2091 | * received by a user agent, the user agent MUST close the |
| 2092 | * connection to the server and discard the received response. |
| 2093 | * |
| 2094 | * 5. If a valid Content-Length header field is present without |
| 2095 | * Transfer-Encoding, its decimal value defines the expected message |
| 2096 | * body length in octets. If the sender closes the connection or |
| 2097 | * the recipient times out before the indicated number of octets are |
| 2098 | * received, the recipient MUST consider the message to be |
| 2099 | * incomplete and close the connection. |
| 2100 | * |
| 2101 | * 6. If this is a request message and none of the above are true, then |
| 2102 | * the message body length is zero (no message body is present). |
| 2103 | * |
| 2104 | * 7. Otherwise, this is a response message without a declared message |
| 2105 | * body length, so the message body length is determined by the |
| 2106 | * number of octets received prior to the server closing the |
| 2107 | * connection. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2108 | */ |
| 2109 | |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2110 | ctx.idx = 0; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2111 | /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */ |
Willy Tarreau | 4979d5c | 2015-05-01 10:06:30 +0200 | [diff] [blame] | 2112 | while (http_find_header2("Transfer-Encoding", 17, req->buf->p, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2113 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
Christopher Faulet | be821b9 | 2017-03-30 11:21:53 +0200 | [diff] [blame] | 2114 | msg->flags |= HTTP_MSGF_TE_CHNK; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2115 | else if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | 34dfc60 | 2015-05-01 10:09:49 +0200 | [diff] [blame] | 2116 | /* chunked not last, return badreq */ |
| 2117 | goto return_bad_req; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2118 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2119 | } |
| 2120 | |
Willy Tarreau | 1c91391 | 2015-04-30 10:57:51 +0200 | [diff] [blame] | 2121 | /* Chunked requests must have their content-length removed */ |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2122 | ctx.idx = 0; |
Willy Tarreau | 1c91391 | 2015-04-30 10:57:51 +0200 | [diff] [blame] | 2123 | if (msg->flags & HTTP_MSGF_TE_CHNK) { |
| 2124 | while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) |
| 2125 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 2126 | } |
Willy Tarreau | 34dfc60 | 2015-05-01 10:09:49 +0200 | [diff] [blame] | 2127 | else while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2128 | signed long long cl; |
| 2129 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2130 | if (!ctx.vlen) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2131 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2132 | goto return_bad_req; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2133 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2134 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2135 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2136 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2137 | goto return_bad_req; /* parse failure */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2138 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2139 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2140 | if (cl < 0) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2141 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2142 | goto return_bad_req; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2143 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2144 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2145 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2146 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2147 | goto return_bad_req; /* already specified, was different */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2148 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2149 | |
Christopher Faulet | be821b9 | 2017-03-30 11:21:53 +0200 | [diff] [blame] | 2150 | msg->flags |= HTTP_MSGF_CNT_LEN; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 2151 | msg->body_len = msg->chunk_len = cl; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2152 | } |
| 2153 | |
Willy Tarreau | 34dfc60 | 2015-05-01 10:09:49 +0200 | [diff] [blame] | 2154 | /* even bodyless requests have a known length */ |
| 2155 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2156 | |
Willy Tarreau | 179085c | 2014-04-28 16:48:56 +0200 | [diff] [blame] | 2157 | /* Until set to anything else, the connection mode is set as Keep-Alive. It will |
| 2158 | * only change if both the request and the config reference something else. |
| 2159 | * Option httpclose by itself sets tunnel mode where headers are mangled. |
| 2160 | * However, if another mode is set, it will affect it (eg: server-close/ |
| 2161 | * keep-alive + httpclose = close). Note that we avoid to redo the same work |
| 2162 | * if FE and BE have the same settings (common). The method consists in |
| 2163 | * checking if options changed between the two calls (implying that either |
| 2164 | * one is non-null, or one of them is non-null and we are there for the first |
| 2165 | * time. |
| 2166 | */ |
| 2167 | if (!(txn->flags & TX_HDR_CONN_PRS) || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2168 | ((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] | 2169 | http_adjust_conn_mode(s, txn, msg); |
Willy Tarreau | 179085c | 2014-04-28 16:48:56 +0200 | [diff] [blame] | 2170 | |
Willy Tarreau | 9fbe18e | 2015-05-01 22:42:08 +0200 | [diff] [blame] | 2171 | /* we may have to wait for the request's body */ |
| 2172 | if ((s->be->options & PR_O_WREQ_BODY) && |
| 2173 | (msg->body_len || (msg->flags & HTTP_MSGF_TE_CHNK))) |
| 2174 | req->analysers |= AN_REQ_HTTP_BODY; |
| 2175 | |
Willy Tarreau | 83ece46 | 2017-12-21 15:13:09 +0100 | [diff] [blame] | 2176 | /* |
| 2177 | * RFC7234#4: |
| 2178 | * A cache MUST write through requests with methods |
| 2179 | * that are unsafe (Section 4.2.1 of [RFC7231]) to |
| 2180 | * the origin server; i.e., a cache is not allowed |
| 2181 | * to generate a reply to such a request before |
| 2182 | * having forwarded the request and having received |
| 2183 | * a corresponding response. |
| 2184 | * |
| 2185 | * RFC7231#4.2.1: |
| 2186 | * Of the request methods defined by this |
| 2187 | * specification, the GET, HEAD, OPTIONS, and TRACE |
| 2188 | * methods are defined to be safe. |
| 2189 | */ |
| 2190 | if (likely(txn->meth == HTTP_METH_GET || |
| 2191 | txn->meth == HTTP_METH_HEAD || |
| 2192 | txn->meth == HTTP_METH_OPTIONS || |
| 2193 | txn->meth == HTTP_METH_TRACE)) |
| 2194 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
| 2195 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2196 | /* end of job, return OK */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 2197 | req->analysers &= ~an_bit; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2198 | req->analyse_exp = TICK_ETERNITY; |
| 2199 | return 1; |
| 2200 | |
| 2201 | return_bad_req: |
| 2202 | /* We centralize bad requests processing here */ |
| 2203 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 2204 | /* we detected a parsing error. We want to archive this request |
| 2205 | * in the dedicated proxy area for later troubleshooting. |
| 2206 | */ |
Emeric Brun | 8c1aaa2 | 2017-06-15 11:30:06 +0200 | [diff] [blame] | 2207 | http_capture_bad_message(sess->fe, &sess->fe->invalid_req, s, msg, msg->err_state, sess->fe); |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2208 | } |
| 2209 | |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 2210 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2211 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 2212 | txn->status = 400; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 2213 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2214 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 2215 | HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 2216 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 2217 | HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1); |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2218 | |
| 2219 | return_prx_cond: |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2220 | if (!(s->flags & SF_ERR_MASK)) |
| 2221 | s->flags |= SF_ERR_PRXCOND; |
| 2222 | if (!(s->flags & SF_FINST_MASK)) |
| 2223 | s->flags |= SF_FINST_R; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2224 | |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 2225 | req->analysers &= AN_REQ_FLT_END; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2226 | req->analyse_exp = TICK_ETERNITY; |
| 2227 | return 0; |
| 2228 | } |
| 2229 | |
Willy Tarreau | 4f8a83c | 2012-06-04 00:26:23 +0200 | [diff] [blame] | 2230 | |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 2231 | /* This function prepares an applet to handle the stats. It can deal with the |
| 2232 | * "100-continue" expectation, check that admin rules are met for POST requests, |
| 2233 | * and program a response message if something was unexpected. It cannot fail |
| 2234 | * 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] | 2235 | * 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] | 2236 | * 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] | 2237 | * is expected to have already assigned an appctx to the stream. |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 2238 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2239 | int http_handle_stats(struct stream *s, struct channel *req) |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 2240 | { |
| 2241 | struct stats_admin_rule *stats_admin_rule; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 2242 | struct stream_interface *si = &s->si[1]; |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 2243 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 2244 | struct http_txn *txn = s->txn; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 2245 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2246 | struct uri_auth *uri_auth = s->be->uri_auth; |
| 2247 | const char *uri, *h, *lookup; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2248 | struct appctx *appctx; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2249 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2250 | appctx = si_appctx(si); |
| 2251 | memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats)); |
| 2252 | appctx->st1 = appctx->st2 = 0; |
| 2253 | appctx->ctx.stats.st_code = STAT_STATUS_INIT; |
| 2254 | appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 2255 | 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] | 2256 | appctx->ctx.stats.flags |= STAT_CHUNKED; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2257 | |
| 2258 | uri = msg->chn->buf->p + msg->sl.rq.u; |
| 2259 | lookup = uri + uri_auth->uri_len; |
| 2260 | |
| 2261 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) { |
| 2262 | if (memcmp(h, ";up", 3) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2263 | appctx->ctx.stats.flags |= STAT_HIDE_DOWN; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2264 | break; |
| 2265 | } |
| 2266 | } |
| 2267 | |
| 2268 | if (uri_auth->refresh) { |
| 2269 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) { |
| 2270 | if (memcmp(h, ";norefresh", 10) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2271 | appctx->ctx.stats.flags |= STAT_NO_REFRESH; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2272 | break; |
| 2273 | } |
| 2274 | } |
| 2275 | } |
| 2276 | |
| 2277 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) { |
| 2278 | if (memcmp(h, ";csv", 4) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2279 | appctx->ctx.stats.flags &= ~STAT_FMT_HTML; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2280 | break; |
| 2281 | } |
| 2282 | } |
| 2283 | |
Willy Tarreau | 1e62df9 | 2016-01-11 18:57:53 +0100 | [diff] [blame] | 2284 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 6; h++) { |
| 2285 | if (memcmp(h, ";typed", 6) == 0) { |
| 2286 | appctx->ctx.stats.flags &= ~STAT_FMT_HTML; |
| 2287 | appctx->ctx.stats.flags |= STAT_FMT_TYPED; |
| 2288 | break; |
| 2289 | } |
| 2290 | } |
| 2291 | |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2292 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) { |
| 2293 | if (memcmp(h, ";st=", 4) == 0) { |
| 2294 | int i; |
| 2295 | h += 4; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2296 | appctx->ctx.stats.st_code = STAT_STATUS_UNKN; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2297 | for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) { |
| 2298 | if (strncmp(stat_status_codes[i], h, 4) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2299 | appctx->ctx.stats.st_code = i; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2300 | break; |
| 2301 | } |
| 2302 | } |
| 2303 | break; |
| 2304 | } |
| 2305 | } |
| 2306 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2307 | appctx->ctx.stats.scope_str = 0; |
| 2308 | appctx->ctx.stats.scope_len = 0; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2309 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) { |
| 2310 | if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) { |
| 2311 | int itx = 0; |
| 2312 | const char *h2; |
| 2313 | char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1]; |
| 2314 | const char *err; |
| 2315 | |
| 2316 | h += strlen(STAT_SCOPE_INPUT_NAME) + 1; |
| 2317 | h2 = h; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2318 | appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2319 | while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') { |
| 2320 | itx++; |
| 2321 | h++; |
| 2322 | } |
| 2323 | |
| 2324 | if (itx > STAT_SCOPE_TXT_MAXLEN) |
| 2325 | itx = STAT_SCOPE_TXT_MAXLEN; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2326 | appctx->ctx.stats.scope_len = itx; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2327 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2328 | /* 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] | 2329 | memcpy(scope_txt, h2, itx); |
| 2330 | scope_txt[itx] = '\0'; |
| 2331 | err = invalid_char(scope_txt); |
| 2332 | if (err) { |
| 2333 | /* bad char in search text => clear scope */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2334 | appctx->ctx.stats.scope_str = 0; |
| 2335 | appctx->ctx.stats.scope_len = 0; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2336 | } |
| 2337 | break; |
| 2338 | } |
| 2339 | } |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 2340 | |
| 2341 | /* now check whether we have some admin rules for this request */ |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2342 | list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) { |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 2343 | int ret = 1; |
| 2344 | |
| 2345 | if (stats_admin_rule->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 2346 | 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] | 2347 | ret = acl_pass(ret); |
| 2348 | if (stats_admin_rule->cond->pol == ACL_COND_UNLESS) |
| 2349 | ret = !ret; |
| 2350 | } |
| 2351 | |
| 2352 | if (ret) { |
| 2353 | /* no rule, or the rule matches */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2354 | appctx->ctx.stats.flags |= STAT_ADMIN; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 2355 | break; |
| 2356 | } |
| 2357 | } |
| 2358 | |
| 2359 | /* Was the status page requested with a POST ? */ |
Willy Tarreau | b8cdf52 | 2015-05-29 01:09:15 +0200 | [diff] [blame] | 2360 | if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2361 | if (appctx->ctx.stats.flags & STAT_ADMIN) { |
Willy Tarreau | cfe7fdd | 2014-04-26 22:08:32 +0200 | [diff] [blame] | 2362 | /* we'll need the request body, possibly after sending 100-continue */ |
Willy Tarreau | b8cdf52 | 2015-05-29 01:09:15 +0200 | [diff] [blame] | 2363 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) |
| 2364 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2365 | appctx->st0 = STAT_HTTP_POST; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 2366 | } |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 2367 | else { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2368 | appctx->ctx.stats.st_code = STAT_STATUS_DENY; |
| 2369 | appctx->st0 = STAT_HTTP_LAST; |
de Lafond Guillaume | 88c278f | 2013-04-15 19:27:10 +0200 | [diff] [blame] | 2370 | } |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 2371 | } |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 2372 | else { |
| 2373 | /* So it was another method (GET/HEAD) */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2374 | appctx->st0 = STAT_HTTP_HEAD; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 2375 | } |
| 2376 | |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 2377 | s->task->nice = -32; /* small boost for HTTP statistics */ |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 2378 | return 1; |
| 2379 | } |
| 2380 | |
Lukas Tribus | 67db8df | 2013-06-23 17:37:13 +0200 | [diff] [blame] | 2381 | /* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets |
| 2382 | * (as per RFC3260 #4 and BCP37 #4.2 and #5.2). |
| 2383 | */ |
Vincent Bernat | 6e61589 | 2016-05-18 16:17:44 +0200 | [diff] [blame] | 2384 | 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] | 2385 | { |
| 2386 | #ifdef IP_TOS |
Vincent Bernat | 6e61589 | 2016-05-18 16:17:44 +0200 | [diff] [blame] | 2387 | if (from->ss_family == AF_INET) |
Lukas Tribus | 67db8df | 2013-06-23 17:37:13 +0200 | [diff] [blame] | 2388 | setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)); |
| 2389 | #endif |
| 2390 | #ifdef IPV6_TCLASS |
Vincent Bernat | 6e61589 | 2016-05-18 16:17:44 +0200 | [diff] [blame] | 2391 | if (from->ss_family == AF_INET6) { |
| 2392 | if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)from)->sin6_addr)) |
Lukas Tribus | 67db8df | 2013-06-23 17:37:13 +0200 | [diff] [blame] | 2393 | /* v4-mapped addresses need IP_TOS */ |
| 2394 | setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)); |
| 2395 | else |
| 2396 | setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos)); |
| 2397 | } |
| 2398 | #endif |
| 2399 | } |
| 2400 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2401 | int http_transform_header_str(struct stream* s, struct http_msg *msg, |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 2402 | const char* name, unsigned int name_len, |
| 2403 | const char *str, struct my_regex *re, |
| 2404 | int action) |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 2405 | { |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 2406 | struct hdr_ctx ctx; |
| 2407 | char *buf = msg->chn->buf->p; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 2408 | struct hdr_idx *idx = &s->txn->hdr_idx; |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 2409 | int (*http_find_hdr_func)(const char *name, int len, char *sol, |
| 2410 | struct hdr_idx *idx, struct hdr_ctx *ctx); |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 2411 | struct chunk *output = get_trash_chunk(); |
| 2412 | |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 2413 | ctx.idx = 0; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 2414 | |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 2415 | /* Choose the header browsing function. */ |
| 2416 | switch (action) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2417 | case ACT_HTTP_REPLACE_VAL: |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 2418 | http_find_hdr_func = http_find_header2; |
| 2419 | break; |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2420 | case ACT_HTTP_REPLACE_HDR: |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 2421 | http_find_hdr_func = http_find_full_header2; |
| 2422 | break; |
| 2423 | default: /* impossible */ |
| 2424 | return -1; |
| 2425 | } |
| 2426 | |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 2427 | while (http_find_hdr_func(name, name_len, buf, idx, &ctx)) { |
| 2428 | struct hdr_idx_elem *hdr = idx->v + ctx.idx; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 2429 | int delta; |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 2430 | char *val = ctx.line + ctx.val; |
| 2431 | char* val_end = val + ctx.vlen; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 2432 | |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 2433 | if (!regex_exec_match2(re, val, val_end-val, MAX_MATCH, pmatch, 0)) |
| 2434 | continue; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 2435 | |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 2436 | output->len = exp_replace(output->str, output->size, val, str, pmatch); |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 2437 | if (output->len == -1) |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 2438 | return -1; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 2439 | |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 2440 | delta = buffer_replace2(msg->chn->buf, val, val_end, output->str, output->len); |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 2441 | |
| 2442 | hdr->len += delta; |
| 2443 | http_msg_move_end(msg, delta); |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 2444 | |
| 2445 | /* Adjust the length of the current value of the index. */ |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 2446 | ctx.vlen += delta; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 2447 | } |
| 2448 | |
| 2449 | return 0; |
| 2450 | } |
| 2451 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2452 | static int http_transform_header(struct stream* s, struct http_msg *msg, |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 2453 | const char* name, unsigned int name_len, |
| 2454 | struct list *fmt, struct my_regex *re, |
| 2455 | int action) |
| 2456 | { |
Christopher Faulet | 07a0fec | 2017-02-08 12:17:07 +0100 | [diff] [blame] | 2457 | struct chunk *replace; |
| 2458 | int ret = -1; |
| 2459 | |
| 2460 | replace = alloc_trash_chunk(); |
| 2461 | if (!replace) |
| 2462 | goto leave; |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 2463 | |
| 2464 | replace->len = build_logline(s, replace->str, replace->size, fmt); |
| 2465 | if (replace->len >= replace->size - 1) |
Christopher Faulet | 07a0fec | 2017-02-08 12:17:07 +0100 | [diff] [blame] | 2466 | goto leave; |
| 2467 | |
| 2468 | ret = http_transform_header_str(s, msg, name, name_len, replace->str, re, action); |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 2469 | |
Christopher Faulet | 07a0fec | 2017-02-08 12:17:07 +0100 | [diff] [blame] | 2470 | leave: |
| 2471 | free_trash_chunk(replace); |
| 2472 | return ret; |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 2473 | } |
| 2474 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2475 | /* Executes the http-request rules <rules> for stream <s>, proxy <px> and |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 2476 | * transaction <txn>. Returns the verdict of the first rule that prevents |
| 2477 | * further processing of the request (auth, deny, ...), and defaults to |
| 2478 | * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or |
| 2479 | * 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] | 2480 | * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL |
| 2481 | * and a deny/tarpit rule is matched, it will be filled with this rule's deny |
| 2482 | * status. |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2483 | */ |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 2484 | enum rule_result |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 2485 | 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] | 2486 | { |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 2487 | struct session *sess = strm_sess(s); |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 2488 | struct http_txn *txn = s->txn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2489 | struct connection *cli_conn; |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 2490 | struct act_rule *rule; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 2491 | struct hdr_ctx ctx; |
Willy Tarreau | ae3c010 | 2014-04-28 23:22:08 +0200 | [diff] [blame] | 2492 | const char *auth_realm; |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 2493 | int act_flags = 0; |
Thierry Fournier | 4b788f7 | 2016-06-01 13:35:36 +0200 | [diff] [blame] | 2494 | int len; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2495 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 2496 | /* If "the current_rule_list" match the executed rule list, we are in |
| 2497 | * resume condition. If a resume is needed it is always in the action |
| 2498 | * and never in the ACL or converters. In this case, we initialise the |
| 2499 | * current rule, and go to the action execution point. |
| 2500 | */ |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 2501 | if (s->current_rule) { |
| 2502 | rule = s->current_rule; |
| 2503 | s->current_rule = NULL; |
| 2504 | if (s->current_rule_list == rules) |
| 2505 | goto resume_execution; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 2506 | } |
| 2507 | s->current_rule_list = rules; |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 2508 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2509 | list_for_each_entry(rule, rules, list) { |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2510 | |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 2511 | /* check optional condition */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2512 | if (rule->cond) { |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 2513 | int ret; |
| 2514 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 2515 | 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] | 2516 | ret = acl_pass(ret); |
| 2517 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2518 | if (rule->cond->pol == ACL_COND_UNLESS) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2519 | ret = !ret; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 2520 | |
| 2521 | if (!ret) /* condition not matched */ |
| 2522 | continue; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2523 | } |
| 2524 | |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 2525 | act_flags |= ACT_FLAG_FIRST; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 2526 | resume_execution: |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 2527 | switch (rule->action) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2528 | case ACT_ACTION_ALLOW: |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 2529 | return HTTP_RULE_RES_STOP; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 2530 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2531 | case ACT_ACTION_DENY: |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 2532 | if (deny_status) |
| 2533 | *deny_status = rule->deny_status; |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 2534 | return HTTP_RULE_RES_DENY; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 2535 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2536 | case ACT_HTTP_REQ_TARPIT: |
Willy Tarreau | ccbcc37 | 2012-12-27 12:37:57 +0100 | [diff] [blame] | 2537 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 2538 | if (deny_status) |
| 2539 | *deny_status = rule->deny_status; |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 2540 | return HTTP_RULE_RES_DENY; |
Willy Tarreau | ccbcc37 | 2012-12-27 12:37:57 +0100 | [diff] [blame] | 2541 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2542 | case ACT_HTTP_REQ_AUTH: |
Willy Tarreau | ae3c010 | 2014-04-28 23:22:08 +0200 | [diff] [blame] | 2543 | /* Auth might be performed on regular http-req rules as well as on stats */ |
| 2544 | auth_realm = rule->arg.auth.realm; |
| 2545 | if (!auth_realm) { |
| 2546 | if (px->uri_auth && rules == &px->uri_auth->http_req_rules) |
| 2547 | auth_realm = STATS_DEFAULT_REALM; |
| 2548 | else |
| 2549 | auth_realm = px->id; |
| 2550 | } |
| 2551 | /* send 401/407 depending on whether we use a proxy or not. We still |
| 2552 | * count one error, because normal browsing won't significantly |
| 2553 | * increase the counter but brute force attempts will. |
| 2554 | */ |
| 2555 | chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm); |
| 2556 | txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401; |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 2557 | http_reply_and_close(s, txn->status, &trash); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2558 | stream_inc_http_err_ctr(s); |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 2559 | return HTTP_RULE_RES_ABRT; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 2560 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2561 | case ACT_HTTP_REDIR: |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 2562 | if (!http_apply_redirect_rule(rule->arg.redir, s, txn)) |
| 2563 | return HTTP_RULE_RES_BADREQ; |
| 2564 | return HTTP_RULE_RES_DONE; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 2565 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2566 | case ACT_HTTP_SET_NICE: |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 2567 | s->task->nice = rule->arg.nice; |
| 2568 | break; |
| 2569 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2570 | case ACT_HTTP_SET_TOS: |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 2571 | if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 2572 | 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] | 2573 | break; |
| 2574 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2575 | case ACT_HTTP_SET_MARK: |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 2576 | #ifdef SO_MARK |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 2577 | if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 2578 | 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] | 2579 | #endif |
| 2580 | break; |
| 2581 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2582 | case ACT_HTTP_SET_LOGL: |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 2583 | s->logs.level = rule->arg.loglevel; |
| 2584 | break; |
| 2585 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2586 | case ACT_HTTP_REPLACE_HDR: |
| 2587 | case ACT_HTTP_REPLACE_VAL: |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 2588 | if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name, |
| 2589 | rule->arg.hdr_add.name_len, |
| 2590 | &rule->arg.hdr_add.fmt, |
| 2591 | &rule->arg.hdr_add.re, rule->action)) |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 2592 | return HTTP_RULE_RES_BADREQ; |
| 2593 | break; |
| 2594 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2595 | case ACT_HTTP_DEL_HDR: |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 2596 | ctx.idx = 0; |
| 2597 | /* remove all occurrences of the header */ |
| 2598 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 2599 | txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 2600 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 2601 | } |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 2602 | break; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 2603 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2604 | case ACT_HTTP_SET_HDR: |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2605 | case ACT_HTTP_ADD_HDR: { |
Thierry Fournier | 4b788f7 | 2016-06-01 13:35:36 +0200 | [diff] [blame] | 2606 | /* The scope of the trash buffer must be limited to this function. The |
| 2607 | * build_logline() function can execute a lot of other function which |
| 2608 | * can use the trash buffer. So for limiting the scope of this global |
| 2609 | * buffer, we build first the header value using build_logline, and |
| 2610 | * after we store the header name. |
| 2611 | */ |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2612 | struct chunk *replace; |
| 2613 | |
| 2614 | replace = alloc_trash_chunk(); |
| 2615 | if (!replace) |
| 2616 | return HTTP_RULE_RES_BADREQ; |
| 2617 | |
Thierry Fournier | 4b788f7 | 2016-06-01 13:35:36 +0200 | [diff] [blame] | 2618 | len = rule->arg.hdr_add.name_len + 2, |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2619 | len += build_logline(s, replace->str + len, replace->size - len, &rule->arg.hdr_add.fmt); |
| 2620 | memcpy(replace->str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len); |
| 2621 | replace->str[rule->arg.hdr_add.name_len] = ':'; |
| 2622 | replace->str[rule->arg.hdr_add.name_len + 1] = ' '; |
| 2623 | replace->len = len; |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 2624 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2625 | if (rule->action == ACT_HTTP_SET_HDR) { |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 2626 | /* remove all occurrences of the header */ |
| 2627 | ctx.idx = 0; |
| 2628 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 2629 | txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 2630 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
| 2631 | } |
| 2632 | } |
| 2633 | |
Tim Duesterhus | 3fd1973 | 2018-05-27 20:35:08 +0200 | [diff] [blame] | 2634 | if (http_header_add_tail2(&txn->req, &txn->hdr_idx, replace->str, replace->len) < 0) { |
| 2635 | static unsigned char rate_limit = 0; |
| 2636 | |
| 2637 | if ((rate_limit++ & 255) == 0) { |
| 2638 | replace->str[rule->arg.hdr_add.name_len] = 0; |
| 2639 | 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, replace->str, s->uniq_id); |
| 2640 | } |
| 2641 | |
| 2642 | HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1); |
| 2643 | if (sess->fe != s->be) |
| 2644 | HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1); |
| 2645 | if (sess->listener->counters) |
| 2646 | HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1); |
| 2647 | } |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2648 | |
| 2649 | free_trash_chunk(replace); |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 2650 | break; |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2651 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2652 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2653 | case ACT_HTTP_DEL_ACL: |
| 2654 | case ACT_HTTP_DEL_MAP: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2655 | struct pat_ref *ref; |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2656 | struct chunk *key; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2657 | |
| 2658 | /* collect reference */ |
| 2659 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 2660 | if (!ref) |
| 2661 | continue; |
| 2662 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2663 | /* allocate key */ |
| 2664 | key = alloc_trash_chunk(); |
| 2665 | if (!key) |
| 2666 | return HTTP_RULE_RES_BADREQ; |
| 2667 | |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2668 | /* collect key */ |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2669 | key->len = build_logline(s, key->str, key->size, &rule->arg.map.key); |
| 2670 | key->str[key->len] = '\0'; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2671 | |
| 2672 | /* perform update */ |
| 2673 | /* returned code: 1=ok, 0=ko */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2674 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2675 | pat_ref_delete(ref, key->str); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2676 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2677 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2678 | free_trash_chunk(key); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2679 | break; |
| 2680 | } |
| 2681 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2682 | case ACT_HTTP_ADD_ACL: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2683 | struct pat_ref *ref; |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2684 | struct chunk *key; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2685 | |
| 2686 | /* collect reference */ |
| 2687 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 2688 | if (!ref) |
| 2689 | continue; |
| 2690 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2691 | /* allocate key */ |
| 2692 | key = alloc_trash_chunk(); |
| 2693 | if (!key) |
| 2694 | return HTTP_RULE_RES_BADREQ; |
| 2695 | |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2696 | /* collect key */ |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2697 | key->len = build_logline(s, key->str, key->size, &rule->arg.map.key); |
| 2698 | key->str[key->len] = '\0'; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2699 | |
| 2700 | /* perform update */ |
| 2701 | /* add entry only if it does not already exist */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2702 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2703 | if (pat_ref_find_elt(ref, key->str) == NULL) |
| 2704 | pat_ref_add(ref, key->str, NULL, NULL); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2705 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2706 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2707 | free_trash_chunk(key); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2708 | break; |
| 2709 | } |
| 2710 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2711 | case ACT_HTTP_SET_MAP: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2712 | struct pat_ref *ref; |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2713 | struct chunk *key, *value; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2714 | |
| 2715 | /* collect reference */ |
| 2716 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 2717 | if (!ref) |
| 2718 | continue; |
| 2719 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2720 | /* allocate key */ |
| 2721 | key = alloc_trash_chunk(); |
| 2722 | if (!key) |
| 2723 | return HTTP_RULE_RES_BADREQ; |
| 2724 | |
| 2725 | /* allocate value */ |
| 2726 | value = alloc_trash_chunk(); |
| 2727 | if (!value) { |
| 2728 | free_trash_chunk(key); |
| 2729 | return HTTP_RULE_RES_BADREQ; |
| 2730 | } |
| 2731 | |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2732 | /* collect key */ |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2733 | key->len = build_logline(s, key->str, key->size, &rule->arg.map.key); |
| 2734 | key->str[key->len] = '\0'; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2735 | |
| 2736 | /* collect value */ |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2737 | value->len = build_logline(s, value->str, value->size, &rule->arg.map.value); |
| 2738 | value->str[value->len] = '\0'; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2739 | |
| 2740 | /* perform update */ |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2741 | if (pat_ref_find_elt(ref, key->str) != NULL) |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2742 | /* update entry if it exists */ |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2743 | pat_ref_set(ref, key->str, value->str, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2744 | else |
| 2745 | /* insert a new entry */ |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2746 | pat_ref_add(ref, key->str, value->str, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2747 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2748 | free_trash_chunk(key); |
| 2749 | free_trash_chunk(value); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2750 | break; |
| 2751 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 2752 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 2753 | case ACT_CUSTOM: |
Willy Tarreau | 7aa15b0 | 2017-12-20 16:56:50 +0100 | [diff] [blame] | 2754 | if ((s->req.flags & CF_READ_ERROR) || |
| 2755 | ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) && |
| 2756 | !(s->si[0].flags & SI_FL_CLEAN_ABRT) && |
| 2757 | (px->options & PR_O_ABRT_CLOSE))) |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 2758 | act_flags |= ACT_FLAG_FINAL; |
Willy Tarreau | 3945868 | 2015-09-27 10:33:15 +0200 | [diff] [blame] | 2759 | |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 2760 | switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) { |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 2761 | case ACT_RET_ERR: |
| 2762 | case ACT_RET_CONT: |
| 2763 | break; |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 2764 | case ACT_RET_STOP: |
| 2765 | return HTTP_RULE_RES_DONE; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 2766 | case ACT_RET_YIELD: |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 2767 | s->current_rule = rule; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 2768 | return HTTP_RULE_RES_YIELD; |
| 2769 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 2770 | break; |
| 2771 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2772 | case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX: |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 2773 | /* Note: only the first valid tracking parameter of each |
| 2774 | * applies. |
| 2775 | */ |
| 2776 | |
Christopher Faulet | 4fce0d8 | 2017-09-18 11:57:31 +0200 | [diff] [blame] | 2777 | if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) { |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 2778 | struct stktable *t; |
| 2779 | struct stksess *ts; |
| 2780 | struct stktable_key *key; |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 2781 | void *ptr1, *ptr2; |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 2782 | |
Thierry FOURNIER | 5ec63e0 | 2015-08-04 09:09:48 +0200 | [diff] [blame] | 2783 | t = rule->arg.trk_ctr.table.t; |
| 2784 | 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] | 2785 | |
| 2786 | if (key && (ts = stktable_get_entry(t, key))) { |
Christopher Faulet | 4fce0d8 | 2017-09-18 11:57:31 +0200 | [diff] [blame] | 2787 | stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts); |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 2788 | |
| 2789 | /* 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] | 2790 | ptr1 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT); |
| 2791 | ptr2 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE); |
| 2792 | if (ptr1 || ptr2) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2793 | HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock); |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 2794 | |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 2795 | if (ptr1) |
| 2796 | stktable_data_cast(ptr1, http_req_cnt)++; |
| 2797 | |
| 2798 | if (ptr2) |
| 2799 | update_freq_ctr_period(&stktable_data_cast(ptr2, http_req_rate), |
| 2800 | t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1); |
| 2801 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2802 | HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock); |
Emeric Brun | 0fed0b0 | 2017-11-29 16:15:07 +0100 | [diff] [blame] | 2803 | |
| 2804 | /* If data was modified, we need to touch to re-schedule sync */ |
| 2805 | stktable_touch_local(t, ts, 0); |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 2806 | } |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 2807 | |
Christopher Faulet | 4fce0d8 | 2017-09-18 11:57:31 +0200 | [diff] [blame] | 2808 | stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT); |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 2809 | if (sess->fe != s->be) |
Christopher Faulet | 4fce0d8 | 2017-09-18 11:57:31 +0200 | [diff] [blame] | 2810 | stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND); |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 2811 | } |
| 2812 | } |
Adis Nezirovic | 2fbcafc | 2015-07-06 15:44:30 +0200 | [diff] [blame] | 2813 | break; |
| 2814 | |
Thierry FOURNIER | 22e4901 | 2015-08-05 19:13:48 +0200 | [diff] [blame] | 2815 | /* other flags exists, but normaly, they never be matched. */ |
| 2816 | default: |
| 2817 | break; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2818 | } |
| 2819 | } |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 2820 | |
| 2821 | /* we reached the end of the rules, nothing to report */ |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 2822 | return HTTP_RULE_RES_CONT; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2823 | } |
| 2824 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2825 | |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 2826 | /* Executes the http-response rules <rules> for stream <s> and proxy <px>. It |
| 2827 | * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP, |
| 2828 | * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT |
| 2829 | * is returned, the process can continue the evaluation of next rule list. If |
| 2830 | * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ |
| 2831 | * is returned, it means the operation could not be processed and a server error |
| 2832 | * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a |
| 2833 | * deny rule. If *YIELD is returned, the caller must call again the function |
| 2834 | * with the same context. |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2835 | */ |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 2836 | static enum rule_result |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 2837 | 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] | 2838 | { |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 2839 | struct session *sess = strm_sess(s); |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 2840 | struct http_txn *txn = s->txn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2841 | struct connection *cli_conn; |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 2842 | struct act_rule *rule; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2843 | struct hdr_ctx ctx; |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 2844 | int act_flags = 0; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2845 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 2846 | /* If "the current_rule_list" match the executed rule list, we are in |
| 2847 | * resume condition. If a resume is needed it is always in the action |
| 2848 | * and never in the ACL or converters. In this case, we initialise the |
| 2849 | * current rule, and go to the action execution point. |
| 2850 | */ |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 2851 | if (s->current_rule) { |
| 2852 | rule = s->current_rule; |
| 2853 | s->current_rule = NULL; |
| 2854 | if (s->current_rule_list == rules) |
| 2855 | goto resume_execution; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 2856 | } |
| 2857 | s->current_rule_list = rules; |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 2858 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2859 | list_for_each_entry(rule, rules, list) { |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2860 | |
| 2861 | /* check optional condition */ |
| 2862 | if (rule->cond) { |
| 2863 | int ret; |
| 2864 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 2865 | 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] | 2866 | ret = acl_pass(ret); |
| 2867 | |
| 2868 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 2869 | ret = !ret; |
| 2870 | |
| 2871 | if (!ret) /* condition not matched */ |
| 2872 | continue; |
| 2873 | } |
| 2874 | |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 2875 | act_flags |= ACT_FLAG_FIRST; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 2876 | resume_execution: |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2877 | switch (rule->action) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2878 | case ACT_ACTION_ALLOW: |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 2879 | return HTTP_RULE_RES_STOP; /* "allow" rules are OK */ |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2880 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2881 | case ACT_ACTION_DENY: |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2882 | txn->flags |= TX_SVDENY; |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 2883 | return HTTP_RULE_RES_STOP; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2884 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2885 | case ACT_HTTP_SET_NICE: |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 2886 | s->task->nice = rule->arg.nice; |
| 2887 | break; |
| 2888 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2889 | case ACT_HTTP_SET_TOS: |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 2890 | if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 2891 | 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] | 2892 | break; |
| 2893 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2894 | case ACT_HTTP_SET_MARK: |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 2895 | #ifdef SO_MARK |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 2896 | if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 2897 | 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] | 2898 | #endif |
| 2899 | break; |
| 2900 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2901 | case ACT_HTTP_SET_LOGL: |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 2902 | s->logs.level = rule->arg.loglevel; |
| 2903 | break; |
| 2904 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2905 | case ACT_HTTP_REPLACE_HDR: |
| 2906 | case ACT_HTTP_REPLACE_VAL: |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 2907 | if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name, |
| 2908 | rule->arg.hdr_add.name_len, |
| 2909 | &rule->arg.hdr_add.fmt, |
| 2910 | &rule->arg.hdr_add.re, rule->action)) |
Christopher Faulet | cdade94 | 2017-02-08 12:41:31 +0100 | [diff] [blame] | 2911 | return HTTP_RULE_RES_BADREQ; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 2912 | break; |
| 2913 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2914 | case ACT_HTTP_DEL_HDR: |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2915 | ctx.idx = 0; |
| 2916 | /* remove all occurrences of the header */ |
| 2917 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 2918 | txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 2919 | http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx); |
| 2920 | } |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 2921 | break; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2922 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2923 | case ACT_HTTP_SET_HDR: |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2924 | case ACT_HTTP_ADD_HDR: { |
| 2925 | struct chunk *replace; |
| 2926 | |
| 2927 | replace = alloc_trash_chunk(); |
| 2928 | if (!replace) |
| 2929 | return HTTP_RULE_RES_BADREQ; |
| 2930 | |
| 2931 | chunk_printf(replace, "%s: ", rule->arg.hdr_add.name); |
| 2932 | memcpy(replace->str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len); |
| 2933 | replace->len = rule->arg.hdr_add.name_len; |
| 2934 | replace->str[replace->len++] = ':'; |
| 2935 | replace->str[replace->len++] = ' '; |
| 2936 | replace->len += build_logline(s, replace->str + replace->len, replace->size - replace->len, |
| 2937 | &rule->arg.hdr_add.fmt); |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 2938 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2939 | if (rule->action == ACT_HTTP_SET_HDR) { |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 2940 | /* remove all occurrences of the header */ |
| 2941 | ctx.idx = 0; |
| 2942 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 2943 | txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 2944 | http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx); |
| 2945 | } |
| 2946 | } |
Tim Duesterhus | 3fd1973 | 2018-05-27 20:35:08 +0200 | [diff] [blame] | 2947 | |
| 2948 | if (http_header_add_tail2(&txn->rsp, &txn->hdr_idx, replace->str, replace->len) < 0) { |
| 2949 | static unsigned char rate_limit = 0; |
| 2950 | |
| 2951 | if ((rate_limit++ & 255) == 0) { |
| 2952 | replace->str[rule->arg.hdr_add.name_len] = 0; |
| 2953 | 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, replace->str, s->uniq_id); |
| 2954 | } |
| 2955 | |
| 2956 | HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1); |
| 2957 | if (sess->fe != s->be) |
| 2958 | HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1); |
| 2959 | if (sess->listener->counters) |
| 2960 | HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1); |
| 2961 | if (objt_server(s->target)) |
| 2962 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_rewrites, 1); |
| 2963 | } |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2964 | |
| 2965 | free_trash_chunk(replace); |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2966 | break; |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2967 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2968 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2969 | case ACT_HTTP_DEL_ACL: |
| 2970 | case ACT_HTTP_DEL_MAP: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2971 | struct pat_ref *ref; |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2972 | struct chunk *key; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2973 | |
| 2974 | /* collect reference */ |
| 2975 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 2976 | if (!ref) |
| 2977 | continue; |
| 2978 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2979 | /* allocate key */ |
| 2980 | key = alloc_trash_chunk(); |
| 2981 | if (!key) |
| 2982 | return HTTP_RULE_RES_BADREQ; |
| 2983 | |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2984 | /* collect key */ |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2985 | key->len = build_logline(s, key->str, key->size, &rule->arg.map.key); |
| 2986 | key->str[key->len] = '\0'; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2987 | |
| 2988 | /* perform update */ |
| 2989 | /* returned code: 1=ok, 0=ko */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2990 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2991 | pat_ref_delete(ref, key->str); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2992 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2993 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2994 | free_trash_chunk(key); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2995 | break; |
| 2996 | } |
| 2997 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2998 | case ACT_HTTP_ADD_ACL: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2999 | struct pat_ref *ref; |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 3000 | struct chunk *key; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3001 | |
| 3002 | /* collect reference */ |
| 3003 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3004 | if (!ref) |
| 3005 | continue; |
| 3006 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 3007 | /* allocate key */ |
| 3008 | key = alloc_trash_chunk(); |
| 3009 | if (!key) |
| 3010 | return HTTP_RULE_RES_BADREQ; |
| 3011 | |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3012 | /* collect key */ |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 3013 | key->len = build_logline(s, key->str, key->size, &rule->arg.map.key); |
| 3014 | key->str[key->len] = '\0'; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3015 | |
| 3016 | /* perform update */ |
| 3017 | /* check if the entry already exists */ |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 3018 | if (pat_ref_find_elt(ref, key->str) == NULL) |
| 3019 | pat_ref_add(ref, key->str, NULL, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3020 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 3021 | free_trash_chunk(key); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3022 | break; |
| 3023 | } |
| 3024 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3025 | case ACT_HTTP_SET_MAP: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3026 | struct pat_ref *ref; |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 3027 | struct chunk *key, *value; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3028 | |
| 3029 | /* collect reference */ |
| 3030 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3031 | if (!ref) |
| 3032 | continue; |
| 3033 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 3034 | /* allocate key */ |
| 3035 | key = alloc_trash_chunk(); |
| 3036 | if (!key) |
| 3037 | return HTTP_RULE_RES_BADREQ; |
| 3038 | |
| 3039 | /* allocate value */ |
| 3040 | value = alloc_trash_chunk(); |
| 3041 | if (!value) { |
| 3042 | free_trash_chunk(key); |
| 3043 | return HTTP_RULE_RES_BADREQ; |
| 3044 | } |
| 3045 | |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3046 | /* collect key */ |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 3047 | key->len = build_logline(s, key->str, key->size, &rule->arg.map.key); |
| 3048 | key->str[key->len] = '\0'; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3049 | |
| 3050 | /* collect value */ |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 3051 | value->len = build_logline(s, value->str, value->size, &rule->arg.map.value); |
| 3052 | value->str[value->len] = '\0'; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3053 | |
| 3054 | /* perform update */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 3055 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 3056 | if (pat_ref_find_elt(ref, key->str) != NULL) |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3057 | /* update entry if it exists */ |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 3058 | pat_ref_set(ref, key->str, value->str, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3059 | else |
| 3060 | /* insert a new entry */ |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 3061 | pat_ref_add(ref, key->str, value->str, NULL); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 3062 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 3063 | free_trash_chunk(key); |
| 3064 | free_trash_chunk(value); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3065 | break; |
| 3066 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 3067 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3068 | case ACT_HTTP_REDIR: |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 3069 | if (!http_apply_redirect_rule(rule->arg.redir, s, txn)) |
| 3070 | return HTTP_RULE_RES_BADREQ; |
| 3071 | return HTTP_RULE_RES_DONE; |
| 3072 | |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 3073 | case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX: |
| 3074 | /* Note: only the first valid tracking parameter of each |
| 3075 | * applies. |
| 3076 | */ |
| 3077 | |
Christopher Faulet | 4fce0d8 | 2017-09-18 11:57:31 +0200 | [diff] [blame] | 3078 | if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) { |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 3079 | struct stktable *t; |
| 3080 | struct stksess *ts; |
| 3081 | struct stktable_key *key; |
| 3082 | void *ptr; |
| 3083 | |
| 3084 | t = rule->arg.trk_ctr.table.t; |
| 3085 | key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_RES | SMP_OPT_FINAL, rule->arg.trk_ctr.expr, NULL); |
| 3086 | |
| 3087 | if (key && (ts = stktable_get_entry(t, key))) { |
Christopher Faulet | 4fce0d8 | 2017-09-18 11:57:31 +0200 | [diff] [blame] | 3088 | stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts); |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 3089 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 3090 | HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock); |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 3091 | |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 3092 | /* let's count a new HTTP request as it's the first time we do it */ |
| 3093 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT); |
| 3094 | if (ptr) |
| 3095 | stktable_data_cast(ptr, http_req_cnt)++; |
| 3096 | |
| 3097 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE); |
| 3098 | if (ptr) |
| 3099 | update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate), |
| 3100 | t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1); |
| 3101 | |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 3102 | /* When the client triggers a 4xx from the server, it's most often due |
| 3103 | * to a missing object or permission. These events should be tracked |
| 3104 | * because if they happen often, it may indicate a brute force or a |
| 3105 | * vulnerability scan. Normally this is done when receiving the response |
| 3106 | * but here we're tracking after this ought to have been done so we have |
| 3107 | * to do it on purpose. |
| 3108 | */ |
Willy Tarreau | 3146a4c | 2016-07-26 15:22:33 +0200 | [diff] [blame] | 3109 | if ((unsigned)(txn->status - 400) < 100) { |
| 3110 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT); |
| 3111 | if (ptr) |
| 3112 | stktable_data_cast(ptr, http_err_cnt)++; |
| 3113 | |
| 3114 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE); |
| 3115 | if (ptr) |
| 3116 | update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate), |
| 3117 | t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1); |
| 3118 | } |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 3119 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 3120 | HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock); |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 3121 | |
Emeric Brun | 0fed0b0 | 2017-11-29 16:15:07 +0100 | [diff] [blame] | 3122 | /* If data was modified, we need to touch to re-schedule sync */ |
| 3123 | stktable_touch_local(t, ts, 0); |
| 3124 | |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 3125 | stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT); |
| 3126 | if (sess->fe != s->be) |
| 3127 | stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND); |
| 3128 | |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 3129 | } |
| 3130 | } |
| 3131 | break; |
| 3132 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 3133 | case ACT_CUSTOM: |
Willy Tarreau | 7aa15b0 | 2017-12-20 16:56:50 +0100 | [diff] [blame] | 3134 | if ((s->req.flags & CF_READ_ERROR) || |
| 3135 | ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) && |
| 3136 | !(s->si[0].flags & SI_FL_CLEAN_ABRT) && |
| 3137 | (px->options & PR_O_ABRT_CLOSE))) |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 3138 | act_flags |= ACT_FLAG_FINAL; |
Willy Tarreau | 3945868 | 2015-09-27 10:33:15 +0200 | [diff] [blame] | 3139 | |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 3140 | switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) { |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 3141 | case ACT_RET_ERR: |
| 3142 | case ACT_RET_CONT: |
| 3143 | break; |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 3144 | case ACT_RET_STOP: |
| 3145 | return HTTP_RULE_RES_STOP; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 3146 | case ACT_RET_YIELD: |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 3147 | s->current_rule = rule; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3148 | return HTTP_RULE_RES_YIELD; |
| 3149 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 3150 | break; |
| 3151 | |
Thierry FOURNIER | 22e4901 | 2015-08-05 19:13:48 +0200 | [diff] [blame] | 3152 | /* other flags exists, but normaly, they never be matched. */ |
| 3153 | default: |
| 3154 | break; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3155 | } |
| 3156 | } |
| 3157 | |
| 3158 | /* we reached the end of the rules, nothing to report */ |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 3159 | return HTTP_RULE_RES_CONT; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3160 | } |
| 3161 | |
| 3162 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3163 | /* Perform an HTTP redirect based on the information in <rule>. The function |
| 3164 | * returns non-zero on success, or zero in case of a, irrecoverable error such |
| 3165 | * as too large a request to build a valid response. |
| 3166 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3167 | 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] | 3168 | { |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 3169 | struct http_msg *req = &txn->req; |
| 3170 | struct http_msg *res = &txn->rsp; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3171 | const char *msg_fmt; |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3172 | struct chunk *chunk; |
| 3173 | int ret = 0; |
| 3174 | |
| 3175 | chunk = alloc_trash_chunk(); |
| 3176 | if (!chunk) |
| 3177 | goto leave; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3178 | |
| 3179 | /* build redirect message */ |
| 3180 | switch(rule->code) { |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 3181 | case 308: |
| 3182 | msg_fmt = HTTP_308; |
| 3183 | break; |
| 3184 | case 307: |
| 3185 | msg_fmt = HTTP_307; |
| 3186 | break; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3187 | case 303: |
| 3188 | msg_fmt = HTTP_303; |
| 3189 | break; |
| 3190 | case 301: |
| 3191 | msg_fmt = HTTP_301; |
| 3192 | break; |
| 3193 | case 302: |
| 3194 | default: |
| 3195 | msg_fmt = HTTP_302; |
| 3196 | break; |
| 3197 | } |
| 3198 | |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3199 | if (unlikely(!chunk_strcpy(chunk, msg_fmt))) |
| 3200 | goto leave; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3201 | |
| 3202 | switch(rule->type) { |
| 3203 | case REDIRECT_TYPE_SCHEME: { |
| 3204 | const char *path; |
| 3205 | const char *host; |
| 3206 | struct hdr_ctx ctx; |
| 3207 | int pathlen; |
| 3208 | int hostlen; |
| 3209 | |
| 3210 | host = ""; |
| 3211 | hostlen = 0; |
| 3212 | ctx.idx = 0; |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 3213 | if (http_find_header2("Host", 4, req->chn->buf->p, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3214 | host = ctx.line + ctx.val; |
| 3215 | hostlen = ctx.vlen; |
| 3216 | } |
| 3217 | |
| 3218 | path = http_get_path(txn); |
| 3219 | /* build message using path */ |
| 3220 | if (path) { |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 3221 | pathlen = req->sl.rq.u_l + (req->chn->buf->p + req->sl.rq.u) - path; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3222 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 3223 | int qs = 0; |
| 3224 | while (qs < pathlen) { |
| 3225 | if (path[qs] == '?') { |
| 3226 | pathlen = qs; |
| 3227 | break; |
| 3228 | } |
| 3229 | qs++; |
| 3230 | } |
| 3231 | } |
| 3232 | } else { |
| 3233 | path = "/"; |
| 3234 | pathlen = 1; |
| 3235 | } |
| 3236 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3237 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
| 3238 | /* check if we can add scheme + "://" + host + path */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3239 | if (chunk->len + rule->rdr_len + 3 + hostlen + pathlen > chunk->size - 4) |
| 3240 | goto leave; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3241 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3242 | /* add scheme */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3243 | memcpy(chunk->str + chunk->len, rule->rdr_str, rule->rdr_len); |
| 3244 | chunk->len += rule->rdr_len; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3245 | } |
| 3246 | else { |
| 3247 | /* add scheme with executing log format */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3248 | chunk->len += build_logline(s, chunk->str + chunk->len, chunk->size - chunk->len, &rule->rdr_fmt); |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3249 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3250 | /* check if we can add scheme + "://" + host + path */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3251 | if (chunk->len + 3 + hostlen + pathlen > chunk->size - 4) |
| 3252 | goto leave; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3253 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3254 | /* add "://" */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3255 | memcpy(chunk->str + chunk->len, "://", 3); |
| 3256 | chunk->len += 3; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3257 | |
| 3258 | /* add host */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3259 | memcpy(chunk->str + chunk->len, host, hostlen); |
| 3260 | chunk->len += hostlen; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3261 | |
| 3262 | /* add path */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3263 | memcpy(chunk->str + chunk->len, path, pathlen); |
| 3264 | chunk->len += pathlen; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3265 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3266 | /* append a slash at the end of the location if needed and missing */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3267 | if (chunk->len && chunk->str[chunk->len - 1] != '/' && |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3268 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3269 | if (chunk->len > chunk->size - 5) |
| 3270 | goto leave; |
| 3271 | chunk->str[chunk->len] = '/'; |
| 3272 | chunk->len++; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3273 | } |
| 3274 | |
| 3275 | break; |
| 3276 | } |
| 3277 | case REDIRECT_TYPE_PREFIX: { |
| 3278 | const char *path; |
| 3279 | int pathlen; |
| 3280 | |
| 3281 | path = http_get_path(txn); |
| 3282 | /* build message using path */ |
| 3283 | if (path) { |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 3284 | pathlen = req->sl.rq.u_l + (req->chn->buf->p + req->sl.rq.u) - path; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3285 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 3286 | int qs = 0; |
| 3287 | while (qs < pathlen) { |
| 3288 | if (path[qs] == '?') { |
| 3289 | pathlen = qs; |
| 3290 | break; |
| 3291 | } |
| 3292 | qs++; |
| 3293 | } |
| 3294 | } |
| 3295 | } else { |
| 3296 | path = "/"; |
| 3297 | pathlen = 1; |
| 3298 | } |
| 3299 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3300 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3301 | if (chunk->len + rule->rdr_len + pathlen > chunk->size - 4) |
| 3302 | goto leave; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3303 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3304 | /* add prefix. Note that if prefix == "/", we don't want to |
| 3305 | * add anything, otherwise it makes it hard for the user to |
| 3306 | * configure a self-redirection. |
| 3307 | */ |
| 3308 | if (rule->rdr_len != 1 || *rule->rdr_str != '/') { |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3309 | memcpy(chunk->str + chunk->len, rule->rdr_str, rule->rdr_len); |
| 3310 | chunk->len += rule->rdr_len; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3311 | } |
| 3312 | } |
| 3313 | else { |
| 3314 | /* add prefix with executing log format */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3315 | chunk->len += build_logline(s, chunk->str + chunk->len, chunk->size - chunk->len, &rule->rdr_fmt); |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3316 | |
| 3317 | /* Check length */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3318 | if (chunk->len + pathlen > chunk->size - 4) |
| 3319 | goto leave; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3320 | } |
| 3321 | |
| 3322 | /* add path */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3323 | memcpy(chunk->str + chunk->len, path, pathlen); |
| 3324 | chunk->len += pathlen; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3325 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3326 | /* append a slash at the end of the location if needed and missing */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3327 | if (chunk->len && chunk->str[chunk->len - 1] != '/' && |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3328 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3329 | if (chunk->len > chunk->size - 5) |
| 3330 | goto leave; |
| 3331 | chunk->str[chunk->len] = '/'; |
| 3332 | chunk->len++; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3333 | } |
| 3334 | |
| 3335 | break; |
| 3336 | } |
| 3337 | case REDIRECT_TYPE_LOCATION: |
| 3338 | default: |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3339 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3340 | if (chunk->len + rule->rdr_len > chunk->size - 4) |
| 3341 | goto leave; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3342 | |
| 3343 | /* add location */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3344 | memcpy(chunk->str + chunk->len, rule->rdr_str, rule->rdr_len); |
| 3345 | chunk->len += rule->rdr_len; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3346 | } |
| 3347 | else { |
| 3348 | /* add location with executing log format */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3349 | chunk->len += build_logline(s, chunk->str + chunk->len, chunk->size - chunk->len, &rule->rdr_fmt); |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3350 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3351 | /* Check left length */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3352 | if (chunk->len > chunk->size - 4) |
| 3353 | goto leave; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3354 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3355 | break; |
| 3356 | } |
| 3357 | |
| 3358 | if (rule->cookie_len) { |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3359 | memcpy(chunk->str + chunk->len, "\r\nSet-Cookie: ", 14); |
| 3360 | chunk->len += 14; |
| 3361 | memcpy(chunk->str + chunk->len, rule->cookie_str, rule->cookie_len); |
| 3362 | chunk->len += rule->cookie_len; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3363 | } |
| 3364 | |
Willy Tarreau | 19b1412 | 2017-02-28 09:48:11 +0100 | [diff] [blame] | 3365 | /* add end of headers and the keep-alive/close status. */ |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3366 | txn->status = rule->code; |
| 3367 | /* let's log the request time */ |
| 3368 | s->logs.tv_request = now; |
| 3369 | |
Christopher Faulet | be821b9 | 2017-03-30 11:21:53 +0200 | [diff] [blame] | 3370 | 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] | 3371 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL || |
| 3372 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) { |
| 3373 | /* keep-alive possible */ |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 3374 | if (!(req->flags & HTTP_MSGF_VER_11)) { |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3375 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3376 | memcpy(chunk->str + chunk->len, "\r\nProxy-Connection: keep-alive", 30); |
| 3377 | chunk->len += 30; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3378 | } else { |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3379 | memcpy(chunk->str + chunk->len, "\r\nConnection: keep-alive", 24); |
| 3380 | chunk->len += 24; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3381 | } |
| 3382 | } |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3383 | memcpy(chunk->str + chunk->len, "\r\n\r\n", 4); |
| 3384 | chunk->len += 4; |
| 3385 | FLT_STRM_CB(s, flt_http_reply(s, txn->status, chunk)); |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3386 | co_inject(res->chn, chunk->str, chunk->len); |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3387 | /* "eat" the request */ |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 3388 | bi_fast_delete(req->chn->buf, req->sov); |
| 3389 | req->next -= req->sov; |
| 3390 | req->sov = 0; |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 3391 | 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] | 3392 | 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] | 3393 | req->msg_state = HTTP_MSG_CLOSED; |
| 3394 | res->msg_state = HTTP_MSG_DONE; |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 3395 | /* Trim any possible response */ |
| 3396 | res->chn->buf->i = 0; |
| 3397 | res->next = res->sov = 0; |
Christopher Faulet | 5d468ca | 2017-09-11 09:27:29 +0200 | [diff] [blame] | 3398 | /* let the server side turn to SI_ST_CLO */ |
| 3399 | channel_shutw_now(req->chn); |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3400 | } else { |
| 3401 | /* keep-alive not possible */ |
| 3402 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3403 | memcpy(chunk->str + chunk->len, "\r\nProxy-Connection: close\r\n\r\n", 29); |
| 3404 | chunk->len += 29; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3405 | } else { |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3406 | memcpy(chunk->str + chunk->len, "\r\nConnection: close\r\n\r\n", 23); |
| 3407 | chunk->len += 23; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3408 | } |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3409 | http_reply_and_close(s, txn->status, chunk); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 3410 | req->chn->analysers &= AN_REQ_FLT_END; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3411 | } |
| 3412 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3413 | if (!(s->flags & SF_ERR_MASK)) |
| 3414 | s->flags |= SF_ERR_LOCAL; |
| 3415 | if (!(s->flags & SF_FINST_MASK)) |
| 3416 | s->flags |= SF_FINST_R; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3417 | |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3418 | ret = 1; |
| 3419 | leave: |
| 3420 | free_trash_chunk(chunk); |
| 3421 | return ret; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3422 | } |
| 3423 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3424 | /* This stream analyser runs all HTTP request processing which is common to |
| 3425 | * frontends and backends, which means blocking ACLs, filters, connection-close, |
| 3426 | * reqadd, stats and redirects. This is performed for the designated proxy. |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3427 | * 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] | 3428 | * either needs more data or wants to immediately abort the request (eg: deny, |
| 3429 | * error, ...). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3430 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3431 | 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] | 3432 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 3433 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3434 | struct http_txn *txn = s->txn; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3435 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3436 | struct redirect_rule *rule; |
Willy Tarreau | f4f0412 | 2010-01-28 18:10:50 +0100 | [diff] [blame] | 3437 | struct cond_wordlist *wl; |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3438 | enum rule_result verdict; |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 3439 | int deny_status = HTTP_ERR_403; |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 3440 | struct connection *conn = objt_conn(sess->origin); |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3441 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 3442 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3443 | /* we need more data */ |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3444 | goto return_prx_yield; |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3445 | } |
| 3446 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3447 | DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n", |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3448 | now_ms, __FUNCTION__, |
| 3449 | s, |
| 3450 | req, |
| 3451 | req->rex, req->wex, |
| 3452 | req->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3453 | req->buf->i, |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3454 | req->analysers); |
| 3455 | |
Willy Tarreau | 6541083 | 2014-04-28 21:25:43 +0200 | [diff] [blame] | 3456 | /* just in case we have some per-backend tracking */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3457 | stream_inc_be_http_req_ctr(s); |
Willy Tarreau | 6541083 | 2014-04-28 21:25:43 +0200 | [diff] [blame] | 3458 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3459 | /* evaluate http-request rules */ |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3460 | if (!LIST_ISEMPTY(&px->http_req_rules)) { |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 3461 | 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] | 3462 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3463 | switch (verdict) { |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3464 | case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */ |
| 3465 | goto return_prx_yield; |
| 3466 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3467 | case HTTP_RULE_RES_CONT: |
| 3468 | case HTTP_RULE_RES_STOP: /* nothing to do */ |
| 3469 | break; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 3470 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3471 | case HTTP_RULE_RES_DENY: /* deny or tarpit */ |
| 3472 | if (txn->flags & TX_CLTARPIT) |
| 3473 | goto tarpit; |
| 3474 | goto deny; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 3475 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3476 | case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */ |
| 3477 | goto return_prx_cond; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 3478 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3479 | case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */ |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 3480 | goto done; |
| 3481 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3482 | case HTTP_RULE_RES_BADREQ: /* failed with a bad request */ |
| 3483 | goto return_bad_req; |
| 3484 | } |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3485 | } |
| 3486 | |
Olivier Houchard | 25ae45a | 2017-11-29 19:51:19 +0100 | [diff] [blame] | 3487 | if (conn && (conn->flags & CO_FL_EARLY_DATA) && |
| 3488 | (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE))) { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 3489 | struct hdr_ctx ctx; |
| 3490 | |
| 3491 | ctx.idx = 0; |
| 3492 | if (!http_find_header2("Early-Data", strlen("Early-Data"), |
| 3493 | s->req.buf->p, &txn->hdr_idx, &ctx)) { |
| 3494 | if (unlikely(http_header_add_tail2(&txn->req, |
| 3495 | &txn->hdr_idx, "Early-Data: 1", |
| 3496 | strlen("Early-Data: 1"))) < 0) { |
| 3497 | goto return_bad_req; |
| 3498 | } |
| 3499 | } |
| 3500 | |
| 3501 | } |
| 3502 | |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 3503 | /* OK at this stage, we know that the request was accepted according to |
| 3504 | * the http-request rules, we can check for the stats. Note that the |
| 3505 | * URI is detected *before* the req* rules in order not to be affected |
| 3506 | * by a possible reqrep, while they are processed *after* so that a |
| 3507 | * reqdeny can still block them. This clearly needs to change in 1.6! |
| 3508 | */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 3509 | if (stats_check_uri(&s->si[1], txn, px)) { |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 3510 | s->target = &http_stats_applet.obj_type; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 3511 | 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] | 3512 | txn->status = 500; |
| 3513 | s->logs.tv_request = now; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 3514 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3515 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3516 | if (!(s->flags & SF_ERR_MASK)) |
| 3517 | s->flags |= SF_ERR_RESOURCE; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 3518 | goto return_prx_cond; |
| 3519 | } |
| 3520 | |
| 3521 | /* parse the whole stats request and extract the relevant information */ |
| 3522 | http_handle_stats(s, req); |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 3523 | 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] | 3524 | /* not all actions implemented: deny, allow, auth */ |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3525 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3526 | if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */ |
| 3527 | goto deny; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 3528 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3529 | if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */ |
| 3530 | goto return_prx_cond; |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 3531 | } |
| 3532 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3533 | /* evaluate the req* rules except reqadd */ |
| 3534 | if (px->req_exp != NULL) { |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 3535 | if (apply_filters_to_request(s, req, px) < 0) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3536 | goto return_bad_req; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3537 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3538 | if (txn->flags & TX_CLDENY) |
| 3539 | goto deny; |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 3540 | |
Jarno Huuskonen | 800d176 | 2017-03-06 14:56:36 +0200 | [diff] [blame] | 3541 | if (txn->flags & TX_CLTARPIT) { |
| 3542 | deny_status = HTTP_ERR_500; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3543 | goto tarpit; |
Jarno Huuskonen | 800d176 | 2017-03-06 14:56:36 +0200 | [diff] [blame] | 3544 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3545 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3546 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3547 | /* add request headers from the rule sets in the same order */ |
| 3548 | list_for_each_entry(wl, &px->req_add, list) { |
| 3549 | if (wl->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 3550 | 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] | 3551 | ret = acl_pass(ret); |
| 3552 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 3553 | ret = !ret; |
| 3554 | if (!ret) |
| 3555 | continue; |
| 3556 | } |
| 3557 | |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 3558 | 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] | 3559 | goto return_bad_req; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 3560 | } |
| 3561 | |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 3562 | |
| 3563 | /* Proceed with the stats now. */ |
William Lallemand | 71bd11a | 2017-11-20 19:13:14 +0100 | [diff] [blame] | 3564 | if (unlikely(objt_applet(s->target) == &http_stats_applet) || |
| 3565 | unlikely(objt_applet(s->target) == &http_cache_applet)) { |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3566 | /* process the stats request now */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3567 | 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] | 3568 | HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1); |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 3569 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3570 | if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is |
| 3571 | s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy |
| 3572 | if (!(s->flags & SF_FINST_MASK)) |
| 3573 | s->flags |= SF_FINST_R; |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 3574 | |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 3575 | /* 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] | 3576 | req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END); |
| 3577 | req->analysers &= ~AN_REQ_FLT_XFER_DATA; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 3578 | req->analysers |= AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3579 | goto done; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3580 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 3581 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3582 | /* check whether we have some ACLs set to redirect this request */ |
| 3583 | list_for_each_entry(rule, &px->redirect_rules, list) { |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 3584 | if (rule->cond) { |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3585 | int ret; |
| 3586 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 3587 | 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] | 3588 | ret = acl_pass(ret); |
| 3589 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 3590 | ret = !ret; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3591 | if (!ret) |
| 3592 | continue; |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 3593 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3594 | if (!http_apply_redirect_rule(rule, s, txn)) |
| 3595 | goto return_bad_req; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3596 | goto done; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3597 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 3598 | |
Willy Tarreau | 2be3939 | 2010-01-03 17:24:51 +0100 | [diff] [blame] | 3599 | /* POST requests may be accompanied with an "Expect: 100-Continue" header. |
| 3600 | * If this happens, then the data will not come immediately, so we must |
| 3601 | * send all what we have without waiting. Note that due to the small gain |
| 3602 | * 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] | 3603 | * 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] | 3604 | * itself once used. |
| 3605 | */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 3606 | req->flags |= CF_SEND_DONTWAIT; |
Willy Tarreau | 2be3939 | 2010-01-03 17:24:51 +0100 | [diff] [blame] | 3607 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3608 | done: /* done with this analyser, continue with next ones that the calling |
| 3609 | * points will have set, if any. |
| 3610 | */ |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3611 | req->analyse_exp = TICK_ETERNITY; |
Thierry FOURNIER | 7566e30 | 2014-08-22 06:55:26 +0200 | [diff] [blame] | 3612 | done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */ |
| 3613 | req->analysers &= ~an_bit; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3614 | return 1; |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 3615 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3616 | tarpit: |
Willy Tarreau | 6a0bca9 | 2017-06-11 17:56:27 +0200 | [diff] [blame] | 3617 | /* Allow cookie logging |
| 3618 | */ |
| 3619 | if (s->be->cookie_name || sess->fe->capture_name) |
| 3620 | manage_client_side_cookies(s, req); |
| 3621 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3622 | /* When a connection is tarpitted, we use the tarpit timeout, |
| 3623 | * which may be the same as the connect timeout if unspecified. |
| 3624 | * If unset, then set it to zero because we really want it to |
| 3625 | * eventually expire. We build the tarpit as an analyser. |
| 3626 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 3627 | channel_erase(&s->req); |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3628 | |
| 3629 | /* wipe the request out so that we can drop the connection early |
| 3630 | * if the client closes first. |
| 3631 | */ |
| 3632 | channel_dont_connect(req); |
Bertrand Paquet | 83a2c3d | 2016-04-06 11:58:31 +0200 | [diff] [blame] | 3633 | |
Jarno Huuskonen | 800d176 | 2017-03-06 14:56:36 +0200 | [diff] [blame] | 3634 | txn->status = http_err_codes[deny_status]; |
| 3635 | |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 3636 | req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */ |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3637 | req->analysers |= AN_REQ_HTTP_TARPIT; |
| 3638 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit); |
| 3639 | if (!req->analyse_exp) |
| 3640 | req->analyse_exp = tick_add(now_ms, 0); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3641 | stream_inc_http_err_ctr(s); |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 3642 | HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3643 | if (sess->fe != s->be) |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 3644 | HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 3645 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 3646 | HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1); |
Thierry FOURNIER | 7566e30 | 2014-08-22 06:55:26 +0200 | [diff] [blame] | 3647 | goto done_without_exp; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3648 | |
| 3649 | deny: /* this request was blocked (denied) */ |
Bertrand Paquet | 83a2c3d | 2016-04-06 11:58:31 +0200 | [diff] [blame] | 3650 | |
| 3651 | /* Allow cookie logging |
| 3652 | */ |
| 3653 | if (s->be->cookie_name || sess->fe->capture_name) |
| 3654 | manage_client_side_cookies(s, req); |
| 3655 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3656 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 3657 | txn->status = http_err_codes[deny_status]; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3658 | s->logs.tv_request = now; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 3659 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3660 | stream_inc_http_err_ctr(s); |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 3661 | HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3662 | if (sess->fe != s->be) |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 3663 | HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 3664 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 3665 | HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1); |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3666 | goto return_prx_cond; |
| 3667 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3668 | return_bad_req: |
| 3669 | /* We centralize bad requests processing here */ |
| 3670 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 3671 | /* we detected a parsing error. We want to archive this request |
| 3672 | * in the dedicated proxy area for later troubleshooting. |
| 3673 | */ |
Emeric Brun | 8c1aaa2 | 2017-06-15 11:30:06 +0200 | [diff] [blame] | 3674 | http_capture_bad_message(sess->fe, &sess->fe->invalid_req, s, msg, msg->err_state, sess->fe); |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3675 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 3676 | |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 3677 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3678 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3679 | txn->status = 400; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 3680 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3681 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 3682 | HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 3683 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 3684 | HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1); |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 3685 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3686 | return_prx_cond: |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3687 | if (!(s->flags & SF_ERR_MASK)) |
| 3688 | s->flags |= SF_ERR_PRXCOND; |
| 3689 | if (!(s->flags & SF_FINST_MASK)) |
| 3690 | s->flags |= SF_FINST_R; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 3691 | |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 3692 | req->analysers &= AN_REQ_FLT_END; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3693 | req->analyse_exp = TICK_ETERNITY; |
| 3694 | return 0; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3695 | |
| 3696 | return_prx_yield: |
| 3697 | channel_dont_connect(req); |
| 3698 | return 0; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3699 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3700 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3701 | /* This function performs all the processing enabled for the current request. |
| 3702 | * It returns 1 if the processing can continue on next analysers, or zero if it |
| 3703 | * needs more data, encounters an error, or wants to immediately abort the |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 3704 | * request. It relies on buffers flags, and updates s->req.analysers. |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3705 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3706 | 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] | 3707 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 3708 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3709 | struct http_txn *txn = s->txn; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3710 | struct http_msg *msg = &txn->req; |
Willy Tarreau | ee335e6 | 2015-04-21 18:15:13 +0200 | [diff] [blame] | 3711 | struct connection *cli_conn = objt_conn(strm_sess(s)->origin); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3712 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 3713 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3714 | /* we need more data */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 3715 | channel_dont_connect(req); |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3716 | return 0; |
| 3717 | } |
| 3718 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3719 | DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n", |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3720 | now_ms, __FUNCTION__, |
| 3721 | s, |
| 3722 | req, |
| 3723 | req->rex, req->wex, |
| 3724 | req->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3725 | req->buf->i, |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3726 | req->analysers); |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3727 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3728 | /* |
| 3729 | * Right now, we know that we have processed the entire headers |
| 3730 | * and that unwanted requests have been filtered out. We can do |
| 3731 | * whatever we want with the remaining request. Also, now we |
| 3732 | * may have separate values for ->fe, ->be. |
| 3733 | */ |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3734 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3735 | /* |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3736 | * If HTTP PROXY is set we simply get remote server address parsing |
| 3737 | * incoming request. Note that this requires that a connection is |
| 3738 | * allocated on the server side. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3739 | */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3740 | 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] | 3741 | struct connection *conn; |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 3742 | char *path; |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 3743 | |
Willy Tarreau | 9471b8c | 2013-12-15 13:31:35 +0100 | [diff] [blame] | 3744 | /* Note that for now we don't reuse existing proxy connections */ |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 3745 | 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] | 3746 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 3747 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3748 | txn->status = 500; |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 3749 | req->analysers &= AN_REQ_FLT_END; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 3750 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 3751 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3752 | if (!(s->flags & SF_ERR_MASK)) |
| 3753 | s->flags |= SF_ERR_RESOURCE; |
| 3754 | if (!(s->flags & SF_FINST_MASK)) |
| 3755 | s->flags |= SF_FINST_R; |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 3756 | |
| 3757 | return 0; |
| 3758 | } |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 3759 | |
| 3760 | path = http_get_path(txn); |
Christopher Faulet | 11ebb20 | 2018-04-13 15:53:12 +0200 | [diff] [blame] | 3761 | if (url2sa(req->buf->p + msg->sl.rq.u, |
| 3762 | path ? path - (req->buf->p + msg->sl.rq.u) : msg->sl.rq.u_l, |
| 3763 | &conn->addr.to, NULL) == -1) |
| 3764 | goto return_bad_req; |
| 3765 | |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 3766 | /* if the path was found, we have to remove everything between |
| 3767 | * req->buf->p + msg->sl.rq.u and path (excluded). If it was not |
| 3768 | * found, we need to replace from req->buf->p + msg->sl.rq.u for |
| 3769 | * u_l characters by a single "/". |
| 3770 | */ |
| 3771 | if (path) { |
| 3772 | char *cur_ptr = req->buf->p; |
| 3773 | char *cur_end = cur_ptr + txn->req.sl.rq.l; |
| 3774 | int delta; |
| 3775 | |
| 3776 | delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0); |
| 3777 | http_msg_move_end(&txn->req, delta); |
| 3778 | cur_end += delta; |
| 3779 | if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL) |
| 3780 | goto return_bad_req; |
| 3781 | } |
| 3782 | else { |
| 3783 | char *cur_ptr = req->buf->p; |
| 3784 | char *cur_end = cur_ptr + txn->req.sl.rq.l; |
| 3785 | int delta; |
| 3786 | |
| 3787 | delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, |
| 3788 | req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1); |
| 3789 | http_msg_move_end(&txn->req, delta); |
| 3790 | cur_end += delta; |
| 3791 | if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL) |
| 3792 | goto return_bad_req; |
| 3793 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3794 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3795 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3796 | /* |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 3797 | * 7: Now we can work with the cookies. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3798 | * Note that doing so might move headers in the request, but |
| 3799 | * the fields will stay coherent and the URI will not move. |
| 3800 | * This should only be performed in the backend. |
| 3801 | */ |
Bertrand Paquet | 83a2c3d | 2016-04-06 11:58:31 +0200 | [diff] [blame] | 3802 | if (s->be->cookie_name || sess->fe->capture_name) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3803 | manage_client_side_cookies(s, req); |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 3804 | |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 3805 | /* add unique-id if "header-unique-id" is specified */ |
| 3806 | |
Thierry Fournier | f4011dd | 2016-03-29 17:23:51 +0200 | [diff] [blame] | 3807 | if (!LIST_ISEMPTY(&sess->fe->format_unique_id) && !s->unique_id) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 3808 | if ((s->unique_id = pool_alloc(pool_head_uniqueid)) == NULL) |
William Lallemand | 5b7ea3a | 2013-08-28 15:44:19 +0200 | [diff] [blame] | 3809 | goto return_bad_req; |
| 3810 | s->unique_id[0] = '\0'; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3811 | 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] | 3812 | } |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 3813 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3814 | if (sess->fe->header_unique_id && s->unique_id) { |
| 3815 | chunk_printf(&trash, "%s: %s", sess->fe->header_unique_id, s->unique_id); |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3816 | if (trash.len < 0) |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 3817 | goto return_bad_req; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3818 | if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0)) |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 3819 | goto return_bad_req; |
| 3820 | } |
| 3821 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 3822 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3823 | * 9: add X-Forwarded-For if either the frontend or the backend |
| 3824 | * asks for it. |
| 3825 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3826 | if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) { |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 3827 | struct hdr_ctx ctx = { .idx = 0 }; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3828 | if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) && |
| 3829 | http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name, |
| 3830 | s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3831 | req->buf->p, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 3832 | /* The header is set to be added only if none is present |
| 3833 | * and we found it, so don't do anything. |
| 3834 | */ |
| 3835 | } |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3836 | else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3837 | /* Add an X-Forwarded-For header unless the source IP is |
| 3838 | * in the 'except' network range. |
| 3839 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3840 | if ((!sess->fe->except_mask.s_addr || |
| 3841 | (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr) |
| 3842 | != sess->fe->except_net.s_addr) && |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3843 | (!s->be->except_mask.s_addr || |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3844 | (((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] | 3845 | != s->be->except_net.s_addr)) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 3846 | int len; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3847 | unsigned char *pn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3848 | pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr; |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 3849 | |
| 3850 | /* Note: we rely on the backend to get the header name to be used for |
| 3851 | * x-forwarded-for, because the header is really meant for the backends. |
| 3852 | * However, if the backend did not specify any option, we have to rely |
| 3853 | * on the frontend's header name. |
| 3854 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3855 | if (s->be->fwdfor_hdr_len) { |
| 3856 | len = s->be->fwdfor_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3857 | memcpy(trash.str, s->be->fwdfor_hdr_name, len); |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 3858 | } else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3859 | len = sess->fe->fwdfor_hdr_len; |
| 3860 | memcpy(trash.str, sess->fe->fwdfor_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 3861 | } |
Willy Tarreau | e9187f8 | 2014-04-14 15:27:14 +0200 | [diff] [blame] | 3862 | len += snprintf(trash.str + len, trash.size - len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]); |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 3863 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3864 | if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0)) |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3865 | goto return_bad_req; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 3866 | } |
| 3867 | } |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3868 | else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3869 | /* FIXME: for the sake of completeness, we should also support |
| 3870 | * 'except' here, although it is mostly useless in this case. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3871 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3872 | int len; |
| 3873 | char pn[INET6_ADDRSTRLEN]; |
| 3874 | inet_ntop(AF_INET6, |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3875 | (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3876 | pn, sizeof(pn)); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3877 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3878 | /* Note: we rely on the backend to get the header name to be used for |
| 3879 | * x-forwarded-for, because the header is really meant for the backends. |
| 3880 | * However, if the backend did not specify any option, we have to rely |
| 3881 | * on the frontend's header name. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3882 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3883 | if (s->be->fwdfor_hdr_len) { |
| 3884 | len = s->be->fwdfor_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3885 | memcpy(trash.str, s->be->fwdfor_hdr_name, len); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3886 | } else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3887 | len = sess->fe->fwdfor_hdr_len; |
| 3888 | memcpy(trash.str, sess->fe->fwdfor_hdr_name, len); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3889 | } |
Willy Tarreau | e9187f8 | 2014-04-14 15:27:14 +0200 | [diff] [blame] | 3890 | len += snprintf(trash.str + len, trash.size - len, ": %s", pn); |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3891 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3892 | if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0)) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3893 | goto return_bad_req; |
| 3894 | } |
| 3895 | } |
| 3896 | |
| 3897 | /* |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3898 | * 10: add X-Original-To if either the frontend or the backend |
| 3899 | * asks for it. |
| 3900 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3901 | if ((sess->fe->options | s->be->options) & PR_O_ORGTO) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3902 | |
| 3903 | /* FIXME: don't know if IPv6 can handle that case too. */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3904 | if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3905 | /* Add an X-Original-To header unless the destination IP is |
| 3906 | * in the 'except' network range. |
| 3907 | */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3908 | conn_get_to_addr(cli_conn); |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3909 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3910 | if (cli_conn->addr.to.ss_family == AF_INET && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3911 | ((!sess->fe->except_mask_to.s_addr || |
| 3912 | (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr) |
| 3913 | != sess->fe->except_to.s_addr) && |
Emeric Brun | 5bd86a8 | 2010-10-22 17:23:04 +0200 | [diff] [blame] | 3914 | (!s->be->except_mask_to.s_addr || |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3915 | (((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] | 3916 | != s->be->except_to.s_addr))) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3917 | int len; |
| 3918 | unsigned char *pn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3919 | pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr; |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3920 | |
| 3921 | /* Note: we rely on the backend to get the header name to be used for |
| 3922 | * x-original-to, because the header is really meant for the backends. |
| 3923 | * However, if the backend did not specify any option, we have to rely |
| 3924 | * on the frontend's header name. |
| 3925 | */ |
| 3926 | if (s->be->orgto_hdr_len) { |
| 3927 | len = s->be->orgto_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3928 | memcpy(trash.str, s->be->orgto_hdr_name, len); |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3929 | } else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3930 | len = sess->fe->orgto_hdr_len; |
| 3931 | memcpy(trash.str, sess->fe->orgto_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 3932 | } |
Willy Tarreau | e9187f8 | 2014-04-14 15:27:14 +0200 | [diff] [blame] | 3933 | len += snprintf(trash.str + len, trash.size - len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]); |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3934 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3935 | if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0)) |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3936 | goto return_bad_req; |
| 3937 | } |
| 3938 | } |
| 3939 | } |
| 3940 | |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 3941 | /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set. |
| 3942 | * If an "Upgrade" token is found, the header is left untouched in order not to have |
| 3943 | * to deal with some servers bugs : some of them fail an Upgrade if anything but |
| 3944 | * "Upgrade" is present in the Connection header. |
| 3945 | */ |
| 3946 | if (!(txn->flags & TX_HDR_CONN_UPG) && |
| 3947 | (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3948 | ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 3949 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) { |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3950 | unsigned int want_flags = 0; |
| 3951 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3952 | if (msg->flags & HTTP_MSGF_VER_11) { |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 3953 | if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3954 | ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 3955 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3956 | !((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3957 | want_flags |= TX_CON_CLO_SET; |
| 3958 | } else { |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 3959 | if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3960 | ((sess->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL && |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 3961 | (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3962 | ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3963 | want_flags |= TX_CON_KAL_SET; |
| 3964 | } |
| 3965 | |
| 3966 | 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] | 3967 | http_change_connection_header(txn, msg, want_flags); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3968 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3969 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3970 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3971 | /* If we have no server assigned yet and we're balancing on url_param |
| 3972 | * with a POST request, we may be interested in checking the body for |
| 3973 | * that parameter. This will be done in another analyser. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3974 | */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3975 | if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) && |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3976 | s->txn->meth == HTTP_METH_POST && s->be->url_param_name != NULL && |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3977 | (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) { |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 3978 | channel_dont_connect(req); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3979 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3980 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3981 | |
Christopher Faulet | be821b9 | 2017-03-30 11:21:53 +0200 | [diff] [blame] | 3982 | req->analysers &= ~AN_REQ_FLT_XFER_DATA; |
| 3983 | req->analysers |= AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 3984 | #ifdef TCP_QUICKACK |
Christopher Faulet | be821b9 | 2017-03-30 11:21:53 +0200 | [diff] [blame] | 3985 | /* We expect some data from the client. Unless we know for sure |
| 3986 | * we already have a full request, we have to re-enable quick-ack |
| 3987 | * in case we previously disabled it, otherwise we might cause |
| 3988 | * the client to delay further data. |
| 3989 | */ |
| 3990 | if ((sess->listener->options & LI_O_NOQUICKACK) && |
| 3991 | cli_conn && conn_ctrl_ready(cli_conn) && |
| 3992 | ((msg->flags & HTTP_MSGF_TE_CHNK) || |
| 3993 | (msg->body_len > req->buf->i - txn->req.eoh - 2))) |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 3994 | setsockopt(cli_conn->handle.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one)); |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 3995 | #endif |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 3996 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3997 | /************************************************************* |
| 3998 | * OK, that's finished for the headers. We have done what we * |
| 3999 | * could. Let's switch to the DATA state. * |
| 4000 | ************************************************************/ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4001 | req->analyse_exp = TICK_ETERNITY; |
| 4002 | req->analysers &= ~an_bit; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4003 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4004 | s->logs.tv_request = now; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4005 | /* OK let's go on with the BODY now */ |
| 4006 | return 1; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4007 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4008 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 4009 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
Willy Tarreau | f073a83 | 2009-03-01 23:21:47 +0100 | [diff] [blame] | 4010 | /* we detected a parsing error. We want to archive this request |
| 4011 | * in the dedicated proxy area for later troubleshooting. |
| 4012 | */ |
Emeric Brun | 8c1aaa2 | 2017-06-15 11:30:06 +0200 | [diff] [blame] | 4013 | http_capture_bad_message(sess->fe, &sess->fe->invalid_req, s, msg, msg->err_state, sess->fe); |
Willy Tarreau | f073a83 | 2009-03-01 23:21:47 +0100 | [diff] [blame] | 4014 | } |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 4015 | |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 4016 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4017 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4018 | txn->status = 400; |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4019 | req->analysers &= AN_REQ_FLT_END; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 4020 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4021 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 4022 | HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4023 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 4024 | HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1); |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 4025 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4026 | if (!(s->flags & SF_ERR_MASK)) |
| 4027 | s->flags |= SF_ERR_PRXCOND; |
| 4028 | if (!(s->flags & SF_FINST_MASK)) |
| 4029 | s->flags |= SF_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 4030 | return 0; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 4031 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 4032 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4033 | /* This function is an analyser which processes the HTTP tarpit. It always |
| 4034 | * returns zero, at the beginning because it prevents any other processing |
| 4035 | * from occurring, and at the end because it terminates the request. |
| 4036 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4037 | 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] | 4038 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4039 | struct http_txn *txn = s->txn; |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4040 | |
| 4041 | /* This connection is being tarpitted. The CLIENT side has |
| 4042 | * already set the connect expiration date to the right |
| 4043 | * timeout. We just have to check that the client is still |
| 4044 | * there and that the timeout has not expired. |
| 4045 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4046 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4047 | if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 && |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4048 | !tick_is_expired(req->analyse_exp, now_ms)) |
| 4049 | return 0; |
| 4050 | |
| 4051 | /* We will set the queue timer to the time spent, just for |
| 4052 | * logging purposes. We fake a 500 server error, so that the |
| 4053 | * attacker will not suspect his connection has been tarpitted. |
| 4054 | * It will not cause trouble to the logs because we can exclude |
| 4055 | * the tarpitted connections by filtering on the 'PT' status flags. |
| 4056 | */ |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4057 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 4058 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4059 | if (!(req->flags & CF_READ_ERROR)) |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 4060 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4061 | |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4062 | req->analysers &= AN_REQ_FLT_END; |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4063 | req->analyse_exp = TICK_ETERNITY; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4064 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4065 | if (!(s->flags & SF_ERR_MASK)) |
| 4066 | s->flags |= SF_ERR_PRXCOND; |
| 4067 | if (!(s->flags & SF_FINST_MASK)) |
| 4068 | s->flags |= SF_FINST_T; |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4069 | return 0; |
| 4070 | } |
| 4071 | |
Willy Tarreau | 5a8f947 | 2014-04-10 11:16:06 +0200 | [diff] [blame] | 4072 | /* This function is an analyser which waits for the HTTP request body. It waits |
| 4073 | * for either the buffer to be full, or the full advertised contents to have |
| 4074 | * reached the buffer. It must only be called after the standard HTTP request |
| 4075 | * processing has occurred, because it expects the request to be parsed and will |
| 4076 | * look for the Expect header. It may send a 100-Continue interim response. It |
| 4077 | * takes in input any state starting from HTTP_MSG_BODY and leaves with one of |
| 4078 | * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it |
| 4079 | * 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] | 4080 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4081 | 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] | 4082 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4083 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4084 | struct http_txn *txn = s->txn; |
| 4085 | struct http_msg *msg = &s->txn->req; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4086 | |
| 4087 | /* We have to parse the HTTP request body to find any required data. |
| 4088 | * "balance url_param check_post" should have been the only way to get |
| 4089 | * into this. We were brought here after HTTP header analysis, so all |
| 4090 | * related structures are ready. |
| 4091 | */ |
| 4092 | |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4093 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
| 4094 | /* This is the first call */ |
| 4095 | if (msg->msg_state < HTTP_MSG_BODY) |
| 4096 | goto missing_data; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4097 | |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4098 | if (msg->msg_state < HTTP_MSG_100_SENT) { |
| 4099 | /* If we have HTTP/1.1 and Expect: 100-continue, then we must |
| 4100 | * send an HTTP/1.1 100 Continue intermediate response. |
| 4101 | */ |
| 4102 | if (msg->flags & HTTP_MSGF_VER_11) { |
| 4103 | struct hdr_ctx ctx; |
| 4104 | ctx.idx = 0; |
| 4105 | /* Expect is allowed in 1.1, look for it */ |
| 4106 | if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) && |
| 4107 | unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) { |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4108 | co_inject(&s->res, http_100_chunk.str, http_100_chunk.len); |
Thierry FOURNIER / OZON.IO | 43ad11d | 2016-12-12 15:19:58 +0100 | [diff] [blame] | 4109 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4110 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4111 | } |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4112 | msg->msg_state = HTTP_MSG_100_SENT; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4113 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4114 | |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 4115 | /* we have msg->sov which points to the first byte of message body. |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 4116 | * req->buf->p still points to the beginning of the message. We |
| 4117 | * must save the body in msg->next because it survives buffer |
| 4118 | * re-alignments. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4119 | */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 4120 | msg->next = msg->sov; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4121 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4122 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4123 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 4124 | else |
| 4125 | msg->msg_state = HTTP_MSG_DATA; |
| 4126 | } |
| 4127 | |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4128 | if (!(msg->flags & HTTP_MSGF_TE_CHNK)) { |
| 4129 | /* 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] | 4130 | if (http_body_bytes(msg) < msg->body_len) |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4131 | goto missing_data; |
| 4132 | |
| 4133 | /* OK we have everything we need now */ |
| 4134 | goto http_end; |
| 4135 | } |
| 4136 | |
| 4137 | /* OK here we're parsing a chunked-encoded message */ |
| 4138 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4139 | if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 4140 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4141 | * 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] | 4142 | * TRAILERS state. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 4143 | */ |
Willy Tarreau | e56cdd3 | 2017-09-21 08:36:33 +0200 | [diff] [blame] | 4144 | unsigned int chunk; |
| 4145 | int ret = h1_parse_chunk_size(req->buf, msg->next, req->buf->i, &chunk); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4146 | |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 4147 | if (!ret) |
| 4148 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4149 | else if (ret < 0) { |
Willy Tarreau | e56cdd3 | 2017-09-21 08:36:33 +0200 | [diff] [blame] | 4150 | msg->err_pos = req->buf->i + ret; |
| 4151 | if (msg->err_pos < 0) |
| 4152 | msg->err_pos += req->buf->size; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4153 | stream_inc_http_err_ctr(s); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4154 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4155 | } |
Willy Tarreau | e56cdd3 | 2017-09-21 08:36:33 +0200 | [diff] [blame] | 4156 | |
| 4157 | msg->chunk_len = chunk; |
| 4158 | msg->body_len += chunk; |
| 4159 | |
| 4160 | msg->sol = ret; |
Christopher Faulet | 113f7de | 2015-12-14 14:52:13 +0100 | [diff] [blame] | 4161 | msg->next += ret; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 4162 | msg->msg_state = msg->chunk_len ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4163 | } |
| 4164 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4165 | /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state. |
Willy Tarreau | e115b49 | 2015-05-01 23:05:14 +0200 | [diff] [blame] | 4166 | * We have the first data byte is in msg->sov + msg->sol. We're waiting |
| 4167 | * for at least a whole chunk or the whole content length bytes after |
| 4168 | * msg->sov + msg->sol. |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4169 | */ |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4170 | if (msg->msg_state == HTTP_MSG_TRAILERS) |
| 4171 | goto http_end; |
| 4172 | |
Willy Tarreau | e115b49 | 2015-05-01 23:05:14 +0200 | [diff] [blame] | 4173 | 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] | 4174 | goto http_end; |
| 4175 | |
| 4176 | missing_data: |
Willy Tarreau | 31a1995 | 2014-04-10 11:50:37 +0200 | [diff] [blame] | 4177 | /* we get here if we need to wait for more data. If the buffer is full, |
| 4178 | * we have the maximum we can expect. |
| 4179 | */ |
| 4180 | if (buffer_full(req->buf, global.tune.maxrewrite)) |
| 4181 | goto http_end; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 4182 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4183 | 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] | 4184 | txn->status = 408; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 4185 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 4186 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4187 | if (!(s->flags & SF_ERR_MASK)) |
| 4188 | s->flags |= SF_ERR_CLITO; |
| 4189 | if (!(s->flags & SF_FINST_MASK)) |
| 4190 | s->flags |= SF_FINST_D; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4191 | goto return_err_msg; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4192 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4193 | |
| 4194 | /* we get here if we need to wait for more data */ |
Willy Tarreau | 31a1995 | 2014-04-10 11:50:37 +0200 | [diff] [blame] | 4195 | if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) { |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4196 | /* Not enough data. We'll re-use the http-request |
| 4197 | * timeout here. Ideally, we should set the timeout |
| 4198 | * relative to the accept() date. We just set the |
| 4199 | * request timeout once at the beginning of the |
| 4200 | * request. |
| 4201 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4202 | channel_dont_connect(req); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4203 | if (!tick_isset(req->analyse_exp)) |
Willy Tarreau | cd7afc0 | 2009-07-12 10:03:17 +0200 | [diff] [blame] | 4204 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4205 | return 0; |
| 4206 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4207 | |
| 4208 | http_end: |
| 4209 | /* The situation will not evolve, so let's give up on the analysis. */ |
| 4210 | s->logs.tv_request = now; /* update the request timer to reflect full request */ |
| 4211 | req->analysers &= ~an_bit; |
| 4212 | req->analyse_exp = TICK_ETERNITY; |
| 4213 | return 1; |
| 4214 | |
| 4215 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 4216 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4217 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4218 | txn->status = 400; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 4219 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4220 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4221 | if (!(s->flags & SF_ERR_MASK)) |
| 4222 | s->flags |= SF_ERR_PRXCOND; |
| 4223 | if (!(s->flags & SF_FINST_MASK)) |
| 4224 | s->flags |= SF_FINST_R; |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 4225 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4226 | return_err_msg: |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4227 | req->analysers &= AN_REQ_FLT_END; |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 4228 | HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4229 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 4230 | HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4231 | return 0; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4232 | } |
| 4233 | |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4234 | /* send a server's name with an outgoing request over an established connection. |
| 4235 | * Note: this function is designed to be called once the request has been scheduled |
| 4236 | * for being forwarded. This is the reason why it rewinds the buffer before |
| 4237 | * proceeding. |
| 4238 | */ |
Willy Tarreau | 45c0d98 | 2012-03-09 12:11:57 +0100 | [diff] [blame] | 4239 | 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] | 4240 | |
| 4241 | struct hdr_ctx ctx; |
| 4242 | |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4243 | char *hdr_name = be->server_id_hdr_name; |
| 4244 | int hdr_name_len = be->server_id_hdr_len; |
Willy Tarreau | 394db37 | 2012-10-12 22:40:39 +0200 | [diff] [blame] | 4245 | struct channel *chn = txn->req.chn; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4246 | char *hdr_val; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4247 | unsigned int old_o, old_i; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4248 | |
William Lallemand | d9e9066 | 2012-01-30 17:27:17 +0100 | [diff] [blame] | 4249 | ctx.idx = 0; |
| 4250 | |
Willy Tarreau | 211cdec | 2014-04-17 20:18:08 +0200 | [diff] [blame] | 4251 | old_o = http_hdr_rewind(&txn->req); |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4252 | if (old_o) { |
| 4253 | /* The request was already skipped, let's restore it */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4254 | b_rew(chn->buf, old_o); |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 4255 | txn->req.next += old_o; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 4256 | txn->req.sov += old_o; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4257 | } |
| 4258 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4259 | old_i = chn->buf->i; |
| 4260 | while (http_find_header2(hdr_name, hdr_name_len, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) { |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4261 | /* remove any existing values from the header */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 4262 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4263 | } |
| 4264 | |
| 4265 | /* Add the new header requested with the server value */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4266 | hdr_val = trash.str; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4267 | memcpy(hdr_val, hdr_name, hdr_name_len); |
| 4268 | hdr_val += hdr_name_len; |
| 4269 | *hdr_val++ = ':'; |
| 4270 | *hdr_val++ = ' '; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4271 | hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val); |
| 4272 | http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str); |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4273 | |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4274 | if (old_o) { |
| 4275 | /* If this was a forwarded request, we must readjust the amount of |
| 4276 | * data to be forwarded in order to take into account the size |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 4277 | * variations. Note that the current state is >= HTTP_MSG_BODY, |
| 4278 | * so we don't have to adjust ->sol. |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4279 | */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 4280 | old_o += chn->buf->i - old_i; |
| 4281 | b_adv(chn->buf, old_o); |
| 4282 | txn->req.next -= old_o; |
| 4283 | txn->req.sov -= old_o; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4284 | } |
| 4285 | |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4286 | return 0; |
| 4287 | } |
| 4288 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4289 | /* Terminate current transaction and prepare a new one. This is very tricky |
| 4290 | * right now but it works. |
| 4291 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4292 | void http_end_txn_clean_session(struct stream *s) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4293 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4294 | int prev_status = s->txn->status; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 4295 | struct proxy *fe = strm_fe(s); |
Willy Tarreau | 858b103 | 2015-12-07 17:04:59 +0100 | [diff] [blame] | 4296 | struct proxy *be = s->be; |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 4297 | struct conn_stream *cs; |
Willy Tarreau | 323a2d9 | 2015-08-04 19:00:17 +0200 | [diff] [blame] | 4298 | struct connection *srv_conn; |
| 4299 | struct server *srv; |
Willy Tarreau | 449d74a | 2015-08-05 17:16:33 +0200 | [diff] [blame] | 4300 | unsigned int prev_flags = s->txn->flags; |
Willy Tarreau | 068621e | 2013-12-23 15:11:25 +0100 | [diff] [blame] | 4301 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4302 | /* FIXME: We need a more portable way of releasing a backend's and a |
| 4303 | * server's connections. We need a safer way to reinitialize buffer |
| 4304 | * flags. We also need a more accurate method for computing per-request |
| 4305 | * data. |
| 4306 | */ |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 4307 | /* |
| 4308 | * XXX cognet: This is probably wrong, this is killing a whole |
| 4309 | * connection, in the new world order, we probably want to just kill |
| 4310 | * the stream, this is to be revisited the day we handle multiple |
| 4311 | * streams in one server connection. |
| 4312 | */ |
| 4313 | cs = objt_cs(s->si[1].end); |
| 4314 | srv_conn = cs_conn(cs); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4315 | |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4316 | /* unless we're doing keep-alive, we want to quickly close the connection |
| 4317 | * to the server. |
| 4318 | */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4319 | if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) || |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4320 | !si_conn_ready(&s->si[1])) { |
| 4321 | s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF; |
| 4322 | si_shutr(&s->si[1]); |
| 4323 | si_shutw(&s->si[1]); |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4324 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4325 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4326 | if (s->flags & SF_BE_ASSIGNED) { |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 4327 | HA_ATOMIC_SUB(&be->beconn, 1); |
Willy Tarreau | 2d5cd47 | 2012-03-01 23:34:37 +0100 | [diff] [blame] | 4328 | if (unlikely(s->srv_conn)) |
| 4329 | sess_change_server(s, NULL); |
| 4330 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4331 | |
| 4332 | s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4333 | stream_process_counters(s); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4334 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4335 | if (s->txn->status) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4336 | int n; |
| 4337 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4338 | n = s->txn->status / 100; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4339 | if (n < 1 || n > 5) |
| 4340 | n = 0; |
| 4341 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4342 | if (fe->mode == PR_MODE_HTTP) { |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 4343 | HA_ATOMIC_ADD(&fe->fe_counters.p.http.rsp[n], 1); |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 4344 | } |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4345 | if ((s->flags & SF_BE_ASSIGNED) && |
Willy Tarreau | 858b103 | 2015-12-07 17:04:59 +0100 | [diff] [blame] | 4346 | (be->mode == PR_MODE_HTTP)) { |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 4347 | HA_ATOMIC_ADD(&be->be_counters.p.http.rsp[n], 1); |
| 4348 | HA_ATOMIC_ADD(&be->be_counters.p.http.cum_req, 1); |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 4349 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4350 | } |
| 4351 | |
| 4352 | /* don't count other requests' data */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4353 | s->logs.bytes_in -= s->req.buf->i; |
| 4354 | s->logs.bytes_out -= s->res.buf->i; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4355 | |
| 4356 | /* let's do a final log if we need it */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4357 | if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait && |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4358 | !(s->flags & SF_MONITOR) && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4359 | (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4360 | s->do_log(s); |
| 4361 | } |
| 4362 | |
Willy Tarreau | d713bcc | 2014-06-25 15:36:04 +0200 | [diff] [blame] | 4363 | /* stop tracking content-based counters */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4364 | stream_stop_content_counters(s); |
| 4365 | stream_update_time_stats(s); |
Willy Tarreau | 4bfc580 | 2014-06-17 12:19:18 +0200 | [diff] [blame] | 4366 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4367 | s->logs.accept_date = date; /* user-visible date for logging */ |
| 4368 | s->logs.tv_accept = now; /* corrected date for internal use */ |
Thierry FOURNIER / OZON.IO | 4cac359 | 2016-07-28 17:19:45 +0200 | [diff] [blame] | 4369 | s->logs.t_handshake = 0; /* There are no handshake in keep alive connection. */ |
| 4370 | s->logs.t_idle = -1; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4371 | tv_zero(&s->logs.tv_request); |
| 4372 | s->logs.t_queue = -1; |
| 4373 | s->logs.t_connect = -1; |
| 4374 | s->logs.t_data = -1; |
| 4375 | s->logs.t_close = 0; |
| 4376 | s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */ |
| 4377 | s->logs.srv_queue_size = 0; /* we will get this number soon */ |
| 4378 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4379 | s->logs.bytes_in = s->req.total = s->req.buf->i; |
| 4380 | s->logs.bytes_out = s->res.total = s->res.buf->i; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4381 | |
| 4382 | if (s->pend_pos) |
| 4383 | pendconn_free(s->pend_pos); |
| 4384 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4385 | if (objt_server(s->target)) { |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4386 | if (s->flags & SF_CURR_SESS) { |
| 4387 | s->flags &= ~SF_CURR_SESS; |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 4388 | HA_ATOMIC_SUB(&objt_server(s->target)->cur_sess, 1); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4389 | } |
Willy Tarreau | 858b103 | 2015-12-07 17:04:59 +0100 | [diff] [blame] | 4390 | if (may_dequeue_tasks(objt_server(s->target), be)) |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4391 | process_srv_queue(objt_server(s->target)); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4392 | } |
| 4393 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4394 | s->target = NULL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4395 | |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4396 | /* only release our endpoint if we don't intend to reuse the |
| 4397 | * connection. |
| 4398 | */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4399 | if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) || |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4400 | !si_conn_ready(&s->si[1])) { |
| 4401 | si_release_endpoint(&s->si[1]); |
Willy Tarreau | 323a2d9 | 2015-08-04 19:00:17 +0200 | [diff] [blame] | 4402 | srv_conn = NULL; |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4403 | } |
| 4404 | |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4405 | s->si[1].state = s->si[1].prev_state = SI_ST_INI; |
| 4406 | s->si[1].err_type = SI_ET_NONE; |
| 4407 | s->si[1].conn_retries = 0; /* used for logging too */ |
| 4408 | s->si[1].exp = TICK_ETERNITY; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4409 | 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] | 4410 | 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] | 4411 | 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] | 4412 | s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST); |
| 4413 | s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED); |
| 4414 | s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP); |
Willy Tarreau | 543db62 | 2012-11-15 16:41:22 +0100 | [diff] [blame] | 4415 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4416 | s->txn->meth = 0; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4417 | http_reset_txn(s); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4418 | s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ; |
Willy Tarreau | 068621e | 2013-12-23 15:11:25 +0100 | [diff] [blame] | 4419 | |
| 4420 | if (prev_status == 401 || prev_status == 407) { |
| 4421 | /* In HTTP keep-alive mode, if we receive a 401, we still have |
| 4422 | * a chance of being able to send the visitor again to the same |
| 4423 | * server over the same connection. This is required by some |
| 4424 | * broken protocols such as NTLM, and anyway whenever there is |
| 4425 | * an opportunity for sending the challenge to the proper place, |
| 4426 | * it's better to do it (at least it helps with debugging). |
| 4427 | */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4428 | s->txn->flags |= TX_PREFER_LAST; |
Willy Tarreau | bd99d58 | 2015-09-02 10:40:43 +0200 | [diff] [blame] | 4429 | if (srv_conn) |
| 4430 | srv_conn->flags |= CO_FL_PRIVATE; |
Willy Tarreau | 068621e | 2013-12-23 15:11:25 +0100 | [diff] [blame] | 4431 | } |
| 4432 | |
Willy Tarreau | 53f9685 | 2016-02-02 18:50:47 +0100 | [diff] [blame] | 4433 | /* Never ever allow to reuse a connection from a non-reuse backend */ |
| 4434 | if (srv_conn && (be->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR) |
| 4435 | srv_conn->flags |= CO_FL_PRIVATE; |
| 4436 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4437 | if (fe->options2 & PR_O2_INDEPSTR) |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4438 | s->si[1].flags |= SI_FL_INDEP_STR; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4439 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4440 | if (fe->options2 & PR_O2_NODELAY) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4441 | s->req.flags |= CF_NEVER_WAIT; |
| 4442 | s->res.flags |= CF_NEVER_WAIT; |
Willy Tarreau | 96e3121 | 2011-05-30 18:10:30 +0200 | [diff] [blame] | 4443 | } |
| 4444 | |
Willy Tarreau | 714ea78 | 2015-11-25 20:11:11 +0100 | [diff] [blame] | 4445 | /* we're removing the analysers, we MUST re-enable events detection. |
| 4446 | * We don't enable close on the response channel since it's either |
| 4447 | * already closed, or in keep-alive with an idle connection handler. |
| 4448 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4449 | channel_auto_read(&s->req); |
| 4450 | channel_auto_close(&s->req); |
| 4451 | channel_auto_read(&s->res); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4452 | |
Willy Tarreau | 1c59bd5 | 2015-11-02 20:20:11 +0100 | [diff] [blame] | 4453 | /* we're in keep-alive with an idle connection, monitor it if not already done */ |
| 4454 | if (srv_conn && LIST_ISEMPTY(&srv_conn->list)) { |
Willy Tarreau | 323a2d9 | 2015-08-04 19:00:17 +0200 | [diff] [blame] | 4455 | srv = objt_server(srv_conn->target); |
Willy Tarreau | 8dff998 | 2015-08-04 20:45:52 +0200 | [diff] [blame] | 4456 | if (!srv) |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 4457 | si_idle_cs(&s->si[1], NULL); |
Willy Tarreau | 53f9685 | 2016-02-02 18:50:47 +0100 | [diff] [blame] | 4458 | else if (srv_conn->flags & CO_FL_PRIVATE) |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 4459 | 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] | 4460 | else if (prev_flags & TX_NOT_FIRST) |
| 4461 | /* note: we check the request, not the connection, but |
| 4462 | * this is valid for strategies SAFE and AGGR, and in |
| 4463 | * case of ALWS, we don't care anyway. |
| 4464 | */ |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 4465 | 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] | 4466 | else |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 4467 | 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] | 4468 | } |
Christopher Faulet | e600624 | 2017-03-10 11:52:44 +0100 | [diff] [blame] | 4469 | s->req.analysers = strm_li(s) ? strm_li(s)->analysers : 0; |
| 4470 | s->res.analysers = 0; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4471 | } |
| 4472 | |
| 4473 | |
| 4474 | /* This function updates the request state machine according to the response |
| 4475 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 4476 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 4477 | * it is only used to find when a request/response couple is complete. Both |
| 4478 | * this function and its equivalent should loop until both return zero. It |
| 4479 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 4480 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4481 | int http_sync_req_state(struct stream *s) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4482 | { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4483 | struct channel *chn = &s->req; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4484 | struct http_txn *txn = s->txn; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4485 | unsigned int old_flags = chn->flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4486 | unsigned int old_state = txn->req.msg_state; |
| 4487 | |
Christopher Faulet | 4be9803 | 2017-07-18 10:48:24 +0200 | [diff] [blame] | 4488 | if (unlikely(txn->req.msg_state < HTTP_MSG_DONE)) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4489 | return 0; |
| 4490 | |
| 4491 | if (txn->req.msg_state == HTTP_MSG_DONE) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4492 | /* No need to read anymore, the request was completely parsed. |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 4493 | * We can shut the read side unless we want to abort_on_close, |
| 4494 | * or we have a POST request. The issue with POST requests is |
| 4495 | * that some browsers still send a CRLF after the request, and |
| 4496 | * this CRLF must be read so that it does not remain in the kernel |
| 4497 | * buffers, otherwise a close could cause an RST on some systems |
| 4498 | * (eg: Linux). |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 4499 | * Note that if we're using keep-alive on the client side, we'd |
| 4500 | * rather poll now and keep the polling enabled for the whole |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4501 | * stream's life than enabling/disabling it between each |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 4502 | * response and next request. |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4503 | */ |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 4504 | if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) && |
| 4505 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) && |
Willy Tarreau | 7aa15b0 | 2017-12-20 16:56:50 +0100 | [diff] [blame] | 4506 | (!(s->be->options & PR_O_ABRT_CLOSE) || |
| 4507 | (s->si[0].flags & SI_FL_CLEAN_ABRT)) && |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 4508 | txn->meth != HTTP_METH_POST) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4509 | channel_dont_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4510 | |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 4511 | /* if the server closes the connection, we want to immediately react |
| 4512 | * and close the socket to save packets and syscalls. |
| 4513 | */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4514 | s->si[1].flags |= SI_FL_NOHALF; |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 4515 | |
Willy Tarreau | 7f876a1 | 2015-11-18 11:59:55 +0100 | [diff] [blame] | 4516 | /* In any case we've finished parsing the request so we must |
| 4517 | * disable Nagle when sending data because 1) we're not going |
| 4518 | * to shut this side, and 2) the server is waiting for us to |
| 4519 | * send pending data. |
| 4520 | */ |
| 4521 | chn->flags |= CF_NEVER_WAIT; |
| 4522 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4523 | if (txn->rsp.msg_state == HTTP_MSG_ERROR) |
| 4524 | goto wait_other_side; |
| 4525 | |
| 4526 | if (txn->rsp.msg_state < HTTP_MSG_DONE) { |
| 4527 | /* The server has not finished to respond, so we |
| 4528 | * don't want to move in order not to upset it. |
| 4529 | */ |
| 4530 | goto wait_other_side; |
| 4531 | } |
| 4532 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4533 | /* When we get here, it means that both the request and the |
| 4534 | * response have finished receiving. Depending on the connection |
| 4535 | * mode, we'll have to wait for the last bytes to leave in either |
| 4536 | * direction, and sometimes for a close to be effective. |
| 4537 | */ |
| 4538 | |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4539 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 4540 | /* Server-close mode : queue a connection close to the server */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4541 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) |
| 4542 | channel_shutw_now(chn); |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4543 | } |
| 4544 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 4545 | /* Option forceclose is set, or either side wants to close, |
| 4546 | * let's enforce it now that we're not expecting any new |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4547 | * data to come. The caller knows the stream is complete |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4548 | * once both states are CLOSED. |
Christopher Faulet | 1486b0a | 2017-07-18 11:42:08 +0200 | [diff] [blame] | 4549 | * |
| 4550 | * However, there is an exception if the response |
| 4551 | * length is undefined. In this case, we need to wait |
| 4552 | * the close from the server. The response will be |
| 4553 | * switched in TUNNEL mode until the end. |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4554 | */ |
Christopher Faulet | 1486b0a | 2017-07-18 11:42:08 +0200 | [diff] [blame] | 4555 | if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) && |
| 4556 | txn->rsp.msg_state != HTTP_MSG_CLOSED) |
| 4557 | goto check_channel_flags; |
| 4558 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4559 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) { |
| 4560 | channel_shutr_now(chn); |
| 4561 | channel_shutw_now(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4562 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4563 | } |
| 4564 | else { |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4565 | /* The last possible modes are keep-alive and tunnel. Tunnel mode |
| 4566 | * will not have any analyser so it needs to poll for reads. |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4567 | */ |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4568 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) { |
| 4569 | channel_auto_read(chn); |
| 4570 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4571 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4572 | } |
| 4573 | |
Christopher Faulet | 4be9803 | 2017-07-18 10:48:24 +0200 | [diff] [blame] | 4574 | goto check_channel_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4575 | } |
| 4576 | |
| 4577 | if (txn->req.msg_state == HTTP_MSG_CLOSING) { |
| 4578 | http_msg_closing: |
| 4579 | /* nothing else to forward, just waiting for the output buffer |
| 4580 | * to be empty and for the shutw_now to take effect. |
| 4581 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4582 | if (channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4583 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 4584 | goto http_msg_closed; |
| 4585 | } |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4586 | else if (chn->flags & CF_SHUTW) { |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 4587 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4588 | txn->req.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4589 | } |
Christopher Faulet | 56d2609 | 2017-07-20 11:05:10 +0200 | [diff] [blame] | 4590 | goto wait_other_side; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4591 | } |
| 4592 | |
| 4593 | if (txn->req.msg_state == HTTP_MSG_CLOSED) { |
| 4594 | http_msg_closed: |
Willy Tarreau | 8059351 | 2017-12-14 10:43:31 +0100 | [diff] [blame] | 4595 | /* if we don't know whether the server will close, we need to hard close */ |
| 4596 | if (txn->rsp.flags & HTTP_MSGF_XFER_LEN) |
| 4597 | s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */ |
| 4598 | |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 4599 | /* see above in MSG_DONE why we only do this in these states */ |
| 4600 | if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) && |
| 4601 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) && |
Willy Tarreau | 7aa15b0 | 2017-12-20 16:56:50 +0100 | [diff] [blame] | 4602 | (!(s->be->options & PR_O_ABRT_CLOSE) || |
| 4603 | (s->si[0].flags & SI_FL_CLEAN_ABRT))) |
Willy Tarreau | 2e7a165 | 2013-12-15 15:32:10 +0100 | [diff] [blame] | 4604 | channel_dont_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4605 | goto wait_other_side; |
| 4606 | } |
| 4607 | |
Christopher Faulet | 4be9803 | 2017-07-18 10:48:24 +0200 | [diff] [blame] | 4608 | check_channel_flags: |
| 4609 | /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */ |
| 4610 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) { |
| 4611 | /* if we've just closed an output, let's switch */ |
Christopher Faulet | 4be9803 | 2017-07-18 10:48:24 +0200 | [diff] [blame] | 4612 | txn->req.msg_state = HTTP_MSG_CLOSING; |
| 4613 | goto http_msg_closing; |
| 4614 | } |
| 4615 | |
| 4616 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4617 | wait_other_side: |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4618 | return txn->req.msg_state != old_state || chn->flags != old_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4619 | } |
| 4620 | |
| 4621 | |
| 4622 | /* This function updates the response state machine according to the request |
| 4623 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 4624 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 4625 | * it is only used to find when a request/response couple is complete. Both |
| 4626 | * this function and its equivalent should loop until both return zero. It |
| 4627 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 4628 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4629 | int http_sync_res_state(struct stream *s) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4630 | { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4631 | struct channel *chn = &s->res; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4632 | struct http_txn *txn = s->txn; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4633 | unsigned int old_flags = chn->flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4634 | unsigned int old_state = txn->rsp.msg_state; |
| 4635 | |
Christopher Faulet | 4be9803 | 2017-07-18 10:48:24 +0200 | [diff] [blame] | 4636 | if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE)) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4637 | return 0; |
| 4638 | |
| 4639 | if (txn->rsp.msg_state == HTTP_MSG_DONE) { |
| 4640 | /* In theory, we don't need to read anymore, but we must |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4641 | * still monitor the server connection for a possible close |
| 4642 | * while the request is being uploaded, so we don't disable |
| 4643 | * reading. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4644 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4645 | /* channel_dont_read(chn); */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4646 | |
| 4647 | if (txn->req.msg_state == HTTP_MSG_ERROR) |
| 4648 | goto wait_other_side; |
| 4649 | |
| 4650 | if (txn->req.msg_state < HTTP_MSG_DONE) { |
| 4651 | /* The client seems to still be sending data, probably |
| 4652 | * because we got an error response during an upload. |
| 4653 | * We have the choice of either breaking the connection |
| 4654 | * or letting it pass through. Let's do the later. |
| 4655 | */ |
| 4656 | goto wait_other_side; |
| 4657 | } |
| 4658 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4659 | /* When we get here, it means that both the request and the |
| 4660 | * response have finished receiving. Depending on the connection |
| 4661 | * mode, we'll have to wait for the last bytes to leave in either |
| 4662 | * direction, and sometimes for a close to be effective. |
| 4663 | */ |
| 4664 | |
| 4665 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 4666 | /* Server-close mode : shut read and wait for the request |
| 4667 | * side to close its output buffer. The caller will detect |
| 4668 | * when we're in DONE and the other is in CLOSED and will |
| 4669 | * catch that for the final cleanup. |
| 4670 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4671 | if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW))) |
| 4672 | channel_shutr_now(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4673 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4674 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 4675 | /* Option forceclose is set, or either side wants to close, |
| 4676 | * let's enforce it now that we're not expecting any new |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4677 | * data to come. The caller knows the stream is complete |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4678 | * once both states are CLOSED. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4679 | */ |
Christopher Faulet | fd04fcf | 2018-02-02 15:54:15 +0100 | [diff] [blame] | 4680 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4681 | channel_shutr_now(chn); |
| 4682 | channel_shutw_now(chn); |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4683 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4684 | } |
| 4685 | else { |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4686 | /* The last possible modes are keep-alive and tunnel. Tunnel will |
| 4687 | * need to forward remaining data. Keep-alive will need to monitor |
| 4688 | * for connection closing. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4689 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4690 | channel_auto_read(chn); |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 4691 | chn->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4692 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) |
| 4693 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4694 | } |
| 4695 | |
Christopher Faulet | 4be9803 | 2017-07-18 10:48:24 +0200 | [diff] [blame] | 4696 | goto check_channel_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4697 | } |
| 4698 | |
| 4699 | if (txn->rsp.msg_state == HTTP_MSG_CLOSING) { |
| 4700 | http_msg_closing: |
| 4701 | /* nothing else to forward, just waiting for the output buffer |
| 4702 | * to be empty and for the shutw_now to take effect. |
| 4703 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4704 | if (channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4705 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 4706 | goto http_msg_closed; |
| 4707 | } |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4708 | else if (chn->flags & CF_SHUTW) { |
Christopher Faulet | a3992e0 | 2017-07-18 10:35:55 +0200 | [diff] [blame] | 4709 | txn->rsp.err_state = txn->rsp.msg_state; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4710 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 4711 | HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4712 | if (objt_server(s->target)) |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 4713 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4714 | } |
Christopher Faulet | 56d2609 | 2017-07-20 11:05:10 +0200 | [diff] [blame] | 4715 | goto wait_other_side; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4716 | } |
| 4717 | |
| 4718 | if (txn->rsp.msg_state == HTTP_MSG_CLOSED) { |
| 4719 | http_msg_closed: |
| 4720 | /* drop any pending data */ |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 4721 | channel_truncate(chn); |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4722 | channel_auto_close(chn); |
| 4723 | channel_auto_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4724 | goto wait_other_side; |
| 4725 | } |
| 4726 | |
Christopher Faulet | 4be9803 | 2017-07-18 10:48:24 +0200 | [diff] [blame] | 4727 | check_channel_flags: |
| 4728 | /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */ |
| 4729 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) { |
| 4730 | /* if we've just closed an output, let's switch */ |
| 4731 | txn->rsp.msg_state = HTTP_MSG_CLOSING; |
| 4732 | goto http_msg_closing; |
| 4733 | } |
| 4734 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4735 | wait_other_side: |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 4736 | /* We force the response to leave immediately if we're waiting for the |
| 4737 | * other side, since there is no pending shutdown to push it out. |
| 4738 | */ |
| 4739 | if (!channel_is_empty(chn)) |
| 4740 | chn->flags |= CF_SEND_DONTWAIT; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4741 | return txn->rsp.msg_state != old_state || chn->flags != old_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4742 | } |
| 4743 | |
| 4744 | |
Christopher Faulet | 894da4c | 2017-07-18 11:29:07 +0200 | [diff] [blame] | 4745 | /* Resync the request and response state machines. */ |
| 4746 | void http_resync_states(struct stream *s) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4747 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4748 | struct http_txn *txn = s->txn; |
Christopher Faulet | 894da4c | 2017-07-18 11:29:07 +0200 | [diff] [blame] | 4749 | #ifdef DEBUG_FULL |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4750 | int old_req_state = txn->req.msg_state; |
| 4751 | int old_res_state = txn->rsp.msg_state; |
Christopher Faulet | 894da4c | 2017-07-18 11:29:07 +0200 | [diff] [blame] | 4752 | #endif |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4753 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4754 | http_sync_req_state(s); |
| 4755 | while (1) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4756 | if (!http_sync_res_state(s)) |
| 4757 | break; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4758 | if (!http_sync_req_state(s)) |
| 4759 | break; |
| 4760 | } |
Willy Tarreau | 3ce10ff | 2014-04-22 08:24:38 +0200 | [diff] [blame] | 4761 | |
Christopher Faulet | 894da4c | 2017-07-18 11:29:07 +0200 | [diff] [blame] | 4762 | DPRINTF(stderr,"[%u] %s: stream=%p old=%s,%s cur=%s,%s " |
| 4763 | "req->analysers=0x%08x res->analysers=0x%08x\n", |
| 4764 | now_ms, __FUNCTION__, s, |
Willy Tarreau | 0da5b3b | 2017-09-21 09:30:46 +0200 | [diff] [blame] | 4765 | h1_msg_state_str(old_req_state), h1_msg_state_str(old_res_state), |
| 4766 | 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] | 4767 | s->req.analysers, s->res.analysers); |
Christopher Faulet | 814d270 | 2017-03-30 11:33:44 +0200 | [diff] [blame] | 4768 | |
| 4769 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4770 | /* OK, both state machines agree on a compatible state. |
| 4771 | * There are a few cases we're interested in : |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4772 | * - HTTP_MSG_CLOSED on both sides means we've reached the end in both |
| 4773 | * directions, so let's simply disable both analysers. |
Christopher Faulet | f77bb53 | 2017-07-18 11:18:46 +0200 | [diff] [blame] | 4774 | * - HTTP_MSG_CLOSED on the response only or HTTP_MSG_ERROR on either |
| 4775 | * means we must abort the request. |
| 4776 | * - HTTP_MSG_TUNNEL on either means we have to disable analyser on |
| 4777 | * corresponding channel. |
| 4778 | * - HTTP_MSG_DONE or HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE |
| 4779 | * on the response with server-close mode means we've completed one |
| 4780 | * request and we must re-initialize the server connection. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4781 | */ |
Christopher Faulet | f77bb53 | 2017-07-18 11:18:46 +0200 | [diff] [blame] | 4782 | if (txn->req.msg_state == HTTP_MSG_CLOSED && |
| 4783 | txn->rsp.msg_state == HTTP_MSG_CLOSED) { |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4784 | s->req.analysers &= AN_REQ_FLT_END; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4785 | channel_auto_close(&s->req); |
| 4786 | channel_auto_read(&s->req); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4787 | s->res.analysers &= AN_RES_FLT_END; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4788 | channel_auto_close(&s->res); |
| 4789 | channel_auto_read(&s->res); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4790 | } |
Christopher Faulet | f77bb53 | 2017-07-18 11:18:46 +0200 | [diff] [blame] | 4791 | else if (txn->rsp.msg_state == HTTP_MSG_CLOSED || |
| 4792 | txn->rsp.msg_state == HTTP_MSG_ERROR || |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 4793 | txn->req.msg_state == HTTP_MSG_ERROR) { |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4794 | s->res.analysers &= AN_RES_FLT_END; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4795 | channel_auto_close(&s->res); |
| 4796 | channel_auto_read(&s->res); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4797 | s->req.analysers &= AN_REQ_FLT_END; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4798 | channel_abort(&s->req); |
| 4799 | channel_auto_close(&s->req); |
| 4800 | channel_auto_read(&s->req); |
| 4801 | channel_truncate(&s->req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4802 | } |
Christopher Faulet | f77bb53 | 2017-07-18 11:18:46 +0200 | [diff] [blame] | 4803 | else if (txn->req.msg_state == HTTP_MSG_TUNNEL || |
| 4804 | txn->rsp.msg_state == HTTP_MSG_TUNNEL) { |
| 4805 | if (txn->req.msg_state == HTTP_MSG_TUNNEL) { |
| 4806 | s->req.analysers &= AN_REQ_FLT_END; |
| 4807 | if (HAS_REQ_DATA_FILTERS(s)) |
| 4808 | s->req.analysers |= AN_REQ_FLT_XFER_DATA; |
| 4809 | } |
| 4810 | if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) { |
| 4811 | s->res.analysers &= AN_RES_FLT_END; |
| 4812 | if (HAS_RSP_DATA_FILTERS(s)) |
| 4813 | s->res.analysers |= AN_RES_FLT_XFER_DATA; |
| 4814 | } |
| 4815 | channel_auto_close(&s->req); |
| 4816 | channel_auto_read(&s->req); |
| 4817 | channel_auto_close(&s->res); |
| 4818 | channel_auto_read(&s->res); |
| 4819 | } |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4820 | else if ((txn->req.msg_state == HTTP_MSG_DONE || |
| 4821 | txn->req.msg_state == HTTP_MSG_CLOSED) && |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4822 | txn->rsp.msg_state == HTTP_MSG_DONE && |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4823 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL || |
| 4824 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) { |
| 4825 | /* server-close/keep-alive: terminate this transaction, |
| 4826 | * possibly killing the server connection and reinitialize |
Willy Tarreau | 3de5bd6 | 2016-05-02 16:07:07 +0200 | [diff] [blame] | 4827 | * a fresh-new transaction, but only once we're sure there's |
| 4828 | * enough room in the request and response buffer to process |
Christopher Faulet | c0c672a | 2017-03-28 11:51:33 +0200 | [diff] [blame] | 4829 | * another request. They must not hold any pending output data |
| 4830 | * and the response buffer must realigned |
| 4831 | * (realign is done is http_end_txn_clean_session). |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4832 | */ |
Christopher Faulet | 894da4c | 2017-07-18 11:29:07 +0200 | [diff] [blame] | 4833 | if (s->req.buf->o) |
Willy Tarreau | 3de5bd6 | 2016-05-02 16:07:07 +0200 | [diff] [blame] | 4834 | s->req.flags |= CF_WAKE_WRITE; |
Christopher Faulet | 894da4c | 2017-07-18 11:29:07 +0200 | [diff] [blame] | 4835 | else if (s->res.buf->o) |
Willy Tarreau | 3de5bd6 | 2016-05-02 16:07:07 +0200 | [diff] [blame] | 4836 | s->res.flags |= CF_WAKE_WRITE; |
Christopher Faulet | a81ff60 | 2017-07-18 22:01:05 +0200 | [diff] [blame] | 4837 | else { |
| 4838 | s->req.analysers = AN_REQ_FLT_END; |
| 4839 | s->res.analysers = AN_RES_FLT_END; |
| 4840 | txn->flags |= TX_WAIT_CLEANUP; |
| 4841 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4842 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4843 | } |
| 4844 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4845 | /* This function is an analyser which forwards request body (including chunk |
| 4846 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 4847 | * zero byte. The only situation where it must not be called is when we're in |
| 4848 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 4849 | * remaining data and to resync after end of body. It expects the msg_state to |
| 4850 | * 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] | 4851 | * 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] | 4852 | * 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] | 4853 | * bytes of pending data + the headers if not already done. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4854 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4855 | 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] | 4856 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4857 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4858 | struct http_txn *txn = s->txn; |
| 4859 | struct http_msg *msg = &s->txn->req; |
Christopher Faulet | 3e34429 | 2015-11-24 16:24:13 +0100 | [diff] [blame] | 4860 | int ret; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4861 | |
Christopher Faulet | 814d270 | 2017-03-30 11:33:44 +0200 | [diff] [blame] | 4862 | DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n", |
| 4863 | now_ms, __FUNCTION__, |
| 4864 | s, |
| 4865 | req, |
| 4866 | req->rex, req->wex, |
| 4867 | req->flags, |
| 4868 | req->buf->i, |
| 4869 | req->analysers); |
| 4870 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4871 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4872 | return 0; |
| 4873 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4874 | if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) || |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4875 | ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) { |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 4876 | /* Output closed while we were sending data. We must abort and |
| 4877 | * wake the other side up. |
| 4878 | */ |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 4879 | msg->err_state = msg->msg_state; |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 4880 | msg->msg_state = HTTP_MSG_ERROR; |
| 4881 | http_resync_states(s); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 4882 | return 1; |
| 4883 | } |
| 4884 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4885 | /* Note that we don't have to send 100-continue back because we don't |
| 4886 | * need the data to complete our job, and it's up to the server to |
| 4887 | * decide whether to return 100, 417 or anything else in return of |
| 4888 | * an "Expect: 100-continue" header. |
| 4889 | */ |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 4890 | if (msg->msg_state == HTTP_MSG_BODY) { |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4891 | msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK) |
| 4892 | ? HTTP_MSG_CHUNK_SIZE |
| 4893 | : HTTP_MSG_DATA); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 4894 | |
| 4895 | /* TODO/filters: when http-buffer-request option is set or if a |
| 4896 | * rule on url_param exists, the first chunk size could be |
| 4897 | * already parsed. In that case, msg->next is after the chunk |
| 4898 | * size (including the CRLF after the size). So this case should |
| 4899 | * be handled to */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4900 | } |
| 4901 | |
Willy Tarreau | 7ba2354 | 2014-04-17 21:50:00 +0200 | [diff] [blame] | 4902 | /* Some post-connect processing might want us to refrain from starting to |
| 4903 | * forward data. Currently, the only reason for this is "balance url_param" |
| 4904 | * whichs need to parse/process the request after we've enabled forwarding. |
| 4905 | */ |
| 4906 | if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4907 | if (!(s->res.flags & CF_READ_ATTACHED)) { |
Willy Tarreau | 7ba2354 | 2014-04-17 21:50:00 +0200 | [diff] [blame] | 4908 | channel_auto_connect(req); |
Willy Tarreau | 644c101 | 2014-04-30 18:11:11 +0200 | [diff] [blame] | 4909 | req->flags |= CF_WAKE_CONNECT; |
Willy Tarreau | ba20dfc | 2018-05-16 11:35:05 +0200 | [diff] [blame] | 4910 | channel_dont_close(req); /* don't fail on early shutr */ |
| 4911 | goto waiting; |
Willy Tarreau | 7ba2354 | 2014-04-17 21:50:00 +0200 | [diff] [blame] | 4912 | } |
| 4913 | msg->flags &= ~HTTP_MSGF_WAIT_CONN; |
| 4914 | } |
| 4915 | |
Willy Tarreau | 80a92c0 | 2014-03-12 10:41:13 +0100 | [diff] [blame] | 4916 | /* in most states, we should abort in case of early close */ |
| 4917 | channel_auto_close(req); |
| 4918 | |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 4919 | if (req->to_forward) { |
| 4920 | /* We can't process the buffer's contents yet */ |
| 4921 | req->flags |= CF_WAKE_WRITE; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4922 | goto missing_data_or_waiting; |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 4923 | } |
| 4924 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4925 | if (msg->msg_state < HTTP_MSG_DONE) { |
| 4926 | ret = ((msg->flags & HTTP_MSGF_TE_CHNK) |
| 4927 | ? http_msg_forward_chunked_body(s, msg) |
| 4928 | : http_msg_forward_body(s, msg)); |
| 4929 | if (!ret) |
| 4930 | goto missing_data_or_waiting; |
| 4931 | if (ret < 0) |
| 4932 | goto return_bad_req; |
| 4933 | } |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 4934 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4935 | /* other states, DONE...TUNNEL */ |
| 4936 | /* we don't want to forward closes on DONE except in tunnel mode. */ |
| 4937 | if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) |
| 4938 | channel_dont_close(req); |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 4939 | |
Christopher Faulet | 894da4c | 2017-07-18 11:29:07 +0200 | [diff] [blame] | 4940 | http_resync_states(s); |
| 4941 | if (!(req->analysers & an_bit)) { |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4942 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 4943 | if (req->flags & CF_SHUTW) { |
| 4944 | /* request errors are most likely due to the |
| 4945 | * server aborting the transfer. */ |
| 4946 | goto aborted_xfer; |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 4947 | } |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4948 | if (msg->err_pos >= 0) |
Emeric Brun | 8c1aaa2 | 2017-06-15 11:30:06 +0200 | [diff] [blame] | 4949 | http_capture_bad_message(sess->fe, &sess->fe->invalid_req, s, msg, msg->err_state, s->be); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4950 | goto return_bad_req; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4951 | } |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4952 | return 1; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4953 | } |
| 4954 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4955 | /* If "option abortonclose" is set on the backend, we want to monitor |
| 4956 | * the client's connection and forward any shutdown notification to the |
| 4957 | * server, which will decide whether to close or to go on processing the |
| 4958 | * request. We only do that in tunnel mode, and not in other modes since |
| 4959 | * it can be abused to exhaust source ports. */ |
Willy Tarreau | 7aa15b0 | 2017-12-20 16:56:50 +0100 | [diff] [blame] | 4960 | 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] | 4961 | channel_auto_read(req); |
| 4962 | if ((req->flags & (CF_SHUTR|CF_READ_NULL)) && |
| 4963 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)) |
| 4964 | s->si[1].flags |= SI_FL_NOLINGER; |
| 4965 | channel_auto_close(req); |
| 4966 | } |
| 4967 | else if (s->txn->meth == HTTP_METH_POST) { |
| 4968 | /* POST requests may require to read extra CRLF sent by broken |
| 4969 | * browsers and which could cause an RST to be sent upon close |
| 4970 | * on some systems (eg: Linux). */ |
| 4971 | channel_auto_read(req); |
| 4972 | } |
| 4973 | return 0; |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 4974 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4975 | missing_data_or_waiting: |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4976 | /* stop waiting for data if the input is closed before the end */ |
Christopher Faulet | a33510b | 2017-03-31 15:37:29 +0200 | [diff] [blame] | 4977 | if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR) { |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4978 | if (!(s->flags & SF_ERR_MASK)) |
| 4979 | s->flags |= SF_ERR_CLICL; |
| 4980 | if (!(s->flags & SF_FINST_MASK)) { |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4981 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4982 | s->flags |= SF_FINST_H; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4983 | else |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4984 | s->flags |= SF_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4985 | } |
| 4986 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 4987 | HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1); |
| 4988 | HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4989 | if (objt_server(s->target)) |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 4990 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4991 | |
| 4992 | goto return_bad_req_stats_ok; |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 4993 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4994 | |
Willy Tarreau | ba20dfc | 2018-05-16 11:35:05 +0200 | [diff] [blame] | 4995 | waiting: |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4996 | /* waiting for the last bits to leave the buffer */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4997 | if (req->flags & CF_SHUTW) |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4998 | goto aborted_xfer; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4999 | |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5000 | /* 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] | 5001 | * 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] | 5002 | * And when content-length is used, we never want to let the possible |
| 5003 | * shutdown be forwarded to the other side, as the state machine will |
| 5004 | * take care of it once the client responds. It's also important to |
| 5005 | * prevent TIME_WAITs from accumulating on the backend side, and for |
| 5006 | * HTTP/2 where the last frame comes with a shutdown. |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5007 | */ |
Willy Tarreau | ff47b3f | 2017-12-29 16:30:31 +0100 | [diff] [blame] | 5008 | if (msg->flags & (HTTP_MSGF_TE_CHNK|HTTP_MSGF_CNT_LEN)) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5009 | channel_dont_close(req); |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5010 | |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5011 | /* 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] | 5012 | * 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] | 5013 | * 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] | 5014 | * modes are already handled by the stream sock layer. We must not do |
| 5015 | * this in content-length mode because it could present the MSG_MORE |
| 5016 | * flag with the last block of forwarded data, which would cause an |
| 5017 | * additional delay to be observed by the receiver. |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5018 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5019 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5020 | req->flags |= CF_EXPECT_MORE; |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5021 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5022 | return 0; |
| 5023 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5024 | return_bad_req: /* let's centralize all bad requests */ |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 5025 | HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 5026 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 5027 | HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1); |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5028 | |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5029 | return_bad_req_stats_ok: |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 5030 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5031 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 5032 | if (txn->status) { |
| 5033 | /* 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] | 5034 | http_reply_and_close(s, txn->status, NULL); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5035 | } else { |
| 5036 | txn->status = 400; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 5037 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5038 | } |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 5039 | req->analysers &= AN_REQ_FLT_END; |
| 5040 | 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] | 5041 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5042 | if (!(s->flags & SF_ERR_MASK)) |
| 5043 | s->flags |= SF_ERR_PRXCOND; |
| 5044 | if (!(s->flags & SF_FINST_MASK)) { |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5045 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5046 | s->flags |= SF_FINST_H; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5047 | else |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5048 | s->flags |= SF_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5049 | } |
| 5050 | return 0; |
| 5051 | |
| 5052 | aborted_xfer: |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 5053 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5054 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 5055 | if (txn->status) { |
| 5056 | /* 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] | 5057 | http_reply_and_close(s, txn->status, NULL); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5058 | } else { |
| 5059 | txn->status = 502; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 5060 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5061 | } |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 5062 | req->analysers &= AN_REQ_FLT_END; |
| 5063 | 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] | 5064 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 5065 | HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1); |
| 5066 | HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5067 | if (objt_server(s->target)) |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 5068 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5069 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5070 | if (!(s->flags & SF_ERR_MASK)) |
| 5071 | s->flags |= SF_ERR_SRVCL; |
| 5072 | if (!(s->flags & SF_FINST_MASK)) { |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5073 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5074 | s->flags |= SF_FINST_H; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5075 | else |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5076 | s->flags |= SF_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5077 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5078 | return 0; |
| 5079 | } |
| 5080 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5081 | /* This stream analyser waits for a complete HTTP response. It returns 1 if the |
| 5082 | * processing can continue on next analysers, or zero if it either needs more |
| 5083 | * data or wants to immediately abort the response (eg: timeout, error, ...). It |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5084 | * 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] | 5085 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 5086 | * abort. |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 5087 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5088 | 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] | 5089 | { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5090 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5091 | struct http_txn *txn = s->txn; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5092 | struct http_msg *msg = &txn->rsp; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5093 | struct hdr_ctx ctx; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5094 | int use_close_only; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5095 | int cur_idx; |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 5096 | int n; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 5097 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5098 | DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n", |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 5099 | now_ms, __FUNCTION__, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5100 | s, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 5101 | rep, |
| 5102 | rep->rex, rep->wex, |
| 5103 | rep->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5104 | rep->buf->i, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 5105 | rep->analysers); |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 5106 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5107 | /* |
| 5108 | * Now parse the partial (or complete) lines. |
| 5109 | * We will check the response syntax, and also join multi-line |
| 5110 | * headers. An index of all the lines will be elaborated while |
| 5111 | * parsing. |
| 5112 | * |
| 5113 | * For the parsing, we use a 28 states FSM. |
| 5114 | * |
| 5115 | * Here is the information we currently have : |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5116 | * rep->buf->p = beginning of response |
| 5117 | * rep->buf->p + msg->eoh = end of processed headers / start of current one |
| 5118 | * rep->buf->p + rep->buf->i = end of input data |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 5119 | * msg->eol = end of current header or line (LF or CRLF) |
| 5120 | * msg->next = first non-visited byte |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5121 | */ |
| 5122 | |
Willy Tarreau | 628c40c | 2014-04-24 19:11:26 +0200 | [diff] [blame] | 5123 | next_one: |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 5124 | /* There's a protected area at the end of the buffer for rewriting |
| 5125 | * purposes. We don't want to start to parse the request if the |
| 5126 | * protected area is affected, because we may have to move processed |
| 5127 | * data later, which is much more complicated. |
| 5128 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5129 | if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) { |
Willy Tarreau | ba0902e | 2015-01-13 14:39:16 +0100 | [diff] [blame] | 5130 | if (unlikely(!channel_is_rewritable(rep))) { |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 5131 | /* some data has still not left the buffer, wake us once that's done */ |
| 5132 | if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) |
| 5133 | goto abort_response; |
| 5134 | channel_dont_close(rep); |
| 5135 | rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | d7ad9f5 | 2013-12-31 17:26:25 +0100 | [diff] [blame] | 5136 | rep->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 5137 | return 0; |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 5138 | } |
| 5139 | |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 5140 | if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) || |
| 5141 | bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite)) |
| 5142 | buffer_slow_realign(rep->buf); |
| 5143 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5144 | if (likely(msg->next < rep->buf->i)) |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 5145 | http_msg_analyzer(msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 5146 | } |
| 5147 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5148 | /* 1: we might have to print this header in debug mode */ |
| 5149 | if (unlikely((global.mode & MODE_DEBUG) && |
| 5150 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | 7d59e90 | 2014-10-21 19:36:09 +0200 | [diff] [blame] | 5151 | msg->msg_state >= HTTP_MSG_BODY)) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5152 | char *eol, *sol; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5153 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5154 | sol = rep->buf->p; |
| 5155 | eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5156 | debug_hdr("srvrep", s, sol, eol); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5157 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5158 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 5159 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5160 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5161 | while (cur_idx) { |
| 5162 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 5163 | debug_hdr("srvhdr", s, sol, eol); |
| 5164 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 5165 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
| 5166 | } |
| 5167 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5168 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5169 | /* |
| 5170 | * Now we quickly check if we have found a full valid response. |
| 5171 | * If not so, we check the FD and buffer states before leaving. |
| 5172 | * A full response is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 5173 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5174 | * responses are checked first. |
| 5175 | * |
| 5176 | * Depending on whether the client is still there or not, we |
| 5177 | * may send an error response back or not. Note that normally |
| 5178 | * we should only check for HTTP status there, and check I/O |
| 5179 | * errors somewhere else. |
| 5180 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5181 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 5182 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5183 | /* Invalid response */ |
| 5184 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 5185 | /* we detected a parsing error. We want to archive this response |
| 5186 | * in the dedicated proxy area for later troubleshooting. |
| 5187 | */ |
| 5188 | hdr_response_bad: |
| 5189 | if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0) |
Emeric Brun | 8c1aaa2 | 2017-06-15 11:30:06 +0200 | [diff] [blame] | 5190 | http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, msg->err_state, sess->fe); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5191 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 5192 | HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5193 | if (objt_server(s->target)) { |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 5194 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5195 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5196 | } |
Willy Tarreau | 6464841 | 2010-03-05 10:41:54 +0100 | [diff] [blame] | 5197 | abort_response: |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5198 | channel_auto_close(rep); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 5199 | rep->analysers &= AN_RES_FLT_END; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5200 | txn->status = 502; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5201 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5202 | channel_truncate(rep); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 5203 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5204 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5205 | if (!(s->flags & SF_ERR_MASK)) |
| 5206 | s->flags |= SF_ERR_PRXCOND; |
| 5207 | if (!(s->flags & SF_FINST_MASK)) |
| 5208 | s->flags |= SF_FINST_H; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5209 | |
| 5210 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5211 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5212 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5213 | /* too large response does not fit in buffer. */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5214 | else if (buffer_full(rep->buf, global.tune.maxrewrite)) { |
Willy Tarreau | fec4d89 | 2011-09-02 20:04:57 +0200 | [diff] [blame] | 5215 | if (msg->err_pos < 0) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5216 | msg->err_pos = rep->buf->i; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5217 | goto hdr_response_bad; |
| 5218 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5219 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5220 | /* read error */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5221 | else if (rep->flags & CF_READ_ERROR) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5222 | if (msg->err_pos >= 0) |
Emeric Brun | 8c1aaa2 | 2017-06-15 11:30:06 +0200 | [diff] [blame] | 5223 | http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, msg->err_state, sess->fe); |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 5224 | else if (txn->flags & TX_NOT_FIRST) |
| 5225 | goto abort_keep_alive; |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 5226 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 5227 | HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5228 | if (objt_server(s->target)) { |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 5229 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5230 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5231 | } |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 5232 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5233 | channel_auto_close(rep); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 5234 | rep->analysers &= AN_RES_FLT_END; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5235 | txn->status = 502; |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 5236 | |
| 5237 | /* Check to see if the server refused the early data. |
| 5238 | * If so, just send a 425 |
| 5239 | */ |
| 5240 | if (objt_cs(s->si[1].end)) { |
| 5241 | struct connection *conn = objt_cs(s->si[1].end)->conn; |
| 5242 | |
| 5243 | if (conn->err_code == CO_ER_SSL_EARLY_FAILED) |
| 5244 | txn->status = 425; |
| 5245 | } |
| 5246 | |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5247 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5248 | channel_truncate(rep); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 5249 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 5250 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5251 | if (!(s->flags & SF_ERR_MASK)) |
| 5252 | s->flags |= SF_ERR_SRVCL; |
| 5253 | if (!(s->flags & SF_FINST_MASK)) |
| 5254 | s->flags |= SF_FINST_H; |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 5255 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5256 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5257 | |
Willy Tarreau | 6f0a7ba | 2014-06-23 15:22:31 +0200 | [diff] [blame] | 5258 | /* read timeout : return a 504 to the client. */ |
| 5259 | else if (rep->flags & CF_READ_TIMEOUT) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5260 | if (msg->err_pos >= 0) |
Emeric Brun | 8c1aaa2 | 2017-06-15 11:30:06 +0200 | [diff] [blame] | 5261 | http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, msg->err_state, sess->fe); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5262 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 5263 | HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5264 | if (objt_server(s->target)) { |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 5265 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5266 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5267 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5268 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5269 | channel_auto_close(rep); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 5270 | rep->analysers &= AN_RES_FLT_END; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5271 | txn->status = 504; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5272 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5273 | channel_truncate(rep); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 5274 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 5275 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5276 | if (!(s->flags & SF_ERR_MASK)) |
| 5277 | s->flags |= SF_ERR_SRVTO; |
| 5278 | if (!(s->flags & SF_FINST_MASK)) |
| 5279 | s->flags |= SF_FINST_H; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5280 | return 0; |
| 5281 | } |
Willy Tarreau | a7c5276 | 2008-08-16 18:40:18 +0200 | [diff] [blame] | 5282 | |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 5283 | /* client abort with an abortonclose */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5284 | 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] | 5285 | HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1); |
| 5286 | HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1); |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 5287 | if (objt_server(s->target)) |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 5288 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1); |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 5289 | |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 5290 | rep->analysers &= AN_RES_FLT_END; |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 5291 | channel_auto_close(rep); |
| 5292 | |
| 5293 | txn->status = 400; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5294 | channel_truncate(rep); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 5295 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 5296 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5297 | if (!(s->flags & SF_ERR_MASK)) |
| 5298 | s->flags |= SF_ERR_CLICL; |
| 5299 | if (!(s->flags & SF_FINST_MASK)) |
| 5300 | s->flags |= SF_FINST_H; |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 5301 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5302 | /* process_stream() will take care of the error */ |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 5303 | return 0; |
| 5304 | } |
| 5305 | |
Willy Tarreau | 3b8c08a | 2011-09-02 20:16:24 +0200 | [diff] [blame] | 5306 | /* 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] | 5307 | else if (rep->flags & CF_SHUTR) { |
Willy Tarreau | 3b8c08a | 2011-09-02 20:16:24 +0200 | [diff] [blame] | 5308 | if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0) |
Emeric Brun | 8c1aaa2 | 2017-06-15 11:30:06 +0200 | [diff] [blame] | 5309 | http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, msg->err_state, sess->fe); |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 5310 | else if (txn->flags & TX_NOT_FIRST) |
| 5311 | goto abort_keep_alive; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5312 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 5313 | HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5314 | if (objt_server(s->target)) { |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 5315 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5316 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5317 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5318 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5319 | channel_auto_close(rep); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 5320 | rep->analysers &= AN_RES_FLT_END; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5321 | txn->status = 502; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5322 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5323 | channel_truncate(rep); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 5324 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5325 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5326 | if (!(s->flags & SF_ERR_MASK)) |
| 5327 | s->flags |= SF_ERR_SRVCL; |
| 5328 | if (!(s->flags & SF_FINST_MASK)) |
| 5329 | s->flags |= SF_FINST_H; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5330 | return 0; |
| 5331 | } |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 5332 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5333 | /* write error to client (we don't send any message then) */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5334 | else if (rep->flags & CF_WRITE_ERROR) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5335 | if (msg->err_pos >= 0) |
Emeric Brun | 8c1aaa2 | 2017-06-15 11:30:06 +0200 | [diff] [blame] | 5336 | http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, msg->err_state, sess->fe); |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 5337 | else if (txn->flags & TX_NOT_FIRST) |
| 5338 | goto abort_keep_alive; |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 5339 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 5340 | HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 5341 | rep->analysers &= AN_RES_FLT_END; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5342 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5343 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5344 | if (!(s->flags & SF_ERR_MASK)) |
| 5345 | s->flags |= SF_ERR_CLICL; |
| 5346 | if (!(s->flags & SF_FINST_MASK)) |
| 5347 | s->flags |= SF_FINST_H; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5348 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5349 | /* process_stream() will take care of the error */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5350 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5351 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5352 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5353 | channel_dont_close(rep); |
Willy Tarreau | 3f3997e | 2013-12-15 15:21:32 +0100 | [diff] [blame] | 5354 | rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5355 | return 0; |
| 5356 | } |
| 5357 | |
| 5358 | /* More interesting part now : we know that we have a complete |
| 5359 | * response which at least looks like HTTP. We have an indicator |
| 5360 | * of each header's length, so we can parse them quickly. |
| 5361 | */ |
| 5362 | |
| 5363 | if (unlikely(msg->err_pos >= 0)) |
Emeric Brun | 8c1aaa2 | 2017-06-15 11:30:06 +0200 | [diff] [blame] | 5364 | http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, msg->err_state, sess->fe); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5365 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5366 | /* |
| 5367 | * 1: get the status code |
| 5368 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5369 | n = rep->buf->p[msg->sl.st.c] - '0'; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5370 | if (n < 1 || n > 5) |
| 5371 | n = 0; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5372 | /* when the client triggers a 4xx from the server, it's most often due |
| 5373 | * to a missing object or permission. These events should be tracked |
| 5374 | * because if they happen often, it may indicate a brute force or a |
| 5375 | * vulnerability scan. |
| 5376 | */ |
| 5377 | if (n == 4) |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5378 | stream_inc_http_err_ctr(s); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5379 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5380 | if (objt_server(s->target)) |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 5381 | 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] | 5382 | |
Willy Tarreau | 91852eb | 2015-05-01 13:26:00 +0200 | [diff] [blame] | 5383 | /* RFC7230#2.6 has enforced the format of the HTTP version string to be |
| 5384 | * exactly one digit "." one digit. This check may be disabled using |
| 5385 | * option accept-invalid-http-response. |
| 5386 | */ |
| 5387 | if (!(s->be->options2 & PR_O2_RSPBUG_OK)) { |
| 5388 | if (msg->sl.st.v_l != 8) { |
| 5389 | msg->err_pos = 0; |
| 5390 | goto hdr_response_bad; |
| 5391 | } |
| 5392 | |
| 5393 | if (rep->buf->p[4] != '/' || |
| 5394 | !isdigit((unsigned char)rep->buf->p[5]) || |
| 5395 | rep->buf->p[6] != '.' || |
| 5396 | !isdigit((unsigned char)rep->buf->p[7])) { |
| 5397 | msg->err_pos = 4; |
| 5398 | goto hdr_response_bad; |
| 5399 | } |
| 5400 | } |
| 5401 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5402 | /* check if the response is HTTP/1.1 or above */ |
| 5403 | if ((msg->sl.st.v_l == 8) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5404 | ((rep->buf->p[5] > '1') || |
| 5405 | ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1')))) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5406 | msg->flags |= HTTP_MSGF_VER_11; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5407 | |
| 5408 | /* "connection" has not been parsed yet */ |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 5409 | 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] | 5410 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5411 | /* transfer length unknown*/ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5412 | msg->flags &= ~HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5413 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5414 | txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5415 | |
Willy Tarreau | 3965040 | 2010-03-15 19:44:39 +0100 | [diff] [blame] | 5416 | /* Adjust server's health based on status code. Note: status codes 501 |
| 5417 | * and 505 are triggered on demand by client request, so we must not |
| 5418 | * count them as server failures. |
| 5419 | */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5420 | if (objt_server(s->target)) { |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 5421 | 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] | 5422 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK); |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 5423 | else |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5424 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS); |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 5425 | } |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5426 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5427 | /* |
Willy Tarreau | a14ad72 | 2017-07-07 11:36:32 +0200 | [diff] [blame] | 5428 | * We may be facing a 100-continue response, or any other informational |
| 5429 | * 1xx response which is non-final, in which case this is not the right |
| 5430 | * response, and we're waiting for the next one. Let's allow this response |
| 5431 | * to go to the client and wait for the next one. There's an exception for |
| 5432 | * 101 which is used later in the code to switch protocols. |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5433 | */ |
Willy Tarreau | a14ad72 | 2017-07-07 11:36:32 +0200 | [diff] [blame] | 5434 | if (txn->status < 200 && |
| 5435 | (txn->status == 100 || txn->status >= 102)) { |
Willy Tarreau | 628c40c | 2014-04-24 19:11:26 +0200 | [diff] [blame] | 5436 | hdr_idx_init(&txn->hdr_idx); |
| 5437 | msg->next -= channel_forward(rep, msg->next); |
| 5438 | msg->msg_state = HTTP_MSG_RPBEFORE; |
| 5439 | txn->status = 0; |
| 5440 | s->logs.t_data = -1; /* was not a response yet */ |
Christopher Faulet | 3e34429 | 2015-11-24 16:24:13 +0100 | [diff] [blame] | 5441 | FLT_STRM_CB(s, flt_http_reset(s, msg)); |
Willy Tarreau | 628c40c | 2014-04-24 19:11:26 +0200 | [diff] [blame] | 5442 | goto next_one; |
Willy Tarreau | a14ad72 | 2017-07-07 11:36:32 +0200 | [diff] [blame] | 5443 | } |
Willy Tarreau | 628c40c | 2014-04-24 19:11:26 +0200 | [diff] [blame] | 5444 | |
Willy Tarreau | a14ad72 | 2017-07-07 11:36:32 +0200 | [diff] [blame] | 5445 | /* |
| 5446 | * 2: check for cacheability. |
| 5447 | */ |
| 5448 | |
| 5449 | switch (txn->status) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5450 | case 200: |
| 5451 | case 203: |
Willy Tarreau | c55ddce | 2017-12-21 11:41:38 +0100 | [diff] [blame] | 5452 | case 204: |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5453 | case 206: |
| 5454 | case 300: |
| 5455 | case 301: |
Willy Tarreau | c55ddce | 2017-12-21 11:41:38 +0100 | [diff] [blame] | 5456 | case 404: |
| 5457 | case 405: |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5458 | case 410: |
Willy Tarreau | c55ddce | 2017-12-21 11:41:38 +0100 | [diff] [blame] | 5459 | case 414: |
| 5460 | case 501: |
Willy Tarreau | 83ece46 | 2017-12-21 15:13:09 +0100 | [diff] [blame] | 5461 | break; |
| 5462 | default: |
Willy Tarreau | c55ddce | 2017-12-21 11:41:38 +0100 | [diff] [blame] | 5463 | /* RFC7231#6.1: |
| 5464 | * Responses with status codes that are defined as |
| 5465 | * cacheable by default (e.g., 200, 203, 204, 206, |
| 5466 | * 300, 301, 404, 405, 410, 414, and 501 in this |
| 5467 | * specification) can be reused by a cache with |
| 5468 | * heuristic expiration unless otherwise indicated |
| 5469 | * by the method definition or explicit cache |
| 5470 | * controls [RFC7234]; all other status codes are |
| 5471 | * not cacheable by default. |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5472 | */ |
Willy Tarreau | 83ece46 | 2017-12-21 15:13:09 +0100 | [diff] [blame] | 5473 | txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5474 | break; |
| 5475 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5476 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5477 | /* |
| 5478 | * 3: we may need to capture headers |
| 5479 | */ |
| 5480 | s->logs.logwait &= ~LW_RESP; |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 5481 | if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap)) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5482 | capture_headers(rep->buf->p, &txn->hdr_idx, |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 5483 | s->res_cap, sess->fe->rsp_cap); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5484 | |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 5485 | /* 4: determine the transfer-length according to RFC2616 #4.4, updated |
| 5486 | * by RFC7230#3.3.3 : |
| 5487 | * |
| 5488 | * The length of a message body is determined by one of the following |
| 5489 | * (in order of precedence): |
| 5490 | * |
Christopher Faulet | d1cd209 | 2016-11-28 10:14:03 +0100 | [diff] [blame] | 5491 | * 1. Any 2xx (Successful) response to a CONNECT request implies that |
| 5492 | * the connection will become a tunnel immediately after the empty |
| 5493 | * line that concludes the header fields. A client MUST ignore |
| 5494 | * any Content-Length or Transfer-Encoding header fields received |
| 5495 | * in such a message. Any 101 response (Switching Protocols) is |
| 5496 | * managed in the same manner. |
| 5497 | * |
| 5498 | * 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] | 5499 | * (Informational), 204 (No Content), or 304 (Not Modified) status |
| 5500 | * code is always terminated by the first empty line after the |
| 5501 | * header fields, regardless of the header fields present in the |
| 5502 | * message, and thus cannot contain a message body. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5503 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 5504 | * 3. If a Transfer-Encoding header field is present and the chunked |
| 5505 | * transfer coding (Section 4.1) is the final encoding, the message |
| 5506 | * body length is determined by reading and decoding the chunked |
| 5507 | * data until the transfer coding indicates the data is complete. |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5508 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 5509 | * If a Transfer-Encoding header field is present in a response and |
| 5510 | * the chunked transfer coding is not the final encoding, the |
| 5511 | * message body length is determined by reading the connection until |
| 5512 | * it is closed by the server. If a Transfer-Encoding header field |
| 5513 | * is present in a request and the chunked transfer coding is not |
| 5514 | * the final encoding, the message body length cannot be determined |
| 5515 | * reliably; the server MUST respond with the 400 (Bad Request) |
| 5516 | * status code and then close the connection. |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5517 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 5518 | * If a message is received with both a Transfer-Encoding and a |
| 5519 | * Content-Length header field, the Transfer-Encoding overrides the |
| 5520 | * Content-Length. Such a message might indicate an attempt to |
| 5521 | * perform request smuggling (Section 9.5) or response splitting |
| 5522 | * (Section 9.4) and ought to be handled as an error. A sender MUST |
| 5523 | * remove the received Content-Length field prior to forwarding such |
| 5524 | * a message downstream. |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5525 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 5526 | * 4. If a message is received without Transfer-Encoding and with |
| 5527 | * either multiple Content-Length header fields having differing |
| 5528 | * field-values or a single Content-Length header field having an |
| 5529 | * invalid value, then the message framing is invalid and the |
| 5530 | * recipient MUST treat it as an unrecoverable error. If this is a |
| 5531 | * request message, the server MUST respond with a 400 (Bad Request) |
| 5532 | * status code and then close the connection. If this is a response |
| 5533 | * message received by a proxy, the proxy MUST close the connection |
| 5534 | * to the server, discard the received response, and send a 502 (Bad |
| 5535 | * Gateway) response to the client. If this is a response message |
| 5536 | * received by a user agent, the user agent MUST close the |
| 5537 | * connection to the server and discard the received response. |
| 5538 | * |
| 5539 | * 5. If a valid Content-Length header field is present without |
| 5540 | * Transfer-Encoding, its decimal value defines the expected message |
| 5541 | * body length in octets. If the sender closes the connection or |
| 5542 | * the recipient times out before the indicated number of octets are |
| 5543 | * received, the recipient MUST consider the message to be |
| 5544 | * incomplete and close the connection. |
| 5545 | * |
| 5546 | * 6. If this is a request message and none of the above are true, then |
| 5547 | * the message body length is zero (no message body is present). |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5548 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 5549 | * 7. Otherwise, this is a response message without a declared message |
| 5550 | * body length, so the message body length is determined by the |
| 5551 | * number of octets received prior to the server closing the |
| 5552 | * connection. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5553 | */ |
| 5554 | |
| 5555 | /* Skip parsing if no content length is possible. The response flags |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 5556 | * 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] | 5557 | * 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] | 5558 | * FIXME: should we parse anyway and return an error on chunked encoding ? |
| 5559 | */ |
Christopher Faulet | d1cd209 | 2016-11-28 10:14:03 +0100 | [diff] [blame] | 5560 | if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || |
| 5561 | txn->status == 101)) { |
| 5562 | /* Either we've established an explicit tunnel, or we're |
| 5563 | * switching the protocol. In both cases, we're very unlikely |
| 5564 | * to understand the next protocols. We have to switch to tunnel |
| 5565 | * mode, so that we transfer the request and responses then let |
| 5566 | * this protocol pass unmodified. When we later implement specific |
| 5567 | * parsers for such protocols, we'll want to check the Upgrade |
| 5568 | * header which contains information about that protocol for |
| 5569 | * responses with status 101 (eg: see RFC2817 about TLS). |
| 5570 | */ |
| 5571 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN; |
| 5572 | msg->flags |= HTTP_MSGF_XFER_LEN; |
| 5573 | goto end; |
| 5574 | } |
| 5575 | |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5576 | if (txn->meth == HTTP_METH_HEAD || |
| 5577 | (txn->status >= 100 && txn->status < 200) || |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5578 | txn->status == 204 || txn->status == 304) { |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5579 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5580 | goto skip_content_length; |
| 5581 | } |
| 5582 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5583 | use_close_only = 0; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5584 | ctx.idx = 0; |
Willy Tarreau | 4979d5c | 2015-05-01 10:06:30 +0200 | [diff] [blame] | 5585 | while (http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5586 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5587 | msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
| 5588 | else if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5589 | /* bad transfer-encoding (chunked followed by something else) */ |
| 5590 | use_close_only = 1; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5591 | msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5592 | break; |
| 5593 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5594 | } |
| 5595 | |
Willy Tarreau | 1c91391 | 2015-04-30 10:57:51 +0200 | [diff] [blame] | 5596 | /* Chunked responses must have their content-length removed */ |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5597 | ctx.idx = 0; |
Willy Tarreau | b4d0c03 | 2015-05-01 10:25:45 +0200 | [diff] [blame] | 5598 | if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) { |
Willy Tarreau | 1c91391 | 2015-04-30 10:57:51 +0200 | [diff] [blame] | 5599 | while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) |
| 5600 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 5601 | } |
Willy Tarreau | b4d0c03 | 2015-05-01 10:25:45 +0200 | [diff] [blame] | 5602 | else while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5603 | signed long long cl; |
| 5604 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5605 | if (!ctx.vlen) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5606 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5607 | goto hdr_response_bad; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5608 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5609 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5610 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5611 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5612 | goto hdr_response_bad; /* parse failure */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5613 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5614 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5615 | if (cl < 0) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5616 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5617 | goto hdr_response_bad; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5618 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5619 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5620 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5621 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5622 | goto hdr_response_bad; /* already specified, was different */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5623 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5624 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5625 | msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 5626 | msg->body_len = msg->chunk_len = cl; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5627 | } |
| 5628 | |
Christopher Faulet | d1cd209 | 2016-11-28 10:14:03 +0100 | [diff] [blame] | 5629 | skip_content_length: |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5630 | /* Now we have to check if we need to modify the Connection header. |
| 5631 | * This is more difficult on the response than it is on the request, |
| 5632 | * because we can have two different HTTP versions and we don't know |
| 5633 | * how the client will interprete a response. For instance, let's say |
| 5634 | * that the client sends a keep-alive request in HTTP/1.0 and gets an |
| 5635 | * HTTP/1.1 response without any header. Maybe it will bound itself to |
| 5636 | * HTTP/1.0 because it only knows about it, and will consider the lack |
| 5637 | * of header as a close, or maybe it knows HTTP/1.1 and can consider |
| 5638 | * the lack of header as a keep-alive. Thus we will use two flags |
| 5639 | * indicating how a request MAY be understood by the client. In case |
| 5640 | * of multiple possibilities, we'll fix the header to be explicit. If |
| 5641 | * ambiguous cases such as both close and keepalive are seen, then we |
| 5642 | * will fall back to explicit close. Note that we won't take risks with |
| 5643 | * HTTP/1.0 clients which may not necessarily understand keep-alive. |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5644 | * See doc/internals/connection-header.txt for the complete matrix. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5645 | */ |
Christopher Faulet | d1cd209 | 2016-11-28 10:14:03 +0100 | [diff] [blame] | 5646 | if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) && |
| 5647 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN || |
| 5648 | ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
| 5649 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) { |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5650 | int to_del = 0; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5651 | |
Willy Tarreau | 70dffda | 2014-01-30 03:07:23 +0100 | [diff] [blame] | 5652 | /* this situation happens when combining pretend-keepalive with httpclose. */ |
| 5653 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5654 | ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 5655 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) |
Willy Tarreau | 70dffda | 2014-01-30 03:07:23 +0100 | [diff] [blame] | 5656 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 5657 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5658 | /* on unknown transfer length, we must close */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5659 | if (!(msg->flags & HTTP_MSGF_XFER_LEN) && |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5660 | (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) |
| 5661 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5662 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5663 | /* now adjust header transformations depending on current state */ |
| 5664 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN || |
| 5665 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 5666 | to_del |= 2; /* remove "keep-alive" on any response */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5667 | if (!(msg->flags & HTTP_MSGF_VER_11)) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5668 | to_del |= 1; /* remove "close" for HTTP/1.0 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5669 | } |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5670 | else { /* SCL / KAL */ |
| 5671 | to_del |= 1; /* remove "close" on any response */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5672 | if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5673 | to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5674 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5675 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5676 | /* Parse and remove some headers from the connection header */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 5677 | http_parse_connection_header(txn, msg, to_del); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5678 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5679 | /* Some keep-alive responses are converted to Server-close if |
| 5680 | * the server wants to close. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5681 | */ |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5682 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) { |
| 5683 | if ((txn->flags & TX_HDR_CONN_CLO) || |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5684 | (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11))) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5685 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 5686 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5687 | } |
| 5688 | |
Christopher Faulet | d1cd209 | 2016-11-28 10:14:03 +0100 | [diff] [blame] | 5689 | end: |
Willy Tarreau | 7959a55 | 2013-09-23 16:44:27 +0200 | [diff] [blame] | 5690 | /* we want to have the response time before we start processing it */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 5691 | s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now); |
Willy Tarreau | 7959a55 | 2013-09-23 16:44:27 +0200 | [diff] [blame] | 5692 | |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 5693 | /* end of job, return OK */ |
| 5694 | rep->analysers &= ~an_bit; |
| 5695 | rep->analyse_exp = TICK_ETERNITY; |
| 5696 | channel_auto_close(rep); |
| 5697 | return 1; |
| 5698 | |
| 5699 | abort_keep_alive: |
| 5700 | /* A keep-alive request to the server failed on a network error. |
| 5701 | * The client is required to retry. We need to close without returning |
| 5702 | * any other information so that the client retries. |
| 5703 | */ |
| 5704 | txn->status = 0; |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 5705 | rep->analysers &= AN_RES_FLT_END; |
| 5706 | s->req.analysers &= AN_REQ_FLT_END; |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 5707 | channel_auto_close(rep); |
| 5708 | s->logs.logwait = 0; |
| 5709 | s->logs.level = 0; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5710 | s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5711 | channel_truncate(rep); |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 5712 | http_reply_and_close(s, txn->status, NULL); |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 5713 | return 0; |
| 5714 | } |
| 5715 | |
| 5716 | /* This function performs all the processing enabled for the current response. |
| 5717 | * 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] | 5718 | * 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] | 5719 | * other functions. It works like process_request (see indications above). |
| 5720 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5721 | 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] | 5722 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 5723 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5724 | struct http_txn *txn = s->txn; |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 5725 | struct http_msg *msg = &txn->rsp; |
| 5726 | struct proxy *cur_proxy; |
| 5727 | struct cond_wordlist *wl; |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 5728 | enum rule_result ret = HTTP_RULE_RES_CONT; |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 5729 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5730 | DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n", |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 5731 | now_ms, __FUNCTION__, |
| 5732 | s, |
| 5733 | rep, |
| 5734 | rep->rex, rep->wex, |
| 5735 | rep->flags, |
| 5736 | rep->buf->i, |
| 5737 | rep->analysers); |
| 5738 | |
| 5739 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */ |
| 5740 | return 0; |
| 5741 | |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 5742 | /* The stats applet needs to adjust the Connection header but we don't |
| 5743 | * apply any filter there. |
| 5744 | */ |
Willy Tarreau | 612adb8 | 2015-03-10 15:25:54 +0100 | [diff] [blame] | 5745 | if (unlikely(objt_applet(s->target) == &http_stats_applet)) { |
| 5746 | rep->analysers &= ~an_bit; |
| 5747 | rep->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 5748 | goto skip_filters; |
Willy Tarreau | 612adb8 | 2015-03-10 15:25:54 +0100 | [diff] [blame] | 5749 | } |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 5750 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5751 | /* |
| 5752 | * We will have to evaluate the filters. |
| 5753 | * As opposed to version 1.2, now they will be evaluated in the |
| 5754 | * filters order and not in the header order. This means that |
| 5755 | * each filter has to be validated among all headers. |
| 5756 | * |
| 5757 | * Filters are tried with ->be first, then with ->fe if it is |
| 5758 | * different from ->be. |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 5759 | * |
| 5760 | * Maybe we are in resume condiion. In this case I choose the |
| 5761 | * "struct proxy" which contains the rule list matching the resume |
| 5762 | * pointer. If none of theses "struct proxy" match, I initialise |
| 5763 | * the process with the first one. |
| 5764 | * |
| 5765 | * In fact, I check only correspondance betwwen the current list |
| 5766 | * pointer and the ->fe rule list. If it doesn't match, I initialize |
| 5767 | * the loop with the ->be. |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5768 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5769 | if (s->current_rule_list == &sess->fe->http_res_rules) |
| 5770 | cur_proxy = sess->fe; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 5771 | else |
| 5772 | cur_proxy = s->be; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5773 | while (1) { |
| 5774 | struct proxy *rule_set = cur_proxy; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5775 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5776 | /* evaluate http-response rules */ |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 5777 | if (ret == HTTP_RULE_RES_CONT) { |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 5778 | 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] | 5779 | |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 5780 | if (ret == HTTP_RULE_RES_BADREQ) |
| 5781 | goto return_srv_prx_502; |
| 5782 | |
| 5783 | if (ret == HTTP_RULE_RES_DONE) { |
| 5784 | rep->analysers &= ~an_bit; |
| 5785 | rep->analyse_exp = TICK_ETERNITY; |
| 5786 | return 1; |
| 5787 | } |
| 5788 | } |
| 5789 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 5790 | /* we need to be called again. */ |
| 5791 | if (ret == HTTP_RULE_RES_YIELD) { |
| 5792 | channel_dont_close(rep); |
| 5793 | return 0; |
| 5794 | } |
| 5795 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5796 | /* try headers filters */ |
| 5797 | if (rule_set->rsp_exp != NULL) { |
| 5798 | if (apply_filters_to_response(s, rep, rule_set) < 0) { |
| 5799 | return_bad_resp: |
| 5800 | if (objt_server(s->target)) { |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 5801 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1); |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5802 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5803 | } |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 5804 | HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1); |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5805 | return_srv_prx_502: |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 5806 | rep->analysers &= AN_RES_FLT_END; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5807 | txn->status = 502; |
| 5808 | s->logs.t_data = -1; /* was not a valid response */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5809 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5810 | channel_truncate(rep); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 5811 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5812 | if (!(s->flags & SF_ERR_MASK)) |
| 5813 | s->flags |= SF_ERR_PRXCOND; |
| 5814 | if (!(s->flags & SF_FINST_MASK)) |
| 5815 | s->flags |= SF_FINST_H; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5816 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5817 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5818 | } |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5819 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5820 | /* has the response been denied ? */ |
| 5821 | if (txn->flags & TX_SVDENY) { |
| 5822 | if (objt_server(s->target)) |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 5823 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5824 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 5825 | HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1); |
| 5826 | HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 5827 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 5828 | HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5829 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5830 | goto return_srv_prx_502; |
| 5831 | } |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 5832 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5833 | /* add response headers from the rule sets in the same order */ |
| 5834 | list_for_each_entry(wl, &rule_set->rsp_add, list) { |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 5835 | if (txn->status < 200 && txn->status != 101) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5836 | break; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5837 | if (wl->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 5838 | 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] | 5839 | ret = acl_pass(ret); |
| 5840 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 5841 | ret = !ret; |
| 5842 | if (!ret) |
| 5843 | continue; |
| 5844 | } |
| 5845 | if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0)) |
| 5846 | goto return_bad_resp; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5847 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5848 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5849 | /* check whether we're already working on the frontend */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5850 | if (cur_proxy == sess->fe) |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5851 | break; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5852 | cur_proxy = sess->fe; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5853 | } |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5854 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 5855 | /* After this point, this anayzer can't return yield, so we can |
| 5856 | * remove the bit corresponding to this analyzer from the list. |
| 5857 | * |
| 5858 | * Note that the intermediate returns and goto found previously |
| 5859 | * reset the analyzers. |
| 5860 | */ |
| 5861 | rep->analysers &= ~an_bit; |
| 5862 | rep->analyse_exp = TICK_ETERNITY; |
| 5863 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5864 | /* OK that's all we can do for 1xx responses */ |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 5865 | if (unlikely(txn->status < 200 && txn->status != 101)) |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5866 | goto skip_header_mangling; |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5867 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5868 | /* |
| 5869 | * Now check for a server cookie. |
| 5870 | */ |
Willy Tarreau | 53a09d5 | 2015-08-10 18:59:40 +0200 | [diff] [blame] | 5871 | 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] | 5872 | manage_server_side_cookies(s, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5873 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5874 | /* |
| 5875 | * Check for cache-control or pragma headers if required. |
| 5876 | */ |
Willy Tarreau | 12b32f2 | 2017-12-21 16:08:09 +0100 | [diff] [blame] | 5877 | 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] | 5878 | check_response_for_cacheability(s, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5879 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5880 | /* |
| 5881 | * Add server cookie in the response if needed |
| 5882 | */ |
| 5883 | if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) && |
| 5884 | !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) && |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5885 | (!(s->flags & SF_DIRECT) || |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5886 | ((s->be->cookie_maxidle || txn->cookie_last_date) && |
| 5887 | (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) || |
| 5888 | (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date |
| 5889 | (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date |
| 5890 | (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) && |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5891 | !(s->flags & SF_IGNORE_PRST)) { |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5892 | /* the server is known, it's not the one the client requested, or the |
| 5893 | * cookie's last seen date needs to be refreshed. We have to |
| 5894 | * insert a set-cookie here, except if we want to insert only on POST |
| 5895 | * requests and this one isn't. Note that servers which don't have cookies |
| 5896 | * (eg: some backup servers) will return a full cookie removal request. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5897 | */ |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5898 | if (!objt_server(s->target)->cookie) { |
| 5899 | chunk_printf(&trash, |
| 5900 | "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/", |
| 5901 | s->be->cookie_name); |
| 5902 | } |
| 5903 | else { |
| 5904 | 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] | 5905 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5906 | if (s->be->cookie_maxidle || s->be->cookie_maxlife) { |
| 5907 | /* emit last_date, which is mandatory */ |
| 5908 | trash.str[trash.len++] = COOKIE_DELIM_DATE; |
| 5909 | s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len); |
| 5910 | trash.len += 5; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5911 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5912 | if (s->be->cookie_maxlife) { |
| 5913 | /* emit first_date, which is either the original one or |
| 5914 | * the current date. |
| 5915 | */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 5916 | trash.str[trash.len++] = COOKIE_DELIM_DATE; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5917 | s30tob64(txn->cookie_first_date ? |
| 5918 | txn->cookie_first_date >> 2 : |
| 5919 | (date.tv_sec+3) >> 2, trash.str + trash.len); |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 5920 | trash.len += 5; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5921 | } |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5922 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5923 | chunk_appendf(&trash, "; path=/"); |
| 5924 | } |
Willy Tarreau | 4992dd2 | 2012-05-31 21:02:17 +0200 | [diff] [blame] | 5925 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5926 | if (s->be->cookie_domain) |
| 5927 | chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain); |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5928 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5929 | if (s->be->ck_opts & PR_CK_HTTPONLY) |
| 5930 | chunk_appendf(&trash, "; HttpOnly"); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5931 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5932 | if (s->be->ck_opts & PR_CK_SECURE) |
| 5933 | chunk_appendf(&trash, "; Secure"); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5934 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5935 | if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0)) |
| 5936 | goto return_bad_resp; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5937 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5938 | txn->flags &= ~TX_SCK_MASK; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5939 | if (objt_server(s->target)->cookie && (s->flags & SF_DIRECT)) |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5940 | /* the server did not change, only the date was updated */ |
| 5941 | txn->flags |= TX_SCK_UPDATED; |
| 5942 | else |
| 5943 | txn->flags |= TX_SCK_INSERTED; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5944 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5945 | /* Here, we will tell an eventual cache on the client side that we don't |
| 5946 | * want it to cache this reply because HTTP/1.0 caches also cache cookies ! |
| 5947 | * Some caches understand the correct form: 'no-cache="set-cookie"', but |
| 5948 | * 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] | 5949 | */ |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5950 | if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5951 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5952 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5953 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5954 | if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, |
| 5955 | "Cache-control: private", 22) < 0)) |
| 5956 | goto return_bad_resp; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5957 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5958 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5959 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5960 | /* |
| 5961 | * Check if result will be cacheable with a cookie. |
| 5962 | * We'll block the response if security checks have caught |
| 5963 | * nasty things such as a cacheable cookie. |
| 5964 | */ |
| 5965 | if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) == |
| 5966 | (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) && |
| 5967 | (s->be->options & PR_O_CHK_CACHE)) { |
| 5968 | /* we're in presence of a cacheable response containing |
| 5969 | * a set-cookie header. We'll block it as requested by |
| 5970 | * the 'checkcache' option, and send an alert. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5971 | */ |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5972 | if (objt_server(s->target)) |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 5973 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1); |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5974 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 5975 | HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1); |
| 5976 | HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 5977 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 5978 | HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5979 | |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5980 | ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 5981 | 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] | 5982 | send_log(s->be, LOG_ALERT, |
| 5983 | "Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 5984 | s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>"); |
| 5985 | goto return_srv_prx_502; |
| 5986 | } |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 5987 | |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 5988 | skip_filters: |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5989 | /* |
| 5990 | * Adjust "Connection: close" or "Connection: keep-alive" if needed. |
| 5991 | * If an "Upgrade" token is found, the header is left untouched in order |
| 5992 | * 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] | 5993 | * if anything but "Upgrade" is present in the Connection header. We don't |
| 5994 | * want to touch any 101 response either since it's switching to another |
| 5995 | * protocol. |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5996 | */ |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 5997 | if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) && |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5998 | (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5999 | ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6000 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) { |
| 6001 | unsigned int want_flags = 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6002 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6003 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 6004 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 6005 | /* we want a keep-alive response here. Keep-alive header |
| 6006 | * required if either side is not 1.1. |
| 6007 | */ |
| 6008 | if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11)) |
| 6009 | want_flags |= TX_CON_KAL_SET; |
| 6010 | } |
| 6011 | else { |
| 6012 | /* we want a close response here. Close header required if |
| 6013 | * the server is 1.1, regardless of the client. |
| 6014 | */ |
| 6015 | if (msg->flags & HTTP_MSGF_VER_11) |
| 6016 | want_flags |= TX_CON_CLO_SET; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6017 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6018 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6019 | if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 6020 | http_change_connection_header(txn, msg, want_flags); |
| 6021 | } |
| 6022 | |
| 6023 | skip_header_mangling: |
Christopher Faulet | 69744d9 | 2017-03-30 10:54:35 +0200 | [diff] [blame] | 6024 | /* Always enter in the body analyzer */ |
| 6025 | rep->analysers &= ~AN_RES_FLT_XFER_DATA; |
| 6026 | rep->analysers |= AN_RES_HTTP_XFER_BODY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6027 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6028 | /* if the user wants to log as soon as possible, without counting |
| 6029 | * bytes from the server, then this is the right moment. We have |
| 6030 | * to temporarily assign bytes_out to log what we currently have. |
| 6031 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6032 | if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) { |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6033 | s->logs.t_close = s->logs.t_data; /* to get a valid end date */ |
| 6034 | s->logs.bytes_out = txn->rsp.eoh; |
| 6035 | s->do_log(s); |
| 6036 | s->logs.bytes_out = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6037 | } |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 6038 | return 1; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6039 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6040 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6041 | /* This function is an analyser which forwards response body (including chunk |
| 6042 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 6043 | * zero byte. The only situation where it must not be called is when we're in |
| 6044 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 6045 | * remaining data and to resync after end of body. It expects the msg_state to |
| 6046 | * 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] | 6047 | * 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] | 6048 | * |
| 6049 | * It is capable of compressing response data both in content-length mode and |
| 6050 | * in chunked mode. The state machines follows different flows depending on |
| 6051 | * whether content-length and chunked modes are used, since there are no |
| 6052 | * trailers in content-length : |
| 6053 | * |
| 6054 | * chk-mode cl-mode |
| 6055 | * ,----- BODY -----. |
| 6056 | * / \ |
| 6057 | * V size > 0 V chk-mode |
| 6058 | * .--> SIZE -------------> DATA -------------> CRLF |
| 6059 | * | | size == 0 | last byte | |
| 6060 | * | v final crlf v inspected | |
| 6061 | * | TRAILERS -----------> DONE | |
| 6062 | * | | |
| 6063 | * `----------------------------------------------' |
| 6064 | * |
| 6065 | * Compression only happens in the DATA state, and must be flushed in final |
| 6066 | * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding |
| 6067 | * is performed at once on final states for all bytes parsed, or when leaving |
| 6068 | * on missing data. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6069 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6070 | 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] | 6071 | { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6072 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 6073 | struct http_txn *txn = s->txn; |
| 6074 | struct http_msg *msg = &s->txn->rsp; |
Christopher Faulet | 3e34429 | 2015-11-24 16:24:13 +0100 | [diff] [blame] | 6075 | int ret; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6076 | |
Christopher Faulet | 814d270 | 2017-03-30 11:33:44 +0200 | [diff] [blame] | 6077 | DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n", |
| 6078 | now_ms, __FUNCTION__, |
| 6079 | s, |
| 6080 | res, |
| 6081 | res->rex, res->wex, |
| 6082 | res->flags, |
| 6083 | res->buf->i, |
| 6084 | res->analysers); |
| 6085 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 6086 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 6087 | return 0; |
| 6088 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 6089 | if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) || |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6090 | ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) || |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 6091 | !s->req.analysers) { |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 6092 | /* Output closed while we were sending data. We must abort and |
| 6093 | * wake the other side up. |
| 6094 | */ |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 6095 | msg->err_state = msg->msg_state; |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 6096 | msg->msg_state = HTTP_MSG_ERROR; |
| 6097 | http_resync_states(s); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 6098 | return 1; |
| 6099 | } |
| 6100 | |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 6101 | /* in most states, we should abort in case of early close */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6102 | channel_auto_close(res); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 6103 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 6104 | if (msg->msg_state == HTTP_MSG_BODY) { |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6105 | msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK) |
| 6106 | ? HTTP_MSG_CHUNK_SIZE |
| 6107 | : HTTP_MSG_DATA); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6108 | } |
| 6109 | |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 6110 | if (res->to_forward) { |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6111 | /* We can't process the buffer's contents yet */ |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 6112 | res->flags |= CF_WAKE_WRITE; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6113 | goto missing_data_or_waiting; |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 6114 | } |
| 6115 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6116 | if (msg->msg_state < HTTP_MSG_DONE) { |
| 6117 | ret = ((msg->flags & HTTP_MSGF_TE_CHNK) |
| 6118 | ? http_msg_forward_chunked_body(s, msg) |
| 6119 | : http_msg_forward_body(s, msg)); |
| 6120 | if (!ret) |
| 6121 | goto missing_data_or_waiting; |
| 6122 | if (ret < 0) |
| 6123 | goto return_bad_res; |
| 6124 | } |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 6125 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6126 | /* other states, DONE...TUNNEL */ |
| 6127 | /* for keep-alive we don't want to forward closes on DONE */ |
| 6128 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 6129 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
| 6130 | channel_dont_close(res); |
Willy Tarreau | 3ce10ff | 2014-04-22 08:24:38 +0200 | [diff] [blame] | 6131 | |
Christopher Faulet | 894da4c | 2017-07-18 11:29:07 +0200 | [diff] [blame] | 6132 | http_resync_states(s); |
| 6133 | if (!(res->analysers & an_bit)) { |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6134 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 6135 | if (res->flags & CF_SHUTW) { |
| 6136 | /* response errors are most likely due to the |
| 6137 | * client aborting the transfer. */ |
| 6138 | goto aborted_xfer; |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 6139 | } |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6140 | if (msg->err_pos >= 0) |
Emeric Brun | 8c1aaa2 | 2017-06-15 11:30:06 +0200 | [diff] [blame] | 6141 | http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, msg->err_state, strm_fe(s)); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6142 | goto return_bad_res; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6143 | } |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6144 | return 1; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6145 | } |
Willy Tarreau | f51d03c | 2016-05-02 15:25:15 +0200 | [diff] [blame] | 6146 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6147 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6148 | missing_data_or_waiting: |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6149 | if (res->flags & CF_SHUTW) |
| 6150 | goto aborted_xfer; |
| 6151 | |
| 6152 | /* stop waiting for data if the input is closed before the end. If the |
| 6153 | * client side was already closed, it means that the client has aborted, |
| 6154 | * so we don't want to count this as a server abort. Otherwise it's a |
| 6155 | * server abort. |
| 6156 | */ |
Christopher Faulet | a33510b | 2017-03-31 15:37:29 +0200 | [diff] [blame] | 6157 | if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6158 | if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW)) |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6159 | goto aborted_xfer; |
Christopher Faulet | a46bbd8 | 2015-06-19 09:00:58 +0200 | [diff] [blame] | 6160 | /* If we have some pending data, we continue the processing */ |
| 6161 | if (!buffer_pending(res->buf)) { |
| 6162 | if (!(s->flags & SF_ERR_MASK)) |
| 6163 | s->flags |= SF_ERR_SRVCL; |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 6164 | HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1); |
Christopher Faulet | a46bbd8 | 2015-06-19 09:00:58 +0200 | [diff] [blame] | 6165 | if (objt_server(s->target)) |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 6166 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1); |
Christopher Faulet | a46bbd8 | 2015-06-19 09:00:58 +0200 | [diff] [blame] | 6167 | goto return_bad_res_stats_ok; |
| 6168 | } |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 6169 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 6170 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 6171 | /* 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] | 6172 | if (!s->req.analysers) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 6173 | goto return_bad_res; |
| 6174 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6175 | /* When TE: chunked is used, we need to get there again to parse |
| 6176 | * 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] | 6177 | * set CF_DONTCLOSE. Similarly, if keep-alive is set on the client side |
| 6178 | * or if there are filters registered on the stream, we don't want to |
| 6179 | * forward a close |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 6180 | */ |
Christopher Faulet | 69744d9 | 2017-03-30 10:54:35 +0200 | [diff] [blame] | 6181 | if ((msg->flags & HTTP_MSGF_TE_CHNK) || |
Christopher Faulet | f1cc5d0 | 2017-02-08 09:45:13 +0100 | [diff] [blame] | 6182 | HAS_DATA_FILTERS(s, res) || |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 6183 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 6184 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6185 | channel_dont_close(res); |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 6186 | |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 6187 | /* 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] | 6188 | * 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] | 6189 | * 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] | 6190 | * modes are already handled by the stream sock layer. We must not do |
| 6191 | * this in content-length mode because it could present the MSG_MORE |
| 6192 | * flag with the last block of forwarded data, which would cause an |
| 6193 | * additional delay to be observed by the receiver. |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 6194 | */ |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 6195 | if ((msg->flags & HTTP_MSGF_TE_CHNK) || (msg->flags & HTTP_MSGF_COMPRESSING)) |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 6196 | res->flags |= CF_EXPECT_MORE; |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 6197 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6198 | /* the stream handler will take care of timeouts and errors */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6199 | return 0; |
| 6200 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 6201 | return_bad_res: /* let's centralize all bad responses */ |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 6202 | HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6203 | if (objt_server(s->target)) |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 6204 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6205 | |
| 6206 | return_bad_res_stats_ok: |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 6207 | txn->rsp.err_state = txn->rsp.msg_state; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6208 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 6209 | /* don't send any error message as we're in the body */ |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 6210 | http_reply_and_close(s, txn->status, NULL); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 6211 | res->analysers &= AN_RES_FLT_END; |
| 6212 | 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] | 6213 | if (objt_server(s->target)) |
| 6214 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6215 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6216 | if (!(s->flags & SF_ERR_MASK)) |
| 6217 | s->flags |= SF_ERR_PRXCOND; |
| 6218 | if (!(s->flags & SF_FINST_MASK)) |
| 6219 | s->flags |= SF_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6220 | return 0; |
| 6221 | |
| 6222 | aborted_xfer: |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 6223 | txn->rsp.err_state = txn->rsp.msg_state; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6224 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
| 6225 | /* don't send any error message as we're in the body */ |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 6226 | http_reply_and_close(s, txn->status, NULL); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 6227 | res->analysers &= AN_RES_FLT_END; |
| 6228 | 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] | 6229 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 6230 | HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1); |
| 6231 | HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6232 | if (objt_server(s->target)) |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 6233 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6234 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6235 | if (!(s->flags & SF_ERR_MASK)) |
| 6236 | s->flags |= SF_ERR_CLICL; |
| 6237 | if (!(s->flags & SF_FINST_MASK)) |
| 6238 | s->flags |= SF_FINST_D; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6239 | return 0; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6240 | } |
| 6241 | |
| 6242 | |
| 6243 | static inline int |
| 6244 | http_msg_forward_body(struct stream *s, struct http_msg *msg) |
| 6245 | { |
| 6246 | struct channel *chn = msg->chn; |
| 6247 | int ret; |
| 6248 | |
| 6249 | /* Here we have the guarantee to be in HTTP_MSG_DATA or HTTP_MSG_ENDING state */ |
| 6250 | |
| 6251 | if (msg->msg_state == HTTP_MSG_ENDING) |
| 6252 | goto ending; |
| 6253 | |
| 6254 | /* Neither content-length, nor transfer-encoding was found, so we must |
| 6255 | * read the body until the server connection is closed. In that case, we |
| 6256 | * eat data as they come. Of course, this happens for response only. */ |
| 6257 | if (!(msg->flags & HTTP_MSGF_XFER_LEN)) { |
| 6258 | unsigned long long len = (chn->buf->i - msg->next); |
| 6259 | msg->chunk_len += len; |
| 6260 | msg->body_len += len; |
| 6261 | } |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 6262 | ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg), |
| 6263 | /* default_ret */ MIN(msg->chunk_len, chn->buf->i - msg->next), |
| 6264 | /* on_error */ goto error); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6265 | msg->next += ret; |
| 6266 | msg->chunk_len -= ret; |
| 6267 | if (msg->chunk_len) { |
| 6268 | /* input empty or output full */ |
| 6269 | if (chn->buf->i > msg->next) |
| 6270 | chn->flags |= CF_WAKE_WRITE; |
| 6271 | goto missing_data_or_waiting; |
| 6272 | } |
| 6273 | |
Christopher Faulet | 1486b0a | 2017-07-18 11:42:08 +0200 | [diff] [blame] | 6274 | /* This check can only be true for a response. HTTP_MSGF_XFER_LEN is |
| 6275 | * always set for a request. */ |
| 6276 | if (!(msg->flags & HTTP_MSGF_XFER_LEN)) { |
| 6277 | /* The server still sending data that should be filtered */ |
| 6278 | if (!(chn->flags & CF_SHUTR) && HAS_DATA_FILTERS(s, chn)) |
| 6279 | goto missing_data_or_waiting; |
Christopher Faulet | fd04fcf | 2018-02-02 15:54:15 +0100 | [diff] [blame] | 6280 | msg->msg_state = HTTP_MSG_TUNNEL; |
| 6281 | goto ending; |
Christopher Faulet | 1486b0a | 2017-07-18 11:42:08 +0200 | [diff] [blame] | 6282 | } |
Christopher Faulet | f1cc5d0 | 2017-02-08 09:45:13 +0100 | [diff] [blame] | 6283 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6284 | msg->msg_state = HTTP_MSG_ENDING; |
| 6285 | |
| 6286 | ending: |
| 6287 | /* we may have some pending data starting at res->buf->p such as a last |
| 6288 | * chunk of data or trailers. */ |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 6289 | ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next), |
| 6290 | /* default_ret */ msg->next, |
| 6291 | /* on_error */ goto error); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6292 | b_adv(chn->buf, ret); |
| 6293 | msg->next -= ret; |
Willy Tarreau | 9962f8f | 2016-06-28 11:52:08 +0200 | [diff] [blame] | 6294 | if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)) |
| 6295 | msg->sov -= ret; |
Christopher Faulet | a9300a3 | 2016-06-28 15:54:44 +0200 | [diff] [blame] | 6296 | if (msg->next) |
| 6297 | goto waiting; |
Willy Tarreau | 9962f8f | 2016-06-28 11:52:08 +0200 | [diff] [blame] | 6298 | |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 6299 | FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg), |
| 6300 | /* default_ret */ 1, |
| 6301 | /* on_error */ goto error, |
| 6302 | /* on_wait */ goto waiting); |
Christopher Faulet | fd04fcf | 2018-02-02 15:54:15 +0100 | [diff] [blame] | 6303 | if (msg->msg_state == HTTP_MSG_ENDING) |
| 6304 | msg->msg_state = HTTP_MSG_DONE; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6305 | return 1; |
| 6306 | |
| 6307 | missing_data_or_waiting: |
| 6308 | /* we may have some pending data starting at chn->buf->p */ |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 6309 | ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next), |
| 6310 | /* default_ret */ msg->next, |
| 6311 | /* on_error */ goto error); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6312 | b_adv(chn->buf, ret); |
| 6313 | msg->next -= ret; |
| 6314 | if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0) |
| 6315 | msg->sov -= ret; |
Christopher Faulet | 75e2eb6 | 2015-12-15 10:41:47 +0100 | [diff] [blame] | 6316 | if (!HAS_DATA_FILTERS(s, chn)) |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6317 | msg->chunk_len -= channel_forward(chn, msg->chunk_len); |
Christopher Faulet | a9300a3 | 2016-06-28 15:54:44 +0200 | [diff] [blame] | 6318 | waiting: |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6319 | return 0; |
| 6320 | error: |
| 6321 | return -1; |
| 6322 | } |
| 6323 | |
| 6324 | static inline int |
| 6325 | http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg) |
| 6326 | { |
| 6327 | struct channel *chn = msg->chn; |
Willy Tarreau | e56cdd3 | 2017-09-21 08:36:33 +0200 | [diff] [blame] | 6328 | unsigned int chunk; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6329 | int ret; |
| 6330 | |
| 6331 | /* Here we have the guarantee to be in one of the following state: |
| 6332 | * HTTP_MSG_DATA, HTTP_MSG_CHUNK_SIZE, HTTP_MSG_CHUNK_CRLF, |
| 6333 | * HTTP_MSG_TRAILERS or HTTP_MSG_ENDING. */ |
| 6334 | |
| 6335 | switch_states: |
| 6336 | switch (msg->msg_state) { |
| 6337 | case HTTP_MSG_DATA: |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 6338 | ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg), |
| 6339 | /* default_ret */ MIN(msg->chunk_len, chn->buf->i - msg->next), |
| 6340 | /* on_error */ goto error); |
| 6341 | msg->next += ret; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6342 | msg->chunk_len -= ret; |
| 6343 | if (msg->chunk_len) { |
| 6344 | /* input empty or output full */ |
| 6345 | if (chn->buf->i > msg->next) |
| 6346 | chn->flags |= CF_WAKE_WRITE; |
| 6347 | goto missing_data_or_waiting; |
| 6348 | } |
| 6349 | |
| 6350 | /* nothing left to forward for this chunk*/ |
| 6351 | msg->msg_state = HTTP_MSG_CHUNK_CRLF; |
| 6352 | /* fall through for HTTP_MSG_CHUNK_CRLF */ |
| 6353 | |
| 6354 | case HTTP_MSG_CHUNK_CRLF: |
| 6355 | /* we want the CRLF after the data */ |
Willy Tarreau | b289256 | 2017-09-21 11:33:54 +0200 | [diff] [blame] | 6356 | ret = h1_skip_chunk_crlf(chn->buf, msg->next, chn->buf->i); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6357 | if (ret == 0) |
| 6358 | goto missing_data_or_waiting; |
Willy Tarreau | b289256 | 2017-09-21 11:33:54 +0200 | [diff] [blame] | 6359 | if (ret < 0) { |
| 6360 | msg->err_pos = chn->buf->i + ret; |
| 6361 | if (msg->err_pos < 0) |
| 6362 | msg->err_pos += chn->buf->size; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6363 | goto chunk_parsing_error; |
Willy Tarreau | b289256 | 2017-09-21 11:33:54 +0200 | [diff] [blame] | 6364 | } |
Christopher Faulet | 113f7de | 2015-12-14 14:52:13 +0100 | [diff] [blame] | 6365 | msg->next += ret; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6366 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 6367 | /* fall through for HTTP_MSG_CHUNK_SIZE */ |
| 6368 | |
| 6369 | case HTTP_MSG_CHUNK_SIZE: |
| 6370 | /* read the chunk size and assign it to ->chunk_len, |
| 6371 | * then set ->next to point to the body and switch to |
| 6372 | * DATA or TRAILERS state. |
| 6373 | */ |
Willy Tarreau | e56cdd3 | 2017-09-21 08:36:33 +0200 | [diff] [blame] | 6374 | ret = h1_parse_chunk_size(chn->buf, msg->next, chn->buf->i, &chunk); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6375 | if (ret == 0) |
| 6376 | goto missing_data_or_waiting; |
Willy Tarreau | e56cdd3 | 2017-09-21 08:36:33 +0200 | [diff] [blame] | 6377 | if (ret < 0) { |
| 6378 | msg->err_pos = chn->buf->i + ret; |
| 6379 | if (msg->err_pos < 0) |
| 6380 | msg->err_pos += chn->buf->size; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6381 | goto chunk_parsing_error; |
Willy Tarreau | e56cdd3 | 2017-09-21 08:36:33 +0200 | [diff] [blame] | 6382 | } |
| 6383 | |
| 6384 | msg->sol = ret; |
Christopher Faulet | 113f7de | 2015-12-14 14:52:13 +0100 | [diff] [blame] | 6385 | msg->next += ret; |
Willy Tarreau | e56cdd3 | 2017-09-21 08:36:33 +0200 | [diff] [blame] | 6386 | msg->chunk_len = chunk; |
| 6387 | msg->body_len += chunk; |
| 6388 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6389 | if (msg->chunk_len) { |
| 6390 | msg->msg_state = HTTP_MSG_DATA; |
| 6391 | goto switch_states; |
| 6392 | } |
| 6393 | msg->msg_state = HTTP_MSG_TRAILERS; |
| 6394 | /* fall through for HTTP_MSG_TRAILERS */ |
| 6395 | |
| 6396 | case HTTP_MSG_TRAILERS: |
| 6397 | ret = http_forward_trailers(msg); |
| 6398 | if (ret < 0) |
| 6399 | goto chunk_parsing_error; |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 6400 | FLT_STRM_DATA_CB(s, chn, flt_http_chunk_trailers(s, msg), |
| 6401 | /* default_ret */ 1, |
| 6402 | /* on_error */ goto error); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6403 | msg->next += msg->sol; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6404 | if (!ret) |
| 6405 | goto missing_data_or_waiting; |
| 6406 | break; |
| 6407 | |
| 6408 | case HTTP_MSG_ENDING: |
| 6409 | goto ending; |
| 6410 | |
| 6411 | default: |
| 6412 | /* This should no happen in this function */ |
| 6413 | goto error; |
| 6414 | } |
| 6415 | |
| 6416 | msg->msg_state = HTTP_MSG_ENDING; |
| 6417 | ending: |
| 6418 | /* we may have some pending data starting at res->buf->p such as a last |
| 6419 | * chunk of data or trailers. */ |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 6420 | 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] | 6421 | /* default_ret */ msg->next, |
| 6422 | /* on_error */ goto error); |
| 6423 | b_adv(chn->buf, ret); |
| 6424 | msg->next -= ret; |
Willy Tarreau | 9962f8f | 2016-06-28 11:52:08 +0200 | [diff] [blame] | 6425 | if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)) |
| 6426 | msg->sov -= ret; |
Christopher Faulet | a9300a3 | 2016-06-28 15:54:44 +0200 | [diff] [blame] | 6427 | if (msg->next) |
| 6428 | goto waiting; |
Willy Tarreau | 9962f8f | 2016-06-28 11:52:08 +0200 | [diff] [blame] | 6429 | |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 6430 | FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg), |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6431 | /* default_ret */ 1, |
| 6432 | /* on_error */ goto error, |
| 6433 | /* on_wait */ goto waiting); |
| 6434 | msg->msg_state = HTTP_MSG_DONE; |
| 6435 | return 1; |
| 6436 | |
| 6437 | missing_data_or_waiting: |
| 6438 | /* we may have some pending data starting at chn->buf->p */ |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 6439 | 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] | 6440 | /* default_ret */ msg->next, |
| 6441 | /* on_error */ goto error); |
| 6442 | b_adv(chn->buf, ret); |
| 6443 | msg->next -= ret; |
| 6444 | if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0) |
| 6445 | msg->sov -= ret; |
Christopher Faulet | 75e2eb6 | 2015-12-15 10:41:47 +0100 | [diff] [blame] | 6446 | if (!HAS_DATA_FILTERS(s, chn)) |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6447 | msg->chunk_len -= channel_forward(chn, msg->chunk_len); |
Christopher Faulet | a9300a3 | 2016-06-28 15:54:44 +0200 | [diff] [blame] | 6448 | waiting: |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6449 | return 0; |
| 6450 | |
| 6451 | chunk_parsing_error: |
| 6452 | if (msg->err_pos >= 0) { |
| 6453 | if (chn->flags & CF_ISRESP) |
Emeric Brun | 8c1aaa2 | 2017-06-15 11:30:06 +0200 | [diff] [blame] | 6454 | http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6455 | msg->msg_state, strm_fe(s)); |
| 6456 | else |
Emeric Brun | 8c1aaa2 | 2017-06-15 11:30:06 +0200 | [diff] [blame] | 6457 | http_capture_bad_message(strm_fe(s), &strm_fe(s)->invalid_req, s, |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6458 | msg, msg->msg_state, s->be); |
| 6459 | } |
| 6460 | error: |
| 6461 | return -1; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6462 | } |
| 6463 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6464 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6465 | /* Iterate the same filter through all request headers. |
| 6466 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6467 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 6468 | * DENY stats. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6469 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6470 | 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] | 6471 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6472 | char *cur_ptr, *cur_end, *cur_next; |
| 6473 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 6474 | struct http_txn *txn = s->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6475 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6476 | int delta; |
Willy Tarreau | 0f7562b | 2007-01-07 15:46:13 +0100 | [diff] [blame] | 6477 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6478 | last_hdr = 0; |
| 6479 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6480 | cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6481 | old_idx = 0; |
| 6482 | |
| 6483 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6484 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6485 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6486 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6487 | (exp->action == ACT_ALLOW || |
| 6488 | exp->action == ACT_DENY || |
| 6489 | exp->action == ACT_TARPIT)) |
| 6490 | return 0; |
| 6491 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6492 | cur_idx = txn->hdr_idx.v[old_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6493 | if (!cur_idx) |
| 6494 | break; |
| 6495 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6496 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6497 | cur_ptr = cur_next; |
| 6498 | cur_end = cur_ptr + cur_hdr->len; |
| 6499 | cur_next = cur_end + cur_hdr->cr + 1; |
| 6500 | |
| 6501 | /* Now we have one header between cur_ptr and cur_end, |
| 6502 | * and the next header starts at cur_next. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6503 | */ |
| 6504 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 6505 | 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] | 6506 | switch (exp->action) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6507 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6508 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6509 | last_hdr = 1; |
| 6510 | break; |
| 6511 | |
| 6512 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6513 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6514 | last_hdr = 1; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6515 | break; |
| 6516 | |
| 6517 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6518 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6519 | last_hdr = 1; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6520 | break; |
| 6521 | |
| 6522 | case ACT_REPLACE: |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 6523 | trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch); |
| 6524 | if (trash.len < 0) |
| 6525 | return -1; |
| 6526 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6527 | delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6528 | /* FIXME: if the user adds a newline in the replacement, the |
| 6529 | * index will not be recalculated for now, and the new line |
| 6530 | * will not be counted as a new header. |
| 6531 | */ |
| 6532 | |
| 6533 | cur_end += delta; |
| 6534 | cur_next += delta; |
| 6535 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6536 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6537 | break; |
| 6538 | |
| 6539 | case ACT_REMOVE: |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6540 | delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6541 | cur_next += delta; |
| 6542 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6543 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6544 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 6545 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6546 | cur_hdr->len = 0; |
| 6547 | cur_end = NULL; /* null-term has been rewritten */ |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 6548 | cur_idx = old_idx; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6549 | break; |
| 6550 | |
| 6551 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6552 | } |
| 6553 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6554 | /* keep the link from this header to next one in case of later |
| 6555 | * removal of next header. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6556 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6557 | old_idx = cur_idx; |
| 6558 | } |
| 6559 | return 0; |
| 6560 | } |
| 6561 | |
| 6562 | |
| 6563 | /* Apply the filter to the request line. |
| 6564 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 6565 | * or -1 if a replacement resulted in an invalid request line. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6566 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 6567 | * DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6568 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6569 | 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] | 6570 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6571 | char *cur_ptr, *cur_end; |
| 6572 | int done; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 6573 | struct http_txn *txn = s->txn; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6574 | int delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6575 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6576 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6577 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6578 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6579 | (exp->action == ACT_ALLOW || |
| 6580 | exp->action == ACT_DENY || |
| 6581 | exp->action == ACT_TARPIT)) |
| 6582 | return 0; |
| 6583 | else if (exp->action == ACT_REMOVE) |
| 6584 | return 0; |
| 6585 | |
| 6586 | done = 0; |
| 6587 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6588 | cur_ptr = req->buf->p; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6589 | cur_end = cur_ptr + txn->req.sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6590 | |
| 6591 | /* Now we have the request line between cur_ptr and cur_end */ |
| 6592 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 6593 | 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] | 6594 | switch (exp->action) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6595 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6596 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6597 | done = 1; |
| 6598 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 6599 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6600 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6601 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6602 | done = 1; |
| 6603 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 6604 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6605 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6606 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6607 | done = 1; |
| 6608 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 6609 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6610 | case ACT_REPLACE: |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 6611 | trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch); |
| 6612 | if (trash.len < 0) |
| 6613 | return -1; |
| 6614 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6615 | delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6616 | /* FIXME: if the user adds a newline in the replacement, the |
| 6617 | * index will not be recalculated for now, and the new line |
| 6618 | * will not be counted as a new header. |
| 6619 | */ |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 6620 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6621 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6622 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 6623 | cur_end = (char *)http_parse_reqline(&txn->req, |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6624 | HTTP_MSG_RQMETH, |
| 6625 | cur_ptr, cur_end + 1, |
| 6626 | NULL, NULL); |
| 6627 | if (unlikely(!cur_end)) |
| 6628 | return -1; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 6629 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6630 | /* we have a full request and we know that we have either a CR |
| 6631 | * or an LF at <ptr>. |
| 6632 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6633 | txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l); |
| 6634 | 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] | 6635 | /* there is no point trying this regex on headers */ |
| 6636 | return 1; |
| 6637 | } |
| 6638 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6639 | return done; |
| 6640 | } |
Willy Tarreau | 97de624 | 2006-12-27 17:18:38 +0100 | [diff] [blame] | 6641 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6642 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6643 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6644 | /* |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6645 | * 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] | 6646 | * 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] | 6647 | * unparsable request. Since it can manage the switch to another backend, it |
| 6648 | * updates the per-proxy DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6649 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6650 | 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] | 6651 | { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 6652 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 6653 | struct http_txn *txn = s->txn; |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6654 | struct hdr_exp *exp; |
| 6655 | |
| 6656 | for (exp = px->req_exp; exp; exp = exp->next) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6657 | int ret; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6658 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6659 | /* |
| 6660 | * The interleaving of transformations and verdicts |
| 6661 | * makes it difficult to decide to continue or stop |
| 6662 | * the evaluation. |
| 6663 | */ |
| 6664 | |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6665 | if (txn->flags & (TX_CLDENY|TX_CLTARPIT)) |
| 6666 | break; |
| 6667 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6668 | if ((txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6669 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6670 | exp->action == ACT_TARPIT || exp->action == ACT_PASS)) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6671 | continue; |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6672 | |
| 6673 | /* if this filter had a condition, evaluate it now and skip to |
| 6674 | * next filter if the condition does not match. |
| 6675 | */ |
| 6676 | if (exp->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 6677 | 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] | 6678 | ret = acl_pass(ret); |
| 6679 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 6680 | ret = !ret; |
| 6681 | |
| 6682 | if (!ret) |
| 6683 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6684 | } |
| 6685 | |
| 6686 | /* Apply the filter to the request line. */ |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6687 | ret = apply_filter_to_req_line(s, req, exp); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6688 | if (unlikely(ret < 0)) |
| 6689 | return -1; |
| 6690 | |
| 6691 | if (likely(ret == 0)) { |
| 6692 | /* The filter did not match the request, it can be |
| 6693 | * iterated through all headers. |
| 6694 | */ |
Willy Tarreau | 34d4c3c | 2015-01-30 20:58:58 +0100 | [diff] [blame] | 6695 | if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0)) |
| 6696 | return -1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6697 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6698 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6699 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6700 | } |
| 6701 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6702 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6703 | /* Find the end of a cookie value contained between <s> and <e>. It works the |
| 6704 | * same way as with headers above except that the semi-colon also ends a token. |
| 6705 | * See RFC2965 for more information. Note that it requires a valid header to |
| 6706 | * return a valid result. |
| 6707 | */ |
| 6708 | char *find_cookie_value_end(char *s, const char *e) |
| 6709 | { |
| 6710 | int quoted, qdpair; |
| 6711 | |
| 6712 | quoted = qdpair = 0; |
| 6713 | for (; s < e; s++) { |
| 6714 | if (qdpair) qdpair = 0; |
| 6715 | else if (quoted) { |
| 6716 | if (*s == '\\') qdpair = 1; |
| 6717 | else if (*s == '"') quoted = 0; |
| 6718 | } |
| 6719 | else if (*s == '"') quoted = 1; |
| 6720 | else if (*s == ',' || *s == ';') return s; |
| 6721 | } |
| 6722 | return s; |
| 6723 | } |
| 6724 | |
| 6725 | /* Delete a value in a header between delimiters <from> and <next> in buffer |
| 6726 | * <buf>. The number of characters displaced is returned, and the pointer to |
| 6727 | * the first delimiter is updated if required. The function tries as much as |
| 6728 | * possible to respect the following principles : |
| 6729 | * - replace <from> delimiter by the <next> one unless <from> points to a |
| 6730 | * colon, in which case <next> is simply removed |
| 6731 | * - set exactly one space character after the new first delimiter, unless |
| 6732 | * there are not enough characters in the block being moved to do so. |
| 6733 | * - remove unneeded spaces before the previous delimiter and after the new |
| 6734 | * one. |
| 6735 | * |
| 6736 | * It is the caller's responsibility to ensure that : |
| 6737 | * - <from> points to a valid delimiter or the colon ; |
| 6738 | * - <next> points to a valid delimiter or the final CR/LF ; |
| 6739 | * - there are non-space chars before <from> ; |
| 6740 | * - there is a CR/LF at or after <next>. |
| 6741 | */ |
Willy Tarreau | af81935 | 2012-08-27 22:08:00 +0200 | [diff] [blame] | 6742 | int del_hdr_value(struct buffer *buf, char **from, char *next) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6743 | { |
| 6744 | char *prev = *from; |
| 6745 | |
| 6746 | if (*prev == ':') { |
| 6747 | /* We're removing the first value, preserve the colon and add a |
| 6748 | * space if possible. |
| 6749 | */ |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 6750 | if (!HTTP_IS_CRLF(*next)) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6751 | next++; |
| 6752 | prev++; |
| 6753 | if (prev < next) |
| 6754 | *prev++ = ' '; |
| 6755 | |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 6756 | while (HTTP_IS_SPHT(*next)) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6757 | next++; |
| 6758 | } else { |
| 6759 | /* Remove useless spaces before the old delimiter. */ |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 6760 | while (HTTP_IS_SPHT(*(prev-1))) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6761 | prev--; |
| 6762 | *from = prev; |
| 6763 | |
| 6764 | /* copy the delimiter and if possible a space if we're |
| 6765 | * not at the end of the line. |
| 6766 | */ |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 6767 | if (!HTTP_IS_CRLF(*next)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6768 | *prev++ = *next++; |
| 6769 | if (prev + 1 < next) |
| 6770 | *prev++ = ' '; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 6771 | while (HTTP_IS_SPHT(*next)) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6772 | next++; |
| 6773 | } |
| 6774 | } |
| 6775 | return buffer_replace2(buf, prev, next, NULL, 0); |
| 6776 | } |
| 6777 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6778 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 6779 | * 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] | 6780 | * desirable to call it only when needed. This code is quite complex because |
| 6781 | * of the multiple very crappy and ambiguous syntaxes we have to support. it |
| 6782 | * highly recommended not to touch this part without a good reason ! |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6783 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6784 | void manage_client_side_cookies(struct stream *s, struct channel *req) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6785 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 6786 | struct http_txn *txn = s->txn; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6787 | struct session *sess = s->sess; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6788 | int preserve_hdr; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6789 | int cur_idx, old_idx; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6790 | char *hdr_beg, *hdr_end, *hdr_next, *del_from; |
| 6791 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6792 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6793 | /* Iterate through the headers, we start with the start line. */ |
Willy Tarreau | 83969f4 | 2007-01-22 08:55:47 +0100 | [diff] [blame] | 6794 | old_idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6795 | hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6796 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6797 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6798 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6799 | int val; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6800 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6801 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6802 | hdr_beg = hdr_next; |
| 6803 | hdr_end = hdr_beg + cur_hdr->len; |
| 6804 | hdr_next = hdr_end + cur_hdr->cr + 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6805 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6806 | /* We have one full header between hdr_beg and hdr_end, and the |
| 6807 | * next header starts at hdr_next. We're only interested in |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6808 | * "Cookie:" headers. |
| 6809 | */ |
| 6810 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6811 | val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6); |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6812 | if (!val) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6813 | old_idx = cur_idx; |
| 6814 | continue; |
| 6815 | } |
| 6816 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6817 | del_from = NULL; /* nothing to be deleted */ |
| 6818 | preserve_hdr = 0; /* assume we may kill the whole header */ |
| 6819 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6820 | /* Now look for cookies. Conforming to RFC2109, we have to support |
| 6821 | * attributes whose name begin with a '$', and associate them with |
| 6822 | * the right cookie, if we want to delete this cookie. |
| 6823 | * So there are 3 cases for each cookie read : |
| 6824 | * 1) it's a special attribute, beginning with a '$' : ignore it. |
| 6825 | * 2) it's a server id cookie that we *MAY* want to delete : save |
| 6826 | * some pointers on it (last semi-colon, beginning of cookie...) |
| 6827 | * 3) it's an application cookie : we *MAY* have to delete a previous |
| 6828 | * "special" cookie. |
| 6829 | * At the end of loop, if a "special" cookie remains, we may have to |
| 6830 | * remove it. If no application cookie persists in the header, we |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6831 | * *MUST* delete it. |
| 6832 | * |
| 6833 | * Note: RFC2965 is unclear about the processing of spaces around |
| 6834 | * the equal sign in the ATTR=VALUE form. A careful inspection of |
| 6835 | * the RFC explicitly allows spaces before it, and not within the |
| 6836 | * tokens (attrs or values). An inspection of RFC2109 allows that |
| 6837 | * too but section 10.1.3 lets one think that spaces may be allowed |
| 6838 | * after the equal sign too, resulting in some (rare) buggy |
| 6839 | * implementations trying to do that. So let's do what servers do. |
| 6840 | * Latest ietf draft forbids spaces all around. Also, earlier RFCs |
| 6841 | * allowed quoted strings in values, with any possible character |
| 6842 | * after a backslash, including control chars and delimitors, which |
| 6843 | * causes parsing to become ambiguous. Browsers also allow spaces |
| 6844 | * within values even without quotes. |
| 6845 | * |
| 6846 | * We have to keep multiple pointers in order to support cookie |
| 6847 | * removal at the beginning, middle or end of header without |
| 6848 | * corrupting the header. All of these headers are valid : |
| 6849 | * |
| 6850 | * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n |
| 6851 | * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n |
| 6852 | * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n |
| 6853 | * | | | | | | | | | |
| 6854 | * | | | | | | | | hdr_end <--+ |
| 6855 | * | | | | | | | +--> next |
| 6856 | * | | | | | | +----> val_end |
| 6857 | * | | | | | +-----------> val_beg |
| 6858 | * | | | | +--------------> equal |
| 6859 | * | | | +----------------> att_end |
| 6860 | * | | +---------------------> att_beg |
| 6861 | * | +--------------------------> prev |
| 6862 | * +--------------------------------> hdr_beg |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6863 | */ |
| 6864 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6865 | for (prev = hdr_beg + 6; prev < hdr_end; prev = next) { |
| 6866 | /* Iterate through all cookies on this line */ |
| 6867 | |
| 6868 | /* find att_beg */ |
| 6869 | att_beg = prev + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 6870 | while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg)) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6871 | att_beg++; |
| 6872 | |
| 6873 | /* find att_end : this is the first character after the last non |
| 6874 | * space before the equal. It may be equal to hdr_end. |
| 6875 | */ |
| 6876 | equal = att_end = att_beg; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6877 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6878 | while (equal < hdr_end) { |
| 6879 | if (*equal == '=' || *equal == ',' || *equal == ';') |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6880 | break; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 6881 | if (HTTP_IS_SPHT(*equal++)) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6882 | continue; |
| 6883 | att_end = equal; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6884 | } |
| 6885 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6886 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 6887 | * is between <att_beg> and <equal>, both may be identical. |
| 6888 | */ |
| 6889 | |
| 6890 | /* look for end of cookie if there is an equal sign */ |
| 6891 | if (equal < hdr_end && *equal == '=') { |
| 6892 | /* look for the beginning of the value */ |
| 6893 | val_beg = equal + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 6894 | while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg)) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6895 | val_beg++; |
| 6896 | |
| 6897 | /* find the end of the value, respecting quotes */ |
| 6898 | next = find_cookie_value_end(val_beg, hdr_end); |
| 6899 | |
| 6900 | /* make val_end point to the first white space or delimitor after the value */ |
| 6901 | val_end = next; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 6902 | while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1))) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6903 | val_end--; |
| 6904 | } else { |
| 6905 | val_beg = val_end = next = equal; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 6906 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6907 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6908 | /* We have nothing to do with attributes beginning with '$'. However, |
| 6909 | * they will automatically be removed if a header before them is removed, |
| 6910 | * since they're supposed to be linked together. |
| 6911 | */ |
| 6912 | if (*att_beg == '$') |
| 6913 | continue; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6914 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6915 | /* Ignore cookies with no equal sign */ |
| 6916 | if (equal == next) { |
| 6917 | /* This is not our cookie, so we must preserve it. But if we already |
| 6918 | * scheduled another cookie for removal, we cannot remove the |
| 6919 | * complete header, but we can remove the previous block itself. |
| 6920 | */ |
| 6921 | preserve_hdr = 1; |
| 6922 | if (del_from != NULL) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6923 | int delta = del_hdr_value(req->buf, &del_from, prev); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6924 | val_end += delta; |
| 6925 | next += delta; |
| 6926 | hdr_end += delta; |
| 6927 | hdr_next += delta; |
| 6928 | cur_hdr->len += delta; |
| 6929 | http_msg_move_end(&txn->req, delta); |
| 6930 | prev = del_from; |
| 6931 | del_from = NULL; |
| 6932 | } |
| 6933 | continue; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 6934 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6935 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6936 | /* if there are spaces around the equal sign, we need to |
| 6937 | * strip them otherwise we'll get trouble for cookie captures, |
| 6938 | * or even for rewrites. Since this happens extremely rarely, |
| 6939 | * it does not hurt performance. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6940 | */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6941 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 6942 | int stripped_before = 0; |
| 6943 | int stripped_after = 0; |
| 6944 | |
| 6945 | if (att_end != equal) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6946 | stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6947 | equal += stripped_before; |
| 6948 | val_beg += stripped_before; |
| 6949 | } |
| 6950 | |
| 6951 | if (val_beg > equal + 1) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6952 | stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6953 | val_beg += stripped_after; |
| 6954 | stripped_before += stripped_after; |
| 6955 | } |
| 6956 | |
| 6957 | val_end += stripped_before; |
| 6958 | next += stripped_before; |
| 6959 | hdr_end += stripped_before; |
| 6960 | hdr_next += stripped_before; |
| 6961 | cur_hdr->len += stripped_before; |
| 6962 | http_msg_move_end(&txn->req, stripped_before); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6963 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6964 | /* now everything is as on the diagram above */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6965 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6966 | /* First, let's see if we want to capture this cookie. We check |
| 6967 | * that we don't already have a client side cookie, because we |
| 6968 | * can only capture one. Also as an optimisation, we ignore |
| 6969 | * cookies shorter than the declared name. |
| 6970 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6971 | if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL && |
| 6972 | (val_end - att_beg >= sess->fe->capture_namelen) && |
| 6973 | memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6974 | int log_len = val_end - att_beg; |
| 6975 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6976 | if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 6977 | ha_alert("HTTP logging : out of memory.\n"); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6978 | } else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6979 | if (log_len > sess->fe->capture_len) |
| 6980 | log_len = sess->fe->capture_len; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6981 | memcpy(txn->cli_cookie, att_beg, log_len); |
| 6982 | txn->cli_cookie[log_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6983 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6984 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6985 | |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6986 | /* Persistence cookies in passive, rewrite or insert mode have the |
| 6987 | * following form : |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6988 | * |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6989 | * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]] |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6990 | * |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6991 | * For cookies in prefix mode, the form is : |
| 6992 | * |
| 6993 | * Cookie: NAME=SRV~VALUE |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6994 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6995 | if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) && |
| 6996 | (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) { |
| 6997 | struct server *srv = s->be->srv; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6998 | char *delim; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6999 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7000 | /* if we're in cookie prefix mode, we'll search the delimitor so that we |
| 7001 | * have the server ID between val_beg and delim, and the original cookie between |
| 7002 | * delim+1 and val_end. Otherwise, delim==val_end : |
| 7003 | * |
| 7004 | * Cookie: NAME=SRV; # in all but prefix modes |
| 7005 | * Cookie: NAME=SRV~OPAQUE ; # in prefix mode |
| 7006 | * | || || | |+-> next |
| 7007 | * | || || | +--> val_end |
| 7008 | * | || || +---------> delim |
| 7009 | * | || |+------------> val_beg |
| 7010 | * | || +-------------> att_end = equal |
| 7011 | * | |+-----------------> att_beg |
| 7012 | * | +------------------> prev |
| 7013 | * +-------------------------> hdr_beg |
| 7014 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7015 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7016 | if (s->be->ck_opts & PR_CK_PFX) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7017 | for (delim = val_beg; delim < val_end; delim++) |
| 7018 | if (*delim == COOKIE_DELIM) |
| 7019 | break; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7020 | } else { |
| 7021 | char *vbar1; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7022 | delim = val_end; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7023 | /* Now check if the cookie contains a date field, which would |
| 7024 | * appear after a vertical bar ('|') just after the server name |
| 7025 | * and before the delimiter. |
| 7026 | */ |
| 7027 | vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg); |
| 7028 | if (vbar1) { |
| 7029 | /* OK, so left of the bar is the server's cookie and |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7030 | * right is the last seen date. It is a base64 encoded |
| 7031 | * 30-bit value representing the UNIX date since the |
| 7032 | * epoch in 4-second quantities. |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7033 | */ |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7034 | int val; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7035 | delim = vbar1++; |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7036 | if (val_end - vbar1 >= 5) { |
| 7037 | val = b64tos30(vbar1); |
| 7038 | if (val > 0) |
| 7039 | txn->cookie_last_date = val << 2; |
| 7040 | } |
| 7041 | /* look for a second vertical bar */ |
| 7042 | vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1); |
| 7043 | if (vbar1 && (val_end - vbar1 > 5)) { |
| 7044 | val = b64tos30(vbar1 + 1); |
| 7045 | if (val > 0) |
| 7046 | txn->cookie_first_date = val << 2; |
| 7047 | } |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7048 | } |
| 7049 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7050 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7051 | /* if the cookie has an expiration date and the proxy wants to check |
| 7052 | * it, then we do that now. We first check if the cookie is too old, |
| 7053 | * then only if it has expired. We detect strict overflow because the |
| 7054 | * time resolution here is not great (4 seconds). Cookies with dates |
| 7055 | * in the future are ignored if their offset is beyond one day. This |
| 7056 | * allows an admin to fix timezone issues without expiring everyone |
| 7057 | * and at the same time avoids keeping unwanted side effects for too |
| 7058 | * long. |
| 7059 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7060 | if (txn->cookie_first_date && s->be->cookie_maxlife && |
| 7061 | (((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] | 7062 | ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) { |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7063 | txn->flags &= ~TX_CK_MASK; |
| 7064 | txn->flags |= TX_CK_OLD; |
| 7065 | delim = val_beg; // let's pretend we have not found the cookie |
| 7066 | txn->cookie_first_date = 0; |
| 7067 | txn->cookie_last_date = 0; |
| 7068 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7069 | else if (txn->cookie_last_date && s->be->cookie_maxidle && |
| 7070 | (((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] | 7071 | ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) { |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7072 | txn->flags &= ~TX_CK_MASK; |
| 7073 | txn->flags |= TX_CK_EXPIRED; |
| 7074 | delim = val_beg; // let's pretend we have not found the cookie |
| 7075 | txn->cookie_first_date = 0; |
| 7076 | txn->cookie_last_date = 0; |
| 7077 | } |
| 7078 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7079 | /* Here, we'll look for the first running server which supports the cookie. |
| 7080 | * This allows to share a same cookie between several servers, for example |
| 7081 | * to dedicate backup servers to specific servers only. |
| 7082 | * However, to prevent clients from sticking to cookie-less backup server |
| 7083 | * when they have incidentely learned an empty cookie, we simply ignore |
| 7084 | * empty cookies and mark them as invalid. |
| 7085 | * The same behaviour is applied when persistence must be ignored. |
| 7086 | */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 7087 | if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED))) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7088 | srv = NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7089 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7090 | while (srv) { |
| 7091 | if (srv->cookie && (srv->cklen == delim - val_beg) && |
| 7092 | !memcmp(val_beg, srv->cookie, delim - val_beg)) { |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 7093 | if ((srv->cur_state != SRV_ST_STOPPED) || |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7094 | (s->be->options & PR_O_PERSIST) || |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 7095 | (s->flags & SF_FORCE_PRST)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7096 | /* we found the server and we can use it */ |
| 7097 | txn->flags &= ~TX_CK_MASK; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 7098 | 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] | 7099 | s->flags |= SF_DIRECT | SF_ASSIGNED; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7100 | s->target = &srv->obj_type; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7101 | break; |
| 7102 | } else { |
| 7103 | /* we found a server, but it's down, |
| 7104 | * mark it as such and go on in case |
| 7105 | * another one is available. |
| 7106 | */ |
| 7107 | txn->flags &= ~TX_CK_MASK; |
| 7108 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7109 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7110 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7111 | srv = srv->next; |
| 7112 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7113 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7114 | 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] | 7115 | /* no server matched this cookie or we deliberately skipped it */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7116 | txn->flags &= ~TX_CK_MASK; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 7117 | if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED))) |
Willy Tarreau | c89ccb6 | 2012-04-05 21:18:22 +0200 | [diff] [blame] | 7118 | txn->flags |= TX_CK_UNUSED; |
| 7119 | else |
| 7120 | txn->flags |= TX_CK_INVALID; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7121 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7122 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7123 | /* depending on the cookie mode, we may have to either : |
| 7124 | * - delete the complete cookie if we're in insert+indirect mode, so that |
| 7125 | * the server never sees it ; |
| 7126 | * - remove the server id from the cookie value, and tag the cookie as an |
| 7127 | * application cookie so that it does not get accidentely removed later, |
| 7128 | * if we're in cookie prefix mode |
| 7129 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7130 | if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7131 | int delta; /* negative */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7132 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7133 | delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7134 | val_end += delta; |
| 7135 | next += delta; |
| 7136 | hdr_end += delta; |
| 7137 | hdr_next += delta; |
| 7138 | cur_hdr->len += delta; |
| 7139 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7140 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7141 | del_from = NULL; |
| 7142 | preserve_hdr = 1; /* we want to keep this cookie */ |
| 7143 | } |
| 7144 | else if (del_from == NULL && |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7145 | (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] | 7146 | del_from = prev; |
| 7147 | } |
| 7148 | } else { |
| 7149 | /* This is not our cookie, so we must preserve it. But if we already |
| 7150 | * scheduled another cookie for removal, we cannot remove the |
| 7151 | * complete header, but we can remove the previous block itself. |
| 7152 | */ |
| 7153 | preserve_hdr = 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7154 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7155 | if (del_from != NULL) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7156 | int delta = del_hdr_value(req->buf, &del_from, prev); |
Willy Tarreau | b810554 | 2010-11-24 18:31:28 +0100 | [diff] [blame] | 7157 | if (att_beg >= del_from) |
| 7158 | att_beg += delta; |
| 7159 | if (att_end >= del_from) |
| 7160 | att_end += delta; |
| 7161 | val_beg += delta; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7162 | val_end += delta; |
| 7163 | next += delta; |
| 7164 | hdr_end += delta; |
| 7165 | hdr_next += delta; |
| 7166 | cur_hdr->len += delta; |
| 7167 | http_msg_move_end(&txn->req, delta); |
| 7168 | prev = del_from; |
| 7169 | del_from = NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7170 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7171 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7172 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7173 | /* continue with next cookie on this header line */ |
| 7174 | att_beg = next; |
| 7175 | } /* for each cookie */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7176 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7177 | /* There are no more cookies on this line. |
| 7178 | * We may still have one (or several) marked for deletion at the |
| 7179 | * end of the line. We must do this now in two ways : |
| 7180 | * - if some cookies must be preserved, we only delete from the |
| 7181 | * mark to the end of line ; |
| 7182 | * - if nothing needs to be preserved, simply delete the whole header |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7183 | */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7184 | if (del_from) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7185 | int delta; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7186 | if (preserve_hdr) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7187 | delta = del_hdr_value(req->buf, &del_from, hdr_end); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7188 | hdr_end = del_from; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7189 | cur_hdr->len += delta; |
| 7190 | } else { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7191 | delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7192 | |
| 7193 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7194 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 7195 | txn->hdr_idx.used--; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7196 | cur_hdr->len = 0; |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 7197 | cur_idx = old_idx; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7198 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7199 | hdr_next += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7200 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7201 | } |
| 7202 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7203 | /* check next header */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7204 | old_idx = cur_idx; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7205 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7206 | } |
| 7207 | |
| 7208 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7209 | /* Iterate the same filter through all response headers contained in <rtr>. |
| 7210 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
| 7211 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7212 | 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] | 7213 | { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7214 | char *cur_ptr, *cur_end, *cur_next; |
| 7215 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7216 | struct http_txn *txn = s->txn; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7217 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7218 | int delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7219 | |
| 7220 | last_hdr = 0; |
| 7221 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7222 | cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7223 | old_idx = 0; |
| 7224 | |
| 7225 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7226 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7227 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7228 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7229 | (exp->action == ACT_ALLOW || |
| 7230 | exp->action == ACT_DENY)) |
| 7231 | return 0; |
| 7232 | |
| 7233 | cur_idx = txn->hdr_idx.v[old_idx].next; |
| 7234 | if (!cur_idx) |
| 7235 | break; |
| 7236 | |
| 7237 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 7238 | cur_ptr = cur_next; |
| 7239 | cur_end = cur_ptr + cur_hdr->len; |
| 7240 | cur_next = cur_end + cur_hdr->cr + 1; |
| 7241 | |
| 7242 | /* Now we have one header between cur_ptr and cur_end, |
| 7243 | * and the next header starts at cur_next. |
| 7244 | */ |
| 7245 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 7246 | 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] | 7247 | switch (exp->action) { |
| 7248 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7249 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7250 | last_hdr = 1; |
| 7251 | break; |
| 7252 | |
| 7253 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7254 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7255 | last_hdr = 1; |
| 7256 | break; |
| 7257 | |
| 7258 | case ACT_REPLACE: |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 7259 | trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch); |
| 7260 | if (trash.len < 0) |
| 7261 | return -1; |
| 7262 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7263 | delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7264 | /* FIXME: if the user adds a newline in the replacement, the |
| 7265 | * index will not be recalculated for now, and the new line |
| 7266 | * will not be counted as a new header. |
| 7267 | */ |
| 7268 | |
| 7269 | cur_end += delta; |
| 7270 | cur_next += delta; |
| 7271 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7272 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7273 | break; |
| 7274 | |
| 7275 | case ACT_REMOVE: |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7276 | delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7277 | cur_next += delta; |
| 7278 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7279 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7280 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 7281 | txn->hdr_idx.used--; |
| 7282 | cur_hdr->len = 0; |
| 7283 | cur_end = NULL; /* null-term has been rewritten */ |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 7284 | cur_idx = old_idx; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7285 | break; |
| 7286 | |
| 7287 | } |
| 7288 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7289 | |
| 7290 | /* keep the link from this header to next one in case of later |
| 7291 | * removal of next header. |
| 7292 | */ |
| 7293 | old_idx = cur_idx; |
| 7294 | } |
| 7295 | return 0; |
| 7296 | } |
| 7297 | |
| 7298 | |
| 7299 | /* Apply the filter to the status line in the response buffer <rtr>. |
| 7300 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 7301 | * or -1 if a replacement resulted in an invalid status line. |
| 7302 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7303 | 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] | 7304 | { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7305 | char *cur_ptr, *cur_end; |
| 7306 | int done; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7307 | struct http_txn *txn = s->txn; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7308 | int delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7309 | |
| 7310 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7311 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7312 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7313 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7314 | (exp->action == ACT_ALLOW || |
| 7315 | exp->action == ACT_DENY)) |
| 7316 | return 0; |
| 7317 | else if (exp->action == ACT_REMOVE) |
| 7318 | return 0; |
| 7319 | |
| 7320 | done = 0; |
| 7321 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7322 | cur_ptr = rtr->buf->p; |
Willy Tarreau | 1ba0e5f | 2010-06-07 13:57:32 +0200 | [diff] [blame] | 7323 | cur_end = cur_ptr + txn->rsp.sl.st.l; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7324 | |
| 7325 | /* Now we have the status line between cur_ptr and cur_end */ |
| 7326 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 7327 | 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] | 7328 | switch (exp->action) { |
| 7329 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7330 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7331 | done = 1; |
| 7332 | break; |
| 7333 | |
| 7334 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7335 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7336 | done = 1; |
| 7337 | break; |
| 7338 | |
| 7339 | case ACT_REPLACE: |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 7340 | trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch); |
| 7341 | if (trash.len < 0) |
| 7342 | return -1; |
| 7343 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7344 | delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7345 | /* FIXME: if the user adds a newline in the replacement, the |
| 7346 | * index will not be recalculated for now, and the new line |
| 7347 | * will not be counted as a new header. |
| 7348 | */ |
| 7349 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7350 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7351 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 7352 | cur_end = (char *)http_parse_stsline(&txn->rsp, |
Willy Tarreau | 0278576 | 2007-04-03 14:45:44 +0200 | [diff] [blame] | 7353 | HTTP_MSG_RPVER, |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7354 | cur_ptr, cur_end + 1, |
| 7355 | NULL, NULL); |
| 7356 | if (unlikely(!cur_end)) |
| 7357 | return -1; |
| 7358 | |
| 7359 | /* we have a full respnse and we know that we have either a CR |
| 7360 | * or an LF at <ptr>. |
| 7361 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7362 | txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l); |
Willy Tarreau | 1ba0e5f | 2010-06-07 13:57:32 +0200 | [diff] [blame] | 7363 | 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] | 7364 | /* there is no point trying this regex on headers */ |
| 7365 | return 1; |
| 7366 | } |
| 7367 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7368 | return done; |
| 7369 | } |
| 7370 | |
| 7371 | |
| 7372 | |
| 7373 | /* |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7374 | * 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] | 7375 | * Returns 0 if everything is alright, or -1 in case a replacement lead to an |
| 7376 | * unparsable response. |
| 7377 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7378 | 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] | 7379 | { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 7380 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7381 | struct http_txn *txn = s->txn; |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7382 | struct hdr_exp *exp; |
| 7383 | |
| 7384 | for (exp = px->rsp_exp; exp; exp = exp->next) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7385 | int ret; |
| 7386 | |
| 7387 | /* |
| 7388 | * The interleaving of transformations and verdicts |
| 7389 | * makes it difficult to decide to continue or stop |
| 7390 | * the evaluation. |
| 7391 | */ |
| 7392 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7393 | if (txn->flags & TX_SVDENY) |
| 7394 | break; |
| 7395 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7396 | if ((txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7397 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 7398 | exp->action == ACT_PASS)) { |
| 7399 | exp = exp->next; |
| 7400 | continue; |
| 7401 | } |
| 7402 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7403 | /* if this filter had a condition, evaluate it now and skip to |
| 7404 | * next filter if the condition does not match. |
| 7405 | */ |
| 7406 | if (exp->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 7407 | 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] | 7408 | ret = acl_pass(ret); |
| 7409 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 7410 | ret = !ret; |
| 7411 | if (!ret) |
| 7412 | continue; |
| 7413 | } |
| 7414 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7415 | /* Apply the filter to the status line. */ |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7416 | ret = apply_filter_to_sts_line(s, rtr, exp); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7417 | if (unlikely(ret < 0)) |
| 7418 | return -1; |
| 7419 | |
| 7420 | if (likely(ret == 0)) { |
| 7421 | /* The filter did not match the response, it can be |
| 7422 | * iterated through all headers. |
| 7423 | */ |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 7424 | if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0)) |
| 7425 | return -1; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7426 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7427 | } |
| 7428 | return 0; |
| 7429 | } |
| 7430 | |
| 7431 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7432 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 7433 | * 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] | 7434 | * desirable to call it only when needed. This function is also used when we |
| 7435 | * 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] | 7436 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7437 | void manage_server_side_cookies(struct stream *s, struct channel *res) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7438 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7439 | struct http_txn *txn = s->txn; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7440 | struct session *sess = s->sess; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 7441 | struct server *srv; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7442 | int is_cookie2; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7443 | int cur_idx, old_idx, delta; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7444 | char *hdr_beg, *hdr_end, *hdr_next; |
| 7445 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7446 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7447 | /* Iterate through the headers. |
| 7448 | * we start with the start line. |
| 7449 | */ |
| 7450 | old_idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7451 | hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7452 | |
| 7453 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 7454 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7455 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7456 | |
| 7457 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7458 | hdr_beg = hdr_next; |
| 7459 | hdr_end = hdr_beg + cur_hdr->len; |
| 7460 | hdr_next = hdr_end + cur_hdr->cr + 1; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7461 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7462 | /* We have one full header between hdr_beg and hdr_end, and the |
| 7463 | * next header starts at hdr_next. We're only interested in |
| 7464 | * "Set-Cookie" and "Set-Cookie2" headers. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7465 | */ |
| 7466 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7467 | is_cookie2 = 0; |
| 7468 | prev = hdr_beg + 10; |
| 7469 | val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10); |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7470 | if (!val) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7471 | val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11); |
| 7472 | if (!val) { |
| 7473 | old_idx = cur_idx; |
| 7474 | continue; |
| 7475 | } |
| 7476 | is_cookie2 = 1; |
| 7477 | prev = hdr_beg + 11; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7478 | } |
| 7479 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7480 | /* OK, right now we know we have a Set-Cookie* at hdr_beg, and |
| 7481 | * <prev> points to the colon. |
| 7482 | */ |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7483 | txn->flags |= TX_SCK_PRESENT; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7484 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7485 | /* Maybe we only wanted to see if there was a Set-Cookie (eg: |
| 7486 | * check-cache is enabled) and we are not interested in checking |
| 7487 | * them. Warning, the cookie capture is declared in the frontend. |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 7488 | */ |
Willy Tarreau | 53a09d5 | 2015-08-10 18:59:40 +0200 | [diff] [blame] | 7489 | if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7490 | return; |
| 7491 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7492 | /* OK so now we know we have to process this response cookie. |
| 7493 | * The format of the Set-Cookie header is slightly different |
| 7494 | * from the format of the Cookie header in that it does not |
| 7495 | * support the comma as a cookie delimiter (thus the header |
| 7496 | * cannot be folded) because the Expires attribute described in |
| 7497 | * the original Netscape's spec may contain an unquoted date |
| 7498 | * with a comma inside. We have to live with this because |
| 7499 | * many browsers don't support Max-Age and some browsers don't |
| 7500 | * support quoted strings. However the Set-Cookie2 header is |
| 7501 | * clean. |
| 7502 | * |
| 7503 | * We have to keep multiple pointers in order to support cookie |
| 7504 | * removal at the beginning, middle or end of header without |
| 7505 | * corrupting the header (in case of set-cookie2). A special |
| 7506 | * pointer, <scav> points to the beginning of the set-cookie-av |
| 7507 | * fields after the first semi-colon. The <next> pointer points |
| 7508 | * either to the end of line (set-cookie) or next unquoted comma |
| 7509 | * (set-cookie2). All of these headers are valid : |
| 7510 | * |
| 7511 | * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n |
| 7512 | * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n |
| 7513 | * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n |
| 7514 | * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n |
| 7515 | * | | | | | | | | | | |
| 7516 | * | | | | | | | | +-> next hdr_end <--+ |
| 7517 | * | | | | | | | +------------> scav |
| 7518 | * | | | | | | +--------------> val_end |
| 7519 | * | | | | | +--------------------> val_beg |
| 7520 | * | | | | +----------------------> equal |
| 7521 | * | | | +------------------------> att_end |
| 7522 | * | | +----------------------------> att_beg |
| 7523 | * | +------------------------------> prev |
| 7524 | * +-----------------------------------------> hdr_beg |
| 7525 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7526 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7527 | for (; prev < hdr_end; prev = next) { |
| 7528 | /* Iterate through all cookies on this line */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7529 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7530 | /* find att_beg */ |
| 7531 | att_beg = prev + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 7532 | while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg)) |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7533 | att_beg++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7534 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7535 | /* find att_end : this is the first character after the last non |
| 7536 | * space before the equal. It may be equal to hdr_end. |
| 7537 | */ |
| 7538 | equal = att_end = att_beg; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7539 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7540 | while (equal < hdr_end) { |
| 7541 | if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ',')) |
| 7542 | break; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 7543 | if (HTTP_IS_SPHT(*equal++)) |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7544 | continue; |
| 7545 | att_end = equal; |
| 7546 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7547 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7548 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 7549 | * is between <att_beg> and <equal>, both may be identical. |
| 7550 | */ |
| 7551 | |
| 7552 | /* look for end of cookie if there is an equal sign */ |
| 7553 | if (equal < hdr_end && *equal == '=') { |
| 7554 | /* look for the beginning of the value */ |
| 7555 | val_beg = equal + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 7556 | while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg)) |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7557 | val_beg++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7558 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7559 | /* find the end of the value, respecting quotes */ |
| 7560 | next = find_cookie_value_end(val_beg, hdr_end); |
| 7561 | |
| 7562 | /* make val_end point to the first white space or delimitor after the value */ |
| 7563 | val_end = next; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 7564 | while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1))) |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7565 | val_end--; |
| 7566 | } else { |
| 7567 | /* <equal> points to next comma, semi-colon or EOL */ |
| 7568 | val_beg = val_end = next = equal; |
| 7569 | } |
| 7570 | |
| 7571 | if (next < hdr_end) { |
| 7572 | /* Set-Cookie2 supports multiple cookies, and <next> points to |
| 7573 | * a colon or semi-colon before the end. So skip all attr-value |
| 7574 | * pairs and look for the next comma. For Set-Cookie, since |
| 7575 | * commas are permitted in values, skip to the end. |
| 7576 | */ |
| 7577 | if (is_cookie2) |
| 7578 | next = find_hdr_value_end(next, hdr_end); |
| 7579 | else |
| 7580 | next = hdr_end; |
| 7581 | } |
| 7582 | |
| 7583 | /* Now everything is as on the diagram above */ |
| 7584 | |
| 7585 | /* Ignore cookies with no equal sign */ |
| 7586 | if (equal == val_end) |
| 7587 | continue; |
| 7588 | |
| 7589 | /* If there are spaces around the equal sign, we need to |
| 7590 | * strip them otherwise we'll get trouble for cookie captures, |
| 7591 | * or even for rewrites. Since this happens extremely rarely, |
| 7592 | * it does not hurt performance. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7593 | */ |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7594 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 7595 | int stripped_before = 0; |
| 7596 | int stripped_after = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7597 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7598 | if (att_end != equal) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7599 | stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7600 | equal += stripped_before; |
| 7601 | val_beg += stripped_before; |
| 7602 | } |
| 7603 | |
| 7604 | if (val_beg > equal + 1) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7605 | stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7606 | val_beg += stripped_after; |
| 7607 | stripped_before += stripped_after; |
| 7608 | } |
| 7609 | |
| 7610 | val_end += stripped_before; |
| 7611 | next += stripped_before; |
| 7612 | hdr_end += stripped_before; |
| 7613 | hdr_next += stripped_before; |
| 7614 | cur_hdr->len += stripped_before; |
Willy Tarreau | 1fc1f45 | 2011-04-07 22:35:37 +0200 | [diff] [blame] | 7615 | http_msg_move_end(&txn->rsp, stripped_before); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7616 | } |
| 7617 | |
| 7618 | /* First, let's see if we want to capture this cookie. We check |
| 7619 | * that we don't already have a server side cookie, because we |
| 7620 | * can only capture one. Also as an optimisation, we ignore |
| 7621 | * cookies shorter than the declared name. |
| 7622 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7623 | if (sess->fe->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 7624 | txn->srv_cookie == NULL && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7625 | (val_end - att_beg >= sess->fe->capture_namelen) && |
| 7626 | memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7627 | int log_len = val_end - att_beg; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7628 | if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 7629 | ha_alert("HTTP logging : out of memory.\n"); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7630 | } |
Willy Tarreau | f70fc75 | 2010-11-19 11:27:18 +0100 | [diff] [blame] | 7631 | else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7632 | if (log_len > sess->fe->capture_len) |
| 7633 | log_len = sess->fe->capture_len; |
Willy Tarreau | f70fc75 | 2010-11-19 11:27:18 +0100 | [diff] [blame] | 7634 | memcpy(txn->srv_cookie, att_beg, log_len); |
| 7635 | txn->srv_cookie[log_len] = 0; |
| 7636 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7637 | } |
| 7638 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7639 | srv = objt_server(s->target); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7640 | /* now check if we need to process it for persistence */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 7641 | if (!(s->flags & SF_IGNORE_PRST) && |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7642 | (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) && |
| 7643 | (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) { |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7644 | /* assume passive cookie by default */ |
| 7645 | txn->flags &= ~TX_SCK_MASK; |
| 7646 | txn->flags |= TX_SCK_FOUND; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7647 | |
| 7648 | /* If the cookie is in insert mode on a known server, we'll delete |
| 7649 | * this occurrence because we'll insert another one later. |
| 7650 | * 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] | 7651 | * a direct access. |
| 7652 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7653 | if (s->be->ck_opts & PR_CK_PSV) { |
Willy Tarreau | ba4c5be | 2010-10-23 12:46:42 +0200 | [diff] [blame] | 7654 | /* The "preserve" flag was set, we don't want to touch the |
| 7655 | * server's cookie. |
| 7656 | */ |
| 7657 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7658 | else if ((srv && (s->be->ck_opts & PR_CK_INS)) || |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 7659 | ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7660 | /* this cookie must be deleted */ |
| 7661 | if (*prev == ':' && next == hdr_end) { |
| 7662 | /* whole header */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7663 | delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7664 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 7665 | txn->hdr_idx.used--; |
| 7666 | cur_hdr->len = 0; |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 7667 | cur_idx = old_idx; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7668 | hdr_next += delta; |
| 7669 | http_msg_move_end(&txn->rsp, delta); |
| 7670 | /* note: while both invalid now, <next> and <hdr_end> |
| 7671 | * are still equal, so the for() will stop as expected. |
| 7672 | */ |
| 7673 | } else { |
| 7674 | /* just remove the value */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7675 | int delta = del_hdr_value(res->buf, &prev, next); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7676 | next = prev; |
| 7677 | hdr_end += delta; |
| 7678 | hdr_next += delta; |
| 7679 | cur_hdr->len += delta; |
| 7680 | http_msg_move_end(&txn->rsp, delta); |
| 7681 | } |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7682 | txn->flags &= ~TX_SCK_MASK; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7683 | txn->flags |= TX_SCK_DELETED; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7684 | /* and go on with next cookie */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7685 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7686 | else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7687 | /* replace bytes val_beg->val_end with the cookie name associated |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7688 | * with this server since we know it. |
| 7689 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7690 | delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7691 | next += delta; |
| 7692 | hdr_end += delta; |
| 7693 | hdr_next += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7694 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7695 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7696 | |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7697 | txn->flags &= ~TX_SCK_MASK; |
| 7698 | txn->flags |= TX_SCK_REPLACED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7699 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7700 | else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7701 | /* insert the cookie name associated with this server |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7702 | * before existing cookie, and insert a delimiter between them.. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7703 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7704 | delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7705 | next += delta; |
| 7706 | hdr_end += delta; |
| 7707 | hdr_next += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7708 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7709 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7710 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 7711 | val_beg[srv->cklen] = COOKIE_DELIM; |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7712 | txn->flags &= ~TX_SCK_MASK; |
| 7713 | txn->flags |= TX_SCK_REPLACED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7714 | } |
| 7715 | } |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7716 | /* that's done for this cookie, check the next one on the same |
| 7717 | * line when next != hdr_end (only if is_cookie2). |
| 7718 | */ |
| 7719 | } |
| 7720 | /* check next header */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7721 | old_idx = cur_idx; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7722 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7723 | } |
| 7724 | |
| 7725 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7726 | /* |
Willy Tarreau | 0ad8e0d | 2017-12-22 15:03:36 +0100 | [diff] [blame] | 7727 | * Parses the Cache-Control and Pragma request header fields to determine if |
| 7728 | * the request may be served from the cache and/or if it is cacheable. Updates |
| 7729 | * s->txn->flags. |
| 7730 | */ |
| 7731 | void check_request_for_cacheability(struct stream *s, struct channel *chn) |
| 7732 | { |
| 7733 | struct http_txn *txn = s->txn; |
| 7734 | char *p1, *p2; |
| 7735 | char *cur_ptr, *cur_end, *cur_next; |
| 7736 | int pragma_found; |
| 7737 | int cc_found; |
| 7738 | int cur_idx; |
| 7739 | |
| 7740 | if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE) |
| 7741 | return; /* nothing more to do here */ |
| 7742 | |
| 7743 | cur_idx = 0; |
| 7744 | pragma_found = cc_found = 0; |
| 7745 | cur_next = chn->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
| 7746 | |
| 7747 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 7748 | struct hdr_idx_elem *cur_hdr; |
| 7749 | int val; |
| 7750 | |
| 7751 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 7752 | cur_ptr = cur_next; |
| 7753 | cur_end = cur_ptr + cur_hdr->len; |
| 7754 | cur_next = cur_end + cur_hdr->cr + 1; |
| 7755 | |
| 7756 | /* We have one full header between cur_ptr and cur_end, and the |
| 7757 | * next header starts at cur_next. |
| 7758 | */ |
| 7759 | |
| 7760 | val = http_header_match2(cur_ptr, cur_end, "Pragma", 6); |
| 7761 | if (val) { |
| 7762 | if ((cur_end - (cur_ptr + val) >= 8) && |
| 7763 | strncasecmp(cur_ptr + val, "no-cache", 8) == 0) { |
| 7764 | pragma_found = 1; |
| 7765 | continue; |
| 7766 | } |
| 7767 | } |
| 7768 | |
William Lallemand | 8a16fe0 | 2018-05-22 11:04:33 +0200 | [diff] [blame] | 7769 | /* Don't use the cache and don't try to store if we found the |
| 7770 | * Authorization header */ |
| 7771 | val = http_header_match2(cur_ptr, cur_end, "Authorization", 13); |
| 7772 | if (val) { |
| 7773 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 7774 | txn->flags |= TX_CACHE_IGNORE; |
| 7775 | continue; |
| 7776 | } |
| 7777 | |
Willy Tarreau | 0ad8e0d | 2017-12-22 15:03:36 +0100 | [diff] [blame] | 7778 | val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13); |
| 7779 | if (!val) |
| 7780 | continue; |
| 7781 | |
| 7782 | /* OK, right now we know we have a cache-control header at cur_ptr */ |
| 7783 | cc_found = 1; |
| 7784 | p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */ |
| 7785 | |
| 7786 | if (p1 >= cur_end) /* no more info */ |
| 7787 | continue; |
| 7788 | |
| 7789 | /* p1 is at the beginning of the value */ |
| 7790 | p2 = p1; |
| 7791 | while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2)) |
| 7792 | p2++; |
| 7793 | |
| 7794 | /* we have a complete value between p1 and p2. We don't check the |
| 7795 | * values after max-age, max-stale nor min-fresh, we simply don't |
| 7796 | * use the cache when they're specified. |
| 7797 | */ |
| 7798 | if (((p2 - p1 == 7) && strncasecmp(p1, "max-age", 7) == 0) || |
| 7799 | ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) || |
| 7800 | ((p2 - p1 == 9) && strncasecmp(p1, "max-stale", 9) == 0) || |
| 7801 | ((p2 - p1 == 9) && strncasecmp(p1, "min-fresh", 9) == 0)) { |
| 7802 | txn->flags |= TX_CACHE_IGNORE; |
| 7803 | continue; |
| 7804 | } |
| 7805 | |
| 7806 | if ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) { |
| 7807 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 7808 | continue; |
| 7809 | } |
| 7810 | } |
| 7811 | |
| 7812 | /* RFC7234#5.4: |
| 7813 | * When the Cache-Control header field is also present and |
| 7814 | * understood in a request, Pragma is ignored. |
| 7815 | * When the Cache-Control header field is not present in a |
| 7816 | * request, caches MUST consider the no-cache request |
| 7817 | * pragma-directive as having the same effect as if |
| 7818 | * "Cache-Control: no-cache" were present. |
| 7819 | */ |
| 7820 | if (!cc_found && pragma_found) |
| 7821 | txn->flags |= TX_CACHE_IGNORE; |
| 7822 | } |
| 7823 | |
| 7824 | /* |
Willy Tarreau | d3900cc | 2017-12-22 15:35:11 +0100 | [diff] [blame] | 7825 | * Check if response is cacheable or not. Updates s->txn->flags. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7826 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7827 | void check_response_for_cacheability(struct stream *s, struct channel *rtr) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7828 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7829 | struct http_txn *txn = s->txn; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7830 | char *p1, *p2; |
| 7831 | |
| 7832 | char *cur_ptr, *cur_end, *cur_next; |
| 7833 | int cur_idx; |
| 7834 | |
Willy Tarreau | 12b32f2 | 2017-12-21 16:08:09 +0100 | [diff] [blame] | 7835 | if (txn->status < 200) { |
| 7836 | /* do not try to cache interim responses! */ |
| 7837 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7838 | return; |
Willy Tarreau | 12b32f2 | 2017-12-21 16:08:09 +0100 | [diff] [blame] | 7839 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7840 | |
| 7841 | /* Iterate through the headers. |
| 7842 | * we start with the start line. |
| 7843 | */ |
| 7844 | cur_idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7845 | cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7846 | |
| 7847 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 7848 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7849 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7850 | |
| 7851 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 7852 | cur_ptr = cur_next; |
| 7853 | cur_end = cur_ptr + cur_hdr->len; |
| 7854 | cur_next = cur_end + cur_hdr->cr + 1; |
| 7855 | |
| 7856 | /* 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] | 7857 | * next header starts at cur_next. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7858 | */ |
| 7859 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7860 | val = http_header_match2(cur_ptr, cur_end, "Pragma", 6); |
| 7861 | if (val) { |
| 7862 | if ((cur_end - (cur_ptr + val) >= 8) && |
| 7863 | strncasecmp(cur_ptr + val, "no-cache", 8) == 0) { |
| 7864 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 7865 | return; |
| 7866 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7867 | } |
| 7868 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7869 | val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13); |
| 7870 | if (!val) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7871 | continue; |
| 7872 | |
| 7873 | /* OK, right now we know we have a cache-control header at cur_ptr */ |
| 7874 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7875 | p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7876 | |
| 7877 | if (p1 >= cur_end) /* no more info */ |
| 7878 | continue; |
| 7879 | |
| 7880 | /* p1 is at the beginning of the value */ |
| 7881 | p2 = p1; |
| 7882 | |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 7883 | while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7884 | p2++; |
| 7885 | |
| 7886 | /* we have a complete value between p1 and p2 */ |
| 7887 | if (p2 < cur_end && *p2 == '=') { |
Willy Tarreau | d3900cc | 2017-12-22 15:35:11 +0100 | [diff] [blame] | 7888 | if (((cur_end - p2) > 1 && (p2 - p1 == 7) && strncasecmp(p1, "max-age=0", 9) == 0) || |
| 7889 | ((cur_end - p2) > 1 && (p2 - p1 == 8) && strncasecmp(p1, "s-maxage=0", 10) == 0)) { |
| 7890 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 7891 | continue; |
| 7892 | } |
| 7893 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7894 | /* we have something of the form no-cache="set-cookie" */ |
| 7895 | if ((cur_end - p1 >= 21) && |
| 7896 | strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0 |
| 7897 | && (p1[20] == '"' || p1[20] == ',')) |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7898 | txn->flags &= ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7899 | continue; |
| 7900 | } |
| 7901 | |
| 7902 | /* OK, so we know that either p2 points to the end of string or to a comma */ |
| 7903 | if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) || |
Willy Tarreau | 5b15f90 | 2013-07-04 12:46:56 +0200 | [diff] [blame] | 7904 | ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) || |
Willy Tarreau | d3900cc | 2017-12-22 15:35:11 +0100 | [diff] [blame] | 7905 | ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0)) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7906 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7907 | return; |
| 7908 | } |
| 7909 | |
| 7910 | if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7911 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7912 | continue; |
| 7913 | } |
| 7914 | } |
| 7915 | } |
| 7916 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7917 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7918 | /* |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7919 | * 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] | 7920 | * for the current backend. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7921 | * |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7922 | * 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] | 7923 | * uri_auth field is valid. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7924 | * |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 7925 | * Returns 1 if stats should be provided, otherwise 0. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7926 | */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7927 | 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] | 7928 | { |
| 7929 | struct uri_auth *uri_auth = backend->uri_auth; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7930 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7931 | const char *uri = msg->chn->buf->p+ msg->sl.rq.u; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7932 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 7933 | if (!uri_auth) |
| 7934 | return 0; |
| 7935 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7936 | 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] | 7937 | return 0; |
| 7938 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7939 | /* check URI size */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7940 | if (uri_auth->uri_len > msg->sl.rq.u_l) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7941 | return 0; |
| 7942 | |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 7943 | if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7944 | return 0; |
| 7945 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7946 | return 1; |
| 7947 | } |
| 7948 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7949 | /* |
| 7950 | * 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] | 7951 | * By default it tries to report the error position as msg->err_pos. However if |
| 7952 | * this one is not set, it will then report msg->next, which is the last known |
| 7953 | * parsing point. The function is able to deal with wrapping buffers. It always |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 7954 | * displays buffers as a contiguous area starting at buf->p. |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7955 | */ |
Emeric Brun | 8c1aaa2 | 2017-06-15 11:30:06 +0200 | [diff] [blame] | 7956 | void http_capture_bad_message(struct proxy *proxy, struct error_snapshot *es, struct stream *s, |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 7957 | struct http_msg *msg, |
Willy Tarreau | 0da5b3b | 2017-09-21 09:30:46 +0200 | [diff] [blame] | 7958 | enum h1_state state, struct proxy *other_end) |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7959 | { |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 7960 | struct session *sess = strm_sess(s); |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 7961 | struct channel *chn = msg->chn; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 7962 | int len1, len2; |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 7963 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 7964 | HA_SPIN_LOCK(PROXY_LOCK, &proxy->lock); |
Willy Tarreau | f3764b7 | 2016-03-31 13:45:10 +0200 | [diff] [blame] | 7965 | es->len = MIN(chn->buf->i, global.tune.bufsize); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7966 | len1 = chn->buf->data + chn->buf->size - chn->buf->p; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 7967 | len1 = MIN(len1, es->len); |
| 7968 | len2 = es->len - len1; /* remaining data if buffer wraps */ |
| 7969 | |
Willy Tarreau | f3764b7 | 2016-03-31 13:45:10 +0200 | [diff] [blame] | 7970 | if (!es->buf) |
| 7971 | es->buf = malloc(global.tune.bufsize); |
| 7972 | |
| 7973 | if (es->buf) { |
| 7974 | memcpy(es->buf, chn->buf->p, len1); |
| 7975 | if (len2) |
| 7976 | memcpy(es->buf + len1, chn->buf->data, len2); |
| 7977 | } |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 7978 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7979 | if (msg->err_pos >= 0) |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 7980 | es->pos = msg->err_pos; |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 7981 | else |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 7982 | es->pos = msg->next; |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 7983 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7984 | es->when = date; // user-visible date |
| 7985 | es->sid = s->uniq_id; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 7986 | es->srv = objt_server(s->target); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7987 | es->oe = other_end; |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 7988 | if (objt_conn(sess->origin)) |
| 7989 | es->src = __objt_conn(sess->origin)->addr.from; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7990 | else |
| 7991 | memset(&es->src, 0, sizeof(es->src)); |
| 7992 | |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 7993 | es->state = state; |
Willy Tarreau | 10479e4 | 2010-12-12 14:00:34 +0100 | [diff] [blame] | 7994 | es->ev_id = error_snapshot_id++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 7995 | es->b_flags = chn->flags; |
Willy Tarreau | d04b1bc | 2012-05-08 11:03:10 +0200 | [diff] [blame] | 7996 | es->s_flags = s->flags; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7997 | es->t_flags = s->txn->flags; |
Willy Tarreau | d04b1bc | 2012-05-08 11:03:10 +0200 | [diff] [blame] | 7998 | es->m_flags = msg->flags; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7999 | es->b_out = chn->buf->o; |
| 8000 | es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 8001 | es->b_tot = chn->total; |
Willy Tarreau | d04b1bc | 2012-05-08 11:03:10 +0200 | [diff] [blame] | 8002 | es->m_clen = msg->chunk_len; |
| 8003 | es->m_blen = msg->body_len; |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 8004 | HA_SPIN_UNLOCK(PROXY_LOCK, &proxy->lock); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8005 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8006 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8007 | /* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of |
| 8008 | * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is |
| 8009 | * performed over the whole headers. Otherwise it must contain a valid header |
| 8010 | * context, initialised with ctx->idx=0 for the first lookup in a series. If |
| 8011 | * <occ> is positive or null, occurrence #occ from the beginning (or last ctx) |
| 8012 | * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less |
| 8013 | * 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] | 8014 | * -1. The value fetch stops at commas, so this function is suited for use with |
| 8015 | * list headers. |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8016 | * 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] | 8017 | */ |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 8018 | 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] | 8019 | struct hdr_idx *idx, int occ, |
| 8020 | struct hdr_ctx *ctx, char **vptr, int *vlen) |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8021 | { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8022 | struct hdr_ctx local_ctx; |
| 8023 | char *ptr_hist[MAX_HDR_HISTORY]; |
| 8024 | int len_hist[MAX_HDR_HISTORY]; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8025 | unsigned int hist_ptr; |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8026 | int found; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8027 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8028 | if (!ctx) { |
| 8029 | local_ctx.idx = 0; |
| 8030 | ctx = &local_ctx; |
| 8031 | } |
| 8032 | |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8033 | if (occ >= 0) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8034 | /* search from the beginning */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8035 | while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8036 | occ--; |
| 8037 | if (occ <= 0) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8038 | *vptr = ctx->line + ctx->val; |
| 8039 | *vlen = ctx->vlen; |
| 8040 | return 1; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8041 | } |
| 8042 | } |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8043 | return 0; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8044 | } |
| 8045 | |
| 8046 | /* negative occurrence, we scan all the list then walk back */ |
| 8047 | if (-occ > MAX_HDR_HISTORY) |
| 8048 | return 0; |
| 8049 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8050 | found = hist_ptr = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8051 | while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8052 | ptr_hist[hist_ptr] = ctx->line + ctx->val; |
| 8053 | len_hist[hist_ptr] = ctx->vlen; |
| 8054 | if (++hist_ptr >= MAX_HDR_HISTORY) |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8055 | hist_ptr = 0; |
| 8056 | found++; |
| 8057 | } |
| 8058 | if (-occ > found) |
| 8059 | return 0; |
| 8060 | /* 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] | 8061 | * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have |
| 8062 | * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ] |
| 8063 | * to remain in the 0..9 range. |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8064 | */ |
Willy Tarreau | 67dad27 | 2013-06-12 22:27:44 +0200 | [diff] [blame] | 8065 | hist_ptr += occ + MAX_HDR_HISTORY; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8066 | if (hist_ptr >= MAX_HDR_HISTORY) |
| 8067 | hist_ptr -= MAX_HDR_HISTORY; |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8068 | *vptr = ptr_hist[hist_ptr]; |
| 8069 | *vlen = len_hist[hist_ptr]; |
| 8070 | return 1; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8071 | } |
| 8072 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 8073 | /* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of |
| 8074 | * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is |
| 8075 | * performed over the whole headers. Otherwise it must contain a valid header |
| 8076 | * context, initialised with ctx->idx=0 for the first lookup in a series. If |
| 8077 | * <occ> is positive or null, occurrence #occ from the beginning (or last ctx) |
| 8078 | * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less |
| 8079 | * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is |
| 8080 | * -1. This function differs from http_get_hdr() in that it only returns full |
| 8081 | * line header values and does not stop at commas. |
| 8082 | * The return value is 0 if nothing was found, or non-zero otherwise. |
| 8083 | */ |
| 8084 | unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen, |
| 8085 | struct hdr_idx *idx, int occ, |
| 8086 | struct hdr_ctx *ctx, char **vptr, int *vlen) |
| 8087 | { |
| 8088 | struct hdr_ctx local_ctx; |
| 8089 | char *ptr_hist[MAX_HDR_HISTORY]; |
| 8090 | int len_hist[MAX_HDR_HISTORY]; |
| 8091 | unsigned int hist_ptr; |
| 8092 | int found; |
| 8093 | |
| 8094 | if (!ctx) { |
| 8095 | local_ctx.idx = 0; |
| 8096 | ctx = &local_ctx; |
| 8097 | } |
| 8098 | |
| 8099 | if (occ >= 0) { |
| 8100 | /* search from the beginning */ |
| 8101 | while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
| 8102 | occ--; |
| 8103 | if (occ <= 0) { |
| 8104 | *vptr = ctx->line + ctx->val; |
| 8105 | *vlen = ctx->vlen; |
| 8106 | return 1; |
| 8107 | } |
| 8108 | } |
| 8109 | return 0; |
| 8110 | } |
| 8111 | |
| 8112 | /* negative occurrence, we scan all the list then walk back */ |
| 8113 | if (-occ > MAX_HDR_HISTORY) |
| 8114 | return 0; |
| 8115 | |
| 8116 | found = hist_ptr = 0; |
| 8117 | while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
| 8118 | ptr_hist[hist_ptr] = ctx->line + ctx->val; |
| 8119 | len_hist[hist_ptr] = ctx->vlen; |
| 8120 | if (++hist_ptr >= MAX_HDR_HISTORY) |
| 8121 | hist_ptr = 0; |
| 8122 | found++; |
| 8123 | } |
| 8124 | if (-occ > found) |
| 8125 | return 0; |
Nenad Merdanovic | 69ad4b9 | 2016-03-29 13:14:30 +0200 | [diff] [blame] | 8126 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 8127 | /* 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] | 8128 | * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have |
| 8129 | * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ] |
| 8130 | * to remain in the 0..9 range. |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 8131 | */ |
Nenad Merdanovic | 69ad4b9 | 2016-03-29 13:14:30 +0200 | [diff] [blame] | 8132 | hist_ptr += occ + MAX_HDR_HISTORY; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 8133 | if (hist_ptr >= MAX_HDR_HISTORY) |
| 8134 | hist_ptr -= MAX_HDR_HISTORY; |
| 8135 | *vptr = ptr_hist[hist_ptr]; |
| 8136 | *vlen = len_hist[hist_ptr]; |
| 8137 | return 1; |
| 8138 | } |
| 8139 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 8140 | /* |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 8141 | * Print a debug line with a header. Always stop at the first CR or LF char, |
| 8142 | * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an |
| 8143 | * arrow is printed after the line which contains the pointer. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8144 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8145 | 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] | 8146 | { |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 8147 | struct session *sess = strm_sess(s); |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 8148 | int max; |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 8149 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8150 | 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] | 8151 | dir, |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 8152 | 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] | 8153 | 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] | 8154 | |
| 8155 | for (max = 0; start + max < end; max++) |
| 8156 | if (start[max] == '\r' || start[max] == '\n') |
| 8157 | break; |
| 8158 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 8159 | UBOUND(max, trash.size - trash.len - 3); |
| 8160 | trash.len += strlcpy2(trash.str + trash.len, start, max + 1); |
| 8161 | trash.str[trash.len++] = '\n'; |
Willy Tarreau | 89efaed | 2013-12-13 15:14:55 +0100 | [diff] [blame] | 8162 | shut_your_big_mouth_gcc(write(1, trash.str, trash.len)); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8163 | } |
| 8164 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8165 | |
| 8166 | /* Allocate a new HTTP transaction for stream <s> unless there is one already. |
| 8167 | * The hdr_idx is allocated as well. In case of allocation failure, everything |
| 8168 | * allocated is freed and NULL is returned. Otherwise the new transaction is |
| 8169 | * assigned to the stream and returned. |
| 8170 | */ |
| 8171 | struct http_txn *http_alloc_txn(struct stream *s) |
| 8172 | { |
| 8173 | struct http_txn *txn = s->txn; |
| 8174 | |
| 8175 | if (txn) |
| 8176 | return txn; |
| 8177 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 8178 | txn = pool_alloc(pool_head_http_txn); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8179 | if (!txn) |
| 8180 | return txn; |
| 8181 | |
| 8182 | txn->hdr_idx.size = global.tune.max_http_hdr; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 8183 | txn->hdr_idx.v = pool_alloc(pool_head_hdr_idx); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8184 | if (!txn->hdr_idx.v) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 8185 | pool_free(pool_head_http_txn, txn); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8186 | return NULL; |
| 8187 | } |
| 8188 | |
| 8189 | s->txn = txn; |
| 8190 | return txn; |
| 8191 | } |
| 8192 | |
Thierry FOURNIER | fd50f0b | 2015-09-25 18:53:18 +0200 | [diff] [blame] | 8193 | void http_txn_reset_req(struct http_txn *txn) |
| 8194 | { |
| 8195 | txn->req.flags = 0; |
| 8196 | txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */ |
| 8197 | txn->req.next = 0; |
| 8198 | txn->req.chunk_len = 0LL; |
| 8199 | txn->req.body_len = 0LL; |
| 8200 | txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */ |
| 8201 | } |
| 8202 | |
| 8203 | void http_txn_reset_res(struct http_txn *txn) |
| 8204 | { |
| 8205 | txn->rsp.flags = 0; |
| 8206 | txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */ |
| 8207 | txn->rsp.next = 0; |
| 8208 | txn->rsp.chunk_len = 0LL; |
| 8209 | txn->rsp.body_len = 0LL; |
| 8210 | txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */ |
| 8211 | } |
| 8212 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8213 | /* |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8214 | * 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] | 8215 | * the required fields are properly allocated and that we only need to (re)init |
| 8216 | * them. This should be used before processing any new request. |
| 8217 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8218 | void http_init_txn(struct stream *s) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8219 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8220 | struct http_txn *txn = s->txn; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 8221 | struct proxy *fe = strm_fe(s); |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8222 | |
| 8223 | txn->flags = 0; |
| 8224 | txn->status = -1; |
| 8225 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 8226 | txn->cookie_first_date = 0; |
| 8227 | txn->cookie_last_date = 0; |
| 8228 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8229 | txn->srv_cookie = NULL; |
| 8230 | txn->cli_cookie = NULL; |
| 8231 | txn->uri = NULL; |
| 8232 | |
Thierry FOURNIER | fd50f0b | 2015-09-25 18:53:18 +0200 | [diff] [blame] | 8233 | http_txn_reset_req(txn); |
| 8234 | http_txn_reset_res(txn); |
| 8235 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 8236 | txn->req.chn = &s->req; |
| 8237 | txn->rsp.chn = &s->res; |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 8238 | |
| 8239 | txn->auth.method = HTTP_AUTH_UNKNOWN; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8240 | |
| 8241 | txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */ |
| 8242 | if (fe->options2 & PR_O2_REQBUG_OK) |
| 8243 | txn->req.err_pos = -1; /* let buggy requests pass */ |
| 8244 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8245 | if (txn->hdr_idx.v) |
| 8246 | hdr_idx_init(&txn->hdr_idx); |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 8247 | |
| 8248 | vars_init(&s->vars_txn, SCOPE_TXN); |
| 8249 | vars_init(&s->vars_reqres, SCOPE_REQ); |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8250 | } |
| 8251 | |
| 8252 | /* to be used at the end of a transaction */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8253 | void http_end_txn(struct stream *s) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8254 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8255 | struct http_txn *txn = s->txn; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 8256 | struct proxy *fe = strm_fe(s); |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8257 | |
| 8258 | /* these ones will have been dynamically allocated */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 8259 | pool_free(pool_head_requri, txn->uri); |
| 8260 | pool_free(pool_head_capture, txn->cli_cookie); |
| 8261 | pool_free(pool_head_capture, txn->srv_cookie); |
| 8262 | pool_free(pool_head_uniqueid, s->unique_id); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 8263 | |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 8264 | s->unique_id = NULL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8265 | txn->uri = NULL; |
| 8266 | txn->srv_cookie = NULL; |
| 8267 | txn->cli_cookie = NULL; |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8268 | |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 8269 | if (s->req_cap) { |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8270 | struct cap_hdr *h; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 8271 | for (h = fe->req_cap; h; h = h->next) |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 8272 | pool_free(h->pool, s->req_cap[h->index]); |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 8273 | memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *)); |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8274 | } |
| 8275 | |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 8276 | if (s->res_cap) { |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8277 | struct cap_hdr *h; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 8278 | for (h = fe->rsp_cap; h; h = h->next) |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 8279 | pool_free(h->pool, s->res_cap[h->index]); |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 8280 | memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *)); |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8281 | } |
| 8282 | |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 8283 | vars_prune(&s->vars_txn, s->sess, s); |
| 8284 | vars_prune(&s->vars_reqres, s->sess, s); |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8285 | } |
| 8286 | |
| 8287 | /* 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] | 8288 | void http_reset_txn(struct stream *s) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8289 | { |
| 8290 | http_end_txn(s); |
| 8291 | http_init_txn(s); |
| 8292 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 8293 | /* reinitialise the current rule list pointer to NULL. We are sure that |
| 8294 | * any rulelist match the NULL pointer. |
| 8295 | */ |
| 8296 | s->current_rule_list = NULL; |
| 8297 | |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 8298 | s->be = strm_fe(s); |
| 8299 | s->logs.logwait = strm_fe(s)->to_log; |
Willy Tarreau | abcd514 | 2013-06-11 17:18:02 +0200 | [diff] [blame] | 8300 | s->logs.level = 0; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8301 | stream_del_srv_conn(s); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 8302 | s->target = NULL; |
Emeric Brun | b982a3d | 2010-01-04 15:45:53 +0100 | [diff] [blame] | 8303 | /* re-init store persistence */ |
| 8304 | s->store_count = 0; |
Willy Tarreau | 1f0da24 | 2014-01-25 11:01:50 +0100 | [diff] [blame] | 8305 | s->uniq_id = global.req_count++; |
Emeric Brun | b982a3d | 2010-01-04 15:45:53 +0100 | [diff] [blame] | 8306 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 8307 | s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */ |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8308 | |
Willy Tarreau | 739cfba | 2010-01-25 23:11:14 +0100 | [diff] [blame] | 8309 | /* We must trim any excess data from the response buffer, because we |
| 8310 | * may have blocked an invalid response from a server that we don't |
| 8311 | * want to accidentely forward once we disable the analysers, nor do |
| 8312 | * we want those data to come along with next response. A typical |
| 8313 | * example of such data would be from a buggy server responding to |
| 8314 | * a HEAD with some data, or sending more than the advertised |
| 8315 | * content-length. |
| 8316 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 8317 | if (unlikely(s->res.buf->i)) |
| 8318 | s->res.buf->i = 0; |
Willy Tarreau | 739cfba | 2010-01-25 23:11:14 +0100 | [diff] [blame] | 8319 | |
Christopher Faulet | c0c672a | 2017-03-28 11:51:33 +0200 | [diff] [blame] | 8320 | /* Now we can realign the response buffer */ |
Willy Tarreau | d5b343b | 2018-06-06 06:42:46 +0200 | [diff] [blame^] | 8321 | c_realign_if_empty(&s->res); |
Christopher Faulet | c0c672a | 2017-03-28 11:51:33 +0200 | [diff] [blame] | 8322 | |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 8323 | s->req.rto = strm_fe(s)->timeout.client; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 8324 | s->req.wto = TICK_ETERNITY; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8325 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 8326 | s->res.rto = TICK_ETERNITY; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 8327 | s->res.wto = strm_fe(s)->timeout.client; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8328 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 8329 | s->req.rex = TICK_ETERNITY; |
| 8330 | s->req.wex = TICK_ETERNITY; |
| 8331 | s->req.analyse_exp = TICK_ETERNITY; |
| 8332 | s->res.rex = TICK_ETERNITY; |
| 8333 | s->res.wex = TICK_ETERNITY; |
| 8334 | s->res.analyse_exp = TICK_ETERNITY; |
Hongbo Long | e39683c | 2017-03-10 18:41:51 +0100 | [diff] [blame] | 8335 | s->si[1].hcto = TICK_ETERNITY; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8336 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8337 | |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 8338 | void free_http_res_rules(struct list *r) |
| 8339 | { |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 8340 | struct act_rule *tr, *pr; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 8341 | |
| 8342 | list_for_each_entry_safe(pr, tr, r, list) { |
| 8343 | LIST_DEL(&pr->list); |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 8344 | regex_free(&pr->arg.hdr_add.re); |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 8345 | free(pr); |
| 8346 | } |
| 8347 | } |
| 8348 | |
| 8349 | void free_http_req_rules(struct list *r) |
| 8350 | { |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 8351 | struct act_rule *tr, *pr; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8352 | |
| 8353 | list_for_each_entry_safe(pr, tr, r, list) { |
| 8354 | LIST_DEL(&pr->list); |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8355 | if (pr->action == ACT_HTTP_REQ_AUTH) |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 8356 | free(pr->arg.auth.realm); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8357 | |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 8358 | regex_free(&pr->arg.hdr_add.re); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8359 | free(pr); |
| 8360 | } |
| 8361 | } |
| 8362 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8363 | /* parse an "http-request" rule */ |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 8364 | 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] | 8365 | { |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 8366 | struct act_rule *rule; |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 8367 | struct action_kw *custom = NULL; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8368 | int cur_arg; |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 8369 | char *error; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8370 | |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 8371 | rule = calloc(1, sizeof(*rule)); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8372 | if (!rule) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8373 | ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8374 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8375 | } |
| 8376 | |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 8377 | if (!strcmp(args[0], "allow")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8378 | rule->action = ACT_ACTION_ALLOW; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8379 | cur_arg = 1; |
Jarno Huuskonen | 800d176 | 2017-03-06 14:56:36 +0200 | [diff] [blame] | 8380 | } 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] | 8381 | int code; |
| 8382 | int hc; |
| 8383 | |
Jarno Huuskonen | 800d176 | 2017-03-06 14:56:36 +0200 | [diff] [blame] | 8384 | if (!strcmp(args[0], "tarpit")) { |
| 8385 | rule->action = ACT_HTTP_REQ_TARPIT; |
| 8386 | rule->deny_status = HTTP_ERR_500; |
| 8387 | } |
| 8388 | else { |
| 8389 | rule->action = ACT_ACTION_DENY; |
| 8390 | rule->deny_status = HTTP_ERR_403; |
| 8391 | } |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8392 | cur_arg = 1; |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 8393 | if (strcmp(args[cur_arg], "deny_status") == 0) { |
| 8394 | cur_arg++; |
| 8395 | if (!args[cur_arg]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8396 | ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing status code.\n", |
| 8397 | file, linenum, proxy_type_str(proxy), proxy->id, args[0]); |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 8398 | goto out_err; |
| 8399 | } |
| 8400 | |
| 8401 | code = atol(args[cur_arg]); |
| 8402 | cur_arg++; |
| 8403 | for (hc = 0; hc < HTTP_ERR_SIZE; hc++) { |
| 8404 | if (http_err_codes[hc] == code) { |
| 8405 | rule->deny_status = hc; |
| 8406 | break; |
| 8407 | } |
| 8408 | } |
| 8409 | |
| 8410 | if (hc >= HTTP_ERR_SIZE) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8411 | ha_warning("parsing [%s:%d] : status code %d not handled, using default code %d.\n", |
| 8412 | file, linenum, code, http_err_codes[rule->deny_status]); |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 8413 | } |
| 8414 | } |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8415 | } else if (!strcmp(args[0], "auth")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8416 | rule->action = ACT_HTTP_REQ_AUTH; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8417 | cur_arg = 1; |
| 8418 | |
| 8419 | while(*args[cur_arg]) { |
| 8420 | if (!strcmp(args[cur_arg], "realm")) { |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 8421 | rule->arg.auth.realm = strdup(args[cur_arg + 1]); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8422 | cur_arg+=2; |
| 8423 | continue; |
| 8424 | } else |
| 8425 | break; |
| 8426 | } |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 8427 | } else if (!strcmp(args[0], "set-nice")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8428 | rule->action = ACT_HTTP_SET_NICE; |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 8429 | cur_arg = 1; |
| 8430 | |
| 8431 | if (!*args[cur_arg] || |
| 8432 | (*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] | 8433 | ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n", |
| 8434 | file, linenum, args[0]); |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 8435 | goto out_err; |
| 8436 | } |
| 8437 | rule->arg.nice = atoi(args[cur_arg]); |
| 8438 | if (rule->arg.nice < -1024) |
| 8439 | rule->arg.nice = -1024; |
| 8440 | else if (rule->arg.nice > 1024) |
| 8441 | rule->arg.nice = 1024; |
| 8442 | cur_arg++; |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 8443 | } else if (!strcmp(args[0], "set-tos")) { |
| 8444 | #ifdef IP_TOS |
| 8445 | char *err; |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8446 | rule->action = ACT_HTTP_SET_TOS; |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 8447 | cur_arg = 1; |
| 8448 | |
| 8449 | if (!*args[cur_arg] || |
| 8450 | (*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] | 8451 | ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n", |
| 8452 | file, linenum, args[0]); |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 8453 | goto out_err; |
| 8454 | } |
| 8455 | |
| 8456 | rule->arg.tos = strtol(args[cur_arg], &err, 0); |
| 8457 | if (err && *err != '\0') { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8458 | ha_alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n", |
| 8459 | file, linenum, err, args[0]); |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 8460 | goto out_err; |
| 8461 | } |
| 8462 | cur_arg++; |
| 8463 | #else |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8464 | 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] | 8465 | goto out_err; |
| 8466 | #endif |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 8467 | } else if (!strcmp(args[0], "set-mark")) { |
| 8468 | #ifdef SO_MARK |
| 8469 | char *err; |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8470 | rule->action = ACT_HTTP_SET_MARK; |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 8471 | cur_arg = 1; |
| 8472 | |
| 8473 | if (!*args[cur_arg] || |
| 8474 | (*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] | 8475 | ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n", |
| 8476 | file, linenum, args[0]); |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 8477 | goto out_err; |
| 8478 | } |
| 8479 | |
| 8480 | rule->arg.mark = strtoul(args[cur_arg], &err, 0); |
| 8481 | if (err && *err != '\0') { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8482 | ha_alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n", |
| 8483 | file, linenum, err, args[0]); |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 8484 | goto out_err; |
| 8485 | } |
| 8486 | cur_arg++; |
| 8487 | global.last_checks |= LSTCHK_NETADM; |
| 8488 | #else |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8489 | 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] | 8490 | goto out_err; |
| 8491 | #endif |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 8492 | } else if (!strcmp(args[0], "set-log-level")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8493 | rule->action = ACT_HTTP_SET_LOGL; |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 8494 | cur_arg = 1; |
| 8495 | |
| 8496 | if (!*args[cur_arg] || |
| 8497 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 8498 | bad_log_level: |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8499 | ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n", |
| 8500 | file, linenum, args[0]); |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 8501 | goto out_err; |
| 8502 | } |
| 8503 | if (strcmp(args[cur_arg], "silent") == 0) |
| 8504 | rule->arg.loglevel = -1; |
| 8505 | else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0) |
| 8506 | goto bad_log_level; |
| 8507 | cur_arg++; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 8508 | } 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] | 8509 | 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] | 8510 | cur_arg = 1; |
| 8511 | |
Willy Tarreau | 8d1c516 | 2013-04-03 14:13:58 +0200 | [diff] [blame] | 8512 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 8513 | (*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] | 8514 | ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n", |
| 8515 | file, linenum, args[0]); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8516 | goto out_err; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 8517 | } |
| 8518 | |
| 8519 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 8520 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 8521 | LIST_INIT(&rule->arg.hdr_add.fmt); |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 8522 | |
Thierry FOURNIER | 1c0054f | 2013-11-20 15:09:52 +0100 | [diff] [blame] | 8523 | proxy->conf.args.ctx = ARGC_HRQ; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8524 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8525 | 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] | 8526 | (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] | 8527 | ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n", |
| 8528 | file, linenum, args[0], error); |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8529 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8530 | goto out_err; |
| 8531 | } |
Willy Tarreau | 59ad1a2 | 2014-01-29 14:39:58 +0100 | [diff] [blame] | 8532 | free(proxy->conf.lfs_file); |
| 8533 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 8534 | proxy->conf.lfs_line = proxy->conf.args.line; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 8535 | cur_arg += 2; |
Willy Tarreau | b854392 | 2014-06-17 18:58:26 +0200 | [diff] [blame] | 8536 | } 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] | 8537 | 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] | 8538 | cur_arg = 1; |
| 8539 | |
| 8540 | if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] || |
Baptiste Assmann | 92df370 | 2014-06-24 11:10:00 +0200 | [diff] [blame] | 8541 | (*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] | 8542 | ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n", |
| 8543 | file, linenum, args[0]); |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 8544 | goto out_err; |
| 8545 | } |
| 8546 | |
| 8547 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 8548 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 8549 | LIST_INIT(&rule->arg.hdr_add.fmt); |
| 8550 | |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 8551 | error = NULL; |
| 8552 | 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] | 8553 | ha_alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum, |
| 8554 | args[cur_arg + 1], error); |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 8555 | free(error); |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 8556 | goto out_err; |
| 8557 | } |
| 8558 | |
| 8559 | proxy->conf.args.ctx = ARGC_HRQ; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8560 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8561 | 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] | 8562 | (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] | 8563 | ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n", |
| 8564 | file, linenum, args[0], error); |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8565 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8566 | goto out_err; |
| 8567 | } |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 8568 | |
| 8569 | free(proxy->conf.lfs_file); |
| 8570 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 8571 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 8572 | cur_arg += 3; |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 8573 | } else if (strcmp(args[0], "del-header") == 0) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8574 | rule->action = ACT_HTTP_DEL_HDR; |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 8575 | cur_arg = 1; |
| 8576 | |
| 8577 | if (!*args[cur_arg] || |
| 8578 | (*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] | 8579 | ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 8580 | file, linenum, args[0]); |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 8581 | goto out_err; |
| 8582 | } |
| 8583 | |
| 8584 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 8585 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 8586 | |
| 8587 | proxy->conf.args.ctx = ARGC_HRQ; |
| 8588 | free(proxy->conf.lfs_file); |
| 8589 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 8590 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 8591 | cur_arg += 1; |
Frédéric Lécaille | a41d531 | 2018-01-29 12:05:07 +0100 | [diff] [blame] | 8592 | } else if (strncmp(args[0], "track-sc", 8) == 0) { |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 8593 | struct sample_expr *expr; |
| 8594 | unsigned int where; |
| 8595 | char *err = NULL; |
Frédéric Lécaille | a41d531 | 2018-01-29 12:05:07 +0100 | [diff] [blame] | 8596 | unsigned int tsc_num; |
| 8597 | const char *tsc_num_str; |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 8598 | |
| 8599 | cur_arg = 1; |
| 8600 | proxy->conf.args.ctx = ARGC_TRK; |
| 8601 | |
Frédéric Lécaille | a41d531 | 2018-01-29 12:05:07 +0100 | [diff] [blame] | 8602 | tsc_num_str = &args[0][8]; |
| 8603 | if (cfg_parse_track_sc_num(&tsc_num, tsc_num_str, tsc_num_str + strlen(tsc_num_str), &err) == -1) { |
| 8604 | ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n", |
| 8605 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], err); |
| 8606 | free(err); |
| 8607 | goto out_err; |
| 8608 | } |
| 8609 | |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 8610 | expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args); |
| 8611 | if (!expr) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8612 | ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n", |
| 8613 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], err); |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 8614 | free(err); |
| 8615 | goto out_err; |
| 8616 | } |
| 8617 | |
| 8618 | where = 0; |
| 8619 | if (proxy->cap & PR_CAP_FE) |
| 8620 | where |= SMP_VAL_FE_HRQ_HDR; |
| 8621 | if (proxy->cap & PR_CAP_BE) |
| 8622 | where |= SMP_VAL_BE_HRQ_HDR; |
| 8623 | |
| 8624 | if (!(expr->fetch->val & where)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8625 | ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :" |
| 8626 | " fetch method '%s' extracts information from '%s', none of which is available here.\n", |
| 8627 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], |
| 8628 | args[cur_arg-1], sample_src_names(expr->fetch->use)); |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 8629 | free(expr); |
| 8630 | goto out_err; |
| 8631 | } |
| 8632 | |
| 8633 | if (strcmp(args[cur_arg], "table") == 0) { |
| 8634 | cur_arg++; |
| 8635 | if (!args[cur_arg]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8636 | ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing table name.\n", |
| 8637 | file, linenum, proxy_type_str(proxy), proxy->id, args[0]); |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 8638 | free(expr); |
| 8639 | goto out_err; |
| 8640 | } |
| 8641 | /* we copy the table name for now, it will be resolved later */ |
Thierry FOURNIER | 5ec63e0 | 2015-08-04 09:09:48 +0200 | [diff] [blame] | 8642 | rule->arg.trk_ctr.table.n = strdup(args[cur_arg]); |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 8643 | cur_arg++; |
| 8644 | } |
Thierry FOURNIER | 5ec63e0 | 2015-08-04 09:09:48 +0200 | [diff] [blame] | 8645 | rule->arg.trk_ctr.expr = expr; |
Frédéric Lécaille | a41d531 | 2018-01-29 12:05:07 +0100 | [diff] [blame] | 8646 | rule->action = ACT_ACTION_TRK_SC0 + tsc_num; |
Christopher Faulet | 78880fb | 2017-09-18 14:43:55 +0200 | [diff] [blame] | 8647 | rule->check_ptr = check_trk_action; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8648 | } else if (strcmp(args[0], "redirect") == 0) { |
| 8649 | struct redirect_rule *redir; |
Willy Tarreau | 6d4890c | 2013-11-18 18:04:25 +0100 | [diff] [blame] | 8650 | char *errmsg = NULL; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8651 | |
Willy Tarreau | be4653b | 2015-05-28 15:26:58 +0200 | [diff] [blame] | 8652 | 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] | 8653 | ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n", |
| 8654 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8655 | goto out_err; |
| 8656 | } |
| 8657 | |
| 8658 | /* this redirect rule might already contain a parsed condition which |
| 8659 | * we'll pass to the http-request rule. |
| 8660 | */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8661 | rule->action = ACT_HTTP_REDIR; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8662 | rule->arg.redir = redir; |
| 8663 | rule->cond = redir->cond; |
| 8664 | redir->cond = NULL; |
| 8665 | cur_arg = 2; |
| 8666 | return rule; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8667 | } else if (strncmp(args[0], "add-acl", 7) == 0) { |
| 8668 | /* http-request add-acl(<reference (acl name)>) <key pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8669 | rule->action = ACT_HTTP_ADD_ACL; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8670 | /* |
| 8671 | * '+ 8' for 'add-acl(' |
| 8672 | * '- 9' for 'add-acl(' + trailing ')' |
| 8673 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 8674 | 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] | 8675 | |
| 8676 | cur_arg = 1; |
| 8677 | |
| 8678 | if (!*args[cur_arg] || |
| 8679 | (*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] | 8680 | ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 8681 | file, linenum, args[0]); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8682 | goto out_err; |
| 8683 | } |
| 8684 | |
| 8685 | LIST_INIT(&rule->arg.map.key); |
| 8686 | proxy->conf.args.ctx = ARGC_HRQ; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8687 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8688 | 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] | 8689 | (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] | 8690 | ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n", |
| 8691 | file, linenum, args[0], error); |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8692 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8693 | goto out_err; |
| 8694 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8695 | free(proxy->conf.lfs_file); |
| 8696 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 8697 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 8698 | cur_arg += 1; |
| 8699 | } else if (strncmp(args[0], "del-acl", 7) == 0) { |
| 8700 | /* http-request del-acl(<reference (acl name)>) <key pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8701 | rule->action = ACT_HTTP_DEL_ACL; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8702 | /* |
| 8703 | * '+ 8' for 'del-acl(' |
| 8704 | * '- 9' for 'del-acl(' + trailing ')' |
| 8705 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 8706 | 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] | 8707 | |
| 8708 | cur_arg = 1; |
| 8709 | |
| 8710 | if (!*args[cur_arg] || |
| 8711 | (*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] | 8712 | ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 8713 | file, linenum, args[0]); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8714 | goto out_err; |
| 8715 | } |
| 8716 | |
| 8717 | LIST_INIT(&rule->arg.map.key); |
| 8718 | proxy->conf.args.ctx = ARGC_HRQ; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8719 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8720 | 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] | 8721 | (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] | 8722 | ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n", |
| 8723 | file, linenum, args[0], error); |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8724 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8725 | goto out_err; |
| 8726 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8727 | free(proxy->conf.lfs_file); |
| 8728 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 8729 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 8730 | cur_arg += 1; |
| 8731 | } else if (strncmp(args[0], "del-map", 7) == 0) { |
| 8732 | /* http-request del-map(<reference (map name)>) <key pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8733 | rule->action = ACT_HTTP_DEL_MAP; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8734 | /* |
| 8735 | * '+ 8' for 'del-map(' |
| 8736 | * '- 9' for 'del-map(' + trailing ')' |
| 8737 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 8738 | 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] | 8739 | |
| 8740 | cur_arg = 1; |
| 8741 | |
| 8742 | if (!*args[cur_arg] || |
| 8743 | (*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] | 8744 | ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 8745 | file, linenum, args[0]); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8746 | goto out_err; |
| 8747 | } |
| 8748 | |
| 8749 | LIST_INIT(&rule->arg.map.key); |
| 8750 | proxy->conf.args.ctx = ARGC_HRQ; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8751 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8752 | 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] | 8753 | (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] | 8754 | ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n", |
| 8755 | file, linenum, args[0], error); |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8756 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8757 | goto out_err; |
| 8758 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8759 | free(proxy->conf.lfs_file); |
| 8760 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 8761 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 8762 | cur_arg += 1; |
| 8763 | } else if (strncmp(args[0], "set-map", 7) == 0) { |
| 8764 | /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8765 | rule->action = ACT_HTTP_SET_MAP; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8766 | /* |
| 8767 | * '+ 8' for 'set-map(' |
| 8768 | * '- 9' for 'set-map(' + trailing ')' |
| 8769 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 8770 | 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] | 8771 | |
| 8772 | cur_arg = 1; |
| 8773 | |
| 8774 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 8775 | (*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] | 8776 | ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n", |
| 8777 | file, linenum, args[0]); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8778 | goto out_err; |
| 8779 | } |
| 8780 | |
| 8781 | LIST_INIT(&rule->arg.map.key); |
| 8782 | LIST_INIT(&rule->arg.map.value); |
| 8783 | proxy->conf.args.ctx = ARGC_HRQ; |
| 8784 | |
| 8785 | /* key pattern */ |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8786 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8787 | 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] | 8788 | (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] | 8789 | ha_alert("parsing [%s:%d]: 'http-request %s' key: %s.\n", |
| 8790 | file, linenum, args[0], error); |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8791 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8792 | goto out_err; |
| 8793 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8794 | |
| 8795 | /* value pattern */ |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8796 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8797 | 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] | 8798 | (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] | 8799 | ha_alert("parsing [%s:%d]: 'http-request %s' pattern: %s.\n", |
| 8800 | file, linenum, args[0], error); |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8801 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8802 | goto out_err; |
| 8803 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8804 | free(proxy->conf.lfs_file); |
| 8805 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 8806 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 8807 | |
| 8808 | cur_arg += 2; |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 8809 | } else if (((custom = action_http_req_custom(args[0])) != NULL)) { |
| 8810 | char *errmsg = NULL; |
| 8811 | cur_arg = 1; |
| 8812 | /* try in the module list */ |
Thierry FOURNIER | 5563e4b | 2015-08-14 19:20:07 +0200 | [diff] [blame] | 8813 | rule->from = ACT_F_HTTP_REQ; |
Thierry FOURNIER | 85c6c97 | 2015-09-22 19:14:35 +0200 | [diff] [blame] | 8814 | rule->kw = custom; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 8815 | 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] | 8816 | ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n", |
| 8817 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 8818 | free(errmsg); |
| 8819 | goto out_err; |
| 8820 | } |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8821 | } else { |
Thierry FOURNIER | ab95e65 | 2015-10-02 08:24:51 +0200 | [diff] [blame] | 8822 | action_build_list(&http_req_keywords.list, &trash); |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8823 | ha_alert("parsing [%s:%d]: 'http-request' expects 'allow', 'deny', 'auth', 'redirect', " |
| 8824 | "'tarpit', 'add-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', " |
| 8825 | "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'track-sc*'" |
| 8826 | "%s%s, but got '%s'%s.\n", |
| 8827 | file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)"); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8828 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8829 | } |
| 8830 | |
| 8831 | if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) { |
| 8832 | struct acl_cond *cond; |
Willy Tarreau | b7451bb | 2012-04-27 12:38:15 +0200 | [diff] [blame] | 8833 | char *errmsg = NULL; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8834 | |
Christopher Faulet | 1b421ea | 2017-09-22 14:38:56 +0200 | [diff] [blame] | 8835 | 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] | 8836 | ha_alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n", |
| 8837 | file, linenum, args[0], errmsg); |
Willy Tarreau | b7451bb | 2012-04-27 12:38:15 +0200 | [diff] [blame] | 8838 | free(errmsg); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8839 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8840 | } |
| 8841 | rule->cond = cond; |
| 8842 | } |
| 8843 | else if (*args[cur_arg]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8844 | ha_alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or" |
| 8845 | " either 'if' or 'unless' followed by a condition but found '%s'.\n", |
| 8846 | file, linenum, args[0], args[cur_arg]); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8847 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8848 | } |
| 8849 | |
| 8850 | return rule; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8851 | out_err: |
| 8852 | free(rule); |
| 8853 | return NULL; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8854 | } |
| 8855 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8856 | /* parse an "http-respose" rule */ |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 8857 | 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] | 8858 | { |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 8859 | struct act_rule *rule; |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 8860 | struct action_kw *custom = NULL; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8861 | int cur_arg; |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 8862 | char *error; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8863 | |
| 8864 | rule = calloc(1, sizeof(*rule)); |
| 8865 | if (!rule) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8866 | ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum); |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8867 | goto out_err; |
| 8868 | } |
| 8869 | |
| 8870 | if (!strcmp(args[0], "allow")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8871 | rule->action = ACT_ACTION_ALLOW; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8872 | cur_arg = 1; |
| 8873 | } else if (!strcmp(args[0], "deny")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8874 | rule->action = ACT_ACTION_DENY; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8875 | cur_arg = 1; |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 8876 | } else if (!strcmp(args[0], "set-nice")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8877 | rule->action = ACT_HTTP_SET_NICE; |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 8878 | cur_arg = 1; |
| 8879 | |
| 8880 | if (!*args[cur_arg] || |
| 8881 | (*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] | 8882 | ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n", |
| 8883 | file, linenum, args[0]); |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 8884 | goto out_err; |
| 8885 | } |
| 8886 | rule->arg.nice = atoi(args[cur_arg]); |
| 8887 | if (rule->arg.nice < -1024) |
| 8888 | rule->arg.nice = -1024; |
| 8889 | else if (rule->arg.nice > 1024) |
| 8890 | rule->arg.nice = 1024; |
| 8891 | cur_arg++; |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 8892 | } else if (!strcmp(args[0], "set-tos")) { |
| 8893 | #ifdef IP_TOS |
| 8894 | char *err; |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8895 | rule->action = ACT_HTTP_SET_TOS; |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 8896 | cur_arg = 1; |
| 8897 | |
| 8898 | if (!*args[cur_arg] || |
| 8899 | (*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] | 8900 | ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n", |
| 8901 | file, linenum, args[0]); |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 8902 | goto out_err; |
| 8903 | } |
| 8904 | |
| 8905 | rule->arg.tos = strtol(args[cur_arg], &err, 0); |
| 8906 | if (err && *err != '\0') { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8907 | ha_alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n", |
| 8908 | file, linenum, err, args[0]); |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 8909 | goto out_err; |
| 8910 | } |
| 8911 | cur_arg++; |
| 8912 | #else |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8913 | 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] | 8914 | goto out_err; |
| 8915 | #endif |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 8916 | } else if (!strcmp(args[0], "set-mark")) { |
| 8917 | #ifdef SO_MARK |
| 8918 | char *err; |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8919 | rule->action = ACT_HTTP_SET_MARK; |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 8920 | cur_arg = 1; |
| 8921 | |
| 8922 | if (!*args[cur_arg] || |
| 8923 | (*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] | 8924 | ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n", |
| 8925 | file, linenum, args[0]); |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 8926 | goto out_err; |
| 8927 | } |
| 8928 | |
| 8929 | rule->arg.mark = strtoul(args[cur_arg], &err, 0); |
| 8930 | if (err && *err != '\0') { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8931 | ha_alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n", |
| 8932 | file, linenum, err, args[0]); |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 8933 | goto out_err; |
| 8934 | } |
| 8935 | cur_arg++; |
| 8936 | global.last_checks |= LSTCHK_NETADM; |
| 8937 | #else |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8938 | 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] | 8939 | goto out_err; |
| 8940 | #endif |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 8941 | } else if (!strcmp(args[0], "set-log-level")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8942 | rule->action = ACT_HTTP_SET_LOGL; |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 8943 | cur_arg = 1; |
| 8944 | |
| 8945 | if (!*args[cur_arg] || |
| 8946 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 8947 | bad_log_level: |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8948 | ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n", |
| 8949 | file, linenum, args[0]); |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 8950 | goto out_err; |
| 8951 | } |
| 8952 | if (strcmp(args[cur_arg], "silent") == 0) |
| 8953 | rule->arg.loglevel = -1; |
Ruoshan Huang | dd01678 | 2016-06-15 22:16:03 +0800 | [diff] [blame] | 8954 | 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] | 8955 | goto bad_log_level; |
| 8956 | cur_arg++; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8957 | } 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] | 8958 | 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] | 8959 | cur_arg = 1; |
| 8960 | |
| 8961 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 8962 | (*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] | 8963 | ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n", |
| 8964 | file, linenum, args[0]); |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8965 | goto out_err; |
| 8966 | } |
| 8967 | |
| 8968 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 8969 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 8970 | LIST_INIT(&rule->arg.hdr_add.fmt); |
| 8971 | |
Thierry FOURNIER | 1c0054f | 2013-11-20 15:09:52 +0100 | [diff] [blame] | 8972 | proxy->conf.args.ctx = ARGC_HRS; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8973 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8974 | 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] | 8975 | (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] | 8976 | ha_alert("parsing [%s:%d]: 'http-response %s': %s.\n", |
| 8977 | file, linenum, args[0], error); |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8978 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8979 | goto out_err; |
| 8980 | } |
Willy Tarreau | 59ad1a2 | 2014-01-29 14:39:58 +0100 | [diff] [blame] | 8981 | free(proxy->conf.lfs_file); |
| 8982 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 8983 | proxy->conf.lfs_line = proxy->conf.args.line; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8984 | cur_arg += 2; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 8985 | } 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] | 8986 | 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] | 8987 | cur_arg = 1; |
| 8988 | |
| 8989 | if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] || |
Baptiste Assmann | 12cb00b | 2014-08-08 17:29:06 +0200 | [diff] [blame] | 8990 | (*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] | 8991 | ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n", |
| 8992 | file, linenum, args[0]); |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 8993 | goto out_err; |
| 8994 | } |
| 8995 | |
| 8996 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 8997 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 8998 | LIST_INIT(&rule->arg.hdr_add.fmt); |
| 8999 | |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 9000 | error = NULL; |
| 9001 | 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] | 9002 | ha_alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum, |
| 9003 | args[cur_arg + 1], error); |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 9004 | free(error); |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9005 | goto out_err; |
| 9006 | } |
| 9007 | |
| 9008 | proxy->conf.args.ctx = ARGC_HRQ; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9009 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9010 | 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] | 9011 | (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] | 9012 | ha_alert("parsing [%s:%d]: 'http-response %s': %s.\n", |
| 9013 | file, linenum, args[0], error); |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9014 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9015 | goto out_err; |
| 9016 | } |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9017 | |
| 9018 | free(proxy->conf.lfs_file); |
| 9019 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9020 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9021 | cur_arg += 3; |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9022 | } else if (strcmp(args[0], "del-header") == 0) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9023 | rule->action = ACT_HTTP_DEL_HDR; |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9024 | cur_arg = 1; |
| 9025 | |
| 9026 | if (!*args[cur_arg] || |
| 9027 | (*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] | 9028 | ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 9029 | file, linenum, args[0]); |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9030 | goto out_err; |
| 9031 | } |
| 9032 | |
| 9033 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9034 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9035 | |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9036 | proxy->conf.args.ctx = ARGC_HRS; |
| 9037 | free(proxy->conf.lfs_file); |
| 9038 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9039 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9040 | cur_arg += 1; |
| 9041 | } else if (strncmp(args[0], "add-acl", 7) == 0) { |
| 9042 | /* http-request add-acl(<reference (acl name)>) <key pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9043 | rule->action = ACT_HTTP_ADD_ACL; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9044 | /* |
| 9045 | * '+ 8' for 'add-acl(' |
| 9046 | * '- 9' for 'add-acl(' + trailing ')' |
| 9047 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9048 | 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] | 9049 | |
| 9050 | cur_arg = 1; |
| 9051 | |
| 9052 | if (!*args[cur_arg] || |
| 9053 | (*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] | 9054 | ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 9055 | file, linenum, args[0]); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9056 | goto out_err; |
| 9057 | } |
| 9058 | |
| 9059 | LIST_INIT(&rule->arg.map.key); |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9060 | proxy->conf.args.ctx = ARGC_HRS; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9061 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9062 | 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] | 9063 | (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] | 9064 | ha_alert("parsing [%s:%d]: 'http-response %s': %s.\n", |
| 9065 | file, linenum, args[0], error); |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9066 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9067 | goto out_err; |
| 9068 | } |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9069 | free(proxy->conf.lfs_file); |
| 9070 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9071 | proxy->conf.lfs_line = proxy->conf.args.line; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9072 | |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9073 | cur_arg += 1; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9074 | } else if (strncmp(args[0], "del-acl", 7) == 0) { |
| 9075 | /* http-response del-acl(<reference (acl name)>) <key pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9076 | rule->action = ACT_HTTP_DEL_ACL; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9077 | /* |
| 9078 | * '+ 8' for 'del-acl(' |
| 9079 | * '- 9' for 'del-acl(' + trailing ')' |
| 9080 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9081 | 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] | 9082 | |
| 9083 | cur_arg = 1; |
| 9084 | |
| 9085 | if (!*args[cur_arg] || |
| 9086 | (*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] | 9087 | ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 9088 | file, linenum, args[0]); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9089 | goto out_err; |
| 9090 | } |
| 9091 | |
| 9092 | LIST_INIT(&rule->arg.map.key); |
| 9093 | proxy->conf.args.ctx = ARGC_HRS; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9094 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9095 | 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] | 9096 | (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] | 9097 | ha_alert("parsing [%s:%d]: 'http-response %s': %s.\n", |
| 9098 | file, linenum, args[0], error); |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9099 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9100 | goto out_err; |
| 9101 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9102 | free(proxy->conf.lfs_file); |
| 9103 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9104 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9105 | cur_arg += 1; |
| 9106 | } else if (strncmp(args[0], "del-map", 7) == 0) { |
| 9107 | /* http-response del-map(<reference (map name)>) <key pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9108 | rule->action = ACT_HTTP_DEL_MAP; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9109 | /* |
| 9110 | * '+ 8' for 'del-map(' |
| 9111 | * '- 9' for 'del-map(' + trailing ')' |
| 9112 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9113 | 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] | 9114 | |
| 9115 | cur_arg = 1; |
| 9116 | |
| 9117 | if (!*args[cur_arg] || |
| 9118 | (*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] | 9119 | ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 9120 | file, linenum, args[0]); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9121 | goto out_err; |
| 9122 | } |
| 9123 | |
| 9124 | LIST_INIT(&rule->arg.map.key); |
| 9125 | proxy->conf.args.ctx = ARGC_HRS; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9126 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9127 | 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] | 9128 | (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] | 9129 | ha_alert("parsing [%s:%d]: 'http-response %s' %s.\n", |
| 9130 | file, linenum, args[0], error); |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9131 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9132 | goto out_err; |
| 9133 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9134 | free(proxy->conf.lfs_file); |
| 9135 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9136 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9137 | cur_arg += 1; |
| 9138 | } else if (strncmp(args[0], "set-map", 7) == 0) { |
| 9139 | /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9140 | rule->action = ACT_HTTP_SET_MAP; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9141 | /* |
| 9142 | * '+ 8' for 'set-map(' |
| 9143 | * '- 9' for 'set-map(' + trailing ')' |
| 9144 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9145 | 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] | 9146 | |
| 9147 | cur_arg = 1; |
| 9148 | |
| 9149 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 9150 | (*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] | 9151 | ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n", |
| 9152 | file, linenum, args[0]); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9153 | goto out_err; |
| 9154 | } |
| 9155 | |
| 9156 | LIST_INIT(&rule->arg.map.key); |
| 9157 | LIST_INIT(&rule->arg.map.value); |
| 9158 | |
| 9159 | proxy->conf.args.ctx = ARGC_HRS; |
| 9160 | |
| 9161 | /* key pattern */ |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9162 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9163 | 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] | 9164 | (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] | 9165 | ha_alert("parsing [%s:%d]: 'http-response %s' name: %s.\n", |
| 9166 | file, linenum, args[0], error); |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9167 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9168 | goto out_err; |
| 9169 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9170 | |
| 9171 | /* value pattern */ |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9172 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9173 | 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] | 9174 | (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] | 9175 | ha_alert("parsing [%s:%d]: 'http-response %s' value: %s.\n", |
| 9176 | file, linenum, args[0], error); |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9177 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9178 | goto out_err; |
| 9179 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9180 | |
| 9181 | free(proxy->conf.lfs_file); |
| 9182 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9183 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9184 | |
| 9185 | cur_arg += 2; |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 9186 | } else if (strcmp(args[0], "redirect") == 0) { |
| 9187 | struct redirect_rule *redir; |
| 9188 | char *errmsg = NULL; |
| 9189 | |
| 9190 | 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] | 9191 | ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n", |
| 9192 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 9193 | goto out_err; |
| 9194 | } |
| 9195 | |
| 9196 | /* this redirect rule might already contain a parsed condition which |
| 9197 | * we'll pass to the http-request rule. |
| 9198 | */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9199 | rule->action = ACT_HTTP_REDIR; |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 9200 | rule->arg.redir = redir; |
| 9201 | rule->cond = redir->cond; |
| 9202 | redir->cond = NULL; |
| 9203 | cur_arg = 2; |
| 9204 | return rule; |
Frédéric Lécaille | a41d531 | 2018-01-29 12:05:07 +0100 | [diff] [blame] | 9205 | } else if (strncmp(args[0], "track-sc", 8) == 0) { |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 9206 | struct sample_expr *expr; |
| 9207 | unsigned int where; |
| 9208 | char *err = NULL; |
Frédéric Lécaille | a41d531 | 2018-01-29 12:05:07 +0100 | [diff] [blame] | 9209 | unsigned int tsc_num; |
| 9210 | const char *tsc_num_str; |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 9211 | |
| 9212 | cur_arg = 1; |
| 9213 | proxy->conf.args.ctx = ARGC_TRK; |
| 9214 | |
Frédéric Lécaille | a41d531 | 2018-01-29 12:05:07 +0100 | [diff] [blame] | 9215 | tsc_num_str = &args[0][8]; |
| 9216 | if (cfg_parse_track_sc_num(&tsc_num, tsc_num_str, tsc_num_str + strlen(tsc_num_str), &err) == -1) { |
| 9217 | ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n", |
| 9218 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], err); |
| 9219 | free(err); |
| 9220 | goto out_err; |
| 9221 | } |
| 9222 | |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 9223 | expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args); |
| 9224 | if (!expr) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 9225 | ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n", |
| 9226 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], err); |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 9227 | free(err); |
| 9228 | goto out_err; |
| 9229 | } |
| 9230 | |
| 9231 | where = 0; |
| 9232 | if (proxy->cap & PR_CAP_FE) |
| 9233 | where |= SMP_VAL_FE_HRS_HDR; |
| 9234 | if (proxy->cap & PR_CAP_BE) |
| 9235 | where |= SMP_VAL_BE_HRS_HDR; |
| 9236 | |
| 9237 | if (!(expr->fetch->val & where)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 9238 | ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule :" |
| 9239 | " fetch method '%s' extracts information from '%s', none of which is available here.\n", |
| 9240 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], |
| 9241 | args[cur_arg-1], sample_src_names(expr->fetch->use)); |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 9242 | free(expr); |
| 9243 | goto out_err; |
| 9244 | } |
| 9245 | |
| 9246 | if (strcmp(args[cur_arg], "table") == 0) { |
| 9247 | cur_arg++; |
| 9248 | if (!args[cur_arg]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 9249 | ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : missing table name.\n", |
| 9250 | file, linenum, proxy_type_str(proxy), proxy->id, args[0]); |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 9251 | free(expr); |
| 9252 | goto out_err; |
| 9253 | } |
| 9254 | /* we copy the table name for now, it will be resolved later */ |
| 9255 | rule->arg.trk_ctr.table.n = strdup(args[cur_arg]); |
| 9256 | cur_arg++; |
| 9257 | } |
| 9258 | rule->arg.trk_ctr.expr = expr; |
Frédéric Lécaille | a41d531 | 2018-01-29 12:05:07 +0100 | [diff] [blame] | 9259 | rule->action = ACT_ACTION_TRK_SC0 + tsc_num; |
Christopher Faulet | 78880fb | 2017-09-18 14:43:55 +0200 | [diff] [blame] | 9260 | rule->check_ptr = check_trk_action; |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 9261 | } else if (((custom = action_http_res_custom(args[0])) != NULL)) { |
| 9262 | char *errmsg = NULL; |
| 9263 | cur_arg = 1; |
| 9264 | /* try in the module list */ |
Thierry FOURNIER | 5563e4b | 2015-08-14 19:20:07 +0200 | [diff] [blame] | 9265 | rule->from = ACT_F_HTTP_RES; |
Thierry FOURNIER | 85c6c97 | 2015-09-22 19:14:35 +0200 | [diff] [blame] | 9266 | rule->kw = custom; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 9267 | 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] | 9268 | ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n", |
| 9269 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 9270 | free(errmsg); |
| 9271 | goto out_err; |
| 9272 | } |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9273 | } else { |
Thierry FOURNIER | ab95e65 | 2015-10-02 08:24:51 +0200 | [diff] [blame] | 9274 | action_build_list(&http_res_keywords.list, &trash); |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 9275 | ha_alert("parsing [%s:%d]: 'http-response' expects 'allow', 'deny', 'redirect', " |
| 9276 | "'add-header', 'del-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', " |
| 9277 | "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'track-sc*'" |
| 9278 | "%s%s, but got '%s'%s.\n", |
| 9279 | file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)"); |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9280 | goto out_err; |
| 9281 | } |
| 9282 | |
| 9283 | if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) { |
| 9284 | struct acl_cond *cond; |
| 9285 | char *errmsg = NULL; |
| 9286 | |
Christopher Faulet | 1b421ea | 2017-09-22 14:38:56 +0200 | [diff] [blame] | 9287 | 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] | 9288 | ha_alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n", |
| 9289 | file, linenum, args[0], errmsg); |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9290 | free(errmsg); |
| 9291 | goto out_err; |
| 9292 | } |
| 9293 | rule->cond = cond; |
| 9294 | } |
| 9295 | else if (*args[cur_arg]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 9296 | ha_alert("parsing [%s:%d]: 'http-response %s' expects" |
| 9297 | " either 'if' or 'unless' followed by a condition but found '%s'.\n", |
| 9298 | file, linenum, args[0], args[cur_arg]); |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9299 | goto out_err; |
| 9300 | } |
| 9301 | |
| 9302 | return rule; |
| 9303 | out_err: |
| 9304 | free(rule); |
| 9305 | return NULL; |
| 9306 | } |
| 9307 | |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9308 | /* 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] | 9309 | * 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] | 9310 | * dynamic log-format rule instead of a static string. Parameter <dir> indicates |
| 9311 | * 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] | 9312 | */ |
| 9313 | 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] | 9314 | const char **args, char **errmsg, int use_fmt, int dir) |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9315 | { |
| 9316 | struct redirect_rule *rule; |
| 9317 | int cur_arg; |
| 9318 | int type = REDIRECT_TYPE_NONE; |
| 9319 | int code = 302; |
| 9320 | const char *destination = NULL; |
| 9321 | const char *cookie = NULL; |
| 9322 | int cookie_set = 0; |
| 9323 | unsigned int flags = REDIRECT_FLAG_NONE; |
| 9324 | struct acl_cond *cond = NULL; |
| 9325 | |
| 9326 | cur_arg = 0; |
| 9327 | while (*(args[cur_arg])) { |
| 9328 | if (strcmp(args[cur_arg], "location") == 0) { |
| 9329 | if (!*args[cur_arg + 1]) |
| 9330 | goto missing_arg; |
| 9331 | |
| 9332 | type = REDIRECT_TYPE_LOCATION; |
| 9333 | cur_arg++; |
| 9334 | destination = args[cur_arg]; |
| 9335 | } |
| 9336 | else if (strcmp(args[cur_arg], "prefix") == 0) { |
| 9337 | if (!*args[cur_arg + 1]) |
| 9338 | goto missing_arg; |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9339 | type = REDIRECT_TYPE_PREFIX; |
| 9340 | cur_arg++; |
| 9341 | destination = args[cur_arg]; |
| 9342 | } |
| 9343 | else if (strcmp(args[cur_arg], "scheme") == 0) { |
| 9344 | if (!*args[cur_arg + 1]) |
| 9345 | goto missing_arg; |
| 9346 | |
| 9347 | type = REDIRECT_TYPE_SCHEME; |
| 9348 | cur_arg++; |
| 9349 | destination = args[cur_arg]; |
| 9350 | } |
| 9351 | else if (strcmp(args[cur_arg], "set-cookie") == 0) { |
| 9352 | if (!*args[cur_arg + 1]) |
| 9353 | goto missing_arg; |
| 9354 | |
| 9355 | cur_arg++; |
| 9356 | cookie = args[cur_arg]; |
| 9357 | cookie_set = 1; |
| 9358 | } |
| 9359 | else if (strcmp(args[cur_arg], "clear-cookie") == 0) { |
| 9360 | if (!*args[cur_arg + 1]) |
| 9361 | goto missing_arg; |
| 9362 | |
| 9363 | cur_arg++; |
| 9364 | cookie = args[cur_arg]; |
| 9365 | cookie_set = 0; |
| 9366 | } |
| 9367 | else if (strcmp(args[cur_arg], "code") == 0) { |
| 9368 | if (!*args[cur_arg + 1]) |
| 9369 | goto missing_arg; |
| 9370 | |
| 9371 | cur_arg++; |
| 9372 | code = atol(args[cur_arg]); |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 9373 | if (code < 301 || code > 308 || (code > 303 && code < 307)) { |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9374 | memprintf(errmsg, |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 9375 | "'%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] | 9376 | args[cur_arg - 1], args[cur_arg]); |
| 9377 | return NULL; |
| 9378 | } |
| 9379 | } |
| 9380 | else if (!strcmp(args[cur_arg],"drop-query")) { |
| 9381 | flags |= REDIRECT_FLAG_DROP_QS; |
| 9382 | } |
| 9383 | else if (!strcmp(args[cur_arg],"append-slash")) { |
| 9384 | flags |= REDIRECT_FLAG_APPEND_SLASH; |
| 9385 | } |
| 9386 | else if (strcmp(args[cur_arg], "if") == 0 || |
| 9387 | strcmp(args[cur_arg], "unless") == 0) { |
Olivier Houchard | fbc74e8 | 2017-11-24 16:54:05 +0100 | [diff] [blame] | 9388 | 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] | 9389 | if (!cond) { |
| 9390 | memprintf(errmsg, "error in condition: %s", *errmsg); |
| 9391 | return NULL; |
| 9392 | } |
| 9393 | break; |
| 9394 | } |
| 9395 | else { |
| 9396 | memprintf(errmsg, |
| 9397 | "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')", |
| 9398 | args[cur_arg]); |
| 9399 | return NULL; |
| 9400 | } |
| 9401 | cur_arg++; |
| 9402 | } |
| 9403 | |
| 9404 | if (type == REDIRECT_TYPE_NONE) { |
| 9405 | memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')"); |
| 9406 | return NULL; |
| 9407 | } |
| 9408 | |
Willy Tarreau | be4653b | 2015-05-28 15:26:58 +0200 | [diff] [blame] | 9409 | if (dir && type != REDIRECT_TYPE_LOCATION) { |
| 9410 | memprintf(errmsg, "response only supports redirect type 'location'"); |
| 9411 | return NULL; |
| 9412 | } |
| 9413 | |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 9414 | rule = calloc(1, sizeof(*rule)); |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9415 | rule->cond = cond; |
Willy Tarreau | 60e0838 | 2013-12-03 00:48:45 +0100 | [diff] [blame] | 9416 | LIST_INIT(&rule->rdr_fmt); |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 9417 | |
| 9418 | if (!use_fmt) { |
| 9419 | /* old-style static redirect rule */ |
| 9420 | rule->rdr_str = strdup(destination); |
| 9421 | rule->rdr_len = strlen(destination); |
| 9422 | } |
| 9423 | else { |
| 9424 | /* log-format based redirect rule */ |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 9425 | |
| 9426 | /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case, |
| 9427 | * if prefix == "/", we don't want to add anything, otherwise it |
| 9428 | * makes it hard for the user to configure a self-redirection. |
| 9429 | */ |
Godbach | d972203 | 2014-12-18 15:44:58 +0800 | [diff] [blame] | 9430 | curproxy->conf.args.ctx = ARGC_RDR; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 9431 | if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) { |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9432 | if (!parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP, |
| 9433 | 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] | 9434 | : (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9435 | errmsg)) { |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9436 | return NULL; |
| 9437 | } |
Willy Tarreau | 59ad1a2 | 2014-01-29 14:39:58 +0100 | [diff] [blame] | 9438 | free(curproxy->conf.lfs_file); |
| 9439 | curproxy->conf.lfs_file = strdup(curproxy->conf.args.file); |
| 9440 | curproxy->conf.lfs_line = curproxy->conf.args.line; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 9441 | } |
| 9442 | } |
| 9443 | |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9444 | if (cookie) { |
| 9445 | /* depending on cookie_set, either we want to set the cookie, or to clear it. |
| 9446 | * a clear consists in appending "; path=/; Max-Age=0;" at the end. |
| 9447 | */ |
| 9448 | rule->cookie_len = strlen(cookie); |
| 9449 | if (cookie_set) { |
| 9450 | rule->cookie_str = malloc(rule->cookie_len + 10); |
| 9451 | memcpy(rule->cookie_str, cookie, rule->cookie_len); |
| 9452 | memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10); |
| 9453 | rule->cookie_len += 9; |
| 9454 | } else { |
| 9455 | rule->cookie_str = malloc(rule->cookie_len + 21); |
| 9456 | memcpy(rule->cookie_str, cookie, rule->cookie_len); |
| 9457 | memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21); |
| 9458 | rule->cookie_len += 20; |
| 9459 | } |
| 9460 | } |
| 9461 | rule->type = type; |
| 9462 | rule->code = code; |
| 9463 | rule->flags = flags; |
| 9464 | LIST_INIT(&rule->list); |
| 9465 | return rule; |
| 9466 | |
| 9467 | missing_arg: |
| 9468 | memprintf(errmsg, "missing argument for '%s'", args[cur_arg]); |
| 9469 | return NULL; |
| 9470 | } |
| 9471 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9472 | /************************************************************************/ |
| 9473 | /* The code below is dedicated to ACL parsing and matching */ |
| 9474 | /************************************************************************/ |
| 9475 | |
| 9476 | |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9477 | /* This function ensures that the prerequisites for an L7 fetch are ready, |
| 9478 | * which means that a request or response is ready. If some data is missing, |
| 9479 | * 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] | 9480 | * to extract data from L7. If <req_vol> is non-null during a request prefetch, |
| 9481 | * another test is made to ensure the required information is not gone. |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9482 | * |
| 9483 | * The function returns : |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9484 | * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to |
| 9485 | * decide whether or not an HTTP message is present ; |
| 9486 | * 0 if the requested data cannot be fetched or if it is certain that |
| 9487 | * we'll never have any HTTP message there ; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9488 | * 1 if an HTTP message is ready |
| 9489 | */ |
James Rosewell | 91a41cb | 2015-09-18 17:11:16 +0100 | [diff] [blame] | 9490 | 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] | 9491 | const struct arg *args, struct sample *smp, int req_vol) |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9492 | { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 9493 | struct http_txn *txn; |
| 9494 | struct http_msg *msg; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9495 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 9496 | /* Note: it is possible that <s> is NULL when called before stream |
| 9497 | * initialization (eg: tcp-request connection), so this function is the |
| 9498 | * one responsible for guarding against this case for all HTTP users. |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9499 | */ |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 9500 | if (!s) |
| 9501 | return 0; |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 9502 | |
Thierry FOURNIER | ed08d6a | 2015-09-24 08:40:18 +0200 | [diff] [blame] | 9503 | if (!s->txn) { |
| 9504 | if (unlikely(!http_alloc_txn(s))) |
| 9505 | return 0; /* not enough memory */ |
| 9506 | http_init_txn(s); |
| 9507 | } |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 9508 | txn = s->txn; |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 9509 | msg = &txn->req; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9510 | |
| 9511 | /* Check for a dependency on a request */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9512 | smp->data.type = SMP_T_BOOL; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9513 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 9514 | if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 9515 | /* If the buffer does not leave enough free space at the end, |
| 9516 | * we must first realign it. |
| 9517 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 9518 | if (s->req.buf->p > s->req.buf->data && |
| 9519 | s->req.buf->i + s->req.buf->p > s->req.buf->data + s->req.buf->size - global.tune.maxrewrite) |
| 9520 | buffer_slow_realign(s->req.buf); |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 9521 | |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9522 | if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 472b1ee | 2013-10-14 22:41:30 +0200 | [diff] [blame] | 9523 | if (msg->msg_state == HTTP_MSG_ERROR) |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9524 | return 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9525 | |
| 9526 | /* Try to decode HTTP request */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 9527 | if (likely(msg->next < s->req.buf->i)) |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9528 | http_msg_analyzer(msg, &txn->hdr_idx); |
| 9529 | |
| 9530 | /* Still no valid request ? */ |
| 9531 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 3bf1b2b | 2012-08-27 20:46:07 +0200 | [diff] [blame] | 9532 | if ((msg->msg_state == HTTP_MSG_ERROR) || |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 9533 | buffer_full(s->req.buf, global.tune.maxrewrite)) { |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9534 | return 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9535 | } |
| 9536 | /* wait for final state */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9537 | smp->flags |= SMP_F_MAY_CHANGE; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9538 | return 0; |
| 9539 | } |
| 9540 | |
| 9541 | /* OK we just got a valid HTTP request. We have some minor |
| 9542 | * preparation to perform so that further checks can rely |
| 9543 | * on HTTP tests. |
| 9544 | */ |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 9545 | |
| 9546 | /* If the request was parsed but was too large, we must absolutely |
| 9547 | * return an error so that it is not processed. At the moment this |
| 9548 | * cannot happen, but if the parsers are to change in the future, |
| 9549 | * we want this check to be maintained. |
| 9550 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 9551 | if (unlikely(s->req.buf->i + s->req.buf->p > |
| 9552 | s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)) { |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 9553 | msg->err_state = msg->msg_state; |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 9554 | msg->msg_state = HTTP_MSG_ERROR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9555 | smp->data.u.sint = 1; |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 9556 | return 1; |
| 9557 | } |
| 9558 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9559 | txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l); |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9560 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 9561 | s->flags |= SF_REDIRECTABLE; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9562 | |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9563 | if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn)) |
| 9564 | return 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9565 | } |
| 9566 | |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9567 | if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) { |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9568 | return 0; /* data might have moved and indexes changed */ |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9569 | } |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9570 | |
| 9571 | /* otherwise everything's ready for the request */ |
| 9572 | } |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 9573 | else { |
| 9574 | /* Check for a dependency on a response */ |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9575 | if (txn->rsp.msg_state < HTTP_MSG_BODY) { |
| 9576 | smp->flags |= SMP_F_MAY_CHANGE; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9577 | return 0; |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9578 | } |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9579 | } |
| 9580 | |
| 9581 | /* everything's OK */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9582 | smp->data.u.sint = 1; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9583 | return 1; |
| 9584 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9585 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9586 | /* 1. Check on METHOD |
| 9587 | * We use the pre-parsed method if it is known, and store its number as an |
| 9588 | * integer. If it is unknown, we use the pointer and the length. |
| 9589 | */ |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 9590 | 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] | 9591 | { |
| 9592 | int len, meth; |
| 9593 | |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 9594 | len = strlen(text); |
| 9595 | meth = find_http_meth(text, len); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9596 | |
| 9597 | pattern->val.i = meth; |
| 9598 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | 912c119 | 2014-08-29 15:15:50 +0200 | [diff] [blame] | 9599 | pattern->ptr.str = (char *)text; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9600 | pattern->len = len; |
| 9601 | } |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 9602 | else { |
| 9603 | pattern->ptr.str = NULL; |
| 9604 | pattern->len = 0; |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 9605 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9606 | return 1; |
| 9607 | } |
| 9608 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 9609 | /* This function fetches the method of current HTTP request and stores |
| 9610 | * it in the global pattern struct as a chunk. There are two possibilities : |
| 9611 | * - if the method is known (not HTTP_METH_OTHER), its identifier is stored |
| 9612 | * in <len> and <ptr> is NULL ; |
| 9613 | * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and |
| 9614 | * <len> to its length. |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 9615 | * This is intended to be used with pat_match_meth() only. |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 9616 | */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 9617 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9618 | 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] | 9619 | { |
| 9620 | int meth; |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 9621 | struct http_txn *txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9622 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 9623 | CHECK_HTTP_MESSAGE_FIRST_PERM(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9624 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 9625 | txn = smp->strm->txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9626 | meth = txn->meth; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9627 | smp->data.type = SMP_T_METH; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9628 | smp->data.u.meth.meth = meth; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9629 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9630 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 9631 | /* ensure the indexes are not affected */ |
| 9632 | return 0; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9633 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9634 | smp->data.u.meth.str.len = txn->req.sl.rq.m_l; |
| 9635 | smp->data.u.meth.str.str = txn->req.chn->buf->p; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9636 | } |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9637 | smp->flags |= SMP_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9638 | return 1; |
| 9639 | } |
| 9640 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 9641 | /* See above how the method is stored in the global pattern */ |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 9642 | 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] | 9643 | { |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 9644 | int icase; |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 9645 | struct pattern_list *lst; |
| 9646 | struct pattern *pattern; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 9647 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 9648 | list_for_each_entry(lst, &expr->patterns, list) { |
| 9649 | pattern = &lst->pat; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9650 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 9651 | /* well-known method */ |
| 9652 | if (pattern->val.i != HTTP_METH_OTHER) { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9653 | if (smp->data.u.meth.meth == pattern->val.i) |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 9654 | return pattern; |
| 9655 | else |
| 9656 | continue; |
| 9657 | } |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 9658 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 9659 | /* Other method, we must compare the strings */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9660 | if (pattern->len != smp->data.u.meth.str.len) |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 9661 | continue; |
| 9662 | |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 9663 | icase = expr->mflags & PAT_MF_IGNORE_CASE; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9664 | if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.meth.str.str, smp->data.u.meth.str.len) == 0) || |
| 9665 | (!icase && strncmp(pattern->ptr.str, smp->data.u.meth.str.str, smp->data.u.meth.str.len) == 0)) |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 9666 | return pattern; |
| 9667 | } |
| 9668 | return NULL; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9669 | } |
| 9670 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 9671 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9672 | 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] | 9673 | { |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 9674 | struct http_txn *txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9675 | char *ptr; |
| 9676 | int len; |
| 9677 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9678 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9679 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 9680 | txn = smp->strm->txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9681 | len = txn->req.sl.rq.v_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9682 | ptr = txn->req.chn->buf->p + txn->req.sl.rq.v; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9683 | |
| 9684 | while ((len-- > 0) && (*ptr++ != '/')); |
| 9685 | if (len <= 0) |
| 9686 | return 0; |
| 9687 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9688 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9689 | smp->data.u.str.str = ptr; |
| 9690 | smp->data.u.str.len = len; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9691 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9692 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9693 | return 1; |
| 9694 | } |
| 9695 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 9696 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9697 | 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] | 9698 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 9699 | struct http_txn *txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9700 | char *ptr; |
| 9701 | int len; |
| 9702 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9703 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9704 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 9705 | txn = smp->strm->txn; |
Willy Tarreau | f26b252 | 2012-12-14 08:33:14 +0100 | [diff] [blame] | 9706 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
| 9707 | return 0; |
| 9708 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9709 | len = txn->rsp.sl.st.v_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9710 | ptr = txn->rsp.chn->buf->p; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9711 | |
| 9712 | while ((len-- > 0) && (*ptr++ != '/')); |
| 9713 | if (len <= 0) |
| 9714 | return 0; |
| 9715 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9716 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9717 | smp->data.u.str.str = ptr; |
| 9718 | smp->data.u.str.len = len; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9719 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9720 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9721 | return 1; |
| 9722 | } |
| 9723 | |
| 9724 | /* 3. Check on Status Code. We manipulate integers here. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 9725 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9726 | 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] | 9727 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 9728 | struct http_txn *txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9729 | char *ptr; |
| 9730 | int len; |
| 9731 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9732 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9733 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 9734 | txn = smp->strm->txn; |
Willy Tarreau | f26b252 | 2012-12-14 08:33:14 +0100 | [diff] [blame] | 9735 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
| 9736 | return 0; |
| 9737 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9738 | len = txn->rsp.sl.st.c_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9739 | ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9740 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9741 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9742 | smp->data.u.sint = __strl2ui(ptr, len); |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9743 | smp->flags = SMP_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9744 | return 1; |
| 9745 | } |
| 9746 | |
Thierry Fournier | f4011dd | 2016-03-29 17:23:51 +0200 | [diff] [blame] | 9747 | static int |
| 9748 | smp_fetch_uniqueid(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 9749 | { |
| 9750 | if (LIST_ISEMPTY(&smp->sess->fe->format_unique_id)) |
| 9751 | return 0; |
| 9752 | |
| 9753 | if (!smp->strm->unique_id) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 9754 | if ((smp->strm->unique_id = pool_alloc(pool_head_uniqueid)) == NULL) |
Thierry Fournier | f4011dd | 2016-03-29 17:23:51 +0200 | [diff] [blame] | 9755 | return 0; |
| 9756 | smp->strm->unique_id[0] = '\0'; |
| 9757 | } |
| 9758 | smp->data.u.str.len = build_logline(smp->strm, smp->strm->unique_id, |
| 9759 | UNIQUEID_LEN, &smp->sess->fe->format_unique_id); |
| 9760 | |
| 9761 | smp->data.type = SMP_T_STR; |
| 9762 | smp->data.u.str.str = smp->strm->unique_id; |
| 9763 | smp->flags = SMP_F_CONST; |
| 9764 | return 1; |
| 9765 | } |
| 9766 | |
Thierry FOURNIER | d7d8881 | 2017-04-19 15:15:14 +0200 | [diff] [blame] | 9767 | /* Returns a string block containing all headers including the |
| 9768 | * empty line wich separes headers from the body. This is useful |
| 9769 | * form some headers analysis. |
| 9770 | */ |
| 9771 | static int |
| 9772 | smp_fetch_hdrs(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 9773 | { |
| 9774 | struct http_msg *msg; |
| 9775 | struct hdr_idx *idx; |
| 9776 | struct http_txn *txn; |
| 9777 | |
| 9778 | CHECK_HTTP_MESSAGE_FIRST(); |
| 9779 | |
| 9780 | txn = smp->strm->txn; |
| 9781 | idx = &txn->hdr_idx; |
| 9782 | msg = &txn->req; |
| 9783 | |
| 9784 | smp->data.type = SMP_T_STR; |
| 9785 | smp->data.u.str.str = msg->chn->buf->p + hdr_idx_first_pos(idx); |
| 9786 | smp->data.u.str.len = msg->eoh - hdr_idx_first_pos(idx) + 1 + |
| 9787 | (msg->chn->buf->p[msg->eoh] == '\r'); |
| 9788 | |
| 9789 | return 1; |
| 9790 | } |
| 9791 | |
Thierry FOURNIER | 5617dce | 2017-04-09 05:38:19 +0200 | [diff] [blame] | 9792 | /* Returns the header request in a length/value encoded format. |
| 9793 | * This is useful for exchanges with the SPOE. |
| 9794 | * |
| 9795 | * A "length value" is a multibyte code encoding numbers. It uses the |
| 9796 | * SPOE format. The encoding is the following: |
| 9797 | * |
| 9798 | * Each couple "header name" / "header value" is composed |
| 9799 | * like this: |
| 9800 | * "length value" "header name bytes" |
| 9801 | * "length value" "header value bytes" |
| 9802 | * When the last header is reached, the header name and the header |
| 9803 | * value are empty. Their length are 0 |
| 9804 | */ |
| 9805 | static int |
| 9806 | smp_fetch_hdrs_bin(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 9807 | { |
| 9808 | struct http_msg *msg; |
| 9809 | struct chunk *temp; |
| 9810 | struct hdr_idx *idx; |
| 9811 | const char *cur_ptr, *cur_next, *p; |
| 9812 | int old_idx, cur_idx; |
| 9813 | struct hdr_idx_elem *cur_hdr; |
| 9814 | const char *hn, *hv; |
| 9815 | int hnl, hvl; |
| 9816 | int ret; |
| 9817 | struct http_txn *txn; |
| 9818 | char *buf; |
| 9819 | char *end; |
| 9820 | |
| 9821 | CHECK_HTTP_MESSAGE_FIRST(); |
| 9822 | |
| 9823 | temp = get_trash_chunk(); |
| 9824 | buf = temp->str; |
| 9825 | end = temp->str + temp->size; |
| 9826 | |
| 9827 | txn = smp->strm->txn; |
| 9828 | idx = &txn->hdr_idx; |
| 9829 | msg = &txn->req; |
| 9830 | |
| 9831 | /* Build array of headers. */ |
| 9832 | old_idx = 0; |
| 9833 | cur_next = msg->chn->buf->p + hdr_idx_first_pos(idx); |
| 9834 | while (1) { |
| 9835 | cur_idx = idx->v[old_idx].next; |
| 9836 | if (!cur_idx) |
| 9837 | break; |
| 9838 | old_idx = cur_idx; |
| 9839 | |
| 9840 | cur_hdr = &idx->v[cur_idx]; |
| 9841 | cur_ptr = cur_next; |
| 9842 | cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1; |
| 9843 | |
| 9844 | /* Now we have one full header at cur_ptr of len cur_hdr->len, |
| 9845 | * and the next header starts at cur_next. We'll check |
| 9846 | * this header in the list as well as against the default |
| 9847 | * rule. |
| 9848 | */ |
| 9849 | |
| 9850 | /* look for ': *'. */ |
| 9851 | hn = cur_ptr; |
| 9852 | for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++); |
| 9853 | if (p >= cur_ptr+cur_hdr->len) |
| 9854 | continue; |
| 9855 | hnl = p - hn; |
| 9856 | p++; |
| 9857 | while (p < cur_ptr + cur_hdr->len && (*p == ' ' || *p == '\t')) |
| 9858 | p++; |
| 9859 | if (p >= cur_ptr + cur_hdr->len) |
| 9860 | continue; |
| 9861 | hv = p; |
| 9862 | hvl = cur_ptr + cur_hdr->len-p; |
| 9863 | |
| 9864 | /* encode the header name. */ |
| 9865 | ret = encode_varint(hnl, &buf, end); |
| 9866 | if (ret == -1) |
| 9867 | return 0; |
| 9868 | if (buf + hnl > end) |
| 9869 | return 0; |
| 9870 | memcpy(buf, hn, hnl); |
| 9871 | buf += hnl; |
| 9872 | |
| 9873 | /* encode and copy the value. */ |
| 9874 | ret = encode_varint(hvl, &buf, end); |
| 9875 | if (ret == -1) |
| 9876 | return 0; |
| 9877 | if (buf + hvl > end) |
| 9878 | return 0; |
| 9879 | memcpy(buf, hv, hvl); |
| 9880 | buf += hvl; |
| 9881 | } |
| 9882 | |
| 9883 | /* encode the end of the header list with empty |
| 9884 | * header name and header value. |
| 9885 | */ |
| 9886 | ret = encode_varint(0, &buf, end); |
| 9887 | if (ret == -1) |
| 9888 | return 0; |
| 9889 | ret = encode_varint(0, &buf, end); |
| 9890 | if (ret == -1) |
| 9891 | return 0; |
| 9892 | |
| 9893 | /* Initialise sample data which will be filled. */ |
| 9894 | smp->data.type = SMP_T_BIN; |
| 9895 | smp->data.u.str.str = temp->str; |
| 9896 | smp->data.u.str.len = buf - temp->str; |
| 9897 | smp->data.u.str.size = temp->size; |
| 9898 | |
| 9899 | return 1; |
| 9900 | } |
| 9901 | |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9902 | /* returns the longest available part of the body. This requires that the body |
| 9903 | * has been waited for using http-buffer-request. |
| 9904 | */ |
| 9905 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9906 | 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] | 9907 | { |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9908 | struct http_msg *msg; |
| 9909 | unsigned long len; |
| 9910 | unsigned long block1; |
| 9911 | char *body; |
| 9912 | struct chunk *temp; |
| 9913 | |
| 9914 | CHECK_HTTP_MESSAGE_FIRST(); |
| 9915 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9916 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 9917 | msg = &smp->strm->txn->req; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9918 | else |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 9919 | msg = &smp->strm->txn->rsp; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9920 | |
| 9921 | len = http_body_bytes(msg); |
| 9922 | body = b_ptr(msg->chn->buf, -http_data_rewind(msg)); |
| 9923 | |
| 9924 | block1 = len; |
| 9925 | if (block1 > msg->chn->buf->data + msg->chn->buf->size - body) |
| 9926 | block1 = msg->chn->buf->data + msg->chn->buf->size - body; |
| 9927 | |
| 9928 | if (block1 == len) { |
| 9929 | /* buffer is not wrapped (or empty) */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9930 | smp->data.type = SMP_T_BIN; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9931 | smp->data.u.str.str = body; |
| 9932 | smp->data.u.str.len = len; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9933 | smp->flags = SMP_F_VOL_TEST | SMP_F_CONST; |
| 9934 | } |
| 9935 | else { |
| 9936 | /* buffer is wrapped, we need to defragment it */ |
| 9937 | temp = get_trash_chunk(); |
| 9938 | memcpy(temp->str, body, block1); |
| 9939 | memcpy(temp->str + block1, msg->chn->buf->data, len - block1); |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9940 | smp->data.type = SMP_T_BIN; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9941 | smp->data.u.str.str = temp->str; |
| 9942 | smp->data.u.str.len = len; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9943 | smp->flags = SMP_F_VOL_TEST; |
| 9944 | } |
| 9945 | return 1; |
| 9946 | } |
| 9947 | |
| 9948 | |
| 9949 | /* returns the available length of the body. This requires that the body |
| 9950 | * has been waited for using http-buffer-request. |
| 9951 | */ |
| 9952 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9953 | 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] | 9954 | { |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9955 | struct http_msg *msg; |
| 9956 | |
| 9957 | CHECK_HTTP_MESSAGE_FIRST(); |
| 9958 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9959 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 9960 | msg = &smp->strm->txn->req; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9961 | else |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 9962 | msg = &smp->strm->txn->rsp; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9963 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9964 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9965 | smp->data.u.sint = http_body_bytes(msg); |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9966 | |
| 9967 | smp->flags = SMP_F_VOL_TEST; |
| 9968 | return 1; |
| 9969 | } |
| 9970 | |
| 9971 | |
| 9972 | /* returns the advertised length of the body, or the advertised size of the |
| 9973 | * chunks available in the buffer. This requires that the body has been waited |
| 9974 | * for using http-buffer-request. |
| 9975 | */ |
| 9976 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9977 | 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] | 9978 | { |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9979 | struct http_msg *msg; |
| 9980 | |
| 9981 | CHECK_HTTP_MESSAGE_FIRST(); |
| 9982 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9983 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 9984 | msg = &smp->strm->txn->req; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9985 | else |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 9986 | msg = &smp->strm->txn->rsp; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9987 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9988 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9989 | smp->data.u.sint = msg->body_len; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9990 | |
| 9991 | smp->flags = SMP_F_VOL_TEST; |
| 9992 | return 1; |
| 9993 | } |
| 9994 | |
| 9995 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9996 | /* 4. Check on URL/URI. A pointer to the URI is stored. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 9997 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9998 | 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] | 9999 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10000 | struct http_txn *txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10001 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10002 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10003 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10004 | txn = smp->strm->txn; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10005 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10006 | smp->data.u.str.len = txn->req.sl.rq.u_l; |
| 10007 | smp->data.u.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10008 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10009 | return 1; |
| 10010 | } |
| 10011 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10012 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10013 | 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] | 10014 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10015 | struct http_txn *txn; |
Willy Tarreau | 4c804ec | 2013-09-30 14:37:14 +0200 | [diff] [blame] | 10016 | struct sockaddr_storage addr; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10017 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10018 | CHECK_HTTP_MESSAGE_FIRST(); |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10019 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10020 | txn = smp->strm->txn; |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 10021 | url2sa(txn->req.chn->buf->p + 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] | 10022 | if (((struct sockaddr_in *)&addr)->sin_family != AF_INET) |
Willy Tarreau | f4362b3 | 2011-12-16 17:49:52 +0100 | [diff] [blame] | 10023 | return 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10024 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10025 | smp->data.type = SMP_T_IPV4; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10026 | smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10027 | smp->flags = 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10028 | return 1; |
| 10029 | } |
| 10030 | |
| 10031 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10032 | 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] | 10033 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10034 | struct http_txn *txn; |
Willy Tarreau | 4c804ec | 2013-09-30 14:37:14 +0200 | [diff] [blame] | 10035 | struct sockaddr_storage addr; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10036 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10037 | CHECK_HTTP_MESSAGE_FIRST(); |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10038 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10039 | txn = smp->strm->txn; |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 10040 | url2sa(txn->req.chn->buf->p + 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] | 10041 | if (((struct sockaddr_in *)&addr)->sin_family != AF_INET) |
| 10042 | return 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10043 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10044 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10045 | smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port); |
Willy Tarreau | 21e5b0e | 2012-04-23 19:25:44 +0200 | [diff] [blame] | 10046 | smp->flags = 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10047 | return 1; |
| 10048 | } |
| 10049 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10050 | /* Fetch an HTTP header. A pointer to the beginning of the value is returned. |
| 10051 | * Accepts an optional argument of type string containing the header field name, |
| 10052 | * and an optional argument of type signed or unsigned integer to request an |
| 10053 | * 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] | 10054 | * headers are considered from the first one. It does not stop on commas and |
| 10055 | * returns full lines instead (useful for User-Agent or Date for example). |
| 10056 | */ |
| 10057 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10058 | 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] | 10059 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10060 | struct hdr_idx *idx; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10061 | struct hdr_ctx *ctx = smp->ctx.a[0]; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10062 | const struct http_msg *msg; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10063 | int occ = 0; |
| 10064 | const char *name_str = NULL; |
| 10065 | int name_len = 0; |
| 10066 | |
| 10067 | if (!ctx) { |
| 10068 | /* first call */ |
| 10069 | ctx = &static_hdr_ctx; |
| 10070 | ctx->idx = 0; |
| 10071 | smp->ctx.a[0] = ctx; |
| 10072 | } |
| 10073 | |
| 10074 | if (args) { |
| 10075 | if (args[0].type != ARGT_STR) |
| 10076 | return 0; |
| 10077 | name_str = args[0].data.str.str; |
| 10078 | name_len = args[0].data.str.len; |
| 10079 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 10080 | if (args[1].type == ARGT_SINT) |
| 10081 | occ = args[1].data.sint; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10082 | } |
| 10083 | |
| 10084 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10085 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10086 | idx = &smp->strm->txn->hdr_idx; |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10087 | 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] | 10088 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10089 | if (ctx && !(smp->flags & SMP_F_NOT_LAST)) |
| 10090 | /* search for header from the beginning */ |
| 10091 | ctx->idx = 0; |
| 10092 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10093 | if (!occ && !(smp->opt & SMP_OPT_ITERATE)) |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10094 | /* no explicit occurrence and single fetch => last header by default */ |
| 10095 | occ = -1; |
| 10096 | |
| 10097 | if (!occ) |
| 10098 | /* prepare to report multiple occurrences for ACL fetches */ |
| 10099 | smp->flags |= SMP_F_NOT_LAST; |
| 10100 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10101 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10102 | smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10103 | if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.u.str.str, &smp->data.u.str.len)) |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10104 | return 1; |
| 10105 | |
| 10106 | smp->flags &= ~SMP_F_NOT_LAST; |
| 10107 | return 0; |
| 10108 | } |
| 10109 | |
| 10110 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
| 10111 | * Accepts exactly 1 argument of type string. It does not stop on commas and |
| 10112 | * returns full lines instead (useful for User-Agent or Date for example). |
| 10113 | */ |
| 10114 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10115 | 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] | 10116 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10117 | struct hdr_idx *idx; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10118 | struct hdr_ctx ctx; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10119 | const struct http_msg *msg; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10120 | int cnt; |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10121 | const char *name = NULL; |
| 10122 | int len = 0; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10123 | |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10124 | if (args && args->type == ARGT_STR) { |
| 10125 | name = args->data.str.str; |
| 10126 | len = args->data.str.len; |
| 10127 | } |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10128 | |
| 10129 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10130 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10131 | idx = &smp->strm->txn->hdr_idx; |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10132 | 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] | 10133 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10134 | ctx.idx = 0; |
| 10135 | cnt = 0; |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10136 | while (http_find_full_header2(name, len, msg->chn->buf->p, idx, &ctx)) |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10137 | cnt++; |
| 10138 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10139 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10140 | smp->data.u.sint = cnt; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10141 | smp->flags = SMP_F_VOL_HDR; |
| 10142 | return 1; |
| 10143 | } |
| 10144 | |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 10145 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10146 | 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] | 10147 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10148 | struct hdr_idx *idx; |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 10149 | struct hdr_ctx ctx; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10150 | const struct http_msg *msg; |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 10151 | struct chunk *temp; |
| 10152 | char del = ','; |
| 10153 | |
| 10154 | if (args && args->type == ARGT_STR) |
| 10155 | del = *args[0].data.str.str; |
| 10156 | |
| 10157 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10158 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10159 | idx = &smp->strm->txn->hdr_idx; |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10160 | 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] | 10161 | |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 10162 | temp = get_trash_chunk(); |
| 10163 | |
| 10164 | ctx.idx = 0; |
| 10165 | while (http_find_next_header(msg->chn->buf->p, idx, &ctx)) { |
| 10166 | if (temp->len) |
| 10167 | temp->str[temp->len++] = del; |
| 10168 | memcpy(temp->str + temp->len, ctx.line, ctx.del); |
| 10169 | temp->len += ctx.del; |
| 10170 | } |
| 10171 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10172 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10173 | smp->data.u.str.str = temp->str; |
| 10174 | smp->data.u.str.len = temp->len; |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 10175 | smp->flags = SMP_F_VOL_HDR; |
| 10176 | return 1; |
| 10177 | } |
| 10178 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10179 | /* Fetch an HTTP header. A pointer to the beginning of the value is returned. |
| 10180 | * Accepts an optional argument of type string containing the header field name, |
| 10181 | * and an optional argument of type signed or unsigned integer to request an |
| 10182 | * 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] | 10183 | * headers are considered from the first one. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10184 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10185 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10186 | 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] | 10187 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10188 | struct hdr_idx *idx; |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 10189 | struct hdr_ctx *ctx = smp->ctx.a[0]; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10190 | const struct http_msg *msg; |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10191 | int occ = 0; |
| 10192 | const char *name_str = NULL; |
| 10193 | int name_len = 0; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10194 | |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 10195 | if (!ctx) { |
| 10196 | /* first call */ |
| 10197 | ctx = &static_hdr_ctx; |
| 10198 | ctx->idx = 0; |
Willy Tarreau | ffb6f08 | 2013-04-02 23:16:53 +0200 | [diff] [blame] | 10199 | smp->ctx.a[0] = ctx; |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 10200 | } |
| 10201 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10202 | if (args) { |
| 10203 | if (args[0].type != ARGT_STR) |
| 10204 | return 0; |
| 10205 | name_str = args[0].data.str.str; |
| 10206 | name_len = args[0].data.str.len; |
| 10207 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 10208 | if (args[1].type == ARGT_SINT) |
| 10209 | occ = args[1].data.sint; |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10210 | } |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10211 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10212 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10213 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10214 | idx = &smp->strm->txn->hdr_idx; |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10215 | 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] | 10216 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10217 | if (ctx && !(smp->flags & SMP_F_NOT_LAST)) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10218 | /* search for header from the beginning */ |
| 10219 | ctx->idx = 0; |
| 10220 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10221 | if (!occ && !(smp->opt & SMP_OPT_ITERATE)) |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10222 | /* no explicit occurrence and single fetch => last header by default */ |
| 10223 | occ = -1; |
| 10224 | |
| 10225 | if (!occ) |
| 10226 | /* prepare to report multiple occurrences for ACL fetches */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10227 | smp->flags |= SMP_F_NOT_LAST; |
Willy Tarreau | 664092c | 2011-12-16 19:11:42 +0100 | [diff] [blame] | 10228 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10229 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10230 | smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10231 | if (http_get_hdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.u.str.str, &smp->data.u.str.len)) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10232 | return 1; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10233 | |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10234 | smp->flags &= ~SMP_F_NOT_LAST; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10235 | return 0; |
| 10236 | } |
| 10237 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10238 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10239 | * Accepts exactly 1 argument of type string. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10240 | */ |
| 10241 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10242 | 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] | 10243 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10244 | struct hdr_idx *idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10245 | struct hdr_ctx ctx; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10246 | const struct http_msg *msg; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10247 | int cnt; |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10248 | const char *name = NULL; |
| 10249 | int len = 0; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10250 | |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10251 | if (args && args->type == ARGT_STR) { |
| 10252 | name = args->data.str.str; |
| 10253 | len = args->data.str.len; |
| 10254 | } |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10255 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10256 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10257 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10258 | idx = &smp->strm->txn->hdr_idx; |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10259 | 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] | 10260 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10261 | ctx.idx = 0; |
| 10262 | cnt = 0; |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10263 | while (http_find_header2(name, len, msg->chn->buf->p, idx, &ctx)) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10264 | cnt++; |
| 10265 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10266 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10267 | smp->data.u.sint = cnt; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10268 | smp->flags = SMP_F_VOL_HDR; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10269 | return 1; |
| 10270 | } |
| 10271 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10272 | /* Fetch an HTTP header's integer value. The integer value is returned. It |
| 10273 | * takes a mandatory argument of type string and an optional one of type int |
| 10274 | * to designate a specific occurrence. It returns an unsigned integer, which |
| 10275 | * may or may not be appropriate for everything. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10276 | */ |
| 10277 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10278 | 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] | 10279 | { |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10280 | int ret = smp_fetch_hdr(args, smp, kw, private); |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10281 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10282 | if (ret > 0) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10283 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10284 | smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len); |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10285 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10286 | |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10287 | return ret; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10288 | } |
| 10289 | |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 10290 | /* Fetch an HTTP header's IP value. takes a mandatory argument of type string |
| 10291 | * and an optional one of type int to designate a specific occurrence. |
| 10292 | * It returns an IPv4 or IPv6 address. |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 10293 | */ |
| 10294 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10295 | 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] | 10296 | { |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10297 | int ret; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10298 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10299 | while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10300 | if (url2ipv4((char *)smp->data.u.str.str, &smp->data.u.ipv4)) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10301 | smp->data.type = SMP_T_IPV4; |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10302 | break; |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 10303 | } else { |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 10304 | struct chunk *temp = get_trash_chunk(); |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10305 | if (smp->data.u.str.len < temp->size - 1) { |
| 10306 | memcpy(temp->str, smp->data.u.str.str, smp->data.u.str.len); |
| 10307 | temp->str[smp->data.u.str.len] = '\0'; |
| 10308 | if (inet_pton(AF_INET6, temp->str, &smp->data.u.ipv6)) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10309 | smp->data.type = SMP_T_IPV6; |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 10310 | break; |
| 10311 | } |
| 10312 | } |
| 10313 | } |
| 10314 | |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10315 | /* if the header doesn't match an IP address, fetch next one */ |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10316 | if (!(smp->flags & SMP_F_NOT_LAST)) |
| 10317 | return 0; |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 10318 | } |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10319 | return ret; |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 10320 | } |
| 10321 | |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10322 | /* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at |
| 10323 | * the first '/' after the possible hostname, and ends before the possible '?'. |
| 10324 | */ |
| 10325 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10326 | 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] | 10327 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10328 | struct http_txn *txn; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10329 | char *ptr, *end; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10330 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10331 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10332 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10333 | txn = smp->strm->txn; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10334 | end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 10335 | ptr = http_get_path(txn); |
| 10336 | if (!ptr) |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10337 | return 0; |
| 10338 | |
| 10339 | /* OK, we got the '/' ! */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10340 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10341 | smp->data.u.str.str = ptr; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10342 | |
| 10343 | while (ptr < end && *ptr != '?') |
| 10344 | ptr++; |
| 10345 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10346 | smp->data.u.str.len = ptr - smp->data.u.str.str; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10347 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10348 | return 1; |
| 10349 | } |
| 10350 | |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10351 | /* This produces a concatenation of the first occurrence of the Host header |
| 10352 | * followed by the path component if it begins with a slash ('/'). This means |
| 10353 | * that '*' will not be added, resulting in exactly the first Host entry. |
| 10354 | * If no Host header is found, then the path is returned as-is. The returned |
| 10355 | * 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] | 10356 | * The returned sample is of type string. |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10357 | */ |
| 10358 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10359 | 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] | 10360 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10361 | struct http_txn *txn; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10362 | char *ptr, *end, *beg; |
| 10363 | struct hdr_ctx ctx; |
Willy Tarreau | 3caf2af | 2014-06-24 17:27:02 +0200 | [diff] [blame] | 10364 | struct chunk *temp; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10365 | |
| 10366 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10367 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10368 | txn = smp->strm->txn; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10369 | ctx.idx = 0; |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 10370 | if (!http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx) || !ctx.vlen) |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10371 | return smp_fetch_path(args, smp, kw, private); |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10372 | |
| 10373 | /* 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] | 10374 | temp = get_trash_chunk(); |
| 10375 | memcpy(temp->str, ctx.line + ctx.val, ctx.vlen); |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10376 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10377 | smp->data.u.str.str = temp->str; |
| 10378 | smp->data.u.str.len = ctx.vlen; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10379 | |
| 10380 | /* now retrieve the path */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 10381 | end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10382 | beg = http_get_path(txn); |
| 10383 | if (!beg) |
| 10384 | beg = end; |
| 10385 | |
| 10386 | for (ptr = beg; ptr < end && *ptr != '?'; ptr++); |
| 10387 | |
| 10388 | if (beg < ptr && *beg == '/') { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10389 | memcpy(smp->data.u.str.str + smp->data.u.str.len, beg, ptr - beg); |
| 10390 | smp->data.u.str.len += ptr - beg; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10391 | } |
| 10392 | |
| 10393 | smp->flags = SMP_F_VOL_1ST; |
| 10394 | return 1; |
| 10395 | } |
| 10396 | |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 10397 | /* This produces a 32-bit hash of the concatenation of the first occurrence of |
| 10398 | * the Host header followed by the path component if it begins with a slash ('/'). |
| 10399 | * This means that '*' will not be added, resulting in exactly the first Host |
| 10400 | * 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] | 10401 | * is hashed using the path hash followed by a full avalanche hash and provides a |
| 10402 | * 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] | 10403 | * high-traffic sites without having to store whole paths. |
| 10404 | */ |
Thierry FOURNIER | 055b9d5 | 2014-07-15 16:11:07 +0200 | [diff] [blame] | 10405 | int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10406 | 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] | 10407 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10408 | struct http_txn *txn; |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 10409 | struct hdr_ctx ctx; |
| 10410 | unsigned int hash = 0; |
| 10411 | char *ptr, *beg, *end; |
| 10412 | int len; |
| 10413 | |
| 10414 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10415 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10416 | txn = smp->strm->txn; |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 10417 | ctx.idx = 0; |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 10418 | if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 10419 | /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */ |
| 10420 | ptr = ctx.line + ctx.val; |
| 10421 | len = ctx.vlen; |
| 10422 | while (len--) |
| 10423 | hash = *(ptr++) + (hash << 6) + (hash << 16) - hash; |
| 10424 | } |
| 10425 | |
| 10426 | /* now retrieve the path */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 10427 | end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l; |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 10428 | beg = http_get_path(txn); |
| 10429 | if (!beg) |
| 10430 | beg = end; |
| 10431 | |
| 10432 | for (ptr = beg; ptr < end && *ptr != '?'; ptr++); |
| 10433 | |
| 10434 | if (beg < ptr && *beg == '/') { |
| 10435 | while (beg < ptr) |
| 10436 | hash = *(beg++) + (hash << 6) + (hash << 16) - hash; |
| 10437 | } |
| 10438 | hash = full_hash(hash); |
| 10439 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10440 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10441 | smp->data.u.sint = hash; |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 10442 | smp->flags = SMP_F_VOL_1ST; |
| 10443 | return 1; |
| 10444 | } |
| 10445 | |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10446 | /* 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] | 10447 | * path as returned by smp_fetch_base32(). The idea is to have per-source and |
| 10448 | * per-path counters. The result is a binary block from 8 to 20 bytes depending |
| 10449 | * 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] | 10450 | * that in environments where IPv6 is insignificant, truncating the output to |
| 10451 | * 8 bytes would still work. |
| 10452 | */ |
| 10453 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10454 | 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] | 10455 | { |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 10456 | struct chunk *temp; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10457 | struct connection *cli_conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10458 | |
| 10459 | if (!cli_conn) |
| 10460 | return 0; |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10461 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10462 | if (!smp_fetch_base32(args, smp, kw, private)) |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10463 | return 0; |
| 10464 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 10465 | temp = get_trash_chunk(); |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10466 | *(unsigned int *)temp->str = htonl(smp->data.u.sint); |
Willy Tarreau | 5ad6e1d | 2014-07-15 21:34:06 +0200 | [diff] [blame] | 10467 | temp->len += sizeof(unsigned int); |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10468 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10469 | switch (cli_conn->addr.from.ss_family) { |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10470 | case AF_INET: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10471 | memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4); |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10472 | temp->len += 4; |
| 10473 | break; |
| 10474 | case AF_INET6: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10475 | memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16); |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10476 | temp->len += 16; |
| 10477 | break; |
| 10478 | default: |
| 10479 | return 0; |
| 10480 | } |
| 10481 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10482 | smp->data.u.str = *temp; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10483 | smp->data.type = SMP_T_BIN; |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10484 | return 1; |
| 10485 | } |
| 10486 | |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 10487 | /* Extracts the query string, which comes after the question mark '?'. If no |
| 10488 | * question mark is found, nothing is returned. Otherwise it returns a sample |
| 10489 | * of type string carrying the whole query string. |
| 10490 | */ |
| 10491 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10492 | 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] | 10493 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10494 | struct http_txn *txn; |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 10495 | char *ptr, *end; |
| 10496 | |
| 10497 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10498 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10499 | txn = smp->strm->txn; |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 10500 | ptr = txn->req.chn->buf->p + txn->req.sl.rq.u; |
| 10501 | end = ptr + txn->req.sl.rq.u_l; |
| 10502 | |
| 10503 | /* look up the '?' */ |
| 10504 | do { |
| 10505 | if (ptr == end) |
| 10506 | return 0; |
| 10507 | } while (*ptr++ != '?'); |
| 10508 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10509 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10510 | smp->data.u.str.str = ptr; |
| 10511 | smp->data.u.str.len = end - ptr; |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 10512 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
| 10513 | return 1; |
| 10514 | } |
| 10515 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10516 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10517 | 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] | 10518 | { |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10519 | /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged |
| 10520 | * as a layer7 ACL, which involves automatic allocation of hdr_idx. |
| 10521 | */ |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10522 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10523 | CHECK_HTTP_MESSAGE_FIRST_PERM(); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10524 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10525 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10526 | smp->data.u.sint = 1; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10527 | return 1; |
| 10528 | } |
| 10529 | |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 10530 | /* return a valid test if the current request is the first one on the connection */ |
| 10531 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10532 | 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] | 10533 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10534 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10535 | smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST); |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 10536 | return 1; |
| 10537 | } |
| 10538 | |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10539 | /* Accepts exactly 1 argument of type userlist */ |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 10540 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10541 | 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] | 10542 | { |
| 10543 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10544 | if (!args || args->type != ARGT_USR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10545 | return 0; |
| 10546 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10547 | CHECK_HTTP_MESSAGE_FIRST(); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 10548 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10549 | if (!get_http_auth(smp->strm)) |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 10550 | return 0; |
| 10551 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10552 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10553 | 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] | 10554 | smp->strm->txn->auth.pass); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 10555 | return 1; |
| 10556 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10557 | |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10558 | /* Accepts exactly 1 argument of type userlist */ |
| 10559 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10560 | 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] | 10561 | { |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10562 | if (!args || args->type != ARGT_USR) |
| 10563 | return 0; |
| 10564 | |
| 10565 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10566 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10567 | if (!get_http_auth(smp->strm)) |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10568 | return 0; |
| 10569 | |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10570 | /* if the user does not belong to the userlist or has a wrong password, |
| 10571 | * report that it unconditionally does not match. Otherwise we return |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 10572 | * a string containing the username. |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10573 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10574 | if (!check_user(args->data.usr, smp->strm->txn->auth.user, |
| 10575 | smp->strm->txn->auth.pass)) |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 10576 | return 0; |
| 10577 | |
| 10578 | /* pat_match_auth() will need the user list */ |
| 10579 | smp->ctx.a[0] = args->data.usr; |
| 10580 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10581 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10582 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10583 | smp->data.u.str.str = smp->strm->txn->auth.user; |
| 10584 | smp->data.u.str.len = strlen(smp->strm->txn->auth.user); |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10585 | |
| 10586 | return 1; |
| 10587 | } |
| 10588 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10589 | /* Try to find the next occurrence of a cookie name in a cookie header value. |
| 10590 | * The lookup begins at <hdr>. The pointer and size of the next occurrence of |
| 10591 | * the cookie value is returned into *value and *value_l, and the function |
| 10592 | * returns a pointer to the next pointer to search from if the value was found. |
| 10593 | * Otherwise if the cookie was not found, NULL is returned and neither value |
| 10594 | * nor value_l are touched. The input <hdr> string should first point to the |
| 10595 | * header's value, and the <hdr_end> pointer must point to the first character |
| 10596 | * not part of the value. <list> must be non-zero if value may represent a list |
| 10597 | * of values (cookie headers). This makes it faster to abort parsing when no |
| 10598 | * list is expected. |
| 10599 | */ |
David Carlier | 4686f79 | 2015-09-25 14:10:50 +0100 | [diff] [blame] | 10600 | char * |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10601 | extract_cookie_value(char *hdr, const char *hdr_end, |
| 10602 | char *cookie_name, size_t cookie_name_l, int list, |
Willy Tarreau | 3fb818c | 2012-04-11 17:21:08 +0200 | [diff] [blame] | 10603 | char **value, int *value_l) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10604 | { |
| 10605 | char *equal, *att_end, *att_beg, *val_beg, *val_end; |
| 10606 | char *next; |
| 10607 | |
| 10608 | /* we search at least a cookie name followed by an equal, and more |
| 10609 | * generally something like this : |
| 10610 | * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n |
| 10611 | */ |
| 10612 | for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) { |
| 10613 | /* Iterate through all cookies on this line */ |
| 10614 | |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 10615 | while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg)) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10616 | att_beg++; |
| 10617 | |
| 10618 | /* find att_end : this is the first character after the last non |
| 10619 | * space before the equal. It may be equal to hdr_end. |
| 10620 | */ |
| 10621 | equal = att_end = att_beg; |
| 10622 | |
| 10623 | while (equal < hdr_end) { |
| 10624 | if (*equal == '=' || *equal == ';' || (list && *equal == ',')) |
| 10625 | break; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 10626 | if (HTTP_IS_SPHT(*equal++)) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10627 | continue; |
| 10628 | att_end = equal; |
| 10629 | } |
| 10630 | |
| 10631 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 10632 | * is between <att_beg> and <equal>, both may be identical. |
| 10633 | */ |
| 10634 | |
| 10635 | /* look for end of cookie if there is an equal sign */ |
| 10636 | if (equal < hdr_end && *equal == '=') { |
| 10637 | /* look for the beginning of the value */ |
| 10638 | val_beg = equal + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 10639 | while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg)) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10640 | val_beg++; |
| 10641 | |
| 10642 | /* find the end of the value, respecting quotes */ |
| 10643 | next = find_cookie_value_end(val_beg, hdr_end); |
| 10644 | |
| 10645 | /* make val_end point to the first white space or delimitor after the value */ |
| 10646 | val_end = next; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 10647 | while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1))) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10648 | val_end--; |
| 10649 | } else { |
| 10650 | val_beg = val_end = next = equal; |
| 10651 | } |
| 10652 | |
| 10653 | /* We have nothing to do with attributes beginning with '$'. However, |
| 10654 | * they will automatically be removed if a header before them is removed, |
| 10655 | * since they're supposed to be linked together. |
| 10656 | */ |
| 10657 | if (*att_beg == '$') |
| 10658 | continue; |
| 10659 | |
| 10660 | /* Ignore cookies with no equal sign */ |
| 10661 | if (equal == next) |
| 10662 | continue; |
| 10663 | |
| 10664 | /* Now we have the cookie name between att_beg and att_end, and |
| 10665 | * its value between val_beg and val_end. |
| 10666 | */ |
| 10667 | |
| 10668 | if (att_end - att_beg == cookie_name_l && |
| 10669 | memcmp(att_beg, cookie_name, cookie_name_l) == 0) { |
| 10670 | /* let's return this value and indicate where to go on from */ |
| 10671 | *value = val_beg; |
| 10672 | *value_l = val_end - val_beg; |
| 10673 | return next + 1; |
| 10674 | } |
| 10675 | |
| 10676 | /* Set-Cookie headers only have the name in the first attr=value part */ |
| 10677 | if (!list) |
| 10678 | break; |
| 10679 | } |
| 10680 | |
| 10681 | return NULL; |
| 10682 | } |
| 10683 | |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10684 | /* Fetch a captured HTTP request header. The index is the position of |
| 10685 | * the "capture" option in the configuration file |
| 10686 | */ |
| 10687 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10688 | 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] | 10689 | { |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10690 | struct proxy *fe = strm_fe(smp->strm); |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10691 | int idx; |
| 10692 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 10693 | if (!args || args->type != ARGT_SINT) |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10694 | return 0; |
| 10695 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 10696 | idx = args->data.sint; |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10697 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10698 | 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] | 10699 | return 0; |
| 10700 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10701 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10702 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10703 | smp->data.u.str.str = smp->strm->req_cap[idx]; |
| 10704 | smp->data.u.str.len = strlen(smp->strm->req_cap[idx]); |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10705 | |
| 10706 | return 1; |
| 10707 | } |
| 10708 | |
| 10709 | /* Fetch a captured HTTP response header. The index is the position of |
| 10710 | * the "capture" option in the configuration file |
| 10711 | */ |
| 10712 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10713 | 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] | 10714 | { |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10715 | struct proxy *fe = strm_fe(smp->strm); |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10716 | int idx; |
| 10717 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 10718 | if (!args || args->type != ARGT_SINT) |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10719 | return 0; |
| 10720 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 10721 | idx = args->data.sint; |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10722 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10723 | 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] | 10724 | return 0; |
| 10725 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10726 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10727 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10728 | smp->data.u.str.str = smp->strm->res_cap[idx]; |
| 10729 | smp->data.u.str.len = strlen(smp->strm->res_cap[idx]); |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10730 | |
| 10731 | return 1; |
| 10732 | } |
| 10733 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10734 | /* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */ |
| 10735 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10736 | 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] | 10737 | { |
| 10738 | struct chunk *temp; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10739 | struct http_txn *txn = smp->strm->txn; |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10740 | char *ptr; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10741 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10742 | if (!txn || !txn->uri) |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10743 | return 0; |
| 10744 | |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10745 | ptr = txn->uri; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10746 | |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10747 | while (*ptr != ' ' && *ptr != '\0') /* find first space */ |
| 10748 | ptr++; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10749 | |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10750 | temp = get_trash_chunk(); |
| 10751 | temp->str = txn->uri; |
| 10752 | temp->len = ptr - txn->uri; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10753 | smp->data.u.str = *temp; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10754 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10755 | smp->flags = SMP_F_CONST; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10756 | |
| 10757 | return 1; |
| 10758 | |
| 10759 | } |
| 10760 | |
| 10761 | /* Extracts the path in the HTTP request, the txn->uri should be filled before the call */ |
| 10762 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10763 | 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] | 10764 | { |
| 10765 | struct chunk *temp; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10766 | struct http_txn *txn = smp->strm->txn; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10767 | char *ptr; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10768 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10769 | if (!txn || !txn->uri) |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10770 | return 0; |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10771 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10772 | ptr = txn->uri; |
| 10773 | |
| 10774 | while (*ptr != ' ' && *ptr != '\0') /* find first space */ |
| 10775 | ptr++; |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10776 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10777 | if (!*ptr) |
| 10778 | return 0; |
| 10779 | |
| 10780 | ptr++; /* skip the space */ |
| 10781 | |
| 10782 | temp = get_trash_chunk(); |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10783 | ptr = temp->str = http_get_path_from_string(ptr); |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10784 | if (!ptr) |
| 10785 | return 0; |
| 10786 | while (*ptr != ' ' && *ptr != '\0') /* find space after URI */ |
| 10787 | ptr++; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10788 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10789 | smp->data.u.str = *temp; |
| 10790 | smp->data.u.str.len = ptr - temp->str; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10791 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10792 | smp->flags = SMP_F_CONST; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10793 | |
| 10794 | return 1; |
| 10795 | } |
| 10796 | |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 10797 | /* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it |
| 10798 | * as a string (either "HTTP/1.0" or "HTTP/1.1"). |
| 10799 | */ |
| 10800 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10801 | 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] | 10802 | { |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10803 | struct http_txn *txn = smp->strm->txn; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 10804 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10805 | if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST) |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 10806 | return 0; |
| 10807 | |
| 10808 | if (txn->req.flags & HTTP_MSGF_VER_11) |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10809 | smp->data.u.str.str = "HTTP/1.1"; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 10810 | else |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10811 | smp->data.u.str.str = "HTTP/1.0"; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 10812 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10813 | smp->data.u.str.len = 8; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10814 | smp->data.type = SMP_T_STR; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 10815 | smp->flags = SMP_F_CONST; |
| 10816 | return 1; |
| 10817 | |
| 10818 | } |
| 10819 | |
| 10820 | /* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it |
| 10821 | * as a string (either "HTTP/1.0" or "HTTP/1.1"). |
| 10822 | */ |
| 10823 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10824 | 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] | 10825 | { |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10826 | struct http_txn *txn = smp->strm->txn; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 10827 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10828 | if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST) |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 10829 | return 0; |
| 10830 | |
| 10831 | if (txn->rsp.flags & HTTP_MSGF_VER_11) |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10832 | smp->data.u.str.str = "HTTP/1.1"; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 10833 | else |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10834 | smp->data.u.str.str = "HTTP/1.0"; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 10835 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10836 | smp->data.u.str.len = 8; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10837 | smp->data.type = SMP_T_STR; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 10838 | smp->flags = SMP_F_CONST; |
| 10839 | return 1; |
| 10840 | |
| 10841 | } |
| 10842 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10843 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10844 | /* 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] | 10845 | * 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] | 10846 | * 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] | 10847 | * 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] | 10848 | * 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] | 10849 | * Accepts exactly 1 argument of type string. If the input options indicate |
| 10850 | * 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] | 10851 | * The returned sample is of type CSTR. Can be used to parse cookies in other |
| 10852 | * files. |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10853 | */ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10854 | 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] | 10855 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10856 | struct http_txn *txn; |
| 10857 | struct hdr_idx *idx; |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 10858 | struct hdr_ctx *ctx = smp->ctx.a[2]; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10859 | const struct http_msg *msg; |
| 10860 | const char *hdr_name; |
| 10861 | int hdr_name_len; |
| 10862 | char *sol; |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10863 | int occ = 0; |
| 10864 | int found = 0; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10865 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10866 | if (!args || args->type != ARGT_STR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10867 | return 0; |
| 10868 | |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 10869 | if (!ctx) { |
| 10870 | /* first call */ |
| 10871 | ctx = &static_hdr_ctx; |
| 10872 | ctx->idx = 0; |
| 10873 | smp->ctx.a[2] = ctx; |
| 10874 | } |
| 10875 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10876 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10877 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10878 | txn = smp->strm->txn; |
| 10879 | idx = &smp->strm->txn->hdr_idx; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10880 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10881 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10882 | msg = &txn->req; |
| 10883 | hdr_name = "Cookie"; |
| 10884 | hdr_name_len = 6; |
| 10885 | } else { |
| 10886 | msg = &txn->rsp; |
| 10887 | hdr_name = "Set-Cookie"; |
| 10888 | hdr_name_len = 10; |
| 10889 | } |
| 10890 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10891 | if (!occ && !(smp->opt & SMP_OPT_ITERATE)) |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10892 | /* no explicit occurrence and single fetch => last cookie by default */ |
| 10893 | occ = -1; |
| 10894 | |
| 10895 | /* OK so basically here, either we want only one value and it's the |
| 10896 | * last one, or we want to iterate over all of them and we fetch the |
| 10897 | * next one. |
| 10898 | */ |
| 10899 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10900 | sol = msg->chn->buf->p; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10901 | if (!(smp->flags & SMP_F_NOT_LAST)) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10902 | /* search for the header from the beginning, we must first initialize |
| 10903 | * the search parameters. |
| 10904 | */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10905 | smp->ctx.a[0] = NULL; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10906 | ctx->idx = 0; |
| 10907 | } |
| 10908 | |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10909 | smp->flags |= SMP_F_VOL_HDR; |
| 10910 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10911 | while (1) { |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10912 | /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */ |
| 10913 | if (!smp->ctx.a[0]) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10914 | if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx)) |
| 10915 | goto out; |
| 10916 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10917 | if (ctx->vlen < args->data.str.len + 1) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10918 | continue; |
| 10919 | |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10920 | smp->ctx.a[0] = ctx->line + ctx->val; |
| 10921 | smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10922 | } |
| 10923 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10924 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10925 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10926 | smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1], |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10927 | args->data.str.str, args->data.str.len, |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10928 | (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ, |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10929 | &smp->data.u.str.str, |
| 10930 | &smp->data.u.str.len); |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10931 | if (smp->ctx.a[0]) { |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10932 | found = 1; |
| 10933 | if (occ >= 0) { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10934 | /* one value was returned into smp->data.u.str.{str,len} */ |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10935 | smp->flags |= SMP_F_NOT_LAST; |
| 10936 | return 1; |
| 10937 | } |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10938 | } |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10939 | /* if we're looking for last occurrence, let's loop */ |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10940 | } |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10941 | /* all cookie headers and values were scanned. If we're looking for the |
| 10942 | * last occurrence, we may return it now. |
| 10943 | */ |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10944 | out: |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10945 | smp->flags &= ~SMP_F_NOT_LAST; |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10946 | return found; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10947 | } |
| 10948 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10949 | /* 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] | 10950 | * 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] | 10951 | * multiple cookies may be parsed on the same line. The returned sample is of |
| 10952 | * type UINT. Accepts exactly 1 argument of type string. |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10953 | */ |
| 10954 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10955 | 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] | 10956 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10957 | struct http_txn *txn; |
| 10958 | struct hdr_idx *idx; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10959 | struct hdr_ctx ctx; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10960 | const struct http_msg *msg; |
| 10961 | const char *hdr_name; |
| 10962 | int hdr_name_len; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10963 | int cnt; |
| 10964 | char *val_beg, *val_end; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10965 | char *sol; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10966 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10967 | if (!args || args->type != ARGT_STR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10968 | return 0; |
| 10969 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10970 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10971 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10972 | txn = smp->strm->txn; |
| 10973 | idx = &smp->strm->txn->hdr_idx; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10974 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10975 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10976 | msg = &txn->req; |
| 10977 | hdr_name = "Cookie"; |
| 10978 | hdr_name_len = 6; |
| 10979 | } else { |
| 10980 | msg = &txn->rsp; |
| 10981 | hdr_name = "Set-Cookie"; |
| 10982 | hdr_name_len = 10; |
| 10983 | } |
| 10984 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10985 | sol = msg->chn->buf->p; |
Willy Tarreau | 46787ed | 2012-04-11 17:28:40 +0200 | [diff] [blame] | 10986 | val_end = val_beg = NULL; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10987 | ctx.idx = 0; |
| 10988 | cnt = 0; |
| 10989 | |
| 10990 | while (1) { |
| 10991 | /* Note: val_beg == NULL every time we need to fetch a new header */ |
| 10992 | if (!val_beg) { |
| 10993 | if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx)) |
| 10994 | break; |
| 10995 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10996 | if (ctx.vlen < args->data.str.len + 1) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10997 | continue; |
| 10998 | |
| 10999 | val_beg = ctx.line + ctx.val; |
| 11000 | val_end = val_beg + ctx.vlen; |
| 11001 | } |
| 11002 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11003 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11004 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11005 | while ((val_beg = extract_cookie_value(val_beg, val_end, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 11006 | args->data.str.str, args->data.str.len, |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11007 | (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ, |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11008 | &smp->data.u.str.str, |
| 11009 | &smp->data.u.str.len))) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11010 | cnt++; |
| 11011 | } |
| 11012 | } |
| 11013 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11014 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11015 | smp->data.u.sint = cnt; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11016 | smp->flags |= SMP_F_VOL_HDR; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11017 | return 1; |
| 11018 | } |
| 11019 | |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 11020 | /* Fetch an cookie's integer value. The integer value is returned. It |
| 11021 | * takes a mandatory argument of type string. It relies on smp_fetch_cookie(). |
| 11022 | */ |
| 11023 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11024 | 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] | 11025 | { |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11026 | int ret = smp_fetch_cookie(args, smp, kw, private); |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 11027 | |
| 11028 | if (ret > 0) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11029 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11030 | smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len); |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 11031 | } |
| 11032 | |
| 11033 | return ret; |
| 11034 | } |
| 11035 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 11036 | /************************************************************************/ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 11037 | /* The code below is dedicated to sample fetches */ |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 11038 | /************************************************************************/ |
| 11039 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11040 | /* |
| 11041 | * Given a path string and its length, find the position of beginning of the |
| 11042 | * query string. Returns NULL if no query string is found in the path. |
| 11043 | * |
| 11044 | * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22: |
| 11045 | * |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11046 | * find_query_string(path, n, '?') points to "yo=mama;ye=daddy" string. |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11047 | */ |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11048 | 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] | 11049 | { |
| 11050 | char *p; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 11051 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11052 | p = memchr(path, delim, path_l); |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11053 | return p ? p + 1 : NULL; |
| 11054 | } |
| 11055 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11056 | static inline int is_param_delimiter(char c, char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11057 | { |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11058 | return c == '&' || c == ';' || c == delim; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11059 | } |
| 11060 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11061 | /* after increasing a pointer value, it can exceed the first buffer |
| 11062 | * size. This function transform the value of <ptr> according with |
| 11063 | * the expected position. <chunks> is an array of the one or two |
| 11064 | * avalaible chunks. The first value is the start of the first chunk, |
| 11065 | * the second value if the end+1 of the first chunks. The third value |
| 11066 | * is NULL or the start of the second chunk and the fourth value is |
| 11067 | * the end+1 of the second chunk. The function returns 1 if does a |
| 11068 | * wrap, else returns 0. |
| 11069 | */ |
| 11070 | static inline int fix_pointer_if_wrap(const char **chunks, const char **ptr) |
| 11071 | { |
| 11072 | if (*ptr < chunks[1]) |
| 11073 | return 0; |
| 11074 | if (!chunks[2]) |
| 11075 | return 0; |
| 11076 | *ptr = chunks[2] + ( *ptr - chunks[1] ); |
| 11077 | return 1; |
| 11078 | } |
| 11079 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11080 | /* |
| 11081 | * Given a url parameter, find the starting position of the first occurence, |
| 11082 | * or NULL if the parameter is not found. |
| 11083 | * |
| 11084 | * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye", |
| 11085 | * the function will return query_string+8. |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11086 | * |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 11087 | * 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] | 11088 | * or the second chunk. The caller must be check the position before using the |
| 11089 | * result. |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11090 | */ |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11091 | static const char * |
| 11092 | find_url_param_pos(const char **chunks, |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11093 | const char* url_param_name, size_t url_param_name_l, |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11094 | char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11095 | { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11096 | const char *pos, *last, *equal; |
| 11097 | const char **bufs = chunks; |
| 11098 | int l1, l2; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11099 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11100 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11101 | pos = bufs[0]; |
| 11102 | last = bufs[1]; |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 11103 | while (pos < last) { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11104 | /* Check the equal. */ |
| 11105 | equal = pos + url_param_name_l; |
| 11106 | if (fix_pointer_if_wrap(chunks, &equal)) { |
| 11107 | if (equal >= chunks[3]) |
| 11108 | return NULL; |
| 11109 | } else { |
| 11110 | if (equal >= chunks[1]) |
| 11111 | return NULL; |
| 11112 | } |
| 11113 | if (*equal == '=') { |
| 11114 | if (pos + url_param_name_l > last) { |
| 11115 | /* process wrap case, we detect a wrap. In this case, the |
| 11116 | * comparison is performed in two parts. |
| 11117 | */ |
| 11118 | |
| 11119 | /* This is the end, we dont have any other chunk. */ |
| 11120 | if (bufs != chunks || !bufs[2]) |
| 11121 | return NULL; |
| 11122 | |
| 11123 | /* Compute the length of each part of the comparison. */ |
| 11124 | l1 = last - pos; |
| 11125 | l2 = url_param_name_l - l1; |
| 11126 | |
| 11127 | /* The second buffer is too short to contain the compared string. */ |
| 11128 | if (bufs[2] + l2 > bufs[3]) |
| 11129 | return NULL; |
| 11130 | |
| 11131 | if (memcmp(pos, url_param_name, l1) == 0 && |
| 11132 | memcmp(bufs[2], url_param_name+l1, l2) == 0) |
| 11133 | return pos; |
| 11134 | |
| 11135 | /* Perform wrapping and jump the string who fail the comparison. */ |
| 11136 | bufs += 2; |
| 11137 | pos = bufs[0] + l2; |
| 11138 | last = bufs[1]; |
| 11139 | |
| 11140 | } else { |
| 11141 | /* process a simple comparison. */ |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 11142 | if (memcmp(pos, url_param_name, url_param_name_l) == 0) |
| 11143 | return pos; |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11144 | pos += url_param_name_l + 1; |
| 11145 | if (fix_pointer_if_wrap(chunks, &pos)) |
| 11146 | last = bufs[2]; |
| 11147 | } |
| 11148 | } |
| 11149 | |
| 11150 | while (1) { |
| 11151 | /* Look for the next delimiter. */ |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 11152 | while (pos < last && !is_param_delimiter(*pos, delim)) |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11153 | pos++; |
| 11154 | if (pos < last) |
| 11155 | break; |
| 11156 | /* process buffer wrapping. */ |
| 11157 | if (bufs != chunks || !bufs[2]) |
| 11158 | return NULL; |
| 11159 | bufs += 2; |
| 11160 | pos = bufs[0]; |
| 11161 | last = bufs[1]; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11162 | } |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11163 | pos++; |
| 11164 | } |
| 11165 | return NULL; |
| 11166 | } |
| 11167 | |
| 11168 | /* |
Cyril Bonté | ce1ef4d | 2015-11-26 21:39:56 +0100 | [diff] [blame] | 11169 | * Given a url parameter name and a query string, find the next value. |
| 11170 | * An empty url_param_name matches the first available parameter. |
| 11171 | * If the parameter is found, 1 is returned and *vstart / *vend are updated to |
| 11172 | * respectively provide a pointer to the value and its end. |
| 11173 | * Otherwise, 0 is returned and vstart/vend are not modified. |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11174 | */ |
| 11175 | static int |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11176 | find_next_url_param(const char **chunks, |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11177 | const char* url_param_name, size_t url_param_name_l, |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11178 | const char **vstart, const char **vend, char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11179 | { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11180 | const char *arg_start, *qs_end; |
| 11181 | const char *value_start, *value_end; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11182 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11183 | arg_start = chunks[0]; |
| 11184 | qs_end = chunks[1]; |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11185 | if (url_param_name_l) { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11186 | /* Looks for an argument name. */ |
| 11187 | arg_start = find_url_param_pos(chunks, |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11188 | url_param_name, url_param_name_l, |
| 11189 | delim); |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11190 | /* Check for wrapping. */ |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 11191 | if (arg_start >= qs_end) |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11192 | qs_end = chunks[3]; |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11193 | } |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11194 | if (!arg_start) |
| 11195 | return 0; |
| 11196 | |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11197 | if (!url_param_name_l) { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11198 | while (1) { |
| 11199 | /* looks for the first argument. */ |
| 11200 | value_start = memchr(arg_start, '=', qs_end - arg_start); |
| 11201 | if (!value_start) { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11202 | /* Check for wrapping. */ |
| 11203 | if (arg_start >= chunks[0] && |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 11204 | arg_start < chunks[1] && |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11205 | chunks[2]) { |
| 11206 | arg_start = chunks[2]; |
| 11207 | qs_end = chunks[3]; |
| 11208 | continue; |
| 11209 | } |
| 11210 | return 0; |
| 11211 | } |
| 11212 | break; |
| 11213 | } |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11214 | value_start++; |
| 11215 | } |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11216 | else { |
| 11217 | /* Jump the argument length. */ |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11218 | value_start = arg_start + url_param_name_l + 1; |
| 11219 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11220 | /* Check for pointer wrapping. */ |
| 11221 | if (fix_pointer_if_wrap(chunks, &value_start)) { |
| 11222 | /* Update the end pointer. */ |
| 11223 | qs_end = chunks[3]; |
| 11224 | |
| 11225 | /* Check for overflow. */ |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 11226 | if (value_start >= qs_end) |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11227 | return 0; |
| 11228 | } |
| 11229 | } |
| 11230 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11231 | value_end = value_start; |
| 11232 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11233 | while (1) { |
| 11234 | while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim)) |
| 11235 | value_end++; |
| 11236 | if (value_end < qs_end) |
| 11237 | break; |
| 11238 | /* process buffer wrapping. */ |
| 11239 | if (value_end >= chunks[0] && |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 11240 | value_end < chunks[1] && |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11241 | chunks[2]) { |
| 11242 | value_end = chunks[2]; |
| 11243 | qs_end = chunks[3]; |
| 11244 | continue; |
| 11245 | } |
| 11246 | break; |
| 11247 | } |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11248 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11249 | *vstart = value_start; |
| 11250 | *vend = value_end; |
Cyril Bonté | ce1ef4d | 2015-11-26 21:39:56 +0100 | [diff] [blame] | 11251 | return 1; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11252 | } |
| 11253 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11254 | /* This scans a URL-encoded query string. It takes an optionally wrapping |
| 11255 | * string whose first contigous chunk has its beginning in ctx->a[0] and end |
| 11256 | * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The |
| 11257 | * pointers are updated for next iteration before leaving. |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11258 | */ |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11259 | static int |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11260 | 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] | 11261 | { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11262 | const char *vstart, *vend; |
| 11263 | struct chunk *temp; |
| 11264 | const char **chunks = (const char **)smp->ctx.a; |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11265 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11266 | if (!find_next_url_param(chunks, |
Thierry FOURNIER | 0948d41 | 2015-05-20 15:22:37 +0200 | [diff] [blame] | 11267 | name, name_len, |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11268 | &vstart, &vend, |
Thierry FOURNIER | 0948d41 | 2015-05-20 15:22:37 +0200 | [diff] [blame] | 11269 | delim)) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11270 | return 0; |
| 11271 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11272 | /* Create sample. If the value is contiguous, return the pointer as CONST, |
| 11273 | * if the value is wrapped, copy-it in a buffer. |
| 11274 | */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11275 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11276 | if (chunks[2] && |
| 11277 | vstart >= chunks[0] && vstart <= chunks[1] && |
| 11278 | vend >= chunks[2] && vend <= chunks[3]) { |
| 11279 | /* Wrapped case. */ |
| 11280 | temp = get_trash_chunk(); |
| 11281 | memcpy(temp->str, vstart, chunks[1] - vstart); |
| 11282 | memcpy(temp->str + ( chunks[1] - vstart ), chunks[2], vend - chunks[2]); |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11283 | smp->data.u.str.str = temp->str; |
| 11284 | smp->data.u.str.len = ( chunks[1] - vstart ) + ( vend - chunks[2] ); |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11285 | } else { |
| 11286 | /* Contiguous case. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11287 | smp->data.u.str.str = (char *)vstart; |
| 11288 | smp->data.u.str.len = vend - vstart; |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11289 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
| 11290 | } |
| 11291 | |
| 11292 | /* Update context, check wrapping. */ |
| 11293 | chunks[0] = vend; |
| 11294 | if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) { |
| 11295 | chunks[1] = chunks[3]; |
| 11296 | chunks[2] = NULL; |
| 11297 | } |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11298 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11299 | if (chunks[0] < chunks[1]) |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11300 | smp->flags |= SMP_F_NOT_LAST; |
| 11301 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11302 | return 1; |
| 11303 | } |
| 11304 | |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11305 | /* This function iterates over each parameter of the query string. It uses |
| 11306 | * 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] | 11307 | * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL. |
| 11308 | * An optional parameter name is passed in args[0], otherwise any parameter is |
| 11309 | * considered. It supports an optional delimiter argument for the beginning of |
| 11310 | * the string in args[1], which defaults to "?". |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11311 | */ |
| 11312 | static int |
| 11313 | smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 11314 | { |
| 11315 | struct http_msg *msg; |
| 11316 | char delim = '?'; |
| 11317 | const char *name; |
| 11318 | int name_len; |
| 11319 | |
Dragan Dosen | 26f77e5 | 2015-05-25 10:02:11 +0200 | [diff] [blame] | 11320 | if (!args || |
| 11321 | (args[0].type && args[0].type != ARGT_STR) || |
| 11322 | (args[1].type && args[1].type != ARGT_STR)) |
| 11323 | return 0; |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11324 | |
Dragan Dosen | 26f77e5 | 2015-05-25 10:02:11 +0200 | [diff] [blame] | 11325 | name = ""; |
| 11326 | name_len = 0; |
| 11327 | if (args->type == ARGT_STR) { |
| 11328 | name = args->data.str.str; |
| 11329 | name_len = args->data.str.len; |
| 11330 | } |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11331 | |
Dragan Dosen | 26f77e5 | 2015-05-25 10:02:11 +0200 | [diff] [blame] | 11332 | if (args[1].type) |
| 11333 | delim = *args[1].data.str.str; |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11334 | |
Dragan Dosen | 26f77e5 | 2015-05-25 10:02:11 +0200 | [diff] [blame] | 11335 | if (!smp->ctx.a[0]) { // first call, find the query string |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11336 | CHECK_HTTP_MESSAGE_FIRST(); |
| 11337 | |
| 11338 | msg = &smp->strm->txn->req; |
| 11339 | |
| 11340 | smp->ctx.a[0] = find_param_list(msg->chn->buf->p + msg->sl.rq.u, |
| 11341 | msg->sl.rq.u_l, delim); |
| 11342 | if (!smp->ctx.a[0]) |
| 11343 | return 0; |
| 11344 | |
| 11345 | smp->ctx.a[1] = msg->chn->buf->p + msg->sl.rq.u + msg->sl.rq.u_l; |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11346 | |
| 11347 | /* Assume that the context is filled with NULL pointer |
| 11348 | * before the first call. |
| 11349 | * smp->ctx.a[2] = NULL; |
| 11350 | * smp->ctx.a[3] = NULL; |
| 11351 | */ |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11352 | } |
| 11353 | |
| 11354 | return smp_fetch_param(delim, name, name_len, args, smp, kw, private); |
| 11355 | } |
| 11356 | |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 11357 | /* This function iterates over each parameter of the body. This requires |
| 11358 | * 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] | 11359 | * ctx->a[0] and ctx->a[1] to store the beginning and end of the first |
| 11360 | * contigous part of the body, and optionally ctx->a[2..3] to reference the |
| 11361 | * optional second part if the body wraps at the end of the buffer. An optional |
| 11362 | * parameter name is passed in args[0], otherwise any parameter is considered. |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 11363 | */ |
| 11364 | static int |
| 11365 | smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 11366 | { |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 11367 | struct http_msg *msg; |
| 11368 | unsigned long len; |
| 11369 | unsigned long block1; |
| 11370 | char *body; |
| 11371 | const char *name; |
| 11372 | int name_len; |
| 11373 | |
| 11374 | if (!args || (args[0].type && args[0].type != ARGT_STR)) |
| 11375 | return 0; |
| 11376 | |
| 11377 | name = ""; |
| 11378 | name_len = 0; |
| 11379 | if (args[0].type == ARGT_STR) { |
| 11380 | name = args[0].data.str.str; |
| 11381 | name_len = args[0].data.str.len; |
| 11382 | } |
| 11383 | |
| 11384 | if (!smp->ctx.a[0]) { // first call, find the query string |
| 11385 | CHECK_HTTP_MESSAGE_FIRST(); |
| 11386 | |
| 11387 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 11388 | msg = &smp->strm->txn->req; |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 11389 | else |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 11390 | msg = &smp->strm->txn->rsp; |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 11391 | |
| 11392 | len = http_body_bytes(msg); |
| 11393 | body = b_ptr(msg->chn->buf, -http_data_rewind(msg)); |
| 11394 | |
| 11395 | block1 = len; |
| 11396 | if (block1 > msg->chn->buf->data + msg->chn->buf->size - body) |
| 11397 | block1 = msg->chn->buf->data + msg->chn->buf->size - body; |
| 11398 | |
| 11399 | if (block1 == len) { |
| 11400 | /* buffer is not wrapped (or empty) */ |
| 11401 | smp->ctx.a[0] = body; |
| 11402 | smp->ctx.a[1] = body + len; |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11403 | |
| 11404 | /* Assume that the context is filled with NULL pointer |
| 11405 | * before the first call. |
| 11406 | * smp->ctx.a[2] = NULL; |
| 11407 | * smp->ctx.a[3] = NULL; |
| 11408 | */ |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 11409 | } |
| 11410 | else { |
| 11411 | /* buffer is wrapped, we need to defragment it */ |
| 11412 | smp->ctx.a[0] = body; |
| 11413 | smp->ctx.a[1] = body + block1; |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11414 | smp->ctx.a[2] = msg->chn->buf->data; |
| 11415 | smp->ctx.a[3] = msg->chn->buf->data + ( len - block1 ); |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 11416 | } |
| 11417 | } |
| 11418 | return smp_fetch_param('&', name, name_len, args, smp, kw, private); |
| 11419 | } |
| 11420 | |
Willy Tarreau | a9fddca | 2012-07-31 07:51:48 +0200 | [diff] [blame] | 11421 | /* Return the signed integer value for the specified url parameter (see url_param |
| 11422 | * above). |
| 11423 | */ |
| 11424 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11425 | 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] | 11426 | { |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11427 | int ret = smp_fetch_url_param(args, smp, kw, private); |
Willy Tarreau | a9fddca | 2012-07-31 07:51:48 +0200 | [diff] [blame] | 11428 | |
| 11429 | if (ret > 0) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11430 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11431 | smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len); |
Willy Tarreau | a9fddca | 2012-07-31 07:51:48 +0200 | [diff] [blame] | 11432 | } |
| 11433 | |
| 11434 | return ret; |
| 11435 | } |
| 11436 | |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11437 | /* This produces a 32-bit hash of the concatenation of the first occurrence of |
| 11438 | * the Host header followed by the path component if it begins with a slash ('/'). |
| 11439 | * This means that '*' will not be added, resulting in exactly the first Host |
| 11440 | * entry. If no Host header is found, then the path is used. The resulting value |
| 11441 | * is hashed using the url hash followed by a full avalanche hash and provides a |
| 11442 | * 32-bit integer value. This fetch is useful for tracking per-URL activity on |
| 11443 | * high-traffic sites without having to store whole paths. |
| 11444 | * this differs from the base32 functions in that it includes the url parameters |
| 11445 | * as well as the path |
| 11446 | */ |
| 11447 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11448 | 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] | 11449 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11450 | struct http_txn *txn; |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11451 | struct hdr_ctx ctx; |
| 11452 | unsigned int hash = 0; |
| 11453 | char *ptr, *beg, *end; |
| 11454 | int len; |
| 11455 | |
| 11456 | CHECK_HTTP_MESSAGE_FIRST(); |
| 11457 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11458 | txn = smp->strm->txn; |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11459 | ctx.idx = 0; |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 11460 | if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) { |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11461 | /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */ |
| 11462 | ptr = ctx.line + ctx.val; |
| 11463 | len = ctx.vlen; |
| 11464 | while (len--) |
| 11465 | hash = *(ptr++) + (hash << 6) + (hash << 16) - hash; |
| 11466 | } |
| 11467 | |
| 11468 | /* now retrieve the path */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 11469 | end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l; |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11470 | beg = http_get_path(txn); |
| 11471 | if (!beg) |
| 11472 | beg = end; |
| 11473 | |
| 11474 | for (ptr = beg; ptr < end ; ptr++); |
| 11475 | |
| 11476 | if (beg < ptr && *beg == '/') { |
| 11477 | while (beg < ptr) |
| 11478 | hash = *(beg++) + (hash << 6) + (hash << 16) - hash; |
| 11479 | } |
| 11480 | hash = full_hash(hash); |
| 11481 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11482 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11483 | smp->data.u.sint = hash; |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11484 | smp->flags = SMP_F_VOL_1ST; |
| 11485 | return 1; |
| 11486 | } |
| 11487 | |
| 11488 | /* This concatenates the source address with the 32-bit hash of the Host and |
| 11489 | * URL as returned by smp_fetch_base32(). The idea is to have per-source and |
| 11490 | * per-url counters. The result is a binary block from 8 to 20 bytes depending |
| 11491 | * on the source address length. The URL hash is stored before the address so |
| 11492 | * that in environments where IPv6 is insignificant, truncating the output to |
| 11493 | * 8 bytes would still work. |
| 11494 | */ |
| 11495 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11496 | 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] | 11497 | { |
| 11498 | struct chunk *temp; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11499 | struct connection *cli_conn = objt_conn(smp->sess->origin); |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11500 | |
Dragan Dosen | db5af61 | 2016-06-16 11:23:01 +0200 | [diff] [blame] | 11501 | if (!cli_conn) |
| 11502 | return 0; |
| 11503 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11504 | if (!smp_fetch_url32(args, smp, kw, private)) |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11505 | return 0; |
| 11506 | |
| 11507 | temp = get_trash_chunk(); |
Dragan Dosen | e5f4133 | 2016-06-16 11:08:08 +0200 | [diff] [blame] | 11508 | *(unsigned int *)temp->str = htonl(smp->data.u.sint); |
| 11509 | temp->len += sizeof(unsigned int); |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11510 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 11511 | switch (cli_conn->addr.from.ss_family) { |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11512 | case AF_INET: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 11513 | memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4); |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11514 | temp->len += 4; |
| 11515 | break; |
| 11516 | case AF_INET6: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 11517 | memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16); |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11518 | temp->len += 16; |
| 11519 | break; |
| 11520 | default: |
| 11521 | return 0; |
| 11522 | } |
| 11523 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11524 | smp->data.u.str = *temp; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11525 | smp->data.type = SMP_T_BIN; |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11526 | return 1; |
| 11527 | } |
| 11528 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 11529 | /* This function is used to validate the arguments passed to any "hdr" fetch |
| 11530 | * keyword. These keywords support an optional positive or negative occurrence |
| 11531 | * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It |
| 11532 | * is assumed that the types are already the correct ones. Returns 0 on error, |
| 11533 | * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an |
| 11534 | * error message in case of error, that the caller is responsible for freeing. |
| 11535 | * The initial location must either be freeable or NULL. |
| 11536 | */ |
Thierry FOURNIER | 49f45af | 2014-12-08 19:50:43 +0100 | [diff] [blame] | 11537 | int val_hdr(struct arg *arg, char **err_msg) |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 11538 | { |
| 11539 | 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] | 11540 | memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY); |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 11541 | return 0; |
| 11542 | } |
| 11543 | return 1; |
| 11544 | } |
| 11545 | |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 11546 | /* takes an UINT value on input supposed to represent the time since EPOCH, |
| 11547 | * adds an optional offset found in args[0] and emits a string representing |
| 11548 | * the date in RFC-1123/5322 format. |
| 11549 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11550 | 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] | 11551 | { |
Cyril Bonté | f78d896 | 2016-01-22 19:40:28 +0100 | [diff] [blame] | 11552 | const char day[7][4] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 11553 | const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; |
| 11554 | struct chunk *temp; |
| 11555 | struct tm *tm; |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 11556 | /* 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] | 11557 | time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL; |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 11558 | |
| 11559 | /* add offset */ |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 11560 | if (args && (args[0].type == ARGT_SINT)) |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 11561 | curr_date += args[0].data.sint; |
| 11562 | |
| 11563 | tm = gmtime(&curr_date); |
Thierry FOURNIER | fac9ccf | 2015-07-08 00:15:20 +0200 | [diff] [blame] | 11564 | if (!tm) |
| 11565 | return 0; |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 11566 | |
| 11567 | temp = get_trash_chunk(); |
| 11568 | temp->len = snprintf(temp->str, temp->size - temp->len, |
| 11569 | "%s, %02d %s %04d %02d:%02d:%02d GMT", |
| 11570 | day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year, |
| 11571 | tm->tm_hour, tm->tm_min, tm->tm_sec); |
| 11572 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11573 | smp->data.u.str = *temp; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11574 | smp->data.type = SMP_T_STR; |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 11575 | return 1; |
| 11576 | } |
| 11577 | |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11578 | /* Match language range with language tag. RFC2616 14.4: |
| 11579 | * |
| 11580 | * A language-range matches a language-tag if it exactly equals |
| 11581 | * the tag, or if it exactly equals a prefix of the tag such |
| 11582 | * that the first tag character following the prefix is "-". |
| 11583 | * |
| 11584 | * Return 1 if the strings match, else return 0. |
| 11585 | */ |
| 11586 | static inline int language_range_match(const char *range, int range_len, |
| 11587 | const char *tag, int tag_len) |
| 11588 | { |
| 11589 | const char *end = range + range_len; |
| 11590 | const char *tend = tag + tag_len; |
| 11591 | while (range < end) { |
| 11592 | if (*range == '-' && tag == tend) |
| 11593 | return 1; |
| 11594 | if (*range != *tag || tag == tend) |
| 11595 | return 0; |
| 11596 | range++; |
| 11597 | tag++; |
| 11598 | } |
| 11599 | /* Return true only if the last char of the tag is matched. */ |
| 11600 | return tag == tend; |
| 11601 | } |
| 11602 | |
| 11603 | /* 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] | 11604 | 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] | 11605 | { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11606 | const char *al = smp->data.u.str.str; |
| 11607 | const char *end = al + smp->data.u.str.len; |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11608 | const char *token; |
| 11609 | int toklen; |
| 11610 | int qvalue; |
| 11611 | const char *str; |
| 11612 | const char *w; |
| 11613 | int best_q = 0; |
| 11614 | |
| 11615 | /* Set the constant to the sample, because the output of the |
| 11616 | * function will be peek in the constant configuration string. |
| 11617 | */ |
| 11618 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11619 | smp->data.u.str.size = 0; |
| 11620 | smp->data.u.str.str = ""; |
| 11621 | smp->data.u.str.len = 0; |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11622 | |
| 11623 | /* Parse the accept language */ |
| 11624 | while (1) { |
| 11625 | |
| 11626 | /* Jump spaces, quit if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 11627 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11628 | al++; |
| 11629 | if (al >= end) |
| 11630 | break; |
| 11631 | |
| 11632 | /* Start of the fisrt word. */ |
| 11633 | token = al; |
| 11634 | |
| 11635 | /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 11636 | while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11637 | al++; |
| 11638 | if (al == token) |
| 11639 | goto expect_comma; |
| 11640 | |
| 11641 | /* Length of the token. */ |
| 11642 | toklen = al - token; |
| 11643 | qvalue = 1000; |
| 11644 | |
| 11645 | /* Check if the token exists in the list. If the token not exists, |
| 11646 | * jump to the next token. |
| 11647 | */ |
| 11648 | str = args[0].data.str.str; |
| 11649 | w = str; |
| 11650 | while (1) { |
| 11651 | if (*str == ';' || *str == '\0') { |
| 11652 | if (language_range_match(token, toklen, w, str-w)) |
| 11653 | goto look_for_q; |
| 11654 | if (*str == '\0') |
| 11655 | goto expect_comma; |
| 11656 | w = str + 1; |
| 11657 | } |
| 11658 | str++; |
| 11659 | } |
| 11660 | goto expect_comma; |
| 11661 | |
| 11662 | look_for_q: |
| 11663 | |
| 11664 | /* Jump spaces, quit if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 11665 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11666 | al++; |
| 11667 | if (al >= end) |
| 11668 | goto process_value; |
| 11669 | |
| 11670 | /* If ',' is found, process the result */ |
| 11671 | if (*al == ',') |
| 11672 | goto process_value; |
| 11673 | |
| 11674 | /* If the character is different from ';', look |
| 11675 | * for the end of the header part in best effort. |
| 11676 | */ |
| 11677 | if (*al != ';') |
| 11678 | goto expect_comma; |
| 11679 | |
| 11680 | /* Assumes that the char is ';', now expect "q=". */ |
| 11681 | al++; |
| 11682 | |
| 11683 | /* Jump spaces, process value if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 11684 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11685 | al++; |
| 11686 | if (al >= end) |
| 11687 | goto process_value; |
| 11688 | |
| 11689 | /* Expect 'q'. If no 'q', continue in best effort */ |
| 11690 | if (*al != 'q') |
| 11691 | goto process_value; |
| 11692 | al++; |
| 11693 | |
| 11694 | /* Jump spaces, process value if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 11695 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11696 | al++; |
| 11697 | if (al >= end) |
| 11698 | goto process_value; |
| 11699 | |
| 11700 | /* Expect '='. If no '=', continue in best effort */ |
| 11701 | if (*al != '=') |
| 11702 | goto process_value; |
| 11703 | al++; |
| 11704 | |
| 11705 | /* Jump spaces, process value if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 11706 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11707 | al++; |
| 11708 | if (al >= end) |
| 11709 | goto process_value; |
| 11710 | |
| 11711 | /* Parse the q value. */ |
| 11712 | qvalue = parse_qvalue(al, &al); |
| 11713 | |
| 11714 | process_value: |
| 11715 | |
| 11716 | /* If the new q value is the best q value, then store the associated |
| 11717 | * language in the response. If qvalue is the biggest value (1000), |
| 11718 | * break the process. |
| 11719 | */ |
| 11720 | if (qvalue > best_q) { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11721 | smp->data.u.str.str = (char *)w; |
| 11722 | smp->data.u.str.len = str - w; |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11723 | if (qvalue >= 1000) |
| 11724 | break; |
| 11725 | best_q = qvalue; |
| 11726 | } |
| 11727 | |
| 11728 | expect_comma: |
| 11729 | |
| 11730 | /* Expect comma or end. If the end is detected, quit the loop. */ |
| 11731 | while (al < end && *al != ',') |
| 11732 | al++; |
| 11733 | if (al >= end) |
| 11734 | break; |
| 11735 | |
| 11736 | /* Comma is found, jump it and restart the analyzer. */ |
| 11737 | al++; |
| 11738 | } |
| 11739 | |
| 11740 | /* Set default value if required. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11741 | if (smp->data.u.str.len == 0 && args[1].type == ARGT_STR) { |
| 11742 | smp->data.u.str.str = args[1].data.str.str; |
| 11743 | smp->data.u.str.len = args[1].data.str.len; |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11744 | } |
| 11745 | |
| 11746 | /* Return true only if a matching language was found. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11747 | return smp->data.u.str.len != 0; |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11748 | } |
| 11749 | |
Thierry FOURNIER | 82ff3c9 | 2015-05-07 15:46:20 +0200 | [diff] [blame] | 11750 | /* This fetch url-decode any input string. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11751 | 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] | 11752 | { |
| 11753 | /* If the constant flag is set or if not size is avalaible at |
| 11754 | * the end of the buffer, copy the string in other buffer |
| 11755 | * before decoding. |
| 11756 | */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11757 | if (smp->flags & SMP_F_CONST || smp->data.u.str.size <= smp->data.u.str.len) { |
Thierry FOURNIER | 82ff3c9 | 2015-05-07 15:46:20 +0200 | [diff] [blame] | 11758 | struct chunk *str = get_trash_chunk(); |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11759 | memcpy(str->str, smp->data.u.str.str, smp->data.u.str.len); |
| 11760 | smp->data.u.str.str = str->str; |
| 11761 | smp->data.u.str.size = str->size; |
Thierry FOURNIER | 82ff3c9 | 2015-05-07 15:46:20 +0200 | [diff] [blame] | 11762 | smp->flags &= ~SMP_F_CONST; |
| 11763 | } |
| 11764 | |
| 11765 | /* Add final \0 required by url_decode(), and convert the input string. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11766 | smp->data.u.str.str[smp->data.u.str.len] = '\0'; |
| 11767 | smp->data.u.str.len = url_decode(smp->data.u.str.str); |
Christopher Faulet | a258479 | 2017-10-05 10:03:12 +0200 | [diff] [blame] | 11768 | return (smp->data.u.str.len >= 0); |
Thierry FOURNIER | 82ff3c9 | 2015-05-07 15:46:20 +0200 | [diff] [blame] | 11769 | } |
| 11770 | |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 11771 | static int smp_conv_req_capture(const struct arg *args, struct sample *smp, void *private) |
| 11772 | { |
| 11773 | struct proxy *fe = strm_fe(smp->strm); |
| 11774 | int idx, i; |
| 11775 | struct cap_hdr *hdr; |
| 11776 | int len; |
| 11777 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 11778 | if (!args || args->type != ARGT_SINT) |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 11779 | return 0; |
| 11780 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 11781 | idx = args->data.sint; |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 11782 | |
| 11783 | /* Check the availibity of the capture id. */ |
| 11784 | if (idx > fe->nb_req_cap - 1) |
| 11785 | return 0; |
| 11786 | |
| 11787 | /* Look for the original configuration. */ |
| 11788 | for (hdr = fe->req_cap, i = fe->nb_req_cap - 1; |
| 11789 | hdr != NULL && i != idx ; |
| 11790 | i--, hdr = hdr->next); |
| 11791 | if (!hdr) |
| 11792 | return 0; |
| 11793 | |
| 11794 | /* check for the memory allocation */ |
| 11795 | if (smp->strm->req_cap[hdr->index] == NULL) |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 11796 | smp->strm->req_cap[hdr->index] = pool_alloc(hdr->pool); |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 11797 | if (smp->strm->req_cap[hdr->index] == NULL) |
| 11798 | return 0; |
| 11799 | |
| 11800 | /* Check length. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11801 | len = smp->data.u.str.len; |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 11802 | if (len > hdr->len) |
| 11803 | len = hdr->len; |
| 11804 | |
| 11805 | /* Capture input data. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11806 | memcpy(smp->strm->req_cap[idx], smp->data.u.str.str, len); |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 11807 | smp->strm->req_cap[idx][len] = '\0'; |
| 11808 | |
| 11809 | return 1; |
| 11810 | } |
| 11811 | |
| 11812 | static int smp_conv_res_capture(const struct arg *args, struct sample *smp, void *private) |
| 11813 | { |
| 11814 | struct proxy *fe = strm_fe(smp->strm); |
| 11815 | int idx, i; |
| 11816 | struct cap_hdr *hdr; |
| 11817 | int len; |
| 11818 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 11819 | if (!args || args->type != ARGT_SINT) |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 11820 | return 0; |
| 11821 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 11822 | idx = args->data.sint; |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 11823 | |
| 11824 | /* Check the availibity of the capture id. */ |
| 11825 | if (idx > fe->nb_rsp_cap - 1) |
| 11826 | return 0; |
| 11827 | |
| 11828 | /* Look for the original configuration. */ |
| 11829 | for (hdr = fe->rsp_cap, i = fe->nb_rsp_cap - 1; |
| 11830 | hdr != NULL && i != idx ; |
| 11831 | i--, hdr = hdr->next); |
| 11832 | if (!hdr) |
| 11833 | return 0; |
| 11834 | |
| 11835 | /* check for the memory allocation */ |
| 11836 | if (smp->strm->res_cap[hdr->index] == NULL) |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 11837 | smp->strm->res_cap[hdr->index] = pool_alloc(hdr->pool); |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 11838 | if (smp->strm->res_cap[hdr->index] == NULL) |
| 11839 | return 0; |
| 11840 | |
| 11841 | /* Check length. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11842 | len = smp->data.u.str.len; |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 11843 | if (len > hdr->len) |
| 11844 | len = hdr->len; |
| 11845 | |
| 11846 | /* Capture input data. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11847 | memcpy(smp->strm->res_cap[idx], smp->data.u.str.str, len); |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 11848 | smp->strm->res_cap[idx][len] = '\0'; |
| 11849 | |
| 11850 | return 1; |
| 11851 | } |
| 11852 | |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11853 | /* 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] | 11854 | * takes the string from the variable 'replace' with length 'len', then modifies |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11855 | * the relevant part of the request line accordingly. Then it updates various |
| 11856 | * pointers to the next elements which were moved, and the total buffer length. |
| 11857 | * 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] | 11858 | * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal |
| 11859 | * error, though this can be revisited when this code is finally exploited. |
| 11860 | * |
| 11861 | * 'action' can be '0' to replace method, '1' to replace path, '2' to replace |
| 11862 | * query string and 3 to replace uri. |
| 11863 | * |
| 11864 | * In query string case, the mark question '?' must be set at the start of the |
| 11865 | * string by the caller, event if the replacement query string is empty. |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11866 | */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11867 | int http_replace_req_line(int action, const char *replace, int len, |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 11868 | struct proxy *px, struct stream *s) |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11869 | { |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 11870 | struct http_txn *txn = s->txn; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11871 | char *cur_ptr, *cur_end; |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11872 | int offset = 0; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11873 | int delta; |
| 11874 | |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11875 | switch (action) { |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11876 | case 0: // method |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 11877 | cur_ptr = s->req.buf->p; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11878 | cur_end = cur_ptr + txn->req.sl.rq.m_l; |
| 11879 | |
| 11880 | /* adjust req line offsets and lengths */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11881 | delta = len - offset - (cur_end - cur_ptr); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11882 | txn->req.sl.rq.m_l += delta; |
| 11883 | txn->req.sl.rq.u += delta; |
| 11884 | txn->req.sl.rq.v += delta; |
| 11885 | break; |
| 11886 | |
| 11887 | case 1: // path |
| 11888 | cur_ptr = http_get_path(txn); |
| 11889 | if (!cur_ptr) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 11890 | cur_ptr = s->req.buf->p + txn->req.sl.rq.u; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11891 | |
| 11892 | cur_end = cur_ptr; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 11893 | while (cur_end < s->req.buf->p + 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] | 11894 | cur_end++; |
| 11895 | |
| 11896 | /* adjust req line offsets and lengths */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11897 | delta = len - offset - (cur_end - cur_ptr); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11898 | txn->req.sl.rq.u_l += delta; |
| 11899 | txn->req.sl.rq.v += delta; |
| 11900 | break; |
| 11901 | |
| 11902 | case 2: // query |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11903 | offset = 1; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 11904 | cur_ptr = s->req.buf->p + txn->req.sl.rq.u; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11905 | cur_end = cur_ptr + txn->req.sl.rq.u_l; |
| 11906 | while (cur_ptr < cur_end && *cur_ptr != '?') |
| 11907 | cur_ptr++; |
| 11908 | |
| 11909 | /* skip the question mark or indicate that we must insert it |
| 11910 | * (but only if the format string is not empty then). |
| 11911 | */ |
| 11912 | if (cur_ptr < cur_end) |
| 11913 | cur_ptr++; |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11914 | else if (len > 1) |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11915 | offset = 0; |
| 11916 | |
| 11917 | /* adjust req line offsets and lengths */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11918 | delta = len - offset - (cur_end - cur_ptr); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11919 | txn->req.sl.rq.u_l += delta; |
| 11920 | txn->req.sl.rq.v += delta; |
| 11921 | break; |
| 11922 | |
| 11923 | case 3: // uri |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 11924 | cur_ptr = s->req.buf->p + txn->req.sl.rq.u; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11925 | cur_end = cur_ptr + txn->req.sl.rq.u_l; |
| 11926 | |
| 11927 | /* adjust req line offsets and lengths */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11928 | delta = len - offset - (cur_end - cur_ptr); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11929 | txn->req.sl.rq.u_l += delta; |
| 11930 | txn->req.sl.rq.v += delta; |
| 11931 | break; |
| 11932 | |
| 11933 | default: |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11934 | return -1; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11935 | } |
| 11936 | |
| 11937 | /* commit changes and adjust end of message */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11938 | delta = buffer_replace2(s->req.buf, cur_ptr, cur_end, replace + offset, len - offset); |
Thierry FOURNIER | 7f6192c | 2015-04-26 18:01:40 +0200 | [diff] [blame] | 11939 | txn->req.sl.rq.l += delta; |
| 11940 | txn->hdr_idx.v[0].len += delta; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11941 | http_msg_move_end(&txn->req, delta); |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11942 | return 0; |
| 11943 | } |
| 11944 | |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 11945 | /* This function replace the HTTP status code and the associated message. The |
| 11946 | * variable <status> contains the new status code. This function never fails. |
| 11947 | */ |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 11948 | 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] | 11949 | { |
| 11950 | struct http_txn *txn = s->txn; |
| 11951 | char *cur_ptr, *cur_end; |
| 11952 | int delta; |
| 11953 | char *res; |
| 11954 | int c_l; |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 11955 | const char *msg = reason; |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 11956 | int msg_len; |
| 11957 | |
| 11958 | chunk_reset(&trash); |
| 11959 | |
| 11960 | res = ultoa_o(status, trash.str, trash.size); |
| 11961 | c_l = res - trash.str; |
| 11962 | |
| 11963 | trash.str[c_l] = ' '; |
| 11964 | trash.len = c_l + 1; |
| 11965 | |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 11966 | /* Do we have a custom reason format string? */ |
| 11967 | if (msg == NULL) |
| 11968 | msg = get_reason(status); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 11969 | msg_len = strlen(msg); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 11970 | strncpy(&trash.str[trash.len], msg, trash.size - trash.len); |
| 11971 | trash.len += msg_len; |
| 11972 | |
| 11973 | cur_ptr = s->res.buf->p + txn->rsp.sl.st.c; |
| 11974 | cur_end = s->res.buf->p + txn->rsp.sl.st.r + txn->rsp.sl.st.r_l; |
| 11975 | |
| 11976 | /* commit changes and adjust message */ |
| 11977 | delta = buffer_replace2(s->res.buf, cur_ptr, cur_end, trash.str, trash.len); |
| 11978 | |
| 11979 | /* adjust res line offsets and lengths */ |
| 11980 | txn->rsp.sl.st.r += c_l - txn->rsp.sl.st.c_l; |
| 11981 | txn->rsp.sl.st.c_l = c_l; |
| 11982 | txn->rsp.sl.st.r_l = msg_len; |
| 11983 | |
| 11984 | delta = trash.len - (cur_end - cur_ptr); |
| 11985 | txn->rsp.sl.st.l += delta; |
| 11986 | txn->hdr_idx.v[0].len += delta; |
| 11987 | http_msg_move_end(&txn->rsp, delta); |
| 11988 | } |
| 11989 | |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11990 | /* This function executes one of the set-{method,path,query,uri} actions. It |
| 11991 | * 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] | 11992 | * the action to be performed in <http.action>, previously filled by function |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11993 | * parse_set_req_line(). The replacement action is excuted by the function |
Thierry FOURNIER | 3f4bc65 | 2015-08-26 16:23:34 +0200 | [diff] [blame] | 11994 | * http_action_set_req_line(). It always returns ACT_RET_CONT. If an error |
| 11995 | * occurs the action is canceled, but the rule processing continue. |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11996 | */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 11997 | 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] | 11998 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11999 | { |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 12000 | struct chunk *replace; |
| 12001 | enum act_return ret = ACT_RET_ERR; |
| 12002 | |
| 12003 | replace = alloc_trash_chunk(); |
| 12004 | if (!replace) |
| 12005 | goto leave; |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12006 | |
| 12007 | /* If we have to create a query string, prepare a '?'. */ |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 12008 | if (rule->arg.http.action == 2) |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 12009 | replace->str[replace->len++] = '?'; |
| 12010 | replace->len += build_logline(s, replace->str + replace->len, replace->size - replace->len, |
| 12011 | &rule->arg.http.logfmt); |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12012 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 12013 | http_replace_req_line(rule->arg.http.action, replace->str, replace->len, px, s); |
| 12014 | |
| 12015 | ret = ACT_RET_CONT; |
| 12016 | |
| 12017 | leave: |
| 12018 | free_trash_chunk(replace); |
| 12019 | return ret; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12020 | } |
| 12021 | |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 12022 | /* This function is just a compliant action wrapper for "set-status". */ |
| 12023 | 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] | 12024 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 12025 | { |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 12026 | http_set_status(rule->arg.status.code, rule->arg.status.reason, s); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 12027 | return ACT_RET_CONT; |
| 12028 | } |
| 12029 | |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12030 | /* parse an http-request action among : |
| 12031 | * set-method |
| 12032 | * set-path |
| 12033 | * set-query |
| 12034 | * set-uri |
| 12035 | * |
| 12036 | * All of them accept a single argument of type string representing a log-format. |
| 12037 | * The resulting rule makes use of arg->act.p[0..1] to store the log-format list |
| 12038 | * 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] | 12039 | * 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] | 12040 | */ |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12041 | enum act_parse_ret parse_set_req_line(const char **args, int *orig_arg, struct proxy *px, |
| 12042 | struct act_rule *rule, char **err) |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12043 | { |
| 12044 | int cur_arg = *orig_arg; |
| 12045 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 12046 | rule->action = ACT_CUSTOM; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12047 | |
| 12048 | switch (args[0][4]) { |
| 12049 | case 'm' : |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 12050 | rule->arg.http.action = 0; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12051 | rule->action_ptr = http_action_set_req_line; |
| 12052 | break; |
| 12053 | case 'p' : |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 12054 | rule->arg.http.action = 1; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12055 | rule->action_ptr = http_action_set_req_line; |
| 12056 | break; |
| 12057 | case 'q' : |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 12058 | rule->arg.http.action = 2; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12059 | rule->action_ptr = http_action_set_req_line; |
| 12060 | break; |
| 12061 | case 'u' : |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 12062 | rule->arg.http.action = 3; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12063 | rule->action_ptr = http_action_set_req_line; |
| 12064 | break; |
| 12065 | default: |
| 12066 | memprintf(err, "internal error: unhandled action '%s'", args[0]); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12067 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12068 | } |
| 12069 | |
| 12070 | if (!*args[cur_arg] || |
| 12071 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 12072 | memprintf(err, "expects exactly 1 argument <format>"); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12073 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12074 | } |
| 12075 | |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 12076 | LIST_INIT(&rule->arg.http.logfmt); |
Olivier Houchard | fbc74e8 | 2017-11-24 16:54:05 +0100 | [diff] [blame] | 12077 | px->conf.args.ctx = ARGC_HRQ; |
| 12078 | if (!parse_logformat_string(args[cur_arg], px, &rule->arg.http.logfmt, LOG_OPT_HTTP, |
| 12079 | (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] | 12080 | return ACT_RET_PRS_ERR; |
| 12081 | } |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12082 | |
| 12083 | (*orig_arg)++; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12084 | return ACT_RET_PRS_OK; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12085 | } |
| 12086 | |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 12087 | /* parse set-status action: |
| 12088 | * This action accepts a single argument of type int representing |
| 12089 | * an http status code. It returns ACT_RET_PRS_OK on success, |
| 12090 | * ACT_RET_PRS_ERR on error. |
| 12091 | */ |
| 12092 | enum act_parse_ret parse_http_set_status(const char **args, int *orig_arg, struct proxy *px, |
| 12093 | struct act_rule *rule, char **err) |
| 12094 | { |
| 12095 | char *error; |
| 12096 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 12097 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 12098 | rule->action_ptr = action_http_set_status; |
| 12099 | |
| 12100 | /* Check if an argument is available */ |
| 12101 | if (!*args[*orig_arg]) { |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 12102 | memprintf(err, "expects 1 argument: <status>; or 3 arguments: <status> reason <fmt>"); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 12103 | return ACT_RET_PRS_ERR; |
| 12104 | } |
| 12105 | |
| 12106 | /* convert status code as integer */ |
| 12107 | rule->arg.status.code = strtol(args[*orig_arg], &error, 10); |
| 12108 | if (*error != '\0' || rule->arg.status.code < 100 || rule->arg.status.code > 999) { |
| 12109 | memprintf(err, "expects an integer status code between 100 and 999"); |
| 12110 | return ACT_RET_PRS_ERR; |
| 12111 | } |
| 12112 | |
| 12113 | (*orig_arg)++; |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 12114 | |
| 12115 | /* set custom reason string */ |
| 12116 | rule->arg.status.reason = NULL; // If null, we use the default reason for the status code. |
| 12117 | if (*args[*orig_arg] && strcmp(args[*orig_arg], "reason") == 0 && |
| 12118 | (*args[*orig_arg + 1] && strcmp(args[*orig_arg + 1], "if") != 0 && strcmp(args[*orig_arg + 1], "unless") != 0)) { |
| 12119 | (*orig_arg)++; |
| 12120 | rule->arg.status.reason = strdup(args[*orig_arg]); |
| 12121 | (*orig_arg)++; |
| 12122 | } |
| 12123 | |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 12124 | return ACT_RET_PRS_OK; |
| 12125 | } |
| 12126 | |
Willy Tarreau | 53275e8 | 2017-11-24 07:52:01 +0100 | [diff] [blame] | 12127 | /* This function executes the "reject" HTTP action. It clears the request and |
| 12128 | * response buffer without sending any response. It can be useful as an HTTP |
| 12129 | * alternative to the silent-drop action to defend against DoS attacks, and may |
| 12130 | * also be used with HTTP/2 to close a connection instead of just a stream. |
| 12131 | * The txn status is unchanged, indicating no response was sent. The termination |
| 12132 | * flags will indicate "PR". It always returns ACT_RET_STOP. |
| 12133 | */ |
| 12134 | enum act_return http_action_reject(struct act_rule *rule, struct proxy *px, |
| 12135 | struct session *sess, struct stream *s, int flags) |
| 12136 | { |
| 12137 | channel_abort(&s->req); |
| 12138 | channel_abort(&s->res); |
| 12139 | s->req.analysers = 0; |
| 12140 | s->res.analysers = 0; |
| 12141 | |
| 12142 | HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1); |
| 12143 | HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1); |
| 12144 | if (sess->listener && sess->listener->counters) |
| 12145 | HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1); |
| 12146 | |
| 12147 | if (!(s->flags & SF_ERR_MASK)) |
| 12148 | s->flags |= SF_ERR_PRXCOND; |
| 12149 | if (!(s->flags & SF_FINST_MASK)) |
| 12150 | s->flags |= SF_FINST_R; |
| 12151 | |
| 12152 | return ACT_RET_CONT; |
| 12153 | } |
| 12154 | |
| 12155 | /* parse the "reject" action: |
| 12156 | * This action takes no argument and returns ACT_RET_PRS_OK on success, |
| 12157 | * ACT_RET_PRS_ERR on error. |
| 12158 | */ |
| 12159 | enum act_parse_ret parse_http_action_reject(const char **args, int *orig_arg, struct proxy *px, |
| 12160 | struct act_rule *rule, char **err) |
| 12161 | { |
| 12162 | rule->action = ACT_CUSTOM; |
| 12163 | rule->action_ptr = http_action_reject; |
| 12164 | return ACT_RET_PRS_OK; |
| 12165 | } |
| 12166 | |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12167 | /* This function executes the "capture" action. It executes a fetch expression, |
| 12168 | * turns the result into a string and puts it in a capture slot. It always |
| 12169 | * returns 1. If an error occurs the action is cancelled, but the rule |
| 12170 | * processing continues. |
| 12171 | */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12172 | 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] | 12173 | struct session *sess, struct stream *s, int flags) |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12174 | { |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12175 | struct sample *key; |
Thierry FOURNIER | 32b1500 | 2015-07-31 08:56:16 +0200 | [diff] [blame] | 12176 | struct cap_hdr *h = rule->arg.cap.hdr; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12177 | char **cap = s->req_cap; |
| 12178 | int len; |
| 12179 | |
Thierry FOURNIER | 32b1500 | 2015-07-31 08:56:16 +0200 | [diff] [blame] | 12180 | 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] | 12181 | if (!key) |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12182 | return ACT_RET_CONT; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12183 | |
| 12184 | if (cap[h->index] == NULL) |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 12185 | cap[h->index] = pool_alloc(h->pool); |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12186 | |
| 12187 | if (cap[h->index] == NULL) /* no more capture memory */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12188 | return ACT_RET_CONT; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12189 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12190 | len = key->data.u.str.len; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12191 | if (len > h->len) |
| 12192 | len = h->len; |
| 12193 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12194 | memcpy(cap[h->index], key->data.u.str.str, len); |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12195 | cap[h->index][len] = 0; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12196 | return ACT_RET_CONT; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12197 | } |
| 12198 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12199 | /* This function executes the "capture" action and store the result in a |
| 12200 | * capture slot if exists. It executes a fetch expression, turns the result |
| 12201 | * into a string and puts it in a capture slot. It always returns 1. If an |
| 12202 | * error occurs the action is cancelled, but the rule processing continues. |
| 12203 | */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12204 | 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] | 12205 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12206 | { |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12207 | struct sample *key; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12208 | struct cap_hdr *h; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12209 | char **cap = s->req_cap; |
| 12210 | struct proxy *fe = strm_fe(s); |
| 12211 | int len; |
| 12212 | int i; |
| 12213 | |
| 12214 | /* Look for the original configuration. */ |
| 12215 | for (h = fe->req_cap, i = fe->nb_req_cap - 1; |
Thierry FOURNIER | e209797 | 2015-07-31 08:56:35 +0200 | [diff] [blame] | 12216 | h != NULL && i != rule->arg.capid.idx ; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12217 | i--, h = h->next); |
| 12218 | if (!h) |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12219 | return ACT_RET_CONT; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12220 | |
Thierry FOURNIER | e209797 | 2015-07-31 08:56:35 +0200 | [diff] [blame] | 12221 | 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] | 12222 | if (!key) |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12223 | return ACT_RET_CONT; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12224 | |
| 12225 | if (cap[h->index] == NULL) |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 12226 | cap[h->index] = pool_alloc(h->pool); |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12227 | |
| 12228 | if (cap[h->index] == NULL) /* no more capture memory */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12229 | return ACT_RET_CONT; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12230 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12231 | len = key->data.u.str.len; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12232 | if (len > h->len) |
| 12233 | len = h->len; |
| 12234 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12235 | memcpy(cap[h->index], key->data.u.str.str, len); |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12236 | cap[h->index][len] = 0; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12237 | return ACT_RET_CONT; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12238 | } |
| 12239 | |
Christopher Faulet | 29730ba | 2017-09-18 15:26:32 +0200 | [diff] [blame] | 12240 | /* Check an "http-request capture" action. |
| 12241 | * |
| 12242 | * The function returns 1 in success case, otherwise, it returns 0 and err is |
| 12243 | * filled. |
| 12244 | */ |
| 12245 | int check_http_req_capture(struct act_rule *rule, struct proxy *px, char **err) |
| 12246 | { |
Christopher Faulet | fd608dd | 2017-12-04 09:45:15 +0100 | [diff] [blame] | 12247 | if (rule->action_ptr != http_action_req_capture_by_id) |
| 12248 | return 1; |
| 12249 | |
Christopher Faulet | 29730ba | 2017-09-18 15:26:32 +0200 | [diff] [blame] | 12250 | if (rule->arg.capid.idx >= px->nb_req_cap) { |
| 12251 | memprintf(err, "unable to find capture id '%d' referenced by http-request capture rule", |
| 12252 | rule->arg.capid.idx); |
| 12253 | return 0; |
| 12254 | } |
| 12255 | |
| 12256 | return 1; |
| 12257 | } |
| 12258 | |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12259 | /* parse an "http-request capture" action. It takes a single argument which is |
| 12260 | * 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] | 12261 | * 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] | 12262 | * 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] | 12263 | */ |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12264 | enum act_parse_ret parse_http_req_capture(const char **args, int *orig_arg, struct proxy *px, |
| 12265 | struct act_rule *rule, char **err) |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12266 | { |
| 12267 | struct sample_expr *expr; |
| 12268 | struct cap_hdr *hdr; |
| 12269 | int cur_arg; |
Willy Tarreau | 3986ac1 | 2015-05-08 16:13:42 +0200 | [diff] [blame] | 12270 | int len = 0; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12271 | |
| 12272 | for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++) |
| 12273 | if (strcmp(args[cur_arg], "if") == 0 || |
| 12274 | strcmp(args[cur_arg], "unless") == 0) |
| 12275 | break; |
| 12276 | |
| 12277 | if (cur_arg < *orig_arg + 3) { |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12278 | memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]"); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12279 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12280 | } |
| 12281 | |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12282 | cur_arg = *orig_arg; |
| 12283 | expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args); |
| 12284 | if (!expr) |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12285 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12286 | |
| 12287 | if (!(expr->fetch->val & SMP_VAL_FE_HRQ_HDR)) { |
| 12288 | memprintf(err, |
| 12289 | "fetch method '%s' extracts information from '%s', none of which is available here", |
| 12290 | args[cur_arg-1], sample_src_names(expr->fetch->use)); |
| 12291 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12292 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12293 | } |
| 12294 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12295 | if (!args[cur_arg] || !*args[cur_arg]) { |
| 12296 | memprintf(err, "expects 'len or 'id'"); |
| 12297 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12298 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12299 | } |
| 12300 | |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12301 | if (strcmp(args[cur_arg], "len") == 0) { |
| 12302 | cur_arg++; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12303 | |
| 12304 | if (!(px->cap & PR_CAP_FE)) { |
| 12305 | memprintf(err, "proxy '%s' has no frontend capability", px->id); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12306 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12307 | } |
| 12308 | |
Olivier Houchard | fbc74e8 | 2017-11-24 16:54:05 +0100 | [diff] [blame] | 12309 | px->conf.args.ctx = ARGC_CAP; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12310 | |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12311 | if (!args[cur_arg]) { |
| 12312 | memprintf(err, "missing length value"); |
| 12313 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12314 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12315 | } |
| 12316 | /* we copy the table name for now, it will be resolved later */ |
| 12317 | len = atoi(args[cur_arg]); |
| 12318 | if (len <= 0) { |
| 12319 | memprintf(err, "length must be > 0"); |
| 12320 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12321 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12322 | } |
| 12323 | cur_arg++; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12324 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12325 | if (!len) { |
| 12326 | memprintf(err, "a positive 'len' argument is mandatory"); |
| 12327 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12328 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12329 | } |
| 12330 | |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 12331 | hdr = calloc(1, sizeof(*hdr)); |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12332 | hdr->next = px->req_cap; |
| 12333 | hdr->name = NULL; /* not a header capture */ |
| 12334 | hdr->namelen = 0; |
| 12335 | hdr->len = len; |
| 12336 | hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED); |
| 12337 | hdr->index = px->nb_req_cap++; |
| 12338 | |
| 12339 | px->req_cap = hdr; |
| 12340 | px->to_log |= LW_REQHDR; |
| 12341 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 12342 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12343 | rule->action_ptr = http_action_req_capture; |
Thierry FOURNIER | 32b1500 | 2015-07-31 08:56:16 +0200 | [diff] [blame] | 12344 | rule->arg.cap.expr = expr; |
| 12345 | rule->arg.cap.hdr = hdr; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12346 | } |
| 12347 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12348 | else if (strcmp(args[cur_arg], "id") == 0) { |
| 12349 | int id; |
| 12350 | char *error; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12351 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12352 | cur_arg++; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12353 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12354 | if (!args[cur_arg]) { |
| 12355 | memprintf(err, "missing id value"); |
| 12356 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12357 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12358 | } |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12359 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12360 | id = strtol(args[cur_arg], &error, 10); |
| 12361 | if (*error != '\0') { |
| 12362 | memprintf(err, "cannot parse id '%s'", args[cur_arg]); |
| 12363 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12364 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12365 | } |
| 12366 | cur_arg++; |
| 12367 | |
Olivier Houchard | fbc74e8 | 2017-11-24 16:54:05 +0100 | [diff] [blame] | 12368 | px->conf.args.ctx = ARGC_CAP; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12369 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 12370 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12371 | rule->action_ptr = http_action_req_capture_by_id; |
Christopher Faulet | 29730ba | 2017-09-18 15:26:32 +0200 | [diff] [blame] | 12372 | rule->check_ptr = check_http_req_capture; |
Thierry FOURNIER | e209797 | 2015-07-31 08:56:35 +0200 | [diff] [blame] | 12373 | rule->arg.capid.expr = expr; |
| 12374 | rule->arg.capid.idx = id; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12375 | } |
| 12376 | |
| 12377 | else { |
| 12378 | memprintf(err, "expects 'len' or 'id', found '%s'", args[cur_arg]); |
| 12379 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12380 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12381 | } |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12382 | |
| 12383 | *orig_arg = cur_arg; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12384 | return ACT_RET_PRS_OK; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12385 | } |
| 12386 | |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12387 | /* This function executes the "capture" action and store the result in a |
| 12388 | * capture slot if exists. It executes a fetch expression, turns the result |
| 12389 | * into a string and puts it in a capture slot. It always returns 1. If an |
| 12390 | * error occurs the action is cancelled, but the rule processing continues. |
| 12391 | */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12392 | 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] | 12393 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12394 | { |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12395 | struct sample *key; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12396 | struct cap_hdr *h; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12397 | char **cap = s->res_cap; |
| 12398 | struct proxy *fe = strm_fe(s); |
| 12399 | int len; |
| 12400 | int i; |
| 12401 | |
| 12402 | /* Look for the original configuration. */ |
| 12403 | for (h = fe->rsp_cap, i = fe->nb_rsp_cap - 1; |
Thierry FOURNIER | e209797 | 2015-07-31 08:56:35 +0200 | [diff] [blame] | 12404 | h != NULL && i != rule->arg.capid.idx ; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12405 | i--, h = h->next); |
| 12406 | if (!h) |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12407 | return ACT_RET_CONT; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12408 | |
Thierry FOURNIER | e209797 | 2015-07-31 08:56:35 +0200 | [diff] [blame] | 12409 | 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] | 12410 | if (!key) |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12411 | return ACT_RET_CONT; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12412 | |
| 12413 | if (cap[h->index] == NULL) |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 12414 | cap[h->index] = pool_alloc(h->pool); |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12415 | |
| 12416 | if (cap[h->index] == NULL) /* no more capture memory */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12417 | return ACT_RET_CONT; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12418 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12419 | len = key->data.u.str.len; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12420 | if (len > h->len) |
| 12421 | len = h->len; |
| 12422 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12423 | memcpy(cap[h->index], key->data.u.str.str, len); |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12424 | cap[h->index][len] = 0; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12425 | return ACT_RET_CONT; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12426 | } |
| 12427 | |
Christopher Faulet | 29730ba | 2017-09-18 15:26:32 +0200 | [diff] [blame] | 12428 | /* Check an "http-response capture" action. |
| 12429 | * |
| 12430 | * The function returns 1 in success case, otherwise, it returns 0 and err is |
| 12431 | * filled. |
| 12432 | */ |
| 12433 | int check_http_res_capture(struct act_rule *rule, struct proxy *px, char **err) |
| 12434 | { |
Christopher Faulet | fd608dd | 2017-12-04 09:45:15 +0100 | [diff] [blame] | 12435 | if (rule->action_ptr != http_action_res_capture_by_id) |
| 12436 | return 1; |
| 12437 | |
Christopher Faulet | 29730ba | 2017-09-18 15:26:32 +0200 | [diff] [blame] | 12438 | if (rule->arg.capid.idx >= px->nb_rsp_cap) { |
| 12439 | memprintf(err, "unable to find capture id '%d' referenced by http-response capture rule", |
| 12440 | rule->arg.capid.idx); |
| 12441 | return 0; |
| 12442 | } |
| 12443 | |
| 12444 | return 1; |
| 12445 | } |
| 12446 | |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12447 | /* parse an "http-response capture" action. It takes a single argument which is |
| 12448 | * a sample fetch expression. It stores the expression into arg->act.p[0] and |
| 12449 | * 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] | 12450 | * 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] | 12451 | */ |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12452 | enum act_parse_ret parse_http_res_capture(const char **args, int *orig_arg, struct proxy *px, |
| 12453 | struct act_rule *rule, char **err) |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12454 | { |
| 12455 | struct sample_expr *expr; |
| 12456 | int cur_arg; |
| 12457 | int id; |
| 12458 | char *error; |
| 12459 | |
| 12460 | for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++) |
| 12461 | if (strcmp(args[cur_arg], "if") == 0 || |
| 12462 | strcmp(args[cur_arg], "unless") == 0) |
| 12463 | break; |
| 12464 | |
| 12465 | if (cur_arg < *orig_arg + 3) { |
Willy Tarreau | 29bdb1c | 2016-06-24 15:36:34 +0200 | [diff] [blame] | 12466 | memprintf(err, "expects <expression> id <idx>"); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12467 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12468 | } |
| 12469 | |
| 12470 | cur_arg = *orig_arg; |
| 12471 | expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args); |
| 12472 | if (!expr) |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12473 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12474 | |
| 12475 | if (!(expr->fetch->val & SMP_VAL_FE_HRS_HDR)) { |
| 12476 | memprintf(err, |
| 12477 | "fetch method '%s' extracts information from '%s', none of which is available here", |
| 12478 | args[cur_arg-1], sample_src_names(expr->fetch->use)); |
| 12479 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12480 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12481 | } |
| 12482 | |
| 12483 | if (!args[cur_arg] || !*args[cur_arg]) { |
Willy Tarreau | 29bdb1c | 2016-06-24 15:36:34 +0200 | [diff] [blame] | 12484 | memprintf(err, "expects 'id'"); |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12485 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12486 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12487 | } |
| 12488 | |
| 12489 | if (strcmp(args[cur_arg], "id") != 0) { |
| 12490 | memprintf(err, "expects 'id', found '%s'", args[cur_arg]); |
| 12491 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12492 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12493 | } |
| 12494 | |
| 12495 | cur_arg++; |
| 12496 | |
| 12497 | if (!args[cur_arg]) { |
| 12498 | memprintf(err, "missing id value"); |
| 12499 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12500 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12501 | } |
| 12502 | |
| 12503 | id = strtol(args[cur_arg], &error, 10); |
| 12504 | if (*error != '\0') { |
| 12505 | memprintf(err, "cannot parse id '%s'", args[cur_arg]); |
| 12506 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12507 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12508 | } |
| 12509 | cur_arg++; |
| 12510 | |
Olivier Houchard | fbc74e8 | 2017-11-24 16:54:05 +0100 | [diff] [blame] | 12511 | px->conf.args.ctx = ARGC_CAP; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12512 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 12513 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12514 | rule->action_ptr = http_action_res_capture_by_id; |
Christopher Faulet | 29730ba | 2017-09-18 15:26:32 +0200 | [diff] [blame] | 12515 | rule->check_ptr = check_http_res_capture; |
Thierry FOURNIER | e209797 | 2015-07-31 08:56:35 +0200 | [diff] [blame] | 12516 | rule->arg.capid.expr = expr; |
| 12517 | rule->arg.capid.idx = id; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12518 | |
| 12519 | *orig_arg = cur_arg; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12520 | return ACT_RET_PRS_OK; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12521 | } |
| 12522 | |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 12523 | /* |
| 12524 | * Return the struct http_req_action_kw associated to a keyword. |
| 12525 | */ |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 12526 | struct action_kw *action_http_req_custom(const char *kw) |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 12527 | { |
Thierry FOURNIER | 322a124 | 2015-08-19 09:07:47 +0200 | [diff] [blame] | 12528 | return action_lookup(&http_req_keywords.list, kw); |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 12529 | } |
| 12530 | |
| 12531 | /* |
| 12532 | * Return the struct http_res_action_kw associated to a keyword. |
| 12533 | */ |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 12534 | struct action_kw *action_http_res_custom(const char *kw) |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 12535 | { |
Thierry FOURNIER | 322a124 | 2015-08-19 09:07:47 +0200 | [diff] [blame] | 12536 | return action_lookup(&http_res_keywords.list, kw); |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 12537 | } |
| 12538 | |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12539 | |
| 12540 | /* "show errors" handler for the CLI. Returns 0 if wants to continue, 1 to stop |
| 12541 | * now. |
| 12542 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 12543 | static int cli_parse_show_errors(char **args, char *payload, struct appctx *appctx, void *private) |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12544 | { |
| 12545 | if (!cli_has_level(appctx, ACCESS_LVL_OPER)) |
| 12546 | return 1; |
| 12547 | |
Willy Tarreau | 234ba2d | 2016-11-25 08:39:10 +0100 | [diff] [blame] | 12548 | if (*args[2]) { |
| 12549 | struct proxy *px; |
| 12550 | |
| 12551 | px = proxy_find_by_name(args[2], 0, 0); |
| 12552 | if (px) |
| 12553 | appctx->ctx.errors.iid = px->uuid; |
| 12554 | else |
| 12555 | appctx->ctx.errors.iid = atoi(args[2]); |
| 12556 | |
| 12557 | if (!appctx->ctx.errors.iid) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 12558 | appctx->ctx.cli.severity = LOG_ERR; |
Willy Tarreau | 234ba2d | 2016-11-25 08:39:10 +0100 | [diff] [blame] | 12559 | appctx->ctx.cli.msg = "No such proxy.\n"; |
| 12560 | appctx->st0 = CLI_ST_PRINT; |
| 12561 | return 1; |
| 12562 | } |
| 12563 | } |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12564 | else |
Willy Tarreau | 234ba2d | 2016-11-25 08:39:10 +0100 | [diff] [blame] | 12565 | appctx->ctx.errors.iid = -1; // dump all proxies |
| 12566 | |
Willy Tarreau | 35069f8 | 2016-11-25 09:16:37 +0100 | [diff] [blame] | 12567 | appctx->ctx.errors.flag = 0; |
| 12568 | if (strcmp(args[3], "request") == 0) |
| 12569 | appctx->ctx.errors.flag |= 4; // ignore response |
| 12570 | else if (strcmp(args[3], "response") == 0) |
| 12571 | appctx->ctx.errors.flag |= 2; // ignore request |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12572 | appctx->ctx.errors.px = NULL; |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12573 | return 0; |
| 12574 | } |
| 12575 | |
| 12576 | /* This function dumps all captured errors onto the stream interface's |
| 12577 | * read buffer. It returns 0 if the output buffer is full and it needs |
| 12578 | * to be called again, otherwise non-zero. |
| 12579 | */ |
| 12580 | static int cli_io_handler_show_errors(struct appctx *appctx) |
| 12581 | { |
| 12582 | struct stream_interface *si = appctx->owner; |
| 12583 | extern const char *monthname[12]; |
| 12584 | |
| 12585 | if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) |
| 12586 | return 1; |
| 12587 | |
| 12588 | chunk_reset(&trash); |
| 12589 | |
| 12590 | if (!appctx->ctx.errors.px) { |
| 12591 | /* the function had not been called yet, let's prepare the |
| 12592 | * buffer for a response. |
| 12593 | */ |
| 12594 | struct tm tm; |
| 12595 | |
| 12596 | get_localtime(date.tv_sec, &tm); |
| 12597 | chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n", |
| 12598 | tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900, |
| 12599 | tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000), |
| 12600 | error_snapshot_id); |
| 12601 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 12602 | if (ci_putchk(si_ic(si), &trash) == -1) { |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12603 | /* Socket buffer full. Let's try again later from the same point */ |
| 12604 | si_applet_cant_put(si); |
| 12605 | return 0; |
| 12606 | } |
| 12607 | |
Olivier Houchard | fbc74e8 | 2017-11-24 16:54:05 +0100 | [diff] [blame] | 12608 | appctx->ctx.errors.px = proxies_list; |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12609 | appctx->ctx.errors.bol = 0; |
| 12610 | appctx->ctx.errors.ptr = -1; |
| 12611 | } |
| 12612 | |
| 12613 | /* we have two inner loops here, one for the proxy, the other one for |
| 12614 | * the buffer. |
| 12615 | */ |
| 12616 | while (appctx->ctx.errors.px) { |
| 12617 | struct error_snapshot *es; |
| 12618 | |
Willy Tarreau | 35069f8 | 2016-11-25 09:16:37 +0100 | [diff] [blame] | 12619 | if ((appctx->ctx.errors.flag & 1) == 0) { |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12620 | es = &appctx->ctx.errors.px->invalid_req; |
Willy Tarreau | 35069f8 | 2016-11-25 09:16:37 +0100 | [diff] [blame] | 12621 | if (appctx->ctx.errors.flag & 2) // skip req |
| 12622 | goto next; |
| 12623 | } |
| 12624 | else { |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12625 | es = &appctx->ctx.errors.px->invalid_rep; |
Willy Tarreau | 35069f8 | 2016-11-25 09:16:37 +0100 | [diff] [blame] | 12626 | if (appctx->ctx.errors.flag & 4) // skip resp |
| 12627 | goto next; |
| 12628 | } |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12629 | |
| 12630 | if (!es->when.tv_sec) |
| 12631 | goto next; |
| 12632 | |
| 12633 | if (appctx->ctx.errors.iid >= 0 && |
| 12634 | appctx->ctx.errors.px->uuid != appctx->ctx.errors.iid && |
| 12635 | es->oe->uuid != appctx->ctx.errors.iid) |
| 12636 | goto next; |
| 12637 | |
| 12638 | if (appctx->ctx.errors.ptr < 0) { |
| 12639 | /* just print headers now */ |
| 12640 | |
| 12641 | char pn[INET6_ADDRSTRLEN]; |
| 12642 | struct tm tm; |
| 12643 | int port; |
| 12644 | |
| 12645 | get_localtime(es->when.tv_sec, &tm); |
| 12646 | chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]", |
| 12647 | tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900, |
| 12648 | tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000)); |
| 12649 | |
| 12650 | switch (addr_to_str(&es->src, pn, sizeof(pn))) { |
| 12651 | case AF_INET: |
| 12652 | case AF_INET6: |
| 12653 | port = get_host_port(&es->src); |
| 12654 | break; |
| 12655 | default: |
| 12656 | port = 0; |
| 12657 | } |
| 12658 | |
Willy Tarreau | 35069f8 | 2016-11-25 09:16:37 +0100 | [diff] [blame] | 12659 | switch (appctx->ctx.errors.flag & 1) { |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12660 | case 0: |
| 12661 | chunk_appendf(&trash, |
| 12662 | " frontend %s (#%d): invalid request\n" |
| 12663 | " backend %s (#%d)", |
| 12664 | appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid, |
| 12665 | (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>", |
| 12666 | (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1); |
| 12667 | break; |
| 12668 | case 1: |
| 12669 | chunk_appendf(&trash, |
| 12670 | " backend %s (#%d): invalid response\n" |
| 12671 | " frontend %s (#%d)", |
| 12672 | appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid, |
| 12673 | es->oe->id, es->oe->uuid); |
| 12674 | break; |
| 12675 | } |
| 12676 | |
| 12677 | chunk_appendf(&trash, |
| 12678 | ", server %s (#%d), event #%u\n" |
| 12679 | " src %s:%d, session #%d, session flags 0x%08x\n" |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 12680 | " HTTP msg state %s(%d), msg flags 0x%08x, tx flags 0x%08x\n" |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12681 | " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n" |
| 12682 | " buffer flags 0x%08x, out %d bytes, total %lld bytes\n" |
| 12683 | " pending %d bytes, wrapping at %d, error at position %d:\n \n", |
| 12684 | es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1, |
| 12685 | es->ev_id, |
| 12686 | pn, port, es->sid, es->s_flags, |
Willy Tarreau | 0da5b3b | 2017-09-21 09:30:46 +0200 | [diff] [blame] | 12687 | h1_msg_state_str(es->state), es->state, es->m_flags, es->t_flags, |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12688 | es->m_clen, es->m_blen, |
| 12689 | es->b_flags, es->b_out, es->b_tot, |
| 12690 | es->len, es->b_wrap, es->pos); |
| 12691 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 12692 | if (ci_putchk(si_ic(si), &trash) == -1) { |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12693 | /* Socket buffer full. Let's try again later from the same point */ |
| 12694 | si_applet_cant_put(si); |
| 12695 | return 0; |
| 12696 | } |
| 12697 | appctx->ctx.errors.ptr = 0; |
| 12698 | appctx->ctx.errors.sid = es->sid; |
| 12699 | } |
| 12700 | |
| 12701 | if (appctx->ctx.errors.sid != es->sid) { |
| 12702 | /* the snapshot changed while we were dumping it */ |
| 12703 | chunk_appendf(&trash, |
| 12704 | " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n"); |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 12705 | if (ci_putchk(si_ic(si), &trash) == -1) { |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12706 | si_applet_cant_put(si); |
| 12707 | return 0; |
| 12708 | } |
| 12709 | goto next; |
| 12710 | } |
| 12711 | |
| 12712 | /* OK, ptr >= 0, so we have to dump the current line */ |
| 12713 | while (es->buf && appctx->ctx.errors.ptr < es->len && appctx->ctx.errors.ptr < global.tune.bufsize) { |
| 12714 | int newptr; |
| 12715 | int newline; |
| 12716 | |
| 12717 | newline = appctx->ctx.errors.bol; |
| 12718 | newptr = dump_text_line(&trash, es->buf, global.tune.bufsize, es->len, &newline, appctx->ctx.errors.ptr); |
| 12719 | if (newptr == appctx->ctx.errors.ptr) |
| 12720 | return 0; |
| 12721 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 12722 | if (ci_putchk(si_ic(si), &trash) == -1) { |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12723 | /* Socket buffer full. Let's try again later from the same point */ |
| 12724 | si_applet_cant_put(si); |
| 12725 | return 0; |
| 12726 | } |
| 12727 | appctx->ctx.errors.ptr = newptr; |
| 12728 | appctx->ctx.errors.bol = newline; |
| 12729 | }; |
| 12730 | next: |
| 12731 | appctx->ctx.errors.bol = 0; |
| 12732 | appctx->ctx.errors.ptr = -1; |
Willy Tarreau | 35069f8 | 2016-11-25 09:16:37 +0100 | [diff] [blame] | 12733 | appctx->ctx.errors.flag ^= 1; |
| 12734 | if (!(appctx->ctx.errors.flag & 1)) |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12735 | appctx->ctx.errors.px = appctx->ctx.errors.px->next; |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12736 | } |
| 12737 | |
| 12738 | /* dump complete */ |
| 12739 | return 1; |
| 12740 | } |
| 12741 | |
| 12742 | /* register cli keywords */ |
| 12743 | static struct cli_kw_list cli_kws = {{ },{ |
| 12744 | { { "show", "errors", NULL }, |
| 12745 | "show errors : report last request and response errors for each proxy", |
| 12746 | cli_parse_show_errors, cli_io_handler_show_errors, NULL, |
| 12747 | }, |
| 12748 | {{},} |
| 12749 | }}; |
| 12750 | |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 12751 | /************************************************************************/ |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12752 | /* All supported ACL keywords must be declared here. */ |
| 12753 | /************************************************************************/ |
| 12754 | |
| 12755 | /* Note: must not be declared <const> as its list will be overwritten. |
| 12756 | * Please take care of keeping this list alphabetically sorted. |
| 12757 | */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 12758 | static struct acl_kw_list acl_kws = {ILH, { |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12759 | { "base", "base", PAT_MATCH_STR }, |
| 12760 | { "base_beg", "base", PAT_MATCH_BEG }, |
| 12761 | { "base_dir", "base", PAT_MATCH_DIR }, |
| 12762 | { "base_dom", "base", PAT_MATCH_DOM }, |
| 12763 | { "base_end", "base", PAT_MATCH_END }, |
| 12764 | { "base_len", "base", PAT_MATCH_LEN }, |
| 12765 | { "base_reg", "base", PAT_MATCH_REG }, |
| 12766 | { "base_sub", "base", PAT_MATCH_SUB }, |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 12767 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12768 | { "cook", "req.cook", PAT_MATCH_STR }, |
| 12769 | { "cook_beg", "req.cook", PAT_MATCH_BEG }, |
| 12770 | { "cook_dir", "req.cook", PAT_MATCH_DIR }, |
| 12771 | { "cook_dom", "req.cook", PAT_MATCH_DOM }, |
| 12772 | { "cook_end", "req.cook", PAT_MATCH_END }, |
| 12773 | { "cook_len", "req.cook", PAT_MATCH_LEN }, |
| 12774 | { "cook_reg", "req.cook", PAT_MATCH_REG }, |
| 12775 | { "cook_sub", "req.cook", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12776 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12777 | { "hdr", "req.hdr", PAT_MATCH_STR }, |
| 12778 | { "hdr_beg", "req.hdr", PAT_MATCH_BEG }, |
| 12779 | { "hdr_dir", "req.hdr", PAT_MATCH_DIR }, |
| 12780 | { "hdr_dom", "req.hdr", PAT_MATCH_DOM }, |
| 12781 | { "hdr_end", "req.hdr", PAT_MATCH_END }, |
| 12782 | { "hdr_len", "req.hdr", PAT_MATCH_LEN }, |
| 12783 | { "hdr_reg", "req.hdr", PAT_MATCH_REG }, |
| 12784 | { "hdr_sub", "req.hdr", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12785 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12786 | /* these two declarations uses strings with list storage (in place |
| 12787 | * of tree storage). The basic match is PAT_MATCH_STR, but the indexation |
| 12788 | * and delete functions are relative to the list management. The parse |
| 12789 | * and match method are related to the corresponding fetch methods. This |
| 12790 | * is very particular ACL declaration mode. |
| 12791 | */ |
| 12792 | { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth }, |
| 12793 | { "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] | 12794 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12795 | { "path", "path", PAT_MATCH_STR }, |
| 12796 | { "path_beg", "path", PAT_MATCH_BEG }, |
| 12797 | { "path_dir", "path", PAT_MATCH_DIR }, |
| 12798 | { "path_dom", "path", PAT_MATCH_DOM }, |
| 12799 | { "path_end", "path", PAT_MATCH_END }, |
| 12800 | { "path_len", "path", PAT_MATCH_LEN }, |
| 12801 | { "path_reg", "path", PAT_MATCH_REG }, |
| 12802 | { "path_sub", "path", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12803 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12804 | { "req_ver", "req.ver", PAT_MATCH_STR }, |
| 12805 | { "resp_ver", "res.ver", PAT_MATCH_STR }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12806 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12807 | { "scook", "res.cook", PAT_MATCH_STR }, |
| 12808 | { "scook_beg", "res.cook", PAT_MATCH_BEG }, |
| 12809 | { "scook_dir", "res.cook", PAT_MATCH_DIR }, |
| 12810 | { "scook_dom", "res.cook", PAT_MATCH_DOM }, |
| 12811 | { "scook_end", "res.cook", PAT_MATCH_END }, |
| 12812 | { "scook_len", "res.cook", PAT_MATCH_LEN }, |
| 12813 | { "scook_reg", "res.cook", PAT_MATCH_REG }, |
| 12814 | { "scook_sub", "res.cook", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12815 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12816 | { "shdr", "res.hdr", PAT_MATCH_STR }, |
| 12817 | { "shdr_beg", "res.hdr", PAT_MATCH_BEG }, |
| 12818 | { "shdr_dir", "res.hdr", PAT_MATCH_DIR }, |
| 12819 | { "shdr_dom", "res.hdr", PAT_MATCH_DOM }, |
| 12820 | { "shdr_end", "res.hdr", PAT_MATCH_END }, |
| 12821 | { "shdr_len", "res.hdr", PAT_MATCH_LEN }, |
| 12822 | { "shdr_reg", "res.hdr", PAT_MATCH_REG }, |
| 12823 | { "shdr_sub", "res.hdr", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12824 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12825 | { "url", "url", PAT_MATCH_STR }, |
| 12826 | { "url_beg", "url", PAT_MATCH_BEG }, |
| 12827 | { "url_dir", "url", PAT_MATCH_DIR }, |
| 12828 | { "url_dom", "url", PAT_MATCH_DOM }, |
| 12829 | { "url_end", "url", PAT_MATCH_END }, |
| 12830 | { "url_len", "url", PAT_MATCH_LEN }, |
| 12831 | { "url_reg", "url", PAT_MATCH_REG }, |
| 12832 | { "url_sub", "url", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12833 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12834 | { "urlp", "urlp", PAT_MATCH_STR }, |
| 12835 | { "urlp_beg", "urlp", PAT_MATCH_BEG }, |
| 12836 | { "urlp_dir", "urlp", PAT_MATCH_DIR }, |
| 12837 | { "urlp_dom", "urlp", PAT_MATCH_DOM }, |
| 12838 | { "urlp_end", "urlp", PAT_MATCH_END }, |
| 12839 | { "urlp_len", "urlp", PAT_MATCH_LEN }, |
| 12840 | { "urlp_reg", "urlp", PAT_MATCH_REG }, |
| 12841 | { "urlp_sub", "urlp", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12842 | |
Willy Tarreau | 8ed669b | 2013-01-11 15:49:37 +0100 | [diff] [blame] | 12843 | { /* END */ }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12844 | }}; |
| 12845 | |
| 12846 | /************************************************************************/ |
| 12847 | /* All supported pattern keywords must be declared here. */ |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 12848 | /************************************************************************/ |
| 12849 | /* Note: must not be declared <const> as its list will be overwritten */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 12850 | static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, { |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12851 | { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 12852 | { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 12853 | { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV }, |
| 12854 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 12855 | /* capture are allocated and are permanent in the stream */ |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 12856 | { "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] | 12857 | |
| 12858 | /* retrieve these captures from the HTTP logs */ |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 12859 | { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP }, |
| 12860 | { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP }, |
| 12861 | { "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] | 12862 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 12863 | { "capture.res.hdr", smp_fetch_capture_header_res, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP }, |
| 12864 | { "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] | 12865 | |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 12866 | /* cookie is valid in both directions (eg: for "stick ...") but cook* |
| 12867 | * are only here to match the ACL's name, are request-only and are used |
| 12868 | * for ACL compatibility only. |
| 12869 | */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12870 | { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 12871 | { "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] | 12872 | { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 12873 | { "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] | 12874 | |
| 12875 | /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are |
| 12876 | * only here to match the ACL's name, are request-only and are used for |
| 12877 | * ACL compatibility only. |
| 12878 | */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12879 | { "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] | 12880 | { "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] | 12881 | { "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] | 12882 | { "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] | 12883 | |
Willy Tarreau | 0a0daec | 2013-04-02 22:44:58 +0200 | [diff] [blame] | 12884 | { "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] | 12885 | { "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] | 12886 | { "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] | 12887 | { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP }, |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12888 | { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 12889 | { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 12890 | |
| 12891 | /* HTTP protocol on the request path */ |
| 12892 | { "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] | 12893 | { "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] | 12894 | |
| 12895 | /* HTTP version on the request path */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12896 | { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 12897 | { "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] | 12898 | |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 12899 | { "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] | 12900 | { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 12901 | { "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] | 12902 | { "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] | 12903 | |
Thierry FOURNIER | d7d8881 | 2017-04-19 15:15:14 +0200 | [diff] [blame] | 12904 | { "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] | 12905 | { "req.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV }, |
| 12906 | |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 12907 | /* HTTP version on the response path */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12908 | { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV }, |
| 12909 | { "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] | 12910 | |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 12911 | /* 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] | 12912 | { "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] | 12913 | { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 12914 | { "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] | 12915 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12916 | { "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] | 12917 | { "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] | 12918 | { "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] | 12919 | { "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] | 12920 | { "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] | 12921 | { "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] | 12922 | { "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] | 12923 | |
| 12924 | /* 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] | 12925 | { "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] | 12926 | { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV }, |
| 12927 | { "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] | 12928 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12929 | { "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] | 12930 | { "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] | 12931 | { "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] | 12932 | { "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] | 12933 | { "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] | 12934 | { "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] | 12935 | { "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] | 12936 | |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 12937 | /* 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] | 12938 | { "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] | 12939 | { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV }, |
| 12940 | { "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] | 12941 | { "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] | 12942 | |
| 12943 | /* 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] | 12944 | { "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] | 12945 | { "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] | 12946 | { "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] | 12947 | { "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] | 12948 | |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 12949 | { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP }, |
Thierry Fournier | 0e00dca | 2016-04-07 15:47:40 +0200 | [diff] [blame] | 12950 | { "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] | 12951 | { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 12952 | { "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] | 12953 | { "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] | 12954 | { "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] | 12955 | { "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] | 12956 | { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 12957 | { "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] | 12958 | { "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] | 12959 | { /* END */ }, |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 12960 | }}; |
| 12961 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 12962 | |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12963 | /************************************************************************/ |
| 12964 | /* All supported converter keywords must be declared here. */ |
| 12965 | /************************************************************************/ |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 12966 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 12967 | static struct sample_conv_kw_list sample_conv_kws = {ILH, { |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 12968 | { "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] | 12969 | { "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] | 12970 | { "capture-req", smp_conv_req_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR}, |
| 12971 | { "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] | 12972 | { "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] | 12973 | { NULL, NULL, 0, 0, 0 }, |
| 12974 | }}; |
| 12975 | |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 12976 | |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12977 | /************************************************************************/ |
| 12978 | /* All supported http-request action keywords must be declared here. */ |
| 12979 | /************************************************************************/ |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 12980 | struct action_kw_list http_req_actions = { |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12981 | .kw = { |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12982 | { "capture", parse_http_req_capture }, |
Willy Tarreau | 53275e8 | 2017-11-24 07:52:01 +0100 | [diff] [blame] | 12983 | { "reject", parse_http_action_reject }, |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12984 | { "set-method", parse_set_req_line }, |
| 12985 | { "set-path", parse_set_req_line }, |
| 12986 | { "set-query", parse_set_req_line }, |
| 12987 | { "set-uri", parse_set_req_line }, |
Willy Tarreau | cb703b0 | 2015-04-03 09:52:01 +0200 | [diff] [blame] | 12988 | { NULL, NULL } |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12989 | } |
| 12990 | }; |
| 12991 | |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 12992 | struct action_kw_list http_res_actions = { |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12993 | .kw = { |
| 12994 | { "capture", parse_http_res_capture }, |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 12995 | { "set-status", parse_http_set_status }, |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12996 | { NULL, NULL } |
| 12997 | } |
| 12998 | }; |
| 12999 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 13000 | __attribute__((constructor)) |
| 13001 | static void __http_protocol_init(void) |
| 13002 | { |
| 13003 | acl_register_keywords(&acl_kws); |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 13004 | sample_register_fetches(&sample_fetch_keywords); |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 13005 | sample_register_convs(&sample_conv_kws); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 13006 | http_req_keywords_register(&http_req_actions); |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 13007 | http_res_keywords_register(&http_res_actions); |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 13008 | cli_register_kw(&cli_kws); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 13009 | } |
| 13010 | |
| 13011 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 13012 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 13013 | * Local variables: |
| 13014 | * c-indent-level: 8 |
| 13015 | * c-basic-offset: 8 |
| 13016 | * End: |
| 13017 | */ |