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> |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 29 | #include <common/chunk.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 30 | #include <common/compat.h> |
| 31 | #include <common/config.h> |
Willy Tarreau | a4cd1f5 | 2006-12-16 19:57:26 +0100 | [diff] [blame] | 32 | #include <common/debug.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 33 | #include <common/memory.h> |
| 34 | #include <common/mini-clist.h> |
| 35 | #include <common/standard.h> |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 36 | #include <common/ticks.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 37 | #include <common/time.h> |
| 38 | #include <common/uri_auth.h> |
| 39 | #include <common/version.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 40 | |
| 41 | #include <types/capture.h> |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 42 | #include <types/cli.h> |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 43 | #include <types/filters.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 44 | #include <types/global.h> |
William Lallemand | 71bd11a | 2017-11-20 19:13:14 +0100 | [diff] [blame] | 45 | #include <types/cache.h> |
William Lallemand | 9ed6203 | 2016-11-21 17:49:11 +0100 | [diff] [blame] | 46 | #include <types/stats.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 47 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 48 | #include <proto/acl.h> |
Thierry FOURNIER | 322a124 | 2015-08-19 09:07:47 +0200 | [diff] [blame] | 49 | #include <proto/action.h> |
Willy Tarreau | 61612d4 | 2012-04-19 18:42:05 +0200 | [diff] [blame] | 50 | #include <proto/arg.h> |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 51 | #include <proto/auth.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 52 | #include <proto/backend.h> |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 53 | #include <proto/channel.h> |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 54 | #include <proto/checks.h> |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 55 | #include <proto/cli.h> |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 56 | #include <proto/compression.h> |
William Lallemand | 9ed6203 | 2016-11-21 17:49:11 +0100 | [diff] [blame] | 57 | #include <proto/stats.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 58 | #include <proto/fd.h> |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 59 | #include <proto/filters.h> |
Willy Tarreau | 03fa5df | 2010-05-24 21:02:37 +0200 | [diff] [blame] | 60 | #include <proto/frontend.h> |
Willy Tarreau | 0da5b3b | 2017-09-21 09:30:46 +0200 | [diff] [blame] | 61 | #include <proto/h1.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 62 | #include <proto/log.h> |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 63 | #include <proto/hdr_idx.h> |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 64 | #include <proto/pattern.h> |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 65 | #include <proto/proto_tcp.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 66 | #include <proto/proto_http.h> |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 67 | #include <proto/proxy.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 68 | #include <proto/queue.h> |
Willy Tarreau | cd3b094 | 2012-04-27 21:52:18 +0200 | [diff] [blame] | 69 | #include <proto/sample.h> |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 70 | #include <proto/server.h> |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 71 | #include <proto/stream.h> |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 72 | #include <proto/stream_interface.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 73 | #include <proto/task.h> |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 74 | #include <proto/pattern.h> |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 75 | #include <proto/vars.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 76 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 77 | const char HTTP_100[] = |
| 78 | "HTTP/1.1 100 Continue\r\n\r\n"; |
| 79 | |
| 80 | const struct chunk http_100_chunk = { |
| 81 | .str = (char *)&HTTP_100, |
| 82 | .len = sizeof(HTTP_100)-1 |
| 83 | }; |
| 84 | |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 85 | /* Warning: no "connection" header is provided with the 3xx messages below */ |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 86 | const char *HTTP_301 = |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 87 | "HTTP/1.1 301 Moved Permanently\r\n" |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 88 | "Content-length: 0\r\n" |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 89 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 90 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 91 | const char *HTTP_302 = |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 92 | "HTTP/1.1 302 Found\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 93 | "Cache-Control: no-cache\r\n" |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 94 | "Content-length: 0\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 95 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 96 | |
| 97 | /* same as 302 except that the browser MUST retry with the GET method */ |
| 98 | const char *HTTP_303 = |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 99 | "HTTP/1.1 303 See Other\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 100 | "Cache-Control: no-cache\r\n" |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 101 | "Content-length: 0\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 102 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 103 | |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 104 | |
| 105 | /* same as 302 except that the browser MUST retry with the same method */ |
| 106 | const char *HTTP_307 = |
| 107 | "HTTP/1.1 307 Temporary Redirect\r\n" |
| 108 | "Cache-Control: no-cache\r\n" |
| 109 | "Content-length: 0\r\n" |
| 110 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 111 | |
| 112 | /* same as 301 except that the browser MUST retry with the same method */ |
| 113 | const char *HTTP_308 = |
| 114 | "HTTP/1.1 308 Permanent Redirect\r\n" |
| 115 | "Content-length: 0\r\n" |
| 116 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 117 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 118 | /* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */ |
| 119 | const char *HTTP_401_fmt = |
| 120 | "HTTP/1.0 401 Unauthorized\r\n" |
| 121 | "Cache-Control: no-cache\r\n" |
| 122 | "Connection: close\r\n" |
Willy Tarreau | 791d66d | 2006-07-08 16:53:38 +0200 | [diff] [blame] | 123 | "Content-Type: text/html\r\n" |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 124 | "WWW-Authenticate: Basic realm=\"%s\"\r\n" |
| 125 | "\r\n" |
| 126 | "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n"; |
| 127 | |
Willy Tarreau | 844a7e7 | 2010-01-31 21:46:18 +0100 | [diff] [blame] | 128 | const char *HTTP_407_fmt = |
| 129 | "HTTP/1.0 407 Unauthorized\r\n" |
| 130 | "Cache-Control: no-cache\r\n" |
| 131 | "Connection: close\r\n" |
| 132 | "Content-Type: text/html\r\n" |
| 133 | "Proxy-Authenticate: Basic realm=\"%s\"\r\n" |
| 134 | "\r\n" |
Godbach | 1f1fae6 | 2014-12-17 16:32:05 +0800 | [diff] [blame] | 135 | "<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] | 136 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 137 | |
| 138 | const int http_err_codes[HTTP_ERR_SIZE] = { |
Willy Tarreau | ae94d4d | 2011-05-11 16:28:49 +0200 | [diff] [blame] | 139 | [HTTP_ERR_200] = 200, /* used by "monitor-uri" */ |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 140 | [HTTP_ERR_400] = 400, |
| 141 | [HTTP_ERR_403] = 403, |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 142 | [HTTP_ERR_405] = 405, |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 143 | [HTTP_ERR_408] = 408, |
Olivier Houchard | 51a76d8 | 2017-10-02 16:12:07 +0200 | [diff] [blame] | 144 | [HTTP_ERR_425] = 425, |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 145 | [HTTP_ERR_429] = 429, |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 146 | [HTTP_ERR_500] = 500, |
| 147 | [HTTP_ERR_502] = 502, |
| 148 | [HTTP_ERR_503] = 503, |
| 149 | [HTTP_ERR_504] = 504, |
| 150 | }; |
| 151 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 152 | static const char *http_err_msgs[HTTP_ERR_SIZE] = { |
Willy Tarreau | ae94d4d | 2011-05-11 16:28:49 +0200 | [diff] [blame] | 153 | [HTTP_ERR_200] = |
| 154 | "HTTP/1.0 200 OK\r\n" |
| 155 | "Cache-Control: no-cache\r\n" |
| 156 | "Connection: close\r\n" |
| 157 | "Content-Type: text/html\r\n" |
| 158 | "\r\n" |
| 159 | "<html><body><h1>200 OK</h1>\nService ready.\n</body></html>\n", |
| 160 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 161 | [HTTP_ERR_400] = |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 162 | "HTTP/1.0 400 Bad request\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 163 | "Cache-Control: no-cache\r\n" |
| 164 | "Connection: close\r\n" |
| 165 | "Content-Type: text/html\r\n" |
| 166 | "\r\n" |
| 167 | "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n", |
| 168 | |
| 169 | [HTTP_ERR_403] = |
| 170 | "HTTP/1.0 403 Forbidden\r\n" |
| 171 | "Cache-Control: no-cache\r\n" |
| 172 | "Connection: close\r\n" |
| 173 | "Content-Type: text/html\r\n" |
| 174 | "\r\n" |
| 175 | "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n", |
| 176 | |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 177 | [HTTP_ERR_405] = |
| 178 | "HTTP/1.0 405 Method Not Allowed\r\n" |
| 179 | "Cache-Control: no-cache\r\n" |
| 180 | "Connection: close\r\n" |
| 181 | "Content-Type: text/html\r\n" |
| 182 | "\r\n" |
| 183 | "<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", |
| 184 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 185 | [HTTP_ERR_408] = |
| 186 | "HTTP/1.0 408 Request Time-out\r\n" |
| 187 | "Cache-Control: no-cache\r\n" |
| 188 | "Connection: close\r\n" |
| 189 | "Content-Type: text/html\r\n" |
| 190 | "\r\n" |
| 191 | "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n", |
| 192 | |
Olivier Houchard | 51a76d8 | 2017-10-02 16:12:07 +0200 | [diff] [blame] | 193 | [HTTP_ERR_425] = |
| 194 | "HTTP/1.0 425 Too Early\r\n" |
| 195 | "Cache-Control: no-cache\r\n" |
| 196 | "Connection: close\r\n" |
| 197 | "Content-Type: text/html\r\n" |
| 198 | "\r\n" |
| 199 | "<html><body><h1>425 Too Early</h1>\nYour browser sent early data.\n</body></html>\n", |
| 200 | |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 201 | [HTTP_ERR_429] = |
| 202 | "HTTP/1.0 429 Too Many Requests\r\n" |
| 203 | "Cache-Control: no-cache\r\n" |
| 204 | "Connection: close\r\n" |
| 205 | "Content-Type: text/html\r\n" |
| 206 | "\r\n" |
| 207 | "<html><body><h1>429 Too Many Requests</h1>\nYou have sent too many requests in a given amount of time.\n</body></html>\n", |
| 208 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 209 | [HTTP_ERR_500] = |
Jarno Huuskonen | 16ad94a | 2017-01-09 14:17:10 +0200 | [diff] [blame] | 210 | "HTTP/1.0 500 Internal Server Error\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 211 | "Cache-Control: no-cache\r\n" |
| 212 | "Connection: close\r\n" |
| 213 | "Content-Type: text/html\r\n" |
| 214 | "\r\n" |
Jarno Huuskonen | 16ad94a | 2017-01-09 14:17:10 +0200 | [diff] [blame] | 215 | "<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] | 216 | |
| 217 | [HTTP_ERR_502] = |
| 218 | "HTTP/1.0 502 Bad Gateway\r\n" |
| 219 | "Cache-Control: no-cache\r\n" |
| 220 | "Connection: close\r\n" |
| 221 | "Content-Type: text/html\r\n" |
| 222 | "\r\n" |
| 223 | "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n", |
| 224 | |
| 225 | [HTTP_ERR_503] = |
| 226 | "HTTP/1.0 503 Service Unavailable\r\n" |
| 227 | "Cache-Control: no-cache\r\n" |
| 228 | "Connection: close\r\n" |
| 229 | "Content-Type: text/html\r\n" |
| 230 | "\r\n" |
| 231 | "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n", |
| 232 | |
| 233 | [HTTP_ERR_504] = |
| 234 | "HTTP/1.0 504 Gateway Time-out\r\n" |
| 235 | "Cache-Control: no-cache\r\n" |
| 236 | "Connection: close\r\n" |
| 237 | "Content-Type: text/html\r\n" |
| 238 | "\r\n" |
| 239 | "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n", |
| 240 | |
| 241 | }; |
| 242 | |
Cyril Bonté | 19979e1 | 2012-04-04 12:57:21 +0200 | [diff] [blame] | 243 | /* status codes available for the stats admin page (strictly 4 chars length) */ |
| 244 | const char *stat_status_codes[STAT_STATUS_SIZE] = { |
| 245 | [STAT_STATUS_DENY] = "DENY", |
| 246 | [STAT_STATUS_DONE] = "DONE", |
| 247 | [STAT_STATUS_ERRP] = "ERRP", |
| 248 | [STAT_STATUS_EXCD] = "EXCD", |
| 249 | [STAT_STATUS_NONE] = "NONE", |
| 250 | [STAT_STATUS_PART] = "PART", |
| 251 | [STAT_STATUS_UNKN] = "UNKN", |
| 252 | }; |
| 253 | |
| 254 | |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 255 | /* List head of all known action keywords for "http-request" */ |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 256 | struct action_kw_list http_req_keywords = { |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 257 | .list = LIST_HEAD_INIT(http_req_keywords.list) |
| 258 | }; |
| 259 | |
| 260 | /* List head of all known action keywords for "http-response" */ |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 261 | struct action_kw_list http_res_keywords = { |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 262 | .list = LIST_HEAD_INIT(http_res_keywords.list) |
| 263 | }; |
| 264 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 265 | /* We must put the messages here since GCC cannot initialize consts depending |
| 266 | * on strlen(). |
| 267 | */ |
| 268 | struct chunk http_err_chunks[HTTP_ERR_SIZE]; |
| 269 | |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 270 | /* 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] | 271 | static THREAD_LOCAL struct hdr_ctx static_hdr_ctx; |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 272 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 273 | #define FD_SETS_ARE_BITFIELDS |
| 274 | #ifdef FD_SETS_ARE_BITFIELDS |
| 275 | /* |
| 276 | * This map is used with all the FD_* macros to check whether a particular bit |
| 277 | * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes |
| 278 | * which should be encoded. When FD_ISSET() returns non-zero, it means that the |
| 279 | * byte should be encoded. Be careful to always pass bytes from 0 to 255 |
| 280 | * exclusively to the macros. |
| 281 | */ |
| 282 | fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| 283 | 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] | 284 | 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] | 285 | |
| 286 | #else |
| 287 | #error "Check if your OS uses bitfields for fd_sets" |
| 288 | #endif |
| 289 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 290 | 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] | 291 | |
David Carlier | 7365f7d | 2016-04-04 11:54:42 +0100 | [diff] [blame] | 292 | static inline int http_msg_forward_body(struct stream *s, struct http_msg *msg); |
| 293 | 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] | 294 | |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 295 | /* This function returns a reason associated with the HTTP status. |
| 296 | * This function never fails, a message is always returned. |
| 297 | */ |
| 298 | const char *get_reason(unsigned int status) |
| 299 | { |
| 300 | switch (status) { |
| 301 | case 100: return "Continue"; |
| 302 | case 101: return "Switching Protocols"; |
| 303 | case 102: return "Processing"; |
| 304 | case 200: return "OK"; |
| 305 | case 201: return "Created"; |
| 306 | case 202: return "Accepted"; |
| 307 | case 203: return "Non-Authoritative Information"; |
| 308 | case 204: return "No Content"; |
| 309 | case 205: return "Reset Content"; |
| 310 | case 206: return "Partial Content"; |
| 311 | case 207: return "Multi-Status"; |
| 312 | case 210: return "Content Different"; |
| 313 | case 226: return "IM Used"; |
| 314 | case 300: return "Multiple Choices"; |
| 315 | case 301: return "Moved Permanently"; |
| 316 | case 302: return "Moved Temporarily"; |
| 317 | case 303: return "See Other"; |
| 318 | case 304: return "Not Modified"; |
| 319 | case 305: return "Use Proxy"; |
| 320 | case 307: return "Temporary Redirect"; |
| 321 | case 308: return "Permanent Redirect"; |
| 322 | case 310: return "Too many Redirects"; |
| 323 | case 400: return "Bad Request"; |
| 324 | case 401: return "Unauthorized"; |
| 325 | case 402: return "Payment Required"; |
| 326 | case 403: return "Forbidden"; |
| 327 | case 404: return "Not Found"; |
| 328 | case 405: return "Method Not Allowed"; |
| 329 | case 406: return "Not Acceptable"; |
| 330 | case 407: return "Proxy Authentication Required"; |
| 331 | case 408: return "Request Time-out"; |
| 332 | case 409: return "Conflict"; |
| 333 | case 410: return "Gone"; |
| 334 | case 411: return "Length Required"; |
| 335 | case 412: return "Precondition Failed"; |
| 336 | case 413: return "Request Entity Too Large"; |
| 337 | case 414: return "Request-URI Too Long"; |
| 338 | case 415: return "Unsupported Media Type"; |
| 339 | case 416: return "Requested range unsatisfiable"; |
| 340 | case 417: return "Expectation failed"; |
| 341 | case 418: return "I'm a teapot"; |
| 342 | case 422: return "Unprocessable entity"; |
| 343 | case 423: return "Locked"; |
| 344 | case 424: return "Method failure"; |
Olivier Houchard | 51a76d8 | 2017-10-02 16:12:07 +0200 | [diff] [blame] | 345 | case 425: return "Too Early"; |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 346 | case 426: return "Upgrade Required"; |
| 347 | case 428: return "Precondition Required"; |
| 348 | case 429: return "Too Many Requests"; |
| 349 | case 431: return "Request Header Fields Too Large"; |
| 350 | case 449: return "Retry With"; |
| 351 | case 450: return "Blocked by Windows Parental Controls"; |
| 352 | case 451: return "Unavailable For Legal Reasons"; |
| 353 | case 456: return "Unrecoverable Error"; |
| 354 | case 499: return "client has closed connection"; |
| 355 | case 500: return "Internal Server Error"; |
| 356 | case 501: return "Not Implemented"; |
Jarno Huuskonen | 59af2df | 2016-12-28 10:49:01 +0200 | [diff] [blame] | 357 | case 502: return "Bad Gateway or Proxy Error"; |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 358 | case 503: return "Service Unavailable"; |
| 359 | case 504: return "Gateway Time-out"; |
| 360 | case 505: return "HTTP Version not supported"; |
| 361 | case 506: return "Variant also negociate"; |
| 362 | case 507: return "Insufficient storage"; |
| 363 | case 508: return "Loop detected"; |
| 364 | case 509: return "Bandwidth Limit Exceeded"; |
| 365 | case 510: return "Not extended"; |
| 366 | case 511: return "Network authentication required"; |
| 367 | case 520: return "Web server is returning an unknown error"; |
| 368 | default: |
| 369 | switch (status) { |
| 370 | case 100 ... 199: return "Informational"; |
| 371 | case 200 ... 299: return "Success"; |
| 372 | case 300 ... 399: return "Redirection"; |
| 373 | case 400 ... 499: return "Client Error"; |
| 374 | case 500 ... 599: return "Server Error"; |
| 375 | default: return "Other"; |
| 376 | } |
| 377 | } |
| 378 | } |
| 379 | |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 380 | /* This function returns HTTP_ERR_<num> (enum) matching http status code. |
| 381 | * Returned value should match codes from http_err_codes. |
| 382 | */ |
| 383 | static const int http_get_status_idx(unsigned int status) |
| 384 | { |
| 385 | switch (status) { |
| 386 | case 200: return HTTP_ERR_200; |
| 387 | case 400: return HTTP_ERR_400; |
| 388 | case 403: return HTTP_ERR_403; |
| 389 | case 405: return HTTP_ERR_405; |
| 390 | case 408: return HTTP_ERR_408; |
Olivier Houchard | 51a76d8 | 2017-10-02 16:12:07 +0200 | [diff] [blame] | 391 | case 425: return HTTP_ERR_425; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 392 | case 429: return HTTP_ERR_429; |
| 393 | case 500: return HTTP_ERR_500; |
| 394 | case 502: return HTTP_ERR_502; |
| 395 | case 503: return HTTP_ERR_503; |
| 396 | case 504: return HTTP_ERR_504; |
| 397 | default: return HTTP_ERR_500; |
| 398 | } |
| 399 | } |
| 400 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 401 | void init_proto_http() |
| 402 | { |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 403 | int i; |
| 404 | char *tmp; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 405 | int msg; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 406 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 407 | for (msg = 0; msg < HTTP_ERR_SIZE; msg++) { |
| 408 | if (!http_err_msgs[msg]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 409 | 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] | 410 | abort(); |
| 411 | } |
| 412 | |
| 413 | http_err_chunks[msg].str = (char *)http_err_msgs[msg]; |
| 414 | http_err_chunks[msg].len = strlen(http_err_msgs[msg]); |
| 415 | } |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 416 | |
| 417 | /* initialize the log header encoding map : '{|}"#' should be encoded with |
| 418 | * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ). |
| 419 | * URL encoding only requires '"', '#' to be encoded as well as non- |
| 420 | * printable characters above. |
| 421 | */ |
| 422 | memset(hdr_encode_map, 0, sizeof(hdr_encode_map)); |
| 423 | memset(url_encode_map, 0, sizeof(url_encode_map)); |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 424 | memset(http_encode_map, 0, sizeof(url_encode_map)); |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 425 | for (i = 0; i < 32; i++) { |
| 426 | FD_SET(i, hdr_encode_map); |
| 427 | FD_SET(i, url_encode_map); |
| 428 | } |
| 429 | for (i = 127; i < 256; i++) { |
| 430 | FD_SET(i, hdr_encode_map); |
| 431 | FD_SET(i, url_encode_map); |
| 432 | } |
| 433 | |
| 434 | tmp = "\"#{|}"; |
| 435 | while (*tmp) { |
| 436 | FD_SET(*tmp, hdr_encode_map); |
| 437 | tmp++; |
| 438 | } |
| 439 | |
| 440 | tmp = "\"#"; |
| 441 | while (*tmp) { |
| 442 | FD_SET(*tmp, url_encode_map); |
| 443 | tmp++; |
| 444 | } |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 445 | |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 446 | /* initialize the http header encoding map. The draft httpbis define the |
| 447 | * header content as: |
| 448 | * |
| 449 | * HTTP-message = start-line |
| 450 | * *( header-field CRLF ) |
| 451 | * CRLF |
| 452 | * [ message-body ] |
| 453 | * header-field = field-name ":" OWS field-value OWS |
| 454 | * field-value = *( field-content / obs-fold ) |
| 455 | * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ] |
| 456 | * obs-fold = CRLF 1*( SP / HTAB ) |
| 457 | * field-vchar = VCHAR / obs-text |
| 458 | * VCHAR = %x21-7E |
| 459 | * obs-text = %x80-FF |
| 460 | * |
| 461 | * All the chars are encoded except "VCHAR", "obs-text", SP and HTAB. |
| 462 | * The encoded chars are form 0x00 to 0x08, 0x0a to 0x1f and 0x7f. The |
| 463 | * "obs-fold" is volontary forgotten because haproxy remove this. |
| 464 | */ |
| 465 | memset(http_encode_map, 0, sizeof(http_encode_map)); |
| 466 | for (i = 0x00; i <= 0x08; i++) |
| 467 | FD_SET(i, http_encode_map); |
| 468 | for (i = 0x0a; i <= 0x1f; i++) |
| 469 | FD_SET(i, http_encode_map); |
| 470 | FD_SET(0x7f, http_encode_map); |
| 471 | |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 472 | /* memory allocations */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 473 | pool_head_http_txn = create_pool("http_txn", sizeof(struct http_txn), MEM_F_SHARED); |
| 474 | pool_head_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED); |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 475 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 476 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 477 | /* |
| 478 | * We have 26 list of methods (1 per first letter), each of which can have |
| 479 | * up to 3 entries (2 valid, 1 null). |
| 480 | */ |
| 481 | struct http_method_desc { |
Willy Tarreau | c8987b3 | 2013-12-06 23:43:17 +0100 | [diff] [blame] | 482 | enum http_meth_t meth; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 483 | int len; |
| 484 | const char text[8]; |
| 485 | }; |
| 486 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 487 | const struct http_method_desc http_methods[26][3] = { |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 488 | ['C' - 'A'] = { |
| 489 | [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" }, |
| 490 | }, |
| 491 | ['D' - 'A'] = { |
| 492 | [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" }, |
| 493 | }, |
| 494 | ['G' - 'A'] = { |
| 495 | [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" }, |
| 496 | }, |
| 497 | ['H' - 'A'] = { |
| 498 | [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" }, |
| 499 | }, |
Christopher Faulet | d57ad64 | 2015-07-31 14:26:57 +0200 | [diff] [blame] | 500 | ['O' - 'A'] = { |
| 501 | [0] = { .meth = HTTP_METH_OPTIONS , .len=7, .text="OPTIONS" }, |
| 502 | }, |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 503 | ['P' - 'A'] = { |
| 504 | [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" }, |
| 505 | [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" }, |
| 506 | }, |
| 507 | ['T' - 'A'] = { |
| 508 | [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" }, |
| 509 | }, |
| 510 | /* rest is empty like this : |
Willy Tarreau | b7ce424 | 2015-09-03 17:15:21 +0200 | [diff] [blame] | 511 | * [0] = { .meth = HTTP_METH_OTHER , .len=0, .text="" }, |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 512 | */ |
| 513 | }; |
| 514 | |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 515 | const struct http_method_name http_known_methods[HTTP_METH_OTHER] = { |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 516 | [HTTP_METH_OPTIONS] = { "OPTIONS", 7 }, |
| 517 | [HTTP_METH_GET] = { "GET", 3 }, |
| 518 | [HTTP_METH_HEAD] = { "HEAD", 4 }, |
| 519 | [HTTP_METH_POST] = { "POST", 4 }, |
| 520 | [HTTP_METH_PUT] = { "PUT", 3 }, |
| 521 | [HTTP_METH_DELETE] = { "DELETE", 6 }, |
| 522 | [HTTP_METH_TRACE] = { "TRACE", 5 }, |
| 523 | [HTTP_METH_CONNECT] = { "CONNECT", 7 }, |
| 524 | }; |
| 525 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 526 | /* |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 527 | * Adds a header and its CRLF at the tail of the message's buffer, just before |
| 528 | * 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] | 529 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 530 | * of headers is automatically adjusted. The number of bytes added is returned |
| 531 | * on success, otherwise <0 is returned indicating an error. |
| 532 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 533 | 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] | 534 | { |
| 535 | int bytes, len; |
| 536 | |
| 537 | len = strlen(text); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 538 | 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] | 539 | if (!bytes) |
| 540 | return -1; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 541 | http_msg_move_end(msg, bytes); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 542 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 543 | } |
| 544 | |
| 545 | /* |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 546 | * Adds a header and its CRLF at the tail of the message's buffer, just before |
| 547 | * 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] | 548 | * the buffer is only opened and the space reserved, but nothing is copied. |
| 549 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 550 | * of headers is automatically adjusted. The number of bytes added is returned |
| 551 | * on success, otherwise <0 is returned indicating an error. |
| 552 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 553 | int http_header_add_tail2(struct http_msg *msg, |
| 554 | struct hdr_idx *hdr_idx, const char *text, int len) |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 555 | { |
| 556 | int bytes; |
| 557 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 558 | 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] | 559 | if (!bytes) |
| 560 | return -1; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 561 | http_msg_move_end(msg, bytes); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 562 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 563 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 564 | |
| 565 | /* |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 566 | * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon. |
| 567 | * If so, returns the position of the first non-space character relative to |
| 568 | * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries |
| 569 | * to return a pointer to the place after the first space. Returns 0 if the |
| 570 | * header name does not match. Checks are case-insensitive. |
| 571 | */ |
| 572 | int http_header_match2(const char *hdr, const char *end, |
| 573 | const char *name, int len) |
| 574 | { |
| 575 | const char *val; |
| 576 | |
| 577 | if (hdr + len >= end) |
| 578 | return 0; |
| 579 | if (hdr[len] != ':') |
| 580 | return 0; |
| 581 | if (strncasecmp(hdr, name, len) != 0) |
| 582 | return 0; |
| 583 | val = hdr + len + 1; |
| 584 | while (val < end && HTTP_IS_SPHT(*val)) |
| 585 | val++; |
| 586 | if ((val >= end) && (len + 2 <= end - hdr)) |
| 587 | return len + 2; /* we may replace starting from second space */ |
| 588 | return val - hdr; |
| 589 | } |
| 590 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 591 | /* Find the first or next occurrence of header <name> in message buffer <sol> |
| 592 | * using headers index <idx>, and return it in the <ctx> structure. This |
| 593 | * structure holds everything necessary to use the header and find next |
| 594 | * occurrence. If its <idx> member is 0, the header is searched from the |
| 595 | * beginning. Otherwise, the next occurrence is returned. The function returns |
| 596 | * 1 when it finds a value, and 0 when there is no more. It is very similar to |
| 597 | * http_find_header2() except that it is designed to work with full-line headers |
| 598 | * whose comma is not a delimiter but is part of the syntax. As a special case, |
| 599 | * if ctx->val is NULL when searching for a new values of a header, the current |
| 600 | * header is rescanned. This allows rescanning after a header deletion. |
| 601 | */ |
| 602 | int http_find_full_header2(const char *name, int len, |
| 603 | char *sol, struct hdr_idx *idx, |
| 604 | struct hdr_ctx *ctx) |
| 605 | { |
| 606 | char *eol, *sov; |
| 607 | int cur_idx, old_idx; |
| 608 | |
| 609 | cur_idx = ctx->idx; |
| 610 | if (cur_idx) { |
| 611 | /* We have previously returned a header, let's search another one */ |
| 612 | sol = ctx->line; |
| 613 | eol = sol + idx->v[cur_idx].len; |
| 614 | goto next_hdr; |
| 615 | } |
| 616 | |
| 617 | /* first request for this header */ |
| 618 | sol += hdr_idx_first_pos(idx); |
| 619 | old_idx = 0; |
| 620 | cur_idx = hdr_idx_first_idx(idx); |
| 621 | while (cur_idx) { |
| 622 | eol = sol + idx->v[cur_idx].len; |
| 623 | |
| 624 | if (len == 0) { |
| 625 | /* No argument was passed, we want any header. |
| 626 | * To achieve this, we simply build a fake request. */ |
| 627 | while (sol + len < eol && sol[len] != ':') |
| 628 | len++; |
| 629 | name = sol; |
| 630 | } |
| 631 | |
| 632 | if ((len < eol - sol) && |
| 633 | (sol[len] == ':') && |
| 634 | (strncasecmp(sol, name, len) == 0)) { |
| 635 | ctx->del = len; |
| 636 | sov = sol + len + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 637 | while (sov < eol && HTTP_IS_LWS(*sov)) |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 638 | sov++; |
| 639 | |
| 640 | ctx->line = sol; |
| 641 | ctx->prev = old_idx; |
| 642 | ctx->idx = cur_idx; |
| 643 | ctx->val = sov - sol; |
| 644 | ctx->tws = 0; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 645 | while (eol > sov && HTTP_IS_LWS(*(eol - 1))) { |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 646 | eol--; |
| 647 | ctx->tws++; |
| 648 | } |
| 649 | ctx->vlen = eol - sov; |
| 650 | return 1; |
| 651 | } |
| 652 | next_hdr: |
| 653 | sol = eol + idx->v[cur_idx].cr + 1; |
| 654 | old_idx = cur_idx; |
| 655 | cur_idx = idx->v[cur_idx].next; |
| 656 | } |
| 657 | return 0; |
| 658 | } |
| 659 | |
Willy Tarreau | c90dc23 | 2015-02-20 13:51:36 +0100 | [diff] [blame] | 660 | /* Find the first or next header field in message buffer <sol> using headers |
| 661 | * index <idx>, and return it in the <ctx> structure. This structure holds |
| 662 | * everything necessary to use the header and find next occurrence. If its |
| 663 | * <idx> member is 0, the first header is retrieved. Otherwise, the next |
| 664 | * occurrence is returned. The function returns 1 when it finds a value, and |
| 665 | * 0 when there is no more. It is equivalent to http_find_full_header2() with |
| 666 | * no header name. |
| 667 | */ |
| 668 | int http_find_next_header(char *sol, struct hdr_idx *idx, struct hdr_ctx *ctx) |
| 669 | { |
| 670 | char *eol, *sov; |
| 671 | int cur_idx, old_idx; |
| 672 | int len; |
| 673 | |
| 674 | cur_idx = ctx->idx; |
| 675 | if (cur_idx) { |
| 676 | /* We have previously returned a header, let's search another one */ |
| 677 | sol = ctx->line; |
| 678 | eol = sol + idx->v[cur_idx].len; |
| 679 | goto next_hdr; |
| 680 | } |
| 681 | |
| 682 | /* first request for this header */ |
| 683 | sol += hdr_idx_first_pos(idx); |
| 684 | old_idx = 0; |
| 685 | cur_idx = hdr_idx_first_idx(idx); |
| 686 | while (cur_idx) { |
| 687 | eol = sol + idx->v[cur_idx].len; |
| 688 | |
| 689 | len = 0; |
| 690 | while (1) { |
| 691 | if (len >= eol - sol) |
| 692 | goto next_hdr; |
| 693 | if (sol[len] == ':') |
| 694 | break; |
| 695 | len++; |
| 696 | } |
| 697 | |
| 698 | ctx->del = len; |
| 699 | sov = sol + len + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 700 | while (sov < eol && HTTP_IS_LWS(*sov)) |
Willy Tarreau | c90dc23 | 2015-02-20 13:51:36 +0100 | [diff] [blame] | 701 | sov++; |
| 702 | |
| 703 | ctx->line = sol; |
| 704 | ctx->prev = old_idx; |
| 705 | ctx->idx = cur_idx; |
| 706 | ctx->val = sov - sol; |
| 707 | ctx->tws = 0; |
| 708 | |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 709 | while (eol > sov && HTTP_IS_LWS(*(eol - 1))) { |
Willy Tarreau | c90dc23 | 2015-02-20 13:51:36 +0100 | [diff] [blame] | 710 | eol--; |
| 711 | ctx->tws++; |
| 712 | } |
| 713 | ctx->vlen = eol - sov; |
| 714 | return 1; |
| 715 | |
| 716 | next_hdr: |
| 717 | sol = eol + idx->v[cur_idx].cr + 1; |
| 718 | old_idx = cur_idx; |
| 719 | cur_idx = idx->v[cur_idx].next; |
| 720 | } |
| 721 | return 0; |
| 722 | } |
| 723 | |
Lukas Tribus | 2395368 | 2017-04-28 13:24:30 +0000 | [diff] [blame] | 724 | /* Find the end of the header value contained between <s> and <e>. See RFC7230, |
| 725 | * 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] | 726 | * a valid result. This works for headers defined as comma-separated lists. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 727 | */ |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 728 | char *find_hdr_value_end(char *s, const char *e) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 729 | { |
| 730 | int quoted, qdpair; |
| 731 | |
| 732 | quoted = qdpair = 0; |
Willy Tarreau | e6d9c21 | 2016-11-05 18:23:38 +0100 | [diff] [blame] | 733 | |
| 734 | #if defined(__x86_64__) || \ |
| 735 | defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) || \ |
| 736 | defined(__ARM_ARCH_7A__) |
| 737 | /* speedup: skip everything not a comma nor a double quote */ |
| 738 | for (; s <= e - sizeof(int); s += sizeof(int)) { |
| 739 | unsigned int c = *(int *)s; // comma |
| 740 | unsigned int q = c; // quote |
| 741 | |
| 742 | c ^= 0x2c2c2c2c; // contains one zero on a comma |
| 743 | q ^= 0x22222222; // contains one zero on a quote |
| 744 | |
| 745 | c = (c - 0x01010101) & ~c; // contains 0x80 below a comma |
| 746 | q = (q - 0x01010101) & ~q; // contains 0x80 below a quote |
| 747 | |
| 748 | if ((c | q) & 0x80808080) |
| 749 | break; // found a comma or a quote |
| 750 | } |
| 751 | #endif |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 752 | for (; s < e; s++) { |
| 753 | if (qdpair) qdpair = 0; |
Willy Tarreau | 0f7f51f | 2010-08-30 11:06:34 +0200 | [diff] [blame] | 754 | else if (quoted) { |
| 755 | if (*s == '\\') qdpair = 1; |
| 756 | else if (*s == '"') quoted = 0; |
| 757 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 758 | else if (*s == '"') quoted = 1; |
| 759 | else if (*s == ',') return s; |
| 760 | } |
| 761 | return s; |
| 762 | } |
| 763 | |
| 764 | /* Find the first or next occurrence of header <name> in message buffer <sol> |
| 765 | * using headers index <idx>, and return it in the <ctx> structure. This |
| 766 | * structure holds everything necessary to use the header and find next |
| 767 | * occurrence. If its <idx> member is 0, the header is searched from the |
| 768 | * beginning. Otherwise, the next occurrence is returned. The function returns |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 769 | * 1 when it finds a value, and 0 when there is no more. It is designed to work |
| 770 | * with headers defined as comma-separated lists. As a special case, if ctx->val |
| 771 | * is NULL when searching for a new values of a header, the current header is |
| 772 | * rescanned. This allows rescanning after a header deletion. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 773 | */ |
| 774 | int http_find_header2(const char *name, int len, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 775 | char *sol, struct hdr_idx *idx, |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 776 | struct hdr_ctx *ctx) |
| 777 | { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 778 | char *eol, *sov; |
| 779 | int cur_idx, old_idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 780 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 781 | cur_idx = ctx->idx; |
| 782 | if (cur_idx) { |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 783 | /* We have previously returned a value, let's search |
| 784 | * another one on the same line. |
| 785 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 786 | sol = ctx->line; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 787 | ctx->del = ctx->val + ctx->vlen + ctx->tws; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 788 | sov = sol + ctx->del; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 789 | eol = sol + idx->v[cur_idx].len; |
| 790 | |
| 791 | if (sov >= eol) |
| 792 | /* no more values in this header */ |
| 793 | goto next_hdr; |
| 794 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 795 | /* values remaining for this header, skip the comma but save it |
| 796 | * for later use (eg: for header deletion). |
| 797 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 798 | sov++; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 799 | while (sov < eol && HTTP_IS_LWS((*sov))) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 800 | sov++; |
| 801 | |
| 802 | goto return_hdr; |
| 803 | } |
| 804 | |
| 805 | /* first request for this header */ |
| 806 | sol += hdr_idx_first_pos(idx); |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 807 | old_idx = 0; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 808 | cur_idx = hdr_idx_first_idx(idx); |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 809 | while (cur_idx) { |
| 810 | eol = sol + idx->v[cur_idx].len; |
| 811 | |
Willy Tarreau | 1ad7c6d | 2007-06-10 21:42:55 +0200 | [diff] [blame] | 812 | if (len == 0) { |
| 813 | /* No argument was passed, we want any header. |
| 814 | * To achieve this, we simply build a fake request. */ |
| 815 | while (sol + len < eol && sol[len] != ':') |
| 816 | len++; |
| 817 | name = sol; |
| 818 | } |
| 819 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 820 | if ((len < eol - sol) && |
| 821 | (sol[len] == ':') && |
| 822 | (strncasecmp(sol, name, len) == 0)) { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 823 | ctx->del = len; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 824 | sov = sol + len + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 825 | while (sov < eol && HTTP_IS_LWS(*sov)) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 826 | sov++; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 827 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 828 | ctx->line = sol; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 829 | ctx->prev = old_idx; |
| 830 | return_hdr: |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 831 | ctx->idx = cur_idx; |
| 832 | ctx->val = sov - sol; |
| 833 | |
| 834 | eol = find_hdr_value_end(sov, eol); |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 835 | ctx->tws = 0; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 836 | while (eol > sov && HTTP_IS_LWS(*(eol - 1))) { |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 837 | eol--; |
| 838 | ctx->tws++; |
| 839 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 840 | ctx->vlen = eol - sov; |
| 841 | return 1; |
| 842 | } |
| 843 | next_hdr: |
| 844 | sol = eol + idx->v[cur_idx].cr + 1; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 845 | old_idx = cur_idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 846 | cur_idx = idx->v[cur_idx].next; |
| 847 | } |
| 848 | return 0; |
| 849 | } |
| 850 | |
| 851 | int http_find_header(const char *name, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 852 | char *sol, struct hdr_idx *idx, |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 853 | struct hdr_ctx *ctx) |
| 854 | { |
| 855 | return http_find_header2(name, strlen(name), sol, idx, ctx); |
| 856 | } |
| 857 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 858 | /* Remove one value of a header. This only works on a <ctx> returned by one of |
| 859 | * the http_find_header functions. The value is removed, as well as surrounding |
| 860 | * 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] | 861 | * 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] | 862 | * message <msg>. The new index is returned. If it is zero, it means there is |
| 863 | * no more header, so any processing may stop. The ctx is always left in a form |
| 864 | * that can be handled by http_find_header2() to find next occurrence. |
| 865 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 866 | 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] | 867 | { |
| 868 | int cur_idx = ctx->idx; |
| 869 | char *sol = ctx->line; |
| 870 | struct hdr_idx_elem *hdr; |
| 871 | int delta, skip_comma; |
| 872 | |
| 873 | if (!cur_idx) |
| 874 | return 0; |
| 875 | |
| 876 | hdr = &idx->v[cur_idx]; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 877 | if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 878 | /* 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] | 879 | 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] | 880 | http_msg_move_end(msg, delta); |
| 881 | idx->used--; |
| 882 | hdr->len = 0; /* unused entry */ |
| 883 | idx->v[ctx->prev].next = idx->v[ctx->idx].next; |
Willy Tarreau | 5c4784f | 2011-02-12 13:07:35 +0100 | [diff] [blame] | 884 | if (idx->tail == ctx->idx) |
| 885 | idx->tail = ctx->prev; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 886 | ctx->idx = ctx->prev; /* walk back to the end of previous header */ |
Willy Tarreau | 7c1c217 | 2015-01-07 17:23:50 +0100 | [diff] [blame] | 887 | 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] | 888 | 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] | 889 | ctx->tws = ctx->vlen = 0; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 890 | return ctx->idx; |
| 891 | } |
| 892 | |
| 893 | /* 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] | 894 | * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the |
| 895 | * last entry of the list, we remove the last separator. |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 896 | */ |
| 897 | |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 898 | skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 899 | delta = buffer_replace2(msg->chn->buf, sol + ctx->del + skip_comma, |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 900 | sol + ctx->val + ctx->vlen + ctx->tws + skip_comma, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 901 | NULL, 0); |
| 902 | hdr->len += delta; |
| 903 | http_msg_move_end(msg, delta); |
| 904 | ctx->val = ctx->del; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 905 | ctx->tws = ctx->vlen = 0; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 906 | return ctx->idx; |
| 907 | } |
| 908 | |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 909 | /* This function handles a server error at the stream interface level. The |
| 910 | * 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] | 911 | * message is copied into the input buffer. |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 912 | * 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] | 913 | * in this buffer will be lost. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 914 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 915 | static void http_server_error(struct stream *s, struct stream_interface *si, |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 916 | int err, int finst, const struct chunk *msg) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 917 | { |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 918 | FLT_STRM_CB(s, flt_http_reply(s, s->txn->status, msg)); |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 919 | channel_auto_read(si_oc(si)); |
| 920 | channel_abort(si_oc(si)); |
| 921 | channel_auto_close(si_oc(si)); |
| 922 | channel_erase(si_oc(si)); |
| 923 | channel_auto_close(si_ic(si)); |
| 924 | channel_auto_read(si_ic(si)); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 925 | if (msg) |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 926 | co_inject(si_ic(si), msg->str, msg->len); |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 927 | if (!(s->flags & SF_ERR_MASK)) |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 928 | s->flags |= err; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 929 | if (!(s->flags & SF_FINST_MASK)) |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 930 | s->flags |= finst; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 931 | } |
| 932 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 933 | /* This function returns the appropriate error location for the given stream |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 934 | * and message. |
| 935 | */ |
| 936 | |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 937 | struct chunk *http_error_message(struct stream *s) |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 938 | { |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 939 | const int msgnum = http_get_status_idx(s->txn->status); |
| 940 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 941 | if (s->be->errmsg[msgnum].str) |
| 942 | return &s->be->errmsg[msgnum]; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 943 | else if (strm_fe(s)->errmsg[msgnum].str) |
| 944 | return &strm_fe(s)->errmsg[msgnum]; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 945 | else |
| 946 | return &http_err_chunks[msgnum]; |
| 947 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 948 | |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 949 | void |
| 950 | http_reply_and_close(struct stream *s, short status, struct chunk *msg) |
| 951 | { |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 952 | s->txn->flags &= ~TX_WAIT_NEXT_RQ; |
Christopher Faulet | 3e34429 | 2015-11-24 16:24:13 +0100 | [diff] [blame] | 953 | FLT_STRM_CB(s, flt_http_reply(s, status, msg)); |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 954 | stream_int_retnclose(&s->si[0], msg); |
| 955 | } |
| 956 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 957 | /* |
Willy Tarreau | b7ce424 | 2015-09-03 17:15:21 +0200 | [diff] [blame] | 958 | * returns a known method among HTTP_METH_* or HTTP_METH_OTHER for all unknown |
| 959 | * ones. |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 960 | */ |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 961 | 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] | 962 | { |
| 963 | unsigned char m; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 964 | const struct http_method_desc *h; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 965 | |
| 966 | m = ((unsigned)*str - 'A'); |
| 967 | |
| 968 | if (m < 26) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 969 | for (h = http_methods[m]; h->len > 0; h++) { |
| 970 | if (unlikely(h->len != len)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 971 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 972 | if (likely(memcmp(str, h->text, h->len) == 0)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 973 | return h->meth; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 974 | }; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 975 | } |
Willy Tarreau | b7ce424 | 2015-09-03 17:15:21 +0200 | [diff] [blame] | 976 | return HTTP_METH_OTHER; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 977 | } |
| 978 | |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 979 | /* Parse the URI from the given transaction (which is assumed to be in request |
| 980 | * phase) and look for the "/" beginning the PATH. If not found, return NULL. |
| 981 | * It is returned otherwise. |
| 982 | */ |
Thierry FOURNIER | 3c33178 | 2015-09-17 19:33:35 +0200 | [diff] [blame] | 983 | char *http_get_path(struct http_txn *txn) |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 984 | { |
| 985 | char *ptr, *end; |
| 986 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 987 | ptr = txn->req.chn->buf->p + txn->req.sl.rq.u; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 988 | end = ptr + txn->req.sl.rq.u_l; |
| 989 | |
| 990 | if (ptr >= end) |
| 991 | return NULL; |
| 992 | |
Lukas Tribus | 2395368 | 2017-04-28 13:24:30 +0000 | [diff] [blame] | 993 | /* RFC7230, par. 2.7 : |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 994 | * Request-URI = "*" | absuri | abspath | authority |
| 995 | */ |
| 996 | |
| 997 | if (*ptr == '*') |
| 998 | return NULL; |
| 999 | |
| 1000 | if (isalpha((unsigned char)*ptr)) { |
| 1001 | /* this is a scheme as described by RFC3986, par. 3.1 */ |
| 1002 | ptr++; |
| 1003 | while (ptr < end && |
| 1004 | (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')) |
| 1005 | ptr++; |
| 1006 | /* skip '://' */ |
| 1007 | if (ptr == end || *ptr++ != ':') |
| 1008 | return NULL; |
| 1009 | if (ptr == end || *ptr++ != '/') |
| 1010 | return NULL; |
| 1011 | if (ptr == end || *ptr++ != '/') |
| 1012 | return NULL; |
| 1013 | } |
| 1014 | /* skip [user[:passwd]@]host[:[port]] */ |
| 1015 | |
| 1016 | while (ptr < end && *ptr != '/') |
| 1017 | ptr++; |
| 1018 | |
| 1019 | if (ptr == end) |
| 1020 | return NULL; |
| 1021 | |
| 1022 | /* OK, we got the '/' ! */ |
| 1023 | return ptr; |
| 1024 | } |
| 1025 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 1026 | /* Parse the URI from the given string and look for the "/" beginning the PATH. |
| 1027 | * If not found, return NULL. It is returned otherwise. |
| 1028 | */ |
| 1029 | static char * |
| 1030 | http_get_path_from_string(char *str) |
| 1031 | { |
| 1032 | char *ptr = str; |
| 1033 | |
| 1034 | /* RFC2616, par. 5.1.2 : |
| 1035 | * Request-URI = "*" | absuri | abspath | authority |
| 1036 | */ |
| 1037 | |
| 1038 | if (*ptr == '*') |
| 1039 | return NULL; |
| 1040 | |
| 1041 | if (isalpha((unsigned char)*ptr)) { |
| 1042 | /* this is a scheme as described by RFC3986, par. 3.1 */ |
| 1043 | ptr++; |
| 1044 | while (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.') |
| 1045 | ptr++; |
| 1046 | /* skip '://' */ |
| 1047 | if (*ptr == '\0' || *ptr++ != ':') |
| 1048 | return NULL; |
| 1049 | if (*ptr == '\0' || *ptr++ != '/') |
| 1050 | return NULL; |
| 1051 | if (*ptr == '\0' || *ptr++ != '/') |
| 1052 | return NULL; |
| 1053 | } |
| 1054 | /* skip [user[:passwd]@]host[:[port]] */ |
| 1055 | |
| 1056 | while (*ptr != '\0' && *ptr != ' ' && *ptr != '/') |
| 1057 | ptr++; |
| 1058 | |
| 1059 | if (*ptr == '\0' || *ptr == ' ') |
| 1060 | return NULL; |
| 1061 | |
| 1062 | /* OK, we got the '/' ! */ |
| 1063 | return ptr; |
| 1064 | } |
| 1065 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 1066 | /* Returns a 302 for a redirectable request that reaches a server working in |
| 1067 | * in redirect mode. This may only be called just after the stream interface |
| 1068 | * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will |
| 1069 | * follow normal proxy processing. NOTE: this function is designed to support |
| 1070 | * being called once data are scheduled for forwarding. |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1071 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1072 | void http_perform_server_redirect(struct stream *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1073 | { |
| 1074 | struct http_txn *txn; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 1075 | struct server *srv; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1076 | char *path; |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 1077 | int len, rewind; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1078 | |
| 1079 | /* 1: create the response header */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1080 | trash.len = strlen(HTTP_302); |
| 1081 | memcpy(trash.str, HTTP_302, trash.len); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1082 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 1083 | srv = objt_server(s->target); |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 1084 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1085 | /* 2: add the server's prefix */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1086 | if (trash.len + srv->rdr_len > trash.size) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1087 | return; |
| 1088 | |
Willy Tarreau | dcb75c4 | 2010-01-10 00:24:22 +0100 | [diff] [blame] | 1089 | /* special prefix "/" means don't change URL */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 1090 | if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1091 | memcpy(trash.str + trash.len, srv->rdr_pfx, srv->rdr_len); |
| 1092 | trash.len += srv->rdr_len; |
Willy Tarreau | dcb75c4 | 2010-01-10 00:24:22 +0100 | [diff] [blame] | 1093 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1094 | |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 1095 | /* 3: add the request URI. Since it was already forwarded, we need |
| 1096 | * to temporarily rewind the buffer. |
| 1097 | */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 1098 | txn = s->txn; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1099 | b_rew(s->req.buf, rewind = http_hdr_rewind(&txn->req)); |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 1100 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1101 | path = http_get_path(txn); |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1102 | 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] | 1103 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1104 | b_adv(s->req.buf, rewind); |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 1105 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1106 | if (!path) |
| 1107 | return; |
| 1108 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1109 | if (trash.len + len > trash.size - 4) /* 4 for CRLF-CRLF */ |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1110 | return; |
| 1111 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1112 | memcpy(trash.str + trash.len, path, len); |
| 1113 | trash.len += len; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1114 | |
| 1115 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1116 | memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29); |
| 1117 | trash.len += 29; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1118 | } else { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1119 | memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23); |
| 1120 | trash.len += 23; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1121 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1122 | |
| 1123 | /* prepare to return without error. */ |
Willy Tarreau | 73b013b | 2012-05-21 16:31:45 +0200 | [diff] [blame] | 1124 | si_shutr(si); |
| 1125 | si_shutw(si); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1126 | si->err_type = SI_ET_NONE; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1127 | si->state = SI_ST_CLO; |
| 1128 | |
| 1129 | /* send the message */ |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 1130 | txn->status = 302; |
| 1131 | http_server_error(s, si, SF_ERR_LOCAL, SF_FINST_C, &trash); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1132 | |
| 1133 | /* 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] | 1134 | srv_inc_sess_ctr(srv); |
Bhaskar Maddala | a20cb85 | 2014-02-03 16:26:46 -0500 | [diff] [blame] | 1135 | srv_set_sess_last(srv); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1136 | } |
| 1137 | |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 1138 | /* Return the error message corresponding to si->err_type. It is assumed |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1139 | * that the server side is closed. Note that err_type is actually a |
| 1140 | * bitmask, where almost only aborts may be cumulated with other |
| 1141 | * values. We consider that aborted operations are more important |
| 1142 | * than timeouts or errors due to the fact that nobody else in the |
| 1143 | * logs might explain incomplete retries. All others should avoid |
| 1144 | * being cumulated. It should normally not be possible to have multiple |
| 1145 | * 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] | 1146 | * Note that connection errors appearing on the second request of a keep-alive |
| 1147 | * connection are not reported since this allows the client to retry. |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1148 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1149 | void http_return_srv_error(struct stream *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1150 | { |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 1151 | int err_type = si->err_type; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1152 | |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 1153 | /* set s->txn->status for http_error_message(s) */ |
| 1154 | s->txn->status = 503; |
| 1155 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1156 | if (err_type & SI_ET_QUEUE_ABRT) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1157 | http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q, |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 1158 | http_error_message(s)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1159 | else if (err_type & SI_ET_CONN_ABRT) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1160 | http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C, |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 1161 | (s->txn->flags & TX_NOT_FIRST) ? NULL : |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 1162 | http_error_message(s)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1163 | else if (err_type & SI_ET_QUEUE_TO) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1164 | http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q, |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 1165 | http_error_message(s)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1166 | else if (err_type & SI_ET_QUEUE_ERR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1167 | http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q, |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 1168 | http_error_message(s)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1169 | else if (err_type & SI_ET_CONN_TO) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1170 | http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C, |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 1171 | (s->txn->flags & TX_NOT_FIRST) ? NULL : |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 1172 | http_error_message(s)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1173 | else if (err_type & SI_ET_CONN_ERR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1174 | http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C, |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 1175 | (s->flags & SF_SRV_REUSED) ? NULL : |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 1176 | http_error_message(s)); |
Willy Tarreau | 2d400bb | 2012-05-14 12:11:47 +0200 | [diff] [blame] | 1177 | else if (err_type & SI_ET_CONN_RES) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1178 | http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C, |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 1179 | (s->txn->flags & TX_NOT_FIRST) ? NULL : |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 1180 | http_error_message(s)); |
| 1181 | else { /* SI_ET_CONN_OTHER and others */ |
| 1182 | s->txn->status = 500; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1183 | http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C, |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 1184 | http_error_message(s)); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 1185 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1186 | } |
| 1187 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1188 | extern const char sess_term_cond[8]; |
| 1189 | extern const char sess_fin_state[8]; |
| 1190 | extern const char *monthname[12]; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 1191 | struct pool_head *pool_head_http_txn; |
| 1192 | struct pool_head *pool_head_requri; |
| 1193 | struct pool_head *pool_head_capture = NULL; |
| 1194 | struct pool_head *pool_head_uniqueid; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1195 | |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1196 | /* |
| 1197 | * Capture headers from message starting at <som> according to header list |
Willy Tarreau | 54da8db | 2014-06-13 16:11:48 +0200 | [diff] [blame] | 1198 | * <cap_hdr>, and fill the <cap> pointers appropriately. |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1199 | */ |
| 1200 | void capture_headers(char *som, struct hdr_idx *idx, |
| 1201 | char **cap, struct cap_hdr *cap_hdr) |
| 1202 | { |
| 1203 | char *eol, *sol, *col, *sov; |
| 1204 | int cur_idx; |
| 1205 | struct cap_hdr *h; |
| 1206 | int len; |
| 1207 | |
| 1208 | sol = som + hdr_idx_first_pos(idx); |
| 1209 | cur_idx = hdr_idx_first_idx(idx); |
| 1210 | |
| 1211 | while (cur_idx) { |
| 1212 | eol = sol + idx->v[cur_idx].len; |
| 1213 | |
| 1214 | col = sol; |
| 1215 | while (col < eol && *col != ':') |
| 1216 | col++; |
| 1217 | |
| 1218 | sov = col + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 1219 | while (sov < eol && HTTP_IS_LWS(*sov)) |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1220 | sov++; |
| 1221 | |
| 1222 | for (h = cap_hdr; h; h = h->next) { |
Willy Tarreau | 54da8db | 2014-06-13 16:11:48 +0200 | [diff] [blame] | 1223 | if (h->namelen && (h->namelen == col - sol) && |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1224 | (strncasecmp(sol, h->name, h->namelen) == 0)) { |
| 1225 | if (cap[h->index] == NULL) |
| 1226 | cap[h->index] = |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 1227 | pool_alloc(h->pool); |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1228 | |
| 1229 | if (cap[h->index] == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1230 | ha_alert("HTTP capture : out of memory.\n"); |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1231 | continue; |
| 1232 | } |
| 1233 | |
| 1234 | len = eol - sov; |
| 1235 | if (len > h->len) |
| 1236 | len = h->len; |
| 1237 | |
| 1238 | memcpy(cap[h->index], sov, len); |
| 1239 | cap[h->index][len]=0; |
| 1240 | } |
| 1241 | } |
| 1242 | sol = eol + idx->v[cur_idx].cr + 1; |
| 1243 | cur_idx = idx->v[cur_idx].next; |
| 1244 | } |
| 1245 | } |
| 1246 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1247 | /* |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1248 | * Returns the data from Authorization header. Function may be called more |
| 1249 | * than once so data is stored in txn->auth_data. When no header is found |
| 1250 | * 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] | 1251 | * searching again for something we are unable to find anyway. However, if |
| 1252 | * 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] | 1253 | * have the credentials overwritten by another stream in parallel. |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1254 | */ |
| 1255 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1256 | int |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1257 | get_http_auth(struct stream *s) |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1258 | { |
| 1259 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 1260 | struct http_txn *txn = s->txn; |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1261 | struct chunk auth_method; |
| 1262 | struct hdr_ctx ctx; |
| 1263 | char *h, *p; |
| 1264 | int len; |
| 1265 | |
| 1266 | #ifdef DEBUG_AUTH |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1267 | printf("Auth for stream %p: %d\n", s, txn->auth.method); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1268 | #endif |
| 1269 | |
| 1270 | if (txn->auth.method == HTTP_AUTH_WRONG) |
| 1271 | return 0; |
| 1272 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1273 | txn->auth.method = HTTP_AUTH_WRONG; |
| 1274 | |
| 1275 | ctx.idx = 0; |
Willy Tarreau | 844a7e7 | 2010-01-31 21:46:18 +0100 | [diff] [blame] | 1276 | |
| 1277 | if (txn->flags & TX_USE_PX_CONN) { |
| 1278 | h = "Proxy-Authorization"; |
| 1279 | len = strlen(h); |
| 1280 | } else { |
| 1281 | h = "Authorization"; |
| 1282 | len = strlen(h); |
| 1283 | } |
| 1284 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1285 | 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] | 1286 | return 0; |
| 1287 | |
| 1288 | h = ctx.line + ctx.val; |
| 1289 | |
| 1290 | p = memchr(h, ' ', ctx.vlen); |
Willy Tarreau | 5c557d1 | 2016-03-13 08:17:02 +0100 | [diff] [blame] | 1291 | len = p - h; |
| 1292 | if (!p || len <= 0) |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1293 | return 0; |
| 1294 | |
David Carlier | 7365f7d | 2016-04-04 11:54:42 +0100 | [diff] [blame] | 1295 | if (chunk_initlen(&auth_method, h, 0, len) != 1) |
| 1296 | return 0; |
| 1297 | |
Willy Tarreau | 5c557d1 | 2016-03-13 08:17:02 +0100 | [diff] [blame] | 1298 | 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] | 1299 | |
| 1300 | if (!strncasecmp("Basic", auth_method.str, auth_method.len)) { |
Christopher Faulet | 6988f67 | 2017-07-27 15:18:52 +0200 | [diff] [blame] | 1301 | struct chunk *http_auth = get_trash_chunk(); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1302 | |
| 1303 | len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len, |
Christopher Faulet | 6988f67 | 2017-07-27 15:18:52 +0200 | [diff] [blame] | 1304 | http_auth->str, global.tune.bufsize - 1); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1305 | |
| 1306 | if (len < 0) |
| 1307 | return 0; |
| 1308 | |
| 1309 | |
Christopher Faulet | 6988f67 | 2017-07-27 15:18:52 +0200 | [diff] [blame] | 1310 | http_auth->str[len] = '\0'; |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1311 | |
Christopher Faulet | 6988f67 | 2017-07-27 15:18:52 +0200 | [diff] [blame] | 1312 | p = strchr(http_auth->str, ':'); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1313 | |
| 1314 | if (!p) |
| 1315 | return 0; |
| 1316 | |
Christopher Faulet | 6988f67 | 2017-07-27 15:18:52 +0200 | [diff] [blame] | 1317 | txn->auth.user = http_auth->str; |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1318 | *p = '\0'; |
| 1319 | txn->auth.pass = p+1; |
| 1320 | |
| 1321 | txn->auth.method = HTTP_AUTH_BASIC; |
| 1322 | return 1; |
| 1323 | } |
| 1324 | |
| 1325 | return 0; |
| 1326 | } |
| 1327 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1328 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1329 | /* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the |
| 1330 | * conversion succeeded, 0 in case of error. If the request was already 1.X, |
| 1331 | * nothing is done and 1 is returned. |
| 1332 | */ |
Willy Tarreau | 418bfcc | 2012-03-09 13:56:20 +0100 | [diff] [blame] | 1333 | static int http_upgrade_v09_to_v10(struct http_txn *txn) |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1334 | { |
| 1335 | int delta; |
| 1336 | char *cur_end; |
Willy Tarreau | 418bfcc | 2012-03-09 13:56:20 +0100 | [diff] [blame] | 1337 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1338 | |
| 1339 | if (msg->sl.rq.v_l != 0) |
| 1340 | return 1; |
| 1341 | |
Apollon Oikonomopoulos | 25a1522 | 2014-04-06 02:46:00 +0300 | [diff] [blame] | 1342 | /* RFC 1945 allows only GET for HTTP/0.9 requests */ |
| 1343 | if (txn->meth != HTTP_METH_GET) |
| 1344 | return 0; |
| 1345 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1346 | cur_end = msg->chn->buf->p + msg->sl.rq.l; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1347 | |
| 1348 | if (msg->sl.rq.u_l == 0) { |
Apollon Oikonomopoulos | 25a1522 | 2014-04-06 02:46:00 +0300 | [diff] [blame] | 1349 | /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */ |
| 1350 | return 0; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1351 | } |
| 1352 | /* add HTTP version */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1353 | 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] | 1354 | http_msg_move_end(msg, delta); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1355 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1356 | cur_end = (char *)http_parse_reqline(msg, |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1357 | HTTP_MSG_RQMETH, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1358 | msg->chn->buf->p, cur_end + 1, |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1359 | NULL, NULL); |
| 1360 | if (unlikely(!cur_end)) |
| 1361 | return 0; |
| 1362 | |
| 1363 | /* we have a full HTTP/1.0 request now and we know that |
| 1364 | * we have either a CR or an LF at <ptr>. |
| 1365 | */ |
| 1366 | hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r'); |
| 1367 | return 1; |
| 1368 | } |
| 1369 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1370 | /* Parse the Connection: header of an HTTP request, looking for both "close" |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1371 | * and "keep-alive" values. If we already know that some headers may safely |
| 1372 | * 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] | 1373 | * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses) |
| 1374 | * - 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] | 1375 | * Presence of the "Upgrade" token is also checked and reported. |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1376 | * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was |
| 1377 | * found, and TX_CON_*_SET is adjusted depending on what is left so only |
| 1378 | * harmless combinations may be removed. Do not call that after changes have |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1379 | * been processed. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1380 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1381 | 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] | 1382 | { |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1383 | struct hdr_ctx ctx; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1384 | const char *hdr_val = "Connection"; |
| 1385 | int hdr_len = 10; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1386 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1387 | if (txn->flags & TX_HDR_CONN_PRS) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1388 | return; |
| 1389 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1390 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1391 | hdr_val = "Proxy-Connection"; |
| 1392 | hdr_len = 16; |
| 1393 | } |
| 1394 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1395 | ctx.idx = 0; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1396 | txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1397 | 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] | 1398 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 1399 | txn->flags |= TX_HDR_CONN_KAL; |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1400 | if (to_del & 2) |
| 1401 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1402 | else |
| 1403 | txn->flags |= TX_CON_KAL_SET; |
| 1404 | } |
| 1405 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 1406 | txn->flags |= TX_HDR_CONN_CLO; |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1407 | if (to_del & 1) |
| 1408 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1409 | else |
| 1410 | txn->flags |= TX_CON_CLO_SET; |
| 1411 | } |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 1412 | else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) { |
| 1413 | txn->flags |= TX_HDR_CONN_UPG; |
| 1414 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1415 | } |
| 1416 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1417 | txn->flags |= TX_HDR_CONN_PRS; |
| 1418 | return; |
| 1419 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1420 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1421 | /* Apply desired changes on the Connection: header. Values may be removed and/or |
| 1422 | * added depending on the <wanted> flags, which are exclusively composed of |
| 1423 | * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The |
| 1424 | * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left. |
| 1425 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1426 | 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] | 1427 | { |
| 1428 | struct hdr_ctx ctx; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1429 | const char *hdr_val = "Connection"; |
| 1430 | int hdr_len = 10; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1431 | |
| 1432 | ctx.idx = 0; |
| 1433 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1434 | |
| 1435 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1436 | hdr_val = "Proxy-Connection"; |
| 1437 | hdr_len = 16; |
| 1438 | } |
| 1439 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1440 | txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1441 | 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] | 1442 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 1443 | if (wanted & TX_CON_KAL_SET) |
| 1444 | txn->flags |= TX_CON_KAL_SET; |
| 1445 | else |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1446 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1447 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1448 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 1449 | if (wanted & TX_CON_CLO_SET) |
| 1450 | txn->flags |= TX_CON_CLO_SET; |
| 1451 | else |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1452 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 1453 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1454 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1455 | |
| 1456 | if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 1457 | return; |
| 1458 | |
| 1459 | if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) { |
| 1460 | txn->flags |= TX_CON_CLO_SET; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1461 | hdr_val = "Connection: close"; |
| 1462 | hdr_len = 17; |
| 1463 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1464 | hdr_val = "Proxy-Connection: close"; |
| 1465 | hdr_len = 23; |
| 1466 | } |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1467 | http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1468 | } |
| 1469 | |
| 1470 | if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) { |
| 1471 | txn->flags |= TX_CON_KAL_SET; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1472 | hdr_val = "Connection: keep-alive"; |
| 1473 | hdr_len = 22; |
| 1474 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1475 | hdr_val = "Proxy-Connection: keep-alive"; |
| 1476 | hdr_len = 28; |
| 1477 | } |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1478 | http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1479 | } |
| 1480 | return; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1481 | } |
| 1482 | |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 1483 | /* Parses a qvalue and returns it multipled by 1000, from 0 to 1000. If the |
| 1484 | * value is larger than 1000, it is bound to 1000. The parser consumes up to |
| 1485 | * 1 digit, one dot and 3 digits and stops on the first invalid character. |
| 1486 | * Unparsable qvalues return 1000 as "q=1.000". |
| 1487 | */ |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 1488 | int parse_qvalue(const char *qvalue, const char **end) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 1489 | { |
| 1490 | int q = 1000; |
| 1491 | |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 1492 | if (!isdigit((unsigned char)*qvalue)) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 1493 | goto out; |
| 1494 | q = (*qvalue++ - '0') * 1000; |
| 1495 | |
| 1496 | if (*qvalue++ != '.') |
| 1497 | goto out; |
| 1498 | |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 1499 | if (!isdigit((unsigned char)*qvalue)) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 1500 | goto out; |
| 1501 | q += (*qvalue++ - '0') * 100; |
| 1502 | |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 1503 | if (!isdigit((unsigned char)*qvalue)) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 1504 | goto out; |
| 1505 | q += (*qvalue++ - '0') * 10; |
| 1506 | |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 1507 | if (!isdigit((unsigned char)*qvalue)) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 1508 | goto out; |
| 1509 | q += (*qvalue++ - '0') * 1; |
| 1510 | out: |
| 1511 | if (q > 1000) |
| 1512 | q = 1000; |
Willy Tarreau | 38b3aa5 | 2014-04-22 23:32:05 +0200 | [diff] [blame] | 1513 | if (end) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 1514 | *end = qvalue; |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 1515 | return q; |
| 1516 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 1517 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1518 | 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] | 1519 | { |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 1520 | struct proxy *fe = strm_fe(s); |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 1521 | int tmp = TX_CON_WANT_KAL; |
| 1522 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1523 | if (!((fe->options2|s->be->options2) & PR_O2_FAKE_KA)) { |
| 1524 | if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN || |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 1525 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN) |
| 1526 | tmp = TX_CON_WANT_TUN; |
| 1527 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1528 | if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 1529 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL) |
| 1530 | tmp = TX_CON_WANT_TUN; |
| 1531 | } |
| 1532 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1533 | if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL || |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 1534 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) { |
| 1535 | /* option httpclose + server_close => forceclose */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1536 | if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 1537 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL) |
| 1538 | tmp = TX_CON_WANT_CLO; |
| 1539 | else |
| 1540 | tmp = TX_CON_WANT_SCL; |
| 1541 | } |
| 1542 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1543 | if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL || |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 1544 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL) |
| 1545 | tmp = TX_CON_WANT_CLO; |
| 1546 | |
| 1547 | if ((txn->flags & TX_CON_WANT_MSK) < tmp) |
| 1548 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp; |
| 1549 | |
| 1550 | if (!(txn->flags & TX_HDR_CONN_PRS) && |
| 1551 | (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) { |
| 1552 | /* parse the Connection header and possibly clean it */ |
| 1553 | int to_del = 0; |
| 1554 | if ((msg->flags & HTTP_MSGF_VER_11) || |
| 1555 | ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1556 | !((fe->options2|s->be->options2) & PR_O2_FAKE_KA))) |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 1557 | to_del |= 2; /* remove "keep-alive" */ |
| 1558 | if (!(msg->flags & HTTP_MSGF_VER_11)) |
| 1559 | to_del |= 1; /* remove "close" */ |
| 1560 | http_parse_connection_header(txn, msg, to_del); |
| 1561 | } |
| 1562 | |
| 1563 | /* check if client or config asks for explicit close in KAL/SCL */ |
| 1564 | if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 1565 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) && |
| 1566 | ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */ |
| 1567 | (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */ |
| 1568 | !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1569 | fe->state == PR_STSTOPPED)) /* frontend is stopping */ |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 1570 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 1571 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 1572 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 1573 | /* This stream analyser waits for a complete HTTP request. It returns 1 if the |
| 1574 | * processing can continue on next analysers, or zero if it either needs more |
| 1575 | * data or wants to immediately abort the request (eg: timeout, error, ...). It |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1576 | * 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] | 1577 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 1578 | * abort. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1579 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1580 | 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] | 1581 | { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1582 | /* |
| 1583 | * We will parse the partial (or complete) lines. |
| 1584 | * We will check the request syntax, and also join multi-line |
| 1585 | * headers. An index of all the lines will be elaborated while |
| 1586 | * parsing. |
| 1587 | * |
| 1588 | * For the parsing, we use a 28 states FSM. |
| 1589 | * |
| 1590 | * Here is the information we currently have : |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1591 | * req->buf->p = beginning of request |
| 1592 | * req->buf->p + msg->eoh = end of processed headers / start of current one |
| 1593 | * req->buf->p + req->buf->i = end of input data |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 1594 | * msg->eol = end of current header or line (LF or CRLF) |
| 1595 | * msg->next = first non-visited byte |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 1596 | * |
| 1597 | * 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] | 1598 | * we will set a few fields (txn->meth, sn->flags/SF_REDIRECTABLE). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 1599 | * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and |
| 1600 | * finally headers capture. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1601 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1602 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1603 | int cur_idx; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 1604 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 1605 | struct http_txn *txn = s->txn; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1606 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 1607 | struct hdr_ctx ctx; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1608 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1609 | 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] | 1610 | now_ms, __FUNCTION__, |
| 1611 | s, |
| 1612 | req, |
| 1613 | req->rex, req->wex, |
| 1614 | req->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1615 | req->buf->i, |
Willy Tarreau | 6bf1736 | 2009-02-24 10:48:35 +0100 | [diff] [blame] | 1616 | req->analysers); |
| 1617 | |
Willy Tarreau | 52a0c60 | 2009-08-16 22:45:38 +0200 | [diff] [blame] | 1618 | /* we're speaking HTTP here, so let's speak HTTP to the client */ |
| 1619 | s->srv_error = http_return_srv_error; |
| 1620 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 1621 | /* There's a protected area at the end of the buffer for rewriting |
| 1622 | * purposes. We don't want to start to parse the request if the |
| 1623 | * protected area is affected, because we may have to move processed |
| 1624 | * data later, which is much more complicated. |
| 1625 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1626 | 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] | 1627 | |
| 1628 | /* This point is executed when some data is avalaible for analysis, |
| 1629 | * so we log the end of the idle time. */ |
| 1630 | if (s->logs.t_idle == -1) |
| 1631 | s->logs.t_idle = tv_ms_elapsed(&s->logs.tv_accept, &now) - s->logs.t_handshake; |
| 1632 | |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 1633 | if (txn->flags & TX_NOT_FIRST) { |
Willy Tarreau | ba0902e | 2015-01-13 14:39:16 +0100 | [diff] [blame] | 1634 | if (unlikely(!channel_is_rewritable(req))) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 1635 | 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] | 1636 | goto failed_keep_alive; |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 1637 | /* 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] | 1638 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 1639 | req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | d7ad9f5 | 2013-12-31 17:26:25 +0100 | [diff] [blame] | 1640 | req->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 1641 | return 0; |
| 1642 | } |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 1643 | if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) || |
| 1644 | bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite)) |
| 1645 | buffer_slow_realign(req->buf); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 1646 | } |
| 1647 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1648 | if (likely(msg->next < req->buf->i)) /* some unparsed data are available */ |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 1649 | http_msg_analyzer(msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 1650 | } |
| 1651 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1652 | /* 1: we might have to print this header in debug mode */ |
| 1653 | if (unlikely((global.mode & MODE_DEBUG) && |
| 1654 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | 7d59e90 | 2014-10-21 19:36:09 +0200 | [diff] [blame] | 1655 | msg->msg_state >= HTTP_MSG_BODY)) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1656 | char *eol, *sol; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1657 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1658 | sol = req->buf->p; |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 1659 | /* this is a bit complex : in case of error on the request line, |
| 1660 | * we know that rq.l is still zero, so we display only the part |
| 1661 | * up to the end of the line (truncated by debug_hdr). |
| 1662 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1663 | 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] | 1664 | debug_hdr("clireq", s, sol, eol); |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 1665 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1666 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 1667 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1668 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1669 | while (cur_idx) { |
| 1670 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 1671 | debug_hdr("clihdr", s, sol, eol); |
| 1672 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 1673 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1674 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1675 | } |
| 1676 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1677 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1678 | /* |
| 1679 | * Now we quickly check if we have found a full valid request. |
| 1680 | * If not so, we check the FD and buffer states before leaving. |
| 1681 | * A full request is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 1682 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 1683 | * requests are checked first. When waiting for a second request |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1684 | * on a keep-alive stream, if we encounter and error, close, t/o, |
| 1685 | * 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] | 1686 | * 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] | 1687 | * process_stream() as a frontend error. |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 1688 | * Last, we may increase some tracked counters' http request errors on |
| 1689 | * the cases that are deliberately the client's fault. For instance, |
| 1690 | * a timeout or connection reset is not counted as an error. However |
| 1691 | * a bad request is. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1692 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1693 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 1694 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1695 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1696 | * First, let's catch bad requests. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1697 | */ |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 1698 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1699 | stream_inc_http_req_ctr(s); |
| 1700 | stream_inc_http_err_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1701 | proxy_inc_fe_req_ctr(sess->fe); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1702 | goto return_bad_req; |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 1703 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1704 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1705 | /* 1: Since we are in header mode, if there's no space |
| 1706 | * left for headers, we won't be able to free more |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1707 | * later, so the stream will never terminate. We |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1708 | * must terminate it now. |
| 1709 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1710 | if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1711 | /* FIXME: check if URI is set and return Status |
| 1712 | * 414 Request URI too long instead. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1713 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1714 | stream_inc_http_req_ctr(s); |
| 1715 | stream_inc_http_err_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1716 | proxy_inc_fe_req_ctr(sess->fe); |
Willy Tarreau | fec4d89 | 2011-09-02 20:04:57 +0200 | [diff] [blame] | 1717 | if (msg->err_pos < 0) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1718 | msg->err_pos = req->buf->i; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1719 | goto return_bad_req; |
| 1720 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1721 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1722 | /* 2: have we encountered a read error ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 1723 | else if (req->flags & CF_READ_ERROR) { |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1724 | if (!(s->flags & SF_ERR_MASK)) |
| 1725 | s->flags |= SF_ERR_CLICL; |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 1726 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 1727 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 1728 | goto failed_keep_alive; |
| 1729 | |
Willy Tarreau | 0f228a0 | 2015-05-01 15:37:53 +0200 | [diff] [blame] | 1730 | if (sess->fe->options & PR_O_IGNORE_PRB) |
| 1731 | goto failed_keep_alive; |
| 1732 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1733 | /* we cannot return any message on error */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 1734 | if (msg->err_pos >= 0) { |
Emeric Brun | 8c1aaa2 | 2017-06-15 11:30:06 +0200 | [diff] [blame] | 1735 | 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] | 1736 | stream_inc_http_err_ctr(s); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 1737 | } |
| 1738 | |
Willy Tarreau | dc979f2 | 2012-12-04 10:39:01 +0100 | [diff] [blame] | 1739 | txn->status = 400; |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 1740 | msg->err_state = msg->msg_state; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1741 | msg->msg_state = HTTP_MSG_ERROR; |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 1742 | http_reply_and_close(s, txn->status, NULL); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 1743 | req->analysers &= AN_REQ_FLT_END; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1744 | stream_inc_http_req_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1745 | proxy_inc_fe_req_ctr(sess->fe); |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 1746 | HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 1747 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 1748 | HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 1749 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1750 | if (!(s->flags & SF_FINST_MASK)) |
| 1751 | s->flags |= SF_FINST_R; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1752 | return 0; |
| 1753 | } |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 1754 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1755 | /* 3: has the read timeout expired ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 1756 | 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] | 1757 | if (!(s->flags & SF_ERR_MASK)) |
| 1758 | s->flags |= SF_ERR_CLITO; |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 1759 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 1760 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 1761 | goto failed_keep_alive; |
| 1762 | |
Willy Tarreau | 0f228a0 | 2015-05-01 15:37:53 +0200 | [diff] [blame] | 1763 | if (sess->fe->options & PR_O_IGNORE_PRB) |
| 1764 | goto failed_keep_alive; |
| 1765 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1766 | /* read timeout : give up with an error message. */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 1767 | if (msg->err_pos >= 0) { |
Emeric Brun | 8c1aaa2 | 2017-06-15 11:30:06 +0200 | [diff] [blame] | 1768 | 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] | 1769 | stream_inc_http_err_ctr(s); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 1770 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1771 | txn->status = 408; |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 1772 | msg->err_state = msg->msg_state; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1773 | msg->msg_state = HTTP_MSG_ERROR; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 1774 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 1775 | req->analysers &= AN_REQ_FLT_END; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 1776 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1777 | stream_inc_http_req_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1778 | proxy_inc_fe_req_ctr(sess->fe); |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 1779 | HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 1780 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 1781 | HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 1782 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1783 | if (!(s->flags & SF_FINST_MASK)) |
| 1784 | s->flags |= SF_FINST_R; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1785 | return 0; |
| 1786 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1787 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1788 | /* 4: have we encountered a close ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 1789 | else if (req->flags & CF_SHUTR) { |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1790 | if (!(s->flags & SF_ERR_MASK)) |
| 1791 | s->flags |= SF_ERR_CLICL; |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 1792 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 1793 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 1794 | goto failed_keep_alive; |
| 1795 | |
Willy Tarreau | 0f228a0 | 2015-05-01 15:37:53 +0200 | [diff] [blame] | 1796 | if (sess->fe->options & PR_O_IGNORE_PRB) |
| 1797 | goto failed_keep_alive; |
| 1798 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 1799 | if (msg->err_pos >= 0) |
Emeric Brun | 8c1aaa2 | 2017-06-15 11:30:06 +0200 | [diff] [blame] | 1800 | 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] | 1801 | txn->status = 400; |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 1802 | msg->err_state = msg->msg_state; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1803 | msg->msg_state = HTTP_MSG_ERROR; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 1804 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 1805 | req->analysers &= AN_REQ_FLT_END; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1806 | stream_inc_http_err_ctr(s); |
| 1807 | stream_inc_http_req_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1808 | proxy_inc_fe_req_ctr(sess->fe); |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 1809 | HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 1810 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 1811 | HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 1812 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1813 | if (!(s->flags & SF_FINST_MASK)) |
| 1814 | s->flags |= SF_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 1815 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1816 | } |
| 1817 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 1818 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 1819 | req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1820 | s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 1821 | #ifdef TCP_QUICKACK |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 1822 | if (sess->listener->options & LI_O_NOQUICKACK && req->buf->i && |
| 1823 | objt_conn(sess->origin) && conn_ctrl_ready(__objt_conn(sess->origin))) { |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 1824 | /* We need more data, we have to re-enable quick-ack in case we |
| 1825 | * previously disabled it, otherwise we might cause the client |
| 1826 | * to delay next data. |
| 1827 | */ |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 1828 | 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] | 1829 | } |
| 1830 | #endif |
Willy Tarreau | 1b194fe | 2009-03-21 21:10:04 +0100 | [diff] [blame] | 1831 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 1832 | if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) { |
| 1833 | /* If the client starts to talk, let's fall back to |
| 1834 | * request timeout processing. |
| 1835 | */ |
| 1836 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 1837 | req->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 1838 | } |
| 1839 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1840 | /* just set the request timeout once at the beginning of the request */ |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 1841 | if (!tick_isset(req->analyse_exp)) { |
| 1842 | if ((msg->msg_state == HTTP_MSG_RQBEFORE) && |
| 1843 | (txn->flags & TX_WAIT_NEXT_RQ) && |
| 1844 | tick_isset(s->be->timeout.httpka)) |
| 1845 | req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka); |
| 1846 | else |
| 1847 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
| 1848 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1849 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1850 | /* we're not ready yet */ |
| 1851 | return 0; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 1852 | |
| 1853 | failed_keep_alive: |
| 1854 | /* Here we process low-level errors for keep-alive requests. In |
| 1855 | * short, if the request is not the first one and it experiences |
| 1856 | * a timeout, read error or shutdown, we just silently close so |
| 1857 | * that the client can try again. |
| 1858 | */ |
| 1859 | txn->status = 0; |
| 1860 | msg->msg_state = HTTP_MSG_RQBEFORE; |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 1861 | req->analysers &= AN_REQ_FLT_END; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 1862 | s->logs.logwait = 0; |
Willy Tarreau | abcd514 | 2013-06-11 17:18:02 +0200 | [diff] [blame] | 1863 | s->logs.level = 0; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1864 | s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 1865 | http_reply_and_close(s, txn->status, NULL); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 1866 | return 0; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1867 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1868 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 1869 | /* OK now we have a complete HTTP request with indexed headers. Let's |
| 1870 | * complete the request parsing by setting a few fields we will need |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1871 | * 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] | 1872 | * 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] | 1873 | * 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] | 1874 | * byte after the last LF. msg->sov points to the first byte of data. |
| 1875 | * msg->eol cannot be trusted because it may have been left uninitialized |
| 1876 | * (for instance in the absence of headers). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 1877 | */ |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1878 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1879 | stream_inc_http_req_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1880 | 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] | 1881 | |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 1882 | if (txn->flags & TX_WAIT_NEXT_RQ) { |
| 1883 | /* kill the pending keep-alive timeout */ |
| 1884 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
| 1885 | req->analyse_exp = TICK_ETERNITY; |
| 1886 | } |
| 1887 | |
| 1888 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 1889 | /* Maybe we found in invalid header name while we were configured not |
| 1890 | * to block on that, so we have to capture it now. |
| 1891 | */ |
| 1892 | if (unlikely(msg->err_pos >= 0)) |
Emeric Brun | 8c1aaa2 | 2017-06-15 11:30:06 +0200 | [diff] [blame] | 1893 | 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] | 1894 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1895 | /* |
| 1896 | * 1: identify the method |
| 1897 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1898 | 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] | 1899 | |
| 1900 | /* we can make use of server redirect on GET and HEAD */ |
| 1901 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1902 | s->flags |= SF_REDIRECTABLE; |
Willy Tarreau | 9165979 | 2017-11-10 19:38:10 +0100 | [diff] [blame] | 1903 | else if (txn->meth == HTTP_METH_OTHER && |
| 1904 | msg->sl.rq.m_l == 3 && memcmp(req->buf->p, "PRI", 3) == 0) { |
| 1905 | /* PRI is reserved for the HTTP/2 preface */ |
| 1906 | msg->err_pos = 0; |
| 1907 | goto return_bad_req; |
| 1908 | } |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 1909 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1910 | /* |
| 1911 | * 2: check if the URI matches the monitor_uri. |
| 1912 | * We have to do this for every request which gets in, because |
| 1913 | * the monitor-uri is defined by the frontend. |
| 1914 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1915 | if (unlikely((sess->fe->monitor_uri_len != 0) && |
| 1916 | (sess->fe->monitor_uri_len == msg->sl.rq.u_l) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1917 | !memcmp(req->buf->p + msg->sl.rq.u, |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1918 | sess->fe->monitor_uri, |
| 1919 | sess->fe->monitor_uri_len))) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1920 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1921 | * We have found the monitor URI |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1922 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1923 | struct acl_cond *cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 1924 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1925 | s->flags |= SF_MONITOR; |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 1926 | HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1); |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 1927 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1928 | /* Check if we want to fail this monitor request or not */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1929 | list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 1930 | 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] | 1931 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1932 | ret = acl_pass(ret); |
| 1933 | if (cond->pol == ACL_COND_UNLESS) |
| 1934 | ret = !ret; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 1935 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1936 | if (ret) { |
| 1937 | /* we fail this request, let's return 503 service unavail */ |
| 1938 | txn->status = 503; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 1939 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1940 | if (!(s->flags & SF_ERR_MASK)) |
| 1941 | 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] | 1942 | goto return_prx_cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 1943 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1944 | } |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 1945 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1946 | /* nothing to fail, let's reply normaly */ |
| 1947 | txn->status = 200; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 1948 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1949 | if (!(s->flags & SF_ERR_MASK)) |
| 1950 | 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] | 1951 | goto return_prx_cond; |
| 1952 | } |
| 1953 | |
| 1954 | /* |
| 1955 | * 3: Maybe we have to copy the original REQURI for the logs ? |
| 1956 | * Note: we cannot log anymore if the request has been |
| 1957 | * classified as invalid. |
| 1958 | */ |
| 1959 | if (unlikely(s->logs.logwait & LW_REQ)) { |
| 1960 | /* we have a complete HTTP request that we must log */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 1961 | if ((txn->uri = pool_alloc(pool_head_requri)) != NULL) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1962 | int urilen = msg->sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1963 | |
Stéphane Cottin | 23e9e93 | 2017-05-18 08:58:41 +0200 | [diff] [blame] | 1964 | if (urilen >= global.tune.requri_len ) |
| 1965 | urilen = global.tune.requri_len - 1; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1966 | memcpy(txn->uri, req->buf->p, urilen); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1967 | txn->uri[urilen] = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1968 | |
Willy Tarreau | d79a3b2 | 2012-12-28 09:40:16 +0100 | [diff] [blame] | 1969 | if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT))) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1970 | s->do_log(s); |
| 1971 | } else { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1972 | ha_alert("HTTP logging : out of memory.\n"); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1973 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1974 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1975 | |
Willy Tarreau | 91852eb | 2015-05-01 13:26:00 +0200 | [diff] [blame] | 1976 | /* RFC7230#2.6 has enforced the format of the HTTP version string to be |
| 1977 | * exactly one digit "." one digit. This check may be disabled using |
| 1978 | * option accept-invalid-http-request. |
| 1979 | */ |
| 1980 | if (!(sess->fe->options2 & PR_O2_REQBUG_OK)) { |
| 1981 | if (msg->sl.rq.v_l != 8) { |
| 1982 | msg->err_pos = msg->sl.rq.v; |
| 1983 | goto return_bad_req; |
| 1984 | } |
| 1985 | |
| 1986 | if (req->buf->p[msg->sl.rq.v + 4] != '/' || |
| 1987 | !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 5]) || |
| 1988 | req->buf->p[msg->sl.rq.v + 6] != '.' || |
| 1989 | !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 7])) { |
| 1990 | msg->err_pos = msg->sl.rq.v + 4; |
| 1991 | goto return_bad_req; |
| 1992 | } |
| 1993 | } |
Willy Tarreau | 1331766 | 2015-05-01 13:47:08 +0200 | [diff] [blame] | 1994 | else { |
| 1995 | /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */ |
| 1996 | if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn)) |
| 1997 | goto return_bad_req; |
| 1998 | } |
Willy Tarreau | 91852eb | 2015-05-01 13:26:00 +0200 | [diff] [blame] | 1999 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2000 | /* ... and check if the request is HTTP/1.1 or above */ |
| 2001 | if ((msg->sl.rq.v_l == 8) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2002 | ((req->buf->p[msg->sl.rq.v + 5] > '1') || |
| 2003 | ((req->buf->p[msg->sl.rq.v + 5] == '1') && |
| 2004 | (req->buf->p[msg->sl.rq.v + 7] >= '1')))) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2005 | msg->flags |= HTTP_MSGF_VER_11; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2006 | |
| 2007 | /* "connection" has not been parsed yet */ |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 2008 | 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] | 2009 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2010 | /* if the frontend has "option http-use-proxy-header", we'll check if |
| 2011 | * we have what looks like a proxied connection instead of a connection, |
| 2012 | * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection. |
| 2013 | * Note that this is *not* RFC-compliant, however browsers and proxies |
| 2014 | * happen to do that despite being non-standard :-( |
| 2015 | * We consider that a request not beginning with either '/' or '*' is |
| 2016 | * a proxied connection, which covers both "scheme://location" and |
| 2017 | * CONNECT ip:port. |
| 2018 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2019 | if ((sess->fe->options2 & PR_O2_USE_PXHDR) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2020 | 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] | 2021 | txn->flags |= TX_USE_PX_CONN; |
| 2022 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2023 | /* transfer length unknown*/ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2024 | msg->flags &= ~HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2025 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2026 | /* 5: we may need to capture headers */ |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 2027 | if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap)) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2028 | capture_headers(req->buf->p, &txn->hdr_idx, |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 2029 | s->req_cap, sess->fe->req_cap); |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 2030 | |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 2031 | /* 6: determine the transfer-length according to RFC2616 #4.4, updated |
| 2032 | * by RFC7230#3.3.3 : |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2033 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 2034 | * The length of a message body is determined by one of the following |
| 2035 | * (in order of precedence): |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2036 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 2037 | * 1. Any response to a HEAD request and any response with a 1xx |
| 2038 | * (Informational), 204 (No Content), or 304 (Not Modified) status |
| 2039 | * code is always terminated by the first empty line after the |
| 2040 | * header fields, regardless of the header fields present in the |
| 2041 | * message, and thus cannot contain a message body. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2042 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 2043 | * 2. Any 2xx (Successful) response to a CONNECT request implies that |
| 2044 | * the connection will become a tunnel immediately after the empty |
| 2045 | * line that concludes the header fields. A client MUST ignore any |
| 2046 | * Content-Length or Transfer-Encoding header fields received in |
| 2047 | * such a message. |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2048 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 2049 | * 3. If a Transfer-Encoding header field is present and the chunked |
| 2050 | * transfer coding (Section 4.1) is the final encoding, the message |
| 2051 | * body length is determined by reading and decoding the chunked |
| 2052 | * data until the transfer coding indicates the data is complete. |
| 2053 | * |
| 2054 | * If a Transfer-Encoding header field is present in a response and |
| 2055 | * the chunked transfer coding is not the final encoding, the |
| 2056 | * message body length is determined by reading the connection until |
| 2057 | * it is closed by the server. If a Transfer-Encoding header field |
| 2058 | * is present in a request and the chunked transfer coding is not |
| 2059 | * the final encoding, the message body length cannot be determined |
| 2060 | * reliably; the server MUST respond with the 400 (Bad Request) |
| 2061 | * status code and then close the connection. |
| 2062 | * |
| 2063 | * If a message is received with both a Transfer-Encoding and a |
| 2064 | * Content-Length header field, the Transfer-Encoding overrides the |
| 2065 | * Content-Length. Such a message might indicate an attempt to |
| 2066 | * perform request smuggling (Section 9.5) or response splitting |
| 2067 | * (Section 9.4) and ought to be handled as an error. A sender MUST |
| 2068 | * remove the received Content-Length field prior to forwarding such |
| 2069 | * a message downstream. |
| 2070 | * |
| 2071 | * 4. If a message is received without Transfer-Encoding and with |
| 2072 | * either multiple Content-Length header fields having differing |
| 2073 | * field-values or a single Content-Length header field having an |
| 2074 | * invalid value, then the message framing is invalid and the |
| 2075 | * recipient MUST treat it as an unrecoverable error. If this is a |
| 2076 | * request message, the server MUST respond with a 400 (Bad Request) |
| 2077 | * status code and then close the connection. If this is a response |
| 2078 | * message received by a proxy, the proxy MUST close the connection |
| 2079 | * to the server, discard the received response, and send a 502 (Bad |
| 2080 | * Gateway) response to the client. If this is a response message |
| 2081 | * received by a user agent, the user agent MUST close the |
| 2082 | * connection to the server and discard the received response. |
| 2083 | * |
| 2084 | * 5. If a valid Content-Length header field is present without |
| 2085 | * Transfer-Encoding, its decimal value defines the expected message |
| 2086 | * body length in octets. If the sender closes the connection or |
| 2087 | * the recipient times out before the indicated number of octets are |
| 2088 | * received, the recipient MUST consider the message to be |
| 2089 | * incomplete and close the connection. |
| 2090 | * |
| 2091 | * 6. If this is a request message and none of the above are true, then |
| 2092 | * the message body length is zero (no message body is present). |
| 2093 | * |
| 2094 | * 7. Otherwise, this is a response message without a declared message |
| 2095 | * body length, so the message body length is determined by the |
| 2096 | * number of octets received prior to the server closing the |
| 2097 | * connection. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2098 | */ |
| 2099 | |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2100 | ctx.idx = 0; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2101 | /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */ |
Willy Tarreau | 4979d5c | 2015-05-01 10:06:30 +0200 | [diff] [blame] | 2102 | 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] | 2103 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
Christopher Faulet | be821b9 | 2017-03-30 11:21:53 +0200 | [diff] [blame] | 2104 | msg->flags |= HTTP_MSGF_TE_CHNK; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2105 | else if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | 34dfc60 | 2015-05-01 10:09:49 +0200 | [diff] [blame] | 2106 | /* chunked not last, return badreq */ |
| 2107 | goto return_bad_req; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2108 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2109 | } |
| 2110 | |
Willy Tarreau | 1c91391 | 2015-04-30 10:57:51 +0200 | [diff] [blame] | 2111 | /* Chunked requests must have their content-length removed */ |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2112 | ctx.idx = 0; |
Willy Tarreau | 1c91391 | 2015-04-30 10:57:51 +0200 | [diff] [blame] | 2113 | if (msg->flags & HTTP_MSGF_TE_CHNK) { |
| 2114 | while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) |
| 2115 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 2116 | } |
Willy Tarreau | 34dfc60 | 2015-05-01 10:09:49 +0200 | [diff] [blame] | 2117 | 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] | 2118 | signed long long cl; |
| 2119 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2120 | if (!ctx.vlen) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2121 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2122 | goto return_bad_req; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2123 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2124 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2125 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2126 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2127 | goto return_bad_req; /* parse failure */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2128 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2129 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2130 | if (cl < 0) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2131 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2132 | goto return_bad_req; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2133 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2134 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2135 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2136 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2137 | goto return_bad_req; /* already specified, was different */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2138 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2139 | |
Christopher Faulet | be821b9 | 2017-03-30 11:21:53 +0200 | [diff] [blame] | 2140 | msg->flags |= HTTP_MSGF_CNT_LEN; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 2141 | msg->body_len = msg->chunk_len = cl; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2142 | } |
| 2143 | |
Willy Tarreau | 34dfc60 | 2015-05-01 10:09:49 +0200 | [diff] [blame] | 2144 | /* even bodyless requests have a known length */ |
| 2145 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2146 | |
Willy Tarreau | 179085c | 2014-04-28 16:48:56 +0200 | [diff] [blame] | 2147 | /* Until set to anything else, the connection mode is set as Keep-Alive. It will |
| 2148 | * only change if both the request and the config reference something else. |
| 2149 | * Option httpclose by itself sets tunnel mode where headers are mangled. |
| 2150 | * However, if another mode is set, it will affect it (eg: server-close/ |
| 2151 | * keep-alive + httpclose = close). Note that we avoid to redo the same work |
| 2152 | * if FE and BE have the same settings (common). The method consists in |
| 2153 | * checking if options changed between the two calls (implying that either |
| 2154 | * one is non-null, or one of them is non-null and we are there for the first |
| 2155 | * time. |
| 2156 | */ |
| 2157 | if (!(txn->flags & TX_HDR_CONN_PRS) || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2158 | ((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] | 2159 | http_adjust_conn_mode(s, txn, msg); |
Willy Tarreau | 179085c | 2014-04-28 16:48:56 +0200 | [diff] [blame] | 2160 | |
Willy Tarreau | 9fbe18e | 2015-05-01 22:42:08 +0200 | [diff] [blame] | 2161 | /* we may have to wait for the request's body */ |
| 2162 | if ((s->be->options & PR_O_WREQ_BODY) && |
| 2163 | (msg->body_len || (msg->flags & HTTP_MSGF_TE_CHNK))) |
| 2164 | req->analysers |= AN_REQ_HTTP_BODY; |
| 2165 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2166 | /* end of job, return OK */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 2167 | req->analysers &= ~an_bit; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2168 | req->analyse_exp = TICK_ETERNITY; |
| 2169 | return 1; |
| 2170 | |
| 2171 | return_bad_req: |
| 2172 | /* We centralize bad requests processing here */ |
| 2173 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 2174 | /* we detected a parsing error. We want to archive this request |
| 2175 | * in the dedicated proxy area for later troubleshooting. |
| 2176 | */ |
Emeric Brun | 8c1aaa2 | 2017-06-15 11:30:06 +0200 | [diff] [blame] | 2177 | 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] | 2178 | } |
| 2179 | |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 2180 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2181 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 2182 | txn->status = 400; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 2183 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2184 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 2185 | HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 2186 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 2187 | HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1); |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2188 | |
| 2189 | return_prx_cond: |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2190 | if (!(s->flags & SF_ERR_MASK)) |
| 2191 | s->flags |= SF_ERR_PRXCOND; |
| 2192 | if (!(s->flags & SF_FINST_MASK)) |
| 2193 | s->flags |= SF_FINST_R; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2194 | |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 2195 | req->analysers &= AN_REQ_FLT_END; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2196 | req->analyse_exp = TICK_ETERNITY; |
| 2197 | return 0; |
| 2198 | } |
| 2199 | |
Willy Tarreau | 4f8a83c | 2012-06-04 00:26:23 +0200 | [diff] [blame] | 2200 | |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 2201 | /* This function prepares an applet to handle the stats. It can deal with the |
| 2202 | * "100-continue" expectation, check that admin rules are met for POST requests, |
| 2203 | * and program a response message if something was unexpected. It cannot fail |
| 2204 | * 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] | 2205 | * 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] | 2206 | * 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] | 2207 | * is expected to have already assigned an appctx to the stream. |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 2208 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2209 | int http_handle_stats(struct stream *s, struct channel *req) |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 2210 | { |
| 2211 | struct stats_admin_rule *stats_admin_rule; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 2212 | struct stream_interface *si = &s->si[1]; |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 2213 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 2214 | struct http_txn *txn = s->txn; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 2215 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2216 | struct uri_auth *uri_auth = s->be->uri_auth; |
| 2217 | const char *uri, *h, *lookup; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2218 | struct appctx *appctx; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2219 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2220 | appctx = si_appctx(si); |
| 2221 | memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats)); |
| 2222 | appctx->st1 = appctx->st2 = 0; |
| 2223 | appctx->ctx.stats.st_code = STAT_STATUS_INIT; |
| 2224 | appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 2225 | 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] | 2226 | appctx->ctx.stats.flags |= STAT_CHUNKED; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2227 | |
| 2228 | uri = msg->chn->buf->p + msg->sl.rq.u; |
| 2229 | lookup = uri + uri_auth->uri_len; |
| 2230 | |
| 2231 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) { |
| 2232 | if (memcmp(h, ";up", 3) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2233 | appctx->ctx.stats.flags |= STAT_HIDE_DOWN; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2234 | break; |
| 2235 | } |
| 2236 | } |
| 2237 | |
| 2238 | if (uri_auth->refresh) { |
| 2239 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) { |
| 2240 | if (memcmp(h, ";norefresh", 10) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2241 | appctx->ctx.stats.flags |= STAT_NO_REFRESH; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2242 | break; |
| 2243 | } |
| 2244 | } |
| 2245 | } |
| 2246 | |
| 2247 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) { |
| 2248 | if (memcmp(h, ";csv", 4) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2249 | appctx->ctx.stats.flags &= ~STAT_FMT_HTML; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2250 | break; |
| 2251 | } |
| 2252 | } |
| 2253 | |
Willy Tarreau | 1e62df9 | 2016-01-11 18:57:53 +0100 | [diff] [blame] | 2254 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 6; h++) { |
| 2255 | if (memcmp(h, ";typed", 6) == 0) { |
| 2256 | appctx->ctx.stats.flags &= ~STAT_FMT_HTML; |
| 2257 | appctx->ctx.stats.flags |= STAT_FMT_TYPED; |
| 2258 | break; |
| 2259 | } |
| 2260 | } |
| 2261 | |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2262 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) { |
| 2263 | if (memcmp(h, ";st=", 4) == 0) { |
| 2264 | int i; |
| 2265 | h += 4; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2266 | appctx->ctx.stats.st_code = STAT_STATUS_UNKN; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2267 | for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) { |
| 2268 | if (strncmp(stat_status_codes[i], h, 4) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2269 | appctx->ctx.stats.st_code = i; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2270 | break; |
| 2271 | } |
| 2272 | } |
| 2273 | break; |
| 2274 | } |
| 2275 | } |
| 2276 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2277 | appctx->ctx.stats.scope_str = 0; |
| 2278 | appctx->ctx.stats.scope_len = 0; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2279 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) { |
| 2280 | if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) { |
| 2281 | int itx = 0; |
| 2282 | const char *h2; |
| 2283 | char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1]; |
| 2284 | const char *err; |
| 2285 | |
| 2286 | h += strlen(STAT_SCOPE_INPUT_NAME) + 1; |
| 2287 | h2 = h; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2288 | appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2289 | while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') { |
| 2290 | itx++; |
| 2291 | h++; |
| 2292 | } |
| 2293 | |
| 2294 | if (itx > STAT_SCOPE_TXT_MAXLEN) |
| 2295 | itx = STAT_SCOPE_TXT_MAXLEN; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2296 | appctx->ctx.stats.scope_len = itx; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2297 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2298 | /* 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] | 2299 | memcpy(scope_txt, h2, itx); |
| 2300 | scope_txt[itx] = '\0'; |
| 2301 | err = invalid_char(scope_txt); |
| 2302 | if (err) { |
| 2303 | /* bad char in search text => clear scope */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2304 | appctx->ctx.stats.scope_str = 0; |
| 2305 | appctx->ctx.stats.scope_len = 0; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2306 | } |
| 2307 | break; |
| 2308 | } |
| 2309 | } |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 2310 | |
| 2311 | /* now check whether we have some admin rules for this request */ |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2312 | list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) { |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 2313 | int ret = 1; |
| 2314 | |
| 2315 | if (stats_admin_rule->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 2316 | 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] | 2317 | ret = acl_pass(ret); |
| 2318 | if (stats_admin_rule->cond->pol == ACL_COND_UNLESS) |
| 2319 | ret = !ret; |
| 2320 | } |
| 2321 | |
| 2322 | if (ret) { |
| 2323 | /* no rule, or the rule matches */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2324 | appctx->ctx.stats.flags |= STAT_ADMIN; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 2325 | break; |
| 2326 | } |
| 2327 | } |
| 2328 | |
| 2329 | /* Was the status page requested with a POST ? */ |
Willy Tarreau | b8cdf52 | 2015-05-29 01:09:15 +0200 | [diff] [blame] | 2330 | if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2331 | if (appctx->ctx.stats.flags & STAT_ADMIN) { |
Willy Tarreau | cfe7fdd | 2014-04-26 22:08:32 +0200 | [diff] [blame] | 2332 | /* we'll need the request body, possibly after sending 100-continue */ |
Willy Tarreau | b8cdf52 | 2015-05-29 01:09:15 +0200 | [diff] [blame] | 2333 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) |
| 2334 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2335 | appctx->st0 = STAT_HTTP_POST; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 2336 | } |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 2337 | else { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2338 | appctx->ctx.stats.st_code = STAT_STATUS_DENY; |
| 2339 | appctx->st0 = STAT_HTTP_LAST; |
de Lafond Guillaume | 88c278f | 2013-04-15 19:27:10 +0200 | [diff] [blame] | 2340 | } |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 2341 | } |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 2342 | else { |
| 2343 | /* So it was another method (GET/HEAD) */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2344 | appctx->st0 = STAT_HTTP_HEAD; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 2345 | } |
| 2346 | |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 2347 | s->task->nice = -32; /* small boost for HTTP statistics */ |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 2348 | return 1; |
| 2349 | } |
| 2350 | |
Lukas Tribus | 67db8df | 2013-06-23 17:37:13 +0200 | [diff] [blame] | 2351 | /* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets |
| 2352 | * (as per RFC3260 #4 and BCP37 #4.2 and #5.2). |
| 2353 | */ |
Vincent Bernat | 6e61589 | 2016-05-18 16:17:44 +0200 | [diff] [blame] | 2354 | 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] | 2355 | { |
| 2356 | #ifdef IP_TOS |
Vincent Bernat | 6e61589 | 2016-05-18 16:17:44 +0200 | [diff] [blame] | 2357 | if (from->ss_family == AF_INET) |
Lukas Tribus | 67db8df | 2013-06-23 17:37:13 +0200 | [diff] [blame] | 2358 | setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)); |
| 2359 | #endif |
| 2360 | #ifdef IPV6_TCLASS |
Vincent Bernat | 6e61589 | 2016-05-18 16:17:44 +0200 | [diff] [blame] | 2361 | if (from->ss_family == AF_INET6) { |
| 2362 | if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)from)->sin6_addr)) |
Lukas Tribus | 67db8df | 2013-06-23 17:37:13 +0200 | [diff] [blame] | 2363 | /* v4-mapped addresses need IP_TOS */ |
| 2364 | setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)); |
| 2365 | else |
| 2366 | setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos)); |
| 2367 | } |
| 2368 | #endif |
| 2369 | } |
| 2370 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2371 | int http_transform_header_str(struct stream* s, struct http_msg *msg, |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 2372 | const char* name, unsigned int name_len, |
| 2373 | const char *str, struct my_regex *re, |
| 2374 | int action) |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 2375 | { |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 2376 | struct hdr_ctx ctx; |
| 2377 | char *buf = msg->chn->buf->p; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 2378 | struct hdr_idx *idx = &s->txn->hdr_idx; |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 2379 | int (*http_find_hdr_func)(const char *name, int len, char *sol, |
| 2380 | struct hdr_idx *idx, struct hdr_ctx *ctx); |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 2381 | struct chunk *output = get_trash_chunk(); |
| 2382 | |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 2383 | ctx.idx = 0; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 2384 | |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 2385 | /* Choose the header browsing function. */ |
| 2386 | switch (action) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2387 | case ACT_HTTP_REPLACE_VAL: |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 2388 | http_find_hdr_func = http_find_header2; |
| 2389 | break; |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2390 | case ACT_HTTP_REPLACE_HDR: |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 2391 | http_find_hdr_func = http_find_full_header2; |
| 2392 | break; |
| 2393 | default: /* impossible */ |
| 2394 | return -1; |
| 2395 | } |
| 2396 | |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 2397 | while (http_find_hdr_func(name, name_len, buf, idx, &ctx)) { |
| 2398 | struct hdr_idx_elem *hdr = idx->v + ctx.idx; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 2399 | int delta; |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 2400 | char *val = ctx.line + ctx.val; |
| 2401 | char* val_end = val + ctx.vlen; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 2402 | |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 2403 | if (!regex_exec_match2(re, val, val_end-val, MAX_MATCH, pmatch, 0)) |
| 2404 | continue; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 2405 | |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 2406 | output->len = exp_replace(output->str, output->size, val, str, pmatch); |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 2407 | if (output->len == -1) |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 2408 | return -1; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 2409 | |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 2410 | 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] | 2411 | |
| 2412 | hdr->len += delta; |
| 2413 | http_msg_move_end(msg, delta); |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 2414 | |
| 2415 | /* Adjust the length of the current value of the index. */ |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 2416 | ctx.vlen += delta; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 2417 | } |
| 2418 | |
| 2419 | return 0; |
| 2420 | } |
| 2421 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2422 | static int http_transform_header(struct stream* s, struct http_msg *msg, |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 2423 | const char* name, unsigned int name_len, |
| 2424 | struct list *fmt, struct my_regex *re, |
| 2425 | int action) |
| 2426 | { |
Christopher Faulet | 07a0fec | 2017-02-08 12:17:07 +0100 | [diff] [blame] | 2427 | struct chunk *replace; |
| 2428 | int ret = -1; |
| 2429 | |
| 2430 | replace = alloc_trash_chunk(); |
| 2431 | if (!replace) |
| 2432 | goto leave; |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 2433 | |
| 2434 | replace->len = build_logline(s, replace->str, replace->size, fmt); |
| 2435 | if (replace->len >= replace->size - 1) |
Christopher Faulet | 07a0fec | 2017-02-08 12:17:07 +0100 | [diff] [blame] | 2436 | goto leave; |
| 2437 | |
| 2438 | 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] | 2439 | |
Christopher Faulet | 07a0fec | 2017-02-08 12:17:07 +0100 | [diff] [blame] | 2440 | leave: |
| 2441 | free_trash_chunk(replace); |
| 2442 | return ret; |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 2443 | } |
| 2444 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2445 | /* Executes the http-request rules <rules> for stream <s>, proxy <px> and |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 2446 | * transaction <txn>. Returns the verdict of the first rule that prevents |
| 2447 | * further processing of the request (auth, deny, ...), and defaults to |
| 2448 | * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or |
| 2449 | * 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] | 2450 | * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL |
| 2451 | * and a deny/tarpit rule is matched, it will be filled with this rule's deny |
| 2452 | * status. |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2453 | */ |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 2454 | enum rule_result |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 2455 | 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] | 2456 | { |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 2457 | struct session *sess = strm_sess(s); |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 2458 | struct http_txn *txn = s->txn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2459 | struct connection *cli_conn; |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 2460 | struct act_rule *rule; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 2461 | struct hdr_ctx ctx; |
Willy Tarreau | ae3c010 | 2014-04-28 23:22:08 +0200 | [diff] [blame] | 2462 | const char *auth_realm; |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 2463 | int act_flags = 0; |
Thierry Fournier | 4b788f7 | 2016-06-01 13:35:36 +0200 | [diff] [blame] | 2464 | int len; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2465 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 2466 | /* If "the current_rule_list" match the executed rule list, we are in |
| 2467 | * resume condition. If a resume is needed it is always in the action |
| 2468 | * and never in the ACL or converters. In this case, we initialise the |
| 2469 | * current rule, and go to the action execution point. |
| 2470 | */ |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 2471 | if (s->current_rule) { |
| 2472 | rule = s->current_rule; |
| 2473 | s->current_rule = NULL; |
| 2474 | if (s->current_rule_list == rules) |
| 2475 | goto resume_execution; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 2476 | } |
| 2477 | s->current_rule_list = rules; |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 2478 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2479 | list_for_each_entry(rule, rules, list) { |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2480 | |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 2481 | /* check optional condition */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2482 | if (rule->cond) { |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 2483 | int ret; |
| 2484 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 2485 | 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] | 2486 | ret = acl_pass(ret); |
| 2487 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2488 | if (rule->cond->pol == ACL_COND_UNLESS) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2489 | ret = !ret; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 2490 | |
| 2491 | if (!ret) /* condition not matched */ |
| 2492 | continue; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2493 | } |
| 2494 | |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 2495 | act_flags |= ACT_FLAG_FIRST; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 2496 | resume_execution: |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 2497 | switch (rule->action) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2498 | case ACT_ACTION_ALLOW: |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 2499 | return HTTP_RULE_RES_STOP; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 2500 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2501 | case ACT_ACTION_DENY: |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 2502 | if (deny_status) |
| 2503 | *deny_status = rule->deny_status; |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 2504 | return HTTP_RULE_RES_DENY; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 2505 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2506 | case ACT_HTTP_REQ_TARPIT: |
Willy Tarreau | ccbcc37 | 2012-12-27 12:37:57 +0100 | [diff] [blame] | 2507 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 2508 | if (deny_status) |
| 2509 | *deny_status = rule->deny_status; |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 2510 | return HTTP_RULE_RES_DENY; |
Willy Tarreau | ccbcc37 | 2012-12-27 12:37:57 +0100 | [diff] [blame] | 2511 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2512 | case ACT_HTTP_REQ_AUTH: |
Willy Tarreau | ae3c010 | 2014-04-28 23:22:08 +0200 | [diff] [blame] | 2513 | /* Auth might be performed on regular http-req rules as well as on stats */ |
| 2514 | auth_realm = rule->arg.auth.realm; |
| 2515 | if (!auth_realm) { |
| 2516 | if (px->uri_auth && rules == &px->uri_auth->http_req_rules) |
| 2517 | auth_realm = STATS_DEFAULT_REALM; |
| 2518 | else |
| 2519 | auth_realm = px->id; |
| 2520 | } |
| 2521 | /* send 401/407 depending on whether we use a proxy or not. We still |
| 2522 | * count one error, because normal browsing won't significantly |
| 2523 | * increase the counter but brute force attempts will. |
| 2524 | */ |
| 2525 | chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm); |
| 2526 | txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401; |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 2527 | http_reply_and_close(s, txn->status, &trash); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2528 | stream_inc_http_err_ctr(s); |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 2529 | return HTTP_RULE_RES_ABRT; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 2530 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2531 | case ACT_HTTP_REDIR: |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 2532 | if (!http_apply_redirect_rule(rule->arg.redir, s, txn)) |
| 2533 | return HTTP_RULE_RES_BADREQ; |
| 2534 | return HTTP_RULE_RES_DONE; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 2535 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2536 | case ACT_HTTP_SET_NICE: |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 2537 | s->task->nice = rule->arg.nice; |
| 2538 | break; |
| 2539 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2540 | case ACT_HTTP_SET_TOS: |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 2541 | if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 2542 | 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] | 2543 | break; |
| 2544 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2545 | case ACT_HTTP_SET_MARK: |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 2546 | #ifdef SO_MARK |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 2547 | if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 2548 | 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] | 2549 | #endif |
| 2550 | break; |
| 2551 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2552 | case ACT_HTTP_SET_LOGL: |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 2553 | s->logs.level = rule->arg.loglevel; |
| 2554 | break; |
| 2555 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2556 | case ACT_HTTP_REPLACE_HDR: |
| 2557 | case ACT_HTTP_REPLACE_VAL: |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 2558 | if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name, |
| 2559 | rule->arg.hdr_add.name_len, |
| 2560 | &rule->arg.hdr_add.fmt, |
| 2561 | &rule->arg.hdr_add.re, rule->action)) |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 2562 | return HTTP_RULE_RES_BADREQ; |
| 2563 | break; |
| 2564 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2565 | case ACT_HTTP_DEL_HDR: |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 2566 | ctx.idx = 0; |
| 2567 | /* remove all occurrences of the header */ |
| 2568 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 2569 | txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 2570 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 2571 | } |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 2572 | break; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 2573 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2574 | case ACT_HTTP_SET_HDR: |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2575 | case ACT_HTTP_ADD_HDR: { |
Thierry Fournier | 4b788f7 | 2016-06-01 13:35:36 +0200 | [diff] [blame] | 2576 | /* The scope of the trash buffer must be limited to this function. The |
| 2577 | * build_logline() function can execute a lot of other function which |
| 2578 | * can use the trash buffer. So for limiting the scope of this global |
| 2579 | * buffer, we build first the header value using build_logline, and |
| 2580 | * after we store the header name. |
| 2581 | */ |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2582 | struct chunk *replace; |
| 2583 | |
| 2584 | replace = alloc_trash_chunk(); |
| 2585 | if (!replace) |
| 2586 | return HTTP_RULE_RES_BADREQ; |
| 2587 | |
Thierry Fournier | 4b788f7 | 2016-06-01 13:35:36 +0200 | [diff] [blame] | 2588 | len = rule->arg.hdr_add.name_len + 2, |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2589 | len += build_logline(s, replace->str + len, replace->size - len, &rule->arg.hdr_add.fmt); |
| 2590 | memcpy(replace->str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len); |
| 2591 | replace->str[rule->arg.hdr_add.name_len] = ':'; |
| 2592 | replace->str[rule->arg.hdr_add.name_len + 1] = ' '; |
| 2593 | replace->len = len; |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 2594 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2595 | if (rule->action == ACT_HTTP_SET_HDR) { |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 2596 | /* remove all occurrences of the header */ |
| 2597 | ctx.idx = 0; |
| 2598 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 2599 | txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 2600 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
| 2601 | } |
| 2602 | } |
| 2603 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2604 | http_header_add_tail2(&txn->req, &txn->hdr_idx, replace->str, replace->len); |
| 2605 | |
| 2606 | free_trash_chunk(replace); |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 2607 | break; |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2608 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2609 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2610 | case ACT_HTTP_DEL_ACL: |
| 2611 | case ACT_HTTP_DEL_MAP: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2612 | struct pat_ref *ref; |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2613 | struct chunk *key; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2614 | |
| 2615 | /* collect reference */ |
| 2616 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 2617 | if (!ref) |
| 2618 | continue; |
| 2619 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2620 | /* allocate key */ |
| 2621 | key = alloc_trash_chunk(); |
| 2622 | if (!key) |
| 2623 | return HTTP_RULE_RES_BADREQ; |
| 2624 | |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2625 | /* collect key */ |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2626 | key->len = build_logline(s, key->str, key->size, &rule->arg.map.key); |
| 2627 | key->str[key->len] = '\0'; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2628 | |
| 2629 | /* perform update */ |
| 2630 | /* returned code: 1=ok, 0=ko */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2631 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2632 | pat_ref_delete(ref, key->str); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2633 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2634 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2635 | free_trash_chunk(key); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2636 | break; |
| 2637 | } |
| 2638 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2639 | case ACT_HTTP_ADD_ACL: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2640 | struct pat_ref *ref; |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2641 | struct chunk *key; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2642 | |
| 2643 | /* collect reference */ |
| 2644 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 2645 | if (!ref) |
| 2646 | continue; |
| 2647 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2648 | /* allocate key */ |
| 2649 | key = alloc_trash_chunk(); |
| 2650 | if (!key) |
| 2651 | return HTTP_RULE_RES_BADREQ; |
| 2652 | |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2653 | /* collect key */ |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2654 | key->len = build_logline(s, key->str, key->size, &rule->arg.map.key); |
| 2655 | key->str[key->len] = '\0'; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2656 | |
| 2657 | /* perform update */ |
| 2658 | /* add entry only if it does not already exist */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2659 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2660 | if (pat_ref_find_elt(ref, key->str) == NULL) |
| 2661 | pat_ref_add(ref, key->str, NULL, NULL); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2662 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2663 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2664 | free_trash_chunk(key); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2665 | break; |
| 2666 | } |
| 2667 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2668 | case ACT_HTTP_SET_MAP: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2669 | struct pat_ref *ref; |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2670 | struct chunk *key, *value; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2671 | |
| 2672 | /* collect reference */ |
| 2673 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 2674 | if (!ref) |
| 2675 | continue; |
| 2676 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2677 | /* allocate key */ |
| 2678 | key = alloc_trash_chunk(); |
| 2679 | if (!key) |
| 2680 | return HTTP_RULE_RES_BADREQ; |
| 2681 | |
| 2682 | /* allocate value */ |
| 2683 | value = alloc_trash_chunk(); |
| 2684 | if (!value) { |
| 2685 | free_trash_chunk(key); |
| 2686 | return HTTP_RULE_RES_BADREQ; |
| 2687 | } |
| 2688 | |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2689 | /* collect key */ |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2690 | key->len = build_logline(s, key->str, key->size, &rule->arg.map.key); |
| 2691 | key->str[key->len] = '\0'; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2692 | |
| 2693 | /* collect value */ |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2694 | value->len = build_logline(s, value->str, value->size, &rule->arg.map.value); |
| 2695 | value->str[value->len] = '\0'; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2696 | |
| 2697 | /* perform update */ |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2698 | if (pat_ref_find_elt(ref, key->str) != NULL) |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2699 | /* update entry if it exists */ |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2700 | pat_ref_set(ref, key->str, value->str, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2701 | else |
| 2702 | /* insert a new entry */ |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2703 | pat_ref_add(ref, key->str, value->str, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2704 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2705 | free_trash_chunk(key); |
| 2706 | free_trash_chunk(value); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2707 | break; |
| 2708 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 2709 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 2710 | case ACT_CUSTOM: |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 2711 | if ((px->options & PR_O_ABRT_CLOSE) && (s->req.flags & (CF_SHUTR|CF_READ_NULL|CF_READ_ERROR))) |
| 2712 | act_flags |= ACT_FLAG_FINAL; |
Willy Tarreau | 3945868 | 2015-09-27 10:33:15 +0200 | [diff] [blame] | 2713 | |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 2714 | switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) { |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 2715 | case ACT_RET_ERR: |
| 2716 | case ACT_RET_CONT: |
| 2717 | break; |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 2718 | case ACT_RET_STOP: |
| 2719 | return HTTP_RULE_RES_DONE; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 2720 | case ACT_RET_YIELD: |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 2721 | s->current_rule = rule; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 2722 | return HTTP_RULE_RES_YIELD; |
| 2723 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 2724 | break; |
| 2725 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2726 | case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX: |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 2727 | /* Note: only the first valid tracking parameter of each |
| 2728 | * applies. |
| 2729 | */ |
| 2730 | |
Christopher Faulet | 4fce0d8 | 2017-09-18 11:57:31 +0200 | [diff] [blame] | 2731 | if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) { |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 2732 | struct stktable *t; |
| 2733 | struct stksess *ts; |
| 2734 | struct stktable_key *key; |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 2735 | void *ptr1, *ptr2; |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 2736 | |
Thierry FOURNIER | 5ec63e0 | 2015-08-04 09:09:48 +0200 | [diff] [blame] | 2737 | t = rule->arg.trk_ctr.table.t; |
| 2738 | 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] | 2739 | |
| 2740 | if (key && (ts = stktable_get_entry(t, key))) { |
Christopher Faulet | 4fce0d8 | 2017-09-18 11:57:31 +0200 | [diff] [blame] | 2741 | stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts); |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 2742 | |
| 2743 | /* 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] | 2744 | ptr1 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT); |
| 2745 | ptr2 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE); |
| 2746 | if (ptr1 || ptr2) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2747 | HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock); |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 2748 | |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 2749 | if (ptr1) |
| 2750 | stktable_data_cast(ptr1, http_req_cnt)++; |
| 2751 | |
| 2752 | if (ptr2) |
| 2753 | update_freq_ctr_period(&stktable_data_cast(ptr2, http_req_rate), |
| 2754 | t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1); |
| 2755 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2756 | HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock); |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 2757 | } |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 2758 | |
Christopher Faulet | 4fce0d8 | 2017-09-18 11:57:31 +0200 | [diff] [blame] | 2759 | stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT); |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 2760 | if (sess->fe != s->be) |
Christopher Faulet | 4fce0d8 | 2017-09-18 11:57:31 +0200 | [diff] [blame] | 2761 | stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND); |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 2762 | } |
| 2763 | } |
Adis Nezirovic | 2fbcafc | 2015-07-06 15:44:30 +0200 | [diff] [blame] | 2764 | break; |
| 2765 | |
Thierry FOURNIER | 22e4901 | 2015-08-05 19:13:48 +0200 | [diff] [blame] | 2766 | /* other flags exists, but normaly, they never be matched. */ |
| 2767 | default: |
| 2768 | break; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2769 | } |
| 2770 | } |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 2771 | |
| 2772 | /* we reached the end of the rules, nothing to report */ |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 2773 | return HTTP_RULE_RES_CONT; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2774 | } |
| 2775 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2776 | |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 2777 | /* Executes the http-response rules <rules> for stream <s> and proxy <px>. It |
| 2778 | * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP, |
| 2779 | * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT |
| 2780 | * is returned, the process can continue the evaluation of next rule list. If |
| 2781 | * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ |
| 2782 | * is returned, it means the operation could not be processed and a server error |
| 2783 | * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a |
| 2784 | * deny rule. If *YIELD is returned, the caller must call again the function |
| 2785 | * with the same context. |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2786 | */ |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 2787 | static enum rule_result |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 2788 | 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] | 2789 | { |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 2790 | struct session *sess = strm_sess(s); |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 2791 | struct http_txn *txn = s->txn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2792 | struct connection *cli_conn; |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 2793 | struct act_rule *rule; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2794 | struct hdr_ctx ctx; |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 2795 | int act_flags = 0; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2796 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 2797 | /* If "the current_rule_list" match the executed rule list, we are in |
| 2798 | * resume condition. If a resume is needed it is always in the action |
| 2799 | * and never in the ACL or converters. In this case, we initialise the |
| 2800 | * current rule, and go to the action execution point. |
| 2801 | */ |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 2802 | if (s->current_rule) { |
| 2803 | rule = s->current_rule; |
| 2804 | s->current_rule = NULL; |
| 2805 | if (s->current_rule_list == rules) |
| 2806 | goto resume_execution; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 2807 | } |
| 2808 | s->current_rule_list = rules; |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 2809 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2810 | list_for_each_entry(rule, rules, list) { |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2811 | |
| 2812 | /* check optional condition */ |
| 2813 | if (rule->cond) { |
| 2814 | int ret; |
| 2815 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 2816 | 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] | 2817 | ret = acl_pass(ret); |
| 2818 | |
| 2819 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 2820 | ret = !ret; |
| 2821 | |
| 2822 | if (!ret) /* condition not matched */ |
| 2823 | continue; |
| 2824 | } |
| 2825 | |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 2826 | act_flags |= ACT_FLAG_FIRST; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 2827 | resume_execution: |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2828 | switch (rule->action) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2829 | case ACT_ACTION_ALLOW: |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 2830 | return HTTP_RULE_RES_STOP; /* "allow" rules are OK */ |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2831 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2832 | case ACT_ACTION_DENY: |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2833 | txn->flags |= TX_SVDENY; |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 2834 | return HTTP_RULE_RES_STOP; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2835 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2836 | case ACT_HTTP_SET_NICE: |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 2837 | s->task->nice = rule->arg.nice; |
| 2838 | break; |
| 2839 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2840 | case ACT_HTTP_SET_TOS: |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 2841 | if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 2842 | 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] | 2843 | break; |
| 2844 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2845 | case ACT_HTTP_SET_MARK: |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 2846 | #ifdef SO_MARK |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 2847 | if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 2848 | 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] | 2849 | #endif |
| 2850 | break; |
| 2851 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2852 | case ACT_HTTP_SET_LOGL: |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 2853 | s->logs.level = rule->arg.loglevel; |
| 2854 | break; |
| 2855 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2856 | case ACT_HTTP_REPLACE_HDR: |
| 2857 | case ACT_HTTP_REPLACE_VAL: |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 2858 | if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name, |
| 2859 | rule->arg.hdr_add.name_len, |
| 2860 | &rule->arg.hdr_add.fmt, |
| 2861 | &rule->arg.hdr_add.re, rule->action)) |
Christopher Faulet | cdade94 | 2017-02-08 12:41:31 +0100 | [diff] [blame] | 2862 | return HTTP_RULE_RES_BADREQ; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 2863 | break; |
| 2864 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2865 | case ACT_HTTP_DEL_HDR: |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2866 | ctx.idx = 0; |
| 2867 | /* remove all occurrences of the header */ |
| 2868 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 2869 | txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 2870 | http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx); |
| 2871 | } |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 2872 | break; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2873 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2874 | case ACT_HTTP_SET_HDR: |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2875 | case ACT_HTTP_ADD_HDR: { |
| 2876 | struct chunk *replace; |
| 2877 | |
| 2878 | replace = alloc_trash_chunk(); |
| 2879 | if (!replace) |
| 2880 | return HTTP_RULE_RES_BADREQ; |
| 2881 | |
| 2882 | chunk_printf(replace, "%s: ", rule->arg.hdr_add.name); |
| 2883 | memcpy(replace->str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len); |
| 2884 | replace->len = rule->arg.hdr_add.name_len; |
| 2885 | replace->str[replace->len++] = ':'; |
| 2886 | replace->str[replace->len++] = ' '; |
| 2887 | replace->len += build_logline(s, replace->str + replace->len, replace->size - replace->len, |
| 2888 | &rule->arg.hdr_add.fmt); |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 2889 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2890 | if (rule->action == ACT_HTTP_SET_HDR) { |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 2891 | /* remove all occurrences of the header */ |
| 2892 | ctx.idx = 0; |
| 2893 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 2894 | txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 2895 | http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx); |
| 2896 | } |
| 2897 | } |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2898 | http_header_add_tail2(&txn->rsp, &txn->hdr_idx, replace->str, replace->len); |
| 2899 | |
| 2900 | free_trash_chunk(replace); |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2901 | break; |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2902 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2903 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2904 | case ACT_HTTP_DEL_ACL: |
| 2905 | case ACT_HTTP_DEL_MAP: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2906 | struct pat_ref *ref; |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2907 | struct chunk *key; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2908 | |
| 2909 | /* collect reference */ |
| 2910 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 2911 | if (!ref) |
| 2912 | continue; |
| 2913 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2914 | /* allocate key */ |
| 2915 | key = alloc_trash_chunk(); |
| 2916 | if (!key) |
| 2917 | return HTTP_RULE_RES_BADREQ; |
| 2918 | |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2919 | /* collect key */ |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2920 | key->len = build_logline(s, key->str, key->size, &rule->arg.map.key); |
| 2921 | key->str[key->len] = '\0'; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2922 | |
| 2923 | /* perform update */ |
| 2924 | /* returned code: 1=ok, 0=ko */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2925 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2926 | pat_ref_delete(ref, key->str); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2927 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2928 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2929 | free_trash_chunk(key); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2930 | break; |
| 2931 | } |
| 2932 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2933 | case ACT_HTTP_ADD_ACL: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2934 | struct pat_ref *ref; |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2935 | struct chunk *key; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2936 | |
| 2937 | /* collect reference */ |
| 2938 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 2939 | if (!ref) |
| 2940 | continue; |
| 2941 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2942 | /* allocate key */ |
| 2943 | key = alloc_trash_chunk(); |
| 2944 | if (!key) |
| 2945 | return HTTP_RULE_RES_BADREQ; |
| 2946 | |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2947 | /* collect key */ |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2948 | key->len = build_logline(s, key->str, key->size, &rule->arg.map.key); |
| 2949 | key->str[key->len] = '\0'; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2950 | |
| 2951 | /* perform update */ |
| 2952 | /* check if the entry already exists */ |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2953 | if (pat_ref_find_elt(ref, key->str) == NULL) |
| 2954 | pat_ref_add(ref, key->str, NULL, NULL); |
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_SET_MAP: { |
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, *value; |
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 | |
| 2974 | /* allocate value */ |
| 2975 | value = alloc_trash_chunk(); |
| 2976 | if (!value) { |
| 2977 | free_trash_chunk(key); |
| 2978 | return HTTP_RULE_RES_BADREQ; |
| 2979 | } |
| 2980 | |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2981 | /* collect key */ |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2982 | key->len = build_logline(s, key->str, key->size, &rule->arg.map.key); |
| 2983 | key->str[key->len] = '\0'; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2984 | |
| 2985 | /* collect value */ |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2986 | value->len = build_logline(s, value->str, value->size, &rule->arg.map.value); |
| 2987 | value->str[value->len] = '\0'; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2988 | |
| 2989 | /* perform update */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2990 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2991 | if (pat_ref_find_elt(ref, key->str) != NULL) |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2992 | /* update entry if it exists */ |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2993 | pat_ref_set(ref, key->str, value->str, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2994 | else |
| 2995 | /* insert a new entry */ |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2996 | pat_ref_add(ref, key->str, value->str, NULL); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2997 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2998 | free_trash_chunk(key); |
| 2999 | free_trash_chunk(value); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3000 | break; |
| 3001 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 3002 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3003 | case ACT_HTTP_REDIR: |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 3004 | if (!http_apply_redirect_rule(rule->arg.redir, s, txn)) |
| 3005 | return HTTP_RULE_RES_BADREQ; |
| 3006 | return HTTP_RULE_RES_DONE; |
| 3007 | |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 3008 | case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX: |
| 3009 | /* Note: only the first valid tracking parameter of each |
| 3010 | * applies. |
| 3011 | */ |
| 3012 | |
Christopher Faulet | 4fce0d8 | 2017-09-18 11:57:31 +0200 | [diff] [blame] | 3013 | if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) { |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 3014 | struct stktable *t; |
| 3015 | struct stksess *ts; |
| 3016 | struct stktable_key *key; |
| 3017 | void *ptr; |
| 3018 | |
| 3019 | t = rule->arg.trk_ctr.table.t; |
| 3020 | key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_RES | SMP_OPT_FINAL, rule->arg.trk_ctr.expr, NULL); |
| 3021 | |
| 3022 | if (key && (ts = stktable_get_entry(t, key))) { |
Christopher Faulet | 4fce0d8 | 2017-09-18 11:57:31 +0200 | [diff] [blame] | 3023 | stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts); |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 3024 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 3025 | HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock); |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 3026 | |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 3027 | /* let's count a new HTTP request as it's the first time we do it */ |
| 3028 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT); |
| 3029 | if (ptr) |
| 3030 | stktable_data_cast(ptr, http_req_cnt)++; |
| 3031 | |
| 3032 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE); |
| 3033 | if (ptr) |
| 3034 | update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate), |
| 3035 | t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1); |
| 3036 | |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 3037 | /* When the client triggers a 4xx from the server, it's most often due |
| 3038 | * to a missing object or permission. These events should be tracked |
| 3039 | * because if they happen often, it may indicate a brute force or a |
| 3040 | * vulnerability scan. Normally this is done when receiving the response |
| 3041 | * but here we're tracking after this ought to have been done so we have |
| 3042 | * to do it on purpose. |
| 3043 | */ |
Willy Tarreau | 3146a4c | 2016-07-26 15:22:33 +0200 | [diff] [blame] | 3044 | if ((unsigned)(txn->status - 400) < 100) { |
| 3045 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT); |
| 3046 | if (ptr) |
| 3047 | stktable_data_cast(ptr, http_err_cnt)++; |
| 3048 | |
| 3049 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE); |
| 3050 | if (ptr) |
| 3051 | update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate), |
| 3052 | t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1); |
| 3053 | } |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 3054 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 3055 | HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock); |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 3056 | |
| 3057 | stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT); |
| 3058 | if (sess->fe != s->be) |
| 3059 | stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND); |
| 3060 | |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 3061 | } |
| 3062 | } |
| 3063 | break; |
| 3064 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 3065 | case ACT_CUSTOM: |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 3066 | if ((px->options & PR_O_ABRT_CLOSE) && (s->req.flags & (CF_SHUTR|CF_READ_NULL|CF_READ_ERROR))) |
| 3067 | act_flags |= ACT_FLAG_FINAL; |
Willy Tarreau | 3945868 | 2015-09-27 10:33:15 +0200 | [diff] [blame] | 3068 | |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 3069 | switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) { |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 3070 | case ACT_RET_ERR: |
| 3071 | case ACT_RET_CONT: |
| 3072 | break; |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 3073 | case ACT_RET_STOP: |
| 3074 | return HTTP_RULE_RES_STOP; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 3075 | case ACT_RET_YIELD: |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 3076 | s->current_rule = rule; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3077 | return HTTP_RULE_RES_YIELD; |
| 3078 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 3079 | break; |
| 3080 | |
Thierry FOURNIER | 22e4901 | 2015-08-05 19:13:48 +0200 | [diff] [blame] | 3081 | /* other flags exists, but normaly, they never be matched. */ |
| 3082 | default: |
| 3083 | break; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3084 | } |
| 3085 | } |
| 3086 | |
| 3087 | /* we reached the end of the rules, nothing to report */ |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 3088 | return HTTP_RULE_RES_CONT; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3089 | } |
| 3090 | |
| 3091 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3092 | /* Perform an HTTP redirect based on the information in <rule>. The function |
| 3093 | * returns non-zero on success, or zero in case of a, irrecoverable error such |
| 3094 | * as too large a request to build a valid response. |
| 3095 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3096 | 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] | 3097 | { |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 3098 | struct http_msg *req = &txn->req; |
| 3099 | struct http_msg *res = &txn->rsp; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3100 | const char *msg_fmt; |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3101 | struct chunk *chunk; |
| 3102 | int ret = 0; |
| 3103 | |
| 3104 | chunk = alloc_trash_chunk(); |
| 3105 | if (!chunk) |
| 3106 | goto leave; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3107 | |
| 3108 | /* build redirect message */ |
| 3109 | switch(rule->code) { |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 3110 | case 308: |
| 3111 | msg_fmt = HTTP_308; |
| 3112 | break; |
| 3113 | case 307: |
| 3114 | msg_fmt = HTTP_307; |
| 3115 | break; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3116 | case 303: |
| 3117 | msg_fmt = HTTP_303; |
| 3118 | break; |
| 3119 | case 301: |
| 3120 | msg_fmt = HTTP_301; |
| 3121 | break; |
| 3122 | case 302: |
| 3123 | default: |
| 3124 | msg_fmt = HTTP_302; |
| 3125 | break; |
| 3126 | } |
| 3127 | |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3128 | if (unlikely(!chunk_strcpy(chunk, msg_fmt))) |
| 3129 | goto leave; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3130 | |
| 3131 | switch(rule->type) { |
| 3132 | case REDIRECT_TYPE_SCHEME: { |
| 3133 | const char *path; |
| 3134 | const char *host; |
| 3135 | struct hdr_ctx ctx; |
| 3136 | int pathlen; |
| 3137 | int hostlen; |
| 3138 | |
| 3139 | host = ""; |
| 3140 | hostlen = 0; |
| 3141 | ctx.idx = 0; |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 3142 | 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] | 3143 | host = ctx.line + ctx.val; |
| 3144 | hostlen = ctx.vlen; |
| 3145 | } |
| 3146 | |
| 3147 | path = http_get_path(txn); |
| 3148 | /* build message using path */ |
| 3149 | if (path) { |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 3150 | 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] | 3151 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 3152 | int qs = 0; |
| 3153 | while (qs < pathlen) { |
| 3154 | if (path[qs] == '?') { |
| 3155 | pathlen = qs; |
| 3156 | break; |
| 3157 | } |
| 3158 | qs++; |
| 3159 | } |
| 3160 | } |
| 3161 | } else { |
| 3162 | path = "/"; |
| 3163 | pathlen = 1; |
| 3164 | } |
| 3165 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3166 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
| 3167 | /* check if we can add scheme + "://" + host + path */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3168 | if (chunk->len + rule->rdr_len + 3 + hostlen + pathlen > chunk->size - 4) |
| 3169 | goto leave; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3170 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3171 | /* add scheme */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3172 | memcpy(chunk->str + chunk->len, rule->rdr_str, rule->rdr_len); |
| 3173 | chunk->len += rule->rdr_len; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3174 | } |
| 3175 | else { |
| 3176 | /* add scheme with executing log format */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3177 | 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] | 3178 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3179 | /* check if we can add scheme + "://" + host + path */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3180 | if (chunk->len + 3 + hostlen + pathlen > chunk->size - 4) |
| 3181 | goto leave; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3182 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3183 | /* add "://" */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3184 | memcpy(chunk->str + chunk->len, "://", 3); |
| 3185 | chunk->len += 3; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3186 | |
| 3187 | /* add host */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3188 | memcpy(chunk->str + chunk->len, host, hostlen); |
| 3189 | chunk->len += hostlen; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3190 | |
| 3191 | /* add path */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3192 | memcpy(chunk->str + chunk->len, path, pathlen); |
| 3193 | chunk->len += pathlen; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3194 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3195 | /* 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] | 3196 | if (chunk->len && chunk->str[chunk->len - 1] != '/' && |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3197 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3198 | if (chunk->len > chunk->size - 5) |
| 3199 | goto leave; |
| 3200 | chunk->str[chunk->len] = '/'; |
| 3201 | chunk->len++; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3202 | } |
| 3203 | |
| 3204 | break; |
| 3205 | } |
| 3206 | case REDIRECT_TYPE_PREFIX: { |
| 3207 | const char *path; |
| 3208 | int pathlen; |
| 3209 | |
| 3210 | path = http_get_path(txn); |
| 3211 | /* build message using path */ |
| 3212 | if (path) { |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 3213 | 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] | 3214 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 3215 | int qs = 0; |
| 3216 | while (qs < pathlen) { |
| 3217 | if (path[qs] == '?') { |
| 3218 | pathlen = qs; |
| 3219 | break; |
| 3220 | } |
| 3221 | qs++; |
| 3222 | } |
| 3223 | } |
| 3224 | } else { |
| 3225 | path = "/"; |
| 3226 | pathlen = 1; |
| 3227 | } |
| 3228 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3229 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3230 | if (chunk->len + rule->rdr_len + pathlen > chunk->size - 4) |
| 3231 | goto leave; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3232 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3233 | /* add prefix. Note that if prefix == "/", we don't want to |
| 3234 | * add anything, otherwise it makes it hard for the user to |
| 3235 | * configure a self-redirection. |
| 3236 | */ |
| 3237 | if (rule->rdr_len != 1 || *rule->rdr_str != '/') { |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3238 | memcpy(chunk->str + chunk->len, rule->rdr_str, rule->rdr_len); |
| 3239 | chunk->len += rule->rdr_len; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3240 | } |
| 3241 | } |
| 3242 | else { |
| 3243 | /* add prefix with executing log format */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3244 | 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] | 3245 | |
| 3246 | /* Check length */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3247 | if (chunk->len + pathlen > chunk->size - 4) |
| 3248 | goto leave; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3249 | } |
| 3250 | |
| 3251 | /* add path */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3252 | memcpy(chunk->str + chunk->len, path, pathlen); |
| 3253 | chunk->len += pathlen; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3254 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3255 | /* 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] | 3256 | if (chunk->len && chunk->str[chunk->len - 1] != '/' && |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3257 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3258 | if (chunk->len > chunk->size - 5) |
| 3259 | goto leave; |
| 3260 | chunk->str[chunk->len] = '/'; |
| 3261 | chunk->len++; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3262 | } |
| 3263 | |
| 3264 | break; |
| 3265 | } |
| 3266 | case REDIRECT_TYPE_LOCATION: |
| 3267 | default: |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3268 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3269 | if (chunk->len + rule->rdr_len > chunk->size - 4) |
| 3270 | goto leave; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3271 | |
| 3272 | /* add location */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3273 | memcpy(chunk->str + chunk->len, rule->rdr_str, rule->rdr_len); |
| 3274 | chunk->len += rule->rdr_len; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3275 | } |
| 3276 | else { |
| 3277 | /* add location with executing log format */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3278 | 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] | 3279 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3280 | /* Check left length */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3281 | if (chunk->len > chunk->size - 4) |
| 3282 | goto leave; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3283 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3284 | break; |
| 3285 | } |
| 3286 | |
| 3287 | if (rule->cookie_len) { |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3288 | memcpy(chunk->str + chunk->len, "\r\nSet-Cookie: ", 14); |
| 3289 | chunk->len += 14; |
| 3290 | memcpy(chunk->str + chunk->len, rule->cookie_str, rule->cookie_len); |
| 3291 | chunk->len += rule->cookie_len; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3292 | } |
| 3293 | |
Willy Tarreau | 19b1412 | 2017-02-28 09:48:11 +0100 | [diff] [blame] | 3294 | /* add end of headers and the keep-alive/close status. */ |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3295 | txn->status = rule->code; |
| 3296 | /* let's log the request time */ |
| 3297 | s->logs.tv_request = now; |
| 3298 | |
Christopher Faulet | be821b9 | 2017-03-30 11:21:53 +0200 | [diff] [blame] | 3299 | 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] | 3300 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL || |
| 3301 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) { |
| 3302 | /* keep-alive possible */ |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 3303 | if (!(req->flags & HTTP_MSGF_VER_11)) { |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3304 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3305 | memcpy(chunk->str + chunk->len, "\r\nProxy-Connection: keep-alive", 30); |
| 3306 | chunk->len += 30; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3307 | } else { |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3308 | memcpy(chunk->str + chunk->len, "\r\nConnection: keep-alive", 24); |
| 3309 | chunk->len += 24; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3310 | } |
| 3311 | } |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3312 | memcpy(chunk->str + chunk->len, "\r\n\r\n", 4); |
| 3313 | chunk->len += 4; |
| 3314 | FLT_STRM_CB(s, flt_http_reply(s, txn->status, chunk)); |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3315 | co_inject(res->chn, chunk->str, chunk->len); |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3316 | /* "eat" the request */ |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 3317 | bi_fast_delete(req->chn->buf, req->sov); |
| 3318 | req->next -= req->sov; |
| 3319 | req->sov = 0; |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 3320 | 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] | 3321 | 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] | 3322 | req->msg_state = HTTP_MSG_CLOSED; |
| 3323 | res->msg_state = HTTP_MSG_DONE; |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 3324 | /* Trim any possible response */ |
| 3325 | res->chn->buf->i = 0; |
| 3326 | res->next = res->sov = 0; |
Christopher Faulet | 5d468ca | 2017-09-11 09:27:29 +0200 | [diff] [blame] | 3327 | /* let the server side turn to SI_ST_CLO */ |
| 3328 | channel_shutw_now(req->chn); |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3329 | } else { |
| 3330 | /* keep-alive not possible */ |
| 3331 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3332 | memcpy(chunk->str + chunk->len, "\r\nProxy-Connection: close\r\n\r\n", 29); |
| 3333 | chunk->len += 29; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3334 | } else { |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3335 | memcpy(chunk->str + chunk->len, "\r\nConnection: close\r\n\r\n", 23); |
| 3336 | chunk->len += 23; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3337 | } |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3338 | http_reply_and_close(s, txn->status, chunk); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 3339 | req->chn->analysers &= AN_REQ_FLT_END; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3340 | } |
| 3341 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3342 | if (!(s->flags & SF_ERR_MASK)) |
| 3343 | s->flags |= SF_ERR_LOCAL; |
| 3344 | if (!(s->flags & SF_FINST_MASK)) |
| 3345 | s->flags |= SF_FINST_R; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3346 | |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3347 | ret = 1; |
| 3348 | leave: |
| 3349 | free_trash_chunk(chunk); |
| 3350 | return ret; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3351 | } |
| 3352 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3353 | /* This stream analyser runs all HTTP request processing which is common to |
| 3354 | * frontends and backends, which means blocking ACLs, filters, connection-close, |
| 3355 | * reqadd, stats and redirects. This is performed for the designated proxy. |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3356 | * 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] | 3357 | * either needs more data or wants to immediately abort the request (eg: deny, |
| 3358 | * error, ...). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3359 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3360 | 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] | 3361 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 3362 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3363 | struct http_txn *txn = s->txn; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3364 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3365 | struct redirect_rule *rule; |
Willy Tarreau | f4f0412 | 2010-01-28 18:10:50 +0100 | [diff] [blame] | 3366 | struct cond_wordlist *wl; |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3367 | enum rule_result verdict; |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 3368 | int deny_status = HTTP_ERR_403; |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 3369 | struct connection *conn = objt_conn(sess->origin); |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3370 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 3371 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3372 | /* we need more data */ |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3373 | goto return_prx_yield; |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3374 | } |
| 3375 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3376 | 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] | 3377 | now_ms, __FUNCTION__, |
| 3378 | s, |
| 3379 | req, |
| 3380 | req->rex, req->wex, |
| 3381 | req->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3382 | req->buf->i, |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3383 | req->analysers); |
| 3384 | |
Willy Tarreau | 6541083 | 2014-04-28 21:25:43 +0200 | [diff] [blame] | 3385 | /* just in case we have some per-backend tracking */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3386 | stream_inc_be_http_req_ctr(s); |
Willy Tarreau | 6541083 | 2014-04-28 21:25:43 +0200 | [diff] [blame] | 3387 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3388 | /* evaluate http-request rules */ |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3389 | if (!LIST_ISEMPTY(&px->http_req_rules)) { |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 3390 | 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] | 3391 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3392 | switch (verdict) { |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3393 | case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */ |
| 3394 | goto return_prx_yield; |
| 3395 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3396 | case HTTP_RULE_RES_CONT: |
| 3397 | case HTTP_RULE_RES_STOP: /* nothing to do */ |
| 3398 | break; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 3399 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3400 | case HTTP_RULE_RES_DENY: /* deny or tarpit */ |
| 3401 | if (txn->flags & TX_CLTARPIT) |
| 3402 | goto tarpit; |
| 3403 | goto deny; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 3404 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3405 | case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */ |
| 3406 | goto return_prx_cond; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 3407 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3408 | case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */ |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 3409 | goto done; |
| 3410 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3411 | case HTTP_RULE_RES_BADREQ: /* failed with a bad request */ |
| 3412 | goto return_bad_req; |
| 3413 | } |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3414 | } |
| 3415 | |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 3416 | if (conn && conn->flags & CO_FL_EARLY_DATA) { |
| 3417 | struct hdr_ctx ctx; |
| 3418 | |
| 3419 | ctx.idx = 0; |
| 3420 | if (!http_find_header2("Early-Data", strlen("Early-Data"), |
| 3421 | s->req.buf->p, &txn->hdr_idx, &ctx)) { |
| 3422 | if (unlikely(http_header_add_tail2(&txn->req, |
| 3423 | &txn->hdr_idx, "Early-Data: 1", |
| 3424 | strlen("Early-Data: 1"))) < 0) { |
| 3425 | goto return_bad_req; |
| 3426 | } |
| 3427 | } |
| 3428 | |
| 3429 | } |
| 3430 | |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 3431 | /* OK at this stage, we know that the request was accepted according to |
| 3432 | * the http-request rules, we can check for the stats. Note that the |
| 3433 | * URI is detected *before* the req* rules in order not to be affected |
| 3434 | * by a possible reqrep, while they are processed *after* so that a |
| 3435 | * reqdeny can still block them. This clearly needs to change in 1.6! |
| 3436 | */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 3437 | if (stats_check_uri(&s->si[1], txn, px)) { |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 3438 | s->target = &http_stats_applet.obj_type; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 3439 | 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] | 3440 | txn->status = 500; |
| 3441 | s->logs.tv_request = now; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 3442 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3443 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3444 | if (!(s->flags & SF_ERR_MASK)) |
| 3445 | s->flags |= SF_ERR_RESOURCE; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 3446 | goto return_prx_cond; |
| 3447 | } |
| 3448 | |
| 3449 | /* parse the whole stats request and extract the relevant information */ |
| 3450 | http_handle_stats(s, req); |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 3451 | 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] | 3452 | /* not all actions implemented: deny, allow, auth */ |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3453 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3454 | if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */ |
| 3455 | goto deny; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 3456 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3457 | if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */ |
| 3458 | goto return_prx_cond; |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 3459 | } |
| 3460 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3461 | /* evaluate the req* rules except reqadd */ |
| 3462 | if (px->req_exp != NULL) { |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 3463 | if (apply_filters_to_request(s, req, px) < 0) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3464 | goto return_bad_req; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3465 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3466 | if (txn->flags & TX_CLDENY) |
| 3467 | goto deny; |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 3468 | |
Jarno Huuskonen | 800d176 | 2017-03-06 14:56:36 +0200 | [diff] [blame] | 3469 | if (txn->flags & TX_CLTARPIT) { |
| 3470 | deny_status = HTTP_ERR_500; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3471 | goto tarpit; |
Jarno Huuskonen | 800d176 | 2017-03-06 14:56:36 +0200 | [diff] [blame] | 3472 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3473 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3474 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3475 | /* add request headers from the rule sets in the same order */ |
| 3476 | list_for_each_entry(wl, &px->req_add, list) { |
| 3477 | if (wl->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 3478 | 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] | 3479 | ret = acl_pass(ret); |
| 3480 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 3481 | ret = !ret; |
| 3482 | if (!ret) |
| 3483 | continue; |
| 3484 | } |
| 3485 | |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 3486 | 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] | 3487 | goto return_bad_req; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 3488 | } |
| 3489 | |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 3490 | |
| 3491 | /* Proceed with the stats now. */ |
William Lallemand | 71bd11a | 2017-11-20 19:13:14 +0100 | [diff] [blame] | 3492 | if (unlikely(objt_applet(s->target) == &http_stats_applet) || |
| 3493 | unlikely(objt_applet(s->target) == &http_cache_applet)) { |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3494 | /* process the stats request now */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3495 | 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] | 3496 | HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1); |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 3497 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3498 | if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is |
| 3499 | s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy |
| 3500 | if (!(s->flags & SF_FINST_MASK)) |
| 3501 | s->flags |= SF_FINST_R; |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 3502 | |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 3503 | /* 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] | 3504 | req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END); |
| 3505 | req->analysers &= ~AN_REQ_FLT_XFER_DATA; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 3506 | req->analysers |= AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3507 | goto done; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3508 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 3509 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3510 | /* check whether we have some ACLs set to redirect this request */ |
| 3511 | list_for_each_entry(rule, &px->redirect_rules, list) { |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 3512 | if (rule->cond) { |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3513 | int ret; |
| 3514 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 3515 | 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] | 3516 | ret = acl_pass(ret); |
| 3517 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 3518 | ret = !ret; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3519 | if (!ret) |
| 3520 | continue; |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 3521 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3522 | if (!http_apply_redirect_rule(rule, s, txn)) |
| 3523 | goto return_bad_req; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3524 | goto done; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3525 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 3526 | |
Willy Tarreau | 2be3939 | 2010-01-03 17:24:51 +0100 | [diff] [blame] | 3527 | /* POST requests may be accompanied with an "Expect: 100-Continue" header. |
| 3528 | * If this happens, then the data will not come immediately, so we must |
| 3529 | * send all what we have without waiting. Note that due to the small gain |
| 3530 | * 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] | 3531 | * 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] | 3532 | * itself once used. |
| 3533 | */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 3534 | req->flags |= CF_SEND_DONTWAIT; |
Willy Tarreau | 2be3939 | 2010-01-03 17:24:51 +0100 | [diff] [blame] | 3535 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3536 | done: /* done with this analyser, continue with next ones that the calling |
| 3537 | * points will have set, if any. |
| 3538 | */ |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3539 | req->analyse_exp = TICK_ETERNITY; |
Thierry FOURNIER | 7566e30 | 2014-08-22 06:55:26 +0200 | [diff] [blame] | 3540 | done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */ |
| 3541 | req->analysers &= ~an_bit; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3542 | return 1; |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 3543 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3544 | tarpit: |
Willy Tarreau | 6a0bca9 | 2017-06-11 17:56:27 +0200 | [diff] [blame] | 3545 | /* Allow cookie logging |
| 3546 | */ |
| 3547 | if (s->be->cookie_name || sess->fe->capture_name) |
| 3548 | manage_client_side_cookies(s, req); |
| 3549 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3550 | /* When a connection is tarpitted, we use the tarpit timeout, |
| 3551 | * which may be the same as the connect timeout if unspecified. |
| 3552 | * If unset, then set it to zero because we really want it to |
| 3553 | * eventually expire. We build the tarpit as an analyser. |
| 3554 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 3555 | channel_erase(&s->req); |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3556 | |
| 3557 | /* wipe the request out so that we can drop the connection early |
| 3558 | * if the client closes first. |
| 3559 | */ |
| 3560 | channel_dont_connect(req); |
Bertrand Paquet | 83a2c3d | 2016-04-06 11:58:31 +0200 | [diff] [blame] | 3561 | |
Jarno Huuskonen | 800d176 | 2017-03-06 14:56:36 +0200 | [diff] [blame] | 3562 | txn->status = http_err_codes[deny_status]; |
| 3563 | |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 3564 | req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */ |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3565 | req->analysers |= AN_REQ_HTTP_TARPIT; |
| 3566 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit); |
| 3567 | if (!req->analyse_exp) |
| 3568 | req->analyse_exp = tick_add(now_ms, 0); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3569 | stream_inc_http_err_ctr(s); |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 3570 | HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3571 | if (sess->fe != s->be) |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 3572 | HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 3573 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 3574 | HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1); |
Thierry FOURNIER | 7566e30 | 2014-08-22 06:55:26 +0200 | [diff] [blame] | 3575 | goto done_without_exp; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3576 | |
| 3577 | deny: /* this request was blocked (denied) */ |
Bertrand Paquet | 83a2c3d | 2016-04-06 11:58:31 +0200 | [diff] [blame] | 3578 | |
| 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 | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3584 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 3585 | txn->status = http_err_codes[deny_status]; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3586 | s->logs.tv_request = now; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 3587 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3588 | stream_inc_http_err_ctr(s); |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 3589 | HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3590 | if (sess->fe != s->be) |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 3591 | HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 3592 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 3593 | HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1); |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3594 | goto return_prx_cond; |
| 3595 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3596 | return_bad_req: |
| 3597 | /* We centralize bad requests processing here */ |
| 3598 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 3599 | /* we detected a parsing error. We want to archive this request |
| 3600 | * in the dedicated proxy area for later troubleshooting. |
| 3601 | */ |
Emeric Brun | 8c1aaa2 | 2017-06-15 11:30:06 +0200 | [diff] [blame] | 3602 | 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] | 3603 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 3604 | |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 3605 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3606 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3607 | txn->status = 400; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 3608 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3609 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 3610 | HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 3611 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 3612 | HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1); |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 3613 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3614 | return_prx_cond: |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3615 | if (!(s->flags & SF_ERR_MASK)) |
| 3616 | s->flags |= SF_ERR_PRXCOND; |
| 3617 | if (!(s->flags & SF_FINST_MASK)) |
| 3618 | s->flags |= SF_FINST_R; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 3619 | |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 3620 | req->analysers &= AN_REQ_FLT_END; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3621 | req->analyse_exp = TICK_ETERNITY; |
| 3622 | return 0; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3623 | |
| 3624 | return_prx_yield: |
| 3625 | channel_dont_connect(req); |
| 3626 | return 0; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3627 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3628 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3629 | /* This function performs all the processing enabled for the current request. |
| 3630 | * It returns 1 if the processing can continue on next analysers, or zero if it |
| 3631 | * needs more data, encounters an error, or wants to immediately abort the |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 3632 | * request. It relies on buffers flags, and updates s->req.analysers. |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3633 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3634 | 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] | 3635 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 3636 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3637 | struct http_txn *txn = s->txn; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3638 | struct http_msg *msg = &txn->req; |
Willy Tarreau | ee335e6 | 2015-04-21 18:15:13 +0200 | [diff] [blame] | 3639 | struct connection *cli_conn = objt_conn(strm_sess(s)->origin); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3640 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 3641 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3642 | /* we need more data */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 3643 | channel_dont_connect(req); |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3644 | return 0; |
| 3645 | } |
| 3646 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3647 | 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] | 3648 | now_ms, __FUNCTION__, |
| 3649 | s, |
| 3650 | req, |
| 3651 | req->rex, req->wex, |
| 3652 | req->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3653 | req->buf->i, |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3654 | req->analysers); |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3655 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3656 | /* |
| 3657 | * Right now, we know that we have processed the entire headers |
| 3658 | * and that unwanted requests have been filtered out. We can do |
| 3659 | * whatever we want with the remaining request. Also, now we |
| 3660 | * may have separate values for ->fe, ->be. |
| 3661 | */ |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3662 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3663 | /* |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3664 | * If HTTP PROXY is set we simply get remote server address parsing |
| 3665 | * incoming request. Note that this requires that a connection is |
| 3666 | * allocated on the server side. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3667 | */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3668 | 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] | 3669 | struct connection *conn; |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 3670 | char *path; |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 3671 | |
Willy Tarreau | 9471b8c | 2013-12-15 13:31:35 +0100 | [diff] [blame] | 3672 | /* Note that for now we don't reuse existing proxy connections */ |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 3673 | 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] | 3674 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 3675 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3676 | txn->status = 500; |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 3677 | req->analysers &= AN_REQ_FLT_END; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 3678 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 3679 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3680 | if (!(s->flags & SF_ERR_MASK)) |
| 3681 | s->flags |= SF_ERR_RESOURCE; |
| 3682 | if (!(s->flags & SF_FINST_MASK)) |
| 3683 | s->flags |= SF_FINST_R; |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 3684 | |
| 3685 | return 0; |
| 3686 | } |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 3687 | |
| 3688 | path = http_get_path(txn); |
| 3689 | url2sa(req->buf->p + msg->sl.rq.u, |
| 3690 | 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] | 3691 | &conn->addr.to, NULL); |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 3692 | /* if the path was found, we have to remove everything between |
| 3693 | * req->buf->p + msg->sl.rq.u and path (excluded). If it was not |
| 3694 | * found, we need to replace from req->buf->p + msg->sl.rq.u for |
| 3695 | * u_l characters by a single "/". |
| 3696 | */ |
| 3697 | if (path) { |
| 3698 | char *cur_ptr = req->buf->p; |
| 3699 | char *cur_end = cur_ptr + txn->req.sl.rq.l; |
| 3700 | int delta; |
| 3701 | |
| 3702 | delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0); |
| 3703 | http_msg_move_end(&txn->req, delta); |
| 3704 | cur_end += delta; |
| 3705 | if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL) |
| 3706 | goto return_bad_req; |
| 3707 | } |
| 3708 | else { |
| 3709 | char *cur_ptr = req->buf->p; |
| 3710 | char *cur_end = cur_ptr + txn->req.sl.rq.l; |
| 3711 | int delta; |
| 3712 | |
| 3713 | delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, |
| 3714 | req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1); |
| 3715 | http_msg_move_end(&txn->req, delta); |
| 3716 | cur_end += delta; |
| 3717 | if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL) |
| 3718 | goto return_bad_req; |
| 3719 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3720 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3721 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3722 | /* |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 3723 | * 7: Now we can work with the cookies. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3724 | * Note that doing so might move headers in the request, but |
| 3725 | * the fields will stay coherent and the URI will not move. |
| 3726 | * This should only be performed in the backend. |
| 3727 | */ |
Bertrand Paquet | 83a2c3d | 2016-04-06 11:58:31 +0200 | [diff] [blame] | 3728 | if (s->be->cookie_name || sess->fe->capture_name) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3729 | manage_client_side_cookies(s, req); |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 3730 | |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 3731 | /* add unique-id if "header-unique-id" is specified */ |
| 3732 | |
Thierry Fournier | f4011dd | 2016-03-29 17:23:51 +0200 | [diff] [blame] | 3733 | if (!LIST_ISEMPTY(&sess->fe->format_unique_id) && !s->unique_id) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 3734 | if ((s->unique_id = pool_alloc(pool_head_uniqueid)) == NULL) |
William Lallemand | 5b7ea3a | 2013-08-28 15:44:19 +0200 | [diff] [blame] | 3735 | goto return_bad_req; |
| 3736 | s->unique_id[0] = '\0'; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3737 | 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] | 3738 | } |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 3739 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3740 | if (sess->fe->header_unique_id && s->unique_id) { |
| 3741 | 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] | 3742 | if (trash.len < 0) |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 3743 | goto return_bad_req; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3744 | 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] | 3745 | goto return_bad_req; |
| 3746 | } |
| 3747 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 3748 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3749 | * 9: add X-Forwarded-For if either the frontend or the backend |
| 3750 | * asks for it. |
| 3751 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3752 | if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) { |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 3753 | struct hdr_ctx ctx = { .idx = 0 }; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3754 | if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) && |
| 3755 | http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name, |
| 3756 | 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] | 3757 | req->buf->p, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 3758 | /* The header is set to be added only if none is present |
| 3759 | * and we found it, so don't do anything. |
| 3760 | */ |
| 3761 | } |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3762 | else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3763 | /* Add an X-Forwarded-For header unless the source IP is |
| 3764 | * in the 'except' network range. |
| 3765 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3766 | if ((!sess->fe->except_mask.s_addr || |
| 3767 | (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr) |
| 3768 | != sess->fe->except_net.s_addr) && |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3769 | (!s->be->except_mask.s_addr || |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3770 | (((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] | 3771 | != s->be->except_net.s_addr)) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 3772 | int len; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3773 | unsigned char *pn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3774 | pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr; |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 3775 | |
| 3776 | /* Note: we rely on the backend to get the header name to be used for |
| 3777 | * x-forwarded-for, because the header is really meant for the backends. |
| 3778 | * However, if the backend did not specify any option, we have to rely |
| 3779 | * on the frontend's header name. |
| 3780 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3781 | if (s->be->fwdfor_hdr_len) { |
| 3782 | len = s->be->fwdfor_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3783 | memcpy(trash.str, s->be->fwdfor_hdr_name, len); |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 3784 | } else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3785 | len = sess->fe->fwdfor_hdr_len; |
| 3786 | memcpy(trash.str, sess->fe->fwdfor_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 3787 | } |
Willy Tarreau | e9187f8 | 2014-04-14 15:27:14 +0200 | [diff] [blame] | 3788 | 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] | 3789 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3790 | 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] | 3791 | goto return_bad_req; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 3792 | } |
| 3793 | } |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3794 | else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3795 | /* FIXME: for the sake of completeness, we should also support |
| 3796 | * 'except' here, although it is mostly useless in this case. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3797 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3798 | int len; |
| 3799 | char pn[INET6_ADDRSTRLEN]; |
| 3800 | inet_ntop(AF_INET6, |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3801 | (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3802 | pn, sizeof(pn)); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3803 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3804 | /* Note: we rely on the backend to get the header name to be used for |
| 3805 | * x-forwarded-for, because the header is really meant for the backends. |
| 3806 | * However, if the backend did not specify any option, we have to rely |
| 3807 | * on the frontend's header name. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3808 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3809 | if (s->be->fwdfor_hdr_len) { |
| 3810 | len = s->be->fwdfor_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3811 | memcpy(trash.str, s->be->fwdfor_hdr_name, len); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3812 | } else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3813 | len = sess->fe->fwdfor_hdr_len; |
| 3814 | memcpy(trash.str, sess->fe->fwdfor_hdr_name, len); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3815 | } |
Willy Tarreau | e9187f8 | 2014-04-14 15:27:14 +0200 | [diff] [blame] | 3816 | len += snprintf(trash.str + len, trash.size - len, ": %s", pn); |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3817 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3818 | if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0)) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3819 | goto return_bad_req; |
| 3820 | } |
| 3821 | } |
| 3822 | |
| 3823 | /* |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3824 | * 10: add X-Original-To if either the frontend or the backend |
| 3825 | * asks for it. |
| 3826 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3827 | if ((sess->fe->options | s->be->options) & PR_O_ORGTO) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3828 | |
| 3829 | /* FIXME: don't know if IPv6 can handle that case too. */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3830 | if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3831 | /* Add an X-Original-To header unless the destination IP is |
| 3832 | * in the 'except' network range. |
| 3833 | */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3834 | conn_get_to_addr(cli_conn); |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3835 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3836 | if (cli_conn->addr.to.ss_family == AF_INET && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3837 | ((!sess->fe->except_mask_to.s_addr || |
| 3838 | (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr) |
| 3839 | != sess->fe->except_to.s_addr) && |
Emeric Brun | 5bd86a8 | 2010-10-22 17:23:04 +0200 | [diff] [blame] | 3840 | (!s->be->except_mask_to.s_addr || |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3841 | (((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] | 3842 | != s->be->except_to.s_addr))) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3843 | int len; |
| 3844 | unsigned char *pn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3845 | pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr; |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3846 | |
| 3847 | /* Note: we rely on the backend to get the header name to be used for |
| 3848 | * x-original-to, because the header is really meant for the backends. |
| 3849 | * However, if the backend did not specify any option, we have to rely |
| 3850 | * on the frontend's header name. |
| 3851 | */ |
| 3852 | if (s->be->orgto_hdr_len) { |
| 3853 | len = s->be->orgto_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3854 | memcpy(trash.str, s->be->orgto_hdr_name, len); |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3855 | } else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3856 | len = sess->fe->orgto_hdr_len; |
| 3857 | memcpy(trash.str, sess->fe->orgto_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 3858 | } |
Willy Tarreau | e9187f8 | 2014-04-14 15:27:14 +0200 | [diff] [blame] | 3859 | 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] | 3860 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3861 | 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] | 3862 | goto return_bad_req; |
| 3863 | } |
| 3864 | } |
| 3865 | } |
| 3866 | |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 3867 | /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set. |
| 3868 | * If an "Upgrade" token is found, the header is left untouched in order not to have |
| 3869 | * to deal with some servers bugs : some of them fail an Upgrade if anything but |
| 3870 | * "Upgrade" is present in the Connection header. |
| 3871 | */ |
| 3872 | if (!(txn->flags & TX_HDR_CONN_UPG) && |
| 3873 | (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3874 | ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 3875 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) { |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3876 | unsigned int want_flags = 0; |
| 3877 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3878 | if (msg->flags & HTTP_MSGF_VER_11) { |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 3879 | if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3880 | ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 3881 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3882 | !((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3883 | want_flags |= TX_CON_CLO_SET; |
| 3884 | } else { |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 3885 | if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3886 | ((sess->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL && |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 3887 | (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3888 | ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3889 | want_flags |= TX_CON_KAL_SET; |
| 3890 | } |
| 3891 | |
| 3892 | 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] | 3893 | http_change_connection_header(txn, msg, want_flags); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3894 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3895 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3896 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3897 | /* If we have no server assigned yet and we're balancing on url_param |
| 3898 | * with a POST request, we may be interested in checking the body for |
| 3899 | * that parameter. This will be done in another analyser. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3900 | */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3901 | if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) && |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3902 | s->txn->meth == HTTP_METH_POST && s->be->url_param_name != NULL && |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3903 | (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) { |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 3904 | channel_dont_connect(req); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3905 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3906 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3907 | |
Christopher Faulet | be821b9 | 2017-03-30 11:21:53 +0200 | [diff] [blame] | 3908 | req->analysers &= ~AN_REQ_FLT_XFER_DATA; |
| 3909 | req->analysers |= AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 3910 | #ifdef TCP_QUICKACK |
Christopher Faulet | be821b9 | 2017-03-30 11:21:53 +0200 | [diff] [blame] | 3911 | /* We expect some data from the client. Unless we know for sure |
| 3912 | * we already have a full request, we have to re-enable quick-ack |
| 3913 | * in case we previously disabled it, otherwise we might cause |
| 3914 | * the client to delay further data. |
| 3915 | */ |
| 3916 | if ((sess->listener->options & LI_O_NOQUICKACK) && |
| 3917 | cli_conn && conn_ctrl_ready(cli_conn) && |
| 3918 | ((msg->flags & HTTP_MSGF_TE_CHNK) || |
| 3919 | (msg->body_len > req->buf->i - txn->req.eoh - 2))) |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 3920 | setsockopt(cli_conn->handle.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one)); |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 3921 | #endif |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 3922 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3923 | /************************************************************* |
| 3924 | * OK, that's finished for the headers. We have done what we * |
| 3925 | * could. Let's switch to the DATA state. * |
| 3926 | ************************************************************/ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3927 | req->analyse_exp = TICK_ETERNITY; |
| 3928 | req->analysers &= ~an_bit; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3929 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3930 | s->logs.tv_request = now; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3931 | /* OK let's go on with the BODY now */ |
| 3932 | return 1; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3933 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3934 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 3935 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
Willy Tarreau | f073a83 | 2009-03-01 23:21:47 +0100 | [diff] [blame] | 3936 | /* we detected a parsing error. We want to archive this request |
| 3937 | * in the dedicated proxy area for later troubleshooting. |
| 3938 | */ |
Emeric Brun | 8c1aaa2 | 2017-06-15 11:30:06 +0200 | [diff] [blame] | 3939 | 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] | 3940 | } |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 3941 | |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 3942 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3943 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3944 | txn->status = 400; |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 3945 | req->analysers &= AN_REQ_FLT_END; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 3946 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3947 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 3948 | HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 3949 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 3950 | HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1); |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3951 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3952 | if (!(s->flags & SF_ERR_MASK)) |
| 3953 | s->flags |= SF_ERR_PRXCOND; |
| 3954 | if (!(s->flags & SF_FINST_MASK)) |
| 3955 | s->flags |= SF_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3956 | return 0; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 3957 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3958 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3959 | /* This function is an analyser which processes the HTTP tarpit. It always |
| 3960 | * returns zero, at the beginning because it prevents any other processing |
| 3961 | * from occurring, and at the end because it terminates the request. |
| 3962 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3963 | 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] | 3964 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3965 | struct http_txn *txn = s->txn; |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3966 | |
| 3967 | /* This connection is being tarpitted. The CLIENT side has |
| 3968 | * already set the connect expiration date to the right |
| 3969 | * timeout. We just have to check that the client is still |
| 3970 | * there and that the timeout has not expired. |
| 3971 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 3972 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 3973 | if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 && |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3974 | !tick_is_expired(req->analyse_exp, now_ms)) |
| 3975 | return 0; |
| 3976 | |
| 3977 | /* We will set the queue timer to the time spent, just for |
| 3978 | * logging purposes. We fake a 500 server error, so that the |
| 3979 | * attacker will not suspect his connection has been tarpitted. |
| 3980 | * It will not cause trouble to the logs because we can exclude |
| 3981 | * the tarpitted connections by filtering on the 'PT' status flags. |
| 3982 | */ |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3983 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 3984 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 3985 | if (!(req->flags & CF_READ_ERROR)) |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 3986 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3987 | |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 3988 | req->analysers &= AN_REQ_FLT_END; |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3989 | req->analyse_exp = TICK_ETERNITY; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3990 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3991 | if (!(s->flags & SF_ERR_MASK)) |
| 3992 | s->flags |= SF_ERR_PRXCOND; |
| 3993 | if (!(s->flags & SF_FINST_MASK)) |
| 3994 | s->flags |= SF_FINST_T; |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3995 | return 0; |
| 3996 | } |
| 3997 | |
Willy Tarreau | 5a8f947 | 2014-04-10 11:16:06 +0200 | [diff] [blame] | 3998 | /* This function is an analyser which waits for the HTTP request body. It waits |
| 3999 | * for either the buffer to be full, or the full advertised contents to have |
| 4000 | * reached the buffer. It must only be called after the standard HTTP request |
| 4001 | * processing has occurred, because it expects the request to be parsed and will |
| 4002 | * look for the Expect header. It may send a 100-Continue interim response. It |
| 4003 | * takes in input any state starting from HTTP_MSG_BODY and leaves with one of |
| 4004 | * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it |
| 4005 | * 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] | 4006 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4007 | 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] | 4008 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4009 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4010 | struct http_txn *txn = s->txn; |
| 4011 | struct http_msg *msg = &s->txn->req; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4012 | |
| 4013 | /* We have to parse the HTTP request body to find any required data. |
| 4014 | * "balance url_param check_post" should have been the only way to get |
| 4015 | * into this. We were brought here after HTTP header analysis, so all |
| 4016 | * related structures are ready. |
| 4017 | */ |
| 4018 | |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4019 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
| 4020 | /* This is the first call */ |
| 4021 | if (msg->msg_state < HTTP_MSG_BODY) |
| 4022 | goto missing_data; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4023 | |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4024 | if (msg->msg_state < HTTP_MSG_100_SENT) { |
| 4025 | /* If we have HTTP/1.1 and Expect: 100-continue, then we must |
| 4026 | * send an HTTP/1.1 100 Continue intermediate response. |
| 4027 | */ |
| 4028 | if (msg->flags & HTTP_MSGF_VER_11) { |
| 4029 | struct hdr_ctx ctx; |
| 4030 | ctx.idx = 0; |
| 4031 | /* Expect is allowed in 1.1, look for it */ |
| 4032 | if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) && |
| 4033 | 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] | 4034 | 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] | 4035 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4036 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4037 | } |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4038 | msg->msg_state = HTTP_MSG_100_SENT; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4039 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4040 | |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 4041 | /* 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] | 4042 | * req->buf->p still points to the beginning of the message. We |
| 4043 | * must save the body in msg->next because it survives buffer |
| 4044 | * re-alignments. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4045 | */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 4046 | msg->next = msg->sov; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4047 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4048 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4049 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 4050 | else |
| 4051 | msg->msg_state = HTTP_MSG_DATA; |
| 4052 | } |
| 4053 | |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4054 | if (!(msg->flags & HTTP_MSGF_TE_CHNK)) { |
| 4055 | /* 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] | 4056 | if (http_body_bytes(msg) < msg->body_len) |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4057 | goto missing_data; |
| 4058 | |
| 4059 | /* OK we have everything we need now */ |
| 4060 | goto http_end; |
| 4061 | } |
| 4062 | |
| 4063 | /* OK here we're parsing a chunked-encoded message */ |
| 4064 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4065 | if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 4066 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4067 | * 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] | 4068 | * TRAILERS state. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 4069 | */ |
Willy Tarreau | e56cdd3 | 2017-09-21 08:36:33 +0200 | [diff] [blame] | 4070 | unsigned int chunk; |
| 4071 | 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] | 4072 | |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 4073 | if (!ret) |
| 4074 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4075 | else if (ret < 0) { |
Willy Tarreau | e56cdd3 | 2017-09-21 08:36:33 +0200 | [diff] [blame] | 4076 | msg->err_pos = req->buf->i + ret; |
| 4077 | if (msg->err_pos < 0) |
| 4078 | msg->err_pos += req->buf->size; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4079 | stream_inc_http_err_ctr(s); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4080 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4081 | } |
Willy Tarreau | e56cdd3 | 2017-09-21 08:36:33 +0200 | [diff] [blame] | 4082 | |
| 4083 | msg->chunk_len = chunk; |
| 4084 | msg->body_len += chunk; |
| 4085 | |
| 4086 | msg->sol = ret; |
Christopher Faulet | 113f7de | 2015-12-14 14:52:13 +0100 | [diff] [blame] | 4087 | msg->next += ret; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 4088 | msg->msg_state = msg->chunk_len ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4089 | } |
| 4090 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4091 | /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state. |
Willy Tarreau | e115b49 | 2015-05-01 23:05:14 +0200 | [diff] [blame] | 4092 | * We have the first data byte is in msg->sov + msg->sol. We're waiting |
| 4093 | * for at least a whole chunk or the whole content length bytes after |
| 4094 | * msg->sov + msg->sol. |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4095 | */ |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4096 | if (msg->msg_state == HTTP_MSG_TRAILERS) |
| 4097 | goto http_end; |
| 4098 | |
Willy Tarreau | e115b49 | 2015-05-01 23:05:14 +0200 | [diff] [blame] | 4099 | 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] | 4100 | goto http_end; |
| 4101 | |
| 4102 | missing_data: |
Willy Tarreau | 31a1995 | 2014-04-10 11:50:37 +0200 | [diff] [blame] | 4103 | /* we get here if we need to wait for more data. If the buffer is full, |
| 4104 | * we have the maximum we can expect. |
| 4105 | */ |
| 4106 | if (buffer_full(req->buf, global.tune.maxrewrite)) |
| 4107 | goto http_end; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 4108 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4109 | 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] | 4110 | txn->status = 408; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 4111 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 4112 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4113 | if (!(s->flags & SF_ERR_MASK)) |
| 4114 | s->flags |= SF_ERR_CLITO; |
| 4115 | if (!(s->flags & SF_FINST_MASK)) |
| 4116 | s->flags |= SF_FINST_D; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4117 | goto return_err_msg; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4118 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4119 | |
| 4120 | /* we get here if we need to wait for more data */ |
Willy Tarreau | 31a1995 | 2014-04-10 11:50:37 +0200 | [diff] [blame] | 4121 | if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) { |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4122 | /* Not enough data. We'll re-use the http-request |
| 4123 | * timeout here. Ideally, we should set the timeout |
| 4124 | * relative to the accept() date. We just set the |
| 4125 | * request timeout once at the beginning of the |
| 4126 | * request. |
| 4127 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4128 | channel_dont_connect(req); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4129 | if (!tick_isset(req->analyse_exp)) |
Willy Tarreau | cd7afc0 | 2009-07-12 10:03:17 +0200 | [diff] [blame] | 4130 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4131 | return 0; |
| 4132 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4133 | |
| 4134 | http_end: |
| 4135 | /* The situation will not evolve, so let's give up on the analysis. */ |
| 4136 | s->logs.tv_request = now; /* update the request timer to reflect full request */ |
| 4137 | req->analysers &= ~an_bit; |
| 4138 | req->analyse_exp = TICK_ETERNITY; |
| 4139 | return 1; |
| 4140 | |
| 4141 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 4142 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4143 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4144 | txn->status = 400; |
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 | 522d6c0 | 2009-12-06 18:49:18 +0100 | [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_PRXCOND; |
| 4149 | if (!(s->flags & SF_FINST_MASK)) |
| 4150 | s->flags |= SF_FINST_R; |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 4151 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4152 | return_err_msg: |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4153 | req->analysers &= AN_REQ_FLT_END; |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 4154 | HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4155 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 4156 | HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4157 | return 0; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4158 | } |
| 4159 | |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4160 | /* send a server's name with an outgoing request over an established connection. |
| 4161 | * Note: this function is designed to be called once the request has been scheduled |
| 4162 | * for being forwarded. This is the reason why it rewinds the buffer before |
| 4163 | * proceeding. |
| 4164 | */ |
Willy Tarreau | 45c0d98 | 2012-03-09 12:11:57 +0100 | [diff] [blame] | 4165 | 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] | 4166 | |
| 4167 | struct hdr_ctx ctx; |
| 4168 | |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4169 | char *hdr_name = be->server_id_hdr_name; |
| 4170 | int hdr_name_len = be->server_id_hdr_len; |
Willy Tarreau | 394db37 | 2012-10-12 22:40:39 +0200 | [diff] [blame] | 4171 | struct channel *chn = txn->req.chn; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4172 | char *hdr_val; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4173 | unsigned int old_o, old_i; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4174 | |
William Lallemand | d9e9066 | 2012-01-30 17:27:17 +0100 | [diff] [blame] | 4175 | ctx.idx = 0; |
| 4176 | |
Willy Tarreau | 211cdec | 2014-04-17 20:18:08 +0200 | [diff] [blame] | 4177 | old_o = http_hdr_rewind(&txn->req); |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4178 | if (old_o) { |
| 4179 | /* The request was already skipped, let's restore it */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4180 | b_rew(chn->buf, old_o); |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 4181 | txn->req.next += old_o; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 4182 | txn->req.sov += old_o; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4183 | } |
| 4184 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4185 | old_i = chn->buf->i; |
| 4186 | 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] | 4187 | /* remove any existing values from the header */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 4188 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4189 | } |
| 4190 | |
| 4191 | /* Add the new header requested with the server value */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4192 | hdr_val = trash.str; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4193 | memcpy(hdr_val, hdr_name, hdr_name_len); |
| 4194 | hdr_val += hdr_name_len; |
| 4195 | *hdr_val++ = ':'; |
| 4196 | *hdr_val++ = ' '; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4197 | hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val); |
| 4198 | 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] | 4199 | |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4200 | if (old_o) { |
| 4201 | /* If this was a forwarded request, we must readjust the amount of |
| 4202 | * data to be forwarded in order to take into account the size |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 4203 | * variations. Note that the current state is >= HTTP_MSG_BODY, |
| 4204 | * so we don't have to adjust ->sol. |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4205 | */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 4206 | old_o += chn->buf->i - old_i; |
| 4207 | b_adv(chn->buf, old_o); |
| 4208 | txn->req.next -= old_o; |
| 4209 | txn->req.sov -= old_o; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4210 | } |
| 4211 | |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4212 | return 0; |
| 4213 | } |
| 4214 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4215 | /* Terminate current transaction and prepare a new one. This is very tricky |
| 4216 | * right now but it works. |
| 4217 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4218 | void http_end_txn_clean_session(struct stream *s) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4219 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4220 | int prev_status = s->txn->status; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 4221 | struct proxy *fe = strm_fe(s); |
Willy Tarreau | 858b103 | 2015-12-07 17:04:59 +0100 | [diff] [blame] | 4222 | struct proxy *be = s->be; |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 4223 | struct conn_stream *cs; |
Willy Tarreau | 323a2d9 | 2015-08-04 19:00:17 +0200 | [diff] [blame] | 4224 | struct connection *srv_conn; |
| 4225 | struct server *srv; |
Willy Tarreau | 449d74a | 2015-08-05 17:16:33 +0200 | [diff] [blame] | 4226 | unsigned int prev_flags = s->txn->flags; |
Willy Tarreau | 068621e | 2013-12-23 15:11:25 +0100 | [diff] [blame] | 4227 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4228 | /* FIXME: We need a more portable way of releasing a backend's and a |
| 4229 | * server's connections. We need a safer way to reinitialize buffer |
| 4230 | * flags. We also need a more accurate method for computing per-request |
| 4231 | * data. |
| 4232 | */ |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 4233 | /* |
| 4234 | * XXX cognet: This is probably wrong, this is killing a whole |
| 4235 | * connection, in the new world order, we probably want to just kill |
| 4236 | * the stream, this is to be revisited the day we handle multiple |
| 4237 | * streams in one server connection. |
| 4238 | */ |
| 4239 | cs = objt_cs(s->si[1].end); |
| 4240 | srv_conn = cs_conn(cs); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4241 | |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4242 | /* unless we're doing keep-alive, we want to quickly close the connection |
| 4243 | * to the server. |
| 4244 | */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4245 | if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) || |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4246 | !si_conn_ready(&s->si[1])) { |
| 4247 | s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF; |
| 4248 | si_shutr(&s->si[1]); |
| 4249 | si_shutw(&s->si[1]); |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4250 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4251 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4252 | if (s->flags & SF_BE_ASSIGNED) { |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 4253 | HA_ATOMIC_SUB(&be->beconn, 1); |
Willy Tarreau | 2d5cd47 | 2012-03-01 23:34:37 +0100 | [diff] [blame] | 4254 | if (unlikely(s->srv_conn)) |
| 4255 | sess_change_server(s, NULL); |
| 4256 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4257 | |
| 4258 | s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4259 | stream_process_counters(s); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4260 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4261 | if (s->txn->status) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4262 | int n; |
| 4263 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4264 | n = s->txn->status / 100; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4265 | if (n < 1 || n > 5) |
| 4266 | n = 0; |
| 4267 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4268 | if (fe->mode == PR_MODE_HTTP) { |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 4269 | HA_ATOMIC_ADD(&fe->fe_counters.p.http.rsp[n], 1); |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 4270 | } |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4271 | if ((s->flags & SF_BE_ASSIGNED) && |
Willy Tarreau | 858b103 | 2015-12-07 17:04:59 +0100 | [diff] [blame] | 4272 | (be->mode == PR_MODE_HTTP)) { |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 4273 | HA_ATOMIC_ADD(&be->be_counters.p.http.rsp[n], 1); |
| 4274 | HA_ATOMIC_ADD(&be->be_counters.p.http.cum_req, 1); |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 4275 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4276 | } |
| 4277 | |
| 4278 | /* don't count other requests' data */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4279 | s->logs.bytes_in -= s->req.buf->i; |
| 4280 | s->logs.bytes_out -= s->res.buf->i; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4281 | |
| 4282 | /* let's do a final log if we need it */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4283 | if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait && |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4284 | !(s->flags & SF_MONITOR) && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4285 | (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4286 | s->do_log(s); |
| 4287 | } |
| 4288 | |
Willy Tarreau | d713bcc | 2014-06-25 15:36:04 +0200 | [diff] [blame] | 4289 | /* stop tracking content-based counters */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4290 | stream_stop_content_counters(s); |
| 4291 | stream_update_time_stats(s); |
Willy Tarreau | 4bfc580 | 2014-06-17 12:19:18 +0200 | [diff] [blame] | 4292 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4293 | s->logs.accept_date = date; /* user-visible date for logging */ |
| 4294 | s->logs.tv_accept = now; /* corrected date for internal use */ |
Thierry FOURNIER / OZON.IO | 4cac359 | 2016-07-28 17:19:45 +0200 | [diff] [blame] | 4295 | s->logs.t_handshake = 0; /* There are no handshake in keep alive connection. */ |
| 4296 | s->logs.t_idle = -1; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4297 | tv_zero(&s->logs.tv_request); |
| 4298 | s->logs.t_queue = -1; |
| 4299 | s->logs.t_connect = -1; |
| 4300 | s->logs.t_data = -1; |
| 4301 | s->logs.t_close = 0; |
| 4302 | s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */ |
| 4303 | s->logs.srv_queue_size = 0; /* we will get this number soon */ |
| 4304 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4305 | s->logs.bytes_in = s->req.total = s->req.buf->i; |
| 4306 | s->logs.bytes_out = s->res.total = s->res.buf->i; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4307 | |
| 4308 | if (s->pend_pos) |
| 4309 | pendconn_free(s->pend_pos); |
| 4310 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4311 | if (objt_server(s->target)) { |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4312 | if (s->flags & SF_CURR_SESS) { |
| 4313 | s->flags &= ~SF_CURR_SESS; |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 4314 | HA_ATOMIC_SUB(&objt_server(s->target)->cur_sess, 1); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4315 | } |
Willy Tarreau | 858b103 | 2015-12-07 17:04:59 +0100 | [diff] [blame] | 4316 | if (may_dequeue_tasks(objt_server(s->target), be)) |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4317 | process_srv_queue(objt_server(s->target)); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4318 | } |
| 4319 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4320 | s->target = NULL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4321 | |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4322 | /* only release our endpoint if we don't intend to reuse the |
| 4323 | * connection. |
| 4324 | */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4325 | if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) || |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4326 | !si_conn_ready(&s->si[1])) { |
| 4327 | si_release_endpoint(&s->si[1]); |
Willy Tarreau | 323a2d9 | 2015-08-04 19:00:17 +0200 | [diff] [blame] | 4328 | srv_conn = NULL; |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4329 | } |
| 4330 | |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4331 | s->si[1].state = s->si[1].prev_state = SI_ST_INI; |
| 4332 | s->si[1].err_type = SI_ET_NONE; |
| 4333 | s->si[1].conn_retries = 0; /* used for logging too */ |
| 4334 | s->si[1].exp = TICK_ETERNITY; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4335 | 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] | 4336 | 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] | 4337 | 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] | 4338 | s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST); |
| 4339 | s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED); |
| 4340 | s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP); |
Willy Tarreau | 543db62 | 2012-11-15 16:41:22 +0100 | [diff] [blame] | 4341 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4342 | s->txn->meth = 0; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4343 | http_reset_txn(s); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4344 | s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ; |
Willy Tarreau | 068621e | 2013-12-23 15:11:25 +0100 | [diff] [blame] | 4345 | |
| 4346 | if (prev_status == 401 || prev_status == 407) { |
| 4347 | /* In HTTP keep-alive mode, if we receive a 401, we still have |
| 4348 | * a chance of being able to send the visitor again to the same |
| 4349 | * server over the same connection. This is required by some |
| 4350 | * broken protocols such as NTLM, and anyway whenever there is |
| 4351 | * an opportunity for sending the challenge to the proper place, |
| 4352 | * it's better to do it (at least it helps with debugging). |
| 4353 | */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4354 | s->txn->flags |= TX_PREFER_LAST; |
Willy Tarreau | bd99d58 | 2015-09-02 10:40:43 +0200 | [diff] [blame] | 4355 | if (srv_conn) |
| 4356 | srv_conn->flags |= CO_FL_PRIVATE; |
Willy Tarreau | 068621e | 2013-12-23 15:11:25 +0100 | [diff] [blame] | 4357 | } |
| 4358 | |
Willy Tarreau | 53f9685 | 2016-02-02 18:50:47 +0100 | [diff] [blame] | 4359 | /* Never ever allow to reuse a connection from a non-reuse backend */ |
| 4360 | if (srv_conn && (be->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR) |
| 4361 | srv_conn->flags |= CO_FL_PRIVATE; |
| 4362 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4363 | if (fe->options2 & PR_O2_INDEPSTR) |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4364 | s->si[1].flags |= SI_FL_INDEP_STR; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4365 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4366 | if (fe->options2 & PR_O2_NODELAY) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4367 | s->req.flags |= CF_NEVER_WAIT; |
| 4368 | s->res.flags |= CF_NEVER_WAIT; |
Willy Tarreau | 96e3121 | 2011-05-30 18:10:30 +0200 | [diff] [blame] | 4369 | } |
| 4370 | |
Willy Tarreau | 714ea78 | 2015-11-25 20:11:11 +0100 | [diff] [blame] | 4371 | /* we're removing the analysers, we MUST re-enable events detection. |
| 4372 | * We don't enable close on the response channel since it's either |
| 4373 | * already closed, or in keep-alive with an idle connection handler. |
| 4374 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4375 | channel_auto_read(&s->req); |
| 4376 | channel_auto_close(&s->req); |
| 4377 | channel_auto_read(&s->res); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4378 | |
Willy Tarreau | 1c59bd5 | 2015-11-02 20:20:11 +0100 | [diff] [blame] | 4379 | /* we're in keep-alive with an idle connection, monitor it if not already done */ |
| 4380 | if (srv_conn && LIST_ISEMPTY(&srv_conn->list)) { |
Willy Tarreau | 323a2d9 | 2015-08-04 19:00:17 +0200 | [diff] [blame] | 4381 | srv = objt_server(srv_conn->target); |
Willy Tarreau | 8dff998 | 2015-08-04 20:45:52 +0200 | [diff] [blame] | 4382 | if (!srv) |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 4383 | si_idle_cs(&s->si[1], NULL); |
Willy Tarreau | 53f9685 | 2016-02-02 18:50:47 +0100 | [diff] [blame] | 4384 | else if (srv_conn->flags & CO_FL_PRIVATE) |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 4385 | 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] | 4386 | else if (prev_flags & TX_NOT_FIRST) |
| 4387 | /* note: we check the request, not the connection, but |
| 4388 | * this is valid for strategies SAFE and AGGR, and in |
| 4389 | * case of ALWS, we don't care anyway. |
| 4390 | */ |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 4391 | 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] | 4392 | else |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 4393 | 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] | 4394 | } |
Christopher Faulet | e600624 | 2017-03-10 11:52:44 +0100 | [diff] [blame] | 4395 | s->req.analysers = strm_li(s) ? strm_li(s)->analysers : 0; |
| 4396 | s->res.analysers = 0; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4397 | } |
| 4398 | |
| 4399 | |
| 4400 | /* This function updates the request state machine according to the response |
| 4401 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 4402 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 4403 | * it is only used to find when a request/response couple is complete. Both |
| 4404 | * this function and its equivalent should loop until both return zero. It |
| 4405 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 4406 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4407 | int http_sync_req_state(struct stream *s) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4408 | { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4409 | struct channel *chn = &s->req; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4410 | struct http_txn *txn = s->txn; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4411 | unsigned int old_flags = chn->flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4412 | unsigned int old_state = txn->req.msg_state; |
| 4413 | |
Christopher Faulet | 4be9803 | 2017-07-18 10:48:24 +0200 | [diff] [blame] | 4414 | if (unlikely(txn->req.msg_state < HTTP_MSG_DONE)) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4415 | return 0; |
| 4416 | |
| 4417 | if (txn->req.msg_state == HTTP_MSG_DONE) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4418 | /* No need to read anymore, the request was completely parsed. |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 4419 | * We can shut the read side unless we want to abort_on_close, |
| 4420 | * or we have a POST request. The issue with POST requests is |
| 4421 | * that some browsers still send a CRLF after the request, and |
| 4422 | * this CRLF must be read so that it does not remain in the kernel |
| 4423 | * buffers, otherwise a close could cause an RST on some systems |
| 4424 | * (eg: Linux). |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 4425 | * Note that if we're using keep-alive on the client side, we'd |
| 4426 | * rather poll now and keep the polling enabled for the whole |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4427 | * stream's life than enabling/disabling it between each |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 4428 | * response and next request. |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4429 | */ |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 4430 | if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) && |
| 4431 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) && |
| 4432 | !(s->be->options & PR_O_ABRT_CLOSE) && |
| 4433 | txn->meth != HTTP_METH_POST) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4434 | channel_dont_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4435 | |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 4436 | /* if the server closes the connection, we want to immediately react |
| 4437 | * and close the socket to save packets and syscalls. |
| 4438 | */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4439 | s->si[1].flags |= SI_FL_NOHALF; |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 4440 | |
Willy Tarreau | 7f876a1 | 2015-11-18 11:59:55 +0100 | [diff] [blame] | 4441 | /* In any case we've finished parsing the request so we must |
| 4442 | * disable Nagle when sending data because 1) we're not going |
| 4443 | * to shut this side, and 2) the server is waiting for us to |
| 4444 | * send pending data. |
| 4445 | */ |
| 4446 | chn->flags |= CF_NEVER_WAIT; |
| 4447 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4448 | if (txn->rsp.msg_state == HTTP_MSG_ERROR) |
| 4449 | goto wait_other_side; |
| 4450 | |
| 4451 | if (txn->rsp.msg_state < HTTP_MSG_DONE) { |
| 4452 | /* The server has not finished to respond, so we |
| 4453 | * don't want to move in order not to upset it. |
| 4454 | */ |
| 4455 | goto wait_other_side; |
| 4456 | } |
| 4457 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4458 | /* When we get here, it means that both the request and the |
| 4459 | * response have finished receiving. Depending on the connection |
| 4460 | * mode, we'll have to wait for the last bytes to leave in either |
| 4461 | * direction, and sometimes for a close to be effective. |
| 4462 | */ |
| 4463 | |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4464 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 4465 | /* Server-close mode : queue a connection close to the server */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4466 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) |
| 4467 | channel_shutw_now(chn); |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4468 | } |
| 4469 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 4470 | /* Option forceclose is set, or either side wants to close, |
| 4471 | * let's enforce it now that we're not expecting any new |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4472 | * data to come. The caller knows the stream is complete |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4473 | * once both states are CLOSED. |
Christopher Faulet | 1486b0a | 2017-07-18 11:42:08 +0200 | [diff] [blame] | 4474 | * |
| 4475 | * However, there is an exception if the response |
| 4476 | * length is undefined. In this case, we need to wait |
| 4477 | * the close from the server. The response will be |
| 4478 | * switched in TUNNEL mode until the end. |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4479 | */ |
Christopher Faulet | 1486b0a | 2017-07-18 11:42:08 +0200 | [diff] [blame] | 4480 | if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) && |
| 4481 | txn->rsp.msg_state != HTTP_MSG_CLOSED) |
| 4482 | goto check_channel_flags; |
| 4483 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4484 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) { |
| 4485 | channel_shutr_now(chn); |
| 4486 | channel_shutw_now(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4487 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4488 | } |
| 4489 | else { |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4490 | /* The last possible modes are keep-alive and tunnel. Tunnel mode |
| 4491 | * will not have any analyser so it needs to poll for reads. |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4492 | */ |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4493 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) { |
| 4494 | channel_auto_read(chn); |
| 4495 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4496 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4497 | } |
| 4498 | |
Christopher Faulet | 4be9803 | 2017-07-18 10:48:24 +0200 | [diff] [blame] | 4499 | goto check_channel_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4500 | } |
| 4501 | |
| 4502 | if (txn->req.msg_state == HTTP_MSG_CLOSING) { |
| 4503 | http_msg_closing: |
| 4504 | /* nothing else to forward, just waiting for the output buffer |
| 4505 | * to be empty and for the shutw_now to take effect. |
| 4506 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4507 | if (channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4508 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 4509 | goto http_msg_closed; |
| 4510 | } |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4511 | else if (chn->flags & CF_SHUTW) { |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 4512 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4513 | txn->req.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4514 | } |
Christopher Faulet | 56d2609 | 2017-07-20 11:05:10 +0200 | [diff] [blame] | 4515 | goto wait_other_side; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4516 | } |
| 4517 | |
| 4518 | if (txn->req.msg_state == HTTP_MSG_CLOSED) { |
| 4519 | http_msg_closed: |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 4520 | /* see above in MSG_DONE why we only do this in these states */ |
| 4521 | if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) && |
| 4522 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) && |
| 4523 | !(s->be->options & PR_O_ABRT_CLOSE)) |
Willy Tarreau | 2e7a165 | 2013-12-15 15:32:10 +0100 | [diff] [blame] | 4524 | channel_dont_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4525 | goto wait_other_side; |
| 4526 | } |
| 4527 | |
Christopher Faulet | 4be9803 | 2017-07-18 10:48:24 +0200 | [diff] [blame] | 4528 | check_channel_flags: |
| 4529 | /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */ |
| 4530 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) { |
| 4531 | /* if we've just closed an output, let's switch */ |
| 4532 | s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */ |
| 4533 | txn->req.msg_state = HTTP_MSG_CLOSING; |
| 4534 | goto http_msg_closing; |
| 4535 | } |
| 4536 | |
| 4537 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4538 | wait_other_side: |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4539 | return txn->req.msg_state != old_state || chn->flags != old_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4540 | } |
| 4541 | |
| 4542 | |
| 4543 | /* This function updates the response state machine according to the request |
| 4544 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 4545 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 4546 | * it is only used to find when a request/response couple is complete. Both |
| 4547 | * this function and its equivalent should loop until both return zero. It |
| 4548 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 4549 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4550 | int http_sync_res_state(struct stream *s) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4551 | { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4552 | struct channel *chn = &s->res; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4553 | struct http_txn *txn = s->txn; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4554 | unsigned int old_flags = chn->flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4555 | unsigned int old_state = txn->rsp.msg_state; |
| 4556 | |
Christopher Faulet | 4be9803 | 2017-07-18 10:48:24 +0200 | [diff] [blame] | 4557 | if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE)) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4558 | return 0; |
| 4559 | |
| 4560 | if (txn->rsp.msg_state == HTTP_MSG_DONE) { |
| 4561 | /* In theory, we don't need to read anymore, but we must |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4562 | * still monitor the server connection for a possible close |
| 4563 | * while the request is being uploaded, so we don't disable |
| 4564 | * reading. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4565 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4566 | /* channel_dont_read(chn); */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4567 | |
| 4568 | if (txn->req.msg_state == HTTP_MSG_ERROR) |
| 4569 | goto wait_other_side; |
| 4570 | |
| 4571 | if (txn->req.msg_state < HTTP_MSG_DONE) { |
| 4572 | /* The client seems to still be sending data, probably |
| 4573 | * because we got an error response during an upload. |
| 4574 | * We have the choice of either breaking the connection |
| 4575 | * or letting it pass through. Let's do the later. |
| 4576 | */ |
| 4577 | goto wait_other_side; |
| 4578 | } |
| 4579 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4580 | /* When we get here, it means that both the request and the |
| 4581 | * response have finished receiving. Depending on the connection |
| 4582 | * mode, we'll have to wait for the last bytes to leave in either |
| 4583 | * direction, and sometimes for a close to be effective. |
| 4584 | */ |
| 4585 | |
| 4586 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 4587 | /* Server-close mode : shut read and wait for the request |
| 4588 | * side to close its output buffer. The caller will detect |
| 4589 | * when we're in DONE and the other is in CLOSED and will |
| 4590 | * catch that for the final cleanup. |
| 4591 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4592 | if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW))) |
| 4593 | channel_shutr_now(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4594 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4595 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 4596 | /* Option forceclose is set, or either side wants to close, |
| 4597 | * let's enforce it now that we're not expecting any new |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4598 | * data to come. The caller knows the stream is complete |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4599 | * once both states are CLOSED. |
Christopher Faulet | 1486b0a | 2017-07-18 11:42:08 +0200 | [diff] [blame] | 4600 | * |
| 4601 | * However, there is an exception if the response length |
| 4602 | * is undefined. In this case, we switch in TUNNEL mode. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4603 | */ |
Christopher Faulet | 1486b0a | 2017-07-18 11:42:08 +0200 | [diff] [blame] | 4604 | if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN)) { |
| 4605 | channel_auto_read(chn); |
| 4606 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
| 4607 | chn->flags |= CF_NEVER_WAIT; |
| 4608 | } |
| 4609 | else if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4610 | channel_shutr_now(chn); |
| 4611 | channel_shutw_now(chn); |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4612 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4613 | } |
| 4614 | else { |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4615 | /* The last possible modes are keep-alive and tunnel. Tunnel will |
| 4616 | * need to forward remaining data. Keep-alive will need to monitor |
| 4617 | * for connection closing. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4618 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4619 | channel_auto_read(chn); |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 4620 | chn->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4621 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) |
| 4622 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4623 | } |
| 4624 | |
Christopher Faulet | 4be9803 | 2017-07-18 10:48:24 +0200 | [diff] [blame] | 4625 | goto check_channel_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4626 | } |
| 4627 | |
| 4628 | if (txn->rsp.msg_state == HTTP_MSG_CLOSING) { |
| 4629 | http_msg_closing: |
| 4630 | /* nothing else to forward, just waiting for the output buffer |
| 4631 | * to be empty and for the shutw_now to take effect. |
| 4632 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4633 | if (channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4634 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 4635 | goto http_msg_closed; |
| 4636 | } |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4637 | else if (chn->flags & CF_SHUTW) { |
Christopher Faulet | a3992e0 | 2017-07-18 10:35:55 +0200 | [diff] [blame] | 4638 | txn->rsp.err_state = txn->rsp.msg_state; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4639 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 4640 | HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4641 | if (objt_server(s->target)) |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 4642 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4643 | } |
Christopher Faulet | 56d2609 | 2017-07-20 11:05:10 +0200 | [diff] [blame] | 4644 | goto wait_other_side; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4645 | } |
| 4646 | |
| 4647 | if (txn->rsp.msg_state == HTTP_MSG_CLOSED) { |
| 4648 | http_msg_closed: |
| 4649 | /* drop any pending data */ |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 4650 | channel_truncate(chn); |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4651 | channel_auto_close(chn); |
| 4652 | channel_auto_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4653 | goto wait_other_side; |
| 4654 | } |
| 4655 | |
Christopher Faulet | 4be9803 | 2017-07-18 10:48:24 +0200 | [diff] [blame] | 4656 | check_channel_flags: |
| 4657 | /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */ |
| 4658 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) { |
| 4659 | /* if we've just closed an output, let's switch */ |
| 4660 | txn->rsp.msg_state = HTTP_MSG_CLOSING; |
| 4661 | goto http_msg_closing; |
| 4662 | } |
| 4663 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4664 | wait_other_side: |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 4665 | /* We force the response to leave immediately if we're waiting for the |
| 4666 | * other side, since there is no pending shutdown to push it out. |
| 4667 | */ |
| 4668 | if (!channel_is_empty(chn)) |
| 4669 | chn->flags |= CF_SEND_DONTWAIT; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4670 | return txn->rsp.msg_state != old_state || chn->flags != old_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4671 | } |
| 4672 | |
| 4673 | |
Christopher Faulet | 894da4c | 2017-07-18 11:29:07 +0200 | [diff] [blame] | 4674 | /* Resync the request and response state machines. */ |
| 4675 | void http_resync_states(struct stream *s) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4676 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4677 | struct http_txn *txn = s->txn; |
Christopher Faulet | 894da4c | 2017-07-18 11:29:07 +0200 | [diff] [blame] | 4678 | #ifdef DEBUG_FULL |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4679 | int old_req_state = txn->req.msg_state; |
| 4680 | int old_res_state = txn->rsp.msg_state; |
Christopher Faulet | 894da4c | 2017-07-18 11:29:07 +0200 | [diff] [blame] | 4681 | #endif |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4682 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4683 | http_sync_req_state(s); |
| 4684 | while (1) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4685 | if (!http_sync_res_state(s)) |
| 4686 | break; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4687 | if (!http_sync_req_state(s)) |
| 4688 | break; |
| 4689 | } |
Willy Tarreau | 3ce10ff | 2014-04-22 08:24:38 +0200 | [diff] [blame] | 4690 | |
Christopher Faulet | 894da4c | 2017-07-18 11:29:07 +0200 | [diff] [blame] | 4691 | DPRINTF(stderr,"[%u] %s: stream=%p old=%s,%s cur=%s,%s " |
| 4692 | "req->analysers=0x%08x res->analysers=0x%08x\n", |
| 4693 | now_ms, __FUNCTION__, s, |
Willy Tarreau | 0da5b3b | 2017-09-21 09:30:46 +0200 | [diff] [blame] | 4694 | h1_msg_state_str(old_req_state), h1_msg_state_str(old_res_state), |
| 4695 | 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] | 4696 | s->req.analysers, s->res.analysers); |
Christopher Faulet | 814d270 | 2017-03-30 11:33:44 +0200 | [diff] [blame] | 4697 | |
| 4698 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4699 | /* OK, both state machines agree on a compatible state. |
| 4700 | * There are a few cases we're interested in : |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4701 | * - HTTP_MSG_CLOSED on both sides means we've reached the end in both |
| 4702 | * directions, so let's simply disable both analysers. |
Christopher Faulet | f77bb53 | 2017-07-18 11:18:46 +0200 | [diff] [blame] | 4703 | * - HTTP_MSG_CLOSED on the response only or HTTP_MSG_ERROR on either |
| 4704 | * means we must abort the request. |
| 4705 | * - HTTP_MSG_TUNNEL on either means we have to disable analyser on |
| 4706 | * corresponding channel. |
| 4707 | * - HTTP_MSG_DONE or HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE |
| 4708 | * on the response with server-close mode means we've completed one |
| 4709 | * request and we must re-initialize the server connection. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4710 | */ |
Christopher Faulet | f77bb53 | 2017-07-18 11:18:46 +0200 | [diff] [blame] | 4711 | if (txn->req.msg_state == HTTP_MSG_CLOSED && |
| 4712 | txn->rsp.msg_state == HTTP_MSG_CLOSED) { |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4713 | s->req.analysers &= AN_REQ_FLT_END; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4714 | channel_auto_close(&s->req); |
| 4715 | channel_auto_read(&s->req); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4716 | s->res.analysers &= AN_RES_FLT_END; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4717 | channel_auto_close(&s->res); |
| 4718 | channel_auto_read(&s->res); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4719 | } |
Christopher Faulet | f77bb53 | 2017-07-18 11:18:46 +0200 | [diff] [blame] | 4720 | else if (txn->rsp.msg_state == HTTP_MSG_CLOSED || |
| 4721 | txn->rsp.msg_state == HTTP_MSG_ERROR || |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 4722 | txn->req.msg_state == HTTP_MSG_ERROR) { |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4723 | s->res.analysers &= AN_RES_FLT_END; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4724 | channel_auto_close(&s->res); |
| 4725 | channel_auto_read(&s->res); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4726 | s->req.analysers &= AN_REQ_FLT_END; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4727 | channel_abort(&s->req); |
| 4728 | channel_auto_close(&s->req); |
| 4729 | channel_auto_read(&s->req); |
| 4730 | channel_truncate(&s->req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4731 | } |
Christopher Faulet | f77bb53 | 2017-07-18 11:18:46 +0200 | [diff] [blame] | 4732 | else if (txn->req.msg_state == HTTP_MSG_TUNNEL || |
| 4733 | txn->rsp.msg_state == HTTP_MSG_TUNNEL) { |
| 4734 | if (txn->req.msg_state == HTTP_MSG_TUNNEL) { |
| 4735 | s->req.analysers &= AN_REQ_FLT_END; |
| 4736 | if (HAS_REQ_DATA_FILTERS(s)) |
| 4737 | s->req.analysers |= AN_REQ_FLT_XFER_DATA; |
| 4738 | } |
| 4739 | if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) { |
| 4740 | s->res.analysers &= AN_RES_FLT_END; |
| 4741 | if (HAS_RSP_DATA_FILTERS(s)) |
| 4742 | s->res.analysers |= AN_RES_FLT_XFER_DATA; |
| 4743 | } |
| 4744 | channel_auto_close(&s->req); |
| 4745 | channel_auto_read(&s->req); |
| 4746 | channel_auto_close(&s->res); |
| 4747 | channel_auto_read(&s->res); |
| 4748 | } |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4749 | else if ((txn->req.msg_state == HTTP_MSG_DONE || |
| 4750 | txn->req.msg_state == HTTP_MSG_CLOSED) && |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4751 | txn->rsp.msg_state == HTTP_MSG_DONE && |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4752 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL || |
| 4753 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) { |
| 4754 | /* server-close/keep-alive: terminate this transaction, |
| 4755 | * possibly killing the server connection and reinitialize |
Willy Tarreau | 3de5bd6 | 2016-05-02 16:07:07 +0200 | [diff] [blame] | 4756 | * a fresh-new transaction, but only once we're sure there's |
| 4757 | * enough room in the request and response buffer to process |
Christopher Faulet | c0c672a | 2017-03-28 11:51:33 +0200 | [diff] [blame] | 4758 | * another request. They must not hold any pending output data |
| 4759 | * and the response buffer must realigned |
| 4760 | * (realign is done is http_end_txn_clean_session). |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4761 | */ |
Christopher Faulet | 894da4c | 2017-07-18 11:29:07 +0200 | [diff] [blame] | 4762 | if (s->req.buf->o) |
Willy Tarreau | 3de5bd6 | 2016-05-02 16:07:07 +0200 | [diff] [blame] | 4763 | s->req.flags |= CF_WAKE_WRITE; |
Christopher Faulet | 894da4c | 2017-07-18 11:29:07 +0200 | [diff] [blame] | 4764 | else if (s->res.buf->o) |
Willy Tarreau | 3de5bd6 | 2016-05-02 16:07:07 +0200 | [diff] [blame] | 4765 | s->res.flags |= CF_WAKE_WRITE; |
Christopher Faulet | a81ff60 | 2017-07-18 22:01:05 +0200 | [diff] [blame] | 4766 | else { |
| 4767 | s->req.analysers = AN_REQ_FLT_END; |
| 4768 | s->res.analysers = AN_RES_FLT_END; |
| 4769 | txn->flags |= TX_WAIT_CLEANUP; |
| 4770 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4771 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4772 | } |
| 4773 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4774 | /* This function is an analyser which forwards request body (including chunk |
| 4775 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 4776 | * zero byte. The only situation where it must not be called is when we're in |
| 4777 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 4778 | * remaining data and to resync after end of body. It expects the msg_state to |
| 4779 | * 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] | 4780 | * 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] | 4781 | * 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] | 4782 | * bytes of pending data + the headers if not already done. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4783 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4784 | 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] | 4785 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4786 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4787 | struct http_txn *txn = s->txn; |
| 4788 | struct http_msg *msg = &s->txn->req; |
Christopher Faulet | 3e34429 | 2015-11-24 16:24:13 +0100 | [diff] [blame] | 4789 | int ret; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4790 | |
Christopher Faulet | 814d270 | 2017-03-30 11:33:44 +0200 | [diff] [blame] | 4791 | DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n", |
| 4792 | now_ms, __FUNCTION__, |
| 4793 | s, |
| 4794 | req, |
| 4795 | req->rex, req->wex, |
| 4796 | req->flags, |
| 4797 | req->buf->i, |
| 4798 | req->analysers); |
| 4799 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4800 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4801 | return 0; |
| 4802 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4803 | 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] | 4804 | ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) { |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 4805 | /* Output closed while we were sending data. We must abort and |
| 4806 | * wake the other side up. |
| 4807 | */ |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 4808 | msg->err_state = msg->msg_state; |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 4809 | msg->msg_state = HTTP_MSG_ERROR; |
| 4810 | http_resync_states(s); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 4811 | return 1; |
| 4812 | } |
| 4813 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4814 | /* Note that we don't have to send 100-continue back because we don't |
| 4815 | * need the data to complete our job, and it's up to the server to |
| 4816 | * decide whether to return 100, 417 or anything else in return of |
| 4817 | * an "Expect: 100-continue" header. |
| 4818 | */ |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 4819 | if (msg->msg_state == HTTP_MSG_BODY) { |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4820 | msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK) |
| 4821 | ? HTTP_MSG_CHUNK_SIZE |
| 4822 | : HTTP_MSG_DATA); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 4823 | |
| 4824 | /* TODO/filters: when http-buffer-request option is set or if a |
| 4825 | * rule on url_param exists, the first chunk size could be |
| 4826 | * already parsed. In that case, msg->next is after the chunk |
| 4827 | * size (including the CRLF after the size). So this case should |
| 4828 | * be handled to */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4829 | } |
| 4830 | |
Willy Tarreau | 7ba2354 | 2014-04-17 21:50:00 +0200 | [diff] [blame] | 4831 | /* Some post-connect processing might want us to refrain from starting to |
| 4832 | * forward data. Currently, the only reason for this is "balance url_param" |
| 4833 | * whichs need to parse/process the request after we've enabled forwarding. |
| 4834 | */ |
| 4835 | if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4836 | if (!(s->res.flags & CF_READ_ATTACHED)) { |
Willy Tarreau | 7ba2354 | 2014-04-17 21:50:00 +0200 | [diff] [blame] | 4837 | channel_auto_connect(req); |
Willy Tarreau | 644c101 | 2014-04-30 18:11:11 +0200 | [diff] [blame] | 4838 | req->flags |= CF_WAKE_CONNECT; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4839 | goto missing_data_or_waiting; |
Willy Tarreau | 7ba2354 | 2014-04-17 21:50:00 +0200 | [diff] [blame] | 4840 | } |
| 4841 | msg->flags &= ~HTTP_MSGF_WAIT_CONN; |
| 4842 | } |
| 4843 | |
Willy Tarreau | 80a92c0 | 2014-03-12 10:41:13 +0100 | [diff] [blame] | 4844 | /* in most states, we should abort in case of early close */ |
| 4845 | channel_auto_close(req); |
| 4846 | |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 4847 | if (req->to_forward) { |
| 4848 | /* We can't process the buffer's contents yet */ |
| 4849 | req->flags |= CF_WAKE_WRITE; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4850 | goto missing_data_or_waiting; |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 4851 | } |
| 4852 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4853 | if (msg->msg_state < HTTP_MSG_DONE) { |
| 4854 | ret = ((msg->flags & HTTP_MSGF_TE_CHNK) |
| 4855 | ? http_msg_forward_chunked_body(s, msg) |
| 4856 | : http_msg_forward_body(s, msg)); |
| 4857 | if (!ret) |
| 4858 | goto missing_data_or_waiting; |
| 4859 | if (ret < 0) |
| 4860 | goto return_bad_req; |
| 4861 | } |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 4862 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4863 | /* other states, DONE...TUNNEL */ |
| 4864 | /* we don't want to forward closes on DONE except in tunnel mode. */ |
| 4865 | if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) |
| 4866 | channel_dont_close(req); |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 4867 | |
Christopher Faulet | 894da4c | 2017-07-18 11:29:07 +0200 | [diff] [blame] | 4868 | http_resync_states(s); |
| 4869 | if (!(req->analysers & an_bit)) { |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4870 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 4871 | if (req->flags & CF_SHUTW) { |
| 4872 | /* request errors are most likely due to the |
| 4873 | * server aborting the transfer. */ |
| 4874 | goto aborted_xfer; |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 4875 | } |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4876 | if (msg->err_pos >= 0) |
Emeric Brun | 8c1aaa2 | 2017-06-15 11:30:06 +0200 | [diff] [blame] | 4877 | 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] | 4878 | goto return_bad_req; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4879 | } |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4880 | return 1; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4881 | } |
| 4882 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4883 | /* If "option abortonclose" is set on the backend, we want to monitor |
| 4884 | * the client's connection and forward any shutdown notification to the |
| 4885 | * server, which will decide whether to close or to go on processing the |
| 4886 | * request. We only do that in tunnel mode, and not in other modes since |
| 4887 | * it can be abused to exhaust source ports. */ |
| 4888 | if (s->be->options & PR_O_ABRT_CLOSE) { |
| 4889 | channel_auto_read(req); |
| 4890 | if ((req->flags & (CF_SHUTR|CF_READ_NULL)) && |
| 4891 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)) |
| 4892 | s->si[1].flags |= SI_FL_NOLINGER; |
| 4893 | channel_auto_close(req); |
| 4894 | } |
| 4895 | else if (s->txn->meth == HTTP_METH_POST) { |
| 4896 | /* POST requests may require to read extra CRLF sent by broken |
| 4897 | * browsers and which could cause an RST to be sent upon close |
| 4898 | * on some systems (eg: Linux). */ |
| 4899 | channel_auto_read(req); |
| 4900 | } |
| 4901 | return 0; |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 4902 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4903 | missing_data_or_waiting: |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4904 | /* stop waiting for data if the input is closed before the end */ |
Christopher Faulet | a33510b | 2017-03-31 15:37:29 +0200 | [diff] [blame] | 4905 | if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR) { |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4906 | if (!(s->flags & SF_ERR_MASK)) |
| 4907 | s->flags |= SF_ERR_CLICL; |
| 4908 | if (!(s->flags & SF_FINST_MASK)) { |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4909 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4910 | s->flags |= SF_FINST_H; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4911 | else |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4912 | s->flags |= SF_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4913 | } |
| 4914 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 4915 | HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1); |
| 4916 | HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4917 | if (objt_server(s->target)) |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 4918 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4919 | |
| 4920 | goto return_bad_req_stats_ok; |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 4921 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4922 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4923 | /* waiting for the last bits to leave the buffer */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4924 | if (req->flags & CF_SHUTW) |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4925 | goto aborted_xfer; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4926 | |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 4927 | /* 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] | 4928 | * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE. |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 4929 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4930 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4931 | channel_dont_close(req); |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 4932 | |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 4933 | /* 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] | 4934 | * 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] | 4935 | * 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] | 4936 | * modes are already handled by the stream sock layer. We must not do |
| 4937 | * this in content-length mode because it could present the MSG_MORE |
| 4938 | * flag with the last block of forwarded data, which would cause an |
| 4939 | * additional delay to be observed by the receiver. |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 4940 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4941 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4942 | req->flags |= CF_EXPECT_MORE; |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 4943 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4944 | return 0; |
| 4945 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4946 | return_bad_req: /* let's centralize all bad requests */ |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 4947 | HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4948 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 4949 | HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1); |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 4950 | |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4951 | return_bad_req_stats_ok: |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 4952 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4953 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4954 | if (txn->status) { |
| 4955 | /* 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] | 4956 | http_reply_and_close(s, txn->status, NULL); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4957 | } else { |
| 4958 | txn->status = 400; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 4959 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4960 | } |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4961 | req->analysers &= AN_REQ_FLT_END; |
| 4962 | 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] | 4963 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4964 | if (!(s->flags & SF_ERR_MASK)) |
| 4965 | s->flags |= SF_ERR_PRXCOND; |
| 4966 | if (!(s->flags & SF_FINST_MASK)) { |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4967 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4968 | s->flags |= SF_FINST_H; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4969 | else |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4970 | s->flags |= SF_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4971 | } |
| 4972 | return 0; |
| 4973 | |
| 4974 | aborted_xfer: |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 4975 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4976 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4977 | if (txn->status) { |
| 4978 | /* 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] | 4979 | http_reply_and_close(s, txn->status, NULL); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4980 | } else { |
| 4981 | txn->status = 502; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 4982 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4983 | } |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4984 | req->analysers &= AN_REQ_FLT_END; |
| 4985 | 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] | 4986 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 4987 | HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1); |
| 4988 | HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4989 | if (objt_server(s->target)) |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 4990 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4991 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4992 | if (!(s->flags & SF_ERR_MASK)) |
| 4993 | s->flags |= SF_ERR_SRVCL; |
| 4994 | if (!(s->flags & SF_FINST_MASK)) { |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4995 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4996 | s->flags |= SF_FINST_H; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4997 | else |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4998 | s->flags |= SF_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4999 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5000 | return 0; |
| 5001 | } |
| 5002 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5003 | /* This stream analyser waits for a complete HTTP response. It returns 1 if the |
| 5004 | * processing can continue on next analysers, or zero if it either needs more |
| 5005 | * data or wants to immediately abort the response (eg: timeout, error, ...). It |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5006 | * 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] | 5007 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 5008 | * abort. |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 5009 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5010 | 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] | 5011 | { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5012 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5013 | struct http_txn *txn = s->txn; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5014 | struct http_msg *msg = &txn->rsp; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5015 | struct hdr_ctx ctx; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5016 | int use_close_only; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5017 | int cur_idx; |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 5018 | int n; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 5019 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5020 | 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] | 5021 | now_ms, __FUNCTION__, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5022 | s, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 5023 | rep, |
| 5024 | rep->rex, rep->wex, |
| 5025 | rep->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5026 | rep->buf->i, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 5027 | rep->analysers); |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 5028 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5029 | /* |
| 5030 | * Now parse the partial (or complete) lines. |
| 5031 | * We will check the response syntax, and also join multi-line |
| 5032 | * headers. An index of all the lines will be elaborated while |
| 5033 | * parsing. |
| 5034 | * |
| 5035 | * For the parsing, we use a 28 states FSM. |
| 5036 | * |
| 5037 | * Here is the information we currently have : |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5038 | * rep->buf->p = beginning of response |
| 5039 | * rep->buf->p + msg->eoh = end of processed headers / start of current one |
| 5040 | * rep->buf->p + rep->buf->i = end of input data |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 5041 | * msg->eol = end of current header or line (LF or CRLF) |
| 5042 | * msg->next = first non-visited byte |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5043 | */ |
| 5044 | |
Willy Tarreau | 628c40c | 2014-04-24 19:11:26 +0200 | [diff] [blame] | 5045 | next_one: |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 5046 | /* There's a protected area at the end of the buffer for rewriting |
| 5047 | * purposes. We don't want to start to parse the request if the |
| 5048 | * protected area is affected, because we may have to move processed |
| 5049 | * data later, which is much more complicated. |
| 5050 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5051 | if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) { |
Willy Tarreau | ba0902e | 2015-01-13 14:39:16 +0100 | [diff] [blame] | 5052 | if (unlikely(!channel_is_rewritable(rep))) { |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 5053 | /* some data has still not left the buffer, wake us once that's done */ |
| 5054 | if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) |
| 5055 | goto abort_response; |
| 5056 | channel_dont_close(rep); |
| 5057 | rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | d7ad9f5 | 2013-12-31 17:26:25 +0100 | [diff] [blame] | 5058 | rep->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 5059 | return 0; |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 5060 | } |
| 5061 | |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 5062 | if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) || |
| 5063 | bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite)) |
| 5064 | buffer_slow_realign(rep->buf); |
| 5065 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5066 | if (likely(msg->next < rep->buf->i)) |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 5067 | http_msg_analyzer(msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 5068 | } |
| 5069 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5070 | /* 1: we might have to print this header in debug mode */ |
| 5071 | if (unlikely((global.mode & MODE_DEBUG) && |
| 5072 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | 7d59e90 | 2014-10-21 19:36:09 +0200 | [diff] [blame] | 5073 | msg->msg_state >= HTTP_MSG_BODY)) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5074 | char *eol, *sol; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5075 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5076 | sol = rep->buf->p; |
| 5077 | 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] | 5078 | debug_hdr("srvrep", s, sol, eol); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5079 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5080 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 5081 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5082 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5083 | while (cur_idx) { |
| 5084 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 5085 | debug_hdr("srvhdr", s, sol, eol); |
| 5086 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 5087 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
| 5088 | } |
| 5089 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5090 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5091 | /* |
| 5092 | * Now we quickly check if we have found a full valid response. |
| 5093 | * If not so, we check the FD and buffer states before leaving. |
| 5094 | * A full response is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 5095 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5096 | * responses are checked first. |
| 5097 | * |
| 5098 | * Depending on whether the client is still there or not, we |
| 5099 | * may send an error response back or not. Note that normally |
| 5100 | * we should only check for HTTP status there, and check I/O |
| 5101 | * errors somewhere else. |
| 5102 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5103 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 5104 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5105 | /* Invalid response */ |
| 5106 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 5107 | /* we detected a parsing error. We want to archive this response |
| 5108 | * in the dedicated proxy area for later troubleshooting. |
| 5109 | */ |
| 5110 | hdr_response_bad: |
| 5111 | if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0) |
Emeric Brun | 8c1aaa2 | 2017-06-15 11:30:06 +0200 | [diff] [blame] | 5112 | 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] | 5113 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 5114 | HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5115 | if (objt_server(s->target)) { |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 5116 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5117 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5118 | } |
Willy Tarreau | 6464841 | 2010-03-05 10:41:54 +0100 | [diff] [blame] | 5119 | abort_response: |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5120 | channel_auto_close(rep); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 5121 | rep->analysers &= AN_RES_FLT_END; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5122 | txn->status = 502; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5123 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5124 | channel_truncate(rep); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 5125 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5126 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5127 | if (!(s->flags & SF_ERR_MASK)) |
| 5128 | s->flags |= SF_ERR_PRXCOND; |
| 5129 | if (!(s->flags & SF_FINST_MASK)) |
| 5130 | s->flags |= SF_FINST_H; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5131 | |
| 5132 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5133 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5134 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5135 | /* too large response does not fit in buffer. */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5136 | else if (buffer_full(rep->buf, global.tune.maxrewrite)) { |
Willy Tarreau | fec4d89 | 2011-09-02 20:04:57 +0200 | [diff] [blame] | 5137 | if (msg->err_pos < 0) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5138 | msg->err_pos = rep->buf->i; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5139 | goto hdr_response_bad; |
| 5140 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5141 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5142 | /* read error */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5143 | else if (rep->flags & CF_READ_ERROR) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5144 | if (msg->err_pos >= 0) |
Emeric Brun | 8c1aaa2 | 2017-06-15 11:30:06 +0200 | [diff] [blame] | 5145 | 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] | 5146 | else if (txn->flags & TX_NOT_FIRST) |
| 5147 | goto abort_keep_alive; |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 5148 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 5149 | HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5150 | if (objt_server(s->target)) { |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 5151 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5152 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5153 | } |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 5154 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5155 | channel_auto_close(rep); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 5156 | rep->analysers &= AN_RES_FLT_END; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5157 | txn->status = 502; |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 5158 | |
| 5159 | /* Check to see if the server refused the early data. |
| 5160 | * If so, just send a 425 |
| 5161 | */ |
| 5162 | if (objt_cs(s->si[1].end)) { |
| 5163 | struct connection *conn = objt_cs(s->si[1].end)->conn; |
| 5164 | |
| 5165 | if (conn->err_code == CO_ER_SSL_EARLY_FAILED) |
| 5166 | txn->status = 425; |
| 5167 | } |
| 5168 | |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5169 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5170 | channel_truncate(rep); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 5171 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 5172 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5173 | if (!(s->flags & SF_ERR_MASK)) |
| 5174 | s->flags |= SF_ERR_SRVCL; |
| 5175 | if (!(s->flags & SF_FINST_MASK)) |
| 5176 | s->flags |= SF_FINST_H; |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 5177 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5178 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5179 | |
Willy Tarreau | 6f0a7ba | 2014-06-23 15:22:31 +0200 | [diff] [blame] | 5180 | /* read timeout : return a 504 to the client. */ |
| 5181 | else if (rep->flags & CF_READ_TIMEOUT) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5182 | if (msg->err_pos >= 0) |
Emeric Brun | 8c1aaa2 | 2017-06-15 11:30:06 +0200 | [diff] [blame] | 5183 | 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] | 5184 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 5185 | HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5186 | if (objt_server(s->target)) { |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 5187 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5188 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5189 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5190 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5191 | channel_auto_close(rep); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 5192 | rep->analysers &= AN_RES_FLT_END; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5193 | txn->status = 504; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5194 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5195 | channel_truncate(rep); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 5196 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 5197 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5198 | if (!(s->flags & SF_ERR_MASK)) |
| 5199 | s->flags |= SF_ERR_SRVTO; |
| 5200 | if (!(s->flags & SF_FINST_MASK)) |
| 5201 | s->flags |= SF_FINST_H; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5202 | return 0; |
| 5203 | } |
Willy Tarreau | a7c5276 | 2008-08-16 18:40:18 +0200 | [diff] [blame] | 5204 | |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 5205 | /* client abort with an abortonclose */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5206 | 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] | 5207 | HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1); |
| 5208 | HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1); |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 5209 | if (objt_server(s->target)) |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 5210 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1); |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 5211 | |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 5212 | rep->analysers &= AN_RES_FLT_END; |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 5213 | channel_auto_close(rep); |
| 5214 | |
| 5215 | txn->status = 400; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5216 | channel_truncate(rep); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 5217 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 5218 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5219 | if (!(s->flags & SF_ERR_MASK)) |
| 5220 | s->flags |= SF_ERR_CLICL; |
| 5221 | if (!(s->flags & SF_FINST_MASK)) |
| 5222 | s->flags |= SF_FINST_H; |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 5223 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5224 | /* process_stream() will take care of the error */ |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 5225 | return 0; |
| 5226 | } |
| 5227 | |
Willy Tarreau | 3b8c08a | 2011-09-02 20:16:24 +0200 | [diff] [blame] | 5228 | /* 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] | 5229 | else if (rep->flags & CF_SHUTR) { |
Willy Tarreau | 3b8c08a | 2011-09-02 20:16:24 +0200 | [diff] [blame] | 5230 | if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0) |
Emeric Brun | 8c1aaa2 | 2017-06-15 11:30:06 +0200 | [diff] [blame] | 5231 | 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] | 5232 | else if (txn->flags & TX_NOT_FIRST) |
| 5233 | goto abort_keep_alive; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5234 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 5235 | HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5236 | if (objt_server(s->target)) { |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 5237 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5238 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5239 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5240 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5241 | channel_auto_close(rep); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 5242 | rep->analysers &= AN_RES_FLT_END; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5243 | txn->status = 502; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5244 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5245 | channel_truncate(rep); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 5246 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5247 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5248 | if (!(s->flags & SF_ERR_MASK)) |
| 5249 | s->flags |= SF_ERR_SRVCL; |
| 5250 | if (!(s->flags & SF_FINST_MASK)) |
| 5251 | s->flags |= SF_FINST_H; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5252 | return 0; |
| 5253 | } |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 5254 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5255 | /* write error to client (we don't send any message then) */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5256 | else if (rep->flags & CF_WRITE_ERROR) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5257 | if (msg->err_pos >= 0) |
Emeric Brun | 8c1aaa2 | 2017-06-15 11:30:06 +0200 | [diff] [blame] | 5258 | 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] | 5259 | else if (txn->flags & TX_NOT_FIRST) |
| 5260 | goto abort_keep_alive; |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 5261 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 5262 | HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 5263 | rep->analysers &= AN_RES_FLT_END; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5264 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5265 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5266 | if (!(s->flags & SF_ERR_MASK)) |
| 5267 | s->flags |= SF_ERR_CLICL; |
| 5268 | if (!(s->flags & SF_FINST_MASK)) |
| 5269 | s->flags |= SF_FINST_H; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5270 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5271 | /* process_stream() will take care of the error */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5272 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5273 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5274 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5275 | channel_dont_close(rep); |
Willy Tarreau | 3f3997e | 2013-12-15 15:21:32 +0100 | [diff] [blame] | 5276 | rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5277 | return 0; |
| 5278 | } |
| 5279 | |
| 5280 | /* More interesting part now : we know that we have a complete |
| 5281 | * response which at least looks like HTTP. We have an indicator |
| 5282 | * of each header's length, so we can parse them quickly. |
| 5283 | */ |
| 5284 | |
| 5285 | if (unlikely(msg->err_pos >= 0)) |
Emeric Brun | 8c1aaa2 | 2017-06-15 11:30:06 +0200 | [diff] [blame] | 5286 | 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] | 5287 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5288 | /* |
| 5289 | * 1: get the status code |
| 5290 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5291 | n = rep->buf->p[msg->sl.st.c] - '0'; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5292 | if (n < 1 || n > 5) |
| 5293 | n = 0; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5294 | /* when the client triggers a 4xx from the server, it's most often due |
| 5295 | * to a missing object or permission. These events should be tracked |
| 5296 | * because if they happen often, it may indicate a brute force or a |
| 5297 | * vulnerability scan. |
| 5298 | */ |
| 5299 | if (n == 4) |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5300 | stream_inc_http_err_ctr(s); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5301 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5302 | if (objt_server(s->target)) |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 5303 | 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] | 5304 | |
Willy Tarreau | 91852eb | 2015-05-01 13:26:00 +0200 | [diff] [blame] | 5305 | /* RFC7230#2.6 has enforced the format of the HTTP version string to be |
| 5306 | * exactly one digit "." one digit. This check may be disabled using |
| 5307 | * option accept-invalid-http-response. |
| 5308 | */ |
| 5309 | if (!(s->be->options2 & PR_O2_RSPBUG_OK)) { |
| 5310 | if (msg->sl.st.v_l != 8) { |
| 5311 | msg->err_pos = 0; |
| 5312 | goto hdr_response_bad; |
| 5313 | } |
| 5314 | |
| 5315 | if (rep->buf->p[4] != '/' || |
| 5316 | !isdigit((unsigned char)rep->buf->p[5]) || |
| 5317 | rep->buf->p[6] != '.' || |
| 5318 | !isdigit((unsigned char)rep->buf->p[7])) { |
| 5319 | msg->err_pos = 4; |
| 5320 | goto hdr_response_bad; |
| 5321 | } |
| 5322 | } |
| 5323 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5324 | /* check if the response is HTTP/1.1 or above */ |
| 5325 | if ((msg->sl.st.v_l == 8) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5326 | ((rep->buf->p[5] > '1') || |
| 5327 | ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1')))) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5328 | msg->flags |= HTTP_MSGF_VER_11; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5329 | |
| 5330 | /* "connection" has not been parsed yet */ |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 5331 | 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] | 5332 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5333 | /* transfer length unknown*/ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5334 | msg->flags &= ~HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5335 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5336 | 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] | 5337 | |
Willy Tarreau | 3965040 | 2010-03-15 19:44:39 +0100 | [diff] [blame] | 5338 | /* Adjust server's health based on status code. Note: status codes 501 |
| 5339 | * and 505 are triggered on demand by client request, so we must not |
| 5340 | * count them as server failures. |
| 5341 | */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5342 | if (objt_server(s->target)) { |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 5343 | 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] | 5344 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK); |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 5345 | else |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5346 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS); |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 5347 | } |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5348 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5349 | /* |
Willy Tarreau | a14ad72 | 2017-07-07 11:36:32 +0200 | [diff] [blame] | 5350 | * We may be facing a 100-continue response, or any other informational |
| 5351 | * 1xx response which is non-final, in which case this is not the right |
| 5352 | * response, and we're waiting for the next one. Let's allow this response |
| 5353 | * to go to the client and wait for the next one. There's an exception for |
| 5354 | * 101 which is used later in the code to switch protocols. |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5355 | */ |
Willy Tarreau | a14ad72 | 2017-07-07 11:36:32 +0200 | [diff] [blame] | 5356 | if (txn->status < 200 && |
| 5357 | (txn->status == 100 || txn->status >= 102)) { |
Willy Tarreau | 628c40c | 2014-04-24 19:11:26 +0200 | [diff] [blame] | 5358 | hdr_idx_init(&txn->hdr_idx); |
| 5359 | msg->next -= channel_forward(rep, msg->next); |
| 5360 | msg->msg_state = HTTP_MSG_RPBEFORE; |
| 5361 | txn->status = 0; |
| 5362 | s->logs.t_data = -1; /* was not a response yet */ |
Christopher Faulet | 3e34429 | 2015-11-24 16:24:13 +0100 | [diff] [blame] | 5363 | FLT_STRM_CB(s, flt_http_reset(s, msg)); |
Willy Tarreau | 628c40c | 2014-04-24 19:11:26 +0200 | [diff] [blame] | 5364 | goto next_one; |
Willy Tarreau | a14ad72 | 2017-07-07 11:36:32 +0200 | [diff] [blame] | 5365 | } |
Willy Tarreau | 628c40c | 2014-04-24 19:11:26 +0200 | [diff] [blame] | 5366 | |
Willy Tarreau | a14ad72 | 2017-07-07 11:36:32 +0200 | [diff] [blame] | 5367 | /* |
| 5368 | * 2: check for cacheability. |
| 5369 | */ |
| 5370 | |
| 5371 | switch (txn->status) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5372 | case 200: |
| 5373 | case 203: |
| 5374 | case 206: |
| 5375 | case 300: |
| 5376 | case 301: |
| 5377 | case 410: |
| 5378 | /* RFC2616 @13.4: |
| 5379 | * "A response received with a status code of |
| 5380 | * 200, 203, 206, 300, 301 or 410 MAY be stored |
| 5381 | * by a cache (...) unless a cache-control |
| 5382 | * directive prohibits caching." |
| 5383 | * |
| 5384 | * RFC2616 @9.5: POST method : |
| 5385 | * "Responses to this method are not cacheable, |
| 5386 | * unless the response includes appropriate |
| 5387 | * Cache-Control or Expires header fields." |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5388 | */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5389 | if (likely(txn->meth != HTTP_METH_POST) && |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 5390 | ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))) |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5391 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
| 5392 | break; |
| 5393 | default: |
| 5394 | break; |
| 5395 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5396 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5397 | /* |
| 5398 | * 3: we may need to capture headers |
| 5399 | */ |
| 5400 | s->logs.logwait &= ~LW_RESP; |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 5401 | if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap)) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5402 | capture_headers(rep->buf->p, &txn->hdr_idx, |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 5403 | s->res_cap, sess->fe->rsp_cap); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5404 | |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 5405 | /* 4: determine the transfer-length according to RFC2616 #4.4, updated |
| 5406 | * by RFC7230#3.3.3 : |
| 5407 | * |
| 5408 | * The length of a message body is determined by one of the following |
| 5409 | * (in order of precedence): |
| 5410 | * |
Christopher Faulet | d1cd209 | 2016-11-28 10:14:03 +0100 | [diff] [blame] | 5411 | * 1. Any 2xx (Successful) response to a CONNECT request implies that |
| 5412 | * the connection will become a tunnel immediately after the empty |
| 5413 | * line that concludes the header fields. A client MUST ignore |
| 5414 | * any Content-Length or Transfer-Encoding header fields received |
| 5415 | * in such a message. Any 101 response (Switching Protocols) is |
| 5416 | * managed in the same manner. |
| 5417 | * |
| 5418 | * 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] | 5419 | * (Informational), 204 (No Content), or 304 (Not Modified) status |
| 5420 | * code is always terminated by the first empty line after the |
| 5421 | * header fields, regardless of the header fields present in the |
| 5422 | * message, and thus cannot contain a message body. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5423 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 5424 | * 3. If a Transfer-Encoding header field is present and the chunked |
| 5425 | * transfer coding (Section 4.1) is the final encoding, the message |
| 5426 | * body length is determined by reading and decoding the chunked |
| 5427 | * data until the transfer coding indicates the data is complete. |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5428 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 5429 | * If a Transfer-Encoding header field is present in a response and |
| 5430 | * the chunked transfer coding is not the final encoding, the |
| 5431 | * message body length is determined by reading the connection until |
| 5432 | * it is closed by the server. If a Transfer-Encoding header field |
| 5433 | * is present in a request and the chunked transfer coding is not |
| 5434 | * the final encoding, the message body length cannot be determined |
| 5435 | * reliably; the server MUST respond with the 400 (Bad Request) |
| 5436 | * status code and then close the connection. |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5437 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 5438 | * If a message is received with both a Transfer-Encoding and a |
| 5439 | * Content-Length header field, the Transfer-Encoding overrides the |
| 5440 | * Content-Length. Such a message might indicate an attempt to |
| 5441 | * perform request smuggling (Section 9.5) or response splitting |
| 5442 | * (Section 9.4) and ought to be handled as an error. A sender MUST |
| 5443 | * remove the received Content-Length field prior to forwarding such |
| 5444 | * a message downstream. |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5445 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 5446 | * 4. If a message is received without Transfer-Encoding and with |
| 5447 | * either multiple Content-Length header fields having differing |
| 5448 | * field-values or a single Content-Length header field having an |
| 5449 | * invalid value, then the message framing is invalid and the |
| 5450 | * recipient MUST treat it as an unrecoverable error. If this is a |
| 5451 | * request message, the server MUST respond with a 400 (Bad Request) |
| 5452 | * status code and then close the connection. If this is a response |
| 5453 | * message received by a proxy, the proxy MUST close the connection |
| 5454 | * to the server, discard the received response, and send a 502 (Bad |
| 5455 | * Gateway) response to the client. If this is a response message |
| 5456 | * received by a user agent, the user agent MUST close the |
| 5457 | * connection to the server and discard the received response. |
| 5458 | * |
| 5459 | * 5. If a valid Content-Length header field is present without |
| 5460 | * Transfer-Encoding, its decimal value defines the expected message |
| 5461 | * body length in octets. If the sender closes the connection or |
| 5462 | * the recipient times out before the indicated number of octets are |
| 5463 | * received, the recipient MUST consider the message to be |
| 5464 | * incomplete and close the connection. |
| 5465 | * |
| 5466 | * 6. If this is a request message and none of the above are true, then |
| 5467 | * the message body length is zero (no message body is present). |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5468 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 5469 | * 7. Otherwise, this is a response message without a declared message |
| 5470 | * body length, so the message body length is determined by the |
| 5471 | * number of octets received prior to the server closing the |
| 5472 | * connection. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5473 | */ |
| 5474 | |
| 5475 | /* Skip parsing if no content length is possible. The response flags |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 5476 | * 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] | 5477 | * 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] | 5478 | * FIXME: should we parse anyway and return an error on chunked encoding ? |
| 5479 | */ |
Christopher Faulet | d1cd209 | 2016-11-28 10:14:03 +0100 | [diff] [blame] | 5480 | if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || |
| 5481 | txn->status == 101)) { |
| 5482 | /* Either we've established an explicit tunnel, or we're |
| 5483 | * switching the protocol. In both cases, we're very unlikely |
| 5484 | * to understand the next protocols. We have to switch to tunnel |
| 5485 | * mode, so that we transfer the request and responses then let |
| 5486 | * this protocol pass unmodified. When we later implement specific |
| 5487 | * parsers for such protocols, we'll want to check the Upgrade |
| 5488 | * header which contains information about that protocol for |
| 5489 | * responses with status 101 (eg: see RFC2817 about TLS). |
| 5490 | */ |
| 5491 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN; |
| 5492 | msg->flags |= HTTP_MSGF_XFER_LEN; |
| 5493 | goto end; |
| 5494 | } |
| 5495 | |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5496 | if (txn->meth == HTTP_METH_HEAD || |
| 5497 | (txn->status >= 100 && txn->status < 200) || |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5498 | txn->status == 204 || txn->status == 304) { |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5499 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5500 | goto skip_content_length; |
| 5501 | } |
| 5502 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5503 | use_close_only = 0; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5504 | ctx.idx = 0; |
Willy Tarreau | 4979d5c | 2015-05-01 10:06:30 +0200 | [diff] [blame] | 5505 | 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] | 5506 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5507 | msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
| 5508 | else if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5509 | /* bad transfer-encoding (chunked followed by something else) */ |
| 5510 | use_close_only = 1; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5511 | msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5512 | break; |
| 5513 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5514 | } |
| 5515 | |
Willy Tarreau | 1c91391 | 2015-04-30 10:57:51 +0200 | [diff] [blame] | 5516 | /* Chunked responses must have their content-length removed */ |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5517 | ctx.idx = 0; |
Willy Tarreau | b4d0c03 | 2015-05-01 10:25:45 +0200 | [diff] [blame] | 5518 | if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) { |
Willy Tarreau | 1c91391 | 2015-04-30 10:57:51 +0200 | [diff] [blame] | 5519 | while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) |
| 5520 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 5521 | } |
Willy Tarreau | b4d0c03 | 2015-05-01 10:25:45 +0200 | [diff] [blame] | 5522 | 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] | 5523 | signed long long cl; |
| 5524 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5525 | if (!ctx.vlen) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5526 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5527 | goto hdr_response_bad; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5528 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5529 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5530 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5531 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5532 | goto hdr_response_bad; /* parse failure */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5533 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5534 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5535 | if (cl < 0) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5536 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5537 | goto hdr_response_bad; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5538 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5539 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5540 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5541 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5542 | goto hdr_response_bad; /* already specified, was different */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5543 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5544 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5545 | msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 5546 | msg->body_len = msg->chunk_len = cl; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5547 | } |
| 5548 | |
Christopher Faulet | d1cd209 | 2016-11-28 10:14:03 +0100 | [diff] [blame] | 5549 | skip_content_length: |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5550 | /* Now we have to check if we need to modify the Connection header. |
| 5551 | * This is more difficult on the response than it is on the request, |
| 5552 | * because we can have two different HTTP versions and we don't know |
| 5553 | * how the client will interprete a response. For instance, let's say |
| 5554 | * that the client sends a keep-alive request in HTTP/1.0 and gets an |
| 5555 | * HTTP/1.1 response without any header. Maybe it will bound itself to |
| 5556 | * HTTP/1.0 because it only knows about it, and will consider the lack |
| 5557 | * of header as a close, or maybe it knows HTTP/1.1 and can consider |
| 5558 | * the lack of header as a keep-alive. Thus we will use two flags |
| 5559 | * indicating how a request MAY be understood by the client. In case |
| 5560 | * of multiple possibilities, we'll fix the header to be explicit. If |
| 5561 | * ambiguous cases such as both close and keepalive are seen, then we |
| 5562 | * will fall back to explicit close. Note that we won't take risks with |
| 5563 | * HTTP/1.0 clients which may not necessarily understand keep-alive. |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5564 | * See doc/internals/connection-header.txt for the complete matrix. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5565 | */ |
Christopher Faulet | d1cd209 | 2016-11-28 10:14:03 +0100 | [diff] [blame] | 5566 | if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) && |
| 5567 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN || |
| 5568 | ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
| 5569 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) { |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5570 | int to_del = 0; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5571 | |
Willy Tarreau | 70dffda | 2014-01-30 03:07:23 +0100 | [diff] [blame] | 5572 | /* this situation happens when combining pretend-keepalive with httpclose. */ |
| 5573 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5574 | ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 5575 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) |
Willy Tarreau | 70dffda | 2014-01-30 03:07:23 +0100 | [diff] [blame] | 5576 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 5577 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5578 | /* on unknown transfer length, we must close */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5579 | if (!(msg->flags & HTTP_MSGF_XFER_LEN) && |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5580 | (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) |
| 5581 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5582 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5583 | /* now adjust header transformations depending on current state */ |
| 5584 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN || |
| 5585 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 5586 | to_del |= 2; /* remove "keep-alive" on any response */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5587 | if (!(msg->flags & HTTP_MSGF_VER_11)) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5588 | to_del |= 1; /* remove "close" for HTTP/1.0 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5589 | } |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5590 | else { /* SCL / KAL */ |
| 5591 | to_del |= 1; /* remove "close" on any response */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5592 | if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5593 | to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5594 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5595 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5596 | /* Parse and remove some headers from the connection header */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 5597 | http_parse_connection_header(txn, msg, to_del); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5598 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5599 | /* Some keep-alive responses are converted to Server-close if |
| 5600 | * the server wants to close. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5601 | */ |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5602 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) { |
| 5603 | if ((txn->flags & TX_HDR_CONN_CLO) || |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5604 | (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11))) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5605 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 5606 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5607 | } |
| 5608 | |
Christopher Faulet | d1cd209 | 2016-11-28 10:14:03 +0100 | [diff] [blame] | 5609 | end: |
Willy Tarreau | 7959a55 | 2013-09-23 16:44:27 +0200 | [diff] [blame] | 5610 | /* we want to have the response time before we start processing it */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 5611 | s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now); |
Willy Tarreau | 7959a55 | 2013-09-23 16:44:27 +0200 | [diff] [blame] | 5612 | |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 5613 | /* end of job, return OK */ |
| 5614 | rep->analysers &= ~an_bit; |
| 5615 | rep->analyse_exp = TICK_ETERNITY; |
| 5616 | channel_auto_close(rep); |
| 5617 | return 1; |
| 5618 | |
| 5619 | abort_keep_alive: |
| 5620 | /* A keep-alive request to the server failed on a network error. |
| 5621 | * The client is required to retry. We need to close without returning |
| 5622 | * any other information so that the client retries. |
| 5623 | */ |
| 5624 | txn->status = 0; |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 5625 | rep->analysers &= AN_RES_FLT_END; |
| 5626 | s->req.analysers &= AN_REQ_FLT_END; |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 5627 | channel_auto_close(rep); |
| 5628 | s->logs.logwait = 0; |
| 5629 | s->logs.level = 0; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5630 | s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5631 | channel_truncate(rep); |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 5632 | http_reply_and_close(s, txn->status, NULL); |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 5633 | return 0; |
| 5634 | } |
| 5635 | |
| 5636 | /* This function performs all the processing enabled for the current response. |
| 5637 | * 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] | 5638 | * 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] | 5639 | * other functions. It works like process_request (see indications above). |
| 5640 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5641 | 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] | 5642 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 5643 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5644 | struct http_txn *txn = s->txn; |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 5645 | struct http_msg *msg = &txn->rsp; |
| 5646 | struct proxy *cur_proxy; |
| 5647 | struct cond_wordlist *wl; |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 5648 | enum rule_result ret = HTTP_RULE_RES_CONT; |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 5649 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5650 | 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] | 5651 | now_ms, __FUNCTION__, |
| 5652 | s, |
| 5653 | rep, |
| 5654 | rep->rex, rep->wex, |
| 5655 | rep->flags, |
| 5656 | rep->buf->i, |
| 5657 | rep->analysers); |
| 5658 | |
| 5659 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */ |
| 5660 | return 0; |
| 5661 | |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 5662 | /* The stats applet needs to adjust the Connection header but we don't |
| 5663 | * apply any filter there. |
| 5664 | */ |
Willy Tarreau | 612adb8 | 2015-03-10 15:25:54 +0100 | [diff] [blame] | 5665 | if (unlikely(objt_applet(s->target) == &http_stats_applet)) { |
| 5666 | rep->analysers &= ~an_bit; |
| 5667 | rep->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 5668 | goto skip_filters; |
Willy Tarreau | 612adb8 | 2015-03-10 15:25:54 +0100 | [diff] [blame] | 5669 | } |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 5670 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5671 | /* |
| 5672 | * We will have to evaluate the filters. |
| 5673 | * As opposed to version 1.2, now they will be evaluated in the |
| 5674 | * filters order and not in the header order. This means that |
| 5675 | * each filter has to be validated among all headers. |
| 5676 | * |
| 5677 | * Filters are tried with ->be first, then with ->fe if it is |
| 5678 | * different from ->be. |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 5679 | * |
| 5680 | * Maybe we are in resume condiion. In this case I choose the |
| 5681 | * "struct proxy" which contains the rule list matching the resume |
| 5682 | * pointer. If none of theses "struct proxy" match, I initialise |
| 5683 | * the process with the first one. |
| 5684 | * |
| 5685 | * In fact, I check only correspondance betwwen the current list |
| 5686 | * pointer and the ->fe rule list. If it doesn't match, I initialize |
| 5687 | * the loop with the ->be. |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5688 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5689 | if (s->current_rule_list == &sess->fe->http_res_rules) |
| 5690 | cur_proxy = sess->fe; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 5691 | else |
| 5692 | cur_proxy = s->be; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5693 | while (1) { |
| 5694 | struct proxy *rule_set = cur_proxy; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5695 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5696 | /* evaluate http-response rules */ |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 5697 | if (ret == HTTP_RULE_RES_CONT) { |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 5698 | 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] | 5699 | |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 5700 | if (ret == HTTP_RULE_RES_BADREQ) |
| 5701 | goto return_srv_prx_502; |
| 5702 | |
| 5703 | if (ret == HTTP_RULE_RES_DONE) { |
| 5704 | rep->analysers &= ~an_bit; |
| 5705 | rep->analyse_exp = TICK_ETERNITY; |
| 5706 | return 1; |
| 5707 | } |
| 5708 | } |
| 5709 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 5710 | /* we need to be called again. */ |
| 5711 | if (ret == HTTP_RULE_RES_YIELD) { |
| 5712 | channel_dont_close(rep); |
| 5713 | return 0; |
| 5714 | } |
| 5715 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5716 | /* try headers filters */ |
| 5717 | if (rule_set->rsp_exp != NULL) { |
| 5718 | if (apply_filters_to_response(s, rep, rule_set) < 0) { |
| 5719 | return_bad_resp: |
| 5720 | if (objt_server(s->target)) { |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 5721 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1); |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5722 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5723 | } |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 5724 | HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1); |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5725 | return_srv_prx_502: |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 5726 | rep->analysers &= AN_RES_FLT_END; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5727 | txn->status = 502; |
| 5728 | s->logs.t_data = -1; /* was not a valid response */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5729 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5730 | channel_truncate(rep); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 5731 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5732 | if (!(s->flags & SF_ERR_MASK)) |
| 5733 | s->flags |= SF_ERR_PRXCOND; |
| 5734 | if (!(s->flags & SF_FINST_MASK)) |
| 5735 | s->flags |= SF_FINST_H; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5736 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5737 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5738 | } |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5739 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5740 | /* has the response been denied ? */ |
| 5741 | if (txn->flags & TX_SVDENY) { |
| 5742 | if (objt_server(s->target)) |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 5743 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5744 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 5745 | HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1); |
| 5746 | HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 5747 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 5748 | HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5749 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5750 | goto return_srv_prx_502; |
| 5751 | } |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 5752 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5753 | /* add response headers from the rule sets in the same order */ |
| 5754 | list_for_each_entry(wl, &rule_set->rsp_add, list) { |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 5755 | if (txn->status < 200 && txn->status != 101) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5756 | break; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5757 | if (wl->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 5758 | 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] | 5759 | ret = acl_pass(ret); |
| 5760 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 5761 | ret = !ret; |
| 5762 | if (!ret) |
| 5763 | continue; |
| 5764 | } |
| 5765 | if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0)) |
| 5766 | goto return_bad_resp; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5767 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5768 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5769 | /* check whether we're already working on the frontend */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5770 | if (cur_proxy == sess->fe) |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5771 | break; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5772 | cur_proxy = sess->fe; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5773 | } |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5774 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 5775 | /* After this point, this anayzer can't return yield, so we can |
| 5776 | * remove the bit corresponding to this analyzer from the list. |
| 5777 | * |
| 5778 | * Note that the intermediate returns and goto found previously |
| 5779 | * reset the analyzers. |
| 5780 | */ |
| 5781 | rep->analysers &= ~an_bit; |
| 5782 | rep->analyse_exp = TICK_ETERNITY; |
| 5783 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5784 | /* OK that's all we can do for 1xx responses */ |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 5785 | if (unlikely(txn->status < 200 && txn->status != 101)) |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5786 | goto skip_header_mangling; |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5787 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5788 | /* |
| 5789 | * Now check for a server cookie. |
| 5790 | */ |
Willy Tarreau | 53a09d5 | 2015-08-10 18:59:40 +0200 | [diff] [blame] | 5791 | 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] | 5792 | manage_server_side_cookies(s, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5793 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5794 | /* |
| 5795 | * Check for cache-control or pragma headers if required. |
| 5796 | */ |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 5797 | if (((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)) && txn->status != 101) |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5798 | check_response_for_cacheability(s, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5799 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5800 | /* |
| 5801 | * Add server cookie in the response if needed |
| 5802 | */ |
| 5803 | if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) && |
| 5804 | !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) && |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5805 | (!(s->flags & SF_DIRECT) || |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5806 | ((s->be->cookie_maxidle || txn->cookie_last_date) && |
| 5807 | (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) || |
| 5808 | (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date |
| 5809 | (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date |
| 5810 | (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) && |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5811 | !(s->flags & SF_IGNORE_PRST)) { |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5812 | /* the server is known, it's not the one the client requested, or the |
| 5813 | * cookie's last seen date needs to be refreshed. We have to |
| 5814 | * insert a set-cookie here, except if we want to insert only on POST |
| 5815 | * requests and this one isn't. Note that servers which don't have cookies |
| 5816 | * (eg: some backup servers) will return a full cookie removal request. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5817 | */ |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5818 | if (!objt_server(s->target)->cookie) { |
| 5819 | chunk_printf(&trash, |
| 5820 | "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/", |
| 5821 | s->be->cookie_name); |
| 5822 | } |
| 5823 | else { |
| 5824 | 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] | 5825 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5826 | if (s->be->cookie_maxidle || s->be->cookie_maxlife) { |
| 5827 | /* emit last_date, which is mandatory */ |
| 5828 | trash.str[trash.len++] = COOKIE_DELIM_DATE; |
| 5829 | s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len); |
| 5830 | trash.len += 5; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5831 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5832 | if (s->be->cookie_maxlife) { |
| 5833 | /* emit first_date, which is either the original one or |
| 5834 | * the current date. |
| 5835 | */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 5836 | trash.str[trash.len++] = COOKIE_DELIM_DATE; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5837 | s30tob64(txn->cookie_first_date ? |
| 5838 | txn->cookie_first_date >> 2 : |
| 5839 | (date.tv_sec+3) >> 2, trash.str + trash.len); |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 5840 | trash.len += 5; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5841 | } |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5842 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5843 | chunk_appendf(&trash, "; path=/"); |
| 5844 | } |
Willy Tarreau | 4992dd2 | 2012-05-31 21:02:17 +0200 | [diff] [blame] | 5845 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5846 | if (s->be->cookie_domain) |
| 5847 | chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain); |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5848 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5849 | if (s->be->ck_opts & PR_CK_HTTPONLY) |
| 5850 | chunk_appendf(&trash, "; HttpOnly"); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5851 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5852 | if (s->be->ck_opts & PR_CK_SECURE) |
| 5853 | chunk_appendf(&trash, "; Secure"); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5854 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5855 | if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0)) |
| 5856 | goto return_bad_resp; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5857 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5858 | txn->flags &= ~TX_SCK_MASK; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5859 | if (objt_server(s->target)->cookie && (s->flags & SF_DIRECT)) |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5860 | /* the server did not change, only the date was updated */ |
| 5861 | txn->flags |= TX_SCK_UPDATED; |
| 5862 | else |
| 5863 | txn->flags |= TX_SCK_INSERTED; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5864 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5865 | /* Here, we will tell an eventual cache on the client side that we don't |
| 5866 | * want it to cache this reply because HTTP/1.0 caches also cache cookies ! |
| 5867 | * Some caches understand the correct form: 'no-cache="set-cookie"', but |
| 5868 | * 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] | 5869 | */ |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5870 | if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5871 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5872 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5873 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5874 | if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, |
| 5875 | "Cache-control: private", 22) < 0)) |
| 5876 | goto return_bad_resp; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5877 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5878 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5879 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5880 | /* |
| 5881 | * Check if result will be cacheable with a cookie. |
| 5882 | * We'll block the response if security checks have caught |
| 5883 | * nasty things such as a cacheable cookie. |
| 5884 | */ |
| 5885 | if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) == |
| 5886 | (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) && |
| 5887 | (s->be->options & PR_O_CHK_CACHE)) { |
| 5888 | /* we're in presence of a cacheable response containing |
| 5889 | * a set-cookie header. We'll block it as requested by |
| 5890 | * the 'checkcache' option, and send an alert. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5891 | */ |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5892 | if (objt_server(s->target)) |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 5893 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1); |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5894 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 5895 | HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1); |
| 5896 | HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 5897 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 5898 | HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5899 | |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5900 | ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 5901 | 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] | 5902 | send_log(s->be, LOG_ALERT, |
| 5903 | "Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 5904 | s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>"); |
| 5905 | goto return_srv_prx_502; |
| 5906 | } |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 5907 | |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 5908 | skip_filters: |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5909 | /* |
| 5910 | * Adjust "Connection: close" or "Connection: keep-alive" if needed. |
| 5911 | * If an "Upgrade" token is found, the header is left untouched in order |
| 5912 | * 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] | 5913 | * if anything but "Upgrade" is present in the Connection header. We don't |
| 5914 | * want to touch any 101 response either since it's switching to another |
| 5915 | * protocol. |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5916 | */ |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 5917 | if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) && |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5918 | (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5919 | ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5920 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) { |
| 5921 | unsigned int want_flags = 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5922 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5923 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 5924 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 5925 | /* we want a keep-alive response here. Keep-alive header |
| 5926 | * required if either side is not 1.1. |
| 5927 | */ |
| 5928 | if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11)) |
| 5929 | want_flags |= TX_CON_KAL_SET; |
| 5930 | } |
| 5931 | else { |
| 5932 | /* we want a close response here. Close header required if |
| 5933 | * the server is 1.1, regardless of the client. |
| 5934 | */ |
| 5935 | if (msg->flags & HTTP_MSGF_VER_11) |
| 5936 | want_flags |= TX_CON_CLO_SET; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5937 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5938 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5939 | if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 5940 | http_change_connection_header(txn, msg, want_flags); |
| 5941 | } |
| 5942 | |
| 5943 | skip_header_mangling: |
Christopher Faulet | 69744d9 | 2017-03-30 10:54:35 +0200 | [diff] [blame] | 5944 | /* Always enter in the body analyzer */ |
| 5945 | rep->analysers &= ~AN_RES_FLT_XFER_DATA; |
| 5946 | rep->analysers |= AN_RES_HTTP_XFER_BODY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5947 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5948 | /* if the user wants to log as soon as possible, without counting |
| 5949 | * bytes from the server, then this is the right moment. We have |
| 5950 | * to temporarily assign bytes_out to log what we currently have. |
| 5951 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5952 | if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) { |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5953 | s->logs.t_close = s->logs.t_data; /* to get a valid end date */ |
| 5954 | s->logs.bytes_out = txn->rsp.eoh; |
| 5955 | s->do_log(s); |
| 5956 | s->logs.bytes_out = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5957 | } |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 5958 | return 1; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5959 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5960 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5961 | /* This function is an analyser which forwards response body (including chunk |
| 5962 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 5963 | * zero byte. The only situation where it must not be called is when we're in |
| 5964 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 5965 | * remaining data and to resync after end of body. It expects the msg_state to |
| 5966 | * 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] | 5967 | * 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] | 5968 | * |
| 5969 | * It is capable of compressing response data both in content-length mode and |
| 5970 | * in chunked mode. The state machines follows different flows depending on |
| 5971 | * whether content-length and chunked modes are used, since there are no |
| 5972 | * trailers in content-length : |
| 5973 | * |
| 5974 | * chk-mode cl-mode |
| 5975 | * ,----- BODY -----. |
| 5976 | * / \ |
| 5977 | * V size > 0 V chk-mode |
| 5978 | * .--> SIZE -------------> DATA -------------> CRLF |
| 5979 | * | | size == 0 | last byte | |
| 5980 | * | v final crlf v inspected | |
| 5981 | * | TRAILERS -----------> DONE | |
| 5982 | * | | |
| 5983 | * `----------------------------------------------' |
| 5984 | * |
| 5985 | * Compression only happens in the DATA state, and must be flushed in final |
| 5986 | * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding |
| 5987 | * is performed at once on final states for all bytes parsed, or when leaving |
| 5988 | * on missing data. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5989 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5990 | 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] | 5991 | { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5992 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5993 | struct http_txn *txn = s->txn; |
| 5994 | struct http_msg *msg = &s->txn->rsp; |
Christopher Faulet | 3e34429 | 2015-11-24 16:24:13 +0100 | [diff] [blame] | 5995 | int ret; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5996 | |
Christopher Faulet | 814d270 | 2017-03-30 11:33:44 +0200 | [diff] [blame] | 5997 | DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n", |
| 5998 | now_ms, __FUNCTION__, |
| 5999 | s, |
| 6000 | res, |
| 6001 | res->rex, res->wex, |
| 6002 | res->flags, |
| 6003 | res->buf->i, |
| 6004 | res->analysers); |
| 6005 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 6006 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 6007 | return 0; |
| 6008 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 6009 | 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] | 6010 | ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) || |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 6011 | !s->req.analysers) { |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 6012 | /* Output closed while we were sending data. We must abort and |
| 6013 | * wake the other side up. |
| 6014 | */ |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 6015 | msg->err_state = msg->msg_state; |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 6016 | msg->msg_state = HTTP_MSG_ERROR; |
| 6017 | http_resync_states(s); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 6018 | return 1; |
| 6019 | } |
| 6020 | |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 6021 | /* in most states, we should abort in case of early close */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6022 | channel_auto_close(res); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 6023 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 6024 | if (msg->msg_state == HTTP_MSG_BODY) { |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6025 | msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK) |
| 6026 | ? HTTP_MSG_CHUNK_SIZE |
| 6027 | : HTTP_MSG_DATA); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6028 | } |
| 6029 | |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 6030 | if (res->to_forward) { |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6031 | /* We can't process the buffer's contents yet */ |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 6032 | res->flags |= CF_WAKE_WRITE; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6033 | goto missing_data_or_waiting; |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 6034 | } |
| 6035 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6036 | if (msg->msg_state < HTTP_MSG_DONE) { |
| 6037 | ret = ((msg->flags & HTTP_MSGF_TE_CHNK) |
| 6038 | ? http_msg_forward_chunked_body(s, msg) |
| 6039 | : http_msg_forward_body(s, msg)); |
| 6040 | if (!ret) |
| 6041 | goto missing_data_or_waiting; |
| 6042 | if (ret < 0) |
| 6043 | goto return_bad_res; |
| 6044 | } |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 6045 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6046 | /* other states, DONE...TUNNEL */ |
| 6047 | /* for keep-alive we don't want to forward closes on DONE */ |
| 6048 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 6049 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
| 6050 | channel_dont_close(res); |
Willy Tarreau | 3ce10ff | 2014-04-22 08:24:38 +0200 | [diff] [blame] | 6051 | |
Christopher Faulet | 894da4c | 2017-07-18 11:29:07 +0200 | [diff] [blame] | 6052 | http_resync_states(s); |
| 6053 | if (!(res->analysers & an_bit)) { |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6054 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 6055 | if (res->flags & CF_SHUTW) { |
| 6056 | /* response errors are most likely due to the |
| 6057 | * client aborting the transfer. */ |
| 6058 | goto aborted_xfer; |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 6059 | } |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6060 | if (msg->err_pos >= 0) |
Emeric Brun | 8c1aaa2 | 2017-06-15 11:30:06 +0200 | [diff] [blame] | 6061 | 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] | 6062 | goto return_bad_res; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6063 | } |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6064 | return 1; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6065 | } |
Willy Tarreau | f51d03c | 2016-05-02 15:25:15 +0200 | [diff] [blame] | 6066 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6067 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6068 | missing_data_or_waiting: |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6069 | if (res->flags & CF_SHUTW) |
| 6070 | goto aborted_xfer; |
| 6071 | |
| 6072 | /* stop waiting for data if the input is closed before the end. If the |
| 6073 | * client side was already closed, it means that the client has aborted, |
| 6074 | * so we don't want to count this as a server abort. Otherwise it's a |
| 6075 | * server abort. |
| 6076 | */ |
Christopher Faulet | a33510b | 2017-03-31 15:37:29 +0200 | [diff] [blame] | 6077 | if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6078 | if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW)) |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6079 | goto aborted_xfer; |
Christopher Faulet | a46bbd8 | 2015-06-19 09:00:58 +0200 | [diff] [blame] | 6080 | /* If we have some pending data, we continue the processing */ |
| 6081 | if (!buffer_pending(res->buf)) { |
| 6082 | if (!(s->flags & SF_ERR_MASK)) |
| 6083 | s->flags |= SF_ERR_SRVCL; |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 6084 | HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1); |
Christopher Faulet | a46bbd8 | 2015-06-19 09:00:58 +0200 | [diff] [blame] | 6085 | if (objt_server(s->target)) |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 6086 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1); |
Christopher Faulet | a46bbd8 | 2015-06-19 09:00:58 +0200 | [diff] [blame] | 6087 | goto return_bad_res_stats_ok; |
| 6088 | } |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 6089 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 6090 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 6091 | /* 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] | 6092 | if (!s->req.analysers) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 6093 | goto return_bad_res; |
| 6094 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6095 | /* When TE: chunked is used, we need to get there again to parse |
| 6096 | * 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] | 6097 | * set CF_DONTCLOSE. Similarly, if keep-alive is set on the client side |
| 6098 | * or if there are filters registered on the stream, we don't want to |
| 6099 | * forward a close |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 6100 | */ |
Christopher Faulet | 69744d9 | 2017-03-30 10:54:35 +0200 | [diff] [blame] | 6101 | if ((msg->flags & HTTP_MSGF_TE_CHNK) || |
Christopher Faulet | f1cc5d0 | 2017-02-08 09:45:13 +0100 | [diff] [blame] | 6102 | HAS_DATA_FILTERS(s, res) || |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 6103 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 6104 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6105 | channel_dont_close(res); |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 6106 | |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 6107 | /* 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] | 6108 | * 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] | 6109 | * 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] | 6110 | * modes are already handled by the stream sock layer. We must not do |
| 6111 | * this in content-length mode because it could present the MSG_MORE |
| 6112 | * flag with the last block of forwarded data, which would cause an |
| 6113 | * additional delay to be observed by the receiver. |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 6114 | */ |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 6115 | if ((msg->flags & HTTP_MSGF_TE_CHNK) || (msg->flags & HTTP_MSGF_COMPRESSING)) |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 6116 | res->flags |= CF_EXPECT_MORE; |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 6117 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6118 | /* the stream handler will take care of timeouts and errors */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6119 | return 0; |
| 6120 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 6121 | return_bad_res: /* let's centralize all bad responses */ |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 6122 | HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6123 | if (objt_server(s->target)) |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 6124 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6125 | |
| 6126 | return_bad_res_stats_ok: |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 6127 | txn->rsp.err_state = txn->rsp.msg_state; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6128 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 6129 | /* don't send any error message as we're in the body */ |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 6130 | http_reply_and_close(s, txn->status, NULL); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 6131 | res->analysers &= AN_RES_FLT_END; |
| 6132 | 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] | 6133 | if (objt_server(s->target)) |
| 6134 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6135 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6136 | if (!(s->flags & SF_ERR_MASK)) |
| 6137 | s->flags |= SF_ERR_PRXCOND; |
| 6138 | if (!(s->flags & SF_FINST_MASK)) |
| 6139 | s->flags |= SF_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6140 | return 0; |
| 6141 | |
| 6142 | aborted_xfer: |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 6143 | txn->rsp.err_state = txn->rsp.msg_state; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6144 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
| 6145 | /* don't send any error message as we're in the body */ |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 6146 | http_reply_and_close(s, txn->status, NULL); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 6147 | res->analysers &= AN_RES_FLT_END; |
| 6148 | 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] | 6149 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 6150 | HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1); |
| 6151 | HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6152 | if (objt_server(s->target)) |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 6153 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6154 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6155 | if (!(s->flags & SF_ERR_MASK)) |
| 6156 | s->flags |= SF_ERR_CLICL; |
| 6157 | if (!(s->flags & SF_FINST_MASK)) |
| 6158 | s->flags |= SF_FINST_D; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6159 | return 0; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6160 | } |
| 6161 | |
| 6162 | |
| 6163 | static inline int |
| 6164 | http_msg_forward_body(struct stream *s, struct http_msg *msg) |
| 6165 | { |
| 6166 | struct channel *chn = msg->chn; |
| 6167 | int ret; |
| 6168 | |
| 6169 | /* Here we have the guarantee to be in HTTP_MSG_DATA or HTTP_MSG_ENDING state */ |
| 6170 | |
| 6171 | if (msg->msg_state == HTTP_MSG_ENDING) |
| 6172 | goto ending; |
| 6173 | |
| 6174 | /* Neither content-length, nor transfer-encoding was found, so we must |
| 6175 | * read the body until the server connection is closed. In that case, we |
| 6176 | * eat data as they come. Of course, this happens for response only. */ |
| 6177 | if (!(msg->flags & HTTP_MSGF_XFER_LEN)) { |
| 6178 | unsigned long long len = (chn->buf->i - msg->next); |
| 6179 | msg->chunk_len += len; |
| 6180 | msg->body_len += len; |
| 6181 | } |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 6182 | ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg), |
| 6183 | /* default_ret */ MIN(msg->chunk_len, chn->buf->i - msg->next), |
| 6184 | /* on_error */ goto error); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6185 | msg->next += ret; |
| 6186 | msg->chunk_len -= ret; |
| 6187 | if (msg->chunk_len) { |
| 6188 | /* input empty or output full */ |
| 6189 | if (chn->buf->i > msg->next) |
| 6190 | chn->flags |= CF_WAKE_WRITE; |
| 6191 | goto missing_data_or_waiting; |
| 6192 | } |
| 6193 | |
Christopher Faulet | 1486b0a | 2017-07-18 11:42:08 +0200 | [diff] [blame] | 6194 | /* This check can only be true for a response. HTTP_MSGF_XFER_LEN is |
| 6195 | * always set for a request. */ |
| 6196 | if (!(msg->flags & HTTP_MSGF_XFER_LEN)) { |
| 6197 | /* The server still sending data that should be filtered */ |
| 6198 | if (!(chn->flags & CF_SHUTR) && HAS_DATA_FILTERS(s, chn)) |
| 6199 | goto missing_data_or_waiting; |
| 6200 | } |
Christopher Faulet | f1cc5d0 | 2017-02-08 09:45:13 +0100 | [diff] [blame] | 6201 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6202 | msg->msg_state = HTTP_MSG_ENDING; |
| 6203 | |
| 6204 | ending: |
| 6205 | /* we may have some pending data starting at res->buf->p such as a last |
| 6206 | * chunk of data or trailers. */ |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 6207 | ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next), |
| 6208 | /* default_ret */ msg->next, |
| 6209 | /* on_error */ goto error); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6210 | b_adv(chn->buf, ret); |
| 6211 | msg->next -= ret; |
Willy Tarreau | 9962f8f | 2016-06-28 11:52:08 +0200 | [diff] [blame] | 6212 | if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)) |
| 6213 | msg->sov -= ret; |
Christopher Faulet | a9300a3 | 2016-06-28 15:54:44 +0200 | [diff] [blame] | 6214 | if (msg->next) |
| 6215 | goto waiting; |
Willy Tarreau | 9962f8f | 2016-06-28 11:52:08 +0200 | [diff] [blame] | 6216 | |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 6217 | FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg), |
| 6218 | /* default_ret */ 1, |
| 6219 | /* on_error */ goto error, |
| 6220 | /* on_wait */ goto waiting); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6221 | msg->msg_state = HTTP_MSG_DONE; |
| 6222 | return 1; |
| 6223 | |
| 6224 | missing_data_or_waiting: |
| 6225 | /* we may have some pending data starting at chn->buf->p */ |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 6226 | ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next), |
| 6227 | /* default_ret */ msg->next, |
| 6228 | /* on_error */ goto error); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6229 | b_adv(chn->buf, ret); |
| 6230 | msg->next -= ret; |
| 6231 | if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0) |
| 6232 | msg->sov -= ret; |
Christopher Faulet | 75e2eb6 | 2015-12-15 10:41:47 +0100 | [diff] [blame] | 6233 | if (!HAS_DATA_FILTERS(s, chn)) |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6234 | msg->chunk_len -= channel_forward(chn, msg->chunk_len); |
Christopher Faulet | a9300a3 | 2016-06-28 15:54:44 +0200 | [diff] [blame] | 6235 | waiting: |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6236 | return 0; |
| 6237 | error: |
| 6238 | return -1; |
| 6239 | } |
| 6240 | |
| 6241 | static inline int |
| 6242 | http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg) |
| 6243 | { |
| 6244 | struct channel *chn = msg->chn; |
Willy Tarreau | e56cdd3 | 2017-09-21 08:36:33 +0200 | [diff] [blame] | 6245 | unsigned int chunk; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6246 | int ret; |
| 6247 | |
| 6248 | /* Here we have the guarantee to be in one of the following state: |
| 6249 | * HTTP_MSG_DATA, HTTP_MSG_CHUNK_SIZE, HTTP_MSG_CHUNK_CRLF, |
| 6250 | * HTTP_MSG_TRAILERS or HTTP_MSG_ENDING. */ |
| 6251 | |
| 6252 | switch_states: |
| 6253 | switch (msg->msg_state) { |
| 6254 | case HTTP_MSG_DATA: |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 6255 | ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg), |
| 6256 | /* default_ret */ MIN(msg->chunk_len, chn->buf->i - msg->next), |
| 6257 | /* on_error */ goto error); |
| 6258 | msg->next += ret; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6259 | msg->chunk_len -= ret; |
| 6260 | if (msg->chunk_len) { |
| 6261 | /* input empty or output full */ |
| 6262 | if (chn->buf->i > msg->next) |
| 6263 | chn->flags |= CF_WAKE_WRITE; |
| 6264 | goto missing_data_or_waiting; |
| 6265 | } |
| 6266 | |
| 6267 | /* nothing left to forward for this chunk*/ |
| 6268 | msg->msg_state = HTTP_MSG_CHUNK_CRLF; |
| 6269 | /* fall through for HTTP_MSG_CHUNK_CRLF */ |
| 6270 | |
| 6271 | case HTTP_MSG_CHUNK_CRLF: |
| 6272 | /* we want the CRLF after the data */ |
Willy Tarreau | b289256 | 2017-09-21 11:33:54 +0200 | [diff] [blame] | 6273 | ret = h1_skip_chunk_crlf(chn->buf, msg->next, chn->buf->i); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6274 | if (ret == 0) |
| 6275 | goto missing_data_or_waiting; |
Willy Tarreau | b289256 | 2017-09-21 11:33:54 +0200 | [diff] [blame] | 6276 | if (ret < 0) { |
| 6277 | msg->err_pos = chn->buf->i + ret; |
| 6278 | if (msg->err_pos < 0) |
| 6279 | msg->err_pos += chn->buf->size; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6280 | goto chunk_parsing_error; |
Willy Tarreau | b289256 | 2017-09-21 11:33:54 +0200 | [diff] [blame] | 6281 | } |
Christopher Faulet | 113f7de | 2015-12-14 14:52:13 +0100 | [diff] [blame] | 6282 | msg->next += ret; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6283 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 6284 | /* fall through for HTTP_MSG_CHUNK_SIZE */ |
| 6285 | |
| 6286 | case HTTP_MSG_CHUNK_SIZE: |
| 6287 | /* read the chunk size and assign it to ->chunk_len, |
| 6288 | * then set ->next to point to the body and switch to |
| 6289 | * DATA or TRAILERS state. |
| 6290 | */ |
Willy Tarreau | e56cdd3 | 2017-09-21 08:36:33 +0200 | [diff] [blame] | 6291 | 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] | 6292 | if (ret == 0) |
| 6293 | goto missing_data_or_waiting; |
Willy Tarreau | e56cdd3 | 2017-09-21 08:36:33 +0200 | [diff] [blame] | 6294 | if (ret < 0) { |
| 6295 | msg->err_pos = chn->buf->i + ret; |
| 6296 | if (msg->err_pos < 0) |
| 6297 | msg->err_pos += chn->buf->size; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6298 | goto chunk_parsing_error; |
Willy Tarreau | e56cdd3 | 2017-09-21 08:36:33 +0200 | [diff] [blame] | 6299 | } |
| 6300 | |
| 6301 | msg->sol = ret; |
Christopher Faulet | 113f7de | 2015-12-14 14:52:13 +0100 | [diff] [blame] | 6302 | msg->next += ret; |
Willy Tarreau | e56cdd3 | 2017-09-21 08:36:33 +0200 | [diff] [blame] | 6303 | msg->chunk_len = chunk; |
| 6304 | msg->body_len += chunk; |
| 6305 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6306 | if (msg->chunk_len) { |
| 6307 | msg->msg_state = HTTP_MSG_DATA; |
| 6308 | goto switch_states; |
| 6309 | } |
| 6310 | msg->msg_state = HTTP_MSG_TRAILERS; |
| 6311 | /* fall through for HTTP_MSG_TRAILERS */ |
| 6312 | |
| 6313 | case HTTP_MSG_TRAILERS: |
| 6314 | ret = http_forward_trailers(msg); |
| 6315 | if (ret < 0) |
| 6316 | goto chunk_parsing_error; |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 6317 | FLT_STRM_DATA_CB(s, chn, flt_http_chunk_trailers(s, msg), |
| 6318 | /* default_ret */ 1, |
| 6319 | /* on_error */ goto error); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6320 | msg->next += msg->sol; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6321 | if (!ret) |
| 6322 | goto missing_data_or_waiting; |
| 6323 | break; |
| 6324 | |
| 6325 | case HTTP_MSG_ENDING: |
| 6326 | goto ending; |
| 6327 | |
| 6328 | default: |
| 6329 | /* This should no happen in this function */ |
| 6330 | goto error; |
| 6331 | } |
| 6332 | |
| 6333 | msg->msg_state = HTTP_MSG_ENDING; |
| 6334 | ending: |
| 6335 | /* we may have some pending data starting at res->buf->p such as a last |
| 6336 | * chunk of data or trailers. */ |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 6337 | 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] | 6338 | /* default_ret */ msg->next, |
| 6339 | /* on_error */ goto error); |
| 6340 | b_adv(chn->buf, ret); |
| 6341 | msg->next -= ret; |
Willy Tarreau | 9962f8f | 2016-06-28 11:52:08 +0200 | [diff] [blame] | 6342 | if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)) |
| 6343 | msg->sov -= ret; |
Christopher Faulet | a9300a3 | 2016-06-28 15:54:44 +0200 | [diff] [blame] | 6344 | if (msg->next) |
| 6345 | goto waiting; |
Willy Tarreau | 9962f8f | 2016-06-28 11:52:08 +0200 | [diff] [blame] | 6346 | |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 6347 | FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg), |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6348 | /* default_ret */ 1, |
| 6349 | /* on_error */ goto error, |
| 6350 | /* on_wait */ goto waiting); |
| 6351 | msg->msg_state = HTTP_MSG_DONE; |
| 6352 | return 1; |
| 6353 | |
| 6354 | missing_data_or_waiting: |
| 6355 | /* we may have some pending data starting at chn->buf->p */ |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 6356 | 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] | 6357 | /* default_ret */ msg->next, |
| 6358 | /* on_error */ goto error); |
| 6359 | b_adv(chn->buf, ret); |
| 6360 | msg->next -= ret; |
| 6361 | if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0) |
| 6362 | msg->sov -= ret; |
Christopher Faulet | 75e2eb6 | 2015-12-15 10:41:47 +0100 | [diff] [blame] | 6363 | if (!HAS_DATA_FILTERS(s, chn)) |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6364 | msg->chunk_len -= channel_forward(chn, msg->chunk_len); |
Christopher Faulet | a9300a3 | 2016-06-28 15:54:44 +0200 | [diff] [blame] | 6365 | waiting: |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6366 | return 0; |
| 6367 | |
| 6368 | chunk_parsing_error: |
| 6369 | if (msg->err_pos >= 0) { |
| 6370 | if (chn->flags & CF_ISRESP) |
Emeric Brun | 8c1aaa2 | 2017-06-15 11:30:06 +0200 | [diff] [blame] | 6371 | http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6372 | msg->msg_state, strm_fe(s)); |
| 6373 | else |
Emeric Brun | 8c1aaa2 | 2017-06-15 11:30:06 +0200 | [diff] [blame] | 6374 | 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] | 6375 | msg, msg->msg_state, s->be); |
| 6376 | } |
| 6377 | error: |
| 6378 | return -1; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6379 | } |
| 6380 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6381 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6382 | /* Iterate the same filter through all request headers. |
| 6383 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6384 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 6385 | * DENY stats. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6386 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6387 | 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] | 6388 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6389 | char *cur_ptr, *cur_end, *cur_next; |
| 6390 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 6391 | struct http_txn *txn = s->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6392 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6393 | int delta; |
Willy Tarreau | 0f7562b | 2007-01-07 15:46:13 +0100 | [diff] [blame] | 6394 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6395 | last_hdr = 0; |
| 6396 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6397 | cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6398 | old_idx = 0; |
| 6399 | |
| 6400 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6401 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6402 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6403 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6404 | (exp->action == ACT_ALLOW || |
| 6405 | exp->action == ACT_DENY || |
| 6406 | exp->action == ACT_TARPIT)) |
| 6407 | return 0; |
| 6408 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6409 | cur_idx = txn->hdr_idx.v[old_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6410 | if (!cur_idx) |
| 6411 | break; |
| 6412 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6413 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6414 | cur_ptr = cur_next; |
| 6415 | cur_end = cur_ptr + cur_hdr->len; |
| 6416 | cur_next = cur_end + cur_hdr->cr + 1; |
| 6417 | |
| 6418 | /* Now we have one header between cur_ptr and cur_end, |
| 6419 | * and the next header starts at cur_next. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6420 | */ |
| 6421 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 6422 | 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] | 6423 | switch (exp->action) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6424 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6425 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6426 | last_hdr = 1; |
| 6427 | break; |
| 6428 | |
| 6429 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6430 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6431 | last_hdr = 1; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6432 | break; |
| 6433 | |
| 6434 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6435 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6436 | last_hdr = 1; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6437 | break; |
| 6438 | |
| 6439 | case ACT_REPLACE: |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 6440 | trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch); |
| 6441 | if (trash.len < 0) |
| 6442 | return -1; |
| 6443 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6444 | 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] | 6445 | /* FIXME: if the user adds a newline in the replacement, the |
| 6446 | * index will not be recalculated for now, and the new line |
| 6447 | * will not be counted as a new header. |
| 6448 | */ |
| 6449 | |
| 6450 | cur_end += delta; |
| 6451 | cur_next += delta; |
| 6452 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6453 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6454 | break; |
| 6455 | |
| 6456 | case ACT_REMOVE: |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6457 | delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6458 | cur_next += delta; |
| 6459 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6460 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6461 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 6462 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6463 | cur_hdr->len = 0; |
| 6464 | cur_end = NULL; /* null-term has been rewritten */ |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 6465 | cur_idx = old_idx; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6466 | break; |
| 6467 | |
| 6468 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6469 | } |
| 6470 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6471 | /* keep the link from this header to next one in case of later |
| 6472 | * removal of next header. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6473 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6474 | old_idx = cur_idx; |
| 6475 | } |
| 6476 | return 0; |
| 6477 | } |
| 6478 | |
| 6479 | |
| 6480 | /* Apply the filter to the request line. |
| 6481 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 6482 | * or -1 if a replacement resulted in an invalid request line. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6483 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 6484 | * DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6485 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6486 | 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] | 6487 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6488 | char *cur_ptr, *cur_end; |
| 6489 | int done; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 6490 | struct http_txn *txn = s->txn; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6491 | int delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6492 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6493 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6494 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6495 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6496 | (exp->action == ACT_ALLOW || |
| 6497 | exp->action == ACT_DENY || |
| 6498 | exp->action == ACT_TARPIT)) |
| 6499 | return 0; |
| 6500 | else if (exp->action == ACT_REMOVE) |
| 6501 | return 0; |
| 6502 | |
| 6503 | done = 0; |
| 6504 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6505 | cur_ptr = req->buf->p; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6506 | cur_end = cur_ptr + txn->req.sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6507 | |
| 6508 | /* Now we have the request line between cur_ptr and cur_end */ |
| 6509 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 6510 | 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] | 6511 | switch (exp->action) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6512 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6513 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6514 | done = 1; |
| 6515 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 6516 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6517 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6518 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6519 | done = 1; |
| 6520 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 6521 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6522 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6523 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6524 | done = 1; |
| 6525 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 6526 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6527 | case ACT_REPLACE: |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 6528 | trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch); |
| 6529 | if (trash.len < 0) |
| 6530 | return -1; |
| 6531 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6532 | 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] | 6533 | /* FIXME: if the user adds a newline in the replacement, the |
| 6534 | * index will not be recalculated for now, and the new line |
| 6535 | * will not be counted as a new header. |
| 6536 | */ |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 6537 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6538 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6539 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 6540 | cur_end = (char *)http_parse_reqline(&txn->req, |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6541 | HTTP_MSG_RQMETH, |
| 6542 | cur_ptr, cur_end + 1, |
| 6543 | NULL, NULL); |
| 6544 | if (unlikely(!cur_end)) |
| 6545 | return -1; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 6546 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6547 | /* we have a full request and we know that we have either a CR |
| 6548 | * or an LF at <ptr>. |
| 6549 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6550 | txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l); |
| 6551 | 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] | 6552 | /* there is no point trying this regex on headers */ |
| 6553 | return 1; |
| 6554 | } |
| 6555 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6556 | return done; |
| 6557 | } |
Willy Tarreau | 97de624 | 2006-12-27 17:18:38 +0100 | [diff] [blame] | 6558 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6559 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6560 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6561 | /* |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6562 | * 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] | 6563 | * 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] | 6564 | * unparsable request. Since it can manage the switch to another backend, it |
| 6565 | * updates the per-proxy DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6566 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6567 | 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] | 6568 | { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 6569 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 6570 | struct http_txn *txn = s->txn; |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6571 | struct hdr_exp *exp; |
| 6572 | |
| 6573 | for (exp = px->req_exp; exp; exp = exp->next) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6574 | int ret; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6575 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6576 | /* |
| 6577 | * The interleaving of transformations and verdicts |
| 6578 | * makes it difficult to decide to continue or stop |
| 6579 | * the evaluation. |
| 6580 | */ |
| 6581 | |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6582 | if (txn->flags & (TX_CLDENY|TX_CLTARPIT)) |
| 6583 | break; |
| 6584 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6585 | if ((txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6586 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6587 | exp->action == ACT_TARPIT || exp->action == ACT_PASS)) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6588 | continue; |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6589 | |
| 6590 | /* if this filter had a condition, evaluate it now and skip to |
| 6591 | * next filter if the condition does not match. |
| 6592 | */ |
| 6593 | if (exp->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 6594 | 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] | 6595 | ret = acl_pass(ret); |
| 6596 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 6597 | ret = !ret; |
| 6598 | |
| 6599 | if (!ret) |
| 6600 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6601 | } |
| 6602 | |
| 6603 | /* Apply the filter to the request line. */ |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6604 | ret = apply_filter_to_req_line(s, req, exp); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6605 | if (unlikely(ret < 0)) |
| 6606 | return -1; |
| 6607 | |
| 6608 | if (likely(ret == 0)) { |
| 6609 | /* The filter did not match the request, it can be |
| 6610 | * iterated through all headers. |
| 6611 | */ |
Willy Tarreau | 34d4c3c | 2015-01-30 20:58:58 +0100 | [diff] [blame] | 6612 | if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0)) |
| 6613 | return -1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6614 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6615 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6616 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6617 | } |
| 6618 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6619 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6620 | /* Find the end of a cookie value contained between <s> and <e>. It works the |
| 6621 | * same way as with headers above except that the semi-colon also ends a token. |
| 6622 | * See RFC2965 for more information. Note that it requires a valid header to |
| 6623 | * return a valid result. |
| 6624 | */ |
| 6625 | char *find_cookie_value_end(char *s, const char *e) |
| 6626 | { |
| 6627 | int quoted, qdpair; |
| 6628 | |
| 6629 | quoted = qdpair = 0; |
| 6630 | for (; s < e; s++) { |
| 6631 | if (qdpair) qdpair = 0; |
| 6632 | else if (quoted) { |
| 6633 | if (*s == '\\') qdpair = 1; |
| 6634 | else if (*s == '"') quoted = 0; |
| 6635 | } |
| 6636 | else if (*s == '"') quoted = 1; |
| 6637 | else if (*s == ',' || *s == ';') return s; |
| 6638 | } |
| 6639 | return s; |
| 6640 | } |
| 6641 | |
| 6642 | /* Delete a value in a header between delimiters <from> and <next> in buffer |
| 6643 | * <buf>. The number of characters displaced is returned, and the pointer to |
| 6644 | * the first delimiter is updated if required. The function tries as much as |
| 6645 | * possible to respect the following principles : |
| 6646 | * - replace <from> delimiter by the <next> one unless <from> points to a |
| 6647 | * colon, in which case <next> is simply removed |
| 6648 | * - set exactly one space character after the new first delimiter, unless |
| 6649 | * there are not enough characters in the block being moved to do so. |
| 6650 | * - remove unneeded spaces before the previous delimiter and after the new |
| 6651 | * one. |
| 6652 | * |
| 6653 | * It is the caller's responsibility to ensure that : |
| 6654 | * - <from> points to a valid delimiter or the colon ; |
| 6655 | * - <next> points to a valid delimiter or the final CR/LF ; |
| 6656 | * - there are non-space chars before <from> ; |
| 6657 | * - there is a CR/LF at or after <next>. |
| 6658 | */ |
Willy Tarreau | af81935 | 2012-08-27 22:08:00 +0200 | [diff] [blame] | 6659 | int del_hdr_value(struct buffer *buf, char **from, char *next) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6660 | { |
| 6661 | char *prev = *from; |
| 6662 | |
| 6663 | if (*prev == ':') { |
| 6664 | /* We're removing the first value, preserve the colon and add a |
| 6665 | * space if possible. |
| 6666 | */ |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 6667 | if (!HTTP_IS_CRLF(*next)) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6668 | next++; |
| 6669 | prev++; |
| 6670 | if (prev < next) |
| 6671 | *prev++ = ' '; |
| 6672 | |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 6673 | while (HTTP_IS_SPHT(*next)) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6674 | next++; |
| 6675 | } else { |
| 6676 | /* Remove useless spaces before the old delimiter. */ |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 6677 | while (HTTP_IS_SPHT(*(prev-1))) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6678 | prev--; |
| 6679 | *from = prev; |
| 6680 | |
| 6681 | /* copy the delimiter and if possible a space if we're |
| 6682 | * not at the end of the line. |
| 6683 | */ |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 6684 | if (!HTTP_IS_CRLF(*next)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6685 | *prev++ = *next++; |
| 6686 | if (prev + 1 < next) |
| 6687 | *prev++ = ' '; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 6688 | while (HTTP_IS_SPHT(*next)) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6689 | next++; |
| 6690 | } |
| 6691 | } |
| 6692 | return buffer_replace2(buf, prev, next, NULL, 0); |
| 6693 | } |
| 6694 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6695 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 6696 | * 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] | 6697 | * desirable to call it only when needed. This code is quite complex because |
| 6698 | * of the multiple very crappy and ambiguous syntaxes we have to support. it |
| 6699 | * highly recommended not to touch this part without a good reason ! |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6700 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6701 | void manage_client_side_cookies(struct stream *s, struct channel *req) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6702 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 6703 | struct http_txn *txn = s->txn; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6704 | struct session *sess = s->sess; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6705 | int preserve_hdr; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6706 | int cur_idx, old_idx; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6707 | char *hdr_beg, *hdr_end, *hdr_next, *del_from; |
| 6708 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6709 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6710 | /* Iterate through the headers, we start with the start line. */ |
Willy Tarreau | 83969f4 | 2007-01-22 08:55:47 +0100 | [diff] [blame] | 6711 | old_idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6712 | hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6713 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6714 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6715 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6716 | int val; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6717 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6718 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6719 | hdr_beg = hdr_next; |
| 6720 | hdr_end = hdr_beg + cur_hdr->len; |
| 6721 | hdr_next = hdr_end + cur_hdr->cr + 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6722 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6723 | /* We have one full header between hdr_beg and hdr_end, and the |
| 6724 | * next header starts at hdr_next. We're only interested in |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6725 | * "Cookie:" headers. |
| 6726 | */ |
| 6727 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6728 | val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6); |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6729 | if (!val) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6730 | old_idx = cur_idx; |
| 6731 | continue; |
| 6732 | } |
| 6733 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6734 | del_from = NULL; /* nothing to be deleted */ |
| 6735 | preserve_hdr = 0; /* assume we may kill the whole header */ |
| 6736 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6737 | /* Now look for cookies. Conforming to RFC2109, we have to support |
| 6738 | * attributes whose name begin with a '$', and associate them with |
| 6739 | * the right cookie, if we want to delete this cookie. |
| 6740 | * So there are 3 cases for each cookie read : |
| 6741 | * 1) it's a special attribute, beginning with a '$' : ignore it. |
| 6742 | * 2) it's a server id cookie that we *MAY* want to delete : save |
| 6743 | * some pointers on it (last semi-colon, beginning of cookie...) |
| 6744 | * 3) it's an application cookie : we *MAY* have to delete a previous |
| 6745 | * "special" cookie. |
| 6746 | * At the end of loop, if a "special" cookie remains, we may have to |
| 6747 | * remove it. If no application cookie persists in the header, we |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6748 | * *MUST* delete it. |
| 6749 | * |
| 6750 | * Note: RFC2965 is unclear about the processing of spaces around |
| 6751 | * the equal sign in the ATTR=VALUE form. A careful inspection of |
| 6752 | * the RFC explicitly allows spaces before it, and not within the |
| 6753 | * tokens (attrs or values). An inspection of RFC2109 allows that |
| 6754 | * too but section 10.1.3 lets one think that spaces may be allowed |
| 6755 | * after the equal sign too, resulting in some (rare) buggy |
| 6756 | * implementations trying to do that. So let's do what servers do. |
| 6757 | * Latest ietf draft forbids spaces all around. Also, earlier RFCs |
| 6758 | * allowed quoted strings in values, with any possible character |
| 6759 | * after a backslash, including control chars and delimitors, which |
| 6760 | * causes parsing to become ambiguous. Browsers also allow spaces |
| 6761 | * within values even without quotes. |
| 6762 | * |
| 6763 | * We have to keep multiple pointers in order to support cookie |
| 6764 | * removal at the beginning, middle or end of header without |
| 6765 | * corrupting the header. All of these headers are valid : |
| 6766 | * |
| 6767 | * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n |
| 6768 | * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n |
| 6769 | * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n |
| 6770 | * | | | | | | | | | |
| 6771 | * | | | | | | | | hdr_end <--+ |
| 6772 | * | | | | | | | +--> next |
| 6773 | * | | | | | | +----> val_end |
| 6774 | * | | | | | +-----------> val_beg |
| 6775 | * | | | | +--------------> equal |
| 6776 | * | | | +----------------> att_end |
| 6777 | * | | +---------------------> att_beg |
| 6778 | * | +--------------------------> prev |
| 6779 | * +--------------------------------> hdr_beg |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6780 | */ |
| 6781 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6782 | for (prev = hdr_beg + 6; prev < hdr_end; prev = next) { |
| 6783 | /* Iterate through all cookies on this line */ |
| 6784 | |
| 6785 | /* find att_beg */ |
| 6786 | att_beg = prev + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 6787 | while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg)) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6788 | att_beg++; |
| 6789 | |
| 6790 | /* find att_end : this is the first character after the last non |
| 6791 | * space before the equal. It may be equal to hdr_end. |
| 6792 | */ |
| 6793 | equal = att_end = att_beg; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6794 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6795 | while (equal < hdr_end) { |
| 6796 | if (*equal == '=' || *equal == ',' || *equal == ';') |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6797 | break; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 6798 | if (HTTP_IS_SPHT(*equal++)) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6799 | continue; |
| 6800 | att_end = equal; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6801 | } |
| 6802 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6803 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 6804 | * is between <att_beg> and <equal>, both may be identical. |
| 6805 | */ |
| 6806 | |
| 6807 | /* look for end of cookie if there is an equal sign */ |
| 6808 | if (equal < hdr_end && *equal == '=') { |
| 6809 | /* look for the beginning of the value */ |
| 6810 | val_beg = equal + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 6811 | while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg)) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6812 | val_beg++; |
| 6813 | |
| 6814 | /* find the end of the value, respecting quotes */ |
| 6815 | next = find_cookie_value_end(val_beg, hdr_end); |
| 6816 | |
| 6817 | /* make val_end point to the first white space or delimitor after the value */ |
| 6818 | val_end = next; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 6819 | while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1))) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6820 | val_end--; |
| 6821 | } else { |
| 6822 | val_beg = val_end = next = equal; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 6823 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6824 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6825 | /* We have nothing to do with attributes beginning with '$'. However, |
| 6826 | * they will automatically be removed if a header before them is removed, |
| 6827 | * since they're supposed to be linked together. |
| 6828 | */ |
| 6829 | if (*att_beg == '$') |
| 6830 | continue; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6831 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6832 | /* Ignore cookies with no equal sign */ |
| 6833 | if (equal == next) { |
| 6834 | /* This is not our cookie, so we must preserve it. But if we already |
| 6835 | * scheduled another cookie for removal, we cannot remove the |
| 6836 | * complete header, but we can remove the previous block itself. |
| 6837 | */ |
| 6838 | preserve_hdr = 1; |
| 6839 | if (del_from != NULL) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6840 | int delta = del_hdr_value(req->buf, &del_from, prev); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6841 | val_end += delta; |
| 6842 | next += delta; |
| 6843 | hdr_end += delta; |
| 6844 | hdr_next += delta; |
| 6845 | cur_hdr->len += delta; |
| 6846 | http_msg_move_end(&txn->req, delta); |
| 6847 | prev = del_from; |
| 6848 | del_from = NULL; |
| 6849 | } |
| 6850 | continue; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 6851 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6852 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6853 | /* if there are spaces around the equal sign, we need to |
| 6854 | * strip them otherwise we'll get trouble for cookie captures, |
| 6855 | * or even for rewrites. Since this happens extremely rarely, |
| 6856 | * it does not hurt performance. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6857 | */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6858 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 6859 | int stripped_before = 0; |
| 6860 | int stripped_after = 0; |
| 6861 | |
| 6862 | if (att_end != equal) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6863 | stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6864 | equal += stripped_before; |
| 6865 | val_beg += stripped_before; |
| 6866 | } |
| 6867 | |
| 6868 | if (val_beg > equal + 1) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6869 | stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6870 | val_beg += stripped_after; |
| 6871 | stripped_before += stripped_after; |
| 6872 | } |
| 6873 | |
| 6874 | val_end += stripped_before; |
| 6875 | next += stripped_before; |
| 6876 | hdr_end += stripped_before; |
| 6877 | hdr_next += stripped_before; |
| 6878 | cur_hdr->len += stripped_before; |
| 6879 | http_msg_move_end(&txn->req, stripped_before); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6880 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6881 | /* now everything is as on the diagram above */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6882 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6883 | /* First, let's see if we want to capture this cookie. We check |
| 6884 | * that we don't already have a client side cookie, because we |
| 6885 | * can only capture one. Also as an optimisation, we ignore |
| 6886 | * cookies shorter than the declared name. |
| 6887 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6888 | if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL && |
| 6889 | (val_end - att_beg >= sess->fe->capture_namelen) && |
| 6890 | memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6891 | int log_len = val_end - att_beg; |
| 6892 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6893 | if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 6894 | ha_alert("HTTP logging : out of memory.\n"); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6895 | } else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6896 | if (log_len > sess->fe->capture_len) |
| 6897 | log_len = sess->fe->capture_len; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6898 | memcpy(txn->cli_cookie, att_beg, log_len); |
| 6899 | txn->cli_cookie[log_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6900 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6901 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6902 | |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6903 | /* Persistence cookies in passive, rewrite or insert mode have the |
| 6904 | * following form : |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6905 | * |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6906 | * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]] |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6907 | * |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6908 | * For cookies in prefix mode, the form is : |
| 6909 | * |
| 6910 | * Cookie: NAME=SRV~VALUE |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6911 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6912 | if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) && |
| 6913 | (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) { |
| 6914 | struct server *srv = s->be->srv; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6915 | char *delim; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6916 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6917 | /* if we're in cookie prefix mode, we'll search the delimitor so that we |
| 6918 | * have the server ID between val_beg and delim, and the original cookie between |
| 6919 | * delim+1 and val_end. Otherwise, delim==val_end : |
| 6920 | * |
| 6921 | * Cookie: NAME=SRV; # in all but prefix modes |
| 6922 | * Cookie: NAME=SRV~OPAQUE ; # in prefix mode |
| 6923 | * | || || | |+-> next |
| 6924 | * | || || | +--> val_end |
| 6925 | * | || || +---------> delim |
| 6926 | * | || |+------------> val_beg |
| 6927 | * | || +-------------> att_end = equal |
| 6928 | * | |+-----------------> att_beg |
| 6929 | * | +------------------> prev |
| 6930 | * +-------------------------> hdr_beg |
| 6931 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6932 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6933 | if (s->be->ck_opts & PR_CK_PFX) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6934 | for (delim = val_beg; delim < val_end; delim++) |
| 6935 | if (*delim == COOKIE_DELIM) |
| 6936 | break; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6937 | } else { |
| 6938 | char *vbar1; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6939 | delim = val_end; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6940 | /* Now check if the cookie contains a date field, which would |
| 6941 | * appear after a vertical bar ('|') just after the server name |
| 6942 | * and before the delimiter. |
| 6943 | */ |
| 6944 | vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg); |
| 6945 | if (vbar1) { |
| 6946 | /* OK, so left of the bar is the server's cookie and |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6947 | * right is the last seen date. It is a base64 encoded |
| 6948 | * 30-bit value representing the UNIX date since the |
| 6949 | * epoch in 4-second quantities. |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6950 | */ |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6951 | int val; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6952 | delim = vbar1++; |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6953 | if (val_end - vbar1 >= 5) { |
| 6954 | val = b64tos30(vbar1); |
| 6955 | if (val > 0) |
| 6956 | txn->cookie_last_date = val << 2; |
| 6957 | } |
| 6958 | /* look for a second vertical bar */ |
| 6959 | vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1); |
| 6960 | if (vbar1 && (val_end - vbar1 > 5)) { |
| 6961 | val = b64tos30(vbar1 + 1); |
| 6962 | if (val > 0) |
| 6963 | txn->cookie_first_date = val << 2; |
| 6964 | } |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6965 | } |
| 6966 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6967 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6968 | /* if the cookie has an expiration date and the proxy wants to check |
| 6969 | * it, then we do that now. We first check if the cookie is too old, |
| 6970 | * then only if it has expired. We detect strict overflow because the |
| 6971 | * time resolution here is not great (4 seconds). Cookies with dates |
| 6972 | * in the future are ignored if their offset is beyond one day. This |
| 6973 | * allows an admin to fix timezone issues without expiring everyone |
| 6974 | * and at the same time avoids keeping unwanted side effects for too |
| 6975 | * long. |
| 6976 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6977 | if (txn->cookie_first_date && s->be->cookie_maxlife && |
| 6978 | (((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] | 6979 | ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) { |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6980 | txn->flags &= ~TX_CK_MASK; |
| 6981 | txn->flags |= TX_CK_OLD; |
| 6982 | delim = val_beg; // let's pretend we have not found the cookie |
| 6983 | txn->cookie_first_date = 0; |
| 6984 | txn->cookie_last_date = 0; |
| 6985 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6986 | else if (txn->cookie_last_date && s->be->cookie_maxidle && |
| 6987 | (((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] | 6988 | ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) { |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6989 | txn->flags &= ~TX_CK_MASK; |
| 6990 | txn->flags |= TX_CK_EXPIRED; |
| 6991 | delim = val_beg; // let's pretend we have not found the cookie |
| 6992 | txn->cookie_first_date = 0; |
| 6993 | txn->cookie_last_date = 0; |
| 6994 | } |
| 6995 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6996 | /* Here, we'll look for the first running server which supports the cookie. |
| 6997 | * This allows to share a same cookie between several servers, for example |
| 6998 | * to dedicate backup servers to specific servers only. |
| 6999 | * However, to prevent clients from sticking to cookie-less backup server |
| 7000 | * when they have incidentely learned an empty cookie, we simply ignore |
| 7001 | * empty cookies and mark them as invalid. |
| 7002 | * The same behaviour is applied when persistence must be ignored. |
| 7003 | */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 7004 | if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED))) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7005 | srv = NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7006 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7007 | while (srv) { |
| 7008 | if (srv->cookie && (srv->cklen == delim - val_beg) && |
| 7009 | !memcmp(val_beg, srv->cookie, delim - val_beg)) { |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 7010 | if ((srv->cur_state != SRV_ST_STOPPED) || |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7011 | (s->be->options & PR_O_PERSIST) || |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 7012 | (s->flags & SF_FORCE_PRST)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7013 | /* we found the server and we can use it */ |
| 7014 | txn->flags &= ~TX_CK_MASK; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 7015 | 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] | 7016 | s->flags |= SF_DIRECT | SF_ASSIGNED; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7017 | s->target = &srv->obj_type; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7018 | break; |
| 7019 | } else { |
| 7020 | /* we found a server, but it's down, |
| 7021 | * mark it as such and go on in case |
| 7022 | * another one is available. |
| 7023 | */ |
| 7024 | txn->flags &= ~TX_CK_MASK; |
| 7025 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7026 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7027 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7028 | srv = srv->next; |
| 7029 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7030 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7031 | 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] | 7032 | /* no server matched this cookie or we deliberately skipped it */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7033 | txn->flags &= ~TX_CK_MASK; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 7034 | if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED))) |
Willy Tarreau | c89ccb6 | 2012-04-05 21:18:22 +0200 | [diff] [blame] | 7035 | txn->flags |= TX_CK_UNUSED; |
| 7036 | else |
| 7037 | txn->flags |= TX_CK_INVALID; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7038 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7039 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7040 | /* depending on the cookie mode, we may have to either : |
| 7041 | * - delete the complete cookie if we're in insert+indirect mode, so that |
| 7042 | * the server never sees it ; |
| 7043 | * - remove the server id from the cookie value, and tag the cookie as an |
| 7044 | * application cookie so that it does not get accidentely removed later, |
| 7045 | * if we're in cookie prefix mode |
| 7046 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7047 | if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7048 | int delta; /* negative */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7049 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7050 | delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7051 | val_end += delta; |
| 7052 | next += delta; |
| 7053 | hdr_end += delta; |
| 7054 | hdr_next += delta; |
| 7055 | cur_hdr->len += delta; |
| 7056 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7057 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7058 | del_from = NULL; |
| 7059 | preserve_hdr = 1; /* we want to keep this cookie */ |
| 7060 | } |
| 7061 | else if (del_from == NULL && |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7062 | (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] | 7063 | del_from = prev; |
| 7064 | } |
| 7065 | } else { |
| 7066 | /* This is not our cookie, so we must preserve it. But if we already |
| 7067 | * scheduled another cookie for removal, we cannot remove the |
| 7068 | * complete header, but we can remove the previous block itself. |
| 7069 | */ |
| 7070 | preserve_hdr = 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7071 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7072 | if (del_from != NULL) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7073 | int delta = del_hdr_value(req->buf, &del_from, prev); |
Willy Tarreau | b810554 | 2010-11-24 18:31:28 +0100 | [diff] [blame] | 7074 | if (att_beg >= del_from) |
| 7075 | att_beg += delta; |
| 7076 | if (att_end >= del_from) |
| 7077 | att_end += delta; |
| 7078 | val_beg += delta; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7079 | val_end += delta; |
| 7080 | next += delta; |
| 7081 | hdr_end += delta; |
| 7082 | hdr_next += delta; |
| 7083 | cur_hdr->len += delta; |
| 7084 | http_msg_move_end(&txn->req, delta); |
| 7085 | prev = del_from; |
| 7086 | del_from = NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7087 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7088 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7089 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7090 | /* continue with next cookie on this header line */ |
| 7091 | att_beg = next; |
| 7092 | } /* for each cookie */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7093 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7094 | /* There are no more cookies on this line. |
| 7095 | * We may still have one (or several) marked for deletion at the |
| 7096 | * end of the line. We must do this now in two ways : |
| 7097 | * - if some cookies must be preserved, we only delete from the |
| 7098 | * mark to the end of line ; |
| 7099 | * - if nothing needs to be preserved, simply delete the whole header |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7100 | */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7101 | if (del_from) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7102 | int delta; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7103 | if (preserve_hdr) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7104 | delta = del_hdr_value(req->buf, &del_from, hdr_end); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7105 | hdr_end = del_from; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7106 | cur_hdr->len += delta; |
| 7107 | } else { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7108 | delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7109 | |
| 7110 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7111 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 7112 | txn->hdr_idx.used--; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7113 | cur_hdr->len = 0; |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 7114 | cur_idx = old_idx; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7115 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7116 | hdr_next += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7117 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7118 | } |
| 7119 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7120 | /* check next header */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7121 | old_idx = cur_idx; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7122 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7123 | } |
| 7124 | |
| 7125 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7126 | /* Iterate the same filter through all response headers contained in <rtr>. |
| 7127 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
| 7128 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7129 | 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] | 7130 | { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7131 | char *cur_ptr, *cur_end, *cur_next; |
| 7132 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7133 | struct http_txn *txn = s->txn; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7134 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7135 | int delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7136 | |
| 7137 | last_hdr = 0; |
| 7138 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7139 | cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7140 | old_idx = 0; |
| 7141 | |
| 7142 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7143 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7144 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7145 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7146 | (exp->action == ACT_ALLOW || |
| 7147 | exp->action == ACT_DENY)) |
| 7148 | return 0; |
| 7149 | |
| 7150 | cur_idx = txn->hdr_idx.v[old_idx].next; |
| 7151 | if (!cur_idx) |
| 7152 | break; |
| 7153 | |
| 7154 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 7155 | cur_ptr = cur_next; |
| 7156 | cur_end = cur_ptr + cur_hdr->len; |
| 7157 | cur_next = cur_end + cur_hdr->cr + 1; |
| 7158 | |
| 7159 | /* Now we have one header between cur_ptr and cur_end, |
| 7160 | * and the next header starts at cur_next. |
| 7161 | */ |
| 7162 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 7163 | 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] | 7164 | switch (exp->action) { |
| 7165 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7166 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7167 | last_hdr = 1; |
| 7168 | break; |
| 7169 | |
| 7170 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7171 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7172 | last_hdr = 1; |
| 7173 | break; |
| 7174 | |
| 7175 | case ACT_REPLACE: |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 7176 | trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch); |
| 7177 | if (trash.len < 0) |
| 7178 | return -1; |
| 7179 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7180 | 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] | 7181 | /* FIXME: if the user adds a newline in the replacement, the |
| 7182 | * index will not be recalculated for now, and the new line |
| 7183 | * will not be counted as a new header. |
| 7184 | */ |
| 7185 | |
| 7186 | cur_end += delta; |
| 7187 | cur_next += delta; |
| 7188 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7189 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7190 | break; |
| 7191 | |
| 7192 | case ACT_REMOVE: |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7193 | delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7194 | cur_next += delta; |
| 7195 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7196 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7197 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 7198 | txn->hdr_idx.used--; |
| 7199 | cur_hdr->len = 0; |
| 7200 | cur_end = NULL; /* null-term has been rewritten */ |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 7201 | cur_idx = old_idx; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7202 | break; |
| 7203 | |
| 7204 | } |
| 7205 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7206 | |
| 7207 | /* keep the link from this header to next one in case of later |
| 7208 | * removal of next header. |
| 7209 | */ |
| 7210 | old_idx = cur_idx; |
| 7211 | } |
| 7212 | return 0; |
| 7213 | } |
| 7214 | |
| 7215 | |
| 7216 | /* Apply the filter to the status line in the response buffer <rtr>. |
| 7217 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 7218 | * or -1 if a replacement resulted in an invalid status line. |
| 7219 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7220 | 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] | 7221 | { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7222 | char *cur_ptr, *cur_end; |
| 7223 | int done; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7224 | struct http_txn *txn = s->txn; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7225 | int delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7226 | |
| 7227 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7228 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7229 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7230 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7231 | (exp->action == ACT_ALLOW || |
| 7232 | exp->action == ACT_DENY)) |
| 7233 | return 0; |
| 7234 | else if (exp->action == ACT_REMOVE) |
| 7235 | return 0; |
| 7236 | |
| 7237 | done = 0; |
| 7238 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7239 | cur_ptr = rtr->buf->p; |
Willy Tarreau | 1ba0e5f | 2010-06-07 13:57:32 +0200 | [diff] [blame] | 7240 | cur_end = cur_ptr + txn->rsp.sl.st.l; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7241 | |
| 7242 | /* Now we have the status line between cur_ptr and cur_end */ |
| 7243 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 7244 | 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] | 7245 | switch (exp->action) { |
| 7246 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7247 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7248 | done = 1; |
| 7249 | break; |
| 7250 | |
| 7251 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7252 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7253 | done = 1; |
| 7254 | break; |
| 7255 | |
| 7256 | case ACT_REPLACE: |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 7257 | trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch); |
| 7258 | if (trash.len < 0) |
| 7259 | return -1; |
| 7260 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7261 | 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] | 7262 | /* FIXME: if the user adds a newline in the replacement, the |
| 7263 | * index will not be recalculated for now, and the new line |
| 7264 | * will not be counted as a new header. |
| 7265 | */ |
| 7266 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7267 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7268 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 7269 | cur_end = (char *)http_parse_stsline(&txn->rsp, |
Willy Tarreau | 0278576 | 2007-04-03 14:45:44 +0200 | [diff] [blame] | 7270 | HTTP_MSG_RPVER, |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7271 | cur_ptr, cur_end + 1, |
| 7272 | NULL, NULL); |
| 7273 | if (unlikely(!cur_end)) |
| 7274 | return -1; |
| 7275 | |
| 7276 | /* we have a full respnse and we know that we have either a CR |
| 7277 | * or an LF at <ptr>. |
| 7278 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7279 | 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] | 7280 | 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] | 7281 | /* there is no point trying this regex on headers */ |
| 7282 | return 1; |
| 7283 | } |
| 7284 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7285 | return done; |
| 7286 | } |
| 7287 | |
| 7288 | |
| 7289 | |
| 7290 | /* |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7291 | * 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] | 7292 | * Returns 0 if everything is alright, or -1 in case a replacement lead to an |
| 7293 | * unparsable response. |
| 7294 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7295 | 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] | 7296 | { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 7297 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7298 | struct http_txn *txn = s->txn; |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7299 | struct hdr_exp *exp; |
| 7300 | |
| 7301 | for (exp = px->rsp_exp; exp; exp = exp->next) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7302 | int ret; |
| 7303 | |
| 7304 | /* |
| 7305 | * The interleaving of transformations and verdicts |
| 7306 | * makes it difficult to decide to continue or stop |
| 7307 | * the evaluation. |
| 7308 | */ |
| 7309 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7310 | if (txn->flags & TX_SVDENY) |
| 7311 | break; |
| 7312 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7313 | if ((txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7314 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 7315 | exp->action == ACT_PASS)) { |
| 7316 | exp = exp->next; |
| 7317 | continue; |
| 7318 | } |
| 7319 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7320 | /* if this filter had a condition, evaluate it now and skip to |
| 7321 | * next filter if the condition does not match. |
| 7322 | */ |
| 7323 | if (exp->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 7324 | 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] | 7325 | ret = acl_pass(ret); |
| 7326 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 7327 | ret = !ret; |
| 7328 | if (!ret) |
| 7329 | continue; |
| 7330 | } |
| 7331 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7332 | /* Apply the filter to the status line. */ |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7333 | ret = apply_filter_to_sts_line(s, rtr, exp); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7334 | if (unlikely(ret < 0)) |
| 7335 | return -1; |
| 7336 | |
| 7337 | if (likely(ret == 0)) { |
| 7338 | /* The filter did not match the response, it can be |
| 7339 | * iterated through all headers. |
| 7340 | */ |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 7341 | if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0)) |
| 7342 | return -1; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7343 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7344 | } |
| 7345 | return 0; |
| 7346 | } |
| 7347 | |
| 7348 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7349 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 7350 | * 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] | 7351 | * desirable to call it only when needed. This function is also used when we |
| 7352 | * 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] | 7353 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7354 | void manage_server_side_cookies(struct stream *s, struct channel *res) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7355 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7356 | struct http_txn *txn = s->txn; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7357 | struct session *sess = s->sess; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 7358 | struct server *srv; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7359 | int is_cookie2; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7360 | int cur_idx, old_idx, delta; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7361 | char *hdr_beg, *hdr_end, *hdr_next; |
| 7362 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7363 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7364 | /* Iterate through the headers. |
| 7365 | * we start with the start line. |
| 7366 | */ |
| 7367 | old_idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7368 | hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7369 | |
| 7370 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 7371 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7372 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7373 | |
| 7374 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7375 | hdr_beg = hdr_next; |
| 7376 | hdr_end = hdr_beg + cur_hdr->len; |
| 7377 | hdr_next = hdr_end + cur_hdr->cr + 1; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7378 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7379 | /* We have one full header between hdr_beg and hdr_end, and the |
| 7380 | * next header starts at hdr_next. We're only interested in |
| 7381 | * "Set-Cookie" and "Set-Cookie2" headers. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7382 | */ |
| 7383 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7384 | is_cookie2 = 0; |
| 7385 | prev = hdr_beg + 10; |
| 7386 | val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10); |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7387 | if (!val) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7388 | val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11); |
| 7389 | if (!val) { |
| 7390 | old_idx = cur_idx; |
| 7391 | continue; |
| 7392 | } |
| 7393 | is_cookie2 = 1; |
| 7394 | prev = hdr_beg + 11; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7395 | } |
| 7396 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7397 | /* OK, right now we know we have a Set-Cookie* at hdr_beg, and |
| 7398 | * <prev> points to the colon. |
| 7399 | */ |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7400 | txn->flags |= TX_SCK_PRESENT; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7401 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7402 | /* Maybe we only wanted to see if there was a Set-Cookie (eg: |
| 7403 | * check-cache is enabled) and we are not interested in checking |
| 7404 | * them. Warning, the cookie capture is declared in the frontend. |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 7405 | */ |
Willy Tarreau | 53a09d5 | 2015-08-10 18:59:40 +0200 | [diff] [blame] | 7406 | if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7407 | return; |
| 7408 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7409 | /* OK so now we know we have to process this response cookie. |
| 7410 | * The format of the Set-Cookie header is slightly different |
| 7411 | * from the format of the Cookie header in that it does not |
| 7412 | * support the comma as a cookie delimiter (thus the header |
| 7413 | * cannot be folded) because the Expires attribute described in |
| 7414 | * the original Netscape's spec may contain an unquoted date |
| 7415 | * with a comma inside. We have to live with this because |
| 7416 | * many browsers don't support Max-Age and some browsers don't |
| 7417 | * support quoted strings. However the Set-Cookie2 header is |
| 7418 | * clean. |
| 7419 | * |
| 7420 | * We have to keep multiple pointers in order to support cookie |
| 7421 | * removal at the beginning, middle or end of header without |
| 7422 | * corrupting the header (in case of set-cookie2). A special |
| 7423 | * pointer, <scav> points to the beginning of the set-cookie-av |
| 7424 | * fields after the first semi-colon. The <next> pointer points |
| 7425 | * either to the end of line (set-cookie) or next unquoted comma |
| 7426 | * (set-cookie2). All of these headers are valid : |
| 7427 | * |
| 7428 | * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n |
| 7429 | * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n |
| 7430 | * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n |
| 7431 | * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n |
| 7432 | * | | | | | | | | | | |
| 7433 | * | | | | | | | | +-> next hdr_end <--+ |
| 7434 | * | | | | | | | +------------> scav |
| 7435 | * | | | | | | +--------------> val_end |
| 7436 | * | | | | | +--------------------> val_beg |
| 7437 | * | | | | +----------------------> equal |
| 7438 | * | | | +------------------------> att_end |
| 7439 | * | | +----------------------------> att_beg |
| 7440 | * | +------------------------------> prev |
| 7441 | * +-----------------------------------------> hdr_beg |
| 7442 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7443 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7444 | for (; prev < hdr_end; prev = next) { |
| 7445 | /* Iterate through all cookies on this line */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7446 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7447 | /* find att_beg */ |
| 7448 | att_beg = prev + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 7449 | while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg)) |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7450 | att_beg++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7451 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7452 | /* find att_end : this is the first character after the last non |
| 7453 | * space before the equal. It may be equal to hdr_end. |
| 7454 | */ |
| 7455 | equal = att_end = att_beg; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7456 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7457 | while (equal < hdr_end) { |
| 7458 | if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ',')) |
| 7459 | break; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 7460 | if (HTTP_IS_SPHT(*equal++)) |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7461 | continue; |
| 7462 | att_end = equal; |
| 7463 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7464 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7465 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 7466 | * is between <att_beg> and <equal>, both may be identical. |
| 7467 | */ |
| 7468 | |
| 7469 | /* look for end of cookie if there is an equal sign */ |
| 7470 | if (equal < hdr_end && *equal == '=') { |
| 7471 | /* look for the beginning of the value */ |
| 7472 | val_beg = equal + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 7473 | while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg)) |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7474 | val_beg++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7475 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7476 | /* find the end of the value, respecting quotes */ |
| 7477 | next = find_cookie_value_end(val_beg, hdr_end); |
| 7478 | |
| 7479 | /* make val_end point to the first white space or delimitor after the value */ |
| 7480 | val_end = next; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 7481 | while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1))) |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7482 | val_end--; |
| 7483 | } else { |
| 7484 | /* <equal> points to next comma, semi-colon or EOL */ |
| 7485 | val_beg = val_end = next = equal; |
| 7486 | } |
| 7487 | |
| 7488 | if (next < hdr_end) { |
| 7489 | /* Set-Cookie2 supports multiple cookies, and <next> points to |
| 7490 | * a colon or semi-colon before the end. So skip all attr-value |
| 7491 | * pairs and look for the next comma. For Set-Cookie, since |
| 7492 | * commas are permitted in values, skip to the end. |
| 7493 | */ |
| 7494 | if (is_cookie2) |
| 7495 | next = find_hdr_value_end(next, hdr_end); |
| 7496 | else |
| 7497 | next = hdr_end; |
| 7498 | } |
| 7499 | |
| 7500 | /* Now everything is as on the diagram above */ |
| 7501 | |
| 7502 | /* Ignore cookies with no equal sign */ |
| 7503 | if (equal == val_end) |
| 7504 | continue; |
| 7505 | |
| 7506 | /* If there are spaces around the equal sign, we need to |
| 7507 | * strip them otherwise we'll get trouble for cookie captures, |
| 7508 | * or even for rewrites. Since this happens extremely rarely, |
| 7509 | * it does not hurt performance. |
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 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 7512 | int stripped_before = 0; |
| 7513 | int stripped_after = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7514 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7515 | if (att_end != equal) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7516 | stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7517 | equal += stripped_before; |
| 7518 | val_beg += stripped_before; |
| 7519 | } |
| 7520 | |
| 7521 | if (val_beg > equal + 1) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7522 | stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7523 | val_beg += stripped_after; |
| 7524 | stripped_before += stripped_after; |
| 7525 | } |
| 7526 | |
| 7527 | val_end += stripped_before; |
| 7528 | next += stripped_before; |
| 7529 | hdr_end += stripped_before; |
| 7530 | hdr_next += stripped_before; |
| 7531 | cur_hdr->len += stripped_before; |
Willy Tarreau | 1fc1f45 | 2011-04-07 22:35:37 +0200 | [diff] [blame] | 7532 | http_msg_move_end(&txn->rsp, stripped_before); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7533 | } |
| 7534 | |
| 7535 | /* First, let's see if we want to capture this cookie. We check |
| 7536 | * that we don't already have a server side cookie, because we |
| 7537 | * can only capture one. Also as an optimisation, we ignore |
| 7538 | * cookies shorter than the declared name. |
| 7539 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7540 | if (sess->fe->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 7541 | txn->srv_cookie == NULL && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7542 | (val_end - att_beg >= sess->fe->capture_namelen) && |
| 7543 | memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7544 | int log_len = val_end - att_beg; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7545 | if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 7546 | ha_alert("HTTP logging : out of memory.\n"); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7547 | } |
Willy Tarreau | f70fc75 | 2010-11-19 11:27:18 +0100 | [diff] [blame] | 7548 | else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7549 | if (log_len > sess->fe->capture_len) |
| 7550 | log_len = sess->fe->capture_len; |
Willy Tarreau | f70fc75 | 2010-11-19 11:27:18 +0100 | [diff] [blame] | 7551 | memcpy(txn->srv_cookie, att_beg, log_len); |
| 7552 | txn->srv_cookie[log_len] = 0; |
| 7553 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7554 | } |
| 7555 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7556 | srv = objt_server(s->target); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7557 | /* now check if we need to process it for persistence */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 7558 | if (!(s->flags & SF_IGNORE_PRST) && |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7559 | (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) && |
| 7560 | (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) { |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7561 | /* assume passive cookie by default */ |
| 7562 | txn->flags &= ~TX_SCK_MASK; |
| 7563 | txn->flags |= TX_SCK_FOUND; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7564 | |
| 7565 | /* If the cookie is in insert mode on a known server, we'll delete |
| 7566 | * this occurrence because we'll insert another one later. |
| 7567 | * 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] | 7568 | * a direct access. |
| 7569 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7570 | if (s->be->ck_opts & PR_CK_PSV) { |
Willy Tarreau | ba4c5be | 2010-10-23 12:46:42 +0200 | [diff] [blame] | 7571 | /* The "preserve" flag was set, we don't want to touch the |
| 7572 | * server's cookie. |
| 7573 | */ |
| 7574 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7575 | else if ((srv && (s->be->ck_opts & PR_CK_INS)) || |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 7576 | ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7577 | /* this cookie must be deleted */ |
| 7578 | if (*prev == ':' && next == hdr_end) { |
| 7579 | /* whole header */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7580 | delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7581 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 7582 | txn->hdr_idx.used--; |
| 7583 | cur_hdr->len = 0; |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 7584 | cur_idx = old_idx; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7585 | hdr_next += delta; |
| 7586 | http_msg_move_end(&txn->rsp, delta); |
| 7587 | /* note: while both invalid now, <next> and <hdr_end> |
| 7588 | * are still equal, so the for() will stop as expected. |
| 7589 | */ |
| 7590 | } else { |
| 7591 | /* just remove the value */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7592 | int delta = del_hdr_value(res->buf, &prev, next); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7593 | next = prev; |
| 7594 | hdr_end += delta; |
| 7595 | hdr_next += delta; |
| 7596 | cur_hdr->len += delta; |
| 7597 | http_msg_move_end(&txn->rsp, delta); |
| 7598 | } |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7599 | txn->flags &= ~TX_SCK_MASK; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7600 | txn->flags |= TX_SCK_DELETED; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7601 | /* and go on with next cookie */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7602 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7603 | else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7604 | /* replace bytes val_beg->val_end with the cookie name associated |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7605 | * with this server since we know it. |
| 7606 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7607 | 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] | 7608 | next += delta; |
| 7609 | hdr_end += delta; |
| 7610 | hdr_next += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7611 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7612 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7613 | |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7614 | txn->flags &= ~TX_SCK_MASK; |
| 7615 | txn->flags |= TX_SCK_REPLACED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7616 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7617 | else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7618 | /* insert the cookie name associated with this server |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7619 | * before existing cookie, and insert a delimiter between them.. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7620 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7621 | 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] | 7622 | next += delta; |
| 7623 | hdr_end += delta; |
| 7624 | hdr_next += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7625 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7626 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7627 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 7628 | val_beg[srv->cklen] = COOKIE_DELIM; |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7629 | txn->flags &= ~TX_SCK_MASK; |
| 7630 | txn->flags |= TX_SCK_REPLACED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7631 | } |
| 7632 | } |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7633 | /* that's done for this cookie, check the next one on the same |
| 7634 | * line when next != hdr_end (only if is_cookie2). |
| 7635 | */ |
| 7636 | } |
| 7637 | /* check next header */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7638 | old_idx = cur_idx; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7639 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7640 | } |
| 7641 | |
| 7642 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7643 | /* |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7644 | * Check if response is cacheable or not. Updates s->flags. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7645 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7646 | void check_response_for_cacheability(struct stream *s, struct channel *rtr) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7647 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7648 | struct http_txn *txn = s->txn; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7649 | char *p1, *p2; |
| 7650 | |
| 7651 | char *cur_ptr, *cur_end, *cur_next; |
| 7652 | int cur_idx; |
| 7653 | |
Willy Tarreau | 5df5187 | 2007-11-25 16:20:08 +0100 | [diff] [blame] | 7654 | if (!(txn->flags & TX_CACHEABLE)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7655 | return; |
| 7656 | |
| 7657 | /* Iterate through the headers. |
| 7658 | * we start with the start line. |
| 7659 | */ |
| 7660 | cur_idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7661 | cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7662 | |
| 7663 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 7664 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7665 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7666 | |
| 7667 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 7668 | cur_ptr = cur_next; |
| 7669 | cur_end = cur_ptr + cur_hdr->len; |
| 7670 | cur_next = cur_end + cur_hdr->cr + 1; |
| 7671 | |
| 7672 | /* We have one full header between cur_ptr and cur_end, and the |
| 7673 | * next header starts at cur_next. We're only interested in |
| 7674 | * "Cookie:" headers. |
| 7675 | */ |
| 7676 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7677 | val = http_header_match2(cur_ptr, cur_end, "Pragma", 6); |
| 7678 | if (val) { |
| 7679 | if ((cur_end - (cur_ptr + val) >= 8) && |
| 7680 | strncasecmp(cur_ptr + val, "no-cache", 8) == 0) { |
| 7681 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 7682 | return; |
| 7683 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7684 | } |
| 7685 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7686 | val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13); |
| 7687 | if (!val) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7688 | continue; |
| 7689 | |
| 7690 | /* OK, right now we know we have a cache-control header at cur_ptr */ |
| 7691 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7692 | p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7693 | |
| 7694 | if (p1 >= cur_end) /* no more info */ |
| 7695 | continue; |
| 7696 | |
| 7697 | /* p1 is at the beginning of the value */ |
| 7698 | p2 = p1; |
| 7699 | |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 7700 | while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7701 | p2++; |
| 7702 | |
| 7703 | /* we have a complete value between p1 and p2 */ |
| 7704 | if (p2 < cur_end && *p2 == '=') { |
| 7705 | /* we have something of the form no-cache="set-cookie" */ |
| 7706 | if ((cur_end - p1 >= 21) && |
| 7707 | strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0 |
| 7708 | && (p1[20] == '"' || p1[20] == ',')) |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7709 | txn->flags &= ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7710 | continue; |
| 7711 | } |
| 7712 | |
| 7713 | /* OK, so we know that either p2 points to the end of string or to a comma */ |
| 7714 | if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) || |
Willy Tarreau | 5b15f90 | 2013-07-04 12:46:56 +0200 | [diff] [blame] | 7715 | ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) || |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7716 | ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) || |
| 7717 | ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) || |
| 7718 | ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7719 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7720 | return; |
| 7721 | } |
| 7722 | |
| 7723 | if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7724 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7725 | continue; |
| 7726 | } |
| 7727 | } |
| 7728 | } |
| 7729 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7730 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7731 | /* |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7732 | * 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] | 7733 | * for the current backend. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7734 | * |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7735 | * 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] | 7736 | * uri_auth field is valid. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7737 | * |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 7738 | * Returns 1 if stats should be provided, otherwise 0. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7739 | */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7740 | 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] | 7741 | { |
| 7742 | struct uri_auth *uri_auth = backend->uri_auth; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7743 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7744 | const char *uri = msg->chn->buf->p+ msg->sl.rq.u; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7745 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 7746 | if (!uri_auth) |
| 7747 | return 0; |
| 7748 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7749 | 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] | 7750 | return 0; |
| 7751 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7752 | /* check URI size */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7753 | if (uri_auth->uri_len > msg->sl.rq.u_l) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7754 | return 0; |
| 7755 | |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 7756 | if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7757 | return 0; |
| 7758 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7759 | return 1; |
| 7760 | } |
| 7761 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7762 | /* |
| 7763 | * 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] | 7764 | * By default it tries to report the error position as msg->err_pos. However if |
| 7765 | * this one is not set, it will then report msg->next, which is the last known |
| 7766 | * 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] | 7767 | * displays buffers as a contiguous area starting at buf->p. |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7768 | */ |
Emeric Brun | 8c1aaa2 | 2017-06-15 11:30:06 +0200 | [diff] [blame] | 7769 | 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] | 7770 | struct http_msg *msg, |
Willy Tarreau | 0da5b3b | 2017-09-21 09:30:46 +0200 | [diff] [blame] | 7771 | enum h1_state state, struct proxy *other_end) |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7772 | { |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 7773 | struct session *sess = strm_sess(s); |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 7774 | struct channel *chn = msg->chn; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 7775 | int len1, len2; |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 7776 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 7777 | HA_SPIN_LOCK(PROXY_LOCK, &proxy->lock); |
Willy Tarreau | f3764b7 | 2016-03-31 13:45:10 +0200 | [diff] [blame] | 7778 | es->len = MIN(chn->buf->i, global.tune.bufsize); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7779 | len1 = chn->buf->data + chn->buf->size - chn->buf->p; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 7780 | len1 = MIN(len1, es->len); |
| 7781 | len2 = es->len - len1; /* remaining data if buffer wraps */ |
| 7782 | |
Willy Tarreau | f3764b7 | 2016-03-31 13:45:10 +0200 | [diff] [blame] | 7783 | if (!es->buf) |
| 7784 | es->buf = malloc(global.tune.bufsize); |
| 7785 | |
| 7786 | if (es->buf) { |
| 7787 | memcpy(es->buf, chn->buf->p, len1); |
| 7788 | if (len2) |
| 7789 | memcpy(es->buf + len1, chn->buf->data, len2); |
| 7790 | } |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 7791 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7792 | if (msg->err_pos >= 0) |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 7793 | es->pos = msg->err_pos; |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 7794 | else |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 7795 | es->pos = msg->next; |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 7796 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7797 | es->when = date; // user-visible date |
| 7798 | es->sid = s->uniq_id; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 7799 | es->srv = objt_server(s->target); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7800 | es->oe = other_end; |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 7801 | if (objt_conn(sess->origin)) |
| 7802 | es->src = __objt_conn(sess->origin)->addr.from; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7803 | else |
| 7804 | memset(&es->src, 0, sizeof(es->src)); |
| 7805 | |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 7806 | es->state = state; |
Willy Tarreau | 10479e4 | 2010-12-12 14:00:34 +0100 | [diff] [blame] | 7807 | es->ev_id = error_snapshot_id++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 7808 | es->b_flags = chn->flags; |
Willy Tarreau | d04b1bc | 2012-05-08 11:03:10 +0200 | [diff] [blame] | 7809 | es->s_flags = s->flags; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7810 | es->t_flags = s->txn->flags; |
Willy Tarreau | d04b1bc | 2012-05-08 11:03:10 +0200 | [diff] [blame] | 7811 | es->m_flags = msg->flags; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7812 | es->b_out = chn->buf->o; |
| 7813 | es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 7814 | es->b_tot = chn->total; |
Willy Tarreau | d04b1bc | 2012-05-08 11:03:10 +0200 | [diff] [blame] | 7815 | es->m_clen = msg->chunk_len; |
| 7816 | es->m_blen = msg->body_len; |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 7817 | HA_SPIN_UNLOCK(PROXY_LOCK, &proxy->lock); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7818 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7819 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7820 | /* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of |
| 7821 | * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is |
| 7822 | * performed over the whole headers. Otherwise it must contain a valid header |
| 7823 | * context, initialised with ctx->idx=0 for the first lookup in a series. If |
| 7824 | * <occ> is positive or null, occurrence #occ from the beginning (or last ctx) |
| 7825 | * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less |
| 7826 | * 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] | 7827 | * -1. The value fetch stops at commas, so this function is suited for use with |
| 7828 | * list headers. |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7829 | * 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] | 7830 | */ |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 7831 | 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] | 7832 | struct hdr_idx *idx, int occ, |
| 7833 | struct hdr_ctx *ctx, char **vptr, int *vlen) |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7834 | { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7835 | struct hdr_ctx local_ctx; |
| 7836 | char *ptr_hist[MAX_HDR_HISTORY]; |
| 7837 | int len_hist[MAX_HDR_HISTORY]; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7838 | unsigned int hist_ptr; |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7839 | int found; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7840 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7841 | if (!ctx) { |
| 7842 | local_ctx.idx = 0; |
| 7843 | ctx = &local_ctx; |
| 7844 | } |
| 7845 | |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7846 | if (occ >= 0) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7847 | /* search from the beginning */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7848 | while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7849 | occ--; |
| 7850 | if (occ <= 0) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7851 | *vptr = ctx->line + ctx->val; |
| 7852 | *vlen = ctx->vlen; |
| 7853 | return 1; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7854 | } |
| 7855 | } |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7856 | return 0; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7857 | } |
| 7858 | |
| 7859 | /* negative occurrence, we scan all the list then walk back */ |
| 7860 | if (-occ > MAX_HDR_HISTORY) |
| 7861 | return 0; |
| 7862 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7863 | found = hist_ptr = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7864 | while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7865 | ptr_hist[hist_ptr] = ctx->line + ctx->val; |
| 7866 | len_hist[hist_ptr] = ctx->vlen; |
| 7867 | if (++hist_ptr >= MAX_HDR_HISTORY) |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7868 | hist_ptr = 0; |
| 7869 | found++; |
| 7870 | } |
| 7871 | if (-occ > found) |
| 7872 | return 0; |
| 7873 | /* 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] | 7874 | * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have |
| 7875 | * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ] |
| 7876 | * to remain in the 0..9 range. |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7877 | */ |
Willy Tarreau | 67dad27 | 2013-06-12 22:27:44 +0200 | [diff] [blame] | 7878 | hist_ptr += occ + MAX_HDR_HISTORY; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7879 | if (hist_ptr >= MAX_HDR_HISTORY) |
| 7880 | hist_ptr -= MAX_HDR_HISTORY; |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7881 | *vptr = ptr_hist[hist_ptr]; |
| 7882 | *vlen = len_hist[hist_ptr]; |
| 7883 | return 1; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7884 | } |
| 7885 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 7886 | /* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of |
| 7887 | * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is |
| 7888 | * performed over the whole headers. Otherwise it must contain a valid header |
| 7889 | * context, initialised with ctx->idx=0 for the first lookup in a series. If |
| 7890 | * <occ> is positive or null, occurrence #occ from the beginning (or last ctx) |
| 7891 | * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less |
| 7892 | * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is |
| 7893 | * -1. This function differs from http_get_hdr() in that it only returns full |
| 7894 | * line header values and does not stop at commas. |
| 7895 | * The return value is 0 if nothing was found, or non-zero otherwise. |
| 7896 | */ |
| 7897 | unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen, |
| 7898 | struct hdr_idx *idx, int occ, |
| 7899 | struct hdr_ctx *ctx, char **vptr, int *vlen) |
| 7900 | { |
| 7901 | struct hdr_ctx local_ctx; |
| 7902 | char *ptr_hist[MAX_HDR_HISTORY]; |
| 7903 | int len_hist[MAX_HDR_HISTORY]; |
| 7904 | unsigned int hist_ptr; |
| 7905 | int found; |
| 7906 | |
| 7907 | if (!ctx) { |
| 7908 | local_ctx.idx = 0; |
| 7909 | ctx = &local_ctx; |
| 7910 | } |
| 7911 | |
| 7912 | if (occ >= 0) { |
| 7913 | /* search from the beginning */ |
| 7914 | while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
| 7915 | occ--; |
| 7916 | if (occ <= 0) { |
| 7917 | *vptr = ctx->line + ctx->val; |
| 7918 | *vlen = ctx->vlen; |
| 7919 | return 1; |
| 7920 | } |
| 7921 | } |
| 7922 | return 0; |
| 7923 | } |
| 7924 | |
| 7925 | /* negative occurrence, we scan all the list then walk back */ |
| 7926 | if (-occ > MAX_HDR_HISTORY) |
| 7927 | return 0; |
| 7928 | |
| 7929 | found = hist_ptr = 0; |
| 7930 | while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
| 7931 | ptr_hist[hist_ptr] = ctx->line + ctx->val; |
| 7932 | len_hist[hist_ptr] = ctx->vlen; |
| 7933 | if (++hist_ptr >= MAX_HDR_HISTORY) |
| 7934 | hist_ptr = 0; |
| 7935 | found++; |
| 7936 | } |
| 7937 | if (-occ > found) |
| 7938 | return 0; |
Nenad Merdanovic | 69ad4b9 | 2016-03-29 13:14:30 +0200 | [diff] [blame] | 7939 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 7940 | /* 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] | 7941 | * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have |
| 7942 | * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ] |
| 7943 | * to remain in the 0..9 range. |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 7944 | */ |
Nenad Merdanovic | 69ad4b9 | 2016-03-29 13:14:30 +0200 | [diff] [blame] | 7945 | hist_ptr += occ + MAX_HDR_HISTORY; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 7946 | if (hist_ptr >= MAX_HDR_HISTORY) |
| 7947 | hist_ptr -= MAX_HDR_HISTORY; |
| 7948 | *vptr = ptr_hist[hist_ptr]; |
| 7949 | *vlen = len_hist[hist_ptr]; |
| 7950 | return 1; |
| 7951 | } |
| 7952 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 7953 | /* |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 7954 | * Print a debug line with a header. Always stop at the first CR or LF char, |
| 7955 | * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an |
| 7956 | * arrow is printed after the line which contains the pointer. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7957 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7958 | 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] | 7959 | { |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 7960 | struct session *sess = strm_sess(s); |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7961 | int max; |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 7962 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7963 | 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] | 7964 | dir, |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 7965 | 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] | 7966 | 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] | 7967 | |
| 7968 | for (max = 0; start + max < end; max++) |
| 7969 | if (start[max] == '\r' || start[max] == '\n') |
| 7970 | break; |
| 7971 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7972 | UBOUND(max, trash.size - trash.len - 3); |
| 7973 | trash.len += strlcpy2(trash.str + trash.len, start, max + 1); |
| 7974 | trash.str[trash.len++] = '\n'; |
Willy Tarreau | 89efaed | 2013-12-13 15:14:55 +0100 | [diff] [blame] | 7975 | shut_your_big_mouth_gcc(write(1, trash.str, trash.len)); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7976 | } |
| 7977 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7978 | |
| 7979 | /* Allocate a new HTTP transaction for stream <s> unless there is one already. |
| 7980 | * The hdr_idx is allocated as well. In case of allocation failure, everything |
| 7981 | * allocated is freed and NULL is returned. Otherwise the new transaction is |
| 7982 | * assigned to the stream and returned. |
| 7983 | */ |
| 7984 | struct http_txn *http_alloc_txn(struct stream *s) |
| 7985 | { |
| 7986 | struct http_txn *txn = s->txn; |
| 7987 | |
| 7988 | if (txn) |
| 7989 | return txn; |
| 7990 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7991 | txn = pool_alloc(pool_head_http_txn); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7992 | if (!txn) |
| 7993 | return txn; |
| 7994 | |
| 7995 | txn->hdr_idx.size = global.tune.max_http_hdr; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7996 | txn->hdr_idx.v = pool_alloc(pool_head_hdr_idx); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7997 | if (!txn->hdr_idx.v) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7998 | pool_free(pool_head_http_txn, txn); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7999 | return NULL; |
| 8000 | } |
| 8001 | |
| 8002 | s->txn = txn; |
| 8003 | return txn; |
| 8004 | } |
| 8005 | |
Thierry FOURNIER | fd50f0b | 2015-09-25 18:53:18 +0200 | [diff] [blame] | 8006 | void http_txn_reset_req(struct http_txn *txn) |
| 8007 | { |
| 8008 | txn->req.flags = 0; |
| 8009 | txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */ |
| 8010 | txn->req.next = 0; |
| 8011 | txn->req.chunk_len = 0LL; |
| 8012 | txn->req.body_len = 0LL; |
| 8013 | txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */ |
| 8014 | } |
| 8015 | |
| 8016 | void http_txn_reset_res(struct http_txn *txn) |
| 8017 | { |
| 8018 | txn->rsp.flags = 0; |
| 8019 | txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */ |
| 8020 | txn->rsp.next = 0; |
| 8021 | txn->rsp.chunk_len = 0LL; |
| 8022 | txn->rsp.body_len = 0LL; |
| 8023 | txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */ |
| 8024 | } |
| 8025 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8026 | /* |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8027 | * 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] | 8028 | * the required fields are properly allocated and that we only need to (re)init |
| 8029 | * them. This should be used before processing any new request. |
| 8030 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8031 | void http_init_txn(struct stream *s) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8032 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8033 | struct http_txn *txn = s->txn; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 8034 | struct proxy *fe = strm_fe(s); |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8035 | |
| 8036 | txn->flags = 0; |
| 8037 | txn->status = -1; |
| 8038 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 8039 | txn->cookie_first_date = 0; |
| 8040 | txn->cookie_last_date = 0; |
| 8041 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8042 | txn->srv_cookie = NULL; |
| 8043 | txn->cli_cookie = NULL; |
| 8044 | txn->uri = NULL; |
| 8045 | |
Thierry FOURNIER | fd50f0b | 2015-09-25 18:53:18 +0200 | [diff] [blame] | 8046 | http_txn_reset_req(txn); |
| 8047 | http_txn_reset_res(txn); |
| 8048 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 8049 | txn->req.chn = &s->req; |
| 8050 | txn->rsp.chn = &s->res; |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 8051 | |
| 8052 | txn->auth.method = HTTP_AUTH_UNKNOWN; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8053 | |
| 8054 | txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */ |
| 8055 | if (fe->options2 & PR_O2_REQBUG_OK) |
| 8056 | txn->req.err_pos = -1; /* let buggy requests pass */ |
| 8057 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8058 | if (txn->hdr_idx.v) |
| 8059 | hdr_idx_init(&txn->hdr_idx); |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 8060 | |
| 8061 | vars_init(&s->vars_txn, SCOPE_TXN); |
| 8062 | vars_init(&s->vars_reqres, SCOPE_REQ); |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8063 | } |
| 8064 | |
| 8065 | /* to be used at the end of a transaction */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8066 | void http_end_txn(struct stream *s) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8067 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8068 | struct http_txn *txn = s->txn; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 8069 | struct proxy *fe = strm_fe(s); |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8070 | |
| 8071 | /* these ones will have been dynamically allocated */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 8072 | pool_free(pool_head_requri, txn->uri); |
| 8073 | pool_free(pool_head_capture, txn->cli_cookie); |
| 8074 | pool_free(pool_head_capture, txn->srv_cookie); |
| 8075 | pool_free(pool_head_uniqueid, s->unique_id); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 8076 | |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 8077 | s->unique_id = NULL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8078 | txn->uri = NULL; |
| 8079 | txn->srv_cookie = NULL; |
| 8080 | txn->cli_cookie = NULL; |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8081 | |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 8082 | if (s->req_cap) { |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8083 | struct cap_hdr *h; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 8084 | for (h = fe->req_cap; h; h = h->next) |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 8085 | pool_free(h->pool, s->req_cap[h->index]); |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 8086 | memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *)); |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8087 | } |
| 8088 | |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 8089 | if (s->res_cap) { |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8090 | struct cap_hdr *h; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 8091 | for (h = fe->rsp_cap; h; h = h->next) |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 8092 | pool_free(h->pool, s->res_cap[h->index]); |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 8093 | memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *)); |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8094 | } |
| 8095 | |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 8096 | vars_prune(&s->vars_txn, s->sess, s); |
| 8097 | vars_prune(&s->vars_reqres, s->sess, s); |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8098 | } |
| 8099 | |
| 8100 | /* 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] | 8101 | void http_reset_txn(struct stream *s) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8102 | { |
| 8103 | http_end_txn(s); |
| 8104 | http_init_txn(s); |
| 8105 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 8106 | /* reinitialise the current rule list pointer to NULL. We are sure that |
| 8107 | * any rulelist match the NULL pointer. |
| 8108 | */ |
| 8109 | s->current_rule_list = NULL; |
| 8110 | |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 8111 | s->be = strm_fe(s); |
| 8112 | s->logs.logwait = strm_fe(s)->to_log; |
Willy Tarreau | abcd514 | 2013-06-11 17:18:02 +0200 | [diff] [blame] | 8113 | s->logs.level = 0; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8114 | stream_del_srv_conn(s); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 8115 | s->target = NULL; |
Emeric Brun | b982a3d | 2010-01-04 15:45:53 +0100 | [diff] [blame] | 8116 | /* re-init store persistence */ |
| 8117 | s->store_count = 0; |
Willy Tarreau | 1f0da24 | 2014-01-25 11:01:50 +0100 | [diff] [blame] | 8118 | s->uniq_id = global.req_count++; |
Emeric Brun | b982a3d | 2010-01-04 15:45:53 +0100 | [diff] [blame] | 8119 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8120 | s->pend_pos = NULL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8121 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 8122 | s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */ |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8123 | |
Willy Tarreau | 739cfba | 2010-01-25 23:11:14 +0100 | [diff] [blame] | 8124 | /* We must trim any excess data from the response buffer, because we |
| 8125 | * may have blocked an invalid response from a server that we don't |
| 8126 | * want to accidentely forward once we disable the analysers, nor do |
| 8127 | * we want those data to come along with next response. A typical |
| 8128 | * example of such data would be from a buggy server responding to |
| 8129 | * a HEAD with some data, or sending more than the advertised |
| 8130 | * content-length. |
| 8131 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 8132 | if (unlikely(s->res.buf->i)) |
| 8133 | s->res.buf->i = 0; |
Willy Tarreau | 739cfba | 2010-01-25 23:11:14 +0100 | [diff] [blame] | 8134 | |
Christopher Faulet | c0c672a | 2017-03-28 11:51:33 +0200 | [diff] [blame] | 8135 | /* Now we can realign the response buffer */ |
| 8136 | buffer_realign(s->res.buf); |
| 8137 | |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 8138 | s->req.rto = strm_fe(s)->timeout.client; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 8139 | s->req.wto = TICK_ETERNITY; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8140 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 8141 | s->res.rto = TICK_ETERNITY; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 8142 | s->res.wto = strm_fe(s)->timeout.client; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8143 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 8144 | s->req.rex = TICK_ETERNITY; |
| 8145 | s->req.wex = TICK_ETERNITY; |
| 8146 | s->req.analyse_exp = TICK_ETERNITY; |
| 8147 | s->res.rex = TICK_ETERNITY; |
| 8148 | s->res.wex = TICK_ETERNITY; |
| 8149 | s->res.analyse_exp = TICK_ETERNITY; |
Hongbo Long | e39683c | 2017-03-10 18:41:51 +0100 | [diff] [blame] | 8150 | s->si[1].hcto = TICK_ETERNITY; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8151 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8152 | |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 8153 | void free_http_res_rules(struct list *r) |
| 8154 | { |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 8155 | struct act_rule *tr, *pr; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 8156 | |
| 8157 | list_for_each_entry_safe(pr, tr, r, list) { |
| 8158 | LIST_DEL(&pr->list); |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 8159 | regex_free(&pr->arg.hdr_add.re); |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 8160 | free(pr); |
| 8161 | } |
| 8162 | } |
| 8163 | |
| 8164 | void free_http_req_rules(struct list *r) |
| 8165 | { |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 8166 | struct act_rule *tr, *pr; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8167 | |
| 8168 | list_for_each_entry_safe(pr, tr, r, list) { |
| 8169 | LIST_DEL(&pr->list); |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8170 | if (pr->action == ACT_HTTP_REQ_AUTH) |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 8171 | free(pr->arg.auth.realm); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8172 | |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 8173 | regex_free(&pr->arg.hdr_add.re); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8174 | free(pr); |
| 8175 | } |
| 8176 | } |
| 8177 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8178 | /* parse an "http-request" rule */ |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 8179 | 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] | 8180 | { |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 8181 | struct act_rule *rule; |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 8182 | struct action_kw *custom = NULL; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8183 | int cur_arg; |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 8184 | char *error; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8185 | |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 8186 | rule = calloc(1, sizeof(*rule)); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8187 | if (!rule) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8188 | ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8189 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8190 | } |
| 8191 | |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 8192 | if (!strcmp(args[0], "allow")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8193 | rule->action = ACT_ACTION_ALLOW; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8194 | cur_arg = 1; |
Jarno Huuskonen | 800d176 | 2017-03-06 14:56:36 +0200 | [diff] [blame] | 8195 | } 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] | 8196 | int code; |
| 8197 | int hc; |
| 8198 | |
Jarno Huuskonen | 800d176 | 2017-03-06 14:56:36 +0200 | [diff] [blame] | 8199 | if (!strcmp(args[0], "tarpit")) { |
| 8200 | rule->action = ACT_HTTP_REQ_TARPIT; |
| 8201 | rule->deny_status = HTTP_ERR_500; |
| 8202 | } |
| 8203 | else { |
| 8204 | rule->action = ACT_ACTION_DENY; |
| 8205 | rule->deny_status = HTTP_ERR_403; |
| 8206 | } |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8207 | cur_arg = 1; |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 8208 | if (strcmp(args[cur_arg], "deny_status") == 0) { |
| 8209 | cur_arg++; |
| 8210 | if (!args[cur_arg]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8211 | ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing status code.\n", |
| 8212 | file, linenum, proxy_type_str(proxy), proxy->id, args[0]); |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 8213 | goto out_err; |
| 8214 | } |
| 8215 | |
| 8216 | code = atol(args[cur_arg]); |
| 8217 | cur_arg++; |
| 8218 | for (hc = 0; hc < HTTP_ERR_SIZE; hc++) { |
| 8219 | if (http_err_codes[hc] == code) { |
| 8220 | rule->deny_status = hc; |
| 8221 | break; |
| 8222 | } |
| 8223 | } |
| 8224 | |
| 8225 | if (hc >= HTTP_ERR_SIZE) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8226 | ha_warning("parsing [%s:%d] : status code %d not handled, using default code %d.\n", |
| 8227 | file, linenum, code, http_err_codes[rule->deny_status]); |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 8228 | } |
| 8229 | } |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8230 | } else if (!strcmp(args[0], "auth")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8231 | rule->action = ACT_HTTP_REQ_AUTH; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8232 | cur_arg = 1; |
| 8233 | |
| 8234 | while(*args[cur_arg]) { |
| 8235 | if (!strcmp(args[cur_arg], "realm")) { |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 8236 | rule->arg.auth.realm = strdup(args[cur_arg + 1]); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8237 | cur_arg+=2; |
| 8238 | continue; |
| 8239 | } else |
| 8240 | break; |
| 8241 | } |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 8242 | } else if (!strcmp(args[0], "set-nice")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8243 | rule->action = ACT_HTTP_SET_NICE; |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 8244 | cur_arg = 1; |
| 8245 | |
| 8246 | if (!*args[cur_arg] || |
| 8247 | (*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] | 8248 | ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n", |
| 8249 | file, linenum, args[0]); |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 8250 | goto out_err; |
| 8251 | } |
| 8252 | rule->arg.nice = atoi(args[cur_arg]); |
| 8253 | if (rule->arg.nice < -1024) |
| 8254 | rule->arg.nice = -1024; |
| 8255 | else if (rule->arg.nice > 1024) |
| 8256 | rule->arg.nice = 1024; |
| 8257 | cur_arg++; |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 8258 | } else if (!strcmp(args[0], "set-tos")) { |
| 8259 | #ifdef IP_TOS |
| 8260 | char *err; |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8261 | rule->action = ACT_HTTP_SET_TOS; |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 8262 | cur_arg = 1; |
| 8263 | |
| 8264 | if (!*args[cur_arg] || |
| 8265 | (*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] | 8266 | ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n", |
| 8267 | file, linenum, args[0]); |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 8268 | goto out_err; |
| 8269 | } |
| 8270 | |
| 8271 | rule->arg.tos = strtol(args[cur_arg], &err, 0); |
| 8272 | if (err && *err != '\0') { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8273 | ha_alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n", |
| 8274 | file, linenum, err, args[0]); |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 8275 | goto out_err; |
| 8276 | } |
| 8277 | cur_arg++; |
| 8278 | #else |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8279 | 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] | 8280 | goto out_err; |
| 8281 | #endif |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 8282 | } else if (!strcmp(args[0], "set-mark")) { |
| 8283 | #ifdef SO_MARK |
| 8284 | char *err; |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8285 | rule->action = ACT_HTTP_SET_MARK; |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 8286 | cur_arg = 1; |
| 8287 | |
| 8288 | if (!*args[cur_arg] || |
| 8289 | (*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] | 8290 | ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n", |
| 8291 | file, linenum, args[0]); |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 8292 | goto out_err; |
| 8293 | } |
| 8294 | |
| 8295 | rule->arg.mark = strtoul(args[cur_arg], &err, 0); |
| 8296 | if (err && *err != '\0') { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8297 | ha_alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n", |
| 8298 | file, linenum, err, args[0]); |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 8299 | goto out_err; |
| 8300 | } |
| 8301 | cur_arg++; |
| 8302 | global.last_checks |= LSTCHK_NETADM; |
| 8303 | #else |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8304 | 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] | 8305 | goto out_err; |
| 8306 | #endif |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 8307 | } else if (!strcmp(args[0], "set-log-level")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8308 | rule->action = ACT_HTTP_SET_LOGL; |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 8309 | cur_arg = 1; |
| 8310 | |
| 8311 | if (!*args[cur_arg] || |
| 8312 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 8313 | bad_log_level: |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8314 | ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n", |
| 8315 | file, linenum, args[0]); |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 8316 | goto out_err; |
| 8317 | } |
| 8318 | if (strcmp(args[cur_arg], "silent") == 0) |
| 8319 | rule->arg.loglevel = -1; |
| 8320 | else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0) |
| 8321 | goto bad_log_level; |
| 8322 | cur_arg++; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 8323 | } 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] | 8324 | 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] | 8325 | cur_arg = 1; |
| 8326 | |
Willy Tarreau | 8d1c516 | 2013-04-03 14:13:58 +0200 | [diff] [blame] | 8327 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 8328 | (*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] | 8329 | ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n", |
| 8330 | file, linenum, args[0]); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8331 | goto out_err; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 8332 | } |
| 8333 | |
| 8334 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 8335 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 8336 | LIST_INIT(&rule->arg.hdr_add.fmt); |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 8337 | |
Thierry FOURNIER | 1c0054f | 2013-11-20 15:09:52 +0100 | [diff] [blame] | 8338 | proxy->conf.args.ctx = ARGC_HRQ; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8339 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8340 | 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] | 8341 | (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] | 8342 | ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n", |
| 8343 | file, linenum, args[0], error); |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8344 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8345 | goto out_err; |
| 8346 | } |
Willy Tarreau | 59ad1a2 | 2014-01-29 14:39:58 +0100 | [diff] [blame] | 8347 | free(proxy->conf.lfs_file); |
| 8348 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 8349 | proxy->conf.lfs_line = proxy->conf.args.line; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 8350 | cur_arg += 2; |
Willy Tarreau | b854392 | 2014-06-17 18:58:26 +0200 | [diff] [blame] | 8351 | } 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] | 8352 | 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] | 8353 | cur_arg = 1; |
| 8354 | |
| 8355 | if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] || |
Baptiste Assmann | 92df370 | 2014-06-24 11:10:00 +0200 | [diff] [blame] | 8356 | (*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] | 8357 | ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n", |
| 8358 | file, linenum, args[0]); |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 8359 | goto out_err; |
| 8360 | } |
| 8361 | |
| 8362 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 8363 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 8364 | LIST_INIT(&rule->arg.hdr_add.fmt); |
| 8365 | |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 8366 | error = NULL; |
| 8367 | 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] | 8368 | ha_alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum, |
| 8369 | args[cur_arg + 1], error); |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 8370 | free(error); |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 8371 | goto out_err; |
| 8372 | } |
| 8373 | |
| 8374 | proxy->conf.args.ctx = ARGC_HRQ; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8375 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8376 | 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] | 8377 | (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] | 8378 | ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n", |
| 8379 | file, linenum, args[0], error); |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8380 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8381 | goto out_err; |
| 8382 | } |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 8383 | |
| 8384 | free(proxy->conf.lfs_file); |
| 8385 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 8386 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 8387 | cur_arg += 3; |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 8388 | } else if (strcmp(args[0], "del-header") == 0) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8389 | rule->action = ACT_HTTP_DEL_HDR; |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 8390 | cur_arg = 1; |
| 8391 | |
| 8392 | if (!*args[cur_arg] || |
| 8393 | (*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] | 8394 | ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 8395 | file, linenum, args[0]); |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 8396 | goto out_err; |
| 8397 | } |
| 8398 | |
| 8399 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 8400 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 8401 | |
| 8402 | proxy->conf.args.ctx = ARGC_HRQ; |
| 8403 | free(proxy->conf.lfs_file); |
| 8404 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 8405 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 8406 | cur_arg += 1; |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 8407 | } else if (strncmp(args[0], "track-sc", 8) == 0 && |
| 8408 | args[0][9] == '\0' && args[0][8] >= '0' && |
Willy Tarreau | e1cfc1f | 2014-10-17 11:53:05 +0200 | [diff] [blame] | 8409 | args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */ |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 8410 | struct sample_expr *expr; |
| 8411 | unsigned int where; |
| 8412 | char *err = NULL; |
| 8413 | |
| 8414 | cur_arg = 1; |
| 8415 | proxy->conf.args.ctx = ARGC_TRK; |
| 8416 | |
| 8417 | expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args); |
| 8418 | if (!expr) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8419 | ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n", |
| 8420 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], err); |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 8421 | free(err); |
| 8422 | goto out_err; |
| 8423 | } |
| 8424 | |
| 8425 | where = 0; |
| 8426 | if (proxy->cap & PR_CAP_FE) |
| 8427 | where |= SMP_VAL_FE_HRQ_HDR; |
| 8428 | if (proxy->cap & PR_CAP_BE) |
| 8429 | where |= SMP_VAL_BE_HRQ_HDR; |
| 8430 | |
| 8431 | if (!(expr->fetch->val & where)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8432 | ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :" |
| 8433 | " fetch method '%s' extracts information from '%s', none of which is available here.\n", |
| 8434 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], |
| 8435 | args[cur_arg-1], sample_src_names(expr->fetch->use)); |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 8436 | free(expr); |
| 8437 | goto out_err; |
| 8438 | } |
| 8439 | |
| 8440 | if (strcmp(args[cur_arg], "table") == 0) { |
| 8441 | cur_arg++; |
| 8442 | if (!args[cur_arg]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8443 | ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing table name.\n", |
| 8444 | file, linenum, proxy_type_str(proxy), proxy->id, args[0]); |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 8445 | free(expr); |
| 8446 | goto out_err; |
| 8447 | } |
| 8448 | /* we copy the table name for now, it will be resolved later */ |
Thierry FOURNIER | 5ec63e0 | 2015-08-04 09:09:48 +0200 | [diff] [blame] | 8449 | rule->arg.trk_ctr.table.n = strdup(args[cur_arg]); |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 8450 | cur_arg++; |
| 8451 | } |
Thierry FOURNIER | 5ec63e0 | 2015-08-04 09:09:48 +0200 | [diff] [blame] | 8452 | rule->arg.trk_ctr.expr = expr; |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8453 | rule->action = ACT_ACTION_TRK_SC0 + args[0][8] - '0'; |
Christopher Faulet | 78880fb | 2017-09-18 14:43:55 +0200 | [diff] [blame] | 8454 | rule->check_ptr = check_trk_action; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8455 | } else if (strcmp(args[0], "redirect") == 0) { |
| 8456 | struct redirect_rule *redir; |
Willy Tarreau | 6d4890c | 2013-11-18 18:04:25 +0100 | [diff] [blame] | 8457 | char *errmsg = NULL; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8458 | |
Willy Tarreau | be4653b | 2015-05-28 15:26:58 +0200 | [diff] [blame] | 8459 | 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] | 8460 | ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n", |
| 8461 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8462 | goto out_err; |
| 8463 | } |
| 8464 | |
| 8465 | /* this redirect rule might already contain a parsed condition which |
| 8466 | * we'll pass to the http-request rule. |
| 8467 | */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8468 | rule->action = ACT_HTTP_REDIR; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8469 | rule->arg.redir = redir; |
| 8470 | rule->cond = redir->cond; |
| 8471 | redir->cond = NULL; |
| 8472 | cur_arg = 2; |
| 8473 | return rule; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8474 | } else if (strncmp(args[0], "add-acl", 7) == 0) { |
| 8475 | /* http-request add-acl(<reference (acl name)>) <key pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8476 | rule->action = ACT_HTTP_ADD_ACL; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8477 | /* |
| 8478 | * '+ 8' for 'add-acl(' |
| 8479 | * '- 9' for 'add-acl(' + trailing ')' |
| 8480 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 8481 | 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] | 8482 | |
| 8483 | cur_arg = 1; |
| 8484 | |
| 8485 | if (!*args[cur_arg] || |
| 8486 | (*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] | 8487 | ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 8488 | file, linenum, args[0]); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8489 | goto out_err; |
| 8490 | } |
| 8491 | |
| 8492 | LIST_INIT(&rule->arg.map.key); |
| 8493 | proxy->conf.args.ctx = ARGC_HRQ; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8494 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8495 | 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] | 8496 | (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] | 8497 | ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n", |
| 8498 | file, linenum, args[0], error); |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8499 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8500 | goto out_err; |
| 8501 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8502 | free(proxy->conf.lfs_file); |
| 8503 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 8504 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 8505 | cur_arg += 1; |
| 8506 | } else if (strncmp(args[0], "del-acl", 7) == 0) { |
| 8507 | /* http-request del-acl(<reference (acl name)>) <key pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8508 | rule->action = ACT_HTTP_DEL_ACL; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8509 | /* |
| 8510 | * '+ 8' for 'del-acl(' |
| 8511 | * '- 9' for 'del-acl(' + trailing ')' |
| 8512 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 8513 | 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] | 8514 | |
| 8515 | cur_arg = 1; |
| 8516 | |
| 8517 | if (!*args[cur_arg] || |
| 8518 | (*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] | 8519 | ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 8520 | file, linenum, args[0]); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8521 | goto out_err; |
| 8522 | } |
| 8523 | |
| 8524 | LIST_INIT(&rule->arg.map.key); |
| 8525 | proxy->conf.args.ctx = ARGC_HRQ; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8526 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8527 | 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] | 8528 | (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] | 8529 | ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n", |
| 8530 | file, linenum, args[0], error); |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8531 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8532 | goto out_err; |
| 8533 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8534 | free(proxy->conf.lfs_file); |
| 8535 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 8536 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 8537 | cur_arg += 1; |
| 8538 | } else if (strncmp(args[0], "del-map", 7) == 0) { |
| 8539 | /* http-request del-map(<reference (map name)>) <key pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8540 | rule->action = ACT_HTTP_DEL_MAP; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8541 | /* |
| 8542 | * '+ 8' for 'del-map(' |
| 8543 | * '- 9' for 'del-map(' + trailing ')' |
| 8544 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 8545 | 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] | 8546 | |
| 8547 | cur_arg = 1; |
| 8548 | |
| 8549 | if (!*args[cur_arg] || |
| 8550 | (*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] | 8551 | ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 8552 | file, linenum, args[0]); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8553 | goto out_err; |
| 8554 | } |
| 8555 | |
| 8556 | LIST_INIT(&rule->arg.map.key); |
| 8557 | proxy->conf.args.ctx = ARGC_HRQ; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8558 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8559 | 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] | 8560 | (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] | 8561 | ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n", |
| 8562 | file, linenum, args[0], error); |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8563 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8564 | goto out_err; |
| 8565 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8566 | free(proxy->conf.lfs_file); |
| 8567 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 8568 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 8569 | cur_arg += 1; |
| 8570 | } else if (strncmp(args[0], "set-map", 7) == 0) { |
| 8571 | /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8572 | rule->action = ACT_HTTP_SET_MAP; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8573 | /* |
| 8574 | * '+ 8' for 'set-map(' |
| 8575 | * '- 9' for 'set-map(' + trailing ')' |
| 8576 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 8577 | 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] | 8578 | |
| 8579 | cur_arg = 1; |
| 8580 | |
| 8581 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 8582 | (*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] | 8583 | ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n", |
| 8584 | file, linenum, args[0]); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8585 | goto out_err; |
| 8586 | } |
| 8587 | |
| 8588 | LIST_INIT(&rule->arg.map.key); |
| 8589 | LIST_INIT(&rule->arg.map.value); |
| 8590 | proxy->conf.args.ctx = ARGC_HRQ; |
| 8591 | |
| 8592 | /* key pattern */ |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8593 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8594 | 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] | 8595 | (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] | 8596 | ha_alert("parsing [%s:%d]: 'http-request %s' key: %s.\n", |
| 8597 | file, linenum, args[0], error); |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8598 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8599 | goto out_err; |
| 8600 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8601 | |
| 8602 | /* value pattern */ |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8603 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8604 | 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] | 8605 | (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] | 8606 | ha_alert("parsing [%s:%d]: 'http-request %s' pattern: %s.\n", |
| 8607 | file, linenum, args[0], error); |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8608 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8609 | goto out_err; |
| 8610 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8611 | free(proxy->conf.lfs_file); |
| 8612 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 8613 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 8614 | |
| 8615 | cur_arg += 2; |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 8616 | } else if (((custom = action_http_req_custom(args[0])) != NULL)) { |
| 8617 | char *errmsg = NULL; |
| 8618 | cur_arg = 1; |
| 8619 | /* try in the module list */ |
Thierry FOURNIER | 5563e4b | 2015-08-14 19:20:07 +0200 | [diff] [blame] | 8620 | rule->from = ACT_F_HTTP_REQ; |
Thierry FOURNIER | 85c6c97 | 2015-09-22 19:14:35 +0200 | [diff] [blame] | 8621 | rule->kw = custom; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 8622 | 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] | 8623 | ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n", |
| 8624 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 8625 | free(errmsg); |
| 8626 | goto out_err; |
| 8627 | } |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8628 | } else { |
Thierry FOURNIER | ab95e65 | 2015-10-02 08:24:51 +0200 | [diff] [blame] | 8629 | action_build_list(&http_req_keywords.list, &trash); |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8630 | ha_alert("parsing [%s:%d]: 'http-request' expects 'allow', 'deny', 'auth', 'redirect', " |
| 8631 | "'tarpit', 'add-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', " |
| 8632 | "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'track-sc*'" |
| 8633 | "%s%s, but got '%s'%s.\n", |
| 8634 | file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)"); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8635 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8636 | } |
| 8637 | |
| 8638 | if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) { |
| 8639 | struct acl_cond *cond; |
Willy Tarreau | b7451bb | 2012-04-27 12:38:15 +0200 | [diff] [blame] | 8640 | char *errmsg = NULL; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8641 | |
Christopher Faulet | 1b421ea | 2017-09-22 14:38:56 +0200 | [diff] [blame] | 8642 | 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] | 8643 | ha_alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n", |
| 8644 | file, linenum, args[0], errmsg); |
Willy Tarreau | b7451bb | 2012-04-27 12:38:15 +0200 | [diff] [blame] | 8645 | free(errmsg); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8646 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8647 | } |
| 8648 | rule->cond = cond; |
| 8649 | } |
| 8650 | else if (*args[cur_arg]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8651 | ha_alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or" |
| 8652 | " either 'if' or 'unless' followed by a condition but found '%s'.\n", |
| 8653 | file, linenum, args[0], args[cur_arg]); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8654 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8655 | } |
| 8656 | |
| 8657 | return rule; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8658 | out_err: |
| 8659 | free(rule); |
| 8660 | return NULL; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8661 | } |
| 8662 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8663 | /* parse an "http-respose" rule */ |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 8664 | 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] | 8665 | { |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 8666 | struct act_rule *rule; |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 8667 | struct action_kw *custom = NULL; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8668 | int cur_arg; |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 8669 | char *error; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8670 | |
| 8671 | rule = calloc(1, sizeof(*rule)); |
| 8672 | if (!rule) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8673 | ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum); |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8674 | goto out_err; |
| 8675 | } |
| 8676 | |
| 8677 | if (!strcmp(args[0], "allow")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8678 | rule->action = ACT_ACTION_ALLOW; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8679 | cur_arg = 1; |
| 8680 | } else if (!strcmp(args[0], "deny")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8681 | rule->action = ACT_ACTION_DENY; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8682 | cur_arg = 1; |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 8683 | } else if (!strcmp(args[0], "set-nice")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8684 | rule->action = ACT_HTTP_SET_NICE; |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 8685 | cur_arg = 1; |
| 8686 | |
| 8687 | if (!*args[cur_arg] || |
| 8688 | (*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] | 8689 | ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n", |
| 8690 | file, linenum, args[0]); |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 8691 | goto out_err; |
| 8692 | } |
| 8693 | rule->arg.nice = atoi(args[cur_arg]); |
| 8694 | if (rule->arg.nice < -1024) |
| 8695 | rule->arg.nice = -1024; |
| 8696 | else if (rule->arg.nice > 1024) |
| 8697 | rule->arg.nice = 1024; |
| 8698 | cur_arg++; |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 8699 | } else if (!strcmp(args[0], "set-tos")) { |
| 8700 | #ifdef IP_TOS |
| 8701 | char *err; |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8702 | rule->action = ACT_HTTP_SET_TOS; |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 8703 | cur_arg = 1; |
| 8704 | |
| 8705 | if (!*args[cur_arg] || |
| 8706 | (*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] | 8707 | ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n", |
| 8708 | file, linenum, args[0]); |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 8709 | goto out_err; |
| 8710 | } |
| 8711 | |
| 8712 | rule->arg.tos = strtol(args[cur_arg], &err, 0); |
| 8713 | if (err && *err != '\0') { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8714 | ha_alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n", |
| 8715 | file, linenum, err, args[0]); |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 8716 | goto out_err; |
| 8717 | } |
| 8718 | cur_arg++; |
| 8719 | #else |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8720 | 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] | 8721 | goto out_err; |
| 8722 | #endif |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 8723 | } else if (!strcmp(args[0], "set-mark")) { |
| 8724 | #ifdef SO_MARK |
| 8725 | char *err; |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8726 | rule->action = ACT_HTTP_SET_MARK; |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 8727 | cur_arg = 1; |
| 8728 | |
| 8729 | if (!*args[cur_arg] || |
| 8730 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8731 | ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n", |
| 8732 | file, linenum, args[0]); |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 8733 | goto out_err; |
| 8734 | } |
| 8735 | |
| 8736 | rule->arg.mark = strtoul(args[cur_arg], &err, 0); |
| 8737 | if (err && *err != '\0') { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8738 | ha_alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n", |
| 8739 | file, linenum, err, args[0]); |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 8740 | goto out_err; |
| 8741 | } |
| 8742 | cur_arg++; |
| 8743 | global.last_checks |= LSTCHK_NETADM; |
| 8744 | #else |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8745 | 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] | 8746 | goto out_err; |
| 8747 | #endif |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 8748 | } else if (!strcmp(args[0], "set-log-level")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8749 | rule->action = ACT_HTTP_SET_LOGL; |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 8750 | cur_arg = 1; |
| 8751 | |
| 8752 | if (!*args[cur_arg] || |
| 8753 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 8754 | bad_log_level: |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8755 | ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n", |
| 8756 | file, linenum, args[0]); |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 8757 | goto out_err; |
| 8758 | } |
| 8759 | if (strcmp(args[cur_arg], "silent") == 0) |
| 8760 | rule->arg.loglevel = -1; |
Ruoshan Huang | dd01678 | 2016-06-15 22:16:03 +0800 | [diff] [blame] | 8761 | 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] | 8762 | goto bad_log_level; |
| 8763 | cur_arg++; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8764 | } 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] | 8765 | 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] | 8766 | cur_arg = 1; |
| 8767 | |
| 8768 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 8769 | (*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] | 8770 | ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n", |
| 8771 | file, linenum, args[0]); |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8772 | goto out_err; |
| 8773 | } |
| 8774 | |
| 8775 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 8776 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 8777 | LIST_INIT(&rule->arg.hdr_add.fmt); |
| 8778 | |
Thierry FOURNIER | 1c0054f | 2013-11-20 15:09:52 +0100 | [diff] [blame] | 8779 | proxy->conf.args.ctx = ARGC_HRS; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8780 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8781 | 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] | 8782 | (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] | 8783 | ha_alert("parsing [%s:%d]: 'http-response %s': %s.\n", |
| 8784 | file, linenum, args[0], error); |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8785 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8786 | goto out_err; |
| 8787 | } |
Willy Tarreau | 59ad1a2 | 2014-01-29 14:39:58 +0100 | [diff] [blame] | 8788 | free(proxy->conf.lfs_file); |
| 8789 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 8790 | proxy->conf.lfs_line = proxy->conf.args.line; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8791 | cur_arg += 2; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 8792 | } 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] | 8793 | 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] | 8794 | cur_arg = 1; |
| 8795 | |
| 8796 | if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] || |
Baptiste Assmann | 12cb00b | 2014-08-08 17:29:06 +0200 | [diff] [blame] | 8797 | (*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] | 8798 | ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n", |
| 8799 | file, linenum, args[0]); |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 8800 | goto out_err; |
| 8801 | } |
| 8802 | |
| 8803 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 8804 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 8805 | LIST_INIT(&rule->arg.hdr_add.fmt); |
| 8806 | |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 8807 | error = NULL; |
| 8808 | 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] | 8809 | ha_alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum, |
| 8810 | args[cur_arg + 1], error); |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 8811 | free(error); |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 8812 | goto out_err; |
| 8813 | } |
| 8814 | |
| 8815 | proxy->conf.args.ctx = ARGC_HRQ; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8816 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8817 | 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] | 8818 | (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] | 8819 | ha_alert("parsing [%s:%d]: 'http-response %s': %s.\n", |
| 8820 | file, linenum, args[0], error); |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8821 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8822 | goto out_err; |
| 8823 | } |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 8824 | |
| 8825 | free(proxy->conf.lfs_file); |
| 8826 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 8827 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 8828 | cur_arg += 3; |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 8829 | } else if (strcmp(args[0], "del-header") == 0) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8830 | rule->action = ACT_HTTP_DEL_HDR; |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 8831 | cur_arg = 1; |
| 8832 | |
| 8833 | if (!*args[cur_arg] || |
| 8834 | (*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] | 8835 | ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 8836 | file, linenum, args[0]); |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 8837 | goto out_err; |
| 8838 | } |
| 8839 | |
| 8840 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 8841 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 8842 | |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8843 | proxy->conf.args.ctx = ARGC_HRS; |
| 8844 | free(proxy->conf.lfs_file); |
| 8845 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 8846 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 8847 | cur_arg += 1; |
| 8848 | } else if (strncmp(args[0], "add-acl", 7) == 0) { |
| 8849 | /* http-request add-acl(<reference (acl name)>) <key pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8850 | rule->action = ACT_HTTP_ADD_ACL; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8851 | /* |
| 8852 | * '+ 8' for 'add-acl(' |
| 8853 | * '- 9' for 'add-acl(' + trailing ')' |
| 8854 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 8855 | 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] | 8856 | |
| 8857 | cur_arg = 1; |
| 8858 | |
| 8859 | if (!*args[cur_arg] || |
| 8860 | (*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] | 8861 | ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 8862 | file, linenum, args[0]); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8863 | goto out_err; |
| 8864 | } |
| 8865 | |
| 8866 | LIST_INIT(&rule->arg.map.key); |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 8867 | proxy->conf.args.ctx = ARGC_HRS; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8868 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8869 | 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] | 8870 | (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] | 8871 | ha_alert("parsing [%s:%d]: 'http-response %s': %s.\n", |
| 8872 | file, linenum, args[0], error); |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8873 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8874 | goto out_err; |
| 8875 | } |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 8876 | free(proxy->conf.lfs_file); |
| 8877 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 8878 | proxy->conf.lfs_line = proxy->conf.args.line; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8879 | |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 8880 | cur_arg += 1; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8881 | } else if (strncmp(args[0], "del-acl", 7) == 0) { |
| 8882 | /* http-response del-acl(<reference (acl name)>) <key pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8883 | rule->action = ACT_HTTP_DEL_ACL; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8884 | /* |
| 8885 | * '+ 8' for 'del-acl(' |
| 8886 | * '- 9' for 'del-acl(' + trailing ')' |
| 8887 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 8888 | 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] | 8889 | |
| 8890 | cur_arg = 1; |
| 8891 | |
| 8892 | if (!*args[cur_arg] || |
| 8893 | (*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] | 8894 | ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 8895 | file, linenum, args[0]); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8896 | goto out_err; |
| 8897 | } |
| 8898 | |
| 8899 | LIST_INIT(&rule->arg.map.key); |
| 8900 | proxy->conf.args.ctx = ARGC_HRS; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8901 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8902 | 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] | 8903 | (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] | 8904 | ha_alert("parsing [%s:%d]: 'http-response %s': %s.\n", |
| 8905 | file, linenum, args[0], error); |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8906 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8907 | goto out_err; |
| 8908 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8909 | free(proxy->conf.lfs_file); |
| 8910 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 8911 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 8912 | cur_arg += 1; |
| 8913 | } else if (strncmp(args[0], "del-map", 7) == 0) { |
| 8914 | /* http-response del-map(<reference (map name)>) <key pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8915 | rule->action = ACT_HTTP_DEL_MAP; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8916 | /* |
| 8917 | * '+ 8' for 'del-map(' |
| 8918 | * '- 9' for 'del-map(' + trailing ')' |
| 8919 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 8920 | 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] | 8921 | |
| 8922 | cur_arg = 1; |
| 8923 | |
| 8924 | if (!*args[cur_arg] || |
| 8925 | (*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] | 8926 | ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 8927 | file, linenum, args[0]); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8928 | goto out_err; |
| 8929 | } |
| 8930 | |
| 8931 | LIST_INIT(&rule->arg.map.key); |
| 8932 | proxy->conf.args.ctx = ARGC_HRS; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8933 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8934 | if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8935 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8936 | ha_alert("parsing [%s:%d]: 'http-response %s' %s.\n", |
| 8937 | file, linenum, args[0], error); |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8938 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8939 | goto out_err; |
| 8940 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8941 | free(proxy->conf.lfs_file); |
| 8942 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 8943 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 8944 | cur_arg += 1; |
| 8945 | } else if (strncmp(args[0], "set-map", 7) == 0) { |
| 8946 | /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8947 | rule->action = ACT_HTTP_SET_MAP; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8948 | /* |
| 8949 | * '+ 8' for 'set-map(' |
| 8950 | * '- 9' for 'set-map(' + trailing ')' |
| 8951 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 8952 | 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] | 8953 | |
| 8954 | cur_arg = 1; |
| 8955 | |
| 8956 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 8957 | (*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] | 8958 | ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n", |
| 8959 | file, linenum, args[0]); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8960 | goto out_err; |
| 8961 | } |
| 8962 | |
| 8963 | LIST_INIT(&rule->arg.map.key); |
| 8964 | LIST_INIT(&rule->arg.map.value); |
| 8965 | |
| 8966 | proxy->conf.args.ctx = ARGC_HRS; |
| 8967 | |
| 8968 | /* key pattern */ |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8969 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8970 | if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8971 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8972 | ha_alert("parsing [%s:%d]: 'http-response %s' name: %s.\n", |
| 8973 | file, linenum, args[0], error); |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8974 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8975 | goto out_err; |
| 8976 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8977 | |
| 8978 | /* value pattern */ |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8979 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8980 | 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] | 8981 | (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] | 8982 | ha_alert("parsing [%s:%d]: 'http-response %s' value: %s.\n", |
| 8983 | file, linenum, args[0], error); |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8984 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8985 | goto out_err; |
| 8986 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8987 | |
| 8988 | free(proxy->conf.lfs_file); |
| 8989 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 8990 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 8991 | |
| 8992 | cur_arg += 2; |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 8993 | } else if (strcmp(args[0], "redirect") == 0) { |
| 8994 | struct redirect_rule *redir; |
| 8995 | char *errmsg = NULL; |
| 8996 | |
| 8997 | 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] | 8998 | ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n", |
| 8999 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 9000 | goto out_err; |
| 9001 | } |
| 9002 | |
| 9003 | /* this redirect rule might already contain a parsed condition which |
| 9004 | * we'll pass to the http-request rule. |
| 9005 | */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9006 | rule->action = ACT_HTTP_REDIR; |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 9007 | rule->arg.redir = redir; |
| 9008 | rule->cond = redir->cond; |
| 9009 | redir->cond = NULL; |
| 9010 | cur_arg = 2; |
| 9011 | return rule; |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 9012 | } else if (strncmp(args[0], "track-sc", 8) == 0 && |
| 9013 | args[0][9] == '\0' && args[0][8] >= '0' && |
| 9014 | args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */ |
| 9015 | struct sample_expr *expr; |
| 9016 | unsigned int where; |
| 9017 | char *err = NULL; |
| 9018 | |
| 9019 | cur_arg = 1; |
| 9020 | proxy->conf.args.ctx = ARGC_TRK; |
| 9021 | |
| 9022 | expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args); |
| 9023 | if (!expr) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 9024 | ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n", |
| 9025 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], err); |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 9026 | free(err); |
| 9027 | goto out_err; |
| 9028 | } |
| 9029 | |
| 9030 | where = 0; |
| 9031 | if (proxy->cap & PR_CAP_FE) |
| 9032 | where |= SMP_VAL_FE_HRS_HDR; |
| 9033 | if (proxy->cap & PR_CAP_BE) |
| 9034 | where |= SMP_VAL_BE_HRS_HDR; |
| 9035 | |
| 9036 | if (!(expr->fetch->val & where)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 9037 | ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule :" |
| 9038 | " fetch method '%s' extracts information from '%s', none of which is available here.\n", |
| 9039 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], |
| 9040 | args[cur_arg-1], sample_src_names(expr->fetch->use)); |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 9041 | free(expr); |
| 9042 | goto out_err; |
| 9043 | } |
| 9044 | |
| 9045 | if (strcmp(args[cur_arg], "table") == 0) { |
| 9046 | cur_arg++; |
| 9047 | if (!args[cur_arg]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 9048 | ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : missing table name.\n", |
| 9049 | file, linenum, proxy_type_str(proxy), proxy->id, args[0]); |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 9050 | free(expr); |
| 9051 | goto out_err; |
| 9052 | } |
| 9053 | /* we copy the table name for now, it will be resolved later */ |
| 9054 | rule->arg.trk_ctr.table.n = strdup(args[cur_arg]); |
| 9055 | cur_arg++; |
| 9056 | } |
| 9057 | rule->arg.trk_ctr.expr = expr; |
| 9058 | rule->action = ACT_ACTION_TRK_SC0 + args[0][8] - '0'; |
Christopher Faulet | 78880fb | 2017-09-18 14:43:55 +0200 | [diff] [blame] | 9059 | rule->check_ptr = check_trk_action; |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 9060 | } else if (((custom = action_http_res_custom(args[0])) != NULL)) { |
| 9061 | char *errmsg = NULL; |
| 9062 | cur_arg = 1; |
| 9063 | /* try in the module list */ |
Thierry FOURNIER | 5563e4b | 2015-08-14 19:20:07 +0200 | [diff] [blame] | 9064 | rule->from = ACT_F_HTTP_RES; |
Thierry FOURNIER | 85c6c97 | 2015-09-22 19:14:35 +0200 | [diff] [blame] | 9065 | rule->kw = custom; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 9066 | 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] | 9067 | ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n", |
| 9068 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 9069 | free(errmsg); |
| 9070 | goto out_err; |
| 9071 | } |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9072 | } else { |
Thierry FOURNIER | ab95e65 | 2015-10-02 08:24:51 +0200 | [diff] [blame] | 9073 | action_build_list(&http_res_keywords.list, &trash); |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 9074 | ha_alert("parsing [%s:%d]: 'http-response' expects 'allow', 'deny', 'redirect', " |
| 9075 | "'add-header', 'del-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', " |
| 9076 | "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'track-sc*'" |
| 9077 | "%s%s, but got '%s'%s.\n", |
| 9078 | file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)"); |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9079 | goto out_err; |
| 9080 | } |
| 9081 | |
| 9082 | if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) { |
| 9083 | struct acl_cond *cond; |
| 9084 | char *errmsg = NULL; |
| 9085 | |
Christopher Faulet | 1b421ea | 2017-09-22 14:38:56 +0200 | [diff] [blame] | 9086 | 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] | 9087 | ha_alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n", |
| 9088 | file, linenum, args[0], errmsg); |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9089 | free(errmsg); |
| 9090 | goto out_err; |
| 9091 | } |
| 9092 | rule->cond = cond; |
| 9093 | } |
| 9094 | else if (*args[cur_arg]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 9095 | ha_alert("parsing [%s:%d]: 'http-response %s' expects" |
| 9096 | " either 'if' or 'unless' followed by a condition but found '%s'.\n", |
| 9097 | file, linenum, args[0], args[cur_arg]); |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9098 | goto out_err; |
| 9099 | } |
| 9100 | |
| 9101 | return rule; |
| 9102 | out_err: |
| 9103 | free(rule); |
| 9104 | return NULL; |
| 9105 | } |
| 9106 | |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9107 | /* 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] | 9108 | * 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] | 9109 | * dynamic log-format rule instead of a static string. Parameter <dir> indicates |
| 9110 | * 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] | 9111 | */ |
| 9112 | 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] | 9113 | const char **args, char **errmsg, int use_fmt, int dir) |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9114 | { |
| 9115 | struct redirect_rule *rule; |
| 9116 | int cur_arg; |
| 9117 | int type = REDIRECT_TYPE_NONE; |
| 9118 | int code = 302; |
| 9119 | const char *destination = NULL; |
| 9120 | const char *cookie = NULL; |
| 9121 | int cookie_set = 0; |
| 9122 | unsigned int flags = REDIRECT_FLAG_NONE; |
| 9123 | struct acl_cond *cond = NULL; |
| 9124 | |
| 9125 | cur_arg = 0; |
| 9126 | while (*(args[cur_arg])) { |
| 9127 | if (strcmp(args[cur_arg], "location") == 0) { |
| 9128 | if (!*args[cur_arg + 1]) |
| 9129 | goto missing_arg; |
| 9130 | |
| 9131 | type = REDIRECT_TYPE_LOCATION; |
| 9132 | cur_arg++; |
| 9133 | destination = args[cur_arg]; |
| 9134 | } |
| 9135 | else if (strcmp(args[cur_arg], "prefix") == 0) { |
| 9136 | if (!*args[cur_arg + 1]) |
| 9137 | goto missing_arg; |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9138 | type = REDIRECT_TYPE_PREFIX; |
| 9139 | cur_arg++; |
| 9140 | destination = args[cur_arg]; |
| 9141 | } |
| 9142 | else if (strcmp(args[cur_arg], "scheme") == 0) { |
| 9143 | if (!*args[cur_arg + 1]) |
| 9144 | goto missing_arg; |
| 9145 | |
| 9146 | type = REDIRECT_TYPE_SCHEME; |
| 9147 | cur_arg++; |
| 9148 | destination = args[cur_arg]; |
| 9149 | } |
| 9150 | else if (strcmp(args[cur_arg], "set-cookie") == 0) { |
| 9151 | if (!*args[cur_arg + 1]) |
| 9152 | goto missing_arg; |
| 9153 | |
| 9154 | cur_arg++; |
| 9155 | cookie = args[cur_arg]; |
| 9156 | cookie_set = 1; |
| 9157 | } |
| 9158 | else if (strcmp(args[cur_arg], "clear-cookie") == 0) { |
| 9159 | if (!*args[cur_arg + 1]) |
| 9160 | goto missing_arg; |
| 9161 | |
| 9162 | cur_arg++; |
| 9163 | cookie = args[cur_arg]; |
| 9164 | cookie_set = 0; |
| 9165 | } |
| 9166 | else if (strcmp(args[cur_arg], "code") == 0) { |
| 9167 | if (!*args[cur_arg + 1]) |
| 9168 | goto missing_arg; |
| 9169 | |
| 9170 | cur_arg++; |
| 9171 | code = atol(args[cur_arg]); |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 9172 | if (code < 301 || code > 308 || (code > 303 && code < 307)) { |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9173 | memprintf(errmsg, |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 9174 | "'%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] | 9175 | args[cur_arg - 1], args[cur_arg]); |
| 9176 | return NULL; |
| 9177 | } |
| 9178 | } |
| 9179 | else if (!strcmp(args[cur_arg],"drop-query")) { |
| 9180 | flags |= REDIRECT_FLAG_DROP_QS; |
| 9181 | } |
| 9182 | else if (!strcmp(args[cur_arg],"append-slash")) { |
| 9183 | flags |= REDIRECT_FLAG_APPEND_SLASH; |
| 9184 | } |
| 9185 | else if (strcmp(args[cur_arg], "if") == 0 || |
| 9186 | strcmp(args[cur_arg], "unless") == 0) { |
Olivier Houchard | fbc74e8 | 2017-11-24 16:54:05 +0100 | [diff] [blame] | 9187 | 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] | 9188 | if (!cond) { |
| 9189 | memprintf(errmsg, "error in condition: %s", *errmsg); |
| 9190 | return NULL; |
| 9191 | } |
| 9192 | break; |
| 9193 | } |
| 9194 | else { |
| 9195 | memprintf(errmsg, |
| 9196 | "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')", |
| 9197 | args[cur_arg]); |
| 9198 | return NULL; |
| 9199 | } |
| 9200 | cur_arg++; |
| 9201 | } |
| 9202 | |
| 9203 | if (type == REDIRECT_TYPE_NONE) { |
| 9204 | memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')"); |
| 9205 | return NULL; |
| 9206 | } |
| 9207 | |
Willy Tarreau | be4653b | 2015-05-28 15:26:58 +0200 | [diff] [blame] | 9208 | if (dir && type != REDIRECT_TYPE_LOCATION) { |
| 9209 | memprintf(errmsg, "response only supports redirect type 'location'"); |
| 9210 | return NULL; |
| 9211 | } |
| 9212 | |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 9213 | rule = calloc(1, sizeof(*rule)); |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9214 | rule->cond = cond; |
Willy Tarreau | 60e0838 | 2013-12-03 00:48:45 +0100 | [diff] [blame] | 9215 | LIST_INIT(&rule->rdr_fmt); |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 9216 | |
| 9217 | if (!use_fmt) { |
| 9218 | /* old-style static redirect rule */ |
| 9219 | rule->rdr_str = strdup(destination); |
| 9220 | rule->rdr_len = strlen(destination); |
| 9221 | } |
| 9222 | else { |
| 9223 | /* log-format based redirect rule */ |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 9224 | |
| 9225 | /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case, |
| 9226 | * if prefix == "/", we don't want to add anything, otherwise it |
| 9227 | * makes it hard for the user to configure a self-redirection. |
| 9228 | */ |
Godbach | d972203 | 2014-12-18 15:44:58 +0800 | [diff] [blame] | 9229 | curproxy->conf.args.ctx = ARGC_RDR; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 9230 | if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) { |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9231 | if (!parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP, |
| 9232 | 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] | 9233 | : (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9234 | errmsg)) { |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9235 | return NULL; |
| 9236 | } |
Willy Tarreau | 59ad1a2 | 2014-01-29 14:39:58 +0100 | [diff] [blame] | 9237 | free(curproxy->conf.lfs_file); |
| 9238 | curproxy->conf.lfs_file = strdup(curproxy->conf.args.file); |
| 9239 | curproxy->conf.lfs_line = curproxy->conf.args.line; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 9240 | } |
| 9241 | } |
| 9242 | |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9243 | if (cookie) { |
| 9244 | /* depending on cookie_set, either we want to set the cookie, or to clear it. |
| 9245 | * a clear consists in appending "; path=/; Max-Age=0;" at the end. |
| 9246 | */ |
| 9247 | rule->cookie_len = strlen(cookie); |
| 9248 | if (cookie_set) { |
| 9249 | rule->cookie_str = malloc(rule->cookie_len + 10); |
| 9250 | memcpy(rule->cookie_str, cookie, rule->cookie_len); |
| 9251 | memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10); |
| 9252 | rule->cookie_len += 9; |
| 9253 | } else { |
| 9254 | rule->cookie_str = malloc(rule->cookie_len + 21); |
| 9255 | memcpy(rule->cookie_str, cookie, rule->cookie_len); |
| 9256 | memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21); |
| 9257 | rule->cookie_len += 20; |
| 9258 | } |
| 9259 | } |
| 9260 | rule->type = type; |
| 9261 | rule->code = code; |
| 9262 | rule->flags = flags; |
| 9263 | LIST_INIT(&rule->list); |
| 9264 | return rule; |
| 9265 | |
| 9266 | missing_arg: |
| 9267 | memprintf(errmsg, "missing argument for '%s'", args[cur_arg]); |
| 9268 | return NULL; |
| 9269 | } |
| 9270 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9271 | /************************************************************************/ |
| 9272 | /* The code below is dedicated to ACL parsing and matching */ |
| 9273 | /************************************************************************/ |
| 9274 | |
| 9275 | |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9276 | /* This function ensures that the prerequisites for an L7 fetch are ready, |
| 9277 | * which means that a request or response is ready. If some data is missing, |
| 9278 | * 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] | 9279 | * to extract data from L7. If <req_vol> is non-null during a request prefetch, |
| 9280 | * another test is made to ensure the required information is not gone. |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9281 | * |
| 9282 | * The function returns : |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9283 | * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to |
| 9284 | * decide whether or not an HTTP message is present ; |
| 9285 | * 0 if the requested data cannot be fetched or if it is certain that |
| 9286 | * we'll never have any HTTP message there ; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9287 | * 1 if an HTTP message is ready |
| 9288 | */ |
James Rosewell | 91a41cb | 2015-09-18 17:11:16 +0100 | [diff] [blame] | 9289 | 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] | 9290 | const struct arg *args, struct sample *smp, int req_vol) |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9291 | { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 9292 | struct http_txn *txn; |
| 9293 | struct http_msg *msg; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9294 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 9295 | /* Note: it is possible that <s> is NULL when called before stream |
| 9296 | * initialization (eg: tcp-request connection), so this function is the |
| 9297 | * one responsible for guarding against this case for all HTTP users. |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9298 | */ |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 9299 | if (!s) |
| 9300 | return 0; |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 9301 | |
Thierry FOURNIER | ed08d6a | 2015-09-24 08:40:18 +0200 | [diff] [blame] | 9302 | if (!s->txn) { |
| 9303 | if (unlikely(!http_alloc_txn(s))) |
| 9304 | return 0; /* not enough memory */ |
| 9305 | http_init_txn(s); |
| 9306 | } |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 9307 | txn = s->txn; |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 9308 | msg = &txn->req; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9309 | |
| 9310 | /* Check for a dependency on a request */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9311 | smp->data.type = SMP_T_BOOL; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9312 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 9313 | if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 9314 | /* If the buffer does not leave enough free space at the end, |
| 9315 | * we must first realign it. |
| 9316 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 9317 | if (s->req.buf->p > s->req.buf->data && |
| 9318 | s->req.buf->i + s->req.buf->p > s->req.buf->data + s->req.buf->size - global.tune.maxrewrite) |
| 9319 | buffer_slow_realign(s->req.buf); |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 9320 | |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9321 | if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 472b1ee | 2013-10-14 22:41:30 +0200 | [diff] [blame] | 9322 | if (msg->msg_state == HTTP_MSG_ERROR) |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9323 | return 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9324 | |
| 9325 | /* Try to decode HTTP request */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 9326 | if (likely(msg->next < s->req.buf->i)) |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9327 | http_msg_analyzer(msg, &txn->hdr_idx); |
| 9328 | |
| 9329 | /* Still no valid request ? */ |
| 9330 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 3bf1b2b | 2012-08-27 20:46:07 +0200 | [diff] [blame] | 9331 | if ((msg->msg_state == HTTP_MSG_ERROR) || |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 9332 | buffer_full(s->req.buf, global.tune.maxrewrite)) { |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9333 | return 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9334 | } |
| 9335 | /* wait for final state */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9336 | smp->flags |= SMP_F_MAY_CHANGE; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9337 | return 0; |
| 9338 | } |
| 9339 | |
| 9340 | /* OK we just got a valid HTTP request. We have some minor |
| 9341 | * preparation to perform so that further checks can rely |
| 9342 | * on HTTP tests. |
| 9343 | */ |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 9344 | |
| 9345 | /* If the request was parsed but was too large, we must absolutely |
| 9346 | * return an error so that it is not processed. At the moment this |
| 9347 | * cannot happen, but if the parsers are to change in the future, |
| 9348 | * we want this check to be maintained. |
| 9349 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 9350 | if (unlikely(s->req.buf->i + s->req.buf->p > |
| 9351 | s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)) { |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 9352 | msg->err_state = msg->msg_state; |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 9353 | msg->msg_state = HTTP_MSG_ERROR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9354 | smp->data.u.sint = 1; |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 9355 | return 1; |
| 9356 | } |
| 9357 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9358 | 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] | 9359 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 9360 | s->flags |= SF_REDIRECTABLE; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9361 | |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9362 | if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn)) |
| 9363 | return 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9364 | } |
| 9365 | |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9366 | if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) { |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9367 | return 0; /* data might have moved and indexes changed */ |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9368 | } |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9369 | |
| 9370 | /* otherwise everything's ready for the request */ |
| 9371 | } |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 9372 | else { |
| 9373 | /* Check for a dependency on a response */ |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9374 | if (txn->rsp.msg_state < HTTP_MSG_BODY) { |
| 9375 | smp->flags |= SMP_F_MAY_CHANGE; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9376 | return 0; |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9377 | } |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9378 | } |
| 9379 | |
| 9380 | /* everything's OK */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9381 | smp->data.u.sint = 1; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9382 | return 1; |
| 9383 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9384 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9385 | /* 1. Check on METHOD |
| 9386 | * We use the pre-parsed method if it is known, and store its number as an |
| 9387 | * integer. If it is unknown, we use the pointer and the length. |
| 9388 | */ |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 9389 | 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] | 9390 | { |
| 9391 | int len, meth; |
| 9392 | |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 9393 | len = strlen(text); |
| 9394 | meth = find_http_meth(text, len); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9395 | |
| 9396 | pattern->val.i = meth; |
| 9397 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | 912c119 | 2014-08-29 15:15:50 +0200 | [diff] [blame] | 9398 | pattern->ptr.str = (char *)text; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9399 | pattern->len = len; |
| 9400 | } |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 9401 | else { |
| 9402 | pattern->ptr.str = NULL; |
| 9403 | pattern->len = 0; |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 9404 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9405 | return 1; |
| 9406 | } |
| 9407 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 9408 | /* This function fetches the method of current HTTP request and stores |
| 9409 | * it in the global pattern struct as a chunk. There are two possibilities : |
| 9410 | * - if the method is known (not HTTP_METH_OTHER), its identifier is stored |
| 9411 | * in <len> and <ptr> is NULL ; |
| 9412 | * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and |
| 9413 | * <len> to its length. |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 9414 | * This is intended to be used with pat_match_meth() only. |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 9415 | */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 9416 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9417 | 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] | 9418 | { |
| 9419 | int meth; |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 9420 | struct http_txn *txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9421 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 9422 | CHECK_HTTP_MESSAGE_FIRST_PERM(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9423 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 9424 | txn = smp->strm->txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9425 | meth = txn->meth; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9426 | smp->data.type = SMP_T_METH; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9427 | smp->data.u.meth.meth = meth; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9428 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9429 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 9430 | /* ensure the indexes are not affected */ |
| 9431 | return 0; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9432 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9433 | smp->data.u.meth.str.len = txn->req.sl.rq.m_l; |
| 9434 | smp->data.u.meth.str.str = txn->req.chn->buf->p; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9435 | } |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9436 | smp->flags |= SMP_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9437 | return 1; |
| 9438 | } |
| 9439 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 9440 | /* See above how the method is stored in the global pattern */ |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 9441 | 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] | 9442 | { |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 9443 | int icase; |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 9444 | struct pattern_list *lst; |
| 9445 | struct pattern *pattern; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 9446 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 9447 | list_for_each_entry(lst, &expr->patterns, list) { |
| 9448 | pattern = &lst->pat; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9449 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 9450 | /* well-known method */ |
| 9451 | if (pattern->val.i != HTTP_METH_OTHER) { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9452 | if (smp->data.u.meth.meth == pattern->val.i) |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 9453 | return pattern; |
| 9454 | else |
| 9455 | continue; |
| 9456 | } |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 9457 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 9458 | /* Other method, we must compare the strings */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9459 | if (pattern->len != smp->data.u.meth.str.len) |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 9460 | continue; |
| 9461 | |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 9462 | icase = expr->mflags & PAT_MF_IGNORE_CASE; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9463 | if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.meth.str.str, smp->data.u.meth.str.len) == 0) || |
| 9464 | (!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] | 9465 | return pattern; |
| 9466 | } |
| 9467 | return NULL; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9468 | } |
| 9469 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 9470 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9471 | 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] | 9472 | { |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 9473 | struct http_txn *txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9474 | char *ptr; |
| 9475 | int len; |
| 9476 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9477 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9478 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 9479 | txn = smp->strm->txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9480 | len = txn->req.sl.rq.v_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9481 | ptr = txn->req.chn->buf->p + txn->req.sl.rq.v; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9482 | |
| 9483 | while ((len-- > 0) && (*ptr++ != '/')); |
| 9484 | if (len <= 0) |
| 9485 | return 0; |
| 9486 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9487 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9488 | smp->data.u.str.str = ptr; |
| 9489 | smp->data.u.str.len = len; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9490 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9491 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9492 | return 1; |
| 9493 | } |
| 9494 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 9495 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9496 | 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] | 9497 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 9498 | struct http_txn *txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9499 | char *ptr; |
| 9500 | int len; |
| 9501 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9502 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9503 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 9504 | txn = smp->strm->txn; |
Willy Tarreau | f26b252 | 2012-12-14 08:33:14 +0100 | [diff] [blame] | 9505 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
| 9506 | return 0; |
| 9507 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9508 | len = txn->rsp.sl.st.v_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9509 | ptr = txn->rsp.chn->buf->p; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9510 | |
| 9511 | while ((len-- > 0) && (*ptr++ != '/')); |
| 9512 | if (len <= 0) |
| 9513 | return 0; |
| 9514 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9515 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9516 | smp->data.u.str.str = ptr; |
| 9517 | smp->data.u.str.len = len; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9518 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9519 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9520 | return 1; |
| 9521 | } |
| 9522 | |
| 9523 | /* 3. Check on Status Code. We manipulate integers here. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 9524 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9525 | 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] | 9526 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 9527 | struct http_txn *txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9528 | char *ptr; |
| 9529 | int len; |
| 9530 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9531 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9532 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 9533 | txn = smp->strm->txn; |
Willy Tarreau | f26b252 | 2012-12-14 08:33:14 +0100 | [diff] [blame] | 9534 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
| 9535 | return 0; |
| 9536 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9537 | len = txn->rsp.sl.st.c_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9538 | ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9539 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9540 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9541 | smp->data.u.sint = __strl2ui(ptr, len); |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9542 | smp->flags = SMP_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9543 | return 1; |
| 9544 | } |
| 9545 | |
Thierry Fournier | f4011dd | 2016-03-29 17:23:51 +0200 | [diff] [blame] | 9546 | static int |
| 9547 | smp_fetch_uniqueid(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 9548 | { |
| 9549 | if (LIST_ISEMPTY(&smp->sess->fe->format_unique_id)) |
| 9550 | return 0; |
| 9551 | |
| 9552 | if (!smp->strm->unique_id) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 9553 | if ((smp->strm->unique_id = pool_alloc(pool_head_uniqueid)) == NULL) |
Thierry Fournier | f4011dd | 2016-03-29 17:23:51 +0200 | [diff] [blame] | 9554 | return 0; |
| 9555 | smp->strm->unique_id[0] = '\0'; |
| 9556 | } |
| 9557 | smp->data.u.str.len = build_logline(smp->strm, smp->strm->unique_id, |
| 9558 | UNIQUEID_LEN, &smp->sess->fe->format_unique_id); |
| 9559 | |
| 9560 | smp->data.type = SMP_T_STR; |
| 9561 | smp->data.u.str.str = smp->strm->unique_id; |
| 9562 | smp->flags = SMP_F_CONST; |
| 9563 | return 1; |
| 9564 | } |
| 9565 | |
Thierry FOURNIER | d7d8881 | 2017-04-19 15:15:14 +0200 | [diff] [blame] | 9566 | /* Returns a string block containing all headers including the |
| 9567 | * empty line wich separes headers from the body. This is useful |
| 9568 | * form some headers analysis. |
| 9569 | */ |
| 9570 | static int |
| 9571 | smp_fetch_hdrs(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 9572 | { |
| 9573 | struct http_msg *msg; |
| 9574 | struct hdr_idx *idx; |
| 9575 | struct http_txn *txn; |
| 9576 | |
| 9577 | CHECK_HTTP_MESSAGE_FIRST(); |
| 9578 | |
| 9579 | txn = smp->strm->txn; |
| 9580 | idx = &txn->hdr_idx; |
| 9581 | msg = &txn->req; |
| 9582 | |
| 9583 | smp->data.type = SMP_T_STR; |
| 9584 | smp->data.u.str.str = msg->chn->buf->p + hdr_idx_first_pos(idx); |
| 9585 | smp->data.u.str.len = msg->eoh - hdr_idx_first_pos(idx) + 1 + |
| 9586 | (msg->chn->buf->p[msg->eoh] == '\r'); |
| 9587 | |
| 9588 | return 1; |
| 9589 | } |
| 9590 | |
Thierry FOURNIER | 5617dce | 2017-04-09 05:38:19 +0200 | [diff] [blame] | 9591 | /* Returns the header request in a length/value encoded format. |
| 9592 | * This is useful for exchanges with the SPOE. |
| 9593 | * |
| 9594 | * A "length value" is a multibyte code encoding numbers. It uses the |
| 9595 | * SPOE format. The encoding is the following: |
| 9596 | * |
| 9597 | * Each couple "header name" / "header value" is composed |
| 9598 | * like this: |
| 9599 | * "length value" "header name bytes" |
| 9600 | * "length value" "header value bytes" |
| 9601 | * When the last header is reached, the header name and the header |
| 9602 | * value are empty. Their length are 0 |
| 9603 | */ |
| 9604 | static int |
| 9605 | smp_fetch_hdrs_bin(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 9606 | { |
| 9607 | struct http_msg *msg; |
| 9608 | struct chunk *temp; |
| 9609 | struct hdr_idx *idx; |
| 9610 | const char *cur_ptr, *cur_next, *p; |
| 9611 | int old_idx, cur_idx; |
| 9612 | struct hdr_idx_elem *cur_hdr; |
| 9613 | const char *hn, *hv; |
| 9614 | int hnl, hvl; |
| 9615 | int ret; |
| 9616 | struct http_txn *txn; |
| 9617 | char *buf; |
| 9618 | char *end; |
| 9619 | |
| 9620 | CHECK_HTTP_MESSAGE_FIRST(); |
| 9621 | |
| 9622 | temp = get_trash_chunk(); |
| 9623 | buf = temp->str; |
| 9624 | end = temp->str + temp->size; |
| 9625 | |
| 9626 | txn = smp->strm->txn; |
| 9627 | idx = &txn->hdr_idx; |
| 9628 | msg = &txn->req; |
| 9629 | |
| 9630 | /* Build array of headers. */ |
| 9631 | old_idx = 0; |
| 9632 | cur_next = msg->chn->buf->p + hdr_idx_first_pos(idx); |
| 9633 | while (1) { |
| 9634 | cur_idx = idx->v[old_idx].next; |
| 9635 | if (!cur_idx) |
| 9636 | break; |
| 9637 | old_idx = cur_idx; |
| 9638 | |
| 9639 | cur_hdr = &idx->v[cur_idx]; |
| 9640 | cur_ptr = cur_next; |
| 9641 | cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1; |
| 9642 | |
| 9643 | /* Now we have one full header at cur_ptr of len cur_hdr->len, |
| 9644 | * and the next header starts at cur_next. We'll check |
| 9645 | * this header in the list as well as against the default |
| 9646 | * rule. |
| 9647 | */ |
| 9648 | |
| 9649 | /* look for ': *'. */ |
| 9650 | hn = cur_ptr; |
| 9651 | for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++); |
| 9652 | if (p >= cur_ptr+cur_hdr->len) |
| 9653 | continue; |
| 9654 | hnl = p - hn; |
| 9655 | p++; |
| 9656 | while (p < cur_ptr + cur_hdr->len && (*p == ' ' || *p == '\t')) |
| 9657 | p++; |
| 9658 | if (p >= cur_ptr + cur_hdr->len) |
| 9659 | continue; |
| 9660 | hv = p; |
| 9661 | hvl = cur_ptr + cur_hdr->len-p; |
| 9662 | |
| 9663 | /* encode the header name. */ |
| 9664 | ret = encode_varint(hnl, &buf, end); |
| 9665 | if (ret == -1) |
| 9666 | return 0; |
| 9667 | if (buf + hnl > end) |
| 9668 | return 0; |
| 9669 | memcpy(buf, hn, hnl); |
| 9670 | buf += hnl; |
| 9671 | |
| 9672 | /* encode and copy the value. */ |
| 9673 | ret = encode_varint(hvl, &buf, end); |
| 9674 | if (ret == -1) |
| 9675 | return 0; |
| 9676 | if (buf + hvl > end) |
| 9677 | return 0; |
| 9678 | memcpy(buf, hv, hvl); |
| 9679 | buf += hvl; |
| 9680 | } |
| 9681 | |
| 9682 | /* encode the end of the header list with empty |
| 9683 | * header name and header value. |
| 9684 | */ |
| 9685 | ret = encode_varint(0, &buf, end); |
| 9686 | if (ret == -1) |
| 9687 | return 0; |
| 9688 | ret = encode_varint(0, &buf, end); |
| 9689 | if (ret == -1) |
| 9690 | return 0; |
| 9691 | |
| 9692 | /* Initialise sample data which will be filled. */ |
| 9693 | smp->data.type = SMP_T_BIN; |
| 9694 | smp->data.u.str.str = temp->str; |
| 9695 | smp->data.u.str.len = buf - temp->str; |
| 9696 | smp->data.u.str.size = temp->size; |
| 9697 | |
| 9698 | return 1; |
| 9699 | } |
| 9700 | |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9701 | /* returns the longest available part of the body. This requires that the body |
| 9702 | * has been waited for using http-buffer-request. |
| 9703 | */ |
| 9704 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9705 | 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] | 9706 | { |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9707 | struct http_msg *msg; |
| 9708 | unsigned long len; |
| 9709 | unsigned long block1; |
| 9710 | char *body; |
| 9711 | struct chunk *temp; |
| 9712 | |
| 9713 | CHECK_HTTP_MESSAGE_FIRST(); |
| 9714 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9715 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 9716 | msg = &smp->strm->txn->req; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9717 | else |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 9718 | msg = &smp->strm->txn->rsp; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9719 | |
| 9720 | len = http_body_bytes(msg); |
| 9721 | body = b_ptr(msg->chn->buf, -http_data_rewind(msg)); |
| 9722 | |
| 9723 | block1 = len; |
| 9724 | if (block1 > msg->chn->buf->data + msg->chn->buf->size - body) |
| 9725 | block1 = msg->chn->buf->data + msg->chn->buf->size - body; |
| 9726 | |
| 9727 | if (block1 == len) { |
| 9728 | /* buffer is not wrapped (or empty) */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9729 | smp->data.type = SMP_T_BIN; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9730 | smp->data.u.str.str = body; |
| 9731 | smp->data.u.str.len = len; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9732 | smp->flags = SMP_F_VOL_TEST | SMP_F_CONST; |
| 9733 | } |
| 9734 | else { |
| 9735 | /* buffer is wrapped, we need to defragment it */ |
| 9736 | temp = get_trash_chunk(); |
| 9737 | memcpy(temp->str, body, block1); |
| 9738 | memcpy(temp->str + block1, msg->chn->buf->data, len - block1); |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9739 | smp->data.type = SMP_T_BIN; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9740 | smp->data.u.str.str = temp->str; |
| 9741 | smp->data.u.str.len = len; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9742 | smp->flags = SMP_F_VOL_TEST; |
| 9743 | } |
| 9744 | return 1; |
| 9745 | } |
| 9746 | |
| 9747 | |
| 9748 | /* returns the available length of the body. This requires that the body |
| 9749 | * has been waited for using http-buffer-request. |
| 9750 | */ |
| 9751 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9752 | 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] | 9753 | { |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9754 | struct http_msg *msg; |
| 9755 | |
| 9756 | CHECK_HTTP_MESSAGE_FIRST(); |
| 9757 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9758 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 9759 | msg = &smp->strm->txn->req; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9760 | else |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 9761 | msg = &smp->strm->txn->rsp; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9762 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9763 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9764 | smp->data.u.sint = http_body_bytes(msg); |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9765 | |
| 9766 | smp->flags = SMP_F_VOL_TEST; |
| 9767 | return 1; |
| 9768 | } |
| 9769 | |
| 9770 | |
| 9771 | /* returns the advertised length of the body, or the advertised size of the |
| 9772 | * chunks available in the buffer. This requires that the body has been waited |
| 9773 | * for using http-buffer-request. |
| 9774 | */ |
| 9775 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9776 | 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] | 9777 | { |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9778 | struct http_msg *msg; |
| 9779 | |
| 9780 | CHECK_HTTP_MESSAGE_FIRST(); |
| 9781 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9782 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 9783 | msg = &smp->strm->txn->req; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9784 | else |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 9785 | msg = &smp->strm->txn->rsp; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9786 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9787 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9788 | smp->data.u.sint = msg->body_len; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9789 | |
| 9790 | smp->flags = SMP_F_VOL_TEST; |
| 9791 | return 1; |
| 9792 | } |
| 9793 | |
| 9794 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9795 | /* 4. Check on URL/URI. A pointer to the URI is stored. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 9796 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9797 | 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] | 9798 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 9799 | struct http_txn *txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9800 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9801 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9802 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 9803 | txn = smp->strm->txn; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9804 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9805 | smp->data.u.str.len = txn->req.sl.rq.u_l; |
| 9806 | 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] | 9807 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9808 | return 1; |
| 9809 | } |
| 9810 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9811 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9812 | 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] | 9813 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 9814 | struct http_txn *txn; |
Willy Tarreau | 4c804ec | 2013-09-30 14:37:14 +0200 | [diff] [blame] | 9815 | struct sockaddr_storage addr; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9816 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9817 | CHECK_HTTP_MESSAGE_FIRST(); |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9818 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 9819 | txn = smp->strm->txn; |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 9820 | 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] | 9821 | if (((struct sockaddr_in *)&addr)->sin_family != AF_INET) |
Willy Tarreau | f4362b3 | 2011-12-16 17:49:52 +0100 | [diff] [blame] | 9822 | return 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9823 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9824 | smp->data.type = SMP_T_IPV4; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9825 | smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9826 | smp->flags = 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9827 | return 1; |
| 9828 | } |
| 9829 | |
| 9830 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9831 | 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] | 9832 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 9833 | struct http_txn *txn; |
Willy Tarreau | 4c804ec | 2013-09-30 14:37:14 +0200 | [diff] [blame] | 9834 | struct sockaddr_storage addr; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9835 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9836 | CHECK_HTTP_MESSAGE_FIRST(); |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9837 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 9838 | txn = smp->strm->txn; |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 9839 | 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] | 9840 | if (((struct sockaddr_in *)&addr)->sin_family != AF_INET) |
| 9841 | return 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9842 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9843 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9844 | smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port); |
Willy Tarreau | 21e5b0e | 2012-04-23 19:25:44 +0200 | [diff] [blame] | 9845 | smp->flags = 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9846 | return 1; |
| 9847 | } |
| 9848 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9849 | /* Fetch an HTTP header. A pointer to the beginning of the value is returned. |
| 9850 | * Accepts an optional argument of type string containing the header field name, |
| 9851 | * and an optional argument of type signed or unsigned integer to request an |
| 9852 | * 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] | 9853 | * headers are considered from the first one. It does not stop on commas and |
| 9854 | * returns full lines instead (useful for User-Agent or Date for example). |
| 9855 | */ |
| 9856 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9857 | 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] | 9858 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 9859 | struct hdr_idx *idx; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 9860 | struct hdr_ctx *ctx = smp->ctx.a[0]; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 9861 | const struct http_msg *msg; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 9862 | int occ = 0; |
| 9863 | const char *name_str = NULL; |
| 9864 | int name_len = 0; |
| 9865 | |
| 9866 | if (!ctx) { |
| 9867 | /* first call */ |
| 9868 | ctx = &static_hdr_ctx; |
| 9869 | ctx->idx = 0; |
| 9870 | smp->ctx.a[0] = ctx; |
| 9871 | } |
| 9872 | |
| 9873 | if (args) { |
| 9874 | if (args[0].type != ARGT_STR) |
| 9875 | return 0; |
| 9876 | name_str = args[0].data.str.str; |
| 9877 | name_len = args[0].data.str.len; |
| 9878 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 9879 | if (args[1].type == ARGT_SINT) |
| 9880 | occ = args[1].data.sint; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 9881 | } |
| 9882 | |
| 9883 | CHECK_HTTP_MESSAGE_FIRST(); |
| 9884 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 9885 | idx = &smp->strm->txn->hdr_idx; |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9886 | 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] | 9887 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 9888 | if (ctx && !(smp->flags & SMP_F_NOT_LAST)) |
| 9889 | /* search for header from the beginning */ |
| 9890 | ctx->idx = 0; |
| 9891 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9892 | if (!occ && !(smp->opt & SMP_OPT_ITERATE)) |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 9893 | /* no explicit occurrence and single fetch => last header by default */ |
| 9894 | occ = -1; |
| 9895 | |
| 9896 | if (!occ) |
| 9897 | /* prepare to report multiple occurrences for ACL fetches */ |
| 9898 | smp->flags |= SMP_F_NOT_LAST; |
| 9899 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9900 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9901 | smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9902 | 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] | 9903 | return 1; |
| 9904 | |
| 9905 | smp->flags &= ~SMP_F_NOT_LAST; |
| 9906 | return 0; |
| 9907 | } |
| 9908 | |
| 9909 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
| 9910 | * Accepts exactly 1 argument of type string. It does not stop on commas and |
| 9911 | * returns full lines instead (useful for User-Agent or Date for example). |
| 9912 | */ |
| 9913 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9914 | 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] | 9915 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 9916 | struct hdr_idx *idx; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 9917 | struct hdr_ctx ctx; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 9918 | const struct http_msg *msg; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 9919 | int cnt; |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 9920 | const char *name = NULL; |
| 9921 | int len = 0; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 9922 | |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 9923 | if (args && args->type == ARGT_STR) { |
| 9924 | name = args->data.str.str; |
| 9925 | len = args->data.str.len; |
| 9926 | } |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 9927 | |
| 9928 | CHECK_HTTP_MESSAGE_FIRST(); |
| 9929 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 9930 | idx = &smp->strm->txn->hdr_idx; |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9931 | 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] | 9932 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 9933 | ctx.idx = 0; |
| 9934 | cnt = 0; |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 9935 | 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] | 9936 | cnt++; |
| 9937 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9938 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9939 | smp->data.u.sint = cnt; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 9940 | smp->flags = SMP_F_VOL_HDR; |
| 9941 | return 1; |
| 9942 | } |
| 9943 | |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 9944 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9945 | 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] | 9946 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 9947 | struct hdr_idx *idx; |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 9948 | struct hdr_ctx ctx; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 9949 | const struct http_msg *msg; |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 9950 | struct chunk *temp; |
| 9951 | char del = ','; |
| 9952 | |
| 9953 | if (args && args->type == ARGT_STR) |
| 9954 | del = *args[0].data.str.str; |
| 9955 | |
| 9956 | CHECK_HTTP_MESSAGE_FIRST(); |
| 9957 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 9958 | idx = &smp->strm->txn->hdr_idx; |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9959 | 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] | 9960 | |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 9961 | temp = get_trash_chunk(); |
| 9962 | |
| 9963 | ctx.idx = 0; |
| 9964 | while (http_find_next_header(msg->chn->buf->p, idx, &ctx)) { |
| 9965 | if (temp->len) |
| 9966 | temp->str[temp->len++] = del; |
| 9967 | memcpy(temp->str + temp->len, ctx.line, ctx.del); |
| 9968 | temp->len += ctx.del; |
| 9969 | } |
| 9970 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9971 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9972 | smp->data.u.str.str = temp->str; |
| 9973 | smp->data.u.str.len = temp->len; |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 9974 | smp->flags = SMP_F_VOL_HDR; |
| 9975 | return 1; |
| 9976 | } |
| 9977 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 9978 | /* Fetch an HTTP header. A pointer to the beginning of the value is returned. |
| 9979 | * Accepts an optional argument of type string containing the header field name, |
| 9980 | * and an optional argument of type signed or unsigned integer to request an |
| 9981 | * 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] | 9982 | * headers are considered from the first one. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9983 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9984 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9985 | 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] | 9986 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 9987 | struct hdr_idx *idx; |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 9988 | struct hdr_ctx *ctx = smp->ctx.a[0]; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 9989 | const struct http_msg *msg; |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9990 | int occ = 0; |
| 9991 | const char *name_str = NULL; |
| 9992 | int name_len = 0; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 9993 | |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 9994 | if (!ctx) { |
| 9995 | /* first call */ |
| 9996 | ctx = &static_hdr_ctx; |
| 9997 | ctx->idx = 0; |
Willy Tarreau | ffb6f08 | 2013-04-02 23:16:53 +0200 | [diff] [blame] | 9998 | smp->ctx.a[0] = ctx; |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 9999 | } |
| 10000 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10001 | if (args) { |
| 10002 | if (args[0].type != ARGT_STR) |
| 10003 | return 0; |
| 10004 | name_str = args[0].data.str.str; |
| 10005 | name_len = args[0].data.str.len; |
| 10006 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 10007 | if (args[1].type == ARGT_SINT) |
| 10008 | occ = args[1].data.sint; |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10009 | } |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10010 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10011 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10012 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10013 | idx = &smp->strm->txn->hdr_idx; |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10014 | 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] | 10015 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10016 | if (ctx && !(smp->flags & SMP_F_NOT_LAST)) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10017 | /* search for header from the beginning */ |
| 10018 | ctx->idx = 0; |
| 10019 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10020 | if (!occ && !(smp->opt & SMP_OPT_ITERATE)) |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10021 | /* no explicit occurrence and single fetch => last header by default */ |
| 10022 | occ = -1; |
| 10023 | |
| 10024 | if (!occ) |
| 10025 | /* prepare to report multiple occurrences for ACL fetches */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10026 | smp->flags |= SMP_F_NOT_LAST; |
Willy Tarreau | 664092c | 2011-12-16 19:11:42 +0100 | [diff] [blame] | 10027 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10028 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10029 | smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10030 | 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] | 10031 | return 1; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10032 | |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10033 | smp->flags &= ~SMP_F_NOT_LAST; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10034 | return 0; |
| 10035 | } |
| 10036 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10037 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10038 | * Accepts exactly 1 argument of type string. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10039 | */ |
| 10040 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10041 | 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] | 10042 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10043 | struct hdr_idx *idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10044 | struct hdr_ctx ctx; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10045 | const struct http_msg *msg; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10046 | int cnt; |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10047 | const char *name = NULL; |
| 10048 | int len = 0; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10049 | |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10050 | if (args && args->type == ARGT_STR) { |
| 10051 | name = args->data.str.str; |
| 10052 | len = args->data.str.len; |
| 10053 | } |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10054 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10055 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10056 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10057 | idx = &smp->strm->txn->hdr_idx; |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10058 | 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] | 10059 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10060 | ctx.idx = 0; |
| 10061 | cnt = 0; |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10062 | while (http_find_header2(name, len, msg->chn->buf->p, idx, &ctx)) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10063 | cnt++; |
| 10064 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10065 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10066 | smp->data.u.sint = cnt; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10067 | smp->flags = SMP_F_VOL_HDR; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10068 | return 1; |
| 10069 | } |
| 10070 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10071 | /* Fetch an HTTP header's integer value. The integer value is returned. It |
| 10072 | * takes a mandatory argument of type string and an optional one of type int |
| 10073 | * to designate a specific occurrence. It returns an unsigned integer, which |
| 10074 | * may or may not be appropriate for everything. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10075 | */ |
| 10076 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10077 | 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] | 10078 | { |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10079 | int ret = smp_fetch_hdr(args, smp, kw, private); |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10080 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10081 | if (ret > 0) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10082 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10083 | 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] | 10084 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10085 | |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10086 | return ret; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10087 | } |
| 10088 | |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 10089 | /* Fetch an HTTP header's IP value. takes a mandatory argument of type string |
| 10090 | * and an optional one of type int to designate a specific occurrence. |
| 10091 | * It returns an IPv4 or IPv6 address. |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 10092 | */ |
| 10093 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10094 | 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] | 10095 | { |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10096 | int ret; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10097 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10098 | while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10099 | if (url2ipv4((char *)smp->data.u.str.str, &smp->data.u.ipv4)) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10100 | smp->data.type = SMP_T_IPV4; |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10101 | break; |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 10102 | } else { |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 10103 | struct chunk *temp = get_trash_chunk(); |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10104 | if (smp->data.u.str.len < temp->size - 1) { |
| 10105 | memcpy(temp->str, smp->data.u.str.str, smp->data.u.str.len); |
| 10106 | temp->str[smp->data.u.str.len] = '\0'; |
| 10107 | if (inet_pton(AF_INET6, temp->str, &smp->data.u.ipv6)) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10108 | smp->data.type = SMP_T_IPV6; |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 10109 | break; |
| 10110 | } |
| 10111 | } |
| 10112 | } |
| 10113 | |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10114 | /* if the header doesn't match an IP address, fetch next one */ |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10115 | if (!(smp->flags & SMP_F_NOT_LAST)) |
| 10116 | return 0; |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 10117 | } |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10118 | return ret; |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 10119 | } |
| 10120 | |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10121 | /* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at |
| 10122 | * the first '/' after the possible hostname, and ends before the possible '?'. |
| 10123 | */ |
| 10124 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10125 | 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] | 10126 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10127 | struct http_txn *txn; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10128 | char *ptr, *end; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10129 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10130 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10131 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10132 | txn = smp->strm->txn; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10133 | 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] | 10134 | ptr = http_get_path(txn); |
| 10135 | if (!ptr) |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10136 | return 0; |
| 10137 | |
| 10138 | /* OK, we got the '/' ! */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10139 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10140 | smp->data.u.str.str = ptr; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10141 | |
| 10142 | while (ptr < end && *ptr != '?') |
| 10143 | ptr++; |
| 10144 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10145 | smp->data.u.str.len = ptr - smp->data.u.str.str; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10146 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10147 | return 1; |
| 10148 | } |
| 10149 | |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10150 | /* This produces a concatenation of the first occurrence of the Host header |
| 10151 | * followed by the path component if it begins with a slash ('/'). This means |
| 10152 | * that '*' will not be added, resulting in exactly the first Host entry. |
| 10153 | * If no Host header is found, then the path is returned as-is. The returned |
| 10154 | * 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] | 10155 | * The returned sample is of type string. |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10156 | */ |
| 10157 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10158 | 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] | 10159 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10160 | struct http_txn *txn; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10161 | char *ptr, *end, *beg; |
| 10162 | struct hdr_ctx ctx; |
Willy Tarreau | 3caf2af | 2014-06-24 17:27:02 +0200 | [diff] [blame] | 10163 | struct chunk *temp; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10164 | |
| 10165 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10166 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10167 | txn = smp->strm->txn; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10168 | ctx.idx = 0; |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 10169 | 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] | 10170 | return smp_fetch_path(args, smp, kw, private); |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10171 | |
| 10172 | /* 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] | 10173 | temp = get_trash_chunk(); |
| 10174 | memcpy(temp->str, ctx.line + ctx.val, ctx.vlen); |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10175 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10176 | smp->data.u.str.str = temp->str; |
| 10177 | smp->data.u.str.len = ctx.vlen; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10178 | |
| 10179 | /* now retrieve the path */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 10180 | 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] | 10181 | beg = http_get_path(txn); |
| 10182 | if (!beg) |
| 10183 | beg = end; |
| 10184 | |
| 10185 | for (ptr = beg; ptr < end && *ptr != '?'; ptr++); |
| 10186 | |
| 10187 | if (beg < ptr && *beg == '/') { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10188 | memcpy(smp->data.u.str.str + smp->data.u.str.len, beg, ptr - beg); |
| 10189 | smp->data.u.str.len += ptr - beg; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10190 | } |
| 10191 | |
| 10192 | smp->flags = SMP_F_VOL_1ST; |
| 10193 | return 1; |
| 10194 | } |
| 10195 | |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 10196 | /* This produces a 32-bit hash of the concatenation of the first occurrence of |
| 10197 | * the Host header followed by the path component if it begins with a slash ('/'). |
| 10198 | * This means that '*' will not be added, resulting in exactly the first Host |
| 10199 | * 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] | 10200 | * is hashed using the path hash followed by a full avalanche hash and provides a |
| 10201 | * 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] | 10202 | * high-traffic sites without having to store whole paths. |
| 10203 | */ |
Thierry FOURNIER | 055b9d5 | 2014-07-15 16:11:07 +0200 | [diff] [blame] | 10204 | int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10205 | 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] | 10206 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10207 | struct http_txn *txn; |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 10208 | struct hdr_ctx ctx; |
| 10209 | unsigned int hash = 0; |
| 10210 | char *ptr, *beg, *end; |
| 10211 | int len; |
| 10212 | |
| 10213 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10214 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10215 | txn = smp->strm->txn; |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 10216 | ctx.idx = 0; |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 10217 | 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] | 10218 | /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */ |
| 10219 | ptr = ctx.line + ctx.val; |
| 10220 | len = ctx.vlen; |
| 10221 | while (len--) |
| 10222 | hash = *(ptr++) + (hash << 6) + (hash << 16) - hash; |
| 10223 | } |
| 10224 | |
| 10225 | /* now retrieve the path */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 10226 | 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] | 10227 | beg = http_get_path(txn); |
| 10228 | if (!beg) |
| 10229 | beg = end; |
| 10230 | |
| 10231 | for (ptr = beg; ptr < end && *ptr != '?'; ptr++); |
| 10232 | |
| 10233 | if (beg < ptr && *beg == '/') { |
| 10234 | while (beg < ptr) |
| 10235 | hash = *(beg++) + (hash << 6) + (hash << 16) - hash; |
| 10236 | } |
| 10237 | hash = full_hash(hash); |
| 10238 | |
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 = hash; |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 10241 | smp->flags = SMP_F_VOL_1ST; |
| 10242 | return 1; |
| 10243 | } |
| 10244 | |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10245 | /* 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] | 10246 | * path as returned by smp_fetch_base32(). The idea is to have per-source and |
| 10247 | * per-path counters. The result is a binary block from 8 to 20 bytes depending |
| 10248 | * 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] | 10249 | * that in environments where IPv6 is insignificant, truncating the output to |
| 10250 | * 8 bytes would still work. |
| 10251 | */ |
| 10252 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10253 | 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] | 10254 | { |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 10255 | struct chunk *temp; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10256 | struct connection *cli_conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10257 | |
| 10258 | if (!cli_conn) |
| 10259 | return 0; |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10260 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10261 | if (!smp_fetch_base32(args, smp, kw, private)) |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10262 | return 0; |
| 10263 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 10264 | temp = get_trash_chunk(); |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10265 | *(unsigned int *)temp->str = htonl(smp->data.u.sint); |
Willy Tarreau | 5ad6e1d | 2014-07-15 21:34:06 +0200 | [diff] [blame] | 10266 | temp->len += sizeof(unsigned int); |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10267 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10268 | switch (cli_conn->addr.from.ss_family) { |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10269 | case AF_INET: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10270 | 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] | 10271 | temp->len += 4; |
| 10272 | break; |
| 10273 | case AF_INET6: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10274 | 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] | 10275 | temp->len += 16; |
| 10276 | break; |
| 10277 | default: |
| 10278 | return 0; |
| 10279 | } |
| 10280 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10281 | smp->data.u.str = *temp; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10282 | smp->data.type = SMP_T_BIN; |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10283 | return 1; |
| 10284 | } |
| 10285 | |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 10286 | /* Extracts the query string, which comes after the question mark '?'. If no |
| 10287 | * question mark is found, nothing is returned. Otherwise it returns a sample |
| 10288 | * of type string carrying the whole query string. |
| 10289 | */ |
| 10290 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10291 | 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] | 10292 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10293 | struct http_txn *txn; |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 10294 | char *ptr, *end; |
| 10295 | |
| 10296 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10297 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10298 | txn = smp->strm->txn; |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 10299 | ptr = txn->req.chn->buf->p + txn->req.sl.rq.u; |
| 10300 | end = ptr + txn->req.sl.rq.u_l; |
| 10301 | |
| 10302 | /* look up the '?' */ |
| 10303 | do { |
| 10304 | if (ptr == end) |
| 10305 | return 0; |
| 10306 | } while (*ptr++ != '?'); |
| 10307 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10308 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10309 | smp->data.u.str.str = ptr; |
| 10310 | smp->data.u.str.len = end - ptr; |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 10311 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
| 10312 | return 1; |
| 10313 | } |
| 10314 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10315 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10316 | 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] | 10317 | { |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10318 | /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged |
| 10319 | * as a layer7 ACL, which involves automatic allocation of hdr_idx. |
| 10320 | */ |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10321 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10322 | CHECK_HTTP_MESSAGE_FIRST_PERM(); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10323 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10324 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10325 | smp->data.u.sint = 1; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10326 | return 1; |
| 10327 | } |
| 10328 | |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 10329 | /* return a valid test if the current request is the first one on the connection */ |
| 10330 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10331 | 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] | 10332 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10333 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10334 | smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST); |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 10335 | return 1; |
| 10336 | } |
| 10337 | |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10338 | /* Accepts exactly 1 argument of type userlist */ |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 10339 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10340 | 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] | 10341 | { |
| 10342 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10343 | if (!args || args->type != ARGT_USR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10344 | return 0; |
| 10345 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10346 | CHECK_HTTP_MESSAGE_FIRST(); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 10347 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10348 | if (!get_http_auth(smp->strm)) |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 10349 | return 0; |
| 10350 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10351 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10352 | 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] | 10353 | smp->strm->txn->auth.pass); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 10354 | return 1; |
| 10355 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10356 | |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10357 | /* Accepts exactly 1 argument of type userlist */ |
| 10358 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10359 | 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] | 10360 | { |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10361 | if (!args || args->type != ARGT_USR) |
| 10362 | return 0; |
| 10363 | |
| 10364 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10365 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10366 | if (!get_http_auth(smp->strm)) |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10367 | return 0; |
| 10368 | |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10369 | /* if the user does not belong to the userlist or has a wrong password, |
| 10370 | * report that it unconditionally does not match. Otherwise we return |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 10371 | * a string containing the username. |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10372 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10373 | if (!check_user(args->data.usr, smp->strm->txn->auth.user, |
| 10374 | smp->strm->txn->auth.pass)) |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 10375 | return 0; |
| 10376 | |
| 10377 | /* pat_match_auth() will need the user list */ |
| 10378 | smp->ctx.a[0] = args->data.usr; |
| 10379 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10380 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10381 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10382 | smp->data.u.str.str = smp->strm->txn->auth.user; |
| 10383 | smp->data.u.str.len = strlen(smp->strm->txn->auth.user); |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10384 | |
| 10385 | return 1; |
| 10386 | } |
| 10387 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10388 | /* Try to find the next occurrence of a cookie name in a cookie header value. |
| 10389 | * The lookup begins at <hdr>. The pointer and size of the next occurrence of |
| 10390 | * the cookie value is returned into *value and *value_l, and the function |
| 10391 | * returns a pointer to the next pointer to search from if the value was found. |
| 10392 | * Otherwise if the cookie was not found, NULL is returned and neither value |
| 10393 | * nor value_l are touched. The input <hdr> string should first point to the |
| 10394 | * header's value, and the <hdr_end> pointer must point to the first character |
| 10395 | * not part of the value. <list> must be non-zero if value may represent a list |
| 10396 | * of values (cookie headers). This makes it faster to abort parsing when no |
| 10397 | * list is expected. |
| 10398 | */ |
David Carlier | 4686f79 | 2015-09-25 14:10:50 +0100 | [diff] [blame] | 10399 | char * |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10400 | extract_cookie_value(char *hdr, const char *hdr_end, |
| 10401 | char *cookie_name, size_t cookie_name_l, int list, |
Willy Tarreau | 3fb818c | 2012-04-11 17:21:08 +0200 | [diff] [blame] | 10402 | char **value, int *value_l) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10403 | { |
| 10404 | char *equal, *att_end, *att_beg, *val_beg, *val_end; |
| 10405 | char *next; |
| 10406 | |
| 10407 | /* we search at least a cookie name followed by an equal, and more |
| 10408 | * generally something like this : |
| 10409 | * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n |
| 10410 | */ |
| 10411 | for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) { |
| 10412 | /* Iterate through all cookies on this line */ |
| 10413 | |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 10414 | while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg)) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10415 | att_beg++; |
| 10416 | |
| 10417 | /* find att_end : this is the first character after the last non |
| 10418 | * space before the equal. It may be equal to hdr_end. |
| 10419 | */ |
| 10420 | equal = att_end = att_beg; |
| 10421 | |
| 10422 | while (equal < hdr_end) { |
| 10423 | if (*equal == '=' || *equal == ';' || (list && *equal == ',')) |
| 10424 | break; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 10425 | if (HTTP_IS_SPHT(*equal++)) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10426 | continue; |
| 10427 | att_end = equal; |
| 10428 | } |
| 10429 | |
| 10430 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 10431 | * is between <att_beg> and <equal>, both may be identical. |
| 10432 | */ |
| 10433 | |
| 10434 | /* look for end of cookie if there is an equal sign */ |
| 10435 | if (equal < hdr_end && *equal == '=') { |
| 10436 | /* look for the beginning of the value */ |
| 10437 | val_beg = equal + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 10438 | while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg)) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10439 | val_beg++; |
| 10440 | |
| 10441 | /* find the end of the value, respecting quotes */ |
| 10442 | next = find_cookie_value_end(val_beg, hdr_end); |
| 10443 | |
| 10444 | /* make val_end point to the first white space or delimitor after the value */ |
| 10445 | val_end = next; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 10446 | while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1))) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10447 | val_end--; |
| 10448 | } else { |
| 10449 | val_beg = val_end = next = equal; |
| 10450 | } |
| 10451 | |
| 10452 | /* We have nothing to do with attributes beginning with '$'. However, |
| 10453 | * they will automatically be removed if a header before them is removed, |
| 10454 | * since they're supposed to be linked together. |
| 10455 | */ |
| 10456 | if (*att_beg == '$') |
| 10457 | continue; |
| 10458 | |
| 10459 | /* Ignore cookies with no equal sign */ |
| 10460 | if (equal == next) |
| 10461 | continue; |
| 10462 | |
| 10463 | /* Now we have the cookie name between att_beg and att_end, and |
| 10464 | * its value between val_beg and val_end. |
| 10465 | */ |
| 10466 | |
| 10467 | if (att_end - att_beg == cookie_name_l && |
| 10468 | memcmp(att_beg, cookie_name, cookie_name_l) == 0) { |
| 10469 | /* let's return this value and indicate where to go on from */ |
| 10470 | *value = val_beg; |
| 10471 | *value_l = val_end - val_beg; |
| 10472 | return next + 1; |
| 10473 | } |
| 10474 | |
| 10475 | /* Set-Cookie headers only have the name in the first attr=value part */ |
| 10476 | if (!list) |
| 10477 | break; |
| 10478 | } |
| 10479 | |
| 10480 | return NULL; |
| 10481 | } |
| 10482 | |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10483 | /* Fetch a captured HTTP request header. The index is the position of |
| 10484 | * the "capture" option in the configuration file |
| 10485 | */ |
| 10486 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10487 | 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] | 10488 | { |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10489 | struct proxy *fe = strm_fe(smp->strm); |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10490 | int idx; |
| 10491 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 10492 | if (!args || args->type != ARGT_SINT) |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10493 | return 0; |
| 10494 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 10495 | idx = args->data.sint; |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10496 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10497 | 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] | 10498 | return 0; |
| 10499 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10500 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10501 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10502 | smp->data.u.str.str = smp->strm->req_cap[idx]; |
| 10503 | smp->data.u.str.len = strlen(smp->strm->req_cap[idx]); |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10504 | |
| 10505 | return 1; |
| 10506 | } |
| 10507 | |
| 10508 | /* Fetch a captured HTTP response header. The index is the position of |
| 10509 | * the "capture" option in the configuration file |
| 10510 | */ |
| 10511 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10512 | 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] | 10513 | { |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10514 | struct proxy *fe = strm_fe(smp->strm); |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10515 | int idx; |
| 10516 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 10517 | if (!args || args->type != ARGT_SINT) |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10518 | return 0; |
| 10519 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 10520 | idx = args->data.sint; |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10521 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10522 | 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] | 10523 | return 0; |
| 10524 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10525 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10526 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10527 | smp->data.u.str.str = smp->strm->res_cap[idx]; |
| 10528 | smp->data.u.str.len = strlen(smp->strm->res_cap[idx]); |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10529 | |
| 10530 | return 1; |
| 10531 | } |
| 10532 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10533 | /* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */ |
| 10534 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10535 | 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] | 10536 | { |
| 10537 | struct chunk *temp; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10538 | struct http_txn *txn = smp->strm->txn; |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10539 | char *ptr; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10540 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10541 | if (!txn || !txn->uri) |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10542 | return 0; |
| 10543 | |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10544 | ptr = txn->uri; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10545 | |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10546 | while (*ptr != ' ' && *ptr != '\0') /* find first space */ |
| 10547 | ptr++; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10548 | |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10549 | temp = get_trash_chunk(); |
| 10550 | temp->str = txn->uri; |
| 10551 | temp->len = ptr - txn->uri; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10552 | smp->data.u.str = *temp; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10553 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10554 | smp->flags = SMP_F_CONST; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10555 | |
| 10556 | return 1; |
| 10557 | |
| 10558 | } |
| 10559 | |
| 10560 | /* Extracts the path in the HTTP request, the txn->uri should be filled before the call */ |
| 10561 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10562 | 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] | 10563 | { |
| 10564 | struct chunk *temp; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10565 | struct http_txn *txn = smp->strm->txn; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10566 | char *ptr; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10567 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10568 | if (!txn || !txn->uri) |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10569 | return 0; |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10570 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10571 | ptr = txn->uri; |
| 10572 | |
| 10573 | while (*ptr != ' ' && *ptr != '\0') /* find first space */ |
| 10574 | ptr++; |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10575 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10576 | if (!*ptr) |
| 10577 | return 0; |
| 10578 | |
| 10579 | ptr++; /* skip the space */ |
| 10580 | |
| 10581 | temp = get_trash_chunk(); |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10582 | ptr = temp->str = http_get_path_from_string(ptr); |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10583 | if (!ptr) |
| 10584 | return 0; |
| 10585 | while (*ptr != ' ' && *ptr != '\0') /* find space after URI */ |
| 10586 | ptr++; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10587 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10588 | smp->data.u.str = *temp; |
| 10589 | smp->data.u.str.len = ptr - temp->str; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10590 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10591 | smp->flags = SMP_F_CONST; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10592 | |
| 10593 | return 1; |
| 10594 | } |
| 10595 | |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 10596 | /* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it |
| 10597 | * as a string (either "HTTP/1.0" or "HTTP/1.1"). |
| 10598 | */ |
| 10599 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10600 | 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] | 10601 | { |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10602 | struct http_txn *txn = smp->strm->txn; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 10603 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10604 | if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST) |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 10605 | return 0; |
| 10606 | |
| 10607 | if (txn->req.flags & HTTP_MSGF_VER_11) |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10608 | smp->data.u.str.str = "HTTP/1.1"; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 10609 | else |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10610 | smp->data.u.str.str = "HTTP/1.0"; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 10611 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10612 | smp->data.u.str.len = 8; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10613 | smp->data.type = SMP_T_STR; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 10614 | smp->flags = SMP_F_CONST; |
| 10615 | return 1; |
| 10616 | |
| 10617 | } |
| 10618 | |
| 10619 | /* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it |
| 10620 | * as a string (either "HTTP/1.0" or "HTTP/1.1"). |
| 10621 | */ |
| 10622 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10623 | 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] | 10624 | { |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10625 | struct http_txn *txn = smp->strm->txn; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 10626 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10627 | if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST) |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 10628 | return 0; |
| 10629 | |
| 10630 | if (txn->rsp.flags & HTTP_MSGF_VER_11) |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10631 | smp->data.u.str.str = "HTTP/1.1"; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 10632 | else |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10633 | smp->data.u.str.str = "HTTP/1.0"; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 10634 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10635 | smp->data.u.str.len = 8; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10636 | smp->data.type = SMP_T_STR; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 10637 | smp->flags = SMP_F_CONST; |
| 10638 | return 1; |
| 10639 | |
| 10640 | } |
| 10641 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10642 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10643 | /* 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] | 10644 | * 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] | 10645 | * 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] | 10646 | * 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] | 10647 | * 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] | 10648 | * Accepts exactly 1 argument of type string. If the input options indicate |
| 10649 | * 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] | 10650 | * The returned sample is of type CSTR. Can be used to parse cookies in other |
| 10651 | * files. |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10652 | */ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10653 | 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] | 10654 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10655 | struct http_txn *txn; |
| 10656 | struct hdr_idx *idx; |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 10657 | struct hdr_ctx *ctx = smp->ctx.a[2]; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10658 | const struct http_msg *msg; |
| 10659 | const char *hdr_name; |
| 10660 | int hdr_name_len; |
| 10661 | char *sol; |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10662 | int occ = 0; |
| 10663 | int found = 0; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10664 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10665 | if (!args || args->type != ARGT_STR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10666 | return 0; |
| 10667 | |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 10668 | if (!ctx) { |
| 10669 | /* first call */ |
| 10670 | ctx = &static_hdr_ctx; |
| 10671 | ctx->idx = 0; |
| 10672 | smp->ctx.a[2] = ctx; |
| 10673 | } |
| 10674 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10675 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10676 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10677 | txn = smp->strm->txn; |
| 10678 | idx = &smp->strm->txn->hdr_idx; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10679 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10680 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10681 | msg = &txn->req; |
| 10682 | hdr_name = "Cookie"; |
| 10683 | hdr_name_len = 6; |
| 10684 | } else { |
| 10685 | msg = &txn->rsp; |
| 10686 | hdr_name = "Set-Cookie"; |
| 10687 | hdr_name_len = 10; |
| 10688 | } |
| 10689 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10690 | if (!occ && !(smp->opt & SMP_OPT_ITERATE)) |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10691 | /* no explicit occurrence and single fetch => last cookie by default */ |
| 10692 | occ = -1; |
| 10693 | |
| 10694 | /* OK so basically here, either we want only one value and it's the |
| 10695 | * last one, or we want to iterate over all of them and we fetch the |
| 10696 | * next one. |
| 10697 | */ |
| 10698 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10699 | sol = msg->chn->buf->p; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10700 | if (!(smp->flags & SMP_F_NOT_LAST)) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10701 | /* search for the header from the beginning, we must first initialize |
| 10702 | * the search parameters. |
| 10703 | */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10704 | smp->ctx.a[0] = NULL; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10705 | ctx->idx = 0; |
| 10706 | } |
| 10707 | |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10708 | smp->flags |= SMP_F_VOL_HDR; |
| 10709 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10710 | while (1) { |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10711 | /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */ |
| 10712 | if (!smp->ctx.a[0]) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10713 | if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx)) |
| 10714 | goto out; |
| 10715 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10716 | if (ctx->vlen < args->data.str.len + 1) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10717 | continue; |
| 10718 | |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10719 | smp->ctx.a[0] = ctx->line + ctx->val; |
| 10720 | smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10721 | } |
| 10722 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10723 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10724 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10725 | 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] | 10726 | args->data.str.str, args->data.str.len, |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10727 | (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ, |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10728 | &smp->data.u.str.str, |
| 10729 | &smp->data.u.str.len); |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10730 | if (smp->ctx.a[0]) { |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10731 | found = 1; |
| 10732 | if (occ >= 0) { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10733 | /* one value was returned into smp->data.u.str.{str,len} */ |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10734 | smp->flags |= SMP_F_NOT_LAST; |
| 10735 | return 1; |
| 10736 | } |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10737 | } |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10738 | /* if we're looking for last occurrence, let's loop */ |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10739 | } |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10740 | /* all cookie headers and values were scanned. If we're looking for the |
| 10741 | * last occurrence, we may return it now. |
| 10742 | */ |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10743 | out: |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10744 | smp->flags &= ~SMP_F_NOT_LAST; |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10745 | return found; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10746 | } |
| 10747 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10748 | /* 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] | 10749 | * 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] | 10750 | * multiple cookies may be parsed on the same line. The returned sample is of |
| 10751 | * type UINT. Accepts exactly 1 argument of type string. |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10752 | */ |
| 10753 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10754 | 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] | 10755 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10756 | struct http_txn *txn; |
| 10757 | struct hdr_idx *idx; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10758 | struct hdr_ctx ctx; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10759 | const struct http_msg *msg; |
| 10760 | const char *hdr_name; |
| 10761 | int hdr_name_len; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10762 | int cnt; |
| 10763 | char *val_beg, *val_end; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10764 | char *sol; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10765 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10766 | if (!args || args->type != ARGT_STR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10767 | return 0; |
| 10768 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10769 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10770 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10771 | txn = smp->strm->txn; |
| 10772 | idx = &smp->strm->txn->hdr_idx; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10773 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10774 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10775 | msg = &txn->req; |
| 10776 | hdr_name = "Cookie"; |
| 10777 | hdr_name_len = 6; |
| 10778 | } else { |
| 10779 | msg = &txn->rsp; |
| 10780 | hdr_name = "Set-Cookie"; |
| 10781 | hdr_name_len = 10; |
| 10782 | } |
| 10783 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10784 | sol = msg->chn->buf->p; |
Willy Tarreau | 46787ed | 2012-04-11 17:28:40 +0200 | [diff] [blame] | 10785 | val_end = val_beg = NULL; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10786 | ctx.idx = 0; |
| 10787 | cnt = 0; |
| 10788 | |
| 10789 | while (1) { |
| 10790 | /* Note: val_beg == NULL every time we need to fetch a new header */ |
| 10791 | if (!val_beg) { |
| 10792 | if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx)) |
| 10793 | break; |
| 10794 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10795 | if (ctx.vlen < args->data.str.len + 1) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10796 | continue; |
| 10797 | |
| 10798 | val_beg = ctx.line + ctx.val; |
| 10799 | val_end = val_beg + ctx.vlen; |
| 10800 | } |
| 10801 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10802 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10803 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10804 | while ((val_beg = extract_cookie_value(val_beg, val_end, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10805 | args->data.str.str, args->data.str.len, |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10806 | (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ, |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10807 | &smp->data.u.str.str, |
| 10808 | &smp->data.u.str.len))) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10809 | cnt++; |
| 10810 | } |
| 10811 | } |
| 10812 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10813 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10814 | smp->data.u.sint = cnt; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10815 | smp->flags |= SMP_F_VOL_HDR; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10816 | return 1; |
| 10817 | } |
| 10818 | |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 10819 | /* Fetch an cookie's integer value. The integer value is returned. It |
| 10820 | * takes a mandatory argument of type string. It relies on smp_fetch_cookie(). |
| 10821 | */ |
| 10822 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10823 | 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] | 10824 | { |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10825 | int ret = smp_fetch_cookie(args, smp, kw, private); |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 10826 | |
| 10827 | if (ret > 0) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10828 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10829 | 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] | 10830 | } |
| 10831 | |
| 10832 | return ret; |
| 10833 | } |
| 10834 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10835 | /************************************************************************/ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 10836 | /* The code below is dedicated to sample fetches */ |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 10837 | /************************************************************************/ |
| 10838 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10839 | /* |
| 10840 | * Given a path string and its length, find the position of beginning of the |
| 10841 | * query string. Returns NULL if no query string is found in the path. |
| 10842 | * |
| 10843 | * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22: |
| 10844 | * |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 10845 | * find_query_string(path, n, '?') points to "yo=mama;ye=daddy" string. |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10846 | */ |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 10847 | 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] | 10848 | { |
| 10849 | char *p; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 10850 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 10851 | p = memchr(path, delim, path_l); |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10852 | return p ? p + 1 : NULL; |
| 10853 | } |
| 10854 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 10855 | static inline int is_param_delimiter(char c, char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10856 | { |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 10857 | return c == '&' || c == ';' || c == delim; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10858 | } |
| 10859 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10860 | /* after increasing a pointer value, it can exceed the first buffer |
| 10861 | * size. This function transform the value of <ptr> according with |
| 10862 | * the expected position. <chunks> is an array of the one or two |
| 10863 | * avalaible chunks. The first value is the start of the first chunk, |
| 10864 | * the second value if the end+1 of the first chunks. The third value |
| 10865 | * is NULL or the start of the second chunk and the fourth value is |
| 10866 | * the end+1 of the second chunk. The function returns 1 if does a |
| 10867 | * wrap, else returns 0. |
| 10868 | */ |
| 10869 | static inline int fix_pointer_if_wrap(const char **chunks, const char **ptr) |
| 10870 | { |
| 10871 | if (*ptr < chunks[1]) |
| 10872 | return 0; |
| 10873 | if (!chunks[2]) |
| 10874 | return 0; |
| 10875 | *ptr = chunks[2] + ( *ptr - chunks[1] ); |
| 10876 | return 1; |
| 10877 | } |
| 10878 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10879 | /* |
| 10880 | * Given a url parameter, find the starting position of the first occurence, |
| 10881 | * or NULL if the parameter is not found. |
| 10882 | * |
| 10883 | * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye", |
| 10884 | * the function will return query_string+8. |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10885 | * |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 10886 | * 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] | 10887 | * or the second chunk. The caller must be check the position before using the |
| 10888 | * result. |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10889 | */ |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10890 | static const char * |
| 10891 | find_url_param_pos(const char **chunks, |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 10892 | const char* url_param_name, size_t url_param_name_l, |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 10893 | char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10894 | { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10895 | const char *pos, *last, *equal; |
| 10896 | const char **bufs = chunks; |
| 10897 | int l1, l2; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10898 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10899 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10900 | pos = bufs[0]; |
| 10901 | last = bufs[1]; |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 10902 | while (pos < last) { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10903 | /* Check the equal. */ |
| 10904 | equal = pos + url_param_name_l; |
| 10905 | if (fix_pointer_if_wrap(chunks, &equal)) { |
| 10906 | if (equal >= chunks[3]) |
| 10907 | return NULL; |
| 10908 | } else { |
| 10909 | if (equal >= chunks[1]) |
| 10910 | return NULL; |
| 10911 | } |
| 10912 | if (*equal == '=') { |
| 10913 | if (pos + url_param_name_l > last) { |
| 10914 | /* process wrap case, we detect a wrap. In this case, the |
| 10915 | * comparison is performed in two parts. |
| 10916 | */ |
| 10917 | |
| 10918 | /* This is the end, we dont have any other chunk. */ |
| 10919 | if (bufs != chunks || !bufs[2]) |
| 10920 | return NULL; |
| 10921 | |
| 10922 | /* Compute the length of each part of the comparison. */ |
| 10923 | l1 = last - pos; |
| 10924 | l2 = url_param_name_l - l1; |
| 10925 | |
| 10926 | /* The second buffer is too short to contain the compared string. */ |
| 10927 | if (bufs[2] + l2 > bufs[3]) |
| 10928 | return NULL; |
| 10929 | |
| 10930 | if (memcmp(pos, url_param_name, l1) == 0 && |
| 10931 | memcmp(bufs[2], url_param_name+l1, l2) == 0) |
| 10932 | return pos; |
| 10933 | |
| 10934 | /* Perform wrapping and jump the string who fail the comparison. */ |
| 10935 | bufs += 2; |
| 10936 | pos = bufs[0] + l2; |
| 10937 | last = bufs[1]; |
| 10938 | |
| 10939 | } else { |
| 10940 | /* process a simple comparison. */ |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 10941 | if (memcmp(pos, url_param_name, url_param_name_l) == 0) |
| 10942 | return pos; |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10943 | pos += url_param_name_l + 1; |
| 10944 | if (fix_pointer_if_wrap(chunks, &pos)) |
| 10945 | last = bufs[2]; |
| 10946 | } |
| 10947 | } |
| 10948 | |
| 10949 | while (1) { |
| 10950 | /* Look for the next delimiter. */ |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 10951 | while (pos < last && !is_param_delimiter(*pos, delim)) |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10952 | pos++; |
| 10953 | if (pos < last) |
| 10954 | break; |
| 10955 | /* process buffer wrapping. */ |
| 10956 | if (bufs != chunks || !bufs[2]) |
| 10957 | return NULL; |
| 10958 | bufs += 2; |
| 10959 | pos = bufs[0]; |
| 10960 | last = bufs[1]; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10961 | } |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10962 | pos++; |
| 10963 | } |
| 10964 | return NULL; |
| 10965 | } |
| 10966 | |
| 10967 | /* |
Cyril Bonté | ce1ef4d | 2015-11-26 21:39:56 +0100 | [diff] [blame] | 10968 | * Given a url parameter name and a query string, find the next value. |
| 10969 | * An empty url_param_name matches the first available parameter. |
| 10970 | * If the parameter is found, 1 is returned and *vstart / *vend are updated to |
| 10971 | * respectively provide a pointer to the value and its end. |
| 10972 | * Otherwise, 0 is returned and vstart/vend are not modified. |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10973 | */ |
| 10974 | static int |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10975 | find_next_url_param(const char **chunks, |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 10976 | const char* url_param_name, size_t url_param_name_l, |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10977 | const char **vstart, const char **vend, char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10978 | { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10979 | const char *arg_start, *qs_end; |
| 10980 | const char *value_start, *value_end; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10981 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10982 | arg_start = chunks[0]; |
| 10983 | qs_end = chunks[1]; |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 10984 | if (url_param_name_l) { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10985 | /* Looks for an argument name. */ |
| 10986 | arg_start = find_url_param_pos(chunks, |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 10987 | url_param_name, url_param_name_l, |
| 10988 | delim); |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10989 | /* Check for wrapping. */ |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 10990 | if (arg_start >= qs_end) |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10991 | qs_end = chunks[3]; |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 10992 | } |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10993 | if (!arg_start) |
| 10994 | return 0; |
| 10995 | |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 10996 | if (!url_param_name_l) { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10997 | while (1) { |
| 10998 | /* looks for the first argument. */ |
| 10999 | value_start = memchr(arg_start, '=', qs_end - arg_start); |
| 11000 | if (!value_start) { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11001 | /* Check for wrapping. */ |
| 11002 | if (arg_start >= chunks[0] && |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 11003 | arg_start < chunks[1] && |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11004 | chunks[2]) { |
| 11005 | arg_start = chunks[2]; |
| 11006 | qs_end = chunks[3]; |
| 11007 | continue; |
| 11008 | } |
| 11009 | return 0; |
| 11010 | } |
| 11011 | break; |
| 11012 | } |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11013 | value_start++; |
| 11014 | } |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11015 | else { |
| 11016 | /* Jump the argument length. */ |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11017 | value_start = arg_start + url_param_name_l + 1; |
| 11018 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11019 | /* Check for pointer wrapping. */ |
| 11020 | if (fix_pointer_if_wrap(chunks, &value_start)) { |
| 11021 | /* Update the end pointer. */ |
| 11022 | qs_end = chunks[3]; |
| 11023 | |
| 11024 | /* Check for overflow. */ |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 11025 | if (value_start >= qs_end) |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11026 | return 0; |
| 11027 | } |
| 11028 | } |
| 11029 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11030 | value_end = value_start; |
| 11031 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11032 | while (1) { |
| 11033 | while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim)) |
| 11034 | value_end++; |
| 11035 | if (value_end < qs_end) |
| 11036 | break; |
| 11037 | /* process buffer wrapping. */ |
| 11038 | if (value_end >= chunks[0] && |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 11039 | value_end < chunks[1] && |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11040 | chunks[2]) { |
| 11041 | value_end = chunks[2]; |
| 11042 | qs_end = chunks[3]; |
| 11043 | continue; |
| 11044 | } |
| 11045 | break; |
| 11046 | } |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11047 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11048 | *vstart = value_start; |
| 11049 | *vend = value_end; |
Cyril Bonté | ce1ef4d | 2015-11-26 21:39:56 +0100 | [diff] [blame] | 11050 | return 1; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11051 | } |
| 11052 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11053 | /* This scans a URL-encoded query string. It takes an optionally wrapping |
| 11054 | * string whose first contigous chunk has its beginning in ctx->a[0] and end |
| 11055 | * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The |
| 11056 | * pointers are updated for next iteration before leaving. |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11057 | */ |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11058 | static int |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11059 | 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] | 11060 | { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11061 | const char *vstart, *vend; |
| 11062 | struct chunk *temp; |
| 11063 | const char **chunks = (const char **)smp->ctx.a; |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11064 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11065 | if (!find_next_url_param(chunks, |
Thierry FOURNIER | 0948d41 | 2015-05-20 15:22:37 +0200 | [diff] [blame] | 11066 | name, name_len, |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11067 | &vstart, &vend, |
Thierry FOURNIER | 0948d41 | 2015-05-20 15:22:37 +0200 | [diff] [blame] | 11068 | delim)) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11069 | return 0; |
| 11070 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11071 | /* Create sample. If the value is contiguous, return the pointer as CONST, |
| 11072 | * if the value is wrapped, copy-it in a buffer. |
| 11073 | */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11074 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11075 | if (chunks[2] && |
| 11076 | vstart >= chunks[0] && vstart <= chunks[1] && |
| 11077 | vend >= chunks[2] && vend <= chunks[3]) { |
| 11078 | /* Wrapped case. */ |
| 11079 | temp = get_trash_chunk(); |
| 11080 | memcpy(temp->str, vstart, chunks[1] - vstart); |
| 11081 | memcpy(temp->str + ( chunks[1] - vstart ), chunks[2], vend - chunks[2]); |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11082 | smp->data.u.str.str = temp->str; |
| 11083 | smp->data.u.str.len = ( chunks[1] - vstart ) + ( vend - chunks[2] ); |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11084 | } else { |
| 11085 | /* Contiguous case. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11086 | smp->data.u.str.str = (char *)vstart; |
| 11087 | smp->data.u.str.len = vend - vstart; |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11088 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
| 11089 | } |
| 11090 | |
| 11091 | /* Update context, check wrapping. */ |
| 11092 | chunks[0] = vend; |
| 11093 | if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) { |
| 11094 | chunks[1] = chunks[3]; |
| 11095 | chunks[2] = NULL; |
| 11096 | } |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11097 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11098 | if (chunks[0] < chunks[1]) |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11099 | smp->flags |= SMP_F_NOT_LAST; |
| 11100 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11101 | return 1; |
| 11102 | } |
| 11103 | |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11104 | /* This function iterates over each parameter of the query string. It uses |
| 11105 | * 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] | 11106 | * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL. |
| 11107 | * An optional parameter name is passed in args[0], otherwise any parameter is |
| 11108 | * considered. It supports an optional delimiter argument for the beginning of |
| 11109 | * the string in args[1], which defaults to "?". |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11110 | */ |
| 11111 | static int |
| 11112 | smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 11113 | { |
| 11114 | struct http_msg *msg; |
| 11115 | char delim = '?'; |
| 11116 | const char *name; |
| 11117 | int name_len; |
| 11118 | |
Dragan Dosen | 26f77e5 | 2015-05-25 10:02:11 +0200 | [diff] [blame] | 11119 | if (!args || |
| 11120 | (args[0].type && args[0].type != ARGT_STR) || |
| 11121 | (args[1].type && args[1].type != ARGT_STR)) |
| 11122 | return 0; |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11123 | |
Dragan Dosen | 26f77e5 | 2015-05-25 10:02:11 +0200 | [diff] [blame] | 11124 | name = ""; |
| 11125 | name_len = 0; |
| 11126 | if (args->type == ARGT_STR) { |
| 11127 | name = args->data.str.str; |
| 11128 | name_len = args->data.str.len; |
| 11129 | } |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11130 | |
Dragan Dosen | 26f77e5 | 2015-05-25 10:02:11 +0200 | [diff] [blame] | 11131 | if (args[1].type) |
| 11132 | delim = *args[1].data.str.str; |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11133 | |
Dragan Dosen | 26f77e5 | 2015-05-25 10:02:11 +0200 | [diff] [blame] | 11134 | if (!smp->ctx.a[0]) { // first call, find the query string |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11135 | CHECK_HTTP_MESSAGE_FIRST(); |
| 11136 | |
| 11137 | msg = &smp->strm->txn->req; |
| 11138 | |
| 11139 | smp->ctx.a[0] = find_param_list(msg->chn->buf->p + msg->sl.rq.u, |
| 11140 | msg->sl.rq.u_l, delim); |
| 11141 | if (!smp->ctx.a[0]) |
| 11142 | return 0; |
| 11143 | |
| 11144 | 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] | 11145 | |
| 11146 | /* Assume that the context is filled with NULL pointer |
| 11147 | * before the first call. |
| 11148 | * smp->ctx.a[2] = NULL; |
| 11149 | * smp->ctx.a[3] = NULL; |
| 11150 | */ |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11151 | } |
| 11152 | |
| 11153 | return smp_fetch_param(delim, name, name_len, args, smp, kw, private); |
| 11154 | } |
| 11155 | |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 11156 | /* This function iterates over each parameter of the body. This requires |
| 11157 | * 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] | 11158 | * ctx->a[0] and ctx->a[1] to store the beginning and end of the first |
| 11159 | * contigous part of the body, and optionally ctx->a[2..3] to reference the |
| 11160 | * optional second part if the body wraps at the end of the buffer. An optional |
| 11161 | * parameter name is passed in args[0], otherwise any parameter is considered. |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 11162 | */ |
| 11163 | static int |
| 11164 | smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 11165 | { |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 11166 | struct http_msg *msg; |
| 11167 | unsigned long len; |
| 11168 | unsigned long block1; |
| 11169 | char *body; |
| 11170 | const char *name; |
| 11171 | int name_len; |
| 11172 | |
| 11173 | if (!args || (args[0].type && args[0].type != ARGT_STR)) |
| 11174 | return 0; |
| 11175 | |
| 11176 | name = ""; |
| 11177 | name_len = 0; |
| 11178 | if (args[0].type == ARGT_STR) { |
| 11179 | name = args[0].data.str.str; |
| 11180 | name_len = args[0].data.str.len; |
| 11181 | } |
| 11182 | |
| 11183 | if (!smp->ctx.a[0]) { // first call, find the query string |
| 11184 | CHECK_HTTP_MESSAGE_FIRST(); |
| 11185 | |
| 11186 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 11187 | msg = &smp->strm->txn->req; |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 11188 | else |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 11189 | msg = &smp->strm->txn->rsp; |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 11190 | |
| 11191 | len = http_body_bytes(msg); |
| 11192 | body = b_ptr(msg->chn->buf, -http_data_rewind(msg)); |
| 11193 | |
| 11194 | block1 = len; |
| 11195 | if (block1 > msg->chn->buf->data + msg->chn->buf->size - body) |
| 11196 | block1 = msg->chn->buf->data + msg->chn->buf->size - body; |
| 11197 | |
| 11198 | if (block1 == len) { |
| 11199 | /* buffer is not wrapped (or empty) */ |
| 11200 | smp->ctx.a[0] = body; |
| 11201 | smp->ctx.a[1] = body + len; |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11202 | |
| 11203 | /* Assume that the context is filled with NULL pointer |
| 11204 | * before the first call. |
| 11205 | * smp->ctx.a[2] = NULL; |
| 11206 | * smp->ctx.a[3] = NULL; |
| 11207 | */ |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 11208 | } |
| 11209 | else { |
| 11210 | /* buffer is wrapped, we need to defragment it */ |
| 11211 | smp->ctx.a[0] = body; |
| 11212 | smp->ctx.a[1] = body + block1; |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11213 | smp->ctx.a[2] = msg->chn->buf->data; |
| 11214 | smp->ctx.a[3] = msg->chn->buf->data + ( len - block1 ); |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 11215 | } |
| 11216 | } |
| 11217 | return smp_fetch_param('&', name, name_len, args, smp, kw, private); |
| 11218 | } |
| 11219 | |
Willy Tarreau | a9fddca | 2012-07-31 07:51:48 +0200 | [diff] [blame] | 11220 | /* Return the signed integer value for the specified url parameter (see url_param |
| 11221 | * above). |
| 11222 | */ |
| 11223 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11224 | 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] | 11225 | { |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11226 | int ret = smp_fetch_url_param(args, smp, kw, private); |
Willy Tarreau | a9fddca | 2012-07-31 07:51:48 +0200 | [diff] [blame] | 11227 | |
| 11228 | if (ret > 0) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11229 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11230 | 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] | 11231 | } |
| 11232 | |
| 11233 | return ret; |
| 11234 | } |
| 11235 | |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11236 | /* This produces a 32-bit hash of the concatenation of the first occurrence of |
| 11237 | * the Host header followed by the path component if it begins with a slash ('/'). |
| 11238 | * This means that '*' will not be added, resulting in exactly the first Host |
| 11239 | * entry. If no Host header is found, then the path is used. The resulting value |
| 11240 | * is hashed using the url hash followed by a full avalanche hash and provides a |
| 11241 | * 32-bit integer value. This fetch is useful for tracking per-URL activity on |
| 11242 | * high-traffic sites without having to store whole paths. |
| 11243 | * this differs from the base32 functions in that it includes the url parameters |
| 11244 | * as well as the path |
| 11245 | */ |
| 11246 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11247 | 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] | 11248 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11249 | struct http_txn *txn; |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11250 | struct hdr_ctx ctx; |
| 11251 | unsigned int hash = 0; |
| 11252 | char *ptr, *beg, *end; |
| 11253 | int len; |
| 11254 | |
| 11255 | CHECK_HTTP_MESSAGE_FIRST(); |
| 11256 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11257 | txn = smp->strm->txn; |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11258 | ctx.idx = 0; |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 11259 | 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] | 11260 | /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */ |
| 11261 | ptr = ctx.line + ctx.val; |
| 11262 | len = ctx.vlen; |
| 11263 | while (len--) |
| 11264 | hash = *(ptr++) + (hash << 6) + (hash << 16) - hash; |
| 11265 | } |
| 11266 | |
| 11267 | /* now retrieve the path */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 11268 | 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] | 11269 | beg = http_get_path(txn); |
| 11270 | if (!beg) |
| 11271 | beg = end; |
| 11272 | |
| 11273 | for (ptr = beg; ptr < end ; ptr++); |
| 11274 | |
| 11275 | if (beg < ptr && *beg == '/') { |
| 11276 | while (beg < ptr) |
| 11277 | hash = *(beg++) + (hash << 6) + (hash << 16) - hash; |
| 11278 | } |
| 11279 | hash = full_hash(hash); |
| 11280 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11281 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11282 | smp->data.u.sint = hash; |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11283 | smp->flags = SMP_F_VOL_1ST; |
| 11284 | return 1; |
| 11285 | } |
| 11286 | |
| 11287 | /* This concatenates the source address with the 32-bit hash of the Host and |
| 11288 | * URL as returned by smp_fetch_base32(). The idea is to have per-source and |
| 11289 | * per-url counters. The result is a binary block from 8 to 20 bytes depending |
| 11290 | * on the source address length. The URL hash is stored before the address so |
| 11291 | * that in environments where IPv6 is insignificant, truncating the output to |
| 11292 | * 8 bytes would still work. |
| 11293 | */ |
| 11294 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11295 | 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] | 11296 | { |
| 11297 | struct chunk *temp; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11298 | struct connection *cli_conn = objt_conn(smp->sess->origin); |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11299 | |
Dragan Dosen | db5af61 | 2016-06-16 11:23:01 +0200 | [diff] [blame] | 11300 | if (!cli_conn) |
| 11301 | return 0; |
| 11302 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11303 | if (!smp_fetch_url32(args, smp, kw, private)) |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11304 | return 0; |
| 11305 | |
| 11306 | temp = get_trash_chunk(); |
Dragan Dosen | e5f4133 | 2016-06-16 11:08:08 +0200 | [diff] [blame] | 11307 | *(unsigned int *)temp->str = htonl(smp->data.u.sint); |
| 11308 | temp->len += sizeof(unsigned int); |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11309 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 11310 | switch (cli_conn->addr.from.ss_family) { |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11311 | case AF_INET: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 11312 | 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] | 11313 | temp->len += 4; |
| 11314 | break; |
| 11315 | case AF_INET6: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 11316 | 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] | 11317 | temp->len += 16; |
| 11318 | break; |
| 11319 | default: |
| 11320 | return 0; |
| 11321 | } |
| 11322 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11323 | smp->data.u.str = *temp; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11324 | smp->data.type = SMP_T_BIN; |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11325 | return 1; |
| 11326 | } |
| 11327 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 11328 | /* This function is used to validate the arguments passed to any "hdr" fetch |
| 11329 | * keyword. These keywords support an optional positive or negative occurrence |
| 11330 | * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It |
| 11331 | * is assumed that the types are already the correct ones. Returns 0 on error, |
| 11332 | * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an |
| 11333 | * error message in case of error, that the caller is responsible for freeing. |
| 11334 | * The initial location must either be freeable or NULL. |
| 11335 | */ |
Thierry FOURNIER | 49f45af | 2014-12-08 19:50:43 +0100 | [diff] [blame] | 11336 | int val_hdr(struct arg *arg, char **err_msg) |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 11337 | { |
| 11338 | 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] | 11339 | memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY); |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 11340 | return 0; |
| 11341 | } |
| 11342 | return 1; |
| 11343 | } |
| 11344 | |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 11345 | /* takes an UINT value on input supposed to represent the time since EPOCH, |
| 11346 | * adds an optional offset found in args[0] and emits a string representing |
| 11347 | * the date in RFC-1123/5322 format. |
| 11348 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11349 | 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] | 11350 | { |
Cyril Bonté | f78d896 | 2016-01-22 19:40:28 +0100 | [diff] [blame] | 11351 | const char day[7][4] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 11352 | const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; |
| 11353 | struct chunk *temp; |
| 11354 | struct tm *tm; |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 11355 | /* 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] | 11356 | time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL; |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 11357 | |
| 11358 | /* add offset */ |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 11359 | if (args && (args[0].type == ARGT_SINT)) |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 11360 | curr_date += args[0].data.sint; |
| 11361 | |
| 11362 | tm = gmtime(&curr_date); |
Thierry FOURNIER | fac9ccf | 2015-07-08 00:15:20 +0200 | [diff] [blame] | 11363 | if (!tm) |
| 11364 | return 0; |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 11365 | |
| 11366 | temp = get_trash_chunk(); |
| 11367 | temp->len = snprintf(temp->str, temp->size - temp->len, |
| 11368 | "%s, %02d %s %04d %02d:%02d:%02d GMT", |
| 11369 | day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year, |
| 11370 | tm->tm_hour, tm->tm_min, tm->tm_sec); |
| 11371 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11372 | smp->data.u.str = *temp; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11373 | smp->data.type = SMP_T_STR; |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 11374 | return 1; |
| 11375 | } |
| 11376 | |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11377 | /* Match language range with language tag. RFC2616 14.4: |
| 11378 | * |
| 11379 | * A language-range matches a language-tag if it exactly equals |
| 11380 | * the tag, or if it exactly equals a prefix of the tag such |
| 11381 | * that the first tag character following the prefix is "-". |
| 11382 | * |
| 11383 | * Return 1 if the strings match, else return 0. |
| 11384 | */ |
| 11385 | static inline int language_range_match(const char *range, int range_len, |
| 11386 | const char *tag, int tag_len) |
| 11387 | { |
| 11388 | const char *end = range + range_len; |
| 11389 | const char *tend = tag + tag_len; |
| 11390 | while (range < end) { |
| 11391 | if (*range == '-' && tag == tend) |
| 11392 | return 1; |
| 11393 | if (*range != *tag || tag == tend) |
| 11394 | return 0; |
| 11395 | range++; |
| 11396 | tag++; |
| 11397 | } |
| 11398 | /* Return true only if the last char of the tag is matched. */ |
| 11399 | return tag == tend; |
| 11400 | } |
| 11401 | |
| 11402 | /* 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] | 11403 | 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] | 11404 | { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11405 | const char *al = smp->data.u.str.str; |
| 11406 | const char *end = al + smp->data.u.str.len; |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11407 | const char *token; |
| 11408 | int toklen; |
| 11409 | int qvalue; |
| 11410 | const char *str; |
| 11411 | const char *w; |
| 11412 | int best_q = 0; |
| 11413 | |
| 11414 | /* Set the constant to the sample, because the output of the |
| 11415 | * function will be peek in the constant configuration string. |
| 11416 | */ |
| 11417 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11418 | smp->data.u.str.size = 0; |
| 11419 | smp->data.u.str.str = ""; |
| 11420 | smp->data.u.str.len = 0; |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11421 | |
| 11422 | /* Parse the accept language */ |
| 11423 | while (1) { |
| 11424 | |
| 11425 | /* Jump spaces, quit if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 11426 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11427 | al++; |
| 11428 | if (al >= end) |
| 11429 | break; |
| 11430 | |
| 11431 | /* Start of the fisrt word. */ |
| 11432 | token = al; |
| 11433 | |
| 11434 | /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 11435 | while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11436 | al++; |
| 11437 | if (al == token) |
| 11438 | goto expect_comma; |
| 11439 | |
| 11440 | /* Length of the token. */ |
| 11441 | toklen = al - token; |
| 11442 | qvalue = 1000; |
| 11443 | |
| 11444 | /* Check if the token exists in the list. If the token not exists, |
| 11445 | * jump to the next token. |
| 11446 | */ |
| 11447 | str = args[0].data.str.str; |
| 11448 | w = str; |
| 11449 | while (1) { |
| 11450 | if (*str == ';' || *str == '\0') { |
| 11451 | if (language_range_match(token, toklen, w, str-w)) |
| 11452 | goto look_for_q; |
| 11453 | if (*str == '\0') |
| 11454 | goto expect_comma; |
| 11455 | w = str + 1; |
| 11456 | } |
| 11457 | str++; |
| 11458 | } |
| 11459 | goto expect_comma; |
| 11460 | |
| 11461 | look_for_q: |
| 11462 | |
| 11463 | /* Jump spaces, quit if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 11464 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11465 | al++; |
| 11466 | if (al >= end) |
| 11467 | goto process_value; |
| 11468 | |
| 11469 | /* If ',' is found, process the result */ |
| 11470 | if (*al == ',') |
| 11471 | goto process_value; |
| 11472 | |
| 11473 | /* If the character is different from ';', look |
| 11474 | * for the end of the header part in best effort. |
| 11475 | */ |
| 11476 | if (*al != ';') |
| 11477 | goto expect_comma; |
| 11478 | |
| 11479 | /* Assumes that the char is ';', now expect "q=". */ |
| 11480 | al++; |
| 11481 | |
| 11482 | /* Jump spaces, process value if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 11483 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11484 | al++; |
| 11485 | if (al >= end) |
| 11486 | goto process_value; |
| 11487 | |
| 11488 | /* Expect 'q'. If no 'q', continue in best effort */ |
| 11489 | if (*al != 'q') |
| 11490 | goto process_value; |
| 11491 | al++; |
| 11492 | |
| 11493 | /* Jump spaces, process value if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 11494 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11495 | al++; |
| 11496 | if (al >= end) |
| 11497 | goto process_value; |
| 11498 | |
| 11499 | /* Expect '='. If no '=', continue in best effort */ |
| 11500 | if (*al != '=') |
| 11501 | goto process_value; |
| 11502 | al++; |
| 11503 | |
| 11504 | /* Jump spaces, process value if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 11505 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11506 | al++; |
| 11507 | if (al >= end) |
| 11508 | goto process_value; |
| 11509 | |
| 11510 | /* Parse the q value. */ |
| 11511 | qvalue = parse_qvalue(al, &al); |
| 11512 | |
| 11513 | process_value: |
| 11514 | |
| 11515 | /* If the new q value is the best q value, then store the associated |
| 11516 | * language in the response. If qvalue is the biggest value (1000), |
| 11517 | * break the process. |
| 11518 | */ |
| 11519 | if (qvalue > best_q) { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11520 | smp->data.u.str.str = (char *)w; |
| 11521 | smp->data.u.str.len = str - w; |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11522 | if (qvalue >= 1000) |
| 11523 | break; |
| 11524 | best_q = qvalue; |
| 11525 | } |
| 11526 | |
| 11527 | expect_comma: |
| 11528 | |
| 11529 | /* Expect comma or end. If the end is detected, quit the loop. */ |
| 11530 | while (al < end && *al != ',') |
| 11531 | al++; |
| 11532 | if (al >= end) |
| 11533 | break; |
| 11534 | |
| 11535 | /* Comma is found, jump it and restart the analyzer. */ |
| 11536 | al++; |
| 11537 | } |
| 11538 | |
| 11539 | /* Set default value if required. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11540 | if (smp->data.u.str.len == 0 && args[1].type == ARGT_STR) { |
| 11541 | smp->data.u.str.str = args[1].data.str.str; |
| 11542 | smp->data.u.str.len = args[1].data.str.len; |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11543 | } |
| 11544 | |
| 11545 | /* Return true only if a matching language was found. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11546 | return smp->data.u.str.len != 0; |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11547 | } |
| 11548 | |
Thierry FOURNIER | 82ff3c9 | 2015-05-07 15:46:20 +0200 | [diff] [blame] | 11549 | /* This fetch url-decode any input string. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11550 | 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] | 11551 | { |
| 11552 | /* If the constant flag is set or if not size is avalaible at |
| 11553 | * the end of the buffer, copy the string in other buffer |
| 11554 | * before decoding. |
| 11555 | */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11556 | 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] | 11557 | struct chunk *str = get_trash_chunk(); |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11558 | memcpy(str->str, smp->data.u.str.str, smp->data.u.str.len); |
| 11559 | smp->data.u.str.str = str->str; |
| 11560 | smp->data.u.str.size = str->size; |
Thierry FOURNIER | 82ff3c9 | 2015-05-07 15:46:20 +0200 | [diff] [blame] | 11561 | smp->flags &= ~SMP_F_CONST; |
| 11562 | } |
| 11563 | |
| 11564 | /* Add final \0 required by url_decode(), and convert the input string. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11565 | smp->data.u.str.str[smp->data.u.str.len] = '\0'; |
| 11566 | smp->data.u.str.len = url_decode(smp->data.u.str.str); |
Christopher Faulet | a258479 | 2017-10-05 10:03:12 +0200 | [diff] [blame] | 11567 | return (smp->data.u.str.len >= 0); |
Thierry FOURNIER | 82ff3c9 | 2015-05-07 15:46:20 +0200 | [diff] [blame] | 11568 | } |
| 11569 | |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 11570 | static int smp_conv_req_capture(const struct arg *args, struct sample *smp, void *private) |
| 11571 | { |
| 11572 | struct proxy *fe = strm_fe(smp->strm); |
| 11573 | int idx, i; |
| 11574 | struct cap_hdr *hdr; |
| 11575 | int len; |
| 11576 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 11577 | if (!args || args->type != ARGT_SINT) |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 11578 | return 0; |
| 11579 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 11580 | idx = args->data.sint; |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 11581 | |
| 11582 | /* Check the availibity of the capture id. */ |
| 11583 | if (idx > fe->nb_req_cap - 1) |
| 11584 | return 0; |
| 11585 | |
| 11586 | /* Look for the original configuration. */ |
| 11587 | for (hdr = fe->req_cap, i = fe->nb_req_cap - 1; |
| 11588 | hdr != NULL && i != idx ; |
| 11589 | i--, hdr = hdr->next); |
| 11590 | if (!hdr) |
| 11591 | return 0; |
| 11592 | |
| 11593 | /* check for the memory allocation */ |
| 11594 | if (smp->strm->req_cap[hdr->index] == NULL) |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 11595 | smp->strm->req_cap[hdr->index] = pool_alloc(hdr->pool); |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 11596 | if (smp->strm->req_cap[hdr->index] == NULL) |
| 11597 | return 0; |
| 11598 | |
| 11599 | /* Check length. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11600 | len = smp->data.u.str.len; |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 11601 | if (len > hdr->len) |
| 11602 | len = hdr->len; |
| 11603 | |
| 11604 | /* Capture input data. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11605 | memcpy(smp->strm->req_cap[idx], smp->data.u.str.str, len); |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 11606 | smp->strm->req_cap[idx][len] = '\0'; |
| 11607 | |
| 11608 | return 1; |
| 11609 | } |
| 11610 | |
| 11611 | static int smp_conv_res_capture(const struct arg *args, struct sample *smp, void *private) |
| 11612 | { |
| 11613 | struct proxy *fe = strm_fe(smp->strm); |
| 11614 | int idx, i; |
| 11615 | struct cap_hdr *hdr; |
| 11616 | int len; |
| 11617 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 11618 | if (!args || args->type != ARGT_SINT) |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 11619 | return 0; |
| 11620 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 11621 | idx = args->data.sint; |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 11622 | |
| 11623 | /* Check the availibity of the capture id. */ |
| 11624 | if (idx > fe->nb_rsp_cap - 1) |
| 11625 | return 0; |
| 11626 | |
| 11627 | /* Look for the original configuration. */ |
| 11628 | for (hdr = fe->rsp_cap, i = fe->nb_rsp_cap - 1; |
| 11629 | hdr != NULL && i != idx ; |
| 11630 | i--, hdr = hdr->next); |
| 11631 | if (!hdr) |
| 11632 | return 0; |
| 11633 | |
| 11634 | /* check for the memory allocation */ |
| 11635 | if (smp->strm->res_cap[hdr->index] == NULL) |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 11636 | smp->strm->res_cap[hdr->index] = pool_alloc(hdr->pool); |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 11637 | if (smp->strm->res_cap[hdr->index] == NULL) |
| 11638 | return 0; |
| 11639 | |
| 11640 | /* Check length. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11641 | len = smp->data.u.str.len; |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 11642 | if (len > hdr->len) |
| 11643 | len = hdr->len; |
| 11644 | |
| 11645 | /* Capture input data. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11646 | memcpy(smp->strm->res_cap[idx], smp->data.u.str.str, len); |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 11647 | smp->strm->res_cap[idx][len] = '\0'; |
| 11648 | |
| 11649 | return 1; |
| 11650 | } |
| 11651 | |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11652 | /* 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] | 11653 | * takes the string from the variable 'replace' with length 'len', then modifies |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11654 | * the relevant part of the request line accordingly. Then it updates various |
| 11655 | * pointers to the next elements which were moved, and the total buffer length. |
| 11656 | * 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] | 11657 | * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal |
| 11658 | * error, though this can be revisited when this code is finally exploited. |
| 11659 | * |
| 11660 | * 'action' can be '0' to replace method, '1' to replace path, '2' to replace |
| 11661 | * query string and 3 to replace uri. |
| 11662 | * |
| 11663 | * In query string case, the mark question '?' must be set at the start of the |
| 11664 | * string by the caller, event if the replacement query string is empty. |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11665 | */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11666 | int http_replace_req_line(int action, const char *replace, int len, |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 11667 | struct proxy *px, struct stream *s) |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11668 | { |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 11669 | struct http_txn *txn = s->txn; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11670 | char *cur_ptr, *cur_end; |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11671 | int offset = 0; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11672 | int delta; |
| 11673 | |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11674 | switch (action) { |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11675 | case 0: // method |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 11676 | cur_ptr = s->req.buf->p; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11677 | cur_end = cur_ptr + txn->req.sl.rq.m_l; |
| 11678 | |
| 11679 | /* adjust req line offsets and lengths */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11680 | delta = len - offset - (cur_end - cur_ptr); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11681 | txn->req.sl.rq.m_l += delta; |
| 11682 | txn->req.sl.rq.u += delta; |
| 11683 | txn->req.sl.rq.v += delta; |
| 11684 | break; |
| 11685 | |
| 11686 | case 1: // path |
| 11687 | cur_ptr = http_get_path(txn); |
| 11688 | if (!cur_ptr) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 11689 | cur_ptr = s->req.buf->p + txn->req.sl.rq.u; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11690 | |
| 11691 | cur_end = cur_ptr; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 11692 | 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] | 11693 | cur_end++; |
| 11694 | |
| 11695 | /* adjust req line offsets and lengths */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11696 | delta = len - offset - (cur_end - cur_ptr); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11697 | txn->req.sl.rq.u_l += delta; |
| 11698 | txn->req.sl.rq.v += delta; |
| 11699 | break; |
| 11700 | |
| 11701 | case 2: // query |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11702 | offset = 1; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 11703 | cur_ptr = s->req.buf->p + txn->req.sl.rq.u; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11704 | cur_end = cur_ptr + txn->req.sl.rq.u_l; |
| 11705 | while (cur_ptr < cur_end && *cur_ptr != '?') |
| 11706 | cur_ptr++; |
| 11707 | |
| 11708 | /* skip the question mark or indicate that we must insert it |
| 11709 | * (but only if the format string is not empty then). |
| 11710 | */ |
| 11711 | if (cur_ptr < cur_end) |
| 11712 | cur_ptr++; |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11713 | else if (len > 1) |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11714 | offset = 0; |
| 11715 | |
| 11716 | /* adjust req line offsets and lengths */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11717 | delta = len - offset - (cur_end - cur_ptr); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11718 | txn->req.sl.rq.u_l += delta; |
| 11719 | txn->req.sl.rq.v += delta; |
| 11720 | break; |
| 11721 | |
| 11722 | case 3: // uri |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 11723 | cur_ptr = s->req.buf->p + txn->req.sl.rq.u; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11724 | cur_end = cur_ptr + txn->req.sl.rq.u_l; |
| 11725 | |
| 11726 | /* adjust req line offsets and lengths */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11727 | delta = len - offset - (cur_end - cur_ptr); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11728 | txn->req.sl.rq.u_l += delta; |
| 11729 | txn->req.sl.rq.v += delta; |
| 11730 | break; |
| 11731 | |
| 11732 | default: |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11733 | return -1; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11734 | } |
| 11735 | |
| 11736 | /* commit changes and adjust end of message */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11737 | 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] | 11738 | txn->req.sl.rq.l += delta; |
| 11739 | txn->hdr_idx.v[0].len += delta; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11740 | http_msg_move_end(&txn->req, delta); |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11741 | return 0; |
| 11742 | } |
| 11743 | |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 11744 | /* This function replace the HTTP status code and the associated message. The |
| 11745 | * variable <status> contains the new status code. This function never fails. |
| 11746 | */ |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 11747 | 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] | 11748 | { |
| 11749 | struct http_txn *txn = s->txn; |
| 11750 | char *cur_ptr, *cur_end; |
| 11751 | int delta; |
| 11752 | char *res; |
| 11753 | int c_l; |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 11754 | const char *msg = reason; |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 11755 | int msg_len; |
| 11756 | |
| 11757 | chunk_reset(&trash); |
| 11758 | |
| 11759 | res = ultoa_o(status, trash.str, trash.size); |
| 11760 | c_l = res - trash.str; |
| 11761 | |
| 11762 | trash.str[c_l] = ' '; |
| 11763 | trash.len = c_l + 1; |
| 11764 | |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 11765 | /* Do we have a custom reason format string? */ |
| 11766 | if (msg == NULL) |
| 11767 | msg = get_reason(status); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 11768 | msg_len = strlen(msg); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 11769 | strncpy(&trash.str[trash.len], msg, trash.size - trash.len); |
| 11770 | trash.len += msg_len; |
| 11771 | |
| 11772 | cur_ptr = s->res.buf->p + txn->rsp.sl.st.c; |
| 11773 | cur_end = s->res.buf->p + txn->rsp.sl.st.r + txn->rsp.sl.st.r_l; |
| 11774 | |
| 11775 | /* commit changes and adjust message */ |
| 11776 | delta = buffer_replace2(s->res.buf, cur_ptr, cur_end, trash.str, trash.len); |
| 11777 | |
| 11778 | /* adjust res line offsets and lengths */ |
| 11779 | txn->rsp.sl.st.r += c_l - txn->rsp.sl.st.c_l; |
| 11780 | txn->rsp.sl.st.c_l = c_l; |
| 11781 | txn->rsp.sl.st.r_l = msg_len; |
| 11782 | |
| 11783 | delta = trash.len - (cur_end - cur_ptr); |
| 11784 | txn->rsp.sl.st.l += delta; |
| 11785 | txn->hdr_idx.v[0].len += delta; |
| 11786 | http_msg_move_end(&txn->rsp, delta); |
| 11787 | } |
| 11788 | |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11789 | /* This function executes one of the set-{method,path,query,uri} actions. It |
| 11790 | * 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] | 11791 | * the action to be performed in <http.action>, previously filled by function |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11792 | * parse_set_req_line(). The replacement action is excuted by the function |
Thierry FOURNIER | 3f4bc65 | 2015-08-26 16:23:34 +0200 | [diff] [blame] | 11793 | * http_action_set_req_line(). It always returns ACT_RET_CONT. If an error |
| 11794 | * occurs the action is canceled, but the rule processing continue. |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11795 | */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 11796 | 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] | 11797 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11798 | { |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 11799 | struct chunk *replace; |
| 11800 | enum act_return ret = ACT_RET_ERR; |
| 11801 | |
| 11802 | replace = alloc_trash_chunk(); |
| 11803 | if (!replace) |
| 11804 | goto leave; |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11805 | |
| 11806 | /* If we have to create a query string, prepare a '?'. */ |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 11807 | if (rule->arg.http.action == 2) |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 11808 | replace->str[replace->len++] = '?'; |
| 11809 | replace->len += build_logline(s, replace->str + replace->len, replace->size - replace->len, |
| 11810 | &rule->arg.http.logfmt); |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11811 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 11812 | http_replace_req_line(rule->arg.http.action, replace->str, replace->len, px, s); |
| 11813 | |
| 11814 | ret = ACT_RET_CONT; |
| 11815 | |
| 11816 | leave: |
| 11817 | free_trash_chunk(replace); |
| 11818 | return ret; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11819 | } |
| 11820 | |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 11821 | /* This function is just a compliant action wrapper for "set-status". */ |
| 11822 | 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] | 11823 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 11824 | { |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 11825 | http_set_status(rule->arg.status.code, rule->arg.status.reason, s); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 11826 | return ACT_RET_CONT; |
| 11827 | } |
| 11828 | |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11829 | /* parse an http-request action among : |
| 11830 | * set-method |
| 11831 | * set-path |
| 11832 | * set-query |
| 11833 | * set-uri |
| 11834 | * |
| 11835 | * All of them accept a single argument of type string representing a log-format. |
| 11836 | * The resulting rule makes use of arg->act.p[0..1] to store the log-format list |
| 11837 | * 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] | 11838 | * 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] | 11839 | */ |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 11840 | enum act_parse_ret parse_set_req_line(const char **args, int *orig_arg, struct proxy *px, |
| 11841 | struct act_rule *rule, char **err) |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11842 | { |
| 11843 | int cur_arg = *orig_arg; |
| 11844 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 11845 | rule->action = ACT_CUSTOM; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11846 | |
| 11847 | switch (args[0][4]) { |
| 11848 | case 'm' : |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 11849 | rule->arg.http.action = 0; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11850 | rule->action_ptr = http_action_set_req_line; |
| 11851 | break; |
| 11852 | case 'p' : |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 11853 | rule->arg.http.action = 1; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11854 | rule->action_ptr = http_action_set_req_line; |
| 11855 | break; |
| 11856 | case 'q' : |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 11857 | rule->arg.http.action = 2; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11858 | rule->action_ptr = http_action_set_req_line; |
| 11859 | break; |
| 11860 | case 'u' : |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 11861 | rule->arg.http.action = 3; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11862 | rule->action_ptr = http_action_set_req_line; |
| 11863 | break; |
| 11864 | default: |
| 11865 | memprintf(err, "internal error: unhandled action '%s'", args[0]); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 11866 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11867 | } |
| 11868 | |
| 11869 | if (!*args[cur_arg] || |
| 11870 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 11871 | memprintf(err, "expects exactly 1 argument <format>"); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 11872 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11873 | } |
| 11874 | |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 11875 | LIST_INIT(&rule->arg.http.logfmt); |
Olivier Houchard | fbc74e8 | 2017-11-24 16:54:05 +0100 | [diff] [blame] | 11876 | px->conf.args.ctx = ARGC_HRQ; |
| 11877 | if (!parse_logformat_string(args[cur_arg], px, &rule->arg.http.logfmt, LOG_OPT_HTTP, |
| 11878 | (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] | 11879 | return ACT_RET_PRS_ERR; |
| 11880 | } |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11881 | |
| 11882 | (*orig_arg)++; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 11883 | return ACT_RET_PRS_OK; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11884 | } |
| 11885 | |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 11886 | /* parse set-status action: |
| 11887 | * This action accepts a single argument of type int representing |
| 11888 | * an http status code. It returns ACT_RET_PRS_OK on success, |
| 11889 | * ACT_RET_PRS_ERR on error. |
| 11890 | */ |
| 11891 | enum act_parse_ret parse_http_set_status(const char **args, int *orig_arg, struct proxy *px, |
| 11892 | struct act_rule *rule, char **err) |
| 11893 | { |
| 11894 | char *error; |
| 11895 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 11896 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 11897 | rule->action_ptr = action_http_set_status; |
| 11898 | |
| 11899 | /* Check if an argument is available */ |
| 11900 | if (!*args[*orig_arg]) { |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 11901 | memprintf(err, "expects 1 argument: <status>; or 3 arguments: <status> reason <fmt>"); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 11902 | return ACT_RET_PRS_ERR; |
| 11903 | } |
| 11904 | |
| 11905 | /* convert status code as integer */ |
| 11906 | rule->arg.status.code = strtol(args[*orig_arg], &error, 10); |
| 11907 | if (*error != '\0' || rule->arg.status.code < 100 || rule->arg.status.code > 999) { |
| 11908 | memprintf(err, "expects an integer status code between 100 and 999"); |
| 11909 | return ACT_RET_PRS_ERR; |
| 11910 | } |
| 11911 | |
| 11912 | (*orig_arg)++; |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 11913 | |
| 11914 | /* set custom reason string */ |
| 11915 | rule->arg.status.reason = NULL; // If null, we use the default reason for the status code. |
| 11916 | if (*args[*orig_arg] && strcmp(args[*orig_arg], "reason") == 0 && |
| 11917 | (*args[*orig_arg + 1] && strcmp(args[*orig_arg + 1], "if") != 0 && strcmp(args[*orig_arg + 1], "unless") != 0)) { |
| 11918 | (*orig_arg)++; |
| 11919 | rule->arg.status.reason = strdup(args[*orig_arg]); |
| 11920 | (*orig_arg)++; |
| 11921 | } |
| 11922 | |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 11923 | return ACT_RET_PRS_OK; |
| 11924 | } |
| 11925 | |
Willy Tarreau | 53275e8 | 2017-11-24 07:52:01 +0100 | [diff] [blame] | 11926 | /* This function executes the "reject" HTTP action. It clears the request and |
| 11927 | * response buffer without sending any response. It can be useful as an HTTP |
| 11928 | * alternative to the silent-drop action to defend against DoS attacks, and may |
| 11929 | * also be used with HTTP/2 to close a connection instead of just a stream. |
| 11930 | * The txn status is unchanged, indicating no response was sent. The termination |
| 11931 | * flags will indicate "PR". It always returns ACT_RET_STOP. |
| 11932 | */ |
| 11933 | enum act_return http_action_reject(struct act_rule *rule, struct proxy *px, |
| 11934 | struct session *sess, struct stream *s, int flags) |
| 11935 | { |
| 11936 | channel_abort(&s->req); |
| 11937 | channel_abort(&s->res); |
| 11938 | s->req.analysers = 0; |
| 11939 | s->res.analysers = 0; |
| 11940 | |
| 11941 | HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1); |
| 11942 | HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1); |
| 11943 | if (sess->listener && sess->listener->counters) |
| 11944 | HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1); |
| 11945 | |
| 11946 | if (!(s->flags & SF_ERR_MASK)) |
| 11947 | s->flags |= SF_ERR_PRXCOND; |
| 11948 | if (!(s->flags & SF_FINST_MASK)) |
| 11949 | s->flags |= SF_FINST_R; |
| 11950 | |
| 11951 | return ACT_RET_CONT; |
| 11952 | } |
| 11953 | |
| 11954 | /* parse the "reject" action: |
| 11955 | * This action takes no argument and returns ACT_RET_PRS_OK on success, |
| 11956 | * ACT_RET_PRS_ERR on error. |
| 11957 | */ |
| 11958 | enum act_parse_ret parse_http_action_reject(const char **args, int *orig_arg, struct proxy *px, |
| 11959 | struct act_rule *rule, char **err) |
| 11960 | { |
| 11961 | rule->action = ACT_CUSTOM; |
| 11962 | rule->action_ptr = http_action_reject; |
| 11963 | return ACT_RET_PRS_OK; |
| 11964 | } |
| 11965 | |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 11966 | /* This function executes the "capture" action. It executes a fetch expression, |
| 11967 | * turns the result into a string and puts it in a capture slot. It always |
| 11968 | * returns 1. If an error occurs the action is cancelled, but the rule |
| 11969 | * processing continues. |
| 11970 | */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 11971 | 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] | 11972 | struct session *sess, struct stream *s, int flags) |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 11973 | { |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 11974 | struct sample *key; |
Thierry FOURNIER | 32b1500 | 2015-07-31 08:56:16 +0200 | [diff] [blame] | 11975 | struct cap_hdr *h = rule->arg.cap.hdr; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 11976 | char **cap = s->req_cap; |
| 11977 | int len; |
| 11978 | |
Thierry FOURNIER | 32b1500 | 2015-07-31 08:56:16 +0200 | [diff] [blame] | 11979 | 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] | 11980 | if (!key) |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 11981 | return ACT_RET_CONT; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 11982 | |
| 11983 | if (cap[h->index] == NULL) |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 11984 | cap[h->index] = pool_alloc(h->pool); |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 11985 | |
| 11986 | if (cap[h->index] == NULL) /* no more capture memory */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 11987 | return ACT_RET_CONT; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 11988 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11989 | len = key->data.u.str.len; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 11990 | if (len > h->len) |
| 11991 | len = h->len; |
| 11992 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11993 | memcpy(cap[h->index], key->data.u.str.str, len); |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 11994 | cap[h->index][len] = 0; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 11995 | return ACT_RET_CONT; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 11996 | } |
| 11997 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 11998 | /* This function executes the "capture" action and store the result in a |
| 11999 | * capture slot if exists. It executes a fetch expression, turns the result |
| 12000 | * into a string and puts it in a capture slot. It always returns 1. If an |
| 12001 | * error occurs the action is cancelled, but the rule processing continues. |
| 12002 | */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12003 | 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] | 12004 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12005 | { |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12006 | struct sample *key; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12007 | struct cap_hdr *h; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12008 | char **cap = s->req_cap; |
| 12009 | struct proxy *fe = strm_fe(s); |
| 12010 | int len; |
| 12011 | int i; |
| 12012 | |
| 12013 | /* Look for the original configuration. */ |
| 12014 | for (h = fe->req_cap, i = fe->nb_req_cap - 1; |
Thierry FOURNIER | e209797 | 2015-07-31 08:56:35 +0200 | [diff] [blame] | 12015 | h != NULL && i != rule->arg.capid.idx ; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12016 | i--, h = h->next); |
| 12017 | if (!h) |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12018 | return ACT_RET_CONT; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12019 | |
Thierry FOURNIER | e209797 | 2015-07-31 08:56:35 +0200 | [diff] [blame] | 12020 | 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] | 12021 | if (!key) |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12022 | return ACT_RET_CONT; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12023 | |
| 12024 | if (cap[h->index] == NULL) |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 12025 | cap[h->index] = pool_alloc(h->pool); |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12026 | |
| 12027 | if (cap[h->index] == NULL) /* no more capture memory */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12028 | return ACT_RET_CONT; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12029 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12030 | len = key->data.u.str.len; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12031 | if (len > h->len) |
| 12032 | len = h->len; |
| 12033 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12034 | memcpy(cap[h->index], key->data.u.str.str, len); |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12035 | cap[h->index][len] = 0; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12036 | return ACT_RET_CONT; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12037 | } |
| 12038 | |
Christopher Faulet | 29730ba | 2017-09-18 15:26:32 +0200 | [diff] [blame] | 12039 | /* Check an "http-request capture" action. |
| 12040 | * |
| 12041 | * The function returns 1 in success case, otherwise, it returns 0 and err is |
| 12042 | * filled. |
| 12043 | */ |
| 12044 | int check_http_req_capture(struct act_rule *rule, struct proxy *px, char **err) |
| 12045 | { |
| 12046 | if (rule->arg.capid.idx >= px->nb_req_cap) { |
| 12047 | memprintf(err, "unable to find capture id '%d' referenced by http-request capture rule", |
| 12048 | rule->arg.capid.idx); |
| 12049 | return 0; |
| 12050 | } |
| 12051 | |
| 12052 | return 1; |
| 12053 | } |
| 12054 | |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12055 | /* parse an "http-request capture" action. It takes a single argument which is |
| 12056 | * 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] | 12057 | * 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] | 12058 | * 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] | 12059 | */ |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12060 | enum act_parse_ret parse_http_req_capture(const char **args, int *orig_arg, struct proxy *px, |
| 12061 | struct act_rule *rule, char **err) |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12062 | { |
| 12063 | struct sample_expr *expr; |
| 12064 | struct cap_hdr *hdr; |
| 12065 | int cur_arg; |
Willy Tarreau | 3986ac1 | 2015-05-08 16:13:42 +0200 | [diff] [blame] | 12066 | int len = 0; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12067 | |
| 12068 | for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++) |
| 12069 | if (strcmp(args[cur_arg], "if") == 0 || |
| 12070 | strcmp(args[cur_arg], "unless") == 0) |
| 12071 | break; |
| 12072 | |
| 12073 | if (cur_arg < *orig_arg + 3) { |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12074 | memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]"); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12075 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12076 | } |
| 12077 | |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12078 | cur_arg = *orig_arg; |
| 12079 | expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args); |
| 12080 | if (!expr) |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12081 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12082 | |
| 12083 | if (!(expr->fetch->val & SMP_VAL_FE_HRQ_HDR)) { |
| 12084 | memprintf(err, |
| 12085 | "fetch method '%s' extracts information from '%s', none of which is available here", |
| 12086 | args[cur_arg-1], sample_src_names(expr->fetch->use)); |
| 12087 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12088 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12089 | } |
| 12090 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12091 | if (!args[cur_arg] || !*args[cur_arg]) { |
| 12092 | memprintf(err, "expects 'len or 'id'"); |
| 12093 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12094 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12095 | } |
| 12096 | |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12097 | if (strcmp(args[cur_arg], "len") == 0) { |
| 12098 | cur_arg++; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12099 | |
| 12100 | if (!(px->cap & PR_CAP_FE)) { |
| 12101 | memprintf(err, "proxy '%s' has no frontend capability", px->id); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12102 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12103 | } |
| 12104 | |
Olivier Houchard | fbc74e8 | 2017-11-24 16:54:05 +0100 | [diff] [blame] | 12105 | px->conf.args.ctx = ARGC_CAP; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12106 | |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12107 | if (!args[cur_arg]) { |
| 12108 | memprintf(err, "missing length value"); |
| 12109 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12110 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12111 | } |
| 12112 | /* we copy the table name for now, it will be resolved later */ |
| 12113 | len = atoi(args[cur_arg]); |
| 12114 | if (len <= 0) { |
| 12115 | memprintf(err, "length must be > 0"); |
| 12116 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12117 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12118 | } |
| 12119 | cur_arg++; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12120 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12121 | if (!len) { |
| 12122 | memprintf(err, "a positive 'len' argument is mandatory"); |
| 12123 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12124 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12125 | } |
| 12126 | |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 12127 | hdr = calloc(1, sizeof(*hdr)); |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12128 | hdr->next = px->req_cap; |
| 12129 | hdr->name = NULL; /* not a header capture */ |
| 12130 | hdr->namelen = 0; |
| 12131 | hdr->len = len; |
| 12132 | hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED); |
| 12133 | hdr->index = px->nb_req_cap++; |
| 12134 | |
| 12135 | px->req_cap = hdr; |
| 12136 | px->to_log |= LW_REQHDR; |
| 12137 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 12138 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12139 | rule->action_ptr = http_action_req_capture; |
Christopher Faulet | 29730ba | 2017-09-18 15:26:32 +0200 | [diff] [blame] | 12140 | rule->check_ptr = check_http_req_capture; |
Thierry FOURNIER | 32b1500 | 2015-07-31 08:56:16 +0200 | [diff] [blame] | 12141 | rule->arg.cap.expr = expr; |
| 12142 | rule->arg.cap.hdr = hdr; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12143 | } |
| 12144 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12145 | else if (strcmp(args[cur_arg], "id") == 0) { |
| 12146 | int id; |
| 12147 | char *error; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12148 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12149 | cur_arg++; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12150 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12151 | if (!args[cur_arg]) { |
| 12152 | memprintf(err, "missing id value"); |
| 12153 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12154 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12155 | } |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12156 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12157 | id = strtol(args[cur_arg], &error, 10); |
| 12158 | if (*error != '\0') { |
| 12159 | memprintf(err, "cannot parse id '%s'", args[cur_arg]); |
| 12160 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12161 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12162 | } |
| 12163 | cur_arg++; |
| 12164 | |
Olivier Houchard | fbc74e8 | 2017-11-24 16:54:05 +0100 | [diff] [blame] | 12165 | px->conf.args.ctx = ARGC_CAP; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12166 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 12167 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12168 | rule->action_ptr = http_action_req_capture_by_id; |
Christopher Faulet | 29730ba | 2017-09-18 15:26:32 +0200 | [diff] [blame] | 12169 | rule->check_ptr = check_http_req_capture; |
Thierry FOURNIER | e209797 | 2015-07-31 08:56:35 +0200 | [diff] [blame] | 12170 | rule->arg.capid.expr = expr; |
| 12171 | rule->arg.capid.idx = id; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12172 | } |
| 12173 | |
| 12174 | else { |
| 12175 | memprintf(err, "expects 'len' or 'id', found '%s'", args[cur_arg]); |
| 12176 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12177 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12178 | } |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12179 | |
| 12180 | *orig_arg = cur_arg; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12181 | return ACT_RET_PRS_OK; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12182 | } |
| 12183 | |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12184 | /* This function executes the "capture" action and store the result in a |
| 12185 | * capture slot if exists. It executes a fetch expression, turns the result |
| 12186 | * into a string and puts it in a capture slot. It always returns 1. If an |
| 12187 | * error occurs the action is cancelled, but the rule processing continues. |
| 12188 | */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12189 | 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] | 12190 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12191 | { |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12192 | struct sample *key; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12193 | struct cap_hdr *h; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12194 | char **cap = s->res_cap; |
| 12195 | struct proxy *fe = strm_fe(s); |
| 12196 | int len; |
| 12197 | int i; |
| 12198 | |
| 12199 | /* Look for the original configuration. */ |
| 12200 | for (h = fe->rsp_cap, i = fe->nb_rsp_cap - 1; |
Thierry FOURNIER | e209797 | 2015-07-31 08:56:35 +0200 | [diff] [blame] | 12201 | h != NULL && i != rule->arg.capid.idx ; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12202 | i--, h = h->next); |
| 12203 | if (!h) |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12204 | return ACT_RET_CONT; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12205 | |
Thierry FOURNIER | e209797 | 2015-07-31 08:56:35 +0200 | [diff] [blame] | 12206 | 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] | 12207 | if (!key) |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12208 | return ACT_RET_CONT; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12209 | |
| 12210 | if (cap[h->index] == NULL) |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 12211 | cap[h->index] = pool_alloc(h->pool); |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12212 | |
| 12213 | if (cap[h->index] == NULL) /* no more capture memory */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12214 | return ACT_RET_CONT; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12215 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12216 | len = key->data.u.str.len; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12217 | if (len > h->len) |
| 12218 | len = h->len; |
| 12219 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12220 | memcpy(cap[h->index], key->data.u.str.str, len); |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12221 | cap[h->index][len] = 0; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12222 | return ACT_RET_CONT; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12223 | } |
| 12224 | |
Christopher Faulet | 29730ba | 2017-09-18 15:26:32 +0200 | [diff] [blame] | 12225 | /* Check an "http-response capture" action. |
| 12226 | * |
| 12227 | * The function returns 1 in success case, otherwise, it returns 0 and err is |
| 12228 | * filled. |
| 12229 | */ |
| 12230 | int check_http_res_capture(struct act_rule *rule, struct proxy *px, char **err) |
| 12231 | { |
| 12232 | if (rule->arg.capid.idx >= px->nb_rsp_cap) { |
| 12233 | memprintf(err, "unable to find capture id '%d' referenced by http-response capture rule", |
| 12234 | rule->arg.capid.idx); |
| 12235 | return 0; |
| 12236 | } |
| 12237 | |
| 12238 | return 1; |
| 12239 | } |
| 12240 | |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12241 | /* parse an "http-response capture" action. It takes a single argument which is |
| 12242 | * a sample fetch expression. It stores the expression into arg->act.p[0] and |
| 12243 | * 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] | 12244 | * 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] | 12245 | */ |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12246 | enum act_parse_ret parse_http_res_capture(const char **args, int *orig_arg, struct proxy *px, |
| 12247 | struct act_rule *rule, char **err) |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12248 | { |
| 12249 | struct sample_expr *expr; |
| 12250 | int cur_arg; |
| 12251 | int id; |
| 12252 | char *error; |
| 12253 | |
| 12254 | for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++) |
| 12255 | if (strcmp(args[cur_arg], "if") == 0 || |
| 12256 | strcmp(args[cur_arg], "unless") == 0) |
| 12257 | break; |
| 12258 | |
| 12259 | if (cur_arg < *orig_arg + 3) { |
Willy Tarreau | 29bdb1c | 2016-06-24 15:36:34 +0200 | [diff] [blame] | 12260 | memprintf(err, "expects <expression> id <idx>"); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12261 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12262 | } |
| 12263 | |
| 12264 | cur_arg = *orig_arg; |
| 12265 | expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args); |
| 12266 | if (!expr) |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12267 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12268 | |
| 12269 | if (!(expr->fetch->val & SMP_VAL_FE_HRS_HDR)) { |
| 12270 | memprintf(err, |
| 12271 | "fetch method '%s' extracts information from '%s', none of which is available here", |
| 12272 | args[cur_arg-1], sample_src_names(expr->fetch->use)); |
| 12273 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12274 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12275 | } |
| 12276 | |
| 12277 | if (!args[cur_arg] || !*args[cur_arg]) { |
Willy Tarreau | 29bdb1c | 2016-06-24 15:36:34 +0200 | [diff] [blame] | 12278 | memprintf(err, "expects 'id'"); |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12279 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12280 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12281 | } |
| 12282 | |
| 12283 | if (strcmp(args[cur_arg], "id") != 0) { |
| 12284 | memprintf(err, "expects 'id', found '%s'", args[cur_arg]); |
| 12285 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12286 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12287 | } |
| 12288 | |
| 12289 | cur_arg++; |
| 12290 | |
| 12291 | if (!args[cur_arg]) { |
| 12292 | memprintf(err, "missing id value"); |
| 12293 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12294 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12295 | } |
| 12296 | |
| 12297 | id = strtol(args[cur_arg], &error, 10); |
| 12298 | if (*error != '\0') { |
| 12299 | memprintf(err, "cannot parse id '%s'", args[cur_arg]); |
| 12300 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12301 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12302 | } |
| 12303 | cur_arg++; |
| 12304 | |
Olivier Houchard | fbc74e8 | 2017-11-24 16:54:05 +0100 | [diff] [blame] | 12305 | px->conf.args.ctx = ARGC_CAP; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12306 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 12307 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12308 | rule->action_ptr = http_action_res_capture_by_id; |
Christopher Faulet | 29730ba | 2017-09-18 15:26:32 +0200 | [diff] [blame] | 12309 | rule->check_ptr = check_http_res_capture; |
Thierry FOURNIER | e209797 | 2015-07-31 08:56:35 +0200 | [diff] [blame] | 12310 | rule->arg.capid.expr = expr; |
| 12311 | rule->arg.capid.idx = id; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12312 | |
| 12313 | *orig_arg = cur_arg; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12314 | return ACT_RET_PRS_OK; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12315 | } |
| 12316 | |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 12317 | /* |
| 12318 | * Return the struct http_req_action_kw associated to a keyword. |
| 12319 | */ |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 12320 | struct action_kw *action_http_req_custom(const char *kw) |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 12321 | { |
Thierry FOURNIER | 322a124 | 2015-08-19 09:07:47 +0200 | [diff] [blame] | 12322 | return action_lookup(&http_req_keywords.list, kw); |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 12323 | } |
| 12324 | |
| 12325 | /* |
| 12326 | * Return the struct http_res_action_kw associated to a keyword. |
| 12327 | */ |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 12328 | struct action_kw *action_http_res_custom(const char *kw) |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 12329 | { |
Thierry FOURNIER | 322a124 | 2015-08-19 09:07:47 +0200 | [diff] [blame] | 12330 | return action_lookup(&http_res_keywords.list, kw); |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 12331 | } |
| 12332 | |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12333 | |
| 12334 | /* "show errors" handler for the CLI. Returns 0 if wants to continue, 1 to stop |
| 12335 | * now. |
| 12336 | */ |
| 12337 | static int cli_parse_show_errors(char **args, struct appctx *appctx, void *private) |
| 12338 | { |
| 12339 | if (!cli_has_level(appctx, ACCESS_LVL_OPER)) |
| 12340 | return 1; |
| 12341 | |
Willy Tarreau | 234ba2d | 2016-11-25 08:39:10 +0100 | [diff] [blame] | 12342 | if (*args[2]) { |
| 12343 | struct proxy *px; |
| 12344 | |
| 12345 | px = proxy_find_by_name(args[2], 0, 0); |
| 12346 | if (px) |
| 12347 | appctx->ctx.errors.iid = px->uuid; |
| 12348 | else |
| 12349 | appctx->ctx.errors.iid = atoi(args[2]); |
| 12350 | |
| 12351 | if (!appctx->ctx.errors.iid) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 12352 | appctx->ctx.cli.severity = LOG_ERR; |
Willy Tarreau | 234ba2d | 2016-11-25 08:39:10 +0100 | [diff] [blame] | 12353 | appctx->ctx.cli.msg = "No such proxy.\n"; |
| 12354 | appctx->st0 = CLI_ST_PRINT; |
| 12355 | return 1; |
| 12356 | } |
| 12357 | } |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12358 | else |
Willy Tarreau | 234ba2d | 2016-11-25 08:39:10 +0100 | [diff] [blame] | 12359 | appctx->ctx.errors.iid = -1; // dump all proxies |
| 12360 | |
Willy Tarreau | 35069f8 | 2016-11-25 09:16:37 +0100 | [diff] [blame] | 12361 | appctx->ctx.errors.flag = 0; |
| 12362 | if (strcmp(args[3], "request") == 0) |
| 12363 | appctx->ctx.errors.flag |= 4; // ignore response |
| 12364 | else if (strcmp(args[3], "response") == 0) |
| 12365 | appctx->ctx.errors.flag |= 2; // ignore request |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12366 | appctx->ctx.errors.px = NULL; |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12367 | return 0; |
| 12368 | } |
| 12369 | |
| 12370 | /* This function dumps all captured errors onto the stream interface's |
| 12371 | * read buffer. It returns 0 if the output buffer is full and it needs |
| 12372 | * to be called again, otherwise non-zero. |
| 12373 | */ |
| 12374 | static int cli_io_handler_show_errors(struct appctx *appctx) |
| 12375 | { |
| 12376 | struct stream_interface *si = appctx->owner; |
| 12377 | extern const char *monthname[12]; |
| 12378 | |
| 12379 | if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) |
| 12380 | return 1; |
| 12381 | |
| 12382 | chunk_reset(&trash); |
| 12383 | |
| 12384 | if (!appctx->ctx.errors.px) { |
| 12385 | /* the function had not been called yet, let's prepare the |
| 12386 | * buffer for a response. |
| 12387 | */ |
| 12388 | struct tm tm; |
| 12389 | |
| 12390 | get_localtime(date.tv_sec, &tm); |
| 12391 | chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n", |
| 12392 | tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900, |
| 12393 | tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000), |
| 12394 | error_snapshot_id); |
| 12395 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 12396 | if (ci_putchk(si_ic(si), &trash) == -1) { |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12397 | /* Socket buffer full. Let's try again later from the same point */ |
| 12398 | si_applet_cant_put(si); |
| 12399 | return 0; |
| 12400 | } |
| 12401 | |
Olivier Houchard | fbc74e8 | 2017-11-24 16:54:05 +0100 | [diff] [blame] | 12402 | appctx->ctx.errors.px = proxies_list; |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12403 | appctx->ctx.errors.bol = 0; |
| 12404 | appctx->ctx.errors.ptr = -1; |
| 12405 | } |
| 12406 | |
| 12407 | /* we have two inner loops here, one for the proxy, the other one for |
| 12408 | * the buffer. |
| 12409 | */ |
| 12410 | while (appctx->ctx.errors.px) { |
| 12411 | struct error_snapshot *es; |
| 12412 | |
Willy Tarreau | 35069f8 | 2016-11-25 09:16:37 +0100 | [diff] [blame] | 12413 | if ((appctx->ctx.errors.flag & 1) == 0) { |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12414 | es = &appctx->ctx.errors.px->invalid_req; |
Willy Tarreau | 35069f8 | 2016-11-25 09:16:37 +0100 | [diff] [blame] | 12415 | if (appctx->ctx.errors.flag & 2) // skip req |
| 12416 | goto next; |
| 12417 | } |
| 12418 | else { |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12419 | es = &appctx->ctx.errors.px->invalid_rep; |
Willy Tarreau | 35069f8 | 2016-11-25 09:16:37 +0100 | [diff] [blame] | 12420 | if (appctx->ctx.errors.flag & 4) // skip resp |
| 12421 | goto next; |
| 12422 | } |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12423 | |
| 12424 | if (!es->when.tv_sec) |
| 12425 | goto next; |
| 12426 | |
| 12427 | if (appctx->ctx.errors.iid >= 0 && |
| 12428 | appctx->ctx.errors.px->uuid != appctx->ctx.errors.iid && |
| 12429 | es->oe->uuid != appctx->ctx.errors.iid) |
| 12430 | goto next; |
| 12431 | |
| 12432 | if (appctx->ctx.errors.ptr < 0) { |
| 12433 | /* just print headers now */ |
| 12434 | |
| 12435 | char pn[INET6_ADDRSTRLEN]; |
| 12436 | struct tm tm; |
| 12437 | int port; |
| 12438 | |
| 12439 | get_localtime(es->when.tv_sec, &tm); |
| 12440 | chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]", |
| 12441 | tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900, |
| 12442 | tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000)); |
| 12443 | |
| 12444 | switch (addr_to_str(&es->src, pn, sizeof(pn))) { |
| 12445 | case AF_INET: |
| 12446 | case AF_INET6: |
| 12447 | port = get_host_port(&es->src); |
| 12448 | break; |
| 12449 | default: |
| 12450 | port = 0; |
| 12451 | } |
| 12452 | |
Willy Tarreau | 35069f8 | 2016-11-25 09:16:37 +0100 | [diff] [blame] | 12453 | switch (appctx->ctx.errors.flag & 1) { |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12454 | case 0: |
| 12455 | chunk_appendf(&trash, |
| 12456 | " frontend %s (#%d): invalid request\n" |
| 12457 | " backend %s (#%d)", |
| 12458 | appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid, |
| 12459 | (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>", |
| 12460 | (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1); |
| 12461 | break; |
| 12462 | case 1: |
| 12463 | chunk_appendf(&trash, |
| 12464 | " backend %s (#%d): invalid response\n" |
| 12465 | " frontend %s (#%d)", |
| 12466 | appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid, |
| 12467 | es->oe->id, es->oe->uuid); |
| 12468 | break; |
| 12469 | } |
| 12470 | |
| 12471 | chunk_appendf(&trash, |
| 12472 | ", server %s (#%d), event #%u\n" |
| 12473 | " src %s:%d, session #%d, session flags 0x%08x\n" |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 12474 | " 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] | 12475 | " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n" |
| 12476 | " buffer flags 0x%08x, out %d bytes, total %lld bytes\n" |
| 12477 | " pending %d bytes, wrapping at %d, error at position %d:\n \n", |
| 12478 | es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1, |
| 12479 | es->ev_id, |
| 12480 | pn, port, es->sid, es->s_flags, |
Willy Tarreau | 0da5b3b | 2017-09-21 09:30:46 +0200 | [diff] [blame] | 12481 | 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] | 12482 | es->m_clen, es->m_blen, |
| 12483 | es->b_flags, es->b_out, es->b_tot, |
| 12484 | es->len, es->b_wrap, es->pos); |
| 12485 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 12486 | if (ci_putchk(si_ic(si), &trash) == -1) { |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12487 | /* Socket buffer full. Let's try again later from the same point */ |
| 12488 | si_applet_cant_put(si); |
| 12489 | return 0; |
| 12490 | } |
| 12491 | appctx->ctx.errors.ptr = 0; |
| 12492 | appctx->ctx.errors.sid = es->sid; |
| 12493 | } |
| 12494 | |
| 12495 | if (appctx->ctx.errors.sid != es->sid) { |
| 12496 | /* the snapshot changed while we were dumping it */ |
| 12497 | chunk_appendf(&trash, |
| 12498 | " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n"); |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 12499 | if (ci_putchk(si_ic(si), &trash) == -1) { |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12500 | si_applet_cant_put(si); |
| 12501 | return 0; |
| 12502 | } |
| 12503 | goto next; |
| 12504 | } |
| 12505 | |
| 12506 | /* OK, ptr >= 0, so we have to dump the current line */ |
| 12507 | while (es->buf && appctx->ctx.errors.ptr < es->len && appctx->ctx.errors.ptr < global.tune.bufsize) { |
| 12508 | int newptr; |
| 12509 | int newline; |
| 12510 | |
| 12511 | newline = appctx->ctx.errors.bol; |
| 12512 | newptr = dump_text_line(&trash, es->buf, global.tune.bufsize, es->len, &newline, appctx->ctx.errors.ptr); |
| 12513 | if (newptr == appctx->ctx.errors.ptr) |
| 12514 | return 0; |
| 12515 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 12516 | if (ci_putchk(si_ic(si), &trash) == -1) { |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12517 | /* Socket buffer full. Let's try again later from the same point */ |
| 12518 | si_applet_cant_put(si); |
| 12519 | return 0; |
| 12520 | } |
| 12521 | appctx->ctx.errors.ptr = newptr; |
| 12522 | appctx->ctx.errors.bol = newline; |
| 12523 | }; |
| 12524 | next: |
| 12525 | appctx->ctx.errors.bol = 0; |
| 12526 | appctx->ctx.errors.ptr = -1; |
Willy Tarreau | 35069f8 | 2016-11-25 09:16:37 +0100 | [diff] [blame] | 12527 | appctx->ctx.errors.flag ^= 1; |
| 12528 | if (!(appctx->ctx.errors.flag & 1)) |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12529 | appctx->ctx.errors.px = appctx->ctx.errors.px->next; |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12530 | } |
| 12531 | |
| 12532 | /* dump complete */ |
| 12533 | return 1; |
| 12534 | } |
| 12535 | |
| 12536 | /* register cli keywords */ |
| 12537 | static struct cli_kw_list cli_kws = {{ },{ |
| 12538 | { { "show", "errors", NULL }, |
| 12539 | "show errors : report last request and response errors for each proxy", |
| 12540 | cli_parse_show_errors, cli_io_handler_show_errors, NULL, |
| 12541 | }, |
| 12542 | {{},} |
| 12543 | }}; |
| 12544 | |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 12545 | /************************************************************************/ |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12546 | /* All supported ACL keywords must be declared here. */ |
| 12547 | /************************************************************************/ |
| 12548 | |
| 12549 | /* Note: must not be declared <const> as its list will be overwritten. |
| 12550 | * Please take care of keeping this list alphabetically sorted. |
| 12551 | */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 12552 | static struct acl_kw_list acl_kws = {ILH, { |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12553 | { "base", "base", PAT_MATCH_STR }, |
| 12554 | { "base_beg", "base", PAT_MATCH_BEG }, |
| 12555 | { "base_dir", "base", PAT_MATCH_DIR }, |
| 12556 | { "base_dom", "base", PAT_MATCH_DOM }, |
| 12557 | { "base_end", "base", PAT_MATCH_END }, |
| 12558 | { "base_len", "base", PAT_MATCH_LEN }, |
| 12559 | { "base_reg", "base", PAT_MATCH_REG }, |
| 12560 | { "base_sub", "base", PAT_MATCH_SUB }, |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 12561 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12562 | { "cook", "req.cook", PAT_MATCH_STR }, |
| 12563 | { "cook_beg", "req.cook", PAT_MATCH_BEG }, |
| 12564 | { "cook_dir", "req.cook", PAT_MATCH_DIR }, |
| 12565 | { "cook_dom", "req.cook", PAT_MATCH_DOM }, |
| 12566 | { "cook_end", "req.cook", PAT_MATCH_END }, |
| 12567 | { "cook_len", "req.cook", PAT_MATCH_LEN }, |
| 12568 | { "cook_reg", "req.cook", PAT_MATCH_REG }, |
| 12569 | { "cook_sub", "req.cook", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12570 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12571 | { "hdr", "req.hdr", PAT_MATCH_STR }, |
| 12572 | { "hdr_beg", "req.hdr", PAT_MATCH_BEG }, |
| 12573 | { "hdr_dir", "req.hdr", PAT_MATCH_DIR }, |
| 12574 | { "hdr_dom", "req.hdr", PAT_MATCH_DOM }, |
| 12575 | { "hdr_end", "req.hdr", PAT_MATCH_END }, |
| 12576 | { "hdr_len", "req.hdr", PAT_MATCH_LEN }, |
| 12577 | { "hdr_reg", "req.hdr", PAT_MATCH_REG }, |
| 12578 | { "hdr_sub", "req.hdr", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12579 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12580 | /* these two declarations uses strings with list storage (in place |
| 12581 | * of tree storage). The basic match is PAT_MATCH_STR, but the indexation |
| 12582 | * and delete functions are relative to the list management. The parse |
| 12583 | * and match method are related to the corresponding fetch methods. This |
| 12584 | * is very particular ACL declaration mode. |
| 12585 | */ |
| 12586 | { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth }, |
| 12587 | { "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] | 12588 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12589 | { "path", "path", PAT_MATCH_STR }, |
| 12590 | { "path_beg", "path", PAT_MATCH_BEG }, |
| 12591 | { "path_dir", "path", PAT_MATCH_DIR }, |
| 12592 | { "path_dom", "path", PAT_MATCH_DOM }, |
| 12593 | { "path_end", "path", PAT_MATCH_END }, |
| 12594 | { "path_len", "path", PAT_MATCH_LEN }, |
| 12595 | { "path_reg", "path", PAT_MATCH_REG }, |
| 12596 | { "path_sub", "path", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12597 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12598 | { "req_ver", "req.ver", PAT_MATCH_STR }, |
| 12599 | { "resp_ver", "res.ver", PAT_MATCH_STR }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12600 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12601 | { "scook", "res.cook", PAT_MATCH_STR }, |
| 12602 | { "scook_beg", "res.cook", PAT_MATCH_BEG }, |
| 12603 | { "scook_dir", "res.cook", PAT_MATCH_DIR }, |
| 12604 | { "scook_dom", "res.cook", PAT_MATCH_DOM }, |
| 12605 | { "scook_end", "res.cook", PAT_MATCH_END }, |
| 12606 | { "scook_len", "res.cook", PAT_MATCH_LEN }, |
| 12607 | { "scook_reg", "res.cook", PAT_MATCH_REG }, |
| 12608 | { "scook_sub", "res.cook", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12609 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12610 | { "shdr", "res.hdr", PAT_MATCH_STR }, |
| 12611 | { "shdr_beg", "res.hdr", PAT_MATCH_BEG }, |
| 12612 | { "shdr_dir", "res.hdr", PAT_MATCH_DIR }, |
| 12613 | { "shdr_dom", "res.hdr", PAT_MATCH_DOM }, |
| 12614 | { "shdr_end", "res.hdr", PAT_MATCH_END }, |
| 12615 | { "shdr_len", "res.hdr", PAT_MATCH_LEN }, |
| 12616 | { "shdr_reg", "res.hdr", PAT_MATCH_REG }, |
| 12617 | { "shdr_sub", "res.hdr", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12618 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12619 | { "url", "url", PAT_MATCH_STR }, |
| 12620 | { "url_beg", "url", PAT_MATCH_BEG }, |
| 12621 | { "url_dir", "url", PAT_MATCH_DIR }, |
| 12622 | { "url_dom", "url", PAT_MATCH_DOM }, |
| 12623 | { "url_end", "url", PAT_MATCH_END }, |
| 12624 | { "url_len", "url", PAT_MATCH_LEN }, |
| 12625 | { "url_reg", "url", PAT_MATCH_REG }, |
| 12626 | { "url_sub", "url", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12627 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12628 | { "urlp", "urlp", PAT_MATCH_STR }, |
| 12629 | { "urlp_beg", "urlp", PAT_MATCH_BEG }, |
| 12630 | { "urlp_dir", "urlp", PAT_MATCH_DIR }, |
| 12631 | { "urlp_dom", "urlp", PAT_MATCH_DOM }, |
| 12632 | { "urlp_end", "urlp", PAT_MATCH_END }, |
| 12633 | { "urlp_len", "urlp", PAT_MATCH_LEN }, |
| 12634 | { "urlp_reg", "urlp", PAT_MATCH_REG }, |
| 12635 | { "urlp_sub", "urlp", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12636 | |
Willy Tarreau | 8ed669b | 2013-01-11 15:49:37 +0100 | [diff] [blame] | 12637 | { /* END */ }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12638 | }}; |
| 12639 | |
| 12640 | /************************************************************************/ |
| 12641 | /* All supported pattern keywords must be declared here. */ |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 12642 | /************************************************************************/ |
| 12643 | /* Note: must not be declared <const> as its list will be overwritten */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 12644 | static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, { |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12645 | { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 12646 | { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 12647 | { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV }, |
| 12648 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 12649 | /* capture are allocated and are permanent in the stream */ |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 12650 | { "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] | 12651 | |
| 12652 | /* retrieve these captures from the HTTP logs */ |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 12653 | { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP }, |
| 12654 | { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP }, |
| 12655 | { "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] | 12656 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 12657 | { "capture.res.hdr", smp_fetch_capture_header_res, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP }, |
| 12658 | { "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] | 12659 | |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 12660 | /* cookie is valid in both directions (eg: for "stick ...") but cook* |
| 12661 | * are only here to match the ACL's name, are request-only and are used |
| 12662 | * for ACL compatibility only. |
| 12663 | */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12664 | { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 12665 | { "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] | 12666 | { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 12667 | { "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] | 12668 | |
| 12669 | /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are |
| 12670 | * only here to match the ACL's name, are request-only and are used for |
| 12671 | * ACL compatibility only. |
| 12672 | */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12673 | { "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] | 12674 | { "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] | 12675 | { "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] | 12676 | { "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] | 12677 | |
Willy Tarreau | 0a0daec | 2013-04-02 22:44:58 +0200 | [diff] [blame] | 12678 | { "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] | 12679 | { "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] | 12680 | { "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] | 12681 | { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP }, |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12682 | { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 12683 | { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 12684 | |
| 12685 | /* HTTP protocol on the request path */ |
| 12686 | { "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] | 12687 | { "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] | 12688 | |
| 12689 | /* HTTP version on the request path */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12690 | { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 12691 | { "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] | 12692 | |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 12693 | { "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] | 12694 | { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 12695 | { "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] | 12696 | { "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] | 12697 | |
Thierry FOURNIER | d7d8881 | 2017-04-19 15:15:14 +0200 | [diff] [blame] | 12698 | { "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] | 12699 | { "req.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV }, |
| 12700 | |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 12701 | /* HTTP version on the response path */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12702 | { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV }, |
| 12703 | { "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] | 12704 | |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 12705 | /* 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] | 12706 | { "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] | 12707 | { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 12708 | { "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] | 12709 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12710 | { "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] | 12711 | { "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] | 12712 | { "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] | 12713 | { "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] | 12714 | { "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] | 12715 | { "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] | 12716 | { "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] | 12717 | |
| 12718 | /* 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] | 12719 | { "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] | 12720 | { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV }, |
| 12721 | { "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] | 12722 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12723 | { "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] | 12724 | { "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] | 12725 | { "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] | 12726 | { "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] | 12727 | { "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] | 12728 | { "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] | 12729 | { "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] | 12730 | |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 12731 | /* 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] | 12732 | { "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] | 12733 | { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV }, |
| 12734 | { "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] | 12735 | { "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] | 12736 | |
| 12737 | /* 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] | 12738 | { "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] | 12739 | { "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] | 12740 | { "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] | 12741 | { "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] | 12742 | |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 12743 | { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP }, |
Thierry Fournier | 0e00dca | 2016-04-07 15:47:40 +0200 | [diff] [blame] | 12744 | { "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] | 12745 | { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 12746 | { "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] | 12747 | { "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] | 12748 | { "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] | 12749 | { "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] | 12750 | { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 12751 | { "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] | 12752 | { "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] | 12753 | { /* END */ }, |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 12754 | }}; |
| 12755 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 12756 | |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12757 | /************************************************************************/ |
| 12758 | /* All supported converter keywords must be declared here. */ |
| 12759 | /************************************************************************/ |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 12760 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 12761 | static struct sample_conv_kw_list sample_conv_kws = {ILH, { |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 12762 | { "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] | 12763 | { "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] | 12764 | { "capture-req", smp_conv_req_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR}, |
| 12765 | { "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] | 12766 | { "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] | 12767 | { NULL, NULL, 0, 0, 0 }, |
| 12768 | }}; |
| 12769 | |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 12770 | |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12771 | /************************************************************************/ |
| 12772 | /* All supported http-request action keywords must be declared here. */ |
| 12773 | /************************************************************************/ |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 12774 | struct action_kw_list http_req_actions = { |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12775 | .kw = { |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12776 | { "capture", parse_http_req_capture }, |
Willy Tarreau | 53275e8 | 2017-11-24 07:52:01 +0100 | [diff] [blame] | 12777 | { "reject", parse_http_action_reject }, |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12778 | { "set-method", parse_set_req_line }, |
| 12779 | { "set-path", parse_set_req_line }, |
| 12780 | { "set-query", parse_set_req_line }, |
| 12781 | { "set-uri", parse_set_req_line }, |
Willy Tarreau | cb703b0 | 2015-04-03 09:52:01 +0200 | [diff] [blame] | 12782 | { NULL, NULL } |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12783 | } |
| 12784 | }; |
| 12785 | |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 12786 | struct action_kw_list http_res_actions = { |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12787 | .kw = { |
| 12788 | { "capture", parse_http_res_capture }, |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 12789 | { "set-status", parse_http_set_status }, |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12790 | { NULL, NULL } |
| 12791 | } |
| 12792 | }; |
| 12793 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 12794 | __attribute__((constructor)) |
| 12795 | static void __http_protocol_init(void) |
| 12796 | { |
| 12797 | acl_register_keywords(&acl_kws); |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 12798 | sample_register_fetches(&sample_fetch_keywords); |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 12799 | sample_register_convs(&sample_conv_kws); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12800 | http_req_keywords_register(&http_req_actions); |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12801 | http_res_keywords_register(&http_res_actions); |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12802 | cli_register_kw(&cli_kws); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 12803 | } |
| 12804 | |
| 12805 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 12806 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 12807 | * Local variables: |
| 12808 | * c-indent-level: 8 |
| 12809 | * c-basic-offset: 8 |
| 12810 | * End: |
| 12811 | */ |