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 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2634 | http_header_add_tail2(&txn->req, &txn->hdr_idx, replace->str, replace->len); |
| 2635 | |
| 2636 | free_trash_chunk(replace); |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 2637 | break; |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2638 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2639 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2640 | case ACT_HTTP_DEL_ACL: |
| 2641 | case ACT_HTTP_DEL_MAP: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2642 | struct pat_ref *ref; |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2643 | struct chunk *key; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2644 | |
| 2645 | /* collect reference */ |
| 2646 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 2647 | if (!ref) |
| 2648 | continue; |
| 2649 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2650 | /* allocate key */ |
| 2651 | key = alloc_trash_chunk(); |
| 2652 | if (!key) |
| 2653 | return HTTP_RULE_RES_BADREQ; |
| 2654 | |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2655 | /* collect key */ |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2656 | key->len = build_logline(s, key->str, key->size, &rule->arg.map.key); |
| 2657 | key->str[key->len] = '\0'; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2658 | |
| 2659 | /* perform update */ |
| 2660 | /* returned code: 1=ok, 0=ko */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2661 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2662 | pat_ref_delete(ref, key->str); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2663 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2664 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2665 | free_trash_chunk(key); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2666 | break; |
| 2667 | } |
| 2668 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2669 | case ACT_HTTP_ADD_ACL: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2670 | struct pat_ref *ref; |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2671 | struct chunk *key; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2672 | |
| 2673 | /* collect reference */ |
| 2674 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 2675 | if (!ref) |
| 2676 | continue; |
| 2677 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2678 | /* allocate key */ |
| 2679 | key = alloc_trash_chunk(); |
| 2680 | if (!key) |
| 2681 | return HTTP_RULE_RES_BADREQ; |
| 2682 | |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2683 | /* collect key */ |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2684 | key->len = build_logline(s, key->str, key->size, &rule->arg.map.key); |
| 2685 | key->str[key->len] = '\0'; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2686 | |
| 2687 | /* perform update */ |
| 2688 | /* add entry only if it does not already exist */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2689 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2690 | if (pat_ref_find_elt(ref, key->str) == NULL) |
| 2691 | pat_ref_add(ref, key->str, NULL, NULL); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2692 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2693 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2694 | free_trash_chunk(key); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2695 | break; |
| 2696 | } |
| 2697 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2698 | case ACT_HTTP_SET_MAP: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2699 | struct pat_ref *ref; |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2700 | struct chunk *key, *value; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2701 | |
| 2702 | /* collect reference */ |
| 2703 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 2704 | if (!ref) |
| 2705 | continue; |
| 2706 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2707 | /* allocate key */ |
| 2708 | key = alloc_trash_chunk(); |
| 2709 | if (!key) |
| 2710 | return HTTP_RULE_RES_BADREQ; |
| 2711 | |
| 2712 | /* allocate value */ |
| 2713 | value = alloc_trash_chunk(); |
| 2714 | if (!value) { |
| 2715 | free_trash_chunk(key); |
| 2716 | return HTTP_RULE_RES_BADREQ; |
| 2717 | } |
| 2718 | |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2719 | /* collect key */ |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2720 | key->len = build_logline(s, key->str, key->size, &rule->arg.map.key); |
| 2721 | key->str[key->len] = '\0'; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2722 | |
| 2723 | /* collect value */ |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2724 | value->len = build_logline(s, value->str, value->size, &rule->arg.map.value); |
| 2725 | value->str[value->len] = '\0'; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2726 | |
| 2727 | /* perform update */ |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2728 | if (pat_ref_find_elt(ref, key->str) != NULL) |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2729 | /* update entry if it exists */ |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2730 | pat_ref_set(ref, key->str, value->str, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2731 | else |
| 2732 | /* insert a new entry */ |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2733 | pat_ref_add(ref, key->str, value->str, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2734 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2735 | free_trash_chunk(key); |
| 2736 | free_trash_chunk(value); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2737 | break; |
| 2738 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 2739 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 2740 | case ACT_CUSTOM: |
Willy Tarreau | 7aa15b0 | 2017-12-20 16:56:50 +0100 | [diff] [blame] | 2741 | if ((s->req.flags & CF_READ_ERROR) || |
| 2742 | ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) && |
| 2743 | !(s->si[0].flags & SI_FL_CLEAN_ABRT) && |
| 2744 | (px->options & PR_O_ABRT_CLOSE))) |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 2745 | act_flags |= ACT_FLAG_FINAL; |
Willy Tarreau | 3945868 | 2015-09-27 10:33:15 +0200 | [diff] [blame] | 2746 | |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 2747 | switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) { |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 2748 | case ACT_RET_ERR: |
| 2749 | case ACT_RET_CONT: |
| 2750 | break; |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 2751 | case ACT_RET_STOP: |
| 2752 | return HTTP_RULE_RES_DONE; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 2753 | case ACT_RET_YIELD: |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 2754 | s->current_rule = rule; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 2755 | return HTTP_RULE_RES_YIELD; |
| 2756 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 2757 | break; |
| 2758 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2759 | case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX: |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 2760 | /* Note: only the first valid tracking parameter of each |
| 2761 | * applies. |
| 2762 | */ |
| 2763 | |
Christopher Faulet | 4fce0d8 | 2017-09-18 11:57:31 +0200 | [diff] [blame] | 2764 | if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) { |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 2765 | struct stktable *t; |
| 2766 | struct stksess *ts; |
| 2767 | struct stktable_key *key; |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 2768 | void *ptr1, *ptr2; |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 2769 | |
Thierry FOURNIER | 5ec63e0 | 2015-08-04 09:09:48 +0200 | [diff] [blame] | 2770 | t = rule->arg.trk_ctr.table.t; |
| 2771 | 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] | 2772 | |
| 2773 | if (key && (ts = stktable_get_entry(t, key))) { |
Christopher Faulet | 4fce0d8 | 2017-09-18 11:57:31 +0200 | [diff] [blame] | 2774 | stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts); |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 2775 | |
| 2776 | /* 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] | 2777 | ptr1 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT); |
| 2778 | ptr2 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE); |
| 2779 | if (ptr1 || ptr2) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2780 | HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock); |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 2781 | |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 2782 | if (ptr1) |
| 2783 | stktable_data_cast(ptr1, http_req_cnt)++; |
| 2784 | |
| 2785 | if (ptr2) |
| 2786 | update_freq_ctr_period(&stktable_data_cast(ptr2, http_req_rate), |
| 2787 | t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1); |
| 2788 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2789 | HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock); |
Emeric Brun | 0fed0b0 | 2017-11-29 16:15:07 +0100 | [diff] [blame] | 2790 | |
| 2791 | /* If data was modified, we need to touch to re-schedule sync */ |
| 2792 | stktable_touch_local(t, ts, 0); |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 2793 | } |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 2794 | |
Christopher Faulet | 4fce0d8 | 2017-09-18 11:57:31 +0200 | [diff] [blame] | 2795 | stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT); |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 2796 | if (sess->fe != s->be) |
Christopher Faulet | 4fce0d8 | 2017-09-18 11:57:31 +0200 | [diff] [blame] | 2797 | stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND); |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 2798 | } |
| 2799 | } |
Adis Nezirovic | 2fbcafc | 2015-07-06 15:44:30 +0200 | [diff] [blame] | 2800 | break; |
| 2801 | |
Thierry FOURNIER | 22e4901 | 2015-08-05 19:13:48 +0200 | [diff] [blame] | 2802 | /* other flags exists, but normaly, they never be matched. */ |
| 2803 | default: |
| 2804 | break; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2805 | } |
| 2806 | } |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 2807 | |
| 2808 | /* we reached the end of the rules, nothing to report */ |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 2809 | return HTTP_RULE_RES_CONT; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2810 | } |
| 2811 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2812 | |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 2813 | /* Executes the http-response rules <rules> for stream <s> and proxy <px>. It |
| 2814 | * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP, |
| 2815 | * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT |
| 2816 | * is returned, the process can continue the evaluation of next rule list. If |
| 2817 | * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ |
| 2818 | * is returned, it means the operation could not be processed and a server error |
| 2819 | * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a |
| 2820 | * deny rule. If *YIELD is returned, the caller must call again the function |
| 2821 | * with the same context. |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2822 | */ |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 2823 | static enum rule_result |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 2824 | 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] | 2825 | { |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 2826 | struct session *sess = strm_sess(s); |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 2827 | struct http_txn *txn = s->txn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2828 | struct connection *cli_conn; |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 2829 | struct act_rule *rule; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2830 | struct hdr_ctx ctx; |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 2831 | int act_flags = 0; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2832 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 2833 | /* If "the current_rule_list" match the executed rule list, we are in |
| 2834 | * resume condition. If a resume is needed it is always in the action |
| 2835 | * and never in the ACL or converters. In this case, we initialise the |
| 2836 | * current rule, and go to the action execution point. |
| 2837 | */ |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 2838 | if (s->current_rule) { |
| 2839 | rule = s->current_rule; |
| 2840 | s->current_rule = NULL; |
| 2841 | if (s->current_rule_list == rules) |
| 2842 | goto resume_execution; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 2843 | } |
| 2844 | s->current_rule_list = rules; |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 2845 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2846 | list_for_each_entry(rule, rules, list) { |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2847 | |
| 2848 | /* check optional condition */ |
| 2849 | if (rule->cond) { |
| 2850 | int ret; |
| 2851 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 2852 | 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] | 2853 | ret = acl_pass(ret); |
| 2854 | |
| 2855 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 2856 | ret = !ret; |
| 2857 | |
| 2858 | if (!ret) /* condition not matched */ |
| 2859 | continue; |
| 2860 | } |
| 2861 | |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 2862 | act_flags |= ACT_FLAG_FIRST; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 2863 | resume_execution: |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2864 | switch (rule->action) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2865 | case ACT_ACTION_ALLOW: |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 2866 | return HTTP_RULE_RES_STOP; /* "allow" rules are OK */ |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2867 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2868 | case ACT_ACTION_DENY: |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2869 | txn->flags |= TX_SVDENY; |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 2870 | return HTTP_RULE_RES_STOP; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2871 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2872 | case ACT_HTTP_SET_NICE: |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 2873 | s->task->nice = rule->arg.nice; |
| 2874 | break; |
| 2875 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2876 | case ACT_HTTP_SET_TOS: |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 2877 | if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 2878 | 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] | 2879 | break; |
| 2880 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2881 | case ACT_HTTP_SET_MARK: |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 2882 | #ifdef SO_MARK |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 2883 | if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 2884 | 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] | 2885 | #endif |
| 2886 | break; |
| 2887 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2888 | case ACT_HTTP_SET_LOGL: |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 2889 | s->logs.level = rule->arg.loglevel; |
| 2890 | break; |
| 2891 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2892 | case ACT_HTTP_REPLACE_HDR: |
| 2893 | case ACT_HTTP_REPLACE_VAL: |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 2894 | if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name, |
| 2895 | rule->arg.hdr_add.name_len, |
| 2896 | &rule->arg.hdr_add.fmt, |
| 2897 | &rule->arg.hdr_add.re, rule->action)) |
Christopher Faulet | cdade94 | 2017-02-08 12:41:31 +0100 | [diff] [blame] | 2898 | return HTTP_RULE_RES_BADREQ; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 2899 | break; |
| 2900 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2901 | case ACT_HTTP_DEL_HDR: |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2902 | ctx.idx = 0; |
| 2903 | /* remove all occurrences of the header */ |
| 2904 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 2905 | txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 2906 | http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx); |
| 2907 | } |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 2908 | break; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2909 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2910 | case ACT_HTTP_SET_HDR: |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2911 | case ACT_HTTP_ADD_HDR: { |
| 2912 | struct chunk *replace; |
| 2913 | |
| 2914 | replace = alloc_trash_chunk(); |
| 2915 | if (!replace) |
| 2916 | return HTTP_RULE_RES_BADREQ; |
| 2917 | |
| 2918 | chunk_printf(replace, "%s: ", rule->arg.hdr_add.name); |
| 2919 | memcpy(replace->str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len); |
| 2920 | replace->len = rule->arg.hdr_add.name_len; |
| 2921 | replace->str[replace->len++] = ':'; |
| 2922 | replace->str[replace->len++] = ' '; |
| 2923 | replace->len += build_logline(s, replace->str + replace->len, replace->size - replace->len, |
| 2924 | &rule->arg.hdr_add.fmt); |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 2925 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2926 | if (rule->action == ACT_HTTP_SET_HDR) { |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 2927 | /* remove all occurrences of the header */ |
| 2928 | ctx.idx = 0; |
| 2929 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 2930 | txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 2931 | http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx); |
| 2932 | } |
| 2933 | } |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2934 | http_header_add_tail2(&txn->rsp, &txn->hdr_idx, replace->str, replace->len); |
| 2935 | |
| 2936 | free_trash_chunk(replace); |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2937 | break; |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2938 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2939 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2940 | case ACT_HTTP_DEL_ACL: |
| 2941 | case ACT_HTTP_DEL_MAP: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2942 | struct pat_ref *ref; |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2943 | struct chunk *key; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2944 | |
| 2945 | /* collect reference */ |
| 2946 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 2947 | if (!ref) |
| 2948 | continue; |
| 2949 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2950 | /* allocate key */ |
| 2951 | key = alloc_trash_chunk(); |
| 2952 | if (!key) |
| 2953 | return HTTP_RULE_RES_BADREQ; |
| 2954 | |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2955 | /* collect key */ |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2956 | key->len = build_logline(s, key->str, key->size, &rule->arg.map.key); |
| 2957 | key->str[key->len] = '\0'; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2958 | |
| 2959 | /* perform update */ |
| 2960 | /* returned code: 1=ok, 0=ko */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2961 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2962 | pat_ref_delete(ref, key->str); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2963 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2964 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2965 | free_trash_chunk(key); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2966 | break; |
| 2967 | } |
| 2968 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2969 | case ACT_HTTP_ADD_ACL: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2970 | struct pat_ref *ref; |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2971 | struct chunk *key; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2972 | |
| 2973 | /* collect reference */ |
| 2974 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 2975 | if (!ref) |
| 2976 | continue; |
| 2977 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2978 | /* allocate key */ |
| 2979 | key = alloc_trash_chunk(); |
| 2980 | if (!key) |
| 2981 | return HTTP_RULE_RES_BADREQ; |
| 2982 | |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2983 | /* collect key */ |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2984 | key->len = build_logline(s, key->str, key->size, &rule->arg.map.key); |
| 2985 | key->str[key->len] = '\0'; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2986 | |
| 2987 | /* perform update */ |
| 2988 | /* check if the entry already exists */ |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2989 | if (pat_ref_find_elt(ref, key->str) == NULL) |
| 2990 | pat_ref_add(ref, key->str, NULL, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2991 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2992 | free_trash_chunk(key); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2993 | break; |
| 2994 | } |
| 2995 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2996 | case ACT_HTTP_SET_MAP: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2997 | struct pat_ref *ref; |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2998 | struct chunk *key, *value; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2999 | |
| 3000 | /* collect reference */ |
| 3001 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3002 | if (!ref) |
| 3003 | continue; |
| 3004 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 3005 | /* allocate key */ |
| 3006 | key = alloc_trash_chunk(); |
| 3007 | if (!key) |
| 3008 | return HTTP_RULE_RES_BADREQ; |
| 3009 | |
| 3010 | /* allocate value */ |
| 3011 | value = alloc_trash_chunk(); |
| 3012 | if (!value) { |
| 3013 | free_trash_chunk(key); |
| 3014 | return HTTP_RULE_RES_BADREQ; |
| 3015 | } |
| 3016 | |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3017 | /* collect key */ |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 3018 | key->len = build_logline(s, key->str, key->size, &rule->arg.map.key); |
| 3019 | key->str[key->len] = '\0'; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3020 | |
| 3021 | /* collect value */ |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 3022 | value->len = build_logline(s, value->str, value->size, &rule->arg.map.value); |
| 3023 | value->str[value->len] = '\0'; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3024 | |
| 3025 | /* perform update */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 3026 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 3027 | if (pat_ref_find_elt(ref, key->str) != NULL) |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3028 | /* update entry if it exists */ |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 3029 | pat_ref_set(ref, key->str, value->str, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3030 | else |
| 3031 | /* insert a new entry */ |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 3032 | pat_ref_add(ref, key->str, value->str, NULL); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 3033 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 3034 | free_trash_chunk(key); |
| 3035 | free_trash_chunk(value); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3036 | break; |
| 3037 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 3038 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3039 | case ACT_HTTP_REDIR: |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 3040 | if (!http_apply_redirect_rule(rule->arg.redir, s, txn)) |
| 3041 | return HTTP_RULE_RES_BADREQ; |
| 3042 | return HTTP_RULE_RES_DONE; |
| 3043 | |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 3044 | case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX: |
| 3045 | /* Note: only the first valid tracking parameter of each |
| 3046 | * applies. |
| 3047 | */ |
| 3048 | |
Christopher Faulet | 4fce0d8 | 2017-09-18 11:57:31 +0200 | [diff] [blame] | 3049 | if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) { |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 3050 | struct stktable *t; |
| 3051 | struct stksess *ts; |
| 3052 | struct stktable_key *key; |
| 3053 | void *ptr; |
| 3054 | |
| 3055 | t = rule->arg.trk_ctr.table.t; |
| 3056 | key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_RES | SMP_OPT_FINAL, rule->arg.trk_ctr.expr, NULL); |
| 3057 | |
| 3058 | if (key && (ts = stktable_get_entry(t, key))) { |
Christopher Faulet | 4fce0d8 | 2017-09-18 11:57:31 +0200 | [diff] [blame] | 3059 | stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts); |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 3060 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 3061 | HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock); |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 3062 | |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 3063 | /* let's count a new HTTP request as it's the first time we do it */ |
| 3064 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT); |
| 3065 | if (ptr) |
| 3066 | stktable_data_cast(ptr, http_req_cnt)++; |
| 3067 | |
| 3068 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE); |
| 3069 | if (ptr) |
| 3070 | update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate), |
| 3071 | t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1); |
| 3072 | |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 3073 | /* When the client triggers a 4xx from the server, it's most often due |
| 3074 | * to a missing object or permission. These events should be tracked |
| 3075 | * because if they happen often, it may indicate a brute force or a |
| 3076 | * vulnerability scan. Normally this is done when receiving the response |
| 3077 | * but here we're tracking after this ought to have been done so we have |
| 3078 | * to do it on purpose. |
| 3079 | */ |
Willy Tarreau | 3146a4c | 2016-07-26 15:22:33 +0200 | [diff] [blame] | 3080 | if ((unsigned)(txn->status - 400) < 100) { |
| 3081 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT); |
| 3082 | if (ptr) |
| 3083 | stktable_data_cast(ptr, http_err_cnt)++; |
| 3084 | |
| 3085 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE); |
| 3086 | if (ptr) |
| 3087 | update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate), |
| 3088 | t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1); |
| 3089 | } |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 3090 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 3091 | HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock); |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 3092 | |
Emeric Brun | 0fed0b0 | 2017-11-29 16:15:07 +0100 | [diff] [blame] | 3093 | /* If data was modified, we need to touch to re-schedule sync */ |
| 3094 | stktable_touch_local(t, ts, 0); |
| 3095 | |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 3096 | stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT); |
| 3097 | if (sess->fe != s->be) |
| 3098 | stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND); |
| 3099 | |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 3100 | } |
| 3101 | } |
| 3102 | break; |
| 3103 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 3104 | case ACT_CUSTOM: |
Willy Tarreau | 7aa15b0 | 2017-12-20 16:56:50 +0100 | [diff] [blame] | 3105 | if ((s->req.flags & CF_READ_ERROR) || |
| 3106 | ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) && |
| 3107 | !(s->si[0].flags & SI_FL_CLEAN_ABRT) && |
| 3108 | (px->options & PR_O_ABRT_CLOSE))) |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 3109 | act_flags |= ACT_FLAG_FINAL; |
Willy Tarreau | 3945868 | 2015-09-27 10:33:15 +0200 | [diff] [blame] | 3110 | |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 3111 | switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) { |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 3112 | case ACT_RET_ERR: |
| 3113 | case ACT_RET_CONT: |
| 3114 | break; |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 3115 | case ACT_RET_STOP: |
| 3116 | return HTTP_RULE_RES_STOP; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 3117 | case ACT_RET_YIELD: |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 3118 | s->current_rule = rule; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3119 | return HTTP_RULE_RES_YIELD; |
| 3120 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 3121 | break; |
| 3122 | |
Thierry FOURNIER | 22e4901 | 2015-08-05 19:13:48 +0200 | [diff] [blame] | 3123 | /* other flags exists, but normaly, they never be matched. */ |
| 3124 | default: |
| 3125 | break; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3126 | } |
| 3127 | } |
| 3128 | |
| 3129 | /* we reached the end of the rules, nothing to report */ |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 3130 | return HTTP_RULE_RES_CONT; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3131 | } |
| 3132 | |
| 3133 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3134 | /* Perform an HTTP redirect based on the information in <rule>. The function |
| 3135 | * returns non-zero on success, or zero in case of a, irrecoverable error such |
| 3136 | * as too large a request to build a valid response. |
| 3137 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3138 | 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] | 3139 | { |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 3140 | struct http_msg *req = &txn->req; |
| 3141 | struct http_msg *res = &txn->rsp; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3142 | const char *msg_fmt; |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3143 | struct chunk *chunk; |
| 3144 | int ret = 0; |
| 3145 | |
| 3146 | chunk = alloc_trash_chunk(); |
| 3147 | if (!chunk) |
| 3148 | goto leave; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3149 | |
| 3150 | /* build redirect message */ |
| 3151 | switch(rule->code) { |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 3152 | case 308: |
| 3153 | msg_fmt = HTTP_308; |
| 3154 | break; |
| 3155 | case 307: |
| 3156 | msg_fmt = HTTP_307; |
| 3157 | break; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3158 | case 303: |
| 3159 | msg_fmt = HTTP_303; |
| 3160 | break; |
| 3161 | case 301: |
| 3162 | msg_fmt = HTTP_301; |
| 3163 | break; |
| 3164 | case 302: |
| 3165 | default: |
| 3166 | msg_fmt = HTTP_302; |
| 3167 | break; |
| 3168 | } |
| 3169 | |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3170 | if (unlikely(!chunk_strcpy(chunk, msg_fmt))) |
| 3171 | goto leave; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3172 | |
| 3173 | switch(rule->type) { |
| 3174 | case REDIRECT_TYPE_SCHEME: { |
| 3175 | const char *path; |
| 3176 | const char *host; |
| 3177 | struct hdr_ctx ctx; |
| 3178 | int pathlen; |
| 3179 | int hostlen; |
| 3180 | |
| 3181 | host = ""; |
| 3182 | hostlen = 0; |
| 3183 | ctx.idx = 0; |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 3184 | 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] | 3185 | host = ctx.line + ctx.val; |
| 3186 | hostlen = ctx.vlen; |
| 3187 | } |
| 3188 | |
| 3189 | path = http_get_path(txn); |
| 3190 | /* build message using path */ |
| 3191 | if (path) { |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 3192 | 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] | 3193 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 3194 | int qs = 0; |
| 3195 | while (qs < pathlen) { |
| 3196 | if (path[qs] == '?') { |
| 3197 | pathlen = qs; |
| 3198 | break; |
| 3199 | } |
| 3200 | qs++; |
| 3201 | } |
| 3202 | } |
| 3203 | } else { |
| 3204 | path = "/"; |
| 3205 | pathlen = 1; |
| 3206 | } |
| 3207 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3208 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
| 3209 | /* check if we can add scheme + "://" + host + path */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3210 | if (chunk->len + rule->rdr_len + 3 + hostlen + pathlen > chunk->size - 4) |
| 3211 | goto leave; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3212 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3213 | /* add scheme */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3214 | memcpy(chunk->str + chunk->len, rule->rdr_str, rule->rdr_len); |
| 3215 | chunk->len += rule->rdr_len; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3216 | } |
| 3217 | else { |
| 3218 | /* add scheme with executing log format */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3219 | 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] | 3220 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3221 | /* check if we can add scheme + "://" + host + path */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3222 | if (chunk->len + 3 + hostlen + pathlen > chunk->size - 4) |
| 3223 | goto leave; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3224 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3225 | /* add "://" */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3226 | memcpy(chunk->str + chunk->len, "://", 3); |
| 3227 | chunk->len += 3; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3228 | |
| 3229 | /* add host */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3230 | memcpy(chunk->str + chunk->len, host, hostlen); |
| 3231 | chunk->len += hostlen; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3232 | |
| 3233 | /* add path */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3234 | memcpy(chunk->str + chunk->len, path, pathlen); |
| 3235 | chunk->len += pathlen; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3236 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3237 | /* 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] | 3238 | if (chunk->len && chunk->str[chunk->len - 1] != '/' && |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3239 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3240 | if (chunk->len > chunk->size - 5) |
| 3241 | goto leave; |
| 3242 | chunk->str[chunk->len] = '/'; |
| 3243 | chunk->len++; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3244 | } |
| 3245 | |
| 3246 | break; |
| 3247 | } |
| 3248 | case REDIRECT_TYPE_PREFIX: { |
| 3249 | const char *path; |
| 3250 | int pathlen; |
| 3251 | |
| 3252 | path = http_get_path(txn); |
| 3253 | /* build message using path */ |
| 3254 | if (path) { |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 3255 | 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] | 3256 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 3257 | int qs = 0; |
| 3258 | while (qs < pathlen) { |
| 3259 | if (path[qs] == '?') { |
| 3260 | pathlen = qs; |
| 3261 | break; |
| 3262 | } |
| 3263 | qs++; |
| 3264 | } |
| 3265 | } |
| 3266 | } else { |
| 3267 | path = "/"; |
| 3268 | pathlen = 1; |
| 3269 | } |
| 3270 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3271 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3272 | if (chunk->len + rule->rdr_len + pathlen > chunk->size - 4) |
| 3273 | goto leave; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3274 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3275 | /* add prefix. Note that if prefix == "/", we don't want to |
| 3276 | * add anything, otherwise it makes it hard for the user to |
| 3277 | * configure a self-redirection. |
| 3278 | */ |
| 3279 | if (rule->rdr_len != 1 || *rule->rdr_str != '/') { |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3280 | memcpy(chunk->str + chunk->len, rule->rdr_str, rule->rdr_len); |
| 3281 | chunk->len += rule->rdr_len; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3282 | } |
| 3283 | } |
| 3284 | else { |
| 3285 | /* add prefix with executing log format */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3286 | 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] | 3287 | |
| 3288 | /* Check length */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3289 | if (chunk->len + pathlen > chunk->size - 4) |
| 3290 | goto leave; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3291 | } |
| 3292 | |
| 3293 | /* add path */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3294 | memcpy(chunk->str + chunk->len, path, pathlen); |
| 3295 | chunk->len += pathlen; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3296 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3297 | /* 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] | 3298 | if (chunk->len && chunk->str[chunk->len - 1] != '/' && |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3299 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3300 | if (chunk->len > chunk->size - 5) |
| 3301 | goto leave; |
| 3302 | chunk->str[chunk->len] = '/'; |
| 3303 | chunk->len++; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3304 | } |
| 3305 | |
| 3306 | break; |
| 3307 | } |
| 3308 | case REDIRECT_TYPE_LOCATION: |
| 3309 | default: |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3310 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3311 | if (chunk->len + rule->rdr_len > chunk->size - 4) |
| 3312 | goto leave; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3313 | |
| 3314 | /* add location */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3315 | memcpy(chunk->str + chunk->len, rule->rdr_str, rule->rdr_len); |
| 3316 | chunk->len += rule->rdr_len; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3317 | } |
| 3318 | else { |
| 3319 | /* add location with executing log format */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3320 | 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] | 3321 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3322 | /* Check left length */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3323 | if (chunk->len > chunk->size - 4) |
| 3324 | goto leave; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3325 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3326 | break; |
| 3327 | } |
| 3328 | |
| 3329 | if (rule->cookie_len) { |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3330 | memcpy(chunk->str + chunk->len, "\r\nSet-Cookie: ", 14); |
| 3331 | chunk->len += 14; |
| 3332 | memcpy(chunk->str + chunk->len, rule->cookie_str, rule->cookie_len); |
| 3333 | chunk->len += rule->cookie_len; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3334 | } |
| 3335 | |
Willy Tarreau | 19b1412 | 2017-02-28 09:48:11 +0100 | [diff] [blame] | 3336 | /* add end of headers and the keep-alive/close status. */ |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3337 | txn->status = rule->code; |
| 3338 | /* let's log the request time */ |
| 3339 | s->logs.tv_request = now; |
| 3340 | |
Christopher Faulet | be821b9 | 2017-03-30 11:21:53 +0200 | [diff] [blame] | 3341 | 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] | 3342 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL || |
| 3343 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) { |
| 3344 | /* keep-alive possible */ |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 3345 | if (!(req->flags & HTTP_MSGF_VER_11)) { |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3346 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3347 | memcpy(chunk->str + chunk->len, "\r\nProxy-Connection: keep-alive", 30); |
| 3348 | chunk->len += 30; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3349 | } else { |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3350 | memcpy(chunk->str + chunk->len, "\r\nConnection: keep-alive", 24); |
| 3351 | chunk->len += 24; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3352 | } |
| 3353 | } |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3354 | memcpy(chunk->str + chunk->len, "\r\n\r\n", 4); |
| 3355 | chunk->len += 4; |
| 3356 | FLT_STRM_CB(s, flt_http_reply(s, txn->status, chunk)); |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3357 | co_inject(res->chn, chunk->str, chunk->len); |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3358 | /* "eat" the request */ |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 3359 | bi_fast_delete(req->chn->buf, req->sov); |
| 3360 | req->next -= req->sov; |
| 3361 | req->sov = 0; |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 3362 | 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] | 3363 | 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] | 3364 | req->msg_state = HTTP_MSG_CLOSED; |
| 3365 | res->msg_state = HTTP_MSG_DONE; |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 3366 | /* Trim any possible response */ |
| 3367 | res->chn->buf->i = 0; |
| 3368 | res->next = res->sov = 0; |
Christopher Faulet | 5d468ca | 2017-09-11 09:27:29 +0200 | [diff] [blame] | 3369 | /* let the server side turn to SI_ST_CLO */ |
| 3370 | channel_shutw_now(req->chn); |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3371 | } else { |
| 3372 | /* keep-alive not possible */ |
| 3373 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3374 | memcpy(chunk->str + chunk->len, "\r\nProxy-Connection: close\r\n\r\n", 29); |
| 3375 | chunk->len += 29; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3376 | } else { |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3377 | memcpy(chunk->str + chunk->len, "\r\nConnection: close\r\n\r\n", 23); |
| 3378 | chunk->len += 23; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3379 | } |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3380 | http_reply_and_close(s, txn->status, chunk); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 3381 | req->chn->analysers &= AN_REQ_FLT_END; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3382 | } |
| 3383 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3384 | if (!(s->flags & SF_ERR_MASK)) |
| 3385 | s->flags |= SF_ERR_LOCAL; |
| 3386 | if (!(s->flags & SF_FINST_MASK)) |
| 3387 | s->flags |= SF_FINST_R; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3388 | |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3389 | ret = 1; |
| 3390 | leave: |
| 3391 | free_trash_chunk(chunk); |
| 3392 | return ret; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3393 | } |
| 3394 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3395 | /* This stream analyser runs all HTTP request processing which is common to |
| 3396 | * frontends and backends, which means blocking ACLs, filters, connection-close, |
| 3397 | * reqadd, stats and redirects. This is performed for the designated proxy. |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3398 | * 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] | 3399 | * either needs more data or wants to immediately abort the request (eg: deny, |
| 3400 | * error, ...). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3401 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3402 | 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] | 3403 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 3404 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3405 | struct http_txn *txn = s->txn; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3406 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3407 | struct redirect_rule *rule; |
Willy Tarreau | f4f0412 | 2010-01-28 18:10:50 +0100 | [diff] [blame] | 3408 | struct cond_wordlist *wl; |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3409 | enum rule_result verdict; |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 3410 | int deny_status = HTTP_ERR_403; |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 3411 | struct connection *conn = objt_conn(sess->origin); |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3412 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 3413 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3414 | /* we need more data */ |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3415 | goto return_prx_yield; |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3416 | } |
| 3417 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3418 | 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] | 3419 | now_ms, __FUNCTION__, |
| 3420 | s, |
| 3421 | req, |
| 3422 | req->rex, req->wex, |
| 3423 | req->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3424 | req->buf->i, |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3425 | req->analysers); |
| 3426 | |
Willy Tarreau | 6541083 | 2014-04-28 21:25:43 +0200 | [diff] [blame] | 3427 | /* just in case we have some per-backend tracking */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3428 | stream_inc_be_http_req_ctr(s); |
Willy Tarreau | 6541083 | 2014-04-28 21:25:43 +0200 | [diff] [blame] | 3429 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3430 | /* evaluate http-request rules */ |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3431 | if (!LIST_ISEMPTY(&px->http_req_rules)) { |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 3432 | 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] | 3433 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3434 | switch (verdict) { |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3435 | case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */ |
| 3436 | goto return_prx_yield; |
| 3437 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3438 | case HTTP_RULE_RES_CONT: |
| 3439 | case HTTP_RULE_RES_STOP: /* nothing to do */ |
| 3440 | break; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 3441 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3442 | case HTTP_RULE_RES_DENY: /* deny or tarpit */ |
| 3443 | if (txn->flags & TX_CLTARPIT) |
| 3444 | goto tarpit; |
| 3445 | goto deny; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 3446 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3447 | case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */ |
| 3448 | goto return_prx_cond; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 3449 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3450 | case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */ |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 3451 | goto done; |
| 3452 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3453 | case HTTP_RULE_RES_BADREQ: /* failed with a bad request */ |
| 3454 | goto return_bad_req; |
| 3455 | } |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3456 | } |
| 3457 | |
Olivier Houchard | 25ae45a | 2017-11-29 19:51:19 +0100 | [diff] [blame] | 3458 | if (conn && (conn->flags & CO_FL_EARLY_DATA) && |
| 3459 | (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE))) { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 3460 | struct hdr_ctx ctx; |
| 3461 | |
| 3462 | ctx.idx = 0; |
| 3463 | if (!http_find_header2("Early-Data", strlen("Early-Data"), |
| 3464 | s->req.buf->p, &txn->hdr_idx, &ctx)) { |
| 3465 | if (unlikely(http_header_add_tail2(&txn->req, |
| 3466 | &txn->hdr_idx, "Early-Data: 1", |
| 3467 | strlen("Early-Data: 1"))) < 0) { |
| 3468 | goto return_bad_req; |
| 3469 | } |
| 3470 | } |
| 3471 | |
| 3472 | } |
| 3473 | |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 3474 | /* OK at this stage, we know that the request was accepted according to |
| 3475 | * the http-request rules, we can check for the stats. Note that the |
| 3476 | * URI is detected *before* the req* rules in order not to be affected |
| 3477 | * by a possible reqrep, while they are processed *after* so that a |
| 3478 | * reqdeny can still block them. This clearly needs to change in 1.6! |
| 3479 | */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 3480 | if (stats_check_uri(&s->si[1], txn, px)) { |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 3481 | s->target = &http_stats_applet.obj_type; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 3482 | 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] | 3483 | txn->status = 500; |
| 3484 | s->logs.tv_request = now; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 3485 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3486 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3487 | if (!(s->flags & SF_ERR_MASK)) |
| 3488 | s->flags |= SF_ERR_RESOURCE; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 3489 | goto return_prx_cond; |
| 3490 | } |
| 3491 | |
| 3492 | /* parse the whole stats request and extract the relevant information */ |
| 3493 | http_handle_stats(s, req); |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 3494 | 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] | 3495 | /* not all actions implemented: deny, allow, auth */ |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3496 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3497 | if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */ |
| 3498 | goto deny; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 3499 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3500 | if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */ |
| 3501 | goto return_prx_cond; |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 3502 | } |
| 3503 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3504 | /* evaluate the req* rules except reqadd */ |
| 3505 | if (px->req_exp != NULL) { |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 3506 | if (apply_filters_to_request(s, req, px) < 0) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3507 | goto return_bad_req; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3508 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3509 | if (txn->flags & TX_CLDENY) |
| 3510 | goto deny; |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 3511 | |
Jarno Huuskonen | 800d176 | 2017-03-06 14:56:36 +0200 | [diff] [blame] | 3512 | if (txn->flags & TX_CLTARPIT) { |
| 3513 | deny_status = HTTP_ERR_500; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3514 | goto tarpit; |
Jarno Huuskonen | 800d176 | 2017-03-06 14:56:36 +0200 | [diff] [blame] | 3515 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3516 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3517 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3518 | /* add request headers from the rule sets in the same order */ |
| 3519 | list_for_each_entry(wl, &px->req_add, list) { |
| 3520 | if (wl->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 3521 | 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] | 3522 | ret = acl_pass(ret); |
| 3523 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 3524 | ret = !ret; |
| 3525 | if (!ret) |
| 3526 | continue; |
| 3527 | } |
| 3528 | |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 3529 | 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] | 3530 | goto return_bad_req; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 3531 | } |
| 3532 | |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 3533 | |
| 3534 | /* Proceed with the stats now. */ |
William Lallemand | 71bd11a | 2017-11-20 19:13:14 +0100 | [diff] [blame] | 3535 | if (unlikely(objt_applet(s->target) == &http_stats_applet) || |
| 3536 | unlikely(objt_applet(s->target) == &http_cache_applet)) { |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3537 | /* process the stats request now */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3538 | 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] | 3539 | HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1); |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 3540 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3541 | if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is |
| 3542 | s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy |
| 3543 | if (!(s->flags & SF_FINST_MASK)) |
| 3544 | s->flags |= SF_FINST_R; |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 3545 | |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 3546 | /* 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] | 3547 | req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END); |
| 3548 | req->analysers &= ~AN_REQ_FLT_XFER_DATA; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 3549 | req->analysers |= AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3550 | goto done; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3551 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 3552 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3553 | /* check whether we have some ACLs set to redirect this request */ |
| 3554 | list_for_each_entry(rule, &px->redirect_rules, list) { |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 3555 | if (rule->cond) { |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3556 | int ret; |
| 3557 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 3558 | 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] | 3559 | ret = acl_pass(ret); |
| 3560 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 3561 | ret = !ret; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3562 | if (!ret) |
| 3563 | continue; |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 3564 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3565 | if (!http_apply_redirect_rule(rule, s, txn)) |
| 3566 | goto return_bad_req; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3567 | goto done; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3568 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 3569 | |
Willy Tarreau | 2be3939 | 2010-01-03 17:24:51 +0100 | [diff] [blame] | 3570 | /* POST requests may be accompanied with an "Expect: 100-Continue" header. |
| 3571 | * If this happens, then the data will not come immediately, so we must |
| 3572 | * send all what we have without waiting. Note that due to the small gain |
| 3573 | * 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] | 3574 | * 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] | 3575 | * itself once used. |
| 3576 | */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 3577 | req->flags |= CF_SEND_DONTWAIT; |
Willy Tarreau | 2be3939 | 2010-01-03 17:24:51 +0100 | [diff] [blame] | 3578 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3579 | done: /* done with this analyser, continue with next ones that the calling |
| 3580 | * points will have set, if any. |
| 3581 | */ |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3582 | req->analyse_exp = TICK_ETERNITY; |
Thierry FOURNIER | 7566e30 | 2014-08-22 06:55:26 +0200 | [diff] [blame] | 3583 | done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */ |
| 3584 | req->analysers &= ~an_bit; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3585 | return 1; |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 3586 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3587 | tarpit: |
Willy Tarreau | 6a0bca9 | 2017-06-11 17:56:27 +0200 | [diff] [blame] | 3588 | /* Allow cookie logging |
| 3589 | */ |
| 3590 | if (s->be->cookie_name || sess->fe->capture_name) |
| 3591 | manage_client_side_cookies(s, req); |
| 3592 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3593 | /* When a connection is tarpitted, we use the tarpit timeout, |
| 3594 | * which may be the same as the connect timeout if unspecified. |
| 3595 | * If unset, then set it to zero because we really want it to |
| 3596 | * eventually expire. We build the tarpit as an analyser. |
| 3597 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 3598 | channel_erase(&s->req); |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3599 | |
| 3600 | /* wipe the request out so that we can drop the connection early |
| 3601 | * if the client closes first. |
| 3602 | */ |
| 3603 | channel_dont_connect(req); |
Bertrand Paquet | 83a2c3d | 2016-04-06 11:58:31 +0200 | [diff] [blame] | 3604 | |
Jarno Huuskonen | 800d176 | 2017-03-06 14:56:36 +0200 | [diff] [blame] | 3605 | txn->status = http_err_codes[deny_status]; |
| 3606 | |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 3607 | req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */ |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3608 | req->analysers |= AN_REQ_HTTP_TARPIT; |
| 3609 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit); |
| 3610 | if (!req->analyse_exp) |
| 3611 | req->analyse_exp = tick_add(now_ms, 0); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3612 | stream_inc_http_err_ctr(s); |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 3613 | HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3614 | if (sess->fe != s->be) |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 3615 | HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 3616 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 3617 | HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1); |
Thierry FOURNIER | 7566e30 | 2014-08-22 06:55:26 +0200 | [diff] [blame] | 3618 | goto done_without_exp; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3619 | |
| 3620 | deny: /* this request was blocked (denied) */ |
Bertrand Paquet | 83a2c3d | 2016-04-06 11:58:31 +0200 | [diff] [blame] | 3621 | |
| 3622 | /* Allow cookie logging |
| 3623 | */ |
| 3624 | if (s->be->cookie_name || sess->fe->capture_name) |
| 3625 | manage_client_side_cookies(s, req); |
| 3626 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3627 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 3628 | txn->status = http_err_codes[deny_status]; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3629 | s->logs.tv_request = now; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 3630 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3631 | stream_inc_http_err_ctr(s); |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 3632 | HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3633 | if (sess->fe != s->be) |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 3634 | HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 3635 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 3636 | HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1); |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3637 | goto return_prx_cond; |
| 3638 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3639 | return_bad_req: |
| 3640 | /* We centralize bad requests processing here */ |
| 3641 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 3642 | /* we detected a parsing error. We want to archive this request |
| 3643 | * in the dedicated proxy area for later troubleshooting. |
| 3644 | */ |
Emeric Brun | 8c1aaa2 | 2017-06-15 11:30:06 +0200 | [diff] [blame] | 3645 | 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] | 3646 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 3647 | |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 3648 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3649 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3650 | txn->status = 400; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 3651 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3652 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 3653 | HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 3654 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 3655 | HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1); |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 3656 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3657 | return_prx_cond: |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3658 | if (!(s->flags & SF_ERR_MASK)) |
| 3659 | s->flags |= SF_ERR_PRXCOND; |
| 3660 | if (!(s->flags & SF_FINST_MASK)) |
| 3661 | s->flags |= SF_FINST_R; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 3662 | |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 3663 | req->analysers &= AN_REQ_FLT_END; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3664 | req->analyse_exp = TICK_ETERNITY; |
| 3665 | return 0; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3666 | |
| 3667 | return_prx_yield: |
| 3668 | channel_dont_connect(req); |
| 3669 | return 0; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3670 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3671 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3672 | /* This function performs all the processing enabled for the current request. |
| 3673 | * It returns 1 if the processing can continue on next analysers, or zero if it |
| 3674 | * needs more data, encounters an error, or wants to immediately abort the |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 3675 | * request. It relies on buffers flags, and updates s->req.analysers. |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3676 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3677 | 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] | 3678 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 3679 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3680 | struct http_txn *txn = s->txn; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3681 | struct http_msg *msg = &txn->req; |
Willy Tarreau | ee335e6 | 2015-04-21 18:15:13 +0200 | [diff] [blame] | 3682 | struct connection *cli_conn = objt_conn(strm_sess(s)->origin); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3683 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 3684 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3685 | /* we need more data */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 3686 | channel_dont_connect(req); |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3687 | return 0; |
| 3688 | } |
| 3689 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3690 | 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] | 3691 | now_ms, __FUNCTION__, |
| 3692 | s, |
| 3693 | req, |
| 3694 | req->rex, req->wex, |
| 3695 | req->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3696 | req->buf->i, |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3697 | req->analysers); |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3698 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3699 | /* |
| 3700 | * Right now, we know that we have processed the entire headers |
| 3701 | * and that unwanted requests have been filtered out. We can do |
| 3702 | * whatever we want with the remaining request. Also, now we |
| 3703 | * may have separate values for ->fe, ->be. |
| 3704 | */ |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3705 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3706 | /* |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3707 | * If HTTP PROXY is set we simply get remote server address parsing |
| 3708 | * incoming request. Note that this requires that a connection is |
| 3709 | * allocated on the server side. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3710 | */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3711 | 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] | 3712 | struct connection *conn; |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 3713 | char *path; |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 3714 | |
Willy Tarreau | 9471b8c | 2013-12-15 13:31:35 +0100 | [diff] [blame] | 3715 | /* Note that for now we don't reuse existing proxy connections */ |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 3716 | 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] | 3717 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 3718 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3719 | txn->status = 500; |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 3720 | req->analysers &= AN_REQ_FLT_END; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 3721 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 3722 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3723 | if (!(s->flags & SF_ERR_MASK)) |
| 3724 | s->flags |= SF_ERR_RESOURCE; |
| 3725 | if (!(s->flags & SF_FINST_MASK)) |
| 3726 | s->flags |= SF_FINST_R; |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 3727 | |
| 3728 | return 0; |
| 3729 | } |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 3730 | |
| 3731 | path = http_get_path(txn); |
Christopher Faulet | 11ebb20 | 2018-04-13 15:53:12 +0200 | [diff] [blame] | 3732 | if (url2sa(req->buf->p + msg->sl.rq.u, |
| 3733 | path ? path - (req->buf->p + msg->sl.rq.u) : msg->sl.rq.u_l, |
| 3734 | &conn->addr.to, NULL) == -1) |
| 3735 | goto return_bad_req; |
| 3736 | |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 3737 | /* if the path was found, we have to remove everything between |
| 3738 | * req->buf->p + msg->sl.rq.u and path (excluded). If it was not |
| 3739 | * found, we need to replace from req->buf->p + msg->sl.rq.u for |
| 3740 | * u_l characters by a single "/". |
| 3741 | */ |
| 3742 | if (path) { |
| 3743 | char *cur_ptr = req->buf->p; |
| 3744 | char *cur_end = cur_ptr + txn->req.sl.rq.l; |
| 3745 | int delta; |
| 3746 | |
| 3747 | delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0); |
| 3748 | http_msg_move_end(&txn->req, delta); |
| 3749 | cur_end += delta; |
| 3750 | if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL) |
| 3751 | goto return_bad_req; |
| 3752 | } |
| 3753 | else { |
| 3754 | char *cur_ptr = req->buf->p; |
| 3755 | char *cur_end = cur_ptr + txn->req.sl.rq.l; |
| 3756 | int delta; |
| 3757 | |
| 3758 | delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, |
| 3759 | req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1); |
| 3760 | http_msg_move_end(&txn->req, delta); |
| 3761 | cur_end += delta; |
| 3762 | if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL) |
| 3763 | goto return_bad_req; |
| 3764 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3765 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3766 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3767 | /* |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 3768 | * 7: Now we can work with the cookies. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3769 | * Note that doing so might move headers in the request, but |
| 3770 | * the fields will stay coherent and the URI will not move. |
| 3771 | * This should only be performed in the backend. |
| 3772 | */ |
Bertrand Paquet | 83a2c3d | 2016-04-06 11:58:31 +0200 | [diff] [blame] | 3773 | if (s->be->cookie_name || sess->fe->capture_name) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3774 | manage_client_side_cookies(s, req); |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 3775 | |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 3776 | /* add unique-id if "header-unique-id" is specified */ |
| 3777 | |
Thierry Fournier | f4011dd | 2016-03-29 17:23:51 +0200 | [diff] [blame] | 3778 | if (!LIST_ISEMPTY(&sess->fe->format_unique_id) && !s->unique_id) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 3779 | if ((s->unique_id = pool_alloc(pool_head_uniqueid)) == NULL) |
William Lallemand | 5b7ea3a | 2013-08-28 15:44:19 +0200 | [diff] [blame] | 3780 | goto return_bad_req; |
| 3781 | s->unique_id[0] = '\0'; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3782 | 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] | 3783 | } |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 3784 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3785 | if (sess->fe->header_unique_id && s->unique_id) { |
| 3786 | 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] | 3787 | if (trash.len < 0) |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 3788 | goto return_bad_req; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3789 | 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] | 3790 | goto return_bad_req; |
| 3791 | } |
| 3792 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 3793 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3794 | * 9: add X-Forwarded-For if either the frontend or the backend |
| 3795 | * asks for it. |
| 3796 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3797 | if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) { |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 3798 | struct hdr_ctx ctx = { .idx = 0 }; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3799 | if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) && |
| 3800 | http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name, |
| 3801 | 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] | 3802 | req->buf->p, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 3803 | /* The header is set to be added only if none is present |
| 3804 | * and we found it, so don't do anything. |
| 3805 | */ |
| 3806 | } |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3807 | else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3808 | /* Add an X-Forwarded-For header unless the source IP is |
| 3809 | * in the 'except' network range. |
| 3810 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3811 | if ((!sess->fe->except_mask.s_addr || |
| 3812 | (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr) |
| 3813 | != sess->fe->except_net.s_addr) && |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3814 | (!s->be->except_mask.s_addr || |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3815 | (((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] | 3816 | != s->be->except_net.s_addr)) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 3817 | int len; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3818 | unsigned char *pn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3819 | pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr; |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 3820 | |
| 3821 | /* Note: we rely on the backend to get the header name to be used for |
| 3822 | * x-forwarded-for, because the header is really meant for the backends. |
| 3823 | * However, if the backend did not specify any option, we have to rely |
| 3824 | * on the frontend's header name. |
| 3825 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3826 | if (s->be->fwdfor_hdr_len) { |
| 3827 | len = s->be->fwdfor_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3828 | memcpy(trash.str, s->be->fwdfor_hdr_name, len); |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 3829 | } else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3830 | len = sess->fe->fwdfor_hdr_len; |
| 3831 | memcpy(trash.str, sess->fe->fwdfor_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 3832 | } |
Willy Tarreau | e9187f8 | 2014-04-14 15:27:14 +0200 | [diff] [blame] | 3833 | 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] | 3834 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3835 | 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] | 3836 | goto return_bad_req; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 3837 | } |
| 3838 | } |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3839 | else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3840 | /* FIXME: for the sake of completeness, we should also support |
| 3841 | * 'except' here, although it is mostly useless in this case. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3842 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3843 | int len; |
| 3844 | char pn[INET6_ADDRSTRLEN]; |
| 3845 | inet_ntop(AF_INET6, |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3846 | (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3847 | pn, sizeof(pn)); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3848 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3849 | /* Note: we rely on the backend to get the header name to be used for |
| 3850 | * x-forwarded-for, because the header is really meant for the backends. |
| 3851 | * However, if the backend did not specify any option, we have to rely |
| 3852 | * on the frontend's header name. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3853 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3854 | if (s->be->fwdfor_hdr_len) { |
| 3855 | len = s->be->fwdfor_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3856 | memcpy(trash.str, s->be->fwdfor_hdr_name, len); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3857 | } else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3858 | len = sess->fe->fwdfor_hdr_len; |
| 3859 | memcpy(trash.str, sess->fe->fwdfor_hdr_name, len); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3860 | } |
Willy Tarreau | e9187f8 | 2014-04-14 15:27:14 +0200 | [diff] [blame] | 3861 | len += snprintf(trash.str + len, trash.size - len, ": %s", pn); |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3862 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3863 | 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] | 3864 | goto return_bad_req; |
| 3865 | } |
| 3866 | } |
| 3867 | |
| 3868 | /* |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3869 | * 10: add X-Original-To if either the frontend or the backend |
| 3870 | * asks for it. |
| 3871 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3872 | if ((sess->fe->options | s->be->options) & PR_O_ORGTO) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3873 | |
| 3874 | /* FIXME: don't know if IPv6 can handle that case too. */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3875 | if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3876 | /* Add an X-Original-To header unless the destination IP is |
| 3877 | * in the 'except' network range. |
| 3878 | */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3879 | conn_get_to_addr(cli_conn); |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3880 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3881 | if (cli_conn->addr.to.ss_family == AF_INET && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3882 | ((!sess->fe->except_mask_to.s_addr || |
| 3883 | (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr) |
| 3884 | != sess->fe->except_to.s_addr) && |
Emeric Brun | 5bd86a8 | 2010-10-22 17:23:04 +0200 | [diff] [blame] | 3885 | (!s->be->except_mask_to.s_addr || |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3886 | (((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] | 3887 | != s->be->except_to.s_addr))) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3888 | int len; |
| 3889 | unsigned char *pn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3890 | pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr; |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3891 | |
| 3892 | /* Note: we rely on the backend to get the header name to be used for |
| 3893 | * x-original-to, because the header is really meant for the backends. |
| 3894 | * However, if the backend did not specify any option, we have to rely |
| 3895 | * on the frontend's header name. |
| 3896 | */ |
| 3897 | if (s->be->orgto_hdr_len) { |
| 3898 | len = s->be->orgto_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3899 | memcpy(trash.str, s->be->orgto_hdr_name, len); |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3900 | } else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3901 | len = sess->fe->orgto_hdr_len; |
| 3902 | memcpy(trash.str, sess->fe->orgto_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 3903 | } |
Willy Tarreau | e9187f8 | 2014-04-14 15:27:14 +0200 | [diff] [blame] | 3904 | 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] | 3905 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3906 | 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] | 3907 | goto return_bad_req; |
| 3908 | } |
| 3909 | } |
| 3910 | } |
| 3911 | |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 3912 | /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set. |
| 3913 | * If an "Upgrade" token is found, the header is left untouched in order not to have |
| 3914 | * to deal with some servers bugs : some of them fail an Upgrade if anything but |
| 3915 | * "Upgrade" is present in the Connection header. |
| 3916 | */ |
| 3917 | if (!(txn->flags & TX_HDR_CONN_UPG) && |
| 3918 | (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3919 | ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 3920 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) { |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3921 | unsigned int want_flags = 0; |
| 3922 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3923 | if (msg->flags & HTTP_MSGF_VER_11) { |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 3924 | if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3925 | ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 3926 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3927 | !((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3928 | want_flags |= TX_CON_CLO_SET; |
| 3929 | } else { |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 3930 | if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3931 | ((sess->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL && |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 3932 | (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3933 | ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3934 | want_flags |= TX_CON_KAL_SET; |
| 3935 | } |
| 3936 | |
| 3937 | 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] | 3938 | http_change_connection_header(txn, msg, want_flags); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3939 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3940 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3941 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3942 | /* If we have no server assigned yet and we're balancing on url_param |
| 3943 | * with a POST request, we may be interested in checking the body for |
| 3944 | * that parameter. This will be done in another analyser. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3945 | */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3946 | if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) && |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3947 | s->txn->meth == HTTP_METH_POST && s->be->url_param_name != NULL && |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3948 | (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) { |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 3949 | channel_dont_connect(req); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3950 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3951 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3952 | |
Christopher Faulet | be821b9 | 2017-03-30 11:21:53 +0200 | [diff] [blame] | 3953 | req->analysers &= ~AN_REQ_FLT_XFER_DATA; |
| 3954 | req->analysers |= AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 3955 | #ifdef TCP_QUICKACK |
Christopher Faulet | be821b9 | 2017-03-30 11:21:53 +0200 | [diff] [blame] | 3956 | /* We expect some data from the client. Unless we know for sure |
| 3957 | * we already have a full request, we have to re-enable quick-ack |
| 3958 | * in case we previously disabled it, otherwise we might cause |
| 3959 | * the client to delay further data. |
| 3960 | */ |
| 3961 | if ((sess->listener->options & LI_O_NOQUICKACK) && |
| 3962 | cli_conn && conn_ctrl_ready(cli_conn) && |
| 3963 | ((msg->flags & HTTP_MSGF_TE_CHNK) || |
| 3964 | (msg->body_len > req->buf->i - txn->req.eoh - 2))) |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 3965 | setsockopt(cli_conn->handle.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one)); |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 3966 | #endif |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 3967 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3968 | /************************************************************* |
| 3969 | * OK, that's finished for the headers. We have done what we * |
| 3970 | * could. Let's switch to the DATA state. * |
| 3971 | ************************************************************/ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3972 | req->analyse_exp = TICK_ETERNITY; |
| 3973 | req->analysers &= ~an_bit; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3974 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3975 | s->logs.tv_request = now; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3976 | /* OK let's go on with the BODY now */ |
| 3977 | return 1; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3978 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3979 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 3980 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
Willy Tarreau | f073a83 | 2009-03-01 23:21:47 +0100 | [diff] [blame] | 3981 | /* we detected a parsing error. We want to archive this request |
| 3982 | * in the dedicated proxy area for later troubleshooting. |
| 3983 | */ |
Emeric Brun | 8c1aaa2 | 2017-06-15 11:30:06 +0200 | [diff] [blame] | 3984 | 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] | 3985 | } |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 3986 | |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 3987 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3988 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3989 | txn->status = 400; |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 3990 | req->analysers &= AN_REQ_FLT_END; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 3991 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3992 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 3993 | HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 3994 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 3995 | HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1); |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3996 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3997 | if (!(s->flags & SF_ERR_MASK)) |
| 3998 | s->flags |= SF_ERR_PRXCOND; |
| 3999 | if (!(s->flags & SF_FINST_MASK)) |
| 4000 | s->flags |= SF_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 4001 | return 0; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 4002 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 4003 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4004 | /* This function is an analyser which processes the HTTP tarpit. It always |
| 4005 | * returns zero, at the beginning because it prevents any other processing |
| 4006 | * from occurring, and at the end because it terminates the request. |
| 4007 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4008 | 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] | 4009 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4010 | struct http_txn *txn = s->txn; |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4011 | |
| 4012 | /* This connection is being tarpitted. The CLIENT side has |
| 4013 | * already set the connect expiration date to the right |
| 4014 | * timeout. We just have to check that the client is still |
| 4015 | * there and that the timeout has not expired. |
| 4016 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4017 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4018 | if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 && |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4019 | !tick_is_expired(req->analyse_exp, now_ms)) |
| 4020 | return 0; |
| 4021 | |
| 4022 | /* We will set the queue timer to the time spent, just for |
| 4023 | * logging purposes. We fake a 500 server error, so that the |
| 4024 | * attacker will not suspect his connection has been tarpitted. |
| 4025 | * It will not cause trouble to the logs because we can exclude |
| 4026 | * the tarpitted connections by filtering on the 'PT' status flags. |
| 4027 | */ |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4028 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 4029 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4030 | if (!(req->flags & CF_READ_ERROR)) |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 4031 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4032 | |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4033 | req->analysers &= AN_REQ_FLT_END; |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4034 | req->analyse_exp = TICK_ETERNITY; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4035 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4036 | if (!(s->flags & SF_ERR_MASK)) |
| 4037 | s->flags |= SF_ERR_PRXCOND; |
| 4038 | if (!(s->flags & SF_FINST_MASK)) |
| 4039 | s->flags |= SF_FINST_T; |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4040 | return 0; |
| 4041 | } |
| 4042 | |
Willy Tarreau | 5a8f947 | 2014-04-10 11:16:06 +0200 | [diff] [blame] | 4043 | /* This function is an analyser which waits for the HTTP request body. It waits |
| 4044 | * for either the buffer to be full, or the full advertised contents to have |
| 4045 | * reached the buffer. It must only be called after the standard HTTP request |
| 4046 | * processing has occurred, because it expects the request to be parsed and will |
| 4047 | * look for the Expect header. It may send a 100-Continue interim response. It |
| 4048 | * takes in input any state starting from HTTP_MSG_BODY and leaves with one of |
| 4049 | * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it |
| 4050 | * 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] | 4051 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4052 | 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] | 4053 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4054 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4055 | struct http_txn *txn = s->txn; |
| 4056 | struct http_msg *msg = &s->txn->req; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4057 | |
| 4058 | /* We have to parse the HTTP request body to find any required data. |
| 4059 | * "balance url_param check_post" should have been the only way to get |
| 4060 | * into this. We were brought here after HTTP header analysis, so all |
| 4061 | * related structures are ready. |
| 4062 | */ |
| 4063 | |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4064 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
| 4065 | /* This is the first call */ |
| 4066 | if (msg->msg_state < HTTP_MSG_BODY) |
| 4067 | goto missing_data; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4068 | |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4069 | if (msg->msg_state < HTTP_MSG_100_SENT) { |
| 4070 | /* If we have HTTP/1.1 and Expect: 100-continue, then we must |
| 4071 | * send an HTTP/1.1 100 Continue intermediate response. |
| 4072 | */ |
| 4073 | if (msg->flags & HTTP_MSGF_VER_11) { |
| 4074 | struct hdr_ctx ctx; |
| 4075 | ctx.idx = 0; |
| 4076 | /* Expect is allowed in 1.1, look for it */ |
| 4077 | if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) && |
| 4078 | 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] | 4079 | 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] | 4080 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4081 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4082 | } |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4083 | msg->msg_state = HTTP_MSG_100_SENT; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4084 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4085 | |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 4086 | /* 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] | 4087 | * req->buf->p still points to the beginning of the message. We |
| 4088 | * must save the body in msg->next because it survives buffer |
| 4089 | * re-alignments. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4090 | */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 4091 | msg->next = msg->sov; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4092 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4093 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4094 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 4095 | else |
| 4096 | msg->msg_state = HTTP_MSG_DATA; |
| 4097 | } |
| 4098 | |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4099 | if (!(msg->flags & HTTP_MSGF_TE_CHNK)) { |
| 4100 | /* 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] | 4101 | if (http_body_bytes(msg) < msg->body_len) |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4102 | goto missing_data; |
| 4103 | |
| 4104 | /* OK we have everything we need now */ |
| 4105 | goto http_end; |
| 4106 | } |
| 4107 | |
| 4108 | /* OK here we're parsing a chunked-encoded message */ |
| 4109 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4110 | if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 4111 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4112 | * 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] | 4113 | * TRAILERS state. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 4114 | */ |
Willy Tarreau | e56cdd3 | 2017-09-21 08:36:33 +0200 | [diff] [blame] | 4115 | unsigned int chunk; |
| 4116 | 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] | 4117 | |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 4118 | if (!ret) |
| 4119 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4120 | else if (ret < 0) { |
Willy Tarreau | e56cdd3 | 2017-09-21 08:36:33 +0200 | [diff] [blame] | 4121 | msg->err_pos = req->buf->i + ret; |
| 4122 | if (msg->err_pos < 0) |
| 4123 | msg->err_pos += req->buf->size; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4124 | stream_inc_http_err_ctr(s); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4125 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4126 | } |
Willy Tarreau | e56cdd3 | 2017-09-21 08:36:33 +0200 | [diff] [blame] | 4127 | |
| 4128 | msg->chunk_len = chunk; |
| 4129 | msg->body_len += chunk; |
| 4130 | |
| 4131 | msg->sol = ret; |
Christopher Faulet | 113f7de | 2015-12-14 14:52:13 +0100 | [diff] [blame] | 4132 | msg->next += ret; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 4133 | msg->msg_state = msg->chunk_len ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4134 | } |
| 4135 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4136 | /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state. |
Willy Tarreau | e115b49 | 2015-05-01 23:05:14 +0200 | [diff] [blame] | 4137 | * We have the first data byte is in msg->sov + msg->sol. We're waiting |
| 4138 | * for at least a whole chunk or the whole content length bytes after |
| 4139 | * msg->sov + msg->sol. |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4140 | */ |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4141 | if (msg->msg_state == HTTP_MSG_TRAILERS) |
| 4142 | goto http_end; |
| 4143 | |
Willy Tarreau | e115b49 | 2015-05-01 23:05:14 +0200 | [diff] [blame] | 4144 | 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] | 4145 | goto http_end; |
| 4146 | |
| 4147 | missing_data: |
Willy Tarreau | 31a1995 | 2014-04-10 11:50:37 +0200 | [diff] [blame] | 4148 | /* we get here if we need to wait for more data. If the buffer is full, |
| 4149 | * we have the maximum we can expect. |
| 4150 | */ |
| 4151 | if (buffer_full(req->buf, global.tune.maxrewrite)) |
| 4152 | goto http_end; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 4153 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4154 | 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] | 4155 | txn->status = 408; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 4156 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 4157 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4158 | if (!(s->flags & SF_ERR_MASK)) |
| 4159 | s->flags |= SF_ERR_CLITO; |
| 4160 | if (!(s->flags & SF_FINST_MASK)) |
| 4161 | s->flags |= SF_FINST_D; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4162 | goto return_err_msg; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4163 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4164 | |
| 4165 | /* we get here if we need to wait for more data */ |
Willy Tarreau | 31a1995 | 2014-04-10 11:50:37 +0200 | [diff] [blame] | 4166 | if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) { |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4167 | /* Not enough data. We'll re-use the http-request |
| 4168 | * timeout here. Ideally, we should set the timeout |
| 4169 | * relative to the accept() date. We just set the |
| 4170 | * request timeout once at the beginning of the |
| 4171 | * request. |
| 4172 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4173 | channel_dont_connect(req); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4174 | if (!tick_isset(req->analyse_exp)) |
Willy Tarreau | cd7afc0 | 2009-07-12 10:03:17 +0200 | [diff] [blame] | 4175 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4176 | return 0; |
| 4177 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4178 | |
| 4179 | http_end: |
| 4180 | /* The situation will not evolve, so let's give up on the analysis. */ |
| 4181 | s->logs.tv_request = now; /* update the request timer to reflect full request */ |
| 4182 | req->analysers &= ~an_bit; |
| 4183 | req->analyse_exp = TICK_ETERNITY; |
| 4184 | return 1; |
| 4185 | |
| 4186 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 4187 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4188 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4189 | txn->status = 400; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 4190 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4191 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4192 | if (!(s->flags & SF_ERR_MASK)) |
| 4193 | s->flags |= SF_ERR_PRXCOND; |
| 4194 | if (!(s->flags & SF_FINST_MASK)) |
| 4195 | s->flags |= SF_FINST_R; |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 4196 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4197 | return_err_msg: |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4198 | req->analysers &= AN_REQ_FLT_END; |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 4199 | HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4200 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 4201 | HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4202 | return 0; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4203 | } |
| 4204 | |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4205 | /* send a server's name with an outgoing request over an established connection. |
| 4206 | * Note: this function is designed to be called once the request has been scheduled |
| 4207 | * for being forwarded. This is the reason why it rewinds the buffer before |
| 4208 | * proceeding. |
| 4209 | */ |
Willy Tarreau | 45c0d98 | 2012-03-09 12:11:57 +0100 | [diff] [blame] | 4210 | 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] | 4211 | |
| 4212 | struct hdr_ctx ctx; |
| 4213 | |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4214 | char *hdr_name = be->server_id_hdr_name; |
| 4215 | int hdr_name_len = be->server_id_hdr_len; |
Willy Tarreau | 394db37 | 2012-10-12 22:40:39 +0200 | [diff] [blame] | 4216 | struct channel *chn = txn->req.chn; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4217 | char *hdr_val; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4218 | unsigned int old_o, old_i; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4219 | |
William Lallemand | d9e9066 | 2012-01-30 17:27:17 +0100 | [diff] [blame] | 4220 | ctx.idx = 0; |
| 4221 | |
Willy Tarreau | 211cdec | 2014-04-17 20:18:08 +0200 | [diff] [blame] | 4222 | old_o = http_hdr_rewind(&txn->req); |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4223 | if (old_o) { |
| 4224 | /* The request was already skipped, let's restore it */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4225 | b_rew(chn->buf, old_o); |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 4226 | txn->req.next += old_o; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 4227 | txn->req.sov += old_o; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4228 | } |
| 4229 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4230 | old_i = chn->buf->i; |
| 4231 | 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] | 4232 | /* remove any existing values from the header */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 4233 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4234 | } |
| 4235 | |
| 4236 | /* Add the new header requested with the server value */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4237 | hdr_val = trash.str; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4238 | memcpy(hdr_val, hdr_name, hdr_name_len); |
| 4239 | hdr_val += hdr_name_len; |
| 4240 | *hdr_val++ = ':'; |
| 4241 | *hdr_val++ = ' '; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4242 | hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val); |
| 4243 | 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] | 4244 | |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4245 | if (old_o) { |
| 4246 | /* If this was a forwarded request, we must readjust the amount of |
| 4247 | * data to be forwarded in order to take into account the size |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 4248 | * variations. Note that the current state is >= HTTP_MSG_BODY, |
| 4249 | * so we don't have to adjust ->sol. |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4250 | */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 4251 | old_o += chn->buf->i - old_i; |
| 4252 | b_adv(chn->buf, old_o); |
| 4253 | txn->req.next -= old_o; |
| 4254 | txn->req.sov -= old_o; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4255 | } |
| 4256 | |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4257 | return 0; |
| 4258 | } |
| 4259 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4260 | /* Terminate current transaction and prepare a new one. This is very tricky |
| 4261 | * right now but it works. |
| 4262 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4263 | void http_end_txn_clean_session(struct stream *s) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4264 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4265 | int prev_status = s->txn->status; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 4266 | struct proxy *fe = strm_fe(s); |
Willy Tarreau | 858b103 | 2015-12-07 17:04:59 +0100 | [diff] [blame] | 4267 | struct proxy *be = s->be; |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 4268 | struct conn_stream *cs; |
Willy Tarreau | 323a2d9 | 2015-08-04 19:00:17 +0200 | [diff] [blame] | 4269 | struct connection *srv_conn; |
| 4270 | struct server *srv; |
Willy Tarreau | 449d74a | 2015-08-05 17:16:33 +0200 | [diff] [blame] | 4271 | unsigned int prev_flags = s->txn->flags; |
Willy Tarreau | 068621e | 2013-12-23 15:11:25 +0100 | [diff] [blame] | 4272 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4273 | /* FIXME: We need a more portable way of releasing a backend's and a |
| 4274 | * server's connections. We need a safer way to reinitialize buffer |
| 4275 | * flags. We also need a more accurate method for computing per-request |
| 4276 | * data. |
| 4277 | */ |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 4278 | /* |
| 4279 | * XXX cognet: This is probably wrong, this is killing a whole |
| 4280 | * connection, in the new world order, we probably want to just kill |
| 4281 | * the stream, this is to be revisited the day we handle multiple |
| 4282 | * streams in one server connection. |
| 4283 | */ |
| 4284 | cs = objt_cs(s->si[1].end); |
| 4285 | srv_conn = cs_conn(cs); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4286 | |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4287 | /* unless we're doing keep-alive, we want to quickly close the connection |
| 4288 | * to the server. |
| 4289 | */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4290 | if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) || |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4291 | !si_conn_ready(&s->si[1])) { |
| 4292 | s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF; |
| 4293 | si_shutr(&s->si[1]); |
| 4294 | si_shutw(&s->si[1]); |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4295 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4296 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4297 | if (s->flags & SF_BE_ASSIGNED) { |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 4298 | HA_ATOMIC_SUB(&be->beconn, 1); |
Willy Tarreau | 2d5cd47 | 2012-03-01 23:34:37 +0100 | [diff] [blame] | 4299 | if (unlikely(s->srv_conn)) |
| 4300 | sess_change_server(s, NULL); |
| 4301 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4302 | |
| 4303 | s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4304 | stream_process_counters(s); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4305 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4306 | if (s->txn->status) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4307 | int n; |
| 4308 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4309 | n = s->txn->status / 100; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4310 | if (n < 1 || n > 5) |
| 4311 | n = 0; |
| 4312 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4313 | if (fe->mode == PR_MODE_HTTP) { |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 4314 | HA_ATOMIC_ADD(&fe->fe_counters.p.http.rsp[n], 1); |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 4315 | } |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4316 | if ((s->flags & SF_BE_ASSIGNED) && |
Willy Tarreau | 858b103 | 2015-12-07 17:04:59 +0100 | [diff] [blame] | 4317 | (be->mode == PR_MODE_HTTP)) { |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 4318 | HA_ATOMIC_ADD(&be->be_counters.p.http.rsp[n], 1); |
| 4319 | HA_ATOMIC_ADD(&be->be_counters.p.http.cum_req, 1); |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 4320 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4321 | } |
| 4322 | |
| 4323 | /* don't count other requests' data */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4324 | s->logs.bytes_in -= s->req.buf->i; |
| 4325 | s->logs.bytes_out -= s->res.buf->i; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4326 | |
| 4327 | /* let's do a final log if we need it */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4328 | if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait && |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4329 | !(s->flags & SF_MONITOR) && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4330 | (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4331 | s->do_log(s); |
| 4332 | } |
| 4333 | |
Willy Tarreau | d713bcc | 2014-06-25 15:36:04 +0200 | [diff] [blame] | 4334 | /* stop tracking content-based counters */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4335 | stream_stop_content_counters(s); |
| 4336 | stream_update_time_stats(s); |
Willy Tarreau | 4bfc580 | 2014-06-17 12:19:18 +0200 | [diff] [blame] | 4337 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4338 | s->logs.accept_date = date; /* user-visible date for logging */ |
| 4339 | s->logs.tv_accept = now; /* corrected date for internal use */ |
Thierry FOURNIER / OZON.IO | 4cac359 | 2016-07-28 17:19:45 +0200 | [diff] [blame] | 4340 | s->logs.t_handshake = 0; /* There are no handshake in keep alive connection. */ |
| 4341 | s->logs.t_idle = -1; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4342 | tv_zero(&s->logs.tv_request); |
| 4343 | s->logs.t_queue = -1; |
| 4344 | s->logs.t_connect = -1; |
| 4345 | s->logs.t_data = -1; |
| 4346 | s->logs.t_close = 0; |
| 4347 | s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */ |
| 4348 | s->logs.srv_queue_size = 0; /* we will get this number soon */ |
| 4349 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4350 | s->logs.bytes_in = s->req.total = s->req.buf->i; |
| 4351 | s->logs.bytes_out = s->res.total = s->res.buf->i; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4352 | |
| 4353 | if (s->pend_pos) |
| 4354 | pendconn_free(s->pend_pos); |
| 4355 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4356 | if (objt_server(s->target)) { |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4357 | if (s->flags & SF_CURR_SESS) { |
| 4358 | s->flags &= ~SF_CURR_SESS; |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 4359 | HA_ATOMIC_SUB(&objt_server(s->target)->cur_sess, 1); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4360 | } |
Willy Tarreau | 858b103 | 2015-12-07 17:04:59 +0100 | [diff] [blame] | 4361 | if (may_dequeue_tasks(objt_server(s->target), be)) |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4362 | process_srv_queue(objt_server(s->target)); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4363 | } |
| 4364 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4365 | s->target = NULL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4366 | |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4367 | /* only release our endpoint if we don't intend to reuse the |
| 4368 | * connection. |
| 4369 | */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4370 | if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) || |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4371 | !si_conn_ready(&s->si[1])) { |
| 4372 | si_release_endpoint(&s->si[1]); |
Willy Tarreau | 323a2d9 | 2015-08-04 19:00:17 +0200 | [diff] [blame] | 4373 | srv_conn = NULL; |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4374 | } |
| 4375 | |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4376 | s->si[1].state = s->si[1].prev_state = SI_ST_INI; |
| 4377 | s->si[1].err_type = SI_ET_NONE; |
| 4378 | s->si[1].conn_retries = 0; /* used for logging too */ |
| 4379 | s->si[1].exp = TICK_ETERNITY; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4380 | 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] | 4381 | 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] | 4382 | 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] | 4383 | s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST); |
| 4384 | s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED); |
| 4385 | s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP); |
Willy Tarreau | 543db62 | 2012-11-15 16:41:22 +0100 | [diff] [blame] | 4386 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4387 | s->txn->meth = 0; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4388 | http_reset_txn(s); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4389 | s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ; |
Willy Tarreau | 068621e | 2013-12-23 15:11:25 +0100 | [diff] [blame] | 4390 | |
| 4391 | if (prev_status == 401 || prev_status == 407) { |
| 4392 | /* In HTTP keep-alive mode, if we receive a 401, we still have |
| 4393 | * a chance of being able to send the visitor again to the same |
| 4394 | * server over the same connection. This is required by some |
| 4395 | * broken protocols such as NTLM, and anyway whenever there is |
| 4396 | * an opportunity for sending the challenge to the proper place, |
| 4397 | * it's better to do it (at least it helps with debugging). |
| 4398 | */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4399 | s->txn->flags |= TX_PREFER_LAST; |
Willy Tarreau | bd99d58 | 2015-09-02 10:40:43 +0200 | [diff] [blame] | 4400 | if (srv_conn) |
| 4401 | srv_conn->flags |= CO_FL_PRIVATE; |
Willy Tarreau | 068621e | 2013-12-23 15:11:25 +0100 | [diff] [blame] | 4402 | } |
| 4403 | |
Willy Tarreau | 53f9685 | 2016-02-02 18:50:47 +0100 | [diff] [blame] | 4404 | /* Never ever allow to reuse a connection from a non-reuse backend */ |
| 4405 | if (srv_conn && (be->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR) |
| 4406 | srv_conn->flags |= CO_FL_PRIVATE; |
| 4407 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4408 | if (fe->options2 & PR_O2_INDEPSTR) |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4409 | s->si[1].flags |= SI_FL_INDEP_STR; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4410 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4411 | if (fe->options2 & PR_O2_NODELAY) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4412 | s->req.flags |= CF_NEVER_WAIT; |
| 4413 | s->res.flags |= CF_NEVER_WAIT; |
Willy Tarreau | 96e3121 | 2011-05-30 18:10:30 +0200 | [diff] [blame] | 4414 | } |
| 4415 | |
Willy Tarreau | 714ea78 | 2015-11-25 20:11:11 +0100 | [diff] [blame] | 4416 | /* we're removing the analysers, we MUST re-enable events detection. |
| 4417 | * We don't enable close on the response channel since it's either |
| 4418 | * already closed, or in keep-alive with an idle connection handler. |
| 4419 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4420 | channel_auto_read(&s->req); |
| 4421 | channel_auto_close(&s->req); |
| 4422 | channel_auto_read(&s->res); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4423 | |
Willy Tarreau | 1c59bd5 | 2015-11-02 20:20:11 +0100 | [diff] [blame] | 4424 | /* we're in keep-alive with an idle connection, monitor it if not already done */ |
| 4425 | if (srv_conn && LIST_ISEMPTY(&srv_conn->list)) { |
Willy Tarreau | 323a2d9 | 2015-08-04 19:00:17 +0200 | [diff] [blame] | 4426 | srv = objt_server(srv_conn->target); |
Willy Tarreau | 8dff998 | 2015-08-04 20:45:52 +0200 | [diff] [blame] | 4427 | if (!srv) |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 4428 | si_idle_cs(&s->si[1], NULL); |
Willy Tarreau | 53f9685 | 2016-02-02 18:50:47 +0100 | [diff] [blame] | 4429 | else if (srv_conn->flags & CO_FL_PRIVATE) |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 4430 | 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] | 4431 | else if (prev_flags & TX_NOT_FIRST) |
| 4432 | /* note: we check the request, not the connection, but |
| 4433 | * this is valid for strategies SAFE and AGGR, and in |
| 4434 | * case of ALWS, we don't care anyway. |
| 4435 | */ |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 4436 | 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] | 4437 | else |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 4438 | 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] | 4439 | } |
Christopher Faulet | e600624 | 2017-03-10 11:52:44 +0100 | [diff] [blame] | 4440 | s->req.analysers = strm_li(s) ? strm_li(s)->analysers : 0; |
| 4441 | s->res.analysers = 0; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4442 | } |
| 4443 | |
| 4444 | |
| 4445 | /* This function updates the request state machine according to the response |
| 4446 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 4447 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 4448 | * it is only used to find when a request/response couple is complete. Both |
| 4449 | * this function and its equivalent should loop until both return zero. It |
| 4450 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 4451 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4452 | int http_sync_req_state(struct stream *s) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4453 | { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4454 | struct channel *chn = &s->req; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4455 | struct http_txn *txn = s->txn; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4456 | unsigned int old_flags = chn->flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4457 | unsigned int old_state = txn->req.msg_state; |
| 4458 | |
Christopher Faulet | 4be9803 | 2017-07-18 10:48:24 +0200 | [diff] [blame] | 4459 | if (unlikely(txn->req.msg_state < HTTP_MSG_DONE)) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4460 | return 0; |
| 4461 | |
| 4462 | if (txn->req.msg_state == HTTP_MSG_DONE) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4463 | /* No need to read anymore, the request was completely parsed. |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 4464 | * We can shut the read side unless we want to abort_on_close, |
| 4465 | * or we have a POST request. The issue with POST requests is |
| 4466 | * that some browsers still send a CRLF after the request, and |
| 4467 | * this CRLF must be read so that it does not remain in the kernel |
| 4468 | * buffers, otherwise a close could cause an RST on some systems |
| 4469 | * (eg: Linux). |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 4470 | * Note that if we're using keep-alive on the client side, we'd |
| 4471 | * rather poll now and keep the polling enabled for the whole |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4472 | * stream's life than enabling/disabling it between each |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 4473 | * response and next request. |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4474 | */ |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 4475 | if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) && |
| 4476 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) && |
Willy Tarreau | 7aa15b0 | 2017-12-20 16:56:50 +0100 | [diff] [blame] | 4477 | (!(s->be->options & PR_O_ABRT_CLOSE) || |
| 4478 | (s->si[0].flags & SI_FL_CLEAN_ABRT)) && |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 4479 | txn->meth != HTTP_METH_POST) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4480 | channel_dont_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4481 | |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 4482 | /* if the server closes the connection, we want to immediately react |
| 4483 | * and close the socket to save packets and syscalls. |
| 4484 | */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4485 | s->si[1].flags |= SI_FL_NOHALF; |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 4486 | |
Willy Tarreau | 7f876a1 | 2015-11-18 11:59:55 +0100 | [diff] [blame] | 4487 | /* In any case we've finished parsing the request so we must |
| 4488 | * disable Nagle when sending data because 1) we're not going |
| 4489 | * to shut this side, and 2) the server is waiting for us to |
| 4490 | * send pending data. |
| 4491 | */ |
| 4492 | chn->flags |= CF_NEVER_WAIT; |
| 4493 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4494 | if (txn->rsp.msg_state == HTTP_MSG_ERROR) |
| 4495 | goto wait_other_side; |
| 4496 | |
| 4497 | if (txn->rsp.msg_state < HTTP_MSG_DONE) { |
| 4498 | /* The server has not finished to respond, so we |
| 4499 | * don't want to move in order not to upset it. |
| 4500 | */ |
| 4501 | goto wait_other_side; |
| 4502 | } |
| 4503 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4504 | /* When we get here, it means that both the request and the |
| 4505 | * response have finished receiving. Depending on the connection |
| 4506 | * mode, we'll have to wait for the last bytes to leave in either |
| 4507 | * direction, and sometimes for a close to be effective. |
| 4508 | */ |
| 4509 | |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4510 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 4511 | /* Server-close mode : queue a connection close to the server */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4512 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) |
| 4513 | channel_shutw_now(chn); |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4514 | } |
| 4515 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 4516 | /* Option forceclose is set, or either side wants to close, |
| 4517 | * let's enforce it now that we're not expecting any new |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4518 | * data to come. The caller knows the stream is complete |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4519 | * once both states are CLOSED. |
Christopher Faulet | 1486b0a | 2017-07-18 11:42:08 +0200 | [diff] [blame] | 4520 | * |
| 4521 | * However, there is an exception if the response |
| 4522 | * length is undefined. In this case, we need to wait |
| 4523 | * the close from the server. The response will be |
| 4524 | * switched in TUNNEL mode until the end. |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4525 | */ |
Christopher Faulet | 1486b0a | 2017-07-18 11:42:08 +0200 | [diff] [blame] | 4526 | if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) && |
| 4527 | txn->rsp.msg_state != HTTP_MSG_CLOSED) |
| 4528 | goto check_channel_flags; |
| 4529 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4530 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) { |
| 4531 | channel_shutr_now(chn); |
| 4532 | channel_shutw_now(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4533 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4534 | } |
| 4535 | else { |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4536 | /* The last possible modes are keep-alive and tunnel. Tunnel mode |
| 4537 | * will not have any analyser so it needs to poll for reads. |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4538 | */ |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4539 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) { |
| 4540 | channel_auto_read(chn); |
| 4541 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4542 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4543 | } |
| 4544 | |
Christopher Faulet | 4be9803 | 2017-07-18 10:48:24 +0200 | [diff] [blame] | 4545 | goto check_channel_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4546 | } |
| 4547 | |
| 4548 | if (txn->req.msg_state == HTTP_MSG_CLOSING) { |
| 4549 | http_msg_closing: |
| 4550 | /* nothing else to forward, just waiting for the output buffer |
| 4551 | * to be empty and for the shutw_now to take effect. |
| 4552 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4553 | if (channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4554 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 4555 | goto http_msg_closed; |
| 4556 | } |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4557 | else if (chn->flags & CF_SHUTW) { |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 4558 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4559 | txn->req.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4560 | } |
Christopher Faulet | 56d2609 | 2017-07-20 11:05:10 +0200 | [diff] [blame] | 4561 | goto wait_other_side; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4562 | } |
| 4563 | |
| 4564 | if (txn->req.msg_state == HTTP_MSG_CLOSED) { |
| 4565 | http_msg_closed: |
Willy Tarreau | 8059351 | 2017-12-14 10:43:31 +0100 | [diff] [blame] | 4566 | /* if we don't know whether the server will close, we need to hard close */ |
| 4567 | if (txn->rsp.flags & HTTP_MSGF_XFER_LEN) |
| 4568 | s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */ |
| 4569 | |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 4570 | /* see above in MSG_DONE why we only do this in these states */ |
| 4571 | if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) && |
| 4572 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) && |
Willy Tarreau | 7aa15b0 | 2017-12-20 16:56:50 +0100 | [diff] [blame] | 4573 | (!(s->be->options & PR_O_ABRT_CLOSE) || |
| 4574 | (s->si[0].flags & SI_FL_CLEAN_ABRT))) |
Willy Tarreau | 2e7a165 | 2013-12-15 15:32:10 +0100 | [diff] [blame] | 4575 | channel_dont_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4576 | goto wait_other_side; |
| 4577 | } |
| 4578 | |
Christopher Faulet | 4be9803 | 2017-07-18 10:48:24 +0200 | [diff] [blame] | 4579 | check_channel_flags: |
| 4580 | /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */ |
| 4581 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) { |
| 4582 | /* if we've just closed an output, let's switch */ |
Christopher Faulet | 4be9803 | 2017-07-18 10:48:24 +0200 | [diff] [blame] | 4583 | txn->req.msg_state = HTTP_MSG_CLOSING; |
| 4584 | goto http_msg_closing; |
| 4585 | } |
| 4586 | |
| 4587 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4588 | wait_other_side: |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4589 | return txn->req.msg_state != old_state || chn->flags != old_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4590 | } |
| 4591 | |
| 4592 | |
| 4593 | /* This function updates the response state machine according to the request |
| 4594 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 4595 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 4596 | * it is only used to find when a request/response couple is complete. Both |
| 4597 | * this function and its equivalent should loop until both return zero. It |
| 4598 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 4599 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4600 | int http_sync_res_state(struct stream *s) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4601 | { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4602 | struct channel *chn = &s->res; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4603 | struct http_txn *txn = s->txn; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4604 | unsigned int old_flags = chn->flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4605 | unsigned int old_state = txn->rsp.msg_state; |
| 4606 | |
Christopher Faulet | 4be9803 | 2017-07-18 10:48:24 +0200 | [diff] [blame] | 4607 | if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE)) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4608 | return 0; |
| 4609 | |
| 4610 | if (txn->rsp.msg_state == HTTP_MSG_DONE) { |
| 4611 | /* In theory, we don't need to read anymore, but we must |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4612 | * still monitor the server connection for a possible close |
| 4613 | * while the request is being uploaded, so we don't disable |
| 4614 | * reading. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4615 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4616 | /* channel_dont_read(chn); */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4617 | |
| 4618 | if (txn->req.msg_state == HTTP_MSG_ERROR) |
| 4619 | goto wait_other_side; |
| 4620 | |
| 4621 | if (txn->req.msg_state < HTTP_MSG_DONE) { |
| 4622 | /* The client seems to still be sending data, probably |
| 4623 | * because we got an error response during an upload. |
| 4624 | * We have the choice of either breaking the connection |
| 4625 | * or letting it pass through. Let's do the later. |
| 4626 | */ |
| 4627 | goto wait_other_side; |
| 4628 | } |
| 4629 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4630 | /* When we get here, it means that both the request and the |
| 4631 | * response have finished receiving. Depending on the connection |
| 4632 | * mode, we'll have to wait for the last bytes to leave in either |
| 4633 | * direction, and sometimes for a close to be effective. |
| 4634 | */ |
| 4635 | |
| 4636 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 4637 | /* Server-close mode : shut read and wait for the request |
| 4638 | * side to close its output buffer. The caller will detect |
| 4639 | * when we're in DONE and the other is in CLOSED and will |
| 4640 | * catch that for the final cleanup. |
| 4641 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4642 | if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW))) |
| 4643 | channel_shutr_now(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4644 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4645 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 4646 | /* Option forceclose is set, or either side wants to close, |
| 4647 | * let's enforce it now that we're not expecting any new |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4648 | * data to come. The caller knows the stream is complete |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4649 | * once both states are CLOSED. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4650 | */ |
Christopher Faulet | fd04fcf | 2018-02-02 15:54:15 +0100 | [diff] [blame] | 4651 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4652 | channel_shutr_now(chn); |
| 4653 | channel_shutw_now(chn); |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4654 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4655 | } |
| 4656 | else { |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4657 | /* The last possible modes are keep-alive and tunnel. Tunnel will |
| 4658 | * need to forward remaining data. Keep-alive will need to monitor |
| 4659 | * for connection closing. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4660 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4661 | channel_auto_read(chn); |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 4662 | chn->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4663 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) |
| 4664 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4665 | } |
| 4666 | |
Christopher Faulet | 4be9803 | 2017-07-18 10:48:24 +0200 | [diff] [blame] | 4667 | goto check_channel_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4668 | } |
| 4669 | |
| 4670 | if (txn->rsp.msg_state == HTTP_MSG_CLOSING) { |
| 4671 | http_msg_closing: |
| 4672 | /* nothing else to forward, just waiting for the output buffer |
| 4673 | * to be empty and for the shutw_now to take effect. |
| 4674 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4675 | if (channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4676 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 4677 | goto http_msg_closed; |
| 4678 | } |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4679 | else if (chn->flags & CF_SHUTW) { |
Christopher Faulet | a3992e0 | 2017-07-18 10:35:55 +0200 | [diff] [blame] | 4680 | txn->rsp.err_state = txn->rsp.msg_state; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4681 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 4682 | HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4683 | if (objt_server(s->target)) |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 4684 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4685 | } |
Christopher Faulet | 56d2609 | 2017-07-20 11:05:10 +0200 | [diff] [blame] | 4686 | goto wait_other_side; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4687 | } |
| 4688 | |
| 4689 | if (txn->rsp.msg_state == HTTP_MSG_CLOSED) { |
| 4690 | http_msg_closed: |
| 4691 | /* drop any pending data */ |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 4692 | channel_truncate(chn); |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4693 | channel_auto_close(chn); |
| 4694 | channel_auto_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4695 | goto wait_other_side; |
| 4696 | } |
| 4697 | |
Christopher Faulet | 4be9803 | 2017-07-18 10:48:24 +0200 | [diff] [blame] | 4698 | check_channel_flags: |
| 4699 | /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */ |
| 4700 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) { |
| 4701 | /* if we've just closed an output, let's switch */ |
| 4702 | txn->rsp.msg_state = HTTP_MSG_CLOSING; |
| 4703 | goto http_msg_closing; |
| 4704 | } |
| 4705 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4706 | wait_other_side: |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 4707 | /* We force the response to leave immediately if we're waiting for the |
| 4708 | * other side, since there is no pending shutdown to push it out. |
| 4709 | */ |
| 4710 | if (!channel_is_empty(chn)) |
| 4711 | chn->flags |= CF_SEND_DONTWAIT; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4712 | return txn->rsp.msg_state != old_state || chn->flags != old_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4713 | } |
| 4714 | |
| 4715 | |
Christopher Faulet | 894da4c | 2017-07-18 11:29:07 +0200 | [diff] [blame] | 4716 | /* Resync the request and response state machines. */ |
| 4717 | void http_resync_states(struct stream *s) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4718 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4719 | struct http_txn *txn = s->txn; |
Christopher Faulet | 894da4c | 2017-07-18 11:29:07 +0200 | [diff] [blame] | 4720 | #ifdef DEBUG_FULL |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4721 | int old_req_state = txn->req.msg_state; |
| 4722 | int old_res_state = txn->rsp.msg_state; |
Christopher Faulet | 894da4c | 2017-07-18 11:29:07 +0200 | [diff] [blame] | 4723 | #endif |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4724 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4725 | http_sync_req_state(s); |
| 4726 | while (1) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4727 | if (!http_sync_res_state(s)) |
| 4728 | break; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4729 | if (!http_sync_req_state(s)) |
| 4730 | break; |
| 4731 | } |
Willy Tarreau | 3ce10ff | 2014-04-22 08:24:38 +0200 | [diff] [blame] | 4732 | |
Christopher Faulet | 894da4c | 2017-07-18 11:29:07 +0200 | [diff] [blame] | 4733 | DPRINTF(stderr,"[%u] %s: stream=%p old=%s,%s cur=%s,%s " |
| 4734 | "req->analysers=0x%08x res->analysers=0x%08x\n", |
| 4735 | now_ms, __FUNCTION__, s, |
Willy Tarreau | 0da5b3b | 2017-09-21 09:30:46 +0200 | [diff] [blame] | 4736 | h1_msg_state_str(old_req_state), h1_msg_state_str(old_res_state), |
| 4737 | 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] | 4738 | s->req.analysers, s->res.analysers); |
Christopher Faulet | 814d270 | 2017-03-30 11:33:44 +0200 | [diff] [blame] | 4739 | |
| 4740 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4741 | /* OK, both state machines agree on a compatible state. |
| 4742 | * There are a few cases we're interested in : |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4743 | * - HTTP_MSG_CLOSED on both sides means we've reached the end in both |
| 4744 | * directions, so let's simply disable both analysers. |
Christopher Faulet | f77bb53 | 2017-07-18 11:18:46 +0200 | [diff] [blame] | 4745 | * - HTTP_MSG_CLOSED on the response only or HTTP_MSG_ERROR on either |
| 4746 | * means we must abort the request. |
| 4747 | * - HTTP_MSG_TUNNEL on either means we have to disable analyser on |
| 4748 | * corresponding channel. |
| 4749 | * - HTTP_MSG_DONE or HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE |
| 4750 | * on the response with server-close mode means we've completed one |
| 4751 | * request and we must re-initialize the server connection. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4752 | */ |
Christopher Faulet | f77bb53 | 2017-07-18 11:18:46 +0200 | [diff] [blame] | 4753 | if (txn->req.msg_state == HTTP_MSG_CLOSED && |
| 4754 | txn->rsp.msg_state == HTTP_MSG_CLOSED) { |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4755 | s->req.analysers &= AN_REQ_FLT_END; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4756 | channel_auto_close(&s->req); |
| 4757 | channel_auto_read(&s->req); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4758 | s->res.analysers &= AN_RES_FLT_END; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4759 | channel_auto_close(&s->res); |
| 4760 | channel_auto_read(&s->res); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4761 | } |
Christopher Faulet | f77bb53 | 2017-07-18 11:18:46 +0200 | [diff] [blame] | 4762 | else if (txn->rsp.msg_state == HTTP_MSG_CLOSED || |
| 4763 | txn->rsp.msg_state == HTTP_MSG_ERROR || |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 4764 | txn->req.msg_state == HTTP_MSG_ERROR) { |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4765 | s->res.analysers &= AN_RES_FLT_END; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4766 | channel_auto_close(&s->res); |
| 4767 | channel_auto_read(&s->res); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4768 | s->req.analysers &= AN_REQ_FLT_END; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4769 | channel_abort(&s->req); |
| 4770 | channel_auto_close(&s->req); |
| 4771 | channel_auto_read(&s->req); |
| 4772 | channel_truncate(&s->req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4773 | } |
Christopher Faulet | f77bb53 | 2017-07-18 11:18:46 +0200 | [diff] [blame] | 4774 | else if (txn->req.msg_state == HTTP_MSG_TUNNEL || |
| 4775 | txn->rsp.msg_state == HTTP_MSG_TUNNEL) { |
| 4776 | if (txn->req.msg_state == HTTP_MSG_TUNNEL) { |
| 4777 | s->req.analysers &= AN_REQ_FLT_END; |
| 4778 | if (HAS_REQ_DATA_FILTERS(s)) |
| 4779 | s->req.analysers |= AN_REQ_FLT_XFER_DATA; |
| 4780 | } |
| 4781 | if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) { |
| 4782 | s->res.analysers &= AN_RES_FLT_END; |
| 4783 | if (HAS_RSP_DATA_FILTERS(s)) |
| 4784 | s->res.analysers |= AN_RES_FLT_XFER_DATA; |
| 4785 | } |
| 4786 | channel_auto_close(&s->req); |
| 4787 | channel_auto_read(&s->req); |
| 4788 | channel_auto_close(&s->res); |
| 4789 | channel_auto_read(&s->res); |
| 4790 | } |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4791 | else if ((txn->req.msg_state == HTTP_MSG_DONE || |
| 4792 | txn->req.msg_state == HTTP_MSG_CLOSED) && |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4793 | txn->rsp.msg_state == HTTP_MSG_DONE && |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4794 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL || |
| 4795 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) { |
| 4796 | /* server-close/keep-alive: terminate this transaction, |
| 4797 | * possibly killing the server connection and reinitialize |
Willy Tarreau | 3de5bd6 | 2016-05-02 16:07:07 +0200 | [diff] [blame] | 4798 | * a fresh-new transaction, but only once we're sure there's |
| 4799 | * enough room in the request and response buffer to process |
Christopher Faulet | c0c672a | 2017-03-28 11:51:33 +0200 | [diff] [blame] | 4800 | * another request. They must not hold any pending output data |
| 4801 | * and the response buffer must realigned |
| 4802 | * (realign is done is http_end_txn_clean_session). |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4803 | */ |
Christopher Faulet | 894da4c | 2017-07-18 11:29:07 +0200 | [diff] [blame] | 4804 | if (s->req.buf->o) |
Willy Tarreau | 3de5bd6 | 2016-05-02 16:07:07 +0200 | [diff] [blame] | 4805 | s->req.flags |= CF_WAKE_WRITE; |
Christopher Faulet | 894da4c | 2017-07-18 11:29:07 +0200 | [diff] [blame] | 4806 | else if (s->res.buf->o) |
Willy Tarreau | 3de5bd6 | 2016-05-02 16:07:07 +0200 | [diff] [blame] | 4807 | s->res.flags |= CF_WAKE_WRITE; |
Christopher Faulet | a81ff60 | 2017-07-18 22:01:05 +0200 | [diff] [blame] | 4808 | else { |
| 4809 | s->req.analysers = AN_REQ_FLT_END; |
| 4810 | s->res.analysers = AN_RES_FLT_END; |
| 4811 | txn->flags |= TX_WAIT_CLEANUP; |
| 4812 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4813 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4814 | } |
| 4815 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4816 | /* This function is an analyser which forwards request body (including chunk |
| 4817 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 4818 | * zero byte. The only situation where it must not be called is when we're in |
| 4819 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 4820 | * remaining data and to resync after end of body. It expects the msg_state to |
| 4821 | * 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] | 4822 | * 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] | 4823 | * 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] | 4824 | * bytes of pending data + the headers if not already done. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4825 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4826 | 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] | 4827 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4828 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4829 | struct http_txn *txn = s->txn; |
| 4830 | struct http_msg *msg = &s->txn->req; |
Christopher Faulet | 3e34429 | 2015-11-24 16:24:13 +0100 | [diff] [blame] | 4831 | int ret; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4832 | |
Christopher Faulet | 814d270 | 2017-03-30 11:33:44 +0200 | [diff] [blame] | 4833 | DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n", |
| 4834 | now_ms, __FUNCTION__, |
| 4835 | s, |
| 4836 | req, |
| 4837 | req->rex, req->wex, |
| 4838 | req->flags, |
| 4839 | req->buf->i, |
| 4840 | req->analysers); |
| 4841 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4842 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4843 | return 0; |
| 4844 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4845 | 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] | 4846 | ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) { |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 4847 | /* Output closed while we were sending data. We must abort and |
| 4848 | * wake the other side up. |
| 4849 | */ |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 4850 | msg->err_state = msg->msg_state; |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 4851 | msg->msg_state = HTTP_MSG_ERROR; |
| 4852 | http_resync_states(s); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 4853 | return 1; |
| 4854 | } |
| 4855 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4856 | /* Note that we don't have to send 100-continue back because we don't |
| 4857 | * need the data to complete our job, and it's up to the server to |
| 4858 | * decide whether to return 100, 417 or anything else in return of |
| 4859 | * an "Expect: 100-continue" header. |
| 4860 | */ |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 4861 | if (msg->msg_state == HTTP_MSG_BODY) { |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4862 | msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK) |
| 4863 | ? HTTP_MSG_CHUNK_SIZE |
| 4864 | : HTTP_MSG_DATA); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 4865 | |
| 4866 | /* TODO/filters: when http-buffer-request option is set or if a |
| 4867 | * rule on url_param exists, the first chunk size could be |
| 4868 | * already parsed. In that case, msg->next is after the chunk |
| 4869 | * size (including the CRLF after the size). So this case should |
| 4870 | * be handled to */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4871 | } |
| 4872 | |
Willy Tarreau | 7ba2354 | 2014-04-17 21:50:00 +0200 | [diff] [blame] | 4873 | /* Some post-connect processing might want us to refrain from starting to |
| 4874 | * forward data. Currently, the only reason for this is "balance url_param" |
| 4875 | * whichs need to parse/process the request after we've enabled forwarding. |
| 4876 | */ |
| 4877 | if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4878 | if (!(s->res.flags & CF_READ_ATTACHED)) { |
Willy Tarreau | 7ba2354 | 2014-04-17 21:50:00 +0200 | [diff] [blame] | 4879 | channel_auto_connect(req); |
Willy Tarreau | 644c101 | 2014-04-30 18:11:11 +0200 | [diff] [blame] | 4880 | req->flags |= CF_WAKE_CONNECT; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4881 | goto missing_data_or_waiting; |
Willy Tarreau | 7ba2354 | 2014-04-17 21:50:00 +0200 | [diff] [blame] | 4882 | } |
| 4883 | msg->flags &= ~HTTP_MSGF_WAIT_CONN; |
| 4884 | } |
| 4885 | |
Willy Tarreau | 80a92c0 | 2014-03-12 10:41:13 +0100 | [diff] [blame] | 4886 | /* in most states, we should abort in case of early close */ |
| 4887 | channel_auto_close(req); |
| 4888 | |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 4889 | if (req->to_forward) { |
| 4890 | /* We can't process the buffer's contents yet */ |
| 4891 | req->flags |= CF_WAKE_WRITE; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4892 | goto missing_data_or_waiting; |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 4893 | } |
| 4894 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4895 | if (msg->msg_state < HTTP_MSG_DONE) { |
| 4896 | ret = ((msg->flags & HTTP_MSGF_TE_CHNK) |
| 4897 | ? http_msg_forward_chunked_body(s, msg) |
| 4898 | : http_msg_forward_body(s, msg)); |
| 4899 | if (!ret) |
| 4900 | goto missing_data_or_waiting; |
| 4901 | if (ret < 0) |
| 4902 | goto return_bad_req; |
| 4903 | } |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 4904 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4905 | /* other states, DONE...TUNNEL */ |
| 4906 | /* we don't want to forward closes on DONE except in tunnel mode. */ |
| 4907 | if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) |
| 4908 | channel_dont_close(req); |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 4909 | |
Christopher Faulet | 894da4c | 2017-07-18 11:29:07 +0200 | [diff] [blame] | 4910 | http_resync_states(s); |
| 4911 | if (!(req->analysers & an_bit)) { |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4912 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 4913 | if (req->flags & CF_SHUTW) { |
| 4914 | /* request errors are most likely due to the |
| 4915 | * server aborting the transfer. */ |
| 4916 | goto aborted_xfer; |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 4917 | } |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4918 | if (msg->err_pos >= 0) |
Emeric Brun | 8c1aaa2 | 2017-06-15 11:30:06 +0200 | [diff] [blame] | 4919 | 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] | 4920 | goto return_bad_req; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4921 | } |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4922 | return 1; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4923 | } |
| 4924 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4925 | /* If "option abortonclose" is set on the backend, we want to monitor |
| 4926 | * the client's connection and forward any shutdown notification to the |
| 4927 | * server, which will decide whether to close or to go on processing the |
| 4928 | * request. We only do that in tunnel mode, and not in other modes since |
| 4929 | * it can be abused to exhaust source ports. */ |
Willy Tarreau | 7aa15b0 | 2017-12-20 16:56:50 +0100 | [diff] [blame] | 4930 | 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] | 4931 | channel_auto_read(req); |
| 4932 | if ((req->flags & (CF_SHUTR|CF_READ_NULL)) && |
| 4933 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)) |
| 4934 | s->si[1].flags |= SI_FL_NOLINGER; |
| 4935 | channel_auto_close(req); |
| 4936 | } |
| 4937 | else if (s->txn->meth == HTTP_METH_POST) { |
| 4938 | /* POST requests may require to read extra CRLF sent by broken |
| 4939 | * browsers and which could cause an RST to be sent upon close |
| 4940 | * on some systems (eg: Linux). */ |
| 4941 | channel_auto_read(req); |
| 4942 | } |
| 4943 | return 0; |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 4944 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4945 | missing_data_or_waiting: |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4946 | /* stop waiting for data if the input is closed before the end */ |
Christopher Faulet | a33510b | 2017-03-31 15:37:29 +0200 | [diff] [blame] | 4947 | if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR) { |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4948 | if (!(s->flags & SF_ERR_MASK)) |
| 4949 | s->flags |= SF_ERR_CLICL; |
| 4950 | if (!(s->flags & SF_FINST_MASK)) { |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4951 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4952 | s->flags |= SF_FINST_H; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4953 | else |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4954 | s->flags |= SF_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4955 | } |
| 4956 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 4957 | HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1); |
| 4958 | HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4959 | if (objt_server(s->target)) |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 4960 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4961 | |
| 4962 | goto return_bad_req_stats_ok; |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 4963 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4964 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4965 | /* waiting for the last bits to leave the buffer */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4966 | if (req->flags & CF_SHUTW) |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4967 | goto aborted_xfer; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4968 | |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 4969 | /* 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] | 4970 | * 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] | 4971 | * And when content-length is used, we never want to let the possible |
| 4972 | * shutdown be forwarded to the other side, as the state machine will |
| 4973 | * take care of it once the client responds. It's also important to |
| 4974 | * prevent TIME_WAITs from accumulating on the backend side, and for |
| 4975 | * HTTP/2 where the last frame comes with a shutdown. |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 4976 | */ |
Willy Tarreau | ff47b3f | 2017-12-29 16:30:31 +0100 | [diff] [blame] | 4977 | if (msg->flags & (HTTP_MSGF_TE_CHNK|HTTP_MSGF_CNT_LEN)) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4978 | channel_dont_close(req); |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 4979 | |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 4980 | /* 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] | 4981 | * 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] | 4982 | * 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] | 4983 | * modes are already handled by the stream sock layer. We must not do |
| 4984 | * this in content-length mode because it could present the MSG_MORE |
| 4985 | * flag with the last block of forwarded data, which would cause an |
| 4986 | * additional delay to be observed by the receiver. |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 4987 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4988 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4989 | req->flags |= CF_EXPECT_MORE; |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 4990 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4991 | return 0; |
| 4992 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4993 | return_bad_req: /* let's centralize all bad requests */ |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 4994 | HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4995 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 4996 | HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1); |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 4997 | |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4998 | return_bad_req_stats_ok: |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 4999 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5000 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 5001 | if (txn->status) { |
| 5002 | /* 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] | 5003 | http_reply_and_close(s, txn->status, NULL); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5004 | } else { |
| 5005 | txn->status = 400; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 5006 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5007 | } |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 5008 | req->analysers &= AN_REQ_FLT_END; |
| 5009 | 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] | 5010 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5011 | if (!(s->flags & SF_ERR_MASK)) |
| 5012 | s->flags |= SF_ERR_PRXCOND; |
| 5013 | if (!(s->flags & SF_FINST_MASK)) { |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5014 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5015 | s->flags |= SF_FINST_H; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5016 | else |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5017 | s->flags |= SF_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5018 | } |
| 5019 | return 0; |
| 5020 | |
| 5021 | aborted_xfer: |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 5022 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5023 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 5024 | if (txn->status) { |
| 5025 | /* 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] | 5026 | http_reply_and_close(s, txn->status, NULL); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5027 | } else { |
| 5028 | txn->status = 502; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 5029 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5030 | } |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 5031 | req->analysers &= AN_REQ_FLT_END; |
| 5032 | 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] | 5033 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 5034 | HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1); |
| 5035 | HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5036 | if (objt_server(s->target)) |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 5037 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5038 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5039 | if (!(s->flags & SF_ERR_MASK)) |
| 5040 | s->flags |= SF_ERR_SRVCL; |
| 5041 | if (!(s->flags & SF_FINST_MASK)) { |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5042 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5043 | s->flags |= SF_FINST_H; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5044 | else |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5045 | s->flags |= SF_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5046 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5047 | return 0; |
| 5048 | } |
| 5049 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5050 | /* This stream analyser waits for a complete HTTP response. It returns 1 if the |
| 5051 | * processing can continue on next analysers, or zero if it either needs more |
| 5052 | * data or wants to immediately abort the response (eg: timeout, error, ...). It |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5053 | * 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] | 5054 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 5055 | * abort. |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 5056 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5057 | 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] | 5058 | { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5059 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5060 | struct http_txn *txn = s->txn; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5061 | struct http_msg *msg = &txn->rsp; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5062 | struct hdr_ctx ctx; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5063 | int use_close_only; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5064 | int cur_idx; |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 5065 | int n; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 5066 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5067 | 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] | 5068 | now_ms, __FUNCTION__, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5069 | s, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 5070 | rep, |
| 5071 | rep->rex, rep->wex, |
| 5072 | rep->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5073 | rep->buf->i, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 5074 | rep->analysers); |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 5075 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5076 | /* |
| 5077 | * Now parse the partial (or complete) lines. |
| 5078 | * We will check the response syntax, and also join multi-line |
| 5079 | * headers. An index of all the lines will be elaborated while |
| 5080 | * parsing. |
| 5081 | * |
| 5082 | * For the parsing, we use a 28 states FSM. |
| 5083 | * |
| 5084 | * Here is the information we currently have : |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5085 | * rep->buf->p = beginning of response |
| 5086 | * rep->buf->p + msg->eoh = end of processed headers / start of current one |
| 5087 | * rep->buf->p + rep->buf->i = end of input data |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 5088 | * msg->eol = end of current header or line (LF or CRLF) |
| 5089 | * msg->next = first non-visited byte |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5090 | */ |
| 5091 | |
Willy Tarreau | 628c40c | 2014-04-24 19:11:26 +0200 | [diff] [blame] | 5092 | next_one: |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 5093 | /* There's a protected area at the end of the buffer for rewriting |
| 5094 | * purposes. We don't want to start to parse the request if the |
| 5095 | * protected area is affected, because we may have to move processed |
| 5096 | * data later, which is much more complicated. |
| 5097 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5098 | if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) { |
Willy Tarreau | ba0902e | 2015-01-13 14:39:16 +0100 | [diff] [blame] | 5099 | if (unlikely(!channel_is_rewritable(rep))) { |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 5100 | /* some data has still not left the buffer, wake us once that's done */ |
| 5101 | if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) |
| 5102 | goto abort_response; |
| 5103 | channel_dont_close(rep); |
| 5104 | rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | d7ad9f5 | 2013-12-31 17:26:25 +0100 | [diff] [blame] | 5105 | rep->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 5106 | return 0; |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 5107 | } |
| 5108 | |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 5109 | if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) || |
| 5110 | bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite)) |
| 5111 | buffer_slow_realign(rep->buf); |
| 5112 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5113 | if (likely(msg->next < rep->buf->i)) |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 5114 | http_msg_analyzer(msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 5115 | } |
| 5116 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5117 | /* 1: we might have to print this header in debug mode */ |
| 5118 | if (unlikely((global.mode & MODE_DEBUG) && |
| 5119 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | 7d59e90 | 2014-10-21 19:36:09 +0200 | [diff] [blame] | 5120 | msg->msg_state >= HTTP_MSG_BODY)) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5121 | char *eol, *sol; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5122 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5123 | sol = rep->buf->p; |
| 5124 | 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] | 5125 | debug_hdr("srvrep", s, sol, eol); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5126 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5127 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 5128 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5129 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5130 | while (cur_idx) { |
| 5131 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 5132 | debug_hdr("srvhdr", s, sol, eol); |
| 5133 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 5134 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
| 5135 | } |
| 5136 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5137 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5138 | /* |
| 5139 | * Now we quickly check if we have found a full valid response. |
| 5140 | * If not so, we check the FD and buffer states before leaving. |
| 5141 | * A full response is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 5142 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5143 | * responses are checked first. |
| 5144 | * |
| 5145 | * Depending on whether the client is still there or not, we |
| 5146 | * may send an error response back or not. Note that normally |
| 5147 | * we should only check for HTTP status there, and check I/O |
| 5148 | * errors somewhere else. |
| 5149 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5150 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 5151 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5152 | /* Invalid response */ |
| 5153 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 5154 | /* we detected a parsing error. We want to archive this response |
| 5155 | * in the dedicated proxy area for later troubleshooting. |
| 5156 | */ |
| 5157 | hdr_response_bad: |
| 5158 | if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0) |
Emeric Brun | 8c1aaa2 | 2017-06-15 11:30:06 +0200 | [diff] [blame] | 5159 | 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] | 5160 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 5161 | HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5162 | if (objt_server(s->target)) { |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 5163 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5164 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5165 | } |
Willy Tarreau | 6464841 | 2010-03-05 10:41:54 +0100 | [diff] [blame] | 5166 | abort_response: |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5167 | channel_auto_close(rep); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 5168 | rep->analysers &= AN_RES_FLT_END; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5169 | txn->status = 502; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5170 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5171 | channel_truncate(rep); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 5172 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5173 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5174 | if (!(s->flags & SF_ERR_MASK)) |
| 5175 | s->flags |= SF_ERR_PRXCOND; |
| 5176 | if (!(s->flags & SF_FINST_MASK)) |
| 5177 | s->flags |= SF_FINST_H; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5178 | |
| 5179 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5180 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5181 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5182 | /* too large response does not fit in buffer. */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5183 | else if (buffer_full(rep->buf, global.tune.maxrewrite)) { |
Willy Tarreau | fec4d89 | 2011-09-02 20:04:57 +0200 | [diff] [blame] | 5184 | if (msg->err_pos < 0) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5185 | msg->err_pos = rep->buf->i; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5186 | goto hdr_response_bad; |
| 5187 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5188 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5189 | /* read error */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5190 | else if (rep->flags & CF_READ_ERROR) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5191 | if (msg->err_pos >= 0) |
Emeric Brun | 8c1aaa2 | 2017-06-15 11:30:06 +0200 | [diff] [blame] | 5192 | 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] | 5193 | else if (txn->flags & TX_NOT_FIRST) |
| 5194 | goto abort_keep_alive; |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 5195 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 5196 | HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5197 | if (objt_server(s->target)) { |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 5198 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5199 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5200 | } |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 5201 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5202 | channel_auto_close(rep); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 5203 | rep->analysers &= AN_RES_FLT_END; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5204 | txn->status = 502; |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 5205 | |
| 5206 | /* Check to see if the server refused the early data. |
| 5207 | * If so, just send a 425 |
| 5208 | */ |
| 5209 | if (objt_cs(s->si[1].end)) { |
| 5210 | struct connection *conn = objt_cs(s->si[1].end)->conn; |
| 5211 | |
| 5212 | if (conn->err_code == CO_ER_SSL_EARLY_FAILED) |
| 5213 | txn->status = 425; |
| 5214 | } |
| 5215 | |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5216 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5217 | channel_truncate(rep); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 5218 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 5219 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5220 | if (!(s->flags & SF_ERR_MASK)) |
| 5221 | s->flags |= SF_ERR_SRVCL; |
| 5222 | if (!(s->flags & SF_FINST_MASK)) |
| 5223 | s->flags |= SF_FINST_H; |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 5224 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5225 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5226 | |
Willy Tarreau | 6f0a7ba | 2014-06-23 15:22:31 +0200 | [diff] [blame] | 5227 | /* read timeout : return a 504 to the client. */ |
| 5228 | else if (rep->flags & CF_READ_TIMEOUT) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5229 | if (msg->err_pos >= 0) |
Emeric Brun | 8c1aaa2 | 2017-06-15 11:30:06 +0200 | [diff] [blame] | 5230 | 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] | 5231 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 5232 | HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5233 | if (objt_server(s->target)) { |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 5234 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5235 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5236 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5237 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5238 | channel_auto_close(rep); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 5239 | rep->analysers &= AN_RES_FLT_END; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5240 | txn->status = 504; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5241 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5242 | channel_truncate(rep); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 5243 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 5244 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5245 | if (!(s->flags & SF_ERR_MASK)) |
| 5246 | s->flags |= SF_ERR_SRVTO; |
| 5247 | if (!(s->flags & SF_FINST_MASK)) |
| 5248 | s->flags |= SF_FINST_H; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5249 | return 0; |
| 5250 | } |
Willy Tarreau | a7c5276 | 2008-08-16 18:40:18 +0200 | [diff] [blame] | 5251 | |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 5252 | /* client abort with an abortonclose */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5253 | 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] | 5254 | HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1); |
| 5255 | HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1); |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 5256 | if (objt_server(s->target)) |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 5257 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1); |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 5258 | |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 5259 | rep->analysers &= AN_RES_FLT_END; |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 5260 | channel_auto_close(rep); |
| 5261 | |
| 5262 | txn->status = 400; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5263 | channel_truncate(rep); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 5264 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 5265 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5266 | if (!(s->flags & SF_ERR_MASK)) |
| 5267 | s->flags |= SF_ERR_CLICL; |
| 5268 | if (!(s->flags & SF_FINST_MASK)) |
| 5269 | s->flags |= SF_FINST_H; |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 5270 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5271 | /* process_stream() will take care of the error */ |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 5272 | return 0; |
| 5273 | } |
| 5274 | |
Willy Tarreau | 3b8c08a | 2011-09-02 20:16:24 +0200 | [diff] [blame] | 5275 | /* 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] | 5276 | else if (rep->flags & CF_SHUTR) { |
Willy Tarreau | 3b8c08a | 2011-09-02 20:16:24 +0200 | [diff] [blame] | 5277 | if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0) |
Emeric Brun | 8c1aaa2 | 2017-06-15 11:30:06 +0200 | [diff] [blame] | 5278 | 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] | 5279 | else if (txn->flags & TX_NOT_FIRST) |
| 5280 | goto abort_keep_alive; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5281 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 5282 | HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5283 | if (objt_server(s->target)) { |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 5284 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5285 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5286 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5287 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5288 | channel_auto_close(rep); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 5289 | rep->analysers &= AN_RES_FLT_END; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5290 | txn->status = 502; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5291 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5292 | channel_truncate(rep); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 5293 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5294 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5295 | if (!(s->flags & SF_ERR_MASK)) |
| 5296 | s->flags |= SF_ERR_SRVCL; |
| 5297 | if (!(s->flags & SF_FINST_MASK)) |
| 5298 | s->flags |= SF_FINST_H; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5299 | return 0; |
| 5300 | } |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 5301 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5302 | /* write error to client (we don't send any message then) */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5303 | else if (rep->flags & CF_WRITE_ERROR) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5304 | if (msg->err_pos >= 0) |
Emeric Brun | 8c1aaa2 | 2017-06-15 11:30:06 +0200 | [diff] [blame] | 5305 | 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] | 5306 | else if (txn->flags & TX_NOT_FIRST) |
| 5307 | goto abort_keep_alive; |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 5308 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 5309 | HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 5310 | rep->analysers &= AN_RES_FLT_END; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5311 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5312 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5313 | if (!(s->flags & SF_ERR_MASK)) |
| 5314 | s->flags |= SF_ERR_CLICL; |
| 5315 | if (!(s->flags & SF_FINST_MASK)) |
| 5316 | s->flags |= SF_FINST_H; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5317 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5318 | /* process_stream() will take care of the error */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5319 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5320 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5321 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5322 | channel_dont_close(rep); |
Willy Tarreau | 3f3997e | 2013-12-15 15:21:32 +0100 | [diff] [blame] | 5323 | rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5324 | return 0; |
| 5325 | } |
| 5326 | |
| 5327 | /* More interesting part now : we know that we have a complete |
| 5328 | * response which at least looks like HTTP. We have an indicator |
| 5329 | * of each header's length, so we can parse them quickly. |
| 5330 | */ |
| 5331 | |
| 5332 | if (unlikely(msg->err_pos >= 0)) |
Emeric Brun | 8c1aaa2 | 2017-06-15 11:30:06 +0200 | [diff] [blame] | 5333 | 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] | 5334 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5335 | /* |
| 5336 | * 1: get the status code |
| 5337 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5338 | n = rep->buf->p[msg->sl.st.c] - '0'; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5339 | if (n < 1 || n > 5) |
| 5340 | n = 0; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5341 | /* when the client triggers a 4xx from the server, it's most often due |
| 5342 | * to a missing object or permission. These events should be tracked |
| 5343 | * because if they happen often, it may indicate a brute force or a |
| 5344 | * vulnerability scan. |
| 5345 | */ |
| 5346 | if (n == 4) |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5347 | stream_inc_http_err_ctr(s); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5348 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5349 | if (objt_server(s->target)) |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 5350 | 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] | 5351 | |
Willy Tarreau | 91852eb | 2015-05-01 13:26:00 +0200 | [diff] [blame] | 5352 | /* RFC7230#2.6 has enforced the format of the HTTP version string to be |
| 5353 | * exactly one digit "." one digit. This check may be disabled using |
| 5354 | * option accept-invalid-http-response. |
| 5355 | */ |
| 5356 | if (!(s->be->options2 & PR_O2_RSPBUG_OK)) { |
| 5357 | if (msg->sl.st.v_l != 8) { |
| 5358 | msg->err_pos = 0; |
| 5359 | goto hdr_response_bad; |
| 5360 | } |
| 5361 | |
| 5362 | if (rep->buf->p[4] != '/' || |
| 5363 | !isdigit((unsigned char)rep->buf->p[5]) || |
| 5364 | rep->buf->p[6] != '.' || |
| 5365 | !isdigit((unsigned char)rep->buf->p[7])) { |
| 5366 | msg->err_pos = 4; |
| 5367 | goto hdr_response_bad; |
| 5368 | } |
| 5369 | } |
| 5370 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5371 | /* check if the response is HTTP/1.1 or above */ |
| 5372 | if ((msg->sl.st.v_l == 8) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5373 | ((rep->buf->p[5] > '1') || |
| 5374 | ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1')))) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5375 | msg->flags |= HTTP_MSGF_VER_11; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5376 | |
| 5377 | /* "connection" has not been parsed yet */ |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 5378 | 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] | 5379 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5380 | /* transfer length unknown*/ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5381 | msg->flags &= ~HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5382 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5383 | 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] | 5384 | |
Willy Tarreau | 3965040 | 2010-03-15 19:44:39 +0100 | [diff] [blame] | 5385 | /* Adjust server's health based on status code. Note: status codes 501 |
| 5386 | * and 505 are triggered on demand by client request, so we must not |
| 5387 | * count them as server failures. |
| 5388 | */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5389 | if (objt_server(s->target)) { |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 5390 | 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] | 5391 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK); |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 5392 | else |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5393 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS); |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 5394 | } |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5395 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5396 | /* |
Willy Tarreau | a14ad72 | 2017-07-07 11:36:32 +0200 | [diff] [blame] | 5397 | * We may be facing a 100-continue response, or any other informational |
| 5398 | * 1xx response which is non-final, in which case this is not the right |
| 5399 | * response, and we're waiting for the next one. Let's allow this response |
| 5400 | * to go to the client and wait for the next one. There's an exception for |
| 5401 | * 101 which is used later in the code to switch protocols. |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5402 | */ |
Willy Tarreau | a14ad72 | 2017-07-07 11:36:32 +0200 | [diff] [blame] | 5403 | if (txn->status < 200 && |
| 5404 | (txn->status == 100 || txn->status >= 102)) { |
Willy Tarreau | 628c40c | 2014-04-24 19:11:26 +0200 | [diff] [blame] | 5405 | hdr_idx_init(&txn->hdr_idx); |
| 5406 | msg->next -= channel_forward(rep, msg->next); |
| 5407 | msg->msg_state = HTTP_MSG_RPBEFORE; |
| 5408 | txn->status = 0; |
| 5409 | s->logs.t_data = -1; /* was not a response yet */ |
Christopher Faulet | 3e34429 | 2015-11-24 16:24:13 +0100 | [diff] [blame] | 5410 | FLT_STRM_CB(s, flt_http_reset(s, msg)); |
Willy Tarreau | 628c40c | 2014-04-24 19:11:26 +0200 | [diff] [blame] | 5411 | goto next_one; |
Willy Tarreau | a14ad72 | 2017-07-07 11:36:32 +0200 | [diff] [blame] | 5412 | } |
Willy Tarreau | 628c40c | 2014-04-24 19:11:26 +0200 | [diff] [blame] | 5413 | |
Willy Tarreau | a14ad72 | 2017-07-07 11:36:32 +0200 | [diff] [blame] | 5414 | /* |
| 5415 | * 2: check for cacheability. |
| 5416 | */ |
| 5417 | |
| 5418 | switch (txn->status) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5419 | case 200: |
| 5420 | case 203: |
Willy Tarreau | c55ddce | 2017-12-21 11:41:38 +0100 | [diff] [blame] | 5421 | case 204: |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5422 | case 206: |
| 5423 | case 300: |
| 5424 | case 301: |
Willy Tarreau | c55ddce | 2017-12-21 11:41:38 +0100 | [diff] [blame] | 5425 | case 404: |
| 5426 | case 405: |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5427 | case 410: |
Willy Tarreau | c55ddce | 2017-12-21 11:41:38 +0100 | [diff] [blame] | 5428 | case 414: |
| 5429 | case 501: |
Willy Tarreau | 83ece46 | 2017-12-21 15:13:09 +0100 | [diff] [blame] | 5430 | break; |
| 5431 | default: |
Willy Tarreau | c55ddce | 2017-12-21 11:41:38 +0100 | [diff] [blame] | 5432 | /* RFC7231#6.1: |
| 5433 | * Responses with status codes that are defined as |
| 5434 | * cacheable by default (e.g., 200, 203, 204, 206, |
| 5435 | * 300, 301, 404, 405, 410, 414, and 501 in this |
| 5436 | * specification) can be reused by a cache with |
| 5437 | * heuristic expiration unless otherwise indicated |
| 5438 | * by the method definition or explicit cache |
| 5439 | * controls [RFC7234]; all other status codes are |
| 5440 | * not cacheable by default. |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5441 | */ |
Willy Tarreau | 83ece46 | 2017-12-21 15:13:09 +0100 | [diff] [blame] | 5442 | txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5443 | break; |
| 5444 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5445 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5446 | /* |
| 5447 | * 3: we may need to capture headers |
| 5448 | */ |
| 5449 | s->logs.logwait &= ~LW_RESP; |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 5450 | if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap)) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5451 | capture_headers(rep->buf->p, &txn->hdr_idx, |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 5452 | s->res_cap, sess->fe->rsp_cap); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5453 | |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 5454 | /* 4: determine the transfer-length according to RFC2616 #4.4, updated |
| 5455 | * by RFC7230#3.3.3 : |
| 5456 | * |
| 5457 | * The length of a message body is determined by one of the following |
| 5458 | * (in order of precedence): |
| 5459 | * |
Christopher Faulet | d1cd209 | 2016-11-28 10:14:03 +0100 | [diff] [blame] | 5460 | * 1. Any 2xx (Successful) response to a CONNECT request implies that |
| 5461 | * the connection will become a tunnel immediately after the empty |
| 5462 | * line that concludes the header fields. A client MUST ignore |
| 5463 | * any Content-Length or Transfer-Encoding header fields received |
| 5464 | * in such a message. Any 101 response (Switching Protocols) is |
| 5465 | * managed in the same manner. |
| 5466 | * |
| 5467 | * 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] | 5468 | * (Informational), 204 (No Content), or 304 (Not Modified) status |
| 5469 | * code is always terminated by the first empty line after the |
| 5470 | * header fields, regardless of the header fields present in the |
| 5471 | * message, and thus cannot contain a message body. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5472 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 5473 | * 3. If a Transfer-Encoding header field is present and the chunked |
| 5474 | * transfer coding (Section 4.1) is the final encoding, the message |
| 5475 | * body length is determined by reading and decoding the chunked |
| 5476 | * data until the transfer coding indicates the data is complete. |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5477 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 5478 | * If a Transfer-Encoding header field is present in a response and |
| 5479 | * the chunked transfer coding is not the final encoding, the |
| 5480 | * message body length is determined by reading the connection until |
| 5481 | * it is closed by the server. If a Transfer-Encoding header field |
| 5482 | * is present in a request and the chunked transfer coding is not |
| 5483 | * the final encoding, the message body length cannot be determined |
| 5484 | * reliably; the server MUST respond with the 400 (Bad Request) |
| 5485 | * status code and then close the connection. |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5486 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 5487 | * If a message is received with both a Transfer-Encoding and a |
| 5488 | * Content-Length header field, the Transfer-Encoding overrides the |
| 5489 | * Content-Length. Such a message might indicate an attempt to |
| 5490 | * perform request smuggling (Section 9.5) or response splitting |
| 5491 | * (Section 9.4) and ought to be handled as an error. A sender MUST |
| 5492 | * remove the received Content-Length field prior to forwarding such |
| 5493 | * a message downstream. |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5494 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 5495 | * 4. If a message is received without Transfer-Encoding and with |
| 5496 | * either multiple Content-Length header fields having differing |
| 5497 | * field-values or a single Content-Length header field having an |
| 5498 | * invalid value, then the message framing is invalid and the |
| 5499 | * recipient MUST treat it as an unrecoverable error. If this is a |
| 5500 | * request message, the server MUST respond with a 400 (Bad Request) |
| 5501 | * status code and then close the connection. If this is a response |
| 5502 | * message received by a proxy, the proxy MUST close the connection |
| 5503 | * to the server, discard the received response, and send a 502 (Bad |
| 5504 | * Gateway) response to the client. If this is a response message |
| 5505 | * received by a user agent, the user agent MUST close the |
| 5506 | * connection to the server and discard the received response. |
| 5507 | * |
| 5508 | * 5. If a valid Content-Length header field is present without |
| 5509 | * Transfer-Encoding, its decimal value defines the expected message |
| 5510 | * body length in octets. If the sender closes the connection or |
| 5511 | * the recipient times out before the indicated number of octets are |
| 5512 | * received, the recipient MUST consider the message to be |
| 5513 | * incomplete and close the connection. |
| 5514 | * |
| 5515 | * 6. If this is a request message and none of the above are true, then |
| 5516 | * the message body length is zero (no message body is present). |
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 | * 7. Otherwise, this is a response message without a declared message |
| 5519 | * body length, so the message body length is determined by the |
| 5520 | * number of octets received prior to the server closing the |
| 5521 | * connection. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5522 | */ |
| 5523 | |
| 5524 | /* Skip parsing if no content length is possible. The response flags |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 5525 | * 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] | 5526 | * 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] | 5527 | * FIXME: should we parse anyway and return an error on chunked encoding ? |
| 5528 | */ |
Christopher Faulet | d1cd209 | 2016-11-28 10:14:03 +0100 | [diff] [blame] | 5529 | if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || |
| 5530 | txn->status == 101)) { |
| 5531 | /* Either we've established an explicit tunnel, or we're |
| 5532 | * switching the protocol. In both cases, we're very unlikely |
| 5533 | * to understand the next protocols. We have to switch to tunnel |
| 5534 | * mode, so that we transfer the request and responses then let |
| 5535 | * this protocol pass unmodified. When we later implement specific |
| 5536 | * parsers for such protocols, we'll want to check the Upgrade |
| 5537 | * header which contains information about that protocol for |
| 5538 | * responses with status 101 (eg: see RFC2817 about TLS). |
| 5539 | */ |
| 5540 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN; |
| 5541 | msg->flags |= HTTP_MSGF_XFER_LEN; |
| 5542 | goto end; |
| 5543 | } |
| 5544 | |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5545 | if (txn->meth == HTTP_METH_HEAD || |
| 5546 | (txn->status >= 100 && txn->status < 200) || |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5547 | txn->status == 204 || txn->status == 304) { |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5548 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5549 | goto skip_content_length; |
| 5550 | } |
| 5551 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5552 | use_close_only = 0; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5553 | ctx.idx = 0; |
Willy Tarreau | 4979d5c | 2015-05-01 10:06:30 +0200 | [diff] [blame] | 5554 | 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] | 5555 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5556 | msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
| 5557 | else if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5558 | /* bad transfer-encoding (chunked followed by something else) */ |
| 5559 | use_close_only = 1; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5560 | msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5561 | break; |
| 5562 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5563 | } |
| 5564 | |
Willy Tarreau | 1c91391 | 2015-04-30 10:57:51 +0200 | [diff] [blame] | 5565 | /* Chunked responses must have their content-length removed */ |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5566 | ctx.idx = 0; |
Willy Tarreau | b4d0c03 | 2015-05-01 10:25:45 +0200 | [diff] [blame] | 5567 | if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) { |
Willy Tarreau | 1c91391 | 2015-04-30 10:57:51 +0200 | [diff] [blame] | 5568 | while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) |
| 5569 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 5570 | } |
Willy Tarreau | b4d0c03 | 2015-05-01 10:25:45 +0200 | [diff] [blame] | 5571 | 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] | 5572 | signed long long cl; |
| 5573 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5574 | if (!ctx.vlen) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5575 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5576 | goto hdr_response_bad; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5577 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5578 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5579 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5580 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5581 | goto hdr_response_bad; /* parse failure */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5582 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5583 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5584 | if (cl < 0) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5585 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5586 | goto hdr_response_bad; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5587 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5588 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5589 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5590 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5591 | goto hdr_response_bad; /* already specified, was different */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5592 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5593 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5594 | msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 5595 | msg->body_len = msg->chunk_len = cl; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5596 | } |
| 5597 | |
Christopher Faulet | d1cd209 | 2016-11-28 10:14:03 +0100 | [diff] [blame] | 5598 | skip_content_length: |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5599 | /* Now we have to check if we need to modify the Connection header. |
| 5600 | * This is more difficult on the response than it is on the request, |
| 5601 | * because we can have two different HTTP versions and we don't know |
| 5602 | * how the client will interprete a response. For instance, let's say |
| 5603 | * that the client sends a keep-alive request in HTTP/1.0 and gets an |
| 5604 | * HTTP/1.1 response without any header. Maybe it will bound itself to |
| 5605 | * HTTP/1.0 because it only knows about it, and will consider the lack |
| 5606 | * of header as a close, or maybe it knows HTTP/1.1 and can consider |
| 5607 | * the lack of header as a keep-alive. Thus we will use two flags |
| 5608 | * indicating how a request MAY be understood by the client. In case |
| 5609 | * of multiple possibilities, we'll fix the header to be explicit. If |
| 5610 | * ambiguous cases such as both close and keepalive are seen, then we |
| 5611 | * will fall back to explicit close. Note that we won't take risks with |
| 5612 | * HTTP/1.0 clients which may not necessarily understand keep-alive. |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5613 | * See doc/internals/connection-header.txt for the complete matrix. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5614 | */ |
Christopher Faulet | d1cd209 | 2016-11-28 10:14:03 +0100 | [diff] [blame] | 5615 | if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) && |
| 5616 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN || |
| 5617 | ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
| 5618 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) { |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5619 | int to_del = 0; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5620 | |
Willy Tarreau | 70dffda | 2014-01-30 03:07:23 +0100 | [diff] [blame] | 5621 | /* this situation happens when combining pretend-keepalive with httpclose. */ |
| 5622 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5623 | ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 5624 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) |
Willy Tarreau | 70dffda | 2014-01-30 03:07:23 +0100 | [diff] [blame] | 5625 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 5626 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5627 | /* on unknown transfer length, we must close */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5628 | if (!(msg->flags & HTTP_MSGF_XFER_LEN) && |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5629 | (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) |
| 5630 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5631 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5632 | /* now adjust header transformations depending on current state */ |
| 5633 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN || |
| 5634 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 5635 | to_del |= 2; /* remove "keep-alive" on any response */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5636 | if (!(msg->flags & HTTP_MSGF_VER_11)) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5637 | to_del |= 1; /* remove "close" for HTTP/1.0 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5638 | } |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5639 | else { /* SCL / KAL */ |
| 5640 | to_del |= 1; /* remove "close" on any response */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5641 | if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5642 | to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5643 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5644 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5645 | /* Parse and remove some headers from the connection header */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 5646 | http_parse_connection_header(txn, msg, to_del); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5647 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5648 | /* Some keep-alive responses are converted to Server-close if |
| 5649 | * the server wants to close. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5650 | */ |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5651 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) { |
| 5652 | if ((txn->flags & TX_HDR_CONN_CLO) || |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5653 | (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11))) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5654 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 5655 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5656 | } |
| 5657 | |
Christopher Faulet | d1cd209 | 2016-11-28 10:14:03 +0100 | [diff] [blame] | 5658 | end: |
Willy Tarreau | 7959a55 | 2013-09-23 16:44:27 +0200 | [diff] [blame] | 5659 | /* we want to have the response time before we start processing it */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 5660 | s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now); |
Willy Tarreau | 7959a55 | 2013-09-23 16:44:27 +0200 | [diff] [blame] | 5661 | |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 5662 | /* end of job, return OK */ |
| 5663 | rep->analysers &= ~an_bit; |
| 5664 | rep->analyse_exp = TICK_ETERNITY; |
| 5665 | channel_auto_close(rep); |
| 5666 | return 1; |
| 5667 | |
| 5668 | abort_keep_alive: |
| 5669 | /* A keep-alive request to the server failed on a network error. |
| 5670 | * The client is required to retry. We need to close without returning |
| 5671 | * any other information so that the client retries. |
| 5672 | */ |
| 5673 | txn->status = 0; |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 5674 | rep->analysers &= AN_RES_FLT_END; |
| 5675 | s->req.analysers &= AN_REQ_FLT_END; |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 5676 | channel_auto_close(rep); |
| 5677 | s->logs.logwait = 0; |
| 5678 | s->logs.level = 0; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5679 | s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5680 | channel_truncate(rep); |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 5681 | http_reply_and_close(s, txn->status, NULL); |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 5682 | return 0; |
| 5683 | } |
| 5684 | |
| 5685 | /* This function performs all the processing enabled for the current response. |
| 5686 | * 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] | 5687 | * 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] | 5688 | * other functions. It works like process_request (see indications above). |
| 5689 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5690 | 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] | 5691 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 5692 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5693 | struct http_txn *txn = s->txn; |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 5694 | struct http_msg *msg = &txn->rsp; |
| 5695 | struct proxy *cur_proxy; |
| 5696 | struct cond_wordlist *wl; |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 5697 | enum rule_result ret = HTTP_RULE_RES_CONT; |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 5698 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5699 | 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] | 5700 | now_ms, __FUNCTION__, |
| 5701 | s, |
| 5702 | rep, |
| 5703 | rep->rex, rep->wex, |
| 5704 | rep->flags, |
| 5705 | rep->buf->i, |
| 5706 | rep->analysers); |
| 5707 | |
| 5708 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */ |
| 5709 | return 0; |
| 5710 | |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 5711 | /* The stats applet needs to adjust the Connection header but we don't |
| 5712 | * apply any filter there. |
| 5713 | */ |
Willy Tarreau | 612adb8 | 2015-03-10 15:25:54 +0100 | [diff] [blame] | 5714 | if (unlikely(objt_applet(s->target) == &http_stats_applet)) { |
| 5715 | rep->analysers &= ~an_bit; |
| 5716 | rep->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 5717 | goto skip_filters; |
Willy Tarreau | 612adb8 | 2015-03-10 15:25:54 +0100 | [diff] [blame] | 5718 | } |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 5719 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5720 | /* |
| 5721 | * We will have to evaluate the filters. |
| 5722 | * As opposed to version 1.2, now they will be evaluated in the |
| 5723 | * filters order and not in the header order. This means that |
| 5724 | * each filter has to be validated among all headers. |
| 5725 | * |
| 5726 | * Filters are tried with ->be first, then with ->fe if it is |
| 5727 | * different from ->be. |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 5728 | * |
| 5729 | * Maybe we are in resume condiion. In this case I choose the |
| 5730 | * "struct proxy" which contains the rule list matching the resume |
| 5731 | * pointer. If none of theses "struct proxy" match, I initialise |
| 5732 | * the process with the first one. |
| 5733 | * |
| 5734 | * In fact, I check only correspondance betwwen the current list |
| 5735 | * pointer and the ->fe rule list. If it doesn't match, I initialize |
| 5736 | * the loop with the ->be. |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5737 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5738 | if (s->current_rule_list == &sess->fe->http_res_rules) |
| 5739 | cur_proxy = sess->fe; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 5740 | else |
| 5741 | cur_proxy = s->be; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5742 | while (1) { |
| 5743 | struct proxy *rule_set = cur_proxy; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5744 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5745 | /* evaluate http-response rules */ |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 5746 | if (ret == HTTP_RULE_RES_CONT) { |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 5747 | 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] | 5748 | |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 5749 | if (ret == HTTP_RULE_RES_BADREQ) |
| 5750 | goto return_srv_prx_502; |
| 5751 | |
| 5752 | if (ret == HTTP_RULE_RES_DONE) { |
| 5753 | rep->analysers &= ~an_bit; |
| 5754 | rep->analyse_exp = TICK_ETERNITY; |
| 5755 | return 1; |
| 5756 | } |
| 5757 | } |
| 5758 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 5759 | /* we need to be called again. */ |
| 5760 | if (ret == HTTP_RULE_RES_YIELD) { |
| 5761 | channel_dont_close(rep); |
| 5762 | return 0; |
| 5763 | } |
| 5764 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5765 | /* try headers filters */ |
| 5766 | if (rule_set->rsp_exp != NULL) { |
| 5767 | if (apply_filters_to_response(s, rep, rule_set) < 0) { |
| 5768 | return_bad_resp: |
| 5769 | if (objt_server(s->target)) { |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 5770 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1); |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5771 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5772 | } |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 5773 | HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1); |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5774 | return_srv_prx_502: |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 5775 | rep->analysers &= AN_RES_FLT_END; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5776 | txn->status = 502; |
| 5777 | s->logs.t_data = -1; /* was not a valid response */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5778 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5779 | channel_truncate(rep); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 5780 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5781 | if (!(s->flags & SF_ERR_MASK)) |
| 5782 | s->flags |= SF_ERR_PRXCOND; |
| 5783 | if (!(s->flags & SF_FINST_MASK)) |
| 5784 | s->flags |= SF_FINST_H; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5785 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5786 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5787 | } |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5788 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5789 | /* has the response been denied ? */ |
| 5790 | if (txn->flags & TX_SVDENY) { |
| 5791 | if (objt_server(s->target)) |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 5792 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5793 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 5794 | HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1); |
| 5795 | HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 5796 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 5797 | HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5798 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5799 | goto return_srv_prx_502; |
| 5800 | } |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 5801 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5802 | /* add response headers from the rule sets in the same order */ |
| 5803 | list_for_each_entry(wl, &rule_set->rsp_add, list) { |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 5804 | if (txn->status < 200 && txn->status != 101) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5805 | break; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5806 | if (wl->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 5807 | 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] | 5808 | ret = acl_pass(ret); |
| 5809 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 5810 | ret = !ret; |
| 5811 | if (!ret) |
| 5812 | continue; |
| 5813 | } |
| 5814 | if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0)) |
| 5815 | goto return_bad_resp; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5816 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5817 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5818 | /* check whether we're already working on the frontend */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5819 | if (cur_proxy == sess->fe) |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5820 | break; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5821 | cur_proxy = sess->fe; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5822 | } |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5823 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 5824 | /* After this point, this anayzer can't return yield, so we can |
| 5825 | * remove the bit corresponding to this analyzer from the list. |
| 5826 | * |
| 5827 | * Note that the intermediate returns and goto found previously |
| 5828 | * reset the analyzers. |
| 5829 | */ |
| 5830 | rep->analysers &= ~an_bit; |
| 5831 | rep->analyse_exp = TICK_ETERNITY; |
| 5832 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5833 | /* OK that's all we can do for 1xx responses */ |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 5834 | if (unlikely(txn->status < 200 && txn->status != 101)) |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5835 | goto skip_header_mangling; |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5836 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5837 | /* |
| 5838 | * Now check for a server cookie. |
| 5839 | */ |
Willy Tarreau | 53a09d5 | 2015-08-10 18:59:40 +0200 | [diff] [blame] | 5840 | 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] | 5841 | manage_server_side_cookies(s, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5842 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5843 | /* |
| 5844 | * Check for cache-control or pragma headers if required. |
| 5845 | */ |
Willy Tarreau | 12b32f2 | 2017-12-21 16:08:09 +0100 | [diff] [blame] | 5846 | 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] | 5847 | check_response_for_cacheability(s, rep); |
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 | /* |
| 5850 | * Add server cookie in the response if needed |
| 5851 | */ |
| 5852 | if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) && |
| 5853 | !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) && |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5854 | (!(s->flags & SF_DIRECT) || |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5855 | ((s->be->cookie_maxidle || txn->cookie_last_date) && |
| 5856 | (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) || |
| 5857 | (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date |
| 5858 | (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date |
| 5859 | (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) && |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5860 | !(s->flags & SF_IGNORE_PRST)) { |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5861 | /* the server is known, it's not the one the client requested, or the |
| 5862 | * cookie's last seen date needs to be refreshed. We have to |
| 5863 | * insert a set-cookie here, except if we want to insert only on POST |
| 5864 | * requests and this one isn't. Note that servers which don't have cookies |
| 5865 | * (eg: some backup servers) will return a full cookie removal request. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5866 | */ |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5867 | if (!objt_server(s->target)->cookie) { |
| 5868 | chunk_printf(&trash, |
| 5869 | "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/", |
| 5870 | s->be->cookie_name); |
| 5871 | } |
| 5872 | else { |
| 5873 | 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] | 5874 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5875 | if (s->be->cookie_maxidle || s->be->cookie_maxlife) { |
| 5876 | /* emit last_date, which is mandatory */ |
| 5877 | trash.str[trash.len++] = COOKIE_DELIM_DATE; |
| 5878 | s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len); |
| 5879 | trash.len += 5; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5880 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5881 | if (s->be->cookie_maxlife) { |
| 5882 | /* emit first_date, which is either the original one or |
| 5883 | * the current date. |
| 5884 | */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 5885 | trash.str[trash.len++] = COOKIE_DELIM_DATE; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5886 | s30tob64(txn->cookie_first_date ? |
| 5887 | txn->cookie_first_date >> 2 : |
| 5888 | (date.tv_sec+3) >> 2, trash.str + trash.len); |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 5889 | trash.len += 5; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5890 | } |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5891 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5892 | chunk_appendf(&trash, "; path=/"); |
| 5893 | } |
Willy Tarreau | 4992dd2 | 2012-05-31 21:02:17 +0200 | [diff] [blame] | 5894 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5895 | if (s->be->cookie_domain) |
| 5896 | chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain); |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5897 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5898 | if (s->be->ck_opts & PR_CK_HTTPONLY) |
| 5899 | chunk_appendf(&trash, "; HttpOnly"); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5900 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5901 | if (s->be->ck_opts & PR_CK_SECURE) |
| 5902 | chunk_appendf(&trash, "; Secure"); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5903 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5904 | if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0)) |
| 5905 | goto return_bad_resp; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5906 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5907 | txn->flags &= ~TX_SCK_MASK; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5908 | if (objt_server(s->target)->cookie && (s->flags & SF_DIRECT)) |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5909 | /* the server did not change, only the date was updated */ |
| 5910 | txn->flags |= TX_SCK_UPDATED; |
| 5911 | else |
| 5912 | txn->flags |= TX_SCK_INSERTED; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5913 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5914 | /* Here, we will tell an eventual cache on the client side that we don't |
| 5915 | * want it to cache this reply because HTTP/1.0 caches also cache cookies ! |
| 5916 | * Some caches understand the correct form: 'no-cache="set-cookie"', but |
| 5917 | * 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] | 5918 | */ |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5919 | if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5920 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5921 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5922 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5923 | if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, |
| 5924 | "Cache-control: private", 22) < 0)) |
| 5925 | goto return_bad_resp; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5926 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5927 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5928 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5929 | /* |
| 5930 | * Check if result will be cacheable with a cookie. |
| 5931 | * We'll block the response if security checks have caught |
| 5932 | * nasty things such as a cacheable cookie. |
| 5933 | */ |
| 5934 | if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) == |
| 5935 | (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) && |
| 5936 | (s->be->options & PR_O_CHK_CACHE)) { |
| 5937 | /* we're in presence of a cacheable response containing |
| 5938 | * a set-cookie header. We'll block it as requested by |
| 5939 | * the 'checkcache' option, and send an alert. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5940 | */ |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5941 | if (objt_server(s->target)) |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 5942 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1); |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5943 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 5944 | HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1); |
| 5945 | HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 5946 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 5947 | HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5948 | |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5949 | ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 5950 | 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] | 5951 | send_log(s->be, LOG_ALERT, |
| 5952 | "Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 5953 | s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>"); |
| 5954 | goto return_srv_prx_502; |
| 5955 | } |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 5956 | |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 5957 | skip_filters: |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5958 | /* |
| 5959 | * Adjust "Connection: close" or "Connection: keep-alive" if needed. |
| 5960 | * If an "Upgrade" token is found, the header is left untouched in order |
| 5961 | * 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] | 5962 | * if anything but "Upgrade" is present in the Connection header. We don't |
| 5963 | * want to touch any 101 response either since it's switching to another |
| 5964 | * protocol. |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5965 | */ |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 5966 | if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) && |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5967 | (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5968 | ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5969 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) { |
| 5970 | unsigned int want_flags = 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5971 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5972 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 5973 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 5974 | /* we want a keep-alive response here. Keep-alive header |
| 5975 | * required if either side is not 1.1. |
| 5976 | */ |
| 5977 | if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11)) |
| 5978 | want_flags |= TX_CON_KAL_SET; |
| 5979 | } |
| 5980 | else { |
| 5981 | /* we want a close response here. Close header required if |
| 5982 | * the server is 1.1, regardless of the client. |
| 5983 | */ |
| 5984 | if (msg->flags & HTTP_MSGF_VER_11) |
| 5985 | want_flags |= TX_CON_CLO_SET; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5986 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5987 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5988 | if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 5989 | http_change_connection_header(txn, msg, want_flags); |
| 5990 | } |
| 5991 | |
| 5992 | skip_header_mangling: |
Christopher Faulet | 69744d9 | 2017-03-30 10:54:35 +0200 | [diff] [blame] | 5993 | /* Always enter in the body analyzer */ |
| 5994 | rep->analysers &= ~AN_RES_FLT_XFER_DATA; |
| 5995 | rep->analysers |= AN_RES_HTTP_XFER_BODY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5996 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5997 | /* if the user wants to log as soon as possible, without counting |
| 5998 | * bytes from the server, then this is the right moment. We have |
| 5999 | * to temporarily assign bytes_out to log what we currently have. |
| 6000 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6001 | if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) { |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6002 | s->logs.t_close = s->logs.t_data; /* to get a valid end date */ |
| 6003 | s->logs.bytes_out = txn->rsp.eoh; |
| 6004 | s->do_log(s); |
| 6005 | s->logs.bytes_out = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6006 | } |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 6007 | return 1; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6008 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6009 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6010 | /* This function is an analyser which forwards response body (including chunk |
| 6011 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 6012 | * zero byte. The only situation where it must not be called is when we're in |
| 6013 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 6014 | * remaining data and to resync after end of body. It expects the msg_state to |
| 6015 | * 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] | 6016 | * 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] | 6017 | * |
| 6018 | * It is capable of compressing response data both in content-length mode and |
| 6019 | * in chunked mode. The state machines follows different flows depending on |
| 6020 | * whether content-length and chunked modes are used, since there are no |
| 6021 | * trailers in content-length : |
| 6022 | * |
| 6023 | * chk-mode cl-mode |
| 6024 | * ,----- BODY -----. |
| 6025 | * / \ |
| 6026 | * V size > 0 V chk-mode |
| 6027 | * .--> SIZE -------------> DATA -------------> CRLF |
| 6028 | * | | size == 0 | last byte | |
| 6029 | * | v final crlf v inspected | |
| 6030 | * | TRAILERS -----------> DONE | |
| 6031 | * | | |
| 6032 | * `----------------------------------------------' |
| 6033 | * |
| 6034 | * Compression only happens in the DATA state, and must be flushed in final |
| 6035 | * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding |
| 6036 | * is performed at once on final states for all bytes parsed, or when leaving |
| 6037 | * on missing data. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6038 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6039 | 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] | 6040 | { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6041 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 6042 | struct http_txn *txn = s->txn; |
| 6043 | struct http_msg *msg = &s->txn->rsp; |
Christopher Faulet | 3e34429 | 2015-11-24 16:24:13 +0100 | [diff] [blame] | 6044 | int ret; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6045 | |
Christopher Faulet | 814d270 | 2017-03-30 11:33:44 +0200 | [diff] [blame] | 6046 | DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n", |
| 6047 | now_ms, __FUNCTION__, |
| 6048 | s, |
| 6049 | res, |
| 6050 | res->rex, res->wex, |
| 6051 | res->flags, |
| 6052 | res->buf->i, |
| 6053 | res->analysers); |
| 6054 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 6055 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 6056 | return 0; |
| 6057 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 6058 | 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] | 6059 | ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) || |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 6060 | !s->req.analysers) { |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 6061 | /* Output closed while we were sending data. We must abort and |
| 6062 | * wake the other side up. |
| 6063 | */ |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 6064 | msg->err_state = msg->msg_state; |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 6065 | msg->msg_state = HTTP_MSG_ERROR; |
| 6066 | http_resync_states(s); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 6067 | return 1; |
| 6068 | } |
| 6069 | |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 6070 | /* in most states, we should abort in case of early close */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6071 | channel_auto_close(res); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 6072 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 6073 | if (msg->msg_state == HTTP_MSG_BODY) { |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6074 | msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK) |
| 6075 | ? HTTP_MSG_CHUNK_SIZE |
| 6076 | : HTTP_MSG_DATA); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6077 | } |
| 6078 | |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 6079 | if (res->to_forward) { |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6080 | /* We can't process the buffer's contents yet */ |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 6081 | res->flags |= CF_WAKE_WRITE; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6082 | goto missing_data_or_waiting; |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 6083 | } |
| 6084 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6085 | if (msg->msg_state < HTTP_MSG_DONE) { |
| 6086 | ret = ((msg->flags & HTTP_MSGF_TE_CHNK) |
| 6087 | ? http_msg_forward_chunked_body(s, msg) |
| 6088 | : http_msg_forward_body(s, msg)); |
| 6089 | if (!ret) |
| 6090 | goto missing_data_or_waiting; |
| 6091 | if (ret < 0) |
| 6092 | goto return_bad_res; |
| 6093 | } |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 6094 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6095 | /* other states, DONE...TUNNEL */ |
| 6096 | /* for keep-alive we don't want to forward closes on DONE */ |
| 6097 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 6098 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
| 6099 | channel_dont_close(res); |
Willy Tarreau | 3ce10ff | 2014-04-22 08:24:38 +0200 | [diff] [blame] | 6100 | |
Christopher Faulet | 894da4c | 2017-07-18 11:29:07 +0200 | [diff] [blame] | 6101 | http_resync_states(s); |
| 6102 | if (!(res->analysers & an_bit)) { |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6103 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 6104 | if (res->flags & CF_SHUTW) { |
| 6105 | /* response errors are most likely due to the |
| 6106 | * client aborting the transfer. */ |
| 6107 | goto aborted_xfer; |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 6108 | } |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6109 | if (msg->err_pos >= 0) |
Emeric Brun | 8c1aaa2 | 2017-06-15 11:30:06 +0200 | [diff] [blame] | 6110 | 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] | 6111 | goto return_bad_res; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6112 | } |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6113 | return 1; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6114 | } |
Willy Tarreau | f51d03c | 2016-05-02 15:25:15 +0200 | [diff] [blame] | 6115 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6116 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6117 | missing_data_or_waiting: |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6118 | if (res->flags & CF_SHUTW) |
| 6119 | goto aborted_xfer; |
| 6120 | |
| 6121 | /* stop waiting for data if the input is closed before the end. If the |
| 6122 | * client side was already closed, it means that the client has aborted, |
| 6123 | * so we don't want to count this as a server abort. Otherwise it's a |
| 6124 | * server abort. |
| 6125 | */ |
Christopher Faulet | a33510b | 2017-03-31 15:37:29 +0200 | [diff] [blame] | 6126 | if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6127 | if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW)) |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6128 | goto aborted_xfer; |
Christopher Faulet | a46bbd8 | 2015-06-19 09:00:58 +0200 | [diff] [blame] | 6129 | /* If we have some pending data, we continue the processing */ |
| 6130 | if (!buffer_pending(res->buf)) { |
| 6131 | if (!(s->flags & SF_ERR_MASK)) |
| 6132 | s->flags |= SF_ERR_SRVCL; |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 6133 | HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1); |
Christopher Faulet | a46bbd8 | 2015-06-19 09:00:58 +0200 | [diff] [blame] | 6134 | if (objt_server(s->target)) |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 6135 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1); |
Christopher Faulet | a46bbd8 | 2015-06-19 09:00:58 +0200 | [diff] [blame] | 6136 | goto return_bad_res_stats_ok; |
| 6137 | } |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 6138 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 6139 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 6140 | /* 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] | 6141 | if (!s->req.analysers) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 6142 | goto return_bad_res; |
| 6143 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6144 | /* When TE: chunked is used, we need to get there again to parse |
| 6145 | * 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] | 6146 | * set CF_DONTCLOSE. Similarly, if keep-alive is set on the client side |
| 6147 | * or if there are filters registered on the stream, we don't want to |
| 6148 | * forward a close |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 6149 | */ |
Christopher Faulet | 69744d9 | 2017-03-30 10:54:35 +0200 | [diff] [blame] | 6150 | if ((msg->flags & HTTP_MSGF_TE_CHNK) || |
Christopher Faulet | f1cc5d0 | 2017-02-08 09:45:13 +0100 | [diff] [blame] | 6151 | HAS_DATA_FILTERS(s, res) || |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 6152 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 6153 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6154 | channel_dont_close(res); |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 6155 | |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 6156 | /* 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] | 6157 | * 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] | 6158 | * 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] | 6159 | * modes are already handled by the stream sock layer. We must not do |
| 6160 | * this in content-length mode because it could present the MSG_MORE |
| 6161 | * flag with the last block of forwarded data, which would cause an |
| 6162 | * additional delay to be observed by the receiver. |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 6163 | */ |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 6164 | if ((msg->flags & HTTP_MSGF_TE_CHNK) || (msg->flags & HTTP_MSGF_COMPRESSING)) |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 6165 | res->flags |= CF_EXPECT_MORE; |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 6166 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6167 | /* the stream handler will take care of timeouts and errors */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6168 | return 0; |
| 6169 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 6170 | return_bad_res: /* let's centralize all bad responses */ |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 6171 | HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6172 | if (objt_server(s->target)) |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 6173 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6174 | |
| 6175 | return_bad_res_stats_ok: |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 6176 | txn->rsp.err_state = txn->rsp.msg_state; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6177 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 6178 | /* don't send any error message as we're in the body */ |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 6179 | http_reply_and_close(s, txn->status, NULL); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 6180 | res->analysers &= AN_RES_FLT_END; |
| 6181 | 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] | 6182 | if (objt_server(s->target)) |
| 6183 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6184 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6185 | if (!(s->flags & SF_ERR_MASK)) |
| 6186 | s->flags |= SF_ERR_PRXCOND; |
| 6187 | if (!(s->flags & SF_FINST_MASK)) |
| 6188 | s->flags |= SF_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6189 | return 0; |
| 6190 | |
| 6191 | aborted_xfer: |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 6192 | txn->rsp.err_state = txn->rsp.msg_state; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6193 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
| 6194 | /* don't send any error message as we're in the body */ |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 6195 | http_reply_and_close(s, txn->status, NULL); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 6196 | res->analysers &= AN_RES_FLT_END; |
| 6197 | 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] | 6198 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 6199 | HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1); |
| 6200 | HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6201 | if (objt_server(s->target)) |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 6202 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6203 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6204 | if (!(s->flags & SF_ERR_MASK)) |
| 6205 | s->flags |= SF_ERR_CLICL; |
| 6206 | if (!(s->flags & SF_FINST_MASK)) |
| 6207 | s->flags |= SF_FINST_D; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6208 | return 0; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6209 | } |
| 6210 | |
| 6211 | |
| 6212 | static inline int |
| 6213 | http_msg_forward_body(struct stream *s, struct http_msg *msg) |
| 6214 | { |
| 6215 | struct channel *chn = msg->chn; |
| 6216 | int ret; |
| 6217 | |
| 6218 | /* Here we have the guarantee to be in HTTP_MSG_DATA or HTTP_MSG_ENDING state */ |
| 6219 | |
| 6220 | if (msg->msg_state == HTTP_MSG_ENDING) |
| 6221 | goto ending; |
| 6222 | |
| 6223 | /* Neither content-length, nor transfer-encoding was found, so we must |
| 6224 | * read the body until the server connection is closed. In that case, we |
| 6225 | * eat data as they come. Of course, this happens for response only. */ |
| 6226 | if (!(msg->flags & HTTP_MSGF_XFER_LEN)) { |
| 6227 | unsigned long long len = (chn->buf->i - msg->next); |
| 6228 | msg->chunk_len += len; |
| 6229 | msg->body_len += len; |
| 6230 | } |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 6231 | ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg), |
| 6232 | /* default_ret */ MIN(msg->chunk_len, chn->buf->i - msg->next), |
| 6233 | /* on_error */ goto error); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6234 | msg->next += ret; |
| 6235 | msg->chunk_len -= ret; |
| 6236 | if (msg->chunk_len) { |
| 6237 | /* input empty or output full */ |
| 6238 | if (chn->buf->i > msg->next) |
| 6239 | chn->flags |= CF_WAKE_WRITE; |
| 6240 | goto missing_data_or_waiting; |
| 6241 | } |
| 6242 | |
Christopher Faulet | 1486b0a | 2017-07-18 11:42:08 +0200 | [diff] [blame] | 6243 | /* This check can only be true for a response. HTTP_MSGF_XFER_LEN is |
| 6244 | * always set for a request. */ |
| 6245 | if (!(msg->flags & HTTP_MSGF_XFER_LEN)) { |
| 6246 | /* The server still sending data that should be filtered */ |
| 6247 | if (!(chn->flags & CF_SHUTR) && HAS_DATA_FILTERS(s, chn)) |
| 6248 | goto missing_data_or_waiting; |
Christopher Faulet | fd04fcf | 2018-02-02 15:54:15 +0100 | [diff] [blame] | 6249 | msg->msg_state = HTTP_MSG_TUNNEL; |
| 6250 | goto ending; |
Christopher Faulet | 1486b0a | 2017-07-18 11:42:08 +0200 | [diff] [blame] | 6251 | } |
Christopher Faulet | f1cc5d0 | 2017-02-08 09:45:13 +0100 | [diff] [blame] | 6252 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6253 | msg->msg_state = HTTP_MSG_ENDING; |
| 6254 | |
| 6255 | ending: |
| 6256 | /* we may have some pending data starting at res->buf->p such as a last |
| 6257 | * chunk of data or trailers. */ |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 6258 | ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next), |
| 6259 | /* default_ret */ msg->next, |
| 6260 | /* on_error */ goto error); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6261 | b_adv(chn->buf, ret); |
| 6262 | msg->next -= ret; |
Willy Tarreau | 9962f8f | 2016-06-28 11:52:08 +0200 | [diff] [blame] | 6263 | if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)) |
| 6264 | msg->sov -= ret; |
Christopher Faulet | a9300a3 | 2016-06-28 15:54:44 +0200 | [diff] [blame] | 6265 | if (msg->next) |
| 6266 | goto waiting; |
Willy Tarreau | 9962f8f | 2016-06-28 11:52:08 +0200 | [diff] [blame] | 6267 | |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 6268 | FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg), |
| 6269 | /* default_ret */ 1, |
| 6270 | /* on_error */ goto error, |
| 6271 | /* on_wait */ goto waiting); |
Christopher Faulet | fd04fcf | 2018-02-02 15:54:15 +0100 | [diff] [blame] | 6272 | if (msg->msg_state == HTTP_MSG_ENDING) |
| 6273 | msg->msg_state = HTTP_MSG_DONE; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6274 | return 1; |
| 6275 | |
| 6276 | missing_data_or_waiting: |
| 6277 | /* we may have some pending data starting at chn->buf->p */ |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 6278 | ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next), |
| 6279 | /* default_ret */ msg->next, |
| 6280 | /* on_error */ goto error); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6281 | b_adv(chn->buf, ret); |
| 6282 | msg->next -= ret; |
| 6283 | if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0) |
| 6284 | msg->sov -= ret; |
Christopher Faulet | 75e2eb6 | 2015-12-15 10:41:47 +0100 | [diff] [blame] | 6285 | if (!HAS_DATA_FILTERS(s, chn)) |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6286 | msg->chunk_len -= channel_forward(chn, msg->chunk_len); |
Christopher Faulet | a9300a3 | 2016-06-28 15:54:44 +0200 | [diff] [blame] | 6287 | waiting: |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6288 | return 0; |
| 6289 | error: |
| 6290 | return -1; |
| 6291 | } |
| 6292 | |
| 6293 | static inline int |
| 6294 | http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg) |
| 6295 | { |
| 6296 | struct channel *chn = msg->chn; |
Willy Tarreau | e56cdd3 | 2017-09-21 08:36:33 +0200 | [diff] [blame] | 6297 | unsigned int chunk; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6298 | int ret; |
| 6299 | |
| 6300 | /* Here we have the guarantee to be in one of the following state: |
| 6301 | * HTTP_MSG_DATA, HTTP_MSG_CHUNK_SIZE, HTTP_MSG_CHUNK_CRLF, |
| 6302 | * HTTP_MSG_TRAILERS or HTTP_MSG_ENDING. */ |
| 6303 | |
| 6304 | switch_states: |
| 6305 | switch (msg->msg_state) { |
| 6306 | case HTTP_MSG_DATA: |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 6307 | ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg), |
| 6308 | /* default_ret */ MIN(msg->chunk_len, chn->buf->i - msg->next), |
| 6309 | /* on_error */ goto error); |
| 6310 | msg->next += ret; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6311 | msg->chunk_len -= ret; |
| 6312 | if (msg->chunk_len) { |
| 6313 | /* input empty or output full */ |
| 6314 | if (chn->buf->i > msg->next) |
| 6315 | chn->flags |= CF_WAKE_WRITE; |
| 6316 | goto missing_data_or_waiting; |
| 6317 | } |
| 6318 | |
| 6319 | /* nothing left to forward for this chunk*/ |
| 6320 | msg->msg_state = HTTP_MSG_CHUNK_CRLF; |
| 6321 | /* fall through for HTTP_MSG_CHUNK_CRLF */ |
| 6322 | |
| 6323 | case HTTP_MSG_CHUNK_CRLF: |
| 6324 | /* we want the CRLF after the data */ |
Willy Tarreau | b289256 | 2017-09-21 11:33:54 +0200 | [diff] [blame] | 6325 | ret = h1_skip_chunk_crlf(chn->buf, msg->next, chn->buf->i); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6326 | if (ret == 0) |
| 6327 | goto missing_data_or_waiting; |
Willy Tarreau | b289256 | 2017-09-21 11:33:54 +0200 | [diff] [blame] | 6328 | if (ret < 0) { |
| 6329 | msg->err_pos = chn->buf->i + ret; |
| 6330 | if (msg->err_pos < 0) |
| 6331 | msg->err_pos += chn->buf->size; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6332 | goto chunk_parsing_error; |
Willy Tarreau | b289256 | 2017-09-21 11:33:54 +0200 | [diff] [blame] | 6333 | } |
Christopher Faulet | 113f7de | 2015-12-14 14:52:13 +0100 | [diff] [blame] | 6334 | msg->next += ret; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6335 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 6336 | /* fall through for HTTP_MSG_CHUNK_SIZE */ |
| 6337 | |
| 6338 | case HTTP_MSG_CHUNK_SIZE: |
| 6339 | /* read the chunk size and assign it to ->chunk_len, |
| 6340 | * then set ->next to point to the body and switch to |
| 6341 | * DATA or TRAILERS state. |
| 6342 | */ |
Willy Tarreau | e56cdd3 | 2017-09-21 08:36:33 +0200 | [diff] [blame] | 6343 | 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] | 6344 | if (ret == 0) |
| 6345 | goto missing_data_or_waiting; |
Willy Tarreau | e56cdd3 | 2017-09-21 08:36:33 +0200 | [diff] [blame] | 6346 | if (ret < 0) { |
| 6347 | msg->err_pos = chn->buf->i + ret; |
| 6348 | if (msg->err_pos < 0) |
| 6349 | msg->err_pos += chn->buf->size; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6350 | goto chunk_parsing_error; |
Willy Tarreau | e56cdd3 | 2017-09-21 08:36:33 +0200 | [diff] [blame] | 6351 | } |
| 6352 | |
| 6353 | msg->sol = ret; |
Christopher Faulet | 113f7de | 2015-12-14 14:52:13 +0100 | [diff] [blame] | 6354 | msg->next += ret; |
Willy Tarreau | e56cdd3 | 2017-09-21 08:36:33 +0200 | [diff] [blame] | 6355 | msg->chunk_len = chunk; |
| 6356 | msg->body_len += chunk; |
| 6357 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6358 | if (msg->chunk_len) { |
| 6359 | msg->msg_state = HTTP_MSG_DATA; |
| 6360 | goto switch_states; |
| 6361 | } |
| 6362 | msg->msg_state = HTTP_MSG_TRAILERS; |
| 6363 | /* fall through for HTTP_MSG_TRAILERS */ |
| 6364 | |
| 6365 | case HTTP_MSG_TRAILERS: |
| 6366 | ret = http_forward_trailers(msg); |
| 6367 | if (ret < 0) |
| 6368 | goto chunk_parsing_error; |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 6369 | FLT_STRM_DATA_CB(s, chn, flt_http_chunk_trailers(s, msg), |
| 6370 | /* default_ret */ 1, |
| 6371 | /* on_error */ goto error); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6372 | msg->next += msg->sol; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6373 | if (!ret) |
| 6374 | goto missing_data_or_waiting; |
| 6375 | break; |
| 6376 | |
| 6377 | case HTTP_MSG_ENDING: |
| 6378 | goto ending; |
| 6379 | |
| 6380 | default: |
| 6381 | /* This should no happen in this function */ |
| 6382 | goto error; |
| 6383 | } |
| 6384 | |
| 6385 | msg->msg_state = HTTP_MSG_ENDING; |
| 6386 | ending: |
| 6387 | /* we may have some pending data starting at res->buf->p such as a last |
| 6388 | * chunk of data or trailers. */ |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 6389 | 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] | 6390 | /* default_ret */ msg->next, |
| 6391 | /* on_error */ goto error); |
| 6392 | b_adv(chn->buf, ret); |
| 6393 | msg->next -= ret; |
Willy Tarreau | 9962f8f | 2016-06-28 11:52:08 +0200 | [diff] [blame] | 6394 | if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)) |
| 6395 | msg->sov -= ret; |
Christopher Faulet | a9300a3 | 2016-06-28 15:54:44 +0200 | [diff] [blame] | 6396 | if (msg->next) |
| 6397 | goto waiting; |
Willy Tarreau | 9962f8f | 2016-06-28 11:52:08 +0200 | [diff] [blame] | 6398 | |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 6399 | FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg), |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6400 | /* default_ret */ 1, |
| 6401 | /* on_error */ goto error, |
| 6402 | /* on_wait */ goto waiting); |
| 6403 | msg->msg_state = HTTP_MSG_DONE; |
| 6404 | return 1; |
| 6405 | |
| 6406 | missing_data_or_waiting: |
| 6407 | /* we may have some pending data starting at chn->buf->p */ |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 6408 | 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] | 6409 | /* default_ret */ msg->next, |
| 6410 | /* on_error */ goto error); |
| 6411 | b_adv(chn->buf, ret); |
| 6412 | msg->next -= ret; |
| 6413 | if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0) |
| 6414 | msg->sov -= ret; |
Christopher Faulet | 75e2eb6 | 2015-12-15 10:41:47 +0100 | [diff] [blame] | 6415 | if (!HAS_DATA_FILTERS(s, chn)) |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6416 | msg->chunk_len -= channel_forward(chn, msg->chunk_len); |
Christopher Faulet | a9300a3 | 2016-06-28 15:54:44 +0200 | [diff] [blame] | 6417 | waiting: |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6418 | return 0; |
| 6419 | |
| 6420 | chunk_parsing_error: |
| 6421 | if (msg->err_pos >= 0) { |
| 6422 | if (chn->flags & CF_ISRESP) |
Emeric Brun | 8c1aaa2 | 2017-06-15 11:30:06 +0200 | [diff] [blame] | 6423 | http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6424 | msg->msg_state, strm_fe(s)); |
| 6425 | else |
Emeric Brun | 8c1aaa2 | 2017-06-15 11:30:06 +0200 | [diff] [blame] | 6426 | 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] | 6427 | msg, msg->msg_state, s->be); |
| 6428 | } |
| 6429 | error: |
| 6430 | return -1; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6431 | } |
| 6432 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6433 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6434 | /* Iterate the same filter through all request headers. |
| 6435 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6436 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 6437 | * DENY stats. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6438 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6439 | 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] | 6440 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6441 | char *cur_ptr, *cur_end, *cur_next; |
| 6442 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 6443 | struct http_txn *txn = s->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6444 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6445 | int delta; |
Willy Tarreau | 0f7562b | 2007-01-07 15:46:13 +0100 | [diff] [blame] | 6446 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6447 | last_hdr = 0; |
| 6448 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6449 | cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6450 | old_idx = 0; |
| 6451 | |
| 6452 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6453 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6454 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6455 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6456 | (exp->action == ACT_ALLOW || |
| 6457 | exp->action == ACT_DENY || |
| 6458 | exp->action == ACT_TARPIT)) |
| 6459 | return 0; |
| 6460 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6461 | cur_idx = txn->hdr_idx.v[old_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6462 | if (!cur_idx) |
| 6463 | break; |
| 6464 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6465 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6466 | cur_ptr = cur_next; |
| 6467 | cur_end = cur_ptr + cur_hdr->len; |
| 6468 | cur_next = cur_end + cur_hdr->cr + 1; |
| 6469 | |
| 6470 | /* Now we have one header between cur_ptr and cur_end, |
| 6471 | * and the next header starts at cur_next. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6472 | */ |
| 6473 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 6474 | 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] | 6475 | switch (exp->action) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6476 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6477 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6478 | last_hdr = 1; |
| 6479 | break; |
| 6480 | |
| 6481 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6482 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6483 | last_hdr = 1; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6484 | break; |
| 6485 | |
| 6486 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6487 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6488 | last_hdr = 1; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6489 | break; |
| 6490 | |
| 6491 | case ACT_REPLACE: |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 6492 | trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch); |
| 6493 | if (trash.len < 0) |
| 6494 | return -1; |
| 6495 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6496 | 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] | 6497 | /* FIXME: if the user adds a newline in the replacement, the |
| 6498 | * index will not be recalculated for now, and the new line |
| 6499 | * will not be counted as a new header. |
| 6500 | */ |
| 6501 | |
| 6502 | cur_end += delta; |
| 6503 | cur_next += delta; |
| 6504 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6505 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6506 | break; |
| 6507 | |
| 6508 | case ACT_REMOVE: |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6509 | delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6510 | cur_next += delta; |
| 6511 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6512 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6513 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 6514 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6515 | cur_hdr->len = 0; |
| 6516 | cur_end = NULL; /* null-term has been rewritten */ |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 6517 | cur_idx = old_idx; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6518 | break; |
| 6519 | |
| 6520 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6521 | } |
| 6522 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6523 | /* keep the link from this header to next one in case of later |
| 6524 | * removal of next header. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6525 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6526 | old_idx = cur_idx; |
| 6527 | } |
| 6528 | return 0; |
| 6529 | } |
| 6530 | |
| 6531 | |
| 6532 | /* Apply the filter to the request line. |
| 6533 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 6534 | * or -1 if a replacement resulted in an invalid request line. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6535 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 6536 | * DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6537 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6538 | 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] | 6539 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6540 | char *cur_ptr, *cur_end; |
| 6541 | int done; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 6542 | struct http_txn *txn = s->txn; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6543 | int delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6544 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6545 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6546 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6547 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6548 | (exp->action == ACT_ALLOW || |
| 6549 | exp->action == ACT_DENY || |
| 6550 | exp->action == ACT_TARPIT)) |
| 6551 | return 0; |
| 6552 | else if (exp->action == ACT_REMOVE) |
| 6553 | return 0; |
| 6554 | |
| 6555 | done = 0; |
| 6556 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6557 | cur_ptr = req->buf->p; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6558 | cur_end = cur_ptr + txn->req.sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6559 | |
| 6560 | /* Now we have the request line between cur_ptr and cur_end */ |
| 6561 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 6562 | 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] | 6563 | switch (exp->action) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6564 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6565 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6566 | done = 1; |
| 6567 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 6568 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6569 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6570 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6571 | done = 1; |
| 6572 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 6573 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6574 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6575 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6576 | done = 1; |
| 6577 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 6578 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6579 | case ACT_REPLACE: |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 6580 | trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch); |
| 6581 | if (trash.len < 0) |
| 6582 | return -1; |
| 6583 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6584 | 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] | 6585 | /* FIXME: if the user adds a newline in the replacement, the |
| 6586 | * index will not be recalculated for now, and the new line |
| 6587 | * will not be counted as a new header. |
| 6588 | */ |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 6589 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6590 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6591 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 6592 | cur_end = (char *)http_parse_reqline(&txn->req, |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6593 | HTTP_MSG_RQMETH, |
| 6594 | cur_ptr, cur_end + 1, |
| 6595 | NULL, NULL); |
| 6596 | if (unlikely(!cur_end)) |
| 6597 | return -1; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 6598 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6599 | /* we have a full request and we know that we have either a CR |
| 6600 | * or an LF at <ptr>. |
| 6601 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6602 | txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l); |
| 6603 | 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] | 6604 | /* there is no point trying this regex on headers */ |
| 6605 | return 1; |
| 6606 | } |
| 6607 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6608 | return done; |
| 6609 | } |
Willy Tarreau | 97de624 | 2006-12-27 17:18:38 +0100 | [diff] [blame] | 6610 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6611 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6612 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6613 | /* |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6614 | * 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] | 6615 | * 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] | 6616 | * unparsable request. Since it can manage the switch to another backend, it |
| 6617 | * updates the per-proxy DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6618 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6619 | 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] | 6620 | { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 6621 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 6622 | struct http_txn *txn = s->txn; |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6623 | struct hdr_exp *exp; |
| 6624 | |
| 6625 | for (exp = px->req_exp; exp; exp = exp->next) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6626 | int ret; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6627 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6628 | /* |
| 6629 | * The interleaving of transformations and verdicts |
| 6630 | * makes it difficult to decide to continue or stop |
| 6631 | * the evaluation. |
| 6632 | */ |
| 6633 | |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6634 | if (txn->flags & (TX_CLDENY|TX_CLTARPIT)) |
| 6635 | break; |
| 6636 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6637 | if ((txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6638 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6639 | exp->action == ACT_TARPIT || exp->action == ACT_PASS)) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6640 | continue; |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6641 | |
| 6642 | /* if this filter had a condition, evaluate it now and skip to |
| 6643 | * next filter if the condition does not match. |
| 6644 | */ |
| 6645 | if (exp->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 6646 | 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] | 6647 | ret = acl_pass(ret); |
| 6648 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 6649 | ret = !ret; |
| 6650 | |
| 6651 | if (!ret) |
| 6652 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6653 | } |
| 6654 | |
| 6655 | /* Apply the filter to the request line. */ |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6656 | ret = apply_filter_to_req_line(s, req, exp); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6657 | if (unlikely(ret < 0)) |
| 6658 | return -1; |
| 6659 | |
| 6660 | if (likely(ret == 0)) { |
| 6661 | /* The filter did not match the request, it can be |
| 6662 | * iterated through all headers. |
| 6663 | */ |
Willy Tarreau | 34d4c3c | 2015-01-30 20:58:58 +0100 | [diff] [blame] | 6664 | if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0)) |
| 6665 | return -1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6666 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6667 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6668 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6669 | } |
| 6670 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6671 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6672 | /* Find the end of a cookie value contained between <s> and <e>. It works the |
| 6673 | * same way as with headers above except that the semi-colon also ends a token. |
| 6674 | * See RFC2965 for more information. Note that it requires a valid header to |
| 6675 | * return a valid result. |
| 6676 | */ |
| 6677 | char *find_cookie_value_end(char *s, const char *e) |
| 6678 | { |
| 6679 | int quoted, qdpair; |
| 6680 | |
| 6681 | quoted = qdpair = 0; |
| 6682 | for (; s < e; s++) { |
| 6683 | if (qdpair) qdpair = 0; |
| 6684 | else if (quoted) { |
| 6685 | if (*s == '\\') qdpair = 1; |
| 6686 | else if (*s == '"') quoted = 0; |
| 6687 | } |
| 6688 | else if (*s == '"') quoted = 1; |
| 6689 | else if (*s == ',' || *s == ';') return s; |
| 6690 | } |
| 6691 | return s; |
| 6692 | } |
| 6693 | |
| 6694 | /* Delete a value in a header between delimiters <from> and <next> in buffer |
| 6695 | * <buf>. The number of characters displaced is returned, and the pointer to |
| 6696 | * the first delimiter is updated if required. The function tries as much as |
| 6697 | * possible to respect the following principles : |
| 6698 | * - replace <from> delimiter by the <next> one unless <from> points to a |
| 6699 | * colon, in which case <next> is simply removed |
| 6700 | * - set exactly one space character after the new first delimiter, unless |
| 6701 | * there are not enough characters in the block being moved to do so. |
| 6702 | * - remove unneeded spaces before the previous delimiter and after the new |
| 6703 | * one. |
| 6704 | * |
| 6705 | * It is the caller's responsibility to ensure that : |
| 6706 | * - <from> points to a valid delimiter or the colon ; |
| 6707 | * - <next> points to a valid delimiter or the final CR/LF ; |
| 6708 | * - there are non-space chars before <from> ; |
| 6709 | * - there is a CR/LF at or after <next>. |
| 6710 | */ |
Willy Tarreau | af81935 | 2012-08-27 22:08:00 +0200 | [diff] [blame] | 6711 | int del_hdr_value(struct buffer *buf, char **from, char *next) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6712 | { |
| 6713 | char *prev = *from; |
| 6714 | |
| 6715 | if (*prev == ':') { |
| 6716 | /* We're removing the first value, preserve the colon and add a |
| 6717 | * space if possible. |
| 6718 | */ |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 6719 | if (!HTTP_IS_CRLF(*next)) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6720 | next++; |
| 6721 | prev++; |
| 6722 | if (prev < next) |
| 6723 | *prev++ = ' '; |
| 6724 | |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 6725 | while (HTTP_IS_SPHT(*next)) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6726 | next++; |
| 6727 | } else { |
| 6728 | /* Remove useless spaces before the old delimiter. */ |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 6729 | while (HTTP_IS_SPHT(*(prev-1))) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6730 | prev--; |
| 6731 | *from = prev; |
| 6732 | |
| 6733 | /* copy the delimiter and if possible a space if we're |
| 6734 | * not at the end of the line. |
| 6735 | */ |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 6736 | if (!HTTP_IS_CRLF(*next)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6737 | *prev++ = *next++; |
| 6738 | if (prev + 1 < next) |
| 6739 | *prev++ = ' '; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 6740 | while (HTTP_IS_SPHT(*next)) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6741 | next++; |
| 6742 | } |
| 6743 | } |
| 6744 | return buffer_replace2(buf, prev, next, NULL, 0); |
| 6745 | } |
| 6746 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6747 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 6748 | * 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] | 6749 | * desirable to call it only when needed. This code is quite complex because |
| 6750 | * of the multiple very crappy and ambiguous syntaxes we have to support. it |
| 6751 | * highly recommended not to touch this part without a good reason ! |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6752 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6753 | void manage_client_side_cookies(struct stream *s, struct channel *req) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6754 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 6755 | struct http_txn *txn = s->txn; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6756 | struct session *sess = s->sess; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6757 | int preserve_hdr; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6758 | int cur_idx, old_idx; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6759 | char *hdr_beg, *hdr_end, *hdr_next, *del_from; |
| 6760 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6761 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6762 | /* Iterate through the headers, we start with the start line. */ |
Willy Tarreau | 83969f4 | 2007-01-22 08:55:47 +0100 | [diff] [blame] | 6763 | old_idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6764 | hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6765 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6766 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6767 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6768 | int val; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6769 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6770 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6771 | hdr_beg = hdr_next; |
| 6772 | hdr_end = hdr_beg + cur_hdr->len; |
| 6773 | hdr_next = hdr_end + cur_hdr->cr + 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6774 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6775 | /* We have one full header between hdr_beg and hdr_end, and the |
| 6776 | * next header starts at hdr_next. We're only interested in |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6777 | * "Cookie:" headers. |
| 6778 | */ |
| 6779 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6780 | val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6); |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6781 | if (!val) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6782 | old_idx = cur_idx; |
| 6783 | continue; |
| 6784 | } |
| 6785 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6786 | del_from = NULL; /* nothing to be deleted */ |
| 6787 | preserve_hdr = 0; /* assume we may kill the whole header */ |
| 6788 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6789 | /* Now look for cookies. Conforming to RFC2109, we have to support |
| 6790 | * attributes whose name begin with a '$', and associate them with |
| 6791 | * the right cookie, if we want to delete this cookie. |
| 6792 | * So there are 3 cases for each cookie read : |
| 6793 | * 1) it's a special attribute, beginning with a '$' : ignore it. |
| 6794 | * 2) it's a server id cookie that we *MAY* want to delete : save |
| 6795 | * some pointers on it (last semi-colon, beginning of cookie...) |
| 6796 | * 3) it's an application cookie : we *MAY* have to delete a previous |
| 6797 | * "special" cookie. |
| 6798 | * At the end of loop, if a "special" cookie remains, we may have to |
| 6799 | * remove it. If no application cookie persists in the header, we |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6800 | * *MUST* delete it. |
| 6801 | * |
| 6802 | * Note: RFC2965 is unclear about the processing of spaces around |
| 6803 | * the equal sign in the ATTR=VALUE form. A careful inspection of |
| 6804 | * the RFC explicitly allows spaces before it, and not within the |
| 6805 | * tokens (attrs or values). An inspection of RFC2109 allows that |
| 6806 | * too but section 10.1.3 lets one think that spaces may be allowed |
| 6807 | * after the equal sign too, resulting in some (rare) buggy |
| 6808 | * implementations trying to do that. So let's do what servers do. |
| 6809 | * Latest ietf draft forbids spaces all around. Also, earlier RFCs |
| 6810 | * allowed quoted strings in values, with any possible character |
| 6811 | * after a backslash, including control chars and delimitors, which |
| 6812 | * causes parsing to become ambiguous. Browsers also allow spaces |
| 6813 | * within values even without quotes. |
| 6814 | * |
| 6815 | * We have to keep multiple pointers in order to support cookie |
| 6816 | * removal at the beginning, middle or end of header without |
| 6817 | * corrupting the header. All of these headers are valid : |
| 6818 | * |
| 6819 | * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n |
| 6820 | * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n |
| 6821 | * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n |
| 6822 | * | | | | | | | | | |
| 6823 | * | | | | | | | | hdr_end <--+ |
| 6824 | * | | | | | | | +--> next |
| 6825 | * | | | | | | +----> val_end |
| 6826 | * | | | | | +-----------> val_beg |
| 6827 | * | | | | +--------------> equal |
| 6828 | * | | | +----------------> att_end |
| 6829 | * | | +---------------------> att_beg |
| 6830 | * | +--------------------------> prev |
| 6831 | * +--------------------------------> hdr_beg |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6832 | */ |
| 6833 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6834 | for (prev = hdr_beg + 6; prev < hdr_end; prev = next) { |
| 6835 | /* Iterate through all cookies on this line */ |
| 6836 | |
| 6837 | /* find att_beg */ |
| 6838 | att_beg = prev + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 6839 | while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg)) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6840 | att_beg++; |
| 6841 | |
| 6842 | /* find att_end : this is the first character after the last non |
| 6843 | * space before the equal. It may be equal to hdr_end. |
| 6844 | */ |
| 6845 | equal = att_end = att_beg; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6846 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6847 | while (equal < hdr_end) { |
| 6848 | if (*equal == '=' || *equal == ',' || *equal == ';') |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6849 | break; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 6850 | if (HTTP_IS_SPHT(*equal++)) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6851 | continue; |
| 6852 | att_end = equal; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6853 | } |
| 6854 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6855 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 6856 | * is between <att_beg> and <equal>, both may be identical. |
| 6857 | */ |
| 6858 | |
| 6859 | /* look for end of cookie if there is an equal sign */ |
| 6860 | if (equal < hdr_end && *equal == '=') { |
| 6861 | /* look for the beginning of the value */ |
| 6862 | val_beg = equal + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 6863 | while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg)) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6864 | val_beg++; |
| 6865 | |
| 6866 | /* find the end of the value, respecting quotes */ |
| 6867 | next = find_cookie_value_end(val_beg, hdr_end); |
| 6868 | |
| 6869 | /* make val_end point to the first white space or delimitor after the value */ |
| 6870 | val_end = next; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 6871 | while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1))) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6872 | val_end--; |
| 6873 | } else { |
| 6874 | val_beg = val_end = next = equal; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 6875 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6876 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6877 | /* We have nothing to do with attributes beginning with '$'. However, |
| 6878 | * they will automatically be removed if a header before them is removed, |
| 6879 | * since they're supposed to be linked together. |
| 6880 | */ |
| 6881 | if (*att_beg == '$') |
| 6882 | continue; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6883 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6884 | /* Ignore cookies with no equal sign */ |
| 6885 | if (equal == next) { |
| 6886 | /* This is not our cookie, so we must preserve it. But if we already |
| 6887 | * scheduled another cookie for removal, we cannot remove the |
| 6888 | * complete header, but we can remove the previous block itself. |
| 6889 | */ |
| 6890 | preserve_hdr = 1; |
| 6891 | if (del_from != NULL) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6892 | int delta = del_hdr_value(req->buf, &del_from, prev); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6893 | val_end += delta; |
| 6894 | next += delta; |
| 6895 | hdr_end += delta; |
| 6896 | hdr_next += delta; |
| 6897 | cur_hdr->len += delta; |
| 6898 | http_msg_move_end(&txn->req, delta); |
| 6899 | prev = del_from; |
| 6900 | del_from = NULL; |
| 6901 | } |
| 6902 | continue; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 6903 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6904 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6905 | /* if there are spaces around the equal sign, we need to |
| 6906 | * strip them otherwise we'll get trouble for cookie captures, |
| 6907 | * or even for rewrites. Since this happens extremely rarely, |
| 6908 | * it does not hurt performance. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6909 | */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6910 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 6911 | int stripped_before = 0; |
| 6912 | int stripped_after = 0; |
| 6913 | |
| 6914 | if (att_end != equal) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6915 | stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6916 | equal += stripped_before; |
| 6917 | val_beg += stripped_before; |
| 6918 | } |
| 6919 | |
| 6920 | if (val_beg > equal + 1) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6921 | stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6922 | val_beg += stripped_after; |
| 6923 | stripped_before += stripped_after; |
| 6924 | } |
| 6925 | |
| 6926 | val_end += stripped_before; |
| 6927 | next += stripped_before; |
| 6928 | hdr_end += stripped_before; |
| 6929 | hdr_next += stripped_before; |
| 6930 | cur_hdr->len += stripped_before; |
| 6931 | http_msg_move_end(&txn->req, stripped_before); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6932 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6933 | /* now everything is as on the diagram above */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6934 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6935 | /* First, let's see if we want to capture this cookie. We check |
| 6936 | * that we don't already have a client side cookie, because we |
| 6937 | * can only capture one. Also as an optimisation, we ignore |
| 6938 | * cookies shorter than the declared name. |
| 6939 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6940 | if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL && |
| 6941 | (val_end - att_beg >= sess->fe->capture_namelen) && |
| 6942 | memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6943 | int log_len = val_end - att_beg; |
| 6944 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6945 | if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 6946 | ha_alert("HTTP logging : out of memory.\n"); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6947 | } else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6948 | if (log_len > sess->fe->capture_len) |
| 6949 | log_len = sess->fe->capture_len; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6950 | memcpy(txn->cli_cookie, att_beg, log_len); |
| 6951 | txn->cli_cookie[log_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6952 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6953 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6954 | |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6955 | /* Persistence cookies in passive, rewrite or insert mode have the |
| 6956 | * following form : |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6957 | * |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6958 | * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]] |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6959 | * |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6960 | * For cookies in prefix mode, the form is : |
| 6961 | * |
| 6962 | * Cookie: NAME=SRV~VALUE |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6963 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6964 | if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) && |
| 6965 | (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) { |
| 6966 | struct server *srv = s->be->srv; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6967 | char *delim; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6968 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6969 | /* if we're in cookie prefix mode, we'll search the delimitor so that we |
| 6970 | * have the server ID between val_beg and delim, and the original cookie between |
| 6971 | * delim+1 and val_end. Otherwise, delim==val_end : |
| 6972 | * |
| 6973 | * Cookie: NAME=SRV; # in all but prefix modes |
| 6974 | * Cookie: NAME=SRV~OPAQUE ; # in prefix mode |
| 6975 | * | || || | |+-> next |
| 6976 | * | || || | +--> val_end |
| 6977 | * | || || +---------> delim |
| 6978 | * | || |+------------> val_beg |
| 6979 | * | || +-------------> att_end = equal |
| 6980 | * | |+-----------------> att_beg |
| 6981 | * | +------------------> prev |
| 6982 | * +-------------------------> hdr_beg |
| 6983 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6984 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6985 | if (s->be->ck_opts & PR_CK_PFX) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6986 | for (delim = val_beg; delim < val_end; delim++) |
| 6987 | if (*delim == COOKIE_DELIM) |
| 6988 | break; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6989 | } else { |
| 6990 | char *vbar1; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6991 | delim = val_end; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6992 | /* Now check if the cookie contains a date field, which would |
| 6993 | * appear after a vertical bar ('|') just after the server name |
| 6994 | * and before the delimiter. |
| 6995 | */ |
| 6996 | vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg); |
| 6997 | if (vbar1) { |
| 6998 | /* OK, so left of the bar is the server's cookie and |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6999 | * right is the last seen date. It is a base64 encoded |
| 7000 | * 30-bit value representing the UNIX date since the |
| 7001 | * epoch in 4-second quantities. |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7002 | */ |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7003 | int val; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7004 | delim = vbar1++; |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7005 | if (val_end - vbar1 >= 5) { |
| 7006 | val = b64tos30(vbar1); |
| 7007 | if (val > 0) |
| 7008 | txn->cookie_last_date = val << 2; |
| 7009 | } |
| 7010 | /* look for a second vertical bar */ |
| 7011 | vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1); |
| 7012 | if (vbar1 && (val_end - vbar1 > 5)) { |
| 7013 | val = b64tos30(vbar1 + 1); |
| 7014 | if (val > 0) |
| 7015 | txn->cookie_first_date = val << 2; |
| 7016 | } |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7017 | } |
| 7018 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7019 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7020 | /* if the cookie has an expiration date and the proxy wants to check |
| 7021 | * it, then we do that now. We first check if the cookie is too old, |
| 7022 | * then only if it has expired. We detect strict overflow because the |
| 7023 | * time resolution here is not great (4 seconds). Cookies with dates |
| 7024 | * in the future are ignored if their offset is beyond one day. This |
| 7025 | * allows an admin to fix timezone issues without expiring everyone |
| 7026 | * and at the same time avoids keeping unwanted side effects for too |
| 7027 | * long. |
| 7028 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7029 | if (txn->cookie_first_date && s->be->cookie_maxlife && |
| 7030 | (((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] | 7031 | ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) { |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7032 | txn->flags &= ~TX_CK_MASK; |
| 7033 | txn->flags |= TX_CK_OLD; |
| 7034 | delim = val_beg; // let's pretend we have not found the cookie |
| 7035 | txn->cookie_first_date = 0; |
| 7036 | txn->cookie_last_date = 0; |
| 7037 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7038 | else if (txn->cookie_last_date && s->be->cookie_maxidle && |
| 7039 | (((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] | 7040 | ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) { |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7041 | txn->flags &= ~TX_CK_MASK; |
| 7042 | txn->flags |= TX_CK_EXPIRED; |
| 7043 | delim = val_beg; // let's pretend we have not found the cookie |
| 7044 | txn->cookie_first_date = 0; |
| 7045 | txn->cookie_last_date = 0; |
| 7046 | } |
| 7047 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7048 | /* Here, we'll look for the first running server which supports the cookie. |
| 7049 | * This allows to share a same cookie between several servers, for example |
| 7050 | * to dedicate backup servers to specific servers only. |
| 7051 | * However, to prevent clients from sticking to cookie-less backup server |
| 7052 | * when they have incidentely learned an empty cookie, we simply ignore |
| 7053 | * empty cookies and mark them as invalid. |
| 7054 | * The same behaviour is applied when persistence must be ignored. |
| 7055 | */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 7056 | if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED))) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7057 | srv = NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7058 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7059 | while (srv) { |
| 7060 | if (srv->cookie && (srv->cklen == delim - val_beg) && |
| 7061 | !memcmp(val_beg, srv->cookie, delim - val_beg)) { |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 7062 | if ((srv->cur_state != SRV_ST_STOPPED) || |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7063 | (s->be->options & PR_O_PERSIST) || |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 7064 | (s->flags & SF_FORCE_PRST)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7065 | /* we found the server and we can use it */ |
| 7066 | txn->flags &= ~TX_CK_MASK; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 7067 | 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] | 7068 | s->flags |= SF_DIRECT | SF_ASSIGNED; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7069 | s->target = &srv->obj_type; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7070 | break; |
| 7071 | } else { |
| 7072 | /* we found a server, but it's down, |
| 7073 | * mark it as such and go on in case |
| 7074 | * another one is available. |
| 7075 | */ |
| 7076 | txn->flags &= ~TX_CK_MASK; |
| 7077 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7078 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7079 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7080 | srv = srv->next; |
| 7081 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7082 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7083 | 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] | 7084 | /* no server matched this cookie or we deliberately skipped it */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7085 | txn->flags &= ~TX_CK_MASK; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 7086 | if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED))) |
Willy Tarreau | c89ccb6 | 2012-04-05 21:18:22 +0200 | [diff] [blame] | 7087 | txn->flags |= TX_CK_UNUSED; |
| 7088 | else |
| 7089 | txn->flags |= TX_CK_INVALID; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7090 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7091 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7092 | /* depending on the cookie mode, we may have to either : |
| 7093 | * - delete the complete cookie if we're in insert+indirect mode, so that |
| 7094 | * the server never sees it ; |
| 7095 | * - remove the server id from the cookie value, and tag the cookie as an |
| 7096 | * application cookie so that it does not get accidentely removed later, |
| 7097 | * if we're in cookie prefix mode |
| 7098 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7099 | if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7100 | int delta; /* negative */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7101 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7102 | delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7103 | val_end += delta; |
| 7104 | next += delta; |
| 7105 | hdr_end += delta; |
| 7106 | hdr_next += delta; |
| 7107 | cur_hdr->len += delta; |
| 7108 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7109 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7110 | del_from = NULL; |
| 7111 | preserve_hdr = 1; /* we want to keep this cookie */ |
| 7112 | } |
| 7113 | else if (del_from == NULL && |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7114 | (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] | 7115 | del_from = prev; |
| 7116 | } |
| 7117 | } else { |
| 7118 | /* This is not our cookie, so we must preserve it. But if we already |
| 7119 | * scheduled another cookie for removal, we cannot remove the |
| 7120 | * complete header, but we can remove the previous block itself. |
| 7121 | */ |
| 7122 | preserve_hdr = 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7123 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7124 | if (del_from != NULL) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7125 | int delta = del_hdr_value(req->buf, &del_from, prev); |
Willy Tarreau | b810554 | 2010-11-24 18:31:28 +0100 | [diff] [blame] | 7126 | if (att_beg >= del_from) |
| 7127 | att_beg += delta; |
| 7128 | if (att_end >= del_from) |
| 7129 | att_end += delta; |
| 7130 | val_beg += delta; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7131 | val_end += delta; |
| 7132 | next += delta; |
| 7133 | hdr_end += delta; |
| 7134 | hdr_next += delta; |
| 7135 | cur_hdr->len += delta; |
| 7136 | http_msg_move_end(&txn->req, delta); |
| 7137 | prev = del_from; |
| 7138 | del_from = NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7139 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7140 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7141 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7142 | /* continue with next cookie on this header line */ |
| 7143 | att_beg = next; |
| 7144 | } /* for each cookie */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7145 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7146 | /* There are no more cookies on this line. |
| 7147 | * We may still have one (or several) marked for deletion at the |
| 7148 | * end of the line. We must do this now in two ways : |
| 7149 | * - if some cookies must be preserved, we only delete from the |
| 7150 | * mark to the end of line ; |
| 7151 | * - if nothing needs to be preserved, simply delete the whole header |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7152 | */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7153 | if (del_from) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7154 | int delta; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7155 | if (preserve_hdr) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7156 | delta = del_hdr_value(req->buf, &del_from, hdr_end); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7157 | hdr_end = del_from; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7158 | cur_hdr->len += delta; |
| 7159 | } else { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7160 | delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7161 | |
| 7162 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7163 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 7164 | txn->hdr_idx.used--; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7165 | cur_hdr->len = 0; |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 7166 | cur_idx = old_idx; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7167 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7168 | hdr_next += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7169 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7170 | } |
| 7171 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7172 | /* check next header */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7173 | old_idx = cur_idx; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7174 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7175 | } |
| 7176 | |
| 7177 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7178 | /* Iterate the same filter through all response headers contained in <rtr>. |
| 7179 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
| 7180 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7181 | 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] | 7182 | { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7183 | char *cur_ptr, *cur_end, *cur_next; |
| 7184 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7185 | struct http_txn *txn = s->txn; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7186 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7187 | int delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7188 | |
| 7189 | last_hdr = 0; |
| 7190 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7191 | cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7192 | old_idx = 0; |
| 7193 | |
| 7194 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7195 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7196 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7197 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7198 | (exp->action == ACT_ALLOW || |
| 7199 | exp->action == ACT_DENY)) |
| 7200 | return 0; |
| 7201 | |
| 7202 | cur_idx = txn->hdr_idx.v[old_idx].next; |
| 7203 | if (!cur_idx) |
| 7204 | break; |
| 7205 | |
| 7206 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 7207 | cur_ptr = cur_next; |
| 7208 | cur_end = cur_ptr + cur_hdr->len; |
| 7209 | cur_next = cur_end + cur_hdr->cr + 1; |
| 7210 | |
| 7211 | /* Now we have one header between cur_ptr and cur_end, |
| 7212 | * and the next header starts at cur_next. |
| 7213 | */ |
| 7214 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 7215 | 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] | 7216 | switch (exp->action) { |
| 7217 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7218 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7219 | last_hdr = 1; |
| 7220 | break; |
| 7221 | |
| 7222 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7223 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7224 | last_hdr = 1; |
| 7225 | break; |
| 7226 | |
| 7227 | case ACT_REPLACE: |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 7228 | trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch); |
| 7229 | if (trash.len < 0) |
| 7230 | return -1; |
| 7231 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7232 | 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] | 7233 | /* FIXME: if the user adds a newline in the replacement, the |
| 7234 | * index will not be recalculated for now, and the new line |
| 7235 | * will not be counted as a new header. |
| 7236 | */ |
| 7237 | |
| 7238 | cur_end += delta; |
| 7239 | cur_next += delta; |
| 7240 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7241 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7242 | break; |
| 7243 | |
| 7244 | case ACT_REMOVE: |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7245 | delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7246 | cur_next += delta; |
| 7247 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7248 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7249 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 7250 | txn->hdr_idx.used--; |
| 7251 | cur_hdr->len = 0; |
| 7252 | cur_end = NULL; /* null-term has been rewritten */ |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 7253 | cur_idx = old_idx; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7254 | break; |
| 7255 | |
| 7256 | } |
| 7257 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7258 | |
| 7259 | /* keep the link from this header to next one in case of later |
| 7260 | * removal of next header. |
| 7261 | */ |
| 7262 | old_idx = cur_idx; |
| 7263 | } |
| 7264 | return 0; |
| 7265 | } |
| 7266 | |
| 7267 | |
| 7268 | /* Apply the filter to the status line in the response buffer <rtr>. |
| 7269 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 7270 | * or -1 if a replacement resulted in an invalid status line. |
| 7271 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7272 | 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] | 7273 | { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7274 | char *cur_ptr, *cur_end; |
| 7275 | int done; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7276 | struct http_txn *txn = s->txn; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7277 | int delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7278 | |
| 7279 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7280 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7281 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7282 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7283 | (exp->action == ACT_ALLOW || |
| 7284 | exp->action == ACT_DENY)) |
| 7285 | return 0; |
| 7286 | else if (exp->action == ACT_REMOVE) |
| 7287 | return 0; |
| 7288 | |
| 7289 | done = 0; |
| 7290 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7291 | cur_ptr = rtr->buf->p; |
Willy Tarreau | 1ba0e5f | 2010-06-07 13:57:32 +0200 | [diff] [blame] | 7292 | cur_end = cur_ptr + txn->rsp.sl.st.l; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7293 | |
| 7294 | /* Now we have the status line between cur_ptr and cur_end */ |
| 7295 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 7296 | 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] | 7297 | switch (exp->action) { |
| 7298 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7299 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7300 | done = 1; |
| 7301 | break; |
| 7302 | |
| 7303 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7304 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7305 | done = 1; |
| 7306 | break; |
| 7307 | |
| 7308 | case ACT_REPLACE: |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 7309 | trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch); |
| 7310 | if (trash.len < 0) |
| 7311 | return -1; |
| 7312 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7313 | 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] | 7314 | /* FIXME: if the user adds a newline in the replacement, the |
| 7315 | * index will not be recalculated for now, and the new line |
| 7316 | * will not be counted as a new header. |
| 7317 | */ |
| 7318 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7319 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7320 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 7321 | cur_end = (char *)http_parse_stsline(&txn->rsp, |
Willy Tarreau | 0278576 | 2007-04-03 14:45:44 +0200 | [diff] [blame] | 7322 | HTTP_MSG_RPVER, |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7323 | cur_ptr, cur_end + 1, |
| 7324 | NULL, NULL); |
| 7325 | if (unlikely(!cur_end)) |
| 7326 | return -1; |
| 7327 | |
| 7328 | /* we have a full respnse and we know that we have either a CR |
| 7329 | * or an LF at <ptr>. |
| 7330 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7331 | 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] | 7332 | 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] | 7333 | /* there is no point trying this regex on headers */ |
| 7334 | return 1; |
| 7335 | } |
| 7336 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7337 | return done; |
| 7338 | } |
| 7339 | |
| 7340 | |
| 7341 | |
| 7342 | /* |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7343 | * 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] | 7344 | * Returns 0 if everything is alright, or -1 in case a replacement lead to an |
| 7345 | * unparsable response. |
| 7346 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7347 | 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] | 7348 | { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 7349 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7350 | struct http_txn *txn = s->txn; |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7351 | struct hdr_exp *exp; |
| 7352 | |
| 7353 | for (exp = px->rsp_exp; exp; exp = exp->next) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7354 | int ret; |
| 7355 | |
| 7356 | /* |
| 7357 | * The interleaving of transformations and verdicts |
| 7358 | * makes it difficult to decide to continue or stop |
| 7359 | * the evaluation. |
| 7360 | */ |
| 7361 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7362 | if (txn->flags & TX_SVDENY) |
| 7363 | break; |
| 7364 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7365 | if ((txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7366 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 7367 | exp->action == ACT_PASS)) { |
| 7368 | exp = exp->next; |
| 7369 | continue; |
| 7370 | } |
| 7371 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7372 | /* if this filter had a condition, evaluate it now and skip to |
| 7373 | * next filter if the condition does not match. |
| 7374 | */ |
| 7375 | if (exp->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 7376 | 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] | 7377 | ret = acl_pass(ret); |
| 7378 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 7379 | ret = !ret; |
| 7380 | if (!ret) |
| 7381 | continue; |
| 7382 | } |
| 7383 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7384 | /* Apply the filter to the status line. */ |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7385 | ret = apply_filter_to_sts_line(s, rtr, exp); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7386 | if (unlikely(ret < 0)) |
| 7387 | return -1; |
| 7388 | |
| 7389 | if (likely(ret == 0)) { |
| 7390 | /* The filter did not match the response, it can be |
| 7391 | * iterated through all headers. |
| 7392 | */ |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 7393 | if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0)) |
| 7394 | return -1; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7395 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7396 | } |
| 7397 | return 0; |
| 7398 | } |
| 7399 | |
| 7400 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7401 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 7402 | * 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] | 7403 | * desirable to call it only when needed. This function is also used when we |
| 7404 | * 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] | 7405 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7406 | void manage_server_side_cookies(struct stream *s, struct channel *res) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7407 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7408 | struct http_txn *txn = s->txn; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7409 | struct session *sess = s->sess; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 7410 | struct server *srv; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7411 | int is_cookie2; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7412 | int cur_idx, old_idx, delta; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7413 | char *hdr_beg, *hdr_end, *hdr_next; |
| 7414 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7415 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7416 | /* Iterate through the headers. |
| 7417 | * we start with the start line. |
| 7418 | */ |
| 7419 | old_idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7420 | hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7421 | |
| 7422 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 7423 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7424 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7425 | |
| 7426 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7427 | hdr_beg = hdr_next; |
| 7428 | hdr_end = hdr_beg + cur_hdr->len; |
| 7429 | hdr_next = hdr_end + cur_hdr->cr + 1; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7430 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7431 | /* We have one full header between hdr_beg and hdr_end, and the |
| 7432 | * next header starts at hdr_next. We're only interested in |
| 7433 | * "Set-Cookie" and "Set-Cookie2" headers. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7434 | */ |
| 7435 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7436 | is_cookie2 = 0; |
| 7437 | prev = hdr_beg + 10; |
| 7438 | val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10); |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7439 | if (!val) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7440 | val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11); |
| 7441 | if (!val) { |
| 7442 | old_idx = cur_idx; |
| 7443 | continue; |
| 7444 | } |
| 7445 | is_cookie2 = 1; |
| 7446 | prev = hdr_beg + 11; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7447 | } |
| 7448 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7449 | /* OK, right now we know we have a Set-Cookie* at hdr_beg, and |
| 7450 | * <prev> points to the colon. |
| 7451 | */ |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7452 | txn->flags |= TX_SCK_PRESENT; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7453 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7454 | /* Maybe we only wanted to see if there was a Set-Cookie (eg: |
| 7455 | * check-cache is enabled) and we are not interested in checking |
| 7456 | * them. Warning, the cookie capture is declared in the frontend. |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 7457 | */ |
Willy Tarreau | 53a09d5 | 2015-08-10 18:59:40 +0200 | [diff] [blame] | 7458 | if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7459 | return; |
| 7460 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7461 | /* OK so now we know we have to process this response cookie. |
| 7462 | * The format of the Set-Cookie header is slightly different |
| 7463 | * from the format of the Cookie header in that it does not |
| 7464 | * support the comma as a cookie delimiter (thus the header |
| 7465 | * cannot be folded) because the Expires attribute described in |
| 7466 | * the original Netscape's spec may contain an unquoted date |
| 7467 | * with a comma inside. We have to live with this because |
| 7468 | * many browsers don't support Max-Age and some browsers don't |
| 7469 | * support quoted strings. However the Set-Cookie2 header is |
| 7470 | * clean. |
| 7471 | * |
| 7472 | * We have to keep multiple pointers in order to support cookie |
| 7473 | * removal at the beginning, middle or end of header without |
| 7474 | * corrupting the header (in case of set-cookie2). A special |
| 7475 | * pointer, <scav> points to the beginning of the set-cookie-av |
| 7476 | * fields after the first semi-colon. The <next> pointer points |
| 7477 | * either to the end of line (set-cookie) or next unquoted comma |
| 7478 | * (set-cookie2). All of these headers are valid : |
| 7479 | * |
| 7480 | * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n |
| 7481 | * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n |
| 7482 | * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n |
| 7483 | * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n |
| 7484 | * | | | | | | | | | | |
| 7485 | * | | | | | | | | +-> next hdr_end <--+ |
| 7486 | * | | | | | | | +------------> scav |
| 7487 | * | | | | | | +--------------> val_end |
| 7488 | * | | | | | +--------------------> val_beg |
| 7489 | * | | | | +----------------------> equal |
| 7490 | * | | | +------------------------> att_end |
| 7491 | * | | +----------------------------> att_beg |
| 7492 | * | +------------------------------> prev |
| 7493 | * +-----------------------------------------> hdr_beg |
| 7494 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7495 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7496 | for (; prev < hdr_end; prev = next) { |
| 7497 | /* Iterate through all cookies on this line */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7498 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7499 | /* find att_beg */ |
| 7500 | att_beg = prev + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 7501 | while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg)) |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7502 | att_beg++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7503 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7504 | /* find att_end : this is the first character after the last non |
| 7505 | * space before the equal. It may be equal to hdr_end. |
| 7506 | */ |
| 7507 | equal = att_end = att_beg; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7508 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7509 | while (equal < hdr_end) { |
| 7510 | if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ',')) |
| 7511 | break; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 7512 | if (HTTP_IS_SPHT(*equal++)) |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7513 | continue; |
| 7514 | att_end = equal; |
| 7515 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7516 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7517 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 7518 | * is between <att_beg> and <equal>, both may be identical. |
| 7519 | */ |
| 7520 | |
| 7521 | /* look for end of cookie if there is an equal sign */ |
| 7522 | if (equal < hdr_end && *equal == '=') { |
| 7523 | /* look for the beginning of the value */ |
| 7524 | val_beg = equal + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 7525 | while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg)) |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7526 | val_beg++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7527 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7528 | /* find the end of the value, respecting quotes */ |
| 7529 | next = find_cookie_value_end(val_beg, hdr_end); |
| 7530 | |
| 7531 | /* make val_end point to the first white space or delimitor after the value */ |
| 7532 | val_end = next; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 7533 | while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1))) |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7534 | val_end--; |
| 7535 | } else { |
| 7536 | /* <equal> points to next comma, semi-colon or EOL */ |
| 7537 | val_beg = val_end = next = equal; |
| 7538 | } |
| 7539 | |
| 7540 | if (next < hdr_end) { |
| 7541 | /* Set-Cookie2 supports multiple cookies, and <next> points to |
| 7542 | * a colon or semi-colon before the end. So skip all attr-value |
| 7543 | * pairs and look for the next comma. For Set-Cookie, since |
| 7544 | * commas are permitted in values, skip to the end. |
| 7545 | */ |
| 7546 | if (is_cookie2) |
| 7547 | next = find_hdr_value_end(next, hdr_end); |
| 7548 | else |
| 7549 | next = hdr_end; |
| 7550 | } |
| 7551 | |
| 7552 | /* Now everything is as on the diagram above */ |
| 7553 | |
| 7554 | /* Ignore cookies with no equal sign */ |
| 7555 | if (equal == val_end) |
| 7556 | continue; |
| 7557 | |
| 7558 | /* If there are spaces around the equal sign, we need to |
| 7559 | * strip them otherwise we'll get trouble for cookie captures, |
| 7560 | * or even for rewrites. Since this happens extremely rarely, |
| 7561 | * it does not hurt performance. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7562 | */ |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7563 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 7564 | int stripped_before = 0; |
| 7565 | int stripped_after = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7566 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7567 | if (att_end != equal) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7568 | stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7569 | equal += stripped_before; |
| 7570 | val_beg += stripped_before; |
| 7571 | } |
| 7572 | |
| 7573 | if (val_beg > equal + 1) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7574 | stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7575 | val_beg += stripped_after; |
| 7576 | stripped_before += stripped_after; |
| 7577 | } |
| 7578 | |
| 7579 | val_end += stripped_before; |
| 7580 | next += stripped_before; |
| 7581 | hdr_end += stripped_before; |
| 7582 | hdr_next += stripped_before; |
| 7583 | cur_hdr->len += stripped_before; |
Willy Tarreau | 1fc1f45 | 2011-04-07 22:35:37 +0200 | [diff] [blame] | 7584 | http_msg_move_end(&txn->rsp, stripped_before); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7585 | } |
| 7586 | |
| 7587 | /* First, let's see if we want to capture this cookie. We check |
| 7588 | * that we don't already have a server side cookie, because we |
| 7589 | * can only capture one. Also as an optimisation, we ignore |
| 7590 | * cookies shorter than the declared name. |
| 7591 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7592 | if (sess->fe->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 7593 | txn->srv_cookie == NULL && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7594 | (val_end - att_beg >= sess->fe->capture_namelen) && |
| 7595 | memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7596 | int log_len = val_end - att_beg; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7597 | if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 7598 | ha_alert("HTTP logging : out of memory.\n"); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7599 | } |
Willy Tarreau | f70fc75 | 2010-11-19 11:27:18 +0100 | [diff] [blame] | 7600 | else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7601 | if (log_len > sess->fe->capture_len) |
| 7602 | log_len = sess->fe->capture_len; |
Willy Tarreau | f70fc75 | 2010-11-19 11:27:18 +0100 | [diff] [blame] | 7603 | memcpy(txn->srv_cookie, att_beg, log_len); |
| 7604 | txn->srv_cookie[log_len] = 0; |
| 7605 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7606 | } |
| 7607 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7608 | srv = objt_server(s->target); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7609 | /* now check if we need to process it for persistence */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 7610 | if (!(s->flags & SF_IGNORE_PRST) && |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7611 | (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) && |
| 7612 | (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) { |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7613 | /* assume passive cookie by default */ |
| 7614 | txn->flags &= ~TX_SCK_MASK; |
| 7615 | txn->flags |= TX_SCK_FOUND; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7616 | |
| 7617 | /* If the cookie is in insert mode on a known server, we'll delete |
| 7618 | * this occurrence because we'll insert another one later. |
| 7619 | * 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] | 7620 | * a direct access. |
| 7621 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7622 | if (s->be->ck_opts & PR_CK_PSV) { |
Willy Tarreau | ba4c5be | 2010-10-23 12:46:42 +0200 | [diff] [blame] | 7623 | /* The "preserve" flag was set, we don't want to touch the |
| 7624 | * server's cookie. |
| 7625 | */ |
| 7626 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7627 | else if ((srv && (s->be->ck_opts & PR_CK_INS)) || |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 7628 | ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7629 | /* this cookie must be deleted */ |
| 7630 | if (*prev == ':' && next == hdr_end) { |
| 7631 | /* whole header */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7632 | delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7633 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 7634 | txn->hdr_idx.used--; |
| 7635 | cur_hdr->len = 0; |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 7636 | cur_idx = old_idx; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7637 | hdr_next += delta; |
| 7638 | http_msg_move_end(&txn->rsp, delta); |
| 7639 | /* note: while both invalid now, <next> and <hdr_end> |
| 7640 | * are still equal, so the for() will stop as expected. |
| 7641 | */ |
| 7642 | } else { |
| 7643 | /* just remove the value */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7644 | int delta = del_hdr_value(res->buf, &prev, next); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7645 | next = prev; |
| 7646 | hdr_end += delta; |
| 7647 | hdr_next += delta; |
| 7648 | cur_hdr->len += delta; |
| 7649 | http_msg_move_end(&txn->rsp, delta); |
| 7650 | } |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7651 | txn->flags &= ~TX_SCK_MASK; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7652 | txn->flags |= TX_SCK_DELETED; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7653 | /* and go on with next cookie */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7654 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7655 | else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7656 | /* replace bytes val_beg->val_end with the cookie name associated |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7657 | * with this server since we know it. |
| 7658 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7659 | 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] | 7660 | next += delta; |
| 7661 | hdr_end += delta; |
| 7662 | hdr_next += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7663 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7664 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7665 | |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7666 | txn->flags &= ~TX_SCK_MASK; |
| 7667 | txn->flags |= TX_SCK_REPLACED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7668 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7669 | else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7670 | /* insert the cookie name associated with this server |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7671 | * before existing cookie, and insert a delimiter between them.. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7672 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7673 | 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] | 7674 | next += delta; |
| 7675 | hdr_end += delta; |
| 7676 | hdr_next += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7677 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7678 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7679 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 7680 | val_beg[srv->cklen] = COOKIE_DELIM; |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7681 | txn->flags &= ~TX_SCK_MASK; |
| 7682 | txn->flags |= TX_SCK_REPLACED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7683 | } |
| 7684 | } |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7685 | /* that's done for this cookie, check the next one on the same |
| 7686 | * line when next != hdr_end (only if is_cookie2). |
| 7687 | */ |
| 7688 | } |
| 7689 | /* check next header */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7690 | old_idx = cur_idx; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7691 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7692 | } |
| 7693 | |
| 7694 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7695 | /* |
Willy Tarreau | 0ad8e0d | 2017-12-22 15:03:36 +0100 | [diff] [blame] | 7696 | * Parses the Cache-Control and Pragma request header fields to determine if |
| 7697 | * the request may be served from the cache and/or if it is cacheable. Updates |
| 7698 | * s->txn->flags. |
| 7699 | */ |
| 7700 | void check_request_for_cacheability(struct stream *s, struct channel *chn) |
| 7701 | { |
| 7702 | struct http_txn *txn = s->txn; |
| 7703 | char *p1, *p2; |
| 7704 | char *cur_ptr, *cur_end, *cur_next; |
| 7705 | int pragma_found; |
| 7706 | int cc_found; |
| 7707 | int cur_idx; |
| 7708 | |
| 7709 | if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE) |
| 7710 | return; /* nothing more to do here */ |
| 7711 | |
| 7712 | cur_idx = 0; |
| 7713 | pragma_found = cc_found = 0; |
| 7714 | cur_next = chn->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
| 7715 | |
| 7716 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 7717 | struct hdr_idx_elem *cur_hdr; |
| 7718 | int val; |
| 7719 | |
| 7720 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 7721 | cur_ptr = cur_next; |
| 7722 | cur_end = cur_ptr + cur_hdr->len; |
| 7723 | cur_next = cur_end + cur_hdr->cr + 1; |
| 7724 | |
| 7725 | /* We have one full header between cur_ptr and cur_end, and the |
| 7726 | * next header starts at cur_next. |
| 7727 | */ |
| 7728 | |
| 7729 | val = http_header_match2(cur_ptr, cur_end, "Pragma", 6); |
| 7730 | if (val) { |
| 7731 | if ((cur_end - (cur_ptr + val) >= 8) && |
| 7732 | strncasecmp(cur_ptr + val, "no-cache", 8) == 0) { |
| 7733 | pragma_found = 1; |
| 7734 | continue; |
| 7735 | } |
| 7736 | } |
| 7737 | |
| 7738 | val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13); |
| 7739 | if (!val) |
| 7740 | continue; |
| 7741 | |
| 7742 | /* OK, right now we know we have a cache-control header at cur_ptr */ |
| 7743 | cc_found = 1; |
| 7744 | p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */ |
| 7745 | |
| 7746 | if (p1 >= cur_end) /* no more info */ |
| 7747 | continue; |
| 7748 | |
| 7749 | /* p1 is at the beginning of the value */ |
| 7750 | p2 = p1; |
| 7751 | while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2)) |
| 7752 | p2++; |
| 7753 | |
| 7754 | /* we have a complete value between p1 and p2. We don't check the |
| 7755 | * values after max-age, max-stale nor min-fresh, we simply don't |
| 7756 | * use the cache when they're specified. |
| 7757 | */ |
| 7758 | if (((p2 - p1 == 7) && strncasecmp(p1, "max-age", 7) == 0) || |
| 7759 | ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) || |
| 7760 | ((p2 - p1 == 9) && strncasecmp(p1, "max-stale", 9) == 0) || |
| 7761 | ((p2 - p1 == 9) && strncasecmp(p1, "min-fresh", 9) == 0)) { |
| 7762 | txn->flags |= TX_CACHE_IGNORE; |
| 7763 | continue; |
| 7764 | } |
| 7765 | |
| 7766 | if ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) { |
| 7767 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 7768 | continue; |
| 7769 | } |
| 7770 | } |
| 7771 | |
| 7772 | /* RFC7234#5.4: |
| 7773 | * When the Cache-Control header field is also present and |
| 7774 | * understood in a request, Pragma is ignored. |
| 7775 | * When the Cache-Control header field is not present in a |
| 7776 | * request, caches MUST consider the no-cache request |
| 7777 | * pragma-directive as having the same effect as if |
| 7778 | * "Cache-Control: no-cache" were present. |
| 7779 | */ |
| 7780 | if (!cc_found && pragma_found) |
| 7781 | txn->flags |= TX_CACHE_IGNORE; |
| 7782 | } |
| 7783 | |
| 7784 | /* |
Willy Tarreau | d3900cc | 2017-12-22 15:35:11 +0100 | [diff] [blame] | 7785 | * Check if response is cacheable or not. Updates s->txn->flags. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7786 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7787 | void check_response_for_cacheability(struct stream *s, struct channel *rtr) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7788 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7789 | struct http_txn *txn = s->txn; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7790 | char *p1, *p2; |
| 7791 | |
| 7792 | char *cur_ptr, *cur_end, *cur_next; |
| 7793 | int cur_idx; |
| 7794 | |
Willy Tarreau | 12b32f2 | 2017-12-21 16:08:09 +0100 | [diff] [blame] | 7795 | if (txn->status < 200) { |
| 7796 | /* do not try to cache interim responses! */ |
| 7797 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7798 | return; |
Willy Tarreau | 12b32f2 | 2017-12-21 16:08:09 +0100 | [diff] [blame] | 7799 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7800 | |
| 7801 | /* Iterate through the headers. |
| 7802 | * we start with the start line. |
| 7803 | */ |
| 7804 | cur_idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7805 | cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7806 | |
| 7807 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 7808 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7809 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7810 | |
| 7811 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 7812 | cur_ptr = cur_next; |
| 7813 | cur_end = cur_ptr + cur_hdr->len; |
| 7814 | cur_next = cur_end + cur_hdr->cr + 1; |
| 7815 | |
| 7816 | /* 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] | 7817 | * next header starts at cur_next. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7818 | */ |
| 7819 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7820 | val = http_header_match2(cur_ptr, cur_end, "Pragma", 6); |
| 7821 | if (val) { |
| 7822 | if ((cur_end - (cur_ptr + val) >= 8) && |
| 7823 | strncasecmp(cur_ptr + val, "no-cache", 8) == 0) { |
| 7824 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 7825 | return; |
| 7826 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7827 | } |
| 7828 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7829 | val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13); |
| 7830 | if (!val) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7831 | continue; |
| 7832 | |
| 7833 | /* OK, right now we know we have a cache-control header at cur_ptr */ |
| 7834 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7835 | p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7836 | |
| 7837 | if (p1 >= cur_end) /* no more info */ |
| 7838 | continue; |
| 7839 | |
| 7840 | /* p1 is at the beginning of the value */ |
| 7841 | p2 = p1; |
| 7842 | |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 7843 | while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7844 | p2++; |
| 7845 | |
| 7846 | /* we have a complete value between p1 and p2 */ |
| 7847 | if (p2 < cur_end && *p2 == '=') { |
Willy Tarreau | d3900cc | 2017-12-22 15:35:11 +0100 | [diff] [blame] | 7848 | if (((cur_end - p2) > 1 && (p2 - p1 == 7) && strncasecmp(p1, "max-age=0", 9) == 0) || |
| 7849 | ((cur_end - p2) > 1 && (p2 - p1 == 8) && strncasecmp(p1, "s-maxage=0", 10) == 0)) { |
| 7850 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 7851 | continue; |
| 7852 | } |
| 7853 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7854 | /* we have something of the form no-cache="set-cookie" */ |
| 7855 | if ((cur_end - p1 >= 21) && |
| 7856 | strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0 |
| 7857 | && (p1[20] == '"' || p1[20] == ',')) |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7858 | txn->flags &= ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7859 | continue; |
| 7860 | } |
| 7861 | |
| 7862 | /* OK, so we know that either p2 points to the end of string or to a comma */ |
| 7863 | if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) || |
Willy Tarreau | 5b15f90 | 2013-07-04 12:46:56 +0200 | [diff] [blame] | 7864 | ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) || |
Willy Tarreau | d3900cc | 2017-12-22 15:35:11 +0100 | [diff] [blame] | 7865 | ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0)) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7866 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7867 | return; |
| 7868 | } |
| 7869 | |
| 7870 | if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7871 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7872 | continue; |
| 7873 | } |
| 7874 | } |
| 7875 | } |
| 7876 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7877 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7878 | /* |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7879 | * 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] | 7880 | * for the current backend. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7881 | * |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7882 | * 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] | 7883 | * uri_auth field is valid. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7884 | * |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 7885 | * Returns 1 if stats should be provided, otherwise 0. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7886 | */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7887 | 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] | 7888 | { |
| 7889 | struct uri_auth *uri_auth = backend->uri_auth; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7890 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7891 | const char *uri = msg->chn->buf->p+ msg->sl.rq.u; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7892 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 7893 | if (!uri_auth) |
| 7894 | return 0; |
| 7895 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7896 | 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] | 7897 | return 0; |
| 7898 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7899 | /* check URI size */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7900 | if (uri_auth->uri_len > msg->sl.rq.u_l) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7901 | return 0; |
| 7902 | |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 7903 | if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7904 | return 0; |
| 7905 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7906 | return 1; |
| 7907 | } |
| 7908 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7909 | /* |
| 7910 | * 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] | 7911 | * By default it tries to report the error position as msg->err_pos. However if |
| 7912 | * this one is not set, it will then report msg->next, which is the last known |
| 7913 | * 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] | 7914 | * displays buffers as a contiguous area starting at buf->p. |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7915 | */ |
Emeric Brun | 8c1aaa2 | 2017-06-15 11:30:06 +0200 | [diff] [blame] | 7916 | 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] | 7917 | struct http_msg *msg, |
Willy Tarreau | 0da5b3b | 2017-09-21 09:30:46 +0200 | [diff] [blame] | 7918 | enum h1_state state, struct proxy *other_end) |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7919 | { |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 7920 | struct session *sess = strm_sess(s); |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 7921 | struct channel *chn = msg->chn; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 7922 | int len1, len2; |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 7923 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 7924 | HA_SPIN_LOCK(PROXY_LOCK, &proxy->lock); |
Willy Tarreau | f3764b7 | 2016-03-31 13:45:10 +0200 | [diff] [blame] | 7925 | es->len = MIN(chn->buf->i, global.tune.bufsize); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7926 | len1 = chn->buf->data + chn->buf->size - chn->buf->p; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 7927 | len1 = MIN(len1, es->len); |
| 7928 | len2 = es->len - len1; /* remaining data if buffer wraps */ |
| 7929 | |
Willy Tarreau | f3764b7 | 2016-03-31 13:45:10 +0200 | [diff] [blame] | 7930 | if (!es->buf) |
| 7931 | es->buf = malloc(global.tune.bufsize); |
| 7932 | |
| 7933 | if (es->buf) { |
| 7934 | memcpy(es->buf, chn->buf->p, len1); |
| 7935 | if (len2) |
| 7936 | memcpy(es->buf + len1, chn->buf->data, len2); |
| 7937 | } |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 7938 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7939 | if (msg->err_pos >= 0) |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 7940 | es->pos = msg->err_pos; |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 7941 | else |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 7942 | es->pos = msg->next; |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 7943 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7944 | es->when = date; // user-visible date |
| 7945 | es->sid = s->uniq_id; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 7946 | es->srv = objt_server(s->target); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7947 | es->oe = other_end; |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 7948 | if (objt_conn(sess->origin)) |
| 7949 | es->src = __objt_conn(sess->origin)->addr.from; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7950 | else |
| 7951 | memset(&es->src, 0, sizeof(es->src)); |
| 7952 | |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 7953 | es->state = state; |
Willy Tarreau | 10479e4 | 2010-12-12 14:00:34 +0100 | [diff] [blame] | 7954 | es->ev_id = error_snapshot_id++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 7955 | es->b_flags = chn->flags; |
Willy Tarreau | d04b1bc | 2012-05-08 11:03:10 +0200 | [diff] [blame] | 7956 | es->s_flags = s->flags; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7957 | es->t_flags = s->txn->flags; |
Willy Tarreau | d04b1bc | 2012-05-08 11:03:10 +0200 | [diff] [blame] | 7958 | es->m_flags = msg->flags; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7959 | es->b_out = chn->buf->o; |
| 7960 | es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 7961 | es->b_tot = chn->total; |
Willy Tarreau | d04b1bc | 2012-05-08 11:03:10 +0200 | [diff] [blame] | 7962 | es->m_clen = msg->chunk_len; |
| 7963 | es->m_blen = msg->body_len; |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 7964 | HA_SPIN_UNLOCK(PROXY_LOCK, &proxy->lock); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7965 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7966 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7967 | /* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of |
| 7968 | * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is |
| 7969 | * performed over the whole headers. Otherwise it must contain a valid header |
| 7970 | * context, initialised with ctx->idx=0 for the first lookup in a series. If |
| 7971 | * <occ> is positive or null, occurrence #occ from the beginning (or last ctx) |
| 7972 | * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less |
| 7973 | * 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] | 7974 | * -1. The value fetch stops at commas, so this function is suited for use with |
| 7975 | * list headers. |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7976 | * 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] | 7977 | */ |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 7978 | 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] | 7979 | struct hdr_idx *idx, int occ, |
| 7980 | struct hdr_ctx *ctx, char **vptr, int *vlen) |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7981 | { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7982 | struct hdr_ctx local_ctx; |
| 7983 | char *ptr_hist[MAX_HDR_HISTORY]; |
| 7984 | int len_hist[MAX_HDR_HISTORY]; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7985 | unsigned int hist_ptr; |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7986 | int found; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7987 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7988 | if (!ctx) { |
| 7989 | local_ctx.idx = 0; |
| 7990 | ctx = &local_ctx; |
| 7991 | } |
| 7992 | |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7993 | if (occ >= 0) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7994 | /* search from the beginning */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7995 | while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7996 | occ--; |
| 7997 | if (occ <= 0) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7998 | *vptr = ctx->line + ctx->val; |
| 7999 | *vlen = ctx->vlen; |
| 8000 | return 1; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8001 | } |
| 8002 | } |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8003 | return 0; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8004 | } |
| 8005 | |
| 8006 | /* negative occurrence, we scan all the list then walk back */ |
| 8007 | if (-occ > MAX_HDR_HISTORY) |
| 8008 | return 0; |
| 8009 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8010 | found = hist_ptr = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8011 | while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8012 | ptr_hist[hist_ptr] = ctx->line + ctx->val; |
| 8013 | len_hist[hist_ptr] = ctx->vlen; |
| 8014 | if (++hist_ptr >= MAX_HDR_HISTORY) |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8015 | hist_ptr = 0; |
| 8016 | found++; |
| 8017 | } |
| 8018 | if (-occ > found) |
| 8019 | return 0; |
| 8020 | /* 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] | 8021 | * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have |
| 8022 | * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ] |
| 8023 | * to remain in the 0..9 range. |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8024 | */ |
Willy Tarreau | 67dad27 | 2013-06-12 22:27:44 +0200 | [diff] [blame] | 8025 | hist_ptr += occ + MAX_HDR_HISTORY; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8026 | if (hist_ptr >= MAX_HDR_HISTORY) |
| 8027 | hist_ptr -= MAX_HDR_HISTORY; |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8028 | *vptr = ptr_hist[hist_ptr]; |
| 8029 | *vlen = len_hist[hist_ptr]; |
| 8030 | return 1; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8031 | } |
| 8032 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 8033 | /* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of |
| 8034 | * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is |
| 8035 | * performed over the whole headers. Otherwise it must contain a valid header |
| 8036 | * context, initialised with ctx->idx=0 for the first lookup in a series. If |
| 8037 | * <occ> is positive or null, occurrence #occ from the beginning (or last ctx) |
| 8038 | * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less |
| 8039 | * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is |
| 8040 | * -1. This function differs from http_get_hdr() in that it only returns full |
| 8041 | * line header values and does not stop at commas. |
| 8042 | * The return value is 0 if nothing was found, or non-zero otherwise. |
| 8043 | */ |
| 8044 | unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen, |
| 8045 | struct hdr_idx *idx, int occ, |
| 8046 | struct hdr_ctx *ctx, char **vptr, int *vlen) |
| 8047 | { |
| 8048 | struct hdr_ctx local_ctx; |
| 8049 | char *ptr_hist[MAX_HDR_HISTORY]; |
| 8050 | int len_hist[MAX_HDR_HISTORY]; |
| 8051 | unsigned int hist_ptr; |
| 8052 | int found; |
| 8053 | |
| 8054 | if (!ctx) { |
| 8055 | local_ctx.idx = 0; |
| 8056 | ctx = &local_ctx; |
| 8057 | } |
| 8058 | |
| 8059 | if (occ >= 0) { |
| 8060 | /* search from the beginning */ |
| 8061 | while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
| 8062 | occ--; |
| 8063 | if (occ <= 0) { |
| 8064 | *vptr = ctx->line + ctx->val; |
| 8065 | *vlen = ctx->vlen; |
| 8066 | return 1; |
| 8067 | } |
| 8068 | } |
| 8069 | return 0; |
| 8070 | } |
| 8071 | |
| 8072 | /* negative occurrence, we scan all the list then walk back */ |
| 8073 | if (-occ > MAX_HDR_HISTORY) |
| 8074 | return 0; |
| 8075 | |
| 8076 | found = hist_ptr = 0; |
| 8077 | while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
| 8078 | ptr_hist[hist_ptr] = ctx->line + ctx->val; |
| 8079 | len_hist[hist_ptr] = ctx->vlen; |
| 8080 | if (++hist_ptr >= MAX_HDR_HISTORY) |
| 8081 | hist_ptr = 0; |
| 8082 | found++; |
| 8083 | } |
| 8084 | if (-occ > found) |
| 8085 | return 0; |
Nenad Merdanovic | 69ad4b9 | 2016-03-29 13:14:30 +0200 | [diff] [blame] | 8086 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 8087 | /* 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] | 8088 | * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have |
| 8089 | * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ] |
| 8090 | * to remain in the 0..9 range. |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 8091 | */ |
Nenad Merdanovic | 69ad4b9 | 2016-03-29 13:14:30 +0200 | [diff] [blame] | 8092 | hist_ptr += occ + MAX_HDR_HISTORY; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 8093 | if (hist_ptr >= MAX_HDR_HISTORY) |
| 8094 | hist_ptr -= MAX_HDR_HISTORY; |
| 8095 | *vptr = ptr_hist[hist_ptr]; |
| 8096 | *vlen = len_hist[hist_ptr]; |
| 8097 | return 1; |
| 8098 | } |
| 8099 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 8100 | /* |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 8101 | * Print a debug line with a header. Always stop at the first CR or LF char, |
| 8102 | * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an |
| 8103 | * arrow is printed after the line which contains the pointer. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8104 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8105 | 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] | 8106 | { |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 8107 | struct session *sess = strm_sess(s); |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 8108 | int max; |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 8109 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8110 | 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] | 8111 | dir, |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 8112 | 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] | 8113 | 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] | 8114 | |
| 8115 | for (max = 0; start + max < end; max++) |
| 8116 | if (start[max] == '\r' || start[max] == '\n') |
| 8117 | break; |
| 8118 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 8119 | UBOUND(max, trash.size - trash.len - 3); |
| 8120 | trash.len += strlcpy2(trash.str + trash.len, start, max + 1); |
| 8121 | trash.str[trash.len++] = '\n'; |
Willy Tarreau | 89efaed | 2013-12-13 15:14:55 +0100 | [diff] [blame] | 8122 | shut_your_big_mouth_gcc(write(1, trash.str, trash.len)); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8123 | } |
| 8124 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8125 | |
| 8126 | /* Allocate a new HTTP transaction for stream <s> unless there is one already. |
| 8127 | * The hdr_idx is allocated as well. In case of allocation failure, everything |
| 8128 | * allocated is freed and NULL is returned. Otherwise the new transaction is |
| 8129 | * assigned to the stream and returned. |
| 8130 | */ |
| 8131 | struct http_txn *http_alloc_txn(struct stream *s) |
| 8132 | { |
| 8133 | struct http_txn *txn = s->txn; |
| 8134 | |
| 8135 | if (txn) |
| 8136 | return txn; |
| 8137 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 8138 | txn = pool_alloc(pool_head_http_txn); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8139 | if (!txn) |
| 8140 | return txn; |
| 8141 | |
| 8142 | txn->hdr_idx.size = global.tune.max_http_hdr; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 8143 | txn->hdr_idx.v = pool_alloc(pool_head_hdr_idx); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8144 | if (!txn->hdr_idx.v) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 8145 | pool_free(pool_head_http_txn, txn); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8146 | return NULL; |
| 8147 | } |
| 8148 | |
| 8149 | s->txn = txn; |
| 8150 | return txn; |
| 8151 | } |
| 8152 | |
Thierry FOURNIER | fd50f0b | 2015-09-25 18:53:18 +0200 | [diff] [blame] | 8153 | void http_txn_reset_req(struct http_txn *txn) |
| 8154 | { |
| 8155 | txn->req.flags = 0; |
| 8156 | txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */ |
| 8157 | txn->req.next = 0; |
| 8158 | txn->req.chunk_len = 0LL; |
| 8159 | txn->req.body_len = 0LL; |
| 8160 | txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */ |
| 8161 | } |
| 8162 | |
| 8163 | void http_txn_reset_res(struct http_txn *txn) |
| 8164 | { |
| 8165 | txn->rsp.flags = 0; |
| 8166 | txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */ |
| 8167 | txn->rsp.next = 0; |
| 8168 | txn->rsp.chunk_len = 0LL; |
| 8169 | txn->rsp.body_len = 0LL; |
| 8170 | txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */ |
| 8171 | } |
| 8172 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8173 | /* |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8174 | * 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] | 8175 | * the required fields are properly allocated and that we only need to (re)init |
| 8176 | * them. This should be used before processing any new request. |
| 8177 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8178 | void http_init_txn(struct stream *s) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8179 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8180 | struct http_txn *txn = s->txn; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 8181 | struct proxy *fe = strm_fe(s); |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8182 | |
| 8183 | txn->flags = 0; |
| 8184 | txn->status = -1; |
| 8185 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 8186 | txn->cookie_first_date = 0; |
| 8187 | txn->cookie_last_date = 0; |
| 8188 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8189 | txn->srv_cookie = NULL; |
| 8190 | txn->cli_cookie = NULL; |
| 8191 | txn->uri = NULL; |
| 8192 | |
Thierry FOURNIER | fd50f0b | 2015-09-25 18:53:18 +0200 | [diff] [blame] | 8193 | http_txn_reset_req(txn); |
| 8194 | http_txn_reset_res(txn); |
| 8195 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 8196 | txn->req.chn = &s->req; |
| 8197 | txn->rsp.chn = &s->res; |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 8198 | |
| 8199 | txn->auth.method = HTTP_AUTH_UNKNOWN; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8200 | |
| 8201 | txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */ |
| 8202 | if (fe->options2 & PR_O2_REQBUG_OK) |
| 8203 | txn->req.err_pos = -1; /* let buggy requests pass */ |
| 8204 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8205 | if (txn->hdr_idx.v) |
| 8206 | hdr_idx_init(&txn->hdr_idx); |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 8207 | |
| 8208 | vars_init(&s->vars_txn, SCOPE_TXN); |
| 8209 | vars_init(&s->vars_reqres, SCOPE_REQ); |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8210 | } |
| 8211 | |
| 8212 | /* to be used at the end of a transaction */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8213 | void http_end_txn(struct stream *s) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8214 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8215 | struct http_txn *txn = s->txn; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 8216 | struct proxy *fe = strm_fe(s); |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8217 | |
| 8218 | /* these ones will have been dynamically allocated */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 8219 | pool_free(pool_head_requri, txn->uri); |
| 8220 | pool_free(pool_head_capture, txn->cli_cookie); |
| 8221 | pool_free(pool_head_capture, txn->srv_cookie); |
| 8222 | pool_free(pool_head_uniqueid, s->unique_id); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 8223 | |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 8224 | s->unique_id = NULL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8225 | txn->uri = NULL; |
| 8226 | txn->srv_cookie = NULL; |
| 8227 | txn->cli_cookie = NULL; |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8228 | |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 8229 | if (s->req_cap) { |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8230 | struct cap_hdr *h; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 8231 | for (h = fe->req_cap; h; h = h->next) |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 8232 | pool_free(h->pool, s->req_cap[h->index]); |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 8233 | memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *)); |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8234 | } |
| 8235 | |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 8236 | if (s->res_cap) { |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8237 | struct cap_hdr *h; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 8238 | for (h = fe->rsp_cap; h; h = h->next) |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 8239 | pool_free(h->pool, s->res_cap[h->index]); |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 8240 | memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *)); |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8241 | } |
| 8242 | |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 8243 | vars_prune(&s->vars_txn, s->sess, s); |
| 8244 | vars_prune(&s->vars_reqres, s->sess, s); |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8245 | } |
| 8246 | |
| 8247 | /* 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] | 8248 | void http_reset_txn(struct stream *s) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8249 | { |
| 8250 | http_end_txn(s); |
| 8251 | http_init_txn(s); |
| 8252 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 8253 | /* reinitialise the current rule list pointer to NULL. We are sure that |
| 8254 | * any rulelist match the NULL pointer. |
| 8255 | */ |
| 8256 | s->current_rule_list = NULL; |
| 8257 | |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 8258 | s->be = strm_fe(s); |
| 8259 | s->logs.logwait = strm_fe(s)->to_log; |
Willy Tarreau | abcd514 | 2013-06-11 17:18:02 +0200 | [diff] [blame] | 8260 | s->logs.level = 0; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8261 | stream_del_srv_conn(s); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 8262 | s->target = NULL; |
Emeric Brun | b982a3d | 2010-01-04 15:45:53 +0100 | [diff] [blame] | 8263 | /* re-init store persistence */ |
| 8264 | s->store_count = 0; |
Willy Tarreau | 1f0da24 | 2014-01-25 11:01:50 +0100 | [diff] [blame] | 8265 | s->uniq_id = global.req_count++; |
Emeric Brun | b982a3d | 2010-01-04 15:45:53 +0100 | [diff] [blame] | 8266 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 8267 | s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */ |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8268 | |
Willy Tarreau | 739cfba | 2010-01-25 23:11:14 +0100 | [diff] [blame] | 8269 | /* We must trim any excess data from the response buffer, because we |
| 8270 | * may have blocked an invalid response from a server that we don't |
| 8271 | * want to accidentely forward once we disable the analysers, nor do |
| 8272 | * we want those data to come along with next response. A typical |
| 8273 | * example of such data would be from a buggy server responding to |
| 8274 | * a HEAD with some data, or sending more than the advertised |
| 8275 | * content-length. |
| 8276 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 8277 | if (unlikely(s->res.buf->i)) |
| 8278 | s->res.buf->i = 0; |
Willy Tarreau | 739cfba | 2010-01-25 23:11:14 +0100 | [diff] [blame] | 8279 | |
Christopher Faulet | c0c672a | 2017-03-28 11:51:33 +0200 | [diff] [blame] | 8280 | /* Now we can realign the response buffer */ |
| 8281 | buffer_realign(s->res.buf); |
| 8282 | |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 8283 | s->req.rto = strm_fe(s)->timeout.client; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 8284 | s->req.wto = TICK_ETERNITY; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8285 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 8286 | s->res.rto = TICK_ETERNITY; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 8287 | s->res.wto = strm_fe(s)->timeout.client; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8288 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 8289 | s->req.rex = TICK_ETERNITY; |
| 8290 | s->req.wex = TICK_ETERNITY; |
| 8291 | s->req.analyse_exp = TICK_ETERNITY; |
| 8292 | s->res.rex = TICK_ETERNITY; |
| 8293 | s->res.wex = TICK_ETERNITY; |
| 8294 | s->res.analyse_exp = TICK_ETERNITY; |
Hongbo Long | e39683c | 2017-03-10 18:41:51 +0100 | [diff] [blame] | 8295 | s->si[1].hcto = TICK_ETERNITY; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8296 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8297 | |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 8298 | void free_http_res_rules(struct list *r) |
| 8299 | { |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 8300 | struct act_rule *tr, *pr; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 8301 | |
| 8302 | list_for_each_entry_safe(pr, tr, r, list) { |
| 8303 | LIST_DEL(&pr->list); |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 8304 | regex_free(&pr->arg.hdr_add.re); |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 8305 | free(pr); |
| 8306 | } |
| 8307 | } |
| 8308 | |
| 8309 | void free_http_req_rules(struct list *r) |
| 8310 | { |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 8311 | struct act_rule *tr, *pr; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8312 | |
| 8313 | list_for_each_entry_safe(pr, tr, r, list) { |
| 8314 | LIST_DEL(&pr->list); |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8315 | if (pr->action == ACT_HTTP_REQ_AUTH) |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 8316 | free(pr->arg.auth.realm); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8317 | |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 8318 | regex_free(&pr->arg.hdr_add.re); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8319 | free(pr); |
| 8320 | } |
| 8321 | } |
| 8322 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8323 | /* parse an "http-request" rule */ |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 8324 | 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] | 8325 | { |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 8326 | struct act_rule *rule; |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 8327 | struct action_kw *custom = NULL; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8328 | int cur_arg; |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 8329 | char *error; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8330 | |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 8331 | rule = calloc(1, sizeof(*rule)); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8332 | if (!rule) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8333 | ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8334 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8335 | } |
| 8336 | |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 8337 | if (!strcmp(args[0], "allow")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8338 | rule->action = ACT_ACTION_ALLOW; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8339 | cur_arg = 1; |
Jarno Huuskonen | 800d176 | 2017-03-06 14:56:36 +0200 | [diff] [blame] | 8340 | } 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] | 8341 | int code; |
| 8342 | int hc; |
| 8343 | |
Jarno Huuskonen | 800d176 | 2017-03-06 14:56:36 +0200 | [diff] [blame] | 8344 | if (!strcmp(args[0], "tarpit")) { |
| 8345 | rule->action = ACT_HTTP_REQ_TARPIT; |
| 8346 | rule->deny_status = HTTP_ERR_500; |
| 8347 | } |
| 8348 | else { |
| 8349 | rule->action = ACT_ACTION_DENY; |
| 8350 | rule->deny_status = HTTP_ERR_403; |
| 8351 | } |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8352 | cur_arg = 1; |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 8353 | if (strcmp(args[cur_arg], "deny_status") == 0) { |
| 8354 | cur_arg++; |
| 8355 | if (!args[cur_arg]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8356 | ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing status code.\n", |
| 8357 | file, linenum, proxy_type_str(proxy), proxy->id, args[0]); |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 8358 | goto out_err; |
| 8359 | } |
| 8360 | |
| 8361 | code = atol(args[cur_arg]); |
| 8362 | cur_arg++; |
| 8363 | for (hc = 0; hc < HTTP_ERR_SIZE; hc++) { |
| 8364 | if (http_err_codes[hc] == code) { |
| 8365 | rule->deny_status = hc; |
| 8366 | break; |
| 8367 | } |
| 8368 | } |
| 8369 | |
| 8370 | if (hc >= HTTP_ERR_SIZE) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8371 | ha_warning("parsing [%s:%d] : status code %d not handled, using default code %d.\n", |
| 8372 | file, linenum, code, http_err_codes[rule->deny_status]); |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 8373 | } |
| 8374 | } |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8375 | } else if (!strcmp(args[0], "auth")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8376 | rule->action = ACT_HTTP_REQ_AUTH; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8377 | cur_arg = 1; |
| 8378 | |
| 8379 | while(*args[cur_arg]) { |
| 8380 | if (!strcmp(args[cur_arg], "realm")) { |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 8381 | rule->arg.auth.realm = strdup(args[cur_arg + 1]); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8382 | cur_arg+=2; |
| 8383 | continue; |
| 8384 | } else |
| 8385 | break; |
| 8386 | } |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 8387 | } else if (!strcmp(args[0], "set-nice")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8388 | rule->action = ACT_HTTP_SET_NICE; |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 8389 | cur_arg = 1; |
| 8390 | |
| 8391 | if (!*args[cur_arg] || |
| 8392 | (*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] | 8393 | ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n", |
| 8394 | file, linenum, args[0]); |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 8395 | goto out_err; |
| 8396 | } |
| 8397 | rule->arg.nice = atoi(args[cur_arg]); |
| 8398 | if (rule->arg.nice < -1024) |
| 8399 | rule->arg.nice = -1024; |
| 8400 | else if (rule->arg.nice > 1024) |
| 8401 | rule->arg.nice = 1024; |
| 8402 | cur_arg++; |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 8403 | } else if (!strcmp(args[0], "set-tos")) { |
| 8404 | #ifdef IP_TOS |
| 8405 | char *err; |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8406 | rule->action = ACT_HTTP_SET_TOS; |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 8407 | cur_arg = 1; |
| 8408 | |
| 8409 | if (!*args[cur_arg] || |
| 8410 | (*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] | 8411 | ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n", |
| 8412 | file, linenum, args[0]); |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 8413 | goto out_err; |
| 8414 | } |
| 8415 | |
| 8416 | rule->arg.tos = strtol(args[cur_arg], &err, 0); |
| 8417 | if (err && *err != '\0') { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8418 | ha_alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n", |
| 8419 | file, linenum, err, args[0]); |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 8420 | goto out_err; |
| 8421 | } |
| 8422 | cur_arg++; |
| 8423 | #else |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8424 | 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] | 8425 | goto out_err; |
| 8426 | #endif |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 8427 | } else if (!strcmp(args[0], "set-mark")) { |
| 8428 | #ifdef SO_MARK |
| 8429 | char *err; |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8430 | rule->action = ACT_HTTP_SET_MARK; |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 8431 | cur_arg = 1; |
| 8432 | |
| 8433 | if (!*args[cur_arg] || |
| 8434 | (*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] | 8435 | ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n", |
| 8436 | file, linenum, args[0]); |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 8437 | goto out_err; |
| 8438 | } |
| 8439 | |
| 8440 | rule->arg.mark = strtoul(args[cur_arg], &err, 0); |
| 8441 | if (err && *err != '\0') { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8442 | ha_alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n", |
| 8443 | file, linenum, err, args[0]); |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 8444 | goto out_err; |
| 8445 | } |
| 8446 | cur_arg++; |
| 8447 | global.last_checks |= LSTCHK_NETADM; |
| 8448 | #else |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8449 | 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] | 8450 | goto out_err; |
| 8451 | #endif |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 8452 | } else if (!strcmp(args[0], "set-log-level")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8453 | rule->action = ACT_HTTP_SET_LOGL; |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 8454 | cur_arg = 1; |
| 8455 | |
| 8456 | if (!*args[cur_arg] || |
| 8457 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 8458 | bad_log_level: |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8459 | ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n", |
| 8460 | file, linenum, args[0]); |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 8461 | goto out_err; |
| 8462 | } |
| 8463 | if (strcmp(args[cur_arg], "silent") == 0) |
| 8464 | rule->arg.loglevel = -1; |
| 8465 | else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0) |
| 8466 | goto bad_log_level; |
| 8467 | cur_arg++; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 8468 | } 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] | 8469 | 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] | 8470 | cur_arg = 1; |
| 8471 | |
Willy Tarreau | 8d1c516 | 2013-04-03 14:13:58 +0200 | [diff] [blame] | 8472 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 8473 | (*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] | 8474 | ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n", |
| 8475 | file, linenum, args[0]); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8476 | goto out_err; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 8477 | } |
| 8478 | |
| 8479 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 8480 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 8481 | LIST_INIT(&rule->arg.hdr_add.fmt); |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 8482 | |
Thierry FOURNIER | 1c0054f | 2013-11-20 15:09:52 +0100 | [diff] [blame] | 8483 | proxy->conf.args.ctx = ARGC_HRQ; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8484 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8485 | 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] | 8486 | (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] | 8487 | ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n", |
| 8488 | file, linenum, args[0], error); |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8489 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8490 | goto out_err; |
| 8491 | } |
Willy Tarreau | 59ad1a2 | 2014-01-29 14:39:58 +0100 | [diff] [blame] | 8492 | free(proxy->conf.lfs_file); |
| 8493 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 8494 | proxy->conf.lfs_line = proxy->conf.args.line; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 8495 | cur_arg += 2; |
Willy Tarreau | b854392 | 2014-06-17 18:58:26 +0200 | [diff] [blame] | 8496 | } 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] | 8497 | 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] | 8498 | cur_arg = 1; |
| 8499 | |
| 8500 | if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] || |
Baptiste Assmann | 92df370 | 2014-06-24 11:10:00 +0200 | [diff] [blame] | 8501 | (*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] | 8502 | ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n", |
| 8503 | file, linenum, args[0]); |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 8504 | goto out_err; |
| 8505 | } |
| 8506 | |
| 8507 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 8508 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 8509 | LIST_INIT(&rule->arg.hdr_add.fmt); |
| 8510 | |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 8511 | error = NULL; |
| 8512 | 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] | 8513 | ha_alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum, |
| 8514 | args[cur_arg + 1], error); |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 8515 | free(error); |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 8516 | goto out_err; |
| 8517 | } |
| 8518 | |
| 8519 | proxy->conf.args.ctx = ARGC_HRQ; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8520 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8521 | 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] | 8522 | (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] | 8523 | ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n", |
| 8524 | file, linenum, args[0], error); |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8525 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8526 | goto out_err; |
| 8527 | } |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 8528 | |
| 8529 | free(proxy->conf.lfs_file); |
| 8530 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 8531 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 8532 | cur_arg += 3; |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 8533 | } else if (strcmp(args[0], "del-header") == 0) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8534 | rule->action = ACT_HTTP_DEL_HDR; |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 8535 | cur_arg = 1; |
| 8536 | |
| 8537 | if (!*args[cur_arg] || |
| 8538 | (*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] | 8539 | ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 8540 | file, linenum, args[0]); |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 8541 | goto out_err; |
| 8542 | } |
| 8543 | |
| 8544 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 8545 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 8546 | |
| 8547 | proxy->conf.args.ctx = ARGC_HRQ; |
| 8548 | free(proxy->conf.lfs_file); |
| 8549 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 8550 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 8551 | cur_arg += 1; |
Frédéric Lécaille | a41d531 | 2018-01-29 12:05:07 +0100 | [diff] [blame] | 8552 | } else if (strncmp(args[0], "track-sc", 8) == 0) { |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 8553 | struct sample_expr *expr; |
| 8554 | unsigned int where; |
| 8555 | char *err = NULL; |
Frédéric Lécaille | a41d531 | 2018-01-29 12:05:07 +0100 | [diff] [blame] | 8556 | unsigned int tsc_num; |
| 8557 | const char *tsc_num_str; |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 8558 | |
| 8559 | cur_arg = 1; |
| 8560 | proxy->conf.args.ctx = ARGC_TRK; |
| 8561 | |
Frédéric Lécaille | a41d531 | 2018-01-29 12:05:07 +0100 | [diff] [blame] | 8562 | tsc_num_str = &args[0][8]; |
| 8563 | if (cfg_parse_track_sc_num(&tsc_num, tsc_num_str, tsc_num_str + strlen(tsc_num_str), &err) == -1) { |
| 8564 | ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n", |
| 8565 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], err); |
| 8566 | free(err); |
| 8567 | goto out_err; |
| 8568 | } |
| 8569 | |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 8570 | expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args); |
| 8571 | if (!expr) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8572 | ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n", |
| 8573 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], err); |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 8574 | free(err); |
| 8575 | goto out_err; |
| 8576 | } |
| 8577 | |
| 8578 | where = 0; |
| 8579 | if (proxy->cap & PR_CAP_FE) |
| 8580 | where |= SMP_VAL_FE_HRQ_HDR; |
| 8581 | if (proxy->cap & PR_CAP_BE) |
| 8582 | where |= SMP_VAL_BE_HRQ_HDR; |
| 8583 | |
| 8584 | if (!(expr->fetch->val & where)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8585 | ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :" |
| 8586 | " fetch method '%s' extracts information from '%s', none of which is available here.\n", |
| 8587 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], |
| 8588 | args[cur_arg-1], sample_src_names(expr->fetch->use)); |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 8589 | free(expr); |
| 8590 | goto out_err; |
| 8591 | } |
| 8592 | |
| 8593 | if (strcmp(args[cur_arg], "table") == 0) { |
| 8594 | cur_arg++; |
| 8595 | if (!args[cur_arg]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8596 | ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing table name.\n", |
| 8597 | file, linenum, proxy_type_str(proxy), proxy->id, args[0]); |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 8598 | free(expr); |
| 8599 | goto out_err; |
| 8600 | } |
| 8601 | /* we copy the table name for now, it will be resolved later */ |
Thierry FOURNIER | 5ec63e0 | 2015-08-04 09:09:48 +0200 | [diff] [blame] | 8602 | rule->arg.trk_ctr.table.n = strdup(args[cur_arg]); |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 8603 | cur_arg++; |
| 8604 | } |
Thierry FOURNIER | 5ec63e0 | 2015-08-04 09:09:48 +0200 | [diff] [blame] | 8605 | rule->arg.trk_ctr.expr = expr; |
Frédéric Lécaille | a41d531 | 2018-01-29 12:05:07 +0100 | [diff] [blame] | 8606 | rule->action = ACT_ACTION_TRK_SC0 + tsc_num; |
Christopher Faulet | 78880fb | 2017-09-18 14:43:55 +0200 | [diff] [blame] | 8607 | rule->check_ptr = check_trk_action; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8608 | } else if (strcmp(args[0], "redirect") == 0) { |
| 8609 | struct redirect_rule *redir; |
Willy Tarreau | 6d4890c | 2013-11-18 18:04:25 +0100 | [diff] [blame] | 8610 | char *errmsg = NULL; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8611 | |
Willy Tarreau | be4653b | 2015-05-28 15:26:58 +0200 | [diff] [blame] | 8612 | 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] | 8613 | ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n", |
| 8614 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8615 | goto out_err; |
| 8616 | } |
| 8617 | |
| 8618 | /* this redirect rule might already contain a parsed condition which |
| 8619 | * we'll pass to the http-request rule. |
| 8620 | */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8621 | rule->action = ACT_HTTP_REDIR; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8622 | rule->arg.redir = redir; |
| 8623 | rule->cond = redir->cond; |
| 8624 | redir->cond = NULL; |
| 8625 | cur_arg = 2; |
| 8626 | return rule; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8627 | } else if (strncmp(args[0], "add-acl", 7) == 0) { |
| 8628 | /* http-request add-acl(<reference (acl name)>) <key pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8629 | rule->action = ACT_HTTP_ADD_ACL; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8630 | /* |
| 8631 | * '+ 8' for 'add-acl(' |
| 8632 | * '- 9' for 'add-acl(' + trailing ')' |
| 8633 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 8634 | 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] | 8635 | |
| 8636 | cur_arg = 1; |
| 8637 | |
| 8638 | if (!*args[cur_arg] || |
| 8639 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8640 | ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 8641 | file, linenum, args[0]); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8642 | goto out_err; |
| 8643 | } |
| 8644 | |
| 8645 | LIST_INIT(&rule->arg.map.key); |
| 8646 | proxy->conf.args.ctx = ARGC_HRQ; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8647 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8648 | 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] | 8649 | (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] | 8650 | ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n", |
| 8651 | file, linenum, args[0], error); |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8652 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8653 | goto out_err; |
| 8654 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8655 | free(proxy->conf.lfs_file); |
| 8656 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 8657 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 8658 | cur_arg += 1; |
| 8659 | } else if (strncmp(args[0], "del-acl", 7) == 0) { |
| 8660 | /* http-request del-acl(<reference (acl name)>) <key pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8661 | rule->action = ACT_HTTP_DEL_ACL; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8662 | /* |
| 8663 | * '+ 8' for 'del-acl(' |
| 8664 | * '- 9' for 'del-acl(' + trailing ')' |
| 8665 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 8666 | 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] | 8667 | |
| 8668 | cur_arg = 1; |
| 8669 | |
| 8670 | if (!*args[cur_arg] || |
| 8671 | (*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] | 8672 | ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 8673 | file, linenum, args[0]); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8674 | goto out_err; |
| 8675 | } |
| 8676 | |
| 8677 | LIST_INIT(&rule->arg.map.key); |
| 8678 | proxy->conf.args.ctx = ARGC_HRQ; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8679 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8680 | 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] | 8681 | (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] | 8682 | ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n", |
| 8683 | file, linenum, args[0], error); |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8684 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8685 | goto out_err; |
| 8686 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8687 | free(proxy->conf.lfs_file); |
| 8688 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 8689 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 8690 | cur_arg += 1; |
| 8691 | } else if (strncmp(args[0], "del-map", 7) == 0) { |
| 8692 | /* http-request del-map(<reference (map name)>) <key pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8693 | rule->action = ACT_HTTP_DEL_MAP; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8694 | /* |
| 8695 | * '+ 8' for 'del-map(' |
| 8696 | * '- 9' for 'del-map(' + trailing ')' |
| 8697 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 8698 | 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] | 8699 | |
| 8700 | cur_arg = 1; |
| 8701 | |
| 8702 | if (!*args[cur_arg] || |
| 8703 | (*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] | 8704 | ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 8705 | file, linenum, args[0]); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8706 | goto out_err; |
| 8707 | } |
| 8708 | |
| 8709 | LIST_INIT(&rule->arg.map.key); |
| 8710 | proxy->conf.args.ctx = ARGC_HRQ; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8711 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8712 | 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] | 8713 | (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] | 8714 | ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n", |
| 8715 | file, linenum, args[0], error); |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8716 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8717 | goto out_err; |
| 8718 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8719 | free(proxy->conf.lfs_file); |
| 8720 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 8721 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 8722 | cur_arg += 1; |
| 8723 | } else if (strncmp(args[0], "set-map", 7) == 0) { |
| 8724 | /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8725 | rule->action = ACT_HTTP_SET_MAP; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8726 | /* |
| 8727 | * '+ 8' for 'set-map(' |
| 8728 | * '- 9' for 'set-map(' + trailing ')' |
| 8729 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 8730 | 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] | 8731 | |
| 8732 | cur_arg = 1; |
| 8733 | |
| 8734 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 8735 | (*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] | 8736 | ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n", |
| 8737 | file, linenum, args[0]); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8738 | goto out_err; |
| 8739 | } |
| 8740 | |
| 8741 | LIST_INIT(&rule->arg.map.key); |
| 8742 | LIST_INIT(&rule->arg.map.value); |
| 8743 | proxy->conf.args.ctx = ARGC_HRQ; |
| 8744 | |
| 8745 | /* key pattern */ |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8746 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8747 | 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] | 8748 | (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] | 8749 | ha_alert("parsing [%s:%d]: 'http-request %s' key: %s.\n", |
| 8750 | file, linenum, args[0], error); |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8751 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8752 | goto out_err; |
| 8753 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8754 | |
| 8755 | /* value pattern */ |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8756 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8757 | 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] | 8758 | (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] | 8759 | ha_alert("parsing [%s:%d]: 'http-request %s' pattern: %s.\n", |
| 8760 | file, linenum, args[0], error); |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8761 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8762 | goto out_err; |
| 8763 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8764 | free(proxy->conf.lfs_file); |
| 8765 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 8766 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 8767 | |
| 8768 | cur_arg += 2; |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 8769 | } else if (((custom = action_http_req_custom(args[0])) != NULL)) { |
| 8770 | char *errmsg = NULL; |
| 8771 | cur_arg = 1; |
| 8772 | /* try in the module list */ |
Thierry FOURNIER | 5563e4b | 2015-08-14 19:20:07 +0200 | [diff] [blame] | 8773 | rule->from = ACT_F_HTTP_REQ; |
Thierry FOURNIER | 85c6c97 | 2015-09-22 19:14:35 +0200 | [diff] [blame] | 8774 | rule->kw = custom; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 8775 | 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] | 8776 | ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n", |
| 8777 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 8778 | free(errmsg); |
| 8779 | goto out_err; |
| 8780 | } |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8781 | } else { |
Thierry FOURNIER | ab95e65 | 2015-10-02 08:24:51 +0200 | [diff] [blame] | 8782 | action_build_list(&http_req_keywords.list, &trash); |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8783 | ha_alert("parsing [%s:%d]: 'http-request' expects 'allow', 'deny', 'auth', 'redirect', " |
| 8784 | "'tarpit', 'add-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', " |
| 8785 | "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'track-sc*'" |
| 8786 | "%s%s, but got '%s'%s.\n", |
| 8787 | file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)"); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8788 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8789 | } |
| 8790 | |
| 8791 | if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) { |
| 8792 | struct acl_cond *cond; |
Willy Tarreau | b7451bb | 2012-04-27 12:38:15 +0200 | [diff] [blame] | 8793 | char *errmsg = NULL; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8794 | |
Christopher Faulet | 1b421ea | 2017-09-22 14:38:56 +0200 | [diff] [blame] | 8795 | 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] | 8796 | ha_alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n", |
| 8797 | file, linenum, args[0], errmsg); |
Willy Tarreau | b7451bb | 2012-04-27 12:38:15 +0200 | [diff] [blame] | 8798 | free(errmsg); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8799 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8800 | } |
| 8801 | rule->cond = cond; |
| 8802 | } |
| 8803 | else if (*args[cur_arg]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8804 | ha_alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or" |
| 8805 | " either 'if' or 'unless' followed by a condition but found '%s'.\n", |
| 8806 | file, linenum, args[0], args[cur_arg]); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8807 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8808 | } |
| 8809 | |
| 8810 | return rule; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8811 | out_err: |
| 8812 | free(rule); |
| 8813 | return NULL; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8814 | } |
| 8815 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8816 | /* parse an "http-respose" rule */ |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 8817 | 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] | 8818 | { |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 8819 | struct act_rule *rule; |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 8820 | struct action_kw *custom = NULL; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8821 | int cur_arg; |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 8822 | char *error; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8823 | |
| 8824 | rule = calloc(1, sizeof(*rule)); |
| 8825 | if (!rule) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8826 | ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum); |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8827 | goto out_err; |
| 8828 | } |
| 8829 | |
| 8830 | if (!strcmp(args[0], "allow")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8831 | rule->action = ACT_ACTION_ALLOW; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8832 | cur_arg = 1; |
| 8833 | } else if (!strcmp(args[0], "deny")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8834 | rule->action = ACT_ACTION_DENY; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8835 | cur_arg = 1; |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 8836 | } else if (!strcmp(args[0], "set-nice")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8837 | rule->action = ACT_HTTP_SET_NICE; |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 8838 | cur_arg = 1; |
| 8839 | |
| 8840 | if (!*args[cur_arg] || |
| 8841 | (*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] | 8842 | ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n", |
| 8843 | file, linenum, args[0]); |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 8844 | goto out_err; |
| 8845 | } |
| 8846 | rule->arg.nice = atoi(args[cur_arg]); |
| 8847 | if (rule->arg.nice < -1024) |
| 8848 | rule->arg.nice = -1024; |
| 8849 | else if (rule->arg.nice > 1024) |
| 8850 | rule->arg.nice = 1024; |
| 8851 | cur_arg++; |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 8852 | } else if (!strcmp(args[0], "set-tos")) { |
| 8853 | #ifdef IP_TOS |
| 8854 | char *err; |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8855 | rule->action = ACT_HTTP_SET_TOS; |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 8856 | cur_arg = 1; |
| 8857 | |
| 8858 | if (!*args[cur_arg] || |
| 8859 | (*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] | 8860 | ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n", |
| 8861 | file, linenum, args[0]); |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 8862 | goto out_err; |
| 8863 | } |
| 8864 | |
| 8865 | rule->arg.tos = strtol(args[cur_arg], &err, 0); |
| 8866 | if (err && *err != '\0') { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8867 | ha_alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n", |
| 8868 | file, linenum, err, args[0]); |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 8869 | goto out_err; |
| 8870 | } |
| 8871 | cur_arg++; |
| 8872 | #else |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8873 | 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] | 8874 | goto out_err; |
| 8875 | #endif |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 8876 | } else if (!strcmp(args[0], "set-mark")) { |
| 8877 | #ifdef SO_MARK |
| 8878 | char *err; |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8879 | rule->action = ACT_HTTP_SET_MARK; |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 8880 | cur_arg = 1; |
| 8881 | |
| 8882 | if (!*args[cur_arg] || |
| 8883 | (*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] | 8884 | ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n", |
| 8885 | file, linenum, args[0]); |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 8886 | goto out_err; |
| 8887 | } |
| 8888 | |
| 8889 | rule->arg.mark = strtoul(args[cur_arg], &err, 0); |
| 8890 | if (err && *err != '\0') { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8891 | ha_alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n", |
| 8892 | file, linenum, err, args[0]); |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 8893 | goto out_err; |
| 8894 | } |
| 8895 | cur_arg++; |
| 8896 | global.last_checks |= LSTCHK_NETADM; |
| 8897 | #else |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8898 | 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] | 8899 | goto out_err; |
| 8900 | #endif |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 8901 | } else if (!strcmp(args[0], "set-log-level")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8902 | rule->action = ACT_HTTP_SET_LOGL; |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 8903 | cur_arg = 1; |
| 8904 | |
| 8905 | if (!*args[cur_arg] || |
| 8906 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 8907 | bad_log_level: |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8908 | ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n", |
| 8909 | file, linenum, args[0]); |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 8910 | goto out_err; |
| 8911 | } |
| 8912 | if (strcmp(args[cur_arg], "silent") == 0) |
| 8913 | rule->arg.loglevel = -1; |
Ruoshan Huang | dd01678 | 2016-06-15 22:16:03 +0800 | [diff] [blame] | 8914 | 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] | 8915 | goto bad_log_level; |
| 8916 | cur_arg++; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8917 | } 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] | 8918 | 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] | 8919 | cur_arg = 1; |
| 8920 | |
| 8921 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 8922 | (*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] | 8923 | ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n", |
| 8924 | file, linenum, args[0]); |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8925 | goto out_err; |
| 8926 | } |
| 8927 | |
| 8928 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 8929 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 8930 | LIST_INIT(&rule->arg.hdr_add.fmt); |
| 8931 | |
Thierry FOURNIER | 1c0054f | 2013-11-20 15:09:52 +0100 | [diff] [blame] | 8932 | proxy->conf.args.ctx = ARGC_HRS; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8933 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8934 | 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] | 8935 | (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] | 8936 | ha_alert("parsing [%s:%d]: 'http-response %s': %s.\n", |
| 8937 | file, linenum, args[0], error); |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8938 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8939 | goto out_err; |
| 8940 | } |
Willy Tarreau | 59ad1a2 | 2014-01-29 14:39:58 +0100 | [diff] [blame] | 8941 | free(proxy->conf.lfs_file); |
| 8942 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 8943 | proxy->conf.lfs_line = proxy->conf.args.line; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8944 | cur_arg += 2; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 8945 | } 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] | 8946 | 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] | 8947 | cur_arg = 1; |
| 8948 | |
| 8949 | if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] || |
Baptiste Assmann | 12cb00b | 2014-08-08 17:29:06 +0200 | [diff] [blame] | 8950 | (*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] | 8951 | ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n", |
| 8952 | file, linenum, args[0]); |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 8953 | goto out_err; |
| 8954 | } |
| 8955 | |
| 8956 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 8957 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 8958 | LIST_INIT(&rule->arg.hdr_add.fmt); |
| 8959 | |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 8960 | error = NULL; |
| 8961 | 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] | 8962 | ha_alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum, |
| 8963 | args[cur_arg + 1], error); |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 8964 | free(error); |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 8965 | goto out_err; |
| 8966 | } |
| 8967 | |
| 8968 | proxy->conf.args.ctx = ARGC_HRQ; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8969 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8970 | 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] | 8971 | (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] | 8972 | ha_alert("parsing [%s:%d]: 'http-response %s': %s.\n", |
| 8973 | file, linenum, args[0], error); |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8974 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8975 | goto out_err; |
| 8976 | } |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 8977 | |
| 8978 | free(proxy->conf.lfs_file); |
| 8979 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 8980 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 8981 | cur_arg += 3; |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 8982 | } else if (strcmp(args[0], "del-header") == 0) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8983 | rule->action = ACT_HTTP_DEL_HDR; |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 8984 | cur_arg = 1; |
| 8985 | |
| 8986 | if (!*args[cur_arg] || |
| 8987 | (*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] | 8988 | ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 8989 | file, linenum, args[0]); |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 8990 | goto out_err; |
| 8991 | } |
| 8992 | |
| 8993 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 8994 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 8995 | |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8996 | proxy->conf.args.ctx = ARGC_HRS; |
| 8997 | free(proxy->conf.lfs_file); |
| 8998 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 8999 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9000 | cur_arg += 1; |
| 9001 | } else if (strncmp(args[0], "add-acl", 7) == 0) { |
| 9002 | /* http-request add-acl(<reference (acl name)>) <key pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9003 | rule->action = ACT_HTTP_ADD_ACL; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9004 | /* |
| 9005 | * '+ 8' for 'add-acl(' |
| 9006 | * '- 9' for 'add-acl(' + trailing ')' |
| 9007 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9008 | 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] | 9009 | |
| 9010 | cur_arg = 1; |
| 9011 | |
| 9012 | if (!*args[cur_arg] || |
| 9013 | (*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] | 9014 | ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 9015 | file, linenum, args[0]); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9016 | goto out_err; |
| 9017 | } |
| 9018 | |
| 9019 | LIST_INIT(&rule->arg.map.key); |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9020 | proxy->conf.args.ctx = ARGC_HRS; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9021 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9022 | 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] | 9023 | (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] | 9024 | ha_alert("parsing [%s:%d]: 'http-response %s': %s.\n", |
| 9025 | file, linenum, args[0], error); |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9026 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9027 | goto out_err; |
| 9028 | } |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9029 | free(proxy->conf.lfs_file); |
| 9030 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9031 | proxy->conf.lfs_line = proxy->conf.args.line; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9032 | |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9033 | cur_arg += 1; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9034 | } else if (strncmp(args[0], "del-acl", 7) == 0) { |
| 9035 | /* http-response del-acl(<reference (acl name)>) <key pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9036 | rule->action = ACT_HTTP_DEL_ACL; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9037 | /* |
| 9038 | * '+ 8' for 'del-acl(' |
| 9039 | * '- 9' for 'del-acl(' + trailing ')' |
| 9040 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9041 | 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] | 9042 | |
| 9043 | cur_arg = 1; |
| 9044 | |
| 9045 | if (!*args[cur_arg] || |
| 9046 | (*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] | 9047 | ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 9048 | file, linenum, args[0]); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9049 | goto out_err; |
| 9050 | } |
| 9051 | |
| 9052 | LIST_INIT(&rule->arg.map.key); |
| 9053 | proxy->conf.args.ctx = ARGC_HRS; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9054 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9055 | 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] | 9056 | (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] | 9057 | ha_alert("parsing [%s:%d]: 'http-response %s': %s.\n", |
| 9058 | file, linenum, args[0], error); |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9059 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9060 | goto out_err; |
| 9061 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9062 | free(proxy->conf.lfs_file); |
| 9063 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9064 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9065 | cur_arg += 1; |
| 9066 | } else if (strncmp(args[0], "del-map", 7) == 0) { |
| 9067 | /* http-response del-map(<reference (map name)>) <key pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9068 | rule->action = ACT_HTTP_DEL_MAP; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9069 | /* |
| 9070 | * '+ 8' for 'del-map(' |
| 9071 | * '- 9' for 'del-map(' + trailing ')' |
| 9072 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9073 | 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] | 9074 | |
| 9075 | cur_arg = 1; |
| 9076 | |
| 9077 | if (!*args[cur_arg] || |
| 9078 | (*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] | 9079 | ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 9080 | file, linenum, args[0]); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9081 | goto out_err; |
| 9082 | } |
| 9083 | |
| 9084 | LIST_INIT(&rule->arg.map.key); |
| 9085 | proxy->conf.args.ctx = ARGC_HRS; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9086 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9087 | 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] | 9088 | (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] | 9089 | ha_alert("parsing [%s:%d]: 'http-response %s' %s.\n", |
| 9090 | file, linenum, args[0], error); |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9091 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9092 | goto out_err; |
| 9093 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9094 | free(proxy->conf.lfs_file); |
| 9095 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9096 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9097 | cur_arg += 1; |
| 9098 | } else if (strncmp(args[0], "set-map", 7) == 0) { |
| 9099 | /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9100 | rule->action = ACT_HTTP_SET_MAP; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9101 | /* |
| 9102 | * '+ 8' for 'set-map(' |
| 9103 | * '- 9' for 'set-map(' + trailing ')' |
| 9104 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9105 | 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] | 9106 | |
| 9107 | cur_arg = 1; |
| 9108 | |
| 9109 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 9110 | (*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] | 9111 | ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n", |
| 9112 | file, linenum, args[0]); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9113 | goto out_err; |
| 9114 | } |
| 9115 | |
| 9116 | LIST_INIT(&rule->arg.map.key); |
| 9117 | LIST_INIT(&rule->arg.map.value); |
| 9118 | |
| 9119 | proxy->conf.args.ctx = ARGC_HRS; |
| 9120 | |
| 9121 | /* key pattern */ |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9122 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9123 | 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] | 9124 | (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] | 9125 | ha_alert("parsing [%s:%d]: 'http-response %s' name: %s.\n", |
| 9126 | file, linenum, args[0], error); |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9127 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9128 | goto out_err; |
| 9129 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9130 | |
| 9131 | /* value pattern */ |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9132 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9133 | 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] | 9134 | (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] | 9135 | ha_alert("parsing [%s:%d]: 'http-response %s' value: %s.\n", |
| 9136 | file, linenum, args[0], error); |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9137 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9138 | goto out_err; |
| 9139 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9140 | |
| 9141 | free(proxy->conf.lfs_file); |
| 9142 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9143 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9144 | |
| 9145 | cur_arg += 2; |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 9146 | } else if (strcmp(args[0], "redirect") == 0) { |
| 9147 | struct redirect_rule *redir; |
| 9148 | char *errmsg = NULL; |
| 9149 | |
| 9150 | 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] | 9151 | ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n", |
| 9152 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 9153 | goto out_err; |
| 9154 | } |
| 9155 | |
| 9156 | /* this redirect rule might already contain a parsed condition which |
| 9157 | * we'll pass to the http-request rule. |
| 9158 | */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9159 | rule->action = ACT_HTTP_REDIR; |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 9160 | rule->arg.redir = redir; |
| 9161 | rule->cond = redir->cond; |
| 9162 | redir->cond = NULL; |
| 9163 | cur_arg = 2; |
| 9164 | return rule; |
Frédéric Lécaille | a41d531 | 2018-01-29 12:05:07 +0100 | [diff] [blame] | 9165 | } else if (strncmp(args[0], "track-sc", 8) == 0) { |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 9166 | struct sample_expr *expr; |
| 9167 | unsigned int where; |
| 9168 | char *err = NULL; |
Frédéric Lécaille | a41d531 | 2018-01-29 12:05:07 +0100 | [diff] [blame] | 9169 | unsigned int tsc_num; |
| 9170 | const char *tsc_num_str; |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 9171 | |
| 9172 | cur_arg = 1; |
| 9173 | proxy->conf.args.ctx = ARGC_TRK; |
| 9174 | |
Frédéric Lécaille | a41d531 | 2018-01-29 12:05:07 +0100 | [diff] [blame] | 9175 | tsc_num_str = &args[0][8]; |
| 9176 | if (cfg_parse_track_sc_num(&tsc_num, tsc_num_str, tsc_num_str + strlen(tsc_num_str), &err) == -1) { |
| 9177 | ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n", |
| 9178 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], err); |
| 9179 | free(err); |
| 9180 | goto out_err; |
| 9181 | } |
| 9182 | |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 9183 | expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args); |
| 9184 | if (!expr) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 9185 | ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n", |
| 9186 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], err); |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 9187 | free(err); |
| 9188 | goto out_err; |
| 9189 | } |
| 9190 | |
| 9191 | where = 0; |
| 9192 | if (proxy->cap & PR_CAP_FE) |
| 9193 | where |= SMP_VAL_FE_HRS_HDR; |
| 9194 | if (proxy->cap & PR_CAP_BE) |
| 9195 | where |= SMP_VAL_BE_HRS_HDR; |
| 9196 | |
| 9197 | if (!(expr->fetch->val & where)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 9198 | ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule :" |
| 9199 | " fetch method '%s' extracts information from '%s', none of which is available here.\n", |
| 9200 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], |
| 9201 | args[cur_arg-1], sample_src_names(expr->fetch->use)); |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 9202 | free(expr); |
| 9203 | goto out_err; |
| 9204 | } |
| 9205 | |
| 9206 | if (strcmp(args[cur_arg], "table") == 0) { |
| 9207 | cur_arg++; |
| 9208 | if (!args[cur_arg]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 9209 | ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : missing table name.\n", |
| 9210 | file, linenum, proxy_type_str(proxy), proxy->id, args[0]); |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 9211 | free(expr); |
| 9212 | goto out_err; |
| 9213 | } |
| 9214 | /* we copy the table name for now, it will be resolved later */ |
| 9215 | rule->arg.trk_ctr.table.n = strdup(args[cur_arg]); |
| 9216 | cur_arg++; |
| 9217 | } |
| 9218 | rule->arg.trk_ctr.expr = expr; |
Frédéric Lécaille | a41d531 | 2018-01-29 12:05:07 +0100 | [diff] [blame] | 9219 | rule->action = ACT_ACTION_TRK_SC0 + tsc_num; |
Christopher Faulet | 78880fb | 2017-09-18 14:43:55 +0200 | [diff] [blame] | 9220 | rule->check_ptr = check_trk_action; |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 9221 | } else if (((custom = action_http_res_custom(args[0])) != NULL)) { |
| 9222 | char *errmsg = NULL; |
| 9223 | cur_arg = 1; |
| 9224 | /* try in the module list */ |
Thierry FOURNIER | 5563e4b | 2015-08-14 19:20:07 +0200 | [diff] [blame] | 9225 | rule->from = ACT_F_HTTP_RES; |
Thierry FOURNIER | 85c6c97 | 2015-09-22 19:14:35 +0200 | [diff] [blame] | 9226 | rule->kw = custom; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 9227 | 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] | 9228 | ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n", |
| 9229 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 9230 | free(errmsg); |
| 9231 | goto out_err; |
| 9232 | } |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9233 | } else { |
Thierry FOURNIER | ab95e65 | 2015-10-02 08:24:51 +0200 | [diff] [blame] | 9234 | action_build_list(&http_res_keywords.list, &trash); |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 9235 | ha_alert("parsing [%s:%d]: 'http-response' expects 'allow', 'deny', 'redirect', " |
| 9236 | "'add-header', 'del-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', " |
| 9237 | "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'track-sc*'" |
| 9238 | "%s%s, but got '%s'%s.\n", |
| 9239 | file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)"); |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9240 | goto out_err; |
| 9241 | } |
| 9242 | |
| 9243 | if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) { |
| 9244 | struct acl_cond *cond; |
| 9245 | char *errmsg = NULL; |
| 9246 | |
Christopher Faulet | 1b421ea | 2017-09-22 14:38:56 +0200 | [diff] [blame] | 9247 | 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] | 9248 | ha_alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n", |
| 9249 | file, linenum, args[0], errmsg); |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9250 | free(errmsg); |
| 9251 | goto out_err; |
| 9252 | } |
| 9253 | rule->cond = cond; |
| 9254 | } |
| 9255 | else if (*args[cur_arg]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 9256 | ha_alert("parsing [%s:%d]: 'http-response %s' expects" |
| 9257 | " either 'if' or 'unless' followed by a condition but found '%s'.\n", |
| 9258 | file, linenum, args[0], args[cur_arg]); |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9259 | goto out_err; |
| 9260 | } |
| 9261 | |
| 9262 | return rule; |
| 9263 | out_err: |
| 9264 | free(rule); |
| 9265 | return NULL; |
| 9266 | } |
| 9267 | |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9268 | /* 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] | 9269 | * 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] | 9270 | * dynamic log-format rule instead of a static string. Parameter <dir> indicates |
| 9271 | * 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] | 9272 | */ |
| 9273 | 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] | 9274 | const char **args, char **errmsg, int use_fmt, int dir) |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9275 | { |
| 9276 | struct redirect_rule *rule; |
| 9277 | int cur_arg; |
| 9278 | int type = REDIRECT_TYPE_NONE; |
| 9279 | int code = 302; |
| 9280 | const char *destination = NULL; |
| 9281 | const char *cookie = NULL; |
| 9282 | int cookie_set = 0; |
| 9283 | unsigned int flags = REDIRECT_FLAG_NONE; |
| 9284 | struct acl_cond *cond = NULL; |
| 9285 | |
| 9286 | cur_arg = 0; |
| 9287 | while (*(args[cur_arg])) { |
| 9288 | if (strcmp(args[cur_arg], "location") == 0) { |
| 9289 | if (!*args[cur_arg + 1]) |
| 9290 | goto missing_arg; |
| 9291 | |
| 9292 | type = REDIRECT_TYPE_LOCATION; |
| 9293 | cur_arg++; |
| 9294 | destination = args[cur_arg]; |
| 9295 | } |
| 9296 | else if (strcmp(args[cur_arg], "prefix") == 0) { |
| 9297 | if (!*args[cur_arg + 1]) |
| 9298 | goto missing_arg; |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9299 | type = REDIRECT_TYPE_PREFIX; |
| 9300 | cur_arg++; |
| 9301 | destination = args[cur_arg]; |
| 9302 | } |
| 9303 | else if (strcmp(args[cur_arg], "scheme") == 0) { |
| 9304 | if (!*args[cur_arg + 1]) |
| 9305 | goto missing_arg; |
| 9306 | |
| 9307 | type = REDIRECT_TYPE_SCHEME; |
| 9308 | cur_arg++; |
| 9309 | destination = args[cur_arg]; |
| 9310 | } |
| 9311 | else if (strcmp(args[cur_arg], "set-cookie") == 0) { |
| 9312 | if (!*args[cur_arg + 1]) |
| 9313 | goto missing_arg; |
| 9314 | |
| 9315 | cur_arg++; |
| 9316 | cookie = args[cur_arg]; |
| 9317 | cookie_set = 1; |
| 9318 | } |
| 9319 | else if (strcmp(args[cur_arg], "clear-cookie") == 0) { |
| 9320 | if (!*args[cur_arg + 1]) |
| 9321 | goto missing_arg; |
| 9322 | |
| 9323 | cur_arg++; |
| 9324 | cookie = args[cur_arg]; |
| 9325 | cookie_set = 0; |
| 9326 | } |
| 9327 | else if (strcmp(args[cur_arg], "code") == 0) { |
| 9328 | if (!*args[cur_arg + 1]) |
| 9329 | goto missing_arg; |
| 9330 | |
| 9331 | cur_arg++; |
| 9332 | code = atol(args[cur_arg]); |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 9333 | if (code < 301 || code > 308 || (code > 303 && code < 307)) { |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9334 | memprintf(errmsg, |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 9335 | "'%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] | 9336 | args[cur_arg - 1], args[cur_arg]); |
| 9337 | return NULL; |
| 9338 | } |
| 9339 | } |
| 9340 | else if (!strcmp(args[cur_arg],"drop-query")) { |
| 9341 | flags |= REDIRECT_FLAG_DROP_QS; |
| 9342 | } |
| 9343 | else if (!strcmp(args[cur_arg],"append-slash")) { |
| 9344 | flags |= REDIRECT_FLAG_APPEND_SLASH; |
| 9345 | } |
| 9346 | else if (strcmp(args[cur_arg], "if") == 0 || |
| 9347 | strcmp(args[cur_arg], "unless") == 0) { |
Olivier Houchard | fbc74e8 | 2017-11-24 16:54:05 +0100 | [diff] [blame] | 9348 | 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] | 9349 | if (!cond) { |
| 9350 | memprintf(errmsg, "error in condition: %s", *errmsg); |
| 9351 | return NULL; |
| 9352 | } |
| 9353 | break; |
| 9354 | } |
| 9355 | else { |
| 9356 | memprintf(errmsg, |
| 9357 | "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')", |
| 9358 | args[cur_arg]); |
| 9359 | return NULL; |
| 9360 | } |
| 9361 | cur_arg++; |
| 9362 | } |
| 9363 | |
| 9364 | if (type == REDIRECT_TYPE_NONE) { |
| 9365 | memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')"); |
| 9366 | return NULL; |
| 9367 | } |
| 9368 | |
Willy Tarreau | be4653b | 2015-05-28 15:26:58 +0200 | [diff] [blame] | 9369 | if (dir && type != REDIRECT_TYPE_LOCATION) { |
| 9370 | memprintf(errmsg, "response only supports redirect type 'location'"); |
| 9371 | return NULL; |
| 9372 | } |
| 9373 | |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 9374 | rule = calloc(1, sizeof(*rule)); |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9375 | rule->cond = cond; |
Willy Tarreau | 60e0838 | 2013-12-03 00:48:45 +0100 | [diff] [blame] | 9376 | LIST_INIT(&rule->rdr_fmt); |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 9377 | |
| 9378 | if (!use_fmt) { |
| 9379 | /* old-style static redirect rule */ |
| 9380 | rule->rdr_str = strdup(destination); |
| 9381 | rule->rdr_len = strlen(destination); |
| 9382 | } |
| 9383 | else { |
| 9384 | /* log-format based redirect rule */ |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 9385 | |
| 9386 | /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case, |
| 9387 | * if prefix == "/", we don't want to add anything, otherwise it |
| 9388 | * makes it hard for the user to configure a self-redirection. |
| 9389 | */ |
Godbach | d972203 | 2014-12-18 15:44:58 +0800 | [diff] [blame] | 9390 | curproxy->conf.args.ctx = ARGC_RDR; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 9391 | if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) { |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9392 | if (!parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP, |
| 9393 | 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] | 9394 | : (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9395 | errmsg)) { |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9396 | return NULL; |
| 9397 | } |
Willy Tarreau | 59ad1a2 | 2014-01-29 14:39:58 +0100 | [diff] [blame] | 9398 | free(curproxy->conf.lfs_file); |
| 9399 | curproxy->conf.lfs_file = strdup(curproxy->conf.args.file); |
| 9400 | curproxy->conf.lfs_line = curproxy->conf.args.line; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 9401 | } |
| 9402 | } |
| 9403 | |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9404 | if (cookie) { |
| 9405 | /* depending on cookie_set, either we want to set the cookie, or to clear it. |
| 9406 | * a clear consists in appending "; path=/; Max-Age=0;" at the end. |
| 9407 | */ |
| 9408 | rule->cookie_len = strlen(cookie); |
| 9409 | if (cookie_set) { |
| 9410 | rule->cookie_str = malloc(rule->cookie_len + 10); |
| 9411 | memcpy(rule->cookie_str, cookie, rule->cookie_len); |
| 9412 | memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10); |
| 9413 | rule->cookie_len += 9; |
| 9414 | } else { |
| 9415 | rule->cookie_str = malloc(rule->cookie_len + 21); |
| 9416 | memcpy(rule->cookie_str, cookie, rule->cookie_len); |
| 9417 | memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21); |
| 9418 | rule->cookie_len += 20; |
| 9419 | } |
| 9420 | } |
| 9421 | rule->type = type; |
| 9422 | rule->code = code; |
| 9423 | rule->flags = flags; |
| 9424 | LIST_INIT(&rule->list); |
| 9425 | return rule; |
| 9426 | |
| 9427 | missing_arg: |
| 9428 | memprintf(errmsg, "missing argument for '%s'", args[cur_arg]); |
| 9429 | return NULL; |
| 9430 | } |
| 9431 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9432 | /************************************************************************/ |
| 9433 | /* The code below is dedicated to ACL parsing and matching */ |
| 9434 | /************************************************************************/ |
| 9435 | |
| 9436 | |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9437 | /* This function ensures that the prerequisites for an L7 fetch are ready, |
| 9438 | * which means that a request or response is ready. If some data is missing, |
| 9439 | * 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] | 9440 | * to extract data from L7. If <req_vol> is non-null during a request prefetch, |
| 9441 | * another test is made to ensure the required information is not gone. |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9442 | * |
| 9443 | * The function returns : |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9444 | * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to |
| 9445 | * decide whether or not an HTTP message is present ; |
| 9446 | * 0 if the requested data cannot be fetched or if it is certain that |
| 9447 | * we'll never have any HTTP message there ; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9448 | * 1 if an HTTP message is ready |
| 9449 | */ |
James Rosewell | 91a41cb | 2015-09-18 17:11:16 +0100 | [diff] [blame] | 9450 | 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] | 9451 | const struct arg *args, struct sample *smp, int req_vol) |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9452 | { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 9453 | struct http_txn *txn; |
| 9454 | struct http_msg *msg; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9455 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 9456 | /* Note: it is possible that <s> is NULL when called before stream |
| 9457 | * initialization (eg: tcp-request connection), so this function is the |
| 9458 | * one responsible for guarding against this case for all HTTP users. |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9459 | */ |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 9460 | if (!s) |
| 9461 | return 0; |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 9462 | |
Thierry FOURNIER | ed08d6a | 2015-09-24 08:40:18 +0200 | [diff] [blame] | 9463 | if (!s->txn) { |
| 9464 | if (unlikely(!http_alloc_txn(s))) |
| 9465 | return 0; /* not enough memory */ |
| 9466 | http_init_txn(s); |
| 9467 | } |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 9468 | txn = s->txn; |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 9469 | msg = &txn->req; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9470 | |
| 9471 | /* Check for a dependency on a request */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9472 | smp->data.type = SMP_T_BOOL; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9473 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 9474 | if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 9475 | /* If the buffer does not leave enough free space at the end, |
| 9476 | * we must first realign it. |
| 9477 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 9478 | if (s->req.buf->p > s->req.buf->data && |
| 9479 | s->req.buf->i + s->req.buf->p > s->req.buf->data + s->req.buf->size - global.tune.maxrewrite) |
| 9480 | buffer_slow_realign(s->req.buf); |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 9481 | |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9482 | if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 472b1ee | 2013-10-14 22:41:30 +0200 | [diff] [blame] | 9483 | if (msg->msg_state == HTTP_MSG_ERROR) |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9484 | return 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9485 | |
| 9486 | /* Try to decode HTTP request */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 9487 | if (likely(msg->next < s->req.buf->i)) |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9488 | http_msg_analyzer(msg, &txn->hdr_idx); |
| 9489 | |
| 9490 | /* Still no valid request ? */ |
| 9491 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 3bf1b2b | 2012-08-27 20:46:07 +0200 | [diff] [blame] | 9492 | if ((msg->msg_state == HTTP_MSG_ERROR) || |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 9493 | buffer_full(s->req.buf, global.tune.maxrewrite)) { |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9494 | return 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9495 | } |
| 9496 | /* wait for final state */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9497 | smp->flags |= SMP_F_MAY_CHANGE; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9498 | return 0; |
| 9499 | } |
| 9500 | |
| 9501 | /* OK we just got a valid HTTP request. We have some minor |
| 9502 | * preparation to perform so that further checks can rely |
| 9503 | * on HTTP tests. |
| 9504 | */ |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 9505 | |
| 9506 | /* If the request was parsed but was too large, we must absolutely |
| 9507 | * return an error so that it is not processed. At the moment this |
| 9508 | * cannot happen, but if the parsers are to change in the future, |
| 9509 | * we want this check to be maintained. |
| 9510 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 9511 | if (unlikely(s->req.buf->i + s->req.buf->p > |
| 9512 | s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)) { |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 9513 | msg->err_state = msg->msg_state; |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 9514 | msg->msg_state = HTTP_MSG_ERROR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9515 | smp->data.u.sint = 1; |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 9516 | return 1; |
| 9517 | } |
| 9518 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9519 | 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] | 9520 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 9521 | s->flags |= SF_REDIRECTABLE; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9522 | |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9523 | if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn)) |
| 9524 | return 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9525 | } |
| 9526 | |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9527 | if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) { |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9528 | return 0; /* data might have moved and indexes changed */ |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9529 | } |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9530 | |
| 9531 | /* otherwise everything's ready for the request */ |
| 9532 | } |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 9533 | else { |
| 9534 | /* Check for a dependency on a response */ |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9535 | if (txn->rsp.msg_state < HTTP_MSG_BODY) { |
| 9536 | smp->flags |= SMP_F_MAY_CHANGE; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9537 | return 0; |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9538 | } |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9539 | } |
| 9540 | |
| 9541 | /* everything's OK */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9542 | smp->data.u.sint = 1; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9543 | return 1; |
| 9544 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9545 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9546 | /* 1. Check on METHOD |
| 9547 | * We use the pre-parsed method if it is known, and store its number as an |
| 9548 | * integer. If it is unknown, we use the pointer and the length. |
| 9549 | */ |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 9550 | 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] | 9551 | { |
| 9552 | int len, meth; |
| 9553 | |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 9554 | len = strlen(text); |
| 9555 | meth = find_http_meth(text, len); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9556 | |
| 9557 | pattern->val.i = meth; |
| 9558 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | 912c119 | 2014-08-29 15:15:50 +0200 | [diff] [blame] | 9559 | pattern->ptr.str = (char *)text; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9560 | pattern->len = len; |
| 9561 | } |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 9562 | else { |
| 9563 | pattern->ptr.str = NULL; |
| 9564 | pattern->len = 0; |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 9565 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9566 | return 1; |
| 9567 | } |
| 9568 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 9569 | /* This function fetches the method of current HTTP request and stores |
| 9570 | * it in the global pattern struct as a chunk. There are two possibilities : |
| 9571 | * - if the method is known (not HTTP_METH_OTHER), its identifier is stored |
| 9572 | * in <len> and <ptr> is NULL ; |
| 9573 | * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and |
| 9574 | * <len> to its length. |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 9575 | * This is intended to be used with pat_match_meth() only. |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 9576 | */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 9577 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9578 | 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] | 9579 | { |
| 9580 | int meth; |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 9581 | struct http_txn *txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9582 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 9583 | CHECK_HTTP_MESSAGE_FIRST_PERM(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9584 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 9585 | txn = smp->strm->txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9586 | meth = txn->meth; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9587 | smp->data.type = SMP_T_METH; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9588 | smp->data.u.meth.meth = meth; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9589 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9590 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 9591 | /* ensure the indexes are not affected */ |
| 9592 | return 0; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9593 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9594 | smp->data.u.meth.str.len = txn->req.sl.rq.m_l; |
| 9595 | smp->data.u.meth.str.str = txn->req.chn->buf->p; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9596 | } |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9597 | smp->flags |= SMP_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9598 | return 1; |
| 9599 | } |
| 9600 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 9601 | /* See above how the method is stored in the global pattern */ |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 9602 | 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] | 9603 | { |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 9604 | int icase; |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 9605 | struct pattern_list *lst; |
| 9606 | struct pattern *pattern; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 9607 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 9608 | list_for_each_entry(lst, &expr->patterns, list) { |
| 9609 | pattern = &lst->pat; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9610 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 9611 | /* well-known method */ |
| 9612 | if (pattern->val.i != HTTP_METH_OTHER) { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9613 | if (smp->data.u.meth.meth == pattern->val.i) |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 9614 | return pattern; |
| 9615 | else |
| 9616 | continue; |
| 9617 | } |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 9618 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 9619 | /* Other method, we must compare the strings */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9620 | if (pattern->len != smp->data.u.meth.str.len) |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 9621 | continue; |
| 9622 | |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 9623 | icase = expr->mflags & PAT_MF_IGNORE_CASE; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9624 | if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.meth.str.str, smp->data.u.meth.str.len) == 0) || |
| 9625 | (!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] | 9626 | return pattern; |
| 9627 | } |
| 9628 | return NULL; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9629 | } |
| 9630 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 9631 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9632 | 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] | 9633 | { |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 9634 | struct http_txn *txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9635 | char *ptr; |
| 9636 | int len; |
| 9637 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9638 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9639 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 9640 | txn = smp->strm->txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9641 | len = txn->req.sl.rq.v_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9642 | ptr = txn->req.chn->buf->p + txn->req.sl.rq.v; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9643 | |
| 9644 | while ((len-- > 0) && (*ptr++ != '/')); |
| 9645 | if (len <= 0) |
| 9646 | return 0; |
| 9647 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9648 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9649 | smp->data.u.str.str = ptr; |
| 9650 | smp->data.u.str.len = len; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9651 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9652 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9653 | return 1; |
| 9654 | } |
| 9655 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 9656 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9657 | 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] | 9658 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 9659 | struct http_txn *txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9660 | char *ptr; |
| 9661 | int len; |
| 9662 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9663 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9664 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 9665 | txn = smp->strm->txn; |
Willy Tarreau | f26b252 | 2012-12-14 08:33:14 +0100 | [diff] [blame] | 9666 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
| 9667 | return 0; |
| 9668 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9669 | len = txn->rsp.sl.st.v_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9670 | ptr = txn->rsp.chn->buf->p; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9671 | |
| 9672 | while ((len-- > 0) && (*ptr++ != '/')); |
| 9673 | if (len <= 0) |
| 9674 | return 0; |
| 9675 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9676 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9677 | smp->data.u.str.str = ptr; |
| 9678 | smp->data.u.str.len = len; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9679 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9680 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9681 | return 1; |
| 9682 | } |
| 9683 | |
| 9684 | /* 3. Check on Status Code. We manipulate integers here. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 9685 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9686 | 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] | 9687 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 9688 | struct http_txn *txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9689 | char *ptr; |
| 9690 | int len; |
| 9691 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9692 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9693 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 9694 | txn = smp->strm->txn; |
Willy Tarreau | f26b252 | 2012-12-14 08:33:14 +0100 | [diff] [blame] | 9695 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
| 9696 | return 0; |
| 9697 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9698 | len = txn->rsp.sl.st.c_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9699 | ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9700 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9701 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9702 | smp->data.u.sint = __strl2ui(ptr, len); |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9703 | smp->flags = SMP_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9704 | return 1; |
| 9705 | } |
| 9706 | |
Thierry Fournier | f4011dd | 2016-03-29 17:23:51 +0200 | [diff] [blame] | 9707 | static int |
| 9708 | smp_fetch_uniqueid(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 9709 | { |
| 9710 | if (LIST_ISEMPTY(&smp->sess->fe->format_unique_id)) |
| 9711 | return 0; |
| 9712 | |
| 9713 | if (!smp->strm->unique_id) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 9714 | if ((smp->strm->unique_id = pool_alloc(pool_head_uniqueid)) == NULL) |
Thierry Fournier | f4011dd | 2016-03-29 17:23:51 +0200 | [diff] [blame] | 9715 | return 0; |
| 9716 | smp->strm->unique_id[0] = '\0'; |
| 9717 | } |
| 9718 | smp->data.u.str.len = build_logline(smp->strm, smp->strm->unique_id, |
| 9719 | UNIQUEID_LEN, &smp->sess->fe->format_unique_id); |
| 9720 | |
| 9721 | smp->data.type = SMP_T_STR; |
| 9722 | smp->data.u.str.str = smp->strm->unique_id; |
| 9723 | smp->flags = SMP_F_CONST; |
| 9724 | return 1; |
| 9725 | } |
| 9726 | |
Thierry FOURNIER | d7d8881 | 2017-04-19 15:15:14 +0200 | [diff] [blame] | 9727 | /* Returns a string block containing all headers including the |
| 9728 | * empty line wich separes headers from the body. This is useful |
| 9729 | * form some headers analysis. |
| 9730 | */ |
| 9731 | static int |
| 9732 | smp_fetch_hdrs(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 9733 | { |
| 9734 | struct http_msg *msg; |
| 9735 | struct hdr_idx *idx; |
| 9736 | struct http_txn *txn; |
| 9737 | |
| 9738 | CHECK_HTTP_MESSAGE_FIRST(); |
| 9739 | |
| 9740 | txn = smp->strm->txn; |
| 9741 | idx = &txn->hdr_idx; |
| 9742 | msg = &txn->req; |
| 9743 | |
| 9744 | smp->data.type = SMP_T_STR; |
| 9745 | smp->data.u.str.str = msg->chn->buf->p + hdr_idx_first_pos(idx); |
| 9746 | smp->data.u.str.len = msg->eoh - hdr_idx_first_pos(idx) + 1 + |
| 9747 | (msg->chn->buf->p[msg->eoh] == '\r'); |
| 9748 | |
| 9749 | return 1; |
| 9750 | } |
| 9751 | |
Thierry FOURNIER | 5617dce | 2017-04-09 05:38:19 +0200 | [diff] [blame] | 9752 | /* Returns the header request in a length/value encoded format. |
| 9753 | * This is useful for exchanges with the SPOE. |
| 9754 | * |
| 9755 | * A "length value" is a multibyte code encoding numbers. It uses the |
| 9756 | * SPOE format. The encoding is the following: |
| 9757 | * |
| 9758 | * Each couple "header name" / "header value" is composed |
| 9759 | * like this: |
| 9760 | * "length value" "header name bytes" |
| 9761 | * "length value" "header value bytes" |
| 9762 | * When the last header is reached, the header name and the header |
| 9763 | * value are empty. Their length are 0 |
| 9764 | */ |
| 9765 | static int |
| 9766 | smp_fetch_hdrs_bin(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 9767 | { |
| 9768 | struct http_msg *msg; |
| 9769 | struct chunk *temp; |
| 9770 | struct hdr_idx *idx; |
| 9771 | const char *cur_ptr, *cur_next, *p; |
| 9772 | int old_idx, cur_idx; |
| 9773 | struct hdr_idx_elem *cur_hdr; |
| 9774 | const char *hn, *hv; |
| 9775 | int hnl, hvl; |
| 9776 | int ret; |
| 9777 | struct http_txn *txn; |
| 9778 | char *buf; |
| 9779 | char *end; |
| 9780 | |
| 9781 | CHECK_HTTP_MESSAGE_FIRST(); |
| 9782 | |
| 9783 | temp = get_trash_chunk(); |
| 9784 | buf = temp->str; |
| 9785 | end = temp->str + temp->size; |
| 9786 | |
| 9787 | txn = smp->strm->txn; |
| 9788 | idx = &txn->hdr_idx; |
| 9789 | msg = &txn->req; |
| 9790 | |
| 9791 | /* Build array of headers. */ |
| 9792 | old_idx = 0; |
| 9793 | cur_next = msg->chn->buf->p + hdr_idx_first_pos(idx); |
| 9794 | while (1) { |
| 9795 | cur_idx = idx->v[old_idx].next; |
| 9796 | if (!cur_idx) |
| 9797 | break; |
| 9798 | old_idx = cur_idx; |
| 9799 | |
| 9800 | cur_hdr = &idx->v[cur_idx]; |
| 9801 | cur_ptr = cur_next; |
| 9802 | cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1; |
| 9803 | |
| 9804 | /* Now we have one full header at cur_ptr of len cur_hdr->len, |
| 9805 | * and the next header starts at cur_next. We'll check |
| 9806 | * this header in the list as well as against the default |
| 9807 | * rule. |
| 9808 | */ |
| 9809 | |
| 9810 | /* look for ': *'. */ |
| 9811 | hn = cur_ptr; |
| 9812 | for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++); |
| 9813 | if (p >= cur_ptr+cur_hdr->len) |
| 9814 | continue; |
| 9815 | hnl = p - hn; |
| 9816 | p++; |
| 9817 | while (p < cur_ptr + cur_hdr->len && (*p == ' ' || *p == '\t')) |
| 9818 | p++; |
| 9819 | if (p >= cur_ptr + cur_hdr->len) |
| 9820 | continue; |
| 9821 | hv = p; |
| 9822 | hvl = cur_ptr + cur_hdr->len-p; |
| 9823 | |
| 9824 | /* encode the header name. */ |
| 9825 | ret = encode_varint(hnl, &buf, end); |
| 9826 | if (ret == -1) |
| 9827 | return 0; |
| 9828 | if (buf + hnl > end) |
| 9829 | return 0; |
| 9830 | memcpy(buf, hn, hnl); |
| 9831 | buf += hnl; |
| 9832 | |
| 9833 | /* encode and copy the value. */ |
| 9834 | ret = encode_varint(hvl, &buf, end); |
| 9835 | if (ret == -1) |
| 9836 | return 0; |
| 9837 | if (buf + hvl > end) |
| 9838 | return 0; |
| 9839 | memcpy(buf, hv, hvl); |
| 9840 | buf += hvl; |
| 9841 | } |
| 9842 | |
| 9843 | /* encode the end of the header list with empty |
| 9844 | * header name and header value. |
| 9845 | */ |
| 9846 | ret = encode_varint(0, &buf, end); |
| 9847 | if (ret == -1) |
| 9848 | return 0; |
| 9849 | ret = encode_varint(0, &buf, end); |
| 9850 | if (ret == -1) |
| 9851 | return 0; |
| 9852 | |
| 9853 | /* Initialise sample data which will be filled. */ |
| 9854 | smp->data.type = SMP_T_BIN; |
| 9855 | smp->data.u.str.str = temp->str; |
| 9856 | smp->data.u.str.len = buf - temp->str; |
| 9857 | smp->data.u.str.size = temp->size; |
| 9858 | |
| 9859 | return 1; |
| 9860 | } |
| 9861 | |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9862 | /* returns the longest available part of the body. This requires that the body |
| 9863 | * has been waited for using http-buffer-request. |
| 9864 | */ |
| 9865 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9866 | 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] | 9867 | { |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9868 | struct http_msg *msg; |
| 9869 | unsigned long len; |
| 9870 | unsigned long block1; |
| 9871 | char *body; |
| 9872 | struct chunk *temp; |
| 9873 | |
| 9874 | CHECK_HTTP_MESSAGE_FIRST(); |
| 9875 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9876 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 9877 | msg = &smp->strm->txn->req; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9878 | else |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 9879 | msg = &smp->strm->txn->rsp; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9880 | |
| 9881 | len = http_body_bytes(msg); |
| 9882 | body = b_ptr(msg->chn->buf, -http_data_rewind(msg)); |
| 9883 | |
| 9884 | block1 = len; |
| 9885 | if (block1 > msg->chn->buf->data + msg->chn->buf->size - body) |
| 9886 | block1 = msg->chn->buf->data + msg->chn->buf->size - body; |
| 9887 | |
| 9888 | if (block1 == len) { |
| 9889 | /* buffer is not wrapped (or empty) */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9890 | smp->data.type = SMP_T_BIN; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9891 | smp->data.u.str.str = body; |
| 9892 | smp->data.u.str.len = len; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9893 | smp->flags = SMP_F_VOL_TEST | SMP_F_CONST; |
| 9894 | } |
| 9895 | else { |
| 9896 | /* buffer is wrapped, we need to defragment it */ |
| 9897 | temp = get_trash_chunk(); |
| 9898 | memcpy(temp->str, body, block1); |
| 9899 | memcpy(temp->str + block1, msg->chn->buf->data, len - block1); |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9900 | smp->data.type = SMP_T_BIN; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9901 | smp->data.u.str.str = temp->str; |
| 9902 | smp->data.u.str.len = len; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9903 | smp->flags = SMP_F_VOL_TEST; |
| 9904 | } |
| 9905 | return 1; |
| 9906 | } |
| 9907 | |
| 9908 | |
| 9909 | /* returns the available length of the body. This requires that the body |
| 9910 | * has been waited for using http-buffer-request. |
| 9911 | */ |
| 9912 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9913 | 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] | 9914 | { |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9915 | struct http_msg *msg; |
| 9916 | |
| 9917 | CHECK_HTTP_MESSAGE_FIRST(); |
| 9918 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9919 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 9920 | msg = &smp->strm->txn->req; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9921 | else |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 9922 | msg = &smp->strm->txn->rsp; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9923 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9924 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9925 | smp->data.u.sint = http_body_bytes(msg); |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9926 | |
| 9927 | smp->flags = SMP_F_VOL_TEST; |
| 9928 | return 1; |
| 9929 | } |
| 9930 | |
| 9931 | |
| 9932 | /* returns the advertised length of the body, or the advertised size of the |
| 9933 | * chunks available in the buffer. This requires that the body has been waited |
| 9934 | * for using http-buffer-request. |
| 9935 | */ |
| 9936 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9937 | 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] | 9938 | { |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9939 | struct http_msg *msg; |
| 9940 | |
| 9941 | CHECK_HTTP_MESSAGE_FIRST(); |
| 9942 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9943 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 9944 | msg = &smp->strm->txn->req; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9945 | else |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 9946 | msg = &smp->strm->txn->rsp; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9947 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9948 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9949 | smp->data.u.sint = msg->body_len; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9950 | |
| 9951 | smp->flags = SMP_F_VOL_TEST; |
| 9952 | return 1; |
| 9953 | } |
| 9954 | |
| 9955 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9956 | /* 4. Check on URL/URI. A pointer to the URI is stored. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 9957 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9958 | 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] | 9959 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 9960 | struct http_txn *txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9961 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9962 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9963 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 9964 | txn = smp->strm->txn; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9965 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9966 | smp->data.u.str.len = txn->req.sl.rq.u_l; |
| 9967 | 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] | 9968 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9969 | return 1; |
| 9970 | } |
| 9971 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9972 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9973 | 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] | 9974 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 9975 | struct http_txn *txn; |
Willy Tarreau | 4c804ec | 2013-09-30 14:37:14 +0200 | [diff] [blame] | 9976 | struct sockaddr_storage addr; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9977 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9978 | CHECK_HTTP_MESSAGE_FIRST(); |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9979 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 9980 | txn = smp->strm->txn; |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 9981 | 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] | 9982 | if (((struct sockaddr_in *)&addr)->sin_family != AF_INET) |
Willy Tarreau | f4362b3 | 2011-12-16 17:49:52 +0100 | [diff] [blame] | 9983 | return 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9984 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9985 | smp->data.type = SMP_T_IPV4; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9986 | smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9987 | smp->flags = 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9988 | return 1; |
| 9989 | } |
| 9990 | |
| 9991 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9992 | 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] | 9993 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 9994 | struct http_txn *txn; |
Willy Tarreau | 4c804ec | 2013-09-30 14:37:14 +0200 | [diff] [blame] | 9995 | struct sockaddr_storage addr; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9996 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9997 | CHECK_HTTP_MESSAGE_FIRST(); |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9998 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 9999 | txn = smp->strm->txn; |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 10000 | 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] | 10001 | if (((struct sockaddr_in *)&addr)->sin_family != AF_INET) |
| 10002 | return 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10003 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10004 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10005 | smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port); |
Willy Tarreau | 21e5b0e | 2012-04-23 19:25:44 +0200 | [diff] [blame] | 10006 | smp->flags = 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10007 | return 1; |
| 10008 | } |
| 10009 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10010 | /* Fetch an HTTP header. A pointer to the beginning of the value is returned. |
| 10011 | * Accepts an optional argument of type string containing the header field name, |
| 10012 | * and an optional argument of type signed or unsigned integer to request an |
| 10013 | * 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] | 10014 | * headers are considered from the first one. It does not stop on commas and |
| 10015 | * returns full lines instead (useful for User-Agent or Date for example). |
| 10016 | */ |
| 10017 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10018 | 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] | 10019 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10020 | struct hdr_idx *idx; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10021 | struct hdr_ctx *ctx = smp->ctx.a[0]; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10022 | const struct http_msg *msg; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10023 | int occ = 0; |
| 10024 | const char *name_str = NULL; |
| 10025 | int name_len = 0; |
| 10026 | |
| 10027 | if (!ctx) { |
| 10028 | /* first call */ |
| 10029 | ctx = &static_hdr_ctx; |
| 10030 | ctx->idx = 0; |
| 10031 | smp->ctx.a[0] = ctx; |
| 10032 | } |
| 10033 | |
| 10034 | if (args) { |
| 10035 | if (args[0].type != ARGT_STR) |
| 10036 | return 0; |
| 10037 | name_str = args[0].data.str.str; |
| 10038 | name_len = args[0].data.str.len; |
| 10039 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 10040 | if (args[1].type == ARGT_SINT) |
| 10041 | occ = args[1].data.sint; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10042 | } |
| 10043 | |
| 10044 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10045 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10046 | idx = &smp->strm->txn->hdr_idx; |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10047 | 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] | 10048 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10049 | if (ctx && !(smp->flags & SMP_F_NOT_LAST)) |
| 10050 | /* search for header from the beginning */ |
| 10051 | ctx->idx = 0; |
| 10052 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10053 | if (!occ && !(smp->opt & SMP_OPT_ITERATE)) |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10054 | /* no explicit occurrence and single fetch => last header by default */ |
| 10055 | occ = -1; |
| 10056 | |
| 10057 | if (!occ) |
| 10058 | /* prepare to report multiple occurrences for ACL fetches */ |
| 10059 | smp->flags |= SMP_F_NOT_LAST; |
| 10060 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10061 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10062 | smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10063 | 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] | 10064 | return 1; |
| 10065 | |
| 10066 | smp->flags &= ~SMP_F_NOT_LAST; |
| 10067 | return 0; |
| 10068 | } |
| 10069 | |
| 10070 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
| 10071 | * Accepts exactly 1 argument of type string. It does not stop on commas and |
| 10072 | * returns full lines instead (useful for User-Agent or Date for example). |
| 10073 | */ |
| 10074 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10075 | 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] | 10076 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10077 | struct hdr_idx *idx; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10078 | struct hdr_ctx ctx; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10079 | const struct http_msg *msg; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10080 | int cnt; |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10081 | const char *name = NULL; |
| 10082 | int len = 0; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10083 | |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10084 | if (args && args->type == ARGT_STR) { |
| 10085 | name = args->data.str.str; |
| 10086 | len = args->data.str.len; |
| 10087 | } |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10088 | |
| 10089 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10090 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10091 | idx = &smp->strm->txn->hdr_idx; |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10092 | 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] | 10093 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10094 | ctx.idx = 0; |
| 10095 | cnt = 0; |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10096 | 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] | 10097 | cnt++; |
| 10098 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10099 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10100 | smp->data.u.sint = cnt; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10101 | smp->flags = SMP_F_VOL_HDR; |
| 10102 | return 1; |
| 10103 | } |
| 10104 | |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 10105 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10106 | 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] | 10107 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10108 | struct hdr_idx *idx; |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 10109 | struct hdr_ctx ctx; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10110 | const struct http_msg *msg; |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 10111 | struct chunk *temp; |
| 10112 | char del = ','; |
| 10113 | |
| 10114 | if (args && args->type == ARGT_STR) |
| 10115 | del = *args[0].data.str.str; |
| 10116 | |
| 10117 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10118 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10119 | idx = &smp->strm->txn->hdr_idx; |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10120 | 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] | 10121 | |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 10122 | temp = get_trash_chunk(); |
| 10123 | |
| 10124 | ctx.idx = 0; |
| 10125 | while (http_find_next_header(msg->chn->buf->p, idx, &ctx)) { |
| 10126 | if (temp->len) |
| 10127 | temp->str[temp->len++] = del; |
| 10128 | memcpy(temp->str + temp->len, ctx.line, ctx.del); |
| 10129 | temp->len += ctx.del; |
| 10130 | } |
| 10131 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10132 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10133 | smp->data.u.str.str = temp->str; |
| 10134 | smp->data.u.str.len = temp->len; |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 10135 | smp->flags = SMP_F_VOL_HDR; |
| 10136 | return 1; |
| 10137 | } |
| 10138 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10139 | /* Fetch an HTTP header. A pointer to the beginning of the value is returned. |
| 10140 | * Accepts an optional argument of type string containing the header field name, |
| 10141 | * and an optional argument of type signed or unsigned integer to request an |
| 10142 | * 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] | 10143 | * headers are considered from the first one. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10144 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10145 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10146 | 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] | 10147 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10148 | struct hdr_idx *idx; |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 10149 | struct hdr_ctx *ctx = smp->ctx.a[0]; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10150 | const struct http_msg *msg; |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10151 | int occ = 0; |
| 10152 | const char *name_str = NULL; |
| 10153 | int name_len = 0; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10154 | |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 10155 | if (!ctx) { |
| 10156 | /* first call */ |
| 10157 | ctx = &static_hdr_ctx; |
| 10158 | ctx->idx = 0; |
Willy Tarreau | ffb6f08 | 2013-04-02 23:16:53 +0200 | [diff] [blame] | 10159 | smp->ctx.a[0] = ctx; |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 10160 | } |
| 10161 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10162 | if (args) { |
| 10163 | if (args[0].type != ARGT_STR) |
| 10164 | return 0; |
| 10165 | name_str = args[0].data.str.str; |
| 10166 | name_len = args[0].data.str.len; |
| 10167 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 10168 | if (args[1].type == ARGT_SINT) |
| 10169 | occ = args[1].data.sint; |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10170 | } |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10171 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10172 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10173 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10174 | idx = &smp->strm->txn->hdr_idx; |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10175 | 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] | 10176 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10177 | if (ctx && !(smp->flags & SMP_F_NOT_LAST)) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10178 | /* search for header from the beginning */ |
| 10179 | ctx->idx = 0; |
| 10180 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10181 | if (!occ && !(smp->opt & SMP_OPT_ITERATE)) |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10182 | /* no explicit occurrence and single fetch => last header by default */ |
| 10183 | occ = -1; |
| 10184 | |
| 10185 | if (!occ) |
| 10186 | /* prepare to report multiple occurrences for ACL fetches */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10187 | smp->flags |= SMP_F_NOT_LAST; |
Willy Tarreau | 664092c | 2011-12-16 19:11:42 +0100 | [diff] [blame] | 10188 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10189 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10190 | smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10191 | 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] | 10192 | return 1; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10193 | |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10194 | smp->flags &= ~SMP_F_NOT_LAST; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10195 | return 0; |
| 10196 | } |
| 10197 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10198 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10199 | * Accepts exactly 1 argument of type string. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10200 | */ |
| 10201 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10202 | 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] | 10203 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10204 | struct hdr_idx *idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10205 | struct hdr_ctx ctx; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10206 | const struct http_msg *msg; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10207 | int cnt; |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10208 | const char *name = NULL; |
| 10209 | int len = 0; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10210 | |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10211 | if (args && args->type == ARGT_STR) { |
| 10212 | name = args->data.str.str; |
| 10213 | len = args->data.str.len; |
| 10214 | } |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10215 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10216 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10217 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10218 | idx = &smp->strm->txn->hdr_idx; |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10219 | 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] | 10220 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10221 | ctx.idx = 0; |
| 10222 | cnt = 0; |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10223 | while (http_find_header2(name, len, msg->chn->buf->p, idx, &ctx)) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10224 | cnt++; |
| 10225 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10226 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10227 | smp->data.u.sint = cnt; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10228 | smp->flags = SMP_F_VOL_HDR; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10229 | return 1; |
| 10230 | } |
| 10231 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10232 | /* Fetch an HTTP header's integer value. The integer value is returned. It |
| 10233 | * takes a mandatory argument of type string and an optional one of type int |
| 10234 | * to designate a specific occurrence. It returns an unsigned integer, which |
| 10235 | * may or may not be appropriate for everything. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10236 | */ |
| 10237 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10238 | 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] | 10239 | { |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10240 | int ret = smp_fetch_hdr(args, smp, kw, private); |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10241 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10242 | if (ret > 0) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10243 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10244 | 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] | 10245 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10246 | |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10247 | return ret; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10248 | } |
| 10249 | |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 10250 | /* Fetch an HTTP header's IP value. takes a mandatory argument of type string |
| 10251 | * and an optional one of type int to designate a specific occurrence. |
| 10252 | * It returns an IPv4 or IPv6 address. |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 10253 | */ |
| 10254 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10255 | 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] | 10256 | { |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10257 | int ret; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10258 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10259 | while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10260 | if (url2ipv4((char *)smp->data.u.str.str, &smp->data.u.ipv4)) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10261 | smp->data.type = SMP_T_IPV4; |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10262 | break; |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 10263 | } else { |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 10264 | struct chunk *temp = get_trash_chunk(); |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10265 | if (smp->data.u.str.len < temp->size - 1) { |
| 10266 | memcpy(temp->str, smp->data.u.str.str, smp->data.u.str.len); |
| 10267 | temp->str[smp->data.u.str.len] = '\0'; |
| 10268 | if (inet_pton(AF_INET6, temp->str, &smp->data.u.ipv6)) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10269 | smp->data.type = SMP_T_IPV6; |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 10270 | break; |
| 10271 | } |
| 10272 | } |
| 10273 | } |
| 10274 | |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10275 | /* if the header doesn't match an IP address, fetch next one */ |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10276 | if (!(smp->flags & SMP_F_NOT_LAST)) |
| 10277 | return 0; |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 10278 | } |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10279 | return ret; |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 10280 | } |
| 10281 | |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10282 | /* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at |
| 10283 | * the first '/' after the possible hostname, and ends before the possible '?'. |
| 10284 | */ |
| 10285 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10286 | 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] | 10287 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10288 | struct http_txn *txn; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10289 | char *ptr, *end; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10290 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10291 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10292 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10293 | txn = smp->strm->txn; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10294 | 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] | 10295 | ptr = http_get_path(txn); |
| 10296 | if (!ptr) |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10297 | return 0; |
| 10298 | |
| 10299 | /* OK, we got the '/' ! */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10300 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10301 | smp->data.u.str.str = ptr; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10302 | |
| 10303 | while (ptr < end && *ptr != '?') |
| 10304 | ptr++; |
| 10305 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10306 | smp->data.u.str.len = ptr - smp->data.u.str.str; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10307 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10308 | return 1; |
| 10309 | } |
| 10310 | |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10311 | /* This produces a concatenation of the first occurrence of the Host header |
| 10312 | * followed by the path component if it begins with a slash ('/'). This means |
| 10313 | * that '*' will not be added, resulting in exactly the first Host entry. |
| 10314 | * If no Host header is found, then the path is returned as-is. The returned |
| 10315 | * 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] | 10316 | * The returned sample is of type string. |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10317 | */ |
| 10318 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10319 | 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] | 10320 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10321 | struct http_txn *txn; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10322 | char *ptr, *end, *beg; |
| 10323 | struct hdr_ctx ctx; |
Willy Tarreau | 3caf2af | 2014-06-24 17:27:02 +0200 | [diff] [blame] | 10324 | struct chunk *temp; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10325 | |
| 10326 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10327 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10328 | txn = smp->strm->txn; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10329 | ctx.idx = 0; |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 10330 | 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] | 10331 | return smp_fetch_path(args, smp, kw, private); |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10332 | |
| 10333 | /* 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] | 10334 | temp = get_trash_chunk(); |
| 10335 | memcpy(temp->str, ctx.line + ctx.val, ctx.vlen); |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10336 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10337 | smp->data.u.str.str = temp->str; |
| 10338 | smp->data.u.str.len = ctx.vlen; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10339 | |
| 10340 | /* now retrieve the path */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 10341 | 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] | 10342 | beg = http_get_path(txn); |
| 10343 | if (!beg) |
| 10344 | beg = end; |
| 10345 | |
| 10346 | for (ptr = beg; ptr < end && *ptr != '?'; ptr++); |
| 10347 | |
| 10348 | if (beg < ptr && *beg == '/') { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10349 | memcpy(smp->data.u.str.str + smp->data.u.str.len, beg, ptr - beg); |
| 10350 | smp->data.u.str.len += ptr - beg; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10351 | } |
| 10352 | |
| 10353 | smp->flags = SMP_F_VOL_1ST; |
| 10354 | return 1; |
| 10355 | } |
| 10356 | |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 10357 | /* This produces a 32-bit hash of the concatenation of the first occurrence of |
| 10358 | * the Host header followed by the path component if it begins with a slash ('/'). |
| 10359 | * This means that '*' will not be added, resulting in exactly the first Host |
| 10360 | * 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] | 10361 | * is hashed using the path hash followed by a full avalanche hash and provides a |
| 10362 | * 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] | 10363 | * high-traffic sites without having to store whole paths. |
| 10364 | */ |
Thierry FOURNIER | 055b9d5 | 2014-07-15 16:11:07 +0200 | [diff] [blame] | 10365 | int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10366 | 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] | 10367 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10368 | struct http_txn *txn; |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 10369 | struct hdr_ctx ctx; |
| 10370 | unsigned int hash = 0; |
| 10371 | char *ptr, *beg, *end; |
| 10372 | int len; |
| 10373 | |
| 10374 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10375 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10376 | txn = smp->strm->txn; |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 10377 | ctx.idx = 0; |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 10378 | 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] | 10379 | /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */ |
| 10380 | ptr = ctx.line + ctx.val; |
| 10381 | len = ctx.vlen; |
| 10382 | while (len--) |
| 10383 | hash = *(ptr++) + (hash << 6) + (hash << 16) - hash; |
| 10384 | } |
| 10385 | |
| 10386 | /* now retrieve the path */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 10387 | 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] | 10388 | beg = http_get_path(txn); |
| 10389 | if (!beg) |
| 10390 | beg = end; |
| 10391 | |
| 10392 | for (ptr = beg; ptr < end && *ptr != '?'; ptr++); |
| 10393 | |
| 10394 | if (beg < ptr && *beg == '/') { |
| 10395 | while (beg < ptr) |
| 10396 | hash = *(beg++) + (hash << 6) + (hash << 16) - hash; |
| 10397 | } |
| 10398 | hash = full_hash(hash); |
| 10399 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10400 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10401 | smp->data.u.sint = hash; |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 10402 | smp->flags = SMP_F_VOL_1ST; |
| 10403 | return 1; |
| 10404 | } |
| 10405 | |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10406 | /* 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] | 10407 | * path as returned by smp_fetch_base32(). The idea is to have per-source and |
| 10408 | * per-path counters. The result is a binary block from 8 to 20 bytes depending |
| 10409 | * 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] | 10410 | * that in environments where IPv6 is insignificant, truncating the output to |
| 10411 | * 8 bytes would still work. |
| 10412 | */ |
| 10413 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10414 | 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] | 10415 | { |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 10416 | struct chunk *temp; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10417 | struct connection *cli_conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10418 | |
| 10419 | if (!cli_conn) |
| 10420 | return 0; |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10421 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10422 | if (!smp_fetch_base32(args, smp, kw, private)) |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10423 | return 0; |
| 10424 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 10425 | temp = get_trash_chunk(); |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10426 | *(unsigned int *)temp->str = htonl(smp->data.u.sint); |
Willy Tarreau | 5ad6e1d | 2014-07-15 21:34:06 +0200 | [diff] [blame] | 10427 | temp->len += sizeof(unsigned int); |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10428 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10429 | switch (cli_conn->addr.from.ss_family) { |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10430 | case AF_INET: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10431 | 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] | 10432 | temp->len += 4; |
| 10433 | break; |
| 10434 | case AF_INET6: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10435 | 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] | 10436 | temp->len += 16; |
| 10437 | break; |
| 10438 | default: |
| 10439 | return 0; |
| 10440 | } |
| 10441 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10442 | smp->data.u.str = *temp; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10443 | smp->data.type = SMP_T_BIN; |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10444 | return 1; |
| 10445 | } |
| 10446 | |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 10447 | /* Extracts the query string, which comes after the question mark '?'. If no |
| 10448 | * question mark is found, nothing is returned. Otherwise it returns a sample |
| 10449 | * of type string carrying the whole query string. |
| 10450 | */ |
| 10451 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10452 | 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] | 10453 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10454 | struct http_txn *txn; |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 10455 | char *ptr, *end; |
| 10456 | |
| 10457 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10458 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10459 | txn = smp->strm->txn; |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 10460 | ptr = txn->req.chn->buf->p + txn->req.sl.rq.u; |
| 10461 | end = ptr + txn->req.sl.rq.u_l; |
| 10462 | |
| 10463 | /* look up the '?' */ |
| 10464 | do { |
| 10465 | if (ptr == end) |
| 10466 | return 0; |
| 10467 | } while (*ptr++ != '?'); |
| 10468 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10469 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10470 | smp->data.u.str.str = ptr; |
| 10471 | smp->data.u.str.len = end - ptr; |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 10472 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
| 10473 | return 1; |
| 10474 | } |
| 10475 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10476 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10477 | 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] | 10478 | { |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10479 | /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged |
| 10480 | * as a layer7 ACL, which involves automatic allocation of hdr_idx. |
| 10481 | */ |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10482 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10483 | CHECK_HTTP_MESSAGE_FIRST_PERM(); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10484 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10485 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10486 | smp->data.u.sint = 1; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10487 | return 1; |
| 10488 | } |
| 10489 | |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 10490 | /* return a valid test if the current request is the first one on the connection */ |
| 10491 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10492 | 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] | 10493 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10494 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10495 | smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST); |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 10496 | return 1; |
| 10497 | } |
| 10498 | |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10499 | /* Accepts exactly 1 argument of type userlist */ |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 10500 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10501 | 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] | 10502 | { |
| 10503 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10504 | if (!args || args->type != ARGT_USR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10505 | return 0; |
| 10506 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10507 | CHECK_HTTP_MESSAGE_FIRST(); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 10508 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10509 | if (!get_http_auth(smp->strm)) |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 10510 | return 0; |
| 10511 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10512 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10513 | 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] | 10514 | smp->strm->txn->auth.pass); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 10515 | return 1; |
| 10516 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10517 | |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10518 | /* Accepts exactly 1 argument of type userlist */ |
| 10519 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10520 | 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] | 10521 | { |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10522 | if (!args || args->type != ARGT_USR) |
| 10523 | return 0; |
| 10524 | |
| 10525 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10526 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10527 | if (!get_http_auth(smp->strm)) |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10528 | return 0; |
| 10529 | |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10530 | /* if the user does not belong to the userlist or has a wrong password, |
| 10531 | * report that it unconditionally does not match. Otherwise we return |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 10532 | * a string containing the username. |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10533 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10534 | if (!check_user(args->data.usr, smp->strm->txn->auth.user, |
| 10535 | smp->strm->txn->auth.pass)) |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 10536 | return 0; |
| 10537 | |
| 10538 | /* pat_match_auth() will need the user list */ |
| 10539 | smp->ctx.a[0] = args->data.usr; |
| 10540 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10541 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10542 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10543 | smp->data.u.str.str = smp->strm->txn->auth.user; |
| 10544 | smp->data.u.str.len = strlen(smp->strm->txn->auth.user); |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10545 | |
| 10546 | return 1; |
| 10547 | } |
| 10548 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10549 | /* Try to find the next occurrence of a cookie name in a cookie header value. |
| 10550 | * The lookup begins at <hdr>. The pointer and size of the next occurrence of |
| 10551 | * the cookie value is returned into *value and *value_l, and the function |
| 10552 | * returns a pointer to the next pointer to search from if the value was found. |
| 10553 | * Otherwise if the cookie was not found, NULL is returned and neither value |
| 10554 | * nor value_l are touched. The input <hdr> string should first point to the |
| 10555 | * header's value, and the <hdr_end> pointer must point to the first character |
| 10556 | * not part of the value. <list> must be non-zero if value may represent a list |
| 10557 | * of values (cookie headers). This makes it faster to abort parsing when no |
| 10558 | * list is expected. |
| 10559 | */ |
David Carlier | 4686f79 | 2015-09-25 14:10:50 +0100 | [diff] [blame] | 10560 | char * |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10561 | extract_cookie_value(char *hdr, const char *hdr_end, |
| 10562 | char *cookie_name, size_t cookie_name_l, int list, |
Willy Tarreau | 3fb818c | 2012-04-11 17:21:08 +0200 | [diff] [blame] | 10563 | char **value, int *value_l) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10564 | { |
| 10565 | char *equal, *att_end, *att_beg, *val_beg, *val_end; |
| 10566 | char *next; |
| 10567 | |
| 10568 | /* we search at least a cookie name followed by an equal, and more |
| 10569 | * generally something like this : |
| 10570 | * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n |
| 10571 | */ |
| 10572 | for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) { |
| 10573 | /* Iterate through all cookies on this line */ |
| 10574 | |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 10575 | while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg)) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10576 | att_beg++; |
| 10577 | |
| 10578 | /* find att_end : this is the first character after the last non |
| 10579 | * space before the equal. It may be equal to hdr_end. |
| 10580 | */ |
| 10581 | equal = att_end = att_beg; |
| 10582 | |
| 10583 | while (equal < hdr_end) { |
| 10584 | if (*equal == '=' || *equal == ';' || (list && *equal == ',')) |
| 10585 | break; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 10586 | if (HTTP_IS_SPHT(*equal++)) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10587 | continue; |
| 10588 | att_end = equal; |
| 10589 | } |
| 10590 | |
| 10591 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 10592 | * is between <att_beg> and <equal>, both may be identical. |
| 10593 | */ |
| 10594 | |
| 10595 | /* look for end of cookie if there is an equal sign */ |
| 10596 | if (equal < hdr_end && *equal == '=') { |
| 10597 | /* look for the beginning of the value */ |
| 10598 | val_beg = equal + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 10599 | while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg)) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10600 | val_beg++; |
| 10601 | |
| 10602 | /* find the end of the value, respecting quotes */ |
| 10603 | next = find_cookie_value_end(val_beg, hdr_end); |
| 10604 | |
| 10605 | /* make val_end point to the first white space or delimitor after the value */ |
| 10606 | val_end = next; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 10607 | while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1))) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10608 | val_end--; |
| 10609 | } else { |
| 10610 | val_beg = val_end = next = equal; |
| 10611 | } |
| 10612 | |
| 10613 | /* We have nothing to do with attributes beginning with '$'. However, |
| 10614 | * they will automatically be removed if a header before them is removed, |
| 10615 | * since they're supposed to be linked together. |
| 10616 | */ |
| 10617 | if (*att_beg == '$') |
| 10618 | continue; |
| 10619 | |
| 10620 | /* Ignore cookies with no equal sign */ |
| 10621 | if (equal == next) |
| 10622 | continue; |
| 10623 | |
| 10624 | /* Now we have the cookie name between att_beg and att_end, and |
| 10625 | * its value between val_beg and val_end. |
| 10626 | */ |
| 10627 | |
| 10628 | if (att_end - att_beg == cookie_name_l && |
| 10629 | memcmp(att_beg, cookie_name, cookie_name_l) == 0) { |
| 10630 | /* let's return this value and indicate where to go on from */ |
| 10631 | *value = val_beg; |
| 10632 | *value_l = val_end - val_beg; |
| 10633 | return next + 1; |
| 10634 | } |
| 10635 | |
| 10636 | /* Set-Cookie headers only have the name in the first attr=value part */ |
| 10637 | if (!list) |
| 10638 | break; |
| 10639 | } |
| 10640 | |
| 10641 | return NULL; |
| 10642 | } |
| 10643 | |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10644 | /* Fetch a captured HTTP request header. The index is the position of |
| 10645 | * the "capture" option in the configuration file |
| 10646 | */ |
| 10647 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10648 | 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] | 10649 | { |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10650 | struct proxy *fe = strm_fe(smp->strm); |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10651 | int idx; |
| 10652 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 10653 | if (!args || args->type != ARGT_SINT) |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10654 | return 0; |
| 10655 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 10656 | idx = args->data.sint; |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10657 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10658 | 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] | 10659 | return 0; |
| 10660 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10661 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10662 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10663 | smp->data.u.str.str = smp->strm->req_cap[idx]; |
| 10664 | smp->data.u.str.len = strlen(smp->strm->req_cap[idx]); |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10665 | |
| 10666 | return 1; |
| 10667 | } |
| 10668 | |
| 10669 | /* Fetch a captured HTTP response header. The index is the position of |
| 10670 | * the "capture" option in the configuration file |
| 10671 | */ |
| 10672 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10673 | 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] | 10674 | { |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10675 | struct proxy *fe = strm_fe(smp->strm); |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10676 | int idx; |
| 10677 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 10678 | if (!args || args->type != ARGT_SINT) |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10679 | return 0; |
| 10680 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 10681 | idx = args->data.sint; |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10682 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10683 | 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] | 10684 | return 0; |
| 10685 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10686 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10687 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10688 | smp->data.u.str.str = smp->strm->res_cap[idx]; |
| 10689 | smp->data.u.str.len = strlen(smp->strm->res_cap[idx]); |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10690 | |
| 10691 | return 1; |
| 10692 | } |
| 10693 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10694 | /* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */ |
| 10695 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10696 | 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] | 10697 | { |
| 10698 | struct chunk *temp; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10699 | struct http_txn *txn = smp->strm->txn; |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10700 | char *ptr; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10701 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10702 | if (!txn || !txn->uri) |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10703 | return 0; |
| 10704 | |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10705 | ptr = txn->uri; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10706 | |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10707 | while (*ptr != ' ' && *ptr != '\0') /* find first space */ |
| 10708 | ptr++; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10709 | |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10710 | temp = get_trash_chunk(); |
| 10711 | temp->str = txn->uri; |
| 10712 | temp->len = ptr - txn->uri; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10713 | smp->data.u.str = *temp; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10714 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10715 | smp->flags = SMP_F_CONST; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10716 | |
| 10717 | return 1; |
| 10718 | |
| 10719 | } |
| 10720 | |
| 10721 | /* Extracts the path in the HTTP request, the txn->uri should be filled before the call */ |
| 10722 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10723 | 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] | 10724 | { |
| 10725 | struct chunk *temp; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10726 | struct http_txn *txn = smp->strm->txn; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10727 | char *ptr; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10728 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10729 | if (!txn || !txn->uri) |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10730 | return 0; |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10731 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10732 | ptr = txn->uri; |
| 10733 | |
| 10734 | while (*ptr != ' ' && *ptr != '\0') /* find first space */ |
| 10735 | ptr++; |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10736 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10737 | if (!*ptr) |
| 10738 | return 0; |
| 10739 | |
| 10740 | ptr++; /* skip the space */ |
| 10741 | |
| 10742 | temp = get_trash_chunk(); |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10743 | ptr = temp->str = http_get_path_from_string(ptr); |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10744 | if (!ptr) |
| 10745 | return 0; |
| 10746 | while (*ptr != ' ' && *ptr != '\0') /* find space after URI */ |
| 10747 | ptr++; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10748 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10749 | smp->data.u.str = *temp; |
| 10750 | smp->data.u.str.len = ptr - temp->str; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10751 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10752 | smp->flags = SMP_F_CONST; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10753 | |
| 10754 | return 1; |
| 10755 | } |
| 10756 | |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 10757 | /* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it |
| 10758 | * as a string (either "HTTP/1.0" or "HTTP/1.1"). |
| 10759 | */ |
| 10760 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10761 | 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] | 10762 | { |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10763 | struct http_txn *txn = smp->strm->txn; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 10764 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10765 | if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST) |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 10766 | return 0; |
| 10767 | |
| 10768 | if (txn->req.flags & HTTP_MSGF_VER_11) |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10769 | smp->data.u.str.str = "HTTP/1.1"; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 10770 | else |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10771 | smp->data.u.str.str = "HTTP/1.0"; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 10772 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10773 | smp->data.u.str.len = 8; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10774 | smp->data.type = SMP_T_STR; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 10775 | smp->flags = SMP_F_CONST; |
| 10776 | return 1; |
| 10777 | |
| 10778 | } |
| 10779 | |
| 10780 | /* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it |
| 10781 | * as a string (either "HTTP/1.0" or "HTTP/1.1"). |
| 10782 | */ |
| 10783 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10784 | 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] | 10785 | { |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10786 | struct http_txn *txn = smp->strm->txn; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 10787 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10788 | if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST) |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 10789 | return 0; |
| 10790 | |
| 10791 | if (txn->rsp.flags & HTTP_MSGF_VER_11) |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10792 | smp->data.u.str.str = "HTTP/1.1"; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 10793 | else |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10794 | smp->data.u.str.str = "HTTP/1.0"; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 10795 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10796 | smp->data.u.str.len = 8; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10797 | smp->data.type = SMP_T_STR; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 10798 | smp->flags = SMP_F_CONST; |
| 10799 | return 1; |
| 10800 | |
| 10801 | } |
| 10802 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10803 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10804 | /* 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] | 10805 | * 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] | 10806 | * 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] | 10807 | * 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] | 10808 | * 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] | 10809 | * Accepts exactly 1 argument of type string. If the input options indicate |
| 10810 | * 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] | 10811 | * The returned sample is of type CSTR. Can be used to parse cookies in other |
| 10812 | * files. |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10813 | */ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10814 | 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] | 10815 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10816 | struct http_txn *txn; |
| 10817 | struct hdr_idx *idx; |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 10818 | struct hdr_ctx *ctx = smp->ctx.a[2]; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10819 | const struct http_msg *msg; |
| 10820 | const char *hdr_name; |
| 10821 | int hdr_name_len; |
| 10822 | char *sol; |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10823 | int occ = 0; |
| 10824 | int found = 0; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10825 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10826 | if (!args || args->type != ARGT_STR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10827 | return 0; |
| 10828 | |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 10829 | if (!ctx) { |
| 10830 | /* first call */ |
| 10831 | ctx = &static_hdr_ctx; |
| 10832 | ctx->idx = 0; |
| 10833 | smp->ctx.a[2] = ctx; |
| 10834 | } |
| 10835 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10836 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10837 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10838 | txn = smp->strm->txn; |
| 10839 | idx = &smp->strm->txn->hdr_idx; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10840 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10841 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10842 | msg = &txn->req; |
| 10843 | hdr_name = "Cookie"; |
| 10844 | hdr_name_len = 6; |
| 10845 | } else { |
| 10846 | msg = &txn->rsp; |
| 10847 | hdr_name = "Set-Cookie"; |
| 10848 | hdr_name_len = 10; |
| 10849 | } |
| 10850 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10851 | if (!occ && !(smp->opt & SMP_OPT_ITERATE)) |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10852 | /* no explicit occurrence and single fetch => last cookie by default */ |
| 10853 | occ = -1; |
| 10854 | |
| 10855 | /* OK so basically here, either we want only one value and it's the |
| 10856 | * last one, or we want to iterate over all of them and we fetch the |
| 10857 | * next one. |
| 10858 | */ |
| 10859 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10860 | sol = msg->chn->buf->p; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10861 | if (!(smp->flags & SMP_F_NOT_LAST)) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10862 | /* search for the header from the beginning, we must first initialize |
| 10863 | * the search parameters. |
| 10864 | */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10865 | smp->ctx.a[0] = NULL; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10866 | ctx->idx = 0; |
| 10867 | } |
| 10868 | |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10869 | smp->flags |= SMP_F_VOL_HDR; |
| 10870 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10871 | while (1) { |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10872 | /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */ |
| 10873 | if (!smp->ctx.a[0]) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10874 | if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx)) |
| 10875 | goto out; |
| 10876 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10877 | if (ctx->vlen < args->data.str.len + 1) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10878 | continue; |
| 10879 | |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10880 | smp->ctx.a[0] = ctx->line + ctx->val; |
| 10881 | smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10882 | } |
| 10883 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10884 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10885 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10886 | 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] | 10887 | args->data.str.str, args->data.str.len, |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10888 | (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ, |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10889 | &smp->data.u.str.str, |
| 10890 | &smp->data.u.str.len); |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10891 | if (smp->ctx.a[0]) { |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10892 | found = 1; |
| 10893 | if (occ >= 0) { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10894 | /* one value was returned into smp->data.u.str.{str,len} */ |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10895 | smp->flags |= SMP_F_NOT_LAST; |
| 10896 | return 1; |
| 10897 | } |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10898 | } |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10899 | /* if we're looking for last occurrence, let's loop */ |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10900 | } |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10901 | /* all cookie headers and values were scanned. If we're looking for the |
| 10902 | * last occurrence, we may return it now. |
| 10903 | */ |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10904 | out: |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10905 | smp->flags &= ~SMP_F_NOT_LAST; |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10906 | return found; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10907 | } |
| 10908 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10909 | /* 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] | 10910 | * 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] | 10911 | * multiple cookies may be parsed on the same line. The returned sample is of |
| 10912 | * type UINT. Accepts exactly 1 argument of type string. |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10913 | */ |
| 10914 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10915 | 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] | 10916 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10917 | struct http_txn *txn; |
| 10918 | struct hdr_idx *idx; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10919 | struct hdr_ctx ctx; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10920 | const struct http_msg *msg; |
| 10921 | const char *hdr_name; |
| 10922 | int hdr_name_len; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10923 | int cnt; |
| 10924 | char *val_beg, *val_end; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10925 | char *sol; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10926 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10927 | if (!args || args->type != ARGT_STR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10928 | return 0; |
| 10929 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10930 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10931 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10932 | txn = smp->strm->txn; |
| 10933 | idx = &smp->strm->txn->hdr_idx; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10934 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10935 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10936 | msg = &txn->req; |
| 10937 | hdr_name = "Cookie"; |
| 10938 | hdr_name_len = 6; |
| 10939 | } else { |
| 10940 | msg = &txn->rsp; |
| 10941 | hdr_name = "Set-Cookie"; |
| 10942 | hdr_name_len = 10; |
| 10943 | } |
| 10944 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10945 | sol = msg->chn->buf->p; |
Willy Tarreau | 46787ed | 2012-04-11 17:28:40 +0200 | [diff] [blame] | 10946 | val_end = val_beg = NULL; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10947 | ctx.idx = 0; |
| 10948 | cnt = 0; |
| 10949 | |
| 10950 | while (1) { |
| 10951 | /* Note: val_beg == NULL every time we need to fetch a new header */ |
| 10952 | if (!val_beg) { |
| 10953 | if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx)) |
| 10954 | break; |
| 10955 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10956 | if (ctx.vlen < args->data.str.len + 1) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10957 | continue; |
| 10958 | |
| 10959 | val_beg = ctx.line + ctx.val; |
| 10960 | val_end = val_beg + ctx.vlen; |
| 10961 | } |
| 10962 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10963 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10964 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10965 | while ((val_beg = extract_cookie_value(val_beg, val_end, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10966 | args->data.str.str, args->data.str.len, |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10967 | (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ, |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10968 | &smp->data.u.str.str, |
| 10969 | &smp->data.u.str.len))) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10970 | cnt++; |
| 10971 | } |
| 10972 | } |
| 10973 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10974 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10975 | smp->data.u.sint = cnt; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10976 | smp->flags |= SMP_F_VOL_HDR; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10977 | return 1; |
| 10978 | } |
| 10979 | |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 10980 | /* Fetch an cookie's integer value. The integer value is returned. It |
| 10981 | * takes a mandatory argument of type string. It relies on smp_fetch_cookie(). |
| 10982 | */ |
| 10983 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10984 | 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] | 10985 | { |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10986 | int ret = smp_fetch_cookie(args, smp, kw, private); |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 10987 | |
| 10988 | if (ret > 0) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10989 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10990 | 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] | 10991 | } |
| 10992 | |
| 10993 | return ret; |
| 10994 | } |
| 10995 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10996 | /************************************************************************/ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 10997 | /* The code below is dedicated to sample fetches */ |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 10998 | /************************************************************************/ |
| 10999 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11000 | /* |
| 11001 | * Given a path string and its length, find the position of beginning of the |
| 11002 | * query string. Returns NULL if no query string is found in the path. |
| 11003 | * |
| 11004 | * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22: |
| 11005 | * |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11006 | * find_query_string(path, n, '?') points to "yo=mama;ye=daddy" string. |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11007 | */ |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11008 | 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] | 11009 | { |
| 11010 | char *p; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 11011 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11012 | p = memchr(path, delim, path_l); |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11013 | return p ? p + 1 : NULL; |
| 11014 | } |
| 11015 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11016 | static inline int is_param_delimiter(char c, char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11017 | { |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11018 | return c == '&' || c == ';' || c == delim; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11019 | } |
| 11020 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11021 | /* after increasing a pointer value, it can exceed the first buffer |
| 11022 | * size. This function transform the value of <ptr> according with |
| 11023 | * the expected position. <chunks> is an array of the one or two |
| 11024 | * avalaible chunks. The first value is the start of the first chunk, |
| 11025 | * the second value if the end+1 of the first chunks. The third value |
| 11026 | * is NULL or the start of the second chunk and the fourth value is |
| 11027 | * the end+1 of the second chunk. The function returns 1 if does a |
| 11028 | * wrap, else returns 0. |
| 11029 | */ |
| 11030 | static inline int fix_pointer_if_wrap(const char **chunks, const char **ptr) |
| 11031 | { |
| 11032 | if (*ptr < chunks[1]) |
| 11033 | return 0; |
| 11034 | if (!chunks[2]) |
| 11035 | return 0; |
| 11036 | *ptr = chunks[2] + ( *ptr - chunks[1] ); |
| 11037 | return 1; |
| 11038 | } |
| 11039 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11040 | /* |
| 11041 | * Given a url parameter, find the starting position of the first occurence, |
| 11042 | * or NULL if the parameter is not found. |
| 11043 | * |
| 11044 | * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye", |
| 11045 | * the function will return query_string+8. |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11046 | * |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 11047 | * 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] | 11048 | * or the second chunk. The caller must be check the position before using the |
| 11049 | * result. |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11050 | */ |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11051 | static const char * |
| 11052 | find_url_param_pos(const char **chunks, |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11053 | const char* url_param_name, size_t url_param_name_l, |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11054 | char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11055 | { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11056 | const char *pos, *last, *equal; |
| 11057 | const char **bufs = chunks; |
| 11058 | int l1, l2; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11059 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11060 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11061 | pos = bufs[0]; |
| 11062 | last = bufs[1]; |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 11063 | while (pos < last) { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11064 | /* Check the equal. */ |
| 11065 | equal = pos + url_param_name_l; |
| 11066 | if (fix_pointer_if_wrap(chunks, &equal)) { |
| 11067 | if (equal >= chunks[3]) |
| 11068 | return NULL; |
| 11069 | } else { |
| 11070 | if (equal >= chunks[1]) |
| 11071 | return NULL; |
| 11072 | } |
| 11073 | if (*equal == '=') { |
| 11074 | if (pos + url_param_name_l > last) { |
| 11075 | /* process wrap case, we detect a wrap. In this case, the |
| 11076 | * comparison is performed in two parts. |
| 11077 | */ |
| 11078 | |
| 11079 | /* This is the end, we dont have any other chunk. */ |
| 11080 | if (bufs != chunks || !bufs[2]) |
| 11081 | return NULL; |
| 11082 | |
| 11083 | /* Compute the length of each part of the comparison. */ |
| 11084 | l1 = last - pos; |
| 11085 | l2 = url_param_name_l - l1; |
| 11086 | |
| 11087 | /* The second buffer is too short to contain the compared string. */ |
| 11088 | if (bufs[2] + l2 > bufs[3]) |
| 11089 | return NULL; |
| 11090 | |
| 11091 | if (memcmp(pos, url_param_name, l1) == 0 && |
| 11092 | memcmp(bufs[2], url_param_name+l1, l2) == 0) |
| 11093 | return pos; |
| 11094 | |
| 11095 | /* Perform wrapping and jump the string who fail the comparison. */ |
| 11096 | bufs += 2; |
| 11097 | pos = bufs[0] + l2; |
| 11098 | last = bufs[1]; |
| 11099 | |
| 11100 | } else { |
| 11101 | /* process a simple comparison. */ |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 11102 | if (memcmp(pos, url_param_name, url_param_name_l) == 0) |
| 11103 | return pos; |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11104 | pos += url_param_name_l + 1; |
| 11105 | if (fix_pointer_if_wrap(chunks, &pos)) |
| 11106 | last = bufs[2]; |
| 11107 | } |
| 11108 | } |
| 11109 | |
| 11110 | while (1) { |
| 11111 | /* Look for the next delimiter. */ |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 11112 | while (pos < last && !is_param_delimiter(*pos, delim)) |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11113 | pos++; |
| 11114 | if (pos < last) |
| 11115 | break; |
| 11116 | /* process buffer wrapping. */ |
| 11117 | if (bufs != chunks || !bufs[2]) |
| 11118 | return NULL; |
| 11119 | bufs += 2; |
| 11120 | pos = bufs[0]; |
| 11121 | last = bufs[1]; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11122 | } |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11123 | pos++; |
| 11124 | } |
| 11125 | return NULL; |
| 11126 | } |
| 11127 | |
| 11128 | /* |
Cyril Bonté | ce1ef4d | 2015-11-26 21:39:56 +0100 | [diff] [blame] | 11129 | * Given a url parameter name and a query string, find the next value. |
| 11130 | * An empty url_param_name matches the first available parameter. |
| 11131 | * If the parameter is found, 1 is returned and *vstart / *vend are updated to |
| 11132 | * respectively provide a pointer to the value and its end. |
| 11133 | * Otherwise, 0 is returned and vstart/vend are not modified. |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11134 | */ |
| 11135 | static int |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11136 | find_next_url_param(const char **chunks, |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11137 | const char* url_param_name, size_t url_param_name_l, |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11138 | const char **vstart, const char **vend, char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11139 | { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11140 | const char *arg_start, *qs_end; |
| 11141 | const char *value_start, *value_end; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11142 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11143 | arg_start = chunks[0]; |
| 11144 | qs_end = chunks[1]; |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11145 | if (url_param_name_l) { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11146 | /* Looks for an argument name. */ |
| 11147 | arg_start = find_url_param_pos(chunks, |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11148 | url_param_name, url_param_name_l, |
| 11149 | delim); |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11150 | /* Check for wrapping. */ |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 11151 | if (arg_start >= qs_end) |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11152 | qs_end = chunks[3]; |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11153 | } |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11154 | if (!arg_start) |
| 11155 | return 0; |
| 11156 | |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11157 | if (!url_param_name_l) { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11158 | while (1) { |
| 11159 | /* looks for the first argument. */ |
| 11160 | value_start = memchr(arg_start, '=', qs_end - arg_start); |
| 11161 | if (!value_start) { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11162 | /* Check for wrapping. */ |
| 11163 | if (arg_start >= chunks[0] && |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 11164 | arg_start < chunks[1] && |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11165 | chunks[2]) { |
| 11166 | arg_start = chunks[2]; |
| 11167 | qs_end = chunks[3]; |
| 11168 | continue; |
| 11169 | } |
| 11170 | return 0; |
| 11171 | } |
| 11172 | break; |
| 11173 | } |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11174 | value_start++; |
| 11175 | } |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11176 | else { |
| 11177 | /* Jump the argument length. */ |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11178 | value_start = arg_start + url_param_name_l + 1; |
| 11179 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11180 | /* Check for pointer wrapping. */ |
| 11181 | if (fix_pointer_if_wrap(chunks, &value_start)) { |
| 11182 | /* Update the end pointer. */ |
| 11183 | qs_end = chunks[3]; |
| 11184 | |
| 11185 | /* Check for overflow. */ |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 11186 | if (value_start >= qs_end) |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11187 | return 0; |
| 11188 | } |
| 11189 | } |
| 11190 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11191 | value_end = value_start; |
| 11192 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11193 | while (1) { |
| 11194 | while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim)) |
| 11195 | value_end++; |
| 11196 | if (value_end < qs_end) |
| 11197 | break; |
| 11198 | /* process buffer wrapping. */ |
| 11199 | if (value_end >= chunks[0] && |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 11200 | value_end < chunks[1] && |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11201 | chunks[2]) { |
| 11202 | value_end = chunks[2]; |
| 11203 | qs_end = chunks[3]; |
| 11204 | continue; |
| 11205 | } |
| 11206 | break; |
| 11207 | } |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11208 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11209 | *vstart = value_start; |
| 11210 | *vend = value_end; |
Cyril Bonté | ce1ef4d | 2015-11-26 21:39:56 +0100 | [diff] [blame] | 11211 | return 1; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11212 | } |
| 11213 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11214 | /* This scans a URL-encoded query string. It takes an optionally wrapping |
| 11215 | * string whose first contigous chunk has its beginning in ctx->a[0] and end |
| 11216 | * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The |
| 11217 | * pointers are updated for next iteration before leaving. |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11218 | */ |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11219 | static int |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11220 | 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] | 11221 | { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11222 | const char *vstart, *vend; |
| 11223 | struct chunk *temp; |
| 11224 | const char **chunks = (const char **)smp->ctx.a; |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11225 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11226 | if (!find_next_url_param(chunks, |
Thierry FOURNIER | 0948d41 | 2015-05-20 15:22:37 +0200 | [diff] [blame] | 11227 | name, name_len, |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11228 | &vstart, &vend, |
Thierry FOURNIER | 0948d41 | 2015-05-20 15:22:37 +0200 | [diff] [blame] | 11229 | delim)) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11230 | return 0; |
| 11231 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11232 | /* Create sample. If the value is contiguous, return the pointer as CONST, |
| 11233 | * if the value is wrapped, copy-it in a buffer. |
| 11234 | */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11235 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11236 | if (chunks[2] && |
| 11237 | vstart >= chunks[0] && vstart <= chunks[1] && |
| 11238 | vend >= chunks[2] && vend <= chunks[3]) { |
| 11239 | /* Wrapped case. */ |
| 11240 | temp = get_trash_chunk(); |
| 11241 | memcpy(temp->str, vstart, chunks[1] - vstart); |
| 11242 | memcpy(temp->str + ( chunks[1] - vstart ), chunks[2], vend - chunks[2]); |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11243 | smp->data.u.str.str = temp->str; |
| 11244 | smp->data.u.str.len = ( chunks[1] - vstart ) + ( vend - chunks[2] ); |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11245 | } else { |
| 11246 | /* Contiguous case. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11247 | smp->data.u.str.str = (char *)vstart; |
| 11248 | smp->data.u.str.len = vend - vstart; |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11249 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
| 11250 | } |
| 11251 | |
| 11252 | /* Update context, check wrapping. */ |
| 11253 | chunks[0] = vend; |
| 11254 | if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) { |
| 11255 | chunks[1] = chunks[3]; |
| 11256 | chunks[2] = NULL; |
| 11257 | } |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11258 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11259 | if (chunks[0] < chunks[1]) |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11260 | smp->flags |= SMP_F_NOT_LAST; |
| 11261 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11262 | return 1; |
| 11263 | } |
| 11264 | |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11265 | /* This function iterates over each parameter of the query string. It uses |
| 11266 | * 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] | 11267 | * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL. |
| 11268 | * An optional parameter name is passed in args[0], otherwise any parameter is |
| 11269 | * considered. It supports an optional delimiter argument for the beginning of |
| 11270 | * the string in args[1], which defaults to "?". |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11271 | */ |
| 11272 | static int |
| 11273 | smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 11274 | { |
| 11275 | struct http_msg *msg; |
| 11276 | char delim = '?'; |
| 11277 | const char *name; |
| 11278 | int name_len; |
| 11279 | |
Dragan Dosen | 26f77e5 | 2015-05-25 10:02:11 +0200 | [diff] [blame] | 11280 | if (!args || |
| 11281 | (args[0].type && args[0].type != ARGT_STR) || |
| 11282 | (args[1].type && args[1].type != ARGT_STR)) |
| 11283 | return 0; |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11284 | |
Dragan Dosen | 26f77e5 | 2015-05-25 10:02:11 +0200 | [diff] [blame] | 11285 | name = ""; |
| 11286 | name_len = 0; |
| 11287 | if (args->type == ARGT_STR) { |
| 11288 | name = args->data.str.str; |
| 11289 | name_len = args->data.str.len; |
| 11290 | } |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11291 | |
Dragan Dosen | 26f77e5 | 2015-05-25 10:02:11 +0200 | [diff] [blame] | 11292 | if (args[1].type) |
| 11293 | delim = *args[1].data.str.str; |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11294 | |
Dragan Dosen | 26f77e5 | 2015-05-25 10:02:11 +0200 | [diff] [blame] | 11295 | if (!smp->ctx.a[0]) { // first call, find the query string |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11296 | CHECK_HTTP_MESSAGE_FIRST(); |
| 11297 | |
| 11298 | msg = &smp->strm->txn->req; |
| 11299 | |
| 11300 | smp->ctx.a[0] = find_param_list(msg->chn->buf->p + msg->sl.rq.u, |
| 11301 | msg->sl.rq.u_l, delim); |
| 11302 | if (!smp->ctx.a[0]) |
| 11303 | return 0; |
| 11304 | |
| 11305 | 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] | 11306 | |
| 11307 | /* Assume that the context is filled with NULL pointer |
| 11308 | * before the first call. |
| 11309 | * smp->ctx.a[2] = NULL; |
| 11310 | * smp->ctx.a[3] = NULL; |
| 11311 | */ |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11312 | } |
| 11313 | |
| 11314 | return smp_fetch_param(delim, name, name_len, args, smp, kw, private); |
| 11315 | } |
| 11316 | |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 11317 | /* This function iterates over each parameter of the body. This requires |
| 11318 | * 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] | 11319 | * ctx->a[0] and ctx->a[1] to store the beginning and end of the first |
| 11320 | * contigous part of the body, and optionally ctx->a[2..3] to reference the |
| 11321 | * optional second part if the body wraps at the end of the buffer. An optional |
| 11322 | * parameter name is passed in args[0], otherwise any parameter is considered. |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 11323 | */ |
| 11324 | static int |
| 11325 | smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 11326 | { |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 11327 | struct http_msg *msg; |
| 11328 | unsigned long len; |
| 11329 | unsigned long block1; |
| 11330 | char *body; |
| 11331 | const char *name; |
| 11332 | int name_len; |
| 11333 | |
| 11334 | if (!args || (args[0].type && args[0].type != ARGT_STR)) |
| 11335 | return 0; |
| 11336 | |
| 11337 | name = ""; |
| 11338 | name_len = 0; |
| 11339 | if (args[0].type == ARGT_STR) { |
| 11340 | name = args[0].data.str.str; |
| 11341 | name_len = args[0].data.str.len; |
| 11342 | } |
| 11343 | |
| 11344 | if (!smp->ctx.a[0]) { // first call, find the query string |
| 11345 | CHECK_HTTP_MESSAGE_FIRST(); |
| 11346 | |
| 11347 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 11348 | msg = &smp->strm->txn->req; |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 11349 | else |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 11350 | msg = &smp->strm->txn->rsp; |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 11351 | |
| 11352 | len = http_body_bytes(msg); |
| 11353 | body = b_ptr(msg->chn->buf, -http_data_rewind(msg)); |
| 11354 | |
| 11355 | block1 = len; |
| 11356 | if (block1 > msg->chn->buf->data + msg->chn->buf->size - body) |
| 11357 | block1 = msg->chn->buf->data + msg->chn->buf->size - body; |
| 11358 | |
| 11359 | if (block1 == len) { |
| 11360 | /* buffer is not wrapped (or empty) */ |
| 11361 | smp->ctx.a[0] = body; |
| 11362 | smp->ctx.a[1] = body + len; |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11363 | |
| 11364 | /* Assume that the context is filled with NULL pointer |
| 11365 | * before the first call. |
| 11366 | * smp->ctx.a[2] = NULL; |
| 11367 | * smp->ctx.a[3] = NULL; |
| 11368 | */ |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 11369 | } |
| 11370 | else { |
| 11371 | /* buffer is wrapped, we need to defragment it */ |
| 11372 | smp->ctx.a[0] = body; |
| 11373 | smp->ctx.a[1] = body + block1; |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11374 | smp->ctx.a[2] = msg->chn->buf->data; |
| 11375 | smp->ctx.a[3] = msg->chn->buf->data + ( len - block1 ); |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 11376 | } |
| 11377 | } |
| 11378 | return smp_fetch_param('&', name, name_len, args, smp, kw, private); |
| 11379 | } |
| 11380 | |
Willy Tarreau | a9fddca | 2012-07-31 07:51:48 +0200 | [diff] [blame] | 11381 | /* Return the signed integer value for the specified url parameter (see url_param |
| 11382 | * above). |
| 11383 | */ |
| 11384 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11385 | 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] | 11386 | { |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11387 | int ret = smp_fetch_url_param(args, smp, kw, private); |
Willy Tarreau | a9fddca | 2012-07-31 07:51:48 +0200 | [diff] [blame] | 11388 | |
| 11389 | if (ret > 0) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11390 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11391 | 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] | 11392 | } |
| 11393 | |
| 11394 | return ret; |
| 11395 | } |
| 11396 | |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11397 | /* This produces a 32-bit hash of the concatenation of the first occurrence of |
| 11398 | * the Host header followed by the path component if it begins with a slash ('/'). |
| 11399 | * This means that '*' will not be added, resulting in exactly the first Host |
| 11400 | * entry. If no Host header is found, then the path is used. The resulting value |
| 11401 | * is hashed using the url hash followed by a full avalanche hash and provides a |
| 11402 | * 32-bit integer value. This fetch is useful for tracking per-URL activity on |
| 11403 | * high-traffic sites without having to store whole paths. |
| 11404 | * this differs from the base32 functions in that it includes the url parameters |
| 11405 | * as well as the path |
| 11406 | */ |
| 11407 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11408 | 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] | 11409 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11410 | struct http_txn *txn; |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11411 | struct hdr_ctx ctx; |
| 11412 | unsigned int hash = 0; |
| 11413 | char *ptr, *beg, *end; |
| 11414 | int len; |
| 11415 | |
| 11416 | CHECK_HTTP_MESSAGE_FIRST(); |
| 11417 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11418 | txn = smp->strm->txn; |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11419 | ctx.idx = 0; |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 11420 | 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] | 11421 | /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */ |
| 11422 | ptr = ctx.line + ctx.val; |
| 11423 | len = ctx.vlen; |
| 11424 | while (len--) |
| 11425 | hash = *(ptr++) + (hash << 6) + (hash << 16) - hash; |
| 11426 | } |
| 11427 | |
| 11428 | /* now retrieve the path */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 11429 | 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] | 11430 | beg = http_get_path(txn); |
| 11431 | if (!beg) |
| 11432 | beg = end; |
| 11433 | |
| 11434 | for (ptr = beg; ptr < end ; ptr++); |
| 11435 | |
| 11436 | if (beg < ptr && *beg == '/') { |
| 11437 | while (beg < ptr) |
| 11438 | hash = *(beg++) + (hash << 6) + (hash << 16) - hash; |
| 11439 | } |
| 11440 | hash = full_hash(hash); |
| 11441 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11442 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11443 | smp->data.u.sint = hash; |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11444 | smp->flags = SMP_F_VOL_1ST; |
| 11445 | return 1; |
| 11446 | } |
| 11447 | |
| 11448 | /* This concatenates the source address with the 32-bit hash of the Host and |
| 11449 | * URL as returned by smp_fetch_base32(). The idea is to have per-source and |
| 11450 | * per-url counters. The result is a binary block from 8 to 20 bytes depending |
| 11451 | * on the source address length. The URL hash is stored before the address so |
| 11452 | * that in environments where IPv6 is insignificant, truncating the output to |
| 11453 | * 8 bytes would still work. |
| 11454 | */ |
| 11455 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11456 | 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] | 11457 | { |
| 11458 | struct chunk *temp; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11459 | struct connection *cli_conn = objt_conn(smp->sess->origin); |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11460 | |
Dragan Dosen | db5af61 | 2016-06-16 11:23:01 +0200 | [diff] [blame] | 11461 | if (!cli_conn) |
| 11462 | return 0; |
| 11463 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11464 | if (!smp_fetch_url32(args, smp, kw, private)) |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11465 | return 0; |
| 11466 | |
| 11467 | temp = get_trash_chunk(); |
Dragan Dosen | e5f4133 | 2016-06-16 11:08:08 +0200 | [diff] [blame] | 11468 | *(unsigned int *)temp->str = htonl(smp->data.u.sint); |
| 11469 | temp->len += sizeof(unsigned int); |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11470 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 11471 | switch (cli_conn->addr.from.ss_family) { |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11472 | case AF_INET: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 11473 | 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] | 11474 | temp->len += 4; |
| 11475 | break; |
| 11476 | case AF_INET6: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 11477 | 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] | 11478 | temp->len += 16; |
| 11479 | break; |
| 11480 | default: |
| 11481 | return 0; |
| 11482 | } |
| 11483 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11484 | smp->data.u.str = *temp; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11485 | smp->data.type = SMP_T_BIN; |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11486 | return 1; |
| 11487 | } |
| 11488 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 11489 | /* This function is used to validate the arguments passed to any "hdr" fetch |
| 11490 | * keyword. These keywords support an optional positive or negative occurrence |
| 11491 | * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It |
| 11492 | * is assumed that the types are already the correct ones. Returns 0 on error, |
| 11493 | * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an |
| 11494 | * error message in case of error, that the caller is responsible for freeing. |
| 11495 | * The initial location must either be freeable or NULL. |
| 11496 | */ |
Thierry FOURNIER | 49f45af | 2014-12-08 19:50:43 +0100 | [diff] [blame] | 11497 | int val_hdr(struct arg *arg, char **err_msg) |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 11498 | { |
| 11499 | 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] | 11500 | memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY); |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 11501 | return 0; |
| 11502 | } |
| 11503 | return 1; |
| 11504 | } |
| 11505 | |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 11506 | /* takes an UINT value on input supposed to represent the time since EPOCH, |
| 11507 | * adds an optional offset found in args[0] and emits a string representing |
| 11508 | * the date in RFC-1123/5322 format. |
| 11509 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11510 | 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] | 11511 | { |
Cyril Bonté | f78d896 | 2016-01-22 19:40:28 +0100 | [diff] [blame] | 11512 | const char day[7][4] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 11513 | const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; |
| 11514 | struct chunk *temp; |
| 11515 | struct tm *tm; |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 11516 | /* 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] | 11517 | time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL; |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 11518 | |
| 11519 | /* add offset */ |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 11520 | if (args && (args[0].type == ARGT_SINT)) |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 11521 | curr_date += args[0].data.sint; |
| 11522 | |
| 11523 | tm = gmtime(&curr_date); |
Thierry FOURNIER | fac9ccf | 2015-07-08 00:15:20 +0200 | [diff] [blame] | 11524 | if (!tm) |
| 11525 | return 0; |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 11526 | |
| 11527 | temp = get_trash_chunk(); |
| 11528 | temp->len = snprintf(temp->str, temp->size - temp->len, |
| 11529 | "%s, %02d %s %04d %02d:%02d:%02d GMT", |
| 11530 | day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year, |
| 11531 | tm->tm_hour, tm->tm_min, tm->tm_sec); |
| 11532 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11533 | smp->data.u.str = *temp; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11534 | smp->data.type = SMP_T_STR; |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 11535 | return 1; |
| 11536 | } |
| 11537 | |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11538 | /* Match language range with language tag. RFC2616 14.4: |
| 11539 | * |
| 11540 | * A language-range matches a language-tag if it exactly equals |
| 11541 | * the tag, or if it exactly equals a prefix of the tag such |
| 11542 | * that the first tag character following the prefix is "-". |
| 11543 | * |
| 11544 | * Return 1 if the strings match, else return 0. |
| 11545 | */ |
| 11546 | static inline int language_range_match(const char *range, int range_len, |
| 11547 | const char *tag, int tag_len) |
| 11548 | { |
| 11549 | const char *end = range + range_len; |
| 11550 | const char *tend = tag + tag_len; |
| 11551 | while (range < end) { |
| 11552 | if (*range == '-' && tag == tend) |
| 11553 | return 1; |
| 11554 | if (*range != *tag || tag == tend) |
| 11555 | return 0; |
| 11556 | range++; |
| 11557 | tag++; |
| 11558 | } |
| 11559 | /* Return true only if the last char of the tag is matched. */ |
| 11560 | return tag == tend; |
| 11561 | } |
| 11562 | |
| 11563 | /* 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] | 11564 | 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] | 11565 | { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11566 | const char *al = smp->data.u.str.str; |
| 11567 | const char *end = al + smp->data.u.str.len; |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11568 | const char *token; |
| 11569 | int toklen; |
| 11570 | int qvalue; |
| 11571 | const char *str; |
| 11572 | const char *w; |
| 11573 | int best_q = 0; |
| 11574 | |
| 11575 | /* Set the constant to the sample, because the output of the |
| 11576 | * function will be peek in the constant configuration string. |
| 11577 | */ |
| 11578 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11579 | smp->data.u.str.size = 0; |
| 11580 | smp->data.u.str.str = ""; |
| 11581 | smp->data.u.str.len = 0; |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11582 | |
| 11583 | /* Parse the accept language */ |
| 11584 | while (1) { |
| 11585 | |
| 11586 | /* Jump spaces, quit if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 11587 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11588 | al++; |
| 11589 | if (al >= end) |
| 11590 | break; |
| 11591 | |
| 11592 | /* Start of the fisrt word. */ |
| 11593 | token = al; |
| 11594 | |
| 11595 | /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 11596 | while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11597 | al++; |
| 11598 | if (al == token) |
| 11599 | goto expect_comma; |
| 11600 | |
| 11601 | /* Length of the token. */ |
| 11602 | toklen = al - token; |
| 11603 | qvalue = 1000; |
| 11604 | |
| 11605 | /* Check if the token exists in the list. If the token not exists, |
| 11606 | * jump to the next token. |
| 11607 | */ |
| 11608 | str = args[0].data.str.str; |
| 11609 | w = str; |
| 11610 | while (1) { |
| 11611 | if (*str == ';' || *str == '\0') { |
| 11612 | if (language_range_match(token, toklen, w, str-w)) |
| 11613 | goto look_for_q; |
| 11614 | if (*str == '\0') |
| 11615 | goto expect_comma; |
| 11616 | w = str + 1; |
| 11617 | } |
| 11618 | str++; |
| 11619 | } |
| 11620 | goto expect_comma; |
| 11621 | |
| 11622 | look_for_q: |
| 11623 | |
| 11624 | /* Jump spaces, quit if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 11625 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11626 | al++; |
| 11627 | if (al >= end) |
| 11628 | goto process_value; |
| 11629 | |
| 11630 | /* If ',' is found, process the result */ |
| 11631 | if (*al == ',') |
| 11632 | goto process_value; |
| 11633 | |
| 11634 | /* If the character is different from ';', look |
| 11635 | * for the end of the header part in best effort. |
| 11636 | */ |
| 11637 | if (*al != ';') |
| 11638 | goto expect_comma; |
| 11639 | |
| 11640 | /* Assumes that the char is ';', now expect "q=". */ |
| 11641 | al++; |
| 11642 | |
| 11643 | /* Jump spaces, process value if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 11644 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11645 | al++; |
| 11646 | if (al >= end) |
| 11647 | goto process_value; |
| 11648 | |
| 11649 | /* Expect 'q'. If no 'q', continue in best effort */ |
| 11650 | if (*al != 'q') |
| 11651 | goto process_value; |
| 11652 | al++; |
| 11653 | |
| 11654 | /* Jump spaces, process value if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 11655 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11656 | al++; |
| 11657 | if (al >= end) |
| 11658 | goto process_value; |
| 11659 | |
| 11660 | /* Expect '='. If no '=', continue in best effort */ |
| 11661 | if (*al != '=') |
| 11662 | goto process_value; |
| 11663 | al++; |
| 11664 | |
| 11665 | /* Jump spaces, process value if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 11666 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11667 | al++; |
| 11668 | if (al >= end) |
| 11669 | goto process_value; |
| 11670 | |
| 11671 | /* Parse the q value. */ |
| 11672 | qvalue = parse_qvalue(al, &al); |
| 11673 | |
| 11674 | process_value: |
| 11675 | |
| 11676 | /* If the new q value is the best q value, then store the associated |
| 11677 | * language in the response. If qvalue is the biggest value (1000), |
| 11678 | * break the process. |
| 11679 | */ |
| 11680 | if (qvalue > best_q) { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11681 | smp->data.u.str.str = (char *)w; |
| 11682 | smp->data.u.str.len = str - w; |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11683 | if (qvalue >= 1000) |
| 11684 | break; |
| 11685 | best_q = qvalue; |
| 11686 | } |
| 11687 | |
| 11688 | expect_comma: |
| 11689 | |
| 11690 | /* Expect comma or end. If the end is detected, quit the loop. */ |
| 11691 | while (al < end && *al != ',') |
| 11692 | al++; |
| 11693 | if (al >= end) |
| 11694 | break; |
| 11695 | |
| 11696 | /* Comma is found, jump it and restart the analyzer. */ |
| 11697 | al++; |
| 11698 | } |
| 11699 | |
| 11700 | /* Set default value if required. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11701 | if (smp->data.u.str.len == 0 && args[1].type == ARGT_STR) { |
| 11702 | smp->data.u.str.str = args[1].data.str.str; |
| 11703 | smp->data.u.str.len = args[1].data.str.len; |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11704 | } |
| 11705 | |
| 11706 | /* Return true only if a matching language was found. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11707 | return smp->data.u.str.len != 0; |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11708 | } |
| 11709 | |
Thierry FOURNIER | 82ff3c9 | 2015-05-07 15:46:20 +0200 | [diff] [blame] | 11710 | /* This fetch url-decode any input string. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11711 | 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] | 11712 | { |
| 11713 | /* If the constant flag is set or if not size is avalaible at |
| 11714 | * the end of the buffer, copy the string in other buffer |
| 11715 | * before decoding. |
| 11716 | */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11717 | 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] | 11718 | struct chunk *str = get_trash_chunk(); |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11719 | memcpy(str->str, smp->data.u.str.str, smp->data.u.str.len); |
| 11720 | smp->data.u.str.str = str->str; |
| 11721 | smp->data.u.str.size = str->size; |
Thierry FOURNIER | 82ff3c9 | 2015-05-07 15:46:20 +0200 | [diff] [blame] | 11722 | smp->flags &= ~SMP_F_CONST; |
| 11723 | } |
| 11724 | |
| 11725 | /* Add final \0 required by url_decode(), and convert the input string. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11726 | smp->data.u.str.str[smp->data.u.str.len] = '\0'; |
| 11727 | smp->data.u.str.len = url_decode(smp->data.u.str.str); |
Christopher Faulet | a258479 | 2017-10-05 10:03:12 +0200 | [diff] [blame] | 11728 | return (smp->data.u.str.len >= 0); |
Thierry FOURNIER | 82ff3c9 | 2015-05-07 15:46:20 +0200 | [diff] [blame] | 11729 | } |
| 11730 | |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 11731 | static int smp_conv_req_capture(const struct arg *args, struct sample *smp, void *private) |
| 11732 | { |
| 11733 | struct proxy *fe = strm_fe(smp->strm); |
| 11734 | int idx, i; |
| 11735 | struct cap_hdr *hdr; |
| 11736 | int len; |
| 11737 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 11738 | if (!args || args->type != ARGT_SINT) |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 11739 | return 0; |
| 11740 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 11741 | idx = args->data.sint; |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 11742 | |
| 11743 | /* Check the availibity of the capture id. */ |
| 11744 | if (idx > fe->nb_req_cap - 1) |
| 11745 | return 0; |
| 11746 | |
| 11747 | /* Look for the original configuration. */ |
| 11748 | for (hdr = fe->req_cap, i = fe->nb_req_cap - 1; |
| 11749 | hdr != NULL && i != idx ; |
| 11750 | i--, hdr = hdr->next); |
| 11751 | if (!hdr) |
| 11752 | return 0; |
| 11753 | |
| 11754 | /* check for the memory allocation */ |
| 11755 | if (smp->strm->req_cap[hdr->index] == NULL) |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 11756 | smp->strm->req_cap[hdr->index] = pool_alloc(hdr->pool); |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 11757 | if (smp->strm->req_cap[hdr->index] == NULL) |
| 11758 | return 0; |
| 11759 | |
| 11760 | /* Check length. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11761 | len = smp->data.u.str.len; |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 11762 | if (len > hdr->len) |
| 11763 | len = hdr->len; |
| 11764 | |
| 11765 | /* Capture input data. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11766 | memcpy(smp->strm->req_cap[idx], smp->data.u.str.str, len); |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 11767 | smp->strm->req_cap[idx][len] = '\0'; |
| 11768 | |
| 11769 | return 1; |
| 11770 | } |
| 11771 | |
| 11772 | static int smp_conv_res_capture(const struct arg *args, struct sample *smp, void *private) |
| 11773 | { |
| 11774 | struct proxy *fe = strm_fe(smp->strm); |
| 11775 | int idx, i; |
| 11776 | struct cap_hdr *hdr; |
| 11777 | int len; |
| 11778 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 11779 | if (!args || args->type != ARGT_SINT) |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 11780 | return 0; |
| 11781 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 11782 | idx = args->data.sint; |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 11783 | |
| 11784 | /* Check the availibity of the capture id. */ |
| 11785 | if (idx > fe->nb_rsp_cap - 1) |
| 11786 | return 0; |
| 11787 | |
| 11788 | /* Look for the original configuration. */ |
| 11789 | for (hdr = fe->rsp_cap, i = fe->nb_rsp_cap - 1; |
| 11790 | hdr != NULL && i != idx ; |
| 11791 | i--, hdr = hdr->next); |
| 11792 | if (!hdr) |
| 11793 | return 0; |
| 11794 | |
| 11795 | /* check for the memory allocation */ |
| 11796 | if (smp->strm->res_cap[hdr->index] == NULL) |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 11797 | smp->strm->res_cap[hdr->index] = pool_alloc(hdr->pool); |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 11798 | if (smp->strm->res_cap[hdr->index] == NULL) |
| 11799 | return 0; |
| 11800 | |
| 11801 | /* Check length. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11802 | len = smp->data.u.str.len; |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 11803 | if (len > hdr->len) |
| 11804 | len = hdr->len; |
| 11805 | |
| 11806 | /* Capture input data. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11807 | memcpy(smp->strm->res_cap[idx], smp->data.u.str.str, len); |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 11808 | smp->strm->res_cap[idx][len] = '\0'; |
| 11809 | |
| 11810 | return 1; |
| 11811 | } |
| 11812 | |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11813 | /* 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] | 11814 | * takes the string from the variable 'replace' with length 'len', then modifies |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11815 | * the relevant part of the request line accordingly. Then it updates various |
| 11816 | * pointers to the next elements which were moved, and the total buffer length. |
| 11817 | * 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] | 11818 | * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal |
| 11819 | * error, though this can be revisited when this code is finally exploited. |
| 11820 | * |
| 11821 | * 'action' can be '0' to replace method, '1' to replace path, '2' to replace |
| 11822 | * query string and 3 to replace uri. |
| 11823 | * |
| 11824 | * In query string case, the mark question '?' must be set at the start of the |
| 11825 | * string by the caller, event if the replacement query string is empty. |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11826 | */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11827 | int http_replace_req_line(int action, const char *replace, int len, |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 11828 | struct proxy *px, struct stream *s) |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11829 | { |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 11830 | struct http_txn *txn = s->txn; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11831 | char *cur_ptr, *cur_end; |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11832 | int offset = 0; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11833 | int delta; |
| 11834 | |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11835 | switch (action) { |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11836 | case 0: // method |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 11837 | cur_ptr = s->req.buf->p; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11838 | cur_end = cur_ptr + txn->req.sl.rq.m_l; |
| 11839 | |
| 11840 | /* adjust req line offsets and lengths */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11841 | delta = len - offset - (cur_end - cur_ptr); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11842 | txn->req.sl.rq.m_l += delta; |
| 11843 | txn->req.sl.rq.u += delta; |
| 11844 | txn->req.sl.rq.v += delta; |
| 11845 | break; |
| 11846 | |
| 11847 | case 1: // path |
| 11848 | cur_ptr = http_get_path(txn); |
| 11849 | if (!cur_ptr) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 11850 | cur_ptr = s->req.buf->p + txn->req.sl.rq.u; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11851 | |
| 11852 | cur_end = cur_ptr; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 11853 | 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] | 11854 | cur_end++; |
| 11855 | |
| 11856 | /* adjust req line offsets and lengths */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11857 | delta = len - offset - (cur_end - cur_ptr); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11858 | txn->req.sl.rq.u_l += delta; |
| 11859 | txn->req.sl.rq.v += delta; |
| 11860 | break; |
| 11861 | |
| 11862 | case 2: // query |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11863 | offset = 1; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 11864 | cur_ptr = s->req.buf->p + txn->req.sl.rq.u; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11865 | cur_end = cur_ptr + txn->req.sl.rq.u_l; |
| 11866 | while (cur_ptr < cur_end && *cur_ptr != '?') |
| 11867 | cur_ptr++; |
| 11868 | |
| 11869 | /* skip the question mark or indicate that we must insert it |
| 11870 | * (but only if the format string is not empty then). |
| 11871 | */ |
| 11872 | if (cur_ptr < cur_end) |
| 11873 | cur_ptr++; |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11874 | else if (len > 1) |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11875 | offset = 0; |
| 11876 | |
| 11877 | /* adjust req line offsets and lengths */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11878 | delta = len - offset - (cur_end - cur_ptr); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11879 | txn->req.sl.rq.u_l += delta; |
| 11880 | txn->req.sl.rq.v += delta; |
| 11881 | break; |
| 11882 | |
| 11883 | case 3: // uri |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 11884 | cur_ptr = s->req.buf->p + txn->req.sl.rq.u; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11885 | cur_end = cur_ptr + txn->req.sl.rq.u_l; |
| 11886 | |
| 11887 | /* adjust req line offsets and lengths */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11888 | delta = len - offset - (cur_end - cur_ptr); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11889 | txn->req.sl.rq.u_l += delta; |
| 11890 | txn->req.sl.rq.v += delta; |
| 11891 | break; |
| 11892 | |
| 11893 | default: |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11894 | return -1; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11895 | } |
| 11896 | |
| 11897 | /* commit changes and adjust end of message */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11898 | 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] | 11899 | txn->req.sl.rq.l += delta; |
| 11900 | txn->hdr_idx.v[0].len += delta; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11901 | http_msg_move_end(&txn->req, delta); |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11902 | return 0; |
| 11903 | } |
| 11904 | |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 11905 | /* This function replace the HTTP status code and the associated message. The |
| 11906 | * variable <status> contains the new status code. This function never fails. |
| 11907 | */ |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 11908 | 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] | 11909 | { |
| 11910 | struct http_txn *txn = s->txn; |
| 11911 | char *cur_ptr, *cur_end; |
| 11912 | int delta; |
| 11913 | char *res; |
| 11914 | int c_l; |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 11915 | const char *msg = reason; |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 11916 | int msg_len; |
| 11917 | |
| 11918 | chunk_reset(&trash); |
| 11919 | |
| 11920 | res = ultoa_o(status, trash.str, trash.size); |
| 11921 | c_l = res - trash.str; |
| 11922 | |
| 11923 | trash.str[c_l] = ' '; |
| 11924 | trash.len = c_l + 1; |
| 11925 | |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 11926 | /* Do we have a custom reason format string? */ |
| 11927 | if (msg == NULL) |
| 11928 | msg = get_reason(status); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 11929 | msg_len = strlen(msg); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 11930 | strncpy(&trash.str[trash.len], msg, trash.size - trash.len); |
| 11931 | trash.len += msg_len; |
| 11932 | |
| 11933 | cur_ptr = s->res.buf->p + txn->rsp.sl.st.c; |
| 11934 | cur_end = s->res.buf->p + txn->rsp.sl.st.r + txn->rsp.sl.st.r_l; |
| 11935 | |
| 11936 | /* commit changes and adjust message */ |
| 11937 | delta = buffer_replace2(s->res.buf, cur_ptr, cur_end, trash.str, trash.len); |
| 11938 | |
| 11939 | /* adjust res line offsets and lengths */ |
| 11940 | txn->rsp.sl.st.r += c_l - txn->rsp.sl.st.c_l; |
| 11941 | txn->rsp.sl.st.c_l = c_l; |
| 11942 | txn->rsp.sl.st.r_l = msg_len; |
| 11943 | |
| 11944 | delta = trash.len - (cur_end - cur_ptr); |
| 11945 | txn->rsp.sl.st.l += delta; |
| 11946 | txn->hdr_idx.v[0].len += delta; |
| 11947 | http_msg_move_end(&txn->rsp, delta); |
| 11948 | } |
| 11949 | |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11950 | /* This function executes one of the set-{method,path,query,uri} actions. It |
| 11951 | * 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] | 11952 | * the action to be performed in <http.action>, previously filled by function |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11953 | * parse_set_req_line(). The replacement action is excuted by the function |
Thierry FOURNIER | 3f4bc65 | 2015-08-26 16:23:34 +0200 | [diff] [blame] | 11954 | * http_action_set_req_line(). It always returns ACT_RET_CONT. If an error |
| 11955 | * occurs the action is canceled, but the rule processing continue. |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11956 | */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 11957 | 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] | 11958 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11959 | { |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 11960 | struct chunk *replace; |
| 11961 | enum act_return ret = ACT_RET_ERR; |
| 11962 | |
| 11963 | replace = alloc_trash_chunk(); |
| 11964 | if (!replace) |
| 11965 | goto leave; |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11966 | |
| 11967 | /* If we have to create a query string, prepare a '?'. */ |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 11968 | if (rule->arg.http.action == 2) |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 11969 | replace->str[replace->len++] = '?'; |
| 11970 | replace->len += build_logline(s, replace->str + replace->len, replace->size - replace->len, |
| 11971 | &rule->arg.http.logfmt); |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11972 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 11973 | http_replace_req_line(rule->arg.http.action, replace->str, replace->len, px, s); |
| 11974 | |
| 11975 | ret = ACT_RET_CONT; |
| 11976 | |
| 11977 | leave: |
| 11978 | free_trash_chunk(replace); |
| 11979 | return ret; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11980 | } |
| 11981 | |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 11982 | /* This function is just a compliant action wrapper for "set-status". */ |
| 11983 | 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] | 11984 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 11985 | { |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 11986 | http_set_status(rule->arg.status.code, rule->arg.status.reason, s); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 11987 | return ACT_RET_CONT; |
| 11988 | } |
| 11989 | |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11990 | /* parse an http-request action among : |
| 11991 | * set-method |
| 11992 | * set-path |
| 11993 | * set-query |
| 11994 | * set-uri |
| 11995 | * |
| 11996 | * All of them accept a single argument of type string representing a log-format. |
| 11997 | * The resulting rule makes use of arg->act.p[0..1] to store the log-format list |
| 11998 | * 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] | 11999 | * 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] | 12000 | */ |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12001 | enum act_parse_ret parse_set_req_line(const char **args, int *orig_arg, struct proxy *px, |
| 12002 | struct act_rule *rule, char **err) |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12003 | { |
| 12004 | int cur_arg = *orig_arg; |
| 12005 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 12006 | rule->action = ACT_CUSTOM; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12007 | |
| 12008 | switch (args[0][4]) { |
| 12009 | case 'm' : |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 12010 | rule->arg.http.action = 0; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12011 | rule->action_ptr = http_action_set_req_line; |
| 12012 | break; |
| 12013 | case 'p' : |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 12014 | rule->arg.http.action = 1; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12015 | rule->action_ptr = http_action_set_req_line; |
| 12016 | break; |
| 12017 | case 'q' : |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 12018 | rule->arg.http.action = 2; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12019 | rule->action_ptr = http_action_set_req_line; |
| 12020 | break; |
| 12021 | case 'u' : |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 12022 | rule->arg.http.action = 3; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12023 | rule->action_ptr = http_action_set_req_line; |
| 12024 | break; |
| 12025 | default: |
| 12026 | memprintf(err, "internal error: unhandled action '%s'", args[0]); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12027 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12028 | } |
| 12029 | |
| 12030 | if (!*args[cur_arg] || |
| 12031 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 12032 | memprintf(err, "expects exactly 1 argument <format>"); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12033 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12034 | } |
| 12035 | |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 12036 | LIST_INIT(&rule->arg.http.logfmt); |
Olivier Houchard | fbc74e8 | 2017-11-24 16:54:05 +0100 | [diff] [blame] | 12037 | px->conf.args.ctx = ARGC_HRQ; |
| 12038 | if (!parse_logformat_string(args[cur_arg], px, &rule->arg.http.logfmt, LOG_OPT_HTTP, |
| 12039 | (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] | 12040 | return ACT_RET_PRS_ERR; |
| 12041 | } |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12042 | |
| 12043 | (*orig_arg)++; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12044 | return ACT_RET_PRS_OK; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12045 | } |
| 12046 | |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 12047 | /* parse set-status action: |
| 12048 | * This action accepts a single argument of type int representing |
| 12049 | * an http status code. It returns ACT_RET_PRS_OK on success, |
| 12050 | * ACT_RET_PRS_ERR on error. |
| 12051 | */ |
| 12052 | enum act_parse_ret parse_http_set_status(const char **args, int *orig_arg, struct proxy *px, |
| 12053 | struct act_rule *rule, char **err) |
| 12054 | { |
| 12055 | char *error; |
| 12056 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 12057 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 12058 | rule->action_ptr = action_http_set_status; |
| 12059 | |
| 12060 | /* Check if an argument is available */ |
| 12061 | if (!*args[*orig_arg]) { |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 12062 | memprintf(err, "expects 1 argument: <status>; or 3 arguments: <status> reason <fmt>"); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 12063 | return ACT_RET_PRS_ERR; |
| 12064 | } |
| 12065 | |
| 12066 | /* convert status code as integer */ |
| 12067 | rule->arg.status.code = strtol(args[*orig_arg], &error, 10); |
| 12068 | if (*error != '\0' || rule->arg.status.code < 100 || rule->arg.status.code > 999) { |
| 12069 | memprintf(err, "expects an integer status code between 100 and 999"); |
| 12070 | return ACT_RET_PRS_ERR; |
| 12071 | } |
| 12072 | |
| 12073 | (*orig_arg)++; |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 12074 | |
| 12075 | /* set custom reason string */ |
| 12076 | rule->arg.status.reason = NULL; // If null, we use the default reason for the status code. |
| 12077 | if (*args[*orig_arg] && strcmp(args[*orig_arg], "reason") == 0 && |
| 12078 | (*args[*orig_arg + 1] && strcmp(args[*orig_arg + 1], "if") != 0 && strcmp(args[*orig_arg + 1], "unless") != 0)) { |
| 12079 | (*orig_arg)++; |
| 12080 | rule->arg.status.reason = strdup(args[*orig_arg]); |
| 12081 | (*orig_arg)++; |
| 12082 | } |
| 12083 | |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 12084 | return ACT_RET_PRS_OK; |
| 12085 | } |
| 12086 | |
Willy Tarreau | 53275e8 | 2017-11-24 07:52:01 +0100 | [diff] [blame] | 12087 | /* This function executes the "reject" HTTP action. It clears the request and |
| 12088 | * response buffer without sending any response. It can be useful as an HTTP |
| 12089 | * alternative to the silent-drop action to defend against DoS attacks, and may |
| 12090 | * also be used with HTTP/2 to close a connection instead of just a stream. |
| 12091 | * The txn status is unchanged, indicating no response was sent. The termination |
| 12092 | * flags will indicate "PR". It always returns ACT_RET_STOP. |
| 12093 | */ |
| 12094 | enum act_return http_action_reject(struct act_rule *rule, struct proxy *px, |
| 12095 | struct session *sess, struct stream *s, int flags) |
| 12096 | { |
| 12097 | channel_abort(&s->req); |
| 12098 | channel_abort(&s->res); |
| 12099 | s->req.analysers = 0; |
| 12100 | s->res.analysers = 0; |
| 12101 | |
| 12102 | HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1); |
| 12103 | HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1); |
| 12104 | if (sess->listener && sess->listener->counters) |
| 12105 | HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1); |
| 12106 | |
| 12107 | if (!(s->flags & SF_ERR_MASK)) |
| 12108 | s->flags |= SF_ERR_PRXCOND; |
| 12109 | if (!(s->flags & SF_FINST_MASK)) |
| 12110 | s->flags |= SF_FINST_R; |
| 12111 | |
| 12112 | return ACT_RET_CONT; |
| 12113 | } |
| 12114 | |
| 12115 | /* parse the "reject" action: |
| 12116 | * This action takes no argument and returns ACT_RET_PRS_OK on success, |
| 12117 | * ACT_RET_PRS_ERR on error. |
| 12118 | */ |
| 12119 | enum act_parse_ret parse_http_action_reject(const char **args, int *orig_arg, struct proxy *px, |
| 12120 | struct act_rule *rule, char **err) |
| 12121 | { |
| 12122 | rule->action = ACT_CUSTOM; |
| 12123 | rule->action_ptr = http_action_reject; |
| 12124 | return ACT_RET_PRS_OK; |
| 12125 | } |
| 12126 | |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12127 | /* This function executes the "capture" action. It executes a fetch expression, |
| 12128 | * turns the result into a string and puts it in a capture slot. It always |
| 12129 | * returns 1. If an error occurs the action is cancelled, but the rule |
| 12130 | * processing continues. |
| 12131 | */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12132 | 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] | 12133 | struct session *sess, struct stream *s, int flags) |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12134 | { |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12135 | struct sample *key; |
Thierry FOURNIER | 32b1500 | 2015-07-31 08:56:16 +0200 | [diff] [blame] | 12136 | struct cap_hdr *h = rule->arg.cap.hdr; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12137 | char **cap = s->req_cap; |
| 12138 | int len; |
| 12139 | |
Thierry FOURNIER | 32b1500 | 2015-07-31 08:56:16 +0200 | [diff] [blame] | 12140 | 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] | 12141 | if (!key) |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12142 | return ACT_RET_CONT; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12143 | |
| 12144 | if (cap[h->index] == NULL) |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 12145 | cap[h->index] = pool_alloc(h->pool); |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12146 | |
| 12147 | if (cap[h->index] == NULL) /* no more capture memory */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12148 | return ACT_RET_CONT; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12149 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12150 | len = key->data.u.str.len; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12151 | if (len > h->len) |
| 12152 | len = h->len; |
| 12153 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12154 | memcpy(cap[h->index], key->data.u.str.str, len); |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12155 | cap[h->index][len] = 0; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12156 | return ACT_RET_CONT; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12157 | } |
| 12158 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12159 | /* This function executes the "capture" action and store the result in a |
| 12160 | * capture slot if exists. It executes a fetch expression, turns the result |
| 12161 | * into a string and puts it in a capture slot. It always returns 1. If an |
| 12162 | * error occurs the action is cancelled, but the rule processing continues. |
| 12163 | */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12164 | 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] | 12165 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12166 | { |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12167 | struct sample *key; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12168 | struct cap_hdr *h; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12169 | char **cap = s->req_cap; |
| 12170 | struct proxy *fe = strm_fe(s); |
| 12171 | int len; |
| 12172 | int i; |
| 12173 | |
| 12174 | /* Look for the original configuration. */ |
| 12175 | for (h = fe->req_cap, i = fe->nb_req_cap - 1; |
Thierry FOURNIER | e209797 | 2015-07-31 08:56:35 +0200 | [diff] [blame] | 12176 | h != NULL && i != rule->arg.capid.idx ; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12177 | i--, h = h->next); |
| 12178 | if (!h) |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12179 | return ACT_RET_CONT; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12180 | |
Thierry FOURNIER | e209797 | 2015-07-31 08:56:35 +0200 | [diff] [blame] | 12181 | 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] | 12182 | if (!key) |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12183 | return ACT_RET_CONT; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12184 | |
| 12185 | if (cap[h->index] == NULL) |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 12186 | cap[h->index] = pool_alloc(h->pool); |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12187 | |
| 12188 | if (cap[h->index] == NULL) /* no more capture memory */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12189 | return ACT_RET_CONT; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12190 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12191 | len = key->data.u.str.len; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12192 | if (len > h->len) |
| 12193 | len = h->len; |
| 12194 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12195 | memcpy(cap[h->index], key->data.u.str.str, len); |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12196 | cap[h->index][len] = 0; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12197 | return ACT_RET_CONT; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12198 | } |
| 12199 | |
Christopher Faulet | 29730ba | 2017-09-18 15:26:32 +0200 | [diff] [blame] | 12200 | /* Check an "http-request capture" action. |
| 12201 | * |
| 12202 | * The function returns 1 in success case, otherwise, it returns 0 and err is |
| 12203 | * filled. |
| 12204 | */ |
| 12205 | int check_http_req_capture(struct act_rule *rule, struct proxy *px, char **err) |
| 12206 | { |
Christopher Faulet | fd608dd | 2017-12-04 09:45:15 +0100 | [diff] [blame] | 12207 | if (rule->action_ptr != http_action_req_capture_by_id) |
| 12208 | return 1; |
| 12209 | |
Christopher Faulet | 29730ba | 2017-09-18 15:26:32 +0200 | [diff] [blame] | 12210 | if (rule->arg.capid.idx >= px->nb_req_cap) { |
| 12211 | memprintf(err, "unable to find capture id '%d' referenced by http-request capture rule", |
| 12212 | rule->arg.capid.idx); |
| 12213 | return 0; |
| 12214 | } |
| 12215 | |
| 12216 | return 1; |
| 12217 | } |
| 12218 | |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12219 | /* parse an "http-request capture" action. It takes a single argument which is |
| 12220 | * 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] | 12221 | * 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] | 12222 | * 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] | 12223 | */ |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12224 | enum act_parse_ret parse_http_req_capture(const char **args, int *orig_arg, struct proxy *px, |
| 12225 | struct act_rule *rule, char **err) |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12226 | { |
| 12227 | struct sample_expr *expr; |
| 12228 | struct cap_hdr *hdr; |
| 12229 | int cur_arg; |
Willy Tarreau | 3986ac1 | 2015-05-08 16:13:42 +0200 | [diff] [blame] | 12230 | int len = 0; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12231 | |
| 12232 | for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++) |
| 12233 | if (strcmp(args[cur_arg], "if") == 0 || |
| 12234 | strcmp(args[cur_arg], "unless") == 0) |
| 12235 | break; |
| 12236 | |
| 12237 | if (cur_arg < *orig_arg + 3) { |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12238 | memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]"); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12239 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12240 | } |
| 12241 | |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12242 | cur_arg = *orig_arg; |
| 12243 | expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args); |
| 12244 | if (!expr) |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12245 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12246 | |
| 12247 | if (!(expr->fetch->val & SMP_VAL_FE_HRQ_HDR)) { |
| 12248 | memprintf(err, |
| 12249 | "fetch method '%s' extracts information from '%s', none of which is available here", |
| 12250 | args[cur_arg-1], sample_src_names(expr->fetch->use)); |
| 12251 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12252 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12253 | } |
| 12254 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12255 | if (!args[cur_arg] || !*args[cur_arg]) { |
| 12256 | memprintf(err, "expects 'len or 'id'"); |
| 12257 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12258 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12259 | } |
| 12260 | |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12261 | if (strcmp(args[cur_arg], "len") == 0) { |
| 12262 | cur_arg++; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12263 | |
| 12264 | if (!(px->cap & PR_CAP_FE)) { |
| 12265 | memprintf(err, "proxy '%s' has no frontend capability", px->id); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12266 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12267 | } |
| 12268 | |
Olivier Houchard | fbc74e8 | 2017-11-24 16:54:05 +0100 | [diff] [blame] | 12269 | px->conf.args.ctx = ARGC_CAP; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12270 | |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12271 | if (!args[cur_arg]) { |
| 12272 | memprintf(err, "missing length value"); |
| 12273 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12274 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12275 | } |
| 12276 | /* we copy the table name for now, it will be resolved later */ |
| 12277 | len = atoi(args[cur_arg]); |
| 12278 | if (len <= 0) { |
| 12279 | memprintf(err, "length must be > 0"); |
| 12280 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12281 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12282 | } |
| 12283 | cur_arg++; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12284 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12285 | if (!len) { |
| 12286 | memprintf(err, "a positive 'len' argument is mandatory"); |
| 12287 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12288 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12289 | } |
| 12290 | |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 12291 | hdr = calloc(1, sizeof(*hdr)); |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12292 | hdr->next = px->req_cap; |
| 12293 | hdr->name = NULL; /* not a header capture */ |
| 12294 | hdr->namelen = 0; |
| 12295 | hdr->len = len; |
| 12296 | hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED); |
| 12297 | hdr->index = px->nb_req_cap++; |
| 12298 | |
| 12299 | px->req_cap = hdr; |
| 12300 | px->to_log |= LW_REQHDR; |
| 12301 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 12302 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12303 | rule->action_ptr = http_action_req_capture; |
Thierry FOURNIER | 32b1500 | 2015-07-31 08:56:16 +0200 | [diff] [blame] | 12304 | rule->arg.cap.expr = expr; |
| 12305 | rule->arg.cap.hdr = hdr; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12306 | } |
| 12307 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12308 | else if (strcmp(args[cur_arg], "id") == 0) { |
| 12309 | int id; |
| 12310 | char *error; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12311 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12312 | cur_arg++; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12313 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12314 | if (!args[cur_arg]) { |
| 12315 | memprintf(err, "missing id value"); |
| 12316 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12317 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12318 | } |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12319 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12320 | id = strtol(args[cur_arg], &error, 10); |
| 12321 | if (*error != '\0') { |
| 12322 | memprintf(err, "cannot parse id '%s'", args[cur_arg]); |
| 12323 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12324 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12325 | } |
| 12326 | cur_arg++; |
| 12327 | |
Olivier Houchard | fbc74e8 | 2017-11-24 16:54:05 +0100 | [diff] [blame] | 12328 | px->conf.args.ctx = ARGC_CAP; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12329 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 12330 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12331 | rule->action_ptr = http_action_req_capture_by_id; |
Christopher Faulet | 29730ba | 2017-09-18 15:26:32 +0200 | [diff] [blame] | 12332 | rule->check_ptr = check_http_req_capture; |
Thierry FOURNIER | e209797 | 2015-07-31 08:56:35 +0200 | [diff] [blame] | 12333 | rule->arg.capid.expr = expr; |
| 12334 | rule->arg.capid.idx = id; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12335 | } |
| 12336 | |
| 12337 | else { |
| 12338 | memprintf(err, "expects 'len' or 'id', found '%s'", args[cur_arg]); |
| 12339 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12340 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12341 | } |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12342 | |
| 12343 | *orig_arg = cur_arg; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12344 | return ACT_RET_PRS_OK; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12345 | } |
| 12346 | |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12347 | /* This function executes the "capture" action and store the result in a |
| 12348 | * capture slot if exists. It executes a fetch expression, turns the result |
| 12349 | * into a string and puts it in a capture slot. It always returns 1. If an |
| 12350 | * error occurs the action is cancelled, but the rule processing continues. |
| 12351 | */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12352 | 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] | 12353 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12354 | { |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12355 | struct sample *key; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12356 | struct cap_hdr *h; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12357 | char **cap = s->res_cap; |
| 12358 | struct proxy *fe = strm_fe(s); |
| 12359 | int len; |
| 12360 | int i; |
| 12361 | |
| 12362 | /* Look for the original configuration. */ |
| 12363 | for (h = fe->rsp_cap, i = fe->nb_rsp_cap - 1; |
Thierry FOURNIER | e209797 | 2015-07-31 08:56:35 +0200 | [diff] [blame] | 12364 | h != NULL && i != rule->arg.capid.idx ; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12365 | i--, h = h->next); |
| 12366 | if (!h) |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12367 | return ACT_RET_CONT; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12368 | |
Thierry FOURNIER | e209797 | 2015-07-31 08:56:35 +0200 | [diff] [blame] | 12369 | 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] | 12370 | if (!key) |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12371 | return ACT_RET_CONT; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12372 | |
| 12373 | if (cap[h->index] == NULL) |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 12374 | cap[h->index] = pool_alloc(h->pool); |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12375 | |
| 12376 | if (cap[h->index] == NULL) /* no more capture memory */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12377 | return ACT_RET_CONT; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12378 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12379 | len = key->data.u.str.len; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12380 | if (len > h->len) |
| 12381 | len = h->len; |
| 12382 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12383 | memcpy(cap[h->index], key->data.u.str.str, len); |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12384 | cap[h->index][len] = 0; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12385 | return ACT_RET_CONT; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12386 | } |
| 12387 | |
Christopher Faulet | 29730ba | 2017-09-18 15:26:32 +0200 | [diff] [blame] | 12388 | /* Check an "http-response capture" action. |
| 12389 | * |
| 12390 | * The function returns 1 in success case, otherwise, it returns 0 and err is |
| 12391 | * filled. |
| 12392 | */ |
| 12393 | int check_http_res_capture(struct act_rule *rule, struct proxy *px, char **err) |
| 12394 | { |
Christopher Faulet | fd608dd | 2017-12-04 09:45:15 +0100 | [diff] [blame] | 12395 | if (rule->action_ptr != http_action_res_capture_by_id) |
| 12396 | return 1; |
| 12397 | |
Christopher Faulet | 29730ba | 2017-09-18 15:26:32 +0200 | [diff] [blame] | 12398 | if (rule->arg.capid.idx >= px->nb_rsp_cap) { |
| 12399 | memprintf(err, "unable to find capture id '%d' referenced by http-response capture rule", |
| 12400 | rule->arg.capid.idx); |
| 12401 | return 0; |
| 12402 | } |
| 12403 | |
| 12404 | return 1; |
| 12405 | } |
| 12406 | |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12407 | /* parse an "http-response capture" action. It takes a single argument which is |
| 12408 | * a sample fetch expression. It stores the expression into arg->act.p[0] and |
| 12409 | * 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] | 12410 | * 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] | 12411 | */ |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12412 | enum act_parse_ret parse_http_res_capture(const char **args, int *orig_arg, struct proxy *px, |
| 12413 | struct act_rule *rule, char **err) |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12414 | { |
| 12415 | struct sample_expr *expr; |
| 12416 | int cur_arg; |
| 12417 | int id; |
| 12418 | char *error; |
| 12419 | |
| 12420 | for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++) |
| 12421 | if (strcmp(args[cur_arg], "if") == 0 || |
| 12422 | strcmp(args[cur_arg], "unless") == 0) |
| 12423 | break; |
| 12424 | |
| 12425 | if (cur_arg < *orig_arg + 3) { |
Willy Tarreau | 29bdb1c | 2016-06-24 15:36:34 +0200 | [diff] [blame] | 12426 | memprintf(err, "expects <expression> id <idx>"); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12427 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12428 | } |
| 12429 | |
| 12430 | cur_arg = *orig_arg; |
| 12431 | expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args); |
| 12432 | if (!expr) |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12433 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12434 | |
| 12435 | if (!(expr->fetch->val & SMP_VAL_FE_HRS_HDR)) { |
| 12436 | memprintf(err, |
| 12437 | "fetch method '%s' extracts information from '%s', none of which is available here", |
| 12438 | args[cur_arg-1], sample_src_names(expr->fetch->use)); |
| 12439 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12440 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12441 | } |
| 12442 | |
| 12443 | if (!args[cur_arg] || !*args[cur_arg]) { |
Willy Tarreau | 29bdb1c | 2016-06-24 15:36:34 +0200 | [diff] [blame] | 12444 | memprintf(err, "expects 'id'"); |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12445 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12446 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12447 | } |
| 12448 | |
| 12449 | if (strcmp(args[cur_arg], "id") != 0) { |
| 12450 | memprintf(err, "expects 'id', found '%s'", args[cur_arg]); |
| 12451 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12452 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12453 | } |
| 12454 | |
| 12455 | cur_arg++; |
| 12456 | |
| 12457 | if (!args[cur_arg]) { |
| 12458 | memprintf(err, "missing id value"); |
| 12459 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12460 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12461 | } |
| 12462 | |
| 12463 | id = strtol(args[cur_arg], &error, 10); |
| 12464 | if (*error != '\0') { |
| 12465 | memprintf(err, "cannot parse id '%s'", args[cur_arg]); |
| 12466 | free(expr); |
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 | cur_arg++; |
| 12470 | |
Olivier Houchard | fbc74e8 | 2017-11-24 16:54:05 +0100 | [diff] [blame] | 12471 | px->conf.args.ctx = ARGC_CAP; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12472 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 12473 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12474 | rule->action_ptr = http_action_res_capture_by_id; |
Christopher Faulet | 29730ba | 2017-09-18 15:26:32 +0200 | [diff] [blame] | 12475 | rule->check_ptr = check_http_res_capture; |
Thierry FOURNIER | e209797 | 2015-07-31 08:56:35 +0200 | [diff] [blame] | 12476 | rule->arg.capid.expr = expr; |
| 12477 | rule->arg.capid.idx = id; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12478 | |
| 12479 | *orig_arg = cur_arg; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12480 | return ACT_RET_PRS_OK; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12481 | } |
| 12482 | |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 12483 | /* |
| 12484 | * Return the struct http_req_action_kw associated to a keyword. |
| 12485 | */ |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 12486 | struct action_kw *action_http_req_custom(const char *kw) |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 12487 | { |
Thierry FOURNIER | 322a124 | 2015-08-19 09:07:47 +0200 | [diff] [blame] | 12488 | return action_lookup(&http_req_keywords.list, kw); |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 12489 | } |
| 12490 | |
| 12491 | /* |
| 12492 | * Return the struct http_res_action_kw associated to a keyword. |
| 12493 | */ |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 12494 | struct action_kw *action_http_res_custom(const char *kw) |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 12495 | { |
Thierry FOURNIER | 322a124 | 2015-08-19 09:07:47 +0200 | [diff] [blame] | 12496 | return action_lookup(&http_res_keywords.list, kw); |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 12497 | } |
| 12498 | |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12499 | |
| 12500 | /* "show errors" handler for the CLI. Returns 0 if wants to continue, 1 to stop |
| 12501 | * now. |
| 12502 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 12503 | 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] | 12504 | { |
| 12505 | if (!cli_has_level(appctx, ACCESS_LVL_OPER)) |
| 12506 | return 1; |
| 12507 | |
Willy Tarreau | 234ba2d | 2016-11-25 08:39:10 +0100 | [diff] [blame] | 12508 | if (*args[2]) { |
| 12509 | struct proxy *px; |
| 12510 | |
| 12511 | px = proxy_find_by_name(args[2], 0, 0); |
| 12512 | if (px) |
| 12513 | appctx->ctx.errors.iid = px->uuid; |
| 12514 | else |
| 12515 | appctx->ctx.errors.iid = atoi(args[2]); |
| 12516 | |
| 12517 | if (!appctx->ctx.errors.iid) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 12518 | appctx->ctx.cli.severity = LOG_ERR; |
Willy Tarreau | 234ba2d | 2016-11-25 08:39:10 +0100 | [diff] [blame] | 12519 | appctx->ctx.cli.msg = "No such proxy.\n"; |
| 12520 | appctx->st0 = CLI_ST_PRINT; |
| 12521 | return 1; |
| 12522 | } |
| 12523 | } |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12524 | else |
Willy Tarreau | 234ba2d | 2016-11-25 08:39:10 +0100 | [diff] [blame] | 12525 | appctx->ctx.errors.iid = -1; // dump all proxies |
| 12526 | |
Willy Tarreau | 35069f8 | 2016-11-25 09:16:37 +0100 | [diff] [blame] | 12527 | appctx->ctx.errors.flag = 0; |
| 12528 | if (strcmp(args[3], "request") == 0) |
| 12529 | appctx->ctx.errors.flag |= 4; // ignore response |
| 12530 | else if (strcmp(args[3], "response") == 0) |
| 12531 | appctx->ctx.errors.flag |= 2; // ignore request |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12532 | appctx->ctx.errors.px = NULL; |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12533 | return 0; |
| 12534 | } |
| 12535 | |
| 12536 | /* This function dumps all captured errors onto the stream interface's |
| 12537 | * read buffer. It returns 0 if the output buffer is full and it needs |
| 12538 | * to be called again, otherwise non-zero. |
| 12539 | */ |
| 12540 | static int cli_io_handler_show_errors(struct appctx *appctx) |
| 12541 | { |
| 12542 | struct stream_interface *si = appctx->owner; |
| 12543 | extern const char *monthname[12]; |
| 12544 | |
| 12545 | if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) |
| 12546 | return 1; |
| 12547 | |
| 12548 | chunk_reset(&trash); |
| 12549 | |
| 12550 | if (!appctx->ctx.errors.px) { |
| 12551 | /* the function had not been called yet, let's prepare the |
| 12552 | * buffer for a response. |
| 12553 | */ |
| 12554 | struct tm tm; |
| 12555 | |
| 12556 | get_localtime(date.tv_sec, &tm); |
| 12557 | chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n", |
| 12558 | tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900, |
| 12559 | tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000), |
| 12560 | error_snapshot_id); |
| 12561 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 12562 | if (ci_putchk(si_ic(si), &trash) == -1) { |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12563 | /* Socket buffer full. Let's try again later from the same point */ |
| 12564 | si_applet_cant_put(si); |
| 12565 | return 0; |
| 12566 | } |
| 12567 | |
Olivier Houchard | fbc74e8 | 2017-11-24 16:54:05 +0100 | [diff] [blame] | 12568 | appctx->ctx.errors.px = proxies_list; |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12569 | appctx->ctx.errors.bol = 0; |
| 12570 | appctx->ctx.errors.ptr = -1; |
| 12571 | } |
| 12572 | |
| 12573 | /* we have two inner loops here, one for the proxy, the other one for |
| 12574 | * the buffer. |
| 12575 | */ |
| 12576 | while (appctx->ctx.errors.px) { |
| 12577 | struct error_snapshot *es; |
| 12578 | |
Willy Tarreau | 35069f8 | 2016-11-25 09:16:37 +0100 | [diff] [blame] | 12579 | if ((appctx->ctx.errors.flag & 1) == 0) { |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12580 | es = &appctx->ctx.errors.px->invalid_req; |
Willy Tarreau | 35069f8 | 2016-11-25 09:16:37 +0100 | [diff] [blame] | 12581 | if (appctx->ctx.errors.flag & 2) // skip req |
| 12582 | goto next; |
| 12583 | } |
| 12584 | else { |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12585 | es = &appctx->ctx.errors.px->invalid_rep; |
Willy Tarreau | 35069f8 | 2016-11-25 09:16:37 +0100 | [diff] [blame] | 12586 | if (appctx->ctx.errors.flag & 4) // skip resp |
| 12587 | goto next; |
| 12588 | } |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12589 | |
| 12590 | if (!es->when.tv_sec) |
| 12591 | goto next; |
| 12592 | |
| 12593 | if (appctx->ctx.errors.iid >= 0 && |
| 12594 | appctx->ctx.errors.px->uuid != appctx->ctx.errors.iid && |
| 12595 | es->oe->uuid != appctx->ctx.errors.iid) |
| 12596 | goto next; |
| 12597 | |
| 12598 | if (appctx->ctx.errors.ptr < 0) { |
| 12599 | /* just print headers now */ |
| 12600 | |
| 12601 | char pn[INET6_ADDRSTRLEN]; |
| 12602 | struct tm tm; |
| 12603 | int port; |
| 12604 | |
| 12605 | get_localtime(es->when.tv_sec, &tm); |
| 12606 | chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]", |
| 12607 | tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900, |
| 12608 | tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000)); |
| 12609 | |
| 12610 | switch (addr_to_str(&es->src, pn, sizeof(pn))) { |
| 12611 | case AF_INET: |
| 12612 | case AF_INET6: |
| 12613 | port = get_host_port(&es->src); |
| 12614 | break; |
| 12615 | default: |
| 12616 | port = 0; |
| 12617 | } |
| 12618 | |
Willy Tarreau | 35069f8 | 2016-11-25 09:16:37 +0100 | [diff] [blame] | 12619 | switch (appctx->ctx.errors.flag & 1) { |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12620 | case 0: |
| 12621 | chunk_appendf(&trash, |
| 12622 | " frontend %s (#%d): invalid request\n" |
| 12623 | " backend %s (#%d)", |
| 12624 | appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid, |
| 12625 | (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>", |
| 12626 | (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1); |
| 12627 | break; |
| 12628 | case 1: |
| 12629 | chunk_appendf(&trash, |
| 12630 | " backend %s (#%d): invalid response\n" |
| 12631 | " frontend %s (#%d)", |
| 12632 | appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid, |
| 12633 | es->oe->id, es->oe->uuid); |
| 12634 | break; |
| 12635 | } |
| 12636 | |
| 12637 | chunk_appendf(&trash, |
| 12638 | ", server %s (#%d), event #%u\n" |
| 12639 | " src %s:%d, session #%d, session flags 0x%08x\n" |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 12640 | " 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] | 12641 | " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n" |
| 12642 | " buffer flags 0x%08x, out %d bytes, total %lld bytes\n" |
| 12643 | " pending %d bytes, wrapping at %d, error at position %d:\n \n", |
| 12644 | es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1, |
| 12645 | es->ev_id, |
| 12646 | pn, port, es->sid, es->s_flags, |
Willy Tarreau | 0da5b3b | 2017-09-21 09:30:46 +0200 | [diff] [blame] | 12647 | 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] | 12648 | es->m_clen, es->m_blen, |
| 12649 | es->b_flags, es->b_out, es->b_tot, |
| 12650 | es->len, es->b_wrap, es->pos); |
| 12651 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 12652 | if (ci_putchk(si_ic(si), &trash) == -1) { |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12653 | /* Socket buffer full. Let's try again later from the same point */ |
| 12654 | si_applet_cant_put(si); |
| 12655 | return 0; |
| 12656 | } |
| 12657 | appctx->ctx.errors.ptr = 0; |
| 12658 | appctx->ctx.errors.sid = es->sid; |
| 12659 | } |
| 12660 | |
| 12661 | if (appctx->ctx.errors.sid != es->sid) { |
| 12662 | /* the snapshot changed while we were dumping it */ |
| 12663 | chunk_appendf(&trash, |
| 12664 | " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n"); |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 12665 | if (ci_putchk(si_ic(si), &trash) == -1) { |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12666 | si_applet_cant_put(si); |
| 12667 | return 0; |
| 12668 | } |
| 12669 | goto next; |
| 12670 | } |
| 12671 | |
| 12672 | /* OK, ptr >= 0, so we have to dump the current line */ |
| 12673 | while (es->buf && appctx->ctx.errors.ptr < es->len && appctx->ctx.errors.ptr < global.tune.bufsize) { |
| 12674 | int newptr; |
| 12675 | int newline; |
| 12676 | |
| 12677 | newline = appctx->ctx.errors.bol; |
| 12678 | newptr = dump_text_line(&trash, es->buf, global.tune.bufsize, es->len, &newline, appctx->ctx.errors.ptr); |
| 12679 | if (newptr == appctx->ctx.errors.ptr) |
| 12680 | return 0; |
| 12681 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 12682 | if (ci_putchk(si_ic(si), &trash) == -1) { |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12683 | /* Socket buffer full. Let's try again later from the same point */ |
| 12684 | si_applet_cant_put(si); |
| 12685 | return 0; |
| 12686 | } |
| 12687 | appctx->ctx.errors.ptr = newptr; |
| 12688 | appctx->ctx.errors.bol = newline; |
| 12689 | }; |
| 12690 | next: |
| 12691 | appctx->ctx.errors.bol = 0; |
| 12692 | appctx->ctx.errors.ptr = -1; |
Willy Tarreau | 35069f8 | 2016-11-25 09:16:37 +0100 | [diff] [blame] | 12693 | appctx->ctx.errors.flag ^= 1; |
| 12694 | if (!(appctx->ctx.errors.flag & 1)) |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12695 | appctx->ctx.errors.px = appctx->ctx.errors.px->next; |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12696 | } |
| 12697 | |
| 12698 | /* dump complete */ |
| 12699 | return 1; |
| 12700 | } |
| 12701 | |
| 12702 | /* register cli keywords */ |
| 12703 | static struct cli_kw_list cli_kws = {{ },{ |
| 12704 | { { "show", "errors", NULL }, |
| 12705 | "show errors : report last request and response errors for each proxy", |
| 12706 | cli_parse_show_errors, cli_io_handler_show_errors, NULL, |
| 12707 | }, |
| 12708 | {{},} |
| 12709 | }}; |
| 12710 | |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 12711 | /************************************************************************/ |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12712 | /* All supported ACL keywords must be declared here. */ |
| 12713 | /************************************************************************/ |
| 12714 | |
| 12715 | /* Note: must not be declared <const> as its list will be overwritten. |
| 12716 | * Please take care of keeping this list alphabetically sorted. |
| 12717 | */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 12718 | static struct acl_kw_list acl_kws = {ILH, { |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12719 | { "base", "base", PAT_MATCH_STR }, |
| 12720 | { "base_beg", "base", PAT_MATCH_BEG }, |
| 12721 | { "base_dir", "base", PAT_MATCH_DIR }, |
| 12722 | { "base_dom", "base", PAT_MATCH_DOM }, |
| 12723 | { "base_end", "base", PAT_MATCH_END }, |
| 12724 | { "base_len", "base", PAT_MATCH_LEN }, |
| 12725 | { "base_reg", "base", PAT_MATCH_REG }, |
| 12726 | { "base_sub", "base", PAT_MATCH_SUB }, |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 12727 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12728 | { "cook", "req.cook", PAT_MATCH_STR }, |
| 12729 | { "cook_beg", "req.cook", PAT_MATCH_BEG }, |
| 12730 | { "cook_dir", "req.cook", PAT_MATCH_DIR }, |
| 12731 | { "cook_dom", "req.cook", PAT_MATCH_DOM }, |
| 12732 | { "cook_end", "req.cook", PAT_MATCH_END }, |
| 12733 | { "cook_len", "req.cook", PAT_MATCH_LEN }, |
| 12734 | { "cook_reg", "req.cook", PAT_MATCH_REG }, |
| 12735 | { "cook_sub", "req.cook", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12736 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12737 | { "hdr", "req.hdr", PAT_MATCH_STR }, |
| 12738 | { "hdr_beg", "req.hdr", PAT_MATCH_BEG }, |
| 12739 | { "hdr_dir", "req.hdr", PAT_MATCH_DIR }, |
| 12740 | { "hdr_dom", "req.hdr", PAT_MATCH_DOM }, |
| 12741 | { "hdr_end", "req.hdr", PAT_MATCH_END }, |
| 12742 | { "hdr_len", "req.hdr", PAT_MATCH_LEN }, |
| 12743 | { "hdr_reg", "req.hdr", PAT_MATCH_REG }, |
| 12744 | { "hdr_sub", "req.hdr", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12745 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12746 | /* these two declarations uses strings with list storage (in place |
| 12747 | * of tree storage). The basic match is PAT_MATCH_STR, but the indexation |
| 12748 | * and delete functions are relative to the list management. The parse |
| 12749 | * and match method are related to the corresponding fetch methods. This |
| 12750 | * is very particular ACL declaration mode. |
| 12751 | */ |
| 12752 | { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth }, |
| 12753 | { "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] | 12754 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12755 | { "path", "path", PAT_MATCH_STR }, |
| 12756 | { "path_beg", "path", PAT_MATCH_BEG }, |
| 12757 | { "path_dir", "path", PAT_MATCH_DIR }, |
| 12758 | { "path_dom", "path", PAT_MATCH_DOM }, |
| 12759 | { "path_end", "path", PAT_MATCH_END }, |
| 12760 | { "path_len", "path", PAT_MATCH_LEN }, |
| 12761 | { "path_reg", "path", PAT_MATCH_REG }, |
| 12762 | { "path_sub", "path", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12763 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12764 | { "req_ver", "req.ver", PAT_MATCH_STR }, |
| 12765 | { "resp_ver", "res.ver", PAT_MATCH_STR }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12766 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12767 | { "scook", "res.cook", PAT_MATCH_STR }, |
| 12768 | { "scook_beg", "res.cook", PAT_MATCH_BEG }, |
| 12769 | { "scook_dir", "res.cook", PAT_MATCH_DIR }, |
| 12770 | { "scook_dom", "res.cook", PAT_MATCH_DOM }, |
| 12771 | { "scook_end", "res.cook", PAT_MATCH_END }, |
| 12772 | { "scook_len", "res.cook", PAT_MATCH_LEN }, |
| 12773 | { "scook_reg", "res.cook", PAT_MATCH_REG }, |
| 12774 | { "scook_sub", "res.cook", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12775 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12776 | { "shdr", "res.hdr", PAT_MATCH_STR }, |
| 12777 | { "shdr_beg", "res.hdr", PAT_MATCH_BEG }, |
| 12778 | { "shdr_dir", "res.hdr", PAT_MATCH_DIR }, |
| 12779 | { "shdr_dom", "res.hdr", PAT_MATCH_DOM }, |
| 12780 | { "shdr_end", "res.hdr", PAT_MATCH_END }, |
| 12781 | { "shdr_len", "res.hdr", PAT_MATCH_LEN }, |
| 12782 | { "shdr_reg", "res.hdr", PAT_MATCH_REG }, |
| 12783 | { "shdr_sub", "res.hdr", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12784 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12785 | { "url", "url", PAT_MATCH_STR }, |
| 12786 | { "url_beg", "url", PAT_MATCH_BEG }, |
| 12787 | { "url_dir", "url", PAT_MATCH_DIR }, |
| 12788 | { "url_dom", "url", PAT_MATCH_DOM }, |
| 12789 | { "url_end", "url", PAT_MATCH_END }, |
| 12790 | { "url_len", "url", PAT_MATCH_LEN }, |
| 12791 | { "url_reg", "url", PAT_MATCH_REG }, |
| 12792 | { "url_sub", "url", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12793 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12794 | { "urlp", "urlp", PAT_MATCH_STR }, |
| 12795 | { "urlp_beg", "urlp", PAT_MATCH_BEG }, |
| 12796 | { "urlp_dir", "urlp", PAT_MATCH_DIR }, |
| 12797 | { "urlp_dom", "urlp", PAT_MATCH_DOM }, |
| 12798 | { "urlp_end", "urlp", PAT_MATCH_END }, |
| 12799 | { "urlp_len", "urlp", PAT_MATCH_LEN }, |
| 12800 | { "urlp_reg", "urlp", PAT_MATCH_REG }, |
| 12801 | { "urlp_sub", "urlp", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12802 | |
Willy Tarreau | 8ed669b | 2013-01-11 15:49:37 +0100 | [diff] [blame] | 12803 | { /* END */ }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12804 | }}; |
| 12805 | |
| 12806 | /************************************************************************/ |
| 12807 | /* All supported pattern keywords must be declared here. */ |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 12808 | /************************************************************************/ |
| 12809 | /* Note: must not be declared <const> as its list will be overwritten */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 12810 | static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, { |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12811 | { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 12812 | { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 12813 | { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV }, |
| 12814 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 12815 | /* capture are allocated and are permanent in the stream */ |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 12816 | { "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] | 12817 | |
| 12818 | /* retrieve these captures from the HTTP logs */ |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 12819 | { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP }, |
| 12820 | { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP }, |
| 12821 | { "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] | 12822 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 12823 | { "capture.res.hdr", smp_fetch_capture_header_res, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP }, |
| 12824 | { "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] | 12825 | |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 12826 | /* cookie is valid in both directions (eg: for "stick ...") but cook* |
| 12827 | * are only here to match the ACL's name, are request-only and are used |
| 12828 | * for ACL compatibility only. |
| 12829 | */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12830 | { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 12831 | { "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] | 12832 | { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 12833 | { "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] | 12834 | |
| 12835 | /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are |
| 12836 | * only here to match the ACL's name, are request-only and are used for |
| 12837 | * ACL compatibility only. |
| 12838 | */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12839 | { "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] | 12840 | { "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] | 12841 | { "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] | 12842 | { "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] | 12843 | |
Willy Tarreau | 0a0daec | 2013-04-02 22:44:58 +0200 | [diff] [blame] | 12844 | { "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] | 12845 | { "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] | 12846 | { "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] | 12847 | { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP }, |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12848 | { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 12849 | { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 12850 | |
| 12851 | /* HTTP protocol on the request path */ |
| 12852 | { "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] | 12853 | { "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] | 12854 | |
| 12855 | /* HTTP version on the request path */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12856 | { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 12857 | { "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] | 12858 | |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 12859 | { "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] | 12860 | { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 12861 | { "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] | 12862 | { "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] | 12863 | |
Thierry FOURNIER | d7d8881 | 2017-04-19 15:15:14 +0200 | [diff] [blame] | 12864 | { "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] | 12865 | { "req.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV }, |
| 12866 | |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 12867 | /* HTTP version on the response path */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12868 | { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV }, |
| 12869 | { "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] | 12870 | |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 12871 | /* 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] | 12872 | { "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] | 12873 | { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 12874 | { "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] | 12875 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12876 | { "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] | 12877 | { "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] | 12878 | { "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] | 12879 | { "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] | 12880 | { "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] | 12881 | { "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] | 12882 | { "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] | 12883 | |
| 12884 | /* 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] | 12885 | { "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] | 12886 | { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV }, |
| 12887 | { "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] | 12888 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12889 | { "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] | 12890 | { "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] | 12891 | { "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] | 12892 | { "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] | 12893 | { "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] | 12894 | { "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] | 12895 | { "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] | 12896 | |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 12897 | /* 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] | 12898 | { "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] | 12899 | { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV }, |
| 12900 | { "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] | 12901 | { "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] | 12902 | |
| 12903 | /* 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] | 12904 | { "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] | 12905 | { "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] | 12906 | { "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] | 12907 | { "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] | 12908 | |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 12909 | { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP }, |
Thierry Fournier | 0e00dca | 2016-04-07 15:47:40 +0200 | [diff] [blame] | 12910 | { "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] | 12911 | { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 12912 | { "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] | 12913 | { "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] | 12914 | { "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] | 12915 | { "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] | 12916 | { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 12917 | { "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] | 12918 | { "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] | 12919 | { /* END */ }, |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 12920 | }}; |
| 12921 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 12922 | |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12923 | /************************************************************************/ |
| 12924 | /* All supported converter keywords must be declared here. */ |
| 12925 | /************************************************************************/ |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 12926 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 12927 | static struct sample_conv_kw_list sample_conv_kws = {ILH, { |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 12928 | { "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] | 12929 | { "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] | 12930 | { "capture-req", smp_conv_req_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR}, |
| 12931 | { "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] | 12932 | { "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] | 12933 | { NULL, NULL, 0, 0, 0 }, |
| 12934 | }}; |
| 12935 | |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 12936 | |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12937 | /************************************************************************/ |
| 12938 | /* All supported http-request action keywords must be declared here. */ |
| 12939 | /************************************************************************/ |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 12940 | struct action_kw_list http_req_actions = { |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12941 | .kw = { |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12942 | { "capture", parse_http_req_capture }, |
Willy Tarreau | 53275e8 | 2017-11-24 07:52:01 +0100 | [diff] [blame] | 12943 | { "reject", parse_http_action_reject }, |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12944 | { "set-method", parse_set_req_line }, |
| 12945 | { "set-path", parse_set_req_line }, |
| 12946 | { "set-query", parse_set_req_line }, |
| 12947 | { "set-uri", parse_set_req_line }, |
Willy Tarreau | cb703b0 | 2015-04-03 09:52:01 +0200 | [diff] [blame] | 12948 | { NULL, NULL } |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12949 | } |
| 12950 | }; |
| 12951 | |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 12952 | struct action_kw_list http_res_actions = { |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12953 | .kw = { |
| 12954 | { "capture", parse_http_res_capture }, |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 12955 | { "set-status", parse_http_set_status }, |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12956 | { NULL, NULL } |
| 12957 | } |
| 12958 | }; |
| 12959 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 12960 | __attribute__((constructor)) |
| 12961 | static void __http_protocol_init(void) |
| 12962 | { |
| 12963 | acl_register_keywords(&acl_kws); |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 12964 | sample_register_fetches(&sample_fetch_keywords); |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 12965 | sample_register_convs(&sample_conv_kws); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12966 | http_req_keywords_register(&http_req_actions); |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12967 | http_res_keywords_register(&http_res_actions); |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12968 | cli_register_kw(&cli_kws); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 12969 | } |
| 12970 | |
| 12971 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 12972 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 12973 | * Local variables: |
| 12974 | * c-indent-level: 8 |
| 12975 | * c-basic-offset: 8 |
| 12976 | * End: |
| 12977 | */ |