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, |
Olivier Houchard | 51a76d8 | 2017-10-02 16:12:07 +0200 | [diff] [blame] | 145 | [HTTP_ERR_425] = 425, |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 146 | [HTTP_ERR_429] = 429, |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 147 | [HTTP_ERR_500] = 500, |
| 148 | [HTTP_ERR_502] = 502, |
| 149 | [HTTP_ERR_503] = 503, |
| 150 | [HTTP_ERR_504] = 504, |
| 151 | }; |
| 152 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 153 | static const char *http_err_msgs[HTTP_ERR_SIZE] = { |
Willy Tarreau | ae94d4d | 2011-05-11 16:28:49 +0200 | [diff] [blame] | 154 | [HTTP_ERR_200] = |
| 155 | "HTTP/1.0 200 OK\r\n" |
| 156 | "Cache-Control: no-cache\r\n" |
| 157 | "Connection: close\r\n" |
| 158 | "Content-Type: text/html\r\n" |
| 159 | "\r\n" |
| 160 | "<html><body><h1>200 OK</h1>\nService ready.\n</body></html>\n", |
| 161 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 162 | [HTTP_ERR_400] = |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 163 | "HTTP/1.0 400 Bad request\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 164 | "Cache-Control: no-cache\r\n" |
| 165 | "Connection: close\r\n" |
| 166 | "Content-Type: text/html\r\n" |
| 167 | "\r\n" |
| 168 | "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n", |
| 169 | |
| 170 | [HTTP_ERR_403] = |
| 171 | "HTTP/1.0 403 Forbidden\r\n" |
| 172 | "Cache-Control: no-cache\r\n" |
| 173 | "Connection: close\r\n" |
| 174 | "Content-Type: text/html\r\n" |
| 175 | "\r\n" |
| 176 | "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n", |
| 177 | |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 178 | [HTTP_ERR_405] = |
| 179 | "HTTP/1.0 405 Method Not Allowed\r\n" |
| 180 | "Cache-Control: no-cache\r\n" |
| 181 | "Connection: close\r\n" |
| 182 | "Content-Type: text/html\r\n" |
| 183 | "\r\n" |
| 184 | "<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", |
| 185 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 186 | [HTTP_ERR_408] = |
| 187 | "HTTP/1.0 408 Request Time-out\r\n" |
| 188 | "Cache-Control: no-cache\r\n" |
| 189 | "Connection: close\r\n" |
| 190 | "Content-Type: text/html\r\n" |
| 191 | "\r\n" |
| 192 | "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n", |
| 193 | |
Olivier Houchard | 51a76d8 | 2017-10-02 16:12:07 +0200 | [diff] [blame] | 194 | [HTTP_ERR_425] = |
| 195 | "HTTP/1.0 425 Too Early\r\n" |
| 196 | "Cache-Control: no-cache\r\n" |
| 197 | "Connection: close\r\n" |
| 198 | "Content-Type: text/html\r\n" |
| 199 | "\r\n" |
| 200 | "<html><body><h1>425 Too Early</h1>\nYour browser sent early data.\n</body></html>\n", |
| 201 | |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 202 | [HTTP_ERR_429] = |
| 203 | "HTTP/1.0 429 Too Many Requests\r\n" |
| 204 | "Cache-Control: no-cache\r\n" |
| 205 | "Connection: close\r\n" |
| 206 | "Content-Type: text/html\r\n" |
| 207 | "\r\n" |
| 208 | "<html><body><h1>429 Too Many Requests</h1>\nYou have sent too many requests in a given amount of time.\n</body></html>\n", |
| 209 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 210 | [HTTP_ERR_500] = |
Jarno Huuskonen | 16ad94a | 2017-01-09 14:17:10 +0200 | [diff] [blame] | 211 | "HTTP/1.0 500 Internal Server Error\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 212 | "Cache-Control: no-cache\r\n" |
| 213 | "Connection: close\r\n" |
| 214 | "Content-Type: text/html\r\n" |
| 215 | "\r\n" |
Jarno Huuskonen | 16ad94a | 2017-01-09 14:17:10 +0200 | [diff] [blame] | 216 | "<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] | 217 | |
| 218 | [HTTP_ERR_502] = |
| 219 | "HTTP/1.0 502 Bad Gateway\r\n" |
| 220 | "Cache-Control: no-cache\r\n" |
| 221 | "Connection: close\r\n" |
| 222 | "Content-Type: text/html\r\n" |
| 223 | "\r\n" |
| 224 | "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n", |
| 225 | |
| 226 | [HTTP_ERR_503] = |
| 227 | "HTTP/1.0 503 Service Unavailable\r\n" |
| 228 | "Cache-Control: no-cache\r\n" |
| 229 | "Connection: close\r\n" |
| 230 | "Content-Type: text/html\r\n" |
| 231 | "\r\n" |
| 232 | "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n", |
| 233 | |
| 234 | [HTTP_ERR_504] = |
| 235 | "HTTP/1.0 504 Gateway Time-out\r\n" |
| 236 | "Cache-Control: no-cache\r\n" |
| 237 | "Connection: close\r\n" |
| 238 | "Content-Type: text/html\r\n" |
| 239 | "\r\n" |
| 240 | "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n", |
| 241 | |
| 242 | }; |
| 243 | |
Cyril Bonté | 19979e1 | 2012-04-04 12:57:21 +0200 | [diff] [blame] | 244 | /* status codes available for the stats admin page (strictly 4 chars length) */ |
| 245 | const char *stat_status_codes[STAT_STATUS_SIZE] = { |
| 246 | [STAT_STATUS_DENY] = "DENY", |
| 247 | [STAT_STATUS_DONE] = "DONE", |
| 248 | [STAT_STATUS_ERRP] = "ERRP", |
| 249 | [STAT_STATUS_EXCD] = "EXCD", |
| 250 | [STAT_STATUS_NONE] = "NONE", |
| 251 | [STAT_STATUS_PART] = "PART", |
| 252 | [STAT_STATUS_UNKN] = "UNKN", |
| 253 | }; |
| 254 | |
| 255 | |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 256 | /* List head of all known action keywords for "http-request" */ |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 257 | struct action_kw_list http_req_keywords = { |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 258 | .list = LIST_HEAD_INIT(http_req_keywords.list) |
| 259 | }; |
| 260 | |
| 261 | /* List head of all known action keywords for "http-response" */ |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 262 | struct action_kw_list http_res_keywords = { |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 263 | .list = LIST_HEAD_INIT(http_res_keywords.list) |
| 264 | }; |
| 265 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 266 | /* We must put the messages here since GCC cannot initialize consts depending |
| 267 | * on strlen(). |
| 268 | */ |
| 269 | struct chunk http_err_chunks[HTTP_ERR_SIZE]; |
| 270 | |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 271 | /* 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] | 272 | static THREAD_LOCAL struct hdr_ctx static_hdr_ctx; |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 273 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 274 | #define FD_SETS_ARE_BITFIELDS |
| 275 | #ifdef FD_SETS_ARE_BITFIELDS |
| 276 | /* |
| 277 | * This map is used with all the FD_* macros to check whether a particular bit |
| 278 | * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes |
| 279 | * which should be encoded. When FD_ISSET() returns non-zero, it means that the |
| 280 | * byte should be encoded. Be careful to always pass bytes from 0 to 255 |
| 281 | * exclusively to the macros. |
| 282 | */ |
| 283 | fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| 284 | 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] | 285 | 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] | 286 | |
| 287 | #else |
| 288 | #error "Check if your OS uses bitfields for fd_sets" |
| 289 | #endif |
| 290 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 291 | 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] | 292 | |
David Carlier | 7365f7d | 2016-04-04 11:54:42 +0100 | [diff] [blame] | 293 | static inline int http_msg_forward_body(struct stream *s, struct http_msg *msg); |
| 294 | 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] | 295 | |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 296 | /* This function returns a reason associated with the HTTP status. |
| 297 | * This function never fails, a message is always returned. |
| 298 | */ |
| 299 | const char *get_reason(unsigned int status) |
| 300 | { |
| 301 | switch (status) { |
| 302 | case 100: return "Continue"; |
| 303 | case 101: return "Switching Protocols"; |
| 304 | case 102: return "Processing"; |
| 305 | case 200: return "OK"; |
| 306 | case 201: return "Created"; |
| 307 | case 202: return "Accepted"; |
| 308 | case 203: return "Non-Authoritative Information"; |
| 309 | case 204: return "No Content"; |
| 310 | case 205: return "Reset Content"; |
| 311 | case 206: return "Partial Content"; |
| 312 | case 207: return "Multi-Status"; |
| 313 | case 210: return "Content Different"; |
| 314 | case 226: return "IM Used"; |
| 315 | case 300: return "Multiple Choices"; |
| 316 | case 301: return "Moved Permanently"; |
| 317 | case 302: return "Moved Temporarily"; |
| 318 | case 303: return "See Other"; |
| 319 | case 304: return "Not Modified"; |
| 320 | case 305: return "Use Proxy"; |
| 321 | case 307: return "Temporary Redirect"; |
| 322 | case 308: return "Permanent Redirect"; |
| 323 | case 310: return "Too many Redirects"; |
| 324 | case 400: return "Bad Request"; |
| 325 | case 401: return "Unauthorized"; |
| 326 | case 402: return "Payment Required"; |
| 327 | case 403: return "Forbidden"; |
| 328 | case 404: return "Not Found"; |
| 329 | case 405: return "Method Not Allowed"; |
| 330 | case 406: return "Not Acceptable"; |
| 331 | case 407: return "Proxy Authentication Required"; |
| 332 | case 408: return "Request Time-out"; |
| 333 | case 409: return "Conflict"; |
| 334 | case 410: return "Gone"; |
| 335 | case 411: return "Length Required"; |
| 336 | case 412: return "Precondition Failed"; |
| 337 | case 413: return "Request Entity Too Large"; |
| 338 | case 414: return "Request-URI Too Long"; |
| 339 | case 415: return "Unsupported Media Type"; |
| 340 | case 416: return "Requested range unsatisfiable"; |
| 341 | case 417: return "Expectation failed"; |
| 342 | case 418: return "I'm a teapot"; |
| 343 | case 422: return "Unprocessable entity"; |
| 344 | case 423: return "Locked"; |
| 345 | case 424: return "Method failure"; |
Olivier Houchard | 51a76d8 | 2017-10-02 16:12:07 +0200 | [diff] [blame] | 346 | case 425: return "Too Early"; |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 347 | case 426: return "Upgrade Required"; |
| 348 | case 428: return "Precondition Required"; |
| 349 | case 429: return "Too Many Requests"; |
| 350 | case 431: return "Request Header Fields Too Large"; |
| 351 | case 449: return "Retry With"; |
| 352 | case 450: return "Blocked by Windows Parental Controls"; |
| 353 | case 451: return "Unavailable For Legal Reasons"; |
| 354 | case 456: return "Unrecoverable Error"; |
| 355 | case 499: return "client has closed connection"; |
| 356 | case 500: return "Internal Server Error"; |
| 357 | case 501: return "Not Implemented"; |
Jarno Huuskonen | 59af2df | 2016-12-28 10:49:01 +0200 | [diff] [blame] | 358 | case 502: return "Bad Gateway or Proxy Error"; |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 359 | case 503: return "Service Unavailable"; |
| 360 | case 504: return "Gateway Time-out"; |
| 361 | case 505: return "HTTP Version not supported"; |
| 362 | case 506: return "Variant also negociate"; |
| 363 | case 507: return "Insufficient storage"; |
| 364 | case 508: return "Loop detected"; |
| 365 | case 509: return "Bandwidth Limit Exceeded"; |
| 366 | case 510: return "Not extended"; |
| 367 | case 511: return "Network authentication required"; |
| 368 | case 520: return "Web server is returning an unknown error"; |
| 369 | default: |
| 370 | switch (status) { |
| 371 | case 100 ... 199: return "Informational"; |
| 372 | case 200 ... 299: return "Success"; |
| 373 | case 300 ... 399: return "Redirection"; |
| 374 | case 400 ... 499: return "Client Error"; |
| 375 | case 500 ... 599: return "Server Error"; |
| 376 | default: return "Other"; |
| 377 | } |
| 378 | } |
| 379 | } |
| 380 | |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 381 | /* This function returns HTTP_ERR_<num> (enum) matching http status code. |
| 382 | * Returned value should match codes from http_err_codes. |
| 383 | */ |
| 384 | static const int http_get_status_idx(unsigned int status) |
| 385 | { |
| 386 | switch (status) { |
| 387 | case 200: return HTTP_ERR_200; |
| 388 | case 400: return HTTP_ERR_400; |
| 389 | case 403: return HTTP_ERR_403; |
| 390 | case 405: return HTTP_ERR_405; |
| 391 | case 408: return HTTP_ERR_408; |
Olivier Houchard | 51a76d8 | 2017-10-02 16:12:07 +0200 | [diff] [blame] | 392 | case 425: return HTTP_ERR_425; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 393 | case 429: return HTTP_ERR_429; |
| 394 | case 500: return HTTP_ERR_500; |
| 395 | case 502: return HTTP_ERR_502; |
| 396 | case 503: return HTTP_ERR_503; |
| 397 | case 504: return HTTP_ERR_504; |
| 398 | default: return HTTP_ERR_500; |
| 399 | } |
| 400 | } |
| 401 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 402 | void init_proto_http() |
| 403 | { |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 404 | int i; |
| 405 | char *tmp; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 406 | int msg; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 407 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 408 | for (msg = 0; msg < HTTP_ERR_SIZE; msg++) { |
| 409 | if (!http_err_msgs[msg]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 410 | 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] | 411 | abort(); |
| 412 | } |
| 413 | |
| 414 | http_err_chunks[msg].str = (char *)http_err_msgs[msg]; |
| 415 | http_err_chunks[msg].len = strlen(http_err_msgs[msg]); |
| 416 | } |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 417 | |
| 418 | /* initialize the log header encoding map : '{|}"#' should be encoded with |
| 419 | * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ). |
| 420 | * URL encoding only requires '"', '#' to be encoded as well as non- |
| 421 | * printable characters above. |
| 422 | */ |
| 423 | memset(hdr_encode_map, 0, sizeof(hdr_encode_map)); |
| 424 | memset(url_encode_map, 0, sizeof(url_encode_map)); |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 425 | memset(http_encode_map, 0, sizeof(url_encode_map)); |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 426 | for (i = 0; i < 32; i++) { |
| 427 | FD_SET(i, hdr_encode_map); |
| 428 | FD_SET(i, url_encode_map); |
| 429 | } |
| 430 | for (i = 127; i < 256; i++) { |
| 431 | FD_SET(i, hdr_encode_map); |
| 432 | FD_SET(i, url_encode_map); |
| 433 | } |
| 434 | |
| 435 | tmp = "\"#{|}"; |
| 436 | while (*tmp) { |
| 437 | FD_SET(*tmp, hdr_encode_map); |
| 438 | tmp++; |
| 439 | } |
| 440 | |
| 441 | tmp = "\"#"; |
| 442 | while (*tmp) { |
| 443 | FD_SET(*tmp, url_encode_map); |
| 444 | tmp++; |
| 445 | } |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 446 | |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 447 | /* initialize the http header encoding map. The draft httpbis define the |
| 448 | * header content as: |
| 449 | * |
| 450 | * HTTP-message = start-line |
| 451 | * *( header-field CRLF ) |
| 452 | * CRLF |
| 453 | * [ message-body ] |
| 454 | * header-field = field-name ":" OWS field-value OWS |
| 455 | * field-value = *( field-content / obs-fold ) |
| 456 | * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ] |
| 457 | * obs-fold = CRLF 1*( SP / HTAB ) |
| 458 | * field-vchar = VCHAR / obs-text |
| 459 | * VCHAR = %x21-7E |
| 460 | * obs-text = %x80-FF |
| 461 | * |
| 462 | * All the chars are encoded except "VCHAR", "obs-text", SP and HTAB. |
| 463 | * The encoded chars are form 0x00 to 0x08, 0x0a to 0x1f and 0x7f. The |
| 464 | * "obs-fold" is volontary forgotten because haproxy remove this. |
| 465 | */ |
| 466 | memset(http_encode_map, 0, sizeof(http_encode_map)); |
| 467 | for (i = 0x00; i <= 0x08; i++) |
| 468 | FD_SET(i, http_encode_map); |
| 469 | for (i = 0x0a; i <= 0x1f; i++) |
| 470 | FD_SET(i, http_encode_map); |
| 471 | FD_SET(0x7f, http_encode_map); |
| 472 | |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 473 | /* memory allocations */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 474 | pool_head_http_txn = create_pool("http_txn", sizeof(struct http_txn), MEM_F_SHARED); |
| 475 | pool_head_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED); |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 476 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 477 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 478 | /* |
| 479 | * We have 26 list of methods (1 per first letter), each of which can have |
| 480 | * up to 3 entries (2 valid, 1 null). |
| 481 | */ |
| 482 | struct http_method_desc { |
Willy Tarreau | c8987b3 | 2013-12-06 23:43:17 +0100 | [diff] [blame] | 483 | enum http_meth_t meth; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 484 | int len; |
| 485 | const char text[8]; |
| 486 | }; |
| 487 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 488 | const struct http_method_desc http_methods[26][3] = { |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 489 | ['C' - 'A'] = { |
| 490 | [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" }, |
| 491 | }, |
| 492 | ['D' - 'A'] = { |
| 493 | [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" }, |
| 494 | }, |
| 495 | ['G' - 'A'] = { |
| 496 | [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" }, |
| 497 | }, |
| 498 | ['H' - 'A'] = { |
| 499 | [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" }, |
| 500 | }, |
Christopher Faulet | d57ad64 | 2015-07-31 14:26:57 +0200 | [diff] [blame] | 501 | ['O' - 'A'] = { |
| 502 | [0] = { .meth = HTTP_METH_OPTIONS , .len=7, .text="OPTIONS" }, |
| 503 | }, |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 504 | ['P' - 'A'] = { |
| 505 | [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" }, |
| 506 | [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" }, |
| 507 | }, |
| 508 | ['T' - 'A'] = { |
| 509 | [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" }, |
| 510 | }, |
| 511 | /* rest is empty like this : |
Willy Tarreau | b7ce424 | 2015-09-03 17:15:21 +0200 | [diff] [blame] | 512 | * [0] = { .meth = HTTP_METH_OTHER , .len=0, .text="" }, |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 513 | */ |
| 514 | }; |
| 515 | |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 516 | const struct http_method_name http_known_methods[HTTP_METH_OTHER] = { |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 517 | [HTTP_METH_OPTIONS] = { "OPTIONS", 7 }, |
| 518 | [HTTP_METH_GET] = { "GET", 3 }, |
| 519 | [HTTP_METH_HEAD] = { "HEAD", 4 }, |
| 520 | [HTTP_METH_POST] = { "POST", 4 }, |
| 521 | [HTTP_METH_PUT] = { "PUT", 3 }, |
| 522 | [HTTP_METH_DELETE] = { "DELETE", 6 }, |
| 523 | [HTTP_METH_TRACE] = { "TRACE", 5 }, |
| 524 | [HTTP_METH_CONNECT] = { "CONNECT", 7 }, |
| 525 | }; |
| 526 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 527 | /* |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 528 | * Adds a header and its CRLF at the tail of the message's buffer, just before |
| 529 | * 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] | 530 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 531 | * of headers is automatically adjusted. The number of bytes added is returned |
| 532 | * on success, otherwise <0 is returned indicating an error. |
| 533 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 534 | 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] | 535 | { |
| 536 | int bytes, len; |
| 537 | |
| 538 | len = strlen(text); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 539 | 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] | 540 | if (!bytes) |
| 541 | return -1; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 542 | http_msg_move_end(msg, bytes); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 543 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 544 | } |
| 545 | |
| 546 | /* |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 547 | * Adds a header and its CRLF at the tail of the message's buffer, just before |
| 548 | * 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] | 549 | * the buffer is only opened and the space reserved, but nothing is copied. |
| 550 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 551 | * of headers is automatically adjusted. The number of bytes added is returned |
| 552 | * on success, otherwise <0 is returned indicating an error. |
| 553 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 554 | int http_header_add_tail2(struct http_msg *msg, |
| 555 | struct hdr_idx *hdr_idx, const char *text, int len) |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 556 | { |
| 557 | int bytes; |
| 558 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 559 | 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] | 560 | if (!bytes) |
| 561 | return -1; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 562 | http_msg_move_end(msg, bytes); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 563 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 564 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 565 | |
| 566 | /* |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 567 | * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon. |
| 568 | * If so, returns the position of the first non-space character relative to |
| 569 | * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries |
| 570 | * to return a pointer to the place after the first space. Returns 0 if the |
| 571 | * header name does not match. Checks are case-insensitive. |
| 572 | */ |
| 573 | int http_header_match2(const char *hdr, const char *end, |
| 574 | const char *name, int len) |
| 575 | { |
| 576 | const char *val; |
| 577 | |
| 578 | if (hdr + len >= end) |
| 579 | return 0; |
| 580 | if (hdr[len] != ':') |
| 581 | return 0; |
| 582 | if (strncasecmp(hdr, name, len) != 0) |
| 583 | return 0; |
| 584 | val = hdr + len + 1; |
| 585 | while (val < end && HTTP_IS_SPHT(*val)) |
| 586 | val++; |
| 587 | if ((val >= end) && (len + 2 <= end - hdr)) |
| 588 | return len + 2; /* we may replace starting from second space */ |
| 589 | return val - hdr; |
| 590 | } |
| 591 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 592 | /* Find the first or next occurrence of header <name> in message buffer <sol> |
| 593 | * using headers index <idx>, and return it in the <ctx> structure. This |
| 594 | * structure holds everything necessary to use the header and find next |
| 595 | * occurrence. If its <idx> member is 0, the header is searched from the |
| 596 | * beginning. Otherwise, the next occurrence is returned. The function returns |
| 597 | * 1 when it finds a value, and 0 when there is no more. It is very similar to |
| 598 | * http_find_header2() except that it is designed to work with full-line headers |
| 599 | * whose comma is not a delimiter but is part of the syntax. As a special case, |
| 600 | * if ctx->val is NULL when searching for a new values of a header, the current |
| 601 | * header is rescanned. This allows rescanning after a header deletion. |
| 602 | */ |
| 603 | int http_find_full_header2(const char *name, int len, |
| 604 | char *sol, struct hdr_idx *idx, |
| 605 | struct hdr_ctx *ctx) |
| 606 | { |
| 607 | char *eol, *sov; |
| 608 | int cur_idx, old_idx; |
| 609 | |
| 610 | cur_idx = ctx->idx; |
| 611 | if (cur_idx) { |
| 612 | /* We have previously returned a header, let's search another one */ |
| 613 | sol = ctx->line; |
| 614 | eol = sol + idx->v[cur_idx].len; |
| 615 | goto next_hdr; |
| 616 | } |
| 617 | |
| 618 | /* first request for this header */ |
| 619 | sol += hdr_idx_first_pos(idx); |
| 620 | old_idx = 0; |
| 621 | cur_idx = hdr_idx_first_idx(idx); |
| 622 | while (cur_idx) { |
| 623 | eol = sol + idx->v[cur_idx].len; |
| 624 | |
| 625 | if (len == 0) { |
| 626 | /* No argument was passed, we want any header. |
| 627 | * To achieve this, we simply build a fake request. */ |
| 628 | while (sol + len < eol && sol[len] != ':') |
| 629 | len++; |
| 630 | name = sol; |
| 631 | } |
| 632 | |
| 633 | if ((len < eol - sol) && |
| 634 | (sol[len] == ':') && |
| 635 | (strncasecmp(sol, name, len) == 0)) { |
| 636 | ctx->del = len; |
| 637 | sov = sol + len + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 638 | while (sov < eol && HTTP_IS_LWS(*sov)) |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 639 | sov++; |
| 640 | |
| 641 | ctx->line = sol; |
| 642 | ctx->prev = old_idx; |
| 643 | ctx->idx = cur_idx; |
| 644 | ctx->val = sov - sol; |
| 645 | ctx->tws = 0; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 646 | while (eol > sov && HTTP_IS_LWS(*(eol - 1))) { |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 647 | eol--; |
| 648 | ctx->tws++; |
| 649 | } |
| 650 | ctx->vlen = eol - sov; |
| 651 | return 1; |
| 652 | } |
| 653 | next_hdr: |
| 654 | sol = eol + idx->v[cur_idx].cr + 1; |
| 655 | old_idx = cur_idx; |
| 656 | cur_idx = idx->v[cur_idx].next; |
| 657 | } |
| 658 | return 0; |
| 659 | } |
| 660 | |
Willy Tarreau | c90dc23 | 2015-02-20 13:51:36 +0100 | [diff] [blame] | 661 | /* Find the first or next header field in message buffer <sol> using headers |
| 662 | * index <idx>, and return it in the <ctx> structure. This structure holds |
| 663 | * everything necessary to use the header and find next occurrence. If its |
| 664 | * <idx> member is 0, the first header is retrieved. Otherwise, the next |
| 665 | * occurrence is returned. The function returns 1 when it finds a value, and |
| 666 | * 0 when there is no more. It is equivalent to http_find_full_header2() with |
| 667 | * no header name. |
| 668 | */ |
| 669 | int http_find_next_header(char *sol, struct hdr_idx *idx, struct hdr_ctx *ctx) |
| 670 | { |
| 671 | char *eol, *sov; |
| 672 | int cur_idx, old_idx; |
| 673 | int len; |
| 674 | |
| 675 | cur_idx = ctx->idx; |
| 676 | if (cur_idx) { |
| 677 | /* We have previously returned a header, let's search another one */ |
| 678 | sol = ctx->line; |
| 679 | eol = sol + idx->v[cur_idx].len; |
| 680 | goto next_hdr; |
| 681 | } |
| 682 | |
| 683 | /* first request for this header */ |
| 684 | sol += hdr_idx_first_pos(idx); |
| 685 | old_idx = 0; |
| 686 | cur_idx = hdr_idx_first_idx(idx); |
| 687 | while (cur_idx) { |
| 688 | eol = sol + idx->v[cur_idx].len; |
| 689 | |
| 690 | len = 0; |
| 691 | while (1) { |
| 692 | if (len >= eol - sol) |
| 693 | goto next_hdr; |
| 694 | if (sol[len] == ':') |
| 695 | break; |
| 696 | len++; |
| 697 | } |
| 698 | |
| 699 | ctx->del = len; |
| 700 | sov = sol + len + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 701 | while (sov < eol && HTTP_IS_LWS(*sov)) |
Willy Tarreau | c90dc23 | 2015-02-20 13:51:36 +0100 | [diff] [blame] | 702 | sov++; |
| 703 | |
| 704 | ctx->line = sol; |
| 705 | ctx->prev = old_idx; |
| 706 | ctx->idx = cur_idx; |
| 707 | ctx->val = sov - sol; |
| 708 | ctx->tws = 0; |
| 709 | |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 710 | while (eol > sov && HTTP_IS_LWS(*(eol - 1))) { |
Willy Tarreau | c90dc23 | 2015-02-20 13:51:36 +0100 | [diff] [blame] | 711 | eol--; |
| 712 | ctx->tws++; |
| 713 | } |
| 714 | ctx->vlen = eol - sov; |
| 715 | return 1; |
| 716 | |
| 717 | next_hdr: |
| 718 | sol = eol + idx->v[cur_idx].cr + 1; |
| 719 | old_idx = cur_idx; |
| 720 | cur_idx = idx->v[cur_idx].next; |
| 721 | } |
| 722 | return 0; |
| 723 | } |
| 724 | |
Lukas Tribus | 2395368 | 2017-04-28 13:24:30 +0000 | [diff] [blame] | 725 | /* Find the end of the header value contained between <s> and <e>. See RFC7230, |
| 726 | * 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] | 727 | * a valid result. This works for headers defined as comma-separated lists. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 728 | */ |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 729 | char *find_hdr_value_end(char *s, const char *e) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 730 | { |
| 731 | int quoted, qdpair; |
| 732 | |
| 733 | quoted = qdpair = 0; |
Willy Tarreau | e6d9c21 | 2016-11-05 18:23:38 +0100 | [diff] [blame] | 734 | |
| 735 | #if defined(__x86_64__) || \ |
| 736 | defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) || \ |
| 737 | defined(__ARM_ARCH_7A__) |
| 738 | /* speedup: skip everything not a comma nor a double quote */ |
| 739 | for (; s <= e - sizeof(int); s += sizeof(int)) { |
| 740 | unsigned int c = *(int *)s; // comma |
| 741 | unsigned int q = c; // quote |
| 742 | |
| 743 | c ^= 0x2c2c2c2c; // contains one zero on a comma |
| 744 | q ^= 0x22222222; // contains one zero on a quote |
| 745 | |
| 746 | c = (c - 0x01010101) & ~c; // contains 0x80 below a comma |
| 747 | q = (q - 0x01010101) & ~q; // contains 0x80 below a quote |
| 748 | |
| 749 | if ((c | q) & 0x80808080) |
| 750 | break; // found a comma or a quote |
| 751 | } |
| 752 | #endif |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 753 | for (; s < e; s++) { |
| 754 | if (qdpair) qdpair = 0; |
Willy Tarreau | 0f7f51f | 2010-08-30 11:06:34 +0200 | [diff] [blame] | 755 | else if (quoted) { |
| 756 | if (*s == '\\') qdpair = 1; |
| 757 | else if (*s == '"') quoted = 0; |
| 758 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 759 | else if (*s == '"') quoted = 1; |
| 760 | else if (*s == ',') return s; |
| 761 | } |
| 762 | return s; |
| 763 | } |
| 764 | |
| 765 | /* Find the first or next occurrence of header <name> in message buffer <sol> |
| 766 | * using headers index <idx>, and return it in the <ctx> structure. This |
| 767 | * structure holds everything necessary to use the header and find next |
| 768 | * occurrence. If its <idx> member is 0, the header is searched from the |
| 769 | * beginning. Otherwise, the next occurrence is returned. The function returns |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 770 | * 1 when it finds a value, and 0 when there is no more. It is designed to work |
| 771 | * with headers defined as comma-separated lists. As a special case, if ctx->val |
| 772 | * is NULL when searching for a new values of a header, the current header is |
| 773 | * rescanned. This allows rescanning after a header deletion. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 774 | */ |
| 775 | int http_find_header2(const char *name, int len, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 776 | char *sol, struct hdr_idx *idx, |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 777 | struct hdr_ctx *ctx) |
| 778 | { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 779 | char *eol, *sov; |
| 780 | int cur_idx, old_idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 781 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 782 | cur_idx = ctx->idx; |
| 783 | if (cur_idx) { |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 784 | /* We have previously returned a value, let's search |
| 785 | * another one on the same line. |
| 786 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 787 | sol = ctx->line; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 788 | ctx->del = ctx->val + ctx->vlen + ctx->tws; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 789 | sov = sol + ctx->del; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 790 | eol = sol + idx->v[cur_idx].len; |
| 791 | |
| 792 | if (sov >= eol) |
| 793 | /* no more values in this header */ |
| 794 | goto next_hdr; |
| 795 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 796 | /* values remaining for this header, skip the comma but save it |
| 797 | * for later use (eg: for header deletion). |
| 798 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 799 | sov++; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 800 | while (sov < eol && HTTP_IS_LWS((*sov))) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 801 | sov++; |
| 802 | |
| 803 | goto return_hdr; |
| 804 | } |
| 805 | |
| 806 | /* first request for this header */ |
| 807 | sol += hdr_idx_first_pos(idx); |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 808 | old_idx = 0; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 809 | cur_idx = hdr_idx_first_idx(idx); |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 810 | while (cur_idx) { |
| 811 | eol = sol + idx->v[cur_idx].len; |
| 812 | |
Willy Tarreau | 1ad7c6d | 2007-06-10 21:42:55 +0200 | [diff] [blame] | 813 | if (len == 0) { |
| 814 | /* No argument was passed, we want any header. |
| 815 | * To achieve this, we simply build a fake request. */ |
| 816 | while (sol + len < eol && sol[len] != ':') |
| 817 | len++; |
| 818 | name = sol; |
| 819 | } |
| 820 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 821 | if ((len < eol - sol) && |
| 822 | (sol[len] == ':') && |
| 823 | (strncasecmp(sol, name, len) == 0)) { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 824 | ctx->del = len; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 825 | sov = sol + len + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 826 | while (sov < eol && HTTP_IS_LWS(*sov)) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 827 | sov++; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 828 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 829 | ctx->line = sol; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 830 | ctx->prev = old_idx; |
| 831 | return_hdr: |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 832 | ctx->idx = cur_idx; |
| 833 | ctx->val = sov - sol; |
| 834 | |
| 835 | eol = find_hdr_value_end(sov, eol); |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 836 | ctx->tws = 0; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 837 | while (eol > sov && HTTP_IS_LWS(*(eol - 1))) { |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 838 | eol--; |
| 839 | ctx->tws++; |
| 840 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 841 | ctx->vlen = eol - sov; |
| 842 | return 1; |
| 843 | } |
| 844 | next_hdr: |
| 845 | sol = eol + idx->v[cur_idx].cr + 1; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 846 | old_idx = cur_idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 847 | cur_idx = idx->v[cur_idx].next; |
| 848 | } |
| 849 | return 0; |
| 850 | } |
| 851 | |
| 852 | int http_find_header(const char *name, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 853 | char *sol, struct hdr_idx *idx, |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 854 | struct hdr_ctx *ctx) |
| 855 | { |
| 856 | return http_find_header2(name, strlen(name), sol, idx, ctx); |
| 857 | } |
| 858 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 859 | /* Remove one value of a header. This only works on a <ctx> returned by one of |
| 860 | * the http_find_header functions. The value is removed, as well as surrounding |
| 861 | * 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] | 862 | * 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] | 863 | * message <msg>. The new index is returned. If it is zero, it means there is |
| 864 | * no more header, so any processing may stop. The ctx is always left in a form |
| 865 | * that can be handled by http_find_header2() to find next occurrence. |
| 866 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 867 | 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] | 868 | { |
| 869 | int cur_idx = ctx->idx; |
| 870 | char *sol = ctx->line; |
| 871 | struct hdr_idx_elem *hdr; |
| 872 | int delta, skip_comma; |
| 873 | |
| 874 | if (!cur_idx) |
| 875 | return 0; |
| 876 | |
| 877 | hdr = &idx->v[cur_idx]; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 878 | if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 879 | /* 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] | 880 | 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] | 881 | http_msg_move_end(msg, delta); |
| 882 | idx->used--; |
| 883 | hdr->len = 0; /* unused entry */ |
| 884 | idx->v[ctx->prev].next = idx->v[ctx->idx].next; |
Willy Tarreau | 5c4784f | 2011-02-12 13:07:35 +0100 | [diff] [blame] | 885 | if (idx->tail == ctx->idx) |
| 886 | idx->tail = ctx->prev; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 887 | ctx->idx = ctx->prev; /* walk back to the end of previous header */ |
Willy Tarreau | 7c1c217 | 2015-01-07 17:23:50 +0100 | [diff] [blame] | 888 | 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] | 889 | 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] | 890 | ctx->tws = ctx->vlen = 0; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 891 | return ctx->idx; |
| 892 | } |
| 893 | |
| 894 | /* 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] | 895 | * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the |
| 896 | * last entry of the list, we remove the last separator. |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 897 | */ |
| 898 | |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 899 | skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 900 | delta = buffer_replace2(msg->chn->buf, sol + ctx->del + skip_comma, |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 901 | sol + ctx->val + ctx->vlen + ctx->tws + skip_comma, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 902 | NULL, 0); |
| 903 | hdr->len += delta; |
| 904 | http_msg_move_end(msg, delta); |
| 905 | ctx->val = ctx->del; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 906 | ctx->tws = ctx->vlen = 0; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 907 | return ctx->idx; |
| 908 | } |
| 909 | |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 910 | /* This function handles a server error at the stream interface level. The |
| 911 | * 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] | 912 | * message is copied into the input buffer. |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 913 | * 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] | 914 | * in this buffer will be lost. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 915 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 916 | static void http_server_error(struct stream *s, struct stream_interface *si, |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 917 | int err, int finst, const struct chunk *msg) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 918 | { |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 919 | FLT_STRM_CB(s, flt_http_reply(s, s->txn->status, msg)); |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 920 | channel_auto_read(si_oc(si)); |
| 921 | channel_abort(si_oc(si)); |
| 922 | channel_auto_close(si_oc(si)); |
| 923 | channel_erase(si_oc(si)); |
| 924 | channel_auto_close(si_ic(si)); |
| 925 | channel_auto_read(si_ic(si)); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 926 | if (msg) |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 927 | co_inject(si_ic(si), msg->str, msg->len); |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 928 | if (!(s->flags & SF_ERR_MASK)) |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 929 | s->flags |= err; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 930 | if (!(s->flags & SF_FINST_MASK)) |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 931 | s->flags |= finst; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 932 | } |
| 933 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 934 | /* This function returns the appropriate error location for the given stream |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 935 | * and message. |
| 936 | */ |
| 937 | |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 938 | struct chunk *http_error_message(struct stream *s) |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 939 | { |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 940 | const int msgnum = http_get_status_idx(s->txn->status); |
| 941 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 942 | if (s->be->errmsg[msgnum].str) |
| 943 | return &s->be->errmsg[msgnum]; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 944 | else if (strm_fe(s)->errmsg[msgnum].str) |
| 945 | return &strm_fe(s)->errmsg[msgnum]; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 946 | else |
| 947 | return &http_err_chunks[msgnum]; |
| 948 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 949 | |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 950 | void |
| 951 | http_reply_and_close(struct stream *s, short status, struct chunk *msg) |
| 952 | { |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 953 | s->txn->flags &= ~TX_WAIT_NEXT_RQ; |
Christopher Faulet | 3e34429 | 2015-11-24 16:24:13 +0100 | [diff] [blame] | 954 | FLT_STRM_CB(s, flt_http_reply(s, status, msg)); |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 955 | stream_int_retnclose(&s->si[0], msg); |
| 956 | } |
| 957 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 958 | /* |
Willy Tarreau | b7ce424 | 2015-09-03 17:15:21 +0200 | [diff] [blame] | 959 | * returns a known method among HTTP_METH_* or HTTP_METH_OTHER for all unknown |
| 960 | * ones. |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 961 | */ |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 962 | 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] | 963 | { |
| 964 | unsigned char m; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 965 | const struct http_method_desc *h; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 966 | |
| 967 | m = ((unsigned)*str - 'A'); |
| 968 | |
| 969 | if (m < 26) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 970 | for (h = http_methods[m]; h->len > 0; h++) { |
| 971 | if (unlikely(h->len != len)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 972 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 973 | if (likely(memcmp(str, h->text, h->len) == 0)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 974 | return h->meth; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 975 | }; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 976 | } |
Willy Tarreau | b7ce424 | 2015-09-03 17:15:21 +0200 | [diff] [blame] | 977 | return HTTP_METH_OTHER; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 978 | } |
| 979 | |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 980 | /* Parse the URI from the given transaction (which is assumed to be in request |
| 981 | * phase) and look for the "/" beginning the PATH. If not found, return NULL. |
| 982 | * It is returned otherwise. |
| 983 | */ |
Thierry FOURNIER | 3c33178 | 2015-09-17 19:33:35 +0200 | [diff] [blame] | 984 | char *http_get_path(struct http_txn *txn) |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 985 | { |
| 986 | char *ptr, *end; |
| 987 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 988 | ptr = txn->req.chn->buf->p + txn->req.sl.rq.u; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 989 | end = ptr + txn->req.sl.rq.u_l; |
| 990 | |
| 991 | if (ptr >= end) |
| 992 | return NULL; |
| 993 | |
Lukas Tribus | 2395368 | 2017-04-28 13:24:30 +0000 | [diff] [blame] | 994 | /* RFC7230, par. 2.7 : |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 995 | * Request-URI = "*" | absuri | abspath | authority |
| 996 | */ |
| 997 | |
| 998 | if (*ptr == '*') |
| 999 | return NULL; |
| 1000 | |
| 1001 | if (isalpha((unsigned char)*ptr)) { |
| 1002 | /* this is a scheme as described by RFC3986, par. 3.1 */ |
| 1003 | ptr++; |
| 1004 | while (ptr < end && |
| 1005 | (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')) |
| 1006 | ptr++; |
| 1007 | /* skip '://' */ |
| 1008 | if (ptr == end || *ptr++ != ':') |
| 1009 | return NULL; |
| 1010 | if (ptr == end || *ptr++ != '/') |
| 1011 | return NULL; |
| 1012 | if (ptr == end || *ptr++ != '/') |
| 1013 | return NULL; |
| 1014 | } |
| 1015 | /* skip [user[:passwd]@]host[:[port]] */ |
| 1016 | |
| 1017 | while (ptr < end && *ptr != '/') |
| 1018 | ptr++; |
| 1019 | |
| 1020 | if (ptr == end) |
| 1021 | return NULL; |
| 1022 | |
| 1023 | /* OK, we got the '/' ! */ |
| 1024 | return ptr; |
| 1025 | } |
| 1026 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 1027 | /* Parse the URI from the given string and look for the "/" beginning the PATH. |
| 1028 | * If not found, return NULL. It is returned otherwise. |
| 1029 | */ |
| 1030 | static char * |
| 1031 | http_get_path_from_string(char *str) |
| 1032 | { |
| 1033 | char *ptr = str; |
| 1034 | |
| 1035 | /* RFC2616, par. 5.1.2 : |
| 1036 | * Request-URI = "*" | absuri | abspath | authority |
| 1037 | */ |
| 1038 | |
| 1039 | if (*ptr == '*') |
| 1040 | return NULL; |
| 1041 | |
| 1042 | if (isalpha((unsigned char)*ptr)) { |
| 1043 | /* this is a scheme as described by RFC3986, par. 3.1 */ |
| 1044 | ptr++; |
| 1045 | while (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.') |
| 1046 | ptr++; |
| 1047 | /* skip '://' */ |
| 1048 | if (*ptr == '\0' || *ptr++ != ':') |
| 1049 | return NULL; |
| 1050 | if (*ptr == '\0' || *ptr++ != '/') |
| 1051 | return NULL; |
| 1052 | if (*ptr == '\0' || *ptr++ != '/') |
| 1053 | return NULL; |
| 1054 | } |
| 1055 | /* skip [user[:passwd]@]host[:[port]] */ |
| 1056 | |
| 1057 | while (*ptr != '\0' && *ptr != ' ' && *ptr != '/') |
| 1058 | ptr++; |
| 1059 | |
| 1060 | if (*ptr == '\0' || *ptr == ' ') |
| 1061 | return NULL; |
| 1062 | |
| 1063 | /* OK, we got the '/' ! */ |
| 1064 | return ptr; |
| 1065 | } |
| 1066 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 1067 | /* Returns a 302 for a redirectable request that reaches a server working in |
| 1068 | * in redirect mode. This may only be called just after the stream interface |
| 1069 | * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will |
| 1070 | * follow normal proxy processing. NOTE: this function is designed to support |
| 1071 | * being called once data are scheduled for forwarding. |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1072 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1073 | void http_perform_server_redirect(struct stream *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1074 | { |
| 1075 | struct http_txn *txn; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 1076 | struct server *srv; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1077 | char *path; |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 1078 | int len, rewind; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1079 | |
| 1080 | /* 1: create the response header */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1081 | trash.len = strlen(HTTP_302); |
| 1082 | memcpy(trash.str, HTTP_302, trash.len); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1083 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 1084 | srv = objt_server(s->target); |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 1085 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1086 | /* 2: add the server's prefix */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1087 | if (trash.len + srv->rdr_len > trash.size) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1088 | return; |
| 1089 | |
Willy Tarreau | dcb75c4 | 2010-01-10 00:24:22 +0100 | [diff] [blame] | 1090 | /* special prefix "/" means don't change URL */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 1091 | if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1092 | memcpy(trash.str + trash.len, srv->rdr_pfx, srv->rdr_len); |
| 1093 | trash.len += srv->rdr_len; |
Willy Tarreau | dcb75c4 | 2010-01-10 00:24:22 +0100 | [diff] [blame] | 1094 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1095 | |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 1096 | /* 3: add the request URI. Since it was already forwarded, we need |
| 1097 | * to temporarily rewind the buffer. |
| 1098 | */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 1099 | txn = s->txn; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1100 | b_rew(s->req.buf, rewind = http_hdr_rewind(&txn->req)); |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 1101 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1102 | path = http_get_path(txn); |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1103 | 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] | 1104 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1105 | b_adv(s->req.buf, rewind); |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 1106 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1107 | if (!path) |
| 1108 | return; |
| 1109 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1110 | if (trash.len + len > trash.size - 4) /* 4 for CRLF-CRLF */ |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1111 | return; |
| 1112 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1113 | memcpy(trash.str + trash.len, path, len); |
| 1114 | trash.len += len; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1115 | |
| 1116 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1117 | memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29); |
| 1118 | trash.len += 29; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1119 | } else { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1120 | memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23); |
| 1121 | trash.len += 23; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1122 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1123 | |
| 1124 | /* prepare to return without error. */ |
Willy Tarreau | 73b013b | 2012-05-21 16:31:45 +0200 | [diff] [blame] | 1125 | si_shutr(si); |
| 1126 | si_shutw(si); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1127 | si->err_type = SI_ET_NONE; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1128 | si->state = SI_ST_CLO; |
| 1129 | |
| 1130 | /* send the message */ |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 1131 | txn->status = 302; |
| 1132 | http_server_error(s, si, SF_ERR_LOCAL, SF_FINST_C, &trash); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1133 | |
| 1134 | /* 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] | 1135 | srv_inc_sess_ctr(srv); |
Bhaskar Maddala | a20cb85 | 2014-02-03 16:26:46 -0500 | [diff] [blame] | 1136 | srv_set_sess_last(srv); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1137 | } |
| 1138 | |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 1139 | /* Return the error message corresponding to si->err_type. It is assumed |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1140 | * that the server side is closed. Note that err_type is actually a |
| 1141 | * bitmask, where almost only aborts may be cumulated with other |
| 1142 | * values. We consider that aborted operations are more important |
| 1143 | * than timeouts or errors due to the fact that nobody else in the |
| 1144 | * logs might explain incomplete retries. All others should avoid |
| 1145 | * being cumulated. It should normally not be possible to have multiple |
| 1146 | * 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] | 1147 | * Note that connection errors appearing on the second request of a keep-alive |
| 1148 | * connection are not reported since this allows the client to retry. |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1149 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1150 | void http_return_srv_error(struct stream *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1151 | { |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 1152 | int err_type = si->err_type; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1153 | |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 1154 | /* set s->txn->status for http_error_message(s) */ |
| 1155 | s->txn->status = 503; |
| 1156 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1157 | if (err_type & SI_ET_QUEUE_ABRT) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1158 | http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q, |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 1159 | http_error_message(s)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1160 | else if (err_type & SI_ET_CONN_ABRT) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1161 | http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C, |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 1162 | (s->txn->flags & TX_NOT_FIRST) ? NULL : |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 1163 | http_error_message(s)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1164 | else if (err_type & SI_ET_QUEUE_TO) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1165 | http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q, |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 1166 | http_error_message(s)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1167 | else if (err_type & SI_ET_QUEUE_ERR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1168 | http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q, |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 1169 | http_error_message(s)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1170 | else if (err_type & SI_ET_CONN_TO) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1171 | http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C, |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 1172 | (s->txn->flags & TX_NOT_FIRST) ? NULL : |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 1173 | http_error_message(s)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1174 | else if (err_type & SI_ET_CONN_ERR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1175 | http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C, |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 1176 | (s->flags & SF_SRV_REUSED) ? NULL : |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 1177 | http_error_message(s)); |
Willy Tarreau | 2d400bb | 2012-05-14 12:11:47 +0200 | [diff] [blame] | 1178 | else if (err_type & SI_ET_CONN_RES) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1179 | http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C, |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 1180 | (s->txn->flags & TX_NOT_FIRST) ? NULL : |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 1181 | http_error_message(s)); |
| 1182 | else { /* SI_ET_CONN_OTHER and others */ |
| 1183 | s->txn->status = 500; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1184 | http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C, |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 1185 | http_error_message(s)); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 1186 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1187 | } |
| 1188 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1189 | extern const char sess_term_cond[8]; |
| 1190 | extern const char sess_fin_state[8]; |
| 1191 | extern const char *monthname[12]; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 1192 | struct pool_head *pool_head_http_txn; |
| 1193 | struct pool_head *pool_head_requri; |
| 1194 | struct pool_head *pool_head_capture = NULL; |
| 1195 | struct pool_head *pool_head_uniqueid; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1196 | |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1197 | /* |
| 1198 | * Capture headers from message starting at <som> according to header list |
Willy Tarreau | 54da8db | 2014-06-13 16:11:48 +0200 | [diff] [blame] | 1199 | * <cap_hdr>, and fill the <cap> pointers appropriately. |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1200 | */ |
| 1201 | void capture_headers(char *som, struct hdr_idx *idx, |
| 1202 | char **cap, struct cap_hdr *cap_hdr) |
| 1203 | { |
| 1204 | char *eol, *sol, *col, *sov; |
| 1205 | int cur_idx; |
| 1206 | struct cap_hdr *h; |
| 1207 | int len; |
| 1208 | |
| 1209 | sol = som + hdr_idx_first_pos(idx); |
| 1210 | cur_idx = hdr_idx_first_idx(idx); |
| 1211 | |
| 1212 | while (cur_idx) { |
| 1213 | eol = sol + idx->v[cur_idx].len; |
| 1214 | |
| 1215 | col = sol; |
| 1216 | while (col < eol && *col != ':') |
| 1217 | col++; |
| 1218 | |
| 1219 | sov = col + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 1220 | while (sov < eol && HTTP_IS_LWS(*sov)) |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1221 | sov++; |
| 1222 | |
| 1223 | for (h = cap_hdr; h; h = h->next) { |
Willy Tarreau | 54da8db | 2014-06-13 16:11:48 +0200 | [diff] [blame] | 1224 | if (h->namelen && (h->namelen == col - sol) && |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1225 | (strncasecmp(sol, h->name, h->namelen) == 0)) { |
| 1226 | if (cap[h->index] == NULL) |
| 1227 | cap[h->index] = |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 1228 | pool_alloc(h->pool); |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1229 | |
| 1230 | if (cap[h->index] == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1231 | ha_alert("HTTP capture : out of memory.\n"); |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1232 | continue; |
| 1233 | } |
| 1234 | |
| 1235 | len = eol - sov; |
| 1236 | if (len > h->len) |
| 1237 | len = h->len; |
| 1238 | |
| 1239 | memcpy(cap[h->index], sov, len); |
| 1240 | cap[h->index][len]=0; |
| 1241 | } |
| 1242 | } |
| 1243 | sol = eol + idx->v[cur_idx].cr + 1; |
| 1244 | cur_idx = idx->v[cur_idx].next; |
| 1245 | } |
| 1246 | } |
| 1247 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1248 | /* |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1249 | * Returns the data from Authorization header. Function may be called more |
| 1250 | * than once so data is stored in txn->auth_data. When no header is found |
| 1251 | * 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] | 1252 | * searching again for something we are unable to find anyway. However, if |
| 1253 | * 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] | 1254 | * have the credentials overwritten by another stream in parallel. |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1255 | */ |
| 1256 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1257 | int |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1258 | get_http_auth(struct stream *s) |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1259 | { |
| 1260 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 1261 | struct http_txn *txn = s->txn; |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1262 | struct chunk auth_method; |
| 1263 | struct hdr_ctx ctx; |
| 1264 | char *h, *p; |
| 1265 | int len; |
| 1266 | |
| 1267 | #ifdef DEBUG_AUTH |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1268 | printf("Auth for stream %p: %d\n", s, txn->auth.method); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1269 | #endif |
| 1270 | |
| 1271 | if (txn->auth.method == HTTP_AUTH_WRONG) |
| 1272 | return 0; |
| 1273 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1274 | txn->auth.method = HTTP_AUTH_WRONG; |
| 1275 | |
| 1276 | ctx.idx = 0; |
Willy Tarreau | 844a7e7 | 2010-01-31 21:46:18 +0100 | [diff] [blame] | 1277 | |
| 1278 | if (txn->flags & TX_USE_PX_CONN) { |
| 1279 | h = "Proxy-Authorization"; |
| 1280 | len = strlen(h); |
| 1281 | } else { |
| 1282 | h = "Authorization"; |
| 1283 | len = strlen(h); |
| 1284 | } |
| 1285 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1286 | 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] | 1287 | return 0; |
| 1288 | |
| 1289 | h = ctx.line + ctx.val; |
| 1290 | |
| 1291 | p = memchr(h, ' ', ctx.vlen); |
Willy Tarreau | 5c557d1 | 2016-03-13 08:17:02 +0100 | [diff] [blame] | 1292 | len = p - h; |
| 1293 | if (!p || len <= 0) |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1294 | return 0; |
| 1295 | |
David Carlier | 7365f7d | 2016-04-04 11:54:42 +0100 | [diff] [blame] | 1296 | if (chunk_initlen(&auth_method, h, 0, len) != 1) |
| 1297 | return 0; |
| 1298 | |
Willy Tarreau | 5c557d1 | 2016-03-13 08:17:02 +0100 | [diff] [blame] | 1299 | 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] | 1300 | |
| 1301 | if (!strncasecmp("Basic", auth_method.str, auth_method.len)) { |
Christopher Faulet | 6988f67 | 2017-07-27 15:18:52 +0200 | [diff] [blame] | 1302 | struct chunk *http_auth = get_trash_chunk(); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1303 | |
| 1304 | len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len, |
Christopher Faulet | 6988f67 | 2017-07-27 15:18:52 +0200 | [diff] [blame] | 1305 | http_auth->str, global.tune.bufsize - 1); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1306 | |
| 1307 | if (len < 0) |
| 1308 | return 0; |
| 1309 | |
| 1310 | |
Christopher Faulet | 6988f67 | 2017-07-27 15:18:52 +0200 | [diff] [blame] | 1311 | http_auth->str[len] = '\0'; |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1312 | |
Christopher Faulet | 6988f67 | 2017-07-27 15:18:52 +0200 | [diff] [blame] | 1313 | p = strchr(http_auth->str, ':'); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1314 | |
| 1315 | if (!p) |
| 1316 | return 0; |
| 1317 | |
Christopher Faulet | 6988f67 | 2017-07-27 15:18:52 +0200 | [diff] [blame] | 1318 | txn->auth.user = http_auth->str; |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1319 | *p = '\0'; |
| 1320 | txn->auth.pass = p+1; |
| 1321 | |
| 1322 | txn->auth.method = HTTP_AUTH_BASIC; |
| 1323 | return 1; |
| 1324 | } |
| 1325 | |
| 1326 | return 0; |
| 1327 | } |
| 1328 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1329 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1330 | /* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the |
| 1331 | * conversion succeeded, 0 in case of error. If the request was already 1.X, |
| 1332 | * nothing is done and 1 is returned. |
| 1333 | */ |
Willy Tarreau | 418bfcc | 2012-03-09 13:56:20 +0100 | [diff] [blame] | 1334 | static int http_upgrade_v09_to_v10(struct http_txn *txn) |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1335 | { |
| 1336 | int delta; |
| 1337 | char *cur_end; |
Willy Tarreau | 418bfcc | 2012-03-09 13:56:20 +0100 | [diff] [blame] | 1338 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1339 | |
| 1340 | if (msg->sl.rq.v_l != 0) |
| 1341 | return 1; |
| 1342 | |
Apollon Oikonomopoulos | 25a1522 | 2014-04-06 02:46:00 +0300 | [diff] [blame] | 1343 | /* RFC 1945 allows only GET for HTTP/0.9 requests */ |
| 1344 | if (txn->meth != HTTP_METH_GET) |
| 1345 | return 0; |
| 1346 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1347 | cur_end = msg->chn->buf->p + msg->sl.rq.l; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1348 | |
| 1349 | if (msg->sl.rq.u_l == 0) { |
Apollon Oikonomopoulos | 25a1522 | 2014-04-06 02:46:00 +0300 | [diff] [blame] | 1350 | /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */ |
| 1351 | return 0; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1352 | } |
| 1353 | /* add HTTP version */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1354 | 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] | 1355 | http_msg_move_end(msg, delta); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1356 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1357 | cur_end = (char *)http_parse_reqline(msg, |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1358 | HTTP_MSG_RQMETH, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1359 | msg->chn->buf->p, cur_end + 1, |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1360 | NULL, NULL); |
| 1361 | if (unlikely(!cur_end)) |
| 1362 | return 0; |
| 1363 | |
| 1364 | /* we have a full HTTP/1.0 request now and we know that |
| 1365 | * we have either a CR or an LF at <ptr>. |
| 1366 | */ |
| 1367 | hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r'); |
| 1368 | return 1; |
| 1369 | } |
| 1370 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1371 | /* Parse the Connection: header of an HTTP request, looking for both "close" |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1372 | * and "keep-alive" values. If we already know that some headers may safely |
| 1373 | * 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] | 1374 | * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses) |
| 1375 | * - 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] | 1376 | * Presence of the "Upgrade" token is also checked and reported. |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1377 | * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was |
| 1378 | * found, and TX_CON_*_SET is adjusted depending on what is left so only |
| 1379 | * harmless combinations may be removed. Do not call that after changes have |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1380 | * been processed. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1381 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1382 | 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] | 1383 | { |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1384 | struct hdr_ctx ctx; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1385 | const char *hdr_val = "Connection"; |
| 1386 | int hdr_len = 10; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1387 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1388 | if (txn->flags & TX_HDR_CONN_PRS) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1389 | return; |
| 1390 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1391 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1392 | hdr_val = "Proxy-Connection"; |
| 1393 | hdr_len = 16; |
| 1394 | } |
| 1395 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1396 | ctx.idx = 0; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1397 | txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1398 | 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] | 1399 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 1400 | txn->flags |= TX_HDR_CONN_KAL; |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1401 | if (to_del & 2) |
| 1402 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1403 | else |
| 1404 | txn->flags |= TX_CON_KAL_SET; |
| 1405 | } |
| 1406 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 1407 | txn->flags |= TX_HDR_CONN_CLO; |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1408 | if (to_del & 1) |
| 1409 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1410 | else |
| 1411 | txn->flags |= TX_CON_CLO_SET; |
| 1412 | } |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 1413 | else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) { |
| 1414 | txn->flags |= TX_HDR_CONN_UPG; |
| 1415 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1416 | } |
| 1417 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1418 | txn->flags |= TX_HDR_CONN_PRS; |
| 1419 | return; |
| 1420 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1421 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1422 | /* Apply desired changes on the Connection: header. Values may be removed and/or |
| 1423 | * added depending on the <wanted> flags, which are exclusively composed of |
| 1424 | * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The |
| 1425 | * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left. |
| 1426 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1427 | 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] | 1428 | { |
| 1429 | struct hdr_ctx ctx; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1430 | const char *hdr_val = "Connection"; |
| 1431 | int hdr_len = 10; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1432 | |
| 1433 | ctx.idx = 0; |
| 1434 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1435 | |
| 1436 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1437 | hdr_val = "Proxy-Connection"; |
| 1438 | hdr_len = 16; |
| 1439 | } |
| 1440 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1441 | txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1442 | 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] | 1443 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 1444 | if (wanted & TX_CON_KAL_SET) |
| 1445 | txn->flags |= TX_CON_KAL_SET; |
| 1446 | else |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1447 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1448 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1449 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 1450 | if (wanted & TX_CON_CLO_SET) |
| 1451 | txn->flags |= TX_CON_CLO_SET; |
| 1452 | else |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1453 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 1454 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1455 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1456 | |
| 1457 | if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 1458 | return; |
| 1459 | |
| 1460 | if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) { |
| 1461 | txn->flags |= TX_CON_CLO_SET; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1462 | hdr_val = "Connection: close"; |
| 1463 | hdr_len = 17; |
| 1464 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1465 | hdr_val = "Proxy-Connection: close"; |
| 1466 | hdr_len = 23; |
| 1467 | } |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1468 | http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1469 | } |
| 1470 | |
| 1471 | if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) { |
| 1472 | txn->flags |= TX_CON_KAL_SET; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1473 | hdr_val = "Connection: keep-alive"; |
| 1474 | hdr_len = 22; |
| 1475 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1476 | hdr_val = "Proxy-Connection: keep-alive"; |
| 1477 | hdr_len = 28; |
| 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 | return; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1482 | } |
| 1483 | |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 1484 | /* Parses a qvalue and returns it multipled by 1000, from 0 to 1000. If the |
| 1485 | * value is larger than 1000, it is bound to 1000. The parser consumes up to |
| 1486 | * 1 digit, one dot and 3 digits and stops on the first invalid character. |
| 1487 | * Unparsable qvalues return 1000 as "q=1.000". |
| 1488 | */ |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 1489 | int parse_qvalue(const char *qvalue, const char **end) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 1490 | { |
| 1491 | int q = 1000; |
| 1492 | |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 1493 | if (!isdigit((unsigned char)*qvalue)) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 1494 | goto out; |
| 1495 | q = (*qvalue++ - '0') * 1000; |
| 1496 | |
| 1497 | if (*qvalue++ != '.') |
| 1498 | goto out; |
| 1499 | |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 1500 | if (!isdigit((unsigned char)*qvalue)) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 1501 | goto out; |
| 1502 | q += (*qvalue++ - '0') * 100; |
| 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') * 10; |
| 1507 | |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 1508 | if (!isdigit((unsigned char)*qvalue)) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 1509 | goto out; |
| 1510 | q += (*qvalue++ - '0') * 1; |
| 1511 | out: |
| 1512 | if (q > 1000) |
| 1513 | q = 1000; |
Willy Tarreau | 38b3aa5 | 2014-04-22 23:32:05 +0200 | [diff] [blame] | 1514 | if (end) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 1515 | *end = qvalue; |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 1516 | return q; |
| 1517 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 1518 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1519 | 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] | 1520 | { |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 1521 | struct proxy *fe = strm_fe(s); |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 1522 | int tmp = TX_CON_WANT_KAL; |
| 1523 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1524 | if (!((fe->options2|s->be->options2) & PR_O2_FAKE_KA)) { |
| 1525 | if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN || |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 1526 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN) |
| 1527 | tmp = TX_CON_WANT_TUN; |
| 1528 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1529 | if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 1530 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL) |
| 1531 | tmp = TX_CON_WANT_TUN; |
| 1532 | } |
| 1533 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1534 | if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL || |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 1535 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) { |
| 1536 | /* option httpclose + server_close => forceclose */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1537 | if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 1538 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL) |
| 1539 | tmp = TX_CON_WANT_CLO; |
| 1540 | else |
| 1541 | tmp = TX_CON_WANT_SCL; |
| 1542 | } |
| 1543 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1544 | if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL || |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 1545 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL) |
| 1546 | tmp = TX_CON_WANT_CLO; |
| 1547 | |
| 1548 | if ((txn->flags & TX_CON_WANT_MSK) < tmp) |
| 1549 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp; |
| 1550 | |
| 1551 | if (!(txn->flags & TX_HDR_CONN_PRS) && |
| 1552 | (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) { |
| 1553 | /* parse the Connection header and possibly clean it */ |
| 1554 | int to_del = 0; |
| 1555 | if ((msg->flags & HTTP_MSGF_VER_11) || |
| 1556 | ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1557 | !((fe->options2|s->be->options2) & PR_O2_FAKE_KA))) |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 1558 | to_del |= 2; /* remove "keep-alive" */ |
| 1559 | if (!(msg->flags & HTTP_MSGF_VER_11)) |
| 1560 | to_del |= 1; /* remove "close" */ |
| 1561 | http_parse_connection_header(txn, msg, to_del); |
| 1562 | } |
| 1563 | |
| 1564 | /* check if client or config asks for explicit close in KAL/SCL */ |
| 1565 | if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 1566 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) && |
| 1567 | ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */ |
| 1568 | (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */ |
| 1569 | !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1570 | fe->state == PR_STSTOPPED)) /* frontend is stopping */ |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 1571 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 1572 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 1573 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 1574 | /* This stream analyser waits for a complete HTTP request. It returns 1 if the |
| 1575 | * processing can continue on next analysers, or zero if it either needs more |
| 1576 | * data or wants to immediately abort the request (eg: timeout, error, ...). It |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1577 | * 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] | 1578 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 1579 | * abort. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1580 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1581 | 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] | 1582 | { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1583 | /* |
| 1584 | * We will parse the partial (or complete) lines. |
| 1585 | * We will check the request syntax, and also join multi-line |
| 1586 | * headers. An index of all the lines will be elaborated while |
| 1587 | * parsing. |
| 1588 | * |
| 1589 | * For the parsing, we use a 28 states FSM. |
| 1590 | * |
| 1591 | * Here is the information we currently have : |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1592 | * req->buf->p = beginning of request |
| 1593 | * req->buf->p + msg->eoh = end of processed headers / start of current one |
| 1594 | * req->buf->p + req->buf->i = end of input data |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 1595 | * msg->eol = end of current header or line (LF or CRLF) |
| 1596 | * msg->next = first non-visited byte |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 1597 | * |
| 1598 | * 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] | 1599 | * we will set a few fields (txn->meth, sn->flags/SF_REDIRECTABLE). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 1600 | * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and |
| 1601 | * finally headers capture. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1602 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1603 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1604 | int cur_idx; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 1605 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 1606 | struct http_txn *txn = s->txn; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1607 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 1608 | struct hdr_ctx ctx; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1609 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1610 | 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] | 1611 | now_ms, __FUNCTION__, |
| 1612 | s, |
| 1613 | req, |
| 1614 | req->rex, req->wex, |
| 1615 | req->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1616 | req->buf->i, |
Willy Tarreau | 6bf1736 | 2009-02-24 10:48:35 +0100 | [diff] [blame] | 1617 | req->analysers); |
| 1618 | |
Willy Tarreau | 52a0c60 | 2009-08-16 22:45:38 +0200 | [diff] [blame] | 1619 | /* we're speaking HTTP here, so let's speak HTTP to the client */ |
| 1620 | s->srv_error = http_return_srv_error; |
| 1621 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 1622 | /* There's a protected area at the end of the buffer for rewriting |
| 1623 | * purposes. We don't want to start to parse the request if the |
| 1624 | * protected area is affected, because we may have to move processed |
| 1625 | * data later, which is much more complicated. |
| 1626 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1627 | if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) { |
Thierry FOURNIER / OZON.IO | 4cac359 | 2016-07-28 17:19:45 +0200 | [diff] [blame] | 1628 | |
| 1629 | /* This point is executed when some data is avalaible for analysis, |
| 1630 | * so we log the end of the idle time. */ |
| 1631 | if (s->logs.t_idle == -1) |
| 1632 | s->logs.t_idle = tv_ms_elapsed(&s->logs.tv_accept, &now) - s->logs.t_handshake; |
| 1633 | |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 1634 | if (txn->flags & TX_NOT_FIRST) { |
Willy Tarreau | ba0902e | 2015-01-13 14:39:16 +0100 | [diff] [blame] | 1635 | if (unlikely(!channel_is_rewritable(req))) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 1636 | 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] | 1637 | goto failed_keep_alive; |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 1638 | /* 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] | 1639 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 1640 | req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | d7ad9f5 | 2013-12-31 17:26:25 +0100 | [diff] [blame] | 1641 | req->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 1642 | return 0; |
| 1643 | } |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 1644 | if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) || |
| 1645 | bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite)) |
| 1646 | buffer_slow_realign(req->buf); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 1647 | } |
| 1648 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1649 | if (likely(msg->next < req->buf->i)) /* some unparsed data are available */ |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 1650 | http_msg_analyzer(msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 1651 | } |
| 1652 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1653 | /* 1: we might have to print this header in debug mode */ |
| 1654 | if (unlikely((global.mode & MODE_DEBUG) && |
| 1655 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | 7d59e90 | 2014-10-21 19:36:09 +0200 | [diff] [blame] | 1656 | msg->msg_state >= HTTP_MSG_BODY)) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1657 | char *eol, *sol; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1658 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1659 | sol = req->buf->p; |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 1660 | /* this is a bit complex : in case of error on the request line, |
| 1661 | * we know that rq.l is still zero, so we display only the part |
| 1662 | * up to the end of the line (truncated by debug_hdr). |
| 1663 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1664 | 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] | 1665 | debug_hdr("clireq", s, sol, eol); |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 1666 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1667 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 1668 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1669 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1670 | while (cur_idx) { |
| 1671 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 1672 | debug_hdr("clihdr", s, sol, eol); |
| 1673 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 1674 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1675 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1676 | } |
| 1677 | |
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 | /* |
| 1680 | * Now we quickly check if we have found a full valid request. |
| 1681 | * If not so, we check the FD and buffer states before leaving. |
| 1682 | * A full request is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 1683 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 1684 | * requests are checked first. When waiting for a second request |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1685 | * on a keep-alive stream, if we encounter and error, close, t/o, |
| 1686 | * 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] | 1687 | * 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] | 1688 | * process_stream() as a frontend error. |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 1689 | * Last, we may increase some tracked counters' http request errors on |
| 1690 | * the cases that are deliberately the client's fault. For instance, |
| 1691 | * a timeout or connection reset is not counted as an error. However |
| 1692 | * a bad request is. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1693 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1694 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 1695 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1696 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1697 | * First, let's catch bad requests. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1698 | */ |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 1699 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1700 | stream_inc_http_req_ctr(s); |
| 1701 | stream_inc_http_err_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1702 | proxy_inc_fe_req_ctr(sess->fe); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1703 | goto return_bad_req; |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 1704 | } |
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 | /* 1: Since we are in header mode, if there's no space |
| 1707 | * left for headers, we won't be able to free more |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1708 | * later, so the stream will never terminate. We |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1709 | * must terminate it now. |
| 1710 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1711 | if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1712 | /* FIXME: check if URI is set and return Status |
| 1713 | * 414 Request URI too long instead. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1714 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1715 | stream_inc_http_req_ctr(s); |
| 1716 | stream_inc_http_err_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1717 | proxy_inc_fe_req_ctr(sess->fe); |
Willy Tarreau | fec4d89 | 2011-09-02 20:04:57 +0200 | [diff] [blame] | 1718 | if (msg->err_pos < 0) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1719 | msg->err_pos = req->buf->i; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1720 | goto return_bad_req; |
| 1721 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1722 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1723 | /* 2: have we encountered a read error ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 1724 | else if (req->flags & CF_READ_ERROR) { |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1725 | if (!(s->flags & SF_ERR_MASK)) |
| 1726 | s->flags |= SF_ERR_CLICL; |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 1727 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 1728 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 1729 | goto failed_keep_alive; |
| 1730 | |
Willy Tarreau | 0f228a0 | 2015-05-01 15:37:53 +0200 | [diff] [blame] | 1731 | if (sess->fe->options & PR_O_IGNORE_PRB) |
| 1732 | goto failed_keep_alive; |
| 1733 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1734 | /* we cannot return any message on error */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 1735 | if (msg->err_pos >= 0) { |
Emeric Brun | 8c1aaa2 | 2017-06-15 11:30:06 +0200 | [diff] [blame] | 1736 | 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] | 1737 | stream_inc_http_err_ctr(s); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 1738 | } |
| 1739 | |
Willy Tarreau | dc979f2 | 2012-12-04 10:39:01 +0100 | [diff] [blame] | 1740 | txn->status = 400; |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 1741 | msg->err_state = msg->msg_state; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1742 | msg->msg_state = HTTP_MSG_ERROR; |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 1743 | http_reply_and_close(s, txn->status, NULL); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 1744 | req->analysers &= AN_REQ_FLT_END; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1745 | stream_inc_http_req_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1746 | proxy_inc_fe_req_ctr(sess->fe); |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 1747 | HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 1748 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 1749 | HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 1750 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1751 | if (!(s->flags & SF_FINST_MASK)) |
| 1752 | s->flags |= SF_FINST_R; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1753 | return 0; |
| 1754 | } |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 1755 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1756 | /* 3: has the read timeout expired ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 1757 | 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] | 1758 | if (!(s->flags & SF_ERR_MASK)) |
| 1759 | s->flags |= SF_ERR_CLITO; |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 1760 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 1761 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 1762 | goto failed_keep_alive; |
| 1763 | |
Willy Tarreau | 0f228a0 | 2015-05-01 15:37:53 +0200 | [diff] [blame] | 1764 | if (sess->fe->options & PR_O_IGNORE_PRB) |
| 1765 | goto failed_keep_alive; |
| 1766 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1767 | /* read timeout : give up with an error message. */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 1768 | if (msg->err_pos >= 0) { |
Emeric Brun | 8c1aaa2 | 2017-06-15 11:30:06 +0200 | [diff] [blame] | 1769 | 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] | 1770 | stream_inc_http_err_ctr(s); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 1771 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1772 | txn->status = 408; |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 1773 | msg->err_state = msg->msg_state; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1774 | msg->msg_state = HTTP_MSG_ERROR; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 1775 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 1776 | req->analysers &= AN_REQ_FLT_END; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 1777 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1778 | stream_inc_http_req_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1779 | proxy_inc_fe_req_ctr(sess->fe); |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 1780 | HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 1781 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 1782 | HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 1783 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1784 | if (!(s->flags & SF_FINST_MASK)) |
| 1785 | s->flags |= SF_FINST_R; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1786 | return 0; |
| 1787 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1788 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1789 | /* 4: have we encountered a close ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 1790 | else if (req->flags & CF_SHUTR) { |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1791 | if (!(s->flags & SF_ERR_MASK)) |
| 1792 | s->flags |= SF_ERR_CLICL; |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 1793 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 1794 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 1795 | goto failed_keep_alive; |
| 1796 | |
Willy Tarreau | 0f228a0 | 2015-05-01 15:37:53 +0200 | [diff] [blame] | 1797 | if (sess->fe->options & PR_O_IGNORE_PRB) |
| 1798 | goto failed_keep_alive; |
| 1799 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 1800 | if (msg->err_pos >= 0) |
Emeric Brun | 8c1aaa2 | 2017-06-15 11:30:06 +0200 | [diff] [blame] | 1801 | 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] | 1802 | txn->status = 400; |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 1803 | msg->err_state = msg->msg_state; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1804 | msg->msg_state = HTTP_MSG_ERROR; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 1805 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 1806 | req->analysers &= AN_REQ_FLT_END; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1807 | stream_inc_http_err_ctr(s); |
| 1808 | stream_inc_http_req_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1809 | proxy_inc_fe_req_ctr(sess->fe); |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 1810 | HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 1811 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 1812 | HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 1813 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1814 | if (!(s->flags & SF_FINST_MASK)) |
| 1815 | s->flags |= SF_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 1816 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1817 | } |
| 1818 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 1819 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 1820 | req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1821 | s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 1822 | #ifdef TCP_QUICKACK |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 1823 | if (sess->listener->options & LI_O_NOQUICKACK && req->buf->i && |
| 1824 | objt_conn(sess->origin) && conn_ctrl_ready(__objt_conn(sess->origin))) { |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 1825 | /* We need more data, we have to re-enable quick-ack in case we |
| 1826 | * previously disabled it, otherwise we might cause the client |
| 1827 | * to delay next data. |
| 1828 | */ |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 1829 | 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] | 1830 | } |
| 1831 | #endif |
Willy Tarreau | 1b194fe | 2009-03-21 21:10:04 +0100 | [diff] [blame] | 1832 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 1833 | if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) { |
| 1834 | /* If the client starts to talk, let's fall back to |
| 1835 | * request timeout processing. |
| 1836 | */ |
| 1837 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 1838 | req->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 1839 | } |
| 1840 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1841 | /* just set the request timeout once at the beginning of the request */ |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 1842 | if (!tick_isset(req->analyse_exp)) { |
| 1843 | if ((msg->msg_state == HTTP_MSG_RQBEFORE) && |
| 1844 | (txn->flags & TX_WAIT_NEXT_RQ) && |
| 1845 | tick_isset(s->be->timeout.httpka)) |
| 1846 | req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka); |
| 1847 | else |
| 1848 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
| 1849 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1850 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1851 | /* we're not ready yet */ |
| 1852 | return 0; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 1853 | |
| 1854 | failed_keep_alive: |
| 1855 | /* Here we process low-level errors for keep-alive requests. In |
| 1856 | * short, if the request is not the first one and it experiences |
| 1857 | * a timeout, read error or shutdown, we just silently close so |
| 1858 | * that the client can try again. |
| 1859 | */ |
| 1860 | txn->status = 0; |
| 1861 | msg->msg_state = HTTP_MSG_RQBEFORE; |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 1862 | req->analysers &= AN_REQ_FLT_END; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 1863 | s->logs.logwait = 0; |
Willy Tarreau | abcd514 | 2013-06-11 17:18:02 +0200 | [diff] [blame] | 1864 | s->logs.level = 0; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1865 | s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 1866 | http_reply_and_close(s, txn->status, NULL); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 1867 | return 0; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1868 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1869 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 1870 | /* OK now we have a complete HTTP request with indexed headers. Let's |
| 1871 | * complete the request parsing by setting a few fields we will need |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1872 | * 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] | 1873 | * 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] | 1874 | * 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] | 1875 | * byte after the last LF. msg->sov points to the first byte of data. |
| 1876 | * msg->eol cannot be trusted because it may have been left uninitialized |
| 1877 | * (for instance in the absence of headers). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 1878 | */ |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1879 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1880 | stream_inc_http_req_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1881 | 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] | 1882 | |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 1883 | if (txn->flags & TX_WAIT_NEXT_RQ) { |
| 1884 | /* kill the pending keep-alive timeout */ |
| 1885 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
| 1886 | req->analyse_exp = TICK_ETERNITY; |
| 1887 | } |
| 1888 | |
| 1889 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 1890 | /* Maybe we found in invalid header name while we were configured not |
| 1891 | * to block on that, so we have to capture it now. |
| 1892 | */ |
| 1893 | if (unlikely(msg->err_pos >= 0)) |
Emeric Brun | 8c1aaa2 | 2017-06-15 11:30:06 +0200 | [diff] [blame] | 1894 | 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] | 1895 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1896 | /* |
| 1897 | * 1: identify the method |
| 1898 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1899 | 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] | 1900 | |
| 1901 | /* we can make use of server redirect on GET and HEAD */ |
| 1902 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1903 | s->flags |= SF_REDIRECTABLE; |
Willy Tarreau | 9165979 | 2017-11-10 19:38:10 +0100 | [diff] [blame] | 1904 | else if (txn->meth == HTTP_METH_OTHER && |
| 1905 | msg->sl.rq.m_l == 3 && memcmp(req->buf->p, "PRI", 3) == 0) { |
| 1906 | /* PRI is reserved for the HTTP/2 preface */ |
| 1907 | msg->err_pos = 0; |
| 1908 | goto return_bad_req; |
| 1909 | } |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 1910 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1911 | /* |
| 1912 | * 2: check if the URI matches the monitor_uri. |
| 1913 | * We have to do this for every request which gets in, because |
| 1914 | * the monitor-uri is defined by the frontend. |
| 1915 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1916 | if (unlikely((sess->fe->monitor_uri_len != 0) && |
| 1917 | (sess->fe->monitor_uri_len == msg->sl.rq.u_l) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1918 | !memcmp(req->buf->p + msg->sl.rq.u, |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1919 | sess->fe->monitor_uri, |
| 1920 | sess->fe->monitor_uri_len))) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1921 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1922 | * We have found the monitor URI |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1923 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1924 | struct acl_cond *cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 1925 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1926 | s->flags |= SF_MONITOR; |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 1927 | HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1); |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 1928 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1929 | /* Check if we want to fail this monitor request or not */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1930 | list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 1931 | 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] | 1932 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1933 | ret = acl_pass(ret); |
| 1934 | if (cond->pol == ACL_COND_UNLESS) |
| 1935 | ret = !ret; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 1936 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1937 | if (ret) { |
| 1938 | /* we fail this request, let's return 503 service unavail */ |
| 1939 | txn->status = 503; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 1940 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1941 | if (!(s->flags & SF_ERR_MASK)) |
| 1942 | 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] | 1943 | goto return_prx_cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 1944 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1945 | } |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 1946 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1947 | /* nothing to fail, let's reply normaly */ |
| 1948 | txn->status = 200; |
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; |
| 1953 | } |
| 1954 | |
| 1955 | /* |
| 1956 | * 3: Maybe we have to copy the original REQURI for the logs ? |
| 1957 | * Note: we cannot log anymore if the request has been |
| 1958 | * classified as invalid. |
| 1959 | */ |
| 1960 | if (unlikely(s->logs.logwait & LW_REQ)) { |
| 1961 | /* we have a complete HTTP request that we must log */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 1962 | if ((txn->uri = pool_alloc(pool_head_requri)) != NULL) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1963 | int urilen = msg->sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1964 | |
Stéphane Cottin | 23e9e93 | 2017-05-18 08:58:41 +0200 | [diff] [blame] | 1965 | if (urilen >= global.tune.requri_len ) |
| 1966 | urilen = global.tune.requri_len - 1; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1967 | memcpy(txn->uri, req->buf->p, urilen); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1968 | txn->uri[urilen] = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1969 | |
Willy Tarreau | d79a3b2 | 2012-12-28 09:40:16 +0100 | [diff] [blame] | 1970 | if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT))) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1971 | s->do_log(s); |
| 1972 | } else { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1973 | ha_alert("HTTP logging : out of memory.\n"); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1974 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1975 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1976 | |
Willy Tarreau | 91852eb | 2015-05-01 13:26:00 +0200 | [diff] [blame] | 1977 | /* RFC7230#2.6 has enforced the format of the HTTP version string to be |
| 1978 | * exactly one digit "." one digit. This check may be disabled using |
| 1979 | * option accept-invalid-http-request. |
| 1980 | */ |
| 1981 | if (!(sess->fe->options2 & PR_O2_REQBUG_OK)) { |
| 1982 | if (msg->sl.rq.v_l != 8) { |
| 1983 | msg->err_pos = msg->sl.rq.v; |
| 1984 | goto return_bad_req; |
| 1985 | } |
| 1986 | |
| 1987 | if (req->buf->p[msg->sl.rq.v + 4] != '/' || |
| 1988 | !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 5]) || |
| 1989 | req->buf->p[msg->sl.rq.v + 6] != '.' || |
| 1990 | !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 7])) { |
| 1991 | msg->err_pos = msg->sl.rq.v + 4; |
| 1992 | goto return_bad_req; |
| 1993 | } |
| 1994 | } |
Willy Tarreau | 1331766 | 2015-05-01 13:47:08 +0200 | [diff] [blame] | 1995 | else { |
| 1996 | /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */ |
| 1997 | if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn)) |
| 1998 | goto return_bad_req; |
| 1999 | } |
Willy Tarreau | 91852eb | 2015-05-01 13:26:00 +0200 | [diff] [blame] | 2000 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2001 | /* ... and check if the request is HTTP/1.1 or above */ |
| 2002 | if ((msg->sl.rq.v_l == 8) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2003 | ((req->buf->p[msg->sl.rq.v + 5] > '1') || |
| 2004 | ((req->buf->p[msg->sl.rq.v + 5] == '1') && |
| 2005 | (req->buf->p[msg->sl.rq.v + 7] >= '1')))) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2006 | msg->flags |= HTTP_MSGF_VER_11; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2007 | |
| 2008 | /* "connection" has not been parsed yet */ |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 2009 | 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] | 2010 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2011 | /* if the frontend has "option http-use-proxy-header", we'll check if |
| 2012 | * we have what looks like a proxied connection instead of a connection, |
| 2013 | * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection. |
| 2014 | * Note that this is *not* RFC-compliant, however browsers and proxies |
| 2015 | * happen to do that despite being non-standard :-( |
| 2016 | * We consider that a request not beginning with either '/' or '*' is |
| 2017 | * a proxied connection, which covers both "scheme://location" and |
| 2018 | * CONNECT ip:port. |
| 2019 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2020 | if ((sess->fe->options2 & PR_O2_USE_PXHDR) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2021 | 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] | 2022 | txn->flags |= TX_USE_PX_CONN; |
| 2023 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2024 | /* transfer length unknown*/ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2025 | msg->flags &= ~HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2026 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2027 | /* 5: we may need to capture headers */ |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 2028 | if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap)) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2029 | capture_headers(req->buf->p, &txn->hdr_idx, |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 2030 | s->req_cap, sess->fe->req_cap); |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 2031 | |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 2032 | /* 6: determine the transfer-length according to RFC2616 #4.4, updated |
| 2033 | * by RFC7230#3.3.3 : |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2034 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 2035 | * The length of a message body is determined by one of the following |
| 2036 | * (in order of precedence): |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2037 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 2038 | * 1. Any response to a HEAD request and any response with a 1xx |
| 2039 | * (Informational), 204 (No Content), or 304 (Not Modified) status |
| 2040 | * code is always terminated by the first empty line after the |
| 2041 | * header fields, regardless of the header fields present in the |
| 2042 | * message, and thus cannot contain a message body. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2043 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 2044 | * 2. Any 2xx (Successful) response to a CONNECT request implies that |
| 2045 | * the connection will become a tunnel immediately after the empty |
| 2046 | * line that concludes the header fields. A client MUST ignore any |
| 2047 | * Content-Length or Transfer-Encoding header fields received in |
| 2048 | * such a message. |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2049 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 2050 | * 3. If a Transfer-Encoding header field is present and the chunked |
| 2051 | * transfer coding (Section 4.1) is the final encoding, the message |
| 2052 | * body length is determined by reading and decoding the chunked |
| 2053 | * data until the transfer coding indicates the data is complete. |
| 2054 | * |
| 2055 | * If a Transfer-Encoding header field is present in a response and |
| 2056 | * the chunked transfer coding is not the final encoding, the |
| 2057 | * message body length is determined by reading the connection until |
| 2058 | * it is closed by the server. If a Transfer-Encoding header field |
| 2059 | * is present in a request and the chunked transfer coding is not |
| 2060 | * the final encoding, the message body length cannot be determined |
| 2061 | * reliably; the server MUST respond with the 400 (Bad Request) |
| 2062 | * status code and then close the connection. |
| 2063 | * |
| 2064 | * If a message is received with both a Transfer-Encoding and a |
| 2065 | * Content-Length header field, the Transfer-Encoding overrides the |
| 2066 | * Content-Length. Such a message might indicate an attempt to |
| 2067 | * perform request smuggling (Section 9.5) or response splitting |
| 2068 | * (Section 9.4) and ought to be handled as an error. A sender MUST |
| 2069 | * remove the received Content-Length field prior to forwarding such |
| 2070 | * a message downstream. |
| 2071 | * |
| 2072 | * 4. If a message is received without Transfer-Encoding and with |
| 2073 | * either multiple Content-Length header fields having differing |
| 2074 | * field-values or a single Content-Length header field having an |
| 2075 | * invalid value, then the message framing is invalid and the |
| 2076 | * recipient MUST treat it as an unrecoverable error. If this is a |
| 2077 | * request message, the server MUST respond with a 400 (Bad Request) |
| 2078 | * status code and then close the connection. If this is a response |
| 2079 | * message received by a proxy, the proxy MUST close the connection |
| 2080 | * to the server, discard the received response, and send a 502 (Bad |
| 2081 | * Gateway) response to the client. If this is a response message |
| 2082 | * received by a user agent, the user agent MUST close the |
| 2083 | * connection to the server and discard the received response. |
| 2084 | * |
| 2085 | * 5. If a valid Content-Length header field is present without |
| 2086 | * Transfer-Encoding, its decimal value defines the expected message |
| 2087 | * body length in octets. If the sender closes the connection or |
| 2088 | * the recipient times out before the indicated number of octets are |
| 2089 | * received, the recipient MUST consider the message to be |
| 2090 | * incomplete and close the connection. |
| 2091 | * |
| 2092 | * 6. If this is a request message and none of the above are true, then |
| 2093 | * the message body length is zero (no message body is present). |
| 2094 | * |
| 2095 | * 7. Otherwise, this is a response message without a declared message |
| 2096 | * body length, so the message body length is determined by the |
| 2097 | * number of octets received prior to the server closing the |
| 2098 | * connection. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2099 | */ |
| 2100 | |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2101 | ctx.idx = 0; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2102 | /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */ |
Willy Tarreau | 4979d5c | 2015-05-01 10:06:30 +0200 | [diff] [blame] | 2103 | 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] | 2104 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
Christopher Faulet | be821b9 | 2017-03-30 11:21:53 +0200 | [diff] [blame] | 2105 | msg->flags |= HTTP_MSGF_TE_CHNK; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2106 | else if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | 34dfc60 | 2015-05-01 10:09:49 +0200 | [diff] [blame] | 2107 | /* chunked not last, return badreq */ |
| 2108 | goto return_bad_req; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2109 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2110 | } |
| 2111 | |
Willy Tarreau | 1c91391 | 2015-04-30 10:57:51 +0200 | [diff] [blame] | 2112 | /* Chunked requests must have their content-length removed */ |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2113 | ctx.idx = 0; |
Willy Tarreau | 1c91391 | 2015-04-30 10:57:51 +0200 | [diff] [blame] | 2114 | if (msg->flags & HTTP_MSGF_TE_CHNK) { |
| 2115 | while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) |
| 2116 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 2117 | } |
Willy Tarreau | 34dfc60 | 2015-05-01 10:09:49 +0200 | [diff] [blame] | 2118 | 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] | 2119 | signed long long cl; |
| 2120 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2121 | if (!ctx.vlen) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2122 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2123 | goto return_bad_req; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2124 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2125 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2126 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2127 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2128 | goto return_bad_req; /* parse failure */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2129 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2130 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2131 | if (cl < 0) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2132 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2133 | goto return_bad_req; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2134 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2135 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2136 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2137 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2138 | goto return_bad_req; /* already specified, was different */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2139 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2140 | |
Christopher Faulet | be821b9 | 2017-03-30 11:21:53 +0200 | [diff] [blame] | 2141 | msg->flags |= HTTP_MSGF_CNT_LEN; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 2142 | msg->body_len = msg->chunk_len = cl; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2143 | } |
| 2144 | |
Willy Tarreau | 34dfc60 | 2015-05-01 10:09:49 +0200 | [diff] [blame] | 2145 | /* even bodyless requests have a known length */ |
| 2146 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2147 | |
Willy Tarreau | 179085c | 2014-04-28 16:48:56 +0200 | [diff] [blame] | 2148 | /* Until set to anything else, the connection mode is set as Keep-Alive. It will |
| 2149 | * only change if both the request and the config reference something else. |
| 2150 | * Option httpclose by itself sets tunnel mode where headers are mangled. |
| 2151 | * However, if another mode is set, it will affect it (eg: server-close/ |
| 2152 | * keep-alive + httpclose = close). Note that we avoid to redo the same work |
| 2153 | * if FE and BE have the same settings (common). The method consists in |
| 2154 | * checking if options changed between the two calls (implying that either |
| 2155 | * one is non-null, or one of them is non-null and we are there for the first |
| 2156 | * time. |
| 2157 | */ |
| 2158 | if (!(txn->flags & TX_HDR_CONN_PRS) || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2159 | ((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] | 2160 | http_adjust_conn_mode(s, txn, msg); |
Willy Tarreau | 179085c | 2014-04-28 16:48:56 +0200 | [diff] [blame] | 2161 | |
Willy Tarreau | 9fbe18e | 2015-05-01 22:42:08 +0200 | [diff] [blame] | 2162 | /* we may have to wait for the request's body */ |
| 2163 | if ((s->be->options & PR_O_WREQ_BODY) && |
| 2164 | (msg->body_len || (msg->flags & HTTP_MSGF_TE_CHNK))) |
| 2165 | req->analysers |= AN_REQ_HTTP_BODY; |
| 2166 | |
Willy Tarreau | 83ece46 | 2017-12-21 15:13:09 +0100 | [diff] [blame] | 2167 | /* |
| 2168 | * RFC7234#4: |
| 2169 | * A cache MUST write through requests with methods |
| 2170 | * that are unsafe (Section 4.2.1 of [RFC7231]) to |
| 2171 | * the origin server; i.e., a cache is not allowed |
| 2172 | * to generate a reply to such a request before |
| 2173 | * having forwarded the request and having received |
| 2174 | * a corresponding response. |
| 2175 | * |
| 2176 | * RFC7231#4.2.1: |
| 2177 | * Of the request methods defined by this |
| 2178 | * specification, the GET, HEAD, OPTIONS, and TRACE |
| 2179 | * methods are defined to be safe. |
| 2180 | */ |
| 2181 | if (likely(txn->meth == HTTP_METH_GET || |
| 2182 | txn->meth == HTTP_METH_HEAD || |
| 2183 | txn->meth == HTTP_METH_OPTIONS || |
| 2184 | txn->meth == HTTP_METH_TRACE)) |
| 2185 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
| 2186 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2187 | /* end of job, return OK */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 2188 | req->analysers &= ~an_bit; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2189 | req->analyse_exp = TICK_ETERNITY; |
| 2190 | return 1; |
| 2191 | |
| 2192 | return_bad_req: |
| 2193 | /* We centralize bad requests processing here */ |
| 2194 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 2195 | /* we detected a parsing error. We want to archive this request |
| 2196 | * in the dedicated proxy area for later troubleshooting. |
| 2197 | */ |
Emeric Brun | 8c1aaa2 | 2017-06-15 11:30:06 +0200 | [diff] [blame] | 2198 | 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] | 2199 | } |
| 2200 | |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 2201 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2202 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 2203 | txn->status = 400; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 2204 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2205 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 2206 | HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 2207 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 2208 | HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1); |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2209 | |
| 2210 | return_prx_cond: |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2211 | if (!(s->flags & SF_ERR_MASK)) |
| 2212 | s->flags |= SF_ERR_PRXCOND; |
| 2213 | if (!(s->flags & SF_FINST_MASK)) |
| 2214 | s->flags |= SF_FINST_R; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2215 | |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 2216 | req->analysers &= AN_REQ_FLT_END; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2217 | req->analyse_exp = TICK_ETERNITY; |
| 2218 | return 0; |
| 2219 | } |
| 2220 | |
Willy Tarreau | 4f8a83c | 2012-06-04 00:26:23 +0200 | [diff] [blame] | 2221 | |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 2222 | /* This function prepares an applet to handle the stats. It can deal with the |
| 2223 | * "100-continue" expectation, check that admin rules are met for POST requests, |
| 2224 | * and program a response message if something was unexpected. It cannot fail |
| 2225 | * 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] | 2226 | * 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] | 2227 | * 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] | 2228 | * is expected to have already assigned an appctx to the stream. |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 2229 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2230 | int http_handle_stats(struct stream *s, struct channel *req) |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 2231 | { |
| 2232 | struct stats_admin_rule *stats_admin_rule; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 2233 | struct stream_interface *si = &s->si[1]; |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 2234 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 2235 | struct http_txn *txn = s->txn; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 2236 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2237 | struct uri_auth *uri_auth = s->be->uri_auth; |
| 2238 | const char *uri, *h, *lookup; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2239 | struct appctx *appctx; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2240 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2241 | appctx = si_appctx(si); |
| 2242 | memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats)); |
| 2243 | appctx->st1 = appctx->st2 = 0; |
| 2244 | appctx->ctx.stats.st_code = STAT_STATUS_INIT; |
| 2245 | appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 2246 | 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] | 2247 | appctx->ctx.stats.flags |= STAT_CHUNKED; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2248 | |
| 2249 | uri = msg->chn->buf->p + msg->sl.rq.u; |
| 2250 | lookup = uri + uri_auth->uri_len; |
| 2251 | |
| 2252 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) { |
| 2253 | if (memcmp(h, ";up", 3) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2254 | appctx->ctx.stats.flags |= STAT_HIDE_DOWN; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2255 | break; |
| 2256 | } |
| 2257 | } |
| 2258 | |
| 2259 | if (uri_auth->refresh) { |
| 2260 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) { |
| 2261 | if (memcmp(h, ";norefresh", 10) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2262 | appctx->ctx.stats.flags |= STAT_NO_REFRESH; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2263 | break; |
| 2264 | } |
| 2265 | } |
| 2266 | } |
| 2267 | |
| 2268 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) { |
| 2269 | if (memcmp(h, ";csv", 4) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2270 | appctx->ctx.stats.flags &= ~STAT_FMT_HTML; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2271 | break; |
| 2272 | } |
| 2273 | } |
| 2274 | |
Willy Tarreau | 1e62df9 | 2016-01-11 18:57:53 +0100 | [diff] [blame] | 2275 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 6; h++) { |
| 2276 | if (memcmp(h, ";typed", 6) == 0) { |
| 2277 | appctx->ctx.stats.flags &= ~STAT_FMT_HTML; |
| 2278 | appctx->ctx.stats.flags |= STAT_FMT_TYPED; |
| 2279 | break; |
| 2280 | } |
| 2281 | } |
| 2282 | |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2283 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) { |
| 2284 | if (memcmp(h, ";st=", 4) == 0) { |
| 2285 | int i; |
| 2286 | h += 4; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2287 | appctx->ctx.stats.st_code = STAT_STATUS_UNKN; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2288 | for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) { |
| 2289 | if (strncmp(stat_status_codes[i], h, 4) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2290 | appctx->ctx.stats.st_code = i; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2291 | break; |
| 2292 | } |
| 2293 | } |
| 2294 | break; |
| 2295 | } |
| 2296 | } |
| 2297 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2298 | appctx->ctx.stats.scope_str = 0; |
| 2299 | appctx->ctx.stats.scope_len = 0; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2300 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) { |
| 2301 | if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) { |
| 2302 | int itx = 0; |
| 2303 | const char *h2; |
| 2304 | char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1]; |
| 2305 | const char *err; |
| 2306 | |
| 2307 | h += strlen(STAT_SCOPE_INPUT_NAME) + 1; |
| 2308 | h2 = h; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2309 | appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2310 | while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') { |
| 2311 | itx++; |
| 2312 | h++; |
| 2313 | } |
| 2314 | |
| 2315 | if (itx > STAT_SCOPE_TXT_MAXLEN) |
| 2316 | itx = STAT_SCOPE_TXT_MAXLEN; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2317 | appctx->ctx.stats.scope_len = itx; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2318 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2319 | /* 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] | 2320 | memcpy(scope_txt, h2, itx); |
| 2321 | scope_txt[itx] = '\0'; |
| 2322 | err = invalid_char(scope_txt); |
| 2323 | if (err) { |
| 2324 | /* bad char in search text => clear scope */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2325 | appctx->ctx.stats.scope_str = 0; |
| 2326 | appctx->ctx.stats.scope_len = 0; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2327 | } |
| 2328 | break; |
| 2329 | } |
| 2330 | } |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 2331 | |
| 2332 | /* now check whether we have some admin rules for this request */ |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2333 | list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) { |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 2334 | int ret = 1; |
| 2335 | |
| 2336 | if (stats_admin_rule->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 2337 | 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] | 2338 | ret = acl_pass(ret); |
| 2339 | if (stats_admin_rule->cond->pol == ACL_COND_UNLESS) |
| 2340 | ret = !ret; |
| 2341 | } |
| 2342 | |
| 2343 | if (ret) { |
| 2344 | /* no rule, or the rule matches */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2345 | appctx->ctx.stats.flags |= STAT_ADMIN; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 2346 | break; |
| 2347 | } |
| 2348 | } |
| 2349 | |
| 2350 | /* Was the status page requested with a POST ? */ |
Willy Tarreau | b8cdf52 | 2015-05-29 01:09:15 +0200 | [diff] [blame] | 2351 | if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2352 | if (appctx->ctx.stats.flags & STAT_ADMIN) { |
Willy Tarreau | cfe7fdd | 2014-04-26 22:08:32 +0200 | [diff] [blame] | 2353 | /* we'll need the request body, possibly after sending 100-continue */ |
Willy Tarreau | b8cdf52 | 2015-05-29 01:09:15 +0200 | [diff] [blame] | 2354 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) |
| 2355 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2356 | appctx->st0 = STAT_HTTP_POST; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 2357 | } |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 2358 | else { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2359 | appctx->ctx.stats.st_code = STAT_STATUS_DENY; |
| 2360 | appctx->st0 = STAT_HTTP_LAST; |
de Lafond Guillaume | 88c278f | 2013-04-15 19:27:10 +0200 | [diff] [blame] | 2361 | } |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 2362 | } |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 2363 | else { |
| 2364 | /* So it was another method (GET/HEAD) */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2365 | appctx->st0 = STAT_HTTP_HEAD; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 2366 | } |
| 2367 | |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 2368 | s->task->nice = -32; /* small boost for HTTP statistics */ |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 2369 | return 1; |
| 2370 | } |
| 2371 | |
Lukas Tribus | 67db8df | 2013-06-23 17:37:13 +0200 | [diff] [blame] | 2372 | /* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets |
| 2373 | * (as per RFC3260 #4 and BCP37 #4.2 and #5.2). |
| 2374 | */ |
Vincent Bernat | 6e61589 | 2016-05-18 16:17:44 +0200 | [diff] [blame] | 2375 | 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] | 2376 | { |
| 2377 | #ifdef IP_TOS |
Vincent Bernat | 6e61589 | 2016-05-18 16:17:44 +0200 | [diff] [blame] | 2378 | if (from->ss_family == AF_INET) |
Lukas Tribus | 67db8df | 2013-06-23 17:37:13 +0200 | [diff] [blame] | 2379 | setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)); |
| 2380 | #endif |
| 2381 | #ifdef IPV6_TCLASS |
Vincent Bernat | 6e61589 | 2016-05-18 16:17:44 +0200 | [diff] [blame] | 2382 | if (from->ss_family == AF_INET6) { |
| 2383 | if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)from)->sin6_addr)) |
Lukas Tribus | 67db8df | 2013-06-23 17:37:13 +0200 | [diff] [blame] | 2384 | /* v4-mapped addresses need IP_TOS */ |
| 2385 | setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)); |
| 2386 | else |
| 2387 | setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos)); |
| 2388 | } |
| 2389 | #endif |
| 2390 | } |
| 2391 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2392 | int http_transform_header_str(struct stream* s, struct http_msg *msg, |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 2393 | const char* name, unsigned int name_len, |
| 2394 | const char *str, struct my_regex *re, |
| 2395 | int action) |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 2396 | { |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 2397 | struct hdr_ctx ctx; |
| 2398 | char *buf = msg->chn->buf->p; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 2399 | struct hdr_idx *idx = &s->txn->hdr_idx; |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 2400 | int (*http_find_hdr_func)(const char *name, int len, char *sol, |
| 2401 | struct hdr_idx *idx, struct hdr_ctx *ctx); |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 2402 | struct chunk *output = get_trash_chunk(); |
| 2403 | |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 2404 | ctx.idx = 0; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 2405 | |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 2406 | /* Choose the header browsing function. */ |
| 2407 | switch (action) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2408 | case ACT_HTTP_REPLACE_VAL: |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 2409 | http_find_hdr_func = http_find_header2; |
| 2410 | break; |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2411 | case ACT_HTTP_REPLACE_HDR: |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 2412 | http_find_hdr_func = http_find_full_header2; |
| 2413 | break; |
| 2414 | default: /* impossible */ |
| 2415 | return -1; |
| 2416 | } |
| 2417 | |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 2418 | while (http_find_hdr_func(name, name_len, buf, idx, &ctx)) { |
| 2419 | struct hdr_idx_elem *hdr = idx->v + ctx.idx; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 2420 | int delta; |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 2421 | char *val = ctx.line + ctx.val; |
| 2422 | char* val_end = val + ctx.vlen; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 2423 | |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 2424 | if (!regex_exec_match2(re, val, val_end-val, MAX_MATCH, pmatch, 0)) |
| 2425 | continue; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 2426 | |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 2427 | output->len = exp_replace(output->str, output->size, val, str, pmatch); |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 2428 | if (output->len == -1) |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 2429 | return -1; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 2430 | |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 2431 | 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] | 2432 | |
| 2433 | hdr->len += delta; |
| 2434 | http_msg_move_end(msg, delta); |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 2435 | |
| 2436 | /* Adjust the length of the current value of the index. */ |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 2437 | ctx.vlen += delta; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 2438 | } |
| 2439 | |
| 2440 | return 0; |
| 2441 | } |
| 2442 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2443 | static int http_transform_header(struct stream* s, struct http_msg *msg, |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 2444 | const char* name, unsigned int name_len, |
| 2445 | struct list *fmt, struct my_regex *re, |
| 2446 | int action) |
| 2447 | { |
Christopher Faulet | 07a0fec | 2017-02-08 12:17:07 +0100 | [diff] [blame] | 2448 | struct chunk *replace; |
| 2449 | int ret = -1; |
| 2450 | |
| 2451 | replace = alloc_trash_chunk(); |
| 2452 | if (!replace) |
| 2453 | goto leave; |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 2454 | |
| 2455 | replace->len = build_logline(s, replace->str, replace->size, fmt); |
| 2456 | if (replace->len >= replace->size - 1) |
Christopher Faulet | 07a0fec | 2017-02-08 12:17:07 +0100 | [diff] [blame] | 2457 | goto leave; |
| 2458 | |
| 2459 | 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] | 2460 | |
Christopher Faulet | 07a0fec | 2017-02-08 12:17:07 +0100 | [diff] [blame] | 2461 | leave: |
| 2462 | free_trash_chunk(replace); |
| 2463 | return ret; |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 2464 | } |
| 2465 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2466 | /* Executes the http-request rules <rules> for stream <s>, proxy <px> and |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 2467 | * transaction <txn>. Returns the verdict of the first rule that prevents |
| 2468 | * further processing of the request (auth, deny, ...), and defaults to |
| 2469 | * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or |
| 2470 | * 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] | 2471 | * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL |
| 2472 | * and a deny/tarpit rule is matched, it will be filled with this rule's deny |
| 2473 | * status. |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2474 | */ |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 2475 | enum rule_result |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 2476 | 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] | 2477 | { |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 2478 | struct session *sess = strm_sess(s); |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 2479 | struct http_txn *txn = s->txn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2480 | struct connection *cli_conn; |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 2481 | struct act_rule *rule; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 2482 | struct hdr_ctx ctx; |
Willy Tarreau | ae3c010 | 2014-04-28 23:22:08 +0200 | [diff] [blame] | 2483 | const char *auth_realm; |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 2484 | int act_flags = 0; |
Thierry Fournier | 4b788f7 | 2016-06-01 13:35:36 +0200 | [diff] [blame] | 2485 | int len; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2486 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 2487 | /* If "the current_rule_list" match the executed rule list, we are in |
| 2488 | * resume condition. If a resume is needed it is always in the action |
| 2489 | * and never in the ACL or converters. In this case, we initialise the |
| 2490 | * current rule, and go to the action execution point. |
| 2491 | */ |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 2492 | if (s->current_rule) { |
| 2493 | rule = s->current_rule; |
| 2494 | s->current_rule = NULL; |
| 2495 | if (s->current_rule_list == rules) |
| 2496 | goto resume_execution; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 2497 | } |
| 2498 | s->current_rule_list = rules; |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 2499 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2500 | list_for_each_entry(rule, rules, list) { |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2501 | |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 2502 | /* check optional condition */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2503 | if (rule->cond) { |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 2504 | int ret; |
| 2505 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 2506 | 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] | 2507 | ret = acl_pass(ret); |
| 2508 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2509 | if (rule->cond->pol == ACL_COND_UNLESS) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2510 | ret = !ret; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 2511 | |
| 2512 | if (!ret) /* condition not matched */ |
| 2513 | continue; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2514 | } |
| 2515 | |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 2516 | act_flags |= ACT_FLAG_FIRST; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 2517 | resume_execution: |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 2518 | switch (rule->action) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2519 | case ACT_ACTION_ALLOW: |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 2520 | return HTTP_RULE_RES_STOP; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 2521 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2522 | case ACT_ACTION_DENY: |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 2523 | if (deny_status) |
| 2524 | *deny_status = rule->deny_status; |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 2525 | return HTTP_RULE_RES_DENY; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 2526 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2527 | case ACT_HTTP_REQ_TARPIT: |
Willy Tarreau | ccbcc37 | 2012-12-27 12:37:57 +0100 | [diff] [blame] | 2528 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 2529 | if (deny_status) |
| 2530 | *deny_status = rule->deny_status; |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 2531 | return HTTP_RULE_RES_DENY; |
Willy Tarreau | ccbcc37 | 2012-12-27 12:37:57 +0100 | [diff] [blame] | 2532 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2533 | case ACT_HTTP_REQ_AUTH: |
Willy Tarreau | ae3c010 | 2014-04-28 23:22:08 +0200 | [diff] [blame] | 2534 | /* Auth might be performed on regular http-req rules as well as on stats */ |
| 2535 | auth_realm = rule->arg.auth.realm; |
| 2536 | if (!auth_realm) { |
| 2537 | if (px->uri_auth && rules == &px->uri_auth->http_req_rules) |
| 2538 | auth_realm = STATS_DEFAULT_REALM; |
| 2539 | else |
| 2540 | auth_realm = px->id; |
| 2541 | } |
| 2542 | /* send 401/407 depending on whether we use a proxy or not. We still |
| 2543 | * count one error, because normal browsing won't significantly |
| 2544 | * increase the counter but brute force attempts will. |
| 2545 | */ |
| 2546 | chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm); |
| 2547 | txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401; |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 2548 | http_reply_and_close(s, txn->status, &trash); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2549 | stream_inc_http_err_ctr(s); |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 2550 | return HTTP_RULE_RES_ABRT; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 2551 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2552 | case ACT_HTTP_REDIR: |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 2553 | if (!http_apply_redirect_rule(rule->arg.redir, s, txn)) |
| 2554 | return HTTP_RULE_RES_BADREQ; |
| 2555 | return HTTP_RULE_RES_DONE; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 2556 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2557 | case ACT_HTTP_SET_NICE: |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 2558 | s->task->nice = rule->arg.nice; |
| 2559 | break; |
| 2560 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2561 | case ACT_HTTP_SET_TOS: |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 2562 | if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 2563 | 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] | 2564 | break; |
| 2565 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2566 | case ACT_HTTP_SET_MARK: |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 2567 | #ifdef SO_MARK |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 2568 | if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 2569 | 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] | 2570 | #endif |
| 2571 | break; |
| 2572 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2573 | case ACT_HTTP_SET_LOGL: |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 2574 | s->logs.level = rule->arg.loglevel; |
| 2575 | break; |
| 2576 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2577 | case ACT_HTTP_REPLACE_HDR: |
| 2578 | case ACT_HTTP_REPLACE_VAL: |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 2579 | if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name, |
| 2580 | rule->arg.hdr_add.name_len, |
| 2581 | &rule->arg.hdr_add.fmt, |
| 2582 | &rule->arg.hdr_add.re, rule->action)) |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 2583 | return HTTP_RULE_RES_BADREQ; |
| 2584 | break; |
| 2585 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2586 | case ACT_HTTP_DEL_HDR: |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 2587 | ctx.idx = 0; |
| 2588 | /* remove all occurrences of the header */ |
| 2589 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 2590 | txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 2591 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 2592 | } |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 2593 | break; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 2594 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2595 | case ACT_HTTP_SET_HDR: |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2596 | case ACT_HTTP_ADD_HDR: { |
Thierry Fournier | 4b788f7 | 2016-06-01 13:35:36 +0200 | [diff] [blame] | 2597 | /* The scope of the trash buffer must be limited to this function. The |
| 2598 | * build_logline() function can execute a lot of other function which |
| 2599 | * can use the trash buffer. So for limiting the scope of this global |
| 2600 | * buffer, we build first the header value using build_logline, and |
| 2601 | * after we store the header name. |
| 2602 | */ |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2603 | struct chunk *replace; |
| 2604 | |
| 2605 | replace = alloc_trash_chunk(); |
| 2606 | if (!replace) |
| 2607 | return HTTP_RULE_RES_BADREQ; |
| 2608 | |
Thierry Fournier | 4b788f7 | 2016-06-01 13:35:36 +0200 | [diff] [blame] | 2609 | len = rule->arg.hdr_add.name_len + 2, |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2610 | len += build_logline(s, replace->str + len, replace->size - len, &rule->arg.hdr_add.fmt); |
| 2611 | memcpy(replace->str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len); |
| 2612 | replace->str[rule->arg.hdr_add.name_len] = ':'; |
| 2613 | replace->str[rule->arg.hdr_add.name_len + 1] = ' '; |
| 2614 | replace->len = len; |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 2615 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2616 | if (rule->action == ACT_HTTP_SET_HDR) { |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 2617 | /* remove all occurrences of the header */ |
| 2618 | ctx.idx = 0; |
| 2619 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 2620 | txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 2621 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
| 2622 | } |
| 2623 | } |
| 2624 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2625 | http_header_add_tail2(&txn->req, &txn->hdr_idx, replace->str, replace->len); |
| 2626 | |
| 2627 | free_trash_chunk(replace); |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 2628 | break; |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2629 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2630 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2631 | case ACT_HTTP_DEL_ACL: |
| 2632 | case ACT_HTTP_DEL_MAP: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2633 | struct pat_ref *ref; |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2634 | struct chunk *key; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2635 | |
| 2636 | /* collect reference */ |
| 2637 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 2638 | if (!ref) |
| 2639 | continue; |
| 2640 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2641 | /* allocate key */ |
| 2642 | key = alloc_trash_chunk(); |
| 2643 | if (!key) |
| 2644 | return HTTP_RULE_RES_BADREQ; |
| 2645 | |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2646 | /* collect key */ |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2647 | key->len = build_logline(s, key->str, key->size, &rule->arg.map.key); |
| 2648 | key->str[key->len] = '\0'; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2649 | |
| 2650 | /* perform update */ |
| 2651 | /* returned code: 1=ok, 0=ko */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2652 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2653 | pat_ref_delete(ref, key->str); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2654 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2655 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2656 | free_trash_chunk(key); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2657 | break; |
| 2658 | } |
| 2659 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2660 | case ACT_HTTP_ADD_ACL: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2661 | struct pat_ref *ref; |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2662 | struct chunk *key; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2663 | |
| 2664 | /* collect reference */ |
| 2665 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 2666 | if (!ref) |
| 2667 | continue; |
| 2668 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2669 | /* allocate key */ |
| 2670 | key = alloc_trash_chunk(); |
| 2671 | if (!key) |
| 2672 | return HTTP_RULE_RES_BADREQ; |
| 2673 | |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2674 | /* collect key */ |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2675 | key->len = build_logline(s, key->str, key->size, &rule->arg.map.key); |
| 2676 | key->str[key->len] = '\0'; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2677 | |
| 2678 | /* perform update */ |
| 2679 | /* add entry only if it does not already exist */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2680 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2681 | if (pat_ref_find_elt(ref, key->str) == NULL) |
| 2682 | pat_ref_add(ref, key->str, NULL, NULL); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2683 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2684 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2685 | free_trash_chunk(key); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2686 | break; |
| 2687 | } |
| 2688 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2689 | case ACT_HTTP_SET_MAP: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2690 | struct pat_ref *ref; |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2691 | struct chunk *key, *value; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2692 | |
| 2693 | /* collect reference */ |
| 2694 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 2695 | if (!ref) |
| 2696 | continue; |
| 2697 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2698 | /* allocate key */ |
| 2699 | key = alloc_trash_chunk(); |
| 2700 | if (!key) |
| 2701 | return HTTP_RULE_RES_BADREQ; |
| 2702 | |
| 2703 | /* allocate value */ |
| 2704 | value = alloc_trash_chunk(); |
| 2705 | if (!value) { |
| 2706 | free_trash_chunk(key); |
| 2707 | return HTTP_RULE_RES_BADREQ; |
| 2708 | } |
| 2709 | |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2710 | /* collect key */ |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2711 | key->len = build_logline(s, key->str, key->size, &rule->arg.map.key); |
| 2712 | key->str[key->len] = '\0'; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2713 | |
| 2714 | /* collect value */ |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2715 | value->len = build_logline(s, value->str, value->size, &rule->arg.map.value); |
| 2716 | value->str[value->len] = '\0'; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2717 | |
| 2718 | /* perform update */ |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2719 | if (pat_ref_find_elt(ref, key->str) != NULL) |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2720 | /* update entry if it exists */ |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2721 | pat_ref_set(ref, key->str, value->str, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2722 | else |
| 2723 | /* insert a new entry */ |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2724 | pat_ref_add(ref, key->str, value->str, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2725 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2726 | free_trash_chunk(key); |
| 2727 | free_trash_chunk(value); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2728 | break; |
| 2729 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 2730 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 2731 | case ACT_CUSTOM: |
Willy Tarreau | 7aa15b0 | 2017-12-20 16:56:50 +0100 | [diff] [blame] | 2732 | if ((s->req.flags & CF_READ_ERROR) || |
| 2733 | ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) && |
| 2734 | !(s->si[0].flags & SI_FL_CLEAN_ABRT) && |
| 2735 | (px->options & PR_O_ABRT_CLOSE))) |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 2736 | act_flags |= ACT_FLAG_FINAL; |
Willy Tarreau | 3945868 | 2015-09-27 10:33:15 +0200 | [diff] [blame] | 2737 | |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 2738 | switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) { |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 2739 | case ACT_RET_ERR: |
| 2740 | case ACT_RET_CONT: |
| 2741 | break; |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 2742 | case ACT_RET_STOP: |
| 2743 | return HTTP_RULE_RES_DONE; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 2744 | case ACT_RET_YIELD: |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 2745 | s->current_rule = rule; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 2746 | return HTTP_RULE_RES_YIELD; |
| 2747 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 2748 | break; |
| 2749 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2750 | case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX: |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 2751 | /* Note: only the first valid tracking parameter of each |
| 2752 | * applies. |
| 2753 | */ |
| 2754 | |
Christopher Faulet | 4fce0d8 | 2017-09-18 11:57:31 +0200 | [diff] [blame] | 2755 | if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) { |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 2756 | struct stktable *t; |
| 2757 | struct stksess *ts; |
| 2758 | struct stktable_key *key; |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 2759 | void *ptr1, *ptr2; |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 2760 | |
Thierry FOURNIER | 5ec63e0 | 2015-08-04 09:09:48 +0200 | [diff] [blame] | 2761 | t = rule->arg.trk_ctr.table.t; |
| 2762 | 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] | 2763 | |
| 2764 | if (key && (ts = stktable_get_entry(t, key))) { |
Christopher Faulet | 4fce0d8 | 2017-09-18 11:57:31 +0200 | [diff] [blame] | 2765 | stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts); |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 2766 | |
| 2767 | /* 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] | 2768 | ptr1 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT); |
| 2769 | ptr2 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE); |
| 2770 | if (ptr1 || ptr2) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2771 | HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock); |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 2772 | |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 2773 | if (ptr1) |
| 2774 | stktable_data_cast(ptr1, http_req_cnt)++; |
| 2775 | |
| 2776 | if (ptr2) |
| 2777 | update_freq_ctr_period(&stktable_data_cast(ptr2, http_req_rate), |
| 2778 | t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1); |
| 2779 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2780 | HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock); |
Emeric Brun | 0fed0b0 | 2017-11-29 16:15:07 +0100 | [diff] [blame] | 2781 | |
| 2782 | /* If data was modified, we need to touch to re-schedule sync */ |
| 2783 | stktable_touch_local(t, ts, 0); |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 2784 | } |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 2785 | |
Christopher Faulet | 4fce0d8 | 2017-09-18 11:57:31 +0200 | [diff] [blame] | 2786 | stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT); |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 2787 | if (sess->fe != s->be) |
Christopher Faulet | 4fce0d8 | 2017-09-18 11:57:31 +0200 | [diff] [blame] | 2788 | stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND); |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 2789 | } |
| 2790 | } |
Adis Nezirovic | 2fbcafc | 2015-07-06 15:44:30 +0200 | [diff] [blame] | 2791 | break; |
| 2792 | |
Thierry FOURNIER | 22e4901 | 2015-08-05 19:13:48 +0200 | [diff] [blame] | 2793 | /* other flags exists, but normaly, they never be matched. */ |
| 2794 | default: |
| 2795 | break; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2796 | } |
| 2797 | } |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 2798 | |
| 2799 | /* we reached the end of the rules, nothing to report */ |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 2800 | return HTTP_RULE_RES_CONT; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2801 | } |
| 2802 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2803 | |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 2804 | /* Executes the http-response rules <rules> for stream <s> and proxy <px>. It |
| 2805 | * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP, |
| 2806 | * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT |
| 2807 | * is returned, the process can continue the evaluation of next rule list. If |
| 2808 | * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ |
| 2809 | * is returned, it means the operation could not be processed and a server error |
| 2810 | * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a |
| 2811 | * deny rule. If *YIELD is returned, the caller must call again the function |
| 2812 | * with the same context. |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2813 | */ |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 2814 | static enum rule_result |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 2815 | 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] | 2816 | { |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 2817 | struct session *sess = strm_sess(s); |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 2818 | struct http_txn *txn = s->txn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2819 | struct connection *cli_conn; |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 2820 | struct act_rule *rule; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2821 | struct hdr_ctx ctx; |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 2822 | int act_flags = 0; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2823 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 2824 | /* If "the current_rule_list" match the executed rule list, we are in |
| 2825 | * resume condition. If a resume is needed it is always in the action |
| 2826 | * and never in the ACL or converters. In this case, we initialise the |
| 2827 | * current rule, and go to the action execution point. |
| 2828 | */ |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 2829 | if (s->current_rule) { |
| 2830 | rule = s->current_rule; |
| 2831 | s->current_rule = NULL; |
| 2832 | if (s->current_rule_list == rules) |
| 2833 | goto resume_execution; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 2834 | } |
| 2835 | s->current_rule_list = rules; |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 2836 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2837 | list_for_each_entry(rule, rules, list) { |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2838 | |
| 2839 | /* check optional condition */ |
| 2840 | if (rule->cond) { |
| 2841 | int ret; |
| 2842 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 2843 | 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] | 2844 | ret = acl_pass(ret); |
| 2845 | |
| 2846 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 2847 | ret = !ret; |
| 2848 | |
| 2849 | if (!ret) /* condition not matched */ |
| 2850 | continue; |
| 2851 | } |
| 2852 | |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 2853 | act_flags |= ACT_FLAG_FIRST; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 2854 | resume_execution: |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2855 | switch (rule->action) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2856 | case ACT_ACTION_ALLOW: |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 2857 | return HTTP_RULE_RES_STOP; /* "allow" rules are OK */ |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2858 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2859 | case ACT_ACTION_DENY: |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2860 | txn->flags |= TX_SVDENY; |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 2861 | return HTTP_RULE_RES_STOP; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2862 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2863 | case ACT_HTTP_SET_NICE: |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 2864 | s->task->nice = rule->arg.nice; |
| 2865 | break; |
| 2866 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2867 | case ACT_HTTP_SET_TOS: |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 2868 | if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 2869 | 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] | 2870 | break; |
| 2871 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2872 | case ACT_HTTP_SET_MARK: |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 2873 | #ifdef SO_MARK |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 2874 | if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 2875 | 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] | 2876 | #endif |
| 2877 | break; |
| 2878 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2879 | case ACT_HTTP_SET_LOGL: |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 2880 | s->logs.level = rule->arg.loglevel; |
| 2881 | break; |
| 2882 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2883 | case ACT_HTTP_REPLACE_HDR: |
| 2884 | case ACT_HTTP_REPLACE_VAL: |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 2885 | if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name, |
| 2886 | rule->arg.hdr_add.name_len, |
| 2887 | &rule->arg.hdr_add.fmt, |
| 2888 | &rule->arg.hdr_add.re, rule->action)) |
Christopher Faulet | cdade94 | 2017-02-08 12:41:31 +0100 | [diff] [blame] | 2889 | return HTTP_RULE_RES_BADREQ; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 2890 | break; |
| 2891 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2892 | case ACT_HTTP_DEL_HDR: |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2893 | ctx.idx = 0; |
| 2894 | /* remove all occurrences of the header */ |
| 2895 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 2896 | txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 2897 | http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx); |
| 2898 | } |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 2899 | break; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2900 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2901 | case ACT_HTTP_SET_HDR: |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2902 | case ACT_HTTP_ADD_HDR: { |
| 2903 | struct chunk *replace; |
| 2904 | |
| 2905 | replace = alloc_trash_chunk(); |
| 2906 | if (!replace) |
| 2907 | return HTTP_RULE_RES_BADREQ; |
| 2908 | |
| 2909 | chunk_printf(replace, "%s: ", rule->arg.hdr_add.name); |
| 2910 | memcpy(replace->str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len); |
| 2911 | replace->len = rule->arg.hdr_add.name_len; |
| 2912 | replace->str[replace->len++] = ':'; |
| 2913 | replace->str[replace->len++] = ' '; |
| 2914 | replace->len += build_logline(s, replace->str + replace->len, replace->size - replace->len, |
| 2915 | &rule->arg.hdr_add.fmt); |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 2916 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2917 | if (rule->action == ACT_HTTP_SET_HDR) { |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 2918 | /* remove all occurrences of the header */ |
| 2919 | ctx.idx = 0; |
| 2920 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 2921 | txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 2922 | http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx); |
| 2923 | } |
| 2924 | } |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2925 | http_header_add_tail2(&txn->rsp, &txn->hdr_idx, replace->str, replace->len); |
| 2926 | |
| 2927 | free_trash_chunk(replace); |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2928 | break; |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2929 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2930 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2931 | case ACT_HTTP_DEL_ACL: |
| 2932 | case ACT_HTTP_DEL_MAP: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2933 | struct pat_ref *ref; |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2934 | struct chunk *key; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2935 | |
| 2936 | /* collect reference */ |
| 2937 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 2938 | if (!ref) |
| 2939 | continue; |
| 2940 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2941 | /* allocate key */ |
| 2942 | key = alloc_trash_chunk(); |
| 2943 | if (!key) |
| 2944 | return HTTP_RULE_RES_BADREQ; |
| 2945 | |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2946 | /* collect key */ |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2947 | key->len = build_logline(s, key->str, key->size, &rule->arg.map.key); |
| 2948 | key->str[key->len] = '\0'; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2949 | |
| 2950 | /* perform update */ |
| 2951 | /* returned code: 1=ok, 0=ko */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2952 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2953 | pat_ref_delete(ref, key->str); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2954 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2955 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2956 | free_trash_chunk(key); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2957 | break; |
| 2958 | } |
| 2959 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2960 | case ACT_HTTP_ADD_ACL: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2961 | struct pat_ref *ref; |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2962 | struct chunk *key; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2963 | |
| 2964 | /* collect reference */ |
| 2965 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 2966 | if (!ref) |
| 2967 | continue; |
| 2968 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2969 | /* allocate key */ |
| 2970 | key = alloc_trash_chunk(); |
| 2971 | if (!key) |
| 2972 | return HTTP_RULE_RES_BADREQ; |
| 2973 | |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2974 | /* collect key */ |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2975 | key->len = build_logline(s, key->str, key->size, &rule->arg.map.key); |
| 2976 | key->str[key->len] = '\0'; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2977 | |
| 2978 | /* perform update */ |
| 2979 | /* check if the entry already exists */ |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2980 | if (pat_ref_find_elt(ref, key->str) == NULL) |
| 2981 | pat_ref_add(ref, key->str, NULL, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2982 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2983 | free_trash_chunk(key); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2984 | break; |
| 2985 | } |
| 2986 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2987 | case ACT_HTTP_SET_MAP: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2988 | struct pat_ref *ref; |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2989 | struct chunk *key, *value; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2990 | |
| 2991 | /* collect reference */ |
| 2992 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 2993 | if (!ref) |
| 2994 | continue; |
| 2995 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2996 | /* allocate key */ |
| 2997 | key = alloc_trash_chunk(); |
| 2998 | if (!key) |
| 2999 | return HTTP_RULE_RES_BADREQ; |
| 3000 | |
| 3001 | /* allocate value */ |
| 3002 | value = alloc_trash_chunk(); |
| 3003 | if (!value) { |
| 3004 | free_trash_chunk(key); |
| 3005 | return HTTP_RULE_RES_BADREQ; |
| 3006 | } |
| 3007 | |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3008 | /* collect key */ |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 3009 | key->len = build_logline(s, key->str, key->size, &rule->arg.map.key); |
| 3010 | key->str[key->len] = '\0'; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3011 | |
| 3012 | /* collect value */ |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 3013 | value->len = build_logline(s, value->str, value->size, &rule->arg.map.value); |
| 3014 | value->str[value->len] = '\0'; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3015 | |
| 3016 | /* perform update */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 3017 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 3018 | if (pat_ref_find_elt(ref, key->str) != NULL) |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3019 | /* update entry if it exists */ |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 3020 | pat_ref_set(ref, key->str, value->str, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3021 | else |
| 3022 | /* insert a new entry */ |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 3023 | pat_ref_add(ref, key->str, value->str, NULL); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 3024 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 3025 | free_trash_chunk(key); |
| 3026 | free_trash_chunk(value); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3027 | break; |
| 3028 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 3029 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3030 | case ACT_HTTP_REDIR: |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 3031 | if (!http_apply_redirect_rule(rule->arg.redir, s, txn)) |
| 3032 | return HTTP_RULE_RES_BADREQ; |
| 3033 | return HTTP_RULE_RES_DONE; |
| 3034 | |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 3035 | case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX: |
| 3036 | /* Note: only the first valid tracking parameter of each |
| 3037 | * applies. |
| 3038 | */ |
| 3039 | |
Christopher Faulet | 4fce0d8 | 2017-09-18 11:57:31 +0200 | [diff] [blame] | 3040 | if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) { |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 3041 | struct stktable *t; |
| 3042 | struct stksess *ts; |
| 3043 | struct stktable_key *key; |
| 3044 | void *ptr; |
| 3045 | |
| 3046 | t = rule->arg.trk_ctr.table.t; |
| 3047 | key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_RES | SMP_OPT_FINAL, rule->arg.trk_ctr.expr, NULL); |
| 3048 | |
| 3049 | if (key && (ts = stktable_get_entry(t, key))) { |
Christopher Faulet | 4fce0d8 | 2017-09-18 11:57:31 +0200 | [diff] [blame] | 3050 | stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts); |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 3051 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 3052 | HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock); |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 3053 | |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 3054 | /* let's count a new HTTP request as it's the first time we do it */ |
| 3055 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT); |
| 3056 | if (ptr) |
| 3057 | stktable_data_cast(ptr, http_req_cnt)++; |
| 3058 | |
| 3059 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE); |
| 3060 | if (ptr) |
| 3061 | update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate), |
| 3062 | t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1); |
| 3063 | |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 3064 | /* When the client triggers a 4xx from the server, it's most often due |
| 3065 | * to a missing object or permission. These events should be tracked |
| 3066 | * because if they happen often, it may indicate a brute force or a |
| 3067 | * vulnerability scan. Normally this is done when receiving the response |
| 3068 | * but here we're tracking after this ought to have been done so we have |
| 3069 | * to do it on purpose. |
| 3070 | */ |
Willy Tarreau | 3146a4c | 2016-07-26 15:22:33 +0200 | [diff] [blame] | 3071 | if ((unsigned)(txn->status - 400) < 100) { |
| 3072 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT); |
| 3073 | if (ptr) |
| 3074 | stktable_data_cast(ptr, http_err_cnt)++; |
| 3075 | |
| 3076 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE); |
| 3077 | if (ptr) |
| 3078 | update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate), |
| 3079 | t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1); |
| 3080 | } |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 3081 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 3082 | HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock); |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 3083 | |
Emeric Brun | 0fed0b0 | 2017-11-29 16:15:07 +0100 | [diff] [blame] | 3084 | /* If data was modified, we need to touch to re-schedule sync */ |
| 3085 | stktable_touch_local(t, ts, 0); |
| 3086 | |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 3087 | stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT); |
| 3088 | if (sess->fe != s->be) |
| 3089 | stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND); |
| 3090 | |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 3091 | } |
| 3092 | } |
| 3093 | break; |
| 3094 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 3095 | case ACT_CUSTOM: |
Willy Tarreau | 7aa15b0 | 2017-12-20 16:56:50 +0100 | [diff] [blame] | 3096 | if ((s->req.flags & CF_READ_ERROR) || |
| 3097 | ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) && |
| 3098 | !(s->si[0].flags & SI_FL_CLEAN_ABRT) && |
| 3099 | (px->options & PR_O_ABRT_CLOSE))) |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 3100 | act_flags |= ACT_FLAG_FINAL; |
Willy Tarreau | 3945868 | 2015-09-27 10:33:15 +0200 | [diff] [blame] | 3101 | |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 3102 | switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) { |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 3103 | case ACT_RET_ERR: |
| 3104 | case ACT_RET_CONT: |
| 3105 | break; |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 3106 | case ACT_RET_STOP: |
| 3107 | return HTTP_RULE_RES_STOP; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 3108 | case ACT_RET_YIELD: |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 3109 | s->current_rule = rule; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3110 | return HTTP_RULE_RES_YIELD; |
| 3111 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 3112 | break; |
| 3113 | |
Thierry FOURNIER | 22e4901 | 2015-08-05 19:13:48 +0200 | [diff] [blame] | 3114 | /* other flags exists, but normaly, they never be matched. */ |
| 3115 | default: |
| 3116 | break; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3117 | } |
| 3118 | } |
| 3119 | |
| 3120 | /* we reached the end of the rules, nothing to report */ |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 3121 | return HTTP_RULE_RES_CONT; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3122 | } |
| 3123 | |
| 3124 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3125 | /* Perform an HTTP redirect based on the information in <rule>. The function |
| 3126 | * returns non-zero on success, or zero in case of a, irrecoverable error such |
| 3127 | * as too large a request to build a valid response. |
| 3128 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3129 | 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] | 3130 | { |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 3131 | struct http_msg *req = &txn->req; |
| 3132 | struct http_msg *res = &txn->rsp; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3133 | const char *msg_fmt; |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3134 | struct chunk *chunk; |
| 3135 | int ret = 0; |
| 3136 | |
| 3137 | chunk = alloc_trash_chunk(); |
| 3138 | if (!chunk) |
| 3139 | goto leave; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3140 | |
| 3141 | /* build redirect message */ |
| 3142 | switch(rule->code) { |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 3143 | case 308: |
| 3144 | msg_fmt = HTTP_308; |
| 3145 | break; |
| 3146 | case 307: |
| 3147 | msg_fmt = HTTP_307; |
| 3148 | break; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3149 | case 303: |
| 3150 | msg_fmt = HTTP_303; |
| 3151 | break; |
| 3152 | case 301: |
| 3153 | msg_fmt = HTTP_301; |
| 3154 | break; |
| 3155 | case 302: |
| 3156 | default: |
| 3157 | msg_fmt = HTTP_302; |
| 3158 | break; |
| 3159 | } |
| 3160 | |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3161 | if (unlikely(!chunk_strcpy(chunk, msg_fmt))) |
| 3162 | goto leave; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3163 | |
| 3164 | switch(rule->type) { |
| 3165 | case REDIRECT_TYPE_SCHEME: { |
| 3166 | const char *path; |
| 3167 | const char *host; |
| 3168 | struct hdr_ctx ctx; |
| 3169 | int pathlen; |
| 3170 | int hostlen; |
| 3171 | |
| 3172 | host = ""; |
| 3173 | hostlen = 0; |
| 3174 | ctx.idx = 0; |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 3175 | 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] | 3176 | host = ctx.line + ctx.val; |
| 3177 | hostlen = ctx.vlen; |
| 3178 | } |
| 3179 | |
| 3180 | path = http_get_path(txn); |
| 3181 | /* build message using path */ |
| 3182 | if (path) { |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 3183 | 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] | 3184 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 3185 | int qs = 0; |
| 3186 | while (qs < pathlen) { |
| 3187 | if (path[qs] == '?') { |
| 3188 | pathlen = qs; |
| 3189 | break; |
| 3190 | } |
| 3191 | qs++; |
| 3192 | } |
| 3193 | } |
| 3194 | } else { |
| 3195 | path = "/"; |
| 3196 | pathlen = 1; |
| 3197 | } |
| 3198 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3199 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
| 3200 | /* check if we can add scheme + "://" + host + path */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3201 | if (chunk->len + rule->rdr_len + 3 + hostlen + pathlen > chunk->size - 4) |
| 3202 | goto leave; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3203 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3204 | /* add scheme */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3205 | memcpy(chunk->str + chunk->len, rule->rdr_str, rule->rdr_len); |
| 3206 | chunk->len += rule->rdr_len; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3207 | } |
| 3208 | else { |
| 3209 | /* add scheme with executing log format */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3210 | 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] | 3211 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3212 | /* check if we can add scheme + "://" + host + path */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3213 | if (chunk->len + 3 + hostlen + pathlen > chunk->size - 4) |
| 3214 | goto leave; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3215 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3216 | /* add "://" */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3217 | memcpy(chunk->str + chunk->len, "://", 3); |
| 3218 | chunk->len += 3; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3219 | |
| 3220 | /* add host */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3221 | memcpy(chunk->str + chunk->len, host, hostlen); |
| 3222 | chunk->len += hostlen; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3223 | |
| 3224 | /* add path */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3225 | memcpy(chunk->str + chunk->len, path, pathlen); |
| 3226 | chunk->len += pathlen; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3227 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3228 | /* 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] | 3229 | if (chunk->len && chunk->str[chunk->len - 1] != '/' && |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3230 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3231 | if (chunk->len > chunk->size - 5) |
| 3232 | goto leave; |
| 3233 | chunk->str[chunk->len] = '/'; |
| 3234 | chunk->len++; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3235 | } |
| 3236 | |
| 3237 | break; |
| 3238 | } |
| 3239 | case REDIRECT_TYPE_PREFIX: { |
| 3240 | const char *path; |
| 3241 | int pathlen; |
| 3242 | |
| 3243 | path = http_get_path(txn); |
| 3244 | /* build message using path */ |
| 3245 | if (path) { |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 3246 | 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] | 3247 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 3248 | int qs = 0; |
| 3249 | while (qs < pathlen) { |
| 3250 | if (path[qs] == '?') { |
| 3251 | pathlen = qs; |
| 3252 | break; |
| 3253 | } |
| 3254 | qs++; |
| 3255 | } |
| 3256 | } |
| 3257 | } else { |
| 3258 | path = "/"; |
| 3259 | pathlen = 1; |
| 3260 | } |
| 3261 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3262 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3263 | if (chunk->len + rule->rdr_len + pathlen > chunk->size - 4) |
| 3264 | goto leave; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3265 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3266 | /* add prefix. Note that if prefix == "/", we don't want to |
| 3267 | * add anything, otherwise it makes it hard for the user to |
| 3268 | * configure a self-redirection. |
| 3269 | */ |
| 3270 | if (rule->rdr_len != 1 || *rule->rdr_str != '/') { |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3271 | memcpy(chunk->str + chunk->len, rule->rdr_str, rule->rdr_len); |
| 3272 | chunk->len += rule->rdr_len; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3273 | } |
| 3274 | } |
| 3275 | else { |
| 3276 | /* add prefix with executing log format */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3277 | 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] | 3278 | |
| 3279 | /* Check length */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3280 | if (chunk->len + pathlen > chunk->size - 4) |
| 3281 | goto leave; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3282 | } |
| 3283 | |
| 3284 | /* add path */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3285 | memcpy(chunk->str + chunk->len, path, pathlen); |
| 3286 | chunk->len += pathlen; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3287 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3288 | /* 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] | 3289 | if (chunk->len && chunk->str[chunk->len - 1] != '/' && |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3290 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3291 | if (chunk->len > chunk->size - 5) |
| 3292 | goto leave; |
| 3293 | chunk->str[chunk->len] = '/'; |
| 3294 | chunk->len++; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3295 | } |
| 3296 | |
| 3297 | break; |
| 3298 | } |
| 3299 | case REDIRECT_TYPE_LOCATION: |
| 3300 | default: |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3301 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3302 | if (chunk->len + rule->rdr_len > chunk->size - 4) |
| 3303 | goto leave; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3304 | |
| 3305 | /* add location */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3306 | memcpy(chunk->str + chunk->len, rule->rdr_str, rule->rdr_len); |
| 3307 | chunk->len += rule->rdr_len; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3308 | } |
| 3309 | else { |
| 3310 | /* add location with executing log format */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3311 | 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] | 3312 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3313 | /* Check left length */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3314 | if (chunk->len > chunk->size - 4) |
| 3315 | goto leave; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3316 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3317 | break; |
| 3318 | } |
| 3319 | |
| 3320 | if (rule->cookie_len) { |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3321 | memcpy(chunk->str + chunk->len, "\r\nSet-Cookie: ", 14); |
| 3322 | chunk->len += 14; |
| 3323 | memcpy(chunk->str + chunk->len, rule->cookie_str, rule->cookie_len); |
| 3324 | chunk->len += rule->cookie_len; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3325 | } |
| 3326 | |
Willy Tarreau | 19b1412 | 2017-02-28 09:48:11 +0100 | [diff] [blame] | 3327 | /* add end of headers and the keep-alive/close status. */ |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3328 | txn->status = rule->code; |
| 3329 | /* let's log the request time */ |
| 3330 | s->logs.tv_request = now; |
| 3331 | |
Christopher Faulet | be821b9 | 2017-03-30 11:21:53 +0200 | [diff] [blame] | 3332 | 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] | 3333 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL || |
| 3334 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) { |
| 3335 | /* keep-alive possible */ |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 3336 | if (!(req->flags & HTTP_MSGF_VER_11)) { |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3337 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3338 | memcpy(chunk->str + chunk->len, "\r\nProxy-Connection: keep-alive", 30); |
| 3339 | chunk->len += 30; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3340 | } else { |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3341 | memcpy(chunk->str + chunk->len, "\r\nConnection: keep-alive", 24); |
| 3342 | chunk->len += 24; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3343 | } |
| 3344 | } |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3345 | memcpy(chunk->str + chunk->len, "\r\n\r\n", 4); |
| 3346 | chunk->len += 4; |
| 3347 | FLT_STRM_CB(s, flt_http_reply(s, txn->status, chunk)); |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3348 | co_inject(res->chn, chunk->str, chunk->len); |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3349 | /* "eat" the request */ |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 3350 | bi_fast_delete(req->chn->buf, req->sov); |
| 3351 | req->next -= req->sov; |
| 3352 | req->sov = 0; |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 3353 | 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] | 3354 | 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] | 3355 | req->msg_state = HTTP_MSG_CLOSED; |
| 3356 | res->msg_state = HTTP_MSG_DONE; |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 3357 | /* Trim any possible response */ |
| 3358 | res->chn->buf->i = 0; |
| 3359 | res->next = res->sov = 0; |
Christopher Faulet | 5d468ca | 2017-09-11 09:27:29 +0200 | [diff] [blame] | 3360 | /* let the server side turn to SI_ST_CLO */ |
| 3361 | channel_shutw_now(req->chn); |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3362 | } else { |
| 3363 | /* keep-alive not possible */ |
| 3364 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3365 | memcpy(chunk->str + chunk->len, "\r\nProxy-Connection: close\r\n\r\n", 29); |
| 3366 | chunk->len += 29; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3367 | } else { |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3368 | memcpy(chunk->str + chunk->len, "\r\nConnection: close\r\n\r\n", 23); |
| 3369 | chunk->len += 23; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3370 | } |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3371 | http_reply_and_close(s, txn->status, chunk); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 3372 | req->chn->analysers &= AN_REQ_FLT_END; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3373 | } |
| 3374 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3375 | if (!(s->flags & SF_ERR_MASK)) |
| 3376 | s->flags |= SF_ERR_LOCAL; |
| 3377 | if (!(s->flags & SF_FINST_MASK)) |
| 3378 | s->flags |= SF_FINST_R; |
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 | ret = 1; |
| 3381 | leave: |
| 3382 | free_trash_chunk(chunk); |
| 3383 | return ret; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3384 | } |
| 3385 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3386 | /* This stream analyser runs all HTTP request processing which is common to |
| 3387 | * frontends and backends, which means blocking ACLs, filters, connection-close, |
| 3388 | * reqadd, stats and redirects. This is performed for the designated proxy. |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3389 | * 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] | 3390 | * either needs more data or wants to immediately abort the request (eg: deny, |
| 3391 | * error, ...). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3392 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3393 | 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] | 3394 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 3395 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3396 | struct http_txn *txn = s->txn; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3397 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3398 | struct redirect_rule *rule; |
Willy Tarreau | f4f0412 | 2010-01-28 18:10:50 +0100 | [diff] [blame] | 3399 | struct cond_wordlist *wl; |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3400 | enum rule_result verdict; |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 3401 | int deny_status = HTTP_ERR_403; |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 3402 | struct connection *conn = objt_conn(sess->origin); |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3403 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 3404 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3405 | /* we need more data */ |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3406 | goto return_prx_yield; |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3407 | } |
| 3408 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3409 | 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] | 3410 | now_ms, __FUNCTION__, |
| 3411 | s, |
| 3412 | req, |
| 3413 | req->rex, req->wex, |
| 3414 | req->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3415 | req->buf->i, |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3416 | req->analysers); |
| 3417 | |
Willy Tarreau | 6541083 | 2014-04-28 21:25:43 +0200 | [diff] [blame] | 3418 | /* just in case we have some per-backend tracking */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3419 | stream_inc_be_http_req_ctr(s); |
Willy Tarreau | 6541083 | 2014-04-28 21:25:43 +0200 | [diff] [blame] | 3420 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3421 | /* evaluate http-request rules */ |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3422 | if (!LIST_ISEMPTY(&px->http_req_rules)) { |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 3423 | 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] | 3424 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3425 | switch (verdict) { |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3426 | case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */ |
| 3427 | goto return_prx_yield; |
| 3428 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3429 | case HTTP_RULE_RES_CONT: |
| 3430 | case HTTP_RULE_RES_STOP: /* nothing to do */ |
| 3431 | break; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 3432 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3433 | case HTTP_RULE_RES_DENY: /* deny or tarpit */ |
| 3434 | if (txn->flags & TX_CLTARPIT) |
| 3435 | goto tarpit; |
| 3436 | goto deny; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 3437 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3438 | case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */ |
| 3439 | goto return_prx_cond; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 3440 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3441 | case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */ |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 3442 | goto done; |
| 3443 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3444 | case HTTP_RULE_RES_BADREQ: /* failed with a bad request */ |
| 3445 | goto return_bad_req; |
| 3446 | } |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3447 | } |
| 3448 | |
Olivier Houchard | 25ae45a | 2017-11-29 19:51:19 +0100 | [diff] [blame] | 3449 | if (conn && (conn->flags & CO_FL_EARLY_DATA) && |
| 3450 | (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE))) { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 3451 | struct hdr_ctx ctx; |
| 3452 | |
| 3453 | ctx.idx = 0; |
| 3454 | if (!http_find_header2("Early-Data", strlen("Early-Data"), |
| 3455 | s->req.buf->p, &txn->hdr_idx, &ctx)) { |
| 3456 | if (unlikely(http_header_add_tail2(&txn->req, |
| 3457 | &txn->hdr_idx, "Early-Data: 1", |
| 3458 | strlen("Early-Data: 1"))) < 0) { |
| 3459 | goto return_bad_req; |
| 3460 | } |
| 3461 | } |
| 3462 | |
| 3463 | } |
| 3464 | |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 3465 | /* OK at this stage, we know that the request was accepted according to |
| 3466 | * the http-request rules, we can check for the stats. Note that the |
| 3467 | * URI is detected *before* the req* rules in order not to be affected |
| 3468 | * by a possible reqrep, while they are processed *after* so that a |
| 3469 | * reqdeny can still block them. This clearly needs to change in 1.6! |
| 3470 | */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 3471 | if (stats_check_uri(&s->si[1], txn, px)) { |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 3472 | s->target = &http_stats_applet.obj_type; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 3473 | 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] | 3474 | txn->status = 500; |
| 3475 | s->logs.tv_request = now; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 3476 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3477 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3478 | if (!(s->flags & SF_ERR_MASK)) |
| 3479 | s->flags |= SF_ERR_RESOURCE; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 3480 | goto return_prx_cond; |
| 3481 | } |
| 3482 | |
| 3483 | /* parse the whole stats request and extract the relevant information */ |
| 3484 | http_handle_stats(s, req); |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 3485 | 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] | 3486 | /* not all actions implemented: deny, allow, auth */ |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3487 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3488 | if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */ |
| 3489 | goto deny; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 3490 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3491 | if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */ |
| 3492 | goto return_prx_cond; |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 3493 | } |
| 3494 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3495 | /* evaluate the req* rules except reqadd */ |
| 3496 | if (px->req_exp != NULL) { |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 3497 | if (apply_filters_to_request(s, req, px) < 0) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3498 | goto return_bad_req; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3499 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3500 | if (txn->flags & TX_CLDENY) |
| 3501 | goto deny; |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 3502 | |
Jarno Huuskonen | 800d176 | 2017-03-06 14:56:36 +0200 | [diff] [blame] | 3503 | if (txn->flags & TX_CLTARPIT) { |
| 3504 | deny_status = HTTP_ERR_500; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3505 | goto tarpit; |
Jarno Huuskonen | 800d176 | 2017-03-06 14:56:36 +0200 | [diff] [blame] | 3506 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3507 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3508 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3509 | /* add request headers from the rule sets in the same order */ |
| 3510 | list_for_each_entry(wl, &px->req_add, list) { |
| 3511 | if (wl->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 3512 | 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] | 3513 | ret = acl_pass(ret); |
| 3514 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 3515 | ret = !ret; |
| 3516 | if (!ret) |
| 3517 | continue; |
| 3518 | } |
| 3519 | |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 3520 | 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] | 3521 | goto return_bad_req; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 3522 | } |
| 3523 | |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 3524 | |
| 3525 | /* Proceed with the stats now. */ |
William Lallemand | 71bd11a | 2017-11-20 19:13:14 +0100 | [diff] [blame] | 3526 | if (unlikely(objt_applet(s->target) == &http_stats_applet) || |
| 3527 | unlikely(objt_applet(s->target) == &http_cache_applet)) { |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3528 | /* process the stats request now */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3529 | 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] | 3530 | HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1); |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 3531 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3532 | if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is |
| 3533 | s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy |
| 3534 | if (!(s->flags & SF_FINST_MASK)) |
| 3535 | s->flags |= SF_FINST_R; |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 3536 | |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 3537 | /* 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] | 3538 | req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END); |
| 3539 | req->analysers &= ~AN_REQ_FLT_XFER_DATA; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 3540 | req->analysers |= AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3541 | goto done; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3542 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 3543 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3544 | /* check whether we have some ACLs set to redirect this request */ |
| 3545 | list_for_each_entry(rule, &px->redirect_rules, list) { |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 3546 | if (rule->cond) { |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3547 | int ret; |
| 3548 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 3549 | 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] | 3550 | ret = acl_pass(ret); |
| 3551 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 3552 | ret = !ret; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3553 | if (!ret) |
| 3554 | continue; |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 3555 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3556 | if (!http_apply_redirect_rule(rule, s, txn)) |
| 3557 | goto return_bad_req; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3558 | goto done; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3559 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 3560 | |
Willy Tarreau | 2be3939 | 2010-01-03 17:24:51 +0100 | [diff] [blame] | 3561 | /* POST requests may be accompanied with an "Expect: 100-Continue" header. |
| 3562 | * If this happens, then the data will not come immediately, so we must |
| 3563 | * send all what we have without waiting. Note that due to the small gain |
| 3564 | * 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] | 3565 | * 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] | 3566 | * itself once used. |
| 3567 | */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 3568 | req->flags |= CF_SEND_DONTWAIT; |
Willy Tarreau | 2be3939 | 2010-01-03 17:24:51 +0100 | [diff] [blame] | 3569 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3570 | done: /* done with this analyser, continue with next ones that the calling |
| 3571 | * points will have set, if any. |
| 3572 | */ |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3573 | req->analyse_exp = TICK_ETERNITY; |
Thierry FOURNIER | 7566e30 | 2014-08-22 06:55:26 +0200 | [diff] [blame] | 3574 | done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */ |
| 3575 | req->analysers &= ~an_bit; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3576 | return 1; |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 3577 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3578 | tarpit: |
Willy Tarreau | 6a0bca9 | 2017-06-11 17:56:27 +0200 | [diff] [blame] | 3579 | /* Allow cookie logging |
| 3580 | */ |
| 3581 | if (s->be->cookie_name || sess->fe->capture_name) |
| 3582 | manage_client_side_cookies(s, req); |
| 3583 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3584 | /* When a connection is tarpitted, we use the tarpit timeout, |
| 3585 | * which may be the same as the connect timeout if unspecified. |
| 3586 | * If unset, then set it to zero because we really want it to |
| 3587 | * eventually expire. We build the tarpit as an analyser. |
| 3588 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 3589 | channel_erase(&s->req); |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3590 | |
| 3591 | /* wipe the request out so that we can drop the connection early |
| 3592 | * if the client closes first. |
| 3593 | */ |
| 3594 | channel_dont_connect(req); |
Bertrand Paquet | 83a2c3d | 2016-04-06 11:58:31 +0200 | [diff] [blame] | 3595 | |
Jarno Huuskonen | 800d176 | 2017-03-06 14:56:36 +0200 | [diff] [blame] | 3596 | txn->status = http_err_codes[deny_status]; |
| 3597 | |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 3598 | req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */ |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3599 | req->analysers |= AN_REQ_HTTP_TARPIT; |
| 3600 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit); |
| 3601 | if (!req->analyse_exp) |
| 3602 | req->analyse_exp = tick_add(now_ms, 0); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3603 | stream_inc_http_err_ctr(s); |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 3604 | HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3605 | if (sess->fe != s->be) |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 3606 | HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 3607 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 3608 | HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1); |
Thierry FOURNIER | 7566e30 | 2014-08-22 06:55:26 +0200 | [diff] [blame] | 3609 | goto done_without_exp; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3610 | |
| 3611 | deny: /* this request was blocked (denied) */ |
Bertrand Paquet | 83a2c3d | 2016-04-06 11:58:31 +0200 | [diff] [blame] | 3612 | |
| 3613 | /* Allow cookie logging |
| 3614 | */ |
| 3615 | if (s->be->cookie_name || sess->fe->capture_name) |
| 3616 | manage_client_side_cookies(s, req); |
| 3617 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3618 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 3619 | txn->status = http_err_codes[deny_status]; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3620 | s->logs.tv_request = now; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 3621 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3622 | stream_inc_http_err_ctr(s); |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 3623 | HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3624 | if (sess->fe != s->be) |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 3625 | HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 3626 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 3627 | HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1); |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3628 | goto return_prx_cond; |
| 3629 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3630 | return_bad_req: |
| 3631 | /* We centralize bad requests processing here */ |
| 3632 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 3633 | /* we detected a parsing error. We want to archive this request |
| 3634 | * in the dedicated proxy area for later troubleshooting. |
| 3635 | */ |
Emeric Brun | 8c1aaa2 | 2017-06-15 11:30:06 +0200 | [diff] [blame] | 3636 | 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] | 3637 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 3638 | |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 3639 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3640 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3641 | txn->status = 400; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 3642 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3643 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 3644 | HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 3645 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 3646 | HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1); |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 3647 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3648 | return_prx_cond: |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3649 | if (!(s->flags & SF_ERR_MASK)) |
| 3650 | s->flags |= SF_ERR_PRXCOND; |
| 3651 | if (!(s->flags & SF_FINST_MASK)) |
| 3652 | s->flags |= SF_FINST_R; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 3653 | |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 3654 | req->analysers &= AN_REQ_FLT_END; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3655 | req->analyse_exp = TICK_ETERNITY; |
| 3656 | return 0; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3657 | |
| 3658 | return_prx_yield: |
| 3659 | channel_dont_connect(req); |
| 3660 | return 0; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3661 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3662 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3663 | /* This function performs all the processing enabled for the current request. |
| 3664 | * It returns 1 if the processing can continue on next analysers, or zero if it |
| 3665 | * needs more data, encounters an error, or wants to immediately abort the |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 3666 | * request. It relies on buffers flags, and updates s->req.analysers. |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3667 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3668 | 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] | 3669 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 3670 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3671 | struct http_txn *txn = s->txn; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3672 | struct http_msg *msg = &txn->req; |
Willy Tarreau | ee335e6 | 2015-04-21 18:15:13 +0200 | [diff] [blame] | 3673 | struct connection *cli_conn = objt_conn(strm_sess(s)->origin); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3674 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 3675 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3676 | /* we need more data */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 3677 | channel_dont_connect(req); |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3678 | return 0; |
| 3679 | } |
| 3680 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3681 | 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] | 3682 | now_ms, __FUNCTION__, |
| 3683 | s, |
| 3684 | req, |
| 3685 | req->rex, req->wex, |
| 3686 | req->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3687 | req->buf->i, |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3688 | req->analysers); |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3689 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3690 | /* |
| 3691 | * Right now, we know that we have processed the entire headers |
| 3692 | * and that unwanted requests have been filtered out. We can do |
| 3693 | * whatever we want with the remaining request. Also, now we |
| 3694 | * may have separate values for ->fe, ->be. |
| 3695 | */ |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3696 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3697 | /* |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3698 | * If HTTP PROXY is set we simply get remote server address parsing |
| 3699 | * incoming request. Note that this requires that a connection is |
| 3700 | * allocated on the server side. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3701 | */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3702 | 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] | 3703 | struct connection *conn; |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 3704 | char *path; |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 3705 | |
Willy Tarreau | 9471b8c | 2013-12-15 13:31:35 +0100 | [diff] [blame] | 3706 | /* Note that for now we don't reuse existing proxy connections */ |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 3707 | 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] | 3708 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 3709 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3710 | txn->status = 500; |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 3711 | req->analysers &= AN_REQ_FLT_END; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 3712 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 3713 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3714 | if (!(s->flags & SF_ERR_MASK)) |
| 3715 | s->flags |= SF_ERR_RESOURCE; |
| 3716 | if (!(s->flags & SF_FINST_MASK)) |
| 3717 | s->flags |= SF_FINST_R; |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 3718 | |
| 3719 | return 0; |
| 3720 | } |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 3721 | |
| 3722 | path = http_get_path(txn); |
| 3723 | url2sa(req->buf->p + msg->sl.rq.u, |
| 3724 | path ? path - (req->buf->p + msg->sl.rq.u) : msg->sl.rq.u_l, |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 3725 | &conn->addr.to, NULL); |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 3726 | /* if the path was found, we have to remove everything between |
| 3727 | * req->buf->p + msg->sl.rq.u and path (excluded). If it was not |
| 3728 | * found, we need to replace from req->buf->p + msg->sl.rq.u for |
| 3729 | * u_l characters by a single "/". |
| 3730 | */ |
| 3731 | if (path) { |
| 3732 | char *cur_ptr = req->buf->p; |
| 3733 | char *cur_end = cur_ptr + txn->req.sl.rq.l; |
| 3734 | int delta; |
| 3735 | |
| 3736 | delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0); |
| 3737 | http_msg_move_end(&txn->req, delta); |
| 3738 | cur_end += delta; |
| 3739 | if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL) |
| 3740 | goto return_bad_req; |
| 3741 | } |
| 3742 | else { |
| 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, |
| 3748 | req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1); |
| 3749 | http_msg_move_end(&txn->req, delta); |
| 3750 | cur_end += delta; |
| 3751 | if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL) |
| 3752 | goto return_bad_req; |
| 3753 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3754 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3755 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3756 | /* |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 3757 | * 7: Now we can work with the cookies. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3758 | * Note that doing so might move headers in the request, but |
| 3759 | * the fields will stay coherent and the URI will not move. |
| 3760 | * This should only be performed in the backend. |
| 3761 | */ |
Bertrand Paquet | 83a2c3d | 2016-04-06 11:58:31 +0200 | [diff] [blame] | 3762 | if (s->be->cookie_name || sess->fe->capture_name) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3763 | manage_client_side_cookies(s, req); |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 3764 | |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 3765 | /* add unique-id if "header-unique-id" is specified */ |
| 3766 | |
Thierry Fournier | f4011dd | 2016-03-29 17:23:51 +0200 | [diff] [blame] | 3767 | if (!LIST_ISEMPTY(&sess->fe->format_unique_id) && !s->unique_id) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 3768 | if ((s->unique_id = pool_alloc(pool_head_uniqueid)) == NULL) |
William Lallemand | 5b7ea3a | 2013-08-28 15:44:19 +0200 | [diff] [blame] | 3769 | goto return_bad_req; |
| 3770 | s->unique_id[0] = '\0'; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3771 | 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] | 3772 | } |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 3773 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3774 | if (sess->fe->header_unique_id && s->unique_id) { |
| 3775 | 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] | 3776 | if (trash.len < 0) |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 3777 | goto return_bad_req; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3778 | 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] | 3779 | goto return_bad_req; |
| 3780 | } |
| 3781 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 3782 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3783 | * 9: add X-Forwarded-For if either the frontend or the backend |
| 3784 | * asks for it. |
| 3785 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3786 | if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) { |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 3787 | struct hdr_ctx ctx = { .idx = 0 }; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3788 | if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) && |
| 3789 | http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name, |
| 3790 | 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] | 3791 | req->buf->p, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 3792 | /* The header is set to be added only if none is present |
| 3793 | * and we found it, so don't do anything. |
| 3794 | */ |
| 3795 | } |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3796 | else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3797 | /* Add an X-Forwarded-For header unless the source IP is |
| 3798 | * in the 'except' network range. |
| 3799 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3800 | if ((!sess->fe->except_mask.s_addr || |
| 3801 | (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr) |
| 3802 | != sess->fe->except_net.s_addr) && |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3803 | (!s->be->except_mask.s_addr || |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3804 | (((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] | 3805 | != s->be->except_net.s_addr)) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 3806 | int len; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3807 | unsigned char *pn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3808 | pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr; |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 3809 | |
| 3810 | /* Note: we rely on the backend to get the header name to be used for |
| 3811 | * x-forwarded-for, because the header is really meant for the backends. |
| 3812 | * However, if the backend did not specify any option, we have to rely |
| 3813 | * on the frontend's header name. |
| 3814 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3815 | if (s->be->fwdfor_hdr_len) { |
| 3816 | len = s->be->fwdfor_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3817 | memcpy(trash.str, s->be->fwdfor_hdr_name, len); |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 3818 | } else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3819 | len = sess->fe->fwdfor_hdr_len; |
| 3820 | memcpy(trash.str, sess->fe->fwdfor_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 3821 | } |
Willy Tarreau | e9187f8 | 2014-04-14 15:27:14 +0200 | [diff] [blame] | 3822 | 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] | 3823 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3824 | 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] | 3825 | goto return_bad_req; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 3826 | } |
| 3827 | } |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3828 | else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3829 | /* FIXME: for the sake of completeness, we should also support |
| 3830 | * 'except' here, although it is mostly useless in this case. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3831 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3832 | int len; |
| 3833 | char pn[INET6_ADDRSTRLEN]; |
| 3834 | inet_ntop(AF_INET6, |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3835 | (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3836 | pn, sizeof(pn)); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3837 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3838 | /* Note: we rely on the backend to get the header name to be used for |
| 3839 | * x-forwarded-for, because the header is really meant for the backends. |
| 3840 | * However, if the backend did not specify any option, we have to rely |
| 3841 | * on the frontend's header name. |
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 | if (s->be->fwdfor_hdr_len) { |
| 3844 | len = s->be->fwdfor_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3845 | memcpy(trash.str, s->be->fwdfor_hdr_name, len); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3846 | } else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3847 | len = sess->fe->fwdfor_hdr_len; |
| 3848 | memcpy(trash.str, sess->fe->fwdfor_hdr_name, len); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3849 | } |
Willy Tarreau | e9187f8 | 2014-04-14 15:27:14 +0200 | [diff] [blame] | 3850 | len += snprintf(trash.str + len, trash.size - len, ": %s", pn); |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3851 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3852 | 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] | 3853 | goto return_bad_req; |
| 3854 | } |
| 3855 | } |
| 3856 | |
| 3857 | /* |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3858 | * 10: add X-Original-To if either the frontend or the backend |
| 3859 | * asks for it. |
| 3860 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3861 | if ((sess->fe->options | s->be->options) & PR_O_ORGTO) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3862 | |
| 3863 | /* FIXME: don't know if IPv6 can handle that case too. */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3864 | if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3865 | /* Add an X-Original-To header unless the destination IP is |
| 3866 | * in the 'except' network range. |
| 3867 | */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3868 | conn_get_to_addr(cli_conn); |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3869 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3870 | if (cli_conn->addr.to.ss_family == AF_INET && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3871 | ((!sess->fe->except_mask_to.s_addr || |
| 3872 | (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr) |
| 3873 | != sess->fe->except_to.s_addr) && |
Emeric Brun | 5bd86a8 | 2010-10-22 17:23:04 +0200 | [diff] [blame] | 3874 | (!s->be->except_mask_to.s_addr || |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3875 | (((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] | 3876 | != s->be->except_to.s_addr))) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3877 | int len; |
| 3878 | unsigned char *pn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3879 | pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr; |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3880 | |
| 3881 | /* Note: we rely on the backend to get the header name to be used for |
| 3882 | * x-original-to, because the header is really meant for the backends. |
| 3883 | * However, if the backend did not specify any option, we have to rely |
| 3884 | * on the frontend's header name. |
| 3885 | */ |
| 3886 | if (s->be->orgto_hdr_len) { |
| 3887 | len = s->be->orgto_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3888 | memcpy(trash.str, s->be->orgto_hdr_name, len); |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3889 | } else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3890 | len = sess->fe->orgto_hdr_len; |
| 3891 | memcpy(trash.str, sess->fe->orgto_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 3892 | } |
Willy Tarreau | e9187f8 | 2014-04-14 15:27:14 +0200 | [diff] [blame] | 3893 | 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] | 3894 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3895 | 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] | 3896 | goto return_bad_req; |
| 3897 | } |
| 3898 | } |
| 3899 | } |
| 3900 | |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 3901 | /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set. |
| 3902 | * If an "Upgrade" token is found, the header is left untouched in order not to have |
| 3903 | * to deal with some servers bugs : some of them fail an Upgrade if anything but |
| 3904 | * "Upgrade" is present in the Connection header. |
| 3905 | */ |
| 3906 | if (!(txn->flags & TX_HDR_CONN_UPG) && |
| 3907 | (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3908 | ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 3909 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) { |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3910 | unsigned int want_flags = 0; |
| 3911 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3912 | if (msg->flags & HTTP_MSGF_VER_11) { |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 3913 | if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3914 | ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 3915 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3916 | !((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3917 | want_flags |= TX_CON_CLO_SET; |
| 3918 | } else { |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 3919 | if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3920 | ((sess->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL && |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 3921 | (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3922 | ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3923 | want_flags |= TX_CON_KAL_SET; |
| 3924 | } |
| 3925 | |
| 3926 | 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] | 3927 | http_change_connection_header(txn, msg, want_flags); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3928 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3929 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3930 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3931 | /* If we have no server assigned yet and we're balancing on url_param |
| 3932 | * with a POST request, we may be interested in checking the body for |
| 3933 | * that parameter. This will be done in another analyser. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3934 | */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3935 | if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) && |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3936 | s->txn->meth == HTTP_METH_POST && s->be->url_param_name != NULL && |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3937 | (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) { |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 3938 | channel_dont_connect(req); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3939 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3940 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3941 | |
Christopher Faulet | be821b9 | 2017-03-30 11:21:53 +0200 | [diff] [blame] | 3942 | req->analysers &= ~AN_REQ_FLT_XFER_DATA; |
| 3943 | req->analysers |= AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 3944 | #ifdef TCP_QUICKACK |
Christopher Faulet | be821b9 | 2017-03-30 11:21:53 +0200 | [diff] [blame] | 3945 | /* We expect some data from the client. Unless we know for sure |
| 3946 | * we already have a full request, we have to re-enable quick-ack |
| 3947 | * in case we previously disabled it, otherwise we might cause |
| 3948 | * the client to delay further data. |
| 3949 | */ |
| 3950 | if ((sess->listener->options & LI_O_NOQUICKACK) && |
| 3951 | cli_conn && conn_ctrl_ready(cli_conn) && |
| 3952 | ((msg->flags & HTTP_MSGF_TE_CHNK) || |
| 3953 | (msg->body_len > req->buf->i - txn->req.eoh - 2))) |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 3954 | setsockopt(cli_conn->handle.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one)); |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 3955 | #endif |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 3956 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3957 | /************************************************************* |
| 3958 | * OK, that's finished for the headers. We have done what we * |
| 3959 | * could. Let's switch to the DATA state. * |
| 3960 | ************************************************************/ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3961 | req->analyse_exp = TICK_ETERNITY; |
| 3962 | req->analysers &= ~an_bit; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3963 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3964 | s->logs.tv_request = now; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3965 | /* OK let's go on with the BODY now */ |
| 3966 | return 1; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3967 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3968 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 3969 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
Willy Tarreau | f073a83 | 2009-03-01 23:21:47 +0100 | [diff] [blame] | 3970 | /* we detected a parsing error. We want to archive this request |
| 3971 | * in the dedicated proxy area for later troubleshooting. |
| 3972 | */ |
Emeric Brun | 8c1aaa2 | 2017-06-15 11:30:06 +0200 | [diff] [blame] | 3973 | 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] | 3974 | } |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 3975 | |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 3976 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3977 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3978 | txn->status = 400; |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 3979 | req->analysers &= AN_REQ_FLT_END; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 3980 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3981 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 3982 | HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 3983 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 3984 | HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1); |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3985 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3986 | if (!(s->flags & SF_ERR_MASK)) |
| 3987 | s->flags |= SF_ERR_PRXCOND; |
| 3988 | if (!(s->flags & SF_FINST_MASK)) |
| 3989 | s->flags |= SF_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3990 | return 0; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 3991 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3992 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3993 | /* This function is an analyser which processes the HTTP tarpit. It always |
| 3994 | * returns zero, at the beginning because it prevents any other processing |
| 3995 | * from occurring, and at the end because it terminates the request. |
| 3996 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3997 | 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] | 3998 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3999 | struct http_txn *txn = s->txn; |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4000 | |
| 4001 | /* This connection is being tarpitted. The CLIENT side has |
| 4002 | * already set the connect expiration date to the right |
| 4003 | * timeout. We just have to check that the client is still |
| 4004 | * there and that the timeout has not expired. |
| 4005 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4006 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4007 | if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 && |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4008 | !tick_is_expired(req->analyse_exp, now_ms)) |
| 4009 | return 0; |
| 4010 | |
| 4011 | /* We will set the queue timer to the time spent, just for |
| 4012 | * logging purposes. We fake a 500 server error, so that the |
| 4013 | * attacker will not suspect his connection has been tarpitted. |
| 4014 | * It will not cause trouble to the logs because we can exclude |
| 4015 | * the tarpitted connections by filtering on the 'PT' status flags. |
| 4016 | */ |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4017 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 4018 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4019 | if (!(req->flags & CF_READ_ERROR)) |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 4020 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4021 | |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4022 | req->analysers &= AN_REQ_FLT_END; |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4023 | req->analyse_exp = TICK_ETERNITY; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4024 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4025 | if (!(s->flags & SF_ERR_MASK)) |
| 4026 | s->flags |= SF_ERR_PRXCOND; |
| 4027 | if (!(s->flags & SF_FINST_MASK)) |
| 4028 | s->flags |= SF_FINST_T; |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4029 | return 0; |
| 4030 | } |
| 4031 | |
Willy Tarreau | 5a8f947 | 2014-04-10 11:16:06 +0200 | [diff] [blame] | 4032 | /* This function is an analyser which waits for the HTTP request body. It waits |
| 4033 | * for either the buffer to be full, or the full advertised contents to have |
| 4034 | * reached the buffer. It must only be called after the standard HTTP request |
| 4035 | * processing has occurred, because it expects the request to be parsed and will |
| 4036 | * look for the Expect header. It may send a 100-Continue interim response. It |
| 4037 | * takes in input any state starting from HTTP_MSG_BODY and leaves with one of |
| 4038 | * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it |
| 4039 | * 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] | 4040 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4041 | 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] | 4042 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4043 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4044 | struct http_txn *txn = s->txn; |
| 4045 | struct http_msg *msg = &s->txn->req; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4046 | |
| 4047 | /* We have to parse the HTTP request body to find any required data. |
| 4048 | * "balance url_param check_post" should have been the only way to get |
| 4049 | * into this. We were brought here after HTTP header analysis, so all |
| 4050 | * related structures are ready. |
| 4051 | */ |
| 4052 | |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4053 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
| 4054 | /* This is the first call */ |
| 4055 | if (msg->msg_state < HTTP_MSG_BODY) |
| 4056 | goto missing_data; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4057 | |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4058 | if (msg->msg_state < HTTP_MSG_100_SENT) { |
| 4059 | /* If we have HTTP/1.1 and Expect: 100-continue, then we must |
| 4060 | * send an HTTP/1.1 100 Continue intermediate response. |
| 4061 | */ |
| 4062 | if (msg->flags & HTTP_MSGF_VER_11) { |
| 4063 | struct hdr_ctx ctx; |
| 4064 | ctx.idx = 0; |
| 4065 | /* Expect is allowed in 1.1, look for it */ |
| 4066 | if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) && |
| 4067 | 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] | 4068 | 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] | 4069 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4070 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4071 | } |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4072 | msg->msg_state = HTTP_MSG_100_SENT; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4073 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4074 | |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 4075 | /* 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] | 4076 | * req->buf->p still points to the beginning of the message. We |
| 4077 | * must save the body in msg->next because it survives buffer |
| 4078 | * re-alignments. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4079 | */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 4080 | msg->next = msg->sov; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4081 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4082 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4083 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 4084 | else |
| 4085 | msg->msg_state = HTTP_MSG_DATA; |
| 4086 | } |
| 4087 | |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4088 | if (!(msg->flags & HTTP_MSGF_TE_CHNK)) { |
| 4089 | /* 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] | 4090 | if (http_body_bytes(msg) < msg->body_len) |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4091 | goto missing_data; |
| 4092 | |
| 4093 | /* OK we have everything we need now */ |
| 4094 | goto http_end; |
| 4095 | } |
| 4096 | |
| 4097 | /* OK here we're parsing a chunked-encoded message */ |
| 4098 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4099 | if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 4100 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4101 | * 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] | 4102 | * TRAILERS state. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 4103 | */ |
Willy Tarreau | e56cdd3 | 2017-09-21 08:36:33 +0200 | [diff] [blame] | 4104 | unsigned int chunk; |
| 4105 | 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] | 4106 | |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 4107 | if (!ret) |
| 4108 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4109 | else if (ret < 0) { |
Willy Tarreau | e56cdd3 | 2017-09-21 08:36:33 +0200 | [diff] [blame] | 4110 | msg->err_pos = req->buf->i + ret; |
| 4111 | if (msg->err_pos < 0) |
| 4112 | msg->err_pos += req->buf->size; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4113 | stream_inc_http_err_ctr(s); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4114 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4115 | } |
Willy Tarreau | e56cdd3 | 2017-09-21 08:36:33 +0200 | [diff] [blame] | 4116 | |
| 4117 | msg->chunk_len = chunk; |
| 4118 | msg->body_len += chunk; |
| 4119 | |
| 4120 | msg->sol = ret; |
Christopher Faulet | 113f7de | 2015-12-14 14:52:13 +0100 | [diff] [blame] | 4121 | msg->next += ret; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 4122 | msg->msg_state = msg->chunk_len ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4123 | } |
| 4124 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4125 | /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state. |
Willy Tarreau | e115b49 | 2015-05-01 23:05:14 +0200 | [diff] [blame] | 4126 | * We have the first data byte is in msg->sov + msg->sol. We're waiting |
| 4127 | * for at least a whole chunk or the whole content length bytes after |
| 4128 | * msg->sov + msg->sol. |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4129 | */ |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4130 | if (msg->msg_state == HTTP_MSG_TRAILERS) |
| 4131 | goto http_end; |
| 4132 | |
Willy Tarreau | e115b49 | 2015-05-01 23:05:14 +0200 | [diff] [blame] | 4133 | 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] | 4134 | goto http_end; |
| 4135 | |
| 4136 | missing_data: |
Willy Tarreau | 31a1995 | 2014-04-10 11:50:37 +0200 | [diff] [blame] | 4137 | /* we get here if we need to wait for more data. If the buffer is full, |
| 4138 | * we have the maximum we can expect. |
| 4139 | */ |
| 4140 | if (buffer_full(req->buf, global.tune.maxrewrite)) |
| 4141 | goto http_end; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 4142 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4143 | 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] | 4144 | txn->status = 408; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 4145 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 4146 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4147 | if (!(s->flags & SF_ERR_MASK)) |
| 4148 | s->flags |= SF_ERR_CLITO; |
| 4149 | if (!(s->flags & SF_FINST_MASK)) |
| 4150 | s->flags |= SF_FINST_D; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4151 | goto return_err_msg; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4152 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4153 | |
| 4154 | /* we get here if we need to wait for more data */ |
Willy Tarreau | 31a1995 | 2014-04-10 11:50:37 +0200 | [diff] [blame] | 4155 | if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) { |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4156 | /* Not enough data. We'll re-use the http-request |
| 4157 | * timeout here. Ideally, we should set the timeout |
| 4158 | * relative to the accept() date. We just set the |
| 4159 | * request timeout once at the beginning of the |
| 4160 | * request. |
| 4161 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4162 | channel_dont_connect(req); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4163 | if (!tick_isset(req->analyse_exp)) |
Willy Tarreau | cd7afc0 | 2009-07-12 10:03:17 +0200 | [diff] [blame] | 4164 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4165 | return 0; |
| 4166 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4167 | |
| 4168 | http_end: |
| 4169 | /* The situation will not evolve, so let's give up on the analysis. */ |
| 4170 | s->logs.tv_request = now; /* update the request timer to reflect full request */ |
| 4171 | req->analysers &= ~an_bit; |
| 4172 | req->analyse_exp = TICK_ETERNITY; |
| 4173 | return 1; |
| 4174 | |
| 4175 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 4176 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4177 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4178 | txn->status = 400; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 4179 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4180 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4181 | if (!(s->flags & SF_ERR_MASK)) |
| 4182 | s->flags |= SF_ERR_PRXCOND; |
| 4183 | if (!(s->flags & SF_FINST_MASK)) |
| 4184 | s->flags |= SF_FINST_R; |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 4185 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4186 | return_err_msg: |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4187 | req->analysers &= AN_REQ_FLT_END; |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 4188 | HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4189 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 4190 | HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4191 | return 0; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4192 | } |
| 4193 | |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4194 | /* send a server's name with an outgoing request over an established connection. |
| 4195 | * Note: this function is designed to be called once the request has been scheduled |
| 4196 | * for being forwarded. This is the reason why it rewinds the buffer before |
| 4197 | * proceeding. |
| 4198 | */ |
Willy Tarreau | 45c0d98 | 2012-03-09 12:11:57 +0100 | [diff] [blame] | 4199 | 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] | 4200 | |
| 4201 | struct hdr_ctx ctx; |
| 4202 | |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4203 | char *hdr_name = be->server_id_hdr_name; |
| 4204 | int hdr_name_len = be->server_id_hdr_len; |
Willy Tarreau | 394db37 | 2012-10-12 22:40:39 +0200 | [diff] [blame] | 4205 | struct channel *chn = txn->req.chn; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4206 | char *hdr_val; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4207 | unsigned int old_o, old_i; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4208 | |
William Lallemand | d9e9066 | 2012-01-30 17:27:17 +0100 | [diff] [blame] | 4209 | ctx.idx = 0; |
| 4210 | |
Willy Tarreau | 211cdec | 2014-04-17 20:18:08 +0200 | [diff] [blame] | 4211 | old_o = http_hdr_rewind(&txn->req); |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4212 | if (old_o) { |
| 4213 | /* The request was already skipped, let's restore it */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4214 | b_rew(chn->buf, old_o); |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 4215 | txn->req.next += old_o; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 4216 | txn->req.sov += old_o; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4217 | } |
| 4218 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4219 | old_i = chn->buf->i; |
| 4220 | 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] | 4221 | /* remove any existing values from the header */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 4222 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4223 | } |
| 4224 | |
| 4225 | /* Add the new header requested with the server value */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4226 | hdr_val = trash.str; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4227 | memcpy(hdr_val, hdr_name, hdr_name_len); |
| 4228 | hdr_val += hdr_name_len; |
| 4229 | *hdr_val++ = ':'; |
| 4230 | *hdr_val++ = ' '; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4231 | hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val); |
| 4232 | 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] | 4233 | |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4234 | if (old_o) { |
| 4235 | /* If this was a forwarded request, we must readjust the amount of |
| 4236 | * data to be forwarded in order to take into account the size |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 4237 | * variations. Note that the current state is >= HTTP_MSG_BODY, |
| 4238 | * so we don't have to adjust ->sol. |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4239 | */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 4240 | old_o += chn->buf->i - old_i; |
| 4241 | b_adv(chn->buf, old_o); |
| 4242 | txn->req.next -= old_o; |
| 4243 | txn->req.sov -= old_o; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4244 | } |
| 4245 | |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4246 | return 0; |
| 4247 | } |
| 4248 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4249 | /* Terminate current transaction and prepare a new one. This is very tricky |
| 4250 | * right now but it works. |
| 4251 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4252 | void http_end_txn_clean_session(struct stream *s) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4253 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4254 | int prev_status = s->txn->status; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 4255 | struct proxy *fe = strm_fe(s); |
Willy Tarreau | 858b103 | 2015-12-07 17:04:59 +0100 | [diff] [blame] | 4256 | struct proxy *be = s->be; |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 4257 | struct conn_stream *cs; |
Willy Tarreau | 323a2d9 | 2015-08-04 19:00:17 +0200 | [diff] [blame] | 4258 | struct connection *srv_conn; |
| 4259 | struct server *srv; |
Willy Tarreau | 449d74a | 2015-08-05 17:16:33 +0200 | [diff] [blame] | 4260 | unsigned int prev_flags = s->txn->flags; |
Willy Tarreau | 068621e | 2013-12-23 15:11:25 +0100 | [diff] [blame] | 4261 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4262 | /* FIXME: We need a more portable way of releasing a backend's and a |
| 4263 | * server's connections. We need a safer way to reinitialize buffer |
| 4264 | * flags. We also need a more accurate method for computing per-request |
| 4265 | * data. |
| 4266 | */ |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 4267 | /* |
| 4268 | * XXX cognet: This is probably wrong, this is killing a whole |
| 4269 | * connection, in the new world order, we probably want to just kill |
| 4270 | * the stream, this is to be revisited the day we handle multiple |
| 4271 | * streams in one server connection. |
| 4272 | */ |
| 4273 | cs = objt_cs(s->si[1].end); |
| 4274 | srv_conn = cs_conn(cs); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4275 | |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4276 | /* unless we're doing keep-alive, we want to quickly close the connection |
| 4277 | * to the server. |
| 4278 | */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4279 | if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) || |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4280 | !si_conn_ready(&s->si[1])) { |
| 4281 | s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF; |
| 4282 | si_shutr(&s->si[1]); |
| 4283 | si_shutw(&s->si[1]); |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4284 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4285 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4286 | if (s->flags & SF_BE_ASSIGNED) { |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 4287 | HA_ATOMIC_SUB(&be->beconn, 1); |
Willy Tarreau | 2d5cd47 | 2012-03-01 23:34:37 +0100 | [diff] [blame] | 4288 | if (unlikely(s->srv_conn)) |
| 4289 | sess_change_server(s, NULL); |
| 4290 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4291 | |
| 4292 | s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4293 | stream_process_counters(s); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4294 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4295 | if (s->txn->status) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4296 | int n; |
| 4297 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4298 | n = s->txn->status / 100; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4299 | if (n < 1 || n > 5) |
| 4300 | n = 0; |
| 4301 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4302 | if (fe->mode == PR_MODE_HTTP) { |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 4303 | HA_ATOMIC_ADD(&fe->fe_counters.p.http.rsp[n], 1); |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 4304 | } |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4305 | if ((s->flags & SF_BE_ASSIGNED) && |
Willy Tarreau | 858b103 | 2015-12-07 17:04:59 +0100 | [diff] [blame] | 4306 | (be->mode == PR_MODE_HTTP)) { |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 4307 | HA_ATOMIC_ADD(&be->be_counters.p.http.rsp[n], 1); |
| 4308 | HA_ATOMIC_ADD(&be->be_counters.p.http.cum_req, 1); |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 4309 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4310 | } |
| 4311 | |
| 4312 | /* don't count other requests' data */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4313 | s->logs.bytes_in -= s->req.buf->i; |
| 4314 | s->logs.bytes_out -= s->res.buf->i; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4315 | |
| 4316 | /* let's do a final log if we need it */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4317 | if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait && |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4318 | !(s->flags & SF_MONITOR) && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4319 | (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4320 | s->do_log(s); |
| 4321 | } |
| 4322 | |
Willy Tarreau | d713bcc | 2014-06-25 15:36:04 +0200 | [diff] [blame] | 4323 | /* stop tracking content-based counters */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4324 | stream_stop_content_counters(s); |
| 4325 | stream_update_time_stats(s); |
Willy Tarreau | 4bfc580 | 2014-06-17 12:19:18 +0200 | [diff] [blame] | 4326 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4327 | s->logs.accept_date = date; /* user-visible date for logging */ |
| 4328 | s->logs.tv_accept = now; /* corrected date for internal use */ |
Thierry FOURNIER / OZON.IO | 4cac359 | 2016-07-28 17:19:45 +0200 | [diff] [blame] | 4329 | s->logs.t_handshake = 0; /* There are no handshake in keep alive connection. */ |
| 4330 | s->logs.t_idle = -1; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4331 | tv_zero(&s->logs.tv_request); |
| 4332 | s->logs.t_queue = -1; |
| 4333 | s->logs.t_connect = -1; |
| 4334 | s->logs.t_data = -1; |
| 4335 | s->logs.t_close = 0; |
| 4336 | s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */ |
| 4337 | s->logs.srv_queue_size = 0; /* we will get this number soon */ |
| 4338 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4339 | s->logs.bytes_in = s->req.total = s->req.buf->i; |
| 4340 | s->logs.bytes_out = s->res.total = s->res.buf->i; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4341 | |
| 4342 | if (s->pend_pos) |
| 4343 | pendconn_free(s->pend_pos); |
| 4344 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4345 | if (objt_server(s->target)) { |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4346 | if (s->flags & SF_CURR_SESS) { |
| 4347 | s->flags &= ~SF_CURR_SESS; |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 4348 | HA_ATOMIC_SUB(&objt_server(s->target)->cur_sess, 1); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4349 | } |
Willy Tarreau | 858b103 | 2015-12-07 17:04:59 +0100 | [diff] [blame] | 4350 | if (may_dequeue_tasks(objt_server(s->target), be)) |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4351 | process_srv_queue(objt_server(s->target)); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4352 | } |
| 4353 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4354 | s->target = NULL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4355 | |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4356 | /* only release our endpoint if we don't intend to reuse the |
| 4357 | * connection. |
| 4358 | */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4359 | if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) || |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4360 | !si_conn_ready(&s->si[1])) { |
| 4361 | si_release_endpoint(&s->si[1]); |
Willy Tarreau | 323a2d9 | 2015-08-04 19:00:17 +0200 | [diff] [blame] | 4362 | srv_conn = NULL; |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4363 | } |
| 4364 | |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4365 | s->si[1].state = s->si[1].prev_state = SI_ST_INI; |
| 4366 | s->si[1].err_type = SI_ET_NONE; |
| 4367 | s->si[1].conn_retries = 0; /* used for logging too */ |
| 4368 | s->si[1].exp = TICK_ETERNITY; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4369 | 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] | 4370 | 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] | 4371 | 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] | 4372 | s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST); |
| 4373 | s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED); |
| 4374 | s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP); |
Willy Tarreau | 543db62 | 2012-11-15 16:41:22 +0100 | [diff] [blame] | 4375 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4376 | s->txn->meth = 0; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4377 | http_reset_txn(s); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4378 | s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ; |
Willy Tarreau | 068621e | 2013-12-23 15:11:25 +0100 | [diff] [blame] | 4379 | |
| 4380 | if (prev_status == 401 || prev_status == 407) { |
| 4381 | /* In HTTP keep-alive mode, if we receive a 401, we still have |
| 4382 | * a chance of being able to send the visitor again to the same |
| 4383 | * server over the same connection. This is required by some |
| 4384 | * broken protocols such as NTLM, and anyway whenever there is |
| 4385 | * an opportunity for sending the challenge to the proper place, |
| 4386 | * it's better to do it (at least it helps with debugging). |
| 4387 | */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4388 | s->txn->flags |= TX_PREFER_LAST; |
Willy Tarreau | bd99d58 | 2015-09-02 10:40:43 +0200 | [diff] [blame] | 4389 | if (srv_conn) |
| 4390 | srv_conn->flags |= CO_FL_PRIVATE; |
Willy Tarreau | 068621e | 2013-12-23 15:11:25 +0100 | [diff] [blame] | 4391 | } |
| 4392 | |
Willy Tarreau | 53f9685 | 2016-02-02 18:50:47 +0100 | [diff] [blame] | 4393 | /* Never ever allow to reuse a connection from a non-reuse backend */ |
| 4394 | if (srv_conn && (be->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR) |
| 4395 | srv_conn->flags |= CO_FL_PRIVATE; |
| 4396 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4397 | if (fe->options2 & PR_O2_INDEPSTR) |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4398 | s->si[1].flags |= SI_FL_INDEP_STR; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4399 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4400 | if (fe->options2 & PR_O2_NODELAY) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4401 | s->req.flags |= CF_NEVER_WAIT; |
| 4402 | s->res.flags |= CF_NEVER_WAIT; |
Willy Tarreau | 96e3121 | 2011-05-30 18:10:30 +0200 | [diff] [blame] | 4403 | } |
| 4404 | |
Willy Tarreau | 714ea78 | 2015-11-25 20:11:11 +0100 | [diff] [blame] | 4405 | /* we're removing the analysers, we MUST re-enable events detection. |
| 4406 | * We don't enable close on the response channel since it's either |
| 4407 | * already closed, or in keep-alive with an idle connection handler. |
| 4408 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4409 | channel_auto_read(&s->req); |
| 4410 | channel_auto_close(&s->req); |
| 4411 | channel_auto_read(&s->res); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4412 | |
Willy Tarreau | 1c59bd5 | 2015-11-02 20:20:11 +0100 | [diff] [blame] | 4413 | /* we're in keep-alive with an idle connection, monitor it if not already done */ |
| 4414 | if (srv_conn && LIST_ISEMPTY(&srv_conn->list)) { |
Willy Tarreau | 323a2d9 | 2015-08-04 19:00:17 +0200 | [diff] [blame] | 4415 | srv = objt_server(srv_conn->target); |
Willy Tarreau | 8dff998 | 2015-08-04 20:45:52 +0200 | [diff] [blame] | 4416 | if (!srv) |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 4417 | si_idle_cs(&s->si[1], NULL); |
Willy Tarreau | 53f9685 | 2016-02-02 18:50:47 +0100 | [diff] [blame] | 4418 | else if (srv_conn->flags & CO_FL_PRIVATE) |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 4419 | 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] | 4420 | else if (prev_flags & TX_NOT_FIRST) |
| 4421 | /* note: we check the request, not the connection, but |
| 4422 | * this is valid for strategies SAFE and AGGR, and in |
| 4423 | * case of ALWS, we don't care anyway. |
| 4424 | */ |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 4425 | 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] | 4426 | else |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 4427 | 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] | 4428 | } |
Christopher Faulet | e600624 | 2017-03-10 11:52:44 +0100 | [diff] [blame] | 4429 | s->req.analysers = strm_li(s) ? strm_li(s)->analysers : 0; |
| 4430 | s->res.analysers = 0; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4431 | } |
| 4432 | |
| 4433 | |
| 4434 | /* This function updates the request state machine according to the response |
| 4435 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 4436 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 4437 | * it is only used to find when a request/response couple is complete. Both |
| 4438 | * this function and its equivalent should loop until both return zero. It |
| 4439 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 4440 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4441 | int http_sync_req_state(struct stream *s) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4442 | { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4443 | struct channel *chn = &s->req; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4444 | struct http_txn *txn = s->txn; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4445 | unsigned int old_flags = chn->flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4446 | unsigned int old_state = txn->req.msg_state; |
| 4447 | |
Christopher Faulet | 4be9803 | 2017-07-18 10:48:24 +0200 | [diff] [blame] | 4448 | if (unlikely(txn->req.msg_state < HTTP_MSG_DONE)) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4449 | return 0; |
| 4450 | |
| 4451 | if (txn->req.msg_state == HTTP_MSG_DONE) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4452 | /* No need to read anymore, the request was completely parsed. |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 4453 | * We can shut the read side unless we want to abort_on_close, |
| 4454 | * or we have a POST request. The issue with POST requests is |
| 4455 | * that some browsers still send a CRLF after the request, and |
| 4456 | * this CRLF must be read so that it does not remain in the kernel |
| 4457 | * buffers, otherwise a close could cause an RST on some systems |
| 4458 | * (eg: Linux). |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 4459 | * Note that if we're using keep-alive on the client side, we'd |
| 4460 | * rather poll now and keep the polling enabled for the whole |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4461 | * stream's life than enabling/disabling it between each |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 4462 | * response and next request. |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4463 | */ |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 4464 | if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) && |
| 4465 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) && |
Willy Tarreau | 7aa15b0 | 2017-12-20 16:56:50 +0100 | [diff] [blame] | 4466 | (!(s->be->options & PR_O_ABRT_CLOSE) || |
| 4467 | (s->si[0].flags & SI_FL_CLEAN_ABRT)) && |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 4468 | txn->meth != HTTP_METH_POST) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4469 | channel_dont_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4470 | |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 4471 | /* if the server closes the connection, we want to immediately react |
| 4472 | * and close the socket to save packets and syscalls. |
| 4473 | */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4474 | s->si[1].flags |= SI_FL_NOHALF; |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 4475 | |
Willy Tarreau | 7f876a1 | 2015-11-18 11:59:55 +0100 | [diff] [blame] | 4476 | /* In any case we've finished parsing the request so we must |
| 4477 | * disable Nagle when sending data because 1) we're not going |
| 4478 | * to shut this side, and 2) the server is waiting for us to |
| 4479 | * send pending data. |
| 4480 | */ |
| 4481 | chn->flags |= CF_NEVER_WAIT; |
| 4482 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4483 | if (txn->rsp.msg_state == HTTP_MSG_ERROR) |
| 4484 | goto wait_other_side; |
| 4485 | |
| 4486 | if (txn->rsp.msg_state < HTTP_MSG_DONE) { |
| 4487 | /* The server has not finished to respond, so we |
| 4488 | * don't want to move in order not to upset it. |
| 4489 | */ |
| 4490 | goto wait_other_side; |
| 4491 | } |
| 4492 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4493 | /* When we get here, it means that both the request and the |
| 4494 | * response have finished receiving. Depending on the connection |
| 4495 | * mode, we'll have to wait for the last bytes to leave in either |
| 4496 | * direction, and sometimes for a close to be effective. |
| 4497 | */ |
| 4498 | |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4499 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 4500 | /* Server-close mode : queue a connection close to the server */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4501 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) |
| 4502 | channel_shutw_now(chn); |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4503 | } |
| 4504 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 4505 | /* Option forceclose is set, or either side wants to close, |
| 4506 | * let's enforce it now that we're not expecting any new |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4507 | * data to come. The caller knows the stream is complete |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4508 | * once both states are CLOSED. |
Christopher Faulet | 1486b0a | 2017-07-18 11:42:08 +0200 | [diff] [blame] | 4509 | * |
| 4510 | * However, there is an exception if the response |
| 4511 | * length is undefined. In this case, we need to wait |
| 4512 | * the close from the server. The response will be |
| 4513 | * switched in TUNNEL mode until the end. |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4514 | */ |
Christopher Faulet | 1486b0a | 2017-07-18 11:42:08 +0200 | [diff] [blame] | 4515 | if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) && |
| 4516 | txn->rsp.msg_state != HTTP_MSG_CLOSED) |
| 4517 | goto check_channel_flags; |
| 4518 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4519 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) { |
| 4520 | channel_shutr_now(chn); |
| 4521 | channel_shutw_now(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4522 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4523 | } |
| 4524 | else { |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4525 | /* The last possible modes are keep-alive and tunnel. Tunnel mode |
| 4526 | * will not have any analyser so it needs to poll for reads. |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4527 | */ |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4528 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) { |
| 4529 | channel_auto_read(chn); |
| 4530 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4531 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4532 | } |
| 4533 | |
Christopher Faulet | 4be9803 | 2017-07-18 10:48:24 +0200 | [diff] [blame] | 4534 | goto check_channel_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4535 | } |
| 4536 | |
| 4537 | if (txn->req.msg_state == HTTP_MSG_CLOSING) { |
| 4538 | http_msg_closing: |
| 4539 | /* nothing else to forward, just waiting for the output buffer |
| 4540 | * to be empty and for the shutw_now to take effect. |
| 4541 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4542 | if (channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4543 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 4544 | goto http_msg_closed; |
| 4545 | } |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4546 | else if (chn->flags & CF_SHUTW) { |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 4547 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4548 | txn->req.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4549 | } |
Christopher Faulet | 56d2609 | 2017-07-20 11:05:10 +0200 | [diff] [blame] | 4550 | goto wait_other_side; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4551 | } |
| 4552 | |
| 4553 | if (txn->req.msg_state == HTTP_MSG_CLOSED) { |
| 4554 | http_msg_closed: |
Willy Tarreau | 8059351 | 2017-12-14 10:43:31 +0100 | [diff] [blame] | 4555 | /* if we don't know whether the server will close, we need to hard close */ |
| 4556 | if (txn->rsp.flags & HTTP_MSGF_XFER_LEN) |
| 4557 | s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */ |
| 4558 | |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 4559 | /* see above in MSG_DONE why we only do this in these states */ |
| 4560 | if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) && |
| 4561 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) && |
Willy Tarreau | 7aa15b0 | 2017-12-20 16:56:50 +0100 | [diff] [blame] | 4562 | (!(s->be->options & PR_O_ABRT_CLOSE) || |
| 4563 | (s->si[0].flags & SI_FL_CLEAN_ABRT))) |
Willy Tarreau | 2e7a165 | 2013-12-15 15:32:10 +0100 | [diff] [blame] | 4564 | channel_dont_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4565 | goto wait_other_side; |
| 4566 | } |
| 4567 | |
Christopher Faulet | 4be9803 | 2017-07-18 10:48:24 +0200 | [diff] [blame] | 4568 | check_channel_flags: |
| 4569 | /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */ |
| 4570 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) { |
| 4571 | /* if we've just closed an output, let's switch */ |
Christopher Faulet | 4be9803 | 2017-07-18 10:48:24 +0200 | [diff] [blame] | 4572 | txn->req.msg_state = HTTP_MSG_CLOSING; |
| 4573 | goto http_msg_closing; |
| 4574 | } |
| 4575 | |
| 4576 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4577 | wait_other_side: |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4578 | return txn->req.msg_state != old_state || chn->flags != old_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4579 | } |
| 4580 | |
| 4581 | |
| 4582 | /* This function updates the response state machine according to the request |
| 4583 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 4584 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 4585 | * it is only used to find when a request/response couple is complete. Both |
| 4586 | * this function and its equivalent should loop until both return zero. It |
| 4587 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 4588 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4589 | int http_sync_res_state(struct stream *s) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4590 | { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4591 | struct channel *chn = &s->res; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4592 | struct http_txn *txn = s->txn; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4593 | unsigned int old_flags = chn->flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4594 | unsigned int old_state = txn->rsp.msg_state; |
| 4595 | |
Christopher Faulet | 4be9803 | 2017-07-18 10:48:24 +0200 | [diff] [blame] | 4596 | if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE)) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4597 | return 0; |
| 4598 | |
| 4599 | if (txn->rsp.msg_state == HTTP_MSG_DONE) { |
| 4600 | /* In theory, we don't need to read anymore, but we must |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4601 | * still monitor the server connection for a possible close |
| 4602 | * while the request is being uploaded, so we don't disable |
| 4603 | * reading. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4604 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4605 | /* channel_dont_read(chn); */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4606 | |
| 4607 | if (txn->req.msg_state == HTTP_MSG_ERROR) |
| 4608 | goto wait_other_side; |
| 4609 | |
| 4610 | if (txn->req.msg_state < HTTP_MSG_DONE) { |
| 4611 | /* The client seems to still be sending data, probably |
| 4612 | * because we got an error response during an upload. |
| 4613 | * We have the choice of either breaking the connection |
| 4614 | * or letting it pass through. Let's do the later. |
| 4615 | */ |
| 4616 | goto wait_other_side; |
| 4617 | } |
| 4618 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4619 | /* When we get here, it means that both the request and the |
| 4620 | * response have finished receiving. Depending on the connection |
| 4621 | * mode, we'll have to wait for the last bytes to leave in either |
| 4622 | * direction, and sometimes for a close to be effective. |
| 4623 | */ |
| 4624 | |
| 4625 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 4626 | /* Server-close mode : shut read and wait for the request |
| 4627 | * side to close its output buffer. The caller will detect |
| 4628 | * when we're in DONE and the other is in CLOSED and will |
| 4629 | * catch that for the final cleanup. |
| 4630 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4631 | if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW))) |
| 4632 | channel_shutr_now(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4633 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4634 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 4635 | /* Option forceclose is set, or either side wants to close, |
| 4636 | * let's enforce it now that we're not expecting any new |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4637 | * data to come. The caller knows the stream is complete |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4638 | * once both states are CLOSED. |
Christopher Faulet | 1486b0a | 2017-07-18 11:42:08 +0200 | [diff] [blame] | 4639 | * |
| 4640 | * However, there is an exception if the response length |
| 4641 | * is undefined. In this case, we switch in TUNNEL mode. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4642 | */ |
Christopher Faulet | 1486b0a | 2017-07-18 11:42:08 +0200 | [diff] [blame] | 4643 | if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN)) { |
| 4644 | channel_auto_read(chn); |
| 4645 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
| 4646 | chn->flags |= CF_NEVER_WAIT; |
| 4647 | } |
| 4648 | else if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4649 | channel_shutr_now(chn); |
| 4650 | channel_shutw_now(chn); |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4651 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4652 | } |
| 4653 | else { |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4654 | /* The last possible modes are keep-alive and tunnel. Tunnel will |
| 4655 | * need to forward remaining data. Keep-alive will need to monitor |
| 4656 | * for connection closing. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4657 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4658 | channel_auto_read(chn); |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 4659 | chn->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4660 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) |
| 4661 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4662 | } |
| 4663 | |
Christopher Faulet | 4be9803 | 2017-07-18 10:48:24 +0200 | [diff] [blame] | 4664 | goto check_channel_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4665 | } |
| 4666 | |
| 4667 | if (txn->rsp.msg_state == HTTP_MSG_CLOSING) { |
| 4668 | http_msg_closing: |
| 4669 | /* nothing else to forward, just waiting for the output buffer |
| 4670 | * to be empty and for the shutw_now to take effect. |
| 4671 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4672 | if (channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4673 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 4674 | goto http_msg_closed; |
| 4675 | } |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4676 | else if (chn->flags & CF_SHUTW) { |
Christopher Faulet | a3992e0 | 2017-07-18 10:35:55 +0200 | [diff] [blame] | 4677 | txn->rsp.err_state = txn->rsp.msg_state; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4678 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 4679 | HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4680 | if (objt_server(s->target)) |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 4681 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4682 | } |
Christopher Faulet | 56d2609 | 2017-07-20 11:05:10 +0200 | [diff] [blame] | 4683 | goto wait_other_side; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4684 | } |
| 4685 | |
| 4686 | if (txn->rsp.msg_state == HTTP_MSG_CLOSED) { |
| 4687 | http_msg_closed: |
| 4688 | /* drop any pending data */ |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 4689 | channel_truncate(chn); |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4690 | channel_auto_close(chn); |
| 4691 | channel_auto_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4692 | goto wait_other_side; |
| 4693 | } |
| 4694 | |
Christopher Faulet | 4be9803 | 2017-07-18 10:48:24 +0200 | [diff] [blame] | 4695 | check_channel_flags: |
| 4696 | /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */ |
| 4697 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) { |
| 4698 | /* if we've just closed an output, let's switch */ |
| 4699 | txn->rsp.msg_state = HTTP_MSG_CLOSING; |
| 4700 | goto http_msg_closing; |
| 4701 | } |
| 4702 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4703 | wait_other_side: |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 4704 | /* We force the response to leave immediately if we're waiting for the |
| 4705 | * other side, since there is no pending shutdown to push it out. |
| 4706 | */ |
| 4707 | if (!channel_is_empty(chn)) |
| 4708 | chn->flags |= CF_SEND_DONTWAIT; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4709 | return txn->rsp.msg_state != old_state || chn->flags != old_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4710 | } |
| 4711 | |
| 4712 | |
Christopher Faulet | 894da4c | 2017-07-18 11:29:07 +0200 | [diff] [blame] | 4713 | /* Resync the request and response state machines. */ |
| 4714 | void http_resync_states(struct stream *s) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4715 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4716 | struct http_txn *txn = s->txn; |
Christopher Faulet | 894da4c | 2017-07-18 11:29:07 +0200 | [diff] [blame] | 4717 | #ifdef DEBUG_FULL |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4718 | int old_req_state = txn->req.msg_state; |
| 4719 | int old_res_state = txn->rsp.msg_state; |
Christopher Faulet | 894da4c | 2017-07-18 11:29:07 +0200 | [diff] [blame] | 4720 | #endif |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4721 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4722 | http_sync_req_state(s); |
| 4723 | while (1) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4724 | if (!http_sync_res_state(s)) |
| 4725 | break; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4726 | if (!http_sync_req_state(s)) |
| 4727 | break; |
| 4728 | } |
Willy Tarreau | 3ce10ff | 2014-04-22 08:24:38 +0200 | [diff] [blame] | 4729 | |
Christopher Faulet | 894da4c | 2017-07-18 11:29:07 +0200 | [diff] [blame] | 4730 | DPRINTF(stderr,"[%u] %s: stream=%p old=%s,%s cur=%s,%s " |
| 4731 | "req->analysers=0x%08x res->analysers=0x%08x\n", |
| 4732 | now_ms, __FUNCTION__, s, |
Willy Tarreau | 0da5b3b | 2017-09-21 09:30:46 +0200 | [diff] [blame] | 4733 | h1_msg_state_str(old_req_state), h1_msg_state_str(old_res_state), |
| 4734 | 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] | 4735 | s->req.analysers, s->res.analysers); |
Christopher Faulet | 814d270 | 2017-03-30 11:33:44 +0200 | [diff] [blame] | 4736 | |
| 4737 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4738 | /* OK, both state machines agree on a compatible state. |
| 4739 | * There are a few cases we're interested in : |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4740 | * - HTTP_MSG_CLOSED on both sides means we've reached the end in both |
| 4741 | * directions, so let's simply disable both analysers. |
Christopher Faulet | f77bb53 | 2017-07-18 11:18:46 +0200 | [diff] [blame] | 4742 | * - HTTP_MSG_CLOSED on the response only or HTTP_MSG_ERROR on either |
| 4743 | * means we must abort the request. |
| 4744 | * - HTTP_MSG_TUNNEL on either means we have to disable analyser on |
| 4745 | * corresponding channel. |
| 4746 | * - HTTP_MSG_DONE or HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE |
| 4747 | * on the response with server-close mode means we've completed one |
| 4748 | * request and we must re-initialize the server connection. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4749 | */ |
Christopher Faulet | f77bb53 | 2017-07-18 11:18:46 +0200 | [diff] [blame] | 4750 | if (txn->req.msg_state == HTTP_MSG_CLOSED && |
| 4751 | txn->rsp.msg_state == HTTP_MSG_CLOSED) { |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4752 | s->req.analysers &= AN_REQ_FLT_END; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4753 | channel_auto_close(&s->req); |
| 4754 | channel_auto_read(&s->req); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4755 | s->res.analysers &= AN_RES_FLT_END; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4756 | channel_auto_close(&s->res); |
| 4757 | channel_auto_read(&s->res); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4758 | } |
Christopher Faulet | f77bb53 | 2017-07-18 11:18:46 +0200 | [diff] [blame] | 4759 | else if (txn->rsp.msg_state == HTTP_MSG_CLOSED || |
| 4760 | txn->rsp.msg_state == HTTP_MSG_ERROR || |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 4761 | txn->req.msg_state == HTTP_MSG_ERROR) { |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4762 | s->res.analysers &= AN_RES_FLT_END; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4763 | channel_auto_close(&s->res); |
| 4764 | channel_auto_read(&s->res); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4765 | s->req.analysers &= AN_REQ_FLT_END; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4766 | channel_abort(&s->req); |
| 4767 | channel_auto_close(&s->req); |
| 4768 | channel_auto_read(&s->req); |
| 4769 | channel_truncate(&s->req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4770 | } |
Christopher Faulet | f77bb53 | 2017-07-18 11:18:46 +0200 | [diff] [blame] | 4771 | else if (txn->req.msg_state == HTTP_MSG_TUNNEL || |
| 4772 | txn->rsp.msg_state == HTTP_MSG_TUNNEL) { |
| 4773 | if (txn->req.msg_state == HTTP_MSG_TUNNEL) { |
| 4774 | s->req.analysers &= AN_REQ_FLT_END; |
| 4775 | if (HAS_REQ_DATA_FILTERS(s)) |
| 4776 | s->req.analysers |= AN_REQ_FLT_XFER_DATA; |
| 4777 | } |
| 4778 | if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) { |
| 4779 | s->res.analysers &= AN_RES_FLT_END; |
| 4780 | if (HAS_RSP_DATA_FILTERS(s)) |
| 4781 | s->res.analysers |= AN_RES_FLT_XFER_DATA; |
| 4782 | } |
| 4783 | channel_auto_close(&s->req); |
| 4784 | channel_auto_read(&s->req); |
| 4785 | channel_auto_close(&s->res); |
| 4786 | channel_auto_read(&s->res); |
| 4787 | } |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4788 | else if ((txn->req.msg_state == HTTP_MSG_DONE || |
| 4789 | txn->req.msg_state == HTTP_MSG_CLOSED) && |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4790 | txn->rsp.msg_state == HTTP_MSG_DONE && |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4791 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL || |
| 4792 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) { |
| 4793 | /* server-close/keep-alive: terminate this transaction, |
| 4794 | * possibly killing the server connection and reinitialize |
Willy Tarreau | 3de5bd6 | 2016-05-02 16:07:07 +0200 | [diff] [blame] | 4795 | * a fresh-new transaction, but only once we're sure there's |
| 4796 | * enough room in the request and response buffer to process |
Christopher Faulet | c0c672a | 2017-03-28 11:51:33 +0200 | [diff] [blame] | 4797 | * another request. They must not hold any pending output data |
| 4798 | * and the response buffer must realigned |
| 4799 | * (realign is done is http_end_txn_clean_session). |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4800 | */ |
Christopher Faulet | 894da4c | 2017-07-18 11:29:07 +0200 | [diff] [blame] | 4801 | if (s->req.buf->o) |
Willy Tarreau | 3de5bd6 | 2016-05-02 16:07:07 +0200 | [diff] [blame] | 4802 | s->req.flags |= CF_WAKE_WRITE; |
Christopher Faulet | 894da4c | 2017-07-18 11:29:07 +0200 | [diff] [blame] | 4803 | else if (s->res.buf->o) |
Willy Tarreau | 3de5bd6 | 2016-05-02 16:07:07 +0200 | [diff] [blame] | 4804 | s->res.flags |= CF_WAKE_WRITE; |
Christopher Faulet | a81ff60 | 2017-07-18 22:01:05 +0200 | [diff] [blame] | 4805 | else { |
| 4806 | s->req.analysers = AN_REQ_FLT_END; |
| 4807 | s->res.analysers = AN_RES_FLT_END; |
| 4808 | txn->flags |= TX_WAIT_CLEANUP; |
| 4809 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4810 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4811 | } |
| 4812 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4813 | /* This function is an analyser which forwards request body (including chunk |
| 4814 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 4815 | * zero byte. The only situation where it must not be called is when we're in |
| 4816 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 4817 | * remaining data and to resync after end of body. It expects the msg_state to |
| 4818 | * 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] | 4819 | * 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] | 4820 | * 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] | 4821 | * bytes of pending data + the headers if not already done. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4822 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4823 | 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] | 4824 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4825 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4826 | struct http_txn *txn = s->txn; |
| 4827 | struct http_msg *msg = &s->txn->req; |
Christopher Faulet | 3e34429 | 2015-11-24 16:24:13 +0100 | [diff] [blame] | 4828 | int ret; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4829 | |
Christopher Faulet | 814d270 | 2017-03-30 11:33:44 +0200 | [diff] [blame] | 4830 | DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n", |
| 4831 | now_ms, __FUNCTION__, |
| 4832 | s, |
| 4833 | req, |
| 4834 | req->rex, req->wex, |
| 4835 | req->flags, |
| 4836 | req->buf->i, |
| 4837 | req->analysers); |
| 4838 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4839 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4840 | return 0; |
| 4841 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4842 | 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] | 4843 | ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) { |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 4844 | /* Output closed while we were sending data. We must abort and |
| 4845 | * wake the other side up. |
| 4846 | */ |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 4847 | msg->err_state = msg->msg_state; |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 4848 | msg->msg_state = HTTP_MSG_ERROR; |
| 4849 | http_resync_states(s); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 4850 | return 1; |
| 4851 | } |
| 4852 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4853 | /* Note that we don't have to send 100-continue back because we don't |
| 4854 | * need the data to complete our job, and it's up to the server to |
| 4855 | * decide whether to return 100, 417 or anything else in return of |
| 4856 | * an "Expect: 100-continue" header. |
| 4857 | */ |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 4858 | if (msg->msg_state == HTTP_MSG_BODY) { |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4859 | msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK) |
| 4860 | ? HTTP_MSG_CHUNK_SIZE |
| 4861 | : HTTP_MSG_DATA); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 4862 | |
| 4863 | /* TODO/filters: when http-buffer-request option is set or if a |
| 4864 | * rule on url_param exists, the first chunk size could be |
| 4865 | * already parsed. In that case, msg->next is after the chunk |
| 4866 | * size (including the CRLF after the size). So this case should |
| 4867 | * be handled to */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4868 | } |
| 4869 | |
Willy Tarreau | 7ba2354 | 2014-04-17 21:50:00 +0200 | [diff] [blame] | 4870 | /* Some post-connect processing might want us to refrain from starting to |
| 4871 | * forward data. Currently, the only reason for this is "balance url_param" |
| 4872 | * whichs need to parse/process the request after we've enabled forwarding. |
| 4873 | */ |
| 4874 | if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4875 | if (!(s->res.flags & CF_READ_ATTACHED)) { |
Willy Tarreau | 7ba2354 | 2014-04-17 21:50:00 +0200 | [diff] [blame] | 4876 | channel_auto_connect(req); |
Willy Tarreau | 644c101 | 2014-04-30 18:11:11 +0200 | [diff] [blame] | 4877 | req->flags |= CF_WAKE_CONNECT; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4878 | goto missing_data_or_waiting; |
Willy Tarreau | 7ba2354 | 2014-04-17 21:50:00 +0200 | [diff] [blame] | 4879 | } |
| 4880 | msg->flags &= ~HTTP_MSGF_WAIT_CONN; |
| 4881 | } |
| 4882 | |
Willy Tarreau | 80a92c0 | 2014-03-12 10:41:13 +0100 | [diff] [blame] | 4883 | /* in most states, we should abort in case of early close */ |
| 4884 | channel_auto_close(req); |
| 4885 | |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 4886 | if (req->to_forward) { |
| 4887 | /* We can't process the buffer's contents yet */ |
| 4888 | req->flags |= CF_WAKE_WRITE; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4889 | goto missing_data_or_waiting; |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 4890 | } |
| 4891 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4892 | if (msg->msg_state < HTTP_MSG_DONE) { |
| 4893 | ret = ((msg->flags & HTTP_MSGF_TE_CHNK) |
| 4894 | ? http_msg_forward_chunked_body(s, msg) |
| 4895 | : http_msg_forward_body(s, msg)); |
| 4896 | if (!ret) |
| 4897 | goto missing_data_or_waiting; |
| 4898 | if (ret < 0) |
| 4899 | goto return_bad_req; |
| 4900 | } |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 4901 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4902 | /* other states, DONE...TUNNEL */ |
| 4903 | /* we don't want to forward closes on DONE except in tunnel mode. */ |
| 4904 | if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) |
| 4905 | channel_dont_close(req); |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 4906 | |
Christopher Faulet | 894da4c | 2017-07-18 11:29:07 +0200 | [diff] [blame] | 4907 | http_resync_states(s); |
| 4908 | if (!(req->analysers & an_bit)) { |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4909 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 4910 | if (req->flags & CF_SHUTW) { |
| 4911 | /* request errors are most likely due to the |
| 4912 | * server aborting the transfer. */ |
| 4913 | goto aborted_xfer; |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 4914 | } |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4915 | if (msg->err_pos >= 0) |
Emeric Brun | 8c1aaa2 | 2017-06-15 11:30:06 +0200 | [diff] [blame] | 4916 | 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] | 4917 | goto return_bad_req; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4918 | } |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4919 | return 1; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4920 | } |
| 4921 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4922 | /* If "option abortonclose" is set on the backend, we want to monitor |
| 4923 | * the client's connection and forward any shutdown notification to the |
| 4924 | * server, which will decide whether to close or to go on processing the |
| 4925 | * request. We only do that in tunnel mode, and not in other modes since |
| 4926 | * it can be abused to exhaust source ports. */ |
Willy Tarreau | 7aa15b0 | 2017-12-20 16:56:50 +0100 | [diff] [blame] | 4927 | 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] | 4928 | channel_auto_read(req); |
| 4929 | if ((req->flags & (CF_SHUTR|CF_READ_NULL)) && |
| 4930 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)) |
| 4931 | s->si[1].flags |= SI_FL_NOLINGER; |
| 4932 | channel_auto_close(req); |
| 4933 | } |
| 4934 | else if (s->txn->meth == HTTP_METH_POST) { |
| 4935 | /* POST requests may require to read extra CRLF sent by broken |
| 4936 | * browsers and which could cause an RST to be sent upon close |
| 4937 | * on some systems (eg: Linux). */ |
| 4938 | channel_auto_read(req); |
| 4939 | } |
| 4940 | return 0; |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 4941 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4942 | missing_data_or_waiting: |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4943 | /* stop waiting for data if the input is closed before the end */ |
Christopher Faulet | a33510b | 2017-03-31 15:37:29 +0200 | [diff] [blame] | 4944 | if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR) { |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4945 | if (!(s->flags & SF_ERR_MASK)) |
| 4946 | s->flags |= SF_ERR_CLICL; |
| 4947 | if (!(s->flags & SF_FINST_MASK)) { |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4948 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4949 | s->flags |= SF_FINST_H; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4950 | else |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4951 | s->flags |= SF_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4952 | } |
| 4953 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 4954 | HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1); |
| 4955 | HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4956 | if (objt_server(s->target)) |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 4957 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4958 | |
| 4959 | goto return_bad_req_stats_ok; |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 4960 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4961 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4962 | /* waiting for the last bits to leave the buffer */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4963 | if (req->flags & CF_SHUTW) |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4964 | goto aborted_xfer; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4965 | |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 4966 | /* 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] | 4967 | * 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] | 4968 | * And when content-length is used, we never want to let the possible |
| 4969 | * shutdown be forwarded to the other side, as the state machine will |
| 4970 | * take care of it once the client responds. It's also important to |
| 4971 | * prevent TIME_WAITs from accumulating on the backend side, and for |
| 4972 | * HTTP/2 where the last frame comes with a shutdown. |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 4973 | */ |
Willy Tarreau | ff47b3f | 2017-12-29 16:30:31 +0100 | [diff] [blame] | 4974 | if (msg->flags & (HTTP_MSGF_TE_CHNK|HTTP_MSGF_CNT_LEN)) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4975 | channel_dont_close(req); |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 4976 | |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 4977 | /* 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] | 4978 | * 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] | 4979 | * 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] | 4980 | * modes are already handled by the stream sock layer. We must not do |
| 4981 | * this in content-length mode because it could present the MSG_MORE |
| 4982 | * flag with the last block of forwarded data, which would cause an |
| 4983 | * additional delay to be observed by the receiver. |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 4984 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4985 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4986 | req->flags |= CF_EXPECT_MORE; |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 4987 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4988 | return 0; |
| 4989 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4990 | return_bad_req: /* let's centralize all bad requests */ |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 4991 | HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4992 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 4993 | HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1); |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 4994 | |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4995 | return_bad_req_stats_ok: |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 4996 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4997 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4998 | if (txn->status) { |
| 4999 | /* 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] | 5000 | http_reply_and_close(s, txn->status, NULL); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5001 | } else { |
| 5002 | txn->status = 400; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 5003 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5004 | } |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 5005 | req->analysers &= AN_REQ_FLT_END; |
| 5006 | 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] | 5007 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5008 | if (!(s->flags & SF_ERR_MASK)) |
| 5009 | s->flags |= SF_ERR_PRXCOND; |
| 5010 | if (!(s->flags & SF_FINST_MASK)) { |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5011 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5012 | s->flags |= SF_FINST_H; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5013 | else |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5014 | s->flags |= SF_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5015 | } |
| 5016 | return 0; |
| 5017 | |
| 5018 | aborted_xfer: |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 5019 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5020 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 5021 | if (txn->status) { |
| 5022 | /* 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] | 5023 | http_reply_and_close(s, txn->status, NULL); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5024 | } else { |
| 5025 | txn->status = 502; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 5026 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5027 | } |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 5028 | req->analysers &= AN_REQ_FLT_END; |
| 5029 | 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] | 5030 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 5031 | HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1); |
| 5032 | HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5033 | if (objt_server(s->target)) |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 5034 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5035 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5036 | if (!(s->flags & SF_ERR_MASK)) |
| 5037 | s->flags |= SF_ERR_SRVCL; |
| 5038 | if (!(s->flags & SF_FINST_MASK)) { |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5039 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5040 | s->flags |= SF_FINST_H; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5041 | else |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5042 | s->flags |= SF_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5043 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5044 | return 0; |
| 5045 | } |
| 5046 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5047 | /* This stream analyser waits for a complete HTTP response. It returns 1 if the |
| 5048 | * processing can continue on next analysers, or zero if it either needs more |
| 5049 | * data or wants to immediately abort the response (eg: timeout, error, ...). It |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5050 | * 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] | 5051 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 5052 | * abort. |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 5053 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5054 | 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] | 5055 | { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5056 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5057 | struct http_txn *txn = s->txn; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5058 | struct http_msg *msg = &txn->rsp; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5059 | struct hdr_ctx ctx; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5060 | int use_close_only; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5061 | int cur_idx; |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 5062 | int n; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 5063 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5064 | 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] | 5065 | now_ms, __FUNCTION__, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5066 | s, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 5067 | rep, |
| 5068 | rep->rex, rep->wex, |
| 5069 | rep->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5070 | rep->buf->i, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 5071 | rep->analysers); |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 5072 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5073 | /* |
| 5074 | * Now parse the partial (or complete) lines. |
| 5075 | * We will check the response syntax, and also join multi-line |
| 5076 | * headers. An index of all the lines will be elaborated while |
| 5077 | * parsing. |
| 5078 | * |
| 5079 | * For the parsing, we use a 28 states FSM. |
| 5080 | * |
| 5081 | * Here is the information we currently have : |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5082 | * rep->buf->p = beginning of response |
| 5083 | * rep->buf->p + msg->eoh = end of processed headers / start of current one |
| 5084 | * rep->buf->p + rep->buf->i = end of input data |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 5085 | * msg->eol = end of current header or line (LF or CRLF) |
| 5086 | * msg->next = first non-visited byte |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5087 | */ |
| 5088 | |
Willy Tarreau | 628c40c | 2014-04-24 19:11:26 +0200 | [diff] [blame] | 5089 | next_one: |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 5090 | /* There's a protected area at the end of the buffer for rewriting |
| 5091 | * purposes. We don't want to start to parse the request if the |
| 5092 | * protected area is affected, because we may have to move processed |
| 5093 | * data later, which is much more complicated. |
| 5094 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5095 | if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) { |
Willy Tarreau | ba0902e | 2015-01-13 14:39:16 +0100 | [diff] [blame] | 5096 | if (unlikely(!channel_is_rewritable(rep))) { |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 5097 | /* some data has still not left the buffer, wake us once that's done */ |
| 5098 | if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) |
| 5099 | goto abort_response; |
| 5100 | channel_dont_close(rep); |
| 5101 | rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | d7ad9f5 | 2013-12-31 17:26:25 +0100 | [diff] [blame] | 5102 | rep->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 5103 | return 0; |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 5104 | } |
| 5105 | |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 5106 | if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) || |
| 5107 | bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite)) |
| 5108 | buffer_slow_realign(rep->buf); |
| 5109 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5110 | if (likely(msg->next < rep->buf->i)) |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 5111 | http_msg_analyzer(msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 5112 | } |
| 5113 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5114 | /* 1: we might have to print this header in debug mode */ |
| 5115 | if (unlikely((global.mode & MODE_DEBUG) && |
| 5116 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | 7d59e90 | 2014-10-21 19:36:09 +0200 | [diff] [blame] | 5117 | msg->msg_state >= HTTP_MSG_BODY)) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5118 | char *eol, *sol; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5119 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5120 | sol = rep->buf->p; |
| 5121 | 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] | 5122 | debug_hdr("srvrep", s, sol, eol); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5123 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5124 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 5125 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
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 | while (cur_idx) { |
| 5128 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 5129 | debug_hdr("srvhdr", s, sol, eol); |
| 5130 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 5131 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
| 5132 | } |
| 5133 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5134 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5135 | /* |
| 5136 | * Now we quickly check if we have found a full valid response. |
| 5137 | * If not so, we check the FD and buffer states before leaving. |
| 5138 | * A full response is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 5139 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5140 | * responses are checked first. |
| 5141 | * |
| 5142 | * Depending on whether the client is still there or not, we |
| 5143 | * may send an error response back or not. Note that normally |
| 5144 | * we should only check for HTTP status there, and check I/O |
| 5145 | * errors somewhere else. |
| 5146 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5147 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 5148 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5149 | /* Invalid response */ |
| 5150 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 5151 | /* we detected a parsing error. We want to archive this response |
| 5152 | * in the dedicated proxy area for later troubleshooting. |
| 5153 | */ |
| 5154 | hdr_response_bad: |
| 5155 | if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0) |
Emeric Brun | 8c1aaa2 | 2017-06-15 11:30:06 +0200 | [diff] [blame] | 5156 | 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] | 5157 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 5158 | HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5159 | if (objt_server(s->target)) { |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 5160 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5161 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5162 | } |
Willy Tarreau | 6464841 | 2010-03-05 10:41:54 +0100 | [diff] [blame] | 5163 | abort_response: |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5164 | channel_auto_close(rep); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 5165 | rep->analysers &= AN_RES_FLT_END; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5166 | txn->status = 502; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5167 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5168 | channel_truncate(rep); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 5169 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5170 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5171 | if (!(s->flags & SF_ERR_MASK)) |
| 5172 | s->flags |= SF_ERR_PRXCOND; |
| 5173 | if (!(s->flags & SF_FINST_MASK)) |
| 5174 | s->flags |= SF_FINST_H; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5175 | |
| 5176 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5177 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5178 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5179 | /* too large response does not fit in buffer. */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5180 | else if (buffer_full(rep->buf, global.tune.maxrewrite)) { |
Willy Tarreau | fec4d89 | 2011-09-02 20:04:57 +0200 | [diff] [blame] | 5181 | if (msg->err_pos < 0) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5182 | msg->err_pos = rep->buf->i; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5183 | goto hdr_response_bad; |
| 5184 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5185 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5186 | /* read error */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5187 | else if (rep->flags & CF_READ_ERROR) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5188 | if (msg->err_pos >= 0) |
Emeric Brun | 8c1aaa2 | 2017-06-15 11:30:06 +0200 | [diff] [blame] | 5189 | 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] | 5190 | else if (txn->flags & TX_NOT_FIRST) |
| 5191 | goto abort_keep_alive; |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 5192 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 5193 | HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5194 | if (objt_server(s->target)) { |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 5195 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5196 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5197 | } |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 5198 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5199 | channel_auto_close(rep); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 5200 | rep->analysers &= AN_RES_FLT_END; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5201 | txn->status = 502; |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 5202 | |
| 5203 | /* Check to see if the server refused the early data. |
| 5204 | * If so, just send a 425 |
| 5205 | */ |
| 5206 | if (objt_cs(s->si[1].end)) { |
| 5207 | struct connection *conn = objt_cs(s->si[1].end)->conn; |
| 5208 | |
| 5209 | if (conn->err_code == CO_ER_SSL_EARLY_FAILED) |
| 5210 | txn->status = 425; |
| 5211 | } |
| 5212 | |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5213 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5214 | channel_truncate(rep); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 5215 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 5216 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5217 | if (!(s->flags & SF_ERR_MASK)) |
| 5218 | s->flags |= SF_ERR_SRVCL; |
| 5219 | if (!(s->flags & SF_FINST_MASK)) |
| 5220 | s->flags |= SF_FINST_H; |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 5221 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5222 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5223 | |
Willy Tarreau | 6f0a7ba | 2014-06-23 15:22:31 +0200 | [diff] [blame] | 5224 | /* read timeout : return a 504 to the client. */ |
| 5225 | else if (rep->flags & CF_READ_TIMEOUT) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5226 | if (msg->err_pos >= 0) |
Emeric Brun | 8c1aaa2 | 2017-06-15 11:30:06 +0200 | [diff] [blame] | 5227 | 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] | 5228 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 5229 | HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5230 | if (objt_server(s->target)) { |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 5231 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5232 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5233 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5234 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5235 | channel_auto_close(rep); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 5236 | rep->analysers &= AN_RES_FLT_END; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5237 | txn->status = 504; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5238 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5239 | channel_truncate(rep); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 5240 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 5241 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5242 | if (!(s->flags & SF_ERR_MASK)) |
| 5243 | s->flags |= SF_ERR_SRVTO; |
| 5244 | if (!(s->flags & SF_FINST_MASK)) |
| 5245 | s->flags |= SF_FINST_H; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5246 | return 0; |
| 5247 | } |
Willy Tarreau | a7c5276 | 2008-08-16 18:40:18 +0200 | [diff] [blame] | 5248 | |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 5249 | /* client abort with an abortonclose */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5250 | 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] | 5251 | HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1); |
| 5252 | HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1); |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 5253 | if (objt_server(s->target)) |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 5254 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1); |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 5255 | |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 5256 | rep->analysers &= AN_RES_FLT_END; |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 5257 | channel_auto_close(rep); |
| 5258 | |
| 5259 | txn->status = 400; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5260 | channel_truncate(rep); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 5261 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 5262 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5263 | if (!(s->flags & SF_ERR_MASK)) |
| 5264 | s->flags |= SF_ERR_CLICL; |
| 5265 | if (!(s->flags & SF_FINST_MASK)) |
| 5266 | s->flags |= SF_FINST_H; |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 5267 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5268 | /* process_stream() will take care of the error */ |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 5269 | return 0; |
| 5270 | } |
| 5271 | |
Willy Tarreau | 3b8c08a | 2011-09-02 20:16:24 +0200 | [diff] [blame] | 5272 | /* 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] | 5273 | else if (rep->flags & CF_SHUTR) { |
Willy Tarreau | 3b8c08a | 2011-09-02 20:16:24 +0200 | [diff] [blame] | 5274 | if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0) |
Emeric Brun | 8c1aaa2 | 2017-06-15 11:30:06 +0200 | [diff] [blame] | 5275 | 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] | 5276 | else if (txn->flags & TX_NOT_FIRST) |
| 5277 | goto abort_keep_alive; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5278 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 5279 | HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5280 | if (objt_server(s->target)) { |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 5281 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5282 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5283 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5284 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5285 | channel_auto_close(rep); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 5286 | rep->analysers &= AN_RES_FLT_END; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5287 | txn->status = 502; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5288 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5289 | channel_truncate(rep); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 5290 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5291 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5292 | if (!(s->flags & SF_ERR_MASK)) |
| 5293 | s->flags |= SF_ERR_SRVCL; |
| 5294 | if (!(s->flags & SF_FINST_MASK)) |
| 5295 | s->flags |= SF_FINST_H; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5296 | return 0; |
| 5297 | } |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 5298 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5299 | /* write error to client (we don't send any message then) */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5300 | else if (rep->flags & CF_WRITE_ERROR) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5301 | if (msg->err_pos >= 0) |
Emeric Brun | 8c1aaa2 | 2017-06-15 11:30:06 +0200 | [diff] [blame] | 5302 | 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] | 5303 | else if (txn->flags & TX_NOT_FIRST) |
| 5304 | goto abort_keep_alive; |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 5305 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 5306 | HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 5307 | rep->analysers &= AN_RES_FLT_END; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5308 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5309 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5310 | if (!(s->flags & SF_ERR_MASK)) |
| 5311 | s->flags |= SF_ERR_CLICL; |
| 5312 | if (!(s->flags & SF_FINST_MASK)) |
| 5313 | s->flags |= SF_FINST_H; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5314 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5315 | /* process_stream() will take care of the error */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5316 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5317 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5318 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5319 | channel_dont_close(rep); |
Willy Tarreau | 3f3997e | 2013-12-15 15:21:32 +0100 | [diff] [blame] | 5320 | rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5321 | return 0; |
| 5322 | } |
| 5323 | |
| 5324 | /* More interesting part now : we know that we have a complete |
| 5325 | * response which at least looks like HTTP. We have an indicator |
| 5326 | * of each header's length, so we can parse them quickly. |
| 5327 | */ |
| 5328 | |
| 5329 | if (unlikely(msg->err_pos >= 0)) |
Emeric Brun | 8c1aaa2 | 2017-06-15 11:30:06 +0200 | [diff] [blame] | 5330 | 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] | 5331 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5332 | /* |
| 5333 | * 1: get the status code |
| 5334 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5335 | n = rep->buf->p[msg->sl.st.c] - '0'; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5336 | if (n < 1 || n > 5) |
| 5337 | n = 0; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5338 | /* when the client triggers a 4xx from the server, it's most often due |
| 5339 | * to a missing object or permission. These events should be tracked |
| 5340 | * because if they happen often, it may indicate a brute force or a |
| 5341 | * vulnerability scan. |
| 5342 | */ |
| 5343 | if (n == 4) |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5344 | stream_inc_http_err_ctr(s); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5345 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5346 | if (objt_server(s->target)) |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 5347 | 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] | 5348 | |
Willy Tarreau | 91852eb | 2015-05-01 13:26:00 +0200 | [diff] [blame] | 5349 | /* RFC7230#2.6 has enforced the format of the HTTP version string to be |
| 5350 | * exactly one digit "." one digit. This check may be disabled using |
| 5351 | * option accept-invalid-http-response. |
| 5352 | */ |
| 5353 | if (!(s->be->options2 & PR_O2_RSPBUG_OK)) { |
| 5354 | if (msg->sl.st.v_l != 8) { |
| 5355 | msg->err_pos = 0; |
| 5356 | goto hdr_response_bad; |
| 5357 | } |
| 5358 | |
| 5359 | if (rep->buf->p[4] != '/' || |
| 5360 | !isdigit((unsigned char)rep->buf->p[5]) || |
| 5361 | rep->buf->p[6] != '.' || |
| 5362 | !isdigit((unsigned char)rep->buf->p[7])) { |
| 5363 | msg->err_pos = 4; |
| 5364 | goto hdr_response_bad; |
| 5365 | } |
| 5366 | } |
| 5367 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5368 | /* check if the response is HTTP/1.1 or above */ |
| 5369 | if ((msg->sl.st.v_l == 8) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5370 | ((rep->buf->p[5] > '1') || |
| 5371 | ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1')))) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5372 | msg->flags |= HTTP_MSGF_VER_11; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5373 | |
| 5374 | /* "connection" has not been parsed yet */ |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 5375 | 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] | 5376 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5377 | /* transfer length unknown*/ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5378 | msg->flags &= ~HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5379 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5380 | 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] | 5381 | |
Willy Tarreau | 3965040 | 2010-03-15 19:44:39 +0100 | [diff] [blame] | 5382 | /* Adjust server's health based on status code. Note: status codes 501 |
| 5383 | * and 505 are triggered on demand by client request, so we must not |
| 5384 | * count them as server failures. |
| 5385 | */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5386 | if (objt_server(s->target)) { |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 5387 | 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] | 5388 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK); |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 5389 | else |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5390 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS); |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 5391 | } |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5392 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5393 | /* |
Willy Tarreau | a14ad72 | 2017-07-07 11:36:32 +0200 | [diff] [blame] | 5394 | * We may be facing a 100-continue response, or any other informational |
| 5395 | * 1xx response which is non-final, in which case this is not the right |
| 5396 | * response, and we're waiting for the next one. Let's allow this response |
| 5397 | * to go to the client and wait for the next one. There's an exception for |
| 5398 | * 101 which is used later in the code to switch protocols. |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5399 | */ |
Willy Tarreau | a14ad72 | 2017-07-07 11:36:32 +0200 | [diff] [blame] | 5400 | if (txn->status < 200 && |
| 5401 | (txn->status == 100 || txn->status >= 102)) { |
Willy Tarreau | 628c40c | 2014-04-24 19:11:26 +0200 | [diff] [blame] | 5402 | hdr_idx_init(&txn->hdr_idx); |
| 5403 | msg->next -= channel_forward(rep, msg->next); |
| 5404 | msg->msg_state = HTTP_MSG_RPBEFORE; |
| 5405 | txn->status = 0; |
| 5406 | s->logs.t_data = -1; /* was not a response yet */ |
Christopher Faulet | 3e34429 | 2015-11-24 16:24:13 +0100 | [diff] [blame] | 5407 | FLT_STRM_CB(s, flt_http_reset(s, msg)); |
Willy Tarreau | 628c40c | 2014-04-24 19:11:26 +0200 | [diff] [blame] | 5408 | goto next_one; |
Willy Tarreau | a14ad72 | 2017-07-07 11:36:32 +0200 | [diff] [blame] | 5409 | } |
Willy Tarreau | 628c40c | 2014-04-24 19:11:26 +0200 | [diff] [blame] | 5410 | |
Willy Tarreau | a14ad72 | 2017-07-07 11:36:32 +0200 | [diff] [blame] | 5411 | /* |
| 5412 | * 2: check for cacheability. |
| 5413 | */ |
| 5414 | |
| 5415 | switch (txn->status) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5416 | case 200: |
| 5417 | case 203: |
Willy Tarreau | c55ddce | 2017-12-21 11:41:38 +0100 | [diff] [blame] | 5418 | case 204: |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5419 | case 206: |
| 5420 | case 300: |
| 5421 | case 301: |
Willy Tarreau | c55ddce | 2017-12-21 11:41:38 +0100 | [diff] [blame] | 5422 | case 404: |
| 5423 | case 405: |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5424 | case 410: |
Willy Tarreau | c55ddce | 2017-12-21 11:41:38 +0100 | [diff] [blame] | 5425 | case 414: |
| 5426 | case 501: |
Willy Tarreau | 83ece46 | 2017-12-21 15:13:09 +0100 | [diff] [blame] | 5427 | break; |
| 5428 | default: |
Willy Tarreau | c55ddce | 2017-12-21 11:41:38 +0100 | [diff] [blame] | 5429 | /* RFC7231#6.1: |
| 5430 | * Responses with status codes that are defined as |
| 5431 | * cacheable by default (e.g., 200, 203, 204, 206, |
| 5432 | * 300, 301, 404, 405, 410, 414, and 501 in this |
| 5433 | * specification) can be reused by a cache with |
| 5434 | * heuristic expiration unless otherwise indicated |
| 5435 | * by the method definition or explicit cache |
| 5436 | * controls [RFC7234]; all other status codes are |
| 5437 | * not cacheable by default. |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5438 | */ |
Willy Tarreau | 83ece46 | 2017-12-21 15:13:09 +0100 | [diff] [blame] | 5439 | txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5440 | break; |
| 5441 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5442 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5443 | /* |
| 5444 | * 3: we may need to capture headers |
| 5445 | */ |
| 5446 | s->logs.logwait &= ~LW_RESP; |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 5447 | if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap)) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5448 | capture_headers(rep->buf->p, &txn->hdr_idx, |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 5449 | s->res_cap, sess->fe->rsp_cap); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5450 | |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 5451 | /* 4: determine the transfer-length according to RFC2616 #4.4, updated |
| 5452 | * by RFC7230#3.3.3 : |
| 5453 | * |
| 5454 | * The length of a message body is determined by one of the following |
| 5455 | * (in order of precedence): |
| 5456 | * |
Christopher Faulet | d1cd209 | 2016-11-28 10:14:03 +0100 | [diff] [blame] | 5457 | * 1. Any 2xx (Successful) response to a CONNECT request implies that |
| 5458 | * the connection will become a tunnel immediately after the empty |
| 5459 | * line that concludes the header fields. A client MUST ignore |
| 5460 | * any Content-Length or Transfer-Encoding header fields received |
| 5461 | * in such a message. Any 101 response (Switching Protocols) is |
| 5462 | * managed in the same manner. |
| 5463 | * |
| 5464 | * 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] | 5465 | * (Informational), 204 (No Content), or 304 (Not Modified) status |
| 5466 | * code is always terminated by the first empty line after the |
| 5467 | * header fields, regardless of the header fields present in the |
| 5468 | * message, and thus cannot contain a message body. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5469 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 5470 | * 3. If a Transfer-Encoding header field is present and the chunked |
| 5471 | * transfer coding (Section 4.1) is the final encoding, the message |
| 5472 | * body length is determined by reading and decoding the chunked |
| 5473 | * data until the transfer coding indicates the data is complete. |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5474 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 5475 | * If a Transfer-Encoding header field is present in a response and |
| 5476 | * the chunked transfer coding is not the final encoding, the |
| 5477 | * message body length is determined by reading the connection until |
| 5478 | * it is closed by the server. If a Transfer-Encoding header field |
| 5479 | * is present in a request and the chunked transfer coding is not |
| 5480 | * the final encoding, the message body length cannot be determined |
| 5481 | * reliably; the server MUST respond with the 400 (Bad Request) |
| 5482 | * status code and then close the connection. |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5483 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 5484 | * If a message is received with both a Transfer-Encoding and a |
| 5485 | * Content-Length header field, the Transfer-Encoding overrides the |
| 5486 | * Content-Length. Such a message might indicate an attempt to |
| 5487 | * perform request smuggling (Section 9.5) or response splitting |
| 5488 | * (Section 9.4) and ought to be handled as an error. A sender MUST |
| 5489 | * remove the received Content-Length field prior to forwarding such |
| 5490 | * a message downstream. |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5491 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 5492 | * 4. If a message is received without Transfer-Encoding and with |
| 5493 | * either multiple Content-Length header fields having differing |
| 5494 | * field-values or a single Content-Length header field having an |
| 5495 | * invalid value, then the message framing is invalid and the |
| 5496 | * recipient MUST treat it as an unrecoverable error. If this is a |
| 5497 | * request message, the server MUST respond with a 400 (Bad Request) |
| 5498 | * status code and then close the connection. If this is a response |
| 5499 | * message received by a proxy, the proxy MUST close the connection |
| 5500 | * to the server, discard the received response, and send a 502 (Bad |
| 5501 | * Gateway) response to the client. If this is a response message |
| 5502 | * received by a user agent, the user agent MUST close the |
| 5503 | * connection to the server and discard the received response. |
| 5504 | * |
| 5505 | * 5. If a valid Content-Length header field is present without |
| 5506 | * Transfer-Encoding, its decimal value defines the expected message |
| 5507 | * body length in octets. If the sender closes the connection or |
| 5508 | * the recipient times out before the indicated number of octets are |
| 5509 | * received, the recipient MUST consider the message to be |
| 5510 | * incomplete and close the connection. |
| 5511 | * |
| 5512 | * 6. If this is a request message and none of the above are true, then |
| 5513 | * the message body length is zero (no message body is present). |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5514 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 5515 | * 7. Otherwise, this is a response message without a declared message |
| 5516 | * body length, so the message body length is determined by the |
| 5517 | * number of octets received prior to the server closing the |
| 5518 | * connection. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5519 | */ |
| 5520 | |
| 5521 | /* Skip parsing if no content length is possible. The response flags |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 5522 | * 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] | 5523 | * 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] | 5524 | * FIXME: should we parse anyway and return an error on chunked encoding ? |
| 5525 | */ |
Christopher Faulet | d1cd209 | 2016-11-28 10:14:03 +0100 | [diff] [blame] | 5526 | if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || |
| 5527 | txn->status == 101)) { |
| 5528 | /* Either we've established an explicit tunnel, or we're |
| 5529 | * switching the protocol. In both cases, we're very unlikely |
| 5530 | * to understand the next protocols. We have to switch to tunnel |
| 5531 | * mode, so that we transfer the request and responses then let |
| 5532 | * this protocol pass unmodified. When we later implement specific |
| 5533 | * parsers for such protocols, we'll want to check the Upgrade |
| 5534 | * header which contains information about that protocol for |
| 5535 | * responses with status 101 (eg: see RFC2817 about TLS). |
| 5536 | */ |
| 5537 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN; |
| 5538 | msg->flags |= HTTP_MSGF_XFER_LEN; |
| 5539 | goto end; |
| 5540 | } |
| 5541 | |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5542 | if (txn->meth == HTTP_METH_HEAD || |
| 5543 | (txn->status >= 100 && txn->status < 200) || |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5544 | txn->status == 204 || txn->status == 304) { |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5545 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5546 | goto skip_content_length; |
| 5547 | } |
| 5548 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5549 | use_close_only = 0; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5550 | ctx.idx = 0; |
Willy Tarreau | 4979d5c | 2015-05-01 10:06:30 +0200 | [diff] [blame] | 5551 | 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] | 5552 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5553 | msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
| 5554 | else if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5555 | /* bad transfer-encoding (chunked followed by something else) */ |
| 5556 | use_close_only = 1; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5557 | msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5558 | break; |
| 5559 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5560 | } |
| 5561 | |
Willy Tarreau | 1c91391 | 2015-04-30 10:57:51 +0200 | [diff] [blame] | 5562 | /* Chunked responses must have their content-length removed */ |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5563 | ctx.idx = 0; |
Willy Tarreau | b4d0c03 | 2015-05-01 10:25:45 +0200 | [diff] [blame] | 5564 | if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) { |
Willy Tarreau | 1c91391 | 2015-04-30 10:57:51 +0200 | [diff] [blame] | 5565 | while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) |
| 5566 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 5567 | } |
Willy Tarreau | b4d0c03 | 2015-05-01 10:25:45 +0200 | [diff] [blame] | 5568 | 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] | 5569 | signed long long cl; |
| 5570 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5571 | if (!ctx.vlen) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5572 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5573 | goto hdr_response_bad; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5574 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5575 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5576 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5577 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5578 | goto hdr_response_bad; /* parse failure */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5579 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5580 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5581 | if (cl < 0) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5582 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5583 | goto hdr_response_bad; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5584 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5585 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5586 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5587 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5588 | goto hdr_response_bad; /* already specified, was different */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5589 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5590 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5591 | msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 5592 | msg->body_len = msg->chunk_len = cl; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5593 | } |
| 5594 | |
Christopher Faulet | d1cd209 | 2016-11-28 10:14:03 +0100 | [diff] [blame] | 5595 | skip_content_length: |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5596 | /* Now we have to check if we need to modify the Connection header. |
| 5597 | * This is more difficult on the response than it is on the request, |
| 5598 | * because we can have two different HTTP versions and we don't know |
| 5599 | * how the client will interprete a response. For instance, let's say |
| 5600 | * that the client sends a keep-alive request in HTTP/1.0 and gets an |
| 5601 | * HTTP/1.1 response without any header. Maybe it will bound itself to |
| 5602 | * HTTP/1.0 because it only knows about it, and will consider the lack |
| 5603 | * of header as a close, or maybe it knows HTTP/1.1 and can consider |
| 5604 | * the lack of header as a keep-alive. Thus we will use two flags |
| 5605 | * indicating how a request MAY be understood by the client. In case |
| 5606 | * of multiple possibilities, we'll fix the header to be explicit. If |
| 5607 | * ambiguous cases such as both close and keepalive are seen, then we |
| 5608 | * will fall back to explicit close. Note that we won't take risks with |
| 5609 | * HTTP/1.0 clients which may not necessarily understand keep-alive. |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5610 | * See doc/internals/connection-header.txt for the complete matrix. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5611 | */ |
Christopher Faulet | d1cd209 | 2016-11-28 10:14:03 +0100 | [diff] [blame] | 5612 | if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) && |
| 5613 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN || |
| 5614 | ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
| 5615 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) { |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5616 | int to_del = 0; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5617 | |
Willy Tarreau | 70dffda | 2014-01-30 03:07:23 +0100 | [diff] [blame] | 5618 | /* this situation happens when combining pretend-keepalive with httpclose. */ |
| 5619 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5620 | ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 5621 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) |
Willy Tarreau | 70dffda | 2014-01-30 03:07:23 +0100 | [diff] [blame] | 5622 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 5623 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5624 | /* on unknown transfer length, we must close */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5625 | if (!(msg->flags & HTTP_MSGF_XFER_LEN) && |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5626 | (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) |
| 5627 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5628 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5629 | /* now adjust header transformations depending on current state */ |
| 5630 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN || |
| 5631 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 5632 | to_del |= 2; /* remove "keep-alive" on any response */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5633 | if (!(msg->flags & HTTP_MSGF_VER_11)) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5634 | to_del |= 1; /* remove "close" for HTTP/1.0 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5635 | } |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5636 | else { /* SCL / KAL */ |
| 5637 | to_del |= 1; /* remove "close" on any response */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5638 | if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5639 | to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5640 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5641 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5642 | /* Parse and remove some headers from the connection header */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 5643 | http_parse_connection_header(txn, msg, to_del); |
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 | /* Some keep-alive responses are converted to Server-close if |
| 5646 | * the server wants to close. |
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 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) { |
| 5649 | if ((txn->flags & TX_HDR_CONN_CLO) || |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5650 | (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11))) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5651 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 5652 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5653 | } |
| 5654 | |
Christopher Faulet | d1cd209 | 2016-11-28 10:14:03 +0100 | [diff] [blame] | 5655 | end: |
Willy Tarreau | 7959a55 | 2013-09-23 16:44:27 +0200 | [diff] [blame] | 5656 | /* we want to have the response time before we start processing it */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 5657 | s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now); |
Willy Tarreau | 7959a55 | 2013-09-23 16:44:27 +0200 | [diff] [blame] | 5658 | |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 5659 | /* end of job, return OK */ |
| 5660 | rep->analysers &= ~an_bit; |
| 5661 | rep->analyse_exp = TICK_ETERNITY; |
| 5662 | channel_auto_close(rep); |
| 5663 | return 1; |
| 5664 | |
| 5665 | abort_keep_alive: |
| 5666 | /* A keep-alive request to the server failed on a network error. |
| 5667 | * The client is required to retry. We need to close without returning |
| 5668 | * any other information so that the client retries. |
| 5669 | */ |
| 5670 | txn->status = 0; |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 5671 | rep->analysers &= AN_RES_FLT_END; |
| 5672 | s->req.analysers &= AN_REQ_FLT_END; |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 5673 | channel_auto_close(rep); |
| 5674 | s->logs.logwait = 0; |
| 5675 | s->logs.level = 0; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5676 | s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5677 | channel_truncate(rep); |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 5678 | http_reply_and_close(s, txn->status, NULL); |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 5679 | return 0; |
| 5680 | } |
| 5681 | |
| 5682 | /* This function performs all the processing enabled for the current response. |
| 5683 | * 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] | 5684 | * 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] | 5685 | * other functions. It works like process_request (see indications above). |
| 5686 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5687 | 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] | 5688 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 5689 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5690 | struct http_txn *txn = s->txn; |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 5691 | struct http_msg *msg = &txn->rsp; |
| 5692 | struct proxy *cur_proxy; |
| 5693 | struct cond_wordlist *wl; |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 5694 | enum rule_result ret = HTTP_RULE_RES_CONT; |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 5695 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5696 | 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] | 5697 | now_ms, __FUNCTION__, |
| 5698 | s, |
| 5699 | rep, |
| 5700 | rep->rex, rep->wex, |
| 5701 | rep->flags, |
| 5702 | rep->buf->i, |
| 5703 | rep->analysers); |
| 5704 | |
| 5705 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */ |
| 5706 | return 0; |
| 5707 | |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 5708 | /* The stats applet needs to adjust the Connection header but we don't |
| 5709 | * apply any filter there. |
| 5710 | */ |
Willy Tarreau | 612adb8 | 2015-03-10 15:25:54 +0100 | [diff] [blame] | 5711 | if (unlikely(objt_applet(s->target) == &http_stats_applet)) { |
| 5712 | rep->analysers &= ~an_bit; |
| 5713 | rep->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 5714 | goto skip_filters; |
Willy Tarreau | 612adb8 | 2015-03-10 15:25:54 +0100 | [diff] [blame] | 5715 | } |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 5716 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5717 | /* |
| 5718 | * We will have to evaluate the filters. |
| 5719 | * As opposed to version 1.2, now they will be evaluated in the |
| 5720 | * filters order and not in the header order. This means that |
| 5721 | * each filter has to be validated among all headers. |
| 5722 | * |
| 5723 | * Filters are tried with ->be first, then with ->fe if it is |
| 5724 | * different from ->be. |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 5725 | * |
| 5726 | * Maybe we are in resume condiion. In this case I choose the |
| 5727 | * "struct proxy" which contains the rule list matching the resume |
| 5728 | * pointer. If none of theses "struct proxy" match, I initialise |
| 5729 | * the process with the first one. |
| 5730 | * |
| 5731 | * In fact, I check only correspondance betwwen the current list |
| 5732 | * pointer and the ->fe rule list. If it doesn't match, I initialize |
| 5733 | * the loop with the ->be. |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5734 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5735 | if (s->current_rule_list == &sess->fe->http_res_rules) |
| 5736 | cur_proxy = sess->fe; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 5737 | else |
| 5738 | cur_proxy = s->be; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5739 | while (1) { |
| 5740 | struct proxy *rule_set = cur_proxy; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5741 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5742 | /* evaluate http-response rules */ |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 5743 | if (ret == HTTP_RULE_RES_CONT) { |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 5744 | 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] | 5745 | |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 5746 | if (ret == HTTP_RULE_RES_BADREQ) |
| 5747 | goto return_srv_prx_502; |
| 5748 | |
| 5749 | if (ret == HTTP_RULE_RES_DONE) { |
| 5750 | rep->analysers &= ~an_bit; |
| 5751 | rep->analyse_exp = TICK_ETERNITY; |
| 5752 | return 1; |
| 5753 | } |
| 5754 | } |
| 5755 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 5756 | /* we need to be called again. */ |
| 5757 | if (ret == HTTP_RULE_RES_YIELD) { |
| 5758 | channel_dont_close(rep); |
| 5759 | return 0; |
| 5760 | } |
| 5761 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5762 | /* try headers filters */ |
| 5763 | if (rule_set->rsp_exp != NULL) { |
| 5764 | if (apply_filters_to_response(s, rep, rule_set) < 0) { |
| 5765 | return_bad_resp: |
| 5766 | if (objt_server(s->target)) { |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 5767 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1); |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5768 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5769 | } |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 5770 | HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1); |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5771 | return_srv_prx_502: |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 5772 | rep->analysers &= AN_RES_FLT_END; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5773 | txn->status = 502; |
| 5774 | s->logs.t_data = -1; /* was not a valid response */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5775 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5776 | channel_truncate(rep); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 5777 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5778 | if (!(s->flags & SF_ERR_MASK)) |
| 5779 | s->flags |= SF_ERR_PRXCOND; |
| 5780 | if (!(s->flags & SF_FINST_MASK)) |
| 5781 | s->flags |= SF_FINST_H; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5782 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5783 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5784 | } |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5785 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5786 | /* has the response been denied ? */ |
| 5787 | if (txn->flags & TX_SVDENY) { |
| 5788 | if (objt_server(s->target)) |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 5789 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5790 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 5791 | HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1); |
| 5792 | HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 5793 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 5794 | HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5795 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5796 | goto return_srv_prx_502; |
| 5797 | } |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 5798 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5799 | /* add response headers from the rule sets in the same order */ |
| 5800 | list_for_each_entry(wl, &rule_set->rsp_add, list) { |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 5801 | if (txn->status < 200 && txn->status != 101) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5802 | break; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5803 | if (wl->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 5804 | 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] | 5805 | ret = acl_pass(ret); |
| 5806 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 5807 | ret = !ret; |
| 5808 | if (!ret) |
| 5809 | continue; |
| 5810 | } |
| 5811 | if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0)) |
| 5812 | goto return_bad_resp; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5813 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5814 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5815 | /* check whether we're already working on the frontend */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5816 | if (cur_proxy == sess->fe) |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5817 | break; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5818 | cur_proxy = sess->fe; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5819 | } |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5820 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 5821 | /* After this point, this anayzer can't return yield, so we can |
| 5822 | * remove the bit corresponding to this analyzer from the list. |
| 5823 | * |
| 5824 | * Note that the intermediate returns and goto found previously |
| 5825 | * reset the analyzers. |
| 5826 | */ |
| 5827 | rep->analysers &= ~an_bit; |
| 5828 | rep->analyse_exp = TICK_ETERNITY; |
| 5829 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5830 | /* OK that's all we can do for 1xx responses */ |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 5831 | if (unlikely(txn->status < 200 && txn->status != 101)) |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5832 | goto skip_header_mangling; |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5833 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5834 | /* |
| 5835 | * Now check for a server cookie. |
| 5836 | */ |
Willy Tarreau | 53a09d5 | 2015-08-10 18:59:40 +0200 | [diff] [blame] | 5837 | 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] | 5838 | manage_server_side_cookies(s, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5839 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5840 | /* |
| 5841 | * Check for cache-control or pragma headers if required. |
| 5842 | */ |
Willy Tarreau | 12b32f2 | 2017-12-21 16:08:09 +0100 | [diff] [blame] | 5843 | 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] | 5844 | check_response_for_cacheability(s, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5845 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5846 | /* |
| 5847 | * Add server cookie in the response if needed |
| 5848 | */ |
| 5849 | if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) && |
| 5850 | !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) && |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5851 | (!(s->flags & SF_DIRECT) || |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5852 | ((s->be->cookie_maxidle || txn->cookie_last_date) && |
| 5853 | (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) || |
| 5854 | (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date |
| 5855 | (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date |
| 5856 | (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) && |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5857 | !(s->flags & SF_IGNORE_PRST)) { |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5858 | /* the server is known, it's not the one the client requested, or the |
| 5859 | * cookie's last seen date needs to be refreshed. We have to |
| 5860 | * insert a set-cookie here, except if we want to insert only on POST |
| 5861 | * requests and this one isn't. Note that servers which don't have cookies |
| 5862 | * (eg: some backup servers) will return a full cookie removal request. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5863 | */ |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5864 | if (!objt_server(s->target)->cookie) { |
| 5865 | chunk_printf(&trash, |
| 5866 | "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/", |
| 5867 | s->be->cookie_name); |
| 5868 | } |
| 5869 | else { |
| 5870 | 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] | 5871 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5872 | if (s->be->cookie_maxidle || s->be->cookie_maxlife) { |
| 5873 | /* emit last_date, which is mandatory */ |
| 5874 | trash.str[trash.len++] = COOKIE_DELIM_DATE; |
| 5875 | s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len); |
| 5876 | trash.len += 5; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5877 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5878 | if (s->be->cookie_maxlife) { |
| 5879 | /* emit first_date, which is either the original one or |
| 5880 | * the current date. |
| 5881 | */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 5882 | trash.str[trash.len++] = COOKIE_DELIM_DATE; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5883 | s30tob64(txn->cookie_first_date ? |
| 5884 | txn->cookie_first_date >> 2 : |
| 5885 | (date.tv_sec+3) >> 2, trash.str + trash.len); |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 5886 | trash.len += 5; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5887 | } |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5888 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5889 | chunk_appendf(&trash, "; path=/"); |
| 5890 | } |
Willy Tarreau | 4992dd2 | 2012-05-31 21:02:17 +0200 | [diff] [blame] | 5891 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5892 | if (s->be->cookie_domain) |
| 5893 | chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain); |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5894 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5895 | if (s->be->ck_opts & PR_CK_HTTPONLY) |
| 5896 | chunk_appendf(&trash, "; HttpOnly"); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5897 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5898 | if (s->be->ck_opts & PR_CK_SECURE) |
| 5899 | chunk_appendf(&trash, "; Secure"); |
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 (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0)) |
| 5902 | goto return_bad_resp; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5903 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5904 | txn->flags &= ~TX_SCK_MASK; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5905 | if (objt_server(s->target)->cookie && (s->flags & SF_DIRECT)) |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5906 | /* the server did not change, only the date was updated */ |
| 5907 | txn->flags |= TX_SCK_UPDATED; |
| 5908 | else |
| 5909 | txn->flags |= TX_SCK_INSERTED; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5910 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5911 | /* Here, we will tell an eventual cache on the client side that we don't |
| 5912 | * want it to cache this reply because HTTP/1.0 caches also cache cookies ! |
| 5913 | * Some caches understand the correct form: 'no-cache="set-cookie"', but |
| 5914 | * 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] | 5915 | */ |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5916 | if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5917 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5918 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5919 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5920 | if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, |
| 5921 | "Cache-control: private", 22) < 0)) |
| 5922 | goto return_bad_resp; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5923 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5924 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5925 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5926 | /* |
| 5927 | * Check if result will be cacheable with a cookie. |
| 5928 | * We'll block the response if security checks have caught |
| 5929 | * nasty things such as a cacheable cookie. |
| 5930 | */ |
| 5931 | if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) == |
| 5932 | (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) && |
| 5933 | (s->be->options & PR_O_CHK_CACHE)) { |
| 5934 | /* we're in presence of a cacheable response containing |
| 5935 | * a set-cookie header. We'll block it as requested by |
| 5936 | * the 'checkcache' option, and send an alert. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5937 | */ |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5938 | if (objt_server(s->target)) |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 5939 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1); |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5940 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 5941 | HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1); |
| 5942 | HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 5943 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 5944 | HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5945 | |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5946 | ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 5947 | 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] | 5948 | send_log(s->be, LOG_ALERT, |
| 5949 | "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>"); |
| 5951 | goto return_srv_prx_502; |
| 5952 | } |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 5953 | |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 5954 | skip_filters: |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5955 | /* |
| 5956 | * Adjust "Connection: close" or "Connection: keep-alive" if needed. |
| 5957 | * If an "Upgrade" token is found, the header is left untouched in order |
| 5958 | * 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] | 5959 | * if anything but "Upgrade" is present in the Connection header. We don't |
| 5960 | * want to touch any 101 response either since it's switching to another |
| 5961 | * protocol. |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5962 | */ |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 5963 | if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) && |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5964 | (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5965 | ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5966 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) { |
| 5967 | unsigned int want_flags = 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5968 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5969 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 5970 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 5971 | /* we want a keep-alive response here. Keep-alive header |
| 5972 | * required if either side is not 1.1. |
| 5973 | */ |
| 5974 | if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11)) |
| 5975 | want_flags |= TX_CON_KAL_SET; |
| 5976 | } |
| 5977 | else { |
| 5978 | /* we want a close response here. Close header required if |
| 5979 | * the server is 1.1, regardless of the client. |
| 5980 | */ |
| 5981 | if (msg->flags & HTTP_MSGF_VER_11) |
| 5982 | want_flags |= TX_CON_CLO_SET; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5983 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5984 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5985 | if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 5986 | http_change_connection_header(txn, msg, want_flags); |
| 5987 | } |
| 5988 | |
| 5989 | skip_header_mangling: |
Christopher Faulet | 69744d9 | 2017-03-30 10:54:35 +0200 | [diff] [blame] | 5990 | /* Always enter in the body analyzer */ |
| 5991 | rep->analysers &= ~AN_RES_FLT_XFER_DATA; |
| 5992 | rep->analysers |= AN_RES_HTTP_XFER_BODY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5993 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5994 | /* if the user wants to log as soon as possible, without counting |
| 5995 | * bytes from the server, then this is the right moment. We have |
| 5996 | * to temporarily assign bytes_out to log what we currently have. |
| 5997 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5998 | if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) { |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5999 | s->logs.t_close = s->logs.t_data; /* to get a valid end date */ |
| 6000 | s->logs.bytes_out = txn->rsp.eoh; |
| 6001 | s->do_log(s); |
| 6002 | s->logs.bytes_out = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6003 | } |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 6004 | return 1; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6005 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6006 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6007 | /* This function is an analyser which forwards response body (including chunk |
| 6008 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 6009 | * zero byte. The only situation where it must not be called is when we're in |
| 6010 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 6011 | * remaining data and to resync after end of body. It expects the msg_state to |
| 6012 | * 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] | 6013 | * 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] | 6014 | * |
| 6015 | * It is capable of compressing response data both in content-length mode and |
| 6016 | * in chunked mode. The state machines follows different flows depending on |
| 6017 | * whether content-length and chunked modes are used, since there are no |
| 6018 | * trailers in content-length : |
| 6019 | * |
| 6020 | * chk-mode cl-mode |
| 6021 | * ,----- BODY -----. |
| 6022 | * / \ |
| 6023 | * V size > 0 V chk-mode |
| 6024 | * .--> SIZE -------------> DATA -------------> CRLF |
| 6025 | * | | size == 0 | last byte | |
| 6026 | * | v final crlf v inspected | |
| 6027 | * | TRAILERS -----------> DONE | |
| 6028 | * | | |
| 6029 | * `----------------------------------------------' |
| 6030 | * |
| 6031 | * Compression only happens in the DATA state, and must be flushed in final |
| 6032 | * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding |
| 6033 | * is performed at once on final states for all bytes parsed, or when leaving |
| 6034 | * on missing data. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6035 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6036 | 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] | 6037 | { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6038 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 6039 | struct http_txn *txn = s->txn; |
| 6040 | struct http_msg *msg = &s->txn->rsp; |
Christopher Faulet | 3e34429 | 2015-11-24 16:24:13 +0100 | [diff] [blame] | 6041 | int ret; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6042 | |
Christopher Faulet | 814d270 | 2017-03-30 11:33:44 +0200 | [diff] [blame] | 6043 | DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n", |
| 6044 | now_ms, __FUNCTION__, |
| 6045 | s, |
| 6046 | res, |
| 6047 | res->rex, res->wex, |
| 6048 | res->flags, |
| 6049 | res->buf->i, |
| 6050 | res->analysers); |
| 6051 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 6052 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 6053 | return 0; |
| 6054 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 6055 | 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] | 6056 | ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) || |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 6057 | !s->req.analysers) { |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 6058 | /* Output closed while we were sending data. We must abort and |
| 6059 | * wake the other side up. |
| 6060 | */ |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 6061 | msg->err_state = msg->msg_state; |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 6062 | msg->msg_state = HTTP_MSG_ERROR; |
| 6063 | http_resync_states(s); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 6064 | return 1; |
| 6065 | } |
| 6066 | |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 6067 | /* in most states, we should abort in case of early close */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6068 | channel_auto_close(res); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 6069 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 6070 | if (msg->msg_state == HTTP_MSG_BODY) { |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6071 | msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK) |
| 6072 | ? HTTP_MSG_CHUNK_SIZE |
| 6073 | : HTTP_MSG_DATA); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6074 | } |
| 6075 | |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 6076 | if (res->to_forward) { |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6077 | /* We can't process the buffer's contents yet */ |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 6078 | res->flags |= CF_WAKE_WRITE; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6079 | goto missing_data_or_waiting; |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 6080 | } |
| 6081 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6082 | if (msg->msg_state < HTTP_MSG_DONE) { |
| 6083 | ret = ((msg->flags & HTTP_MSGF_TE_CHNK) |
| 6084 | ? http_msg_forward_chunked_body(s, msg) |
| 6085 | : http_msg_forward_body(s, msg)); |
| 6086 | if (!ret) |
| 6087 | goto missing_data_or_waiting; |
| 6088 | if (ret < 0) |
| 6089 | goto return_bad_res; |
| 6090 | } |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 6091 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6092 | /* other states, DONE...TUNNEL */ |
| 6093 | /* for keep-alive we don't want to forward closes on DONE */ |
| 6094 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 6095 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
| 6096 | channel_dont_close(res); |
Willy Tarreau | 3ce10ff | 2014-04-22 08:24:38 +0200 | [diff] [blame] | 6097 | |
Christopher Faulet | 894da4c | 2017-07-18 11:29:07 +0200 | [diff] [blame] | 6098 | http_resync_states(s); |
| 6099 | if (!(res->analysers & an_bit)) { |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6100 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 6101 | if (res->flags & CF_SHUTW) { |
| 6102 | /* response errors are most likely due to the |
| 6103 | * client aborting the transfer. */ |
| 6104 | goto aborted_xfer; |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 6105 | } |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6106 | if (msg->err_pos >= 0) |
Emeric Brun | 8c1aaa2 | 2017-06-15 11:30:06 +0200 | [diff] [blame] | 6107 | 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] | 6108 | goto return_bad_res; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6109 | } |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6110 | return 1; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6111 | } |
Willy Tarreau | f51d03c | 2016-05-02 15:25:15 +0200 | [diff] [blame] | 6112 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6113 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6114 | missing_data_or_waiting: |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6115 | if (res->flags & CF_SHUTW) |
| 6116 | goto aborted_xfer; |
| 6117 | |
| 6118 | /* stop waiting for data if the input is closed before the end. If the |
| 6119 | * client side was already closed, it means that the client has aborted, |
| 6120 | * so we don't want to count this as a server abort. Otherwise it's a |
| 6121 | * server abort. |
| 6122 | */ |
Christopher Faulet | a33510b | 2017-03-31 15:37:29 +0200 | [diff] [blame] | 6123 | if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6124 | if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW)) |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6125 | goto aborted_xfer; |
Christopher Faulet | a46bbd8 | 2015-06-19 09:00:58 +0200 | [diff] [blame] | 6126 | /* If we have some pending data, we continue the processing */ |
| 6127 | if (!buffer_pending(res->buf)) { |
| 6128 | if (!(s->flags & SF_ERR_MASK)) |
| 6129 | s->flags |= SF_ERR_SRVCL; |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 6130 | HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1); |
Christopher Faulet | a46bbd8 | 2015-06-19 09:00:58 +0200 | [diff] [blame] | 6131 | if (objt_server(s->target)) |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 6132 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1); |
Christopher Faulet | a46bbd8 | 2015-06-19 09:00:58 +0200 | [diff] [blame] | 6133 | goto return_bad_res_stats_ok; |
| 6134 | } |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 6135 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 6136 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 6137 | /* 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] | 6138 | if (!s->req.analysers) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 6139 | goto return_bad_res; |
| 6140 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6141 | /* When TE: chunked is used, we need to get there again to parse |
| 6142 | * 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] | 6143 | * set CF_DONTCLOSE. Similarly, if keep-alive is set on the client side |
| 6144 | * or if there are filters registered on the stream, we don't want to |
| 6145 | * forward a close |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 6146 | */ |
Christopher Faulet | 69744d9 | 2017-03-30 10:54:35 +0200 | [diff] [blame] | 6147 | if ((msg->flags & HTTP_MSGF_TE_CHNK) || |
Christopher Faulet | f1cc5d0 | 2017-02-08 09:45:13 +0100 | [diff] [blame] | 6148 | HAS_DATA_FILTERS(s, res) || |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 6149 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 6150 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6151 | channel_dont_close(res); |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 6152 | |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 6153 | /* 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] | 6154 | * 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] | 6155 | * 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] | 6156 | * modes are already handled by the stream sock layer. We must not do |
| 6157 | * this in content-length mode because it could present the MSG_MORE |
| 6158 | * flag with the last block of forwarded data, which would cause an |
| 6159 | * additional delay to be observed by the receiver. |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 6160 | */ |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 6161 | if ((msg->flags & HTTP_MSGF_TE_CHNK) || (msg->flags & HTTP_MSGF_COMPRESSING)) |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 6162 | res->flags |= CF_EXPECT_MORE; |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 6163 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6164 | /* the stream handler will take care of timeouts and errors */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6165 | return 0; |
| 6166 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 6167 | return_bad_res: /* let's centralize all bad responses */ |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 6168 | HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6169 | if (objt_server(s->target)) |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 6170 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6171 | |
| 6172 | return_bad_res_stats_ok: |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 6173 | txn->rsp.err_state = txn->rsp.msg_state; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6174 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 6175 | /* don't send any error message as we're in the body */ |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 6176 | http_reply_and_close(s, txn->status, NULL); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 6177 | res->analysers &= AN_RES_FLT_END; |
| 6178 | 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] | 6179 | if (objt_server(s->target)) |
| 6180 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6181 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6182 | if (!(s->flags & SF_ERR_MASK)) |
| 6183 | s->flags |= SF_ERR_PRXCOND; |
| 6184 | if (!(s->flags & SF_FINST_MASK)) |
| 6185 | s->flags |= SF_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6186 | return 0; |
| 6187 | |
| 6188 | aborted_xfer: |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 6189 | txn->rsp.err_state = txn->rsp.msg_state; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6190 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
| 6191 | /* don't send any error message as we're in the body */ |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 6192 | http_reply_and_close(s, txn->status, NULL); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 6193 | res->analysers &= AN_RES_FLT_END; |
| 6194 | 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] | 6195 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 6196 | HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1); |
| 6197 | HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6198 | if (objt_server(s->target)) |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 6199 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6200 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6201 | if (!(s->flags & SF_ERR_MASK)) |
| 6202 | s->flags |= SF_ERR_CLICL; |
| 6203 | if (!(s->flags & SF_FINST_MASK)) |
| 6204 | s->flags |= SF_FINST_D; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6205 | return 0; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6206 | } |
| 6207 | |
| 6208 | |
| 6209 | static inline int |
| 6210 | http_msg_forward_body(struct stream *s, struct http_msg *msg) |
| 6211 | { |
| 6212 | struct channel *chn = msg->chn; |
| 6213 | int ret; |
| 6214 | |
| 6215 | /* Here we have the guarantee to be in HTTP_MSG_DATA or HTTP_MSG_ENDING state */ |
| 6216 | |
| 6217 | if (msg->msg_state == HTTP_MSG_ENDING) |
| 6218 | goto ending; |
| 6219 | |
| 6220 | /* Neither content-length, nor transfer-encoding was found, so we must |
| 6221 | * read the body until the server connection is closed. In that case, we |
| 6222 | * eat data as they come. Of course, this happens for response only. */ |
| 6223 | if (!(msg->flags & HTTP_MSGF_XFER_LEN)) { |
| 6224 | unsigned long long len = (chn->buf->i - msg->next); |
| 6225 | msg->chunk_len += len; |
| 6226 | msg->body_len += len; |
| 6227 | } |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 6228 | ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg), |
| 6229 | /* default_ret */ MIN(msg->chunk_len, chn->buf->i - msg->next), |
| 6230 | /* on_error */ goto error); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6231 | msg->next += ret; |
| 6232 | msg->chunk_len -= ret; |
| 6233 | if (msg->chunk_len) { |
| 6234 | /* input empty or output full */ |
| 6235 | if (chn->buf->i > msg->next) |
| 6236 | chn->flags |= CF_WAKE_WRITE; |
| 6237 | goto missing_data_or_waiting; |
| 6238 | } |
| 6239 | |
Christopher Faulet | 1486b0a | 2017-07-18 11:42:08 +0200 | [diff] [blame] | 6240 | /* This check can only be true for a response. HTTP_MSGF_XFER_LEN is |
| 6241 | * always set for a request. */ |
| 6242 | if (!(msg->flags & HTTP_MSGF_XFER_LEN)) { |
| 6243 | /* The server still sending data that should be filtered */ |
| 6244 | if (!(chn->flags & CF_SHUTR) && HAS_DATA_FILTERS(s, chn)) |
| 6245 | goto missing_data_or_waiting; |
| 6246 | } |
Christopher Faulet | f1cc5d0 | 2017-02-08 09:45:13 +0100 | [diff] [blame] | 6247 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6248 | msg->msg_state = HTTP_MSG_ENDING; |
| 6249 | |
| 6250 | ending: |
| 6251 | /* we may have some pending data starting at res->buf->p such as a last |
| 6252 | * chunk of data or trailers. */ |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 6253 | ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next), |
| 6254 | /* default_ret */ msg->next, |
| 6255 | /* on_error */ goto error); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6256 | b_adv(chn->buf, ret); |
| 6257 | msg->next -= ret; |
Willy Tarreau | 9962f8f | 2016-06-28 11:52:08 +0200 | [diff] [blame] | 6258 | if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)) |
| 6259 | msg->sov -= ret; |
Christopher Faulet | a9300a3 | 2016-06-28 15:54:44 +0200 | [diff] [blame] | 6260 | if (msg->next) |
| 6261 | goto waiting; |
Willy Tarreau | 9962f8f | 2016-06-28 11:52:08 +0200 | [diff] [blame] | 6262 | |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 6263 | FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg), |
| 6264 | /* default_ret */ 1, |
| 6265 | /* on_error */ goto error, |
| 6266 | /* on_wait */ goto waiting); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6267 | msg->msg_state = HTTP_MSG_DONE; |
| 6268 | return 1; |
| 6269 | |
| 6270 | missing_data_or_waiting: |
| 6271 | /* we may have some pending data starting at chn->buf->p */ |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 6272 | ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next), |
| 6273 | /* default_ret */ msg->next, |
| 6274 | /* on_error */ goto error); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6275 | b_adv(chn->buf, ret); |
| 6276 | msg->next -= ret; |
| 6277 | if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0) |
| 6278 | msg->sov -= ret; |
Christopher Faulet | 75e2eb6 | 2015-12-15 10:41:47 +0100 | [diff] [blame] | 6279 | if (!HAS_DATA_FILTERS(s, chn)) |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6280 | msg->chunk_len -= channel_forward(chn, msg->chunk_len); |
Christopher Faulet | a9300a3 | 2016-06-28 15:54:44 +0200 | [diff] [blame] | 6281 | waiting: |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6282 | return 0; |
| 6283 | error: |
| 6284 | return -1; |
| 6285 | } |
| 6286 | |
| 6287 | static inline int |
| 6288 | http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg) |
| 6289 | { |
| 6290 | struct channel *chn = msg->chn; |
Willy Tarreau | e56cdd3 | 2017-09-21 08:36:33 +0200 | [diff] [blame] | 6291 | unsigned int chunk; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6292 | int ret; |
| 6293 | |
| 6294 | /* Here we have the guarantee to be in one of the following state: |
| 6295 | * HTTP_MSG_DATA, HTTP_MSG_CHUNK_SIZE, HTTP_MSG_CHUNK_CRLF, |
| 6296 | * HTTP_MSG_TRAILERS or HTTP_MSG_ENDING. */ |
| 6297 | |
| 6298 | switch_states: |
| 6299 | switch (msg->msg_state) { |
| 6300 | case HTTP_MSG_DATA: |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 6301 | ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg), |
| 6302 | /* default_ret */ MIN(msg->chunk_len, chn->buf->i - msg->next), |
| 6303 | /* on_error */ goto error); |
| 6304 | msg->next += ret; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6305 | msg->chunk_len -= ret; |
| 6306 | if (msg->chunk_len) { |
| 6307 | /* input empty or output full */ |
| 6308 | if (chn->buf->i > msg->next) |
| 6309 | chn->flags |= CF_WAKE_WRITE; |
| 6310 | goto missing_data_or_waiting; |
| 6311 | } |
| 6312 | |
| 6313 | /* nothing left to forward for this chunk*/ |
| 6314 | msg->msg_state = HTTP_MSG_CHUNK_CRLF; |
| 6315 | /* fall through for HTTP_MSG_CHUNK_CRLF */ |
| 6316 | |
| 6317 | case HTTP_MSG_CHUNK_CRLF: |
| 6318 | /* we want the CRLF after the data */ |
Willy Tarreau | b289256 | 2017-09-21 11:33:54 +0200 | [diff] [blame] | 6319 | ret = h1_skip_chunk_crlf(chn->buf, msg->next, chn->buf->i); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6320 | if (ret == 0) |
| 6321 | goto missing_data_or_waiting; |
Willy Tarreau | b289256 | 2017-09-21 11:33:54 +0200 | [diff] [blame] | 6322 | if (ret < 0) { |
| 6323 | msg->err_pos = chn->buf->i + ret; |
| 6324 | if (msg->err_pos < 0) |
| 6325 | msg->err_pos += chn->buf->size; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6326 | goto chunk_parsing_error; |
Willy Tarreau | b289256 | 2017-09-21 11:33:54 +0200 | [diff] [blame] | 6327 | } |
Christopher Faulet | 113f7de | 2015-12-14 14:52:13 +0100 | [diff] [blame] | 6328 | msg->next += ret; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6329 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 6330 | /* fall through for HTTP_MSG_CHUNK_SIZE */ |
| 6331 | |
| 6332 | case HTTP_MSG_CHUNK_SIZE: |
| 6333 | /* read the chunk size and assign it to ->chunk_len, |
| 6334 | * then set ->next to point to the body and switch to |
| 6335 | * DATA or TRAILERS state. |
| 6336 | */ |
Willy Tarreau | e56cdd3 | 2017-09-21 08:36:33 +0200 | [diff] [blame] | 6337 | 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] | 6338 | if (ret == 0) |
| 6339 | goto missing_data_or_waiting; |
Willy Tarreau | e56cdd3 | 2017-09-21 08:36:33 +0200 | [diff] [blame] | 6340 | if (ret < 0) { |
| 6341 | msg->err_pos = chn->buf->i + ret; |
| 6342 | if (msg->err_pos < 0) |
| 6343 | msg->err_pos += chn->buf->size; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6344 | goto chunk_parsing_error; |
Willy Tarreau | e56cdd3 | 2017-09-21 08:36:33 +0200 | [diff] [blame] | 6345 | } |
| 6346 | |
| 6347 | msg->sol = ret; |
Christopher Faulet | 113f7de | 2015-12-14 14:52:13 +0100 | [diff] [blame] | 6348 | msg->next += ret; |
Willy Tarreau | e56cdd3 | 2017-09-21 08:36:33 +0200 | [diff] [blame] | 6349 | msg->chunk_len = chunk; |
| 6350 | msg->body_len += chunk; |
| 6351 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6352 | if (msg->chunk_len) { |
| 6353 | msg->msg_state = HTTP_MSG_DATA; |
| 6354 | goto switch_states; |
| 6355 | } |
| 6356 | msg->msg_state = HTTP_MSG_TRAILERS; |
| 6357 | /* fall through for HTTP_MSG_TRAILERS */ |
| 6358 | |
| 6359 | case HTTP_MSG_TRAILERS: |
| 6360 | ret = http_forward_trailers(msg); |
| 6361 | if (ret < 0) |
| 6362 | goto chunk_parsing_error; |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 6363 | FLT_STRM_DATA_CB(s, chn, flt_http_chunk_trailers(s, msg), |
| 6364 | /* default_ret */ 1, |
| 6365 | /* on_error */ goto error); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6366 | msg->next += msg->sol; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6367 | if (!ret) |
| 6368 | goto missing_data_or_waiting; |
| 6369 | break; |
| 6370 | |
| 6371 | case HTTP_MSG_ENDING: |
| 6372 | goto ending; |
| 6373 | |
| 6374 | default: |
| 6375 | /* This should no happen in this function */ |
| 6376 | goto error; |
| 6377 | } |
| 6378 | |
| 6379 | msg->msg_state = HTTP_MSG_ENDING; |
| 6380 | ending: |
| 6381 | /* we may have some pending data starting at res->buf->p such as a last |
| 6382 | * chunk of data or trailers. */ |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 6383 | 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] | 6384 | /* default_ret */ msg->next, |
| 6385 | /* on_error */ goto error); |
| 6386 | b_adv(chn->buf, ret); |
| 6387 | msg->next -= ret; |
Willy Tarreau | 9962f8f | 2016-06-28 11:52:08 +0200 | [diff] [blame] | 6388 | if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)) |
| 6389 | msg->sov -= ret; |
Christopher Faulet | a9300a3 | 2016-06-28 15:54:44 +0200 | [diff] [blame] | 6390 | if (msg->next) |
| 6391 | goto waiting; |
Willy Tarreau | 9962f8f | 2016-06-28 11:52:08 +0200 | [diff] [blame] | 6392 | |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 6393 | FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg), |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6394 | /* default_ret */ 1, |
| 6395 | /* on_error */ goto error, |
| 6396 | /* on_wait */ goto waiting); |
| 6397 | msg->msg_state = HTTP_MSG_DONE; |
| 6398 | return 1; |
| 6399 | |
| 6400 | missing_data_or_waiting: |
| 6401 | /* we may have some pending data starting at chn->buf->p */ |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 6402 | 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] | 6403 | /* default_ret */ msg->next, |
| 6404 | /* on_error */ goto error); |
| 6405 | b_adv(chn->buf, ret); |
| 6406 | msg->next -= ret; |
| 6407 | if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0) |
| 6408 | msg->sov -= ret; |
Christopher Faulet | 75e2eb6 | 2015-12-15 10:41:47 +0100 | [diff] [blame] | 6409 | if (!HAS_DATA_FILTERS(s, chn)) |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6410 | msg->chunk_len -= channel_forward(chn, msg->chunk_len); |
Christopher Faulet | a9300a3 | 2016-06-28 15:54:44 +0200 | [diff] [blame] | 6411 | waiting: |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6412 | return 0; |
| 6413 | |
| 6414 | chunk_parsing_error: |
| 6415 | if (msg->err_pos >= 0) { |
| 6416 | if (chn->flags & CF_ISRESP) |
Emeric Brun | 8c1aaa2 | 2017-06-15 11:30:06 +0200 | [diff] [blame] | 6417 | http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6418 | msg->msg_state, strm_fe(s)); |
| 6419 | else |
Emeric Brun | 8c1aaa2 | 2017-06-15 11:30:06 +0200 | [diff] [blame] | 6420 | 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] | 6421 | msg, msg->msg_state, s->be); |
| 6422 | } |
| 6423 | error: |
| 6424 | return -1; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6425 | } |
| 6426 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6427 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6428 | /* Iterate the same filter through all request headers. |
| 6429 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6430 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 6431 | * DENY stats. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6432 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6433 | 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] | 6434 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6435 | char *cur_ptr, *cur_end, *cur_next; |
| 6436 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 6437 | struct http_txn *txn = s->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6438 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6439 | int delta; |
Willy Tarreau | 0f7562b | 2007-01-07 15:46:13 +0100 | [diff] [blame] | 6440 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6441 | last_hdr = 0; |
| 6442 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6443 | cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6444 | old_idx = 0; |
| 6445 | |
| 6446 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6447 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6448 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6449 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6450 | (exp->action == ACT_ALLOW || |
| 6451 | exp->action == ACT_DENY || |
| 6452 | exp->action == ACT_TARPIT)) |
| 6453 | return 0; |
| 6454 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6455 | cur_idx = txn->hdr_idx.v[old_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6456 | if (!cur_idx) |
| 6457 | break; |
| 6458 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6459 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6460 | cur_ptr = cur_next; |
| 6461 | cur_end = cur_ptr + cur_hdr->len; |
| 6462 | cur_next = cur_end + cur_hdr->cr + 1; |
| 6463 | |
| 6464 | /* Now we have one header between cur_ptr and cur_end, |
| 6465 | * and the next header starts at cur_next. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6466 | */ |
| 6467 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 6468 | 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] | 6469 | switch (exp->action) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6470 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6471 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6472 | last_hdr = 1; |
| 6473 | break; |
| 6474 | |
| 6475 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6476 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6477 | last_hdr = 1; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6478 | break; |
| 6479 | |
| 6480 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6481 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6482 | last_hdr = 1; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6483 | break; |
| 6484 | |
| 6485 | case ACT_REPLACE: |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 6486 | trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch); |
| 6487 | if (trash.len < 0) |
| 6488 | return -1; |
| 6489 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6490 | 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] | 6491 | /* FIXME: if the user adds a newline in the replacement, the |
| 6492 | * index will not be recalculated for now, and the new line |
| 6493 | * will not be counted as a new header. |
| 6494 | */ |
| 6495 | |
| 6496 | cur_end += delta; |
| 6497 | cur_next += delta; |
| 6498 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6499 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6500 | break; |
| 6501 | |
| 6502 | case ACT_REMOVE: |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6503 | delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6504 | cur_next += delta; |
| 6505 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6506 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6507 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 6508 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6509 | cur_hdr->len = 0; |
| 6510 | cur_end = NULL; /* null-term has been rewritten */ |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 6511 | cur_idx = old_idx; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6512 | break; |
| 6513 | |
| 6514 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6515 | } |
| 6516 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6517 | /* keep the link from this header to next one in case of later |
| 6518 | * removal of next header. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6519 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6520 | old_idx = cur_idx; |
| 6521 | } |
| 6522 | return 0; |
| 6523 | } |
| 6524 | |
| 6525 | |
| 6526 | /* Apply the filter to the request line. |
| 6527 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 6528 | * or -1 if a replacement resulted in an invalid request line. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6529 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 6530 | * DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6531 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6532 | 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] | 6533 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6534 | char *cur_ptr, *cur_end; |
| 6535 | int done; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 6536 | struct http_txn *txn = s->txn; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6537 | int delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6538 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6539 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6540 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6541 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6542 | (exp->action == ACT_ALLOW || |
| 6543 | exp->action == ACT_DENY || |
| 6544 | exp->action == ACT_TARPIT)) |
| 6545 | return 0; |
| 6546 | else if (exp->action == ACT_REMOVE) |
| 6547 | return 0; |
| 6548 | |
| 6549 | done = 0; |
| 6550 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6551 | cur_ptr = req->buf->p; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6552 | cur_end = cur_ptr + txn->req.sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6553 | |
| 6554 | /* Now we have the request line between cur_ptr and cur_end */ |
| 6555 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 6556 | 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] | 6557 | switch (exp->action) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6558 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6559 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6560 | done = 1; |
| 6561 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 6562 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6563 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6564 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6565 | done = 1; |
| 6566 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 6567 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6568 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6569 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6570 | done = 1; |
| 6571 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 6572 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6573 | case ACT_REPLACE: |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 6574 | trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch); |
| 6575 | if (trash.len < 0) |
| 6576 | return -1; |
| 6577 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6578 | 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] | 6579 | /* FIXME: if the user adds a newline in the replacement, the |
| 6580 | * index will not be recalculated for now, and the new line |
| 6581 | * will not be counted as a new header. |
| 6582 | */ |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 6583 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6584 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6585 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 6586 | cur_end = (char *)http_parse_reqline(&txn->req, |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6587 | HTTP_MSG_RQMETH, |
| 6588 | cur_ptr, cur_end + 1, |
| 6589 | NULL, NULL); |
| 6590 | if (unlikely(!cur_end)) |
| 6591 | return -1; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 6592 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6593 | /* we have a full request and we know that we have either a CR |
| 6594 | * or an LF at <ptr>. |
| 6595 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6596 | txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l); |
| 6597 | 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] | 6598 | /* there is no point trying this regex on headers */ |
| 6599 | return 1; |
| 6600 | } |
| 6601 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6602 | return done; |
| 6603 | } |
Willy Tarreau | 97de624 | 2006-12-27 17:18:38 +0100 | [diff] [blame] | 6604 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6605 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6606 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6607 | /* |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6608 | * 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] | 6609 | * 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] | 6610 | * unparsable request. Since it can manage the switch to another backend, it |
| 6611 | * updates the per-proxy DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6612 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6613 | 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] | 6614 | { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 6615 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 6616 | struct http_txn *txn = s->txn; |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6617 | struct hdr_exp *exp; |
| 6618 | |
| 6619 | for (exp = px->req_exp; exp; exp = exp->next) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6620 | int ret; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6621 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6622 | /* |
| 6623 | * The interleaving of transformations and verdicts |
| 6624 | * makes it difficult to decide to continue or stop |
| 6625 | * the evaluation. |
| 6626 | */ |
| 6627 | |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6628 | if (txn->flags & (TX_CLDENY|TX_CLTARPIT)) |
| 6629 | break; |
| 6630 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6631 | if ((txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6632 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6633 | exp->action == ACT_TARPIT || exp->action == ACT_PASS)) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6634 | continue; |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6635 | |
| 6636 | /* if this filter had a condition, evaluate it now and skip to |
| 6637 | * next filter if the condition does not match. |
| 6638 | */ |
| 6639 | if (exp->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 6640 | 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] | 6641 | ret = acl_pass(ret); |
| 6642 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 6643 | ret = !ret; |
| 6644 | |
| 6645 | if (!ret) |
| 6646 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6647 | } |
| 6648 | |
| 6649 | /* Apply the filter to the request line. */ |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6650 | ret = apply_filter_to_req_line(s, req, exp); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6651 | if (unlikely(ret < 0)) |
| 6652 | return -1; |
| 6653 | |
| 6654 | if (likely(ret == 0)) { |
| 6655 | /* The filter did not match the request, it can be |
| 6656 | * iterated through all headers. |
| 6657 | */ |
Willy Tarreau | 34d4c3c | 2015-01-30 20:58:58 +0100 | [diff] [blame] | 6658 | if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0)) |
| 6659 | return -1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6660 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6661 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6662 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6663 | } |
| 6664 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6665 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6666 | /* Find the end of a cookie value contained between <s> and <e>. It works the |
| 6667 | * same way as with headers above except that the semi-colon also ends a token. |
| 6668 | * See RFC2965 for more information. Note that it requires a valid header to |
| 6669 | * return a valid result. |
| 6670 | */ |
| 6671 | char *find_cookie_value_end(char *s, const char *e) |
| 6672 | { |
| 6673 | int quoted, qdpair; |
| 6674 | |
| 6675 | quoted = qdpair = 0; |
| 6676 | for (; s < e; s++) { |
| 6677 | if (qdpair) qdpair = 0; |
| 6678 | else if (quoted) { |
| 6679 | if (*s == '\\') qdpair = 1; |
| 6680 | else if (*s == '"') quoted = 0; |
| 6681 | } |
| 6682 | else if (*s == '"') quoted = 1; |
| 6683 | else if (*s == ',' || *s == ';') return s; |
| 6684 | } |
| 6685 | return s; |
| 6686 | } |
| 6687 | |
| 6688 | /* Delete a value in a header between delimiters <from> and <next> in buffer |
| 6689 | * <buf>. The number of characters displaced is returned, and the pointer to |
| 6690 | * the first delimiter is updated if required. The function tries as much as |
| 6691 | * possible to respect the following principles : |
| 6692 | * - replace <from> delimiter by the <next> one unless <from> points to a |
| 6693 | * colon, in which case <next> is simply removed |
| 6694 | * - set exactly one space character after the new first delimiter, unless |
| 6695 | * there are not enough characters in the block being moved to do so. |
| 6696 | * - remove unneeded spaces before the previous delimiter and after the new |
| 6697 | * one. |
| 6698 | * |
| 6699 | * It is the caller's responsibility to ensure that : |
| 6700 | * - <from> points to a valid delimiter or the colon ; |
| 6701 | * - <next> points to a valid delimiter or the final CR/LF ; |
| 6702 | * - there are non-space chars before <from> ; |
| 6703 | * - there is a CR/LF at or after <next>. |
| 6704 | */ |
Willy Tarreau | af81935 | 2012-08-27 22:08:00 +0200 | [diff] [blame] | 6705 | int del_hdr_value(struct buffer *buf, char **from, char *next) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6706 | { |
| 6707 | char *prev = *from; |
| 6708 | |
| 6709 | if (*prev == ':') { |
| 6710 | /* We're removing the first value, preserve the colon and add a |
| 6711 | * space if possible. |
| 6712 | */ |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 6713 | if (!HTTP_IS_CRLF(*next)) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6714 | next++; |
| 6715 | prev++; |
| 6716 | if (prev < next) |
| 6717 | *prev++ = ' '; |
| 6718 | |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 6719 | while (HTTP_IS_SPHT(*next)) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6720 | next++; |
| 6721 | } else { |
| 6722 | /* Remove useless spaces before the old delimiter. */ |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 6723 | while (HTTP_IS_SPHT(*(prev-1))) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6724 | prev--; |
| 6725 | *from = prev; |
| 6726 | |
| 6727 | /* copy the delimiter and if possible a space if we're |
| 6728 | * not at the end of the line. |
| 6729 | */ |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 6730 | if (!HTTP_IS_CRLF(*next)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6731 | *prev++ = *next++; |
| 6732 | if (prev + 1 < next) |
| 6733 | *prev++ = ' '; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 6734 | while (HTTP_IS_SPHT(*next)) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6735 | next++; |
| 6736 | } |
| 6737 | } |
| 6738 | return buffer_replace2(buf, prev, next, NULL, 0); |
| 6739 | } |
| 6740 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6741 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 6742 | * 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] | 6743 | * desirable to call it only when needed. This code is quite complex because |
| 6744 | * of the multiple very crappy and ambiguous syntaxes we have to support. it |
| 6745 | * highly recommended not to touch this part without a good reason ! |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6746 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6747 | void manage_client_side_cookies(struct stream *s, struct channel *req) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6748 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 6749 | struct http_txn *txn = s->txn; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6750 | struct session *sess = s->sess; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6751 | int preserve_hdr; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6752 | int cur_idx, old_idx; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6753 | char *hdr_beg, *hdr_end, *hdr_next, *del_from; |
| 6754 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6755 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6756 | /* Iterate through the headers, we start with the start line. */ |
Willy Tarreau | 83969f4 | 2007-01-22 08:55:47 +0100 | [diff] [blame] | 6757 | old_idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6758 | hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6759 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6760 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6761 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6762 | int val; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6763 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6764 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6765 | hdr_beg = hdr_next; |
| 6766 | hdr_end = hdr_beg + cur_hdr->len; |
| 6767 | hdr_next = hdr_end + cur_hdr->cr + 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6768 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6769 | /* We have one full header between hdr_beg and hdr_end, and the |
| 6770 | * next header starts at hdr_next. We're only interested in |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6771 | * "Cookie:" headers. |
| 6772 | */ |
| 6773 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6774 | val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6); |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6775 | if (!val) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6776 | old_idx = cur_idx; |
| 6777 | continue; |
| 6778 | } |
| 6779 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6780 | del_from = NULL; /* nothing to be deleted */ |
| 6781 | preserve_hdr = 0; /* assume we may kill the whole header */ |
| 6782 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6783 | /* Now look for cookies. Conforming to RFC2109, we have to support |
| 6784 | * attributes whose name begin with a '$', and associate them with |
| 6785 | * the right cookie, if we want to delete this cookie. |
| 6786 | * So there are 3 cases for each cookie read : |
| 6787 | * 1) it's a special attribute, beginning with a '$' : ignore it. |
| 6788 | * 2) it's a server id cookie that we *MAY* want to delete : save |
| 6789 | * some pointers on it (last semi-colon, beginning of cookie...) |
| 6790 | * 3) it's an application cookie : we *MAY* have to delete a previous |
| 6791 | * "special" cookie. |
| 6792 | * At the end of loop, if a "special" cookie remains, we may have to |
| 6793 | * remove it. If no application cookie persists in the header, we |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6794 | * *MUST* delete it. |
| 6795 | * |
| 6796 | * Note: RFC2965 is unclear about the processing of spaces around |
| 6797 | * the equal sign in the ATTR=VALUE form. A careful inspection of |
| 6798 | * the RFC explicitly allows spaces before it, and not within the |
| 6799 | * tokens (attrs or values). An inspection of RFC2109 allows that |
| 6800 | * too but section 10.1.3 lets one think that spaces may be allowed |
| 6801 | * after the equal sign too, resulting in some (rare) buggy |
| 6802 | * implementations trying to do that. So let's do what servers do. |
| 6803 | * Latest ietf draft forbids spaces all around. Also, earlier RFCs |
| 6804 | * allowed quoted strings in values, with any possible character |
| 6805 | * after a backslash, including control chars and delimitors, which |
| 6806 | * causes parsing to become ambiguous. Browsers also allow spaces |
| 6807 | * within values even without quotes. |
| 6808 | * |
| 6809 | * We have to keep multiple pointers in order to support cookie |
| 6810 | * removal at the beginning, middle or end of header without |
| 6811 | * corrupting the header. All of these headers are valid : |
| 6812 | * |
| 6813 | * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n |
| 6814 | * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n |
| 6815 | * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n |
| 6816 | * | | | | | | | | | |
| 6817 | * | | | | | | | | hdr_end <--+ |
| 6818 | * | | | | | | | +--> next |
| 6819 | * | | | | | | +----> val_end |
| 6820 | * | | | | | +-----------> val_beg |
| 6821 | * | | | | +--------------> equal |
| 6822 | * | | | +----------------> att_end |
| 6823 | * | | +---------------------> att_beg |
| 6824 | * | +--------------------------> prev |
| 6825 | * +--------------------------------> hdr_beg |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6826 | */ |
| 6827 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6828 | for (prev = hdr_beg + 6; prev < hdr_end; prev = next) { |
| 6829 | /* Iterate through all cookies on this line */ |
| 6830 | |
| 6831 | /* find att_beg */ |
| 6832 | att_beg = prev + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 6833 | while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg)) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6834 | att_beg++; |
| 6835 | |
| 6836 | /* find att_end : this is the first character after the last non |
| 6837 | * space before the equal. It may be equal to hdr_end. |
| 6838 | */ |
| 6839 | equal = att_end = att_beg; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6840 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6841 | while (equal < hdr_end) { |
| 6842 | if (*equal == '=' || *equal == ',' || *equal == ';') |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6843 | break; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 6844 | if (HTTP_IS_SPHT(*equal++)) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6845 | continue; |
| 6846 | att_end = equal; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6847 | } |
| 6848 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6849 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 6850 | * is between <att_beg> and <equal>, both may be identical. |
| 6851 | */ |
| 6852 | |
| 6853 | /* look for end of cookie if there is an equal sign */ |
| 6854 | if (equal < hdr_end && *equal == '=') { |
| 6855 | /* look for the beginning of the value */ |
| 6856 | val_beg = equal + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 6857 | while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg)) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6858 | val_beg++; |
| 6859 | |
| 6860 | /* find the end of the value, respecting quotes */ |
| 6861 | next = find_cookie_value_end(val_beg, hdr_end); |
| 6862 | |
| 6863 | /* make val_end point to the first white space or delimitor after the value */ |
| 6864 | val_end = next; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 6865 | while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1))) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6866 | val_end--; |
| 6867 | } else { |
| 6868 | val_beg = val_end = next = equal; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 6869 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6870 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6871 | /* We have nothing to do with attributes beginning with '$'. However, |
| 6872 | * they will automatically be removed if a header before them is removed, |
| 6873 | * since they're supposed to be linked together. |
| 6874 | */ |
| 6875 | if (*att_beg == '$') |
| 6876 | continue; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6877 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6878 | /* Ignore cookies with no equal sign */ |
| 6879 | if (equal == next) { |
| 6880 | /* This is not our cookie, so we must preserve it. But if we already |
| 6881 | * scheduled another cookie for removal, we cannot remove the |
| 6882 | * complete header, but we can remove the previous block itself. |
| 6883 | */ |
| 6884 | preserve_hdr = 1; |
| 6885 | if (del_from != NULL) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6886 | int delta = del_hdr_value(req->buf, &del_from, prev); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6887 | val_end += delta; |
| 6888 | next += delta; |
| 6889 | hdr_end += delta; |
| 6890 | hdr_next += delta; |
| 6891 | cur_hdr->len += delta; |
| 6892 | http_msg_move_end(&txn->req, delta); |
| 6893 | prev = del_from; |
| 6894 | del_from = NULL; |
| 6895 | } |
| 6896 | continue; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 6897 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6898 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6899 | /* if there are spaces around the equal sign, we need to |
| 6900 | * strip them otherwise we'll get trouble for cookie captures, |
| 6901 | * or even for rewrites. Since this happens extremely rarely, |
| 6902 | * it does not hurt performance. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6903 | */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6904 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 6905 | int stripped_before = 0; |
| 6906 | int stripped_after = 0; |
| 6907 | |
| 6908 | if (att_end != equal) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6909 | stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6910 | equal += stripped_before; |
| 6911 | val_beg += stripped_before; |
| 6912 | } |
| 6913 | |
| 6914 | if (val_beg > equal + 1) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6915 | stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6916 | val_beg += stripped_after; |
| 6917 | stripped_before += stripped_after; |
| 6918 | } |
| 6919 | |
| 6920 | val_end += stripped_before; |
| 6921 | next += stripped_before; |
| 6922 | hdr_end += stripped_before; |
| 6923 | hdr_next += stripped_before; |
| 6924 | cur_hdr->len += stripped_before; |
| 6925 | http_msg_move_end(&txn->req, stripped_before); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6926 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6927 | /* now everything is as on the diagram above */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6928 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6929 | /* First, let's see if we want to capture this cookie. We check |
| 6930 | * that we don't already have a client side cookie, because we |
| 6931 | * can only capture one. Also as an optimisation, we ignore |
| 6932 | * cookies shorter than the declared name. |
| 6933 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6934 | if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL && |
| 6935 | (val_end - att_beg >= sess->fe->capture_namelen) && |
| 6936 | memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6937 | int log_len = val_end - att_beg; |
| 6938 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6939 | if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 6940 | ha_alert("HTTP logging : out of memory.\n"); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6941 | } else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6942 | if (log_len > sess->fe->capture_len) |
| 6943 | log_len = sess->fe->capture_len; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6944 | memcpy(txn->cli_cookie, att_beg, log_len); |
| 6945 | txn->cli_cookie[log_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6946 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6947 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6948 | |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6949 | /* Persistence cookies in passive, rewrite or insert mode have the |
| 6950 | * following form : |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6951 | * |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6952 | * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]] |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6953 | * |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6954 | * For cookies in prefix mode, the form is : |
| 6955 | * |
| 6956 | * Cookie: NAME=SRV~VALUE |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6957 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6958 | if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) && |
| 6959 | (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) { |
| 6960 | struct server *srv = s->be->srv; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6961 | char *delim; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6962 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6963 | /* if we're in cookie prefix mode, we'll search the delimitor so that we |
| 6964 | * have the server ID between val_beg and delim, and the original cookie between |
| 6965 | * delim+1 and val_end. Otherwise, delim==val_end : |
| 6966 | * |
| 6967 | * Cookie: NAME=SRV; # in all but prefix modes |
| 6968 | * Cookie: NAME=SRV~OPAQUE ; # in prefix mode |
| 6969 | * | || || | |+-> next |
| 6970 | * | || || | +--> val_end |
| 6971 | * | || || +---------> delim |
| 6972 | * | || |+------------> val_beg |
| 6973 | * | || +-------------> att_end = equal |
| 6974 | * | |+-----------------> att_beg |
| 6975 | * | +------------------> prev |
| 6976 | * +-------------------------> hdr_beg |
| 6977 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6978 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6979 | if (s->be->ck_opts & PR_CK_PFX) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6980 | for (delim = val_beg; delim < val_end; delim++) |
| 6981 | if (*delim == COOKIE_DELIM) |
| 6982 | break; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6983 | } else { |
| 6984 | char *vbar1; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6985 | delim = val_end; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6986 | /* Now check if the cookie contains a date field, which would |
| 6987 | * appear after a vertical bar ('|') just after the server name |
| 6988 | * and before the delimiter. |
| 6989 | */ |
| 6990 | vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg); |
| 6991 | if (vbar1) { |
| 6992 | /* OK, so left of the bar is the server's cookie and |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6993 | * right is the last seen date. It is a base64 encoded |
| 6994 | * 30-bit value representing the UNIX date since the |
| 6995 | * epoch in 4-second quantities. |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6996 | */ |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6997 | int val; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6998 | delim = vbar1++; |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6999 | if (val_end - vbar1 >= 5) { |
| 7000 | val = b64tos30(vbar1); |
| 7001 | if (val > 0) |
| 7002 | txn->cookie_last_date = val << 2; |
| 7003 | } |
| 7004 | /* look for a second vertical bar */ |
| 7005 | vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1); |
| 7006 | if (vbar1 && (val_end - vbar1 > 5)) { |
| 7007 | val = b64tos30(vbar1 + 1); |
| 7008 | if (val > 0) |
| 7009 | txn->cookie_first_date = val << 2; |
| 7010 | } |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7011 | } |
| 7012 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7013 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7014 | /* if the cookie has an expiration date and the proxy wants to check |
| 7015 | * it, then we do that now. We first check if the cookie is too old, |
| 7016 | * then only if it has expired. We detect strict overflow because the |
| 7017 | * time resolution here is not great (4 seconds). Cookies with dates |
| 7018 | * in the future are ignored if their offset is beyond one day. This |
| 7019 | * allows an admin to fix timezone issues without expiring everyone |
| 7020 | * and at the same time avoids keeping unwanted side effects for too |
| 7021 | * long. |
| 7022 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7023 | if (txn->cookie_first_date && s->be->cookie_maxlife && |
| 7024 | (((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] | 7025 | ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) { |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7026 | txn->flags &= ~TX_CK_MASK; |
| 7027 | txn->flags |= TX_CK_OLD; |
| 7028 | delim = val_beg; // let's pretend we have not found the cookie |
| 7029 | txn->cookie_first_date = 0; |
| 7030 | txn->cookie_last_date = 0; |
| 7031 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7032 | else if (txn->cookie_last_date && s->be->cookie_maxidle && |
| 7033 | (((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] | 7034 | ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) { |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7035 | txn->flags &= ~TX_CK_MASK; |
| 7036 | txn->flags |= TX_CK_EXPIRED; |
| 7037 | delim = val_beg; // let's pretend we have not found the cookie |
| 7038 | txn->cookie_first_date = 0; |
| 7039 | txn->cookie_last_date = 0; |
| 7040 | } |
| 7041 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7042 | /* Here, we'll look for the first running server which supports the cookie. |
| 7043 | * This allows to share a same cookie between several servers, for example |
| 7044 | * to dedicate backup servers to specific servers only. |
| 7045 | * However, to prevent clients from sticking to cookie-less backup server |
| 7046 | * when they have incidentely learned an empty cookie, we simply ignore |
| 7047 | * empty cookies and mark them as invalid. |
| 7048 | * The same behaviour is applied when persistence must be ignored. |
| 7049 | */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 7050 | if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED))) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7051 | srv = NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7052 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7053 | while (srv) { |
| 7054 | if (srv->cookie && (srv->cklen == delim - val_beg) && |
| 7055 | !memcmp(val_beg, srv->cookie, delim - val_beg)) { |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 7056 | if ((srv->cur_state != SRV_ST_STOPPED) || |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7057 | (s->be->options & PR_O_PERSIST) || |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 7058 | (s->flags & SF_FORCE_PRST)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7059 | /* we found the server and we can use it */ |
| 7060 | txn->flags &= ~TX_CK_MASK; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 7061 | 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] | 7062 | s->flags |= SF_DIRECT | SF_ASSIGNED; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7063 | s->target = &srv->obj_type; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7064 | break; |
| 7065 | } else { |
| 7066 | /* we found a server, but it's down, |
| 7067 | * mark it as such and go on in case |
| 7068 | * another one is available. |
| 7069 | */ |
| 7070 | txn->flags &= ~TX_CK_MASK; |
| 7071 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7072 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7073 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7074 | srv = srv->next; |
| 7075 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7076 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7077 | 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] | 7078 | /* no server matched this cookie or we deliberately skipped it */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7079 | txn->flags &= ~TX_CK_MASK; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 7080 | if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED))) |
Willy Tarreau | c89ccb6 | 2012-04-05 21:18:22 +0200 | [diff] [blame] | 7081 | txn->flags |= TX_CK_UNUSED; |
| 7082 | else |
| 7083 | txn->flags |= TX_CK_INVALID; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7084 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7085 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7086 | /* depending on the cookie mode, we may have to either : |
| 7087 | * - delete the complete cookie if we're in insert+indirect mode, so that |
| 7088 | * the server never sees it ; |
| 7089 | * - remove the server id from the cookie value, and tag the cookie as an |
| 7090 | * application cookie so that it does not get accidentely removed later, |
| 7091 | * if we're in cookie prefix mode |
| 7092 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7093 | if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7094 | int delta; /* negative */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7095 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7096 | delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7097 | val_end += delta; |
| 7098 | next += delta; |
| 7099 | hdr_end += delta; |
| 7100 | hdr_next += delta; |
| 7101 | cur_hdr->len += delta; |
| 7102 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7103 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7104 | del_from = NULL; |
| 7105 | preserve_hdr = 1; /* we want to keep this cookie */ |
| 7106 | } |
| 7107 | else if (del_from == NULL && |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7108 | (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] | 7109 | del_from = prev; |
| 7110 | } |
| 7111 | } else { |
| 7112 | /* This is not our cookie, so we must preserve it. But if we already |
| 7113 | * scheduled another cookie for removal, we cannot remove the |
| 7114 | * complete header, but we can remove the previous block itself. |
| 7115 | */ |
| 7116 | preserve_hdr = 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7117 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7118 | if (del_from != NULL) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7119 | int delta = del_hdr_value(req->buf, &del_from, prev); |
Willy Tarreau | b810554 | 2010-11-24 18:31:28 +0100 | [diff] [blame] | 7120 | if (att_beg >= del_from) |
| 7121 | att_beg += delta; |
| 7122 | if (att_end >= del_from) |
| 7123 | att_end += delta; |
| 7124 | val_beg += delta; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7125 | val_end += delta; |
| 7126 | next += delta; |
| 7127 | hdr_end += delta; |
| 7128 | hdr_next += delta; |
| 7129 | cur_hdr->len += delta; |
| 7130 | http_msg_move_end(&txn->req, delta); |
| 7131 | prev = del_from; |
| 7132 | del_from = NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7133 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7134 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7135 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7136 | /* continue with next cookie on this header line */ |
| 7137 | att_beg = next; |
| 7138 | } /* for each cookie */ |
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 | /* There are no more cookies on this line. |
| 7141 | * We may still have one (or several) marked for deletion at the |
| 7142 | * end of the line. We must do this now in two ways : |
| 7143 | * - if some cookies must be preserved, we only delete from the |
| 7144 | * mark to the end of line ; |
| 7145 | * - if nothing needs to be preserved, simply delete the whole header |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7146 | */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7147 | if (del_from) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7148 | int delta; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7149 | if (preserve_hdr) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7150 | delta = del_hdr_value(req->buf, &del_from, hdr_end); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7151 | hdr_end = del_from; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7152 | cur_hdr->len += delta; |
| 7153 | } else { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7154 | delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7155 | |
| 7156 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7157 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 7158 | txn->hdr_idx.used--; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7159 | cur_hdr->len = 0; |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 7160 | cur_idx = old_idx; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7161 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7162 | hdr_next += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7163 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7164 | } |
| 7165 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7166 | /* check next header */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7167 | old_idx = cur_idx; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7168 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7169 | } |
| 7170 | |
| 7171 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7172 | /* Iterate the same filter through all response headers contained in <rtr>. |
| 7173 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
| 7174 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7175 | 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] | 7176 | { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7177 | char *cur_ptr, *cur_end, *cur_next; |
| 7178 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7179 | struct http_txn *txn = s->txn; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7180 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7181 | int delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7182 | |
| 7183 | last_hdr = 0; |
| 7184 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7185 | cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7186 | old_idx = 0; |
| 7187 | |
| 7188 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7189 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7190 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7191 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7192 | (exp->action == ACT_ALLOW || |
| 7193 | exp->action == ACT_DENY)) |
| 7194 | return 0; |
| 7195 | |
| 7196 | cur_idx = txn->hdr_idx.v[old_idx].next; |
| 7197 | if (!cur_idx) |
| 7198 | break; |
| 7199 | |
| 7200 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 7201 | cur_ptr = cur_next; |
| 7202 | cur_end = cur_ptr + cur_hdr->len; |
| 7203 | cur_next = cur_end + cur_hdr->cr + 1; |
| 7204 | |
| 7205 | /* Now we have one header between cur_ptr and cur_end, |
| 7206 | * and the next header starts at cur_next. |
| 7207 | */ |
| 7208 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 7209 | 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] | 7210 | switch (exp->action) { |
| 7211 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7212 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7213 | last_hdr = 1; |
| 7214 | break; |
| 7215 | |
| 7216 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7217 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7218 | last_hdr = 1; |
| 7219 | break; |
| 7220 | |
| 7221 | case ACT_REPLACE: |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 7222 | trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch); |
| 7223 | if (trash.len < 0) |
| 7224 | return -1; |
| 7225 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7226 | 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] | 7227 | /* FIXME: if the user adds a newline in the replacement, the |
| 7228 | * index will not be recalculated for now, and the new line |
| 7229 | * will not be counted as a new header. |
| 7230 | */ |
| 7231 | |
| 7232 | cur_end += delta; |
| 7233 | cur_next += delta; |
| 7234 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7235 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7236 | break; |
| 7237 | |
| 7238 | case ACT_REMOVE: |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7239 | delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7240 | cur_next += delta; |
| 7241 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7242 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7243 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 7244 | txn->hdr_idx.used--; |
| 7245 | cur_hdr->len = 0; |
| 7246 | cur_end = NULL; /* null-term has been rewritten */ |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 7247 | cur_idx = old_idx; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7248 | break; |
| 7249 | |
| 7250 | } |
| 7251 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7252 | |
| 7253 | /* keep the link from this header to next one in case of later |
| 7254 | * removal of next header. |
| 7255 | */ |
| 7256 | old_idx = cur_idx; |
| 7257 | } |
| 7258 | return 0; |
| 7259 | } |
| 7260 | |
| 7261 | |
| 7262 | /* Apply the filter to the status line in the response buffer <rtr>. |
| 7263 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 7264 | * or -1 if a replacement resulted in an invalid status line. |
| 7265 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7266 | 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] | 7267 | { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7268 | char *cur_ptr, *cur_end; |
| 7269 | int done; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7270 | struct http_txn *txn = s->txn; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7271 | int delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7272 | |
| 7273 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7274 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7275 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7276 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7277 | (exp->action == ACT_ALLOW || |
| 7278 | exp->action == ACT_DENY)) |
| 7279 | return 0; |
| 7280 | else if (exp->action == ACT_REMOVE) |
| 7281 | return 0; |
| 7282 | |
| 7283 | done = 0; |
| 7284 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7285 | cur_ptr = rtr->buf->p; |
Willy Tarreau | 1ba0e5f | 2010-06-07 13:57:32 +0200 | [diff] [blame] | 7286 | cur_end = cur_ptr + txn->rsp.sl.st.l; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7287 | |
| 7288 | /* Now we have the status line between cur_ptr and cur_end */ |
| 7289 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 7290 | 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] | 7291 | switch (exp->action) { |
| 7292 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7293 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7294 | done = 1; |
| 7295 | break; |
| 7296 | |
| 7297 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7298 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7299 | done = 1; |
| 7300 | break; |
| 7301 | |
| 7302 | case ACT_REPLACE: |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 7303 | trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch); |
| 7304 | if (trash.len < 0) |
| 7305 | return -1; |
| 7306 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7307 | 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] | 7308 | /* FIXME: if the user adds a newline in the replacement, the |
| 7309 | * index will not be recalculated for now, and the new line |
| 7310 | * will not be counted as a new header. |
| 7311 | */ |
| 7312 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7313 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7314 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 7315 | cur_end = (char *)http_parse_stsline(&txn->rsp, |
Willy Tarreau | 0278576 | 2007-04-03 14:45:44 +0200 | [diff] [blame] | 7316 | HTTP_MSG_RPVER, |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7317 | cur_ptr, cur_end + 1, |
| 7318 | NULL, NULL); |
| 7319 | if (unlikely(!cur_end)) |
| 7320 | return -1; |
| 7321 | |
| 7322 | /* we have a full respnse and we know that we have either a CR |
| 7323 | * or an LF at <ptr>. |
| 7324 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7325 | 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] | 7326 | 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] | 7327 | /* there is no point trying this regex on headers */ |
| 7328 | return 1; |
| 7329 | } |
| 7330 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7331 | return done; |
| 7332 | } |
| 7333 | |
| 7334 | |
| 7335 | |
| 7336 | /* |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7337 | * 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] | 7338 | * Returns 0 if everything is alright, or -1 in case a replacement lead to an |
| 7339 | * unparsable response. |
| 7340 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7341 | 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] | 7342 | { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 7343 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7344 | struct http_txn *txn = s->txn; |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7345 | struct hdr_exp *exp; |
| 7346 | |
| 7347 | for (exp = px->rsp_exp; exp; exp = exp->next) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7348 | int ret; |
| 7349 | |
| 7350 | /* |
| 7351 | * The interleaving of transformations and verdicts |
| 7352 | * makes it difficult to decide to continue or stop |
| 7353 | * the evaluation. |
| 7354 | */ |
| 7355 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7356 | if (txn->flags & TX_SVDENY) |
| 7357 | break; |
| 7358 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7359 | if ((txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7360 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 7361 | exp->action == ACT_PASS)) { |
| 7362 | exp = exp->next; |
| 7363 | continue; |
| 7364 | } |
| 7365 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7366 | /* if this filter had a condition, evaluate it now and skip to |
| 7367 | * next filter if the condition does not match. |
| 7368 | */ |
| 7369 | if (exp->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 7370 | 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] | 7371 | ret = acl_pass(ret); |
| 7372 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 7373 | ret = !ret; |
| 7374 | if (!ret) |
| 7375 | continue; |
| 7376 | } |
| 7377 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7378 | /* Apply the filter to the status line. */ |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7379 | ret = apply_filter_to_sts_line(s, rtr, exp); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7380 | if (unlikely(ret < 0)) |
| 7381 | return -1; |
| 7382 | |
| 7383 | if (likely(ret == 0)) { |
| 7384 | /* The filter did not match the response, it can be |
| 7385 | * iterated through all headers. |
| 7386 | */ |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 7387 | if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0)) |
| 7388 | return -1; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7389 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7390 | } |
| 7391 | return 0; |
| 7392 | } |
| 7393 | |
| 7394 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7395 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 7396 | * 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] | 7397 | * desirable to call it only when needed. This function is also used when we |
| 7398 | * 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] | 7399 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7400 | void manage_server_side_cookies(struct stream *s, struct channel *res) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7401 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7402 | struct http_txn *txn = s->txn; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7403 | struct session *sess = s->sess; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 7404 | struct server *srv; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7405 | int is_cookie2; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7406 | int cur_idx, old_idx, delta; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7407 | char *hdr_beg, *hdr_end, *hdr_next; |
| 7408 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7409 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7410 | /* Iterate through the headers. |
| 7411 | * we start with the start line. |
| 7412 | */ |
| 7413 | old_idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7414 | hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7415 | |
| 7416 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 7417 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7418 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7419 | |
| 7420 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7421 | hdr_beg = hdr_next; |
| 7422 | hdr_end = hdr_beg + cur_hdr->len; |
| 7423 | hdr_next = hdr_end + cur_hdr->cr + 1; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7424 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7425 | /* We have one full header between hdr_beg and hdr_end, and the |
| 7426 | * next header starts at hdr_next. We're only interested in |
| 7427 | * "Set-Cookie" and "Set-Cookie2" headers. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7428 | */ |
| 7429 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7430 | is_cookie2 = 0; |
| 7431 | prev = hdr_beg + 10; |
| 7432 | val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10); |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7433 | if (!val) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7434 | val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11); |
| 7435 | if (!val) { |
| 7436 | old_idx = cur_idx; |
| 7437 | continue; |
| 7438 | } |
| 7439 | is_cookie2 = 1; |
| 7440 | prev = hdr_beg + 11; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7441 | } |
| 7442 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7443 | /* OK, right now we know we have a Set-Cookie* at hdr_beg, and |
| 7444 | * <prev> points to the colon. |
| 7445 | */ |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7446 | txn->flags |= TX_SCK_PRESENT; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7447 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7448 | /* Maybe we only wanted to see if there was a Set-Cookie (eg: |
| 7449 | * check-cache is enabled) and we are not interested in checking |
| 7450 | * them. Warning, the cookie capture is declared in the frontend. |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 7451 | */ |
Willy Tarreau | 53a09d5 | 2015-08-10 18:59:40 +0200 | [diff] [blame] | 7452 | if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7453 | return; |
| 7454 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7455 | /* OK so now we know we have to process this response cookie. |
| 7456 | * The format of the Set-Cookie header is slightly different |
| 7457 | * from the format of the Cookie header in that it does not |
| 7458 | * support the comma as a cookie delimiter (thus the header |
| 7459 | * cannot be folded) because the Expires attribute described in |
| 7460 | * the original Netscape's spec may contain an unquoted date |
| 7461 | * with a comma inside. We have to live with this because |
| 7462 | * many browsers don't support Max-Age and some browsers don't |
| 7463 | * support quoted strings. However the Set-Cookie2 header is |
| 7464 | * clean. |
| 7465 | * |
| 7466 | * We have to keep multiple pointers in order to support cookie |
| 7467 | * removal at the beginning, middle or end of header without |
| 7468 | * corrupting the header (in case of set-cookie2). A special |
| 7469 | * pointer, <scav> points to the beginning of the set-cookie-av |
| 7470 | * fields after the first semi-colon. The <next> pointer points |
| 7471 | * either to the end of line (set-cookie) or next unquoted comma |
| 7472 | * (set-cookie2). All of these headers are valid : |
| 7473 | * |
| 7474 | * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n |
| 7475 | * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n |
| 7476 | * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n |
| 7477 | * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n |
| 7478 | * | | | | | | | | | | |
| 7479 | * | | | | | | | | +-> next hdr_end <--+ |
| 7480 | * | | | | | | | +------------> scav |
| 7481 | * | | | | | | +--------------> val_end |
| 7482 | * | | | | | +--------------------> val_beg |
| 7483 | * | | | | +----------------------> equal |
| 7484 | * | | | +------------------------> att_end |
| 7485 | * | | +----------------------------> att_beg |
| 7486 | * | +------------------------------> prev |
| 7487 | * +-----------------------------------------> hdr_beg |
| 7488 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7489 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7490 | for (; prev < hdr_end; prev = next) { |
| 7491 | /* Iterate through all cookies on this line */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7492 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7493 | /* find att_beg */ |
| 7494 | att_beg = prev + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 7495 | while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg)) |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7496 | att_beg++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7497 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7498 | /* find att_end : this is the first character after the last non |
| 7499 | * space before the equal. It may be equal to hdr_end. |
| 7500 | */ |
| 7501 | equal = att_end = att_beg; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7502 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7503 | while (equal < hdr_end) { |
| 7504 | if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ',')) |
| 7505 | break; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 7506 | if (HTTP_IS_SPHT(*equal++)) |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7507 | continue; |
| 7508 | att_end = equal; |
| 7509 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7510 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7511 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 7512 | * is between <att_beg> and <equal>, both may be identical. |
| 7513 | */ |
| 7514 | |
| 7515 | /* look for end of cookie if there is an equal sign */ |
| 7516 | if (equal < hdr_end && *equal == '=') { |
| 7517 | /* look for the beginning of the value */ |
| 7518 | val_beg = equal + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 7519 | while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg)) |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7520 | val_beg++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7521 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7522 | /* find the end of the value, respecting quotes */ |
| 7523 | next = find_cookie_value_end(val_beg, hdr_end); |
| 7524 | |
| 7525 | /* make val_end point to the first white space or delimitor after the value */ |
| 7526 | val_end = next; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 7527 | while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1))) |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7528 | val_end--; |
| 7529 | } else { |
| 7530 | /* <equal> points to next comma, semi-colon or EOL */ |
| 7531 | val_beg = val_end = next = equal; |
| 7532 | } |
| 7533 | |
| 7534 | if (next < hdr_end) { |
| 7535 | /* Set-Cookie2 supports multiple cookies, and <next> points to |
| 7536 | * a colon or semi-colon before the end. So skip all attr-value |
| 7537 | * pairs and look for the next comma. For Set-Cookie, since |
| 7538 | * commas are permitted in values, skip to the end. |
| 7539 | */ |
| 7540 | if (is_cookie2) |
| 7541 | next = find_hdr_value_end(next, hdr_end); |
| 7542 | else |
| 7543 | next = hdr_end; |
| 7544 | } |
| 7545 | |
| 7546 | /* Now everything is as on the diagram above */ |
| 7547 | |
| 7548 | /* Ignore cookies with no equal sign */ |
| 7549 | if (equal == val_end) |
| 7550 | continue; |
| 7551 | |
| 7552 | /* If there are spaces around the equal sign, we need to |
| 7553 | * strip them otherwise we'll get trouble for cookie captures, |
| 7554 | * or even for rewrites. Since this happens extremely rarely, |
| 7555 | * it does not hurt performance. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7556 | */ |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7557 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 7558 | int stripped_before = 0; |
| 7559 | int stripped_after = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7560 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7561 | if (att_end != equal) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7562 | stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7563 | equal += stripped_before; |
| 7564 | val_beg += stripped_before; |
| 7565 | } |
| 7566 | |
| 7567 | if (val_beg > equal + 1) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7568 | stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7569 | val_beg += stripped_after; |
| 7570 | stripped_before += stripped_after; |
| 7571 | } |
| 7572 | |
| 7573 | val_end += stripped_before; |
| 7574 | next += stripped_before; |
| 7575 | hdr_end += stripped_before; |
| 7576 | hdr_next += stripped_before; |
| 7577 | cur_hdr->len += stripped_before; |
Willy Tarreau | 1fc1f45 | 2011-04-07 22:35:37 +0200 | [diff] [blame] | 7578 | http_msg_move_end(&txn->rsp, stripped_before); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7579 | } |
| 7580 | |
| 7581 | /* First, let's see if we want to capture this cookie. We check |
| 7582 | * that we don't already have a server side cookie, because we |
| 7583 | * can only capture one. Also as an optimisation, we ignore |
| 7584 | * cookies shorter than the declared name. |
| 7585 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7586 | if (sess->fe->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 7587 | txn->srv_cookie == NULL && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7588 | (val_end - att_beg >= sess->fe->capture_namelen) && |
| 7589 | memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7590 | int log_len = val_end - att_beg; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7591 | if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 7592 | ha_alert("HTTP logging : out of memory.\n"); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7593 | } |
Willy Tarreau | f70fc75 | 2010-11-19 11:27:18 +0100 | [diff] [blame] | 7594 | else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7595 | if (log_len > sess->fe->capture_len) |
| 7596 | log_len = sess->fe->capture_len; |
Willy Tarreau | f70fc75 | 2010-11-19 11:27:18 +0100 | [diff] [blame] | 7597 | memcpy(txn->srv_cookie, att_beg, log_len); |
| 7598 | txn->srv_cookie[log_len] = 0; |
| 7599 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7600 | } |
| 7601 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7602 | srv = objt_server(s->target); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7603 | /* now check if we need to process it for persistence */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 7604 | if (!(s->flags & SF_IGNORE_PRST) && |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7605 | (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) && |
| 7606 | (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) { |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7607 | /* assume passive cookie by default */ |
| 7608 | txn->flags &= ~TX_SCK_MASK; |
| 7609 | txn->flags |= TX_SCK_FOUND; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7610 | |
| 7611 | /* If the cookie is in insert mode on a known server, we'll delete |
| 7612 | * this occurrence because we'll insert another one later. |
| 7613 | * 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] | 7614 | * a direct access. |
| 7615 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7616 | if (s->be->ck_opts & PR_CK_PSV) { |
Willy Tarreau | ba4c5be | 2010-10-23 12:46:42 +0200 | [diff] [blame] | 7617 | /* The "preserve" flag was set, we don't want to touch the |
| 7618 | * server's cookie. |
| 7619 | */ |
| 7620 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7621 | else if ((srv && (s->be->ck_opts & PR_CK_INS)) || |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 7622 | ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7623 | /* this cookie must be deleted */ |
| 7624 | if (*prev == ':' && next == hdr_end) { |
| 7625 | /* whole header */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7626 | delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7627 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 7628 | txn->hdr_idx.used--; |
| 7629 | cur_hdr->len = 0; |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 7630 | cur_idx = old_idx; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7631 | hdr_next += delta; |
| 7632 | http_msg_move_end(&txn->rsp, delta); |
| 7633 | /* note: while both invalid now, <next> and <hdr_end> |
| 7634 | * are still equal, so the for() will stop as expected. |
| 7635 | */ |
| 7636 | } else { |
| 7637 | /* just remove the value */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7638 | int delta = del_hdr_value(res->buf, &prev, next); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7639 | next = prev; |
| 7640 | hdr_end += delta; |
| 7641 | hdr_next += delta; |
| 7642 | cur_hdr->len += delta; |
| 7643 | http_msg_move_end(&txn->rsp, delta); |
| 7644 | } |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7645 | txn->flags &= ~TX_SCK_MASK; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7646 | txn->flags |= TX_SCK_DELETED; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7647 | /* and go on with next cookie */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7648 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7649 | else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7650 | /* replace bytes val_beg->val_end with the cookie name associated |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7651 | * with this server since we know it. |
| 7652 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7653 | 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] | 7654 | next += delta; |
| 7655 | hdr_end += delta; |
| 7656 | hdr_next += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7657 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7658 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7659 | |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7660 | txn->flags &= ~TX_SCK_MASK; |
| 7661 | txn->flags |= TX_SCK_REPLACED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7662 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7663 | else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7664 | /* insert the cookie name associated with this server |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7665 | * before existing cookie, and insert a delimiter between them.. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7666 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7667 | 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] | 7668 | next += delta; |
| 7669 | hdr_end += delta; |
| 7670 | hdr_next += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7671 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7672 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7673 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 7674 | val_beg[srv->cklen] = COOKIE_DELIM; |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7675 | txn->flags &= ~TX_SCK_MASK; |
| 7676 | txn->flags |= TX_SCK_REPLACED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7677 | } |
| 7678 | } |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7679 | /* that's done for this cookie, check the next one on the same |
| 7680 | * line when next != hdr_end (only if is_cookie2). |
| 7681 | */ |
| 7682 | } |
| 7683 | /* check next header */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7684 | old_idx = cur_idx; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7685 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7686 | } |
| 7687 | |
| 7688 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7689 | /* |
Willy Tarreau | 0ad8e0d | 2017-12-22 15:03:36 +0100 | [diff] [blame] | 7690 | * Parses the Cache-Control and Pragma request header fields to determine if |
| 7691 | * the request may be served from the cache and/or if it is cacheable. Updates |
| 7692 | * s->txn->flags. |
| 7693 | */ |
| 7694 | void check_request_for_cacheability(struct stream *s, struct channel *chn) |
| 7695 | { |
| 7696 | struct http_txn *txn = s->txn; |
| 7697 | char *p1, *p2; |
| 7698 | char *cur_ptr, *cur_end, *cur_next; |
| 7699 | int pragma_found; |
| 7700 | int cc_found; |
| 7701 | int cur_idx; |
| 7702 | |
| 7703 | if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE) |
| 7704 | return; /* nothing more to do here */ |
| 7705 | |
| 7706 | cur_idx = 0; |
| 7707 | pragma_found = cc_found = 0; |
| 7708 | cur_next = chn->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
| 7709 | |
| 7710 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 7711 | struct hdr_idx_elem *cur_hdr; |
| 7712 | int val; |
| 7713 | |
| 7714 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 7715 | cur_ptr = cur_next; |
| 7716 | cur_end = cur_ptr + cur_hdr->len; |
| 7717 | cur_next = cur_end + cur_hdr->cr + 1; |
| 7718 | |
| 7719 | /* We have one full header between cur_ptr and cur_end, and the |
| 7720 | * next header starts at cur_next. |
| 7721 | */ |
| 7722 | |
| 7723 | val = http_header_match2(cur_ptr, cur_end, "Pragma", 6); |
| 7724 | if (val) { |
| 7725 | if ((cur_end - (cur_ptr + val) >= 8) && |
| 7726 | strncasecmp(cur_ptr + val, "no-cache", 8) == 0) { |
| 7727 | pragma_found = 1; |
| 7728 | continue; |
| 7729 | } |
| 7730 | } |
| 7731 | |
| 7732 | val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13); |
| 7733 | if (!val) |
| 7734 | continue; |
| 7735 | |
| 7736 | /* OK, right now we know we have a cache-control header at cur_ptr */ |
| 7737 | cc_found = 1; |
| 7738 | p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */ |
| 7739 | |
| 7740 | if (p1 >= cur_end) /* no more info */ |
| 7741 | continue; |
| 7742 | |
| 7743 | /* p1 is at the beginning of the value */ |
| 7744 | p2 = p1; |
| 7745 | while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2)) |
| 7746 | p2++; |
| 7747 | |
| 7748 | /* we have a complete value between p1 and p2. We don't check the |
| 7749 | * values after max-age, max-stale nor min-fresh, we simply don't |
| 7750 | * use the cache when they're specified. |
| 7751 | */ |
| 7752 | if (((p2 - p1 == 7) && strncasecmp(p1, "max-age", 7) == 0) || |
| 7753 | ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) || |
| 7754 | ((p2 - p1 == 9) && strncasecmp(p1, "max-stale", 9) == 0) || |
| 7755 | ((p2 - p1 == 9) && strncasecmp(p1, "min-fresh", 9) == 0)) { |
| 7756 | txn->flags |= TX_CACHE_IGNORE; |
| 7757 | continue; |
| 7758 | } |
| 7759 | |
| 7760 | if ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) { |
| 7761 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 7762 | continue; |
| 7763 | } |
| 7764 | } |
| 7765 | |
| 7766 | /* RFC7234#5.4: |
| 7767 | * When the Cache-Control header field is also present and |
| 7768 | * understood in a request, Pragma is ignored. |
| 7769 | * When the Cache-Control header field is not present in a |
| 7770 | * request, caches MUST consider the no-cache request |
| 7771 | * pragma-directive as having the same effect as if |
| 7772 | * "Cache-Control: no-cache" were present. |
| 7773 | */ |
| 7774 | if (!cc_found && pragma_found) |
| 7775 | txn->flags |= TX_CACHE_IGNORE; |
| 7776 | } |
| 7777 | |
| 7778 | /* |
Willy Tarreau | d3900cc | 2017-12-22 15:35:11 +0100 | [diff] [blame] | 7779 | * Check if response is cacheable or not. Updates s->txn->flags. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7780 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7781 | void check_response_for_cacheability(struct stream *s, struct channel *rtr) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7782 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7783 | struct http_txn *txn = s->txn; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7784 | char *p1, *p2; |
| 7785 | |
| 7786 | char *cur_ptr, *cur_end, *cur_next; |
| 7787 | int cur_idx; |
| 7788 | |
Willy Tarreau | 12b32f2 | 2017-12-21 16:08:09 +0100 | [diff] [blame] | 7789 | if (txn->status < 200) { |
| 7790 | /* do not try to cache interim responses! */ |
| 7791 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7792 | return; |
Willy Tarreau | 12b32f2 | 2017-12-21 16:08:09 +0100 | [diff] [blame] | 7793 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7794 | |
| 7795 | /* Iterate through the headers. |
| 7796 | * we start with the start line. |
| 7797 | */ |
| 7798 | cur_idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7799 | cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7800 | |
| 7801 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 7802 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7803 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7804 | |
| 7805 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 7806 | cur_ptr = cur_next; |
| 7807 | cur_end = cur_ptr + cur_hdr->len; |
| 7808 | cur_next = cur_end + cur_hdr->cr + 1; |
| 7809 | |
| 7810 | /* 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] | 7811 | * next header starts at cur_next. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7812 | */ |
| 7813 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7814 | val = http_header_match2(cur_ptr, cur_end, "Pragma", 6); |
| 7815 | if (val) { |
| 7816 | if ((cur_end - (cur_ptr + val) >= 8) && |
| 7817 | strncasecmp(cur_ptr + val, "no-cache", 8) == 0) { |
| 7818 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 7819 | return; |
| 7820 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7821 | } |
| 7822 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7823 | val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13); |
| 7824 | if (!val) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7825 | continue; |
| 7826 | |
| 7827 | /* OK, right now we know we have a cache-control header at cur_ptr */ |
| 7828 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7829 | p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7830 | |
| 7831 | if (p1 >= cur_end) /* no more info */ |
| 7832 | continue; |
| 7833 | |
| 7834 | /* p1 is at the beginning of the value */ |
| 7835 | p2 = p1; |
| 7836 | |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 7837 | while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7838 | p2++; |
| 7839 | |
| 7840 | /* we have a complete value between p1 and p2 */ |
| 7841 | if (p2 < cur_end && *p2 == '=') { |
Willy Tarreau | d3900cc | 2017-12-22 15:35:11 +0100 | [diff] [blame] | 7842 | if (((cur_end - p2) > 1 && (p2 - p1 == 7) && strncasecmp(p1, "max-age=0", 9) == 0) || |
| 7843 | ((cur_end - p2) > 1 && (p2 - p1 == 8) && strncasecmp(p1, "s-maxage=0", 10) == 0)) { |
| 7844 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 7845 | continue; |
| 7846 | } |
| 7847 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7848 | /* we have something of the form no-cache="set-cookie" */ |
| 7849 | if ((cur_end - p1 >= 21) && |
| 7850 | strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0 |
| 7851 | && (p1[20] == '"' || p1[20] == ',')) |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7852 | txn->flags &= ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7853 | continue; |
| 7854 | } |
| 7855 | |
| 7856 | /* OK, so we know that either p2 points to the end of string or to a comma */ |
| 7857 | if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) || |
Willy Tarreau | 5b15f90 | 2013-07-04 12:46:56 +0200 | [diff] [blame] | 7858 | ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) || |
Willy Tarreau | d3900cc | 2017-12-22 15:35:11 +0100 | [diff] [blame] | 7859 | ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0)) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7860 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7861 | return; |
| 7862 | } |
| 7863 | |
| 7864 | if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7865 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7866 | continue; |
| 7867 | } |
| 7868 | } |
| 7869 | } |
| 7870 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7871 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7872 | /* |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7873 | * 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] | 7874 | * for the current backend. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7875 | * |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7876 | * 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] | 7877 | * uri_auth field is valid. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7878 | * |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 7879 | * Returns 1 if stats should be provided, otherwise 0. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7880 | */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7881 | 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] | 7882 | { |
| 7883 | struct uri_auth *uri_auth = backend->uri_auth; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7884 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7885 | const char *uri = msg->chn->buf->p+ msg->sl.rq.u; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7886 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 7887 | if (!uri_auth) |
| 7888 | return 0; |
| 7889 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7890 | 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] | 7891 | return 0; |
| 7892 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7893 | /* check URI size */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7894 | if (uri_auth->uri_len > msg->sl.rq.u_l) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7895 | return 0; |
| 7896 | |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 7897 | if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7898 | return 0; |
| 7899 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7900 | return 1; |
| 7901 | } |
| 7902 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7903 | /* |
| 7904 | * 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] | 7905 | * By default it tries to report the error position as msg->err_pos. However if |
| 7906 | * this one is not set, it will then report msg->next, which is the last known |
| 7907 | * 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] | 7908 | * displays buffers as a contiguous area starting at buf->p. |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7909 | */ |
Emeric Brun | 8c1aaa2 | 2017-06-15 11:30:06 +0200 | [diff] [blame] | 7910 | 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] | 7911 | struct http_msg *msg, |
Willy Tarreau | 0da5b3b | 2017-09-21 09:30:46 +0200 | [diff] [blame] | 7912 | enum h1_state state, struct proxy *other_end) |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7913 | { |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 7914 | struct session *sess = strm_sess(s); |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 7915 | struct channel *chn = msg->chn; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 7916 | int len1, len2; |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 7917 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 7918 | HA_SPIN_LOCK(PROXY_LOCK, &proxy->lock); |
Willy Tarreau | f3764b7 | 2016-03-31 13:45:10 +0200 | [diff] [blame] | 7919 | es->len = MIN(chn->buf->i, global.tune.bufsize); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7920 | len1 = chn->buf->data + chn->buf->size - chn->buf->p; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 7921 | len1 = MIN(len1, es->len); |
| 7922 | len2 = es->len - len1; /* remaining data if buffer wraps */ |
| 7923 | |
Willy Tarreau | f3764b7 | 2016-03-31 13:45:10 +0200 | [diff] [blame] | 7924 | if (!es->buf) |
| 7925 | es->buf = malloc(global.tune.bufsize); |
| 7926 | |
| 7927 | if (es->buf) { |
| 7928 | memcpy(es->buf, chn->buf->p, len1); |
| 7929 | if (len2) |
| 7930 | memcpy(es->buf + len1, chn->buf->data, len2); |
| 7931 | } |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 7932 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7933 | if (msg->err_pos >= 0) |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 7934 | es->pos = msg->err_pos; |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 7935 | else |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 7936 | es->pos = msg->next; |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 7937 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7938 | es->when = date; // user-visible date |
| 7939 | es->sid = s->uniq_id; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 7940 | es->srv = objt_server(s->target); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7941 | es->oe = other_end; |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 7942 | if (objt_conn(sess->origin)) |
| 7943 | es->src = __objt_conn(sess->origin)->addr.from; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7944 | else |
| 7945 | memset(&es->src, 0, sizeof(es->src)); |
| 7946 | |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 7947 | es->state = state; |
Willy Tarreau | 10479e4 | 2010-12-12 14:00:34 +0100 | [diff] [blame] | 7948 | es->ev_id = error_snapshot_id++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 7949 | es->b_flags = chn->flags; |
Willy Tarreau | d04b1bc | 2012-05-08 11:03:10 +0200 | [diff] [blame] | 7950 | es->s_flags = s->flags; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7951 | es->t_flags = s->txn->flags; |
Willy Tarreau | d04b1bc | 2012-05-08 11:03:10 +0200 | [diff] [blame] | 7952 | es->m_flags = msg->flags; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7953 | es->b_out = chn->buf->o; |
| 7954 | es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 7955 | es->b_tot = chn->total; |
Willy Tarreau | d04b1bc | 2012-05-08 11:03:10 +0200 | [diff] [blame] | 7956 | es->m_clen = msg->chunk_len; |
| 7957 | es->m_blen = msg->body_len; |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 7958 | HA_SPIN_UNLOCK(PROXY_LOCK, &proxy->lock); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7959 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7960 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7961 | /* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of |
| 7962 | * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is |
| 7963 | * performed over the whole headers. Otherwise it must contain a valid header |
| 7964 | * context, initialised with ctx->idx=0 for the first lookup in a series. If |
| 7965 | * <occ> is positive or null, occurrence #occ from the beginning (or last ctx) |
| 7966 | * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less |
| 7967 | * 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] | 7968 | * -1. The value fetch stops at commas, so this function is suited for use with |
| 7969 | * list headers. |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7970 | * 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] | 7971 | */ |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 7972 | 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] | 7973 | struct hdr_idx *idx, int occ, |
| 7974 | struct hdr_ctx *ctx, char **vptr, int *vlen) |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7975 | { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7976 | struct hdr_ctx local_ctx; |
| 7977 | char *ptr_hist[MAX_HDR_HISTORY]; |
| 7978 | int len_hist[MAX_HDR_HISTORY]; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7979 | unsigned int hist_ptr; |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7980 | int found; |
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 | if (!ctx) { |
| 7983 | local_ctx.idx = 0; |
| 7984 | ctx = &local_ctx; |
| 7985 | } |
| 7986 | |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7987 | if (occ >= 0) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7988 | /* search from the beginning */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7989 | while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7990 | occ--; |
| 7991 | if (occ <= 0) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7992 | *vptr = ctx->line + ctx->val; |
| 7993 | *vlen = ctx->vlen; |
| 7994 | return 1; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7995 | } |
| 7996 | } |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7997 | return 0; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7998 | } |
| 7999 | |
| 8000 | /* negative occurrence, we scan all the list then walk back */ |
| 8001 | if (-occ > MAX_HDR_HISTORY) |
| 8002 | return 0; |
| 8003 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8004 | found = hist_ptr = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8005 | while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8006 | ptr_hist[hist_ptr] = ctx->line + ctx->val; |
| 8007 | len_hist[hist_ptr] = ctx->vlen; |
| 8008 | if (++hist_ptr >= MAX_HDR_HISTORY) |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8009 | hist_ptr = 0; |
| 8010 | found++; |
| 8011 | } |
| 8012 | if (-occ > found) |
| 8013 | return 0; |
| 8014 | /* 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] | 8015 | * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have |
| 8016 | * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ] |
| 8017 | * to remain in the 0..9 range. |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8018 | */ |
Willy Tarreau | 67dad27 | 2013-06-12 22:27:44 +0200 | [diff] [blame] | 8019 | hist_ptr += occ + MAX_HDR_HISTORY; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8020 | if (hist_ptr >= MAX_HDR_HISTORY) |
| 8021 | hist_ptr -= MAX_HDR_HISTORY; |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8022 | *vptr = ptr_hist[hist_ptr]; |
| 8023 | *vlen = len_hist[hist_ptr]; |
| 8024 | return 1; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8025 | } |
| 8026 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 8027 | /* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of |
| 8028 | * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is |
| 8029 | * performed over the whole headers. Otherwise it must contain a valid header |
| 8030 | * context, initialised with ctx->idx=0 for the first lookup in a series. If |
| 8031 | * <occ> is positive or null, occurrence #occ from the beginning (or last ctx) |
| 8032 | * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less |
| 8033 | * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is |
| 8034 | * -1. This function differs from http_get_hdr() in that it only returns full |
| 8035 | * line header values and does not stop at commas. |
| 8036 | * The return value is 0 if nothing was found, or non-zero otherwise. |
| 8037 | */ |
| 8038 | unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen, |
| 8039 | struct hdr_idx *idx, int occ, |
| 8040 | struct hdr_ctx *ctx, char **vptr, int *vlen) |
| 8041 | { |
| 8042 | struct hdr_ctx local_ctx; |
| 8043 | char *ptr_hist[MAX_HDR_HISTORY]; |
| 8044 | int len_hist[MAX_HDR_HISTORY]; |
| 8045 | unsigned int hist_ptr; |
| 8046 | int found; |
| 8047 | |
| 8048 | if (!ctx) { |
| 8049 | local_ctx.idx = 0; |
| 8050 | ctx = &local_ctx; |
| 8051 | } |
| 8052 | |
| 8053 | if (occ >= 0) { |
| 8054 | /* search from the beginning */ |
| 8055 | while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
| 8056 | occ--; |
| 8057 | if (occ <= 0) { |
| 8058 | *vptr = ctx->line + ctx->val; |
| 8059 | *vlen = ctx->vlen; |
| 8060 | return 1; |
| 8061 | } |
| 8062 | } |
| 8063 | return 0; |
| 8064 | } |
| 8065 | |
| 8066 | /* negative occurrence, we scan all the list then walk back */ |
| 8067 | if (-occ > MAX_HDR_HISTORY) |
| 8068 | return 0; |
| 8069 | |
| 8070 | found = hist_ptr = 0; |
| 8071 | while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
| 8072 | ptr_hist[hist_ptr] = ctx->line + ctx->val; |
| 8073 | len_hist[hist_ptr] = ctx->vlen; |
| 8074 | if (++hist_ptr >= MAX_HDR_HISTORY) |
| 8075 | hist_ptr = 0; |
| 8076 | found++; |
| 8077 | } |
| 8078 | if (-occ > found) |
| 8079 | return 0; |
Nenad Merdanovic | 69ad4b9 | 2016-03-29 13:14:30 +0200 | [diff] [blame] | 8080 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 8081 | /* 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] | 8082 | * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have |
| 8083 | * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ] |
| 8084 | * to remain in the 0..9 range. |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 8085 | */ |
Nenad Merdanovic | 69ad4b9 | 2016-03-29 13:14:30 +0200 | [diff] [blame] | 8086 | hist_ptr += occ + MAX_HDR_HISTORY; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 8087 | if (hist_ptr >= MAX_HDR_HISTORY) |
| 8088 | hist_ptr -= MAX_HDR_HISTORY; |
| 8089 | *vptr = ptr_hist[hist_ptr]; |
| 8090 | *vlen = len_hist[hist_ptr]; |
| 8091 | return 1; |
| 8092 | } |
| 8093 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 8094 | /* |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 8095 | * Print a debug line with a header. Always stop at the first CR or LF char, |
| 8096 | * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an |
| 8097 | * arrow is printed after the line which contains the pointer. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8098 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8099 | 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] | 8100 | { |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 8101 | struct session *sess = strm_sess(s); |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 8102 | int max; |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 8103 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8104 | 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] | 8105 | dir, |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 8106 | 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] | 8107 | 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] | 8108 | |
| 8109 | for (max = 0; start + max < end; max++) |
| 8110 | if (start[max] == '\r' || start[max] == '\n') |
| 8111 | break; |
| 8112 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 8113 | UBOUND(max, trash.size - trash.len - 3); |
| 8114 | trash.len += strlcpy2(trash.str + trash.len, start, max + 1); |
| 8115 | trash.str[trash.len++] = '\n'; |
Willy Tarreau | 89efaed | 2013-12-13 15:14:55 +0100 | [diff] [blame] | 8116 | shut_your_big_mouth_gcc(write(1, trash.str, trash.len)); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8117 | } |
| 8118 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8119 | |
| 8120 | /* Allocate a new HTTP transaction for stream <s> unless there is one already. |
| 8121 | * The hdr_idx is allocated as well. In case of allocation failure, everything |
| 8122 | * allocated is freed and NULL is returned. Otherwise the new transaction is |
| 8123 | * assigned to the stream and returned. |
| 8124 | */ |
| 8125 | struct http_txn *http_alloc_txn(struct stream *s) |
| 8126 | { |
| 8127 | struct http_txn *txn = s->txn; |
| 8128 | |
| 8129 | if (txn) |
| 8130 | return txn; |
| 8131 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 8132 | txn = pool_alloc(pool_head_http_txn); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8133 | if (!txn) |
| 8134 | return txn; |
| 8135 | |
| 8136 | txn->hdr_idx.size = global.tune.max_http_hdr; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 8137 | txn->hdr_idx.v = pool_alloc(pool_head_hdr_idx); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8138 | if (!txn->hdr_idx.v) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 8139 | pool_free(pool_head_http_txn, txn); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8140 | return NULL; |
| 8141 | } |
| 8142 | |
| 8143 | s->txn = txn; |
| 8144 | return txn; |
| 8145 | } |
| 8146 | |
Thierry FOURNIER | fd50f0b | 2015-09-25 18:53:18 +0200 | [diff] [blame] | 8147 | void http_txn_reset_req(struct http_txn *txn) |
| 8148 | { |
| 8149 | txn->req.flags = 0; |
| 8150 | txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */ |
| 8151 | txn->req.next = 0; |
| 8152 | txn->req.chunk_len = 0LL; |
| 8153 | txn->req.body_len = 0LL; |
| 8154 | txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */ |
| 8155 | } |
| 8156 | |
| 8157 | void http_txn_reset_res(struct http_txn *txn) |
| 8158 | { |
| 8159 | txn->rsp.flags = 0; |
| 8160 | txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */ |
| 8161 | txn->rsp.next = 0; |
| 8162 | txn->rsp.chunk_len = 0LL; |
| 8163 | txn->rsp.body_len = 0LL; |
| 8164 | txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */ |
| 8165 | } |
| 8166 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8167 | /* |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8168 | * 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] | 8169 | * the required fields are properly allocated and that we only need to (re)init |
| 8170 | * them. This should be used before processing any new request. |
| 8171 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8172 | void http_init_txn(struct stream *s) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8173 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8174 | struct http_txn *txn = s->txn; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 8175 | struct proxy *fe = strm_fe(s); |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8176 | |
| 8177 | txn->flags = 0; |
| 8178 | txn->status = -1; |
| 8179 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 8180 | txn->cookie_first_date = 0; |
| 8181 | txn->cookie_last_date = 0; |
| 8182 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8183 | txn->srv_cookie = NULL; |
| 8184 | txn->cli_cookie = NULL; |
| 8185 | txn->uri = NULL; |
| 8186 | |
Thierry FOURNIER | fd50f0b | 2015-09-25 18:53:18 +0200 | [diff] [blame] | 8187 | http_txn_reset_req(txn); |
| 8188 | http_txn_reset_res(txn); |
| 8189 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 8190 | txn->req.chn = &s->req; |
| 8191 | txn->rsp.chn = &s->res; |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 8192 | |
| 8193 | txn->auth.method = HTTP_AUTH_UNKNOWN; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8194 | |
| 8195 | txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */ |
| 8196 | if (fe->options2 & PR_O2_REQBUG_OK) |
| 8197 | txn->req.err_pos = -1; /* let buggy requests pass */ |
| 8198 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8199 | if (txn->hdr_idx.v) |
| 8200 | hdr_idx_init(&txn->hdr_idx); |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 8201 | |
| 8202 | vars_init(&s->vars_txn, SCOPE_TXN); |
| 8203 | vars_init(&s->vars_reqres, SCOPE_REQ); |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8204 | } |
| 8205 | |
| 8206 | /* to be used at the end of a transaction */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8207 | void http_end_txn(struct stream *s) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8208 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8209 | struct http_txn *txn = s->txn; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 8210 | struct proxy *fe = strm_fe(s); |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8211 | |
| 8212 | /* these ones will have been dynamically allocated */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 8213 | pool_free(pool_head_requri, txn->uri); |
| 8214 | pool_free(pool_head_capture, txn->cli_cookie); |
| 8215 | pool_free(pool_head_capture, txn->srv_cookie); |
| 8216 | pool_free(pool_head_uniqueid, s->unique_id); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 8217 | |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 8218 | s->unique_id = NULL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8219 | txn->uri = NULL; |
| 8220 | txn->srv_cookie = NULL; |
| 8221 | txn->cli_cookie = NULL; |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8222 | |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 8223 | if (s->req_cap) { |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8224 | struct cap_hdr *h; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 8225 | for (h = fe->req_cap; h; h = h->next) |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 8226 | pool_free(h->pool, s->req_cap[h->index]); |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 8227 | memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *)); |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8228 | } |
| 8229 | |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 8230 | if (s->res_cap) { |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8231 | struct cap_hdr *h; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 8232 | for (h = fe->rsp_cap; h; h = h->next) |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 8233 | pool_free(h->pool, s->res_cap[h->index]); |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 8234 | memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *)); |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8235 | } |
| 8236 | |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 8237 | vars_prune(&s->vars_txn, s->sess, s); |
| 8238 | vars_prune(&s->vars_reqres, s->sess, s); |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8239 | } |
| 8240 | |
| 8241 | /* 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] | 8242 | void http_reset_txn(struct stream *s) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8243 | { |
| 8244 | http_end_txn(s); |
| 8245 | http_init_txn(s); |
| 8246 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 8247 | /* reinitialise the current rule list pointer to NULL. We are sure that |
| 8248 | * any rulelist match the NULL pointer. |
| 8249 | */ |
| 8250 | s->current_rule_list = NULL; |
| 8251 | |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 8252 | s->be = strm_fe(s); |
| 8253 | s->logs.logwait = strm_fe(s)->to_log; |
Willy Tarreau | abcd514 | 2013-06-11 17:18:02 +0200 | [diff] [blame] | 8254 | s->logs.level = 0; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8255 | stream_del_srv_conn(s); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 8256 | s->target = NULL; |
Emeric Brun | b982a3d | 2010-01-04 15:45:53 +0100 | [diff] [blame] | 8257 | /* re-init store persistence */ |
| 8258 | s->store_count = 0; |
Willy Tarreau | 1f0da24 | 2014-01-25 11:01:50 +0100 | [diff] [blame] | 8259 | s->uniq_id = global.req_count++; |
Emeric Brun | b982a3d | 2010-01-04 15:45:53 +0100 | [diff] [blame] | 8260 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8261 | s->pend_pos = NULL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8262 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 8263 | s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */ |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8264 | |
Willy Tarreau | 739cfba | 2010-01-25 23:11:14 +0100 | [diff] [blame] | 8265 | /* We must trim any excess data from the response buffer, because we |
| 8266 | * may have blocked an invalid response from a server that we don't |
| 8267 | * want to accidentely forward once we disable the analysers, nor do |
| 8268 | * we want those data to come along with next response. A typical |
| 8269 | * example of such data would be from a buggy server responding to |
| 8270 | * a HEAD with some data, or sending more than the advertised |
| 8271 | * content-length. |
| 8272 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 8273 | if (unlikely(s->res.buf->i)) |
| 8274 | s->res.buf->i = 0; |
Willy Tarreau | 739cfba | 2010-01-25 23:11:14 +0100 | [diff] [blame] | 8275 | |
Christopher Faulet | c0c672a | 2017-03-28 11:51:33 +0200 | [diff] [blame] | 8276 | /* Now we can realign the response buffer */ |
| 8277 | buffer_realign(s->res.buf); |
| 8278 | |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 8279 | s->req.rto = strm_fe(s)->timeout.client; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 8280 | s->req.wto = TICK_ETERNITY; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8281 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 8282 | s->res.rto = TICK_ETERNITY; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 8283 | s->res.wto = strm_fe(s)->timeout.client; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8284 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 8285 | s->req.rex = TICK_ETERNITY; |
| 8286 | s->req.wex = TICK_ETERNITY; |
| 8287 | s->req.analyse_exp = TICK_ETERNITY; |
| 8288 | s->res.rex = TICK_ETERNITY; |
| 8289 | s->res.wex = TICK_ETERNITY; |
| 8290 | s->res.analyse_exp = TICK_ETERNITY; |
Hongbo Long | e39683c | 2017-03-10 18:41:51 +0100 | [diff] [blame] | 8291 | s->si[1].hcto = TICK_ETERNITY; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8292 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8293 | |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 8294 | void free_http_res_rules(struct list *r) |
| 8295 | { |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 8296 | struct act_rule *tr, *pr; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 8297 | |
| 8298 | list_for_each_entry_safe(pr, tr, r, list) { |
| 8299 | LIST_DEL(&pr->list); |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 8300 | regex_free(&pr->arg.hdr_add.re); |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 8301 | free(pr); |
| 8302 | } |
| 8303 | } |
| 8304 | |
| 8305 | void free_http_req_rules(struct list *r) |
| 8306 | { |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 8307 | struct act_rule *tr, *pr; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8308 | |
| 8309 | list_for_each_entry_safe(pr, tr, r, list) { |
| 8310 | LIST_DEL(&pr->list); |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8311 | if (pr->action == ACT_HTTP_REQ_AUTH) |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 8312 | free(pr->arg.auth.realm); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8313 | |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 8314 | regex_free(&pr->arg.hdr_add.re); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8315 | free(pr); |
| 8316 | } |
| 8317 | } |
| 8318 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8319 | /* parse an "http-request" rule */ |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 8320 | 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] | 8321 | { |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 8322 | struct act_rule *rule; |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 8323 | struct action_kw *custom = NULL; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8324 | int cur_arg; |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 8325 | char *error; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8326 | |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 8327 | rule = calloc(1, sizeof(*rule)); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8328 | if (!rule) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8329 | ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8330 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8331 | } |
| 8332 | |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 8333 | if (!strcmp(args[0], "allow")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8334 | rule->action = ACT_ACTION_ALLOW; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8335 | cur_arg = 1; |
Jarno Huuskonen | 800d176 | 2017-03-06 14:56:36 +0200 | [diff] [blame] | 8336 | } 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] | 8337 | int code; |
| 8338 | int hc; |
| 8339 | |
Jarno Huuskonen | 800d176 | 2017-03-06 14:56:36 +0200 | [diff] [blame] | 8340 | if (!strcmp(args[0], "tarpit")) { |
| 8341 | rule->action = ACT_HTTP_REQ_TARPIT; |
| 8342 | rule->deny_status = HTTP_ERR_500; |
| 8343 | } |
| 8344 | else { |
| 8345 | rule->action = ACT_ACTION_DENY; |
| 8346 | rule->deny_status = HTTP_ERR_403; |
| 8347 | } |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8348 | cur_arg = 1; |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 8349 | if (strcmp(args[cur_arg], "deny_status") == 0) { |
| 8350 | cur_arg++; |
| 8351 | if (!args[cur_arg]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8352 | ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing status code.\n", |
| 8353 | file, linenum, proxy_type_str(proxy), proxy->id, args[0]); |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 8354 | goto out_err; |
| 8355 | } |
| 8356 | |
| 8357 | code = atol(args[cur_arg]); |
| 8358 | cur_arg++; |
| 8359 | for (hc = 0; hc < HTTP_ERR_SIZE; hc++) { |
| 8360 | if (http_err_codes[hc] == code) { |
| 8361 | rule->deny_status = hc; |
| 8362 | break; |
| 8363 | } |
| 8364 | } |
| 8365 | |
| 8366 | if (hc >= HTTP_ERR_SIZE) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8367 | ha_warning("parsing [%s:%d] : status code %d not handled, using default code %d.\n", |
| 8368 | file, linenum, code, http_err_codes[rule->deny_status]); |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 8369 | } |
| 8370 | } |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8371 | } else if (!strcmp(args[0], "auth")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8372 | rule->action = ACT_HTTP_REQ_AUTH; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8373 | cur_arg = 1; |
| 8374 | |
| 8375 | while(*args[cur_arg]) { |
| 8376 | if (!strcmp(args[cur_arg], "realm")) { |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 8377 | rule->arg.auth.realm = strdup(args[cur_arg + 1]); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8378 | cur_arg+=2; |
| 8379 | continue; |
| 8380 | } else |
| 8381 | break; |
| 8382 | } |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 8383 | } else if (!strcmp(args[0], "set-nice")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8384 | rule->action = ACT_HTTP_SET_NICE; |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 8385 | cur_arg = 1; |
| 8386 | |
| 8387 | if (!*args[cur_arg] || |
| 8388 | (*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] | 8389 | ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n", |
| 8390 | file, linenum, args[0]); |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 8391 | goto out_err; |
| 8392 | } |
| 8393 | rule->arg.nice = atoi(args[cur_arg]); |
| 8394 | if (rule->arg.nice < -1024) |
| 8395 | rule->arg.nice = -1024; |
| 8396 | else if (rule->arg.nice > 1024) |
| 8397 | rule->arg.nice = 1024; |
| 8398 | cur_arg++; |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 8399 | } else if (!strcmp(args[0], "set-tos")) { |
| 8400 | #ifdef IP_TOS |
| 8401 | char *err; |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8402 | rule->action = ACT_HTTP_SET_TOS; |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 8403 | cur_arg = 1; |
| 8404 | |
| 8405 | if (!*args[cur_arg] || |
| 8406 | (*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] | 8407 | ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n", |
| 8408 | file, linenum, args[0]); |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 8409 | goto out_err; |
| 8410 | } |
| 8411 | |
| 8412 | rule->arg.tos = strtol(args[cur_arg], &err, 0); |
| 8413 | if (err && *err != '\0') { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8414 | ha_alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n", |
| 8415 | file, linenum, err, args[0]); |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 8416 | goto out_err; |
| 8417 | } |
| 8418 | cur_arg++; |
| 8419 | #else |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8420 | 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] | 8421 | goto out_err; |
| 8422 | #endif |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 8423 | } else if (!strcmp(args[0], "set-mark")) { |
| 8424 | #ifdef SO_MARK |
| 8425 | char *err; |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8426 | rule->action = ACT_HTTP_SET_MARK; |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 8427 | cur_arg = 1; |
| 8428 | |
| 8429 | if (!*args[cur_arg] || |
| 8430 | (*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] | 8431 | ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n", |
| 8432 | file, linenum, args[0]); |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 8433 | goto out_err; |
| 8434 | } |
| 8435 | |
| 8436 | rule->arg.mark = strtoul(args[cur_arg], &err, 0); |
| 8437 | if (err && *err != '\0') { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8438 | ha_alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n", |
| 8439 | file, linenum, err, args[0]); |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 8440 | goto out_err; |
| 8441 | } |
| 8442 | cur_arg++; |
| 8443 | global.last_checks |= LSTCHK_NETADM; |
| 8444 | #else |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8445 | 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] | 8446 | goto out_err; |
| 8447 | #endif |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 8448 | } else if (!strcmp(args[0], "set-log-level")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8449 | rule->action = ACT_HTTP_SET_LOGL; |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 8450 | cur_arg = 1; |
| 8451 | |
| 8452 | if (!*args[cur_arg] || |
| 8453 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 8454 | bad_log_level: |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8455 | ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n", |
| 8456 | file, linenum, args[0]); |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 8457 | goto out_err; |
| 8458 | } |
| 8459 | if (strcmp(args[cur_arg], "silent") == 0) |
| 8460 | rule->arg.loglevel = -1; |
| 8461 | else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0) |
| 8462 | goto bad_log_level; |
| 8463 | cur_arg++; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 8464 | } 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] | 8465 | 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] | 8466 | cur_arg = 1; |
| 8467 | |
Willy Tarreau | 8d1c516 | 2013-04-03 14:13:58 +0200 | [diff] [blame] | 8468 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 8469 | (*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] | 8470 | ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n", |
| 8471 | file, linenum, args[0]); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8472 | goto out_err; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 8473 | } |
| 8474 | |
| 8475 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 8476 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 8477 | LIST_INIT(&rule->arg.hdr_add.fmt); |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 8478 | |
Thierry FOURNIER | 1c0054f | 2013-11-20 15:09:52 +0100 | [diff] [blame] | 8479 | proxy->conf.args.ctx = ARGC_HRQ; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8480 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8481 | 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] | 8482 | (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] | 8483 | ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n", |
| 8484 | file, linenum, args[0], error); |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8485 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8486 | goto out_err; |
| 8487 | } |
Willy Tarreau | 59ad1a2 | 2014-01-29 14:39:58 +0100 | [diff] [blame] | 8488 | free(proxy->conf.lfs_file); |
| 8489 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 8490 | proxy->conf.lfs_line = proxy->conf.args.line; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 8491 | cur_arg += 2; |
Willy Tarreau | b854392 | 2014-06-17 18:58:26 +0200 | [diff] [blame] | 8492 | } 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] | 8493 | 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] | 8494 | cur_arg = 1; |
| 8495 | |
| 8496 | if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] || |
Baptiste Assmann | 92df370 | 2014-06-24 11:10:00 +0200 | [diff] [blame] | 8497 | (*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] | 8498 | ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n", |
| 8499 | file, linenum, args[0]); |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 8500 | goto out_err; |
| 8501 | } |
| 8502 | |
| 8503 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 8504 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 8505 | LIST_INIT(&rule->arg.hdr_add.fmt); |
| 8506 | |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 8507 | error = NULL; |
| 8508 | 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] | 8509 | ha_alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum, |
| 8510 | args[cur_arg + 1], error); |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 8511 | free(error); |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 8512 | goto out_err; |
| 8513 | } |
| 8514 | |
| 8515 | proxy->conf.args.ctx = ARGC_HRQ; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8516 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8517 | 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] | 8518 | (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] | 8519 | ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n", |
| 8520 | file, linenum, args[0], error); |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8521 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8522 | goto out_err; |
| 8523 | } |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 8524 | |
| 8525 | free(proxy->conf.lfs_file); |
| 8526 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 8527 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 8528 | cur_arg += 3; |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 8529 | } else if (strcmp(args[0], "del-header") == 0) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8530 | rule->action = ACT_HTTP_DEL_HDR; |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 8531 | cur_arg = 1; |
| 8532 | |
| 8533 | if (!*args[cur_arg] || |
| 8534 | (*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] | 8535 | ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 8536 | file, linenum, args[0]); |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 8537 | goto out_err; |
| 8538 | } |
| 8539 | |
| 8540 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 8541 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 8542 | |
| 8543 | proxy->conf.args.ctx = ARGC_HRQ; |
| 8544 | free(proxy->conf.lfs_file); |
| 8545 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 8546 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 8547 | cur_arg += 1; |
Frédéric Lécaille | a41d531 | 2018-01-29 12:05:07 +0100 | [diff] [blame] | 8548 | } else if (strncmp(args[0], "track-sc", 8) == 0) { |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 8549 | struct sample_expr *expr; |
| 8550 | unsigned int where; |
| 8551 | char *err = NULL; |
Frédéric Lécaille | a41d531 | 2018-01-29 12:05:07 +0100 | [diff] [blame] | 8552 | unsigned int tsc_num; |
| 8553 | const char *tsc_num_str; |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 8554 | |
| 8555 | cur_arg = 1; |
| 8556 | proxy->conf.args.ctx = ARGC_TRK; |
| 8557 | |
Frédéric Lécaille | a41d531 | 2018-01-29 12:05:07 +0100 | [diff] [blame] | 8558 | tsc_num_str = &args[0][8]; |
| 8559 | if (cfg_parse_track_sc_num(&tsc_num, tsc_num_str, tsc_num_str + strlen(tsc_num_str), &err) == -1) { |
| 8560 | ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n", |
| 8561 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], err); |
| 8562 | free(err); |
| 8563 | goto out_err; |
| 8564 | } |
| 8565 | |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 8566 | expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args); |
| 8567 | if (!expr) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8568 | ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n", |
| 8569 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], err); |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 8570 | free(err); |
| 8571 | goto out_err; |
| 8572 | } |
| 8573 | |
| 8574 | where = 0; |
| 8575 | if (proxy->cap & PR_CAP_FE) |
| 8576 | where |= SMP_VAL_FE_HRQ_HDR; |
| 8577 | if (proxy->cap & PR_CAP_BE) |
| 8578 | where |= SMP_VAL_BE_HRQ_HDR; |
| 8579 | |
| 8580 | if (!(expr->fetch->val & where)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8581 | ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :" |
| 8582 | " fetch method '%s' extracts information from '%s', none of which is available here.\n", |
| 8583 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], |
| 8584 | args[cur_arg-1], sample_src_names(expr->fetch->use)); |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 8585 | free(expr); |
| 8586 | goto out_err; |
| 8587 | } |
| 8588 | |
| 8589 | if (strcmp(args[cur_arg], "table") == 0) { |
| 8590 | cur_arg++; |
| 8591 | if (!args[cur_arg]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8592 | ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing table name.\n", |
| 8593 | file, linenum, proxy_type_str(proxy), proxy->id, args[0]); |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 8594 | free(expr); |
| 8595 | goto out_err; |
| 8596 | } |
| 8597 | /* we copy the table name for now, it will be resolved later */ |
Thierry FOURNIER | 5ec63e0 | 2015-08-04 09:09:48 +0200 | [diff] [blame] | 8598 | rule->arg.trk_ctr.table.n = strdup(args[cur_arg]); |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 8599 | cur_arg++; |
| 8600 | } |
Thierry FOURNIER | 5ec63e0 | 2015-08-04 09:09:48 +0200 | [diff] [blame] | 8601 | rule->arg.trk_ctr.expr = expr; |
Frédéric Lécaille | a41d531 | 2018-01-29 12:05:07 +0100 | [diff] [blame] | 8602 | rule->action = ACT_ACTION_TRK_SC0 + tsc_num; |
Christopher Faulet | 78880fb | 2017-09-18 14:43:55 +0200 | [diff] [blame] | 8603 | rule->check_ptr = check_trk_action; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8604 | } else if (strcmp(args[0], "redirect") == 0) { |
| 8605 | struct redirect_rule *redir; |
Willy Tarreau | 6d4890c | 2013-11-18 18:04:25 +0100 | [diff] [blame] | 8606 | char *errmsg = NULL; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8607 | |
Willy Tarreau | be4653b | 2015-05-28 15:26:58 +0200 | [diff] [blame] | 8608 | 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] | 8609 | ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n", |
| 8610 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8611 | goto out_err; |
| 8612 | } |
| 8613 | |
| 8614 | /* this redirect rule might already contain a parsed condition which |
| 8615 | * we'll pass to the http-request rule. |
| 8616 | */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8617 | rule->action = ACT_HTTP_REDIR; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8618 | rule->arg.redir = redir; |
| 8619 | rule->cond = redir->cond; |
| 8620 | redir->cond = NULL; |
| 8621 | cur_arg = 2; |
| 8622 | return rule; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8623 | } else if (strncmp(args[0], "add-acl", 7) == 0) { |
| 8624 | /* http-request add-acl(<reference (acl name)>) <key pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8625 | rule->action = ACT_HTTP_ADD_ACL; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8626 | /* |
| 8627 | * '+ 8' for 'add-acl(' |
| 8628 | * '- 9' for 'add-acl(' + trailing ')' |
| 8629 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 8630 | 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] | 8631 | |
| 8632 | cur_arg = 1; |
| 8633 | |
| 8634 | if (!*args[cur_arg] || |
| 8635 | (*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] | 8636 | ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 8637 | file, linenum, args[0]); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8638 | goto out_err; |
| 8639 | } |
| 8640 | |
| 8641 | LIST_INIT(&rule->arg.map.key); |
| 8642 | proxy->conf.args.ctx = ARGC_HRQ; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8643 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8644 | 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] | 8645 | (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] | 8646 | ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n", |
| 8647 | file, linenum, args[0], error); |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8648 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8649 | goto out_err; |
| 8650 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8651 | free(proxy->conf.lfs_file); |
| 8652 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 8653 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 8654 | cur_arg += 1; |
| 8655 | } else if (strncmp(args[0], "del-acl", 7) == 0) { |
| 8656 | /* http-request del-acl(<reference (acl name)>) <key pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8657 | rule->action = ACT_HTTP_DEL_ACL; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8658 | /* |
| 8659 | * '+ 8' for 'del-acl(' |
| 8660 | * '- 9' for 'del-acl(' + trailing ')' |
| 8661 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 8662 | 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] | 8663 | |
| 8664 | cur_arg = 1; |
| 8665 | |
| 8666 | if (!*args[cur_arg] || |
| 8667 | (*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] | 8668 | ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 8669 | file, linenum, args[0]); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8670 | goto out_err; |
| 8671 | } |
| 8672 | |
| 8673 | LIST_INIT(&rule->arg.map.key); |
| 8674 | proxy->conf.args.ctx = ARGC_HRQ; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8675 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8676 | 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] | 8677 | (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] | 8678 | ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n", |
| 8679 | file, linenum, args[0], error); |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8680 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8681 | goto out_err; |
| 8682 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8683 | free(proxy->conf.lfs_file); |
| 8684 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 8685 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 8686 | cur_arg += 1; |
| 8687 | } else if (strncmp(args[0], "del-map", 7) == 0) { |
| 8688 | /* http-request del-map(<reference (map name)>) <key pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8689 | rule->action = ACT_HTTP_DEL_MAP; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8690 | /* |
| 8691 | * '+ 8' for 'del-map(' |
| 8692 | * '- 9' for 'del-map(' + trailing ')' |
| 8693 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 8694 | 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] | 8695 | |
| 8696 | cur_arg = 1; |
| 8697 | |
| 8698 | if (!*args[cur_arg] || |
| 8699 | (*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] | 8700 | ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 8701 | file, linenum, args[0]); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8702 | goto out_err; |
| 8703 | } |
| 8704 | |
| 8705 | LIST_INIT(&rule->arg.map.key); |
| 8706 | proxy->conf.args.ctx = ARGC_HRQ; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8707 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8708 | 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] | 8709 | (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] | 8710 | ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n", |
| 8711 | file, linenum, args[0], error); |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8712 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8713 | goto out_err; |
| 8714 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8715 | free(proxy->conf.lfs_file); |
| 8716 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 8717 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 8718 | cur_arg += 1; |
| 8719 | } else if (strncmp(args[0], "set-map", 7) == 0) { |
| 8720 | /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8721 | rule->action = ACT_HTTP_SET_MAP; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8722 | /* |
| 8723 | * '+ 8' for 'set-map(' |
| 8724 | * '- 9' for 'set-map(' + trailing ')' |
| 8725 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 8726 | 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] | 8727 | |
| 8728 | cur_arg = 1; |
| 8729 | |
| 8730 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 8731 | (*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] | 8732 | ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n", |
| 8733 | file, linenum, args[0]); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8734 | goto out_err; |
| 8735 | } |
| 8736 | |
| 8737 | LIST_INIT(&rule->arg.map.key); |
| 8738 | LIST_INIT(&rule->arg.map.value); |
| 8739 | proxy->conf.args.ctx = ARGC_HRQ; |
| 8740 | |
| 8741 | /* key pattern */ |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8742 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8743 | 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] | 8744 | (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] | 8745 | ha_alert("parsing [%s:%d]: 'http-request %s' key: %s.\n", |
| 8746 | file, linenum, args[0], error); |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8747 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8748 | goto out_err; |
| 8749 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8750 | |
| 8751 | /* value pattern */ |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8752 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8753 | 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] | 8754 | (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] | 8755 | ha_alert("parsing [%s:%d]: 'http-request %s' pattern: %s.\n", |
| 8756 | file, linenum, args[0], error); |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8757 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8758 | goto out_err; |
| 8759 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8760 | free(proxy->conf.lfs_file); |
| 8761 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 8762 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 8763 | |
| 8764 | cur_arg += 2; |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 8765 | } else if (((custom = action_http_req_custom(args[0])) != NULL)) { |
| 8766 | char *errmsg = NULL; |
| 8767 | cur_arg = 1; |
| 8768 | /* try in the module list */ |
Thierry FOURNIER | 5563e4b | 2015-08-14 19:20:07 +0200 | [diff] [blame] | 8769 | rule->from = ACT_F_HTTP_REQ; |
Thierry FOURNIER | 85c6c97 | 2015-09-22 19:14:35 +0200 | [diff] [blame] | 8770 | rule->kw = custom; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 8771 | 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] | 8772 | ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n", |
| 8773 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 8774 | free(errmsg); |
| 8775 | goto out_err; |
| 8776 | } |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8777 | } else { |
Thierry FOURNIER | ab95e65 | 2015-10-02 08:24:51 +0200 | [diff] [blame] | 8778 | action_build_list(&http_req_keywords.list, &trash); |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8779 | ha_alert("parsing [%s:%d]: 'http-request' expects 'allow', 'deny', 'auth', 'redirect', " |
| 8780 | "'tarpit', 'add-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', " |
| 8781 | "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'track-sc*'" |
| 8782 | "%s%s, but got '%s'%s.\n", |
| 8783 | file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)"); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8784 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8785 | } |
| 8786 | |
| 8787 | if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) { |
| 8788 | struct acl_cond *cond; |
Willy Tarreau | b7451bb | 2012-04-27 12:38:15 +0200 | [diff] [blame] | 8789 | char *errmsg = NULL; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8790 | |
Christopher Faulet | 1b421ea | 2017-09-22 14:38:56 +0200 | [diff] [blame] | 8791 | 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] | 8792 | ha_alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n", |
| 8793 | file, linenum, args[0], errmsg); |
Willy Tarreau | b7451bb | 2012-04-27 12:38:15 +0200 | [diff] [blame] | 8794 | free(errmsg); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8795 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8796 | } |
| 8797 | rule->cond = cond; |
| 8798 | } |
| 8799 | else if (*args[cur_arg]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8800 | ha_alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or" |
| 8801 | " either 'if' or 'unless' followed by a condition but found '%s'.\n", |
| 8802 | file, linenum, args[0], args[cur_arg]); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8803 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8804 | } |
| 8805 | |
| 8806 | return rule; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8807 | out_err: |
| 8808 | free(rule); |
| 8809 | return NULL; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8810 | } |
| 8811 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8812 | /* parse an "http-respose" rule */ |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 8813 | 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] | 8814 | { |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 8815 | struct act_rule *rule; |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 8816 | struct action_kw *custom = NULL; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8817 | int cur_arg; |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 8818 | char *error; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8819 | |
| 8820 | rule = calloc(1, sizeof(*rule)); |
| 8821 | if (!rule) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8822 | ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum); |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8823 | goto out_err; |
| 8824 | } |
| 8825 | |
| 8826 | if (!strcmp(args[0], "allow")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8827 | rule->action = ACT_ACTION_ALLOW; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8828 | cur_arg = 1; |
| 8829 | } else if (!strcmp(args[0], "deny")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8830 | rule->action = ACT_ACTION_DENY; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8831 | cur_arg = 1; |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 8832 | } else if (!strcmp(args[0], "set-nice")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8833 | rule->action = ACT_HTTP_SET_NICE; |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 8834 | cur_arg = 1; |
| 8835 | |
| 8836 | if (!*args[cur_arg] || |
| 8837 | (*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] | 8838 | ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n", |
| 8839 | file, linenum, args[0]); |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 8840 | goto out_err; |
| 8841 | } |
| 8842 | rule->arg.nice = atoi(args[cur_arg]); |
| 8843 | if (rule->arg.nice < -1024) |
| 8844 | rule->arg.nice = -1024; |
| 8845 | else if (rule->arg.nice > 1024) |
| 8846 | rule->arg.nice = 1024; |
| 8847 | cur_arg++; |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 8848 | } else if (!strcmp(args[0], "set-tos")) { |
| 8849 | #ifdef IP_TOS |
| 8850 | char *err; |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8851 | rule->action = ACT_HTTP_SET_TOS; |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 8852 | cur_arg = 1; |
| 8853 | |
| 8854 | if (!*args[cur_arg] || |
| 8855 | (*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] | 8856 | ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n", |
| 8857 | file, linenum, args[0]); |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 8858 | goto out_err; |
| 8859 | } |
| 8860 | |
| 8861 | rule->arg.tos = strtol(args[cur_arg], &err, 0); |
| 8862 | if (err && *err != '\0') { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8863 | ha_alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n", |
| 8864 | file, linenum, err, args[0]); |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 8865 | goto out_err; |
| 8866 | } |
| 8867 | cur_arg++; |
| 8868 | #else |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8869 | 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] | 8870 | goto out_err; |
| 8871 | #endif |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 8872 | } else if (!strcmp(args[0], "set-mark")) { |
| 8873 | #ifdef SO_MARK |
| 8874 | char *err; |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8875 | rule->action = ACT_HTTP_SET_MARK; |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 8876 | cur_arg = 1; |
| 8877 | |
| 8878 | if (!*args[cur_arg] || |
| 8879 | (*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] | 8880 | ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n", |
| 8881 | file, linenum, args[0]); |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 8882 | goto out_err; |
| 8883 | } |
| 8884 | |
| 8885 | rule->arg.mark = strtoul(args[cur_arg], &err, 0); |
| 8886 | if (err && *err != '\0') { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8887 | ha_alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n", |
| 8888 | file, linenum, err, args[0]); |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 8889 | goto out_err; |
| 8890 | } |
| 8891 | cur_arg++; |
| 8892 | global.last_checks |= LSTCHK_NETADM; |
| 8893 | #else |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8894 | 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] | 8895 | goto out_err; |
| 8896 | #endif |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 8897 | } else if (!strcmp(args[0], "set-log-level")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8898 | rule->action = ACT_HTTP_SET_LOGL; |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 8899 | cur_arg = 1; |
| 8900 | |
| 8901 | if (!*args[cur_arg] || |
| 8902 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 8903 | bad_log_level: |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8904 | ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n", |
| 8905 | file, linenum, args[0]); |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 8906 | goto out_err; |
| 8907 | } |
| 8908 | if (strcmp(args[cur_arg], "silent") == 0) |
| 8909 | rule->arg.loglevel = -1; |
Ruoshan Huang | dd01678 | 2016-06-15 22:16:03 +0800 | [diff] [blame] | 8910 | 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] | 8911 | goto bad_log_level; |
| 8912 | cur_arg++; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8913 | } 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] | 8914 | 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] | 8915 | cur_arg = 1; |
| 8916 | |
| 8917 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 8918 | (*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] | 8919 | ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n", |
| 8920 | file, linenum, args[0]); |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8921 | goto out_err; |
| 8922 | } |
| 8923 | |
| 8924 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 8925 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 8926 | LIST_INIT(&rule->arg.hdr_add.fmt); |
| 8927 | |
Thierry FOURNIER | 1c0054f | 2013-11-20 15:09:52 +0100 | [diff] [blame] | 8928 | proxy->conf.args.ctx = ARGC_HRS; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8929 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8930 | 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] | 8931 | (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] | 8932 | ha_alert("parsing [%s:%d]: 'http-response %s': %s.\n", |
| 8933 | file, linenum, args[0], error); |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8934 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8935 | goto out_err; |
| 8936 | } |
Willy Tarreau | 59ad1a2 | 2014-01-29 14:39:58 +0100 | [diff] [blame] | 8937 | free(proxy->conf.lfs_file); |
| 8938 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 8939 | proxy->conf.lfs_line = proxy->conf.args.line; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8940 | cur_arg += 2; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 8941 | } 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] | 8942 | 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] | 8943 | cur_arg = 1; |
| 8944 | |
| 8945 | if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] || |
Baptiste Assmann | 12cb00b | 2014-08-08 17:29:06 +0200 | [diff] [blame] | 8946 | (*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] | 8947 | ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n", |
| 8948 | file, linenum, args[0]); |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 8949 | goto out_err; |
| 8950 | } |
| 8951 | |
| 8952 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 8953 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 8954 | LIST_INIT(&rule->arg.hdr_add.fmt); |
| 8955 | |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 8956 | error = NULL; |
| 8957 | 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] | 8958 | ha_alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum, |
| 8959 | args[cur_arg + 1], error); |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 8960 | free(error); |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 8961 | goto out_err; |
| 8962 | } |
| 8963 | |
| 8964 | proxy->conf.args.ctx = ARGC_HRQ; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8965 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8966 | 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] | 8967 | (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] | 8968 | ha_alert("parsing [%s:%d]: 'http-response %s': %s.\n", |
| 8969 | file, linenum, args[0], error); |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8970 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8971 | goto out_err; |
| 8972 | } |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 8973 | |
| 8974 | free(proxy->conf.lfs_file); |
| 8975 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 8976 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 8977 | cur_arg += 3; |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 8978 | } else if (strcmp(args[0], "del-header") == 0) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8979 | rule->action = ACT_HTTP_DEL_HDR; |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 8980 | cur_arg = 1; |
| 8981 | |
| 8982 | if (!*args[cur_arg] || |
| 8983 | (*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] | 8984 | ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 8985 | file, linenum, args[0]); |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 8986 | goto out_err; |
| 8987 | } |
| 8988 | |
| 8989 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 8990 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 8991 | |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8992 | proxy->conf.args.ctx = ARGC_HRS; |
| 8993 | free(proxy->conf.lfs_file); |
| 8994 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 8995 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 8996 | cur_arg += 1; |
| 8997 | } else if (strncmp(args[0], "add-acl", 7) == 0) { |
| 8998 | /* http-request add-acl(<reference (acl name)>) <key pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8999 | rule->action = ACT_HTTP_ADD_ACL; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9000 | /* |
| 9001 | * '+ 8' for 'add-acl(' |
| 9002 | * '- 9' for 'add-acl(' + trailing ')' |
| 9003 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9004 | 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] | 9005 | |
| 9006 | cur_arg = 1; |
| 9007 | |
| 9008 | if (!*args[cur_arg] || |
| 9009 | (*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] | 9010 | ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 9011 | file, linenum, args[0]); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9012 | goto out_err; |
| 9013 | } |
| 9014 | |
| 9015 | LIST_INIT(&rule->arg.map.key); |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9016 | proxy->conf.args.ctx = ARGC_HRS; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9017 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9018 | 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] | 9019 | (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] | 9020 | ha_alert("parsing [%s:%d]: 'http-response %s': %s.\n", |
| 9021 | file, linenum, args[0], error); |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9022 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9023 | goto out_err; |
| 9024 | } |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9025 | free(proxy->conf.lfs_file); |
| 9026 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9027 | proxy->conf.lfs_line = proxy->conf.args.line; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9028 | |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9029 | cur_arg += 1; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9030 | } else if (strncmp(args[0], "del-acl", 7) == 0) { |
| 9031 | /* http-response del-acl(<reference (acl name)>) <key pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9032 | rule->action = ACT_HTTP_DEL_ACL; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9033 | /* |
| 9034 | * '+ 8' for 'del-acl(' |
| 9035 | * '- 9' for 'del-acl(' + trailing ')' |
| 9036 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9037 | 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] | 9038 | |
| 9039 | cur_arg = 1; |
| 9040 | |
| 9041 | if (!*args[cur_arg] || |
| 9042 | (*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] | 9043 | ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 9044 | file, linenum, args[0]); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9045 | goto out_err; |
| 9046 | } |
| 9047 | |
| 9048 | LIST_INIT(&rule->arg.map.key); |
| 9049 | proxy->conf.args.ctx = ARGC_HRS; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9050 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9051 | 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] | 9052 | (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] | 9053 | ha_alert("parsing [%s:%d]: 'http-response %s': %s.\n", |
| 9054 | file, linenum, args[0], error); |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9055 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9056 | goto out_err; |
| 9057 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9058 | free(proxy->conf.lfs_file); |
| 9059 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9060 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9061 | cur_arg += 1; |
| 9062 | } else if (strncmp(args[0], "del-map", 7) == 0) { |
| 9063 | /* http-response del-map(<reference (map name)>) <key pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9064 | rule->action = ACT_HTTP_DEL_MAP; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9065 | /* |
| 9066 | * '+ 8' for 'del-map(' |
| 9067 | * '- 9' for 'del-map(' + trailing ')' |
| 9068 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9069 | 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] | 9070 | |
| 9071 | cur_arg = 1; |
| 9072 | |
| 9073 | if (!*args[cur_arg] || |
| 9074 | (*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] | 9075 | ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 9076 | file, linenum, args[0]); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9077 | goto out_err; |
| 9078 | } |
| 9079 | |
| 9080 | LIST_INIT(&rule->arg.map.key); |
| 9081 | proxy->conf.args.ctx = ARGC_HRS; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9082 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9083 | 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] | 9084 | (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] | 9085 | ha_alert("parsing [%s:%d]: 'http-response %s' %s.\n", |
| 9086 | file, linenum, args[0], error); |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9087 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9088 | goto out_err; |
| 9089 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9090 | free(proxy->conf.lfs_file); |
| 9091 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9092 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9093 | cur_arg += 1; |
| 9094 | } else if (strncmp(args[0], "set-map", 7) == 0) { |
| 9095 | /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9096 | rule->action = ACT_HTTP_SET_MAP; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9097 | /* |
| 9098 | * '+ 8' for 'set-map(' |
| 9099 | * '- 9' for 'set-map(' + trailing ')' |
| 9100 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9101 | 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] | 9102 | |
| 9103 | cur_arg = 1; |
| 9104 | |
| 9105 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 9106 | (*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] | 9107 | ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n", |
| 9108 | file, linenum, args[0]); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9109 | goto out_err; |
| 9110 | } |
| 9111 | |
| 9112 | LIST_INIT(&rule->arg.map.key); |
| 9113 | LIST_INIT(&rule->arg.map.value); |
| 9114 | |
| 9115 | proxy->conf.args.ctx = ARGC_HRS; |
| 9116 | |
| 9117 | /* key pattern */ |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9118 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9119 | 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] | 9120 | (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] | 9121 | ha_alert("parsing [%s:%d]: 'http-response %s' name: %s.\n", |
| 9122 | file, linenum, args[0], error); |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9123 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9124 | goto out_err; |
| 9125 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9126 | |
| 9127 | /* value pattern */ |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9128 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9129 | 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] | 9130 | (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] | 9131 | ha_alert("parsing [%s:%d]: 'http-response %s' value: %s.\n", |
| 9132 | file, linenum, args[0], error); |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 9133 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9134 | goto out_err; |
| 9135 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9136 | |
| 9137 | free(proxy->conf.lfs_file); |
| 9138 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9139 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9140 | |
| 9141 | cur_arg += 2; |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 9142 | } else if (strcmp(args[0], "redirect") == 0) { |
| 9143 | struct redirect_rule *redir; |
| 9144 | char *errmsg = NULL; |
| 9145 | |
| 9146 | 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] | 9147 | ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n", |
| 9148 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 9149 | goto out_err; |
| 9150 | } |
| 9151 | |
| 9152 | /* this redirect rule might already contain a parsed condition which |
| 9153 | * we'll pass to the http-request rule. |
| 9154 | */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9155 | rule->action = ACT_HTTP_REDIR; |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 9156 | rule->arg.redir = redir; |
| 9157 | rule->cond = redir->cond; |
| 9158 | redir->cond = NULL; |
| 9159 | cur_arg = 2; |
| 9160 | return rule; |
Frédéric Lécaille | a41d531 | 2018-01-29 12:05:07 +0100 | [diff] [blame] | 9161 | } else if (strncmp(args[0], "track-sc", 8) == 0) { |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 9162 | struct sample_expr *expr; |
| 9163 | unsigned int where; |
| 9164 | char *err = NULL; |
Frédéric Lécaille | a41d531 | 2018-01-29 12:05:07 +0100 | [diff] [blame] | 9165 | unsigned int tsc_num; |
| 9166 | const char *tsc_num_str; |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 9167 | |
| 9168 | cur_arg = 1; |
| 9169 | proxy->conf.args.ctx = ARGC_TRK; |
| 9170 | |
Frédéric Lécaille | a41d531 | 2018-01-29 12:05:07 +0100 | [diff] [blame] | 9171 | tsc_num_str = &args[0][8]; |
| 9172 | if (cfg_parse_track_sc_num(&tsc_num, tsc_num_str, tsc_num_str + strlen(tsc_num_str), &err) == -1) { |
| 9173 | ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n", |
| 9174 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], err); |
| 9175 | free(err); |
| 9176 | goto out_err; |
| 9177 | } |
| 9178 | |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 9179 | expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args); |
| 9180 | if (!expr) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 9181 | ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n", |
| 9182 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], err); |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 9183 | free(err); |
| 9184 | goto out_err; |
| 9185 | } |
| 9186 | |
| 9187 | where = 0; |
| 9188 | if (proxy->cap & PR_CAP_FE) |
| 9189 | where |= SMP_VAL_FE_HRS_HDR; |
| 9190 | if (proxy->cap & PR_CAP_BE) |
| 9191 | where |= SMP_VAL_BE_HRS_HDR; |
| 9192 | |
| 9193 | if (!(expr->fetch->val & where)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 9194 | ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule :" |
| 9195 | " fetch method '%s' extracts information from '%s', none of which is available here.\n", |
| 9196 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], |
| 9197 | args[cur_arg-1], sample_src_names(expr->fetch->use)); |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 9198 | free(expr); |
| 9199 | goto out_err; |
| 9200 | } |
| 9201 | |
| 9202 | if (strcmp(args[cur_arg], "table") == 0) { |
| 9203 | cur_arg++; |
| 9204 | if (!args[cur_arg]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 9205 | ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : missing table name.\n", |
| 9206 | file, linenum, proxy_type_str(proxy), proxy->id, args[0]); |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 9207 | free(expr); |
| 9208 | goto out_err; |
| 9209 | } |
| 9210 | /* we copy the table name for now, it will be resolved later */ |
| 9211 | rule->arg.trk_ctr.table.n = strdup(args[cur_arg]); |
| 9212 | cur_arg++; |
| 9213 | } |
| 9214 | rule->arg.trk_ctr.expr = expr; |
Frédéric Lécaille | a41d531 | 2018-01-29 12:05:07 +0100 | [diff] [blame] | 9215 | rule->action = ACT_ACTION_TRK_SC0 + tsc_num; |
Christopher Faulet | 78880fb | 2017-09-18 14:43:55 +0200 | [diff] [blame] | 9216 | rule->check_ptr = check_trk_action; |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 9217 | } else if (((custom = action_http_res_custom(args[0])) != NULL)) { |
| 9218 | char *errmsg = NULL; |
| 9219 | cur_arg = 1; |
| 9220 | /* try in the module list */ |
Thierry FOURNIER | 5563e4b | 2015-08-14 19:20:07 +0200 | [diff] [blame] | 9221 | rule->from = ACT_F_HTTP_RES; |
Thierry FOURNIER | 85c6c97 | 2015-09-22 19:14:35 +0200 | [diff] [blame] | 9222 | rule->kw = custom; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 9223 | 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] | 9224 | ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n", |
| 9225 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 9226 | free(errmsg); |
| 9227 | goto out_err; |
| 9228 | } |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9229 | } else { |
Thierry FOURNIER | ab95e65 | 2015-10-02 08:24:51 +0200 | [diff] [blame] | 9230 | action_build_list(&http_res_keywords.list, &trash); |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 9231 | ha_alert("parsing [%s:%d]: 'http-response' expects 'allow', 'deny', 'redirect', " |
| 9232 | "'add-header', 'del-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', " |
| 9233 | "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'track-sc*'" |
| 9234 | "%s%s, but got '%s'%s.\n", |
| 9235 | file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)"); |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9236 | goto out_err; |
| 9237 | } |
| 9238 | |
| 9239 | if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) { |
| 9240 | struct acl_cond *cond; |
| 9241 | char *errmsg = NULL; |
| 9242 | |
Christopher Faulet | 1b421ea | 2017-09-22 14:38:56 +0200 | [diff] [blame] | 9243 | 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] | 9244 | ha_alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n", |
| 9245 | file, linenum, args[0], errmsg); |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9246 | free(errmsg); |
| 9247 | goto out_err; |
| 9248 | } |
| 9249 | rule->cond = cond; |
| 9250 | } |
| 9251 | else if (*args[cur_arg]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 9252 | ha_alert("parsing [%s:%d]: 'http-response %s' expects" |
| 9253 | " either 'if' or 'unless' followed by a condition but found '%s'.\n", |
| 9254 | file, linenum, args[0], args[cur_arg]); |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9255 | goto out_err; |
| 9256 | } |
| 9257 | |
| 9258 | return rule; |
| 9259 | out_err: |
| 9260 | free(rule); |
| 9261 | return NULL; |
| 9262 | } |
| 9263 | |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9264 | /* 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] | 9265 | * 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] | 9266 | * dynamic log-format rule instead of a static string. Parameter <dir> indicates |
| 9267 | * 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] | 9268 | */ |
| 9269 | 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] | 9270 | const char **args, char **errmsg, int use_fmt, int dir) |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9271 | { |
| 9272 | struct redirect_rule *rule; |
| 9273 | int cur_arg; |
| 9274 | int type = REDIRECT_TYPE_NONE; |
| 9275 | int code = 302; |
| 9276 | const char *destination = NULL; |
| 9277 | const char *cookie = NULL; |
| 9278 | int cookie_set = 0; |
| 9279 | unsigned int flags = REDIRECT_FLAG_NONE; |
| 9280 | struct acl_cond *cond = NULL; |
| 9281 | |
| 9282 | cur_arg = 0; |
| 9283 | while (*(args[cur_arg])) { |
| 9284 | if (strcmp(args[cur_arg], "location") == 0) { |
| 9285 | if (!*args[cur_arg + 1]) |
| 9286 | goto missing_arg; |
| 9287 | |
| 9288 | type = REDIRECT_TYPE_LOCATION; |
| 9289 | cur_arg++; |
| 9290 | destination = args[cur_arg]; |
| 9291 | } |
| 9292 | else if (strcmp(args[cur_arg], "prefix") == 0) { |
| 9293 | if (!*args[cur_arg + 1]) |
| 9294 | goto missing_arg; |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9295 | type = REDIRECT_TYPE_PREFIX; |
| 9296 | cur_arg++; |
| 9297 | destination = args[cur_arg]; |
| 9298 | } |
| 9299 | else if (strcmp(args[cur_arg], "scheme") == 0) { |
| 9300 | if (!*args[cur_arg + 1]) |
| 9301 | goto missing_arg; |
| 9302 | |
| 9303 | type = REDIRECT_TYPE_SCHEME; |
| 9304 | cur_arg++; |
| 9305 | destination = args[cur_arg]; |
| 9306 | } |
| 9307 | else if (strcmp(args[cur_arg], "set-cookie") == 0) { |
| 9308 | if (!*args[cur_arg + 1]) |
| 9309 | goto missing_arg; |
| 9310 | |
| 9311 | cur_arg++; |
| 9312 | cookie = args[cur_arg]; |
| 9313 | cookie_set = 1; |
| 9314 | } |
| 9315 | else if (strcmp(args[cur_arg], "clear-cookie") == 0) { |
| 9316 | if (!*args[cur_arg + 1]) |
| 9317 | goto missing_arg; |
| 9318 | |
| 9319 | cur_arg++; |
| 9320 | cookie = args[cur_arg]; |
| 9321 | cookie_set = 0; |
| 9322 | } |
| 9323 | else if (strcmp(args[cur_arg], "code") == 0) { |
| 9324 | if (!*args[cur_arg + 1]) |
| 9325 | goto missing_arg; |
| 9326 | |
| 9327 | cur_arg++; |
| 9328 | code = atol(args[cur_arg]); |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 9329 | if (code < 301 || code > 308 || (code > 303 && code < 307)) { |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9330 | memprintf(errmsg, |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 9331 | "'%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] | 9332 | args[cur_arg - 1], args[cur_arg]); |
| 9333 | return NULL; |
| 9334 | } |
| 9335 | } |
| 9336 | else if (!strcmp(args[cur_arg],"drop-query")) { |
| 9337 | flags |= REDIRECT_FLAG_DROP_QS; |
| 9338 | } |
| 9339 | else if (!strcmp(args[cur_arg],"append-slash")) { |
| 9340 | flags |= REDIRECT_FLAG_APPEND_SLASH; |
| 9341 | } |
| 9342 | else if (strcmp(args[cur_arg], "if") == 0 || |
| 9343 | strcmp(args[cur_arg], "unless") == 0) { |
Olivier Houchard | fbc74e8 | 2017-11-24 16:54:05 +0100 | [diff] [blame] | 9344 | 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] | 9345 | if (!cond) { |
| 9346 | memprintf(errmsg, "error in condition: %s", *errmsg); |
| 9347 | return NULL; |
| 9348 | } |
| 9349 | break; |
| 9350 | } |
| 9351 | else { |
| 9352 | memprintf(errmsg, |
| 9353 | "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')", |
| 9354 | args[cur_arg]); |
| 9355 | return NULL; |
| 9356 | } |
| 9357 | cur_arg++; |
| 9358 | } |
| 9359 | |
| 9360 | if (type == REDIRECT_TYPE_NONE) { |
| 9361 | memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')"); |
| 9362 | return NULL; |
| 9363 | } |
| 9364 | |
Willy Tarreau | be4653b | 2015-05-28 15:26:58 +0200 | [diff] [blame] | 9365 | if (dir && type != REDIRECT_TYPE_LOCATION) { |
| 9366 | memprintf(errmsg, "response only supports redirect type 'location'"); |
| 9367 | return NULL; |
| 9368 | } |
| 9369 | |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 9370 | rule = calloc(1, sizeof(*rule)); |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9371 | rule->cond = cond; |
Willy Tarreau | 60e0838 | 2013-12-03 00:48:45 +0100 | [diff] [blame] | 9372 | LIST_INIT(&rule->rdr_fmt); |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 9373 | |
| 9374 | if (!use_fmt) { |
| 9375 | /* old-style static redirect rule */ |
| 9376 | rule->rdr_str = strdup(destination); |
| 9377 | rule->rdr_len = strlen(destination); |
| 9378 | } |
| 9379 | else { |
| 9380 | /* log-format based redirect rule */ |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 9381 | |
| 9382 | /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case, |
| 9383 | * if prefix == "/", we don't want to add anything, otherwise it |
| 9384 | * makes it hard for the user to configure a self-redirection. |
| 9385 | */ |
Godbach | d972203 | 2014-12-18 15:44:58 +0800 | [diff] [blame] | 9386 | curproxy->conf.args.ctx = ARGC_RDR; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 9387 | if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) { |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9388 | if (!parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP, |
| 9389 | 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] | 9390 | : (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9391 | errmsg)) { |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9392 | return NULL; |
| 9393 | } |
Willy Tarreau | 59ad1a2 | 2014-01-29 14:39:58 +0100 | [diff] [blame] | 9394 | free(curproxy->conf.lfs_file); |
| 9395 | curproxy->conf.lfs_file = strdup(curproxy->conf.args.file); |
| 9396 | curproxy->conf.lfs_line = curproxy->conf.args.line; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 9397 | } |
| 9398 | } |
| 9399 | |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9400 | if (cookie) { |
| 9401 | /* depending on cookie_set, either we want to set the cookie, or to clear it. |
| 9402 | * a clear consists in appending "; path=/; Max-Age=0;" at the end. |
| 9403 | */ |
| 9404 | rule->cookie_len = strlen(cookie); |
| 9405 | if (cookie_set) { |
| 9406 | rule->cookie_str = malloc(rule->cookie_len + 10); |
| 9407 | memcpy(rule->cookie_str, cookie, rule->cookie_len); |
| 9408 | memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10); |
| 9409 | rule->cookie_len += 9; |
| 9410 | } else { |
| 9411 | rule->cookie_str = malloc(rule->cookie_len + 21); |
| 9412 | memcpy(rule->cookie_str, cookie, rule->cookie_len); |
| 9413 | memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21); |
| 9414 | rule->cookie_len += 20; |
| 9415 | } |
| 9416 | } |
| 9417 | rule->type = type; |
| 9418 | rule->code = code; |
| 9419 | rule->flags = flags; |
| 9420 | LIST_INIT(&rule->list); |
| 9421 | return rule; |
| 9422 | |
| 9423 | missing_arg: |
| 9424 | memprintf(errmsg, "missing argument for '%s'", args[cur_arg]); |
| 9425 | return NULL; |
| 9426 | } |
| 9427 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9428 | /************************************************************************/ |
| 9429 | /* The code below is dedicated to ACL parsing and matching */ |
| 9430 | /************************************************************************/ |
| 9431 | |
| 9432 | |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9433 | /* This function ensures that the prerequisites for an L7 fetch are ready, |
| 9434 | * which means that a request or response is ready. If some data is missing, |
| 9435 | * 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] | 9436 | * to extract data from L7. If <req_vol> is non-null during a request prefetch, |
| 9437 | * another test is made to ensure the required information is not gone. |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9438 | * |
| 9439 | * The function returns : |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9440 | * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to |
| 9441 | * decide whether or not an HTTP message is present ; |
| 9442 | * 0 if the requested data cannot be fetched or if it is certain that |
| 9443 | * we'll never have any HTTP message there ; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9444 | * 1 if an HTTP message is ready |
| 9445 | */ |
James Rosewell | 91a41cb | 2015-09-18 17:11:16 +0100 | [diff] [blame] | 9446 | 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] | 9447 | const struct arg *args, struct sample *smp, int req_vol) |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9448 | { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 9449 | struct http_txn *txn; |
| 9450 | struct http_msg *msg; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9451 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 9452 | /* Note: it is possible that <s> is NULL when called before stream |
| 9453 | * initialization (eg: tcp-request connection), so this function is the |
| 9454 | * one responsible for guarding against this case for all HTTP users. |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9455 | */ |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 9456 | if (!s) |
| 9457 | return 0; |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 9458 | |
Thierry FOURNIER | ed08d6a | 2015-09-24 08:40:18 +0200 | [diff] [blame] | 9459 | if (!s->txn) { |
| 9460 | if (unlikely(!http_alloc_txn(s))) |
| 9461 | return 0; /* not enough memory */ |
| 9462 | http_init_txn(s); |
| 9463 | } |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 9464 | txn = s->txn; |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 9465 | msg = &txn->req; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9466 | |
| 9467 | /* Check for a dependency on a request */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9468 | smp->data.type = SMP_T_BOOL; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9469 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 9470 | if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 9471 | /* If the buffer does not leave enough free space at the end, |
| 9472 | * we must first realign it. |
| 9473 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 9474 | if (s->req.buf->p > s->req.buf->data && |
| 9475 | s->req.buf->i + s->req.buf->p > s->req.buf->data + s->req.buf->size - global.tune.maxrewrite) |
| 9476 | buffer_slow_realign(s->req.buf); |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 9477 | |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9478 | if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 472b1ee | 2013-10-14 22:41:30 +0200 | [diff] [blame] | 9479 | if (msg->msg_state == HTTP_MSG_ERROR) |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9480 | return 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9481 | |
| 9482 | /* Try to decode HTTP request */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 9483 | if (likely(msg->next < s->req.buf->i)) |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9484 | http_msg_analyzer(msg, &txn->hdr_idx); |
| 9485 | |
| 9486 | /* Still no valid request ? */ |
| 9487 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 3bf1b2b | 2012-08-27 20:46:07 +0200 | [diff] [blame] | 9488 | if ((msg->msg_state == HTTP_MSG_ERROR) || |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 9489 | buffer_full(s->req.buf, global.tune.maxrewrite)) { |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9490 | return 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9491 | } |
| 9492 | /* wait for final state */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9493 | smp->flags |= SMP_F_MAY_CHANGE; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9494 | return 0; |
| 9495 | } |
| 9496 | |
| 9497 | /* OK we just got a valid HTTP request. We have some minor |
| 9498 | * preparation to perform so that further checks can rely |
| 9499 | * on HTTP tests. |
| 9500 | */ |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 9501 | |
| 9502 | /* If the request was parsed but was too large, we must absolutely |
| 9503 | * return an error so that it is not processed. At the moment this |
| 9504 | * cannot happen, but if the parsers are to change in the future, |
| 9505 | * we want this check to be maintained. |
| 9506 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 9507 | if (unlikely(s->req.buf->i + s->req.buf->p > |
| 9508 | s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)) { |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 9509 | msg->err_state = msg->msg_state; |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 9510 | msg->msg_state = HTTP_MSG_ERROR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9511 | smp->data.u.sint = 1; |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 9512 | return 1; |
| 9513 | } |
| 9514 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9515 | 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] | 9516 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 9517 | s->flags |= SF_REDIRECTABLE; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9518 | |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9519 | if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn)) |
| 9520 | return 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9521 | } |
| 9522 | |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9523 | if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) { |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9524 | return 0; /* data might have moved and indexes changed */ |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9525 | } |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9526 | |
| 9527 | /* otherwise everything's ready for the request */ |
| 9528 | } |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 9529 | else { |
| 9530 | /* Check for a dependency on a response */ |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9531 | if (txn->rsp.msg_state < HTTP_MSG_BODY) { |
| 9532 | smp->flags |= SMP_F_MAY_CHANGE; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9533 | return 0; |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9534 | } |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9535 | } |
| 9536 | |
| 9537 | /* everything's OK */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9538 | smp->data.u.sint = 1; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9539 | return 1; |
| 9540 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9541 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9542 | /* 1. Check on METHOD |
| 9543 | * We use the pre-parsed method if it is known, and store its number as an |
| 9544 | * integer. If it is unknown, we use the pointer and the length. |
| 9545 | */ |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 9546 | 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] | 9547 | { |
| 9548 | int len, meth; |
| 9549 | |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 9550 | len = strlen(text); |
| 9551 | meth = find_http_meth(text, len); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9552 | |
| 9553 | pattern->val.i = meth; |
| 9554 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | 912c119 | 2014-08-29 15:15:50 +0200 | [diff] [blame] | 9555 | pattern->ptr.str = (char *)text; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9556 | pattern->len = len; |
| 9557 | } |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 9558 | else { |
| 9559 | pattern->ptr.str = NULL; |
| 9560 | pattern->len = 0; |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 9561 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9562 | return 1; |
| 9563 | } |
| 9564 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 9565 | /* This function fetches the method of current HTTP request and stores |
| 9566 | * it in the global pattern struct as a chunk. There are two possibilities : |
| 9567 | * - if the method is known (not HTTP_METH_OTHER), its identifier is stored |
| 9568 | * in <len> and <ptr> is NULL ; |
| 9569 | * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and |
| 9570 | * <len> to its length. |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 9571 | * This is intended to be used with pat_match_meth() only. |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 9572 | */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 9573 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9574 | 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] | 9575 | { |
| 9576 | int meth; |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 9577 | struct http_txn *txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9578 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 9579 | CHECK_HTTP_MESSAGE_FIRST_PERM(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9580 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 9581 | txn = smp->strm->txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9582 | meth = txn->meth; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9583 | smp->data.type = SMP_T_METH; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9584 | smp->data.u.meth.meth = meth; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9585 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9586 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 9587 | /* ensure the indexes are not affected */ |
| 9588 | return 0; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9589 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9590 | smp->data.u.meth.str.len = txn->req.sl.rq.m_l; |
| 9591 | smp->data.u.meth.str.str = txn->req.chn->buf->p; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9592 | } |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9593 | smp->flags |= SMP_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9594 | return 1; |
| 9595 | } |
| 9596 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 9597 | /* See above how the method is stored in the global pattern */ |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 9598 | 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] | 9599 | { |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 9600 | int icase; |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 9601 | struct pattern_list *lst; |
| 9602 | struct pattern *pattern; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 9603 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 9604 | list_for_each_entry(lst, &expr->patterns, list) { |
| 9605 | pattern = &lst->pat; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9606 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 9607 | /* well-known method */ |
| 9608 | if (pattern->val.i != HTTP_METH_OTHER) { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9609 | if (smp->data.u.meth.meth == pattern->val.i) |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 9610 | return pattern; |
| 9611 | else |
| 9612 | continue; |
| 9613 | } |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 9614 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 9615 | /* Other method, we must compare the strings */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9616 | if (pattern->len != smp->data.u.meth.str.len) |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 9617 | continue; |
| 9618 | |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 9619 | icase = expr->mflags & PAT_MF_IGNORE_CASE; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9620 | if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.meth.str.str, smp->data.u.meth.str.len) == 0) || |
| 9621 | (!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] | 9622 | return pattern; |
| 9623 | } |
| 9624 | return NULL; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9625 | } |
| 9626 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 9627 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9628 | 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] | 9629 | { |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 9630 | struct http_txn *txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9631 | char *ptr; |
| 9632 | int len; |
| 9633 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9634 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9635 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 9636 | txn = smp->strm->txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9637 | len = txn->req.sl.rq.v_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9638 | ptr = txn->req.chn->buf->p + txn->req.sl.rq.v; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9639 | |
| 9640 | while ((len-- > 0) && (*ptr++ != '/')); |
| 9641 | if (len <= 0) |
| 9642 | return 0; |
| 9643 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9644 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9645 | smp->data.u.str.str = ptr; |
| 9646 | smp->data.u.str.len = len; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9647 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9648 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9649 | return 1; |
| 9650 | } |
| 9651 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 9652 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9653 | 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] | 9654 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 9655 | struct http_txn *txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9656 | char *ptr; |
| 9657 | int len; |
| 9658 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9659 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9660 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 9661 | txn = smp->strm->txn; |
Willy Tarreau | f26b252 | 2012-12-14 08:33:14 +0100 | [diff] [blame] | 9662 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
| 9663 | return 0; |
| 9664 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9665 | len = txn->rsp.sl.st.v_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9666 | ptr = txn->rsp.chn->buf->p; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9667 | |
| 9668 | while ((len-- > 0) && (*ptr++ != '/')); |
| 9669 | if (len <= 0) |
| 9670 | return 0; |
| 9671 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9672 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9673 | smp->data.u.str.str = ptr; |
| 9674 | smp->data.u.str.len = len; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9675 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9676 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9677 | return 1; |
| 9678 | } |
| 9679 | |
| 9680 | /* 3. Check on Status Code. We manipulate integers here. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 9681 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9682 | 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] | 9683 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 9684 | struct http_txn *txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9685 | char *ptr; |
| 9686 | int len; |
| 9687 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9688 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9689 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 9690 | txn = smp->strm->txn; |
Willy Tarreau | f26b252 | 2012-12-14 08:33:14 +0100 | [diff] [blame] | 9691 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
| 9692 | return 0; |
| 9693 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9694 | len = txn->rsp.sl.st.c_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9695 | ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9696 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9697 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9698 | smp->data.u.sint = __strl2ui(ptr, len); |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9699 | smp->flags = SMP_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9700 | return 1; |
| 9701 | } |
| 9702 | |
Thierry Fournier | f4011dd | 2016-03-29 17:23:51 +0200 | [diff] [blame] | 9703 | static int |
| 9704 | smp_fetch_uniqueid(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 9705 | { |
| 9706 | if (LIST_ISEMPTY(&smp->sess->fe->format_unique_id)) |
| 9707 | return 0; |
| 9708 | |
| 9709 | if (!smp->strm->unique_id) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 9710 | if ((smp->strm->unique_id = pool_alloc(pool_head_uniqueid)) == NULL) |
Thierry Fournier | f4011dd | 2016-03-29 17:23:51 +0200 | [diff] [blame] | 9711 | return 0; |
| 9712 | smp->strm->unique_id[0] = '\0'; |
| 9713 | } |
| 9714 | smp->data.u.str.len = build_logline(smp->strm, smp->strm->unique_id, |
| 9715 | UNIQUEID_LEN, &smp->sess->fe->format_unique_id); |
| 9716 | |
| 9717 | smp->data.type = SMP_T_STR; |
| 9718 | smp->data.u.str.str = smp->strm->unique_id; |
| 9719 | smp->flags = SMP_F_CONST; |
| 9720 | return 1; |
| 9721 | } |
| 9722 | |
Thierry FOURNIER | d7d8881 | 2017-04-19 15:15:14 +0200 | [diff] [blame] | 9723 | /* Returns a string block containing all headers including the |
| 9724 | * empty line wich separes headers from the body. This is useful |
| 9725 | * form some headers analysis. |
| 9726 | */ |
| 9727 | static int |
| 9728 | smp_fetch_hdrs(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 9729 | { |
| 9730 | struct http_msg *msg; |
| 9731 | struct hdr_idx *idx; |
| 9732 | struct http_txn *txn; |
| 9733 | |
| 9734 | CHECK_HTTP_MESSAGE_FIRST(); |
| 9735 | |
| 9736 | txn = smp->strm->txn; |
| 9737 | idx = &txn->hdr_idx; |
| 9738 | msg = &txn->req; |
| 9739 | |
| 9740 | smp->data.type = SMP_T_STR; |
| 9741 | smp->data.u.str.str = msg->chn->buf->p + hdr_idx_first_pos(idx); |
| 9742 | smp->data.u.str.len = msg->eoh - hdr_idx_first_pos(idx) + 1 + |
| 9743 | (msg->chn->buf->p[msg->eoh] == '\r'); |
| 9744 | |
| 9745 | return 1; |
| 9746 | } |
| 9747 | |
Thierry FOURNIER | 5617dce | 2017-04-09 05:38:19 +0200 | [diff] [blame] | 9748 | /* Returns the header request in a length/value encoded format. |
| 9749 | * This is useful for exchanges with the SPOE. |
| 9750 | * |
| 9751 | * A "length value" is a multibyte code encoding numbers. It uses the |
| 9752 | * SPOE format. The encoding is the following: |
| 9753 | * |
| 9754 | * Each couple "header name" / "header value" is composed |
| 9755 | * like this: |
| 9756 | * "length value" "header name bytes" |
| 9757 | * "length value" "header value bytes" |
| 9758 | * When the last header is reached, the header name and the header |
| 9759 | * value are empty. Their length are 0 |
| 9760 | */ |
| 9761 | static int |
| 9762 | smp_fetch_hdrs_bin(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 9763 | { |
| 9764 | struct http_msg *msg; |
| 9765 | struct chunk *temp; |
| 9766 | struct hdr_idx *idx; |
| 9767 | const char *cur_ptr, *cur_next, *p; |
| 9768 | int old_idx, cur_idx; |
| 9769 | struct hdr_idx_elem *cur_hdr; |
| 9770 | const char *hn, *hv; |
| 9771 | int hnl, hvl; |
| 9772 | int ret; |
| 9773 | struct http_txn *txn; |
| 9774 | char *buf; |
| 9775 | char *end; |
| 9776 | |
| 9777 | CHECK_HTTP_MESSAGE_FIRST(); |
| 9778 | |
| 9779 | temp = get_trash_chunk(); |
| 9780 | buf = temp->str; |
| 9781 | end = temp->str + temp->size; |
| 9782 | |
| 9783 | txn = smp->strm->txn; |
| 9784 | idx = &txn->hdr_idx; |
| 9785 | msg = &txn->req; |
| 9786 | |
| 9787 | /* Build array of headers. */ |
| 9788 | old_idx = 0; |
| 9789 | cur_next = msg->chn->buf->p + hdr_idx_first_pos(idx); |
| 9790 | while (1) { |
| 9791 | cur_idx = idx->v[old_idx].next; |
| 9792 | if (!cur_idx) |
| 9793 | break; |
| 9794 | old_idx = cur_idx; |
| 9795 | |
| 9796 | cur_hdr = &idx->v[cur_idx]; |
| 9797 | cur_ptr = cur_next; |
| 9798 | cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1; |
| 9799 | |
| 9800 | /* Now we have one full header at cur_ptr of len cur_hdr->len, |
| 9801 | * and the next header starts at cur_next. We'll check |
| 9802 | * this header in the list as well as against the default |
| 9803 | * rule. |
| 9804 | */ |
| 9805 | |
| 9806 | /* look for ': *'. */ |
| 9807 | hn = cur_ptr; |
| 9808 | for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++); |
| 9809 | if (p >= cur_ptr+cur_hdr->len) |
| 9810 | continue; |
| 9811 | hnl = p - hn; |
| 9812 | p++; |
| 9813 | while (p < cur_ptr + cur_hdr->len && (*p == ' ' || *p == '\t')) |
| 9814 | p++; |
| 9815 | if (p >= cur_ptr + cur_hdr->len) |
| 9816 | continue; |
| 9817 | hv = p; |
| 9818 | hvl = cur_ptr + cur_hdr->len-p; |
| 9819 | |
| 9820 | /* encode the header name. */ |
| 9821 | ret = encode_varint(hnl, &buf, end); |
| 9822 | if (ret == -1) |
| 9823 | return 0; |
| 9824 | if (buf + hnl > end) |
| 9825 | return 0; |
| 9826 | memcpy(buf, hn, hnl); |
| 9827 | buf += hnl; |
| 9828 | |
| 9829 | /* encode and copy the value. */ |
| 9830 | ret = encode_varint(hvl, &buf, end); |
| 9831 | if (ret == -1) |
| 9832 | return 0; |
| 9833 | if (buf + hvl > end) |
| 9834 | return 0; |
| 9835 | memcpy(buf, hv, hvl); |
| 9836 | buf += hvl; |
| 9837 | } |
| 9838 | |
| 9839 | /* encode the end of the header list with empty |
| 9840 | * header name and header value. |
| 9841 | */ |
| 9842 | ret = encode_varint(0, &buf, end); |
| 9843 | if (ret == -1) |
| 9844 | return 0; |
| 9845 | ret = encode_varint(0, &buf, end); |
| 9846 | if (ret == -1) |
| 9847 | return 0; |
| 9848 | |
| 9849 | /* Initialise sample data which will be filled. */ |
| 9850 | smp->data.type = SMP_T_BIN; |
| 9851 | smp->data.u.str.str = temp->str; |
| 9852 | smp->data.u.str.len = buf - temp->str; |
| 9853 | smp->data.u.str.size = temp->size; |
| 9854 | |
| 9855 | return 1; |
| 9856 | } |
| 9857 | |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9858 | /* returns the longest available part of the body. This requires that the body |
| 9859 | * has been waited for using http-buffer-request. |
| 9860 | */ |
| 9861 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9862 | 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] | 9863 | { |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9864 | struct http_msg *msg; |
| 9865 | unsigned long len; |
| 9866 | unsigned long block1; |
| 9867 | char *body; |
| 9868 | struct chunk *temp; |
| 9869 | |
| 9870 | CHECK_HTTP_MESSAGE_FIRST(); |
| 9871 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9872 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 9873 | msg = &smp->strm->txn->req; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9874 | else |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 9875 | msg = &smp->strm->txn->rsp; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9876 | |
| 9877 | len = http_body_bytes(msg); |
| 9878 | body = b_ptr(msg->chn->buf, -http_data_rewind(msg)); |
| 9879 | |
| 9880 | block1 = len; |
| 9881 | if (block1 > msg->chn->buf->data + msg->chn->buf->size - body) |
| 9882 | block1 = msg->chn->buf->data + msg->chn->buf->size - body; |
| 9883 | |
| 9884 | if (block1 == len) { |
| 9885 | /* buffer is not wrapped (or empty) */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9886 | smp->data.type = SMP_T_BIN; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9887 | smp->data.u.str.str = body; |
| 9888 | smp->data.u.str.len = len; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9889 | smp->flags = SMP_F_VOL_TEST | SMP_F_CONST; |
| 9890 | } |
| 9891 | else { |
| 9892 | /* buffer is wrapped, we need to defragment it */ |
| 9893 | temp = get_trash_chunk(); |
| 9894 | memcpy(temp->str, body, block1); |
| 9895 | memcpy(temp->str + block1, msg->chn->buf->data, len - block1); |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9896 | smp->data.type = SMP_T_BIN; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9897 | smp->data.u.str.str = temp->str; |
| 9898 | smp->data.u.str.len = len; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9899 | smp->flags = SMP_F_VOL_TEST; |
| 9900 | } |
| 9901 | return 1; |
| 9902 | } |
| 9903 | |
| 9904 | |
| 9905 | /* returns the available length of the body. This requires that the body |
| 9906 | * has been waited for using http-buffer-request. |
| 9907 | */ |
| 9908 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9909 | 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] | 9910 | { |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9911 | struct http_msg *msg; |
| 9912 | |
| 9913 | CHECK_HTTP_MESSAGE_FIRST(); |
| 9914 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9915 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 9916 | msg = &smp->strm->txn->req; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9917 | else |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 9918 | msg = &smp->strm->txn->rsp; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9919 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9920 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9921 | smp->data.u.sint = http_body_bytes(msg); |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9922 | |
| 9923 | smp->flags = SMP_F_VOL_TEST; |
| 9924 | return 1; |
| 9925 | } |
| 9926 | |
| 9927 | |
| 9928 | /* returns the advertised length of the body, or the advertised size of the |
| 9929 | * chunks available in the buffer. This requires that the body has been waited |
| 9930 | * for using http-buffer-request. |
| 9931 | */ |
| 9932 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9933 | 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] | 9934 | { |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9935 | struct http_msg *msg; |
| 9936 | |
| 9937 | CHECK_HTTP_MESSAGE_FIRST(); |
| 9938 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9939 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 9940 | msg = &smp->strm->txn->req; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9941 | else |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 9942 | msg = &smp->strm->txn->rsp; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9943 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9944 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9945 | smp->data.u.sint = msg->body_len; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9946 | |
| 9947 | smp->flags = SMP_F_VOL_TEST; |
| 9948 | return 1; |
| 9949 | } |
| 9950 | |
| 9951 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9952 | /* 4. Check on URL/URI. A pointer to the URI is stored. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 9953 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9954 | 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] | 9955 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 9956 | struct http_txn *txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9957 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9958 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9959 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 9960 | txn = smp->strm->txn; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9961 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9962 | smp->data.u.str.len = txn->req.sl.rq.u_l; |
| 9963 | 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] | 9964 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9965 | return 1; |
| 9966 | } |
| 9967 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9968 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9969 | 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] | 9970 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 9971 | struct http_txn *txn; |
Willy Tarreau | 4c804ec | 2013-09-30 14:37:14 +0200 | [diff] [blame] | 9972 | struct sockaddr_storage addr; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9973 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9974 | CHECK_HTTP_MESSAGE_FIRST(); |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9975 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 9976 | txn = smp->strm->txn; |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 9977 | 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] | 9978 | if (((struct sockaddr_in *)&addr)->sin_family != AF_INET) |
Willy Tarreau | f4362b3 | 2011-12-16 17:49:52 +0100 | [diff] [blame] | 9979 | return 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9980 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9981 | smp->data.type = SMP_T_IPV4; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9982 | smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9983 | smp->flags = 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9984 | return 1; |
| 9985 | } |
| 9986 | |
| 9987 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9988 | 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] | 9989 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 9990 | struct http_txn *txn; |
Willy Tarreau | 4c804ec | 2013-09-30 14:37:14 +0200 | [diff] [blame] | 9991 | struct sockaddr_storage addr; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9992 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9993 | CHECK_HTTP_MESSAGE_FIRST(); |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9994 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 9995 | txn = smp->strm->txn; |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 9996 | 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] | 9997 | if (((struct sockaddr_in *)&addr)->sin_family != AF_INET) |
| 9998 | return 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9999 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10000 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10001 | smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port); |
Willy Tarreau | 21e5b0e | 2012-04-23 19:25:44 +0200 | [diff] [blame] | 10002 | smp->flags = 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10003 | return 1; |
| 10004 | } |
| 10005 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10006 | /* Fetch an HTTP header. A pointer to the beginning of the value is returned. |
| 10007 | * Accepts an optional argument of type string containing the header field name, |
| 10008 | * and an optional argument of type signed or unsigned integer to request an |
| 10009 | * 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] | 10010 | * headers are considered from the first one. It does not stop on commas and |
| 10011 | * returns full lines instead (useful for User-Agent or Date for example). |
| 10012 | */ |
| 10013 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10014 | 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] | 10015 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10016 | struct hdr_idx *idx; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10017 | struct hdr_ctx *ctx = smp->ctx.a[0]; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10018 | const struct http_msg *msg; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10019 | int occ = 0; |
| 10020 | const char *name_str = NULL; |
| 10021 | int name_len = 0; |
| 10022 | |
| 10023 | if (!ctx) { |
| 10024 | /* first call */ |
| 10025 | ctx = &static_hdr_ctx; |
| 10026 | ctx->idx = 0; |
| 10027 | smp->ctx.a[0] = ctx; |
| 10028 | } |
| 10029 | |
| 10030 | if (args) { |
| 10031 | if (args[0].type != ARGT_STR) |
| 10032 | return 0; |
| 10033 | name_str = args[0].data.str.str; |
| 10034 | name_len = args[0].data.str.len; |
| 10035 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 10036 | if (args[1].type == ARGT_SINT) |
| 10037 | occ = args[1].data.sint; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10038 | } |
| 10039 | |
| 10040 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10041 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10042 | idx = &smp->strm->txn->hdr_idx; |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10043 | 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] | 10044 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10045 | if (ctx && !(smp->flags & SMP_F_NOT_LAST)) |
| 10046 | /* search for header from the beginning */ |
| 10047 | ctx->idx = 0; |
| 10048 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10049 | if (!occ && !(smp->opt & SMP_OPT_ITERATE)) |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10050 | /* no explicit occurrence and single fetch => last header by default */ |
| 10051 | occ = -1; |
| 10052 | |
| 10053 | if (!occ) |
| 10054 | /* prepare to report multiple occurrences for ACL fetches */ |
| 10055 | smp->flags |= SMP_F_NOT_LAST; |
| 10056 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10057 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10058 | smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10059 | 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] | 10060 | return 1; |
| 10061 | |
| 10062 | smp->flags &= ~SMP_F_NOT_LAST; |
| 10063 | return 0; |
| 10064 | } |
| 10065 | |
| 10066 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
| 10067 | * Accepts exactly 1 argument of type string. It does not stop on commas and |
| 10068 | * returns full lines instead (useful for User-Agent or Date for example). |
| 10069 | */ |
| 10070 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10071 | 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] | 10072 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10073 | struct hdr_idx *idx; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10074 | struct hdr_ctx ctx; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10075 | const struct http_msg *msg; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10076 | int cnt; |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10077 | const char *name = NULL; |
| 10078 | int len = 0; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10079 | |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10080 | if (args && args->type == ARGT_STR) { |
| 10081 | name = args->data.str.str; |
| 10082 | len = args->data.str.len; |
| 10083 | } |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10084 | |
| 10085 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10086 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10087 | idx = &smp->strm->txn->hdr_idx; |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10088 | 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] | 10089 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10090 | ctx.idx = 0; |
| 10091 | cnt = 0; |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10092 | 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] | 10093 | cnt++; |
| 10094 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10095 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10096 | smp->data.u.sint = cnt; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10097 | smp->flags = SMP_F_VOL_HDR; |
| 10098 | return 1; |
| 10099 | } |
| 10100 | |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 10101 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10102 | 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] | 10103 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10104 | struct hdr_idx *idx; |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 10105 | struct hdr_ctx ctx; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10106 | const struct http_msg *msg; |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 10107 | struct chunk *temp; |
| 10108 | char del = ','; |
| 10109 | |
| 10110 | if (args && args->type == ARGT_STR) |
| 10111 | del = *args[0].data.str.str; |
| 10112 | |
| 10113 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10114 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10115 | idx = &smp->strm->txn->hdr_idx; |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10116 | 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] | 10117 | |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 10118 | temp = get_trash_chunk(); |
| 10119 | |
| 10120 | ctx.idx = 0; |
| 10121 | while (http_find_next_header(msg->chn->buf->p, idx, &ctx)) { |
| 10122 | if (temp->len) |
| 10123 | temp->str[temp->len++] = del; |
| 10124 | memcpy(temp->str + temp->len, ctx.line, ctx.del); |
| 10125 | temp->len += ctx.del; |
| 10126 | } |
| 10127 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10128 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10129 | smp->data.u.str.str = temp->str; |
| 10130 | smp->data.u.str.len = temp->len; |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 10131 | smp->flags = SMP_F_VOL_HDR; |
| 10132 | return 1; |
| 10133 | } |
| 10134 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10135 | /* Fetch an HTTP header. A pointer to the beginning of the value is returned. |
| 10136 | * Accepts an optional argument of type string containing the header field name, |
| 10137 | * and an optional argument of type signed or unsigned integer to request an |
| 10138 | * 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] | 10139 | * headers are considered from the first one. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10140 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10141 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10142 | 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] | 10143 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10144 | struct hdr_idx *idx; |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 10145 | struct hdr_ctx *ctx = smp->ctx.a[0]; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10146 | const struct http_msg *msg; |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10147 | int occ = 0; |
| 10148 | const char *name_str = NULL; |
| 10149 | int name_len = 0; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10150 | |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 10151 | if (!ctx) { |
| 10152 | /* first call */ |
| 10153 | ctx = &static_hdr_ctx; |
| 10154 | ctx->idx = 0; |
Willy Tarreau | ffb6f08 | 2013-04-02 23:16:53 +0200 | [diff] [blame] | 10155 | smp->ctx.a[0] = ctx; |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 10156 | } |
| 10157 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10158 | if (args) { |
| 10159 | if (args[0].type != ARGT_STR) |
| 10160 | return 0; |
| 10161 | name_str = args[0].data.str.str; |
| 10162 | name_len = args[0].data.str.len; |
| 10163 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 10164 | if (args[1].type == ARGT_SINT) |
| 10165 | occ = args[1].data.sint; |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10166 | } |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10167 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10168 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10169 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10170 | idx = &smp->strm->txn->hdr_idx; |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10171 | 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] | 10172 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10173 | if (ctx && !(smp->flags & SMP_F_NOT_LAST)) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10174 | /* search for header from the beginning */ |
| 10175 | ctx->idx = 0; |
| 10176 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10177 | if (!occ && !(smp->opt & SMP_OPT_ITERATE)) |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10178 | /* no explicit occurrence and single fetch => last header by default */ |
| 10179 | occ = -1; |
| 10180 | |
| 10181 | if (!occ) |
| 10182 | /* prepare to report multiple occurrences for ACL fetches */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10183 | smp->flags |= SMP_F_NOT_LAST; |
Willy Tarreau | 664092c | 2011-12-16 19:11:42 +0100 | [diff] [blame] | 10184 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10185 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10186 | smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10187 | 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] | 10188 | return 1; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10189 | |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10190 | smp->flags &= ~SMP_F_NOT_LAST; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10191 | return 0; |
| 10192 | } |
| 10193 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10194 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10195 | * Accepts exactly 1 argument of type string. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10196 | */ |
| 10197 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10198 | 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] | 10199 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10200 | struct hdr_idx *idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10201 | struct hdr_ctx ctx; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10202 | const struct http_msg *msg; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10203 | int cnt; |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10204 | const char *name = NULL; |
| 10205 | int len = 0; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10206 | |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10207 | if (args && args->type == ARGT_STR) { |
| 10208 | name = args->data.str.str; |
| 10209 | len = args->data.str.len; |
| 10210 | } |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10211 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10212 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10213 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10214 | idx = &smp->strm->txn->hdr_idx; |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10215 | msg = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &smp->strm->txn->req : &smp->strm->txn->rsp; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10216 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10217 | ctx.idx = 0; |
| 10218 | cnt = 0; |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10219 | while (http_find_header2(name, len, msg->chn->buf->p, idx, &ctx)) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10220 | cnt++; |
| 10221 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10222 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10223 | smp->data.u.sint = cnt; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10224 | smp->flags = SMP_F_VOL_HDR; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10225 | return 1; |
| 10226 | } |
| 10227 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10228 | /* Fetch an HTTP header's integer value. The integer value is returned. It |
| 10229 | * takes a mandatory argument of type string and an optional one of type int |
| 10230 | * to designate a specific occurrence. It returns an unsigned integer, which |
| 10231 | * may or may not be appropriate for everything. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10232 | */ |
| 10233 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10234 | 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] | 10235 | { |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10236 | int ret = smp_fetch_hdr(args, smp, kw, private); |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10237 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10238 | if (ret > 0) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10239 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10240 | 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] | 10241 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10242 | |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10243 | return ret; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10244 | } |
| 10245 | |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 10246 | /* Fetch an HTTP header's IP value. takes a mandatory argument of type string |
| 10247 | * and an optional one of type int to designate a specific occurrence. |
| 10248 | * It returns an IPv4 or IPv6 address. |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 10249 | */ |
| 10250 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10251 | 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] | 10252 | { |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10253 | int ret; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10254 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10255 | while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10256 | if (url2ipv4((char *)smp->data.u.str.str, &smp->data.u.ipv4)) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10257 | smp->data.type = SMP_T_IPV4; |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10258 | break; |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 10259 | } else { |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 10260 | struct chunk *temp = get_trash_chunk(); |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10261 | if (smp->data.u.str.len < temp->size - 1) { |
| 10262 | memcpy(temp->str, smp->data.u.str.str, smp->data.u.str.len); |
| 10263 | temp->str[smp->data.u.str.len] = '\0'; |
| 10264 | if (inet_pton(AF_INET6, temp->str, &smp->data.u.ipv6)) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10265 | smp->data.type = SMP_T_IPV6; |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 10266 | break; |
| 10267 | } |
| 10268 | } |
| 10269 | } |
| 10270 | |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10271 | /* if the header doesn't match an IP address, fetch next one */ |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10272 | if (!(smp->flags & SMP_F_NOT_LAST)) |
| 10273 | return 0; |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 10274 | } |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10275 | return ret; |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 10276 | } |
| 10277 | |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10278 | /* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at |
| 10279 | * the first '/' after the possible hostname, and ends before the possible '?'. |
| 10280 | */ |
| 10281 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10282 | 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] | 10283 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10284 | struct http_txn *txn; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10285 | char *ptr, *end; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10286 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10287 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10288 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10289 | txn = smp->strm->txn; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10290 | 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] | 10291 | ptr = http_get_path(txn); |
| 10292 | if (!ptr) |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10293 | return 0; |
| 10294 | |
| 10295 | /* OK, we got the '/' ! */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10296 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10297 | smp->data.u.str.str = ptr; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10298 | |
| 10299 | while (ptr < end && *ptr != '?') |
| 10300 | ptr++; |
| 10301 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10302 | smp->data.u.str.len = ptr - smp->data.u.str.str; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10303 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10304 | return 1; |
| 10305 | } |
| 10306 | |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10307 | /* This produces a concatenation of the first occurrence of the Host header |
| 10308 | * followed by the path component if it begins with a slash ('/'). This means |
| 10309 | * that '*' will not be added, resulting in exactly the first Host entry. |
| 10310 | * If no Host header is found, then the path is returned as-is. The returned |
| 10311 | * 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] | 10312 | * The returned sample is of type string. |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10313 | */ |
| 10314 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10315 | 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] | 10316 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10317 | struct http_txn *txn; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10318 | char *ptr, *end, *beg; |
| 10319 | struct hdr_ctx ctx; |
Willy Tarreau | 3caf2af | 2014-06-24 17:27:02 +0200 | [diff] [blame] | 10320 | struct chunk *temp; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10321 | |
| 10322 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10323 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10324 | txn = smp->strm->txn; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10325 | ctx.idx = 0; |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 10326 | 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] | 10327 | return smp_fetch_path(args, smp, kw, private); |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10328 | |
| 10329 | /* 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] | 10330 | temp = get_trash_chunk(); |
| 10331 | memcpy(temp->str, ctx.line + ctx.val, ctx.vlen); |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10332 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10333 | smp->data.u.str.str = temp->str; |
| 10334 | smp->data.u.str.len = ctx.vlen; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10335 | |
| 10336 | /* now retrieve the path */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 10337 | 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] | 10338 | beg = http_get_path(txn); |
| 10339 | if (!beg) |
| 10340 | beg = end; |
| 10341 | |
| 10342 | for (ptr = beg; ptr < end && *ptr != '?'; ptr++); |
| 10343 | |
| 10344 | if (beg < ptr && *beg == '/') { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10345 | memcpy(smp->data.u.str.str + smp->data.u.str.len, beg, ptr - beg); |
| 10346 | smp->data.u.str.len += ptr - beg; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10347 | } |
| 10348 | |
| 10349 | smp->flags = SMP_F_VOL_1ST; |
| 10350 | return 1; |
| 10351 | } |
| 10352 | |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 10353 | /* This produces a 32-bit hash of the concatenation of the first occurrence of |
| 10354 | * the Host header followed by the path component if it begins with a slash ('/'). |
| 10355 | * This means that '*' will not be added, resulting in exactly the first Host |
| 10356 | * 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] | 10357 | * is hashed using the path hash followed by a full avalanche hash and provides a |
| 10358 | * 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] | 10359 | * high-traffic sites without having to store whole paths. |
| 10360 | */ |
Thierry FOURNIER | 055b9d5 | 2014-07-15 16:11:07 +0200 | [diff] [blame] | 10361 | int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10362 | 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] | 10363 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10364 | struct http_txn *txn; |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 10365 | struct hdr_ctx ctx; |
| 10366 | unsigned int hash = 0; |
| 10367 | char *ptr, *beg, *end; |
| 10368 | int len; |
| 10369 | |
| 10370 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10371 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10372 | txn = smp->strm->txn; |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 10373 | ctx.idx = 0; |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 10374 | 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] | 10375 | /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */ |
| 10376 | ptr = ctx.line + ctx.val; |
| 10377 | len = ctx.vlen; |
| 10378 | while (len--) |
| 10379 | hash = *(ptr++) + (hash << 6) + (hash << 16) - hash; |
| 10380 | } |
| 10381 | |
| 10382 | /* now retrieve the path */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 10383 | 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] | 10384 | beg = http_get_path(txn); |
| 10385 | if (!beg) |
| 10386 | beg = end; |
| 10387 | |
| 10388 | for (ptr = beg; ptr < end && *ptr != '?'; ptr++); |
| 10389 | |
| 10390 | if (beg < ptr && *beg == '/') { |
| 10391 | while (beg < ptr) |
| 10392 | hash = *(beg++) + (hash << 6) + (hash << 16) - hash; |
| 10393 | } |
| 10394 | hash = full_hash(hash); |
| 10395 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10396 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10397 | smp->data.u.sint = hash; |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 10398 | smp->flags = SMP_F_VOL_1ST; |
| 10399 | return 1; |
| 10400 | } |
| 10401 | |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10402 | /* 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] | 10403 | * path as returned by smp_fetch_base32(). The idea is to have per-source and |
| 10404 | * per-path counters. The result is a binary block from 8 to 20 bytes depending |
| 10405 | * 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] | 10406 | * that in environments where IPv6 is insignificant, truncating the output to |
| 10407 | * 8 bytes would still work. |
| 10408 | */ |
| 10409 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10410 | 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] | 10411 | { |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 10412 | struct chunk *temp; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10413 | struct connection *cli_conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10414 | |
| 10415 | if (!cli_conn) |
| 10416 | return 0; |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10417 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10418 | if (!smp_fetch_base32(args, smp, kw, private)) |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10419 | return 0; |
| 10420 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 10421 | temp = get_trash_chunk(); |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10422 | *(unsigned int *)temp->str = htonl(smp->data.u.sint); |
Willy Tarreau | 5ad6e1d | 2014-07-15 21:34:06 +0200 | [diff] [blame] | 10423 | temp->len += sizeof(unsigned int); |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10424 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10425 | switch (cli_conn->addr.from.ss_family) { |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10426 | case AF_INET: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10427 | 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] | 10428 | temp->len += 4; |
| 10429 | break; |
| 10430 | case AF_INET6: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10431 | 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] | 10432 | temp->len += 16; |
| 10433 | break; |
| 10434 | default: |
| 10435 | return 0; |
| 10436 | } |
| 10437 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10438 | smp->data.u.str = *temp; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10439 | smp->data.type = SMP_T_BIN; |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10440 | return 1; |
| 10441 | } |
| 10442 | |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 10443 | /* Extracts the query string, which comes after the question mark '?'. If no |
| 10444 | * question mark is found, nothing is returned. Otherwise it returns a sample |
| 10445 | * of type string carrying the whole query string. |
| 10446 | */ |
| 10447 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10448 | 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] | 10449 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10450 | struct http_txn *txn; |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 10451 | char *ptr, *end; |
| 10452 | |
| 10453 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10454 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10455 | txn = smp->strm->txn; |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 10456 | ptr = txn->req.chn->buf->p + txn->req.sl.rq.u; |
| 10457 | end = ptr + txn->req.sl.rq.u_l; |
| 10458 | |
| 10459 | /* look up the '?' */ |
| 10460 | do { |
| 10461 | if (ptr == end) |
| 10462 | return 0; |
| 10463 | } while (*ptr++ != '?'); |
| 10464 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10465 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10466 | smp->data.u.str.str = ptr; |
| 10467 | smp->data.u.str.len = end - ptr; |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 10468 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
| 10469 | return 1; |
| 10470 | } |
| 10471 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10472 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10473 | 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] | 10474 | { |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10475 | /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged |
| 10476 | * as a layer7 ACL, which involves automatic allocation of hdr_idx. |
| 10477 | */ |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10478 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10479 | CHECK_HTTP_MESSAGE_FIRST_PERM(); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10480 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10481 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10482 | smp->data.u.sint = 1; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10483 | return 1; |
| 10484 | } |
| 10485 | |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 10486 | /* return a valid test if the current request is the first one on the connection */ |
| 10487 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10488 | 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] | 10489 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10490 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10491 | smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST); |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 10492 | return 1; |
| 10493 | } |
| 10494 | |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10495 | /* Accepts exactly 1 argument of type userlist */ |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 10496 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10497 | 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] | 10498 | { |
| 10499 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10500 | if (!args || args->type != ARGT_USR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10501 | return 0; |
| 10502 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10503 | CHECK_HTTP_MESSAGE_FIRST(); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 10504 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10505 | if (!get_http_auth(smp->strm)) |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 10506 | return 0; |
| 10507 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10508 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10509 | 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] | 10510 | smp->strm->txn->auth.pass); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 10511 | return 1; |
| 10512 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10513 | |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10514 | /* Accepts exactly 1 argument of type userlist */ |
| 10515 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10516 | 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] | 10517 | { |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10518 | if (!args || args->type != ARGT_USR) |
| 10519 | return 0; |
| 10520 | |
| 10521 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10522 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10523 | if (!get_http_auth(smp->strm)) |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10524 | return 0; |
| 10525 | |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10526 | /* if the user does not belong to the userlist or has a wrong password, |
| 10527 | * report that it unconditionally does not match. Otherwise we return |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 10528 | * a string containing the username. |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10529 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10530 | if (!check_user(args->data.usr, smp->strm->txn->auth.user, |
| 10531 | smp->strm->txn->auth.pass)) |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 10532 | return 0; |
| 10533 | |
| 10534 | /* pat_match_auth() will need the user list */ |
| 10535 | smp->ctx.a[0] = args->data.usr; |
| 10536 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10537 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10538 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10539 | smp->data.u.str.str = smp->strm->txn->auth.user; |
| 10540 | smp->data.u.str.len = strlen(smp->strm->txn->auth.user); |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10541 | |
| 10542 | return 1; |
| 10543 | } |
| 10544 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10545 | /* Try to find the next occurrence of a cookie name in a cookie header value. |
| 10546 | * The lookup begins at <hdr>. The pointer and size of the next occurrence of |
| 10547 | * the cookie value is returned into *value and *value_l, and the function |
| 10548 | * returns a pointer to the next pointer to search from if the value was found. |
| 10549 | * Otherwise if the cookie was not found, NULL is returned and neither value |
| 10550 | * nor value_l are touched. The input <hdr> string should first point to the |
| 10551 | * header's value, and the <hdr_end> pointer must point to the first character |
| 10552 | * not part of the value. <list> must be non-zero if value may represent a list |
| 10553 | * of values (cookie headers). This makes it faster to abort parsing when no |
| 10554 | * list is expected. |
| 10555 | */ |
David Carlier | 4686f79 | 2015-09-25 14:10:50 +0100 | [diff] [blame] | 10556 | char * |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10557 | extract_cookie_value(char *hdr, const char *hdr_end, |
| 10558 | char *cookie_name, size_t cookie_name_l, int list, |
Willy Tarreau | 3fb818c | 2012-04-11 17:21:08 +0200 | [diff] [blame] | 10559 | char **value, int *value_l) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10560 | { |
| 10561 | char *equal, *att_end, *att_beg, *val_beg, *val_end; |
| 10562 | char *next; |
| 10563 | |
| 10564 | /* we search at least a cookie name followed by an equal, and more |
| 10565 | * generally something like this : |
| 10566 | * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n |
| 10567 | */ |
| 10568 | for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) { |
| 10569 | /* Iterate through all cookies on this line */ |
| 10570 | |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 10571 | while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg)) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10572 | att_beg++; |
| 10573 | |
| 10574 | /* find att_end : this is the first character after the last non |
| 10575 | * space before the equal. It may be equal to hdr_end. |
| 10576 | */ |
| 10577 | equal = att_end = att_beg; |
| 10578 | |
| 10579 | while (equal < hdr_end) { |
| 10580 | if (*equal == '=' || *equal == ';' || (list && *equal == ',')) |
| 10581 | break; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 10582 | if (HTTP_IS_SPHT(*equal++)) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10583 | continue; |
| 10584 | att_end = equal; |
| 10585 | } |
| 10586 | |
| 10587 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 10588 | * is between <att_beg> and <equal>, both may be identical. |
| 10589 | */ |
| 10590 | |
| 10591 | /* look for end of cookie if there is an equal sign */ |
| 10592 | if (equal < hdr_end && *equal == '=') { |
| 10593 | /* look for the beginning of the value */ |
| 10594 | val_beg = equal + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 10595 | while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg)) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10596 | val_beg++; |
| 10597 | |
| 10598 | /* find the end of the value, respecting quotes */ |
| 10599 | next = find_cookie_value_end(val_beg, hdr_end); |
| 10600 | |
| 10601 | /* make val_end point to the first white space or delimitor after the value */ |
| 10602 | val_end = next; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 10603 | while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1))) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10604 | val_end--; |
| 10605 | } else { |
| 10606 | val_beg = val_end = next = equal; |
| 10607 | } |
| 10608 | |
| 10609 | /* We have nothing to do with attributes beginning with '$'. However, |
| 10610 | * they will automatically be removed if a header before them is removed, |
| 10611 | * since they're supposed to be linked together. |
| 10612 | */ |
| 10613 | if (*att_beg == '$') |
| 10614 | continue; |
| 10615 | |
| 10616 | /* Ignore cookies with no equal sign */ |
| 10617 | if (equal == next) |
| 10618 | continue; |
| 10619 | |
| 10620 | /* Now we have the cookie name between att_beg and att_end, and |
| 10621 | * its value between val_beg and val_end. |
| 10622 | */ |
| 10623 | |
| 10624 | if (att_end - att_beg == cookie_name_l && |
| 10625 | memcmp(att_beg, cookie_name, cookie_name_l) == 0) { |
| 10626 | /* let's return this value and indicate where to go on from */ |
| 10627 | *value = val_beg; |
| 10628 | *value_l = val_end - val_beg; |
| 10629 | return next + 1; |
| 10630 | } |
| 10631 | |
| 10632 | /* Set-Cookie headers only have the name in the first attr=value part */ |
| 10633 | if (!list) |
| 10634 | break; |
| 10635 | } |
| 10636 | |
| 10637 | return NULL; |
| 10638 | } |
| 10639 | |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10640 | /* Fetch a captured HTTP request header. The index is the position of |
| 10641 | * the "capture" option in the configuration file |
| 10642 | */ |
| 10643 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10644 | 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] | 10645 | { |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10646 | struct proxy *fe = strm_fe(smp->strm); |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10647 | int idx; |
| 10648 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 10649 | if (!args || args->type != ARGT_SINT) |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10650 | return 0; |
| 10651 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 10652 | idx = args->data.sint; |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10653 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10654 | 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] | 10655 | return 0; |
| 10656 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10657 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10658 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10659 | smp->data.u.str.str = smp->strm->req_cap[idx]; |
| 10660 | smp->data.u.str.len = strlen(smp->strm->req_cap[idx]); |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10661 | |
| 10662 | return 1; |
| 10663 | } |
| 10664 | |
| 10665 | /* Fetch a captured HTTP response header. The index is the position of |
| 10666 | * the "capture" option in the configuration file |
| 10667 | */ |
| 10668 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10669 | 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] | 10670 | { |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10671 | struct proxy *fe = strm_fe(smp->strm); |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10672 | int idx; |
| 10673 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 10674 | if (!args || args->type != ARGT_SINT) |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10675 | return 0; |
| 10676 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 10677 | idx = args->data.sint; |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10678 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10679 | 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] | 10680 | return 0; |
| 10681 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10682 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10683 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10684 | smp->data.u.str.str = smp->strm->res_cap[idx]; |
| 10685 | smp->data.u.str.len = strlen(smp->strm->res_cap[idx]); |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10686 | |
| 10687 | return 1; |
| 10688 | } |
| 10689 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10690 | /* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */ |
| 10691 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10692 | 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] | 10693 | { |
| 10694 | struct chunk *temp; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10695 | struct http_txn *txn = smp->strm->txn; |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10696 | char *ptr; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10697 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10698 | if (!txn || !txn->uri) |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10699 | return 0; |
| 10700 | |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10701 | ptr = txn->uri; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10702 | |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10703 | while (*ptr != ' ' && *ptr != '\0') /* find first space */ |
| 10704 | ptr++; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10705 | |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10706 | temp = get_trash_chunk(); |
| 10707 | temp->str = txn->uri; |
| 10708 | temp->len = ptr - txn->uri; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10709 | smp->data.u.str = *temp; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10710 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10711 | smp->flags = SMP_F_CONST; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10712 | |
| 10713 | return 1; |
| 10714 | |
| 10715 | } |
| 10716 | |
| 10717 | /* Extracts the path in the HTTP request, the txn->uri should be filled before the call */ |
| 10718 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10719 | 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] | 10720 | { |
| 10721 | struct chunk *temp; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10722 | struct http_txn *txn = smp->strm->txn; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10723 | char *ptr; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10724 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10725 | if (!txn || !txn->uri) |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10726 | return 0; |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10727 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10728 | ptr = txn->uri; |
| 10729 | |
| 10730 | while (*ptr != ' ' && *ptr != '\0') /* find first space */ |
| 10731 | ptr++; |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10732 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10733 | if (!*ptr) |
| 10734 | return 0; |
| 10735 | |
| 10736 | ptr++; /* skip the space */ |
| 10737 | |
| 10738 | temp = get_trash_chunk(); |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10739 | ptr = temp->str = http_get_path_from_string(ptr); |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10740 | if (!ptr) |
| 10741 | return 0; |
| 10742 | while (*ptr != ' ' && *ptr != '\0') /* find space after URI */ |
| 10743 | ptr++; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10744 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10745 | smp->data.u.str = *temp; |
| 10746 | smp->data.u.str.len = ptr - temp->str; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10747 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10748 | smp->flags = SMP_F_CONST; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10749 | |
| 10750 | return 1; |
| 10751 | } |
| 10752 | |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 10753 | /* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it |
| 10754 | * as a string (either "HTTP/1.0" or "HTTP/1.1"). |
| 10755 | */ |
| 10756 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10757 | 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] | 10758 | { |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10759 | struct http_txn *txn = smp->strm->txn; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 10760 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10761 | if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST) |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 10762 | return 0; |
| 10763 | |
| 10764 | if (txn->req.flags & HTTP_MSGF_VER_11) |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10765 | smp->data.u.str.str = "HTTP/1.1"; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 10766 | else |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10767 | smp->data.u.str.str = "HTTP/1.0"; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 10768 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10769 | smp->data.u.str.len = 8; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10770 | smp->data.type = SMP_T_STR; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 10771 | smp->flags = SMP_F_CONST; |
| 10772 | return 1; |
| 10773 | |
| 10774 | } |
| 10775 | |
| 10776 | /* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it |
| 10777 | * as a string (either "HTTP/1.0" or "HTTP/1.1"). |
| 10778 | */ |
| 10779 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10780 | 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] | 10781 | { |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10782 | struct http_txn *txn = smp->strm->txn; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 10783 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10784 | if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST) |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 10785 | return 0; |
| 10786 | |
| 10787 | if (txn->rsp.flags & HTTP_MSGF_VER_11) |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10788 | smp->data.u.str.str = "HTTP/1.1"; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 10789 | else |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10790 | smp->data.u.str.str = "HTTP/1.0"; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 10791 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10792 | smp->data.u.str.len = 8; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10793 | smp->data.type = SMP_T_STR; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 10794 | smp->flags = SMP_F_CONST; |
| 10795 | return 1; |
| 10796 | |
| 10797 | } |
| 10798 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10799 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10800 | /* 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] | 10801 | * 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] | 10802 | * 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] | 10803 | * 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] | 10804 | * 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] | 10805 | * Accepts exactly 1 argument of type string. If the input options indicate |
| 10806 | * 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] | 10807 | * The returned sample is of type CSTR. Can be used to parse cookies in other |
| 10808 | * files. |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10809 | */ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10810 | 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] | 10811 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10812 | struct http_txn *txn; |
| 10813 | struct hdr_idx *idx; |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 10814 | struct hdr_ctx *ctx = smp->ctx.a[2]; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10815 | const struct http_msg *msg; |
| 10816 | const char *hdr_name; |
| 10817 | int hdr_name_len; |
| 10818 | char *sol; |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10819 | int occ = 0; |
| 10820 | int found = 0; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10821 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10822 | if (!args || args->type != ARGT_STR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10823 | return 0; |
| 10824 | |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 10825 | if (!ctx) { |
| 10826 | /* first call */ |
| 10827 | ctx = &static_hdr_ctx; |
| 10828 | ctx->idx = 0; |
| 10829 | smp->ctx.a[2] = ctx; |
| 10830 | } |
| 10831 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10832 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10833 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10834 | txn = smp->strm->txn; |
| 10835 | idx = &smp->strm->txn->hdr_idx; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10836 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10837 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10838 | msg = &txn->req; |
| 10839 | hdr_name = "Cookie"; |
| 10840 | hdr_name_len = 6; |
| 10841 | } else { |
| 10842 | msg = &txn->rsp; |
| 10843 | hdr_name = "Set-Cookie"; |
| 10844 | hdr_name_len = 10; |
| 10845 | } |
| 10846 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10847 | if (!occ && !(smp->opt & SMP_OPT_ITERATE)) |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10848 | /* no explicit occurrence and single fetch => last cookie by default */ |
| 10849 | occ = -1; |
| 10850 | |
| 10851 | /* OK so basically here, either we want only one value and it's the |
| 10852 | * last one, or we want to iterate over all of them and we fetch the |
| 10853 | * next one. |
| 10854 | */ |
| 10855 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10856 | sol = msg->chn->buf->p; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10857 | if (!(smp->flags & SMP_F_NOT_LAST)) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10858 | /* search for the header from the beginning, we must first initialize |
| 10859 | * the search parameters. |
| 10860 | */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10861 | smp->ctx.a[0] = NULL; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10862 | ctx->idx = 0; |
| 10863 | } |
| 10864 | |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10865 | smp->flags |= SMP_F_VOL_HDR; |
| 10866 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10867 | while (1) { |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10868 | /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */ |
| 10869 | if (!smp->ctx.a[0]) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10870 | if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx)) |
| 10871 | goto out; |
| 10872 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10873 | if (ctx->vlen < args->data.str.len + 1) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10874 | continue; |
| 10875 | |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10876 | smp->ctx.a[0] = ctx->line + ctx->val; |
| 10877 | smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10878 | } |
| 10879 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10880 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10881 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10882 | 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] | 10883 | args->data.str.str, args->data.str.len, |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10884 | (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ, |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10885 | &smp->data.u.str.str, |
| 10886 | &smp->data.u.str.len); |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10887 | if (smp->ctx.a[0]) { |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10888 | found = 1; |
| 10889 | if (occ >= 0) { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10890 | /* one value was returned into smp->data.u.str.{str,len} */ |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10891 | smp->flags |= SMP_F_NOT_LAST; |
| 10892 | return 1; |
| 10893 | } |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10894 | } |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10895 | /* if we're looking for last occurrence, let's loop */ |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10896 | } |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10897 | /* all cookie headers and values were scanned. If we're looking for the |
| 10898 | * last occurrence, we may return it now. |
| 10899 | */ |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10900 | out: |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10901 | smp->flags &= ~SMP_F_NOT_LAST; |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10902 | return found; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10903 | } |
| 10904 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10905 | /* 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] | 10906 | * 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] | 10907 | * multiple cookies may be parsed on the same line. The returned sample is of |
| 10908 | * type UINT. Accepts exactly 1 argument of type string. |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10909 | */ |
| 10910 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10911 | 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] | 10912 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10913 | struct http_txn *txn; |
| 10914 | struct hdr_idx *idx; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10915 | struct hdr_ctx ctx; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10916 | const struct http_msg *msg; |
| 10917 | const char *hdr_name; |
| 10918 | int hdr_name_len; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10919 | int cnt; |
| 10920 | char *val_beg, *val_end; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10921 | char *sol; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10922 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10923 | if (!args || args->type != ARGT_STR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10924 | return 0; |
| 10925 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10926 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10927 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10928 | txn = smp->strm->txn; |
| 10929 | idx = &smp->strm->txn->hdr_idx; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10930 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10931 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10932 | msg = &txn->req; |
| 10933 | hdr_name = "Cookie"; |
| 10934 | hdr_name_len = 6; |
| 10935 | } else { |
| 10936 | msg = &txn->rsp; |
| 10937 | hdr_name = "Set-Cookie"; |
| 10938 | hdr_name_len = 10; |
| 10939 | } |
| 10940 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10941 | sol = msg->chn->buf->p; |
Willy Tarreau | 46787ed | 2012-04-11 17:28:40 +0200 | [diff] [blame] | 10942 | val_end = val_beg = NULL; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10943 | ctx.idx = 0; |
| 10944 | cnt = 0; |
| 10945 | |
| 10946 | while (1) { |
| 10947 | /* Note: val_beg == NULL every time we need to fetch a new header */ |
| 10948 | if (!val_beg) { |
| 10949 | if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx)) |
| 10950 | break; |
| 10951 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10952 | if (ctx.vlen < args->data.str.len + 1) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10953 | continue; |
| 10954 | |
| 10955 | val_beg = ctx.line + ctx.val; |
| 10956 | val_end = val_beg + ctx.vlen; |
| 10957 | } |
| 10958 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10959 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10960 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10961 | while ((val_beg = extract_cookie_value(val_beg, val_end, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10962 | args->data.str.str, args->data.str.len, |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10963 | (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ, |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10964 | &smp->data.u.str.str, |
| 10965 | &smp->data.u.str.len))) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10966 | cnt++; |
| 10967 | } |
| 10968 | } |
| 10969 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10970 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10971 | smp->data.u.sint = cnt; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10972 | smp->flags |= SMP_F_VOL_HDR; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10973 | return 1; |
| 10974 | } |
| 10975 | |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 10976 | /* Fetch an cookie's integer value. The integer value is returned. It |
| 10977 | * takes a mandatory argument of type string. It relies on smp_fetch_cookie(). |
| 10978 | */ |
| 10979 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10980 | 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] | 10981 | { |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10982 | int ret = smp_fetch_cookie(args, smp, kw, private); |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 10983 | |
| 10984 | if (ret > 0) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10985 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10986 | 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] | 10987 | } |
| 10988 | |
| 10989 | return ret; |
| 10990 | } |
| 10991 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10992 | /************************************************************************/ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 10993 | /* The code below is dedicated to sample fetches */ |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 10994 | /************************************************************************/ |
| 10995 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10996 | /* |
| 10997 | * Given a path string and its length, find the position of beginning of the |
| 10998 | * query string. Returns NULL if no query string is found in the path. |
| 10999 | * |
| 11000 | * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22: |
| 11001 | * |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11002 | * find_query_string(path, n, '?') points to "yo=mama;ye=daddy" string. |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11003 | */ |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11004 | 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] | 11005 | { |
| 11006 | char *p; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 11007 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11008 | p = memchr(path, delim, path_l); |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11009 | return p ? p + 1 : NULL; |
| 11010 | } |
| 11011 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11012 | static inline int is_param_delimiter(char c, char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11013 | { |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11014 | return c == '&' || c == ';' || c == delim; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11015 | } |
| 11016 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11017 | /* after increasing a pointer value, it can exceed the first buffer |
| 11018 | * size. This function transform the value of <ptr> according with |
| 11019 | * the expected position. <chunks> is an array of the one or two |
| 11020 | * avalaible chunks. The first value is the start of the first chunk, |
| 11021 | * the second value if the end+1 of the first chunks. The third value |
| 11022 | * is NULL or the start of the second chunk and the fourth value is |
| 11023 | * the end+1 of the second chunk. The function returns 1 if does a |
| 11024 | * wrap, else returns 0. |
| 11025 | */ |
| 11026 | static inline int fix_pointer_if_wrap(const char **chunks, const char **ptr) |
| 11027 | { |
| 11028 | if (*ptr < chunks[1]) |
| 11029 | return 0; |
| 11030 | if (!chunks[2]) |
| 11031 | return 0; |
| 11032 | *ptr = chunks[2] + ( *ptr - chunks[1] ); |
| 11033 | return 1; |
| 11034 | } |
| 11035 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11036 | /* |
| 11037 | * Given a url parameter, find the starting position of the first occurence, |
| 11038 | * or NULL if the parameter is not found. |
| 11039 | * |
| 11040 | * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye", |
| 11041 | * the function will return query_string+8. |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11042 | * |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 11043 | * 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] | 11044 | * or the second chunk. The caller must be check the position before using the |
| 11045 | * result. |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11046 | */ |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11047 | static const char * |
| 11048 | find_url_param_pos(const char **chunks, |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11049 | const char* url_param_name, size_t url_param_name_l, |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11050 | char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11051 | { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11052 | const char *pos, *last, *equal; |
| 11053 | const char **bufs = chunks; |
| 11054 | int l1, l2; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11055 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11056 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11057 | pos = bufs[0]; |
| 11058 | last = bufs[1]; |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 11059 | while (pos < last) { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11060 | /* Check the equal. */ |
| 11061 | equal = pos + url_param_name_l; |
| 11062 | if (fix_pointer_if_wrap(chunks, &equal)) { |
| 11063 | if (equal >= chunks[3]) |
| 11064 | return NULL; |
| 11065 | } else { |
| 11066 | if (equal >= chunks[1]) |
| 11067 | return NULL; |
| 11068 | } |
| 11069 | if (*equal == '=') { |
| 11070 | if (pos + url_param_name_l > last) { |
| 11071 | /* process wrap case, we detect a wrap. In this case, the |
| 11072 | * comparison is performed in two parts. |
| 11073 | */ |
| 11074 | |
| 11075 | /* This is the end, we dont have any other chunk. */ |
| 11076 | if (bufs != chunks || !bufs[2]) |
| 11077 | return NULL; |
| 11078 | |
| 11079 | /* Compute the length of each part of the comparison. */ |
| 11080 | l1 = last - pos; |
| 11081 | l2 = url_param_name_l - l1; |
| 11082 | |
| 11083 | /* The second buffer is too short to contain the compared string. */ |
| 11084 | if (bufs[2] + l2 > bufs[3]) |
| 11085 | return NULL; |
| 11086 | |
| 11087 | if (memcmp(pos, url_param_name, l1) == 0 && |
| 11088 | memcmp(bufs[2], url_param_name+l1, l2) == 0) |
| 11089 | return pos; |
| 11090 | |
| 11091 | /* Perform wrapping and jump the string who fail the comparison. */ |
| 11092 | bufs += 2; |
| 11093 | pos = bufs[0] + l2; |
| 11094 | last = bufs[1]; |
| 11095 | |
| 11096 | } else { |
| 11097 | /* process a simple comparison. */ |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 11098 | if (memcmp(pos, url_param_name, url_param_name_l) == 0) |
| 11099 | return pos; |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11100 | pos += url_param_name_l + 1; |
| 11101 | if (fix_pointer_if_wrap(chunks, &pos)) |
| 11102 | last = bufs[2]; |
| 11103 | } |
| 11104 | } |
| 11105 | |
| 11106 | while (1) { |
| 11107 | /* Look for the next delimiter. */ |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 11108 | while (pos < last && !is_param_delimiter(*pos, delim)) |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11109 | pos++; |
| 11110 | if (pos < last) |
| 11111 | break; |
| 11112 | /* process buffer wrapping. */ |
| 11113 | if (bufs != chunks || !bufs[2]) |
| 11114 | return NULL; |
| 11115 | bufs += 2; |
| 11116 | pos = bufs[0]; |
| 11117 | last = bufs[1]; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11118 | } |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11119 | pos++; |
| 11120 | } |
| 11121 | return NULL; |
| 11122 | } |
| 11123 | |
| 11124 | /* |
Cyril Bonté | ce1ef4d | 2015-11-26 21:39:56 +0100 | [diff] [blame] | 11125 | * Given a url parameter name and a query string, find the next value. |
| 11126 | * An empty url_param_name matches the first available parameter. |
| 11127 | * If the parameter is found, 1 is returned and *vstart / *vend are updated to |
| 11128 | * respectively provide a pointer to the value and its end. |
| 11129 | * Otherwise, 0 is returned and vstart/vend are not modified. |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11130 | */ |
| 11131 | static int |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11132 | find_next_url_param(const char **chunks, |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11133 | const char* url_param_name, size_t url_param_name_l, |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11134 | const char **vstart, const char **vend, char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11135 | { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11136 | const char *arg_start, *qs_end; |
| 11137 | const char *value_start, *value_end; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11138 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11139 | arg_start = chunks[0]; |
| 11140 | qs_end = chunks[1]; |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11141 | if (url_param_name_l) { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11142 | /* Looks for an argument name. */ |
| 11143 | arg_start = find_url_param_pos(chunks, |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11144 | url_param_name, url_param_name_l, |
| 11145 | delim); |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11146 | /* Check for wrapping. */ |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 11147 | if (arg_start >= qs_end) |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11148 | qs_end = chunks[3]; |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11149 | } |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11150 | if (!arg_start) |
| 11151 | return 0; |
| 11152 | |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11153 | if (!url_param_name_l) { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11154 | while (1) { |
| 11155 | /* looks for the first argument. */ |
| 11156 | value_start = memchr(arg_start, '=', qs_end - arg_start); |
| 11157 | if (!value_start) { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11158 | /* Check for wrapping. */ |
| 11159 | if (arg_start >= chunks[0] && |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 11160 | arg_start < chunks[1] && |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11161 | chunks[2]) { |
| 11162 | arg_start = chunks[2]; |
| 11163 | qs_end = chunks[3]; |
| 11164 | continue; |
| 11165 | } |
| 11166 | return 0; |
| 11167 | } |
| 11168 | break; |
| 11169 | } |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11170 | value_start++; |
| 11171 | } |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11172 | else { |
| 11173 | /* Jump the argument length. */ |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11174 | value_start = arg_start + url_param_name_l + 1; |
| 11175 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11176 | /* Check for pointer wrapping. */ |
| 11177 | if (fix_pointer_if_wrap(chunks, &value_start)) { |
| 11178 | /* Update the end pointer. */ |
| 11179 | qs_end = chunks[3]; |
| 11180 | |
| 11181 | /* Check for overflow. */ |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 11182 | if (value_start >= qs_end) |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11183 | return 0; |
| 11184 | } |
| 11185 | } |
| 11186 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11187 | value_end = value_start; |
| 11188 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11189 | while (1) { |
| 11190 | while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim)) |
| 11191 | value_end++; |
| 11192 | if (value_end < qs_end) |
| 11193 | break; |
| 11194 | /* process buffer wrapping. */ |
| 11195 | if (value_end >= chunks[0] && |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 11196 | value_end < chunks[1] && |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11197 | chunks[2]) { |
| 11198 | value_end = chunks[2]; |
| 11199 | qs_end = chunks[3]; |
| 11200 | continue; |
| 11201 | } |
| 11202 | break; |
| 11203 | } |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11204 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11205 | *vstart = value_start; |
| 11206 | *vend = value_end; |
Cyril Bonté | ce1ef4d | 2015-11-26 21:39:56 +0100 | [diff] [blame] | 11207 | return 1; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11208 | } |
| 11209 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11210 | /* This scans a URL-encoded query string. It takes an optionally wrapping |
| 11211 | * string whose first contigous chunk has its beginning in ctx->a[0] and end |
| 11212 | * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The |
| 11213 | * pointers are updated for next iteration before leaving. |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11214 | */ |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11215 | static int |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11216 | 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] | 11217 | { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11218 | const char *vstart, *vend; |
| 11219 | struct chunk *temp; |
| 11220 | const char **chunks = (const char **)smp->ctx.a; |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11221 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11222 | if (!find_next_url_param(chunks, |
Thierry FOURNIER | 0948d41 | 2015-05-20 15:22:37 +0200 | [diff] [blame] | 11223 | name, name_len, |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11224 | &vstart, &vend, |
Thierry FOURNIER | 0948d41 | 2015-05-20 15:22:37 +0200 | [diff] [blame] | 11225 | delim)) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11226 | return 0; |
| 11227 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11228 | /* Create sample. If the value is contiguous, return the pointer as CONST, |
| 11229 | * if the value is wrapped, copy-it in a buffer. |
| 11230 | */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11231 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11232 | if (chunks[2] && |
| 11233 | vstart >= chunks[0] && vstart <= chunks[1] && |
| 11234 | vend >= chunks[2] && vend <= chunks[3]) { |
| 11235 | /* Wrapped case. */ |
| 11236 | temp = get_trash_chunk(); |
| 11237 | memcpy(temp->str, vstart, chunks[1] - vstart); |
| 11238 | memcpy(temp->str + ( chunks[1] - vstart ), chunks[2], vend - chunks[2]); |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11239 | smp->data.u.str.str = temp->str; |
| 11240 | smp->data.u.str.len = ( chunks[1] - vstart ) + ( vend - chunks[2] ); |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11241 | } else { |
| 11242 | /* Contiguous case. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11243 | smp->data.u.str.str = (char *)vstart; |
| 11244 | smp->data.u.str.len = vend - vstart; |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11245 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
| 11246 | } |
| 11247 | |
| 11248 | /* Update context, check wrapping. */ |
| 11249 | chunks[0] = vend; |
| 11250 | if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) { |
| 11251 | chunks[1] = chunks[3]; |
| 11252 | chunks[2] = NULL; |
| 11253 | } |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11254 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11255 | if (chunks[0] < chunks[1]) |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11256 | smp->flags |= SMP_F_NOT_LAST; |
| 11257 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11258 | return 1; |
| 11259 | } |
| 11260 | |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11261 | /* This function iterates over each parameter of the query string. It uses |
| 11262 | * 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] | 11263 | * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL. |
| 11264 | * An optional parameter name is passed in args[0], otherwise any parameter is |
| 11265 | * considered. It supports an optional delimiter argument for the beginning of |
| 11266 | * the string in args[1], which defaults to "?". |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11267 | */ |
| 11268 | static int |
| 11269 | smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 11270 | { |
| 11271 | struct http_msg *msg; |
| 11272 | char delim = '?'; |
| 11273 | const char *name; |
| 11274 | int name_len; |
| 11275 | |
Dragan Dosen | 26f77e5 | 2015-05-25 10:02:11 +0200 | [diff] [blame] | 11276 | if (!args || |
| 11277 | (args[0].type && args[0].type != ARGT_STR) || |
| 11278 | (args[1].type && args[1].type != ARGT_STR)) |
| 11279 | return 0; |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11280 | |
Dragan Dosen | 26f77e5 | 2015-05-25 10:02:11 +0200 | [diff] [blame] | 11281 | name = ""; |
| 11282 | name_len = 0; |
| 11283 | if (args->type == ARGT_STR) { |
| 11284 | name = args->data.str.str; |
| 11285 | name_len = args->data.str.len; |
| 11286 | } |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11287 | |
Dragan Dosen | 26f77e5 | 2015-05-25 10:02:11 +0200 | [diff] [blame] | 11288 | if (args[1].type) |
| 11289 | delim = *args[1].data.str.str; |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11290 | |
Dragan Dosen | 26f77e5 | 2015-05-25 10:02:11 +0200 | [diff] [blame] | 11291 | if (!smp->ctx.a[0]) { // first call, find the query string |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11292 | CHECK_HTTP_MESSAGE_FIRST(); |
| 11293 | |
| 11294 | msg = &smp->strm->txn->req; |
| 11295 | |
| 11296 | smp->ctx.a[0] = find_param_list(msg->chn->buf->p + msg->sl.rq.u, |
| 11297 | msg->sl.rq.u_l, delim); |
| 11298 | if (!smp->ctx.a[0]) |
| 11299 | return 0; |
| 11300 | |
| 11301 | 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] | 11302 | |
| 11303 | /* Assume that the context is filled with NULL pointer |
| 11304 | * before the first call. |
| 11305 | * smp->ctx.a[2] = NULL; |
| 11306 | * smp->ctx.a[3] = NULL; |
| 11307 | */ |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11308 | } |
| 11309 | |
| 11310 | return smp_fetch_param(delim, name, name_len, args, smp, kw, private); |
| 11311 | } |
| 11312 | |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 11313 | /* This function iterates over each parameter of the body. This requires |
| 11314 | * 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] | 11315 | * ctx->a[0] and ctx->a[1] to store the beginning and end of the first |
| 11316 | * contigous part of the body, and optionally ctx->a[2..3] to reference the |
| 11317 | * optional second part if the body wraps at the end of the buffer. An optional |
| 11318 | * parameter name is passed in args[0], otherwise any parameter is considered. |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 11319 | */ |
| 11320 | static int |
| 11321 | smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 11322 | { |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 11323 | struct http_msg *msg; |
| 11324 | unsigned long len; |
| 11325 | unsigned long block1; |
| 11326 | char *body; |
| 11327 | const char *name; |
| 11328 | int name_len; |
| 11329 | |
| 11330 | if (!args || (args[0].type && args[0].type != ARGT_STR)) |
| 11331 | return 0; |
| 11332 | |
| 11333 | name = ""; |
| 11334 | name_len = 0; |
| 11335 | if (args[0].type == ARGT_STR) { |
| 11336 | name = args[0].data.str.str; |
| 11337 | name_len = args[0].data.str.len; |
| 11338 | } |
| 11339 | |
| 11340 | if (!smp->ctx.a[0]) { // first call, find the query string |
| 11341 | CHECK_HTTP_MESSAGE_FIRST(); |
| 11342 | |
| 11343 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 11344 | msg = &smp->strm->txn->req; |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 11345 | else |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 11346 | msg = &smp->strm->txn->rsp; |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 11347 | |
| 11348 | len = http_body_bytes(msg); |
| 11349 | body = b_ptr(msg->chn->buf, -http_data_rewind(msg)); |
| 11350 | |
| 11351 | block1 = len; |
| 11352 | if (block1 > msg->chn->buf->data + msg->chn->buf->size - body) |
| 11353 | block1 = msg->chn->buf->data + msg->chn->buf->size - body; |
| 11354 | |
| 11355 | if (block1 == len) { |
| 11356 | /* buffer is not wrapped (or empty) */ |
| 11357 | smp->ctx.a[0] = body; |
| 11358 | smp->ctx.a[1] = body + len; |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11359 | |
| 11360 | /* Assume that the context is filled with NULL pointer |
| 11361 | * before the first call. |
| 11362 | * smp->ctx.a[2] = NULL; |
| 11363 | * smp->ctx.a[3] = NULL; |
| 11364 | */ |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 11365 | } |
| 11366 | else { |
| 11367 | /* buffer is wrapped, we need to defragment it */ |
| 11368 | smp->ctx.a[0] = body; |
| 11369 | smp->ctx.a[1] = body + block1; |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11370 | smp->ctx.a[2] = msg->chn->buf->data; |
| 11371 | smp->ctx.a[3] = msg->chn->buf->data + ( len - block1 ); |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 11372 | } |
| 11373 | } |
| 11374 | return smp_fetch_param('&', name, name_len, args, smp, kw, private); |
| 11375 | } |
| 11376 | |
Willy Tarreau | a9fddca | 2012-07-31 07:51:48 +0200 | [diff] [blame] | 11377 | /* Return the signed integer value for the specified url parameter (see url_param |
| 11378 | * above). |
| 11379 | */ |
| 11380 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11381 | 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] | 11382 | { |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11383 | int ret = smp_fetch_url_param(args, smp, kw, private); |
Willy Tarreau | a9fddca | 2012-07-31 07:51:48 +0200 | [diff] [blame] | 11384 | |
| 11385 | if (ret > 0) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11386 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11387 | 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] | 11388 | } |
| 11389 | |
| 11390 | return ret; |
| 11391 | } |
| 11392 | |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11393 | /* This produces a 32-bit hash of the concatenation of the first occurrence of |
| 11394 | * the Host header followed by the path component if it begins with a slash ('/'). |
| 11395 | * This means that '*' will not be added, resulting in exactly the first Host |
| 11396 | * entry. If no Host header is found, then the path is used. The resulting value |
| 11397 | * is hashed using the url hash followed by a full avalanche hash and provides a |
| 11398 | * 32-bit integer value. This fetch is useful for tracking per-URL activity on |
| 11399 | * high-traffic sites without having to store whole paths. |
| 11400 | * this differs from the base32 functions in that it includes the url parameters |
| 11401 | * as well as the path |
| 11402 | */ |
| 11403 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11404 | 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] | 11405 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11406 | struct http_txn *txn; |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11407 | struct hdr_ctx ctx; |
| 11408 | unsigned int hash = 0; |
| 11409 | char *ptr, *beg, *end; |
| 11410 | int len; |
| 11411 | |
| 11412 | CHECK_HTTP_MESSAGE_FIRST(); |
| 11413 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11414 | txn = smp->strm->txn; |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11415 | ctx.idx = 0; |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 11416 | 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] | 11417 | /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */ |
| 11418 | ptr = ctx.line + ctx.val; |
| 11419 | len = ctx.vlen; |
| 11420 | while (len--) |
| 11421 | hash = *(ptr++) + (hash << 6) + (hash << 16) - hash; |
| 11422 | } |
| 11423 | |
| 11424 | /* now retrieve the path */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 11425 | 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] | 11426 | beg = http_get_path(txn); |
| 11427 | if (!beg) |
| 11428 | beg = end; |
| 11429 | |
| 11430 | for (ptr = beg; ptr < end ; ptr++); |
| 11431 | |
| 11432 | if (beg < ptr && *beg == '/') { |
| 11433 | while (beg < ptr) |
| 11434 | hash = *(beg++) + (hash << 6) + (hash << 16) - hash; |
| 11435 | } |
| 11436 | hash = full_hash(hash); |
| 11437 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11438 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11439 | smp->data.u.sint = hash; |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11440 | smp->flags = SMP_F_VOL_1ST; |
| 11441 | return 1; |
| 11442 | } |
| 11443 | |
| 11444 | /* This concatenates the source address with the 32-bit hash of the Host and |
| 11445 | * URL as returned by smp_fetch_base32(). The idea is to have per-source and |
| 11446 | * per-url counters. The result is a binary block from 8 to 20 bytes depending |
| 11447 | * on the source address length. The URL hash is stored before the address so |
| 11448 | * that in environments where IPv6 is insignificant, truncating the output to |
| 11449 | * 8 bytes would still work. |
| 11450 | */ |
| 11451 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11452 | 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] | 11453 | { |
| 11454 | struct chunk *temp; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11455 | struct connection *cli_conn = objt_conn(smp->sess->origin); |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11456 | |
Dragan Dosen | db5af61 | 2016-06-16 11:23:01 +0200 | [diff] [blame] | 11457 | if (!cli_conn) |
| 11458 | return 0; |
| 11459 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11460 | if (!smp_fetch_url32(args, smp, kw, private)) |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11461 | return 0; |
| 11462 | |
| 11463 | temp = get_trash_chunk(); |
Dragan Dosen | e5f4133 | 2016-06-16 11:08:08 +0200 | [diff] [blame] | 11464 | *(unsigned int *)temp->str = htonl(smp->data.u.sint); |
| 11465 | temp->len += sizeof(unsigned int); |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11466 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 11467 | switch (cli_conn->addr.from.ss_family) { |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11468 | case AF_INET: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 11469 | 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] | 11470 | temp->len += 4; |
| 11471 | break; |
| 11472 | case AF_INET6: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 11473 | 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] | 11474 | temp->len += 16; |
| 11475 | break; |
| 11476 | default: |
| 11477 | return 0; |
| 11478 | } |
| 11479 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11480 | smp->data.u.str = *temp; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11481 | smp->data.type = SMP_T_BIN; |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11482 | return 1; |
| 11483 | } |
| 11484 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 11485 | /* This function is used to validate the arguments passed to any "hdr" fetch |
| 11486 | * keyword. These keywords support an optional positive or negative occurrence |
| 11487 | * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It |
| 11488 | * is assumed that the types are already the correct ones. Returns 0 on error, |
| 11489 | * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an |
| 11490 | * error message in case of error, that the caller is responsible for freeing. |
| 11491 | * The initial location must either be freeable or NULL. |
| 11492 | */ |
Thierry FOURNIER | 49f45af | 2014-12-08 19:50:43 +0100 | [diff] [blame] | 11493 | int val_hdr(struct arg *arg, char **err_msg) |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 11494 | { |
| 11495 | 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] | 11496 | memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY); |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 11497 | return 0; |
| 11498 | } |
| 11499 | return 1; |
| 11500 | } |
| 11501 | |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 11502 | /* takes an UINT value on input supposed to represent the time since EPOCH, |
| 11503 | * adds an optional offset found in args[0] and emits a string representing |
| 11504 | * the date in RFC-1123/5322 format. |
| 11505 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11506 | 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] | 11507 | { |
Cyril Bonté | f78d896 | 2016-01-22 19:40:28 +0100 | [diff] [blame] | 11508 | const char day[7][4] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 11509 | const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; |
| 11510 | struct chunk *temp; |
| 11511 | struct tm *tm; |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 11512 | /* 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] | 11513 | time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL; |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 11514 | |
| 11515 | /* add offset */ |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 11516 | if (args && (args[0].type == ARGT_SINT)) |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 11517 | curr_date += args[0].data.sint; |
| 11518 | |
| 11519 | tm = gmtime(&curr_date); |
Thierry FOURNIER | fac9ccf | 2015-07-08 00:15:20 +0200 | [diff] [blame] | 11520 | if (!tm) |
| 11521 | return 0; |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 11522 | |
| 11523 | temp = get_trash_chunk(); |
| 11524 | temp->len = snprintf(temp->str, temp->size - temp->len, |
| 11525 | "%s, %02d %s %04d %02d:%02d:%02d GMT", |
| 11526 | day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year, |
| 11527 | tm->tm_hour, tm->tm_min, tm->tm_sec); |
| 11528 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11529 | smp->data.u.str = *temp; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11530 | smp->data.type = SMP_T_STR; |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 11531 | return 1; |
| 11532 | } |
| 11533 | |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11534 | /* Match language range with language tag. RFC2616 14.4: |
| 11535 | * |
| 11536 | * A language-range matches a language-tag if it exactly equals |
| 11537 | * the tag, or if it exactly equals a prefix of the tag such |
| 11538 | * that the first tag character following the prefix is "-". |
| 11539 | * |
| 11540 | * Return 1 if the strings match, else return 0. |
| 11541 | */ |
| 11542 | static inline int language_range_match(const char *range, int range_len, |
| 11543 | const char *tag, int tag_len) |
| 11544 | { |
| 11545 | const char *end = range + range_len; |
| 11546 | const char *tend = tag + tag_len; |
| 11547 | while (range < end) { |
| 11548 | if (*range == '-' && tag == tend) |
| 11549 | return 1; |
| 11550 | if (*range != *tag || tag == tend) |
| 11551 | return 0; |
| 11552 | range++; |
| 11553 | tag++; |
| 11554 | } |
| 11555 | /* Return true only if the last char of the tag is matched. */ |
| 11556 | return tag == tend; |
| 11557 | } |
| 11558 | |
| 11559 | /* 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] | 11560 | 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] | 11561 | { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11562 | const char *al = smp->data.u.str.str; |
| 11563 | const char *end = al + smp->data.u.str.len; |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11564 | const char *token; |
| 11565 | int toklen; |
| 11566 | int qvalue; |
| 11567 | const char *str; |
| 11568 | const char *w; |
| 11569 | int best_q = 0; |
| 11570 | |
| 11571 | /* Set the constant to the sample, because the output of the |
| 11572 | * function will be peek in the constant configuration string. |
| 11573 | */ |
| 11574 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11575 | smp->data.u.str.size = 0; |
| 11576 | smp->data.u.str.str = ""; |
| 11577 | smp->data.u.str.len = 0; |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11578 | |
| 11579 | /* Parse the accept language */ |
| 11580 | while (1) { |
| 11581 | |
| 11582 | /* Jump spaces, quit if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 11583 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11584 | al++; |
| 11585 | if (al >= end) |
| 11586 | break; |
| 11587 | |
| 11588 | /* Start of the fisrt word. */ |
| 11589 | token = al; |
| 11590 | |
| 11591 | /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 11592 | while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11593 | al++; |
| 11594 | if (al == token) |
| 11595 | goto expect_comma; |
| 11596 | |
| 11597 | /* Length of the token. */ |
| 11598 | toklen = al - token; |
| 11599 | qvalue = 1000; |
| 11600 | |
| 11601 | /* Check if the token exists in the list. If the token not exists, |
| 11602 | * jump to the next token. |
| 11603 | */ |
| 11604 | str = args[0].data.str.str; |
| 11605 | w = str; |
| 11606 | while (1) { |
| 11607 | if (*str == ';' || *str == '\0') { |
| 11608 | if (language_range_match(token, toklen, w, str-w)) |
| 11609 | goto look_for_q; |
| 11610 | if (*str == '\0') |
| 11611 | goto expect_comma; |
| 11612 | w = str + 1; |
| 11613 | } |
| 11614 | str++; |
| 11615 | } |
| 11616 | goto expect_comma; |
| 11617 | |
| 11618 | look_for_q: |
| 11619 | |
| 11620 | /* Jump spaces, quit if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 11621 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11622 | al++; |
| 11623 | if (al >= end) |
| 11624 | goto process_value; |
| 11625 | |
| 11626 | /* If ',' is found, process the result */ |
| 11627 | if (*al == ',') |
| 11628 | goto process_value; |
| 11629 | |
| 11630 | /* If the character is different from ';', look |
| 11631 | * for the end of the header part in best effort. |
| 11632 | */ |
| 11633 | if (*al != ';') |
| 11634 | goto expect_comma; |
| 11635 | |
| 11636 | /* Assumes that the char is ';', now expect "q=". */ |
| 11637 | al++; |
| 11638 | |
| 11639 | /* Jump spaces, process value if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 11640 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11641 | al++; |
| 11642 | if (al >= end) |
| 11643 | goto process_value; |
| 11644 | |
| 11645 | /* Expect 'q'. If no 'q', continue in best effort */ |
| 11646 | if (*al != 'q') |
| 11647 | goto process_value; |
| 11648 | al++; |
| 11649 | |
| 11650 | /* Jump spaces, process value if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 11651 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11652 | al++; |
| 11653 | if (al >= end) |
| 11654 | goto process_value; |
| 11655 | |
| 11656 | /* Expect '='. If no '=', continue in best effort */ |
| 11657 | if (*al != '=') |
| 11658 | goto process_value; |
| 11659 | al++; |
| 11660 | |
| 11661 | /* Jump spaces, process value if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 11662 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11663 | al++; |
| 11664 | if (al >= end) |
| 11665 | goto process_value; |
| 11666 | |
| 11667 | /* Parse the q value. */ |
| 11668 | qvalue = parse_qvalue(al, &al); |
| 11669 | |
| 11670 | process_value: |
| 11671 | |
| 11672 | /* If the new q value is the best q value, then store the associated |
| 11673 | * language in the response. If qvalue is the biggest value (1000), |
| 11674 | * break the process. |
| 11675 | */ |
| 11676 | if (qvalue > best_q) { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11677 | smp->data.u.str.str = (char *)w; |
| 11678 | smp->data.u.str.len = str - w; |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11679 | if (qvalue >= 1000) |
| 11680 | break; |
| 11681 | best_q = qvalue; |
| 11682 | } |
| 11683 | |
| 11684 | expect_comma: |
| 11685 | |
| 11686 | /* Expect comma or end. If the end is detected, quit the loop. */ |
| 11687 | while (al < end && *al != ',') |
| 11688 | al++; |
| 11689 | if (al >= end) |
| 11690 | break; |
| 11691 | |
| 11692 | /* Comma is found, jump it and restart the analyzer. */ |
| 11693 | al++; |
| 11694 | } |
| 11695 | |
| 11696 | /* Set default value if required. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11697 | if (smp->data.u.str.len == 0 && args[1].type == ARGT_STR) { |
| 11698 | smp->data.u.str.str = args[1].data.str.str; |
| 11699 | smp->data.u.str.len = args[1].data.str.len; |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11700 | } |
| 11701 | |
| 11702 | /* Return true only if a matching language was found. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11703 | return smp->data.u.str.len != 0; |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11704 | } |
| 11705 | |
Thierry FOURNIER | 82ff3c9 | 2015-05-07 15:46:20 +0200 | [diff] [blame] | 11706 | /* This fetch url-decode any input string. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11707 | 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] | 11708 | { |
| 11709 | /* If the constant flag is set or if not size is avalaible at |
| 11710 | * the end of the buffer, copy the string in other buffer |
| 11711 | * before decoding. |
| 11712 | */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11713 | 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] | 11714 | struct chunk *str = get_trash_chunk(); |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11715 | memcpy(str->str, smp->data.u.str.str, smp->data.u.str.len); |
| 11716 | smp->data.u.str.str = str->str; |
| 11717 | smp->data.u.str.size = str->size; |
Thierry FOURNIER | 82ff3c9 | 2015-05-07 15:46:20 +0200 | [diff] [blame] | 11718 | smp->flags &= ~SMP_F_CONST; |
| 11719 | } |
| 11720 | |
| 11721 | /* Add final \0 required by url_decode(), and convert the input string. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11722 | smp->data.u.str.str[smp->data.u.str.len] = '\0'; |
| 11723 | smp->data.u.str.len = url_decode(smp->data.u.str.str); |
Christopher Faulet | a258479 | 2017-10-05 10:03:12 +0200 | [diff] [blame] | 11724 | return (smp->data.u.str.len >= 0); |
Thierry FOURNIER | 82ff3c9 | 2015-05-07 15:46:20 +0200 | [diff] [blame] | 11725 | } |
| 11726 | |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 11727 | static int smp_conv_req_capture(const struct arg *args, struct sample *smp, void *private) |
| 11728 | { |
| 11729 | struct proxy *fe = strm_fe(smp->strm); |
| 11730 | int idx, i; |
| 11731 | struct cap_hdr *hdr; |
| 11732 | int len; |
| 11733 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 11734 | if (!args || args->type != ARGT_SINT) |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 11735 | return 0; |
| 11736 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 11737 | idx = args->data.sint; |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 11738 | |
| 11739 | /* Check the availibity of the capture id. */ |
| 11740 | if (idx > fe->nb_req_cap - 1) |
| 11741 | return 0; |
| 11742 | |
| 11743 | /* Look for the original configuration. */ |
| 11744 | for (hdr = fe->req_cap, i = fe->nb_req_cap - 1; |
| 11745 | hdr != NULL && i != idx ; |
| 11746 | i--, hdr = hdr->next); |
| 11747 | if (!hdr) |
| 11748 | return 0; |
| 11749 | |
| 11750 | /* check for the memory allocation */ |
| 11751 | if (smp->strm->req_cap[hdr->index] == NULL) |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 11752 | smp->strm->req_cap[hdr->index] = pool_alloc(hdr->pool); |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 11753 | if (smp->strm->req_cap[hdr->index] == NULL) |
| 11754 | return 0; |
| 11755 | |
| 11756 | /* Check length. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11757 | len = smp->data.u.str.len; |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 11758 | if (len > hdr->len) |
| 11759 | len = hdr->len; |
| 11760 | |
| 11761 | /* Capture input data. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11762 | memcpy(smp->strm->req_cap[idx], smp->data.u.str.str, len); |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 11763 | smp->strm->req_cap[idx][len] = '\0'; |
| 11764 | |
| 11765 | return 1; |
| 11766 | } |
| 11767 | |
| 11768 | static int smp_conv_res_capture(const struct arg *args, struct sample *smp, void *private) |
| 11769 | { |
| 11770 | struct proxy *fe = strm_fe(smp->strm); |
| 11771 | int idx, i; |
| 11772 | struct cap_hdr *hdr; |
| 11773 | int len; |
| 11774 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 11775 | if (!args || args->type != ARGT_SINT) |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 11776 | return 0; |
| 11777 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 11778 | idx = args->data.sint; |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 11779 | |
| 11780 | /* Check the availibity of the capture id. */ |
| 11781 | if (idx > fe->nb_rsp_cap - 1) |
| 11782 | return 0; |
| 11783 | |
| 11784 | /* Look for the original configuration. */ |
| 11785 | for (hdr = fe->rsp_cap, i = fe->nb_rsp_cap - 1; |
| 11786 | hdr != NULL && i != idx ; |
| 11787 | i--, hdr = hdr->next); |
| 11788 | if (!hdr) |
| 11789 | return 0; |
| 11790 | |
| 11791 | /* check for the memory allocation */ |
| 11792 | if (smp->strm->res_cap[hdr->index] == NULL) |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 11793 | smp->strm->res_cap[hdr->index] = pool_alloc(hdr->pool); |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 11794 | if (smp->strm->res_cap[hdr->index] == NULL) |
| 11795 | return 0; |
| 11796 | |
| 11797 | /* Check length. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11798 | len = smp->data.u.str.len; |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 11799 | if (len > hdr->len) |
| 11800 | len = hdr->len; |
| 11801 | |
| 11802 | /* Capture input data. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11803 | memcpy(smp->strm->res_cap[idx], smp->data.u.str.str, len); |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 11804 | smp->strm->res_cap[idx][len] = '\0'; |
| 11805 | |
| 11806 | return 1; |
| 11807 | } |
| 11808 | |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11809 | /* 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] | 11810 | * takes the string from the variable 'replace' with length 'len', then modifies |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11811 | * the relevant part of the request line accordingly. Then it updates various |
| 11812 | * pointers to the next elements which were moved, and the total buffer length. |
| 11813 | * 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] | 11814 | * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal |
| 11815 | * error, though this can be revisited when this code is finally exploited. |
| 11816 | * |
| 11817 | * 'action' can be '0' to replace method, '1' to replace path, '2' to replace |
| 11818 | * query string and 3 to replace uri. |
| 11819 | * |
| 11820 | * In query string case, the mark question '?' must be set at the start of the |
| 11821 | * string by the caller, event if the replacement query string is empty. |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11822 | */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11823 | int http_replace_req_line(int action, const char *replace, int len, |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 11824 | struct proxy *px, struct stream *s) |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11825 | { |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 11826 | struct http_txn *txn = s->txn; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11827 | char *cur_ptr, *cur_end; |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11828 | int offset = 0; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11829 | int delta; |
| 11830 | |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11831 | switch (action) { |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11832 | case 0: // method |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 11833 | cur_ptr = s->req.buf->p; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11834 | cur_end = cur_ptr + txn->req.sl.rq.m_l; |
| 11835 | |
| 11836 | /* adjust req line offsets and lengths */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11837 | delta = len - offset - (cur_end - cur_ptr); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11838 | txn->req.sl.rq.m_l += delta; |
| 11839 | txn->req.sl.rq.u += delta; |
| 11840 | txn->req.sl.rq.v += delta; |
| 11841 | break; |
| 11842 | |
| 11843 | case 1: // path |
| 11844 | cur_ptr = http_get_path(txn); |
| 11845 | if (!cur_ptr) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 11846 | cur_ptr = s->req.buf->p + txn->req.sl.rq.u; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11847 | |
| 11848 | cur_end = cur_ptr; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 11849 | 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] | 11850 | cur_end++; |
| 11851 | |
| 11852 | /* adjust req line offsets and lengths */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11853 | delta = len - offset - (cur_end - cur_ptr); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11854 | txn->req.sl.rq.u_l += delta; |
| 11855 | txn->req.sl.rq.v += delta; |
| 11856 | break; |
| 11857 | |
| 11858 | case 2: // query |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11859 | offset = 1; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 11860 | cur_ptr = s->req.buf->p + txn->req.sl.rq.u; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11861 | cur_end = cur_ptr + txn->req.sl.rq.u_l; |
| 11862 | while (cur_ptr < cur_end && *cur_ptr != '?') |
| 11863 | cur_ptr++; |
| 11864 | |
| 11865 | /* skip the question mark or indicate that we must insert it |
| 11866 | * (but only if the format string is not empty then). |
| 11867 | */ |
| 11868 | if (cur_ptr < cur_end) |
| 11869 | cur_ptr++; |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11870 | else if (len > 1) |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11871 | offset = 0; |
| 11872 | |
| 11873 | /* adjust req line offsets and lengths */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11874 | delta = len - offset - (cur_end - cur_ptr); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11875 | txn->req.sl.rq.u_l += delta; |
| 11876 | txn->req.sl.rq.v += delta; |
| 11877 | break; |
| 11878 | |
| 11879 | case 3: // uri |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 11880 | cur_ptr = s->req.buf->p + txn->req.sl.rq.u; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11881 | cur_end = cur_ptr + txn->req.sl.rq.u_l; |
| 11882 | |
| 11883 | /* adjust req line offsets and lengths */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11884 | delta = len - offset - (cur_end - cur_ptr); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11885 | txn->req.sl.rq.u_l += delta; |
| 11886 | txn->req.sl.rq.v += delta; |
| 11887 | break; |
| 11888 | |
| 11889 | default: |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11890 | return -1; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11891 | } |
| 11892 | |
| 11893 | /* commit changes and adjust end of message */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11894 | 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] | 11895 | txn->req.sl.rq.l += delta; |
| 11896 | txn->hdr_idx.v[0].len += delta; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11897 | http_msg_move_end(&txn->req, delta); |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11898 | return 0; |
| 11899 | } |
| 11900 | |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 11901 | /* This function replace the HTTP status code and the associated message. The |
| 11902 | * variable <status> contains the new status code. This function never fails. |
| 11903 | */ |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 11904 | 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] | 11905 | { |
| 11906 | struct http_txn *txn = s->txn; |
| 11907 | char *cur_ptr, *cur_end; |
| 11908 | int delta; |
| 11909 | char *res; |
| 11910 | int c_l; |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 11911 | const char *msg = reason; |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 11912 | int msg_len; |
| 11913 | |
| 11914 | chunk_reset(&trash); |
| 11915 | |
| 11916 | res = ultoa_o(status, trash.str, trash.size); |
| 11917 | c_l = res - trash.str; |
| 11918 | |
| 11919 | trash.str[c_l] = ' '; |
| 11920 | trash.len = c_l + 1; |
| 11921 | |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 11922 | /* Do we have a custom reason format string? */ |
| 11923 | if (msg == NULL) |
| 11924 | msg = get_reason(status); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 11925 | msg_len = strlen(msg); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 11926 | strncpy(&trash.str[trash.len], msg, trash.size - trash.len); |
| 11927 | trash.len += msg_len; |
| 11928 | |
| 11929 | cur_ptr = s->res.buf->p + txn->rsp.sl.st.c; |
| 11930 | cur_end = s->res.buf->p + txn->rsp.sl.st.r + txn->rsp.sl.st.r_l; |
| 11931 | |
| 11932 | /* commit changes and adjust message */ |
| 11933 | delta = buffer_replace2(s->res.buf, cur_ptr, cur_end, trash.str, trash.len); |
| 11934 | |
| 11935 | /* adjust res line offsets and lengths */ |
| 11936 | txn->rsp.sl.st.r += c_l - txn->rsp.sl.st.c_l; |
| 11937 | txn->rsp.sl.st.c_l = c_l; |
| 11938 | txn->rsp.sl.st.r_l = msg_len; |
| 11939 | |
| 11940 | delta = trash.len - (cur_end - cur_ptr); |
| 11941 | txn->rsp.sl.st.l += delta; |
| 11942 | txn->hdr_idx.v[0].len += delta; |
| 11943 | http_msg_move_end(&txn->rsp, delta); |
| 11944 | } |
| 11945 | |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11946 | /* This function executes one of the set-{method,path,query,uri} actions. It |
| 11947 | * 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] | 11948 | * the action to be performed in <http.action>, previously filled by function |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11949 | * parse_set_req_line(). The replacement action is excuted by the function |
Thierry FOURNIER | 3f4bc65 | 2015-08-26 16:23:34 +0200 | [diff] [blame] | 11950 | * http_action_set_req_line(). It always returns ACT_RET_CONT. If an error |
| 11951 | * occurs the action is canceled, but the rule processing continue. |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11952 | */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 11953 | 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] | 11954 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11955 | { |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 11956 | struct chunk *replace; |
| 11957 | enum act_return ret = ACT_RET_ERR; |
| 11958 | |
| 11959 | replace = alloc_trash_chunk(); |
| 11960 | if (!replace) |
| 11961 | goto leave; |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11962 | |
| 11963 | /* If we have to create a query string, prepare a '?'. */ |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 11964 | if (rule->arg.http.action == 2) |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 11965 | replace->str[replace->len++] = '?'; |
| 11966 | replace->len += build_logline(s, replace->str + replace->len, replace->size - replace->len, |
| 11967 | &rule->arg.http.logfmt); |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11968 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 11969 | http_replace_req_line(rule->arg.http.action, replace->str, replace->len, px, s); |
| 11970 | |
| 11971 | ret = ACT_RET_CONT; |
| 11972 | |
| 11973 | leave: |
| 11974 | free_trash_chunk(replace); |
| 11975 | return ret; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11976 | } |
| 11977 | |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 11978 | /* This function is just a compliant action wrapper for "set-status". */ |
| 11979 | 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] | 11980 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 11981 | { |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 11982 | http_set_status(rule->arg.status.code, rule->arg.status.reason, s); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 11983 | return ACT_RET_CONT; |
| 11984 | } |
| 11985 | |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11986 | /* parse an http-request action among : |
| 11987 | * set-method |
| 11988 | * set-path |
| 11989 | * set-query |
| 11990 | * set-uri |
| 11991 | * |
| 11992 | * All of them accept a single argument of type string representing a log-format. |
| 11993 | * The resulting rule makes use of arg->act.p[0..1] to store the log-format list |
| 11994 | * 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] | 11995 | * 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] | 11996 | */ |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 11997 | enum act_parse_ret parse_set_req_line(const char **args, int *orig_arg, struct proxy *px, |
| 11998 | struct act_rule *rule, char **err) |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11999 | { |
| 12000 | int cur_arg = *orig_arg; |
| 12001 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 12002 | rule->action = ACT_CUSTOM; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12003 | |
| 12004 | switch (args[0][4]) { |
| 12005 | case 'm' : |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 12006 | rule->arg.http.action = 0; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12007 | rule->action_ptr = http_action_set_req_line; |
| 12008 | break; |
| 12009 | case 'p' : |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 12010 | rule->arg.http.action = 1; |
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 'q' : |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 12014 | rule->arg.http.action = 2; |
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 'u' : |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 12018 | rule->arg.http.action = 3; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12019 | rule->action_ptr = http_action_set_req_line; |
| 12020 | break; |
| 12021 | default: |
| 12022 | memprintf(err, "internal error: unhandled action '%s'", args[0]); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12023 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12024 | } |
| 12025 | |
| 12026 | if (!*args[cur_arg] || |
| 12027 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 12028 | memprintf(err, "expects exactly 1 argument <format>"); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12029 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12030 | } |
| 12031 | |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 12032 | LIST_INIT(&rule->arg.http.logfmt); |
Olivier Houchard | fbc74e8 | 2017-11-24 16:54:05 +0100 | [diff] [blame] | 12033 | px->conf.args.ctx = ARGC_HRQ; |
| 12034 | if (!parse_logformat_string(args[cur_arg], px, &rule->arg.http.logfmt, LOG_OPT_HTTP, |
| 12035 | (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] | 12036 | return ACT_RET_PRS_ERR; |
| 12037 | } |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12038 | |
| 12039 | (*orig_arg)++; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12040 | return ACT_RET_PRS_OK; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12041 | } |
| 12042 | |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 12043 | /* parse set-status action: |
| 12044 | * This action accepts a single argument of type int representing |
| 12045 | * an http status code. It returns ACT_RET_PRS_OK on success, |
| 12046 | * ACT_RET_PRS_ERR on error. |
| 12047 | */ |
| 12048 | enum act_parse_ret parse_http_set_status(const char **args, int *orig_arg, struct proxy *px, |
| 12049 | struct act_rule *rule, char **err) |
| 12050 | { |
| 12051 | char *error; |
| 12052 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 12053 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 12054 | rule->action_ptr = action_http_set_status; |
| 12055 | |
| 12056 | /* Check if an argument is available */ |
| 12057 | if (!*args[*orig_arg]) { |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 12058 | memprintf(err, "expects 1 argument: <status>; or 3 arguments: <status> reason <fmt>"); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 12059 | return ACT_RET_PRS_ERR; |
| 12060 | } |
| 12061 | |
| 12062 | /* convert status code as integer */ |
| 12063 | rule->arg.status.code = strtol(args[*orig_arg], &error, 10); |
| 12064 | if (*error != '\0' || rule->arg.status.code < 100 || rule->arg.status.code > 999) { |
| 12065 | memprintf(err, "expects an integer status code between 100 and 999"); |
| 12066 | return ACT_RET_PRS_ERR; |
| 12067 | } |
| 12068 | |
| 12069 | (*orig_arg)++; |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 12070 | |
| 12071 | /* set custom reason string */ |
| 12072 | rule->arg.status.reason = NULL; // If null, we use the default reason for the status code. |
| 12073 | if (*args[*orig_arg] && strcmp(args[*orig_arg], "reason") == 0 && |
| 12074 | (*args[*orig_arg + 1] && strcmp(args[*orig_arg + 1], "if") != 0 && strcmp(args[*orig_arg + 1], "unless") != 0)) { |
| 12075 | (*orig_arg)++; |
| 12076 | rule->arg.status.reason = strdup(args[*orig_arg]); |
| 12077 | (*orig_arg)++; |
| 12078 | } |
| 12079 | |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 12080 | return ACT_RET_PRS_OK; |
| 12081 | } |
| 12082 | |
Willy Tarreau | 53275e8 | 2017-11-24 07:52:01 +0100 | [diff] [blame] | 12083 | /* This function executes the "reject" HTTP action. It clears the request and |
| 12084 | * response buffer without sending any response. It can be useful as an HTTP |
| 12085 | * alternative to the silent-drop action to defend against DoS attacks, and may |
| 12086 | * also be used with HTTP/2 to close a connection instead of just a stream. |
| 12087 | * The txn status is unchanged, indicating no response was sent. The termination |
| 12088 | * flags will indicate "PR". It always returns ACT_RET_STOP. |
| 12089 | */ |
| 12090 | enum act_return http_action_reject(struct act_rule *rule, struct proxy *px, |
| 12091 | struct session *sess, struct stream *s, int flags) |
| 12092 | { |
| 12093 | channel_abort(&s->req); |
| 12094 | channel_abort(&s->res); |
| 12095 | s->req.analysers = 0; |
| 12096 | s->res.analysers = 0; |
| 12097 | |
| 12098 | HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1); |
| 12099 | HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1); |
| 12100 | if (sess->listener && sess->listener->counters) |
| 12101 | HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1); |
| 12102 | |
| 12103 | if (!(s->flags & SF_ERR_MASK)) |
| 12104 | s->flags |= SF_ERR_PRXCOND; |
| 12105 | if (!(s->flags & SF_FINST_MASK)) |
| 12106 | s->flags |= SF_FINST_R; |
| 12107 | |
| 12108 | return ACT_RET_CONT; |
| 12109 | } |
| 12110 | |
| 12111 | /* parse the "reject" action: |
| 12112 | * This action takes no argument and returns ACT_RET_PRS_OK on success, |
| 12113 | * ACT_RET_PRS_ERR on error. |
| 12114 | */ |
| 12115 | enum act_parse_ret parse_http_action_reject(const char **args, int *orig_arg, struct proxy *px, |
| 12116 | struct act_rule *rule, char **err) |
| 12117 | { |
| 12118 | rule->action = ACT_CUSTOM; |
| 12119 | rule->action_ptr = http_action_reject; |
| 12120 | return ACT_RET_PRS_OK; |
| 12121 | } |
| 12122 | |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12123 | /* This function executes the "capture" action. It executes a fetch expression, |
| 12124 | * turns the result into a string and puts it in a capture slot. It always |
| 12125 | * returns 1. If an error occurs the action is cancelled, but the rule |
| 12126 | * processing continues. |
| 12127 | */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12128 | 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] | 12129 | struct session *sess, struct stream *s, int flags) |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12130 | { |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12131 | struct sample *key; |
Thierry FOURNIER | 32b1500 | 2015-07-31 08:56:16 +0200 | [diff] [blame] | 12132 | struct cap_hdr *h = rule->arg.cap.hdr; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12133 | char **cap = s->req_cap; |
| 12134 | int len; |
| 12135 | |
Thierry FOURNIER | 32b1500 | 2015-07-31 08:56:16 +0200 | [diff] [blame] | 12136 | 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] | 12137 | if (!key) |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12138 | return ACT_RET_CONT; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12139 | |
| 12140 | if (cap[h->index] == NULL) |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 12141 | cap[h->index] = pool_alloc(h->pool); |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12142 | |
| 12143 | if (cap[h->index] == NULL) /* no more capture memory */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12144 | return ACT_RET_CONT; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12145 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12146 | len = key->data.u.str.len; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12147 | if (len > h->len) |
| 12148 | len = h->len; |
| 12149 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12150 | memcpy(cap[h->index], key->data.u.str.str, len); |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12151 | cap[h->index][len] = 0; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12152 | return ACT_RET_CONT; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12153 | } |
| 12154 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12155 | /* This function executes the "capture" action and store the result in a |
| 12156 | * capture slot if exists. It executes a fetch expression, turns the result |
| 12157 | * into a string and puts it in a capture slot. It always returns 1. If an |
| 12158 | * error occurs the action is cancelled, but the rule processing continues. |
| 12159 | */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12160 | 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] | 12161 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12162 | { |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12163 | struct sample *key; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12164 | struct cap_hdr *h; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12165 | char **cap = s->req_cap; |
| 12166 | struct proxy *fe = strm_fe(s); |
| 12167 | int len; |
| 12168 | int i; |
| 12169 | |
| 12170 | /* Look for the original configuration. */ |
| 12171 | for (h = fe->req_cap, i = fe->nb_req_cap - 1; |
Thierry FOURNIER | e209797 | 2015-07-31 08:56:35 +0200 | [diff] [blame] | 12172 | h != NULL && i != rule->arg.capid.idx ; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12173 | i--, h = h->next); |
| 12174 | if (!h) |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12175 | return ACT_RET_CONT; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12176 | |
Thierry FOURNIER | e209797 | 2015-07-31 08:56:35 +0200 | [diff] [blame] | 12177 | 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] | 12178 | if (!key) |
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 | |
| 12181 | if (cap[h->index] == NULL) |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 12182 | cap[h->index] = pool_alloc(h->pool); |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12183 | |
| 12184 | if (cap[h->index] == NULL) /* no more capture memory */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12185 | return ACT_RET_CONT; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12186 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12187 | len = key->data.u.str.len; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12188 | if (len > h->len) |
| 12189 | len = h->len; |
| 12190 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12191 | memcpy(cap[h->index], key->data.u.str.str, len); |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12192 | cap[h->index][len] = 0; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12193 | return ACT_RET_CONT; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12194 | } |
| 12195 | |
Christopher Faulet | 29730ba | 2017-09-18 15:26:32 +0200 | [diff] [blame] | 12196 | /* Check an "http-request capture" action. |
| 12197 | * |
| 12198 | * The function returns 1 in success case, otherwise, it returns 0 and err is |
| 12199 | * filled. |
| 12200 | */ |
| 12201 | int check_http_req_capture(struct act_rule *rule, struct proxy *px, char **err) |
| 12202 | { |
Christopher Faulet | fd608dd | 2017-12-04 09:45:15 +0100 | [diff] [blame] | 12203 | if (rule->action_ptr != http_action_req_capture_by_id) |
| 12204 | return 1; |
| 12205 | |
Christopher Faulet | 29730ba | 2017-09-18 15:26:32 +0200 | [diff] [blame] | 12206 | if (rule->arg.capid.idx >= px->nb_req_cap) { |
| 12207 | memprintf(err, "unable to find capture id '%d' referenced by http-request capture rule", |
| 12208 | rule->arg.capid.idx); |
| 12209 | return 0; |
| 12210 | } |
| 12211 | |
| 12212 | return 1; |
| 12213 | } |
| 12214 | |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12215 | /* parse an "http-request capture" action. It takes a single argument which is |
| 12216 | * 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] | 12217 | * 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] | 12218 | * 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] | 12219 | */ |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12220 | enum act_parse_ret parse_http_req_capture(const char **args, int *orig_arg, struct proxy *px, |
| 12221 | struct act_rule *rule, char **err) |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12222 | { |
| 12223 | struct sample_expr *expr; |
| 12224 | struct cap_hdr *hdr; |
| 12225 | int cur_arg; |
Willy Tarreau | 3986ac1 | 2015-05-08 16:13:42 +0200 | [diff] [blame] | 12226 | int len = 0; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12227 | |
| 12228 | for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++) |
| 12229 | if (strcmp(args[cur_arg], "if") == 0 || |
| 12230 | strcmp(args[cur_arg], "unless") == 0) |
| 12231 | break; |
| 12232 | |
| 12233 | if (cur_arg < *orig_arg + 3) { |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12234 | memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]"); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12235 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12236 | } |
| 12237 | |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12238 | cur_arg = *orig_arg; |
| 12239 | expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args); |
| 12240 | if (!expr) |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12241 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12242 | |
| 12243 | if (!(expr->fetch->val & SMP_VAL_FE_HRQ_HDR)) { |
| 12244 | memprintf(err, |
| 12245 | "fetch method '%s' extracts information from '%s', none of which is available here", |
| 12246 | args[cur_arg-1], sample_src_names(expr->fetch->use)); |
| 12247 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12248 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12249 | } |
| 12250 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12251 | if (!args[cur_arg] || !*args[cur_arg]) { |
| 12252 | memprintf(err, "expects 'len or 'id'"); |
| 12253 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12254 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12255 | } |
| 12256 | |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12257 | if (strcmp(args[cur_arg], "len") == 0) { |
| 12258 | cur_arg++; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12259 | |
| 12260 | if (!(px->cap & PR_CAP_FE)) { |
| 12261 | memprintf(err, "proxy '%s' has no frontend capability", px->id); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12262 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12263 | } |
| 12264 | |
Olivier Houchard | fbc74e8 | 2017-11-24 16:54:05 +0100 | [diff] [blame] | 12265 | px->conf.args.ctx = ARGC_CAP; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12266 | |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12267 | if (!args[cur_arg]) { |
| 12268 | memprintf(err, "missing length value"); |
| 12269 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12270 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12271 | } |
| 12272 | /* we copy the table name for now, it will be resolved later */ |
| 12273 | len = atoi(args[cur_arg]); |
| 12274 | if (len <= 0) { |
| 12275 | memprintf(err, "length must be > 0"); |
| 12276 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12277 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12278 | } |
| 12279 | cur_arg++; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12280 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12281 | if (!len) { |
| 12282 | memprintf(err, "a positive 'len' argument is mandatory"); |
| 12283 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12284 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12285 | } |
| 12286 | |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 12287 | hdr = calloc(1, sizeof(*hdr)); |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12288 | hdr->next = px->req_cap; |
| 12289 | hdr->name = NULL; /* not a header capture */ |
| 12290 | hdr->namelen = 0; |
| 12291 | hdr->len = len; |
| 12292 | hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED); |
| 12293 | hdr->index = px->nb_req_cap++; |
| 12294 | |
| 12295 | px->req_cap = hdr; |
| 12296 | px->to_log |= LW_REQHDR; |
| 12297 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 12298 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12299 | rule->action_ptr = http_action_req_capture; |
Thierry FOURNIER | 32b1500 | 2015-07-31 08:56:16 +0200 | [diff] [blame] | 12300 | rule->arg.cap.expr = expr; |
| 12301 | rule->arg.cap.hdr = hdr; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12302 | } |
| 12303 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12304 | else if (strcmp(args[cur_arg], "id") == 0) { |
| 12305 | int id; |
| 12306 | char *error; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12307 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12308 | cur_arg++; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12309 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12310 | if (!args[cur_arg]) { |
| 12311 | memprintf(err, "missing id value"); |
| 12312 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12313 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12314 | } |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12315 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12316 | id = strtol(args[cur_arg], &error, 10); |
| 12317 | if (*error != '\0') { |
| 12318 | memprintf(err, "cannot parse id '%s'", args[cur_arg]); |
| 12319 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12320 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12321 | } |
| 12322 | cur_arg++; |
| 12323 | |
Olivier Houchard | fbc74e8 | 2017-11-24 16:54:05 +0100 | [diff] [blame] | 12324 | px->conf.args.ctx = ARGC_CAP; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12325 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 12326 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12327 | rule->action_ptr = http_action_req_capture_by_id; |
Christopher Faulet | 29730ba | 2017-09-18 15:26:32 +0200 | [diff] [blame] | 12328 | rule->check_ptr = check_http_req_capture; |
Thierry FOURNIER | e209797 | 2015-07-31 08:56:35 +0200 | [diff] [blame] | 12329 | rule->arg.capid.expr = expr; |
| 12330 | rule->arg.capid.idx = id; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12331 | } |
| 12332 | |
| 12333 | else { |
| 12334 | memprintf(err, "expects 'len' or 'id', found '%s'", args[cur_arg]); |
| 12335 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12336 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12337 | } |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12338 | |
| 12339 | *orig_arg = cur_arg; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12340 | return ACT_RET_PRS_OK; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12341 | } |
| 12342 | |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12343 | /* This function executes the "capture" action and store the result in a |
| 12344 | * capture slot if exists. It executes a fetch expression, turns the result |
| 12345 | * into a string and puts it in a capture slot. It always returns 1. If an |
| 12346 | * error occurs the action is cancelled, but the rule processing continues. |
| 12347 | */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12348 | 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] | 12349 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12350 | { |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12351 | struct sample *key; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12352 | struct cap_hdr *h; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12353 | char **cap = s->res_cap; |
| 12354 | struct proxy *fe = strm_fe(s); |
| 12355 | int len; |
| 12356 | int i; |
| 12357 | |
| 12358 | /* Look for the original configuration. */ |
| 12359 | for (h = fe->rsp_cap, i = fe->nb_rsp_cap - 1; |
Thierry FOURNIER | e209797 | 2015-07-31 08:56:35 +0200 | [diff] [blame] | 12360 | h != NULL && i != rule->arg.capid.idx ; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12361 | i--, h = h->next); |
| 12362 | if (!h) |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12363 | return ACT_RET_CONT; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12364 | |
Thierry FOURNIER | e209797 | 2015-07-31 08:56:35 +0200 | [diff] [blame] | 12365 | 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] | 12366 | if (!key) |
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 | |
| 12369 | if (cap[h->index] == NULL) |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 12370 | cap[h->index] = pool_alloc(h->pool); |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12371 | |
| 12372 | if (cap[h->index] == NULL) /* no more capture memory */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12373 | return ACT_RET_CONT; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12374 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12375 | len = key->data.u.str.len; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12376 | if (len > h->len) |
| 12377 | len = h->len; |
| 12378 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12379 | memcpy(cap[h->index], key->data.u.str.str, len); |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12380 | cap[h->index][len] = 0; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12381 | return ACT_RET_CONT; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12382 | } |
| 12383 | |
Christopher Faulet | 29730ba | 2017-09-18 15:26:32 +0200 | [diff] [blame] | 12384 | /* Check an "http-response capture" action. |
| 12385 | * |
| 12386 | * The function returns 1 in success case, otherwise, it returns 0 and err is |
| 12387 | * filled. |
| 12388 | */ |
| 12389 | int check_http_res_capture(struct act_rule *rule, struct proxy *px, char **err) |
| 12390 | { |
Christopher Faulet | fd608dd | 2017-12-04 09:45:15 +0100 | [diff] [blame] | 12391 | if (rule->action_ptr != http_action_res_capture_by_id) |
| 12392 | return 1; |
| 12393 | |
Christopher Faulet | 29730ba | 2017-09-18 15:26:32 +0200 | [diff] [blame] | 12394 | if (rule->arg.capid.idx >= px->nb_rsp_cap) { |
| 12395 | memprintf(err, "unable to find capture id '%d' referenced by http-response capture rule", |
| 12396 | rule->arg.capid.idx); |
| 12397 | return 0; |
| 12398 | } |
| 12399 | |
| 12400 | return 1; |
| 12401 | } |
| 12402 | |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12403 | /* parse an "http-response capture" action. It takes a single argument which is |
| 12404 | * a sample fetch expression. It stores the expression into arg->act.p[0] and |
| 12405 | * 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] | 12406 | * 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] | 12407 | */ |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12408 | enum act_parse_ret parse_http_res_capture(const char **args, int *orig_arg, struct proxy *px, |
| 12409 | struct act_rule *rule, char **err) |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12410 | { |
| 12411 | struct sample_expr *expr; |
| 12412 | int cur_arg; |
| 12413 | int id; |
| 12414 | char *error; |
| 12415 | |
| 12416 | for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++) |
| 12417 | if (strcmp(args[cur_arg], "if") == 0 || |
| 12418 | strcmp(args[cur_arg], "unless") == 0) |
| 12419 | break; |
| 12420 | |
| 12421 | if (cur_arg < *orig_arg + 3) { |
Willy Tarreau | 29bdb1c | 2016-06-24 15:36:34 +0200 | [diff] [blame] | 12422 | memprintf(err, "expects <expression> id <idx>"); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12423 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12424 | } |
| 12425 | |
| 12426 | cur_arg = *orig_arg; |
| 12427 | expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args); |
| 12428 | if (!expr) |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12429 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12430 | |
| 12431 | if (!(expr->fetch->val & SMP_VAL_FE_HRS_HDR)) { |
| 12432 | memprintf(err, |
| 12433 | "fetch method '%s' extracts information from '%s', none of which is available here", |
| 12434 | args[cur_arg-1], sample_src_names(expr->fetch->use)); |
| 12435 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12436 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12437 | } |
| 12438 | |
| 12439 | if (!args[cur_arg] || !*args[cur_arg]) { |
Willy Tarreau | 29bdb1c | 2016-06-24 15:36:34 +0200 | [diff] [blame] | 12440 | memprintf(err, "expects 'id'"); |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12441 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12442 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12443 | } |
| 12444 | |
| 12445 | if (strcmp(args[cur_arg], "id") != 0) { |
| 12446 | memprintf(err, "expects 'id', found '%s'", args[cur_arg]); |
| 12447 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12448 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12449 | } |
| 12450 | |
| 12451 | cur_arg++; |
| 12452 | |
| 12453 | if (!args[cur_arg]) { |
| 12454 | memprintf(err, "missing id value"); |
| 12455 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12456 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12457 | } |
| 12458 | |
| 12459 | id = strtol(args[cur_arg], &error, 10); |
| 12460 | if (*error != '\0') { |
| 12461 | memprintf(err, "cannot parse id '%s'", args[cur_arg]); |
| 12462 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12463 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12464 | } |
| 12465 | cur_arg++; |
| 12466 | |
Olivier Houchard | fbc74e8 | 2017-11-24 16:54:05 +0100 | [diff] [blame] | 12467 | px->conf.args.ctx = ARGC_CAP; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12468 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 12469 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12470 | rule->action_ptr = http_action_res_capture_by_id; |
Christopher Faulet | 29730ba | 2017-09-18 15:26:32 +0200 | [diff] [blame] | 12471 | rule->check_ptr = check_http_res_capture; |
Thierry FOURNIER | e209797 | 2015-07-31 08:56:35 +0200 | [diff] [blame] | 12472 | rule->arg.capid.expr = expr; |
| 12473 | rule->arg.capid.idx = id; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12474 | |
| 12475 | *orig_arg = cur_arg; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12476 | return ACT_RET_PRS_OK; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12477 | } |
| 12478 | |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 12479 | /* |
| 12480 | * Return the struct http_req_action_kw associated to a keyword. |
| 12481 | */ |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 12482 | struct action_kw *action_http_req_custom(const char *kw) |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 12483 | { |
Thierry FOURNIER | 322a124 | 2015-08-19 09:07:47 +0200 | [diff] [blame] | 12484 | return action_lookup(&http_req_keywords.list, kw); |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 12485 | } |
| 12486 | |
| 12487 | /* |
| 12488 | * Return the struct http_res_action_kw associated to a keyword. |
| 12489 | */ |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 12490 | struct action_kw *action_http_res_custom(const char *kw) |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 12491 | { |
Thierry FOURNIER | 322a124 | 2015-08-19 09:07:47 +0200 | [diff] [blame] | 12492 | return action_lookup(&http_res_keywords.list, kw); |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 12493 | } |
| 12494 | |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12495 | |
| 12496 | /* "show errors" handler for the CLI. Returns 0 if wants to continue, 1 to stop |
| 12497 | * now. |
| 12498 | */ |
| 12499 | static int cli_parse_show_errors(char **args, struct appctx *appctx, void *private) |
| 12500 | { |
| 12501 | if (!cli_has_level(appctx, ACCESS_LVL_OPER)) |
| 12502 | return 1; |
| 12503 | |
Willy Tarreau | 234ba2d | 2016-11-25 08:39:10 +0100 | [diff] [blame] | 12504 | if (*args[2]) { |
| 12505 | struct proxy *px; |
| 12506 | |
| 12507 | px = proxy_find_by_name(args[2], 0, 0); |
| 12508 | if (px) |
| 12509 | appctx->ctx.errors.iid = px->uuid; |
| 12510 | else |
| 12511 | appctx->ctx.errors.iid = atoi(args[2]); |
| 12512 | |
| 12513 | if (!appctx->ctx.errors.iid) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 12514 | appctx->ctx.cli.severity = LOG_ERR; |
Willy Tarreau | 234ba2d | 2016-11-25 08:39:10 +0100 | [diff] [blame] | 12515 | appctx->ctx.cli.msg = "No such proxy.\n"; |
| 12516 | appctx->st0 = CLI_ST_PRINT; |
| 12517 | return 1; |
| 12518 | } |
| 12519 | } |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12520 | else |
Willy Tarreau | 234ba2d | 2016-11-25 08:39:10 +0100 | [diff] [blame] | 12521 | appctx->ctx.errors.iid = -1; // dump all proxies |
| 12522 | |
Willy Tarreau | 35069f8 | 2016-11-25 09:16:37 +0100 | [diff] [blame] | 12523 | appctx->ctx.errors.flag = 0; |
| 12524 | if (strcmp(args[3], "request") == 0) |
| 12525 | appctx->ctx.errors.flag |= 4; // ignore response |
| 12526 | else if (strcmp(args[3], "response") == 0) |
| 12527 | appctx->ctx.errors.flag |= 2; // ignore request |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12528 | appctx->ctx.errors.px = NULL; |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12529 | return 0; |
| 12530 | } |
| 12531 | |
| 12532 | /* This function dumps all captured errors onto the stream interface's |
| 12533 | * read buffer. It returns 0 if the output buffer is full and it needs |
| 12534 | * to be called again, otherwise non-zero. |
| 12535 | */ |
| 12536 | static int cli_io_handler_show_errors(struct appctx *appctx) |
| 12537 | { |
| 12538 | struct stream_interface *si = appctx->owner; |
| 12539 | extern const char *monthname[12]; |
| 12540 | |
| 12541 | if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) |
| 12542 | return 1; |
| 12543 | |
| 12544 | chunk_reset(&trash); |
| 12545 | |
| 12546 | if (!appctx->ctx.errors.px) { |
| 12547 | /* the function had not been called yet, let's prepare the |
| 12548 | * buffer for a response. |
| 12549 | */ |
| 12550 | struct tm tm; |
| 12551 | |
| 12552 | get_localtime(date.tv_sec, &tm); |
| 12553 | chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n", |
| 12554 | tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900, |
| 12555 | tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000), |
| 12556 | error_snapshot_id); |
| 12557 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 12558 | if (ci_putchk(si_ic(si), &trash) == -1) { |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12559 | /* Socket buffer full. Let's try again later from the same point */ |
| 12560 | si_applet_cant_put(si); |
| 12561 | return 0; |
| 12562 | } |
| 12563 | |
Olivier Houchard | fbc74e8 | 2017-11-24 16:54:05 +0100 | [diff] [blame] | 12564 | appctx->ctx.errors.px = proxies_list; |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12565 | appctx->ctx.errors.bol = 0; |
| 12566 | appctx->ctx.errors.ptr = -1; |
| 12567 | } |
| 12568 | |
| 12569 | /* we have two inner loops here, one for the proxy, the other one for |
| 12570 | * the buffer. |
| 12571 | */ |
| 12572 | while (appctx->ctx.errors.px) { |
| 12573 | struct error_snapshot *es; |
| 12574 | |
Willy Tarreau | 35069f8 | 2016-11-25 09:16:37 +0100 | [diff] [blame] | 12575 | if ((appctx->ctx.errors.flag & 1) == 0) { |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12576 | es = &appctx->ctx.errors.px->invalid_req; |
Willy Tarreau | 35069f8 | 2016-11-25 09:16:37 +0100 | [diff] [blame] | 12577 | if (appctx->ctx.errors.flag & 2) // skip req |
| 12578 | goto next; |
| 12579 | } |
| 12580 | else { |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12581 | es = &appctx->ctx.errors.px->invalid_rep; |
Willy Tarreau | 35069f8 | 2016-11-25 09:16:37 +0100 | [diff] [blame] | 12582 | if (appctx->ctx.errors.flag & 4) // skip resp |
| 12583 | goto next; |
| 12584 | } |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12585 | |
| 12586 | if (!es->when.tv_sec) |
| 12587 | goto next; |
| 12588 | |
| 12589 | if (appctx->ctx.errors.iid >= 0 && |
| 12590 | appctx->ctx.errors.px->uuid != appctx->ctx.errors.iid && |
| 12591 | es->oe->uuid != appctx->ctx.errors.iid) |
| 12592 | goto next; |
| 12593 | |
| 12594 | if (appctx->ctx.errors.ptr < 0) { |
| 12595 | /* just print headers now */ |
| 12596 | |
| 12597 | char pn[INET6_ADDRSTRLEN]; |
| 12598 | struct tm tm; |
| 12599 | int port; |
| 12600 | |
| 12601 | get_localtime(es->when.tv_sec, &tm); |
| 12602 | chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]", |
| 12603 | tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900, |
| 12604 | tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000)); |
| 12605 | |
| 12606 | switch (addr_to_str(&es->src, pn, sizeof(pn))) { |
| 12607 | case AF_INET: |
| 12608 | case AF_INET6: |
| 12609 | port = get_host_port(&es->src); |
| 12610 | break; |
| 12611 | default: |
| 12612 | port = 0; |
| 12613 | } |
| 12614 | |
Willy Tarreau | 35069f8 | 2016-11-25 09:16:37 +0100 | [diff] [blame] | 12615 | switch (appctx->ctx.errors.flag & 1) { |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12616 | case 0: |
| 12617 | chunk_appendf(&trash, |
| 12618 | " frontend %s (#%d): invalid request\n" |
| 12619 | " backend %s (#%d)", |
| 12620 | appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid, |
| 12621 | (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>", |
| 12622 | (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1); |
| 12623 | break; |
| 12624 | case 1: |
| 12625 | chunk_appendf(&trash, |
| 12626 | " backend %s (#%d): invalid response\n" |
| 12627 | " frontend %s (#%d)", |
| 12628 | appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid, |
| 12629 | es->oe->id, es->oe->uuid); |
| 12630 | break; |
| 12631 | } |
| 12632 | |
| 12633 | chunk_appendf(&trash, |
| 12634 | ", server %s (#%d), event #%u\n" |
| 12635 | " src %s:%d, session #%d, session flags 0x%08x\n" |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 12636 | " 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] | 12637 | " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n" |
| 12638 | " buffer flags 0x%08x, out %d bytes, total %lld bytes\n" |
| 12639 | " pending %d bytes, wrapping at %d, error at position %d:\n \n", |
| 12640 | es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1, |
| 12641 | es->ev_id, |
| 12642 | pn, port, es->sid, es->s_flags, |
Willy Tarreau | 0da5b3b | 2017-09-21 09:30:46 +0200 | [diff] [blame] | 12643 | 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] | 12644 | es->m_clen, es->m_blen, |
| 12645 | es->b_flags, es->b_out, es->b_tot, |
| 12646 | es->len, es->b_wrap, es->pos); |
| 12647 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 12648 | if (ci_putchk(si_ic(si), &trash) == -1) { |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12649 | /* Socket buffer full. Let's try again later from the same point */ |
| 12650 | si_applet_cant_put(si); |
| 12651 | return 0; |
| 12652 | } |
| 12653 | appctx->ctx.errors.ptr = 0; |
| 12654 | appctx->ctx.errors.sid = es->sid; |
| 12655 | } |
| 12656 | |
| 12657 | if (appctx->ctx.errors.sid != es->sid) { |
| 12658 | /* the snapshot changed while we were dumping it */ |
| 12659 | chunk_appendf(&trash, |
| 12660 | " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n"); |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 12661 | if (ci_putchk(si_ic(si), &trash) == -1) { |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12662 | si_applet_cant_put(si); |
| 12663 | return 0; |
| 12664 | } |
| 12665 | goto next; |
| 12666 | } |
| 12667 | |
| 12668 | /* OK, ptr >= 0, so we have to dump the current line */ |
| 12669 | while (es->buf && appctx->ctx.errors.ptr < es->len && appctx->ctx.errors.ptr < global.tune.bufsize) { |
| 12670 | int newptr; |
| 12671 | int newline; |
| 12672 | |
| 12673 | newline = appctx->ctx.errors.bol; |
| 12674 | newptr = dump_text_line(&trash, es->buf, global.tune.bufsize, es->len, &newline, appctx->ctx.errors.ptr); |
| 12675 | if (newptr == appctx->ctx.errors.ptr) |
| 12676 | return 0; |
| 12677 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 12678 | if (ci_putchk(si_ic(si), &trash) == -1) { |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12679 | /* Socket buffer full. Let's try again later from the same point */ |
| 12680 | si_applet_cant_put(si); |
| 12681 | return 0; |
| 12682 | } |
| 12683 | appctx->ctx.errors.ptr = newptr; |
| 12684 | appctx->ctx.errors.bol = newline; |
| 12685 | }; |
| 12686 | next: |
| 12687 | appctx->ctx.errors.bol = 0; |
| 12688 | appctx->ctx.errors.ptr = -1; |
Willy Tarreau | 35069f8 | 2016-11-25 09:16:37 +0100 | [diff] [blame] | 12689 | appctx->ctx.errors.flag ^= 1; |
| 12690 | if (!(appctx->ctx.errors.flag & 1)) |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12691 | appctx->ctx.errors.px = appctx->ctx.errors.px->next; |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12692 | } |
| 12693 | |
| 12694 | /* dump complete */ |
| 12695 | return 1; |
| 12696 | } |
| 12697 | |
| 12698 | /* register cli keywords */ |
| 12699 | static struct cli_kw_list cli_kws = {{ },{ |
| 12700 | { { "show", "errors", NULL }, |
| 12701 | "show errors : report last request and response errors for each proxy", |
| 12702 | cli_parse_show_errors, cli_io_handler_show_errors, NULL, |
| 12703 | }, |
| 12704 | {{},} |
| 12705 | }}; |
| 12706 | |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 12707 | /************************************************************************/ |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12708 | /* All supported ACL keywords must be declared here. */ |
| 12709 | /************************************************************************/ |
| 12710 | |
| 12711 | /* Note: must not be declared <const> as its list will be overwritten. |
| 12712 | * Please take care of keeping this list alphabetically sorted. |
| 12713 | */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 12714 | static struct acl_kw_list acl_kws = {ILH, { |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12715 | { "base", "base", PAT_MATCH_STR }, |
| 12716 | { "base_beg", "base", PAT_MATCH_BEG }, |
| 12717 | { "base_dir", "base", PAT_MATCH_DIR }, |
| 12718 | { "base_dom", "base", PAT_MATCH_DOM }, |
| 12719 | { "base_end", "base", PAT_MATCH_END }, |
| 12720 | { "base_len", "base", PAT_MATCH_LEN }, |
| 12721 | { "base_reg", "base", PAT_MATCH_REG }, |
| 12722 | { "base_sub", "base", PAT_MATCH_SUB }, |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 12723 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12724 | { "cook", "req.cook", PAT_MATCH_STR }, |
| 12725 | { "cook_beg", "req.cook", PAT_MATCH_BEG }, |
| 12726 | { "cook_dir", "req.cook", PAT_MATCH_DIR }, |
| 12727 | { "cook_dom", "req.cook", PAT_MATCH_DOM }, |
| 12728 | { "cook_end", "req.cook", PAT_MATCH_END }, |
| 12729 | { "cook_len", "req.cook", PAT_MATCH_LEN }, |
| 12730 | { "cook_reg", "req.cook", PAT_MATCH_REG }, |
| 12731 | { "cook_sub", "req.cook", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12732 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12733 | { "hdr", "req.hdr", PAT_MATCH_STR }, |
| 12734 | { "hdr_beg", "req.hdr", PAT_MATCH_BEG }, |
| 12735 | { "hdr_dir", "req.hdr", PAT_MATCH_DIR }, |
| 12736 | { "hdr_dom", "req.hdr", PAT_MATCH_DOM }, |
| 12737 | { "hdr_end", "req.hdr", PAT_MATCH_END }, |
| 12738 | { "hdr_len", "req.hdr", PAT_MATCH_LEN }, |
| 12739 | { "hdr_reg", "req.hdr", PAT_MATCH_REG }, |
| 12740 | { "hdr_sub", "req.hdr", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12741 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12742 | /* these two declarations uses strings with list storage (in place |
| 12743 | * of tree storage). The basic match is PAT_MATCH_STR, but the indexation |
| 12744 | * and delete functions are relative to the list management. The parse |
| 12745 | * and match method are related to the corresponding fetch methods. This |
| 12746 | * is very particular ACL declaration mode. |
| 12747 | */ |
| 12748 | { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth }, |
| 12749 | { "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] | 12750 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12751 | { "path", "path", PAT_MATCH_STR }, |
| 12752 | { "path_beg", "path", PAT_MATCH_BEG }, |
| 12753 | { "path_dir", "path", PAT_MATCH_DIR }, |
| 12754 | { "path_dom", "path", PAT_MATCH_DOM }, |
| 12755 | { "path_end", "path", PAT_MATCH_END }, |
| 12756 | { "path_len", "path", PAT_MATCH_LEN }, |
| 12757 | { "path_reg", "path", PAT_MATCH_REG }, |
| 12758 | { "path_sub", "path", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12759 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12760 | { "req_ver", "req.ver", PAT_MATCH_STR }, |
| 12761 | { "resp_ver", "res.ver", PAT_MATCH_STR }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12762 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12763 | { "scook", "res.cook", PAT_MATCH_STR }, |
| 12764 | { "scook_beg", "res.cook", PAT_MATCH_BEG }, |
| 12765 | { "scook_dir", "res.cook", PAT_MATCH_DIR }, |
| 12766 | { "scook_dom", "res.cook", PAT_MATCH_DOM }, |
| 12767 | { "scook_end", "res.cook", PAT_MATCH_END }, |
| 12768 | { "scook_len", "res.cook", PAT_MATCH_LEN }, |
| 12769 | { "scook_reg", "res.cook", PAT_MATCH_REG }, |
| 12770 | { "scook_sub", "res.cook", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12771 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12772 | { "shdr", "res.hdr", PAT_MATCH_STR }, |
| 12773 | { "shdr_beg", "res.hdr", PAT_MATCH_BEG }, |
| 12774 | { "shdr_dir", "res.hdr", PAT_MATCH_DIR }, |
| 12775 | { "shdr_dom", "res.hdr", PAT_MATCH_DOM }, |
| 12776 | { "shdr_end", "res.hdr", PAT_MATCH_END }, |
| 12777 | { "shdr_len", "res.hdr", PAT_MATCH_LEN }, |
| 12778 | { "shdr_reg", "res.hdr", PAT_MATCH_REG }, |
| 12779 | { "shdr_sub", "res.hdr", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12780 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12781 | { "url", "url", PAT_MATCH_STR }, |
| 12782 | { "url_beg", "url", PAT_MATCH_BEG }, |
| 12783 | { "url_dir", "url", PAT_MATCH_DIR }, |
| 12784 | { "url_dom", "url", PAT_MATCH_DOM }, |
| 12785 | { "url_end", "url", PAT_MATCH_END }, |
| 12786 | { "url_len", "url", PAT_MATCH_LEN }, |
| 12787 | { "url_reg", "url", PAT_MATCH_REG }, |
| 12788 | { "url_sub", "url", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12789 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12790 | { "urlp", "urlp", PAT_MATCH_STR }, |
| 12791 | { "urlp_beg", "urlp", PAT_MATCH_BEG }, |
| 12792 | { "urlp_dir", "urlp", PAT_MATCH_DIR }, |
| 12793 | { "urlp_dom", "urlp", PAT_MATCH_DOM }, |
| 12794 | { "urlp_end", "urlp", PAT_MATCH_END }, |
| 12795 | { "urlp_len", "urlp", PAT_MATCH_LEN }, |
| 12796 | { "urlp_reg", "urlp", PAT_MATCH_REG }, |
| 12797 | { "urlp_sub", "urlp", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12798 | |
Willy Tarreau | 8ed669b | 2013-01-11 15:49:37 +0100 | [diff] [blame] | 12799 | { /* END */ }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12800 | }}; |
| 12801 | |
| 12802 | /************************************************************************/ |
| 12803 | /* All supported pattern keywords must be declared here. */ |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 12804 | /************************************************************************/ |
| 12805 | /* Note: must not be declared <const> as its list will be overwritten */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 12806 | static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, { |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12807 | { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 12808 | { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 12809 | { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV }, |
| 12810 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 12811 | /* capture are allocated and are permanent in the stream */ |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 12812 | { "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] | 12813 | |
| 12814 | /* retrieve these captures from the HTTP logs */ |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 12815 | { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP }, |
| 12816 | { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP }, |
| 12817 | { "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] | 12818 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 12819 | { "capture.res.hdr", smp_fetch_capture_header_res, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP }, |
| 12820 | { "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] | 12821 | |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 12822 | /* cookie is valid in both directions (eg: for "stick ...") but cook* |
| 12823 | * are only here to match the ACL's name, are request-only and are used |
| 12824 | * for ACL compatibility only. |
| 12825 | */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12826 | { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 12827 | { "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] | 12828 | { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 12829 | { "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] | 12830 | |
| 12831 | /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are |
| 12832 | * only here to match the ACL's name, are request-only and are used for |
| 12833 | * ACL compatibility only. |
| 12834 | */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12835 | { "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] | 12836 | { "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] | 12837 | { "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] | 12838 | { "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] | 12839 | |
Willy Tarreau | 0a0daec | 2013-04-02 22:44:58 +0200 | [diff] [blame] | 12840 | { "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] | 12841 | { "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] | 12842 | { "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] | 12843 | { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP }, |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12844 | { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 12845 | { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 12846 | |
| 12847 | /* HTTP protocol on the request path */ |
| 12848 | { "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] | 12849 | { "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] | 12850 | |
| 12851 | /* HTTP version on the request path */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12852 | { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 12853 | { "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] | 12854 | |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 12855 | { "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] | 12856 | { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 12857 | { "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] | 12858 | { "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] | 12859 | |
Thierry FOURNIER | d7d8881 | 2017-04-19 15:15:14 +0200 | [diff] [blame] | 12860 | { "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] | 12861 | { "req.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV }, |
| 12862 | |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 12863 | /* HTTP version on the response path */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12864 | { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV }, |
| 12865 | { "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] | 12866 | |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 12867 | /* 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] | 12868 | { "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] | 12869 | { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 12870 | { "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] | 12871 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12872 | { "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] | 12873 | { "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] | 12874 | { "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] | 12875 | { "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] | 12876 | { "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] | 12877 | { "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] | 12878 | { "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] | 12879 | |
| 12880 | /* 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] | 12881 | { "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] | 12882 | { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV }, |
| 12883 | { "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] | 12884 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12885 | { "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] | 12886 | { "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] | 12887 | { "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] | 12888 | { "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] | 12889 | { "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] | 12890 | { "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] | 12891 | { "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] | 12892 | |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 12893 | /* 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] | 12894 | { "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] | 12895 | { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV }, |
| 12896 | { "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] | 12897 | { "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] | 12898 | |
| 12899 | /* 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] | 12900 | { "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] | 12901 | { "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] | 12902 | { "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] | 12903 | { "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] | 12904 | |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 12905 | { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP }, |
Thierry Fournier | 0e00dca | 2016-04-07 15:47:40 +0200 | [diff] [blame] | 12906 | { "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] | 12907 | { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 12908 | { "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] | 12909 | { "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] | 12910 | { "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] | 12911 | { "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] | 12912 | { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 12913 | { "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] | 12914 | { "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] | 12915 | { /* END */ }, |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 12916 | }}; |
| 12917 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 12918 | |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12919 | /************************************************************************/ |
| 12920 | /* All supported converter keywords must be declared here. */ |
| 12921 | /************************************************************************/ |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 12922 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 12923 | static struct sample_conv_kw_list sample_conv_kws = {ILH, { |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 12924 | { "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] | 12925 | { "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] | 12926 | { "capture-req", smp_conv_req_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR}, |
| 12927 | { "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] | 12928 | { "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] | 12929 | { NULL, NULL, 0, 0, 0 }, |
| 12930 | }}; |
| 12931 | |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 12932 | |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12933 | /************************************************************************/ |
| 12934 | /* All supported http-request action keywords must be declared here. */ |
| 12935 | /************************************************************************/ |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 12936 | struct action_kw_list http_req_actions = { |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12937 | .kw = { |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12938 | { "capture", parse_http_req_capture }, |
Willy Tarreau | 53275e8 | 2017-11-24 07:52:01 +0100 | [diff] [blame] | 12939 | { "reject", parse_http_action_reject }, |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12940 | { "set-method", parse_set_req_line }, |
| 12941 | { "set-path", parse_set_req_line }, |
| 12942 | { "set-query", parse_set_req_line }, |
| 12943 | { "set-uri", parse_set_req_line }, |
Willy Tarreau | cb703b0 | 2015-04-03 09:52:01 +0200 | [diff] [blame] | 12944 | { NULL, NULL } |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12945 | } |
| 12946 | }; |
| 12947 | |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 12948 | struct action_kw_list http_res_actions = { |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12949 | .kw = { |
| 12950 | { "capture", parse_http_res_capture }, |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 12951 | { "set-status", parse_http_set_status }, |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12952 | { NULL, NULL } |
| 12953 | } |
| 12954 | }; |
| 12955 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 12956 | __attribute__((constructor)) |
| 12957 | static void __http_protocol_init(void) |
| 12958 | { |
| 12959 | acl_register_keywords(&acl_kws); |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 12960 | sample_register_fetches(&sample_fetch_keywords); |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 12961 | sample_register_convs(&sample_conv_kws); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12962 | http_req_keywords_register(&http_req_actions); |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12963 | http_res_keywords_register(&http_res_actions); |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12964 | cli_register_kw(&cli_kws); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 12965 | } |
| 12966 | |
| 12967 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 12968 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 12969 | * Local variables: |
| 12970 | * c-indent-level: 8 |
| 12971 | * c-basic-offset: 8 |
| 12972 | * End: |
| 12973 | */ |