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 | 9ed6203 | 2016-11-21 17:49:11 +0100 | [diff] [blame] | 45 | #include <types/stats.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 46 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 47 | #include <proto/acl.h> |
Thierry FOURNIER | 322a124 | 2015-08-19 09:07:47 +0200 | [diff] [blame] | 48 | #include <proto/action.h> |
Willy Tarreau | 61612d4 | 2012-04-19 18:42:05 +0200 | [diff] [blame] | 49 | #include <proto/arg.h> |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 50 | #include <proto/auth.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 51 | #include <proto/backend.h> |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 52 | #include <proto/channel.h> |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 53 | #include <proto/checks.h> |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 54 | #include <proto/cli.h> |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 55 | #include <proto/compression.h> |
William Lallemand | 9ed6203 | 2016-11-21 17:49:11 +0100 | [diff] [blame] | 56 | #include <proto/stats.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 57 | #include <proto/fd.h> |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 58 | #include <proto/filters.h> |
Willy Tarreau | 03fa5df | 2010-05-24 21:02:37 +0200 | [diff] [blame] | 59 | #include <proto/frontend.h> |
Willy Tarreau | 0da5b3b | 2017-09-21 09:30:46 +0200 | [diff] [blame] | 60 | #include <proto/h1.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 61 | #include <proto/log.h> |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 62 | #include <proto/hdr_idx.h> |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 63 | #include <proto/pattern.h> |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 64 | #include <proto/proto_tcp.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 65 | #include <proto/proto_http.h> |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 66 | #include <proto/proxy.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 67 | #include <proto/queue.h> |
Willy Tarreau | cd3b094 | 2012-04-27 21:52:18 +0200 | [diff] [blame] | 68 | #include <proto/sample.h> |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 69 | #include <proto/server.h> |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 70 | #include <proto/stream.h> |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 71 | #include <proto/stream_interface.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 72 | #include <proto/task.h> |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 73 | #include <proto/pattern.h> |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 74 | #include <proto/vars.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 75 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 76 | const char HTTP_100[] = |
| 77 | "HTTP/1.1 100 Continue\r\n\r\n"; |
| 78 | |
| 79 | const struct chunk http_100_chunk = { |
| 80 | .str = (char *)&HTTP_100, |
| 81 | .len = sizeof(HTTP_100)-1 |
| 82 | }; |
| 83 | |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 84 | /* Warning: no "connection" header is provided with the 3xx messages below */ |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 85 | const char *HTTP_301 = |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 86 | "HTTP/1.1 301 Moved Permanently\r\n" |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 87 | "Content-length: 0\r\n" |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 88 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 89 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 90 | const char *HTTP_302 = |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 91 | "HTTP/1.1 302 Found\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 92 | "Cache-Control: no-cache\r\n" |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 93 | "Content-length: 0\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 94 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 95 | |
| 96 | /* same as 302 except that the browser MUST retry with the GET method */ |
| 97 | const char *HTTP_303 = |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 98 | "HTTP/1.1 303 See Other\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 99 | "Cache-Control: no-cache\r\n" |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 100 | "Content-length: 0\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 101 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 102 | |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 103 | |
| 104 | /* same as 302 except that the browser MUST retry with the same method */ |
| 105 | const char *HTTP_307 = |
| 106 | "HTTP/1.1 307 Temporary Redirect\r\n" |
| 107 | "Cache-Control: no-cache\r\n" |
| 108 | "Content-length: 0\r\n" |
| 109 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 110 | |
| 111 | /* same as 301 except that the browser MUST retry with the same method */ |
| 112 | const char *HTTP_308 = |
| 113 | "HTTP/1.1 308 Permanent Redirect\r\n" |
| 114 | "Content-length: 0\r\n" |
| 115 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 116 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 117 | /* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */ |
| 118 | const char *HTTP_401_fmt = |
| 119 | "HTTP/1.0 401 Unauthorized\r\n" |
| 120 | "Cache-Control: no-cache\r\n" |
| 121 | "Connection: close\r\n" |
Willy Tarreau | 791d66d | 2006-07-08 16:53:38 +0200 | [diff] [blame] | 122 | "Content-Type: text/html\r\n" |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 123 | "WWW-Authenticate: Basic realm=\"%s\"\r\n" |
| 124 | "\r\n" |
| 125 | "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n"; |
| 126 | |
Willy Tarreau | 844a7e7 | 2010-01-31 21:46:18 +0100 | [diff] [blame] | 127 | const char *HTTP_407_fmt = |
| 128 | "HTTP/1.0 407 Unauthorized\r\n" |
| 129 | "Cache-Control: no-cache\r\n" |
| 130 | "Connection: close\r\n" |
| 131 | "Content-Type: text/html\r\n" |
| 132 | "Proxy-Authenticate: Basic realm=\"%s\"\r\n" |
| 133 | "\r\n" |
Godbach | 1f1fae6 | 2014-12-17 16:32:05 +0800 | [diff] [blame] | 134 | "<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] | 135 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 136 | |
| 137 | const int http_err_codes[HTTP_ERR_SIZE] = { |
Willy Tarreau | ae94d4d | 2011-05-11 16:28:49 +0200 | [diff] [blame] | 138 | [HTTP_ERR_200] = 200, /* used by "monitor-uri" */ |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 139 | [HTTP_ERR_400] = 400, |
| 140 | [HTTP_ERR_403] = 403, |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 141 | [HTTP_ERR_405] = 405, |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 142 | [HTTP_ERR_408] = 408, |
Olivier Houchard | 51a76d8 | 2017-10-02 16:12:07 +0200 | [diff] [blame] | 143 | [HTTP_ERR_425] = 425, |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 144 | [HTTP_ERR_429] = 429, |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 145 | [HTTP_ERR_500] = 500, |
| 146 | [HTTP_ERR_502] = 502, |
| 147 | [HTTP_ERR_503] = 503, |
| 148 | [HTTP_ERR_504] = 504, |
| 149 | }; |
| 150 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 151 | static const char *http_err_msgs[HTTP_ERR_SIZE] = { |
Willy Tarreau | ae94d4d | 2011-05-11 16:28:49 +0200 | [diff] [blame] | 152 | [HTTP_ERR_200] = |
| 153 | "HTTP/1.0 200 OK\r\n" |
| 154 | "Cache-Control: no-cache\r\n" |
| 155 | "Connection: close\r\n" |
| 156 | "Content-Type: text/html\r\n" |
| 157 | "\r\n" |
| 158 | "<html><body><h1>200 OK</h1>\nService ready.\n</body></html>\n", |
| 159 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 160 | [HTTP_ERR_400] = |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 161 | "HTTP/1.0 400 Bad request\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 162 | "Cache-Control: no-cache\r\n" |
| 163 | "Connection: close\r\n" |
| 164 | "Content-Type: text/html\r\n" |
| 165 | "\r\n" |
| 166 | "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n", |
| 167 | |
| 168 | [HTTP_ERR_403] = |
| 169 | "HTTP/1.0 403 Forbidden\r\n" |
| 170 | "Cache-Control: no-cache\r\n" |
| 171 | "Connection: close\r\n" |
| 172 | "Content-Type: text/html\r\n" |
| 173 | "\r\n" |
| 174 | "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n", |
| 175 | |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 176 | [HTTP_ERR_405] = |
| 177 | "HTTP/1.0 405 Method Not Allowed\r\n" |
| 178 | "Cache-Control: no-cache\r\n" |
| 179 | "Connection: close\r\n" |
| 180 | "Content-Type: text/html\r\n" |
| 181 | "\r\n" |
| 182 | "<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", |
| 183 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 184 | [HTTP_ERR_408] = |
| 185 | "HTTP/1.0 408 Request Time-out\r\n" |
| 186 | "Cache-Control: no-cache\r\n" |
| 187 | "Connection: close\r\n" |
| 188 | "Content-Type: text/html\r\n" |
| 189 | "\r\n" |
| 190 | "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n", |
| 191 | |
Olivier Houchard | 51a76d8 | 2017-10-02 16:12:07 +0200 | [diff] [blame] | 192 | [HTTP_ERR_425] = |
| 193 | "HTTP/1.0 425 Too Early\r\n" |
| 194 | "Cache-Control: no-cache\r\n" |
| 195 | "Connection: close\r\n" |
| 196 | "Content-Type: text/html\r\n" |
| 197 | "\r\n" |
| 198 | "<html><body><h1>425 Too Early</h1>\nYour browser sent early data.\n</body></html>\n", |
| 199 | |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 200 | [HTTP_ERR_429] = |
| 201 | "HTTP/1.0 429 Too Many Requests\r\n" |
| 202 | "Cache-Control: no-cache\r\n" |
| 203 | "Connection: close\r\n" |
| 204 | "Content-Type: text/html\r\n" |
| 205 | "\r\n" |
| 206 | "<html><body><h1>429 Too Many Requests</h1>\nYou have sent too many requests in a given amount of time.\n</body></html>\n", |
| 207 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 208 | [HTTP_ERR_500] = |
Jarno Huuskonen | 16ad94a | 2017-01-09 14:17:10 +0200 | [diff] [blame] | 209 | "HTTP/1.0 500 Internal Server Error\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 210 | "Cache-Control: no-cache\r\n" |
| 211 | "Connection: close\r\n" |
| 212 | "Content-Type: text/html\r\n" |
| 213 | "\r\n" |
Jarno Huuskonen | 16ad94a | 2017-01-09 14:17:10 +0200 | [diff] [blame] | 214 | "<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] | 215 | |
| 216 | [HTTP_ERR_502] = |
| 217 | "HTTP/1.0 502 Bad Gateway\r\n" |
| 218 | "Cache-Control: no-cache\r\n" |
| 219 | "Connection: close\r\n" |
| 220 | "Content-Type: text/html\r\n" |
| 221 | "\r\n" |
| 222 | "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n", |
| 223 | |
| 224 | [HTTP_ERR_503] = |
| 225 | "HTTP/1.0 503 Service Unavailable\r\n" |
| 226 | "Cache-Control: no-cache\r\n" |
| 227 | "Connection: close\r\n" |
| 228 | "Content-Type: text/html\r\n" |
| 229 | "\r\n" |
| 230 | "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n", |
| 231 | |
| 232 | [HTTP_ERR_504] = |
| 233 | "HTTP/1.0 504 Gateway Time-out\r\n" |
| 234 | "Cache-Control: no-cache\r\n" |
| 235 | "Connection: close\r\n" |
| 236 | "Content-Type: text/html\r\n" |
| 237 | "\r\n" |
| 238 | "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n", |
| 239 | |
| 240 | }; |
| 241 | |
Cyril Bonté | 19979e1 | 2012-04-04 12:57:21 +0200 | [diff] [blame] | 242 | /* status codes available for the stats admin page (strictly 4 chars length) */ |
| 243 | const char *stat_status_codes[STAT_STATUS_SIZE] = { |
| 244 | [STAT_STATUS_DENY] = "DENY", |
| 245 | [STAT_STATUS_DONE] = "DONE", |
| 246 | [STAT_STATUS_ERRP] = "ERRP", |
| 247 | [STAT_STATUS_EXCD] = "EXCD", |
| 248 | [STAT_STATUS_NONE] = "NONE", |
| 249 | [STAT_STATUS_PART] = "PART", |
| 250 | [STAT_STATUS_UNKN] = "UNKN", |
| 251 | }; |
| 252 | |
| 253 | |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 254 | /* List head of all known action keywords for "http-request" */ |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 255 | struct action_kw_list http_req_keywords = { |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 256 | .list = LIST_HEAD_INIT(http_req_keywords.list) |
| 257 | }; |
| 258 | |
| 259 | /* List head of all known action keywords for "http-response" */ |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 260 | struct action_kw_list http_res_keywords = { |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 261 | .list = LIST_HEAD_INIT(http_res_keywords.list) |
| 262 | }; |
| 263 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 264 | /* We must put the messages here since GCC cannot initialize consts depending |
| 265 | * on strlen(). |
| 266 | */ |
| 267 | struct chunk http_err_chunks[HTTP_ERR_SIZE]; |
| 268 | |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 269 | /* 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] | 270 | static THREAD_LOCAL struct hdr_ctx static_hdr_ctx; |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 271 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 272 | #define FD_SETS_ARE_BITFIELDS |
| 273 | #ifdef FD_SETS_ARE_BITFIELDS |
| 274 | /* |
| 275 | * This map is used with all the FD_* macros to check whether a particular bit |
| 276 | * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes |
| 277 | * which should be encoded. When FD_ISSET() returns non-zero, it means that the |
| 278 | * byte should be encoded. Be careful to always pass bytes from 0 to 255 |
| 279 | * exclusively to the macros. |
| 280 | */ |
| 281 | fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| 282 | 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] | 283 | 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] | 284 | |
| 285 | #else |
| 286 | #error "Check if your OS uses bitfields for fd_sets" |
| 287 | #endif |
| 288 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 289 | 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] | 290 | |
David Carlier | 7365f7d | 2016-04-04 11:54:42 +0100 | [diff] [blame] | 291 | static inline int http_msg_forward_body(struct stream *s, struct http_msg *msg); |
| 292 | 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] | 293 | |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 294 | /* This function returns a reason associated with the HTTP status. |
| 295 | * This function never fails, a message is always returned. |
| 296 | */ |
| 297 | const char *get_reason(unsigned int status) |
| 298 | { |
| 299 | switch (status) { |
| 300 | case 100: return "Continue"; |
| 301 | case 101: return "Switching Protocols"; |
| 302 | case 102: return "Processing"; |
| 303 | case 200: return "OK"; |
| 304 | case 201: return "Created"; |
| 305 | case 202: return "Accepted"; |
| 306 | case 203: return "Non-Authoritative Information"; |
| 307 | case 204: return "No Content"; |
| 308 | case 205: return "Reset Content"; |
| 309 | case 206: return "Partial Content"; |
| 310 | case 207: return "Multi-Status"; |
| 311 | case 210: return "Content Different"; |
| 312 | case 226: return "IM Used"; |
| 313 | case 300: return "Multiple Choices"; |
| 314 | case 301: return "Moved Permanently"; |
| 315 | case 302: return "Moved Temporarily"; |
| 316 | case 303: return "See Other"; |
| 317 | case 304: return "Not Modified"; |
| 318 | case 305: return "Use Proxy"; |
| 319 | case 307: return "Temporary Redirect"; |
| 320 | case 308: return "Permanent Redirect"; |
| 321 | case 310: return "Too many Redirects"; |
| 322 | case 400: return "Bad Request"; |
| 323 | case 401: return "Unauthorized"; |
| 324 | case 402: return "Payment Required"; |
| 325 | case 403: return "Forbidden"; |
| 326 | case 404: return "Not Found"; |
| 327 | case 405: return "Method Not Allowed"; |
| 328 | case 406: return "Not Acceptable"; |
| 329 | case 407: return "Proxy Authentication Required"; |
| 330 | case 408: return "Request Time-out"; |
| 331 | case 409: return "Conflict"; |
| 332 | case 410: return "Gone"; |
| 333 | case 411: return "Length Required"; |
| 334 | case 412: return "Precondition Failed"; |
| 335 | case 413: return "Request Entity Too Large"; |
| 336 | case 414: return "Request-URI Too Long"; |
| 337 | case 415: return "Unsupported Media Type"; |
| 338 | case 416: return "Requested range unsatisfiable"; |
| 339 | case 417: return "Expectation failed"; |
| 340 | case 418: return "I'm a teapot"; |
| 341 | case 422: return "Unprocessable entity"; |
| 342 | case 423: return "Locked"; |
| 343 | case 424: return "Method failure"; |
Olivier Houchard | 51a76d8 | 2017-10-02 16:12:07 +0200 | [diff] [blame] | 344 | case 425: return "Too Early"; |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 345 | case 426: return "Upgrade Required"; |
| 346 | case 428: return "Precondition Required"; |
| 347 | case 429: return "Too Many Requests"; |
| 348 | case 431: return "Request Header Fields Too Large"; |
| 349 | case 449: return "Retry With"; |
| 350 | case 450: return "Blocked by Windows Parental Controls"; |
| 351 | case 451: return "Unavailable For Legal Reasons"; |
| 352 | case 456: return "Unrecoverable Error"; |
| 353 | case 499: return "client has closed connection"; |
| 354 | case 500: return "Internal Server Error"; |
| 355 | case 501: return "Not Implemented"; |
Jarno Huuskonen | 59af2df | 2016-12-28 10:49:01 +0200 | [diff] [blame] | 356 | case 502: return "Bad Gateway or Proxy Error"; |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 357 | case 503: return "Service Unavailable"; |
| 358 | case 504: return "Gateway Time-out"; |
| 359 | case 505: return "HTTP Version not supported"; |
| 360 | case 506: return "Variant also negociate"; |
| 361 | case 507: return "Insufficient storage"; |
| 362 | case 508: return "Loop detected"; |
| 363 | case 509: return "Bandwidth Limit Exceeded"; |
| 364 | case 510: return "Not extended"; |
| 365 | case 511: return "Network authentication required"; |
| 366 | case 520: return "Web server is returning an unknown error"; |
| 367 | default: |
| 368 | switch (status) { |
| 369 | case 100 ... 199: return "Informational"; |
| 370 | case 200 ... 299: return "Success"; |
| 371 | case 300 ... 399: return "Redirection"; |
| 372 | case 400 ... 499: return "Client Error"; |
| 373 | case 500 ... 599: return "Server Error"; |
| 374 | default: return "Other"; |
| 375 | } |
| 376 | } |
| 377 | } |
| 378 | |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 379 | /* This function returns HTTP_ERR_<num> (enum) matching http status code. |
| 380 | * Returned value should match codes from http_err_codes. |
| 381 | */ |
| 382 | static const int http_get_status_idx(unsigned int status) |
| 383 | { |
| 384 | switch (status) { |
| 385 | case 200: return HTTP_ERR_200; |
| 386 | case 400: return HTTP_ERR_400; |
| 387 | case 403: return HTTP_ERR_403; |
| 388 | case 405: return HTTP_ERR_405; |
| 389 | case 408: return HTTP_ERR_408; |
Olivier Houchard | 51a76d8 | 2017-10-02 16:12:07 +0200 | [diff] [blame] | 390 | case 425: return HTTP_ERR_425; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 391 | case 429: return HTTP_ERR_429; |
| 392 | case 500: return HTTP_ERR_500; |
| 393 | case 502: return HTTP_ERR_502; |
| 394 | case 503: return HTTP_ERR_503; |
| 395 | case 504: return HTTP_ERR_504; |
| 396 | default: return HTTP_ERR_500; |
| 397 | } |
| 398 | } |
| 399 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 400 | void init_proto_http() |
| 401 | { |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 402 | int i; |
| 403 | char *tmp; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 404 | int msg; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 405 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 406 | for (msg = 0; msg < HTTP_ERR_SIZE; msg++) { |
| 407 | if (!http_err_msgs[msg]) { |
| 408 | Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg); |
| 409 | abort(); |
| 410 | } |
| 411 | |
| 412 | http_err_chunks[msg].str = (char *)http_err_msgs[msg]; |
| 413 | http_err_chunks[msg].len = strlen(http_err_msgs[msg]); |
| 414 | } |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 415 | |
| 416 | /* initialize the log header encoding map : '{|}"#' should be encoded with |
| 417 | * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ). |
| 418 | * URL encoding only requires '"', '#' to be encoded as well as non- |
| 419 | * printable characters above. |
| 420 | */ |
| 421 | memset(hdr_encode_map, 0, sizeof(hdr_encode_map)); |
| 422 | memset(url_encode_map, 0, sizeof(url_encode_map)); |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 423 | memset(http_encode_map, 0, sizeof(url_encode_map)); |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 424 | for (i = 0; i < 32; i++) { |
| 425 | FD_SET(i, hdr_encode_map); |
| 426 | FD_SET(i, url_encode_map); |
| 427 | } |
| 428 | for (i = 127; i < 256; i++) { |
| 429 | FD_SET(i, hdr_encode_map); |
| 430 | FD_SET(i, url_encode_map); |
| 431 | } |
| 432 | |
| 433 | tmp = "\"#{|}"; |
| 434 | while (*tmp) { |
| 435 | FD_SET(*tmp, hdr_encode_map); |
| 436 | tmp++; |
| 437 | } |
| 438 | |
| 439 | tmp = "\"#"; |
| 440 | while (*tmp) { |
| 441 | FD_SET(*tmp, url_encode_map); |
| 442 | tmp++; |
| 443 | } |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 444 | |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 445 | /* initialize the http header encoding map. The draft httpbis define the |
| 446 | * header content as: |
| 447 | * |
| 448 | * HTTP-message = start-line |
| 449 | * *( header-field CRLF ) |
| 450 | * CRLF |
| 451 | * [ message-body ] |
| 452 | * header-field = field-name ":" OWS field-value OWS |
| 453 | * field-value = *( field-content / obs-fold ) |
| 454 | * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ] |
| 455 | * obs-fold = CRLF 1*( SP / HTAB ) |
| 456 | * field-vchar = VCHAR / obs-text |
| 457 | * VCHAR = %x21-7E |
| 458 | * obs-text = %x80-FF |
| 459 | * |
| 460 | * All the chars are encoded except "VCHAR", "obs-text", SP and HTAB. |
| 461 | * The encoded chars are form 0x00 to 0x08, 0x0a to 0x1f and 0x7f. The |
| 462 | * "obs-fold" is volontary forgotten because haproxy remove this. |
| 463 | */ |
| 464 | memset(http_encode_map, 0, sizeof(http_encode_map)); |
| 465 | for (i = 0x00; i <= 0x08; i++) |
| 466 | FD_SET(i, http_encode_map); |
| 467 | for (i = 0x0a; i <= 0x1f; i++) |
| 468 | FD_SET(i, http_encode_map); |
| 469 | FD_SET(0x7f, http_encode_map); |
| 470 | |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 471 | /* memory allocations */ |
Willy Tarreau | 63986c7 | 2015-04-03 22:55:33 +0200 | [diff] [blame] | 472 | pool2_http_txn = create_pool("http_txn", sizeof(struct http_txn), MEM_F_SHARED); |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 473 | pool2_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED); |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 474 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 475 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 476 | /* |
| 477 | * We have 26 list of methods (1 per first letter), each of which can have |
| 478 | * up to 3 entries (2 valid, 1 null). |
| 479 | */ |
| 480 | struct http_method_desc { |
Willy Tarreau | c8987b3 | 2013-12-06 23:43:17 +0100 | [diff] [blame] | 481 | enum http_meth_t meth; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 482 | int len; |
| 483 | const char text[8]; |
| 484 | }; |
| 485 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 486 | const struct http_method_desc http_methods[26][3] = { |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 487 | ['C' - 'A'] = { |
| 488 | [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" }, |
| 489 | }, |
| 490 | ['D' - 'A'] = { |
| 491 | [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" }, |
| 492 | }, |
| 493 | ['G' - 'A'] = { |
| 494 | [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" }, |
| 495 | }, |
| 496 | ['H' - 'A'] = { |
| 497 | [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" }, |
| 498 | }, |
Christopher Faulet | d57ad64 | 2015-07-31 14:26:57 +0200 | [diff] [blame] | 499 | ['O' - 'A'] = { |
| 500 | [0] = { .meth = HTTP_METH_OPTIONS , .len=7, .text="OPTIONS" }, |
| 501 | }, |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 502 | ['P' - 'A'] = { |
| 503 | [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" }, |
| 504 | [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" }, |
| 505 | }, |
| 506 | ['T' - 'A'] = { |
| 507 | [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" }, |
| 508 | }, |
| 509 | /* rest is empty like this : |
Willy Tarreau | b7ce424 | 2015-09-03 17:15:21 +0200 | [diff] [blame] | 510 | * [0] = { .meth = HTTP_METH_OTHER , .len=0, .text="" }, |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 511 | */ |
| 512 | }; |
| 513 | |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 514 | const struct http_method_name http_known_methods[HTTP_METH_OTHER] = { |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 515 | [HTTP_METH_OPTIONS] = { "OPTIONS", 7 }, |
| 516 | [HTTP_METH_GET] = { "GET", 3 }, |
| 517 | [HTTP_METH_HEAD] = { "HEAD", 4 }, |
| 518 | [HTTP_METH_POST] = { "POST", 4 }, |
| 519 | [HTTP_METH_PUT] = { "PUT", 3 }, |
| 520 | [HTTP_METH_DELETE] = { "DELETE", 6 }, |
| 521 | [HTTP_METH_TRACE] = { "TRACE", 5 }, |
| 522 | [HTTP_METH_CONNECT] = { "CONNECT", 7 }, |
| 523 | }; |
| 524 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 525 | /* |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 526 | * Adds a header and its CRLF at the tail of the message's buffer, just before |
| 527 | * 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] | 528 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 529 | * of headers is automatically adjusted. The number of bytes added is returned |
| 530 | * on success, otherwise <0 is returned indicating an error. |
| 531 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 532 | 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] | 533 | { |
| 534 | int bytes, len; |
| 535 | |
| 536 | len = strlen(text); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 537 | 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] | 538 | if (!bytes) |
| 539 | return -1; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 540 | http_msg_move_end(msg, bytes); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 541 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 542 | } |
| 543 | |
| 544 | /* |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 545 | * Adds a header and its CRLF at the tail of the message's buffer, just before |
| 546 | * 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] | 547 | * the buffer is only opened and the space reserved, but nothing is copied. |
| 548 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 549 | * of headers is automatically adjusted. The number of bytes added is returned |
| 550 | * on success, otherwise <0 is returned indicating an error. |
| 551 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 552 | int http_header_add_tail2(struct http_msg *msg, |
| 553 | struct hdr_idx *hdr_idx, const char *text, int len) |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 554 | { |
| 555 | int bytes; |
| 556 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 557 | 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] | 558 | if (!bytes) |
| 559 | return -1; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 560 | http_msg_move_end(msg, bytes); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 561 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 562 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 563 | |
| 564 | /* |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 565 | * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon. |
| 566 | * If so, returns the position of the first non-space character relative to |
| 567 | * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries |
| 568 | * to return a pointer to the place after the first space. Returns 0 if the |
| 569 | * header name does not match. Checks are case-insensitive. |
| 570 | */ |
| 571 | int http_header_match2(const char *hdr, const char *end, |
| 572 | const char *name, int len) |
| 573 | { |
| 574 | const char *val; |
| 575 | |
| 576 | if (hdr + len >= end) |
| 577 | return 0; |
| 578 | if (hdr[len] != ':') |
| 579 | return 0; |
| 580 | if (strncasecmp(hdr, name, len) != 0) |
| 581 | return 0; |
| 582 | val = hdr + len + 1; |
| 583 | while (val < end && HTTP_IS_SPHT(*val)) |
| 584 | val++; |
| 585 | if ((val >= end) && (len + 2 <= end - hdr)) |
| 586 | return len + 2; /* we may replace starting from second space */ |
| 587 | return val - hdr; |
| 588 | } |
| 589 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 590 | /* Find the first or next occurrence of header <name> in message buffer <sol> |
| 591 | * using headers index <idx>, and return it in the <ctx> structure. This |
| 592 | * structure holds everything necessary to use the header and find next |
| 593 | * occurrence. If its <idx> member is 0, the header is searched from the |
| 594 | * beginning. Otherwise, the next occurrence is returned. The function returns |
| 595 | * 1 when it finds a value, and 0 when there is no more. It is very similar to |
| 596 | * http_find_header2() except that it is designed to work with full-line headers |
| 597 | * whose comma is not a delimiter but is part of the syntax. As a special case, |
| 598 | * if ctx->val is NULL when searching for a new values of a header, the current |
| 599 | * header is rescanned. This allows rescanning after a header deletion. |
| 600 | */ |
| 601 | int http_find_full_header2(const char *name, int len, |
| 602 | char *sol, struct hdr_idx *idx, |
| 603 | struct hdr_ctx *ctx) |
| 604 | { |
| 605 | char *eol, *sov; |
| 606 | int cur_idx, old_idx; |
| 607 | |
| 608 | cur_idx = ctx->idx; |
| 609 | if (cur_idx) { |
| 610 | /* We have previously returned a header, let's search another one */ |
| 611 | sol = ctx->line; |
| 612 | eol = sol + idx->v[cur_idx].len; |
| 613 | goto next_hdr; |
| 614 | } |
| 615 | |
| 616 | /* first request for this header */ |
| 617 | sol += hdr_idx_first_pos(idx); |
| 618 | old_idx = 0; |
| 619 | cur_idx = hdr_idx_first_idx(idx); |
| 620 | while (cur_idx) { |
| 621 | eol = sol + idx->v[cur_idx].len; |
| 622 | |
| 623 | if (len == 0) { |
| 624 | /* No argument was passed, we want any header. |
| 625 | * To achieve this, we simply build a fake request. */ |
| 626 | while (sol + len < eol && sol[len] != ':') |
| 627 | len++; |
| 628 | name = sol; |
| 629 | } |
| 630 | |
| 631 | if ((len < eol - sol) && |
| 632 | (sol[len] == ':') && |
| 633 | (strncasecmp(sol, name, len) == 0)) { |
| 634 | ctx->del = len; |
| 635 | sov = sol + len + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 636 | while (sov < eol && HTTP_IS_LWS(*sov)) |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 637 | sov++; |
| 638 | |
| 639 | ctx->line = sol; |
| 640 | ctx->prev = old_idx; |
| 641 | ctx->idx = cur_idx; |
| 642 | ctx->val = sov - sol; |
| 643 | ctx->tws = 0; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 644 | while (eol > sov && HTTP_IS_LWS(*(eol - 1))) { |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 645 | eol--; |
| 646 | ctx->tws++; |
| 647 | } |
| 648 | ctx->vlen = eol - sov; |
| 649 | return 1; |
| 650 | } |
| 651 | next_hdr: |
| 652 | sol = eol + idx->v[cur_idx].cr + 1; |
| 653 | old_idx = cur_idx; |
| 654 | cur_idx = idx->v[cur_idx].next; |
| 655 | } |
| 656 | return 0; |
| 657 | } |
| 658 | |
Willy Tarreau | c90dc23 | 2015-02-20 13:51:36 +0100 | [diff] [blame] | 659 | /* Find the first or next header field in message buffer <sol> using headers |
| 660 | * index <idx>, and return it in the <ctx> structure. This structure holds |
| 661 | * everything necessary to use the header and find next occurrence. If its |
| 662 | * <idx> member is 0, the first header is retrieved. Otherwise, the next |
| 663 | * occurrence is returned. The function returns 1 when it finds a value, and |
| 664 | * 0 when there is no more. It is equivalent to http_find_full_header2() with |
| 665 | * no header name. |
| 666 | */ |
| 667 | int http_find_next_header(char *sol, struct hdr_idx *idx, struct hdr_ctx *ctx) |
| 668 | { |
| 669 | char *eol, *sov; |
| 670 | int cur_idx, old_idx; |
| 671 | int len; |
| 672 | |
| 673 | cur_idx = ctx->idx; |
| 674 | if (cur_idx) { |
| 675 | /* We have previously returned a header, let's search another one */ |
| 676 | sol = ctx->line; |
| 677 | eol = sol + idx->v[cur_idx].len; |
| 678 | goto next_hdr; |
| 679 | } |
| 680 | |
| 681 | /* first request for this header */ |
| 682 | sol += hdr_idx_first_pos(idx); |
| 683 | old_idx = 0; |
| 684 | cur_idx = hdr_idx_first_idx(idx); |
| 685 | while (cur_idx) { |
| 686 | eol = sol + idx->v[cur_idx].len; |
| 687 | |
| 688 | len = 0; |
| 689 | while (1) { |
| 690 | if (len >= eol - sol) |
| 691 | goto next_hdr; |
| 692 | if (sol[len] == ':') |
| 693 | break; |
| 694 | len++; |
| 695 | } |
| 696 | |
| 697 | ctx->del = len; |
| 698 | sov = sol + len + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 699 | while (sov < eol && HTTP_IS_LWS(*sov)) |
Willy Tarreau | c90dc23 | 2015-02-20 13:51:36 +0100 | [diff] [blame] | 700 | sov++; |
| 701 | |
| 702 | ctx->line = sol; |
| 703 | ctx->prev = old_idx; |
| 704 | ctx->idx = cur_idx; |
| 705 | ctx->val = sov - sol; |
| 706 | ctx->tws = 0; |
| 707 | |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 708 | while (eol > sov && HTTP_IS_LWS(*(eol - 1))) { |
Willy Tarreau | c90dc23 | 2015-02-20 13:51:36 +0100 | [diff] [blame] | 709 | eol--; |
| 710 | ctx->tws++; |
| 711 | } |
| 712 | ctx->vlen = eol - sov; |
| 713 | return 1; |
| 714 | |
| 715 | next_hdr: |
| 716 | sol = eol + idx->v[cur_idx].cr + 1; |
| 717 | old_idx = cur_idx; |
| 718 | cur_idx = idx->v[cur_idx].next; |
| 719 | } |
| 720 | return 0; |
| 721 | } |
| 722 | |
Lukas Tribus | 2395368 | 2017-04-28 13:24:30 +0000 | [diff] [blame] | 723 | /* Find the end of the header value contained between <s> and <e>. See RFC7230, |
| 724 | * 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] | 725 | * a valid result. This works for headers defined as comma-separated lists. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 726 | */ |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 727 | char *find_hdr_value_end(char *s, const char *e) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 728 | { |
| 729 | int quoted, qdpair; |
| 730 | |
| 731 | quoted = qdpair = 0; |
Willy Tarreau | e6d9c21 | 2016-11-05 18:23:38 +0100 | [diff] [blame] | 732 | |
| 733 | #if defined(__x86_64__) || \ |
| 734 | defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) || \ |
| 735 | defined(__ARM_ARCH_7A__) |
| 736 | /* speedup: skip everything not a comma nor a double quote */ |
| 737 | for (; s <= e - sizeof(int); s += sizeof(int)) { |
| 738 | unsigned int c = *(int *)s; // comma |
| 739 | unsigned int q = c; // quote |
| 740 | |
| 741 | c ^= 0x2c2c2c2c; // contains one zero on a comma |
| 742 | q ^= 0x22222222; // contains one zero on a quote |
| 743 | |
| 744 | c = (c - 0x01010101) & ~c; // contains 0x80 below a comma |
| 745 | q = (q - 0x01010101) & ~q; // contains 0x80 below a quote |
| 746 | |
| 747 | if ((c | q) & 0x80808080) |
| 748 | break; // found a comma or a quote |
| 749 | } |
| 750 | #endif |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 751 | for (; s < e; s++) { |
| 752 | if (qdpair) qdpair = 0; |
Willy Tarreau | 0f7f51f | 2010-08-30 11:06:34 +0200 | [diff] [blame] | 753 | else if (quoted) { |
| 754 | if (*s == '\\') qdpair = 1; |
| 755 | else if (*s == '"') quoted = 0; |
| 756 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 757 | else if (*s == '"') quoted = 1; |
| 758 | else if (*s == ',') return s; |
| 759 | } |
| 760 | return s; |
| 761 | } |
| 762 | |
| 763 | /* Find the first or next occurrence of header <name> in message buffer <sol> |
| 764 | * using headers index <idx>, and return it in the <ctx> structure. This |
| 765 | * structure holds everything necessary to use the header and find next |
| 766 | * occurrence. If its <idx> member is 0, the header is searched from the |
| 767 | * beginning. Otherwise, the next occurrence is returned. The function returns |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 768 | * 1 when it finds a value, and 0 when there is no more. It is designed to work |
| 769 | * with headers defined as comma-separated lists. As a special case, if ctx->val |
| 770 | * is NULL when searching for a new values of a header, the current header is |
| 771 | * rescanned. This allows rescanning after a header deletion. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 772 | */ |
| 773 | int http_find_header2(const char *name, int len, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 774 | char *sol, struct hdr_idx *idx, |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 775 | struct hdr_ctx *ctx) |
| 776 | { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 777 | char *eol, *sov; |
| 778 | int cur_idx, old_idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 779 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 780 | cur_idx = ctx->idx; |
| 781 | if (cur_idx) { |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 782 | /* We have previously returned a value, let's search |
| 783 | * another one on the same line. |
| 784 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 785 | sol = ctx->line; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 786 | ctx->del = ctx->val + ctx->vlen + ctx->tws; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 787 | sov = sol + ctx->del; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 788 | eol = sol + idx->v[cur_idx].len; |
| 789 | |
| 790 | if (sov >= eol) |
| 791 | /* no more values in this header */ |
| 792 | goto next_hdr; |
| 793 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 794 | /* values remaining for this header, skip the comma but save it |
| 795 | * for later use (eg: for header deletion). |
| 796 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 797 | sov++; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 798 | while (sov < eol && HTTP_IS_LWS((*sov))) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 799 | sov++; |
| 800 | |
| 801 | goto return_hdr; |
| 802 | } |
| 803 | |
| 804 | /* first request for this header */ |
| 805 | sol += hdr_idx_first_pos(idx); |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 806 | old_idx = 0; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 807 | cur_idx = hdr_idx_first_idx(idx); |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 808 | while (cur_idx) { |
| 809 | eol = sol + idx->v[cur_idx].len; |
| 810 | |
Willy Tarreau | 1ad7c6d | 2007-06-10 21:42:55 +0200 | [diff] [blame] | 811 | if (len == 0) { |
| 812 | /* No argument was passed, we want any header. |
| 813 | * To achieve this, we simply build a fake request. */ |
| 814 | while (sol + len < eol && sol[len] != ':') |
| 815 | len++; |
| 816 | name = sol; |
| 817 | } |
| 818 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 819 | if ((len < eol - sol) && |
| 820 | (sol[len] == ':') && |
| 821 | (strncasecmp(sol, name, len) == 0)) { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 822 | ctx->del = len; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 823 | sov = sol + len + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 824 | while (sov < eol && HTTP_IS_LWS(*sov)) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 825 | sov++; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 826 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 827 | ctx->line = sol; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 828 | ctx->prev = old_idx; |
| 829 | return_hdr: |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 830 | ctx->idx = cur_idx; |
| 831 | ctx->val = sov - sol; |
| 832 | |
| 833 | eol = find_hdr_value_end(sov, eol); |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 834 | ctx->tws = 0; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 835 | while (eol > sov && HTTP_IS_LWS(*(eol - 1))) { |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 836 | eol--; |
| 837 | ctx->tws++; |
| 838 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 839 | ctx->vlen = eol - sov; |
| 840 | return 1; |
| 841 | } |
| 842 | next_hdr: |
| 843 | sol = eol + idx->v[cur_idx].cr + 1; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 844 | old_idx = cur_idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 845 | cur_idx = idx->v[cur_idx].next; |
| 846 | } |
| 847 | return 0; |
| 848 | } |
| 849 | |
| 850 | int http_find_header(const char *name, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 851 | char *sol, struct hdr_idx *idx, |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 852 | struct hdr_ctx *ctx) |
| 853 | { |
| 854 | return http_find_header2(name, strlen(name), sol, idx, ctx); |
| 855 | } |
| 856 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 857 | /* Remove one value of a header. This only works on a <ctx> returned by one of |
| 858 | * the http_find_header functions. The value is removed, as well as surrounding |
| 859 | * 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] | 860 | * 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] | 861 | * message <msg>. The new index is returned. If it is zero, it means there is |
| 862 | * no more header, so any processing may stop. The ctx is always left in a form |
| 863 | * that can be handled by http_find_header2() to find next occurrence. |
| 864 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 865 | 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] | 866 | { |
| 867 | int cur_idx = ctx->idx; |
| 868 | char *sol = ctx->line; |
| 869 | struct hdr_idx_elem *hdr; |
| 870 | int delta, skip_comma; |
| 871 | |
| 872 | if (!cur_idx) |
| 873 | return 0; |
| 874 | |
| 875 | hdr = &idx->v[cur_idx]; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 876 | if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 877 | /* 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] | 878 | 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] | 879 | http_msg_move_end(msg, delta); |
| 880 | idx->used--; |
| 881 | hdr->len = 0; /* unused entry */ |
| 882 | idx->v[ctx->prev].next = idx->v[ctx->idx].next; |
Willy Tarreau | 5c4784f | 2011-02-12 13:07:35 +0100 | [diff] [blame] | 883 | if (idx->tail == ctx->idx) |
| 884 | idx->tail = ctx->prev; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 885 | ctx->idx = ctx->prev; /* walk back to the end of previous header */ |
Willy Tarreau | 7c1c217 | 2015-01-07 17:23:50 +0100 | [diff] [blame] | 886 | 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] | 887 | 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] | 888 | ctx->tws = ctx->vlen = 0; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 889 | return ctx->idx; |
| 890 | } |
| 891 | |
| 892 | /* 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] | 893 | * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the |
| 894 | * last entry of the list, we remove the last separator. |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 895 | */ |
| 896 | |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 897 | skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 898 | delta = buffer_replace2(msg->chn->buf, sol + ctx->del + skip_comma, |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 899 | sol + ctx->val + ctx->vlen + ctx->tws + skip_comma, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 900 | NULL, 0); |
| 901 | hdr->len += delta; |
| 902 | http_msg_move_end(msg, delta); |
| 903 | ctx->val = ctx->del; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 904 | ctx->tws = ctx->vlen = 0; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 905 | return ctx->idx; |
| 906 | } |
| 907 | |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 908 | /* This function handles a server error at the stream interface level. The |
| 909 | * 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] | 910 | * message is copied into the input buffer. |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 911 | * 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] | 912 | * in this buffer will be lost. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 913 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 914 | static void http_server_error(struct stream *s, struct stream_interface *si, |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 915 | int err, int finst, const struct chunk *msg) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 916 | { |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 917 | FLT_STRM_CB(s, flt_http_reply(s, s->txn->status, msg)); |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 918 | channel_auto_read(si_oc(si)); |
| 919 | channel_abort(si_oc(si)); |
| 920 | channel_auto_close(si_oc(si)); |
| 921 | channel_erase(si_oc(si)); |
| 922 | channel_auto_close(si_ic(si)); |
| 923 | channel_auto_read(si_ic(si)); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 924 | if (msg) |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 925 | co_inject(si_ic(si), msg->str, msg->len); |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 926 | if (!(s->flags & SF_ERR_MASK)) |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 927 | s->flags |= err; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 928 | if (!(s->flags & SF_FINST_MASK)) |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 929 | s->flags |= finst; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 930 | } |
| 931 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 932 | /* This function returns the appropriate error location for the given stream |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 933 | * and message. |
| 934 | */ |
| 935 | |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 936 | struct chunk *http_error_message(struct stream *s) |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 937 | { |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 938 | const int msgnum = http_get_status_idx(s->txn->status); |
| 939 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 940 | if (s->be->errmsg[msgnum].str) |
| 941 | return &s->be->errmsg[msgnum]; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 942 | else if (strm_fe(s)->errmsg[msgnum].str) |
| 943 | return &strm_fe(s)->errmsg[msgnum]; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 944 | else |
| 945 | return &http_err_chunks[msgnum]; |
| 946 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 947 | |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 948 | void |
| 949 | http_reply_and_close(struct stream *s, short status, struct chunk *msg) |
| 950 | { |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 951 | s->txn->flags &= ~TX_WAIT_NEXT_RQ; |
Christopher Faulet | 3e34429 | 2015-11-24 16:24:13 +0100 | [diff] [blame] | 952 | FLT_STRM_CB(s, flt_http_reply(s, status, msg)); |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 953 | stream_int_retnclose(&s->si[0], msg); |
| 954 | } |
| 955 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 956 | /* |
Willy Tarreau | b7ce424 | 2015-09-03 17:15:21 +0200 | [diff] [blame] | 957 | * returns a known method among HTTP_METH_* or HTTP_METH_OTHER for all unknown |
| 958 | * ones. |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 959 | */ |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 960 | 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] | 961 | { |
| 962 | unsigned char m; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 963 | const struct http_method_desc *h; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 964 | |
| 965 | m = ((unsigned)*str - 'A'); |
| 966 | |
| 967 | if (m < 26) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 968 | for (h = http_methods[m]; h->len > 0; h++) { |
| 969 | if (unlikely(h->len != len)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 970 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 971 | if (likely(memcmp(str, h->text, h->len) == 0)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 972 | return h->meth; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 973 | }; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 974 | } |
Willy Tarreau | b7ce424 | 2015-09-03 17:15:21 +0200 | [diff] [blame] | 975 | return HTTP_METH_OTHER; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 976 | } |
| 977 | |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 978 | /* Parse the URI from the given transaction (which is assumed to be in request |
| 979 | * phase) and look for the "/" beginning the PATH. If not found, return NULL. |
| 980 | * It is returned otherwise. |
| 981 | */ |
Thierry FOURNIER | 3c33178 | 2015-09-17 19:33:35 +0200 | [diff] [blame] | 982 | char *http_get_path(struct http_txn *txn) |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 983 | { |
| 984 | char *ptr, *end; |
| 985 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 986 | ptr = txn->req.chn->buf->p + txn->req.sl.rq.u; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 987 | end = ptr + txn->req.sl.rq.u_l; |
| 988 | |
| 989 | if (ptr >= end) |
| 990 | return NULL; |
| 991 | |
Lukas Tribus | 2395368 | 2017-04-28 13:24:30 +0000 | [diff] [blame] | 992 | /* RFC7230, par. 2.7 : |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 993 | * Request-URI = "*" | absuri | abspath | authority |
| 994 | */ |
| 995 | |
| 996 | if (*ptr == '*') |
| 997 | return NULL; |
| 998 | |
| 999 | if (isalpha((unsigned char)*ptr)) { |
| 1000 | /* this is a scheme as described by RFC3986, par. 3.1 */ |
| 1001 | ptr++; |
| 1002 | while (ptr < end && |
| 1003 | (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')) |
| 1004 | ptr++; |
| 1005 | /* skip '://' */ |
| 1006 | if (ptr == end || *ptr++ != ':') |
| 1007 | return NULL; |
| 1008 | if (ptr == end || *ptr++ != '/') |
| 1009 | return NULL; |
| 1010 | if (ptr == end || *ptr++ != '/') |
| 1011 | return NULL; |
| 1012 | } |
| 1013 | /* skip [user[:passwd]@]host[:[port]] */ |
| 1014 | |
| 1015 | while (ptr < end && *ptr != '/') |
| 1016 | ptr++; |
| 1017 | |
| 1018 | if (ptr == end) |
| 1019 | return NULL; |
| 1020 | |
| 1021 | /* OK, we got the '/' ! */ |
| 1022 | return ptr; |
| 1023 | } |
| 1024 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 1025 | /* Parse the URI from the given string and look for the "/" beginning the PATH. |
| 1026 | * If not found, return NULL. It is returned otherwise. |
| 1027 | */ |
| 1028 | static char * |
| 1029 | http_get_path_from_string(char *str) |
| 1030 | { |
| 1031 | char *ptr = str; |
| 1032 | |
| 1033 | /* RFC2616, par. 5.1.2 : |
| 1034 | * Request-URI = "*" | absuri | abspath | authority |
| 1035 | */ |
| 1036 | |
| 1037 | if (*ptr == '*') |
| 1038 | return NULL; |
| 1039 | |
| 1040 | if (isalpha((unsigned char)*ptr)) { |
| 1041 | /* this is a scheme as described by RFC3986, par. 3.1 */ |
| 1042 | ptr++; |
| 1043 | while (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.') |
| 1044 | ptr++; |
| 1045 | /* skip '://' */ |
| 1046 | if (*ptr == '\0' || *ptr++ != ':') |
| 1047 | return NULL; |
| 1048 | if (*ptr == '\0' || *ptr++ != '/') |
| 1049 | return NULL; |
| 1050 | if (*ptr == '\0' || *ptr++ != '/') |
| 1051 | return NULL; |
| 1052 | } |
| 1053 | /* skip [user[:passwd]@]host[:[port]] */ |
| 1054 | |
| 1055 | while (*ptr != '\0' && *ptr != ' ' && *ptr != '/') |
| 1056 | ptr++; |
| 1057 | |
| 1058 | if (*ptr == '\0' || *ptr == ' ') |
| 1059 | return NULL; |
| 1060 | |
| 1061 | /* OK, we got the '/' ! */ |
| 1062 | return ptr; |
| 1063 | } |
| 1064 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 1065 | /* Returns a 302 for a redirectable request that reaches a server working in |
| 1066 | * in redirect mode. This may only be called just after the stream interface |
| 1067 | * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will |
| 1068 | * follow normal proxy processing. NOTE: this function is designed to support |
| 1069 | * being called once data are scheduled for forwarding. |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1070 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1071 | void http_perform_server_redirect(struct stream *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1072 | { |
| 1073 | struct http_txn *txn; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 1074 | struct server *srv; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1075 | char *path; |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 1076 | int len, rewind; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1077 | |
| 1078 | /* 1: create the response header */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1079 | trash.len = strlen(HTTP_302); |
| 1080 | memcpy(trash.str, HTTP_302, trash.len); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1081 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 1082 | srv = objt_server(s->target); |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 1083 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1084 | /* 2: add the server's prefix */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1085 | if (trash.len + srv->rdr_len > trash.size) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1086 | return; |
| 1087 | |
Willy Tarreau | dcb75c4 | 2010-01-10 00:24:22 +0100 | [diff] [blame] | 1088 | /* special prefix "/" means don't change URL */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 1089 | if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1090 | memcpy(trash.str + trash.len, srv->rdr_pfx, srv->rdr_len); |
| 1091 | trash.len += srv->rdr_len; |
Willy Tarreau | dcb75c4 | 2010-01-10 00:24:22 +0100 | [diff] [blame] | 1092 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1093 | |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 1094 | /* 3: add the request URI. Since it was already forwarded, we need |
| 1095 | * to temporarily rewind the buffer. |
| 1096 | */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 1097 | txn = s->txn; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1098 | b_rew(s->req.buf, rewind = http_hdr_rewind(&txn->req)); |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 1099 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1100 | path = http_get_path(txn); |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1101 | 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] | 1102 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1103 | b_adv(s->req.buf, rewind); |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 1104 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1105 | if (!path) |
| 1106 | return; |
| 1107 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1108 | if (trash.len + len > trash.size - 4) /* 4 for CRLF-CRLF */ |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1109 | return; |
| 1110 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1111 | memcpy(trash.str + trash.len, path, len); |
| 1112 | trash.len += len; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1113 | |
| 1114 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1115 | memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29); |
| 1116 | trash.len += 29; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1117 | } else { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1118 | memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23); |
| 1119 | trash.len += 23; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1120 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1121 | |
| 1122 | /* prepare to return without error. */ |
Willy Tarreau | 73b013b | 2012-05-21 16:31:45 +0200 | [diff] [blame] | 1123 | si_shutr(si); |
| 1124 | si_shutw(si); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1125 | si->err_type = SI_ET_NONE; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1126 | si->state = SI_ST_CLO; |
| 1127 | |
| 1128 | /* send the message */ |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 1129 | txn->status = 302; |
| 1130 | http_server_error(s, si, SF_ERR_LOCAL, SF_FINST_C, &trash); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1131 | |
| 1132 | /* 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] | 1133 | srv_inc_sess_ctr(srv); |
Bhaskar Maddala | a20cb85 | 2014-02-03 16:26:46 -0500 | [diff] [blame] | 1134 | srv_set_sess_last(srv); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1135 | } |
| 1136 | |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 1137 | /* Return the error message corresponding to si->err_type. It is assumed |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1138 | * that the server side is closed. Note that err_type is actually a |
| 1139 | * bitmask, where almost only aborts may be cumulated with other |
| 1140 | * values. We consider that aborted operations are more important |
| 1141 | * than timeouts or errors due to the fact that nobody else in the |
| 1142 | * logs might explain incomplete retries. All others should avoid |
| 1143 | * being cumulated. It should normally not be possible to have multiple |
| 1144 | * 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] | 1145 | * Note that connection errors appearing on the second request of a keep-alive |
| 1146 | * connection are not reported since this allows the client to retry. |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1147 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1148 | void http_return_srv_error(struct stream *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1149 | { |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 1150 | int err_type = si->err_type; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1151 | |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 1152 | /* set s->txn->status for http_error_message(s) */ |
| 1153 | s->txn->status = 503; |
| 1154 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1155 | if (err_type & SI_ET_QUEUE_ABRT) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1156 | http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q, |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 1157 | http_error_message(s)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1158 | else if (err_type & SI_ET_CONN_ABRT) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1159 | http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C, |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 1160 | (s->txn->flags & TX_NOT_FIRST) ? NULL : |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 1161 | http_error_message(s)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1162 | else if (err_type & SI_ET_QUEUE_TO) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1163 | http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q, |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 1164 | http_error_message(s)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1165 | else if (err_type & SI_ET_QUEUE_ERR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1166 | http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q, |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 1167 | http_error_message(s)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1168 | else if (err_type & SI_ET_CONN_TO) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1169 | http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C, |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 1170 | (s->txn->flags & TX_NOT_FIRST) ? NULL : |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 1171 | http_error_message(s)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1172 | else if (err_type & SI_ET_CONN_ERR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1173 | http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C, |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 1174 | (s->flags & SF_SRV_REUSED) ? NULL : |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 1175 | http_error_message(s)); |
Willy Tarreau | 2d400bb | 2012-05-14 12:11:47 +0200 | [diff] [blame] | 1176 | else if (err_type & SI_ET_CONN_RES) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1177 | http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C, |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 1178 | (s->txn->flags & TX_NOT_FIRST) ? NULL : |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 1179 | http_error_message(s)); |
| 1180 | else { /* SI_ET_CONN_OTHER and others */ |
| 1181 | s->txn->status = 500; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1182 | http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C, |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 1183 | http_error_message(s)); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 1184 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1185 | } |
| 1186 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1187 | extern const char sess_term_cond[8]; |
| 1188 | extern const char sess_fin_state[8]; |
| 1189 | extern const char *monthname[12]; |
Willy Tarreau | 63986c7 | 2015-04-03 22:55:33 +0200 | [diff] [blame] | 1190 | struct pool_head *pool2_http_txn; |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 1191 | struct pool_head *pool2_requri; |
Willy Tarreau | 193b8c6 | 2012-11-22 00:17:38 +0100 | [diff] [blame] | 1192 | struct pool_head *pool2_capture = NULL; |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 1193 | struct pool_head *pool2_uniqueid; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1194 | |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1195 | /* |
| 1196 | * Capture headers from message starting at <som> according to header list |
Willy Tarreau | 54da8db | 2014-06-13 16:11:48 +0200 | [diff] [blame] | 1197 | * <cap_hdr>, and fill the <cap> pointers appropriately. |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1198 | */ |
| 1199 | void capture_headers(char *som, struct hdr_idx *idx, |
| 1200 | char **cap, struct cap_hdr *cap_hdr) |
| 1201 | { |
| 1202 | char *eol, *sol, *col, *sov; |
| 1203 | int cur_idx; |
| 1204 | struct cap_hdr *h; |
| 1205 | int len; |
| 1206 | |
| 1207 | sol = som + hdr_idx_first_pos(idx); |
| 1208 | cur_idx = hdr_idx_first_idx(idx); |
| 1209 | |
| 1210 | while (cur_idx) { |
| 1211 | eol = sol + idx->v[cur_idx].len; |
| 1212 | |
| 1213 | col = sol; |
| 1214 | while (col < eol && *col != ':') |
| 1215 | col++; |
| 1216 | |
| 1217 | sov = col + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 1218 | while (sov < eol && HTTP_IS_LWS(*sov)) |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1219 | sov++; |
| 1220 | |
| 1221 | for (h = cap_hdr; h; h = h->next) { |
Willy Tarreau | 54da8db | 2014-06-13 16:11:48 +0200 | [diff] [blame] | 1222 | if (h->namelen && (h->namelen == col - sol) && |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1223 | (strncasecmp(sol, h->name, h->namelen) == 0)) { |
| 1224 | if (cap[h->index] == NULL) |
| 1225 | cap[h->index] = |
Willy Tarreau | cf7f320 | 2007-05-13 22:46:04 +0200 | [diff] [blame] | 1226 | pool_alloc2(h->pool); |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1227 | |
| 1228 | if (cap[h->index] == NULL) { |
| 1229 | Alert("HTTP capture : out of memory.\n"); |
| 1230 | continue; |
| 1231 | } |
| 1232 | |
| 1233 | len = eol - sov; |
| 1234 | if (len > h->len) |
| 1235 | len = h->len; |
| 1236 | |
| 1237 | memcpy(cap[h->index], sov, len); |
| 1238 | cap[h->index][len]=0; |
| 1239 | } |
| 1240 | } |
| 1241 | sol = eol + idx->v[cur_idx].cr + 1; |
| 1242 | cur_idx = idx->v[cur_idx].next; |
| 1243 | } |
| 1244 | } |
| 1245 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1246 | /* |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1247 | * Returns the data from Authorization header. Function may be called more |
| 1248 | * than once so data is stored in txn->auth_data. When no header is found |
| 1249 | * 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] | 1250 | * searching again for something we are unable to find anyway. However, if |
| 1251 | * 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] | 1252 | * have the credentials overwritten by another stream in parallel. |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1253 | */ |
| 1254 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1255 | int |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1256 | get_http_auth(struct stream *s) |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1257 | { |
| 1258 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 1259 | struct http_txn *txn = s->txn; |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1260 | struct chunk auth_method; |
| 1261 | struct hdr_ctx ctx; |
| 1262 | char *h, *p; |
| 1263 | int len; |
| 1264 | |
| 1265 | #ifdef DEBUG_AUTH |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1266 | printf("Auth for stream %p: %d\n", s, txn->auth.method); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1267 | #endif |
| 1268 | |
| 1269 | if (txn->auth.method == HTTP_AUTH_WRONG) |
| 1270 | return 0; |
| 1271 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1272 | txn->auth.method = HTTP_AUTH_WRONG; |
| 1273 | |
| 1274 | ctx.idx = 0; |
Willy Tarreau | 844a7e7 | 2010-01-31 21:46:18 +0100 | [diff] [blame] | 1275 | |
| 1276 | if (txn->flags & TX_USE_PX_CONN) { |
| 1277 | h = "Proxy-Authorization"; |
| 1278 | len = strlen(h); |
| 1279 | } else { |
| 1280 | h = "Authorization"; |
| 1281 | len = strlen(h); |
| 1282 | } |
| 1283 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1284 | 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] | 1285 | return 0; |
| 1286 | |
| 1287 | h = ctx.line + ctx.val; |
| 1288 | |
| 1289 | p = memchr(h, ' ', ctx.vlen); |
Willy Tarreau | 5c557d1 | 2016-03-13 08:17:02 +0100 | [diff] [blame] | 1290 | len = p - h; |
| 1291 | if (!p || len <= 0) |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1292 | return 0; |
| 1293 | |
David Carlier | 7365f7d | 2016-04-04 11:54:42 +0100 | [diff] [blame] | 1294 | if (chunk_initlen(&auth_method, h, 0, len) != 1) |
| 1295 | return 0; |
| 1296 | |
Willy Tarreau | 5c557d1 | 2016-03-13 08:17:02 +0100 | [diff] [blame] | 1297 | 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] | 1298 | |
| 1299 | if (!strncasecmp("Basic", auth_method.str, auth_method.len)) { |
Christopher Faulet | 6988f67 | 2017-07-27 15:18:52 +0200 | [diff] [blame] | 1300 | struct chunk *http_auth = get_trash_chunk(); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1301 | |
| 1302 | len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len, |
Christopher Faulet | 6988f67 | 2017-07-27 15:18:52 +0200 | [diff] [blame] | 1303 | http_auth->str, global.tune.bufsize - 1); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1304 | |
| 1305 | if (len < 0) |
| 1306 | return 0; |
| 1307 | |
| 1308 | |
Christopher Faulet | 6988f67 | 2017-07-27 15:18:52 +0200 | [diff] [blame] | 1309 | http_auth->str[len] = '\0'; |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1310 | |
Christopher Faulet | 6988f67 | 2017-07-27 15:18:52 +0200 | [diff] [blame] | 1311 | p = strchr(http_auth->str, ':'); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1312 | |
| 1313 | if (!p) |
| 1314 | return 0; |
| 1315 | |
Christopher Faulet | 6988f67 | 2017-07-27 15:18:52 +0200 | [diff] [blame] | 1316 | txn->auth.user = http_auth->str; |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1317 | *p = '\0'; |
| 1318 | txn->auth.pass = p+1; |
| 1319 | |
| 1320 | txn->auth.method = HTTP_AUTH_BASIC; |
| 1321 | return 1; |
| 1322 | } |
| 1323 | |
| 1324 | return 0; |
| 1325 | } |
| 1326 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1327 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1328 | /* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the |
| 1329 | * conversion succeeded, 0 in case of error. If the request was already 1.X, |
| 1330 | * nothing is done and 1 is returned. |
| 1331 | */ |
Willy Tarreau | 418bfcc | 2012-03-09 13:56:20 +0100 | [diff] [blame] | 1332 | static int http_upgrade_v09_to_v10(struct http_txn *txn) |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1333 | { |
| 1334 | int delta; |
| 1335 | char *cur_end; |
Willy Tarreau | 418bfcc | 2012-03-09 13:56:20 +0100 | [diff] [blame] | 1336 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1337 | |
| 1338 | if (msg->sl.rq.v_l != 0) |
| 1339 | return 1; |
| 1340 | |
Apollon Oikonomopoulos | 25a1522 | 2014-04-06 02:46:00 +0300 | [diff] [blame] | 1341 | /* RFC 1945 allows only GET for HTTP/0.9 requests */ |
| 1342 | if (txn->meth != HTTP_METH_GET) |
| 1343 | return 0; |
| 1344 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1345 | cur_end = msg->chn->buf->p + msg->sl.rq.l; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1346 | |
| 1347 | if (msg->sl.rq.u_l == 0) { |
Apollon Oikonomopoulos | 25a1522 | 2014-04-06 02:46:00 +0300 | [diff] [blame] | 1348 | /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */ |
| 1349 | return 0; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1350 | } |
| 1351 | /* add HTTP version */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1352 | 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] | 1353 | http_msg_move_end(msg, delta); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1354 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1355 | cur_end = (char *)http_parse_reqline(msg, |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1356 | HTTP_MSG_RQMETH, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1357 | msg->chn->buf->p, cur_end + 1, |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1358 | NULL, NULL); |
| 1359 | if (unlikely(!cur_end)) |
| 1360 | return 0; |
| 1361 | |
| 1362 | /* we have a full HTTP/1.0 request now and we know that |
| 1363 | * we have either a CR or an LF at <ptr>. |
| 1364 | */ |
| 1365 | hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r'); |
| 1366 | return 1; |
| 1367 | } |
| 1368 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1369 | /* Parse the Connection: header of an HTTP request, looking for both "close" |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1370 | * and "keep-alive" values. If we already know that some headers may safely |
| 1371 | * 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] | 1372 | * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses) |
| 1373 | * - 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] | 1374 | * Presence of the "Upgrade" token is also checked and reported. |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1375 | * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was |
| 1376 | * found, and TX_CON_*_SET is adjusted depending on what is left so only |
| 1377 | * harmless combinations may be removed. Do not call that after changes have |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1378 | * been processed. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1379 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1380 | 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] | 1381 | { |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1382 | struct hdr_ctx ctx; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1383 | const char *hdr_val = "Connection"; |
| 1384 | int hdr_len = 10; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1385 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1386 | if (txn->flags & TX_HDR_CONN_PRS) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1387 | return; |
| 1388 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1389 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1390 | hdr_val = "Proxy-Connection"; |
| 1391 | hdr_len = 16; |
| 1392 | } |
| 1393 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1394 | ctx.idx = 0; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1395 | txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1396 | 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] | 1397 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 1398 | txn->flags |= TX_HDR_CONN_KAL; |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1399 | if (to_del & 2) |
| 1400 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1401 | else |
| 1402 | txn->flags |= TX_CON_KAL_SET; |
| 1403 | } |
| 1404 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 1405 | txn->flags |= TX_HDR_CONN_CLO; |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1406 | if (to_del & 1) |
| 1407 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1408 | else |
| 1409 | txn->flags |= TX_CON_CLO_SET; |
| 1410 | } |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 1411 | else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) { |
| 1412 | txn->flags |= TX_HDR_CONN_UPG; |
| 1413 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1414 | } |
| 1415 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1416 | txn->flags |= TX_HDR_CONN_PRS; |
| 1417 | return; |
| 1418 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1419 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1420 | /* Apply desired changes on the Connection: header. Values may be removed and/or |
| 1421 | * added depending on the <wanted> flags, which are exclusively composed of |
| 1422 | * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The |
| 1423 | * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left. |
| 1424 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1425 | 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] | 1426 | { |
| 1427 | struct hdr_ctx ctx; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1428 | const char *hdr_val = "Connection"; |
| 1429 | int hdr_len = 10; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1430 | |
| 1431 | ctx.idx = 0; |
| 1432 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1433 | |
| 1434 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1435 | hdr_val = "Proxy-Connection"; |
| 1436 | hdr_len = 16; |
| 1437 | } |
| 1438 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1439 | txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1440 | 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] | 1441 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 1442 | if (wanted & TX_CON_KAL_SET) |
| 1443 | txn->flags |= TX_CON_KAL_SET; |
| 1444 | else |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1445 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1446 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1447 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 1448 | if (wanted & TX_CON_CLO_SET) |
| 1449 | txn->flags |= TX_CON_CLO_SET; |
| 1450 | else |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1451 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 1452 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1453 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1454 | |
| 1455 | if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 1456 | return; |
| 1457 | |
| 1458 | if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) { |
| 1459 | txn->flags |= TX_CON_CLO_SET; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1460 | hdr_val = "Connection: close"; |
| 1461 | hdr_len = 17; |
| 1462 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1463 | hdr_val = "Proxy-Connection: close"; |
| 1464 | hdr_len = 23; |
| 1465 | } |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1466 | http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1467 | } |
| 1468 | |
| 1469 | if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) { |
| 1470 | txn->flags |= TX_CON_KAL_SET; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1471 | hdr_val = "Connection: keep-alive"; |
| 1472 | hdr_len = 22; |
| 1473 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1474 | hdr_val = "Proxy-Connection: keep-alive"; |
| 1475 | hdr_len = 28; |
| 1476 | } |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1477 | http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1478 | } |
| 1479 | return; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1480 | } |
| 1481 | |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 1482 | /* Parses a qvalue and returns it multipled by 1000, from 0 to 1000. If the |
| 1483 | * value is larger than 1000, it is bound to 1000. The parser consumes up to |
| 1484 | * 1 digit, one dot and 3 digits and stops on the first invalid character. |
| 1485 | * Unparsable qvalues return 1000 as "q=1.000". |
| 1486 | */ |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 1487 | int parse_qvalue(const char *qvalue, const char **end) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 1488 | { |
| 1489 | int q = 1000; |
| 1490 | |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 1491 | if (!isdigit((unsigned char)*qvalue)) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 1492 | goto out; |
| 1493 | q = (*qvalue++ - '0') * 1000; |
| 1494 | |
| 1495 | if (*qvalue++ != '.') |
| 1496 | goto out; |
| 1497 | |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 1498 | if (!isdigit((unsigned char)*qvalue)) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 1499 | goto out; |
| 1500 | q += (*qvalue++ - '0') * 100; |
| 1501 | |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 1502 | if (!isdigit((unsigned char)*qvalue)) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 1503 | goto out; |
| 1504 | q += (*qvalue++ - '0') * 10; |
| 1505 | |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 1506 | if (!isdigit((unsigned char)*qvalue)) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 1507 | goto out; |
| 1508 | q += (*qvalue++ - '0') * 1; |
| 1509 | out: |
| 1510 | if (q > 1000) |
| 1511 | q = 1000; |
Willy Tarreau | 38b3aa5 | 2014-04-22 23:32:05 +0200 | [diff] [blame] | 1512 | if (end) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 1513 | *end = qvalue; |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 1514 | return q; |
| 1515 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 1516 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1517 | 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] | 1518 | { |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 1519 | struct proxy *fe = strm_fe(s); |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 1520 | int tmp = TX_CON_WANT_KAL; |
| 1521 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1522 | if (!((fe->options2|s->be->options2) & PR_O2_FAKE_KA)) { |
| 1523 | if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN || |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 1524 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN) |
| 1525 | tmp = TX_CON_WANT_TUN; |
| 1526 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1527 | if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 1528 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL) |
| 1529 | tmp = TX_CON_WANT_TUN; |
| 1530 | } |
| 1531 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1532 | if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL || |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 1533 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) { |
| 1534 | /* option httpclose + server_close => forceclose */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1535 | if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 1536 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL) |
| 1537 | tmp = TX_CON_WANT_CLO; |
| 1538 | else |
| 1539 | tmp = TX_CON_WANT_SCL; |
| 1540 | } |
| 1541 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1542 | if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL || |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 1543 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL) |
| 1544 | tmp = TX_CON_WANT_CLO; |
| 1545 | |
| 1546 | if ((txn->flags & TX_CON_WANT_MSK) < tmp) |
| 1547 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp; |
| 1548 | |
| 1549 | if (!(txn->flags & TX_HDR_CONN_PRS) && |
| 1550 | (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) { |
| 1551 | /* parse the Connection header and possibly clean it */ |
| 1552 | int to_del = 0; |
| 1553 | if ((msg->flags & HTTP_MSGF_VER_11) || |
| 1554 | ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1555 | !((fe->options2|s->be->options2) & PR_O2_FAKE_KA))) |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 1556 | to_del |= 2; /* remove "keep-alive" */ |
| 1557 | if (!(msg->flags & HTTP_MSGF_VER_11)) |
| 1558 | to_del |= 1; /* remove "close" */ |
| 1559 | http_parse_connection_header(txn, msg, to_del); |
| 1560 | } |
| 1561 | |
| 1562 | /* check if client or config asks for explicit close in KAL/SCL */ |
| 1563 | if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 1564 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) && |
| 1565 | ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */ |
| 1566 | (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */ |
| 1567 | !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1568 | fe->state == PR_STSTOPPED)) /* frontend is stopping */ |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 1569 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 1570 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 1571 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 1572 | /* This stream analyser waits for a complete HTTP request. It returns 1 if the |
| 1573 | * processing can continue on next analysers, or zero if it either needs more |
| 1574 | * data or wants to immediately abort the request (eg: timeout, error, ...). It |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1575 | * 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] | 1576 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 1577 | * abort. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1578 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1579 | 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] | 1580 | { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1581 | /* |
| 1582 | * We will parse the partial (or complete) lines. |
| 1583 | * We will check the request syntax, and also join multi-line |
| 1584 | * headers. An index of all the lines will be elaborated while |
| 1585 | * parsing. |
| 1586 | * |
| 1587 | * For the parsing, we use a 28 states FSM. |
| 1588 | * |
| 1589 | * Here is the information we currently have : |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1590 | * req->buf->p = beginning of request |
| 1591 | * req->buf->p + msg->eoh = end of processed headers / start of current one |
| 1592 | * req->buf->p + req->buf->i = end of input data |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 1593 | * msg->eol = end of current header or line (LF or CRLF) |
| 1594 | * msg->next = first non-visited byte |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 1595 | * |
| 1596 | * 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] | 1597 | * we will set a few fields (txn->meth, sn->flags/SF_REDIRECTABLE). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 1598 | * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and |
| 1599 | * finally headers capture. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1600 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1601 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1602 | int cur_idx; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 1603 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 1604 | struct http_txn *txn = s->txn; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1605 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 1606 | struct hdr_ctx ctx; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1607 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1608 | 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] | 1609 | now_ms, __FUNCTION__, |
| 1610 | s, |
| 1611 | req, |
| 1612 | req->rex, req->wex, |
| 1613 | req->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1614 | req->buf->i, |
Willy Tarreau | 6bf1736 | 2009-02-24 10:48:35 +0100 | [diff] [blame] | 1615 | req->analysers); |
| 1616 | |
Willy Tarreau | 52a0c60 | 2009-08-16 22:45:38 +0200 | [diff] [blame] | 1617 | /* we're speaking HTTP here, so let's speak HTTP to the client */ |
| 1618 | s->srv_error = http_return_srv_error; |
| 1619 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 1620 | /* There's a protected area at the end of the buffer for rewriting |
| 1621 | * purposes. We don't want to start to parse the request if the |
| 1622 | * protected area is affected, because we may have to move processed |
| 1623 | * data later, which is much more complicated. |
| 1624 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1625 | 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] | 1626 | |
| 1627 | /* This point is executed when some data is avalaible for analysis, |
| 1628 | * so we log the end of the idle time. */ |
| 1629 | if (s->logs.t_idle == -1) |
| 1630 | s->logs.t_idle = tv_ms_elapsed(&s->logs.tv_accept, &now) - s->logs.t_handshake; |
| 1631 | |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 1632 | if (txn->flags & TX_NOT_FIRST) { |
Willy Tarreau | ba0902e | 2015-01-13 14:39:16 +0100 | [diff] [blame] | 1633 | if (unlikely(!channel_is_rewritable(req))) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 1634 | 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] | 1635 | goto failed_keep_alive; |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 1636 | /* 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] | 1637 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 1638 | req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | d7ad9f5 | 2013-12-31 17:26:25 +0100 | [diff] [blame] | 1639 | req->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 1640 | return 0; |
| 1641 | } |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 1642 | if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) || |
| 1643 | bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite)) |
| 1644 | buffer_slow_realign(req->buf); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 1645 | } |
| 1646 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1647 | if (likely(msg->next < req->buf->i)) /* some unparsed data are available */ |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 1648 | http_msg_analyzer(msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 1649 | } |
| 1650 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1651 | /* 1: we might have to print this header in debug mode */ |
| 1652 | if (unlikely((global.mode & MODE_DEBUG) && |
| 1653 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | 7d59e90 | 2014-10-21 19:36:09 +0200 | [diff] [blame] | 1654 | msg->msg_state >= HTTP_MSG_BODY)) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1655 | char *eol, *sol; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1656 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1657 | sol = req->buf->p; |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 1658 | /* this is a bit complex : in case of error on the request line, |
| 1659 | * we know that rq.l is still zero, so we display only the part |
| 1660 | * up to the end of the line (truncated by debug_hdr). |
| 1661 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1662 | 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] | 1663 | debug_hdr("clireq", s, sol, eol); |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 1664 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1665 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 1666 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1667 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1668 | while (cur_idx) { |
| 1669 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 1670 | debug_hdr("clihdr", s, sol, eol); |
| 1671 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 1672 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1673 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1674 | } |
| 1675 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1676 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1677 | /* |
| 1678 | * Now we quickly check if we have found a full valid request. |
| 1679 | * If not so, we check the FD and buffer states before leaving. |
| 1680 | * A full request is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 1681 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 1682 | * requests are checked first. When waiting for a second request |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1683 | * on a keep-alive stream, if we encounter and error, close, t/o, |
| 1684 | * 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] | 1685 | * 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] | 1686 | * process_stream() as a frontend error. |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 1687 | * Last, we may increase some tracked counters' http request errors on |
| 1688 | * the cases that are deliberately the client's fault. For instance, |
| 1689 | * a timeout or connection reset is not counted as an error. However |
| 1690 | * a bad request is. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1691 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1692 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 1693 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1694 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1695 | * First, let's catch bad requests. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1696 | */ |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 1697 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1698 | stream_inc_http_req_ctr(s); |
| 1699 | stream_inc_http_err_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1700 | proxy_inc_fe_req_ctr(sess->fe); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1701 | goto return_bad_req; |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 1702 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1703 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1704 | /* 1: Since we are in header mode, if there's no space |
| 1705 | * left for headers, we won't be able to free more |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1706 | * later, so the stream will never terminate. We |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1707 | * must terminate it now. |
| 1708 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1709 | if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1710 | /* FIXME: check if URI is set and return Status |
| 1711 | * 414 Request URI too long instead. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1712 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1713 | stream_inc_http_req_ctr(s); |
| 1714 | stream_inc_http_err_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1715 | proxy_inc_fe_req_ctr(sess->fe); |
Willy Tarreau | fec4d89 | 2011-09-02 20:04:57 +0200 | [diff] [blame] | 1716 | if (msg->err_pos < 0) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1717 | msg->err_pos = req->buf->i; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1718 | goto return_bad_req; |
| 1719 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1720 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1721 | /* 2: have we encountered a read error ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 1722 | else if (req->flags & CF_READ_ERROR) { |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1723 | if (!(s->flags & SF_ERR_MASK)) |
| 1724 | s->flags |= SF_ERR_CLICL; |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 1725 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 1726 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 1727 | goto failed_keep_alive; |
| 1728 | |
Willy Tarreau | 0f228a0 | 2015-05-01 15:37:53 +0200 | [diff] [blame] | 1729 | if (sess->fe->options & PR_O_IGNORE_PRB) |
| 1730 | goto failed_keep_alive; |
| 1731 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1732 | /* we cannot return any message on error */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 1733 | if (msg->err_pos >= 0) { |
Emeric Brun | 8c1aaa2 | 2017-06-15 11:30:06 +0200 | [diff] [blame] | 1734 | 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] | 1735 | stream_inc_http_err_ctr(s); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 1736 | } |
| 1737 | |
Willy Tarreau | dc979f2 | 2012-12-04 10:39:01 +0100 | [diff] [blame] | 1738 | txn->status = 400; |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 1739 | msg->err_state = msg->msg_state; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1740 | msg->msg_state = HTTP_MSG_ERROR; |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 1741 | http_reply_and_close(s, txn->status, NULL); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 1742 | req->analysers &= AN_REQ_FLT_END; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1743 | stream_inc_http_req_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1744 | proxy_inc_fe_req_ctr(sess->fe); |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 1745 | HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 1746 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 1747 | HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 1748 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1749 | if (!(s->flags & SF_FINST_MASK)) |
| 1750 | s->flags |= SF_FINST_R; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1751 | return 0; |
| 1752 | } |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 1753 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1754 | /* 3: has the read timeout expired ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 1755 | 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] | 1756 | if (!(s->flags & SF_ERR_MASK)) |
| 1757 | s->flags |= SF_ERR_CLITO; |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 1758 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 1759 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 1760 | goto failed_keep_alive; |
| 1761 | |
Willy Tarreau | 0f228a0 | 2015-05-01 15:37:53 +0200 | [diff] [blame] | 1762 | if (sess->fe->options & PR_O_IGNORE_PRB) |
| 1763 | goto failed_keep_alive; |
| 1764 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1765 | /* read timeout : give up with an error message. */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 1766 | if (msg->err_pos >= 0) { |
Emeric Brun | 8c1aaa2 | 2017-06-15 11:30:06 +0200 | [diff] [blame] | 1767 | 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] | 1768 | stream_inc_http_err_ctr(s); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 1769 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1770 | txn->status = 408; |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 1771 | msg->err_state = msg->msg_state; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1772 | msg->msg_state = HTTP_MSG_ERROR; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 1773 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 1774 | req->analysers &= AN_REQ_FLT_END; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 1775 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1776 | stream_inc_http_req_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1777 | proxy_inc_fe_req_ctr(sess->fe); |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 1778 | HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 1779 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 1780 | HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 1781 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1782 | if (!(s->flags & SF_FINST_MASK)) |
| 1783 | s->flags |= SF_FINST_R; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1784 | return 0; |
| 1785 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1786 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1787 | /* 4: have we encountered a close ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 1788 | else if (req->flags & CF_SHUTR) { |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1789 | if (!(s->flags & SF_ERR_MASK)) |
| 1790 | s->flags |= SF_ERR_CLICL; |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 1791 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 1792 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 1793 | goto failed_keep_alive; |
| 1794 | |
Willy Tarreau | 0f228a0 | 2015-05-01 15:37:53 +0200 | [diff] [blame] | 1795 | if (sess->fe->options & PR_O_IGNORE_PRB) |
| 1796 | goto failed_keep_alive; |
| 1797 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 1798 | if (msg->err_pos >= 0) |
Emeric Brun | 8c1aaa2 | 2017-06-15 11:30:06 +0200 | [diff] [blame] | 1799 | 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] | 1800 | txn->status = 400; |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 1801 | msg->err_state = msg->msg_state; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1802 | msg->msg_state = HTTP_MSG_ERROR; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 1803 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 1804 | req->analysers &= AN_REQ_FLT_END; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1805 | stream_inc_http_err_ctr(s); |
| 1806 | stream_inc_http_req_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1807 | proxy_inc_fe_req_ctr(sess->fe); |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 1808 | HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 1809 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 1810 | HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 1811 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1812 | if (!(s->flags & SF_FINST_MASK)) |
| 1813 | s->flags |= SF_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 1814 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1815 | } |
| 1816 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 1817 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 1818 | req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1819 | s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 1820 | #ifdef TCP_QUICKACK |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 1821 | if (sess->listener->options & LI_O_NOQUICKACK && req->buf->i && |
| 1822 | objt_conn(sess->origin) && conn_ctrl_ready(__objt_conn(sess->origin))) { |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 1823 | /* We need more data, we have to re-enable quick-ack in case we |
| 1824 | * previously disabled it, otherwise we might cause the client |
| 1825 | * to delay next data. |
| 1826 | */ |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 1827 | 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] | 1828 | } |
| 1829 | #endif |
Willy Tarreau | 1b194fe | 2009-03-21 21:10:04 +0100 | [diff] [blame] | 1830 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 1831 | if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) { |
| 1832 | /* If the client starts to talk, let's fall back to |
| 1833 | * request timeout processing. |
| 1834 | */ |
| 1835 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 1836 | req->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 1837 | } |
| 1838 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1839 | /* just set the request timeout once at the beginning of the request */ |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 1840 | if (!tick_isset(req->analyse_exp)) { |
| 1841 | if ((msg->msg_state == HTTP_MSG_RQBEFORE) && |
| 1842 | (txn->flags & TX_WAIT_NEXT_RQ) && |
| 1843 | tick_isset(s->be->timeout.httpka)) |
| 1844 | req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka); |
| 1845 | else |
| 1846 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
| 1847 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1848 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1849 | /* we're not ready yet */ |
| 1850 | return 0; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 1851 | |
| 1852 | failed_keep_alive: |
| 1853 | /* Here we process low-level errors for keep-alive requests. In |
| 1854 | * short, if the request is not the first one and it experiences |
| 1855 | * a timeout, read error or shutdown, we just silently close so |
| 1856 | * that the client can try again. |
| 1857 | */ |
| 1858 | txn->status = 0; |
| 1859 | msg->msg_state = HTTP_MSG_RQBEFORE; |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 1860 | req->analysers &= AN_REQ_FLT_END; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 1861 | s->logs.logwait = 0; |
Willy Tarreau | abcd514 | 2013-06-11 17:18:02 +0200 | [diff] [blame] | 1862 | s->logs.level = 0; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1863 | s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 1864 | http_reply_and_close(s, txn->status, NULL); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 1865 | return 0; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1866 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1867 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 1868 | /* OK now we have a complete HTTP request with indexed headers. Let's |
| 1869 | * complete the request parsing by setting a few fields we will need |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1870 | * 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] | 1871 | * 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] | 1872 | * 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] | 1873 | * byte after the last LF. msg->sov points to the first byte of data. |
| 1874 | * msg->eol cannot be trusted because it may have been left uninitialized |
| 1875 | * (for instance in the absence of headers). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 1876 | */ |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1877 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1878 | stream_inc_http_req_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1879 | 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] | 1880 | |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 1881 | if (txn->flags & TX_WAIT_NEXT_RQ) { |
| 1882 | /* kill the pending keep-alive timeout */ |
| 1883 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
| 1884 | req->analyse_exp = TICK_ETERNITY; |
| 1885 | } |
| 1886 | |
| 1887 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 1888 | /* Maybe we found in invalid header name while we were configured not |
| 1889 | * to block on that, so we have to capture it now. |
| 1890 | */ |
| 1891 | if (unlikely(msg->err_pos >= 0)) |
Emeric Brun | 8c1aaa2 | 2017-06-15 11:30:06 +0200 | [diff] [blame] | 1892 | 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] | 1893 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1894 | /* |
| 1895 | * 1: identify the method |
| 1896 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1897 | 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] | 1898 | |
| 1899 | /* we can make use of server redirect on GET and HEAD */ |
| 1900 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1901 | s->flags |= SF_REDIRECTABLE; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 1902 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1903 | /* |
| 1904 | * 2: check if the URI matches the monitor_uri. |
| 1905 | * We have to do this for every request which gets in, because |
| 1906 | * the monitor-uri is defined by the frontend. |
| 1907 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1908 | if (unlikely((sess->fe->monitor_uri_len != 0) && |
| 1909 | (sess->fe->monitor_uri_len == msg->sl.rq.u_l) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1910 | !memcmp(req->buf->p + msg->sl.rq.u, |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1911 | sess->fe->monitor_uri, |
| 1912 | sess->fe->monitor_uri_len))) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1913 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1914 | * We have found the monitor URI |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1915 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1916 | struct acl_cond *cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 1917 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1918 | s->flags |= SF_MONITOR; |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 1919 | HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1); |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 1920 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1921 | /* Check if we want to fail this monitor request or not */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1922 | list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 1923 | 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] | 1924 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1925 | ret = acl_pass(ret); |
| 1926 | if (cond->pol == ACL_COND_UNLESS) |
| 1927 | ret = !ret; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 1928 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1929 | if (ret) { |
| 1930 | /* we fail this request, let's return 503 service unavail */ |
| 1931 | txn->status = 503; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 1932 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1933 | if (!(s->flags & SF_ERR_MASK)) |
| 1934 | 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] | 1935 | goto return_prx_cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 1936 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1937 | } |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 1938 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1939 | /* nothing to fail, let's reply normaly */ |
| 1940 | txn->status = 200; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 1941 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1942 | if (!(s->flags & SF_ERR_MASK)) |
| 1943 | 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] | 1944 | goto return_prx_cond; |
| 1945 | } |
| 1946 | |
| 1947 | /* |
| 1948 | * 3: Maybe we have to copy the original REQURI for the logs ? |
| 1949 | * Note: we cannot log anymore if the request has been |
| 1950 | * classified as invalid. |
| 1951 | */ |
| 1952 | if (unlikely(s->logs.logwait & LW_REQ)) { |
| 1953 | /* we have a complete HTTP request that we must log */ |
| 1954 | if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) { |
| 1955 | int urilen = msg->sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1956 | |
Stéphane Cottin | 23e9e93 | 2017-05-18 08:58:41 +0200 | [diff] [blame] | 1957 | if (urilen >= global.tune.requri_len ) |
| 1958 | urilen = global.tune.requri_len - 1; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1959 | memcpy(txn->uri, req->buf->p, urilen); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1960 | txn->uri[urilen] = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1961 | |
Willy Tarreau | d79a3b2 | 2012-12-28 09:40:16 +0100 | [diff] [blame] | 1962 | if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT))) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1963 | s->do_log(s); |
| 1964 | } else { |
| 1965 | Alert("HTTP logging : out of memory.\n"); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1966 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1967 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1968 | |
Willy Tarreau | 91852eb | 2015-05-01 13:26:00 +0200 | [diff] [blame] | 1969 | /* RFC7230#2.6 has enforced the format of the HTTP version string to be |
| 1970 | * exactly one digit "." one digit. This check may be disabled using |
| 1971 | * option accept-invalid-http-request. |
| 1972 | */ |
| 1973 | if (!(sess->fe->options2 & PR_O2_REQBUG_OK)) { |
| 1974 | if (msg->sl.rq.v_l != 8) { |
| 1975 | msg->err_pos = msg->sl.rq.v; |
| 1976 | goto return_bad_req; |
| 1977 | } |
| 1978 | |
| 1979 | if (req->buf->p[msg->sl.rq.v + 4] != '/' || |
| 1980 | !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 5]) || |
| 1981 | req->buf->p[msg->sl.rq.v + 6] != '.' || |
| 1982 | !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 7])) { |
| 1983 | msg->err_pos = msg->sl.rq.v + 4; |
| 1984 | goto return_bad_req; |
| 1985 | } |
| 1986 | } |
Willy Tarreau | 1331766 | 2015-05-01 13:47:08 +0200 | [diff] [blame] | 1987 | else { |
| 1988 | /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */ |
| 1989 | if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn)) |
| 1990 | goto return_bad_req; |
| 1991 | } |
Willy Tarreau | 91852eb | 2015-05-01 13:26:00 +0200 | [diff] [blame] | 1992 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1993 | /* ... and check if the request is HTTP/1.1 or above */ |
| 1994 | if ((msg->sl.rq.v_l == 8) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1995 | ((req->buf->p[msg->sl.rq.v + 5] > '1') || |
| 1996 | ((req->buf->p[msg->sl.rq.v + 5] == '1') && |
| 1997 | (req->buf->p[msg->sl.rq.v + 7] >= '1')))) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 1998 | msg->flags |= HTTP_MSGF_VER_11; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1999 | |
| 2000 | /* "connection" has not been parsed yet */ |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 2001 | 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] | 2002 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2003 | /* if the frontend has "option http-use-proxy-header", we'll check if |
| 2004 | * we have what looks like a proxied connection instead of a connection, |
| 2005 | * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection. |
| 2006 | * Note that this is *not* RFC-compliant, however browsers and proxies |
| 2007 | * happen to do that despite being non-standard :-( |
| 2008 | * We consider that a request not beginning with either '/' or '*' is |
| 2009 | * a proxied connection, which covers both "scheme://location" and |
| 2010 | * CONNECT ip:port. |
| 2011 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2012 | if ((sess->fe->options2 & PR_O2_USE_PXHDR) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2013 | 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] | 2014 | txn->flags |= TX_USE_PX_CONN; |
| 2015 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2016 | /* transfer length unknown*/ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2017 | msg->flags &= ~HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2018 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2019 | /* 5: we may need to capture headers */ |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 2020 | if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap)) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2021 | capture_headers(req->buf->p, &txn->hdr_idx, |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 2022 | s->req_cap, sess->fe->req_cap); |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 2023 | |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 2024 | /* 6: determine the transfer-length according to RFC2616 #4.4, updated |
| 2025 | * by RFC7230#3.3.3 : |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2026 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 2027 | * The length of a message body is determined by one of the following |
| 2028 | * (in order of precedence): |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2029 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 2030 | * 1. Any response to a HEAD request and any response with a 1xx |
| 2031 | * (Informational), 204 (No Content), or 304 (Not Modified) status |
| 2032 | * code is always terminated by the first empty line after the |
| 2033 | * header fields, regardless of the header fields present in the |
| 2034 | * message, and thus cannot contain a message body. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2035 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 2036 | * 2. Any 2xx (Successful) response to a CONNECT request implies that |
| 2037 | * the connection will become a tunnel immediately after the empty |
| 2038 | * line that concludes the header fields. A client MUST ignore any |
| 2039 | * Content-Length or Transfer-Encoding header fields received in |
| 2040 | * such a message. |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2041 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 2042 | * 3. If a Transfer-Encoding header field is present and the chunked |
| 2043 | * transfer coding (Section 4.1) is the final encoding, the message |
| 2044 | * body length is determined by reading and decoding the chunked |
| 2045 | * data until the transfer coding indicates the data is complete. |
| 2046 | * |
| 2047 | * If a Transfer-Encoding header field is present in a response and |
| 2048 | * the chunked transfer coding is not the final encoding, the |
| 2049 | * message body length is determined by reading the connection until |
| 2050 | * it is closed by the server. If a Transfer-Encoding header field |
| 2051 | * is present in a request and the chunked transfer coding is not |
| 2052 | * the final encoding, the message body length cannot be determined |
| 2053 | * reliably; the server MUST respond with the 400 (Bad Request) |
| 2054 | * status code and then close the connection. |
| 2055 | * |
| 2056 | * If a message is received with both a Transfer-Encoding and a |
| 2057 | * Content-Length header field, the Transfer-Encoding overrides the |
| 2058 | * Content-Length. Such a message might indicate an attempt to |
| 2059 | * perform request smuggling (Section 9.5) or response splitting |
| 2060 | * (Section 9.4) and ought to be handled as an error. A sender MUST |
| 2061 | * remove the received Content-Length field prior to forwarding such |
| 2062 | * a message downstream. |
| 2063 | * |
| 2064 | * 4. If a message is received without Transfer-Encoding and with |
| 2065 | * either multiple Content-Length header fields having differing |
| 2066 | * field-values or a single Content-Length header field having an |
| 2067 | * invalid value, then the message framing is invalid and the |
| 2068 | * recipient MUST treat it as an unrecoverable error. If this is a |
| 2069 | * request message, the server MUST respond with a 400 (Bad Request) |
| 2070 | * status code and then close the connection. If this is a response |
| 2071 | * message received by a proxy, the proxy MUST close the connection |
| 2072 | * to the server, discard the received response, and send a 502 (Bad |
| 2073 | * Gateway) response to the client. If this is a response message |
| 2074 | * received by a user agent, the user agent MUST close the |
| 2075 | * connection to the server and discard the received response. |
| 2076 | * |
| 2077 | * 5. If a valid Content-Length header field is present without |
| 2078 | * Transfer-Encoding, its decimal value defines the expected message |
| 2079 | * body length in octets. If the sender closes the connection or |
| 2080 | * the recipient times out before the indicated number of octets are |
| 2081 | * received, the recipient MUST consider the message to be |
| 2082 | * incomplete and close the connection. |
| 2083 | * |
| 2084 | * 6. If this is a request message and none of the above are true, then |
| 2085 | * the message body length is zero (no message body is present). |
| 2086 | * |
| 2087 | * 7. Otherwise, this is a response message without a declared message |
| 2088 | * body length, so the message body length is determined by the |
| 2089 | * number of octets received prior to the server closing the |
| 2090 | * connection. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2091 | */ |
| 2092 | |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2093 | ctx.idx = 0; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2094 | /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */ |
Willy Tarreau | 4979d5c | 2015-05-01 10:06:30 +0200 | [diff] [blame] | 2095 | 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] | 2096 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
Christopher Faulet | be821b9 | 2017-03-30 11:21:53 +0200 | [diff] [blame] | 2097 | msg->flags |= HTTP_MSGF_TE_CHNK; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2098 | else if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | 34dfc60 | 2015-05-01 10:09:49 +0200 | [diff] [blame] | 2099 | /* chunked not last, return badreq */ |
| 2100 | goto return_bad_req; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2101 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2102 | } |
| 2103 | |
Willy Tarreau | 1c91391 | 2015-04-30 10:57:51 +0200 | [diff] [blame] | 2104 | /* Chunked requests must have their content-length removed */ |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2105 | ctx.idx = 0; |
Willy Tarreau | 1c91391 | 2015-04-30 10:57:51 +0200 | [diff] [blame] | 2106 | if (msg->flags & HTTP_MSGF_TE_CHNK) { |
| 2107 | while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) |
| 2108 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 2109 | } |
Willy Tarreau | 34dfc60 | 2015-05-01 10:09:49 +0200 | [diff] [blame] | 2110 | 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] | 2111 | signed long long cl; |
| 2112 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2113 | if (!ctx.vlen) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2114 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2115 | goto return_bad_req; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2116 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2117 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2118 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2119 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2120 | goto return_bad_req; /* parse failure */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2121 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2122 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2123 | if (cl < 0) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2124 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2125 | goto return_bad_req; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2126 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2127 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2128 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2129 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2130 | goto return_bad_req; /* already specified, was different */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2131 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2132 | |
Christopher Faulet | be821b9 | 2017-03-30 11:21:53 +0200 | [diff] [blame] | 2133 | msg->flags |= HTTP_MSGF_CNT_LEN; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 2134 | msg->body_len = msg->chunk_len = cl; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2135 | } |
| 2136 | |
Willy Tarreau | 34dfc60 | 2015-05-01 10:09:49 +0200 | [diff] [blame] | 2137 | /* even bodyless requests have a known length */ |
| 2138 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2139 | |
Willy Tarreau | 179085c | 2014-04-28 16:48:56 +0200 | [diff] [blame] | 2140 | /* Until set to anything else, the connection mode is set as Keep-Alive. It will |
| 2141 | * only change if both the request and the config reference something else. |
| 2142 | * Option httpclose by itself sets tunnel mode where headers are mangled. |
| 2143 | * However, if another mode is set, it will affect it (eg: server-close/ |
| 2144 | * keep-alive + httpclose = close). Note that we avoid to redo the same work |
| 2145 | * if FE and BE have the same settings (common). The method consists in |
| 2146 | * checking if options changed between the two calls (implying that either |
| 2147 | * one is non-null, or one of them is non-null and we are there for the first |
| 2148 | * time. |
| 2149 | */ |
| 2150 | if (!(txn->flags & TX_HDR_CONN_PRS) || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2151 | ((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] | 2152 | http_adjust_conn_mode(s, txn, msg); |
Willy Tarreau | 179085c | 2014-04-28 16:48:56 +0200 | [diff] [blame] | 2153 | |
Willy Tarreau | 9fbe18e | 2015-05-01 22:42:08 +0200 | [diff] [blame] | 2154 | /* we may have to wait for the request's body */ |
| 2155 | if ((s->be->options & PR_O_WREQ_BODY) && |
| 2156 | (msg->body_len || (msg->flags & HTTP_MSGF_TE_CHNK))) |
| 2157 | req->analysers |= AN_REQ_HTTP_BODY; |
| 2158 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2159 | /* end of job, return OK */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 2160 | req->analysers &= ~an_bit; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2161 | req->analyse_exp = TICK_ETERNITY; |
| 2162 | return 1; |
| 2163 | |
| 2164 | return_bad_req: |
| 2165 | /* We centralize bad requests processing here */ |
| 2166 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 2167 | /* we detected a parsing error. We want to archive this request |
| 2168 | * in the dedicated proxy area for later troubleshooting. |
| 2169 | */ |
Emeric Brun | 8c1aaa2 | 2017-06-15 11:30:06 +0200 | [diff] [blame] | 2170 | 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] | 2171 | } |
| 2172 | |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 2173 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2174 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 2175 | txn->status = 400; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 2176 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2177 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 2178 | HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 2179 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 2180 | HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1); |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2181 | |
| 2182 | return_prx_cond: |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2183 | if (!(s->flags & SF_ERR_MASK)) |
| 2184 | s->flags |= SF_ERR_PRXCOND; |
| 2185 | if (!(s->flags & SF_FINST_MASK)) |
| 2186 | s->flags |= SF_FINST_R; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2187 | |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 2188 | req->analysers &= AN_REQ_FLT_END; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2189 | req->analyse_exp = TICK_ETERNITY; |
| 2190 | return 0; |
| 2191 | } |
| 2192 | |
Willy Tarreau | 4f8a83c | 2012-06-04 00:26:23 +0200 | [diff] [blame] | 2193 | |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 2194 | /* This function prepares an applet to handle the stats. It can deal with the |
| 2195 | * "100-continue" expectation, check that admin rules are met for POST requests, |
| 2196 | * and program a response message if something was unexpected. It cannot fail |
| 2197 | * 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] | 2198 | * 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] | 2199 | * 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] | 2200 | * is expected to have already assigned an appctx to the stream. |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 2201 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2202 | int http_handle_stats(struct stream *s, struct channel *req) |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 2203 | { |
| 2204 | struct stats_admin_rule *stats_admin_rule; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 2205 | struct stream_interface *si = &s->si[1]; |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 2206 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 2207 | struct http_txn *txn = s->txn; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 2208 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2209 | struct uri_auth *uri_auth = s->be->uri_auth; |
| 2210 | const char *uri, *h, *lookup; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2211 | struct appctx *appctx; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2212 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2213 | appctx = si_appctx(si); |
| 2214 | memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats)); |
| 2215 | appctx->st1 = appctx->st2 = 0; |
| 2216 | appctx->ctx.stats.st_code = STAT_STATUS_INIT; |
| 2217 | appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 2218 | 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] | 2219 | appctx->ctx.stats.flags |= STAT_CHUNKED; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2220 | |
| 2221 | uri = msg->chn->buf->p + msg->sl.rq.u; |
| 2222 | lookup = uri + uri_auth->uri_len; |
| 2223 | |
| 2224 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) { |
| 2225 | if (memcmp(h, ";up", 3) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2226 | appctx->ctx.stats.flags |= STAT_HIDE_DOWN; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2227 | break; |
| 2228 | } |
| 2229 | } |
| 2230 | |
| 2231 | if (uri_auth->refresh) { |
| 2232 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) { |
| 2233 | if (memcmp(h, ";norefresh", 10) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2234 | appctx->ctx.stats.flags |= STAT_NO_REFRESH; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2235 | break; |
| 2236 | } |
| 2237 | } |
| 2238 | } |
| 2239 | |
| 2240 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) { |
| 2241 | if (memcmp(h, ";csv", 4) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2242 | appctx->ctx.stats.flags &= ~STAT_FMT_HTML; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2243 | break; |
| 2244 | } |
| 2245 | } |
| 2246 | |
Willy Tarreau | 1e62df9 | 2016-01-11 18:57:53 +0100 | [diff] [blame] | 2247 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 6; h++) { |
| 2248 | if (memcmp(h, ";typed", 6) == 0) { |
| 2249 | appctx->ctx.stats.flags &= ~STAT_FMT_HTML; |
| 2250 | appctx->ctx.stats.flags |= STAT_FMT_TYPED; |
| 2251 | break; |
| 2252 | } |
| 2253 | } |
| 2254 | |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2255 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) { |
| 2256 | if (memcmp(h, ";st=", 4) == 0) { |
| 2257 | int i; |
| 2258 | h += 4; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2259 | appctx->ctx.stats.st_code = STAT_STATUS_UNKN; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2260 | for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) { |
| 2261 | if (strncmp(stat_status_codes[i], h, 4) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2262 | appctx->ctx.stats.st_code = i; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2263 | break; |
| 2264 | } |
| 2265 | } |
| 2266 | break; |
| 2267 | } |
| 2268 | } |
| 2269 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2270 | appctx->ctx.stats.scope_str = 0; |
| 2271 | appctx->ctx.stats.scope_len = 0; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2272 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) { |
| 2273 | if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) { |
| 2274 | int itx = 0; |
| 2275 | const char *h2; |
| 2276 | char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1]; |
| 2277 | const char *err; |
| 2278 | |
| 2279 | h += strlen(STAT_SCOPE_INPUT_NAME) + 1; |
| 2280 | h2 = h; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2281 | appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2282 | while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') { |
| 2283 | itx++; |
| 2284 | h++; |
| 2285 | } |
| 2286 | |
| 2287 | if (itx > STAT_SCOPE_TXT_MAXLEN) |
| 2288 | itx = STAT_SCOPE_TXT_MAXLEN; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2289 | appctx->ctx.stats.scope_len = itx; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2290 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2291 | /* 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] | 2292 | memcpy(scope_txt, h2, itx); |
| 2293 | scope_txt[itx] = '\0'; |
| 2294 | err = invalid_char(scope_txt); |
| 2295 | if (err) { |
| 2296 | /* bad char in search text => clear scope */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2297 | appctx->ctx.stats.scope_str = 0; |
| 2298 | appctx->ctx.stats.scope_len = 0; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2299 | } |
| 2300 | break; |
| 2301 | } |
| 2302 | } |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 2303 | |
| 2304 | /* now check whether we have some admin rules for this request */ |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2305 | list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) { |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 2306 | int ret = 1; |
| 2307 | |
| 2308 | if (stats_admin_rule->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 2309 | 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] | 2310 | ret = acl_pass(ret); |
| 2311 | if (stats_admin_rule->cond->pol == ACL_COND_UNLESS) |
| 2312 | ret = !ret; |
| 2313 | } |
| 2314 | |
| 2315 | if (ret) { |
| 2316 | /* no rule, or the rule matches */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2317 | appctx->ctx.stats.flags |= STAT_ADMIN; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 2318 | break; |
| 2319 | } |
| 2320 | } |
| 2321 | |
| 2322 | /* Was the status page requested with a POST ? */ |
Willy Tarreau | b8cdf52 | 2015-05-29 01:09:15 +0200 | [diff] [blame] | 2323 | if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2324 | if (appctx->ctx.stats.flags & STAT_ADMIN) { |
Willy Tarreau | cfe7fdd | 2014-04-26 22:08:32 +0200 | [diff] [blame] | 2325 | /* we'll need the request body, possibly after sending 100-continue */ |
Willy Tarreau | b8cdf52 | 2015-05-29 01:09:15 +0200 | [diff] [blame] | 2326 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) |
| 2327 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2328 | appctx->st0 = STAT_HTTP_POST; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 2329 | } |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 2330 | else { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2331 | appctx->ctx.stats.st_code = STAT_STATUS_DENY; |
| 2332 | appctx->st0 = STAT_HTTP_LAST; |
de Lafond Guillaume | 88c278f | 2013-04-15 19:27:10 +0200 | [diff] [blame] | 2333 | } |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 2334 | } |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 2335 | else { |
| 2336 | /* So it was another method (GET/HEAD) */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2337 | appctx->st0 = STAT_HTTP_HEAD; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 2338 | } |
| 2339 | |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 2340 | s->task->nice = -32; /* small boost for HTTP statistics */ |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 2341 | return 1; |
| 2342 | } |
| 2343 | |
Lukas Tribus | 67db8df | 2013-06-23 17:37:13 +0200 | [diff] [blame] | 2344 | /* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets |
| 2345 | * (as per RFC3260 #4 and BCP37 #4.2 and #5.2). |
| 2346 | */ |
Vincent Bernat | 6e61589 | 2016-05-18 16:17:44 +0200 | [diff] [blame] | 2347 | 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] | 2348 | { |
| 2349 | #ifdef IP_TOS |
Vincent Bernat | 6e61589 | 2016-05-18 16:17:44 +0200 | [diff] [blame] | 2350 | if (from->ss_family == AF_INET) |
Lukas Tribus | 67db8df | 2013-06-23 17:37:13 +0200 | [diff] [blame] | 2351 | setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)); |
| 2352 | #endif |
| 2353 | #ifdef IPV6_TCLASS |
Vincent Bernat | 6e61589 | 2016-05-18 16:17:44 +0200 | [diff] [blame] | 2354 | if (from->ss_family == AF_INET6) { |
| 2355 | if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)from)->sin6_addr)) |
Lukas Tribus | 67db8df | 2013-06-23 17:37:13 +0200 | [diff] [blame] | 2356 | /* v4-mapped addresses need IP_TOS */ |
| 2357 | setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)); |
| 2358 | else |
| 2359 | setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos)); |
| 2360 | } |
| 2361 | #endif |
| 2362 | } |
| 2363 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2364 | int http_transform_header_str(struct stream* s, struct http_msg *msg, |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 2365 | const char* name, unsigned int name_len, |
| 2366 | const char *str, struct my_regex *re, |
| 2367 | int action) |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 2368 | { |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 2369 | struct hdr_ctx ctx; |
| 2370 | char *buf = msg->chn->buf->p; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 2371 | struct hdr_idx *idx = &s->txn->hdr_idx; |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 2372 | int (*http_find_hdr_func)(const char *name, int len, char *sol, |
| 2373 | struct hdr_idx *idx, struct hdr_ctx *ctx); |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 2374 | struct chunk *output = get_trash_chunk(); |
| 2375 | |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 2376 | ctx.idx = 0; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 2377 | |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 2378 | /* Choose the header browsing function. */ |
| 2379 | switch (action) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2380 | case ACT_HTTP_REPLACE_VAL: |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 2381 | http_find_hdr_func = http_find_header2; |
| 2382 | break; |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2383 | case ACT_HTTP_REPLACE_HDR: |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 2384 | http_find_hdr_func = http_find_full_header2; |
| 2385 | break; |
| 2386 | default: /* impossible */ |
| 2387 | return -1; |
| 2388 | } |
| 2389 | |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 2390 | while (http_find_hdr_func(name, name_len, buf, idx, &ctx)) { |
| 2391 | struct hdr_idx_elem *hdr = idx->v + ctx.idx; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 2392 | int delta; |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 2393 | char *val = ctx.line + ctx.val; |
| 2394 | char* val_end = val + ctx.vlen; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 2395 | |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 2396 | if (!regex_exec_match2(re, val, val_end-val, MAX_MATCH, pmatch, 0)) |
| 2397 | continue; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 2398 | |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 2399 | output->len = exp_replace(output->str, output->size, val, str, pmatch); |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 2400 | if (output->len == -1) |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 2401 | return -1; |
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 | 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] | 2404 | |
| 2405 | hdr->len += delta; |
| 2406 | http_msg_move_end(msg, delta); |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 2407 | |
| 2408 | /* Adjust the length of the current value of the index. */ |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 2409 | ctx.vlen += delta; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 2410 | } |
| 2411 | |
| 2412 | return 0; |
| 2413 | } |
| 2414 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2415 | static int http_transform_header(struct stream* s, struct http_msg *msg, |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 2416 | const char* name, unsigned int name_len, |
| 2417 | struct list *fmt, struct my_regex *re, |
| 2418 | int action) |
| 2419 | { |
Christopher Faulet | 07a0fec | 2017-02-08 12:17:07 +0100 | [diff] [blame] | 2420 | struct chunk *replace; |
| 2421 | int ret = -1; |
| 2422 | |
| 2423 | replace = alloc_trash_chunk(); |
| 2424 | if (!replace) |
| 2425 | goto leave; |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 2426 | |
| 2427 | replace->len = build_logline(s, replace->str, replace->size, fmt); |
| 2428 | if (replace->len >= replace->size - 1) |
Christopher Faulet | 07a0fec | 2017-02-08 12:17:07 +0100 | [diff] [blame] | 2429 | goto leave; |
| 2430 | |
| 2431 | 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] | 2432 | |
Christopher Faulet | 07a0fec | 2017-02-08 12:17:07 +0100 | [diff] [blame] | 2433 | leave: |
| 2434 | free_trash_chunk(replace); |
| 2435 | return ret; |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 2436 | } |
| 2437 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2438 | /* Executes the http-request rules <rules> for stream <s>, proxy <px> and |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 2439 | * transaction <txn>. Returns the verdict of the first rule that prevents |
| 2440 | * further processing of the request (auth, deny, ...), and defaults to |
| 2441 | * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or |
| 2442 | * 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] | 2443 | * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL |
| 2444 | * and a deny/tarpit rule is matched, it will be filled with this rule's deny |
| 2445 | * status. |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2446 | */ |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 2447 | enum rule_result |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 2448 | 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] | 2449 | { |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 2450 | struct session *sess = strm_sess(s); |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 2451 | struct http_txn *txn = s->txn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2452 | struct connection *cli_conn; |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 2453 | struct act_rule *rule; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 2454 | struct hdr_ctx ctx; |
Willy Tarreau | ae3c010 | 2014-04-28 23:22:08 +0200 | [diff] [blame] | 2455 | const char *auth_realm; |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 2456 | int act_flags = 0; |
Thierry Fournier | 4b788f7 | 2016-06-01 13:35:36 +0200 | [diff] [blame] | 2457 | int len; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2458 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 2459 | /* If "the current_rule_list" match the executed rule list, we are in |
| 2460 | * resume condition. If a resume is needed it is always in the action |
| 2461 | * and never in the ACL or converters. In this case, we initialise the |
| 2462 | * current rule, and go to the action execution point. |
| 2463 | */ |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 2464 | if (s->current_rule) { |
| 2465 | rule = s->current_rule; |
| 2466 | s->current_rule = NULL; |
| 2467 | if (s->current_rule_list == rules) |
| 2468 | goto resume_execution; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 2469 | } |
| 2470 | s->current_rule_list = rules; |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 2471 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2472 | list_for_each_entry(rule, rules, list) { |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2473 | |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 2474 | /* check optional condition */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2475 | if (rule->cond) { |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 2476 | int ret; |
| 2477 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 2478 | 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] | 2479 | ret = acl_pass(ret); |
| 2480 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2481 | if (rule->cond->pol == ACL_COND_UNLESS) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2482 | ret = !ret; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 2483 | |
| 2484 | if (!ret) /* condition not matched */ |
| 2485 | continue; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2486 | } |
| 2487 | |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 2488 | act_flags |= ACT_FLAG_FIRST; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 2489 | resume_execution: |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 2490 | switch (rule->action) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2491 | case ACT_ACTION_ALLOW: |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 2492 | return HTTP_RULE_RES_STOP; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 2493 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2494 | case ACT_ACTION_DENY: |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 2495 | if (deny_status) |
| 2496 | *deny_status = rule->deny_status; |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 2497 | return HTTP_RULE_RES_DENY; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 2498 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2499 | case ACT_HTTP_REQ_TARPIT: |
Willy Tarreau | ccbcc37 | 2012-12-27 12:37:57 +0100 | [diff] [blame] | 2500 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 2501 | if (deny_status) |
| 2502 | *deny_status = rule->deny_status; |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 2503 | return HTTP_RULE_RES_DENY; |
Willy Tarreau | ccbcc37 | 2012-12-27 12:37:57 +0100 | [diff] [blame] | 2504 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2505 | case ACT_HTTP_REQ_AUTH: |
Willy Tarreau | ae3c010 | 2014-04-28 23:22:08 +0200 | [diff] [blame] | 2506 | /* Auth might be performed on regular http-req rules as well as on stats */ |
| 2507 | auth_realm = rule->arg.auth.realm; |
| 2508 | if (!auth_realm) { |
| 2509 | if (px->uri_auth && rules == &px->uri_auth->http_req_rules) |
| 2510 | auth_realm = STATS_DEFAULT_REALM; |
| 2511 | else |
| 2512 | auth_realm = px->id; |
| 2513 | } |
| 2514 | /* send 401/407 depending on whether we use a proxy or not. We still |
| 2515 | * count one error, because normal browsing won't significantly |
| 2516 | * increase the counter but brute force attempts will. |
| 2517 | */ |
| 2518 | chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm); |
| 2519 | txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401; |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 2520 | http_reply_and_close(s, txn->status, &trash); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2521 | stream_inc_http_err_ctr(s); |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 2522 | return HTTP_RULE_RES_ABRT; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 2523 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2524 | case ACT_HTTP_REDIR: |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 2525 | if (!http_apply_redirect_rule(rule->arg.redir, s, txn)) |
| 2526 | return HTTP_RULE_RES_BADREQ; |
| 2527 | return HTTP_RULE_RES_DONE; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 2528 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2529 | case ACT_HTTP_SET_NICE: |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 2530 | s->task->nice = rule->arg.nice; |
| 2531 | break; |
| 2532 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2533 | case ACT_HTTP_SET_TOS: |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 2534 | if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 2535 | 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] | 2536 | break; |
| 2537 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2538 | case ACT_HTTP_SET_MARK: |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 2539 | #ifdef SO_MARK |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 2540 | if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 2541 | 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] | 2542 | #endif |
| 2543 | break; |
| 2544 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2545 | case ACT_HTTP_SET_LOGL: |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 2546 | s->logs.level = rule->arg.loglevel; |
| 2547 | break; |
| 2548 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2549 | case ACT_HTTP_REPLACE_HDR: |
| 2550 | case ACT_HTTP_REPLACE_VAL: |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 2551 | if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name, |
| 2552 | rule->arg.hdr_add.name_len, |
| 2553 | &rule->arg.hdr_add.fmt, |
| 2554 | &rule->arg.hdr_add.re, rule->action)) |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 2555 | return HTTP_RULE_RES_BADREQ; |
| 2556 | break; |
| 2557 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2558 | case ACT_HTTP_DEL_HDR: |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 2559 | ctx.idx = 0; |
| 2560 | /* remove all occurrences of the header */ |
| 2561 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 2562 | txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 2563 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 2564 | } |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 2565 | break; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 2566 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2567 | case ACT_HTTP_SET_HDR: |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2568 | case ACT_HTTP_ADD_HDR: { |
Thierry Fournier | 4b788f7 | 2016-06-01 13:35:36 +0200 | [diff] [blame] | 2569 | /* The scope of the trash buffer must be limited to this function. The |
| 2570 | * build_logline() function can execute a lot of other function which |
| 2571 | * can use the trash buffer. So for limiting the scope of this global |
| 2572 | * buffer, we build first the header value using build_logline, and |
| 2573 | * after we store the header name. |
| 2574 | */ |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2575 | struct chunk *replace; |
| 2576 | |
| 2577 | replace = alloc_trash_chunk(); |
| 2578 | if (!replace) |
| 2579 | return HTTP_RULE_RES_BADREQ; |
| 2580 | |
Thierry Fournier | 4b788f7 | 2016-06-01 13:35:36 +0200 | [diff] [blame] | 2581 | len = rule->arg.hdr_add.name_len + 2, |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2582 | len += build_logline(s, replace->str + len, replace->size - len, &rule->arg.hdr_add.fmt); |
| 2583 | memcpy(replace->str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len); |
| 2584 | replace->str[rule->arg.hdr_add.name_len] = ':'; |
| 2585 | replace->str[rule->arg.hdr_add.name_len + 1] = ' '; |
| 2586 | replace->len = len; |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 2587 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2588 | if (rule->action == ACT_HTTP_SET_HDR) { |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 2589 | /* remove all occurrences of the header */ |
| 2590 | ctx.idx = 0; |
| 2591 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 2592 | txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 2593 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
| 2594 | } |
| 2595 | } |
| 2596 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2597 | http_header_add_tail2(&txn->req, &txn->hdr_idx, replace->str, replace->len); |
| 2598 | |
| 2599 | free_trash_chunk(replace); |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 2600 | break; |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2601 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2602 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2603 | case ACT_HTTP_DEL_ACL: |
| 2604 | case ACT_HTTP_DEL_MAP: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2605 | struct pat_ref *ref; |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2606 | struct chunk *key; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2607 | |
| 2608 | /* collect reference */ |
| 2609 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 2610 | if (!ref) |
| 2611 | continue; |
| 2612 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2613 | /* allocate key */ |
| 2614 | key = alloc_trash_chunk(); |
| 2615 | if (!key) |
| 2616 | return HTTP_RULE_RES_BADREQ; |
| 2617 | |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2618 | /* collect key */ |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2619 | key->len = build_logline(s, key->str, key->size, &rule->arg.map.key); |
| 2620 | key->str[key->len] = '\0'; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2621 | |
| 2622 | /* perform update */ |
| 2623 | /* returned code: 1=ok, 0=ko */ |
Emeric Brun | b5997f7 | 2017-07-03 11:34:05 +0200 | [diff] [blame] | 2624 | SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2625 | pat_ref_delete(ref, key->str); |
Emeric Brun | b5997f7 | 2017-07-03 11:34:05 +0200 | [diff] [blame] | 2626 | SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2627 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2628 | free_trash_chunk(key); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2629 | break; |
| 2630 | } |
| 2631 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2632 | case ACT_HTTP_ADD_ACL: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2633 | struct pat_ref *ref; |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2634 | struct chunk *key; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2635 | |
| 2636 | /* collect reference */ |
| 2637 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 2638 | if (!ref) |
| 2639 | continue; |
| 2640 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2641 | /* allocate key */ |
| 2642 | key = alloc_trash_chunk(); |
| 2643 | if (!key) |
| 2644 | return HTTP_RULE_RES_BADREQ; |
| 2645 | |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2646 | /* collect key */ |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2647 | key->len = build_logline(s, key->str, key->size, &rule->arg.map.key); |
| 2648 | key->str[key->len] = '\0'; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2649 | |
| 2650 | /* perform update */ |
| 2651 | /* add entry only if it does not already exist */ |
Emeric Brun | b5997f7 | 2017-07-03 11:34:05 +0200 | [diff] [blame] | 2652 | SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2653 | if (pat_ref_find_elt(ref, key->str) == NULL) |
| 2654 | pat_ref_add(ref, key->str, NULL, NULL); |
Emeric Brun | b5997f7 | 2017-07-03 11:34:05 +0200 | [diff] [blame] | 2655 | SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2656 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2657 | free_trash_chunk(key); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2658 | break; |
| 2659 | } |
| 2660 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2661 | case ACT_HTTP_SET_MAP: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2662 | struct pat_ref *ref; |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2663 | struct chunk *key, *value; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2664 | |
| 2665 | /* collect reference */ |
| 2666 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 2667 | if (!ref) |
| 2668 | continue; |
| 2669 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2670 | /* allocate key */ |
| 2671 | key = alloc_trash_chunk(); |
| 2672 | if (!key) |
| 2673 | return HTTP_RULE_RES_BADREQ; |
| 2674 | |
| 2675 | /* allocate value */ |
| 2676 | value = alloc_trash_chunk(); |
| 2677 | if (!value) { |
| 2678 | free_trash_chunk(key); |
| 2679 | return HTTP_RULE_RES_BADREQ; |
| 2680 | } |
| 2681 | |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2682 | /* collect key */ |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2683 | key->len = build_logline(s, key->str, key->size, &rule->arg.map.key); |
| 2684 | key->str[key->len] = '\0'; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2685 | |
| 2686 | /* collect value */ |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2687 | value->len = build_logline(s, value->str, value->size, &rule->arg.map.value); |
| 2688 | value->str[value->len] = '\0'; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2689 | |
| 2690 | /* perform update */ |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2691 | if (pat_ref_find_elt(ref, key->str) != NULL) |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2692 | /* update entry if it exists */ |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2693 | pat_ref_set(ref, key->str, value->str, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2694 | else |
| 2695 | /* insert a new entry */ |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2696 | pat_ref_add(ref, key->str, value->str, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2697 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2698 | free_trash_chunk(key); |
| 2699 | free_trash_chunk(value); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2700 | break; |
| 2701 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 2702 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 2703 | case ACT_CUSTOM: |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 2704 | if ((px->options & PR_O_ABRT_CLOSE) && (s->req.flags & (CF_SHUTR|CF_READ_NULL|CF_READ_ERROR))) |
| 2705 | act_flags |= ACT_FLAG_FINAL; |
Willy Tarreau | 3945868 | 2015-09-27 10:33:15 +0200 | [diff] [blame] | 2706 | |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 2707 | switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) { |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 2708 | case ACT_RET_ERR: |
| 2709 | case ACT_RET_CONT: |
| 2710 | break; |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 2711 | case ACT_RET_STOP: |
| 2712 | return HTTP_RULE_RES_DONE; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 2713 | case ACT_RET_YIELD: |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 2714 | s->current_rule = rule; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 2715 | return HTTP_RULE_RES_YIELD; |
| 2716 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 2717 | break; |
| 2718 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2719 | case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX: |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 2720 | /* Note: only the first valid tracking parameter of each |
| 2721 | * applies. |
| 2722 | */ |
| 2723 | |
Christopher Faulet | 4fce0d8 | 2017-09-18 11:57:31 +0200 | [diff] [blame] | 2724 | if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) { |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 2725 | struct stktable *t; |
| 2726 | struct stksess *ts; |
| 2727 | struct stktable_key *key; |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 2728 | void *ptr1, *ptr2; |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 2729 | |
Thierry FOURNIER | 5ec63e0 | 2015-08-04 09:09:48 +0200 | [diff] [blame] | 2730 | t = rule->arg.trk_ctr.table.t; |
| 2731 | 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] | 2732 | |
| 2733 | if (key && (ts = stktable_get_entry(t, key))) { |
Christopher Faulet | 4fce0d8 | 2017-09-18 11:57:31 +0200 | [diff] [blame] | 2734 | stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts); |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 2735 | |
| 2736 | /* 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] | 2737 | ptr1 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT); |
| 2738 | ptr2 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE); |
| 2739 | if (ptr1 || ptr2) { |
| 2740 | RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock); |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 2741 | |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 2742 | if (ptr1) |
| 2743 | stktable_data_cast(ptr1, http_req_cnt)++; |
| 2744 | |
| 2745 | if (ptr2) |
| 2746 | update_freq_ctr_period(&stktable_data_cast(ptr2, http_req_rate), |
| 2747 | t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1); |
| 2748 | |
| 2749 | RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock); |
| 2750 | } |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 2751 | |
Christopher Faulet | 4fce0d8 | 2017-09-18 11:57:31 +0200 | [diff] [blame] | 2752 | stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT); |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 2753 | if (sess->fe != s->be) |
Christopher Faulet | 4fce0d8 | 2017-09-18 11:57:31 +0200 | [diff] [blame] | 2754 | stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND); |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 2755 | } |
| 2756 | } |
Adis Nezirovic | 2fbcafc | 2015-07-06 15:44:30 +0200 | [diff] [blame] | 2757 | break; |
| 2758 | |
Thierry FOURNIER | 22e4901 | 2015-08-05 19:13:48 +0200 | [diff] [blame] | 2759 | /* other flags exists, but normaly, they never be matched. */ |
| 2760 | default: |
| 2761 | break; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2762 | } |
| 2763 | } |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 2764 | |
| 2765 | /* we reached the end of the rules, nothing to report */ |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 2766 | return HTTP_RULE_RES_CONT; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2767 | } |
| 2768 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2769 | |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 2770 | /* Executes the http-response rules <rules> for stream <s> and proxy <px>. It |
| 2771 | * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP, |
| 2772 | * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT |
| 2773 | * is returned, the process can continue the evaluation of next rule list. If |
| 2774 | * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ |
| 2775 | * is returned, it means the operation could not be processed and a server error |
| 2776 | * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a |
| 2777 | * deny rule. If *YIELD is returned, the caller must call again the function |
| 2778 | * with the same context. |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2779 | */ |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 2780 | static enum rule_result |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 2781 | 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] | 2782 | { |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 2783 | struct session *sess = strm_sess(s); |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 2784 | struct http_txn *txn = s->txn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2785 | struct connection *cli_conn; |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 2786 | struct act_rule *rule; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2787 | struct hdr_ctx ctx; |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 2788 | int act_flags = 0; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2789 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 2790 | /* If "the current_rule_list" match the executed rule list, we are in |
| 2791 | * resume condition. If a resume is needed it is always in the action |
| 2792 | * and never in the ACL or converters. In this case, we initialise the |
| 2793 | * current rule, and go to the action execution point. |
| 2794 | */ |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 2795 | if (s->current_rule) { |
| 2796 | rule = s->current_rule; |
| 2797 | s->current_rule = NULL; |
| 2798 | if (s->current_rule_list == rules) |
| 2799 | goto resume_execution; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 2800 | } |
| 2801 | s->current_rule_list = rules; |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 2802 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2803 | list_for_each_entry(rule, rules, list) { |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2804 | |
| 2805 | /* check optional condition */ |
| 2806 | if (rule->cond) { |
| 2807 | int ret; |
| 2808 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 2809 | 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] | 2810 | ret = acl_pass(ret); |
| 2811 | |
| 2812 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 2813 | ret = !ret; |
| 2814 | |
| 2815 | if (!ret) /* condition not matched */ |
| 2816 | continue; |
| 2817 | } |
| 2818 | |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 2819 | act_flags |= ACT_FLAG_FIRST; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 2820 | resume_execution: |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2821 | switch (rule->action) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2822 | case ACT_ACTION_ALLOW: |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 2823 | return HTTP_RULE_RES_STOP; /* "allow" rules are OK */ |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2824 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2825 | case ACT_ACTION_DENY: |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2826 | txn->flags |= TX_SVDENY; |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 2827 | return HTTP_RULE_RES_STOP; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2828 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2829 | case ACT_HTTP_SET_NICE: |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 2830 | s->task->nice = rule->arg.nice; |
| 2831 | break; |
| 2832 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2833 | case ACT_HTTP_SET_TOS: |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 2834 | if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 2835 | 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] | 2836 | break; |
| 2837 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2838 | case ACT_HTTP_SET_MARK: |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 2839 | #ifdef SO_MARK |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 2840 | if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 2841 | 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] | 2842 | #endif |
| 2843 | break; |
| 2844 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2845 | case ACT_HTTP_SET_LOGL: |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 2846 | s->logs.level = rule->arg.loglevel; |
| 2847 | break; |
| 2848 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2849 | case ACT_HTTP_REPLACE_HDR: |
| 2850 | case ACT_HTTP_REPLACE_VAL: |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 2851 | if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name, |
| 2852 | rule->arg.hdr_add.name_len, |
| 2853 | &rule->arg.hdr_add.fmt, |
| 2854 | &rule->arg.hdr_add.re, rule->action)) |
Christopher Faulet | cdade94 | 2017-02-08 12:41:31 +0100 | [diff] [blame] | 2855 | return HTTP_RULE_RES_BADREQ; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 2856 | break; |
| 2857 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2858 | case ACT_HTTP_DEL_HDR: |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2859 | ctx.idx = 0; |
| 2860 | /* remove all occurrences of the header */ |
| 2861 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 2862 | txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 2863 | http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx); |
| 2864 | } |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 2865 | break; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2866 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2867 | case ACT_HTTP_SET_HDR: |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2868 | case ACT_HTTP_ADD_HDR: { |
| 2869 | struct chunk *replace; |
| 2870 | |
| 2871 | replace = alloc_trash_chunk(); |
| 2872 | if (!replace) |
| 2873 | return HTTP_RULE_RES_BADREQ; |
| 2874 | |
| 2875 | chunk_printf(replace, "%s: ", rule->arg.hdr_add.name); |
| 2876 | memcpy(replace->str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len); |
| 2877 | replace->len = rule->arg.hdr_add.name_len; |
| 2878 | replace->str[replace->len++] = ':'; |
| 2879 | replace->str[replace->len++] = ' '; |
| 2880 | replace->len += build_logline(s, replace->str + replace->len, replace->size - replace->len, |
| 2881 | &rule->arg.hdr_add.fmt); |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 2882 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2883 | if (rule->action == ACT_HTTP_SET_HDR) { |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 2884 | /* remove all occurrences of the header */ |
| 2885 | ctx.idx = 0; |
| 2886 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 2887 | txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 2888 | http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx); |
| 2889 | } |
| 2890 | } |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2891 | http_header_add_tail2(&txn->rsp, &txn->hdr_idx, replace->str, replace->len); |
| 2892 | |
| 2893 | free_trash_chunk(replace); |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2894 | break; |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2895 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2896 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2897 | case ACT_HTTP_DEL_ACL: |
| 2898 | case ACT_HTTP_DEL_MAP: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2899 | struct pat_ref *ref; |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2900 | struct chunk *key; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2901 | |
| 2902 | /* collect reference */ |
| 2903 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 2904 | if (!ref) |
| 2905 | continue; |
| 2906 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2907 | /* allocate key */ |
| 2908 | key = alloc_trash_chunk(); |
| 2909 | if (!key) |
| 2910 | return HTTP_RULE_RES_BADREQ; |
| 2911 | |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2912 | /* collect key */ |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2913 | key->len = build_logline(s, key->str, key->size, &rule->arg.map.key); |
| 2914 | key->str[key->len] = '\0'; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2915 | |
| 2916 | /* perform update */ |
| 2917 | /* returned code: 1=ok, 0=ko */ |
Emeric Brun | b5997f7 | 2017-07-03 11:34:05 +0200 | [diff] [blame] | 2918 | SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2919 | pat_ref_delete(ref, key->str); |
Emeric Brun | b5997f7 | 2017-07-03 11:34:05 +0200 | [diff] [blame] | 2920 | SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2921 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2922 | free_trash_chunk(key); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2923 | break; |
| 2924 | } |
| 2925 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2926 | case ACT_HTTP_ADD_ACL: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2927 | struct pat_ref *ref; |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2928 | struct chunk *key; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2929 | |
| 2930 | /* collect reference */ |
| 2931 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 2932 | if (!ref) |
| 2933 | continue; |
| 2934 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2935 | /* allocate key */ |
| 2936 | key = alloc_trash_chunk(); |
| 2937 | if (!key) |
| 2938 | return HTTP_RULE_RES_BADREQ; |
| 2939 | |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2940 | /* collect key */ |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2941 | key->len = build_logline(s, key->str, key->size, &rule->arg.map.key); |
| 2942 | key->str[key->len] = '\0'; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2943 | |
| 2944 | /* perform update */ |
| 2945 | /* check if the entry already exists */ |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2946 | if (pat_ref_find_elt(ref, key->str) == NULL) |
| 2947 | pat_ref_add(ref, key->str, NULL, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2948 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2949 | free_trash_chunk(key); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2950 | break; |
| 2951 | } |
| 2952 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2953 | case ACT_HTTP_SET_MAP: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2954 | struct pat_ref *ref; |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2955 | struct chunk *key, *value; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2956 | |
| 2957 | /* collect reference */ |
| 2958 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 2959 | if (!ref) |
| 2960 | continue; |
| 2961 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2962 | /* allocate key */ |
| 2963 | key = alloc_trash_chunk(); |
| 2964 | if (!key) |
| 2965 | return HTTP_RULE_RES_BADREQ; |
| 2966 | |
| 2967 | /* allocate value */ |
| 2968 | value = alloc_trash_chunk(); |
| 2969 | if (!value) { |
| 2970 | free_trash_chunk(key); |
| 2971 | return HTTP_RULE_RES_BADREQ; |
| 2972 | } |
| 2973 | |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2974 | /* collect key */ |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2975 | key->len = build_logline(s, key->str, key->size, &rule->arg.map.key); |
| 2976 | key->str[key->len] = '\0'; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2977 | |
| 2978 | /* collect value */ |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2979 | value->len = build_logline(s, value->str, value->size, &rule->arg.map.value); |
| 2980 | value->str[value->len] = '\0'; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2981 | |
| 2982 | /* perform update */ |
Emeric Brun | b5997f7 | 2017-07-03 11:34:05 +0200 | [diff] [blame] | 2983 | SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2984 | if (pat_ref_find_elt(ref, key->str) != NULL) |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2985 | /* update entry if it exists */ |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2986 | pat_ref_set(ref, key->str, value->str, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2987 | else |
| 2988 | /* insert a new entry */ |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2989 | pat_ref_add(ref, key->str, value->str, NULL); |
Emeric Brun | b5997f7 | 2017-07-03 11:34:05 +0200 | [diff] [blame] | 2990 | SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2991 | free_trash_chunk(key); |
| 2992 | free_trash_chunk(value); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2993 | break; |
| 2994 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 2995 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2996 | case ACT_HTTP_REDIR: |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 2997 | if (!http_apply_redirect_rule(rule->arg.redir, s, txn)) |
| 2998 | return HTTP_RULE_RES_BADREQ; |
| 2999 | return HTTP_RULE_RES_DONE; |
| 3000 | |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 3001 | case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX: |
| 3002 | /* Note: only the first valid tracking parameter of each |
| 3003 | * applies. |
| 3004 | */ |
| 3005 | |
Christopher Faulet | 4fce0d8 | 2017-09-18 11:57:31 +0200 | [diff] [blame] | 3006 | if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) { |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 3007 | struct stktable *t; |
| 3008 | struct stksess *ts; |
| 3009 | struct stktable_key *key; |
| 3010 | void *ptr; |
| 3011 | |
| 3012 | t = rule->arg.trk_ctr.table.t; |
| 3013 | key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_RES | SMP_OPT_FINAL, rule->arg.trk_ctr.expr, NULL); |
| 3014 | |
| 3015 | if (key && (ts = stktable_get_entry(t, key))) { |
Christopher Faulet | 4fce0d8 | 2017-09-18 11:57:31 +0200 | [diff] [blame] | 3016 | stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts); |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 3017 | |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 3018 | RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock); |
| 3019 | |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 3020 | /* let's count a new HTTP request as it's the first time we do it */ |
| 3021 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT); |
| 3022 | if (ptr) |
| 3023 | stktable_data_cast(ptr, http_req_cnt)++; |
| 3024 | |
| 3025 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE); |
| 3026 | if (ptr) |
| 3027 | update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate), |
| 3028 | t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1); |
| 3029 | |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 3030 | /* When the client triggers a 4xx from the server, it's most often due |
| 3031 | * to a missing object or permission. These events should be tracked |
| 3032 | * because if they happen often, it may indicate a brute force or a |
| 3033 | * vulnerability scan. Normally this is done when receiving the response |
| 3034 | * but here we're tracking after this ought to have been done so we have |
| 3035 | * to do it on purpose. |
| 3036 | */ |
Willy Tarreau | 3146a4c | 2016-07-26 15:22:33 +0200 | [diff] [blame] | 3037 | if ((unsigned)(txn->status - 400) < 100) { |
| 3038 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT); |
| 3039 | if (ptr) |
| 3040 | stktable_data_cast(ptr, http_err_cnt)++; |
| 3041 | |
| 3042 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE); |
| 3043 | if (ptr) |
| 3044 | update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate), |
| 3045 | t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1); |
| 3046 | } |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 3047 | |
| 3048 | RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock); |
| 3049 | |
| 3050 | stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT); |
| 3051 | if (sess->fe != s->be) |
| 3052 | stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND); |
| 3053 | |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 3054 | } |
| 3055 | } |
| 3056 | break; |
| 3057 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 3058 | case ACT_CUSTOM: |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 3059 | if ((px->options & PR_O_ABRT_CLOSE) && (s->req.flags & (CF_SHUTR|CF_READ_NULL|CF_READ_ERROR))) |
| 3060 | act_flags |= ACT_FLAG_FINAL; |
Willy Tarreau | 3945868 | 2015-09-27 10:33:15 +0200 | [diff] [blame] | 3061 | |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 3062 | switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) { |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 3063 | case ACT_RET_ERR: |
| 3064 | case ACT_RET_CONT: |
| 3065 | break; |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 3066 | case ACT_RET_STOP: |
| 3067 | return HTTP_RULE_RES_STOP; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 3068 | case ACT_RET_YIELD: |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 3069 | s->current_rule = rule; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3070 | return HTTP_RULE_RES_YIELD; |
| 3071 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 3072 | break; |
| 3073 | |
Thierry FOURNIER | 22e4901 | 2015-08-05 19:13:48 +0200 | [diff] [blame] | 3074 | /* other flags exists, but normaly, they never be matched. */ |
| 3075 | default: |
| 3076 | break; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3077 | } |
| 3078 | } |
| 3079 | |
| 3080 | /* we reached the end of the rules, nothing to report */ |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 3081 | return HTTP_RULE_RES_CONT; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3082 | } |
| 3083 | |
| 3084 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3085 | /* Perform an HTTP redirect based on the information in <rule>. The function |
| 3086 | * returns non-zero on success, or zero in case of a, irrecoverable error such |
| 3087 | * as too large a request to build a valid response. |
| 3088 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3089 | 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] | 3090 | { |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 3091 | struct http_msg *req = &txn->req; |
| 3092 | struct http_msg *res = &txn->rsp; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3093 | const char *msg_fmt; |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3094 | struct chunk *chunk; |
| 3095 | int ret = 0; |
| 3096 | |
| 3097 | chunk = alloc_trash_chunk(); |
| 3098 | if (!chunk) |
| 3099 | goto leave; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3100 | |
| 3101 | /* build redirect message */ |
| 3102 | switch(rule->code) { |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 3103 | case 308: |
| 3104 | msg_fmt = HTTP_308; |
| 3105 | break; |
| 3106 | case 307: |
| 3107 | msg_fmt = HTTP_307; |
| 3108 | break; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3109 | case 303: |
| 3110 | msg_fmt = HTTP_303; |
| 3111 | break; |
| 3112 | case 301: |
| 3113 | msg_fmt = HTTP_301; |
| 3114 | break; |
| 3115 | case 302: |
| 3116 | default: |
| 3117 | msg_fmt = HTTP_302; |
| 3118 | break; |
| 3119 | } |
| 3120 | |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3121 | if (unlikely(!chunk_strcpy(chunk, msg_fmt))) |
| 3122 | goto leave; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3123 | |
| 3124 | switch(rule->type) { |
| 3125 | case REDIRECT_TYPE_SCHEME: { |
| 3126 | const char *path; |
| 3127 | const char *host; |
| 3128 | struct hdr_ctx ctx; |
| 3129 | int pathlen; |
| 3130 | int hostlen; |
| 3131 | |
| 3132 | host = ""; |
| 3133 | hostlen = 0; |
| 3134 | ctx.idx = 0; |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 3135 | 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] | 3136 | host = ctx.line + ctx.val; |
| 3137 | hostlen = ctx.vlen; |
| 3138 | } |
| 3139 | |
| 3140 | path = http_get_path(txn); |
| 3141 | /* build message using path */ |
| 3142 | if (path) { |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 3143 | 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] | 3144 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 3145 | int qs = 0; |
| 3146 | while (qs < pathlen) { |
| 3147 | if (path[qs] == '?') { |
| 3148 | pathlen = qs; |
| 3149 | break; |
| 3150 | } |
| 3151 | qs++; |
| 3152 | } |
| 3153 | } |
| 3154 | } else { |
| 3155 | path = "/"; |
| 3156 | pathlen = 1; |
| 3157 | } |
| 3158 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3159 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
| 3160 | /* check if we can add scheme + "://" + host + path */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3161 | if (chunk->len + rule->rdr_len + 3 + hostlen + pathlen > chunk->size - 4) |
| 3162 | goto leave; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3163 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3164 | /* add scheme */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3165 | memcpy(chunk->str + chunk->len, rule->rdr_str, rule->rdr_len); |
| 3166 | chunk->len += rule->rdr_len; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3167 | } |
| 3168 | else { |
| 3169 | /* add scheme with executing log format */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3170 | 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] | 3171 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3172 | /* check if we can add scheme + "://" + host + path */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3173 | if (chunk->len + 3 + hostlen + pathlen > chunk->size - 4) |
| 3174 | goto leave; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3175 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3176 | /* add "://" */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3177 | memcpy(chunk->str + chunk->len, "://", 3); |
| 3178 | chunk->len += 3; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3179 | |
| 3180 | /* add host */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3181 | memcpy(chunk->str + chunk->len, host, hostlen); |
| 3182 | chunk->len += hostlen; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3183 | |
| 3184 | /* add path */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3185 | memcpy(chunk->str + chunk->len, path, pathlen); |
| 3186 | chunk->len += pathlen; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3187 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3188 | /* 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] | 3189 | if (chunk->len && chunk->str[chunk->len - 1] != '/' && |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3190 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3191 | if (chunk->len > chunk->size - 5) |
| 3192 | goto leave; |
| 3193 | chunk->str[chunk->len] = '/'; |
| 3194 | chunk->len++; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3195 | } |
| 3196 | |
| 3197 | break; |
| 3198 | } |
| 3199 | case REDIRECT_TYPE_PREFIX: { |
| 3200 | const char *path; |
| 3201 | int pathlen; |
| 3202 | |
| 3203 | path = http_get_path(txn); |
| 3204 | /* build message using path */ |
| 3205 | if (path) { |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 3206 | 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] | 3207 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 3208 | int qs = 0; |
| 3209 | while (qs < pathlen) { |
| 3210 | if (path[qs] == '?') { |
| 3211 | pathlen = qs; |
| 3212 | break; |
| 3213 | } |
| 3214 | qs++; |
| 3215 | } |
| 3216 | } |
| 3217 | } else { |
| 3218 | path = "/"; |
| 3219 | pathlen = 1; |
| 3220 | } |
| 3221 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3222 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3223 | if (chunk->len + rule->rdr_len + pathlen > chunk->size - 4) |
| 3224 | goto leave; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3225 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3226 | /* add prefix. Note that if prefix == "/", we don't want to |
| 3227 | * add anything, otherwise it makes it hard for the user to |
| 3228 | * configure a self-redirection. |
| 3229 | */ |
| 3230 | if (rule->rdr_len != 1 || *rule->rdr_str != '/') { |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3231 | memcpy(chunk->str + chunk->len, rule->rdr_str, rule->rdr_len); |
| 3232 | chunk->len += rule->rdr_len; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3233 | } |
| 3234 | } |
| 3235 | else { |
| 3236 | /* add prefix with executing log format */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3237 | 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] | 3238 | |
| 3239 | /* Check length */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3240 | if (chunk->len + pathlen > chunk->size - 4) |
| 3241 | goto leave; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3242 | } |
| 3243 | |
| 3244 | /* add path */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3245 | memcpy(chunk->str + chunk->len, path, pathlen); |
| 3246 | chunk->len += pathlen; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3247 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3248 | /* 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] | 3249 | if (chunk->len && chunk->str[chunk->len - 1] != '/' && |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3250 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3251 | if (chunk->len > chunk->size - 5) |
| 3252 | goto leave; |
| 3253 | chunk->str[chunk->len] = '/'; |
| 3254 | chunk->len++; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3255 | } |
| 3256 | |
| 3257 | break; |
| 3258 | } |
| 3259 | case REDIRECT_TYPE_LOCATION: |
| 3260 | default: |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3261 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3262 | if (chunk->len + rule->rdr_len > chunk->size - 4) |
| 3263 | goto leave; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3264 | |
| 3265 | /* add location */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3266 | memcpy(chunk->str + chunk->len, rule->rdr_str, rule->rdr_len); |
| 3267 | chunk->len += rule->rdr_len; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3268 | } |
| 3269 | else { |
| 3270 | /* add location with executing log format */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3271 | 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] | 3272 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3273 | /* Check left length */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3274 | if (chunk->len > chunk->size - 4) |
| 3275 | goto leave; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3276 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3277 | break; |
| 3278 | } |
| 3279 | |
| 3280 | if (rule->cookie_len) { |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3281 | memcpy(chunk->str + chunk->len, "\r\nSet-Cookie: ", 14); |
| 3282 | chunk->len += 14; |
| 3283 | memcpy(chunk->str + chunk->len, rule->cookie_str, rule->cookie_len); |
| 3284 | chunk->len += rule->cookie_len; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3285 | } |
| 3286 | |
Willy Tarreau | 19b1412 | 2017-02-28 09:48:11 +0100 | [diff] [blame] | 3287 | /* add end of headers and the keep-alive/close status. */ |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3288 | txn->status = rule->code; |
| 3289 | /* let's log the request time */ |
| 3290 | s->logs.tv_request = now; |
| 3291 | |
Christopher Faulet | be821b9 | 2017-03-30 11:21:53 +0200 | [diff] [blame] | 3292 | 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] | 3293 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL || |
| 3294 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) { |
| 3295 | /* keep-alive possible */ |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 3296 | if (!(req->flags & HTTP_MSGF_VER_11)) { |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3297 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3298 | memcpy(chunk->str + chunk->len, "\r\nProxy-Connection: keep-alive", 30); |
| 3299 | chunk->len += 30; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3300 | } else { |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3301 | memcpy(chunk->str + chunk->len, "\r\nConnection: keep-alive", 24); |
| 3302 | chunk->len += 24; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3303 | } |
| 3304 | } |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3305 | memcpy(chunk->str + chunk->len, "\r\n\r\n", 4); |
| 3306 | chunk->len += 4; |
| 3307 | FLT_STRM_CB(s, flt_http_reply(s, txn->status, chunk)); |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3308 | co_inject(res->chn, chunk->str, chunk->len); |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3309 | /* "eat" the request */ |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 3310 | bi_fast_delete(req->chn->buf, req->sov); |
| 3311 | req->next -= req->sov; |
| 3312 | req->sov = 0; |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 3313 | 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] | 3314 | 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] | 3315 | req->msg_state = HTTP_MSG_CLOSED; |
| 3316 | res->msg_state = HTTP_MSG_DONE; |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 3317 | /* Trim any possible response */ |
| 3318 | res->chn->buf->i = 0; |
| 3319 | res->next = res->sov = 0; |
Christopher Faulet | 5d468ca | 2017-09-11 09:27:29 +0200 | [diff] [blame] | 3320 | /* let the server side turn to SI_ST_CLO */ |
| 3321 | channel_shutw_now(req->chn); |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3322 | } else { |
| 3323 | /* keep-alive not possible */ |
| 3324 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3325 | memcpy(chunk->str + chunk->len, "\r\nProxy-Connection: close\r\n\r\n", 29); |
| 3326 | chunk->len += 29; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3327 | } else { |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3328 | memcpy(chunk->str + chunk->len, "\r\nConnection: close\r\n\r\n", 23); |
| 3329 | chunk->len += 23; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3330 | } |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3331 | http_reply_and_close(s, txn->status, chunk); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 3332 | req->chn->analysers &= AN_REQ_FLT_END; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3333 | } |
| 3334 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3335 | if (!(s->flags & SF_ERR_MASK)) |
| 3336 | s->flags |= SF_ERR_LOCAL; |
| 3337 | if (!(s->flags & SF_FINST_MASK)) |
| 3338 | s->flags |= SF_FINST_R; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3339 | |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3340 | ret = 1; |
| 3341 | leave: |
| 3342 | free_trash_chunk(chunk); |
| 3343 | return ret; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3344 | } |
| 3345 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3346 | /* This stream analyser runs all HTTP request processing which is common to |
| 3347 | * frontends and backends, which means blocking ACLs, filters, connection-close, |
| 3348 | * reqadd, stats and redirects. This is performed for the designated proxy. |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3349 | * 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] | 3350 | * either needs more data or wants to immediately abort the request (eg: deny, |
| 3351 | * error, ...). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3352 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3353 | 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] | 3354 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 3355 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3356 | struct http_txn *txn = s->txn; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3357 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3358 | struct redirect_rule *rule; |
Willy Tarreau | f4f0412 | 2010-01-28 18:10:50 +0100 | [diff] [blame] | 3359 | struct cond_wordlist *wl; |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3360 | enum rule_result verdict; |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 3361 | int deny_status = HTTP_ERR_403; |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 3362 | struct connection *conn = objt_conn(sess->origin); |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3363 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 3364 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3365 | /* we need more data */ |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3366 | goto return_prx_yield; |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3367 | } |
| 3368 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3369 | 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] | 3370 | now_ms, __FUNCTION__, |
| 3371 | s, |
| 3372 | req, |
| 3373 | req->rex, req->wex, |
| 3374 | req->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3375 | req->buf->i, |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3376 | req->analysers); |
| 3377 | |
Willy Tarreau | 6541083 | 2014-04-28 21:25:43 +0200 | [diff] [blame] | 3378 | /* just in case we have some per-backend tracking */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3379 | stream_inc_be_http_req_ctr(s); |
Willy Tarreau | 6541083 | 2014-04-28 21:25:43 +0200 | [diff] [blame] | 3380 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3381 | /* evaluate http-request rules */ |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3382 | if (!LIST_ISEMPTY(&px->http_req_rules)) { |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 3383 | 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] | 3384 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3385 | switch (verdict) { |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3386 | case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */ |
| 3387 | goto return_prx_yield; |
| 3388 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3389 | case HTTP_RULE_RES_CONT: |
| 3390 | case HTTP_RULE_RES_STOP: /* nothing to do */ |
| 3391 | break; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 3392 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3393 | case HTTP_RULE_RES_DENY: /* deny or tarpit */ |
| 3394 | if (txn->flags & TX_CLTARPIT) |
| 3395 | goto tarpit; |
| 3396 | goto deny; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 3397 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3398 | case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */ |
| 3399 | goto return_prx_cond; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 3400 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3401 | case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */ |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 3402 | goto done; |
| 3403 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3404 | case HTTP_RULE_RES_BADREQ: /* failed with a bad request */ |
| 3405 | goto return_bad_req; |
| 3406 | } |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3407 | } |
| 3408 | |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 3409 | if (conn && conn->flags & CO_FL_EARLY_DATA) { |
| 3410 | struct hdr_ctx ctx; |
| 3411 | |
| 3412 | ctx.idx = 0; |
| 3413 | if (!http_find_header2("Early-Data", strlen("Early-Data"), |
| 3414 | s->req.buf->p, &txn->hdr_idx, &ctx)) { |
| 3415 | if (unlikely(http_header_add_tail2(&txn->req, |
| 3416 | &txn->hdr_idx, "Early-Data: 1", |
| 3417 | strlen("Early-Data: 1"))) < 0) { |
| 3418 | goto return_bad_req; |
| 3419 | } |
| 3420 | } |
| 3421 | |
| 3422 | } |
| 3423 | |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 3424 | /* OK at this stage, we know that the request was accepted according to |
| 3425 | * the http-request rules, we can check for the stats. Note that the |
| 3426 | * URI is detected *before* the req* rules in order not to be affected |
| 3427 | * by a possible reqrep, while they are processed *after* so that a |
| 3428 | * reqdeny can still block them. This clearly needs to change in 1.6! |
| 3429 | */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 3430 | if (stats_check_uri(&s->si[1], txn, px)) { |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 3431 | s->target = &http_stats_applet.obj_type; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 3432 | 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] | 3433 | txn->status = 500; |
| 3434 | s->logs.tv_request = now; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 3435 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3436 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3437 | if (!(s->flags & SF_ERR_MASK)) |
| 3438 | s->flags |= SF_ERR_RESOURCE; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 3439 | goto return_prx_cond; |
| 3440 | } |
| 3441 | |
| 3442 | /* parse the whole stats request and extract the relevant information */ |
| 3443 | http_handle_stats(s, req); |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 3444 | 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] | 3445 | /* not all actions implemented: deny, allow, auth */ |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3446 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3447 | if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */ |
| 3448 | goto deny; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 3449 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3450 | if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */ |
| 3451 | goto return_prx_cond; |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 3452 | } |
| 3453 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3454 | /* evaluate the req* rules except reqadd */ |
| 3455 | if (px->req_exp != NULL) { |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 3456 | if (apply_filters_to_request(s, req, px) < 0) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3457 | goto return_bad_req; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3458 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3459 | if (txn->flags & TX_CLDENY) |
| 3460 | goto deny; |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 3461 | |
Jarno Huuskonen | 800d176 | 2017-03-06 14:56:36 +0200 | [diff] [blame] | 3462 | if (txn->flags & TX_CLTARPIT) { |
| 3463 | deny_status = HTTP_ERR_500; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3464 | goto tarpit; |
Jarno Huuskonen | 800d176 | 2017-03-06 14:56:36 +0200 | [diff] [blame] | 3465 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3466 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3467 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3468 | /* add request headers from the rule sets in the same order */ |
| 3469 | list_for_each_entry(wl, &px->req_add, list) { |
| 3470 | if (wl->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 3471 | 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] | 3472 | ret = acl_pass(ret); |
| 3473 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 3474 | ret = !ret; |
| 3475 | if (!ret) |
| 3476 | continue; |
| 3477 | } |
| 3478 | |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 3479 | 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] | 3480 | goto return_bad_req; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 3481 | } |
| 3482 | |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 3483 | |
| 3484 | /* Proceed with the stats now. */ |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3485 | if (unlikely(objt_applet(s->target) == &http_stats_applet)) { |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3486 | /* process the stats request now */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3487 | 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] | 3488 | HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1); |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 3489 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3490 | if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is |
| 3491 | s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy |
| 3492 | if (!(s->flags & SF_FINST_MASK)) |
| 3493 | s->flags |= SF_FINST_R; |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 3494 | |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 3495 | /* 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] | 3496 | req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END); |
| 3497 | req->analysers &= ~AN_REQ_FLT_XFER_DATA; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 3498 | req->analysers |= AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3499 | goto done; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3500 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 3501 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3502 | /* check whether we have some ACLs set to redirect this request */ |
| 3503 | list_for_each_entry(rule, &px->redirect_rules, list) { |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 3504 | if (rule->cond) { |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3505 | int ret; |
| 3506 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 3507 | 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] | 3508 | ret = acl_pass(ret); |
| 3509 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 3510 | ret = !ret; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3511 | if (!ret) |
| 3512 | continue; |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 3513 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3514 | if (!http_apply_redirect_rule(rule, s, txn)) |
| 3515 | goto return_bad_req; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3516 | goto done; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3517 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 3518 | |
Willy Tarreau | 2be3939 | 2010-01-03 17:24:51 +0100 | [diff] [blame] | 3519 | /* POST requests may be accompanied with an "Expect: 100-Continue" header. |
| 3520 | * If this happens, then the data will not come immediately, so we must |
| 3521 | * send all what we have without waiting. Note that due to the small gain |
| 3522 | * 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] | 3523 | * 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] | 3524 | * itself once used. |
| 3525 | */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 3526 | req->flags |= CF_SEND_DONTWAIT; |
Willy Tarreau | 2be3939 | 2010-01-03 17:24:51 +0100 | [diff] [blame] | 3527 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3528 | done: /* done with this analyser, continue with next ones that the calling |
| 3529 | * points will have set, if any. |
| 3530 | */ |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3531 | req->analyse_exp = TICK_ETERNITY; |
Thierry FOURNIER | 7566e30 | 2014-08-22 06:55:26 +0200 | [diff] [blame] | 3532 | done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */ |
| 3533 | req->analysers &= ~an_bit; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3534 | return 1; |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 3535 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3536 | tarpit: |
Willy Tarreau | 6a0bca9 | 2017-06-11 17:56:27 +0200 | [diff] [blame] | 3537 | /* Allow cookie logging |
| 3538 | */ |
| 3539 | if (s->be->cookie_name || sess->fe->capture_name) |
| 3540 | manage_client_side_cookies(s, req); |
| 3541 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3542 | /* When a connection is tarpitted, we use the tarpit timeout, |
| 3543 | * which may be the same as the connect timeout if unspecified. |
| 3544 | * If unset, then set it to zero because we really want it to |
| 3545 | * eventually expire. We build the tarpit as an analyser. |
| 3546 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 3547 | channel_erase(&s->req); |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3548 | |
| 3549 | /* wipe the request out so that we can drop the connection early |
| 3550 | * if the client closes first. |
| 3551 | */ |
| 3552 | channel_dont_connect(req); |
Bertrand Paquet | 83a2c3d | 2016-04-06 11:58:31 +0200 | [diff] [blame] | 3553 | |
Jarno Huuskonen | 800d176 | 2017-03-06 14:56:36 +0200 | [diff] [blame] | 3554 | txn->status = http_err_codes[deny_status]; |
| 3555 | |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 3556 | req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */ |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3557 | req->analysers |= AN_REQ_HTTP_TARPIT; |
| 3558 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit); |
| 3559 | if (!req->analyse_exp) |
| 3560 | req->analyse_exp = tick_add(now_ms, 0); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3561 | stream_inc_http_err_ctr(s); |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 3562 | HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3563 | if (sess->fe != s->be) |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 3564 | HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 3565 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 3566 | HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1); |
Thierry FOURNIER | 7566e30 | 2014-08-22 06:55:26 +0200 | [diff] [blame] | 3567 | goto done_without_exp; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3568 | |
| 3569 | deny: /* this request was blocked (denied) */ |
Bertrand Paquet | 83a2c3d | 2016-04-06 11:58:31 +0200 | [diff] [blame] | 3570 | |
| 3571 | /* Allow cookie logging |
| 3572 | */ |
| 3573 | if (s->be->cookie_name || sess->fe->capture_name) |
| 3574 | manage_client_side_cookies(s, req); |
| 3575 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3576 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 3577 | txn->status = http_err_codes[deny_status]; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3578 | s->logs.tv_request = now; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 3579 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3580 | stream_inc_http_err_ctr(s); |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 3581 | HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3582 | if (sess->fe != s->be) |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 3583 | HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 3584 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 3585 | HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1); |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3586 | goto return_prx_cond; |
| 3587 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3588 | return_bad_req: |
| 3589 | /* We centralize bad requests processing here */ |
| 3590 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 3591 | /* we detected a parsing error. We want to archive this request |
| 3592 | * in the dedicated proxy area for later troubleshooting. |
| 3593 | */ |
Emeric Brun | 8c1aaa2 | 2017-06-15 11:30:06 +0200 | [diff] [blame] | 3594 | 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] | 3595 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 3596 | |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 3597 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3598 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3599 | txn->status = 400; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 3600 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3601 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 3602 | HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 3603 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 3604 | HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1); |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 3605 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3606 | return_prx_cond: |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3607 | if (!(s->flags & SF_ERR_MASK)) |
| 3608 | s->flags |= SF_ERR_PRXCOND; |
| 3609 | if (!(s->flags & SF_FINST_MASK)) |
| 3610 | s->flags |= SF_FINST_R; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 3611 | |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 3612 | req->analysers &= AN_REQ_FLT_END; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3613 | req->analyse_exp = TICK_ETERNITY; |
| 3614 | return 0; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3615 | |
| 3616 | return_prx_yield: |
| 3617 | channel_dont_connect(req); |
| 3618 | return 0; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3619 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3620 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3621 | /* This function performs all the processing enabled for the current request. |
| 3622 | * It returns 1 if the processing can continue on next analysers, or zero if it |
| 3623 | * needs more data, encounters an error, or wants to immediately abort the |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 3624 | * request. It relies on buffers flags, and updates s->req.analysers. |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3625 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3626 | 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] | 3627 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 3628 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3629 | struct http_txn *txn = s->txn; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3630 | struct http_msg *msg = &txn->req; |
Willy Tarreau | ee335e6 | 2015-04-21 18:15:13 +0200 | [diff] [blame] | 3631 | struct connection *cli_conn = objt_conn(strm_sess(s)->origin); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3632 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 3633 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3634 | /* we need more data */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 3635 | channel_dont_connect(req); |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3636 | return 0; |
| 3637 | } |
| 3638 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3639 | 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] | 3640 | now_ms, __FUNCTION__, |
| 3641 | s, |
| 3642 | req, |
| 3643 | req->rex, req->wex, |
| 3644 | req->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3645 | req->buf->i, |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3646 | req->analysers); |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3647 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3648 | /* |
| 3649 | * Right now, we know that we have processed the entire headers |
| 3650 | * and that unwanted requests have been filtered out. We can do |
| 3651 | * whatever we want with the remaining request. Also, now we |
| 3652 | * may have separate values for ->fe, ->be. |
| 3653 | */ |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3654 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3655 | /* |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3656 | * If HTTP PROXY is set we simply get remote server address parsing |
| 3657 | * incoming request. Note that this requires that a connection is |
| 3658 | * allocated on the server side. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3659 | */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3660 | 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] | 3661 | struct connection *conn; |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 3662 | char *path; |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 3663 | |
Willy Tarreau | 9471b8c | 2013-12-15 13:31:35 +0100 | [diff] [blame] | 3664 | /* Note that for now we don't reuse existing proxy connections */ |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 3665 | 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] | 3666 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 3667 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3668 | txn->status = 500; |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 3669 | req->analysers &= AN_REQ_FLT_END; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 3670 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 3671 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3672 | if (!(s->flags & SF_ERR_MASK)) |
| 3673 | s->flags |= SF_ERR_RESOURCE; |
| 3674 | if (!(s->flags & SF_FINST_MASK)) |
| 3675 | s->flags |= SF_FINST_R; |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 3676 | |
| 3677 | return 0; |
| 3678 | } |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 3679 | |
| 3680 | path = http_get_path(txn); |
| 3681 | url2sa(req->buf->p + msg->sl.rq.u, |
| 3682 | 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] | 3683 | &conn->addr.to, NULL); |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 3684 | /* if the path was found, we have to remove everything between |
| 3685 | * req->buf->p + msg->sl.rq.u and path (excluded). If it was not |
| 3686 | * found, we need to replace from req->buf->p + msg->sl.rq.u for |
| 3687 | * u_l characters by a single "/". |
| 3688 | */ |
| 3689 | if (path) { |
| 3690 | char *cur_ptr = req->buf->p; |
| 3691 | char *cur_end = cur_ptr + txn->req.sl.rq.l; |
| 3692 | int delta; |
| 3693 | |
| 3694 | delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0); |
| 3695 | http_msg_move_end(&txn->req, delta); |
| 3696 | cur_end += delta; |
| 3697 | if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL) |
| 3698 | goto return_bad_req; |
| 3699 | } |
| 3700 | else { |
| 3701 | char *cur_ptr = req->buf->p; |
| 3702 | char *cur_end = cur_ptr + txn->req.sl.rq.l; |
| 3703 | int delta; |
| 3704 | |
| 3705 | delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, |
| 3706 | req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1); |
| 3707 | http_msg_move_end(&txn->req, delta); |
| 3708 | cur_end += delta; |
| 3709 | if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL) |
| 3710 | goto return_bad_req; |
| 3711 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3712 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3713 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3714 | /* |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 3715 | * 7: Now we can work with the cookies. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3716 | * Note that doing so might move headers in the request, but |
| 3717 | * the fields will stay coherent and the URI will not move. |
| 3718 | * This should only be performed in the backend. |
| 3719 | */ |
Bertrand Paquet | 83a2c3d | 2016-04-06 11:58:31 +0200 | [diff] [blame] | 3720 | if (s->be->cookie_name || sess->fe->capture_name) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3721 | manage_client_side_cookies(s, req); |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 3722 | |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 3723 | /* add unique-id if "header-unique-id" is specified */ |
| 3724 | |
Thierry Fournier | f4011dd | 2016-03-29 17:23:51 +0200 | [diff] [blame] | 3725 | if (!LIST_ISEMPTY(&sess->fe->format_unique_id) && !s->unique_id) { |
William Lallemand | 5b7ea3a | 2013-08-28 15:44:19 +0200 | [diff] [blame] | 3726 | if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL) |
| 3727 | goto return_bad_req; |
| 3728 | s->unique_id[0] = '\0'; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3729 | 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] | 3730 | } |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 3731 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3732 | if (sess->fe->header_unique_id && s->unique_id) { |
| 3733 | 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] | 3734 | if (trash.len < 0) |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 3735 | goto return_bad_req; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3736 | 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] | 3737 | goto return_bad_req; |
| 3738 | } |
| 3739 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 3740 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3741 | * 9: add X-Forwarded-For if either the frontend or the backend |
| 3742 | * asks for it. |
| 3743 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3744 | if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) { |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 3745 | struct hdr_ctx ctx = { .idx = 0 }; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3746 | if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) && |
| 3747 | http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name, |
| 3748 | 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] | 3749 | req->buf->p, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 3750 | /* The header is set to be added only if none is present |
| 3751 | * and we found it, so don't do anything. |
| 3752 | */ |
| 3753 | } |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3754 | else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3755 | /* Add an X-Forwarded-For header unless the source IP is |
| 3756 | * in the 'except' network range. |
| 3757 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3758 | if ((!sess->fe->except_mask.s_addr || |
| 3759 | (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr) |
| 3760 | != sess->fe->except_net.s_addr) && |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3761 | (!s->be->except_mask.s_addr || |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3762 | (((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] | 3763 | != s->be->except_net.s_addr)) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 3764 | int len; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3765 | unsigned char *pn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3766 | pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr; |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 3767 | |
| 3768 | /* Note: we rely on the backend to get the header name to be used for |
| 3769 | * x-forwarded-for, because the header is really meant for the backends. |
| 3770 | * However, if the backend did not specify any option, we have to rely |
| 3771 | * on the frontend's header name. |
| 3772 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3773 | if (s->be->fwdfor_hdr_len) { |
| 3774 | len = s->be->fwdfor_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3775 | memcpy(trash.str, s->be->fwdfor_hdr_name, len); |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 3776 | } else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3777 | len = sess->fe->fwdfor_hdr_len; |
| 3778 | memcpy(trash.str, sess->fe->fwdfor_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 3779 | } |
Willy Tarreau | e9187f8 | 2014-04-14 15:27:14 +0200 | [diff] [blame] | 3780 | 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] | 3781 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3782 | 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] | 3783 | goto return_bad_req; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 3784 | } |
| 3785 | } |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3786 | else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3787 | /* FIXME: for the sake of completeness, we should also support |
| 3788 | * 'except' here, although it is mostly useless in this case. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3789 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3790 | int len; |
| 3791 | char pn[INET6_ADDRSTRLEN]; |
| 3792 | inet_ntop(AF_INET6, |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3793 | (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3794 | pn, sizeof(pn)); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3795 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3796 | /* Note: we rely on the backend to get the header name to be used for |
| 3797 | * x-forwarded-for, because the header is really meant for the backends. |
| 3798 | * However, if the backend did not specify any option, we have to rely |
| 3799 | * on the frontend's header name. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3800 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3801 | if (s->be->fwdfor_hdr_len) { |
| 3802 | len = s->be->fwdfor_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3803 | memcpy(trash.str, s->be->fwdfor_hdr_name, len); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3804 | } else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3805 | len = sess->fe->fwdfor_hdr_len; |
| 3806 | memcpy(trash.str, sess->fe->fwdfor_hdr_name, len); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3807 | } |
Willy Tarreau | e9187f8 | 2014-04-14 15:27:14 +0200 | [diff] [blame] | 3808 | len += snprintf(trash.str + len, trash.size - len, ": %s", pn); |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3809 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3810 | 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] | 3811 | goto return_bad_req; |
| 3812 | } |
| 3813 | } |
| 3814 | |
| 3815 | /* |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3816 | * 10: add X-Original-To if either the frontend or the backend |
| 3817 | * asks for it. |
| 3818 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3819 | if ((sess->fe->options | s->be->options) & PR_O_ORGTO) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3820 | |
| 3821 | /* FIXME: don't know if IPv6 can handle that case too. */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3822 | if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3823 | /* Add an X-Original-To header unless the destination IP is |
| 3824 | * in the 'except' network range. |
| 3825 | */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3826 | conn_get_to_addr(cli_conn); |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3827 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3828 | if (cli_conn->addr.to.ss_family == AF_INET && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3829 | ((!sess->fe->except_mask_to.s_addr || |
| 3830 | (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr) |
| 3831 | != sess->fe->except_to.s_addr) && |
Emeric Brun | 5bd86a8 | 2010-10-22 17:23:04 +0200 | [diff] [blame] | 3832 | (!s->be->except_mask_to.s_addr || |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3833 | (((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] | 3834 | != s->be->except_to.s_addr))) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3835 | int len; |
| 3836 | unsigned char *pn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3837 | pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr; |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3838 | |
| 3839 | /* Note: we rely on the backend to get the header name to be used for |
| 3840 | * x-original-to, because the header is really meant for the backends. |
| 3841 | * However, if the backend did not specify any option, we have to rely |
| 3842 | * on the frontend's header name. |
| 3843 | */ |
| 3844 | if (s->be->orgto_hdr_len) { |
| 3845 | len = s->be->orgto_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3846 | memcpy(trash.str, s->be->orgto_hdr_name, len); |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3847 | } else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3848 | len = sess->fe->orgto_hdr_len; |
| 3849 | memcpy(trash.str, sess->fe->orgto_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 3850 | } |
Willy Tarreau | e9187f8 | 2014-04-14 15:27:14 +0200 | [diff] [blame] | 3851 | 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] | 3852 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3853 | 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] | 3854 | goto return_bad_req; |
| 3855 | } |
| 3856 | } |
| 3857 | } |
| 3858 | |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 3859 | /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set. |
| 3860 | * If an "Upgrade" token is found, the header is left untouched in order not to have |
| 3861 | * to deal with some servers bugs : some of them fail an Upgrade if anything but |
| 3862 | * "Upgrade" is present in the Connection header. |
| 3863 | */ |
| 3864 | if (!(txn->flags & TX_HDR_CONN_UPG) && |
| 3865 | (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3866 | ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 3867 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) { |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3868 | unsigned int want_flags = 0; |
| 3869 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3870 | if (msg->flags & HTTP_MSGF_VER_11) { |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 3871 | if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3872 | ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 3873 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3874 | !((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3875 | want_flags |= TX_CON_CLO_SET; |
| 3876 | } else { |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 3877 | if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3878 | ((sess->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL && |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 3879 | (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3880 | ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3881 | want_flags |= TX_CON_KAL_SET; |
| 3882 | } |
| 3883 | |
| 3884 | 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] | 3885 | http_change_connection_header(txn, msg, want_flags); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3886 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3887 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3888 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3889 | /* If we have no server assigned yet and we're balancing on url_param |
| 3890 | * with a POST request, we may be interested in checking the body for |
| 3891 | * that parameter. This will be done in another analyser. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3892 | */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3893 | if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) && |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3894 | s->txn->meth == HTTP_METH_POST && s->be->url_param_name != NULL && |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3895 | (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) { |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 3896 | channel_dont_connect(req); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3897 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3898 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3899 | |
Christopher Faulet | be821b9 | 2017-03-30 11:21:53 +0200 | [diff] [blame] | 3900 | req->analysers &= ~AN_REQ_FLT_XFER_DATA; |
| 3901 | req->analysers |= AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 3902 | #ifdef TCP_QUICKACK |
Christopher Faulet | be821b9 | 2017-03-30 11:21:53 +0200 | [diff] [blame] | 3903 | /* We expect some data from the client. Unless we know for sure |
| 3904 | * we already have a full request, we have to re-enable quick-ack |
| 3905 | * in case we previously disabled it, otherwise we might cause |
| 3906 | * the client to delay further data. |
| 3907 | */ |
| 3908 | if ((sess->listener->options & LI_O_NOQUICKACK) && |
| 3909 | cli_conn && conn_ctrl_ready(cli_conn) && |
| 3910 | ((msg->flags & HTTP_MSGF_TE_CHNK) || |
| 3911 | (msg->body_len > req->buf->i - txn->req.eoh - 2))) |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 3912 | setsockopt(cli_conn->handle.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one)); |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 3913 | #endif |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 3914 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3915 | /************************************************************* |
| 3916 | * OK, that's finished for the headers. We have done what we * |
| 3917 | * could. Let's switch to the DATA state. * |
| 3918 | ************************************************************/ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3919 | req->analyse_exp = TICK_ETERNITY; |
| 3920 | req->analysers &= ~an_bit; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3921 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3922 | s->logs.tv_request = now; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3923 | /* OK let's go on with the BODY now */ |
| 3924 | return 1; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3925 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3926 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 3927 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
Willy Tarreau | f073a83 | 2009-03-01 23:21:47 +0100 | [diff] [blame] | 3928 | /* we detected a parsing error. We want to archive this request |
| 3929 | * in the dedicated proxy area for later troubleshooting. |
| 3930 | */ |
Emeric Brun | 8c1aaa2 | 2017-06-15 11:30:06 +0200 | [diff] [blame] | 3931 | 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] | 3932 | } |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 3933 | |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 3934 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3935 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3936 | txn->status = 400; |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 3937 | req->analysers &= AN_REQ_FLT_END; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 3938 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3939 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 3940 | HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 3941 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 3942 | HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1); |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3943 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3944 | if (!(s->flags & SF_ERR_MASK)) |
| 3945 | s->flags |= SF_ERR_PRXCOND; |
| 3946 | if (!(s->flags & SF_FINST_MASK)) |
| 3947 | s->flags |= SF_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3948 | return 0; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 3949 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3950 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3951 | /* This function is an analyser which processes the HTTP tarpit. It always |
| 3952 | * returns zero, at the beginning because it prevents any other processing |
| 3953 | * from occurring, and at the end because it terminates the request. |
| 3954 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3955 | 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] | 3956 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3957 | struct http_txn *txn = s->txn; |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3958 | |
| 3959 | /* This connection is being tarpitted. The CLIENT side has |
| 3960 | * already set the connect expiration date to the right |
| 3961 | * timeout. We just have to check that the client is still |
| 3962 | * there and that the timeout has not expired. |
| 3963 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 3964 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 3965 | if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 && |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3966 | !tick_is_expired(req->analyse_exp, now_ms)) |
| 3967 | return 0; |
| 3968 | |
| 3969 | /* We will set the queue timer to the time spent, just for |
| 3970 | * logging purposes. We fake a 500 server error, so that the |
| 3971 | * attacker will not suspect his connection has been tarpitted. |
| 3972 | * It will not cause trouble to the logs because we can exclude |
| 3973 | * the tarpitted connections by filtering on the 'PT' status flags. |
| 3974 | */ |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3975 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 3976 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 3977 | if (!(req->flags & CF_READ_ERROR)) |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 3978 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3979 | |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 3980 | req->analysers &= AN_REQ_FLT_END; |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3981 | req->analyse_exp = TICK_ETERNITY; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3982 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3983 | if (!(s->flags & SF_ERR_MASK)) |
| 3984 | s->flags |= SF_ERR_PRXCOND; |
| 3985 | if (!(s->flags & SF_FINST_MASK)) |
| 3986 | s->flags |= SF_FINST_T; |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3987 | return 0; |
| 3988 | } |
| 3989 | |
Willy Tarreau | 5a8f947 | 2014-04-10 11:16:06 +0200 | [diff] [blame] | 3990 | /* This function is an analyser which waits for the HTTP request body. It waits |
| 3991 | * for either the buffer to be full, or the full advertised contents to have |
| 3992 | * reached the buffer. It must only be called after the standard HTTP request |
| 3993 | * processing has occurred, because it expects the request to be parsed and will |
| 3994 | * look for the Expect header. It may send a 100-Continue interim response. It |
| 3995 | * takes in input any state starting from HTTP_MSG_BODY and leaves with one of |
| 3996 | * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it |
| 3997 | * 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] | 3998 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3999 | 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] | 4000 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4001 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4002 | struct http_txn *txn = s->txn; |
| 4003 | struct http_msg *msg = &s->txn->req; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4004 | |
| 4005 | /* We have to parse the HTTP request body to find any required data. |
| 4006 | * "balance url_param check_post" should have been the only way to get |
| 4007 | * into this. We were brought here after HTTP header analysis, so all |
| 4008 | * related structures are ready. |
| 4009 | */ |
| 4010 | |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4011 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
| 4012 | /* This is the first call */ |
| 4013 | if (msg->msg_state < HTTP_MSG_BODY) |
| 4014 | goto missing_data; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4015 | |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4016 | if (msg->msg_state < HTTP_MSG_100_SENT) { |
| 4017 | /* If we have HTTP/1.1 and Expect: 100-continue, then we must |
| 4018 | * send an HTTP/1.1 100 Continue intermediate response. |
| 4019 | */ |
| 4020 | if (msg->flags & HTTP_MSGF_VER_11) { |
| 4021 | struct hdr_ctx ctx; |
| 4022 | ctx.idx = 0; |
| 4023 | /* Expect is allowed in 1.1, look for it */ |
| 4024 | if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) && |
| 4025 | 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] | 4026 | 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] | 4027 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4028 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4029 | } |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4030 | msg->msg_state = HTTP_MSG_100_SENT; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4031 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4032 | |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 4033 | /* 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] | 4034 | * req->buf->p still points to the beginning of the message. We |
| 4035 | * must save the body in msg->next because it survives buffer |
| 4036 | * re-alignments. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4037 | */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 4038 | msg->next = msg->sov; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4039 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4040 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4041 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 4042 | else |
| 4043 | msg->msg_state = HTTP_MSG_DATA; |
| 4044 | } |
| 4045 | |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4046 | if (!(msg->flags & HTTP_MSGF_TE_CHNK)) { |
| 4047 | /* 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] | 4048 | if (http_body_bytes(msg) < msg->body_len) |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4049 | goto missing_data; |
| 4050 | |
| 4051 | /* OK we have everything we need now */ |
| 4052 | goto http_end; |
| 4053 | } |
| 4054 | |
| 4055 | /* OK here we're parsing a chunked-encoded message */ |
| 4056 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4057 | if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 4058 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4059 | * 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] | 4060 | * TRAILERS state. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 4061 | */ |
Willy Tarreau | e56cdd3 | 2017-09-21 08:36:33 +0200 | [diff] [blame] | 4062 | unsigned int chunk; |
| 4063 | 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] | 4064 | |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 4065 | if (!ret) |
| 4066 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4067 | else if (ret < 0) { |
Willy Tarreau | e56cdd3 | 2017-09-21 08:36:33 +0200 | [diff] [blame] | 4068 | msg->err_pos = req->buf->i + ret; |
| 4069 | if (msg->err_pos < 0) |
| 4070 | msg->err_pos += req->buf->size; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4071 | stream_inc_http_err_ctr(s); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4072 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4073 | } |
Willy Tarreau | e56cdd3 | 2017-09-21 08:36:33 +0200 | [diff] [blame] | 4074 | |
| 4075 | msg->chunk_len = chunk; |
| 4076 | msg->body_len += chunk; |
| 4077 | |
| 4078 | msg->sol = ret; |
Christopher Faulet | 113f7de | 2015-12-14 14:52:13 +0100 | [diff] [blame] | 4079 | msg->next += ret; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 4080 | msg->msg_state = msg->chunk_len ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4081 | } |
| 4082 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4083 | /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state. |
Willy Tarreau | e115b49 | 2015-05-01 23:05:14 +0200 | [diff] [blame] | 4084 | * We have the first data byte is in msg->sov + msg->sol. We're waiting |
| 4085 | * for at least a whole chunk or the whole content length bytes after |
| 4086 | * msg->sov + msg->sol. |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4087 | */ |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4088 | if (msg->msg_state == HTTP_MSG_TRAILERS) |
| 4089 | goto http_end; |
| 4090 | |
Willy Tarreau | e115b49 | 2015-05-01 23:05:14 +0200 | [diff] [blame] | 4091 | 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] | 4092 | goto http_end; |
| 4093 | |
| 4094 | missing_data: |
Willy Tarreau | 31a1995 | 2014-04-10 11:50:37 +0200 | [diff] [blame] | 4095 | /* we get here if we need to wait for more data. If the buffer is full, |
| 4096 | * we have the maximum we can expect. |
| 4097 | */ |
| 4098 | if (buffer_full(req->buf, global.tune.maxrewrite)) |
| 4099 | goto http_end; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 4100 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4101 | 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] | 4102 | txn->status = 408; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 4103 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 4104 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4105 | if (!(s->flags & SF_ERR_MASK)) |
| 4106 | s->flags |= SF_ERR_CLITO; |
| 4107 | if (!(s->flags & SF_FINST_MASK)) |
| 4108 | s->flags |= SF_FINST_D; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4109 | goto return_err_msg; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4110 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4111 | |
| 4112 | /* we get here if we need to wait for more data */ |
Willy Tarreau | 31a1995 | 2014-04-10 11:50:37 +0200 | [diff] [blame] | 4113 | if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) { |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4114 | /* Not enough data. We'll re-use the http-request |
| 4115 | * timeout here. Ideally, we should set the timeout |
| 4116 | * relative to the accept() date. We just set the |
| 4117 | * request timeout once at the beginning of the |
| 4118 | * request. |
| 4119 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4120 | channel_dont_connect(req); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4121 | if (!tick_isset(req->analyse_exp)) |
Willy Tarreau | cd7afc0 | 2009-07-12 10:03:17 +0200 | [diff] [blame] | 4122 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4123 | return 0; |
| 4124 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4125 | |
| 4126 | http_end: |
| 4127 | /* The situation will not evolve, so let's give up on the analysis. */ |
| 4128 | s->logs.tv_request = now; /* update the request timer to reflect full request */ |
| 4129 | req->analysers &= ~an_bit; |
| 4130 | req->analyse_exp = TICK_ETERNITY; |
| 4131 | return 1; |
| 4132 | |
| 4133 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 4134 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4135 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4136 | txn->status = 400; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 4137 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4138 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4139 | if (!(s->flags & SF_ERR_MASK)) |
| 4140 | s->flags |= SF_ERR_PRXCOND; |
| 4141 | if (!(s->flags & SF_FINST_MASK)) |
| 4142 | s->flags |= SF_FINST_R; |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 4143 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4144 | return_err_msg: |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4145 | req->analysers &= AN_REQ_FLT_END; |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 4146 | HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4147 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 4148 | HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4149 | return 0; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4150 | } |
| 4151 | |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4152 | /* send a server's name with an outgoing request over an established connection. |
| 4153 | * Note: this function is designed to be called once the request has been scheduled |
| 4154 | * for being forwarded. This is the reason why it rewinds the buffer before |
| 4155 | * proceeding. |
| 4156 | */ |
Willy Tarreau | 45c0d98 | 2012-03-09 12:11:57 +0100 | [diff] [blame] | 4157 | 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] | 4158 | |
| 4159 | struct hdr_ctx ctx; |
| 4160 | |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4161 | char *hdr_name = be->server_id_hdr_name; |
| 4162 | int hdr_name_len = be->server_id_hdr_len; |
Willy Tarreau | 394db37 | 2012-10-12 22:40:39 +0200 | [diff] [blame] | 4163 | struct channel *chn = txn->req.chn; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4164 | char *hdr_val; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4165 | unsigned int old_o, old_i; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4166 | |
William Lallemand | d9e9066 | 2012-01-30 17:27:17 +0100 | [diff] [blame] | 4167 | ctx.idx = 0; |
| 4168 | |
Willy Tarreau | 211cdec | 2014-04-17 20:18:08 +0200 | [diff] [blame] | 4169 | old_o = http_hdr_rewind(&txn->req); |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4170 | if (old_o) { |
| 4171 | /* The request was already skipped, let's restore it */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4172 | b_rew(chn->buf, old_o); |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 4173 | txn->req.next += old_o; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 4174 | txn->req.sov += old_o; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4175 | } |
| 4176 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4177 | old_i = chn->buf->i; |
| 4178 | 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] | 4179 | /* remove any existing values from the header */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 4180 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4181 | } |
| 4182 | |
| 4183 | /* Add the new header requested with the server value */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4184 | hdr_val = trash.str; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4185 | memcpy(hdr_val, hdr_name, hdr_name_len); |
| 4186 | hdr_val += hdr_name_len; |
| 4187 | *hdr_val++ = ':'; |
| 4188 | *hdr_val++ = ' '; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4189 | hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val); |
| 4190 | 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] | 4191 | |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4192 | if (old_o) { |
| 4193 | /* If this was a forwarded request, we must readjust the amount of |
| 4194 | * data to be forwarded in order to take into account the size |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 4195 | * variations. Note that the current state is >= HTTP_MSG_BODY, |
| 4196 | * so we don't have to adjust ->sol. |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4197 | */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 4198 | old_o += chn->buf->i - old_i; |
| 4199 | b_adv(chn->buf, old_o); |
| 4200 | txn->req.next -= old_o; |
| 4201 | txn->req.sov -= old_o; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4202 | } |
| 4203 | |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4204 | return 0; |
| 4205 | } |
| 4206 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4207 | /* Terminate current transaction and prepare a new one. This is very tricky |
| 4208 | * right now but it works. |
| 4209 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4210 | void http_end_txn_clean_session(struct stream *s) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4211 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4212 | int prev_status = s->txn->status; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 4213 | struct proxy *fe = strm_fe(s); |
Willy Tarreau | 858b103 | 2015-12-07 17:04:59 +0100 | [diff] [blame] | 4214 | struct proxy *be = s->be; |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 4215 | struct conn_stream *cs; |
Willy Tarreau | 323a2d9 | 2015-08-04 19:00:17 +0200 | [diff] [blame] | 4216 | struct connection *srv_conn; |
| 4217 | struct server *srv; |
Willy Tarreau | 449d74a | 2015-08-05 17:16:33 +0200 | [diff] [blame] | 4218 | unsigned int prev_flags = s->txn->flags; |
Willy Tarreau | 068621e | 2013-12-23 15:11:25 +0100 | [diff] [blame] | 4219 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4220 | /* FIXME: We need a more portable way of releasing a backend's and a |
| 4221 | * server's connections. We need a safer way to reinitialize buffer |
| 4222 | * flags. We also need a more accurate method for computing per-request |
| 4223 | * data. |
| 4224 | */ |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 4225 | /* |
| 4226 | * XXX cognet: This is probably wrong, this is killing a whole |
| 4227 | * connection, in the new world order, we probably want to just kill |
| 4228 | * the stream, this is to be revisited the day we handle multiple |
| 4229 | * streams in one server connection. |
| 4230 | */ |
| 4231 | cs = objt_cs(s->si[1].end); |
| 4232 | srv_conn = cs_conn(cs); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4233 | |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4234 | /* unless we're doing keep-alive, we want to quickly close the connection |
| 4235 | * to the server. |
| 4236 | */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4237 | if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) || |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4238 | !si_conn_ready(&s->si[1])) { |
| 4239 | s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF; |
| 4240 | si_shutr(&s->si[1]); |
| 4241 | si_shutw(&s->si[1]); |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4242 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4243 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4244 | if (s->flags & SF_BE_ASSIGNED) { |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 4245 | HA_ATOMIC_SUB(&be->beconn, 1); |
Willy Tarreau | 2d5cd47 | 2012-03-01 23:34:37 +0100 | [diff] [blame] | 4246 | if (unlikely(s->srv_conn)) |
| 4247 | sess_change_server(s, NULL); |
| 4248 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4249 | |
| 4250 | s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4251 | stream_process_counters(s); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4252 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4253 | if (s->txn->status) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4254 | int n; |
| 4255 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4256 | n = s->txn->status / 100; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4257 | if (n < 1 || n > 5) |
| 4258 | n = 0; |
| 4259 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4260 | if (fe->mode == PR_MODE_HTTP) { |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 4261 | HA_ATOMIC_ADD(&fe->fe_counters.p.http.rsp[n], 1); |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 4262 | } |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4263 | if ((s->flags & SF_BE_ASSIGNED) && |
Willy Tarreau | 858b103 | 2015-12-07 17:04:59 +0100 | [diff] [blame] | 4264 | (be->mode == PR_MODE_HTTP)) { |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 4265 | HA_ATOMIC_ADD(&be->be_counters.p.http.rsp[n], 1); |
| 4266 | HA_ATOMIC_ADD(&be->be_counters.p.http.cum_req, 1); |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 4267 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4268 | } |
| 4269 | |
| 4270 | /* don't count other requests' data */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4271 | s->logs.bytes_in -= s->req.buf->i; |
| 4272 | s->logs.bytes_out -= s->res.buf->i; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4273 | |
| 4274 | /* let's do a final log if we need it */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4275 | if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait && |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4276 | !(s->flags & SF_MONITOR) && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4277 | (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4278 | s->do_log(s); |
| 4279 | } |
| 4280 | |
Willy Tarreau | d713bcc | 2014-06-25 15:36:04 +0200 | [diff] [blame] | 4281 | /* stop tracking content-based counters */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4282 | stream_stop_content_counters(s); |
| 4283 | stream_update_time_stats(s); |
Willy Tarreau | 4bfc580 | 2014-06-17 12:19:18 +0200 | [diff] [blame] | 4284 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4285 | s->logs.accept_date = date; /* user-visible date for logging */ |
| 4286 | s->logs.tv_accept = now; /* corrected date for internal use */ |
Thierry FOURNIER / OZON.IO | 4cac359 | 2016-07-28 17:19:45 +0200 | [diff] [blame] | 4287 | s->logs.t_handshake = 0; /* There are no handshake in keep alive connection. */ |
| 4288 | s->logs.t_idle = -1; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4289 | tv_zero(&s->logs.tv_request); |
| 4290 | s->logs.t_queue = -1; |
| 4291 | s->logs.t_connect = -1; |
| 4292 | s->logs.t_data = -1; |
| 4293 | s->logs.t_close = 0; |
| 4294 | s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */ |
| 4295 | s->logs.srv_queue_size = 0; /* we will get this number soon */ |
| 4296 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4297 | s->logs.bytes_in = s->req.total = s->req.buf->i; |
| 4298 | s->logs.bytes_out = s->res.total = s->res.buf->i; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4299 | |
| 4300 | if (s->pend_pos) |
| 4301 | pendconn_free(s->pend_pos); |
| 4302 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4303 | if (objt_server(s->target)) { |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4304 | if (s->flags & SF_CURR_SESS) { |
| 4305 | s->flags &= ~SF_CURR_SESS; |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 4306 | HA_ATOMIC_SUB(&objt_server(s->target)->cur_sess, 1); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4307 | } |
Willy Tarreau | 858b103 | 2015-12-07 17:04:59 +0100 | [diff] [blame] | 4308 | if (may_dequeue_tasks(objt_server(s->target), be)) |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4309 | process_srv_queue(objt_server(s->target)); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4310 | } |
| 4311 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4312 | s->target = NULL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4313 | |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4314 | /* only release our endpoint if we don't intend to reuse the |
| 4315 | * connection. |
| 4316 | */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4317 | if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) || |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4318 | !si_conn_ready(&s->si[1])) { |
| 4319 | si_release_endpoint(&s->si[1]); |
Willy Tarreau | 323a2d9 | 2015-08-04 19:00:17 +0200 | [diff] [blame] | 4320 | srv_conn = NULL; |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4321 | } |
| 4322 | |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4323 | s->si[1].state = s->si[1].prev_state = SI_ST_INI; |
| 4324 | s->si[1].err_type = SI_ET_NONE; |
| 4325 | s->si[1].conn_retries = 0; /* used for logging too */ |
| 4326 | s->si[1].exp = TICK_ETERNITY; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4327 | 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] | 4328 | 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); |
| 4329 | 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); |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4330 | s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST); |
| 4331 | s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED); |
| 4332 | s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP); |
Willy Tarreau | 543db62 | 2012-11-15 16:41:22 +0100 | [diff] [blame] | 4333 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4334 | s->txn->meth = 0; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4335 | http_reset_txn(s); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4336 | s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ; |
Willy Tarreau | 068621e | 2013-12-23 15:11:25 +0100 | [diff] [blame] | 4337 | |
| 4338 | if (prev_status == 401 || prev_status == 407) { |
| 4339 | /* In HTTP keep-alive mode, if we receive a 401, we still have |
| 4340 | * a chance of being able to send the visitor again to the same |
| 4341 | * server over the same connection. This is required by some |
| 4342 | * broken protocols such as NTLM, and anyway whenever there is |
| 4343 | * an opportunity for sending the challenge to the proper place, |
| 4344 | * it's better to do it (at least it helps with debugging). |
| 4345 | */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4346 | s->txn->flags |= TX_PREFER_LAST; |
Willy Tarreau | bd99d58 | 2015-09-02 10:40:43 +0200 | [diff] [blame] | 4347 | if (srv_conn) |
| 4348 | srv_conn->flags |= CO_FL_PRIVATE; |
Willy Tarreau | 068621e | 2013-12-23 15:11:25 +0100 | [diff] [blame] | 4349 | } |
| 4350 | |
Willy Tarreau | 53f9685 | 2016-02-02 18:50:47 +0100 | [diff] [blame] | 4351 | /* Never ever allow to reuse a connection from a non-reuse backend */ |
| 4352 | if (srv_conn && (be->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR) |
| 4353 | srv_conn->flags |= CO_FL_PRIVATE; |
| 4354 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4355 | if (fe->options2 & PR_O2_INDEPSTR) |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4356 | s->si[1].flags |= SI_FL_INDEP_STR; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4357 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4358 | if (fe->options2 & PR_O2_NODELAY) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4359 | s->req.flags |= CF_NEVER_WAIT; |
| 4360 | s->res.flags |= CF_NEVER_WAIT; |
Willy Tarreau | 96e3121 | 2011-05-30 18:10:30 +0200 | [diff] [blame] | 4361 | } |
| 4362 | |
Willy Tarreau | 714ea78 | 2015-11-25 20:11:11 +0100 | [diff] [blame] | 4363 | /* we're removing the analysers, we MUST re-enable events detection. |
| 4364 | * We don't enable close on the response channel since it's either |
| 4365 | * already closed, or in keep-alive with an idle connection handler. |
| 4366 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4367 | channel_auto_read(&s->req); |
| 4368 | channel_auto_close(&s->req); |
| 4369 | channel_auto_read(&s->res); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4370 | |
Willy Tarreau | 1c59bd5 | 2015-11-02 20:20:11 +0100 | [diff] [blame] | 4371 | /* we're in keep-alive with an idle connection, monitor it if not already done */ |
| 4372 | if (srv_conn && LIST_ISEMPTY(&srv_conn->list)) { |
Willy Tarreau | 323a2d9 | 2015-08-04 19:00:17 +0200 | [diff] [blame] | 4373 | srv = objt_server(srv_conn->target); |
Willy Tarreau | 8dff998 | 2015-08-04 20:45:52 +0200 | [diff] [blame] | 4374 | if (!srv) |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 4375 | si_idle_cs(&s->si[1], NULL); |
Willy Tarreau | 53f9685 | 2016-02-02 18:50:47 +0100 | [diff] [blame] | 4376 | else if (srv_conn->flags & CO_FL_PRIVATE) |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 4377 | 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] | 4378 | else if (prev_flags & TX_NOT_FIRST) |
| 4379 | /* note: we check the request, not the connection, but |
| 4380 | * this is valid for strategies SAFE and AGGR, and in |
| 4381 | * case of ALWS, we don't care anyway. |
| 4382 | */ |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 4383 | 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] | 4384 | else |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 4385 | 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] | 4386 | } |
Christopher Faulet | e600624 | 2017-03-10 11:52:44 +0100 | [diff] [blame] | 4387 | s->req.analysers = strm_li(s) ? strm_li(s)->analysers : 0; |
| 4388 | s->res.analysers = 0; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4389 | } |
| 4390 | |
| 4391 | |
| 4392 | /* This function updates the request state machine according to the response |
| 4393 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 4394 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 4395 | * it is only used to find when a request/response couple is complete. Both |
| 4396 | * this function and its equivalent should loop until both return zero. It |
| 4397 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 4398 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4399 | int http_sync_req_state(struct stream *s) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4400 | { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4401 | struct channel *chn = &s->req; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4402 | struct http_txn *txn = s->txn; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4403 | unsigned int old_flags = chn->flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4404 | unsigned int old_state = txn->req.msg_state; |
| 4405 | |
Christopher Faulet | 4be9803 | 2017-07-18 10:48:24 +0200 | [diff] [blame] | 4406 | if (unlikely(txn->req.msg_state < HTTP_MSG_DONE)) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4407 | return 0; |
| 4408 | |
| 4409 | if (txn->req.msg_state == HTTP_MSG_DONE) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4410 | /* No need to read anymore, the request was completely parsed. |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 4411 | * We can shut the read side unless we want to abort_on_close, |
| 4412 | * or we have a POST request. The issue with POST requests is |
| 4413 | * that some browsers still send a CRLF after the request, and |
| 4414 | * this CRLF must be read so that it does not remain in the kernel |
| 4415 | * buffers, otherwise a close could cause an RST on some systems |
| 4416 | * (eg: Linux). |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 4417 | * Note that if we're using keep-alive on the client side, we'd |
| 4418 | * rather poll now and keep the polling enabled for the whole |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4419 | * stream's life than enabling/disabling it between each |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 4420 | * response and next request. |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4421 | */ |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 4422 | if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) && |
| 4423 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) && |
| 4424 | !(s->be->options & PR_O_ABRT_CLOSE) && |
| 4425 | txn->meth != HTTP_METH_POST) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4426 | channel_dont_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4427 | |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 4428 | /* if the server closes the connection, we want to immediately react |
| 4429 | * and close the socket to save packets and syscalls. |
| 4430 | */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4431 | s->si[1].flags |= SI_FL_NOHALF; |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 4432 | |
Willy Tarreau | 7f876a1 | 2015-11-18 11:59:55 +0100 | [diff] [blame] | 4433 | /* In any case we've finished parsing the request so we must |
| 4434 | * disable Nagle when sending data because 1) we're not going |
| 4435 | * to shut this side, and 2) the server is waiting for us to |
| 4436 | * send pending data. |
| 4437 | */ |
| 4438 | chn->flags |= CF_NEVER_WAIT; |
| 4439 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4440 | if (txn->rsp.msg_state == HTTP_MSG_ERROR) |
| 4441 | goto wait_other_side; |
| 4442 | |
| 4443 | if (txn->rsp.msg_state < HTTP_MSG_DONE) { |
| 4444 | /* The server has not finished to respond, so we |
| 4445 | * don't want to move in order not to upset it. |
| 4446 | */ |
| 4447 | goto wait_other_side; |
| 4448 | } |
| 4449 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4450 | /* When we get here, it means that both the request and the |
| 4451 | * response have finished receiving. Depending on the connection |
| 4452 | * mode, we'll have to wait for the last bytes to leave in either |
| 4453 | * direction, and sometimes for a close to be effective. |
| 4454 | */ |
| 4455 | |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4456 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 4457 | /* Server-close mode : queue a connection close to the server */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4458 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) |
| 4459 | channel_shutw_now(chn); |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4460 | } |
| 4461 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 4462 | /* Option forceclose is set, or either side wants to close, |
| 4463 | * let's enforce it now that we're not expecting any new |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4464 | * data to come. The caller knows the stream is complete |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4465 | * once both states are CLOSED. |
Christopher Faulet | 1486b0a | 2017-07-18 11:42:08 +0200 | [diff] [blame] | 4466 | * |
| 4467 | * However, there is an exception if the response |
| 4468 | * length is undefined. In this case, we need to wait |
| 4469 | * the close from the server. The response will be |
| 4470 | * switched in TUNNEL mode until the end. |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4471 | */ |
Christopher Faulet | 1486b0a | 2017-07-18 11:42:08 +0200 | [diff] [blame] | 4472 | if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) && |
| 4473 | txn->rsp.msg_state != HTTP_MSG_CLOSED) |
| 4474 | goto check_channel_flags; |
| 4475 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4476 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) { |
| 4477 | channel_shutr_now(chn); |
| 4478 | channel_shutw_now(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4479 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4480 | } |
| 4481 | else { |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4482 | /* The last possible modes are keep-alive and tunnel. Tunnel mode |
| 4483 | * will not have any analyser so it needs to poll for reads. |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4484 | */ |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4485 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) { |
| 4486 | channel_auto_read(chn); |
| 4487 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4488 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4489 | } |
| 4490 | |
Christopher Faulet | 4be9803 | 2017-07-18 10:48:24 +0200 | [diff] [blame] | 4491 | goto check_channel_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4492 | } |
| 4493 | |
| 4494 | if (txn->req.msg_state == HTTP_MSG_CLOSING) { |
| 4495 | http_msg_closing: |
| 4496 | /* nothing else to forward, just waiting for the output buffer |
| 4497 | * to be empty and for the shutw_now to take effect. |
| 4498 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4499 | if (channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4500 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 4501 | goto http_msg_closed; |
| 4502 | } |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4503 | else if (chn->flags & CF_SHUTW) { |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 4504 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4505 | txn->req.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4506 | } |
Christopher Faulet | 56d2609 | 2017-07-20 11:05:10 +0200 | [diff] [blame] | 4507 | goto wait_other_side; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4508 | } |
| 4509 | |
| 4510 | if (txn->req.msg_state == HTTP_MSG_CLOSED) { |
| 4511 | http_msg_closed: |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 4512 | /* see above in MSG_DONE why we only do this in these states */ |
| 4513 | if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) && |
| 4514 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) && |
| 4515 | !(s->be->options & PR_O_ABRT_CLOSE)) |
Willy Tarreau | 2e7a165 | 2013-12-15 15:32:10 +0100 | [diff] [blame] | 4516 | channel_dont_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4517 | goto wait_other_side; |
| 4518 | } |
| 4519 | |
Christopher Faulet | 4be9803 | 2017-07-18 10:48:24 +0200 | [diff] [blame] | 4520 | check_channel_flags: |
| 4521 | /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */ |
| 4522 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) { |
| 4523 | /* if we've just closed an output, let's switch */ |
| 4524 | s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */ |
| 4525 | txn->req.msg_state = HTTP_MSG_CLOSING; |
| 4526 | goto http_msg_closing; |
| 4527 | } |
| 4528 | |
| 4529 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4530 | wait_other_side: |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4531 | return txn->req.msg_state != old_state || chn->flags != old_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4532 | } |
| 4533 | |
| 4534 | |
| 4535 | /* This function updates the response state machine according to the request |
| 4536 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 4537 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 4538 | * it is only used to find when a request/response couple is complete. Both |
| 4539 | * this function and its equivalent should loop until both return zero. It |
| 4540 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 4541 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4542 | int http_sync_res_state(struct stream *s) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4543 | { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4544 | struct channel *chn = &s->res; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4545 | struct http_txn *txn = s->txn; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4546 | unsigned int old_flags = chn->flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4547 | unsigned int old_state = txn->rsp.msg_state; |
| 4548 | |
Christopher Faulet | 4be9803 | 2017-07-18 10:48:24 +0200 | [diff] [blame] | 4549 | if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE)) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4550 | return 0; |
| 4551 | |
| 4552 | if (txn->rsp.msg_state == HTTP_MSG_DONE) { |
| 4553 | /* In theory, we don't need to read anymore, but we must |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4554 | * still monitor the server connection for a possible close |
| 4555 | * while the request is being uploaded, so we don't disable |
| 4556 | * reading. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4557 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4558 | /* channel_dont_read(chn); */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4559 | |
| 4560 | if (txn->req.msg_state == HTTP_MSG_ERROR) |
| 4561 | goto wait_other_side; |
| 4562 | |
| 4563 | if (txn->req.msg_state < HTTP_MSG_DONE) { |
| 4564 | /* The client seems to still be sending data, probably |
| 4565 | * because we got an error response during an upload. |
| 4566 | * We have the choice of either breaking the connection |
| 4567 | * or letting it pass through. Let's do the later. |
| 4568 | */ |
| 4569 | goto wait_other_side; |
| 4570 | } |
| 4571 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4572 | /* When we get here, it means that both the request and the |
| 4573 | * response have finished receiving. Depending on the connection |
| 4574 | * mode, we'll have to wait for the last bytes to leave in either |
| 4575 | * direction, and sometimes for a close to be effective. |
| 4576 | */ |
| 4577 | |
| 4578 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 4579 | /* Server-close mode : shut read and wait for the request |
| 4580 | * side to close its output buffer. The caller will detect |
| 4581 | * when we're in DONE and the other is in CLOSED and will |
| 4582 | * catch that for the final cleanup. |
| 4583 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4584 | if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW))) |
| 4585 | channel_shutr_now(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4586 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4587 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 4588 | /* Option forceclose is set, or either side wants to close, |
| 4589 | * let's enforce it now that we're not expecting any new |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4590 | * data to come. The caller knows the stream is complete |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4591 | * once both states are CLOSED. |
Christopher Faulet | 1486b0a | 2017-07-18 11:42:08 +0200 | [diff] [blame] | 4592 | * |
| 4593 | * However, there is an exception if the response length |
| 4594 | * is undefined. In this case, we switch in TUNNEL mode. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4595 | */ |
Christopher Faulet | 1486b0a | 2017-07-18 11:42:08 +0200 | [diff] [blame] | 4596 | if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN)) { |
| 4597 | channel_auto_read(chn); |
| 4598 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
| 4599 | chn->flags |= CF_NEVER_WAIT; |
| 4600 | } |
| 4601 | else if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4602 | channel_shutr_now(chn); |
| 4603 | channel_shutw_now(chn); |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4604 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4605 | } |
| 4606 | else { |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4607 | /* The last possible modes are keep-alive and tunnel. Tunnel will |
| 4608 | * need to forward remaining data. Keep-alive will need to monitor |
| 4609 | * for connection closing. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4610 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4611 | channel_auto_read(chn); |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 4612 | chn->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4613 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) |
| 4614 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4615 | } |
| 4616 | |
Christopher Faulet | 4be9803 | 2017-07-18 10:48:24 +0200 | [diff] [blame] | 4617 | goto check_channel_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4618 | } |
| 4619 | |
| 4620 | if (txn->rsp.msg_state == HTTP_MSG_CLOSING) { |
| 4621 | http_msg_closing: |
| 4622 | /* nothing else to forward, just waiting for the output buffer |
| 4623 | * to be empty and for the shutw_now to take effect. |
| 4624 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4625 | if (channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4626 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 4627 | goto http_msg_closed; |
| 4628 | } |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4629 | else if (chn->flags & CF_SHUTW) { |
Christopher Faulet | a3992e0 | 2017-07-18 10:35:55 +0200 | [diff] [blame] | 4630 | txn->rsp.err_state = txn->rsp.msg_state; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4631 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 4632 | HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4633 | if (objt_server(s->target)) |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 4634 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4635 | } |
Christopher Faulet | 56d2609 | 2017-07-20 11:05:10 +0200 | [diff] [blame] | 4636 | goto wait_other_side; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4637 | } |
| 4638 | |
| 4639 | if (txn->rsp.msg_state == HTTP_MSG_CLOSED) { |
| 4640 | http_msg_closed: |
| 4641 | /* drop any pending data */ |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 4642 | channel_truncate(chn); |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4643 | channel_auto_close(chn); |
| 4644 | channel_auto_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4645 | goto wait_other_side; |
| 4646 | } |
| 4647 | |
Christopher Faulet | 4be9803 | 2017-07-18 10:48:24 +0200 | [diff] [blame] | 4648 | check_channel_flags: |
| 4649 | /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */ |
| 4650 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) { |
| 4651 | /* if we've just closed an output, let's switch */ |
| 4652 | txn->rsp.msg_state = HTTP_MSG_CLOSING; |
| 4653 | goto http_msg_closing; |
| 4654 | } |
| 4655 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4656 | wait_other_side: |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 4657 | /* We force the response to leave immediately if we're waiting for the |
| 4658 | * other side, since there is no pending shutdown to push it out. |
| 4659 | */ |
| 4660 | if (!channel_is_empty(chn)) |
| 4661 | chn->flags |= CF_SEND_DONTWAIT; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4662 | return txn->rsp.msg_state != old_state || chn->flags != old_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4663 | } |
| 4664 | |
| 4665 | |
Christopher Faulet | 894da4c | 2017-07-18 11:29:07 +0200 | [diff] [blame] | 4666 | /* Resync the request and response state machines. */ |
| 4667 | void http_resync_states(struct stream *s) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4668 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4669 | struct http_txn *txn = s->txn; |
Christopher Faulet | 894da4c | 2017-07-18 11:29:07 +0200 | [diff] [blame] | 4670 | #ifdef DEBUG_FULL |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4671 | int old_req_state = txn->req.msg_state; |
| 4672 | int old_res_state = txn->rsp.msg_state; |
Christopher Faulet | 894da4c | 2017-07-18 11:29:07 +0200 | [diff] [blame] | 4673 | #endif |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4674 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4675 | http_sync_req_state(s); |
| 4676 | while (1) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4677 | if (!http_sync_res_state(s)) |
| 4678 | break; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4679 | if (!http_sync_req_state(s)) |
| 4680 | break; |
| 4681 | } |
Willy Tarreau | 3ce10ff | 2014-04-22 08:24:38 +0200 | [diff] [blame] | 4682 | |
Christopher Faulet | 894da4c | 2017-07-18 11:29:07 +0200 | [diff] [blame] | 4683 | DPRINTF(stderr,"[%u] %s: stream=%p old=%s,%s cur=%s,%s " |
| 4684 | "req->analysers=0x%08x res->analysers=0x%08x\n", |
| 4685 | now_ms, __FUNCTION__, s, |
Willy Tarreau | 0da5b3b | 2017-09-21 09:30:46 +0200 | [diff] [blame] | 4686 | h1_msg_state_str(old_req_state), h1_msg_state_str(old_res_state), |
| 4687 | 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] | 4688 | s->req.analysers, s->res.analysers); |
Christopher Faulet | 814d270 | 2017-03-30 11:33:44 +0200 | [diff] [blame] | 4689 | |
| 4690 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4691 | /* OK, both state machines agree on a compatible state. |
| 4692 | * There are a few cases we're interested in : |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4693 | * - HTTP_MSG_CLOSED on both sides means we've reached the end in both |
| 4694 | * directions, so let's simply disable both analysers. |
Christopher Faulet | f77bb53 | 2017-07-18 11:18:46 +0200 | [diff] [blame] | 4695 | * - HTTP_MSG_CLOSED on the response only or HTTP_MSG_ERROR on either |
| 4696 | * means we must abort the request. |
| 4697 | * - HTTP_MSG_TUNNEL on either means we have to disable analyser on |
| 4698 | * corresponding channel. |
| 4699 | * - HTTP_MSG_DONE or HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE |
| 4700 | * on the response with server-close mode means we've completed one |
| 4701 | * request and we must re-initialize the server connection. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4702 | */ |
Christopher Faulet | f77bb53 | 2017-07-18 11:18:46 +0200 | [diff] [blame] | 4703 | if (txn->req.msg_state == HTTP_MSG_CLOSED && |
| 4704 | txn->rsp.msg_state == HTTP_MSG_CLOSED) { |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4705 | s->req.analysers &= AN_REQ_FLT_END; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4706 | channel_auto_close(&s->req); |
| 4707 | channel_auto_read(&s->req); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4708 | s->res.analysers &= AN_RES_FLT_END; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4709 | channel_auto_close(&s->res); |
| 4710 | channel_auto_read(&s->res); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4711 | } |
Christopher Faulet | f77bb53 | 2017-07-18 11:18:46 +0200 | [diff] [blame] | 4712 | else if (txn->rsp.msg_state == HTTP_MSG_CLOSED || |
| 4713 | txn->rsp.msg_state == HTTP_MSG_ERROR || |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 4714 | txn->req.msg_state == HTTP_MSG_ERROR) { |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4715 | s->res.analysers &= AN_RES_FLT_END; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4716 | channel_auto_close(&s->res); |
| 4717 | channel_auto_read(&s->res); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4718 | s->req.analysers &= AN_REQ_FLT_END; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4719 | channel_abort(&s->req); |
| 4720 | channel_auto_close(&s->req); |
| 4721 | channel_auto_read(&s->req); |
| 4722 | channel_truncate(&s->req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4723 | } |
Christopher Faulet | f77bb53 | 2017-07-18 11:18:46 +0200 | [diff] [blame] | 4724 | else if (txn->req.msg_state == HTTP_MSG_TUNNEL || |
| 4725 | txn->rsp.msg_state == HTTP_MSG_TUNNEL) { |
| 4726 | if (txn->req.msg_state == HTTP_MSG_TUNNEL) { |
| 4727 | s->req.analysers &= AN_REQ_FLT_END; |
| 4728 | if (HAS_REQ_DATA_FILTERS(s)) |
| 4729 | s->req.analysers |= AN_REQ_FLT_XFER_DATA; |
| 4730 | } |
| 4731 | if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) { |
| 4732 | s->res.analysers &= AN_RES_FLT_END; |
| 4733 | if (HAS_RSP_DATA_FILTERS(s)) |
| 4734 | s->res.analysers |= AN_RES_FLT_XFER_DATA; |
| 4735 | } |
| 4736 | channel_auto_close(&s->req); |
| 4737 | channel_auto_read(&s->req); |
| 4738 | channel_auto_close(&s->res); |
| 4739 | channel_auto_read(&s->res); |
| 4740 | } |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4741 | else if ((txn->req.msg_state == HTTP_MSG_DONE || |
| 4742 | txn->req.msg_state == HTTP_MSG_CLOSED) && |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4743 | txn->rsp.msg_state == HTTP_MSG_DONE && |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4744 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL || |
| 4745 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) { |
| 4746 | /* server-close/keep-alive: terminate this transaction, |
| 4747 | * possibly killing the server connection and reinitialize |
Willy Tarreau | 3de5bd6 | 2016-05-02 16:07:07 +0200 | [diff] [blame] | 4748 | * a fresh-new transaction, but only once we're sure there's |
| 4749 | * enough room in the request and response buffer to process |
Christopher Faulet | c0c672a | 2017-03-28 11:51:33 +0200 | [diff] [blame] | 4750 | * another request. They must not hold any pending output data |
| 4751 | * and the response buffer must realigned |
| 4752 | * (realign is done is http_end_txn_clean_session). |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4753 | */ |
Christopher Faulet | 894da4c | 2017-07-18 11:29:07 +0200 | [diff] [blame] | 4754 | if (s->req.buf->o) |
Willy Tarreau | 3de5bd6 | 2016-05-02 16:07:07 +0200 | [diff] [blame] | 4755 | s->req.flags |= CF_WAKE_WRITE; |
Christopher Faulet | 894da4c | 2017-07-18 11:29:07 +0200 | [diff] [blame] | 4756 | else if (s->res.buf->o) |
Willy Tarreau | 3de5bd6 | 2016-05-02 16:07:07 +0200 | [diff] [blame] | 4757 | s->res.flags |= CF_WAKE_WRITE; |
Christopher Faulet | a81ff60 | 2017-07-18 22:01:05 +0200 | [diff] [blame] | 4758 | else { |
| 4759 | s->req.analysers = AN_REQ_FLT_END; |
| 4760 | s->res.analysers = AN_RES_FLT_END; |
| 4761 | txn->flags |= TX_WAIT_CLEANUP; |
| 4762 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4763 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4764 | } |
| 4765 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4766 | /* This function is an analyser which forwards request body (including chunk |
| 4767 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 4768 | * zero byte. The only situation where it must not be called is when we're in |
| 4769 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 4770 | * remaining data and to resync after end of body. It expects the msg_state to |
| 4771 | * 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] | 4772 | * 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] | 4773 | * 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] | 4774 | * bytes of pending data + the headers if not already done. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4775 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4776 | 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] | 4777 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4778 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4779 | struct http_txn *txn = s->txn; |
| 4780 | struct http_msg *msg = &s->txn->req; |
Christopher Faulet | 3e34429 | 2015-11-24 16:24:13 +0100 | [diff] [blame] | 4781 | int ret; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4782 | |
Christopher Faulet | 814d270 | 2017-03-30 11:33:44 +0200 | [diff] [blame] | 4783 | DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n", |
| 4784 | now_ms, __FUNCTION__, |
| 4785 | s, |
| 4786 | req, |
| 4787 | req->rex, req->wex, |
| 4788 | req->flags, |
| 4789 | req->buf->i, |
| 4790 | req->analysers); |
| 4791 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4792 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4793 | return 0; |
| 4794 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4795 | 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] | 4796 | ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) { |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 4797 | /* Output closed while we were sending data. We must abort and |
| 4798 | * wake the other side up. |
| 4799 | */ |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 4800 | msg->err_state = msg->msg_state; |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 4801 | msg->msg_state = HTTP_MSG_ERROR; |
| 4802 | http_resync_states(s); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 4803 | return 1; |
| 4804 | } |
| 4805 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4806 | /* Note that we don't have to send 100-continue back because we don't |
| 4807 | * need the data to complete our job, and it's up to the server to |
| 4808 | * decide whether to return 100, 417 or anything else in return of |
| 4809 | * an "Expect: 100-continue" header. |
| 4810 | */ |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 4811 | if (msg->msg_state == HTTP_MSG_BODY) { |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4812 | msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK) |
| 4813 | ? HTTP_MSG_CHUNK_SIZE |
| 4814 | : HTTP_MSG_DATA); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 4815 | |
| 4816 | /* TODO/filters: when http-buffer-request option is set or if a |
| 4817 | * rule on url_param exists, the first chunk size could be |
| 4818 | * already parsed. In that case, msg->next is after the chunk |
| 4819 | * size (including the CRLF after the size). So this case should |
| 4820 | * be handled to */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4821 | } |
| 4822 | |
Willy Tarreau | 7ba2354 | 2014-04-17 21:50:00 +0200 | [diff] [blame] | 4823 | /* Some post-connect processing might want us to refrain from starting to |
| 4824 | * forward data. Currently, the only reason for this is "balance url_param" |
| 4825 | * whichs need to parse/process the request after we've enabled forwarding. |
| 4826 | */ |
| 4827 | if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4828 | if (!(s->res.flags & CF_READ_ATTACHED)) { |
Willy Tarreau | 7ba2354 | 2014-04-17 21:50:00 +0200 | [diff] [blame] | 4829 | channel_auto_connect(req); |
Willy Tarreau | 644c101 | 2014-04-30 18:11:11 +0200 | [diff] [blame] | 4830 | req->flags |= CF_WAKE_CONNECT; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4831 | goto missing_data_or_waiting; |
Willy Tarreau | 7ba2354 | 2014-04-17 21:50:00 +0200 | [diff] [blame] | 4832 | } |
| 4833 | msg->flags &= ~HTTP_MSGF_WAIT_CONN; |
| 4834 | } |
| 4835 | |
Willy Tarreau | 80a92c0 | 2014-03-12 10:41:13 +0100 | [diff] [blame] | 4836 | /* in most states, we should abort in case of early close */ |
| 4837 | channel_auto_close(req); |
| 4838 | |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 4839 | if (req->to_forward) { |
| 4840 | /* We can't process the buffer's contents yet */ |
| 4841 | req->flags |= CF_WAKE_WRITE; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4842 | goto missing_data_or_waiting; |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 4843 | } |
| 4844 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4845 | if (msg->msg_state < HTTP_MSG_DONE) { |
| 4846 | ret = ((msg->flags & HTTP_MSGF_TE_CHNK) |
| 4847 | ? http_msg_forward_chunked_body(s, msg) |
| 4848 | : http_msg_forward_body(s, msg)); |
| 4849 | if (!ret) |
| 4850 | goto missing_data_or_waiting; |
| 4851 | if (ret < 0) |
| 4852 | goto return_bad_req; |
| 4853 | } |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 4854 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4855 | /* other states, DONE...TUNNEL */ |
| 4856 | /* we don't want to forward closes on DONE except in tunnel mode. */ |
| 4857 | if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) |
| 4858 | channel_dont_close(req); |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 4859 | |
Christopher Faulet | 894da4c | 2017-07-18 11:29:07 +0200 | [diff] [blame] | 4860 | http_resync_states(s); |
| 4861 | if (!(req->analysers & an_bit)) { |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4862 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 4863 | if (req->flags & CF_SHUTW) { |
| 4864 | /* request errors are most likely due to the |
| 4865 | * server aborting the transfer. */ |
| 4866 | goto aborted_xfer; |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 4867 | } |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4868 | if (msg->err_pos >= 0) |
Emeric Brun | 8c1aaa2 | 2017-06-15 11:30:06 +0200 | [diff] [blame] | 4869 | 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] | 4870 | goto return_bad_req; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4871 | } |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4872 | return 1; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4873 | } |
| 4874 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4875 | /* If "option abortonclose" is set on the backend, we want to monitor |
| 4876 | * the client's connection and forward any shutdown notification to the |
| 4877 | * server, which will decide whether to close or to go on processing the |
| 4878 | * request. We only do that in tunnel mode, and not in other modes since |
| 4879 | * it can be abused to exhaust source ports. */ |
| 4880 | if (s->be->options & PR_O_ABRT_CLOSE) { |
| 4881 | channel_auto_read(req); |
| 4882 | if ((req->flags & (CF_SHUTR|CF_READ_NULL)) && |
| 4883 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)) |
| 4884 | s->si[1].flags |= SI_FL_NOLINGER; |
| 4885 | channel_auto_close(req); |
| 4886 | } |
| 4887 | else if (s->txn->meth == HTTP_METH_POST) { |
| 4888 | /* POST requests may require to read extra CRLF sent by broken |
| 4889 | * browsers and which could cause an RST to be sent upon close |
| 4890 | * on some systems (eg: Linux). */ |
| 4891 | channel_auto_read(req); |
| 4892 | } |
| 4893 | return 0; |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 4894 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4895 | missing_data_or_waiting: |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4896 | /* stop waiting for data if the input is closed before the end */ |
Christopher Faulet | a33510b | 2017-03-31 15:37:29 +0200 | [diff] [blame] | 4897 | if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR) { |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4898 | if (!(s->flags & SF_ERR_MASK)) |
| 4899 | s->flags |= SF_ERR_CLICL; |
| 4900 | if (!(s->flags & SF_FINST_MASK)) { |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4901 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4902 | s->flags |= SF_FINST_H; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4903 | else |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4904 | s->flags |= SF_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4905 | } |
| 4906 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 4907 | HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1); |
| 4908 | HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4909 | if (objt_server(s->target)) |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 4910 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4911 | |
| 4912 | goto return_bad_req_stats_ok; |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 4913 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4914 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4915 | /* waiting for the last bits to leave the buffer */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4916 | if (req->flags & CF_SHUTW) |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4917 | goto aborted_xfer; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4918 | |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 4919 | /* 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] | 4920 | * 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] | 4921 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4922 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4923 | channel_dont_close(req); |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 4924 | |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 4925 | /* 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] | 4926 | * 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] | 4927 | * 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] | 4928 | * modes are already handled by the stream sock layer. We must not do |
| 4929 | * this in content-length mode because it could present the MSG_MORE |
| 4930 | * flag with the last block of forwarded data, which would cause an |
| 4931 | * additional delay to be observed by the receiver. |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 4932 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4933 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4934 | req->flags |= CF_EXPECT_MORE; |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 4935 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4936 | return 0; |
| 4937 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4938 | return_bad_req: /* let's centralize all bad requests */ |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 4939 | HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4940 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 4941 | HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1); |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 4942 | |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4943 | return_bad_req_stats_ok: |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 4944 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4945 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4946 | if (txn->status) { |
| 4947 | /* 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] | 4948 | http_reply_and_close(s, txn->status, NULL); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4949 | } else { |
| 4950 | txn->status = 400; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 4951 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4952 | } |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4953 | req->analysers &= AN_REQ_FLT_END; |
| 4954 | 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] | 4955 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4956 | if (!(s->flags & SF_ERR_MASK)) |
| 4957 | s->flags |= SF_ERR_PRXCOND; |
| 4958 | if (!(s->flags & SF_FINST_MASK)) { |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4959 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4960 | s->flags |= SF_FINST_H; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4961 | else |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4962 | s->flags |= SF_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4963 | } |
| 4964 | return 0; |
| 4965 | |
| 4966 | aborted_xfer: |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 4967 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4968 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4969 | if (txn->status) { |
| 4970 | /* 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] | 4971 | http_reply_and_close(s, txn->status, NULL); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4972 | } else { |
| 4973 | txn->status = 502; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 4974 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4975 | } |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4976 | req->analysers &= AN_REQ_FLT_END; |
| 4977 | 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] | 4978 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 4979 | HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1); |
| 4980 | HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4981 | if (objt_server(s->target)) |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 4982 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4983 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4984 | if (!(s->flags & SF_ERR_MASK)) |
| 4985 | s->flags |= SF_ERR_SRVCL; |
| 4986 | if (!(s->flags & SF_FINST_MASK)) { |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4987 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4988 | s->flags |= SF_FINST_H; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4989 | else |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4990 | s->flags |= SF_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4991 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4992 | return 0; |
| 4993 | } |
| 4994 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4995 | /* This stream analyser waits for a complete HTTP response. It returns 1 if the |
| 4996 | * processing can continue on next analysers, or zero if it either needs more |
| 4997 | * data or wants to immediately abort the response (eg: timeout, error, ...). It |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4998 | * 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] | 4999 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 5000 | * abort. |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 5001 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5002 | 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] | 5003 | { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5004 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5005 | struct http_txn *txn = s->txn; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5006 | struct http_msg *msg = &txn->rsp; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5007 | struct hdr_ctx ctx; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5008 | int use_close_only; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5009 | int cur_idx; |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 5010 | int n; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 5011 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5012 | 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] | 5013 | now_ms, __FUNCTION__, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5014 | s, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 5015 | rep, |
| 5016 | rep->rex, rep->wex, |
| 5017 | rep->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5018 | rep->buf->i, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 5019 | rep->analysers); |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 5020 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5021 | /* |
| 5022 | * Now parse the partial (or complete) lines. |
| 5023 | * We will check the response syntax, and also join multi-line |
| 5024 | * headers. An index of all the lines will be elaborated while |
| 5025 | * parsing. |
| 5026 | * |
| 5027 | * For the parsing, we use a 28 states FSM. |
| 5028 | * |
| 5029 | * Here is the information we currently have : |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5030 | * rep->buf->p = beginning of response |
| 5031 | * rep->buf->p + msg->eoh = end of processed headers / start of current one |
| 5032 | * rep->buf->p + rep->buf->i = end of input data |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 5033 | * msg->eol = end of current header or line (LF or CRLF) |
| 5034 | * msg->next = first non-visited byte |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5035 | */ |
| 5036 | |
Willy Tarreau | 628c40c | 2014-04-24 19:11:26 +0200 | [diff] [blame] | 5037 | next_one: |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 5038 | /* There's a protected area at the end of the buffer for rewriting |
| 5039 | * purposes. We don't want to start to parse the request if the |
| 5040 | * protected area is affected, because we may have to move processed |
| 5041 | * data later, which is much more complicated. |
| 5042 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5043 | if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) { |
Willy Tarreau | ba0902e | 2015-01-13 14:39:16 +0100 | [diff] [blame] | 5044 | if (unlikely(!channel_is_rewritable(rep))) { |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 5045 | /* some data has still not left the buffer, wake us once that's done */ |
| 5046 | if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) |
| 5047 | goto abort_response; |
| 5048 | channel_dont_close(rep); |
| 5049 | rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | d7ad9f5 | 2013-12-31 17:26:25 +0100 | [diff] [blame] | 5050 | rep->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 5051 | return 0; |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 5052 | } |
| 5053 | |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 5054 | if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) || |
| 5055 | bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite)) |
| 5056 | buffer_slow_realign(rep->buf); |
| 5057 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5058 | if (likely(msg->next < rep->buf->i)) |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 5059 | http_msg_analyzer(msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 5060 | } |
| 5061 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5062 | /* 1: we might have to print this header in debug mode */ |
| 5063 | if (unlikely((global.mode & MODE_DEBUG) && |
| 5064 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | 7d59e90 | 2014-10-21 19:36:09 +0200 | [diff] [blame] | 5065 | msg->msg_state >= HTTP_MSG_BODY)) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5066 | char *eol, *sol; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5067 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5068 | sol = rep->buf->p; |
| 5069 | 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] | 5070 | debug_hdr("srvrep", s, sol, eol); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5071 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5072 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 5073 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5074 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5075 | while (cur_idx) { |
| 5076 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 5077 | debug_hdr("srvhdr", s, sol, eol); |
| 5078 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 5079 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
| 5080 | } |
| 5081 | } |
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 | /* |
| 5084 | * Now we quickly check if we have found a full valid response. |
| 5085 | * If not so, we check the FD and buffer states before leaving. |
| 5086 | * A full response is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 5087 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5088 | * responses are checked first. |
| 5089 | * |
| 5090 | * Depending on whether the client is still there or not, we |
| 5091 | * may send an error response back or not. Note that normally |
| 5092 | * we should only check for HTTP status there, and check I/O |
| 5093 | * errors somewhere else. |
| 5094 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5095 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 5096 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5097 | /* Invalid response */ |
| 5098 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 5099 | /* we detected a parsing error. We want to archive this response |
| 5100 | * in the dedicated proxy area for later troubleshooting. |
| 5101 | */ |
| 5102 | hdr_response_bad: |
| 5103 | if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0) |
Emeric Brun | 8c1aaa2 | 2017-06-15 11:30:06 +0200 | [diff] [blame] | 5104 | 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] | 5105 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 5106 | HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5107 | if (objt_server(s->target)) { |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 5108 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5109 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5110 | } |
Willy Tarreau | 6464841 | 2010-03-05 10:41:54 +0100 | [diff] [blame] | 5111 | abort_response: |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5112 | channel_auto_close(rep); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 5113 | rep->analysers &= AN_RES_FLT_END; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5114 | txn->status = 502; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5115 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5116 | channel_truncate(rep); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 5117 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5118 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5119 | if (!(s->flags & SF_ERR_MASK)) |
| 5120 | s->flags |= SF_ERR_PRXCOND; |
| 5121 | if (!(s->flags & SF_FINST_MASK)) |
| 5122 | s->flags |= SF_FINST_H; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5123 | |
| 5124 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5125 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5126 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5127 | /* too large response does not fit in buffer. */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5128 | else if (buffer_full(rep->buf, global.tune.maxrewrite)) { |
Willy Tarreau | fec4d89 | 2011-09-02 20:04:57 +0200 | [diff] [blame] | 5129 | if (msg->err_pos < 0) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5130 | msg->err_pos = rep->buf->i; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5131 | goto hdr_response_bad; |
| 5132 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5133 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5134 | /* read error */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5135 | else if (rep->flags & CF_READ_ERROR) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5136 | if (msg->err_pos >= 0) |
Emeric Brun | 8c1aaa2 | 2017-06-15 11:30:06 +0200 | [diff] [blame] | 5137 | 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] | 5138 | else if (txn->flags & TX_NOT_FIRST) |
| 5139 | goto abort_keep_alive; |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 5140 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 5141 | HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5142 | if (objt_server(s->target)) { |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 5143 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5144 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5145 | } |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 5146 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5147 | channel_auto_close(rep); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 5148 | rep->analysers &= AN_RES_FLT_END; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5149 | txn->status = 502; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5150 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5151 | channel_truncate(rep); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 5152 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 5153 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5154 | if (!(s->flags & SF_ERR_MASK)) |
| 5155 | s->flags |= SF_ERR_SRVCL; |
| 5156 | if (!(s->flags & SF_FINST_MASK)) |
| 5157 | s->flags |= SF_FINST_H; |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 5158 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5159 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5160 | |
Willy Tarreau | 6f0a7ba | 2014-06-23 15:22:31 +0200 | [diff] [blame] | 5161 | /* read timeout : return a 504 to the client. */ |
| 5162 | else if (rep->flags & CF_READ_TIMEOUT) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5163 | if (msg->err_pos >= 0) |
Emeric Brun | 8c1aaa2 | 2017-06-15 11:30:06 +0200 | [diff] [blame] | 5164 | 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] | 5165 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 5166 | HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5167 | if (objt_server(s->target)) { |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 5168 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5169 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5170 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5171 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5172 | channel_auto_close(rep); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 5173 | rep->analysers &= AN_RES_FLT_END; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5174 | txn->status = 504; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5175 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5176 | channel_truncate(rep); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 5177 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 5178 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5179 | if (!(s->flags & SF_ERR_MASK)) |
| 5180 | s->flags |= SF_ERR_SRVTO; |
| 5181 | if (!(s->flags & SF_FINST_MASK)) |
| 5182 | s->flags |= SF_FINST_H; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5183 | return 0; |
| 5184 | } |
Willy Tarreau | a7c5276 | 2008-08-16 18:40:18 +0200 | [diff] [blame] | 5185 | |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 5186 | /* client abort with an abortonclose */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5187 | 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] | 5188 | HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1); |
| 5189 | HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1); |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 5190 | if (objt_server(s->target)) |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 5191 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1); |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 5192 | |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 5193 | rep->analysers &= AN_RES_FLT_END; |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 5194 | channel_auto_close(rep); |
| 5195 | |
| 5196 | txn->status = 400; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5197 | channel_truncate(rep); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 5198 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 5199 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5200 | if (!(s->flags & SF_ERR_MASK)) |
| 5201 | s->flags |= SF_ERR_CLICL; |
| 5202 | if (!(s->flags & SF_FINST_MASK)) |
| 5203 | s->flags |= SF_FINST_H; |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 5204 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5205 | /* process_stream() will take care of the error */ |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 5206 | return 0; |
| 5207 | } |
| 5208 | |
Willy Tarreau | 3b8c08a | 2011-09-02 20:16:24 +0200 | [diff] [blame] | 5209 | /* 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] | 5210 | else if (rep->flags & CF_SHUTR) { |
Willy Tarreau | 3b8c08a | 2011-09-02 20:16:24 +0200 | [diff] [blame] | 5211 | if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0) |
Emeric Brun | 8c1aaa2 | 2017-06-15 11:30:06 +0200 | [diff] [blame] | 5212 | 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] | 5213 | else if (txn->flags & TX_NOT_FIRST) |
| 5214 | goto abort_keep_alive; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5215 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 5216 | HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5217 | if (objt_server(s->target)) { |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 5218 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5219 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5220 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5221 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5222 | channel_auto_close(rep); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 5223 | rep->analysers &= AN_RES_FLT_END; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5224 | txn->status = 502; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5225 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5226 | channel_truncate(rep); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 5227 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5228 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5229 | if (!(s->flags & SF_ERR_MASK)) |
| 5230 | s->flags |= SF_ERR_SRVCL; |
| 5231 | if (!(s->flags & SF_FINST_MASK)) |
| 5232 | s->flags |= SF_FINST_H; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5233 | return 0; |
| 5234 | } |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 5235 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5236 | /* write error to client (we don't send any message then) */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5237 | else if (rep->flags & CF_WRITE_ERROR) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5238 | if (msg->err_pos >= 0) |
Emeric Brun | 8c1aaa2 | 2017-06-15 11:30:06 +0200 | [diff] [blame] | 5239 | 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] | 5240 | else if (txn->flags & TX_NOT_FIRST) |
| 5241 | goto abort_keep_alive; |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 5242 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 5243 | HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 5244 | rep->analysers &= AN_RES_FLT_END; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5245 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5246 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5247 | if (!(s->flags & SF_ERR_MASK)) |
| 5248 | s->flags |= SF_ERR_CLICL; |
| 5249 | if (!(s->flags & SF_FINST_MASK)) |
| 5250 | s->flags |= SF_FINST_H; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5251 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5252 | /* process_stream() will take care of the error */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5253 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5254 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5255 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5256 | channel_dont_close(rep); |
Willy Tarreau | 3f3997e | 2013-12-15 15:21:32 +0100 | [diff] [blame] | 5257 | rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5258 | return 0; |
| 5259 | } |
| 5260 | |
| 5261 | /* More interesting part now : we know that we have a complete |
| 5262 | * response which at least looks like HTTP. We have an indicator |
| 5263 | * of each header's length, so we can parse them quickly. |
| 5264 | */ |
| 5265 | |
| 5266 | if (unlikely(msg->err_pos >= 0)) |
Emeric Brun | 8c1aaa2 | 2017-06-15 11:30:06 +0200 | [diff] [blame] | 5267 | 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] | 5268 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5269 | /* |
| 5270 | * 1: get the status code |
| 5271 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5272 | n = rep->buf->p[msg->sl.st.c] - '0'; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5273 | if (n < 1 || n > 5) |
| 5274 | n = 0; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5275 | /* when the client triggers a 4xx from the server, it's most often due |
| 5276 | * to a missing object or permission. These events should be tracked |
| 5277 | * because if they happen often, it may indicate a brute force or a |
| 5278 | * vulnerability scan. |
| 5279 | */ |
| 5280 | if (n == 4) |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5281 | stream_inc_http_err_ctr(s); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5282 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5283 | if (objt_server(s->target)) |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 5284 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.p.http.rsp[n], 1); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5285 | |
Willy Tarreau | 91852eb | 2015-05-01 13:26:00 +0200 | [diff] [blame] | 5286 | /* RFC7230#2.6 has enforced the format of the HTTP version string to be |
| 5287 | * exactly one digit "." one digit. This check may be disabled using |
| 5288 | * option accept-invalid-http-response. |
| 5289 | */ |
| 5290 | if (!(s->be->options2 & PR_O2_RSPBUG_OK)) { |
| 5291 | if (msg->sl.st.v_l != 8) { |
| 5292 | msg->err_pos = 0; |
| 5293 | goto hdr_response_bad; |
| 5294 | } |
| 5295 | |
| 5296 | if (rep->buf->p[4] != '/' || |
| 5297 | !isdigit((unsigned char)rep->buf->p[5]) || |
| 5298 | rep->buf->p[6] != '.' || |
| 5299 | !isdigit((unsigned char)rep->buf->p[7])) { |
| 5300 | msg->err_pos = 4; |
| 5301 | goto hdr_response_bad; |
| 5302 | } |
| 5303 | } |
| 5304 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5305 | /* check if the response is HTTP/1.1 or above */ |
| 5306 | if ((msg->sl.st.v_l == 8) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5307 | ((rep->buf->p[5] > '1') || |
| 5308 | ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1')))) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5309 | msg->flags |= HTTP_MSGF_VER_11; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5310 | |
| 5311 | /* "connection" has not been parsed yet */ |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 5312 | 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] | 5313 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5314 | /* transfer length unknown*/ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5315 | msg->flags &= ~HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5316 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5317 | 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] | 5318 | |
Willy Tarreau | 3965040 | 2010-03-15 19:44:39 +0100 | [diff] [blame] | 5319 | /* Adjust server's health based on status code. Note: status codes 501 |
| 5320 | * and 505 are triggered on demand by client request, so we must not |
| 5321 | * count them as server failures. |
| 5322 | */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5323 | if (objt_server(s->target)) { |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 5324 | 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] | 5325 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK); |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 5326 | else |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5327 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS); |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 5328 | } |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5329 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5330 | /* |
Willy Tarreau | a14ad72 | 2017-07-07 11:36:32 +0200 | [diff] [blame] | 5331 | * We may be facing a 100-continue response, or any other informational |
| 5332 | * 1xx response which is non-final, in which case this is not the right |
| 5333 | * response, and we're waiting for the next one. Let's allow this response |
| 5334 | * to go to the client and wait for the next one. There's an exception for |
| 5335 | * 101 which is used later in the code to switch protocols. |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5336 | */ |
Willy Tarreau | a14ad72 | 2017-07-07 11:36:32 +0200 | [diff] [blame] | 5337 | if (txn->status < 200 && |
| 5338 | (txn->status == 100 || txn->status >= 102)) { |
Willy Tarreau | 628c40c | 2014-04-24 19:11:26 +0200 | [diff] [blame] | 5339 | hdr_idx_init(&txn->hdr_idx); |
| 5340 | msg->next -= channel_forward(rep, msg->next); |
| 5341 | msg->msg_state = HTTP_MSG_RPBEFORE; |
| 5342 | txn->status = 0; |
| 5343 | s->logs.t_data = -1; /* was not a response yet */ |
Christopher Faulet | 3e34429 | 2015-11-24 16:24:13 +0100 | [diff] [blame] | 5344 | FLT_STRM_CB(s, flt_http_reset(s, msg)); |
Willy Tarreau | 628c40c | 2014-04-24 19:11:26 +0200 | [diff] [blame] | 5345 | goto next_one; |
Willy Tarreau | a14ad72 | 2017-07-07 11:36:32 +0200 | [diff] [blame] | 5346 | } |
Willy Tarreau | 628c40c | 2014-04-24 19:11:26 +0200 | [diff] [blame] | 5347 | |
Willy Tarreau | a14ad72 | 2017-07-07 11:36:32 +0200 | [diff] [blame] | 5348 | /* |
| 5349 | * 2: check for cacheability. |
| 5350 | */ |
| 5351 | |
| 5352 | switch (txn->status) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5353 | case 200: |
| 5354 | case 203: |
| 5355 | case 206: |
| 5356 | case 300: |
| 5357 | case 301: |
| 5358 | case 410: |
| 5359 | /* RFC2616 @13.4: |
| 5360 | * "A response received with a status code of |
| 5361 | * 200, 203, 206, 300, 301 or 410 MAY be stored |
| 5362 | * by a cache (...) unless a cache-control |
| 5363 | * directive prohibits caching." |
| 5364 | * |
| 5365 | * RFC2616 @9.5: POST method : |
| 5366 | * "Responses to this method are not cacheable, |
| 5367 | * unless the response includes appropriate |
| 5368 | * Cache-Control or Expires header fields." |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5369 | */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5370 | if (likely(txn->meth != HTTP_METH_POST) && |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 5371 | ((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] | 5372 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
| 5373 | break; |
| 5374 | default: |
| 5375 | break; |
| 5376 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5377 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5378 | /* |
| 5379 | * 3: we may need to capture headers |
| 5380 | */ |
| 5381 | s->logs.logwait &= ~LW_RESP; |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 5382 | if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap)) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5383 | capture_headers(rep->buf->p, &txn->hdr_idx, |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 5384 | s->res_cap, sess->fe->rsp_cap); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5385 | |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 5386 | /* 4: determine the transfer-length according to RFC2616 #4.4, updated |
| 5387 | * by RFC7230#3.3.3 : |
| 5388 | * |
| 5389 | * The length of a message body is determined by one of the following |
| 5390 | * (in order of precedence): |
| 5391 | * |
Christopher Faulet | d1cd209 | 2016-11-28 10:14:03 +0100 | [diff] [blame] | 5392 | * 1. Any 2xx (Successful) response to a CONNECT request implies that |
| 5393 | * the connection will become a tunnel immediately after the empty |
| 5394 | * line that concludes the header fields. A client MUST ignore |
| 5395 | * any Content-Length or Transfer-Encoding header fields received |
| 5396 | * in such a message. Any 101 response (Switching Protocols) is |
| 5397 | * managed in the same manner. |
| 5398 | * |
| 5399 | * 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] | 5400 | * (Informational), 204 (No Content), or 304 (Not Modified) status |
| 5401 | * code is always terminated by the first empty line after the |
| 5402 | * header fields, regardless of the header fields present in the |
| 5403 | * message, and thus cannot contain a message body. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5404 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 5405 | * 3. If a Transfer-Encoding header field is present and the chunked |
| 5406 | * transfer coding (Section 4.1) is the final encoding, the message |
| 5407 | * body length is determined by reading and decoding the chunked |
| 5408 | * data until the transfer coding indicates the data is complete. |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5409 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 5410 | * If a Transfer-Encoding header field is present in a response and |
| 5411 | * the chunked transfer coding is not the final encoding, the |
| 5412 | * message body length is determined by reading the connection until |
| 5413 | * it is closed by the server. If a Transfer-Encoding header field |
| 5414 | * is present in a request and the chunked transfer coding is not |
| 5415 | * the final encoding, the message body length cannot be determined |
| 5416 | * reliably; the server MUST respond with the 400 (Bad Request) |
| 5417 | * status code and then close the connection. |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5418 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 5419 | * If a message is received with both a Transfer-Encoding and a |
| 5420 | * Content-Length header field, the Transfer-Encoding overrides the |
| 5421 | * Content-Length. Such a message might indicate an attempt to |
| 5422 | * perform request smuggling (Section 9.5) or response splitting |
| 5423 | * (Section 9.4) and ought to be handled as an error. A sender MUST |
| 5424 | * remove the received Content-Length field prior to forwarding such |
| 5425 | * a message downstream. |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5426 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 5427 | * 4. If a message is received without Transfer-Encoding and with |
| 5428 | * either multiple Content-Length header fields having differing |
| 5429 | * field-values or a single Content-Length header field having an |
| 5430 | * invalid value, then the message framing is invalid and the |
| 5431 | * recipient MUST treat it as an unrecoverable error. If this is a |
| 5432 | * request message, the server MUST respond with a 400 (Bad Request) |
| 5433 | * status code and then close the connection. If this is a response |
| 5434 | * message received by a proxy, the proxy MUST close the connection |
| 5435 | * to the server, discard the received response, and send a 502 (Bad |
| 5436 | * Gateway) response to the client. If this is a response message |
| 5437 | * received by a user agent, the user agent MUST close the |
| 5438 | * connection to the server and discard the received response. |
| 5439 | * |
| 5440 | * 5. If a valid Content-Length header field is present without |
| 5441 | * Transfer-Encoding, its decimal value defines the expected message |
| 5442 | * body length in octets. If the sender closes the connection or |
| 5443 | * the recipient times out before the indicated number of octets are |
| 5444 | * received, the recipient MUST consider the message to be |
| 5445 | * incomplete and close the connection. |
| 5446 | * |
| 5447 | * 6. If this is a request message and none of the above are true, then |
| 5448 | * the message body length is zero (no message body is present). |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5449 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 5450 | * 7. Otherwise, this is a response message without a declared message |
| 5451 | * body length, so the message body length is determined by the |
| 5452 | * number of octets received prior to the server closing the |
| 5453 | * connection. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5454 | */ |
| 5455 | |
| 5456 | /* Skip parsing if no content length is possible. The response flags |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 5457 | * 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] | 5458 | * 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] | 5459 | * FIXME: should we parse anyway and return an error on chunked encoding ? |
| 5460 | */ |
Christopher Faulet | d1cd209 | 2016-11-28 10:14:03 +0100 | [diff] [blame] | 5461 | if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || |
| 5462 | txn->status == 101)) { |
| 5463 | /* Either we've established an explicit tunnel, or we're |
| 5464 | * switching the protocol. In both cases, we're very unlikely |
| 5465 | * to understand the next protocols. We have to switch to tunnel |
| 5466 | * mode, so that we transfer the request and responses then let |
| 5467 | * this protocol pass unmodified. When we later implement specific |
| 5468 | * parsers for such protocols, we'll want to check the Upgrade |
| 5469 | * header which contains information about that protocol for |
| 5470 | * responses with status 101 (eg: see RFC2817 about TLS). |
| 5471 | */ |
| 5472 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN; |
| 5473 | msg->flags |= HTTP_MSGF_XFER_LEN; |
| 5474 | goto end; |
| 5475 | } |
| 5476 | |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5477 | if (txn->meth == HTTP_METH_HEAD || |
| 5478 | (txn->status >= 100 && txn->status < 200) || |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5479 | txn->status == 204 || txn->status == 304) { |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5480 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5481 | goto skip_content_length; |
| 5482 | } |
| 5483 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5484 | use_close_only = 0; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5485 | ctx.idx = 0; |
Willy Tarreau | 4979d5c | 2015-05-01 10:06:30 +0200 | [diff] [blame] | 5486 | 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] | 5487 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5488 | msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
| 5489 | else if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5490 | /* bad transfer-encoding (chunked followed by something else) */ |
| 5491 | use_close_only = 1; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5492 | msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5493 | break; |
| 5494 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5495 | } |
| 5496 | |
Willy Tarreau | 1c91391 | 2015-04-30 10:57:51 +0200 | [diff] [blame] | 5497 | /* Chunked responses must have their content-length removed */ |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5498 | ctx.idx = 0; |
Willy Tarreau | b4d0c03 | 2015-05-01 10:25:45 +0200 | [diff] [blame] | 5499 | if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) { |
Willy Tarreau | 1c91391 | 2015-04-30 10:57:51 +0200 | [diff] [blame] | 5500 | while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) |
| 5501 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 5502 | } |
Willy Tarreau | b4d0c03 | 2015-05-01 10:25:45 +0200 | [diff] [blame] | 5503 | 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] | 5504 | signed long long cl; |
| 5505 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5506 | if (!ctx.vlen) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5507 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5508 | goto hdr_response_bad; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5509 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5510 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5511 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5512 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5513 | goto hdr_response_bad; /* parse failure */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5514 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5515 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5516 | if (cl < 0) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5517 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5518 | goto hdr_response_bad; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5519 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5520 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5521 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5522 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5523 | goto hdr_response_bad; /* already specified, was different */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5524 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5525 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5526 | msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 5527 | msg->body_len = msg->chunk_len = cl; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5528 | } |
| 5529 | |
Christopher Faulet | d1cd209 | 2016-11-28 10:14:03 +0100 | [diff] [blame] | 5530 | skip_content_length: |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5531 | /* Now we have to check if we need to modify the Connection header. |
| 5532 | * This is more difficult on the response than it is on the request, |
| 5533 | * because we can have two different HTTP versions and we don't know |
| 5534 | * how the client will interprete a response. For instance, let's say |
| 5535 | * that the client sends a keep-alive request in HTTP/1.0 and gets an |
| 5536 | * HTTP/1.1 response without any header. Maybe it will bound itself to |
| 5537 | * HTTP/1.0 because it only knows about it, and will consider the lack |
| 5538 | * of header as a close, or maybe it knows HTTP/1.1 and can consider |
| 5539 | * the lack of header as a keep-alive. Thus we will use two flags |
| 5540 | * indicating how a request MAY be understood by the client. In case |
| 5541 | * of multiple possibilities, we'll fix the header to be explicit. If |
| 5542 | * ambiguous cases such as both close and keepalive are seen, then we |
| 5543 | * will fall back to explicit close. Note that we won't take risks with |
| 5544 | * HTTP/1.0 clients which may not necessarily understand keep-alive. |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5545 | * See doc/internals/connection-header.txt for the complete matrix. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5546 | */ |
Christopher Faulet | d1cd209 | 2016-11-28 10:14:03 +0100 | [diff] [blame] | 5547 | if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) && |
| 5548 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN || |
| 5549 | ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
| 5550 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) { |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5551 | int to_del = 0; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5552 | |
Willy Tarreau | 70dffda | 2014-01-30 03:07:23 +0100 | [diff] [blame] | 5553 | /* this situation happens when combining pretend-keepalive with httpclose. */ |
| 5554 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5555 | ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 5556 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) |
Willy Tarreau | 70dffda | 2014-01-30 03:07:23 +0100 | [diff] [blame] | 5557 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 5558 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5559 | /* on unknown transfer length, we must close */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5560 | if (!(msg->flags & HTTP_MSGF_XFER_LEN) && |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5561 | (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) |
| 5562 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5563 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5564 | /* now adjust header transformations depending on current state */ |
| 5565 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN || |
| 5566 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 5567 | to_del |= 2; /* remove "keep-alive" on any response */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5568 | if (!(msg->flags & HTTP_MSGF_VER_11)) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5569 | to_del |= 1; /* remove "close" for HTTP/1.0 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5570 | } |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5571 | else { /* SCL / KAL */ |
| 5572 | to_del |= 1; /* remove "close" on any response */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5573 | if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5574 | to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5575 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5576 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5577 | /* Parse and remove some headers from the connection header */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 5578 | http_parse_connection_header(txn, msg, to_del); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5579 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5580 | /* Some keep-alive responses are converted to Server-close if |
| 5581 | * the server wants to close. |
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 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) { |
| 5584 | if ((txn->flags & TX_HDR_CONN_CLO) || |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5585 | (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11))) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5586 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 5587 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5588 | } |
| 5589 | |
Christopher Faulet | d1cd209 | 2016-11-28 10:14:03 +0100 | [diff] [blame] | 5590 | end: |
Willy Tarreau | 7959a55 | 2013-09-23 16:44:27 +0200 | [diff] [blame] | 5591 | /* we want to have the response time before we start processing it */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 5592 | s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now); |
Willy Tarreau | 7959a55 | 2013-09-23 16:44:27 +0200 | [diff] [blame] | 5593 | |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 5594 | /* end of job, return OK */ |
| 5595 | rep->analysers &= ~an_bit; |
| 5596 | rep->analyse_exp = TICK_ETERNITY; |
| 5597 | channel_auto_close(rep); |
| 5598 | return 1; |
| 5599 | |
| 5600 | abort_keep_alive: |
| 5601 | /* A keep-alive request to the server failed on a network error. |
| 5602 | * The client is required to retry. We need to close without returning |
| 5603 | * any other information so that the client retries. |
| 5604 | */ |
| 5605 | txn->status = 0; |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 5606 | rep->analysers &= AN_RES_FLT_END; |
| 5607 | s->req.analysers &= AN_REQ_FLT_END; |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 5608 | channel_auto_close(rep); |
| 5609 | s->logs.logwait = 0; |
| 5610 | s->logs.level = 0; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5611 | s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5612 | channel_truncate(rep); |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 5613 | http_reply_and_close(s, txn->status, NULL); |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 5614 | return 0; |
| 5615 | } |
| 5616 | |
| 5617 | /* This function performs all the processing enabled for the current response. |
| 5618 | * 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] | 5619 | * 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] | 5620 | * other functions. It works like process_request (see indications above). |
| 5621 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5622 | 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] | 5623 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 5624 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5625 | struct http_txn *txn = s->txn; |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 5626 | struct http_msg *msg = &txn->rsp; |
| 5627 | struct proxy *cur_proxy; |
| 5628 | struct cond_wordlist *wl; |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 5629 | enum rule_result ret = HTTP_RULE_RES_CONT; |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 5630 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5631 | 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] | 5632 | now_ms, __FUNCTION__, |
| 5633 | s, |
| 5634 | rep, |
| 5635 | rep->rex, rep->wex, |
| 5636 | rep->flags, |
| 5637 | rep->buf->i, |
| 5638 | rep->analysers); |
| 5639 | |
| 5640 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */ |
| 5641 | return 0; |
| 5642 | |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 5643 | /* The stats applet needs to adjust the Connection header but we don't |
| 5644 | * apply any filter there. |
| 5645 | */ |
Willy Tarreau | 612adb8 | 2015-03-10 15:25:54 +0100 | [diff] [blame] | 5646 | if (unlikely(objt_applet(s->target) == &http_stats_applet)) { |
| 5647 | rep->analysers &= ~an_bit; |
| 5648 | rep->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 5649 | goto skip_filters; |
Willy Tarreau | 612adb8 | 2015-03-10 15:25:54 +0100 | [diff] [blame] | 5650 | } |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 5651 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5652 | /* |
| 5653 | * We will have to evaluate the filters. |
| 5654 | * As opposed to version 1.2, now they will be evaluated in the |
| 5655 | * filters order and not in the header order. This means that |
| 5656 | * each filter has to be validated among all headers. |
| 5657 | * |
| 5658 | * Filters are tried with ->be first, then with ->fe if it is |
| 5659 | * different from ->be. |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 5660 | * |
| 5661 | * Maybe we are in resume condiion. In this case I choose the |
| 5662 | * "struct proxy" which contains the rule list matching the resume |
| 5663 | * pointer. If none of theses "struct proxy" match, I initialise |
| 5664 | * the process with the first one. |
| 5665 | * |
| 5666 | * In fact, I check only correspondance betwwen the current list |
| 5667 | * pointer and the ->fe rule list. If it doesn't match, I initialize |
| 5668 | * the loop with the ->be. |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5669 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5670 | if (s->current_rule_list == &sess->fe->http_res_rules) |
| 5671 | cur_proxy = sess->fe; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 5672 | else |
| 5673 | cur_proxy = s->be; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5674 | while (1) { |
| 5675 | struct proxy *rule_set = cur_proxy; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5676 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5677 | /* evaluate http-response rules */ |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 5678 | if (ret == HTTP_RULE_RES_CONT) { |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 5679 | 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] | 5680 | |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 5681 | if (ret == HTTP_RULE_RES_BADREQ) |
| 5682 | goto return_srv_prx_502; |
| 5683 | |
| 5684 | if (ret == HTTP_RULE_RES_DONE) { |
| 5685 | rep->analysers &= ~an_bit; |
| 5686 | rep->analyse_exp = TICK_ETERNITY; |
| 5687 | return 1; |
| 5688 | } |
| 5689 | } |
| 5690 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 5691 | /* we need to be called again. */ |
| 5692 | if (ret == HTTP_RULE_RES_YIELD) { |
| 5693 | channel_dont_close(rep); |
| 5694 | return 0; |
| 5695 | } |
| 5696 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5697 | /* try headers filters */ |
| 5698 | if (rule_set->rsp_exp != NULL) { |
| 5699 | if (apply_filters_to_response(s, rep, rule_set) < 0) { |
| 5700 | return_bad_resp: |
| 5701 | if (objt_server(s->target)) { |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 5702 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1); |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5703 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5704 | } |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 5705 | HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1); |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5706 | return_srv_prx_502: |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 5707 | rep->analysers &= AN_RES_FLT_END; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5708 | txn->status = 502; |
| 5709 | s->logs.t_data = -1; /* was not a valid response */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5710 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5711 | channel_truncate(rep); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 5712 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5713 | if (!(s->flags & SF_ERR_MASK)) |
| 5714 | s->flags |= SF_ERR_PRXCOND; |
| 5715 | if (!(s->flags & SF_FINST_MASK)) |
| 5716 | s->flags |= SF_FINST_H; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5717 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5718 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5719 | } |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5720 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5721 | /* has the response been denied ? */ |
| 5722 | if (txn->flags & TX_SVDENY) { |
| 5723 | if (objt_server(s->target)) |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 5724 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5725 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 5726 | HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1); |
| 5727 | HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 5728 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 5729 | HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5730 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5731 | goto return_srv_prx_502; |
| 5732 | } |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 5733 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5734 | /* add response headers from the rule sets in the same order */ |
| 5735 | list_for_each_entry(wl, &rule_set->rsp_add, list) { |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 5736 | if (txn->status < 200 && txn->status != 101) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5737 | break; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5738 | if (wl->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 5739 | 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] | 5740 | ret = acl_pass(ret); |
| 5741 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 5742 | ret = !ret; |
| 5743 | if (!ret) |
| 5744 | continue; |
| 5745 | } |
| 5746 | if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0)) |
| 5747 | goto return_bad_resp; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5748 | } |
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 | /* check whether we're already working on the frontend */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5751 | if (cur_proxy == sess->fe) |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5752 | break; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5753 | cur_proxy = sess->fe; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5754 | } |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5755 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 5756 | /* After this point, this anayzer can't return yield, so we can |
| 5757 | * remove the bit corresponding to this analyzer from the list. |
| 5758 | * |
| 5759 | * Note that the intermediate returns and goto found previously |
| 5760 | * reset the analyzers. |
| 5761 | */ |
| 5762 | rep->analysers &= ~an_bit; |
| 5763 | rep->analyse_exp = TICK_ETERNITY; |
| 5764 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5765 | /* OK that's all we can do for 1xx responses */ |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 5766 | if (unlikely(txn->status < 200 && txn->status != 101)) |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5767 | goto skip_header_mangling; |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5768 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5769 | /* |
| 5770 | * Now check for a server cookie. |
| 5771 | */ |
Willy Tarreau | 53a09d5 | 2015-08-10 18:59:40 +0200 | [diff] [blame] | 5772 | 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] | 5773 | manage_server_side_cookies(s, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5774 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5775 | /* |
| 5776 | * Check for cache-control or pragma headers if required. |
| 5777 | */ |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 5778 | 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] | 5779 | check_response_for_cacheability(s, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5780 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5781 | /* |
| 5782 | * Add server cookie in the response if needed |
| 5783 | */ |
| 5784 | if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) && |
| 5785 | !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) && |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5786 | (!(s->flags & SF_DIRECT) || |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5787 | ((s->be->cookie_maxidle || txn->cookie_last_date) && |
| 5788 | (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) || |
| 5789 | (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date |
| 5790 | (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date |
| 5791 | (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) && |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5792 | !(s->flags & SF_IGNORE_PRST)) { |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5793 | /* the server is known, it's not the one the client requested, or the |
| 5794 | * cookie's last seen date needs to be refreshed. We have to |
| 5795 | * insert a set-cookie here, except if we want to insert only on POST |
| 5796 | * requests and this one isn't. Note that servers which don't have cookies |
| 5797 | * (eg: some backup servers) will return a full cookie removal request. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5798 | */ |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5799 | if (!objt_server(s->target)->cookie) { |
| 5800 | chunk_printf(&trash, |
| 5801 | "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/", |
| 5802 | s->be->cookie_name); |
| 5803 | } |
| 5804 | else { |
| 5805 | 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] | 5806 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5807 | if (s->be->cookie_maxidle || s->be->cookie_maxlife) { |
| 5808 | /* emit last_date, which is mandatory */ |
| 5809 | trash.str[trash.len++] = COOKIE_DELIM_DATE; |
| 5810 | s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len); |
| 5811 | trash.len += 5; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5812 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5813 | if (s->be->cookie_maxlife) { |
| 5814 | /* emit first_date, which is either the original one or |
| 5815 | * the current date. |
| 5816 | */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 5817 | trash.str[trash.len++] = COOKIE_DELIM_DATE; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5818 | s30tob64(txn->cookie_first_date ? |
| 5819 | txn->cookie_first_date >> 2 : |
| 5820 | (date.tv_sec+3) >> 2, trash.str + trash.len); |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 5821 | trash.len += 5; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5822 | } |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5823 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5824 | chunk_appendf(&trash, "; path=/"); |
| 5825 | } |
Willy Tarreau | 4992dd2 | 2012-05-31 21:02:17 +0200 | [diff] [blame] | 5826 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5827 | if (s->be->cookie_domain) |
| 5828 | chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain); |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5829 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5830 | if (s->be->ck_opts & PR_CK_HTTPONLY) |
| 5831 | chunk_appendf(&trash, "; HttpOnly"); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5832 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5833 | if (s->be->ck_opts & PR_CK_SECURE) |
| 5834 | chunk_appendf(&trash, "; Secure"); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5835 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5836 | if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0)) |
| 5837 | goto return_bad_resp; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5838 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5839 | txn->flags &= ~TX_SCK_MASK; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5840 | if (objt_server(s->target)->cookie && (s->flags & SF_DIRECT)) |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5841 | /* the server did not change, only the date was updated */ |
| 5842 | txn->flags |= TX_SCK_UPDATED; |
| 5843 | else |
| 5844 | txn->flags |= TX_SCK_INSERTED; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5845 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5846 | /* Here, we will tell an eventual cache on the client side that we don't |
| 5847 | * want it to cache this reply because HTTP/1.0 caches also cache cookies ! |
| 5848 | * Some caches understand the correct form: 'no-cache="set-cookie"', but |
| 5849 | * 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] | 5850 | */ |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5851 | if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5852 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5853 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +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, |
| 5856 | "Cache-control: private", 22) < 0)) |
| 5857 | goto return_bad_resp; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5858 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5859 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5860 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5861 | /* |
| 5862 | * Check if result will be cacheable with a cookie. |
| 5863 | * We'll block the response if security checks have caught |
| 5864 | * nasty things such as a cacheable cookie. |
| 5865 | */ |
| 5866 | if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) == |
| 5867 | (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) && |
| 5868 | (s->be->options & PR_O_CHK_CACHE)) { |
| 5869 | /* we're in presence of a cacheable response containing |
| 5870 | * a set-cookie header. We'll block it as requested by |
| 5871 | * the 'checkcache' option, and send an alert. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5872 | */ |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5873 | if (objt_server(s->target)) |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 5874 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1); |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5875 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 5876 | HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1); |
| 5877 | HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 5878 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 5879 | HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5880 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5881 | Alert("Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 5882 | s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>"); |
| 5883 | send_log(s->be, LOG_ALERT, |
| 5884 | "Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 5885 | s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>"); |
| 5886 | goto return_srv_prx_502; |
| 5887 | } |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 5888 | |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 5889 | skip_filters: |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5890 | /* |
| 5891 | * Adjust "Connection: close" or "Connection: keep-alive" if needed. |
| 5892 | * If an "Upgrade" token is found, the header is left untouched in order |
| 5893 | * 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] | 5894 | * if anything but "Upgrade" is present in the Connection header. We don't |
| 5895 | * want to touch any 101 response either since it's switching to another |
| 5896 | * protocol. |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5897 | */ |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 5898 | if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) && |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5899 | (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5900 | ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5901 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) { |
| 5902 | unsigned int want_flags = 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5903 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5904 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 5905 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 5906 | /* we want a keep-alive response here. Keep-alive header |
| 5907 | * required if either side is not 1.1. |
| 5908 | */ |
| 5909 | if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11)) |
| 5910 | want_flags |= TX_CON_KAL_SET; |
| 5911 | } |
| 5912 | else { |
| 5913 | /* we want a close response here. Close header required if |
| 5914 | * the server is 1.1, regardless of the client. |
| 5915 | */ |
| 5916 | if (msg->flags & HTTP_MSGF_VER_11) |
| 5917 | want_flags |= TX_CON_CLO_SET; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5918 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5919 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5920 | if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 5921 | http_change_connection_header(txn, msg, want_flags); |
| 5922 | } |
| 5923 | |
| 5924 | skip_header_mangling: |
Christopher Faulet | 69744d9 | 2017-03-30 10:54:35 +0200 | [diff] [blame] | 5925 | /* Always enter in the body analyzer */ |
| 5926 | rep->analysers &= ~AN_RES_FLT_XFER_DATA; |
| 5927 | rep->analysers |= AN_RES_HTTP_XFER_BODY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5928 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5929 | /* if the user wants to log as soon as possible, without counting |
| 5930 | * bytes from the server, then this is the right moment. We have |
| 5931 | * to temporarily assign bytes_out to log what we currently have. |
| 5932 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5933 | if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) { |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5934 | s->logs.t_close = s->logs.t_data; /* to get a valid end date */ |
| 5935 | s->logs.bytes_out = txn->rsp.eoh; |
| 5936 | s->do_log(s); |
| 5937 | s->logs.bytes_out = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5938 | } |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 5939 | return 1; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5940 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5941 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5942 | /* This function is an analyser which forwards response body (including chunk |
| 5943 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 5944 | * zero byte. The only situation where it must not be called is when we're in |
| 5945 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 5946 | * remaining data and to resync after end of body. It expects the msg_state to |
| 5947 | * 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] | 5948 | * 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] | 5949 | * |
| 5950 | * It is capable of compressing response data both in content-length mode and |
| 5951 | * in chunked mode. The state machines follows different flows depending on |
| 5952 | * whether content-length and chunked modes are used, since there are no |
| 5953 | * trailers in content-length : |
| 5954 | * |
| 5955 | * chk-mode cl-mode |
| 5956 | * ,----- BODY -----. |
| 5957 | * / \ |
| 5958 | * V size > 0 V chk-mode |
| 5959 | * .--> SIZE -------------> DATA -------------> CRLF |
| 5960 | * | | size == 0 | last byte | |
| 5961 | * | v final crlf v inspected | |
| 5962 | * | TRAILERS -----------> DONE | |
| 5963 | * | | |
| 5964 | * `----------------------------------------------' |
| 5965 | * |
| 5966 | * Compression only happens in the DATA state, and must be flushed in final |
| 5967 | * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding |
| 5968 | * is performed at once on final states for all bytes parsed, or when leaving |
| 5969 | * on missing data. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5970 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5971 | 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] | 5972 | { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5973 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5974 | struct http_txn *txn = s->txn; |
| 5975 | struct http_msg *msg = &s->txn->rsp; |
Christopher Faulet | 3e34429 | 2015-11-24 16:24:13 +0100 | [diff] [blame] | 5976 | int ret; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5977 | |
Christopher Faulet | 814d270 | 2017-03-30 11:33:44 +0200 | [diff] [blame] | 5978 | DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n", |
| 5979 | now_ms, __FUNCTION__, |
| 5980 | s, |
| 5981 | res, |
| 5982 | res->rex, res->wex, |
| 5983 | res->flags, |
| 5984 | res->buf->i, |
| 5985 | res->analysers); |
| 5986 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5987 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 5988 | return 0; |
| 5989 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5990 | 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] | 5991 | ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) || |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 5992 | !s->req.analysers) { |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 5993 | /* Output closed while we were sending data. We must abort and |
| 5994 | * wake the other side up. |
| 5995 | */ |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 5996 | msg->err_state = msg->msg_state; |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 5997 | msg->msg_state = HTTP_MSG_ERROR; |
| 5998 | http_resync_states(s); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 5999 | return 1; |
| 6000 | } |
| 6001 | |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 6002 | /* in most states, we should abort in case of early close */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6003 | channel_auto_close(res); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 6004 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 6005 | if (msg->msg_state == HTTP_MSG_BODY) { |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6006 | msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK) |
| 6007 | ? HTTP_MSG_CHUNK_SIZE |
| 6008 | : HTTP_MSG_DATA); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6009 | } |
| 6010 | |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 6011 | if (res->to_forward) { |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6012 | /* We can't process the buffer's contents yet */ |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 6013 | res->flags |= CF_WAKE_WRITE; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6014 | goto missing_data_or_waiting; |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 6015 | } |
| 6016 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6017 | if (msg->msg_state < HTTP_MSG_DONE) { |
| 6018 | ret = ((msg->flags & HTTP_MSGF_TE_CHNK) |
| 6019 | ? http_msg_forward_chunked_body(s, msg) |
| 6020 | : http_msg_forward_body(s, msg)); |
| 6021 | if (!ret) |
| 6022 | goto missing_data_or_waiting; |
| 6023 | if (ret < 0) |
| 6024 | goto return_bad_res; |
| 6025 | } |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 6026 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6027 | /* other states, DONE...TUNNEL */ |
| 6028 | /* for keep-alive we don't want to forward closes on DONE */ |
| 6029 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 6030 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
| 6031 | channel_dont_close(res); |
Willy Tarreau | 3ce10ff | 2014-04-22 08:24:38 +0200 | [diff] [blame] | 6032 | |
Christopher Faulet | 894da4c | 2017-07-18 11:29:07 +0200 | [diff] [blame] | 6033 | http_resync_states(s); |
| 6034 | if (!(res->analysers & an_bit)) { |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6035 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 6036 | if (res->flags & CF_SHUTW) { |
| 6037 | /* response errors are most likely due to the |
| 6038 | * client aborting the transfer. */ |
| 6039 | goto aborted_xfer; |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 6040 | } |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6041 | if (msg->err_pos >= 0) |
Emeric Brun | 8c1aaa2 | 2017-06-15 11:30:06 +0200 | [diff] [blame] | 6042 | 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] | 6043 | goto return_bad_res; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6044 | } |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6045 | return 1; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6046 | } |
Willy Tarreau | f51d03c | 2016-05-02 15:25:15 +0200 | [diff] [blame] | 6047 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6048 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6049 | missing_data_or_waiting: |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6050 | if (res->flags & CF_SHUTW) |
| 6051 | goto aborted_xfer; |
| 6052 | |
| 6053 | /* stop waiting for data if the input is closed before the end. If the |
| 6054 | * client side was already closed, it means that the client has aborted, |
| 6055 | * so we don't want to count this as a server abort. Otherwise it's a |
| 6056 | * server abort. |
| 6057 | */ |
Christopher Faulet | a33510b | 2017-03-31 15:37:29 +0200 | [diff] [blame] | 6058 | if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6059 | if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW)) |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6060 | goto aborted_xfer; |
Christopher Faulet | a46bbd8 | 2015-06-19 09:00:58 +0200 | [diff] [blame] | 6061 | /* If we have some pending data, we continue the processing */ |
| 6062 | if (!buffer_pending(res->buf)) { |
| 6063 | if (!(s->flags & SF_ERR_MASK)) |
| 6064 | s->flags |= SF_ERR_SRVCL; |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 6065 | HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1); |
Christopher Faulet | a46bbd8 | 2015-06-19 09:00:58 +0200 | [diff] [blame] | 6066 | if (objt_server(s->target)) |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 6067 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1); |
Christopher Faulet | a46bbd8 | 2015-06-19 09:00:58 +0200 | [diff] [blame] | 6068 | goto return_bad_res_stats_ok; |
| 6069 | } |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 6070 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 6071 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 6072 | /* 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] | 6073 | if (!s->req.analysers) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 6074 | goto return_bad_res; |
| 6075 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6076 | /* When TE: chunked is used, we need to get there again to parse |
| 6077 | * 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] | 6078 | * set CF_DONTCLOSE. Similarly, if keep-alive is set on the client side |
| 6079 | * or if there are filters registered on the stream, we don't want to |
| 6080 | * forward a close |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 6081 | */ |
Christopher Faulet | 69744d9 | 2017-03-30 10:54:35 +0200 | [diff] [blame] | 6082 | if ((msg->flags & HTTP_MSGF_TE_CHNK) || |
Christopher Faulet | f1cc5d0 | 2017-02-08 09:45:13 +0100 | [diff] [blame] | 6083 | HAS_DATA_FILTERS(s, res) || |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 6084 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 6085 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6086 | channel_dont_close(res); |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 6087 | |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 6088 | /* 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] | 6089 | * 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] | 6090 | * 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] | 6091 | * modes are already handled by the stream sock layer. We must not do |
| 6092 | * this in content-length mode because it could present the MSG_MORE |
| 6093 | * flag with the last block of forwarded data, which would cause an |
| 6094 | * additional delay to be observed by the receiver. |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 6095 | */ |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 6096 | if ((msg->flags & HTTP_MSGF_TE_CHNK) || (msg->flags & HTTP_MSGF_COMPRESSING)) |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 6097 | res->flags |= CF_EXPECT_MORE; |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 6098 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6099 | /* the stream handler will take care of timeouts and errors */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6100 | return 0; |
| 6101 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 6102 | return_bad_res: /* let's centralize all bad responses */ |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 6103 | HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6104 | if (objt_server(s->target)) |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 6105 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6106 | |
| 6107 | return_bad_res_stats_ok: |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 6108 | txn->rsp.err_state = txn->rsp.msg_state; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6109 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 6110 | /* don't send any error message as we're in the body */ |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 6111 | http_reply_and_close(s, txn->status, NULL); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 6112 | res->analysers &= AN_RES_FLT_END; |
| 6113 | 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] | 6114 | if (objt_server(s->target)) |
| 6115 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6116 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6117 | if (!(s->flags & SF_ERR_MASK)) |
| 6118 | s->flags |= SF_ERR_PRXCOND; |
| 6119 | if (!(s->flags & SF_FINST_MASK)) |
| 6120 | s->flags |= SF_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6121 | return 0; |
| 6122 | |
| 6123 | aborted_xfer: |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 6124 | txn->rsp.err_state = txn->rsp.msg_state; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6125 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
| 6126 | /* don't send any error message as we're in the body */ |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 6127 | http_reply_and_close(s, txn->status, NULL); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 6128 | res->analysers &= AN_RES_FLT_END; |
| 6129 | 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] | 6130 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 6131 | HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1); |
| 6132 | HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6133 | if (objt_server(s->target)) |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 6134 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +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_CLICL; |
| 6138 | if (!(s->flags & SF_FINST_MASK)) |
| 6139 | s->flags |= SF_FINST_D; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6140 | return 0; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6141 | } |
| 6142 | |
| 6143 | |
| 6144 | static inline int |
| 6145 | http_msg_forward_body(struct stream *s, struct http_msg *msg) |
| 6146 | { |
| 6147 | struct channel *chn = msg->chn; |
| 6148 | int ret; |
| 6149 | |
| 6150 | /* Here we have the guarantee to be in HTTP_MSG_DATA or HTTP_MSG_ENDING state */ |
| 6151 | |
| 6152 | if (msg->msg_state == HTTP_MSG_ENDING) |
| 6153 | goto ending; |
| 6154 | |
| 6155 | /* Neither content-length, nor transfer-encoding was found, so we must |
| 6156 | * read the body until the server connection is closed. In that case, we |
| 6157 | * eat data as they come. Of course, this happens for response only. */ |
| 6158 | if (!(msg->flags & HTTP_MSGF_XFER_LEN)) { |
| 6159 | unsigned long long len = (chn->buf->i - msg->next); |
| 6160 | msg->chunk_len += len; |
| 6161 | msg->body_len += len; |
| 6162 | } |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 6163 | ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg), |
| 6164 | /* default_ret */ MIN(msg->chunk_len, chn->buf->i - msg->next), |
| 6165 | /* on_error */ goto error); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6166 | msg->next += ret; |
| 6167 | msg->chunk_len -= ret; |
| 6168 | if (msg->chunk_len) { |
| 6169 | /* input empty or output full */ |
| 6170 | if (chn->buf->i > msg->next) |
| 6171 | chn->flags |= CF_WAKE_WRITE; |
| 6172 | goto missing_data_or_waiting; |
| 6173 | } |
| 6174 | |
Christopher Faulet | 1486b0a | 2017-07-18 11:42:08 +0200 | [diff] [blame] | 6175 | /* This check can only be true for a response. HTTP_MSGF_XFER_LEN is |
| 6176 | * always set for a request. */ |
| 6177 | if (!(msg->flags & HTTP_MSGF_XFER_LEN)) { |
| 6178 | /* The server still sending data that should be filtered */ |
| 6179 | if (!(chn->flags & CF_SHUTR) && HAS_DATA_FILTERS(s, chn)) |
| 6180 | goto missing_data_or_waiting; |
| 6181 | } |
Christopher Faulet | f1cc5d0 | 2017-02-08 09:45:13 +0100 | [diff] [blame] | 6182 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6183 | msg->msg_state = HTTP_MSG_ENDING; |
| 6184 | |
| 6185 | ending: |
| 6186 | /* we may have some pending data starting at res->buf->p such as a last |
| 6187 | * chunk of data or trailers. */ |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 6188 | ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next), |
| 6189 | /* default_ret */ msg->next, |
| 6190 | /* on_error */ goto error); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6191 | b_adv(chn->buf, ret); |
| 6192 | msg->next -= ret; |
Willy Tarreau | 9962f8f | 2016-06-28 11:52:08 +0200 | [diff] [blame] | 6193 | if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)) |
| 6194 | msg->sov -= ret; |
Christopher Faulet | a9300a3 | 2016-06-28 15:54:44 +0200 | [diff] [blame] | 6195 | if (msg->next) |
| 6196 | goto waiting; |
Willy Tarreau | 9962f8f | 2016-06-28 11:52:08 +0200 | [diff] [blame] | 6197 | |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 6198 | FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg), |
| 6199 | /* default_ret */ 1, |
| 6200 | /* on_error */ goto error, |
| 6201 | /* on_wait */ goto waiting); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6202 | msg->msg_state = HTTP_MSG_DONE; |
| 6203 | return 1; |
| 6204 | |
| 6205 | missing_data_or_waiting: |
| 6206 | /* we may have some pending data starting at chn->buf->p */ |
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; |
| 6212 | if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0) |
| 6213 | msg->sov -= ret; |
Christopher Faulet | 75e2eb6 | 2015-12-15 10:41:47 +0100 | [diff] [blame] | 6214 | if (!HAS_DATA_FILTERS(s, chn)) |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6215 | msg->chunk_len -= channel_forward(chn, msg->chunk_len); |
Christopher Faulet | a9300a3 | 2016-06-28 15:54:44 +0200 | [diff] [blame] | 6216 | waiting: |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6217 | return 0; |
| 6218 | error: |
| 6219 | return -1; |
| 6220 | } |
| 6221 | |
| 6222 | static inline int |
| 6223 | http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg) |
| 6224 | { |
| 6225 | struct channel *chn = msg->chn; |
Willy Tarreau | e56cdd3 | 2017-09-21 08:36:33 +0200 | [diff] [blame] | 6226 | unsigned int chunk; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6227 | int ret; |
| 6228 | |
| 6229 | /* Here we have the guarantee to be in one of the following state: |
| 6230 | * HTTP_MSG_DATA, HTTP_MSG_CHUNK_SIZE, HTTP_MSG_CHUNK_CRLF, |
| 6231 | * HTTP_MSG_TRAILERS or HTTP_MSG_ENDING. */ |
| 6232 | |
| 6233 | switch_states: |
| 6234 | switch (msg->msg_state) { |
| 6235 | case HTTP_MSG_DATA: |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 6236 | ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg), |
| 6237 | /* default_ret */ MIN(msg->chunk_len, chn->buf->i - msg->next), |
| 6238 | /* on_error */ goto error); |
| 6239 | msg->next += ret; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6240 | msg->chunk_len -= ret; |
| 6241 | if (msg->chunk_len) { |
| 6242 | /* input empty or output full */ |
| 6243 | if (chn->buf->i > msg->next) |
| 6244 | chn->flags |= CF_WAKE_WRITE; |
| 6245 | goto missing_data_or_waiting; |
| 6246 | } |
| 6247 | |
| 6248 | /* nothing left to forward for this chunk*/ |
| 6249 | msg->msg_state = HTTP_MSG_CHUNK_CRLF; |
| 6250 | /* fall through for HTTP_MSG_CHUNK_CRLF */ |
| 6251 | |
| 6252 | case HTTP_MSG_CHUNK_CRLF: |
| 6253 | /* we want the CRLF after the data */ |
Willy Tarreau | b289256 | 2017-09-21 11:33:54 +0200 | [diff] [blame] | 6254 | ret = h1_skip_chunk_crlf(chn->buf, msg->next, chn->buf->i); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6255 | if (ret == 0) |
| 6256 | goto missing_data_or_waiting; |
Willy Tarreau | b289256 | 2017-09-21 11:33:54 +0200 | [diff] [blame] | 6257 | if (ret < 0) { |
| 6258 | msg->err_pos = chn->buf->i + ret; |
| 6259 | if (msg->err_pos < 0) |
| 6260 | msg->err_pos += chn->buf->size; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6261 | goto chunk_parsing_error; |
Willy Tarreau | b289256 | 2017-09-21 11:33:54 +0200 | [diff] [blame] | 6262 | } |
Christopher Faulet | 113f7de | 2015-12-14 14:52:13 +0100 | [diff] [blame] | 6263 | msg->next += ret; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6264 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 6265 | /* fall through for HTTP_MSG_CHUNK_SIZE */ |
| 6266 | |
| 6267 | case HTTP_MSG_CHUNK_SIZE: |
| 6268 | /* read the chunk size and assign it to ->chunk_len, |
| 6269 | * then set ->next to point to the body and switch to |
| 6270 | * DATA or TRAILERS state. |
| 6271 | */ |
Willy Tarreau | e56cdd3 | 2017-09-21 08:36:33 +0200 | [diff] [blame] | 6272 | 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] | 6273 | if (ret == 0) |
| 6274 | goto missing_data_or_waiting; |
Willy Tarreau | e56cdd3 | 2017-09-21 08:36:33 +0200 | [diff] [blame] | 6275 | if (ret < 0) { |
| 6276 | msg->err_pos = chn->buf->i + ret; |
| 6277 | if (msg->err_pos < 0) |
| 6278 | msg->err_pos += chn->buf->size; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6279 | goto chunk_parsing_error; |
Willy Tarreau | e56cdd3 | 2017-09-21 08:36:33 +0200 | [diff] [blame] | 6280 | } |
| 6281 | |
| 6282 | msg->sol = ret; |
Christopher Faulet | 113f7de | 2015-12-14 14:52:13 +0100 | [diff] [blame] | 6283 | msg->next += ret; |
Willy Tarreau | e56cdd3 | 2017-09-21 08:36:33 +0200 | [diff] [blame] | 6284 | msg->chunk_len = chunk; |
| 6285 | msg->body_len += chunk; |
| 6286 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6287 | if (msg->chunk_len) { |
| 6288 | msg->msg_state = HTTP_MSG_DATA; |
| 6289 | goto switch_states; |
| 6290 | } |
| 6291 | msg->msg_state = HTTP_MSG_TRAILERS; |
| 6292 | /* fall through for HTTP_MSG_TRAILERS */ |
| 6293 | |
| 6294 | case HTTP_MSG_TRAILERS: |
| 6295 | ret = http_forward_trailers(msg); |
| 6296 | if (ret < 0) |
| 6297 | goto chunk_parsing_error; |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 6298 | FLT_STRM_DATA_CB(s, chn, flt_http_chunk_trailers(s, msg), |
| 6299 | /* default_ret */ 1, |
| 6300 | /* on_error */ goto error); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6301 | msg->next += msg->sol; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6302 | if (!ret) |
| 6303 | goto missing_data_or_waiting; |
| 6304 | break; |
| 6305 | |
| 6306 | case HTTP_MSG_ENDING: |
| 6307 | goto ending; |
| 6308 | |
| 6309 | default: |
| 6310 | /* This should no happen in this function */ |
| 6311 | goto error; |
| 6312 | } |
| 6313 | |
| 6314 | msg->msg_state = HTTP_MSG_ENDING; |
| 6315 | ending: |
| 6316 | /* we may have some pending data starting at res->buf->p such as a last |
| 6317 | * chunk of data or trailers. */ |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 6318 | 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] | 6319 | /* default_ret */ msg->next, |
| 6320 | /* on_error */ goto error); |
| 6321 | b_adv(chn->buf, ret); |
| 6322 | msg->next -= ret; |
Willy Tarreau | 9962f8f | 2016-06-28 11:52:08 +0200 | [diff] [blame] | 6323 | if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)) |
| 6324 | msg->sov -= ret; |
Christopher Faulet | a9300a3 | 2016-06-28 15:54:44 +0200 | [diff] [blame] | 6325 | if (msg->next) |
| 6326 | goto waiting; |
Willy Tarreau | 9962f8f | 2016-06-28 11:52:08 +0200 | [diff] [blame] | 6327 | |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 6328 | FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg), |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6329 | /* default_ret */ 1, |
| 6330 | /* on_error */ goto error, |
| 6331 | /* on_wait */ goto waiting); |
| 6332 | msg->msg_state = HTTP_MSG_DONE; |
| 6333 | return 1; |
| 6334 | |
| 6335 | missing_data_or_waiting: |
| 6336 | /* we may have some pending data starting at chn->buf->p */ |
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; |
| 6342 | if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0) |
| 6343 | msg->sov -= ret; |
Christopher Faulet | 75e2eb6 | 2015-12-15 10:41:47 +0100 | [diff] [blame] | 6344 | if (!HAS_DATA_FILTERS(s, chn)) |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6345 | msg->chunk_len -= channel_forward(chn, msg->chunk_len); |
Christopher Faulet | a9300a3 | 2016-06-28 15:54:44 +0200 | [diff] [blame] | 6346 | waiting: |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6347 | return 0; |
| 6348 | |
| 6349 | chunk_parsing_error: |
| 6350 | if (msg->err_pos >= 0) { |
| 6351 | if (chn->flags & CF_ISRESP) |
Emeric Brun | 8c1aaa2 | 2017-06-15 11:30:06 +0200 | [diff] [blame] | 6352 | http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6353 | msg->msg_state, strm_fe(s)); |
| 6354 | else |
Emeric Brun | 8c1aaa2 | 2017-06-15 11:30:06 +0200 | [diff] [blame] | 6355 | 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] | 6356 | msg, msg->msg_state, s->be); |
| 6357 | } |
| 6358 | error: |
| 6359 | return -1; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6360 | } |
| 6361 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6362 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6363 | /* Iterate the same filter through all request headers. |
| 6364 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6365 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 6366 | * DENY stats. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6367 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6368 | 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] | 6369 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6370 | char *cur_ptr, *cur_end, *cur_next; |
| 6371 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 6372 | struct http_txn *txn = s->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6373 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6374 | int delta; |
Willy Tarreau | 0f7562b | 2007-01-07 15:46:13 +0100 | [diff] [blame] | 6375 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6376 | last_hdr = 0; |
| 6377 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6378 | cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6379 | old_idx = 0; |
| 6380 | |
| 6381 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6382 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6383 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6384 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6385 | (exp->action == ACT_ALLOW || |
| 6386 | exp->action == ACT_DENY || |
| 6387 | exp->action == ACT_TARPIT)) |
| 6388 | return 0; |
| 6389 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6390 | cur_idx = txn->hdr_idx.v[old_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6391 | if (!cur_idx) |
| 6392 | break; |
| 6393 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6394 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6395 | cur_ptr = cur_next; |
| 6396 | cur_end = cur_ptr + cur_hdr->len; |
| 6397 | cur_next = cur_end + cur_hdr->cr + 1; |
| 6398 | |
| 6399 | /* Now we have one header between cur_ptr and cur_end, |
| 6400 | * and the next header starts at cur_next. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6401 | */ |
| 6402 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 6403 | 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] | 6404 | switch (exp->action) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6405 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6406 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6407 | last_hdr = 1; |
| 6408 | break; |
| 6409 | |
| 6410 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6411 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6412 | last_hdr = 1; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6413 | break; |
| 6414 | |
| 6415 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6416 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6417 | last_hdr = 1; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6418 | break; |
| 6419 | |
| 6420 | case ACT_REPLACE: |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 6421 | trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch); |
| 6422 | if (trash.len < 0) |
| 6423 | return -1; |
| 6424 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6425 | 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] | 6426 | /* FIXME: if the user adds a newline in the replacement, the |
| 6427 | * index will not be recalculated for now, and the new line |
| 6428 | * will not be counted as a new header. |
| 6429 | */ |
| 6430 | |
| 6431 | cur_end += delta; |
| 6432 | cur_next += delta; |
| 6433 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6434 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6435 | break; |
| 6436 | |
| 6437 | case ACT_REMOVE: |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6438 | delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6439 | cur_next += delta; |
| 6440 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6441 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6442 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 6443 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6444 | cur_hdr->len = 0; |
| 6445 | cur_end = NULL; /* null-term has been rewritten */ |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 6446 | cur_idx = old_idx; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6447 | break; |
| 6448 | |
| 6449 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6450 | } |
| 6451 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6452 | /* keep the link from this header to next one in case of later |
| 6453 | * removal of next header. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6454 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6455 | old_idx = cur_idx; |
| 6456 | } |
| 6457 | return 0; |
| 6458 | } |
| 6459 | |
| 6460 | |
| 6461 | /* Apply the filter to the request line. |
| 6462 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 6463 | * or -1 if a replacement resulted in an invalid request line. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6464 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 6465 | * DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6466 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6467 | 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] | 6468 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6469 | char *cur_ptr, *cur_end; |
| 6470 | int done; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 6471 | struct http_txn *txn = s->txn; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6472 | int delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6473 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6474 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6475 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6476 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6477 | (exp->action == ACT_ALLOW || |
| 6478 | exp->action == ACT_DENY || |
| 6479 | exp->action == ACT_TARPIT)) |
| 6480 | return 0; |
| 6481 | else if (exp->action == ACT_REMOVE) |
| 6482 | return 0; |
| 6483 | |
| 6484 | done = 0; |
| 6485 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6486 | cur_ptr = req->buf->p; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6487 | cur_end = cur_ptr + txn->req.sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6488 | |
| 6489 | /* Now we have the request line between cur_ptr and cur_end */ |
| 6490 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 6491 | 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] | 6492 | switch (exp->action) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6493 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6494 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6495 | done = 1; |
| 6496 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 6497 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6498 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6499 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6500 | done = 1; |
| 6501 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 6502 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6503 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6504 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6505 | done = 1; |
| 6506 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 6507 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6508 | case ACT_REPLACE: |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 6509 | trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch); |
| 6510 | if (trash.len < 0) |
| 6511 | return -1; |
| 6512 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6513 | 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] | 6514 | /* FIXME: if the user adds a newline in the replacement, the |
| 6515 | * index will not be recalculated for now, and the new line |
| 6516 | * will not be counted as a new header. |
| 6517 | */ |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 6518 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6519 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6520 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 6521 | cur_end = (char *)http_parse_reqline(&txn->req, |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6522 | HTTP_MSG_RQMETH, |
| 6523 | cur_ptr, cur_end + 1, |
| 6524 | NULL, NULL); |
| 6525 | if (unlikely(!cur_end)) |
| 6526 | return -1; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 6527 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6528 | /* we have a full request and we know that we have either a CR |
| 6529 | * or an LF at <ptr>. |
| 6530 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6531 | txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l); |
| 6532 | 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] | 6533 | /* there is no point trying this regex on headers */ |
| 6534 | return 1; |
| 6535 | } |
| 6536 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6537 | return done; |
| 6538 | } |
Willy Tarreau | 97de624 | 2006-12-27 17:18:38 +0100 | [diff] [blame] | 6539 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6540 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6541 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6542 | /* |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6543 | * 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] | 6544 | * 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] | 6545 | * unparsable request. Since it can manage the switch to another backend, it |
| 6546 | * updates the per-proxy DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6547 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6548 | 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] | 6549 | { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 6550 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 6551 | struct http_txn *txn = s->txn; |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6552 | struct hdr_exp *exp; |
| 6553 | |
| 6554 | for (exp = px->req_exp; exp; exp = exp->next) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6555 | int ret; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6556 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6557 | /* |
| 6558 | * The interleaving of transformations and verdicts |
| 6559 | * makes it difficult to decide to continue or stop |
| 6560 | * the evaluation. |
| 6561 | */ |
| 6562 | |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6563 | if (txn->flags & (TX_CLDENY|TX_CLTARPIT)) |
| 6564 | break; |
| 6565 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6566 | if ((txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6567 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6568 | exp->action == ACT_TARPIT || exp->action == ACT_PASS)) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6569 | continue; |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6570 | |
| 6571 | /* if this filter had a condition, evaluate it now and skip to |
| 6572 | * next filter if the condition does not match. |
| 6573 | */ |
| 6574 | if (exp->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 6575 | 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] | 6576 | ret = acl_pass(ret); |
| 6577 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 6578 | ret = !ret; |
| 6579 | |
| 6580 | if (!ret) |
| 6581 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6582 | } |
| 6583 | |
| 6584 | /* Apply the filter to the request line. */ |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6585 | ret = apply_filter_to_req_line(s, req, exp); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6586 | if (unlikely(ret < 0)) |
| 6587 | return -1; |
| 6588 | |
| 6589 | if (likely(ret == 0)) { |
| 6590 | /* The filter did not match the request, it can be |
| 6591 | * iterated through all headers. |
| 6592 | */ |
Willy Tarreau | 34d4c3c | 2015-01-30 20:58:58 +0100 | [diff] [blame] | 6593 | if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0)) |
| 6594 | return -1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6595 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6596 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6597 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6598 | } |
| 6599 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6600 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6601 | /* Find the end of a cookie value contained between <s> and <e>. It works the |
| 6602 | * same way as with headers above except that the semi-colon also ends a token. |
| 6603 | * See RFC2965 for more information. Note that it requires a valid header to |
| 6604 | * return a valid result. |
| 6605 | */ |
| 6606 | char *find_cookie_value_end(char *s, const char *e) |
| 6607 | { |
| 6608 | int quoted, qdpair; |
| 6609 | |
| 6610 | quoted = qdpair = 0; |
| 6611 | for (; s < e; s++) { |
| 6612 | if (qdpair) qdpair = 0; |
| 6613 | else if (quoted) { |
| 6614 | if (*s == '\\') qdpair = 1; |
| 6615 | else if (*s == '"') quoted = 0; |
| 6616 | } |
| 6617 | else if (*s == '"') quoted = 1; |
| 6618 | else if (*s == ',' || *s == ';') return s; |
| 6619 | } |
| 6620 | return s; |
| 6621 | } |
| 6622 | |
| 6623 | /* Delete a value in a header between delimiters <from> and <next> in buffer |
| 6624 | * <buf>. The number of characters displaced is returned, and the pointer to |
| 6625 | * the first delimiter is updated if required. The function tries as much as |
| 6626 | * possible to respect the following principles : |
| 6627 | * - replace <from> delimiter by the <next> one unless <from> points to a |
| 6628 | * colon, in which case <next> is simply removed |
| 6629 | * - set exactly one space character after the new first delimiter, unless |
| 6630 | * there are not enough characters in the block being moved to do so. |
| 6631 | * - remove unneeded spaces before the previous delimiter and after the new |
| 6632 | * one. |
| 6633 | * |
| 6634 | * It is the caller's responsibility to ensure that : |
| 6635 | * - <from> points to a valid delimiter or the colon ; |
| 6636 | * - <next> points to a valid delimiter or the final CR/LF ; |
| 6637 | * - there are non-space chars before <from> ; |
| 6638 | * - there is a CR/LF at or after <next>. |
| 6639 | */ |
Willy Tarreau | af81935 | 2012-08-27 22:08:00 +0200 | [diff] [blame] | 6640 | int del_hdr_value(struct buffer *buf, char **from, char *next) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6641 | { |
| 6642 | char *prev = *from; |
| 6643 | |
| 6644 | if (*prev == ':') { |
| 6645 | /* We're removing the first value, preserve the colon and add a |
| 6646 | * space if possible. |
| 6647 | */ |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 6648 | if (!HTTP_IS_CRLF(*next)) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6649 | next++; |
| 6650 | prev++; |
| 6651 | if (prev < next) |
| 6652 | *prev++ = ' '; |
| 6653 | |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 6654 | while (HTTP_IS_SPHT(*next)) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6655 | next++; |
| 6656 | } else { |
| 6657 | /* Remove useless spaces before the old delimiter. */ |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 6658 | while (HTTP_IS_SPHT(*(prev-1))) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6659 | prev--; |
| 6660 | *from = prev; |
| 6661 | |
| 6662 | /* copy the delimiter and if possible a space if we're |
| 6663 | * not at the end of the line. |
| 6664 | */ |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 6665 | if (!HTTP_IS_CRLF(*next)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6666 | *prev++ = *next++; |
| 6667 | if (prev + 1 < next) |
| 6668 | *prev++ = ' '; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 6669 | while (HTTP_IS_SPHT(*next)) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6670 | next++; |
| 6671 | } |
| 6672 | } |
| 6673 | return buffer_replace2(buf, prev, next, NULL, 0); |
| 6674 | } |
| 6675 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6676 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 6677 | * 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] | 6678 | * desirable to call it only when needed. This code is quite complex because |
| 6679 | * of the multiple very crappy and ambiguous syntaxes we have to support. it |
| 6680 | * highly recommended not to touch this part without a good reason ! |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6681 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6682 | void manage_client_side_cookies(struct stream *s, struct channel *req) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6683 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 6684 | struct http_txn *txn = s->txn; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6685 | struct session *sess = s->sess; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6686 | int preserve_hdr; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6687 | int cur_idx, old_idx; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6688 | char *hdr_beg, *hdr_end, *hdr_next, *del_from; |
| 6689 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6690 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6691 | /* Iterate through the headers, we start with the start line. */ |
Willy Tarreau | 83969f4 | 2007-01-22 08:55:47 +0100 | [diff] [blame] | 6692 | old_idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6693 | hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6694 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6695 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6696 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6697 | int val; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6698 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6699 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6700 | hdr_beg = hdr_next; |
| 6701 | hdr_end = hdr_beg + cur_hdr->len; |
| 6702 | hdr_next = hdr_end + cur_hdr->cr + 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6703 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6704 | /* We have one full header between hdr_beg and hdr_end, and the |
| 6705 | * next header starts at hdr_next. We're only interested in |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6706 | * "Cookie:" headers. |
| 6707 | */ |
| 6708 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6709 | val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6); |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6710 | if (!val) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6711 | old_idx = cur_idx; |
| 6712 | continue; |
| 6713 | } |
| 6714 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6715 | del_from = NULL; /* nothing to be deleted */ |
| 6716 | preserve_hdr = 0; /* assume we may kill the whole header */ |
| 6717 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6718 | /* Now look for cookies. Conforming to RFC2109, we have to support |
| 6719 | * attributes whose name begin with a '$', and associate them with |
| 6720 | * the right cookie, if we want to delete this cookie. |
| 6721 | * So there are 3 cases for each cookie read : |
| 6722 | * 1) it's a special attribute, beginning with a '$' : ignore it. |
| 6723 | * 2) it's a server id cookie that we *MAY* want to delete : save |
| 6724 | * some pointers on it (last semi-colon, beginning of cookie...) |
| 6725 | * 3) it's an application cookie : we *MAY* have to delete a previous |
| 6726 | * "special" cookie. |
| 6727 | * At the end of loop, if a "special" cookie remains, we may have to |
| 6728 | * remove it. If no application cookie persists in the header, we |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6729 | * *MUST* delete it. |
| 6730 | * |
| 6731 | * Note: RFC2965 is unclear about the processing of spaces around |
| 6732 | * the equal sign in the ATTR=VALUE form. A careful inspection of |
| 6733 | * the RFC explicitly allows spaces before it, and not within the |
| 6734 | * tokens (attrs or values). An inspection of RFC2109 allows that |
| 6735 | * too but section 10.1.3 lets one think that spaces may be allowed |
| 6736 | * after the equal sign too, resulting in some (rare) buggy |
| 6737 | * implementations trying to do that. So let's do what servers do. |
| 6738 | * Latest ietf draft forbids spaces all around. Also, earlier RFCs |
| 6739 | * allowed quoted strings in values, with any possible character |
| 6740 | * after a backslash, including control chars and delimitors, which |
| 6741 | * causes parsing to become ambiguous. Browsers also allow spaces |
| 6742 | * within values even without quotes. |
| 6743 | * |
| 6744 | * We have to keep multiple pointers in order to support cookie |
| 6745 | * removal at the beginning, middle or end of header without |
| 6746 | * corrupting the header. All of these headers are valid : |
| 6747 | * |
| 6748 | * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n |
| 6749 | * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n |
| 6750 | * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n |
| 6751 | * | | | | | | | | | |
| 6752 | * | | | | | | | | hdr_end <--+ |
| 6753 | * | | | | | | | +--> next |
| 6754 | * | | | | | | +----> val_end |
| 6755 | * | | | | | +-----------> val_beg |
| 6756 | * | | | | +--------------> equal |
| 6757 | * | | | +----------------> att_end |
| 6758 | * | | +---------------------> att_beg |
| 6759 | * | +--------------------------> prev |
| 6760 | * +--------------------------------> hdr_beg |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6761 | */ |
| 6762 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6763 | for (prev = hdr_beg + 6; prev < hdr_end; prev = next) { |
| 6764 | /* Iterate through all cookies on this line */ |
| 6765 | |
| 6766 | /* find att_beg */ |
| 6767 | att_beg = prev + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 6768 | while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg)) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6769 | att_beg++; |
| 6770 | |
| 6771 | /* find att_end : this is the first character after the last non |
| 6772 | * space before the equal. It may be equal to hdr_end. |
| 6773 | */ |
| 6774 | equal = att_end = att_beg; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6775 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6776 | while (equal < hdr_end) { |
| 6777 | if (*equal == '=' || *equal == ',' || *equal == ';') |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6778 | break; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 6779 | if (HTTP_IS_SPHT(*equal++)) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6780 | continue; |
| 6781 | att_end = equal; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6782 | } |
| 6783 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6784 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 6785 | * is between <att_beg> and <equal>, both may be identical. |
| 6786 | */ |
| 6787 | |
| 6788 | /* look for end of cookie if there is an equal sign */ |
| 6789 | if (equal < hdr_end && *equal == '=') { |
| 6790 | /* look for the beginning of the value */ |
| 6791 | val_beg = equal + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 6792 | while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg)) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6793 | val_beg++; |
| 6794 | |
| 6795 | /* find the end of the value, respecting quotes */ |
| 6796 | next = find_cookie_value_end(val_beg, hdr_end); |
| 6797 | |
| 6798 | /* make val_end point to the first white space or delimitor after the value */ |
| 6799 | val_end = next; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 6800 | while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1))) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6801 | val_end--; |
| 6802 | } else { |
| 6803 | val_beg = val_end = next = equal; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 6804 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6805 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6806 | /* We have nothing to do with attributes beginning with '$'. However, |
| 6807 | * they will automatically be removed if a header before them is removed, |
| 6808 | * since they're supposed to be linked together. |
| 6809 | */ |
| 6810 | if (*att_beg == '$') |
| 6811 | continue; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6812 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6813 | /* Ignore cookies with no equal sign */ |
| 6814 | if (equal == next) { |
| 6815 | /* This is not our cookie, so we must preserve it. But if we already |
| 6816 | * scheduled another cookie for removal, we cannot remove the |
| 6817 | * complete header, but we can remove the previous block itself. |
| 6818 | */ |
| 6819 | preserve_hdr = 1; |
| 6820 | if (del_from != NULL) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6821 | int delta = del_hdr_value(req->buf, &del_from, prev); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6822 | val_end += delta; |
| 6823 | next += delta; |
| 6824 | hdr_end += delta; |
| 6825 | hdr_next += delta; |
| 6826 | cur_hdr->len += delta; |
| 6827 | http_msg_move_end(&txn->req, delta); |
| 6828 | prev = del_from; |
| 6829 | del_from = NULL; |
| 6830 | } |
| 6831 | continue; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 6832 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6833 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6834 | /* if there are spaces around the equal sign, we need to |
| 6835 | * strip them otherwise we'll get trouble for cookie captures, |
| 6836 | * or even for rewrites. Since this happens extremely rarely, |
| 6837 | * it does not hurt performance. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6838 | */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6839 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 6840 | int stripped_before = 0; |
| 6841 | int stripped_after = 0; |
| 6842 | |
| 6843 | if (att_end != equal) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6844 | stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6845 | equal += stripped_before; |
| 6846 | val_beg += stripped_before; |
| 6847 | } |
| 6848 | |
| 6849 | if (val_beg > equal + 1) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6850 | stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6851 | val_beg += stripped_after; |
| 6852 | stripped_before += stripped_after; |
| 6853 | } |
| 6854 | |
| 6855 | val_end += stripped_before; |
| 6856 | next += stripped_before; |
| 6857 | hdr_end += stripped_before; |
| 6858 | hdr_next += stripped_before; |
| 6859 | cur_hdr->len += stripped_before; |
| 6860 | http_msg_move_end(&txn->req, stripped_before); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6861 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6862 | /* now everything is as on the diagram above */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6863 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6864 | /* First, let's see if we want to capture this cookie. We check |
| 6865 | * that we don't already have a client side cookie, because we |
| 6866 | * can only capture one. Also as an optimisation, we ignore |
| 6867 | * cookies shorter than the declared name. |
| 6868 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6869 | if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL && |
| 6870 | (val_end - att_beg >= sess->fe->capture_namelen) && |
| 6871 | memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6872 | int log_len = val_end - att_beg; |
| 6873 | |
| 6874 | if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) { |
| 6875 | Alert("HTTP logging : out of memory.\n"); |
| 6876 | } else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6877 | if (log_len > sess->fe->capture_len) |
| 6878 | log_len = sess->fe->capture_len; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6879 | memcpy(txn->cli_cookie, att_beg, log_len); |
| 6880 | txn->cli_cookie[log_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6881 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6882 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6883 | |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6884 | /* Persistence cookies in passive, rewrite or insert mode have the |
| 6885 | * following form : |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6886 | * |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6887 | * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]] |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6888 | * |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6889 | * For cookies in prefix mode, the form is : |
| 6890 | * |
| 6891 | * Cookie: NAME=SRV~VALUE |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6892 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6893 | if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) && |
| 6894 | (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) { |
| 6895 | struct server *srv = s->be->srv; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6896 | char *delim; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6897 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6898 | /* if we're in cookie prefix mode, we'll search the delimitor so that we |
| 6899 | * have the server ID between val_beg and delim, and the original cookie between |
| 6900 | * delim+1 and val_end. Otherwise, delim==val_end : |
| 6901 | * |
| 6902 | * Cookie: NAME=SRV; # in all but prefix modes |
| 6903 | * Cookie: NAME=SRV~OPAQUE ; # in prefix mode |
| 6904 | * | || || | |+-> next |
| 6905 | * | || || | +--> val_end |
| 6906 | * | || || +---------> delim |
| 6907 | * | || |+------------> val_beg |
| 6908 | * | || +-------------> att_end = equal |
| 6909 | * | |+-----------------> att_beg |
| 6910 | * | +------------------> prev |
| 6911 | * +-------------------------> hdr_beg |
| 6912 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6913 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6914 | if (s->be->ck_opts & PR_CK_PFX) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6915 | for (delim = val_beg; delim < val_end; delim++) |
| 6916 | if (*delim == COOKIE_DELIM) |
| 6917 | break; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6918 | } else { |
| 6919 | char *vbar1; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6920 | delim = val_end; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6921 | /* Now check if the cookie contains a date field, which would |
| 6922 | * appear after a vertical bar ('|') just after the server name |
| 6923 | * and before the delimiter. |
| 6924 | */ |
| 6925 | vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg); |
| 6926 | if (vbar1) { |
| 6927 | /* OK, so left of the bar is the server's cookie and |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6928 | * right is the last seen date. It is a base64 encoded |
| 6929 | * 30-bit value representing the UNIX date since the |
| 6930 | * epoch in 4-second quantities. |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6931 | */ |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6932 | int val; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6933 | delim = vbar1++; |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6934 | if (val_end - vbar1 >= 5) { |
| 6935 | val = b64tos30(vbar1); |
| 6936 | if (val > 0) |
| 6937 | txn->cookie_last_date = val << 2; |
| 6938 | } |
| 6939 | /* look for a second vertical bar */ |
| 6940 | vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1); |
| 6941 | if (vbar1 && (val_end - vbar1 > 5)) { |
| 6942 | val = b64tos30(vbar1 + 1); |
| 6943 | if (val > 0) |
| 6944 | txn->cookie_first_date = val << 2; |
| 6945 | } |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6946 | } |
| 6947 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6948 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6949 | /* if the cookie has an expiration date and the proxy wants to check |
| 6950 | * it, then we do that now. We first check if the cookie is too old, |
| 6951 | * then only if it has expired. We detect strict overflow because the |
| 6952 | * time resolution here is not great (4 seconds). Cookies with dates |
| 6953 | * in the future are ignored if their offset is beyond one day. This |
| 6954 | * allows an admin to fix timezone issues without expiring everyone |
| 6955 | * and at the same time avoids keeping unwanted side effects for too |
| 6956 | * long. |
| 6957 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6958 | if (txn->cookie_first_date && s->be->cookie_maxlife && |
| 6959 | (((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] | 6960 | ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) { |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6961 | txn->flags &= ~TX_CK_MASK; |
| 6962 | txn->flags |= TX_CK_OLD; |
| 6963 | delim = val_beg; // let's pretend we have not found the cookie |
| 6964 | txn->cookie_first_date = 0; |
| 6965 | txn->cookie_last_date = 0; |
| 6966 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6967 | else if (txn->cookie_last_date && s->be->cookie_maxidle && |
| 6968 | (((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] | 6969 | ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) { |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6970 | txn->flags &= ~TX_CK_MASK; |
| 6971 | txn->flags |= TX_CK_EXPIRED; |
| 6972 | delim = val_beg; // let's pretend we have not found the cookie |
| 6973 | txn->cookie_first_date = 0; |
| 6974 | txn->cookie_last_date = 0; |
| 6975 | } |
| 6976 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6977 | /* Here, we'll look for the first running server which supports the cookie. |
| 6978 | * This allows to share a same cookie between several servers, for example |
| 6979 | * to dedicate backup servers to specific servers only. |
| 6980 | * However, to prevent clients from sticking to cookie-less backup server |
| 6981 | * when they have incidentely learned an empty cookie, we simply ignore |
| 6982 | * empty cookies and mark them as invalid. |
| 6983 | * The same behaviour is applied when persistence must be ignored. |
| 6984 | */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6985 | if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED))) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6986 | srv = NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6987 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6988 | while (srv) { |
| 6989 | if (srv->cookie && (srv->cklen == delim - val_beg) && |
| 6990 | !memcmp(val_beg, srv->cookie, delim - val_beg)) { |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 6991 | if ((srv->cur_state != SRV_ST_STOPPED) || |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6992 | (s->be->options & PR_O_PERSIST) || |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6993 | (s->flags & SF_FORCE_PRST)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6994 | /* we found the server and we can use it */ |
| 6995 | txn->flags &= ~TX_CK_MASK; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 6996 | 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] | 6997 | s->flags |= SF_DIRECT | SF_ASSIGNED; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6998 | s->target = &srv->obj_type; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6999 | break; |
| 7000 | } else { |
| 7001 | /* we found a server, but it's down, |
| 7002 | * mark it as such and go on in case |
| 7003 | * another one is available. |
| 7004 | */ |
| 7005 | txn->flags &= ~TX_CK_MASK; |
| 7006 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7007 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7008 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7009 | srv = srv->next; |
| 7010 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7011 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7012 | 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] | 7013 | /* no server matched this cookie or we deliberately skipped it */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7014 | txn->flags &= ~TX_CK_MASK; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 7015 | if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED))) |
Willy Tarreau | c89ccb6 | 2012-04-05 21:18:22 +0200 | [diff] [blame] | 7016 | txn->flags |= TX_CK_UNUSED; |
| 7017 | else |
| 7018 | txn->flags |= TX_CK_INVALID; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7019 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7020 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7021 | /* depending on the cookie mode, we may have to either : |
| 7022 | * - delete the complete cookie if we're in insert+indirect mode, so that |
| 7023 | * the server never sees it ; |
| 7024 | * - remove the server id from the cookie value, and tag the cookie as an |
| 7025 | * application cookie so that it does not get accidentely removed later, |
| 7026 | * if we're in cookie prefix mode |
| 7027 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7028 | if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7029 | int delta; /* negative */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7030 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7031 | delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7032 | val_end += delta; |
| 7033 | next += delta; |
| 7034 | hdr_end += delta; |
| 7035 | hdr_next += delta; |
| 7036 | cur_hdr->len += delta; |
| 7037 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7038 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7039 | del_from = NULL; |
| 7040 | preserve_hdr = 1; /* we want to keep this cookie */ |
| 7041 | } |
| 7042 | else if (del_from == NULL && |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7043 | (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] | 7044 | del_from = prev; |
| 7045 | } |
| 7046 | } else { |
| 7047 | /* This is not our cookie, so we must preserve it. But if we already |
| 7048 | * scheduled another cookie for removal, we cannot remove the |
| 7049 | * complete header, but we can remove the previous block itself. |
| 7050 | */ |
| 7051 | preserve_hdr = 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7052 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7053 | if (del_from != NULL) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7054 | int delta = del_hdr_value(req->buf, &del_from, prev); |
Willy Tarreau | b810554 | 2010-11-24 18:31:28 +0100 | [diff] [blame] | 7055 | if (att_beg >= del_from) |
| 7056 | att_beg += delta; |
| 7057 | if (att_end >= del_from) |
| 7058 | att_end += delta; |
| 7059 | val_beg += delta; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7060 | val_end += delta; |
| 7061 | next += delta; |
| 7062 | hdr_end += delta; |
| 7063 | hdr_next += delta; |
| 7064 | cur_hdr->len += delta; |
| 7065 | http_msg_move_end(&txn->req, delta); |
| 7066 | prev = del_from; |
| 7067 | del_from = NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7068 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7069 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7070 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7071 | /* continue with next cookie on this header line */ |
| 7072 | att_beg = next; |
| 7073 | } /* for each cookie */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7074 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7075 | /* There are no more cookies on this line. |
| 7076 | * We may still have one (or several) marked for deletion at the |
| 7077 | * end of the line. We must do this now in two ways : |
| 7078 | * - if some cookies must be preserved, we only delete from the |
| 7079 | * mark to the end of line ; |
| 7080 | * - if nothing needs to be preserved, simply delete the whole header |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7081 | */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7082 | if (del_from) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7083 | int delta; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7084 | if (preserve_hdr) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7085 | delta = del_hdr_value(req->buf, &del_from, hdr_end); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7086 | hdr_end = del_from; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7087 | cur_hdr->len += delta; |
| 7088 | } else { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7089 | delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7090 | |
| 7091 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7092 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 7093 | txn->hdr_idx.used--; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7094 | cur_hdr->len = 0; |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 7095 | cur_idx = old_idx; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7096 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7097 | hdr_next += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7098 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7099 | } |
| 7100 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7101 | /* check next header */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7102 | old_idx = cur_idx; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7103 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7104 | } |
| 7105 | |
| 7106 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7107 | /* Iterate the same filter through all response headers contained in <rtr>. |
| 7108 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
| 7109 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7110 | 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] | 7111 | { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7112 | char *cur_ptr, *cur_end, *cur_next; |
| 7113 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7114 | struct http_txn *txn = s->txn; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7115 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7116 | int delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7117 | |
| 7118 | last_hdr = 0; |
| 7119 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7120 | cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7121 | old_idx = 0; |
| 7122 | |
| 7123 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7124 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7125 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7126 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7127 | (exp->action == ACT_ALLOW || |
| 7128 | exp->action == ACT_DENY)) |
| 7129 | return 0; |
| 7130 | |
| 7131 | cur_idx = txn->hdr_idx.v[old_idx].next; |
| 7132 | if (!cur_idx) |
| 7133 | break; |
| 7134 | |
| 7135 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 7136 | cur_ptr = cur_next; |
| 7137 | cur_end = cur_ptr + cur_hdr->len; |
| 7138 | cur_next = cur_end + cur_hdr->cr + 1; |
| 7139 | |
| 7140 | /* Now we have one header between cur_ptr and cur_end, |
| 7141 | * and the next header starts at cur_next. |
| 7142 | */ |
| 7143 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 7144 | 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] | 7145 | switch (exp->action) { |
| 7146 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7147 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7148 | last_hdr = 1; |
| 7149 | break; |
| 7150 | |
| 7151 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7152 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7153 | last_hdr = 1; |
| 7154 | break; |
| 7155 | |
| 7156 | case ACT_REPLACE: |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 7157 | trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch); |
| 7158 | if (trash.len < 0) |
| 7159 | return -1; |
| 7160 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7161 | 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] | 7162 | /* FIXME: if the user adds a newline in the replacement, the |
| 7163 | * index will not be recalculated for now, and the new line |
| 7164 | * will not be counted as a new header. |
| 7165 | */ |
| 7166 | |
| 7167 | cur_end += delta; |
| 7168 | cur_next += delta; |
| 7169 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7170 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7171 | break; |
| 7172 | |
| 7173 | case ACT_REMOVE: |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7174 | delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7175 | cur_next += delta; |
| 7176 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7177 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7178 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 7179 | txn->hdr_idx.used--; |
| 7180 | cur_hdr->len = 0; |
| 7181 | cur_end = NULL; /* null-term has been rewritten */ |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 7182 | cur_idx = old_idx; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7183 | break; |
| 7184 | |
| 7185 | } |
| 7186 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7187 | |
| 7188 | /* keep the link from this header to next one in case of later |
| 7189 | * removal of next header. |
| 7190 | */ |
| 7191 | old_idx = cur_idx; |
| 7192 | } |
| 7193 | return 0; |
| 7194 | } |
| 7195 | |
| 7196 | |
| 7197 | /* Apply the filter to the status line in the response buffer <rtr>. |
| 7198 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 7199 | * or -1 if a replacement resulted in an invalid status line. |
| 7200 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7201 | 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] | 7202 | { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7203 | char *cur_ptr, *cur_end; |
| 7204 | int done; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7205 | struct http_txn *txn = s->txn; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7206 | int delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7207 | |
| 7208 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7209 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7210 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7211 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7212 | (exp->action == ACT_ALLOW || |
| 7213 | exp->action == ACT_DENY)) |
| 7214 | return 0; |
| 7215 | else if (exp->action == ACT_REMOVE) |
| 7216 | return 0; |
| 7217 | |
| 7218 | done = 0; |
| 7219 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7220 | cur_ptr = rtr->buf->p; |
Willy Tarreau | 1ba0e5f | 2010-06-07 13:57:32 +0200 | [diff] [blame] | 7221 | cur_end = cur_ptr + txn->rsp.sl.st.l; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7222 | |
| 7223 | /* Now we have the status line between cur_ptr and cur_end */ |
| 7224 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 7225 | 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] | 7226 | switch (exp->action) { |
| 7227 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7228 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7229 | done = 1; |
| 7230 | break; |
| 7231 | |
| 7232 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7233 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7234 | done = 1; |
| 7235 | break; |
| 7236 | |
| 7237 | case ACT_REPLACE: |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 7238 | trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch); |
| 7239 | if (trash.len < 0) |
| 7240 | return -1; |
| 7241 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7242 | 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] | 7243 | /* FIXME: if the user adds a newline in the replacement, the |
| 7244 | * index will not be recalculated for now, and the new line |
| 7245 | * will not be counted as a new header. |
| 7246 | */ |
| 7247 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7248 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7249 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 7250 | cur_end = (char *)http_parse_stsline(&txn->rsp, |
Willy Tarreau | 0278576 | 2007-04-03 14:45:44 +0200 | [diff] [blame] | 7251 | HTTP_MSG_RPVER, |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7252 | cur_ptr, cur_end + 1, |
| 7253 | NULL, NULL); |
| 7254 | if (unlikely(!cur_end)) |
| 7255 | return -1; |
| 7256 | |
| 7257 | /* we have a full respnse and we know that we have either a CR |
| 7258 | * or an LF at <ptr>. |
| 7259 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7260 | 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] | 7261 | 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] | 7262 | /* there is no point trying this regex on headers */ |
| 7263 | return 1; |
| 7264 | } |
| 7265 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7266 | return done; |
| 7267 | } |
| 7268 | |
| 7269 | |
| 7270 | |
| 7271 | /* |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7272 | * 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] | 7273 | * Returns 0 if everything is alright, or -1 in case a replacement lead to an |
| 7274 | * unparsable response. |
| 7275 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7276 | 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] | 7277 | { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 7278 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7279 | struct http_txn *txn = s->txn; |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7280 | struct hdr_exp *exp; |
| 7281 | |
| 7282 | for (exp = px->rsp_exp; exp; exp = exp->next) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7283 | int ret; |
| 7284 | |
| 7285 | /* |
| 7286 | * The interleaving of transformations and verdicts |
| 7287 | * makes it difficult to decide to continue or stop |
| 7288 | * the evaluation. |
| 7289 | */ |
| 7290 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7291 | if (txn->flags & TX_SVDENY) |
| 7292 | break; |
| 7293 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7294 | if ((txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7295 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 7296 | exp->action == ACT_PASS)) { |
| 7297 | exp = exp->next; |
| 7298 | continue; |
| 7299 | } |
| 7300 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7301 | /* if this filter had a condition, evaluate it now and skip to |
| 7302 | * next filter if the condition does not match. |
| 7303 | */ |
| 7304 | if (exp->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 7305 | 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] | 7306 | ret = acl_pass(ret); |
| 7307 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 7308 | ret = !ret; |
| 7309 | if (!ret) |
| 7310 | continue; |
| 7311 | } |
| 7312 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7313 | /* Apply the filter to the status line. */ |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7314 | ret = apply_filter_to_sts_line(s, rtr, exp); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7315 | if (unlikely(ret < 0)) |
| 7316 | return -1; |
| 7317 | |
| 7318 | if (likely(ret == 0)) { |
| 7319 | /* The filter did not match the response, it can be |
| 7320 | * iterated through all headers. |
| 7321 | */ |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 7322 | if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0)) |
| 7323 | return -1; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7324 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7325 | } |
| 7326 | return 0; |
| 7327 | } |
| 7328 | |
| 7329 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7330 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 7331 | * 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] | 7332 | * desirable to call it only when needed. This function is also used when we |
| 7333 | * 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] | 7334 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7335 | void manage_server_side_cookies(struct stream *s, struct channel *res) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7336 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7337 | struct http_txn *txn = s->txn; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7338 | struct session *sess = s->sess; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 7339 | struct server *srv; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7340 | int is_cookie2; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7341 | int cur_idx, old_idx, delta; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7342 | char *hdr_beg, *hdr_end, *hdr_next; |
| 7343 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7344 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7345 | /* Iterate through the headers. |
| 7346 | * we start with the start line. |
| 7347 | */ |
| 7348 | old_idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7349 | hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7350 | |
| 7351 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 7352 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7353 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7354 | |
| 7355 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7356 | hdr_beg = hdr_next; |
| 7357 | hdr_end = hdr_beg + cur_hdr->len; |
| 7358 | hdr_next = hdr_end + cur_hdr->cr + 1; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7359 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7360 | /* We have one full header between hdr_beg and hdr_end, and the |
| 7361 | * next header starts at hdr_next. We're only interested in |
| 7362 | * "Set-Cookie" and "Set-Cookie2" headers. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7363 | */ |
| 7364 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7365 | is_cookie2 = 0; |
| 7366 | prev = hdr_beg + 10; |
| 7367 | val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10); |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7368 | if (!val) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7369 | val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11); |
| 7370 | if (!val) { |
| 7371 | old_idx = cur_idx; |
| 7372 | continue; |
| 7373 | } |
| 7374 | is_cookie2 = 1; |
| 7375 | prev = hdr_beg + 11; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7376 | } |
| 7377 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7378 | /* OK, right now we know we have a Set-Cookie* at hdr_beg, and |
| 7379 | * <prev> points to the colon. |
| 7380 | */ |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7381 | txn->flags |= TX_SCK_PRESENT; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7382 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7383 | /* Maybe we only wanted to see if there was a Set-Cookie (eg: |
| 7384 | * check-cache is enabled) and we are not interested in checking |
| 7385 | * them. Warning, the cookie capture is declared in the frontend. |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 7386 | */ |
Willy Tarreau | 53a09d5 | 2015-08-10 18:59:40 +0200 | [diff] [blame] | 7387 | if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7388 | return; |
| 7389 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7390 | /* OK so now we know we have to process this response cookie. |
| 7391 | * The format of the Set-Cookie header is slightly different |
| 7392 | * from the format of the Cookie header in that it does not |
| 7393 | * support the comma as a cookie delimiter (thus the header |
| 7394 | * cannot be folded) because the Expires attribute described in |
| 7395 | * the original Netscape's spec may contain an unquoted date |
| 7396 | * with a comma inside. We have to live with this because |
| 7397 | * many browsers don't support Max-Age and some browsers don't |
| 7398 | * support quoted strings. However the Set-Cookie2 header is |
| 7399 | * clean. |
| 7400 | * |
| 7401 | * We have to keep multiple pointers in order to support cookie |
| 7402 | * removal at the beginning, middle or end of header without |
| 7403 | * corrupting the header (in case of set-cookie2). A special |
| 7404 | * pointer, <scav> points to the beginning of the set-cookie-av |
| 7405 | * fields after the first semi-colon. The <next> pointer points |
| 7406 | * either to the end of line (set-cookie) or next unquoted comma |
| 7407 | * (set-cookie2). All of these headers are valid : |
| 7408 | * |
| 7409 | * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n |
| 7410 | * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n |
| 7411 | * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n |
| 7412 | * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n |
| 7413 | * | | | | | | | | | | |
| 7414 | * | | | | | | | | +-> next hdr_end <--+ |
| 7415 | * | | | | | | | +------------> scav |
| 7416 | * | | | | | | +--------------> val_end |
| 7417 | * | | | | | +--------------------> val_beg |
| 7418 | * | | | | +----------------------> equal |
| 7419 | * | | | +------------------------> att_end |
| 7420 | * | | +----------------------------> att_beg |
| 7421 | * | +------------------------------> prev |
| 7422 | * +-----------------------------------------> hdr_beg |
| 7423 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7424 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7425 | for (; prev < hdr_end; prev = next) { |
| 7426 | /* Iterate through all cookies on this line */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7427 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7428 | /* find att_beg */ |
| 7429 | att_beg = prev + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 7430 | while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg)) |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7431 | att_beg++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7432 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7433 | /* find att_end : this is the first character after the last non |
| 7434 | * space before the equal. It may be equal to hdr_end. |
| 7435 | */ |
| 7436 | equal = att_end = att_beg; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7437 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7438 | while (equal < hdr_end) { |
| 7439 | if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ',')) |
| 7440 | break; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 7441 | if (HTTP_IS_SPHT(*equal++)) |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7442 | continue; |
| 7443 | att_end = equal; |
| 7444 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7445 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7446 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 7447 | * is between <att_beg> and <equal>, both may be identical. |
| 7448 | */ |
| 7449 | |
| 7450 | /* look for end of cookie if there is an equal sign */ |
| 7451 | if (equal < hdr_end && *equal == '=') { |
| 7452 | /* look for the beginning of the value */ |
| 7453 | val_beg = equal + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 7454 | while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg)) |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7455 | val_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 | /* find the end of the value, respecting quotes */ |
| 7458 | next = find_cookie_value_end(val_beg, hdr_end); |
| 7459 | |
| 7460 | /* make val_end point to the first white space or delimitor after the value */ |
| 7461 | val_end = next; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 7462 | while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1))) |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7463 | val_end--; |
| 7464 | } else { |
| 7465 | /* <equal> points to next comma, semi-colon or EOL */ |
| 7466 | val_beg = val_end = next = equal; |
| 7467 | } |
| 7468 | |
| 7469 | if (next < hdr_end) { |
| 7470 | /* Set-Cookie2 supports multiple cookies, and <next> points to |
| 7471 | * a colon or semi-colon before the end. So skip all attr-value |
| 7472 | * pairs and look for the next comma. For Set-Cookie, since |
| 7473 | * commas are permitted in values, skip to the end. |
| 7474 | */ |
| 7475 | if (is_cookie2) |
| 7476 | next = find_hdr_value_end(next, hdr_end); |
| 7477 | else |
| 7478 | next = hdr_end; |
| 7479 | } |
| 7480 | |
| 7481 | /* Now everything is as on the diagram above */ |
| 7482 | |
| 7483 | /* Ignore cookies with no equal sign */ |
| 7484 | if (equal == val_end) |
| 7485 | continue; |
| 7486 | |
| 7487 | /* If there are spaces around the equal sign, we need to |
| 7488 | * strip them otherwise we'll get trouble for cookie captures, |
| 7489 | * or even for rewrites. Since this happens extremely rarely, |
| 7490 | * it does not hurt performance. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7491 | */ |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7492 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 7493 | int stripped_before = 0; |
| 7494 | int stripped_after = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7495 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7496 | if (att_end != equal) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7497 | stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7498 | equal += stripped_before; |
| 7499 | val_beg += stripped_before; |
| 7500 | } |
| 7501 | |
| 7502 | if (val_beg > equal + 1) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7503 | stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7504 | val_beg += stripped_after; |
| 7505 | stripped_before += stripped_after; |
| 7506 | } |
| 7507 | |
| 7508 | val_end += stripped_before; |
| 7509 | next += stripped_before; |
| 7510 | hdr_end += stripped_before; |
| 7511 | hdr_next += stripped_before; |
| 7512 | cur_hdr->len += stripped_before; |
Willy Tarreau | 1fc1f45 | 2011-04-07 22:35:37 +0200 | [diff] [blame] | 7513 | http_msg_move_end(&txn->rsp, stripped_before); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7514 | } |
| 7515 | |
| 7516 | /* First, let's see if we want to capture this cookie. We check |
| 7517 | * that we don't already have a server side cookie, because we |
| 7518 | * can only capture one. Also as an optimisation, we ignore |
| 7519 | * cookies shorter than the declared name. |
| 7520 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7521 | if (sess->fe->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 7522 | txn->srv_cookie == NULL && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7523 | (val_end - att_beg >= sess->fe->capture_namelen) && |
| 7524 | memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7525 | int log_len = val_end - att_beg; |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 7526 | if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7527 | Alert("HTTP logging : out of memory.\n"); |
| 7528 | } |
Willy Tarreau | f70fc75 | 2010-11-19 11:27:18 +0100 | [diff] [blame] | 7529 | else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7530 | if (log_len > sess->fe->capture_len) |
| 7531 | log_len = sess->fe->capture_len; |
Willy Tarreau | f70fc75 | 2010-11-19 11:27:18 +0100 | [diff] [blame] | 7532 | memcpy(txn->srv_cookie, att_beg, log_len); |
| 7533 | txn->srv_cookie[log_len] = 0; |
| 7534 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7535 | } |
| 7536 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7537 | srv = objt_server(s->target); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7538 | /* now check if we need to process it for persistence */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 7539 | if (!(s->flags & SF_IGNORE_PRST) && |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7540 | (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) && |
| 7541 | (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) { |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7542 | /* assume passive cookie by default */ |
| 7543 | txn->flags &= ~TX_SCK_MASK; |
| 7544 | txn->flags |= TX_SCK_FOUND; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7545 | |
| 7546 | /* If the cookie is in insert mode on a known server, we'll delete |
| 7547 | * this occurrence because we'll insert another one later. |
| 7548 | * 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] | 7549 | * a direct access. |
| 7550 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7551 | if (s->be->ck_opts & PR_CK_PSV) { |
Willy Tarreau | ba4c5be | 2010-10-23 12:46:42 +0200 | [diff] [blame] | 7552 | /* The "preserve" flag was set, we don't want to touch the |
| 7553 | * server's cookie. |
| 7554 | */ |
| 7555 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7556 | else if ((srv && (s->be->ck_opts & PR_CK_INS)) || |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 7557 | ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7558 | /* this cookie must be deleted */ |
| 7559 | if (*prev == ':' && next == hdr_end) { |
| 7560 | /* whole header */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7561 | delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7562 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 7563 | txn->hdr_idx.used--; |
| 7564 | cur_hdr->len = 0; |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 7565 | cur_idx = old_idx; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7566 | hdr_next += delta; |
| 7567 | http_msg_move_end(&txn->rsp, delta); |
| 7568 | /* note: while both invalid now, <next> and <hdr_end> |
| 7569 | * are still equal, so the for() will stop as expected. |
| 7570 | */ |
| 7571 | } else { |
| 7572 | /* just remove the value */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7573 | int delta = del_hdr_value(res->buf, &prev, next); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7574 | next = prev; |
| 7575 | hdr_end += delta; |
| 7576 | hdr_next += delta; |
| 7577 | cur_hdr->len += delta; |
| 7578 | http_msg_move_end(&txn->rsp, delta); |
| 7579 | } |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7580 | txn->flags &= ~TX_SCK_MASK; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7581 | txn->flags |= TX_SCK_DELETED; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7582 | /* and go on with next cookie */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7583 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7584 | else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7585 | /* replace bytes val_beg->val_end with the cookie name associated |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7586 | * with this server since we know it. |
| 7587 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7588 | 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] | 7589 | next += delta; |
| 7590 | hdr_end += delta; |
| 7591 | hdr_next += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7592 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7593 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7594 | |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7595 | txn->flags &= ~TX_SCK_MASK; |
| 7596 | txn->flags |= TX_SCK_REPLACED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7597 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7598 | else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7599 | /* insert the cookie name associated with this server |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7600 | * before existing cookie, and insert a delimiter between them.. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7601 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7602 | 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] | 7603 | next += delta; |
| 7604 | hdr_end += delta; |
| 7605 | hdr_next += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7606 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7607 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7608 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 7609 | val_beg[srv->cklen] = COOKIE_DELIM; |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7610 | txn->flags &= ~TX_SCK_MASK; |
| 7611 | txn->flags |= TX_SCK_REPLACED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7612 | } |
| 7613 | } |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7614 | /* that's done for this cookie, check the next one on the same |
| 7615 | * line when next != hdr_end (only if is_cookie2). |
| 7616 | */ |
| 7617 | } |
| 7618 | /* check next header */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7619 | old_idx = cur_idx; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7620 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7621 | } |
| 7622 | |
| 7623 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7624 | /* |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7625 | * Check if response is cacheable or not. Updates s->flags. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7626 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7627 | void check_response_for_cacheability(struct stream *s, struct channel *rtr) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7628 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7629 | struct http_txn *txn = s->txn; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7630 | char *p1, *p2; |
| 7631 | |
| 7632 | char *cur_ptr, *cur_end, *cur_next; |
| 7633 | int cur_idx; |
| 7634 | |
Willy Tarreau | 5df5187 | 2007-11-25 16:20:08 +0100 | [diff] [blame] | 7635 | if (!(txn->flags & TX_CACHEABLE)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7636 | return; |
| 7637 | |
| 7638 | /* Iterate through the headers. |
| 7639 | * we start with the start line. |
| 7640 | */ |
| 7641 | cur_idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7642 | cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7643 | |
| 7644 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 7645 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7646 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7647 | |
| 7648 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 7649 | cur_ptr = cur_next; |
| 7650 | cur_end = cur_ptr + cur_hdr->len; |
| 7651 | cur_next = cur_end + cur_hdr->cr + 1; |
| 7652 | |
| 7653 | /* We have one full header between cur_ptr and cur_end, and the |
| 7654 | * next header starts at cur_next. We're only interested in |
| 7655 | * "Cookie:" headers. |
| 7656 | */ |
| 7657 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7658 | val = http_header_match2(cur_ptr, cur_end, "Pragma", 6); |
| 7659 | if (val) { |
| 7660 | if ((cur_end - (cur_ptr + val) >= 8) && |
| 7661 | strncasecmp(cur_ptr + val, "no-cache", 8) == 0) { |
| 7662 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 7663 | return; |
| 7664 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7665 | } |
| 7666 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7667 | val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13); |
| 7668 | if (!val) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7669 | continue; |
| 7670 | |
| 7671 | /* OK, right now we know we have a cache-control header at cur_ptr */ |
| 7672 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7673 | p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7674 | |
| 7675 | if (p1 >= cur_end) /* no more info */ |
| 7676 | continue; |
| 7677 | |
| 7678 | /* p1 is at the beginning of the value */ |
| 7679 | p2 = p1; |
| 7680 | |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 7681 | while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7682 | p2++; |
| 7683 | |
| 7684 | /* we have a complete value between p1 and p2 */ |
| 7685 | if (p2 < cur_end && *p2 == '=') { |
| 7686 | /* we have something of the form no-cache="set-cookie" */ |
| 7687 | if ((cur_end - p1 >= 21) && |
| 7688 | strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0 |
| 7689 | && (p1[20] == '"' || p1[20] == ',')) |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7690 | txn->flags &= ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7691 | continue; |
| 7692 | } |
| 7693 | |
| 7694 | /* OK, so we know that either p2 points to the end of string or to a comma */ |
| 7695 | if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) || |
Willy Tarreau | 5b15f90 | 2013-07-04 12:46:56 +0200 | [diff] [blame] | 7696 | ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) || |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7697 | ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) || |
| 7698 | ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) || |
| 7699 | ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7700 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7701 | return; |
| 7702 | } |
| 7703 | |
| 7704 | if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7705 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7706 | continue; |
| 7707 | } |
| 7708 | } |
| 7709 | } |
| 7710 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7711 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7712 | /* |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7713 | * 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] | 7714 | * for the current backend. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7715 | * |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7716 | * 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] | 7717 | * uri_auth field is valid. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7718 | * |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 7719 | * Returns 1 if stats should be provided, otherwise 0. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7720 | */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7721 | 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] | 7722 | { |
| 7723 | struct uri_auth *uri_auth = backend->uri_auth; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7724 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7725 | const char *uri = msg->chn->buf->p+ msg->sl.rq.u; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7726 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 7727 | if (!uri_auth) |
| 7728 | return 0; |
| 7729 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7730 | 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] | 7731 | return 0; |
| 7732 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7733 | /* check URI size */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7734 | if (uri_auth->uri_len > msg->sl.rq.u_l) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7735 | return 0; |
| 7736 | |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 7737 | if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7738 | return 0; |
| 7739 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7740 | return 1; |
| 7741 | } |
| 7742 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7743 | /* |
| 7744 | * 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] | 7745 | * By default it tries to report the error position as msg->err_pos. However if |
| 7746 | * this one is not set, it will then report msg->next, which is the last known |
| 7747 | * 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] | 7748 | * displays buffers as a contiguous area starting at buf->p. |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7749 | */ |
Emeric Brun | 8c1aaa2 | 2017-06-15 11:30:06 +0200 | [diff] [blame] | 7750 | 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] | 7751 | struct http_msg *msg, |
Willy Tarreau | 0da5b3b | 2017-09-21 09:30:46 +0200 | [diff] [blame] | 7752 | enum h1_state state, struct proxy *other_end) |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7753 | { |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 7754 | struct session *sess = strm_sess(s); |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 7755 | struct channel *chn = msg->chn; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 7756 | int len1, len2; |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 7757 | |
Emeric Brun | 8c1aaa2 | 2017-06-15 11:30:06 +0200 | [diff] [blame] | 7758 | SPIN_LOCK(PROXY_LOCK, &proxy->lock); |
Willy Tarreau | f3764b7 | 2016-03-31 13:45:10 +0200 | [diff] [blame] | 7759 | es->len = MIN(chn->buf->i, global.tune.bufsize); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7760 | len1 = chn->buf->data + chn->buf->size - chn->buf->p; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 7761 | len1 = MIN(len1, es->len); |
| 7762 | len2 = es->len - len1; /* remaining data if buffer wraps */ |
| 7763 | |
Willy Tarreau | f3764b7 | 2016-03-31 13:45:10 +0200 | [diff] [blame] | 7764 | if (!es->buf) |
| 7765 | es->buf = malloc(global.tune.bufsize); |
| 7766 | |
| 7767 | if (es->buf) { |
| 7768 | memcpy(es->buf, chn->buf->p, len1); |
| 7769 | if (len2) |
| 7770 | memcpy(es->buf + len1, chn->buf->data, len2); |
| 7771 | } |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 7772 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7773 | if (msg->err_pos >= 0) |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 7774 | es->pos = msg->err_pos; |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 7775 | else |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 7776 | es->pos = msg->next; |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 7777 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7778 | es->when = date; // user-visible date |
| 7779 | es->sid = s->uniq_id; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 7780 | es->srv = objt_server(s->target); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7781 | es->oe = other_end; |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 7782 | if (objt_conn(sess->origin)) |
| 7783 | es->src = __objt_conn(sess->origin)->addr.from; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7784 | else |
| 7785 | memset(&es->src, 0, sizeof(es->src)); |
| 7786 | |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 7787 | es->state = state; |
Willy Tarreau | 10479e4 | 2010-12-12 14:00:34 +0100 | [diff] [blame] | 7788 | es->ev_id = error_snapshot_id++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 7789 | es->b_flags = chn->flags; |
Willy Tarreau | d04b1bc | 2012-05-08 11:03:10 +0200 | [diff] [blame] | 7790 | es->s_flags = s->flags; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7791 | es->t_flags = s->txn->flags; |
Willy Tarreau | d04b1bc | 2012-05-08 11:03:10 +0200 | [diff] [blame] | 7792 | es->m_flags = msg->flags; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7793 | es->b_out = chn->buf->o; |
| 7794 | es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 7795 | es->b_tot = chn->total; |
Willy Tarreau | d04b1bc | 2012-05-08 11:03:10 +0200 | [diff] [blame] | 7796 | es->m_clen = msg->chunk_len; |
| 7797 | es->m_blen = msg->body_len; |
Emeric Brun | 8c1aaa2 | 2017-06-15 11:30:06 +0200 | [diff] [blame] | 7798 | SPIN_UNLOCK(PROXY_LOCK, &proxy->lock); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7799 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7800 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7801 | /* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of |
| 7802 | * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is |
| 7803 | * performed over the whole headers. Otherwise it must contain a valid header |
| 7804 | * context, initialised with ctx->idx=0 for the first lookup in a series. If |
| 7805 | * <occ> is positive or null, occurrence #occ from the beginning (or last ctx) |
| 7806 | * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less |
| 7807 | * 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] | 7808 | * -1. The value fetch stops at commas, so this function is suited for use with |
| 7809 | * list headers. |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7810 | * 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] | 7811 | */ |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 7812 | 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] | 7813 | struct hdr_idx *idx, int occ, |
| 7814 | struct hdr_ctx *ctx, char **vptr, int *vlen) |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7815 | { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7816 | struct hdr_ctx local_ctx; |
| 7817 | char *ptr_hist[MAX_HDR_HISTORY]; |
| 7818 | int len_hist[MAX_HDR_HISTORY]; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7819 | unsigned int hist_ptr; |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7820 | int found; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7821 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7822 | if (!ctx) { |
| 7823 | local_ctx.idx = 0; |
| 7824 | ctx = &local_ctx; |
| 7825 | } |
| 7826 | |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7827 | if (occ >= 0) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7828 | /* search from the beginning */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7829 | while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7830 | occ--; |
| 7831 | if (occ <= 0) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7832 | *vptr = ctx->line + ctx->val; |
| 7833 | *vlen = ctx->vlen; |
| 7834 | return 1; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7835 | } |
| 7836 | } |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7837 | return 0; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7838 | } |
| 7839 | |
| 7840 | /* negative occurrence, we scan all the list then walk back */ |
| 7841 | if (-occ > MAX_HDR_HISTORY) |
| 7842 | return 0; |
| 7843 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7844 | found = hist_ptr = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7845 | while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7846 | ptr_hist[hist_ptr] = ctx->line + ctx->val; |
| 7847 | len_hist[hist_ptr] = ctx->vlen; |
| 7848 | if (++hist_ptr >= MAX_HDR_HISTORY) |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7849 | hist_ptr = 0; |
| 7850 | found++; |
| 7851 | } |
| 7852 | if (-occ > found) |
| 7853 | return 0; |
| 7854 | /* 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] | 7855 | * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have |
| 7856 | * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ] |
| 7857 | * to remain in the 0..9 range. |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7858 | */ |
Willy Tarreau | 67dad27 | 2013-06-12 22:27:44 +0200 | [diff] [blame] | 7859 | hist_ptr += occ + MAX_HDR_HISTORY; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7860 | if (hist_ptr >= MAX_HDR_HISTORY) |
| 7861 | hist_ptr -= MAX_HDR_HISTORY; |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7862 | *vptr = ptr_hist[hist_ptr]; |
| 7863 | *vlen = len_hist[hist_ptr]; |
| 7864 | return 1; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7865 | } |
| 7866 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 7867 | /* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of |
| 7868 | * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is |
| 7869 | * performed over the whole headers. Otherwise it must contain a valid header |
| 7870 | * context, initialised with ctx->idx=0 for the first lookup in a series. If |
| 7871 | * <occ> is positive or null, occurrence #occ from the beginning (or last ctx) |
| 7872 | * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less |
| 7873 | * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is |
| 7874 | * -1. This function differs from http_get_hdr() in that it only returns full |
| 7875 | * line header values and does not stop at commas. |
| 7876 | * The return value is 0 if nothing was found, or non-zero otherwise. |
| 7877 | */ |
| 7878 | unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen, |
| 7879 | struct hdr_idx *idx, int occ, |
| 7880 | struct hdr_ctx *ctx, char **vptr, int *vlen) |
| 7881 | { |
| 7882 | struct hdr_ctx local_ctx; |
| 7883 | char *ptr_hist[MAX_HDR_HISTORY]; |
| 7884 | int len_hist[MAX_HDR_HISTORY]; |
| 7885 | unsigned int hist_ptr; |
| 7886 | int found; |
| 7887 | |
| 7888 | if (!ctx) { |
| 7889 | local_ctx.idx = 0; |
| 7890 | ctx = &local_ctx; |
| 7891 | } |
| 7892 | |
| 7893 | if (occ >= 0) { |
| 7894 | /* search from the beginning */ |
| 7895 | while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
| 7896 | occ--; |
| 7897 | if (occ <= 0) { |
| 7898 | *vptr = ctx->line + ctx->val; |
| 7899 | *vlen = ctx->vlen; |
| 7900 | return 1; |
| 7901 | } |
| 7902 | } |
| 7903 | return 0; |
| 7904 | } |
| 7905 | |
| 7906 | /* negative occurrence, we scan all the list then walk back */ |
| 7907 | if (-occ > MAX_HDR_HISTORY) |
| 7908 | return 0; |
| 7909 | |
| 7910 | found = hist_ptr = 0; |
| 7911 | while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
| 7912 | ptr_hist[hist_ptr] = ctx->line + ctx->val; |
| 7913 | len_hist[hist_ptr] = ctx->vlen; |
| 7914 | if (++hist_ptr >= MAX_HDR_HISTORY) |
| 7915 | hist_ptr = 0; |
| 7916 | found++; |
| 7917 | } |
| 7918 | if (-occ > found) |
| 7919 | return 0; |
Nenad Merdanovic | 69ad4b9 | 2016-03-29 13:14:30 +0200 | [diff] [blame] | 7920 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 7921 | /* 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] | 7922 | * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have |
| 7923 | * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ] |
| 7924 | * to remain in the 0..9 range. |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 7925 | */ |
Nenad Merdanovic | 69ad4b9 | 2016-03-29 13:14:30 +0200 | [diff] [blame] | 7926 | hist_ptr += occ + MAX_HDR_HISTORY; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 7927 | if (hist_ptr >= MAX_HDR_HISTORY) |
| 7928 | hist_ptr -= MAX_HDR_HISTORY; |
| 7929 | *vptr = ptr_hist[hist_ptr]; |
| 7930 | *vlen = len_hist[hist_ptr]; |
| 7931 | return 1; |
| 7932 | } |
| 7933 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 7934 | /* |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 7935 | * Print a debug line with a header. Always stop at the first CR or LF char, |
| 7936 | * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an |
| 7937 | * arrow is printed after the line which contains the pointer. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7938 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7939 | 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] | 7940 | { |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 7941 | struct session *sess = strm_sess(s); |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7942 | int max; |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 7943 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7944 | 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] | 7945 | dir, |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 7946 | 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] | 7947 | 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] | 7948 | |
| 7949 | for (max = 0; start + max < end; max++) |
| 7950 | if (start[max] == '\r' || start[max] == '\n') |
| 7951 | break; |
| 7952 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7953 | UBOUND(max, trash.size - trash.len - 3); |
| 7954 | trash.len += strlcpy2(trash.str + trash.len, start, max + 1); |
| 7955 | trash.str[trash.len++] = '\n'; |
Willy Tarreau | 89efaed | 2013-12-13 15:14:55 +0100 | [diff] [blame] | 7956 | shut_your_big_mouth_gcc(write(1, trash.str, trash.len)); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7957 | } |
| 7958 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7959 | |
| 7960 | /* Allocate a new HTTP transaction for stream <s> unless there is one already. |
| 7961 | * The hdr_idx is allocated as well. In case of allocation failure, everything |
| 7962 | * allocated is freed and NULL is returned. Otherwise the new transaction is |
| 7963 | * assigned to the stream and returned. |
| 7964 | */ |
| 7965 | struct http_txn *http_alloc_txn(struct stream *s) |
| 7966 | { |
| 7967 | struct http_txn *txn = s->txn; |
| 7968 | |
| 7969 | if (txn) |
| 7970 | return txn; |
| 7971 | |
| 7972 | txn = pool_alloc2(pool2_http_txn); |
| 7973 | if (!txn) |
| 7974 | return txn; |
| 7975 | |
| 7976 | txn->hdr_idx.size = global.tune.max_http_hdr; |
| 7977 | txn->hdr_idx.v = pool_alloc2(pool2_hdr_idx); |
| 7978 | if (!txn->hdr_idx.v) { |
| 7979 | pool_free2(pool2_http_txn, txn); |
| 7980 | return NULL; |
| 7981 | } |
| 7982 | |
| 7983 | s->txn = txn; |
| 7984 | return txn; |
| 7985 | } |
| 7986 | |
Thierry FOURNIER | fd50f0b | 2015-09-25 18:53:18 +0200 | [diff] [blame] | 7987 | void http_txn_reset_req(struct http_txn *txn) |
| 7988 | { |
| 7989 | txn->req.flags = 0; |
| 7990 | txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */ |
| 7991 | txn->req.next = 0; |
| 7992 | txn->req.chunk_len = 0LL; |
| 7993 | txn->req.body_len = 0LL; |
| 7994 | txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */ |
| 7995 | } |
| 7996 | |
| 7997 | void http_txn_reset_res(struct http_txn *txn) |
| 7998 | { |
| 7999 | txn->rsp.flags = 0; |
| 8000 | txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */ |
| 8001 | txn->rsp.next = 0; |
| 8002 | txn->rsp.chunk_len = 0LL; |
| 8003 | txn->rsp.body_len = 0LL; |
| 8004 | txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */ |
| 8005 | } |
| 8006 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8007 | /* |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8008 | * 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] | 8009 | * the required fields are properly allocated and that we only need to (re)init |
| 8010 | * them. This should be used before processing any new request. |
| 8011 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8012 | void http_init_txn(struct stream *s) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8013 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8014 | struct http_txn *txn = s->txn; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 8015 | struct proxy *fe = strm_fe(s); |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8016 | |
| 8017 | txn->flags = 0; |
| 8018 | txn->status = -1; |
| 8019 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 8020 | txn->cookie_first_date = 0; |
| 8021 | txn->cookie_last_date = 0; |
| 8022 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8023 | txn->srv_cookie = NULL; |
| 8024 | txn->cli_cookie = NULL; |
| 8025 | txn->uri = NULL; |
| 8026 | |
Thierry FOURNIER | fd50f0b | 2015-09-25 18:53:18 +0200 | [diff] [blame] | 8027 | http_txn_reset_req(txn); |
| 8028 | http_txn_reset_res(txn); |
| 8029 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 8030 | txn->req.chn = &s->req; |
| 8031 | txn->rsp.chn = &s->res; |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 8032 | |
| 8033 | txn->auth.method = HTTP_AUTH_UNKNOWN; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8034 | |
| 8035 | txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */ |
| 8036 | if (fe->options2 & PR_O2_REQBUG_OK) |
| 8037 | txn->req.err_pos = -1; /* let buggy requests pass */ |
| 8038 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8039 | if (txn->hdr_idx.v) |
| 8040 | hdr_idx_init(&txn->hdr_idx); |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 8041 | |
| 8042 | vars_init(&s->vars_txn, SCOPE_TXN); |
| 8043 | vars_init(&s->vars_reqres, SCOPE_REQ); |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8044 | } |
| 8045 | |
| 8046 | /* to be used at the end of a transaction */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8047 | void http_end_txn(struct stream *s) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8048 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8049 | struct http_txn *txn = s->txn; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 8050 | struct proxy *fe = strm_fe(s); |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8051 | |
| 8052 | /* these ones will have been dynamically allocated */ |
| 8053 | pool_free2(pool2_requri, txn->uri); |
| 8054 | pool_free2(pool2_capture, txn->cli_cookie); |
| 8055 | pool_free2(pool2_capture, txn->srv_cookie); |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 8056 | pool_free2(pool2_uniqueid, s->unique_id); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 8057 | |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 8058 | s->unique_id = NULL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8059 | txn->uri = NULL; |
| 8060 | txn->srv_cookie = NULL; |
| 8061 | txn->cli_cookie = NULL; |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8062 | |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 8063 | if (s->req_cap) { |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8064 | struct cap_hdr *h; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 8065 | for (h = fe->req_cap; h; h = h->next) |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 8066 | pool_free2(h->pool, s->req_cap[h->index]); |
| 8067 | memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *)); |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8068 | } |
| 8069 | |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 8070 | if (s->res_cap) { |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8071 | struct cap_hdr *h; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 8072 | for (h = fe->rsp_cap; h; h = h->next) |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 8073 | pool_free2(h->pool, s->res_cap[h->index]); |
| 8074 | memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *)); |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8075 | } |
| 8076 | |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 8077 | vars_prune(&s->vars_txn, s->sess, s); |
| 8078 | vars_prune(&s->vars_reqres, s->sess, s); |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8079 | } |
| 8080 | |
| 8081 | /* 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] | 8082 | void http_reset_txn(struct stream *s) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8083 | { |
| 8084 | http_end_txn(s); |
| 8085 | http_init_txn(s); |
| 8086 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 8087 | /* reinitialise the current rule list pointer to NULL. We are sure that |
| 8088 | * any rulelist match the NULL pointer. |
| 8089 | */ |
| 8090 | s->current_rule_list = NULL; |
| 8091 | |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 8092 | s->be = strm_fe(s); |
| 8093 | s->logs.logwait = strm_fe(s)->to_log; |
Willy Tarreau | abcd514 | 2013-06-11 17:18:02 +0200 | [diff] [blame] | 8094 | s->logs.level = 0; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8095 | stream_del_srv_conn(s); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 8096 | s->target = NULL; |
Emeric Brun | b982a3d | 2010-01-04 15:45:53 +0100 | [diff] [blame] | 8097 | /* re-init store persistence */ |
| 8098 | s->store_count = 0; |
Willy Tarreau | 1f0da24 | 2014-01-25 11:01:50 +0100 | [diff] [blame] | 8099 | s->uniq_id = global.req_count++; |
Emeric Brun | b982a3d | 2010-01-04 15:45:53 +0100 | [diff] [blame] | 8100 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8101 | s->pend_pos = NULL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8102 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 8103 | s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */ |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8104 | |
Willy Tarreau | 739cfba | 2010-01-25 23:11:14 +0100 | [diff] [blame] | 8105 | /* We must trim any excess data from the response buffer, because we |
| 8106 | * may have blocked an invalid response from a server that we don't |
| 8107 | * want to accidentely forward once we disable the analysers, nor do |
| 8108 | * we want those data to come along with next response. A typical |
| 8109 | * example of such data would be from a buggy server responding to |
| 8110 | * a HEAD with some data, or sending more than the advertised |
| 8111 | * content-length. |
| 8112 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 8113 | if (unlikely(s->res.buf->i)) |
| 8114 | s->res.buf->i = 0; |
Willy Tarreau | 739cfba | 2010-01-25 23:11:14 +0100 | [diff] [blame] | 8115 | |
Christopher Faulet | c0c672a | 2017-03-28 11:51:33 +0200 | [diff] [blame] | 8116 | /* Now we can realign the response buffer */ |
| 8117 | buffer_realign(s->res.buf); |
| 8118 | |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 8119 | s->req.rto = strm_fe(s)->timeout.client; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 8120 | s->req.wto = TICK_ETERNITY; |
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->res.rto = TICK_ETERNITY; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 8123 | s->res.wto = strm_fe(s)->timeout.client; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8124 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 8125 | s->req.rex = TICK_ETERNITY; |
| 8126 | s->req.wex = TICK_ETERNITY; |
| 8127 | s->req.analyse_exp = TICK_ETERNITY; |
| 8128 | s->res.rex = TICK_ETERNITY; |
| 8129 | s->res.wex = TICK_ETERNITY; |
| 8130 | s->res.analyse_exp = TICK_ETERNITY; |
Hongbo Long | e39683c | 2017-03-10 18:41:51 +0100 | [diff] [blame] | 8131 | s->si[1].hcto = TICK_ETERNITY; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8132 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8133 | |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 8134 | void free_http_res_rules(struct list *r) |
| 8135 | { |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 8136 | struct act_rule *tr, *pr; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 8137 | |
| 8138 | list_for_each_entry_safe(pr, tr, r, list) { |
| 8139 | LIST_DEL(&pr->list); |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 8140 | regex_free(&pr->arg.hdr_add.re); |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 8141 | free(pr); |
| 8142 | } |
| 8143 | } |
| 8144 | |
| 8145 | void free_http_req_rules(struct list *r) |
| 8146 | { |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 8147 | struct act_rule *tr, *pr; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8148 | |
| 8149 | list_for_each_entry_safe(pr, tr, r, list) { |
| 8150 | LIST_DEL(&pr->list); |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8151 | if (pr->action == ACT_HTTP_REQ_AUTH) |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 8152 | free(pr->arg.auth.realm); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8153 | |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 8154 | regex_free(&pr->arg.hdr_add.re); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8155 | free(pr); |
| 8156 | } |
| 8157 | } |
| 8158 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8159 | /* parse an "http-request" rule */ |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 8160 | 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] | 8161 | { |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 8162 | struct act_rule *rule; |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 8163 | struct action_kw *custom = NULL; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8164 | int cur_arg; |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 8165 | char *error; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8166 | |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 8167 | rule = calloc(1, sizeof(*rule)); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8168 | if (!rule) { |
| 8169 | Alert("parsing [%s:%d]: out of memory.\n", file, linenum); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8170 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8171 | } |
| 8172 | |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 8173 | if (!strcmp(args[0], "allow")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8174 | rule->action = ACT_ACTION_ALLOW; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8175 | cur_arg = 1; |
Jarno Huuskonen | 800d176 | 2017-03-06 14:56:36 +0200 | [diff] [blame] | 8176 | } 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] | 8177 | int code; |
| 8178 | int hc; |
| 8179 | |
Jarno Huuskonen | 800d176 | 2017-03-06 14:56:36 +0200 | [diff] [blame] | 8180 | if (!strcmp(args[0], "tarpit")) { |
| 8181 | rule->action = ACT_HTTP_REQ_TARPIT; |
| 8182 | rule->deny_status = HTTP_ERR_500; |
| 8183 | } |
| 8184 | else { |
| 8185 | rule->action = ACT_ACTION_DENY; |
| 8186 | rule->deny_status = HTTP_ERR_403; |
| 8187 | } |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8188 | cur_arg = 1; |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 8189 | if (strcmp(args[cur_arg], "deny_status") == 0) { |
| 8190 | cur_arg++; |
| 8191 | if (!args[cur_arg]) { |
| 8192 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing status code.\n", |
| 8193 | file, linenum, proxy_type_str(proxy), proxy->id, args[0]); |
| 8194 | goto out_err; |
| 8195 | } |
| 8196 | |
| 8197 | code = atol(args[cur_arg]); |
| 8198 | cur_arg++; |
| 8199 | for (hc = 0; hc < HTTP_ERR_SIZE; hc++) { |
| 8200 | if (http_err_codes[hc] == code) { |
| 8201 | rule->deny_status = hc; |
| 8202 | break; |
| 8203 | } |
| 8204 | } |
| 8205 | |
| 8206 | if (hc >= HTTP_ERR_SIZE) { |
Jarno Huuskonen | 800d176 | 2017-03-06 14:56:36 +0200 | [diff] [blame] | 8207 | Warning("parsing [%s:%d] : status code %d not handled, using default code %d.\n", |
| 8208 | file, linenum, code, http_err_codes[rule->deny_status]); |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 8209 | } |
| 8210 | } |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8211 | } else if (!strcmp(args[0], "auth")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8212 | rule->action = ACT_HTTP_REQ_AUTH; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8213 | cur_arg = 1; |
| 8214 | |
| 8215 | while(*args[cur_arg]) { |
| 8216 | if (!strcmp(args[cur_arg], "realm")) { |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 8217 | rule->arg.auth.realm = strdup(args[cur_arg + 1]); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8218 | cur_arg+=2; |
| 8219 | continue; |
| 8220 | } else |
| 8221 | break; |
| 8222 | } |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 8223 | } else if (!strcmp(args[0], "set-nice")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8224 | rule->action = ACT_HTTP_SET_NICE; |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 8225 | cur_arg = 1; |
| 8226 | |
| 8227 | if (!*args[cur_arg] || |
| 8228 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 8229 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n", |
| 8230 | file, linenum, args[0]); |
| 8231 | goto out_err; |
| 8232 | } |
| 8233 | rule->arg.nice = atoi(args[cur_arg]); |
| 8234 | if (rule->arg.nice < -1024) |
| 8235 | rule->arg.nice = -1024; |
| 8236 | else if (rule->arg.nice > 1024) |
| 8237 | rule->arg.nice = 1024; |
| 8238 | cur_arg++; |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 8239 | } else if (!strcmp(args[0], "set-tos")) { |
| 8240 | #ifdef IP_TOS |
| 8241 | char *err; |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8242 | rule->action = ACT_HTTP_SET_TOS; |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 8243 | cur_arg = 1; |
| 8244 | |
| 8245 | if (!*args[cur_arg] || |
| 8246 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 8247 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n", |
| 8248 | file, linenum, args[0]); |
| 8249 | goto out_err; |
| 8250 | } |
| 8251 | |
| 8252 | rule->arg.tos = strtol(args[cur_arg], &err, 0); |
| 8253 | if (err && *err != '\0') { |
| 8254 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n", |
| 8255 | file, linenum, err, args[0]); |
| 8256 | goto out_err; |
| 8257 | } |
| 8258 | cur_arg++; |
| 8259 | #else |
| 8260 | Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]); |
| 8261 | goto out_err; |
| 8262 | #endif |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 8263 | } else if (!strcmp(args[0], "set-mark")) { |
| 8264 | #ifdef SO_MARK |
| 8265 | char *err; |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8266 | rule->action = ACT_HTTP_SET_MARK; |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 8267 | cur_arg = 1; |
| 8268 | |
| 8269 | if (!*args[cur_arg] || |
| 8270 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 8271 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n", |
| 8272 | file, linenum, args[0]); |
| 8273 | goto out_err; |
| 8274 | } |
| 8275 | |
| 8276 | rule->arg.mark = strtoul(args[cur_arg], &err, 0); |
| 8277 | if (err && *err != '\0') { |
| 8278 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n", |
| 8279 | file, linenum, err, args[0]); |
| 8280 | goto out_err; |
| 8281 | } |
| 8282 | cur_arg++; |
| 8283 | global.last_checks |= LSTCHK_NETADM; |
| 8284 | #else |
| 8285 | Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]); |
| 8286 | goto out_err; |
| 8287 | #endif |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 8288 | } else if (!strcmp(args[0], "set-log-level")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8289 | rule->action = ACT_HTTP_SET_LOGL; |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 8290 | cur_arg = 1; |
| 8291 | |
| 8292 | if (!*args[cur_arg] || |
| 8293 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 8294 | bad_log_level: |
| 8295 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n", |
| 8296 | file, linenum, args[0]); |
| 8297 | goto out_err; |
| 8298 | } |
| 8299 | if (strcmp(args[cur_arg], "silent") == 0) |
| 8300 | rule->arg.loglevel = -1; |
| 8301 | else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0) |
| 8302 | goto bad_log_level; |
| 8303 | cur_arg++; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 8304 | } 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] | 8305 | 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] | 8306 | cur_arg = 1; |
| 8307 | |
Willy Tarreau | 8d1c516 | 2013-04-03 14:13:58 +0200 | [diff] [blame] | 8308 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 8309 | (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) { |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 8310 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n", |
| 8311 | file, linenum, args[0]); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8312 | goto out_err; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 8313 | } |
| 8314 | |
| 8315 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 8316 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 8317 | LIST_INIT(&rule->arg.hdr_add.fmt); |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 8318 | |
Thierry FOURNIER | 1c0054f | 2013-11-20 15:09:52 +0100 | [diff] [blame] | 8319 | proxy->conf.args.ctx = ARGC_HRQ; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8320 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8321 | 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] | 8322 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) { |
| 8323 | Alert("parsing [%s:%d]: 'http-request %s': %s.\n", |
| 8324 | file, linenum, args[0], error); |
| 8325 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8326 | goto out_err; |
| 8327 | } |
Willy Tarreau | 59ad1a2 | 2014-01-29 14:39:58 +0100 | [diff] [blame] | 8328 | free(proxy->conf.lfs_file); |
| 8329 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 8330 | proxy->conf.lfs_line = proxy->conf.args.line; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 8331 | cur_arg += 2; |
Willy Tarreau | b854392 | 2014-06-17 18:58:26 +0200 | [diff] [blame] | 8332 | } 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] | 8333 | 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] | 8334 | cur_arg = 1; |
| 8335 | |
| 8336 | if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] || |
Baptiste Assmann | 92df370 | 2014-06-24 11:10:00 +0200 | [diff] [blame] | 8337 | (*args[cur_arg+3] && strcmp(args[cur_arg+3], "if") != 0 && strcmp(args[cur_arg+3], "unless") != 0)) { |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 8338 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n", |
| 8339 | file, linenum, args[0]); |
| 8340 | goto out_err; |
| 8341 | } |
| 8342 | |
| 8343 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 8344 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 8345 | LIST_INIT(&rule->arg.hdr_add.fmt); |
| 8346 | |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 8347 | error = NULL; |
| 8348 | if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) { |
| 8349 | Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum, |
| 8350 | args[cur_arg + 1], error); |
| 8351 | free(error); |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 8352 | goto out_err; |
| 8353 | } |
| 8354 | |
| 8355 | proxy->conf.args.ctx = ARGC_HRQ; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8356 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8357 | 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] | 8358 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) { |
| 8359 | Alert("parsing [%s:%d]: 'http-request %s': %s.\n", |
| 8360 | file, linenum, args[0], error); |
| 8361 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8362 | goto out_err; |
| 8363 | } |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 8364 | |
| 8365 | free(proxy->conf.lfs_file); |
| 8366 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 8367 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 8368 | cur_arg += 3; |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 8369 | } else if (strcmp(args[0], "del-header") == 0) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8370 | rule->action = ACT_HTTP_DEL_HDR; |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 8371 | cur_arg = 1; |
| 8372 | |
| 8373 | if (!*args[cur_arg] || |
| 8374 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 8375 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 8376 | file, linenum, args[0]); |
| 8377 | goto out_err; |
| 8378 | } |
| 8379 | |
| 8380 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 8381 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 8382 | |
| 8383 | proxy->conf.args.ctx = ARGC_HRQ; |
| 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 += 1; |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 8388 | } else if (strncmp(args[0], "track-sc", 8) == 0 && |
| 8389 | args[0][9] == '\0' && args[0][8] >= '0' && |
Willy Tarreau | e1cfc1f | 2014-10-17 11:53:05 +0200 | [diff] [blame] | 8390 | args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */ |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 8391 | struct sample_expr *expr; |
| 8392 | unsigned int where; |
| 8393 | char *err = NULL; |
| 8394 | |
| 8395 | cur_arg = 1; |
| 8396 | proxy->conf.args.ctx = ARGC_TRK; |
| 8397 | |
| 8398 | expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args); |
| 8399 | if (!expr) { |
| 8400 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n", |
| 8401 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], err); |
| 8402 | free(err); |
| 8403 | goto out_err; |
| 8404 | } |
| 8405 | |
| 8406 | where = 0; |
| 8407 | if (proxy->cap & PR_CAP_FE) |
| 8408 | where |= SMP_VAL_FE_HRQ_HDR; |
| 8409 | if (proxy->cap & PR_CAP_BE) |
| 8410 | where |= SMP_VAL_BE_HRQ_HDR; |
| 8411 | |
| 8412 | if (!(expr->fetch->val & where)) { |
| 8413 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :" |
| 8414 | " fetch method '%s' extracts information from '%s', none of which is available here.\n", |
| 8415 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], |
| 8416 | args[cur_arg-1], sample_src_names(expr->fetch->use)); |
| 8417 | free(expr); |
| 8418 | goto out_err; |
| 8419 | } |
| 8420 | |
| 8421 | if (strcmp(args[cur_arg], "table") == 0) { |
| 8422 | cur_arg++; |
| 8423 | if (!args[cur_arg]) { |
| 8424 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing table name.\n", |
| 8425 | file, linenum, proxy_type_str(proxy), proxy->id, args[0]); |
| 8426 | free(expr); |
| 8427 | goto out_err; |
| 8428 | } |
| 8429 | /* we copy the table name for now, it will be resolved later */ |
Thierry FOURNIER | 5ec63e0 | 2015-08-04 09:09:48 +0200 | [diff] [blame] | 8430 | rule->arg.trk_ctr.table.n = strdup(args[cur_arg]); |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 8431 | cur_arg++; |
| 8432 | } |
Thierry FOURNIER | 5ec63e0 | 2015-08-04 09:09:48 +0200 | [diff] [blame] | 8433 | rule->arg.trk_ctr.expr = expr; |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8434 | rule->action = ACT_ACTION_TRK_SC0 + args[0][8] - '0'; |
Christopher Faulet | 78880fb | 2017-09-18 14:43:55 +0200 | [diff] [blame] | 8435 | rule->check_ptr = check_trk_action; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8436 | } else if (strcmp(args[0], "redirect") == 0) { |
| 8437 | struct redirect_rule *redir; |
Willy Tarreau | 6d4890c | 2013-11-18 18:04:25 +0100 | [diff] [blame] | 8438 | char *errmsg = NULL; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8439 | |
Willy Tarreau | be4653b | 2015-05-28 15:26:58 +0200 | [diff] [blame] | 8440 | if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 0)) == NULL) { |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8441 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n", |
| 8442 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
| 8443 | goto out_err; |
| 8444 | } |
| 8445 | |
| 8446 | /* this redirect rule might already contain a parsed condition which |
| 8447 | * we'll pass to the http-request rule. |
| 8448 | */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8449 | rule->action = ACT_HTTP_REDIR; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8450 | rule->arg.redir = redir; |
| 8451 | rule->cond = redir->cond; |
| 8452 | redir->cond = NULL; |
| 8453 | cur_arg = 2; |
| 8454 | return rule; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8455 | } else if (strncmp(args[0], "add-acl", 7) == 0) { |
| 8456 | /* http-request add-acl(<reference (acl name)>) <key pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8457 | rule->action = ACT_HTTP_ADD_ACL; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8458 | /* |
| 8459 | * '+ 8' for 'add-acl(' |
| 8460 | * '- 9' for 'add-acl(' + trailing ')' |
| 8461 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 8462 | 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] | 8463 | |
| 8464 | cur_arg = 1; |
| 8465 | |
| 8466 | if (!*args[cur_arg] || |
| 8467 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 8468 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 8469 | file, linenum, args[0]); |
| 8470 | goto out_err; |
| 8471 | } |
| 8472 | |
| 8473 | LIST_INIT(&rule->arg.map.key); |
| 8474 | proxy->conf.args.ctx = ARGC_HRQ; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8475 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8476 | 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] | 8477 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) { |
| 8478 | Alert("parsing [%s:%d]: 'http-request %s': %s.\n", |
| 8479 | file, linenum, args[0], error); |
| 8480 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8481 | goto out_err; |
| 8482 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8483 | free(proxy->conf.lfs_file); |
| 8484 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 8485 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 8486 | cur_arg += 1; |
| 8487 | } else if (strncmp(args[0], "del-acl", 7) == 0) { |
| 8488 | /* http-request del-acl(<reference (acl name)>) <key pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8489 | rule->action = ACT_HTTP_DEL_ACL; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8490 | /* |
| 8491 | * '+ 8' for 'del-acl(' |
| 8492 | * '- 9' for 'del-acl(' + trailing ')' |
| 8493 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 8494 | 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] | 8495 | |
| 8496 | cur_arg = 1; |
| 8497 | |
| 8498 | if (!*args[cur_arg] || |
| 8499 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 8500 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 8501 | file, linenum, args[0]); |
| 8502 | goto out_err; |
| 8503 | } |
| 8504 | |
| 8505 | LIST_INIT(&rule->arg.map.key); |
| 8506 | proxy->conf.args.ctx = ARGC_HRQ; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8507 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8508 | 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] | 8509 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) { |
| 8510 | Alert("parsing [%s:%d]: 'http-request %s': %s.\n", |
| 8511 | file, linenum, args[0], error); |
| 8512 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8513 | goto out_err; |
| 8514 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8515 | free(proxy->conf.lfs_file); |
| 8516 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 8517 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 8518 | cur_arg += 1; |
| 8519 | } else if (strncmp(args[0], "del-map", 7) == 0) { |
| 8520 | /* http-request del-map(<reference (map name)>) <key pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8521 | rule->action = ACT_HTTP_DEL_MAP; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8522 | /* |
| 8523 | * '+ 8' for 'del-map(' |
| 8524 | * '- 9' for 'del-map(' + trailing ')' |
| 8525 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 8526 | 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] | 8527 | |
| 8528 | cur_arg = 1; |
| 8529 | |
| 8530 | if (!*args[cur_arg] || |
| 8531 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 8532 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 8533 | file, linenum, args[0]); |
| 8534 | goto out_err; |
| 8535 | } |
| 8536 | |
| 8537 | LIST_INIT(&rule->arg.map.key); |
| 8538 | proxy->conf.args.ctx = ARGC_HRQ; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8539 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8540 | 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] | 8541 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) { |
| 8542 | Alert("parsing [%s:%d]: 'http-request %s': %s.\n", |
| 8543 | file, linenum, args[0], error); |
| 8544 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8545 | goto out_err; |
| 8546 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8547 | free(proxy->conf.lfs_file); |
| 8548 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 8549 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 8550 | cur_arg += 1; |
| 8551 | } else if (strncmp(args[0], "set-map", 7) == 0) { |
| 8552 | /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8553 | rule->action = ACT_HTTP_SET_MAP; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8554 | /* |
| 8555 | * '+ 8' for 'set-map(' |
| 8556 | * '- 9' for 'set-map(' + trailing ')' |
| 8557 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 8558 | 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] | 8559 | |
| 8560 | cur_arg = 1; |
| 8561 | |
| 8562 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 8563 | (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) { |
| 8564 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n", |
| 8565 | file, linenum, args[0]); |
| 8566 | goto out_err; |
| 8567 | } |
| 8568 | |
| 8569 | LIST_INIT(&rule->arg.map.key); |
| 8570 | LIST_INIT(&rule->arg.map.value); |
| 8571 | proxy->conf.args.ctx = ARGC_HRQ; |
| 8572 | |
| 8573 | /* key pattern */ |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8574 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8575 | 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] | 8576 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) { |
| 8577 | Alert("parsing [%s:%d]: 'http-request %s' key: %s.\n", |
| 8578 | file, linenum, args[0], error); |
| 8579 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8580 | goto out_err; |
| 8581 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8582 | |
| 8583 | /* value pattern */ |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8584 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8585 | 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] | 8586 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) { |
| 8587 | Alert("parsing [%s:%d]: 'http-request %s' pattern: %s.\n", |
| 8588 | file, linenum, args[0], error); |
| 8589 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8590 | goto out_err; |
| 8591 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8592 | free(proxy->conf.lfs_file); |
| 8593 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 8594 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 8595 | |
| 8596 | cur_arg += 2; |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 8597 | } else if (((custom = action_http_req_custom(args[0])) != NULL)) { |
| 8598 | char *errmsg = NULL; |
| 8599 | cur_arg = 1; |
| 8600 | /* try in the module list */ |
Thierry FOURNIER | 5563e4b | 2015-08-14 19:20:07 +0200 | [diff] [blame] | 8601 | rule->from = ACT_F_HTTP_REQ; |
Thierry FOURNIER | 85c6c97 | 2015-09-22 19:14:35 +0200 | [diff] [blame] | 8602 | rule->kw = custom; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 8603 | if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) { |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 8604 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n", |
| 8605 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
| 8606 | free(errmsg); |
| 8607 | goto out_err; |
| 8608 | } |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8609 | } else { |
Thierry FOURNIER | ab95e65 | 2015-10-02 08:24:51 +0200 | [diff] [blame] | 8610 | action_build_list(&http_req_keywords.list, &trash); |
| 8611 | Alert("parsing [%s:%d]: 'http-request' expects 'allow', 'deny', 'auth', 'redirect', " |
| 8612 | "'tarpit', 'add-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', " |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 8613 | "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'track-sc*'" |
William Lallemand | 2e785f2 | 2016-05-25 01:48:42 +0200 | [diff] [blame] | 8614 | "%s%s, but got '%s'%s.\n", |
Thierry FOURNIER | ab95e65 | 2015-10-02 08:24:51 +0200 | [diff] [blame] | 8615 | file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)"); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8616 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8617 | } |
| 8618 | |
| 8619 | if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) { |
| 8620 | struct acl_cond *cond; |
Willy Tarreau | b7451bb | 2012-04-27 12:38:15 +0200 | [diff] [blame] | 8621 | char *errmsg = NULL; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8622 | |
Christopher Faulet | 1b421ea | 2017-09-22 14:38:56 +0200 | [diff] [blame] | 8623 | if ((cond = build_acl_cond(file, linenum, &proxy->acl, proxy, args+cur_arg, &errmsg)) == NULL) { |
Willy Tarreau | b7451bb | 2012-04-27 12:38:15 +0200 | [diff] [blame] | 8624 | Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n", |
| 8625 | file, linenum, args[0], errmsg); |
| 8626 | free(errmsg); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8627 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8628 | } |
| 8629 | rule->cond = cond; |
| 8630 | } |
| 8631 | else if (*args[cur_arg]) { |
| 8632 | Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or" |
| 8633 | " either 'if' or 'unless' followed by a condition but found '%s'.\n", |
| 8634 | file, linenum, args[0], args[cur_arg]); |
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 | return rule; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8639 | out_err: |
| 8640 | free(rule); |
| 8641 | return NULL; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8642 | } |
| 8643 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8644 | /* parse an "http-respose" rule */ |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 8645 | 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] | 8646 | { |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 8647 | struct act_rule *rule; |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 8648 | struct action_kw *custom = NULL; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8649 | int cur_arg; |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 8650 | char *error; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8651 | |
| 8652 | rule = calloc(1, sizeof(*rule)); |
| 8653 | if (!rule) { |
| 8654 | Alert("parsing [%s:%d]: out of memory.\n", file, linenum); |
| 8655 | goto out_err; |
| 8656 | } |
| 8657 | |
| 8658 | if (!strcmp(args[0], "allow")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8659 | rule->action = ACT_ACTION_ALLOW; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8660 | cur_arg = 1; |
| 8661 | } else if (!strcmp(args[0], "deny")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8662 | rule->action = ACT_ACTION_DENY; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8663 | cur_arg = 1; |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 8664 | } else if (!strcmp(args[0], "set-nice")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8665 | rule->action = ACT_HTTP_SET_NICE; |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 8666 | cur_arg = 1; |
| 8667 | |
| 8668 | if (!*args[cur_arg] || |
| 8669 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 8670 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n", |
| 8671 | file, linenum, args[0]); |
| 8672 | goto out_err; |
| 8673 | } |
| 8674 | rule->arg.nice = atoi(args[cur_arg]); |
| 8675 | if (rule->arg.nice < -1024) |
| 8676 | rule->arg.nice = -1024; |
| 8677 | else if (rule->arg.nice > 1024) |
| 8678 | rule->arg.nice = 1024; |
| 8679 | cur_arg++; |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 8680 | } else if (!strcmp(args[0], "set-tos")) { |
| 8681 | #ifdef IP_TOS |
| 8682 | char *err; |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8683 | rule->action = ACT_HTTP_SET_TOS; |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 8684 | cur_arg = 1; |
| 8685 | |
| 8686 | if (!*args[cur_arg] || |
| 8687 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 8688 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n", |
| 8689 | file, linenum, args[0]); |
| 8690 | goto out_err; |
| 8691 | } |
| 8692 | |
| 8693 | rule->arg.tos = strtol(args[cur_arg], &err, 0); |
| 8694 | if (err && *err != '\0') { |
| 8695 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n", |
| 8696 | file, linenum, err, args[0]); |
| 8697 | goto out_err; |
| 8698 | } |
| 8699 | cur_arg++; |
| 8700 | #else |
| 8701 | Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]); |
| 8702 | goto out_err; |
| 8703 | #endif |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 8704 | } else if (!strcmp(args[0], "set-mark")) { |
| 8705 | #ifdef SO_MARK |
| 8706 | char *err; |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8707 | rule->action = ACT_HTTP_SET_MARK; |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 8708 | cur_arg = 1; |
| 8709 | |
| 8710 | if (!*args[cur_arg] || |
| 8711 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 8712 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n", |
| 8713 | file, linenum, args[0]); |
| 8714 | goto out_err; |
| 8715 | } |
| 8716 | |
| 8717 | rule->arg.mark = strtoul(args[cur_arg], &err, 0); |
| 8718 | if (err && *err != '\0') { |
| 8719 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n", |
| 8720 | file, linenum, err, args[0]); |
| 8721 | goto out_err; |
| 8722 | } |
| 8723 | cur_arg++; |
| 8724 | global.last_checks |= LSTCHK_NETADM; |
| 8725 | #else |
| 8726 | Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]); |
| 8727 | goto out_err; |
| 8728 | #endif |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 8729 | } else if (!strcmp(args[0], "set-log-level")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8730 | rule->action = ACT_HTTP_SET_LOGL; |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 8731 | cur_arg = 1; |
| 8732 | |
| 8733 | if (!*args[cur_arg] || |
| 8734 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 8735 | bad_log_level: |
| 8736 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n", |
| 8737 | file, linenum, args[0]); |
| 8738 | goto out_err; |
| 8739 | } |
| 8740 | if (strcmp(args[cur_arg], "silent") == 0) |
| 8741 | rule->arg.loglevel = -1; |
Ruoshan Huang | dd01678 | 2016-06-15 22:16:03 +0800 | [diff] [blame] | 8742 | 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] | 8743 | goto bad_log_level; |
| 8744 | cur_arg++; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8745 | } 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] | 8746 | 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] | 8747 | cur_arg = 1; |
| 8748 | |
| 8749 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 8750 | (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) { |
| 8751 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n", |
| 8752 | file, linenum, args[0]); |
| 8753 | goto out_err; |
| 8754 | } |
| 8755 | |
| 8756 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 8757 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 8758 | LIST_INIT(&rule->arg.hdr_add.fmt); |
| 8759 | |
Thierry FOURNIER | 1c0054f | 2013-11-20 15:09:52 +0100 | [diff] [blame] | 8760 | proxy->conf.args.ctx = ARGC_HRS; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8761 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8762 | 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] | 8763 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) { |
| 8764 | Alert("parsing [%s:%d]: 'http-response %s': %s.\n", |
| 8765 | file, linenum, args[0], error); |
| 8766 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8767 | goto out_err; |
| 8768 | } |
Willy Tarreau | 59ad1a2 | 2014-01-29 14:39:58 +0100 | [diff] [blame] | 8769 | free(proxy->conf.lfs_file); |
| 8770 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 8771 | proxy->conf.lfs_line = proxy->conf.args.line; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8772 | cur_arg += 2; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 8773 | } 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] | 8774 | 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] | 8775 | cur_arg = 1; |
| 8776 | |
| 8777 | if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] || |
Baptiste Assmann | 12cb00b | 2014-08-08 17:29:06 +0200 | [diff] [blame] | 8778 | (*args[cur_arg+3] && strcmp(args[cur_arg+3], "if") != 0 && strcmp(args[cur_arg+3], "unless") != 0)) { |
| 8779 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n", |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 8780 | file, linenum, args[0]); |
| 8781 | goto out_err; |
| 8782 | } |
| 8783 | |
| 8784 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 8785 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 8786 | LIST_INIT(&rule->arg.hdr_add.fmt); |
| 8787 | |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 8788 | error = NULL; |
| 8789 | if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) { |
| 8790 | Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum, |
| 8791 | args[cur_arg + 1], error); |
| 8792 | free(error); |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 8793 | goto out_err; |
| 8794 | } |
| 8795 | |
| 8796 | proxy->conf.args.ctx = ARGC_HRQ; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8797 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8798 | 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] | 8799 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) { |
| 8800 | Alert("parsing [%s:%d]: 'http-response %s': %s.\n", |
| 8801 | file, linenum, args[0], error); |
| 8802 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8803 | goto out_err; |
| 8804 | } |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 8805 | |
| 8806 | free(proxy->conf.lfs_file); |
| 8807 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 8808 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 8809 | cur_arg += 3; |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 8810 | } else if (strcmp(args[0], "del-header") == 0) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8811 | rule->action = ACT_HTTP_DEL_HDR; |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 8812 | cur_arg = 1; |
| 8813 | |
| 8814 | if (!*args[cur_arg] || |
| 8815 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 8816 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 8817 | file, linenum, args[0]); |
| 8818 | goto out_err; |
| 8819 | } |
| 8820 | |
| 8821 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 8822 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 8823 | |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8824 | proxy->conf.args.ctx = ARGC_HRS; |
| 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 += 1; |
| 8829 | } else if (strncmp(args[0], "add-acl", 7) == 0) { |
| 8830 | /* http-request add-acl(<reference (acl name)>) <key pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8831 | rule->action = ACT_HTTP_ADD_ACL; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8832 | /* |
| 8833 | * '+ 8' for 'add-acl(' |
| 8834 | * '- 9' for 'add-acl(' + trailing ')' |
| 8835 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 8836 | 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] | 8837 | |
| 8838 | cur_arg = 1; |
| 8839 | |
| 8840 | if (!*args[cur_arg] || |
| 8841 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 8842 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 8843 | file, linenum, args[0]); |
| 8844 | goto out_err; |
| 8845 | } |
| 8846 | |
| 8847 | LIST_INIT(&rule->arg.map.key); |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 8848 | proxy->conf.args.ctx = ARGC_HRS; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8849 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8850 | 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] | 8851 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) { |
| 8852 | Alert("parsing [%s:%d]: 'http-response %s': %s.\n", |
| 8853 | file, linenum, args[0], error); |
| 8854 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8855 | goto out_err; |
| 8856 | } |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 8857 | free(proxy->conf.lfs_file); |
| 8858 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 8859 | proxy->conf.lfs_line = proxy->conf.args.line; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8860 | |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 8861 | cur_arg += 1; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8862 | } else if (strncmp(args[0], "del-acl", 7) == 0) { |
| 8863 | /* http-response del-acl(<reference (acl name)>) <key pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8864 | rule->action = ACT_HTTP_DEL_ACL; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8865 | /* |
| 8866 | * '+ 8' for 'del-acl(' |
| 8867 | * '- 9' for 'del-acl(' + trailing ')' |
| 8868 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 8869 | 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] | 8870 | |
| 8871 | cur_arg = 1; |
| 8872 | |
| 8873 | if (!*args[cur_arg] || |
| 8874 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 8875 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 8876 | file, linenum, args[0]); |
| 8877 | goto out_err; |
| 8878 | } |
| 8879 | |
| 8880 | LIST_INIT(&rule->arg.map.key); |
| 8881 | proxy->conf.args.ctx = ARGC_HRS; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8882 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8883 | 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] | 8884 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) { |
| 8885 | Alert("parsing [%s:%d]: 'http-response %s': %s.\n", |
| 8886 | file, linenum, args[0], error); |
| 8887 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8888 | goto out_err; |
| 8889 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8890 | free(proxy->conf.lfs_file); |
| 8891 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 8892 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 8893 | cur_arg += 1; |
| 8894 | } else if (strncmp(args[0], "del-map", 7) == 0) { |
| 8895 | /* http-response del-map(<reference (map name)>) <key pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8896 | rule->action = ACT_HTTP_DEL_MAP; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8897 | /* |
| 8898 | * '+ 8' for 'del-map(' |
| 8899 | * '- 9' for 'del-map(' + trailing ')' |
| 8900 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 8901 | 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] | 8902 | |
| 8903 | cur_arg = 1; |
| 8904 | |
| 8905 | if (!*args[cur_arg] || |
| 8906 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 8907 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 8908 | file, linenum, args[0]); |
| 8909 | goto out_err; |
| 8910 | } |
| 8911 | |
| 8912 | LIST_INIT(&rule->arg.map.key); |
| 8913 | proxy->conf.args.ctx = ARGC_HRS; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8914 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8915 | 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] | 8916 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) { |
| 8917 | Alert("parsing [%s:%d]: 'http-response %s' %s.\n", |
| 8918 | file, linenum, args[0], error); |
| 8919 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8920 | goto out_err; |
| 8921 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8922 | free(proxy->conf.lfs_file); |
| 8923 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 8924 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 8925 | cur_arg += 1; |
| 8926 | } else if (strncmp(args[0], "set-map", 7) == 0) { |
| 8927 | /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8928 | rule->action = ACT_HTTP_SET_MAP; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8929 | /* |
| 8930 | * '+ 8' for 'set-map(' |
| 8931 | * '- 9' for 'set-map(' + trailing ')' |
| 8932 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 8933 | 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] | 8934 | |
| 8935 | cur_arg = 1; |
| 8936 | |
| 8937 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 8938 | (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) { |
| 8939 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n", |
| 8940 | file, linenum, args[0]); |
| 8941 | goto out_err; |
| 8942 | } |
| 8943 | |
| 8944 | LIST_INIT(&rule->arg.map.key); |
| 8945 | LIST_INIT(&rule->arg.map.value); |
| 8946 | |
| 8947 | proxy->conf.args.ctx = ARGC_HRS; |
| 8948 | |
| 8949 | /* key pattern */ |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8950 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8951 | 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] | 8952 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) { |
| 8953 | Alert("parsing [%s:%d]: 'http-response %s' name: %s.\n", |
| 8954 | file, linenum, args[0], error); |
| 8955 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8956 | goto out_err; |
| 8957 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8958 | |
| 8959 | /* value pattern */ |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8960 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8961 | 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] | 8962 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) { |
| 8963 | Alert("parsing [%s:%d]: 'http-response %s' value: %s.\n", |
| 8964 | file, linenum, args[0], error); |
| 8965 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8966 | goto out_err; |
| 8967 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8968 | |
| 8969 | free(proxy->conf.lfs_file); |
| 8970 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 8971 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 8972 | |
| 8973 | cur_arg += 2; |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 8974 | } else if (strcmp(args[0], "redirect") == 0) { |
| 8975 | struct redirect_rule *redir; |
| 8976 | char *errmsg = NULL; |
| 8977 | |
| 8978 | if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 1)) == NULL) { |
| 8979 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n", |
| 8980 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
| 8981 | goto out_err; |
| 8982 | } |
| 8983 | |
| 8984 | /* this redirect rule might already contain a parsed condition which |
| 8985 | * we'll pass to the http-request rule. |
| 8986 | */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8987 | rule->action = ACT_HTTP_REDIR; |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 8988 | rule->arg.redir = redir; |
| 8989 | rule->cond = redir->cond; |
| 8990 | redir->cond = NULL; |
| 8991 | cur_arg = 2; |
| 8992 | return rule; |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 8993 | } else if (strncmp(args[0], "track-sc", 8) == 0 && |
| 8994 | args[0][9] == '\0' && args[0][8] >= '0' && |
| 8995 | args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */ |
| 8996 | struct sample_expr *expr; |
| 8997 | unsigned int where; |
| 8998 | char *err = NULL; |
| 8999 | |
| 9000 | cur_arg = 1; |
| 9001 | proxy->conf.args.ctx = ARGC_TRK; |
| 9002 | |
| 9003 | expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args); |
| 9004 | if (!expr) { |
| 9005 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n", |
| 9006 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], err); |
| 9007 | free(err); |
| 9008 | goto out_err; |
| 9009 | } |
| 9010 | |
| 9011 | where = 0; |
| 9012 | if (proxy->cap & PR_CAP_FE) |
| 9013 | where |= SMP_VAL_FE_HRS_HDR; |
| 9014 | if (proxy->cap & PR_CAP_BE) |
| 9015 | where |= SMP_VAL_BE_HRS_HDR; |
| 9016 | |
| 9017 | if (!(expr->fetch->val & where)) { |
| 9018 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule :" |
| 9019 | " fetch method '%s' extracts information from '%s', none of which is available here.\n", |
| 9020 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], |
| 9021 | args[cur_arg-1], sample_src_names(expr->fetch->use)); |
| 9022 | free(expr); |
| 9023 | goto out_err; |
| 9024 | } |
| 9025 | |
| 9026 | if (strcmp(args[cur_arg], "table") == 0) { |
| 9027 | cur_arg++; |
| 9028 | if (!args[cur_arg]) { |
| 9029 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : missing table name.\n", |
| 9030 | file, linenum, proxy_type_str(proxy), proxy->id, args[0]); |
| 9031 | free(expr); |
| 9032 | goto out_err; |
| 9033 | } |
| 9034 | /* we copy the table name for now, it will be resolved later */ |
| 9035 | rule->arg.trk_ctr.table.n = strdup(args[cur_arg]); |
| 9036 | cur_arg++; |
| 9037 | } |
| 9038 | rule->arg.trk_ctr.expr = expr; |
| 9039 | rule->action = ACT_ACTION_TRK_SC0 + args[0][8] - '0'; |
Christopher Faulet | 78880fb | 2017-09-18 14:43:55 +0200 | [diff] [blame] | 9040 | rule->check_ptr = check_trk_action; |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 9041 | } else if (((custom = action_http_res_custom(args[0])) != NULL)) { |
| 9042 | char *errmsg = NULL; |
| 9043 | cur_arg = 1; |
| 9044 | /* try in the module list */ |
Thierry FOURNIER | 5563e4b | 2015-08-14 19:20:07 +0200 | [diff] [blame] | 9045 | rule->from = ACT_F_HTTP_RES; |
Thierry FOURNIER | 85c6c97 | 2015-09-22 19:14:35 +0200 | [diff] [blame] | 9046 | rule->kw = custom; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 9047 | if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) { |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 9048 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n", |
| 9049 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
| 9050 | free(errmsg); |
| 9051 | goto out_err; |
| 9052 | } |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9053 | } else { |
Thierry FOURNIER | ab95e65 | 2015-10-02 08:24:51 +0200 | [diff] [blame] | 9054 | action_build_list(&http_res_keywords.list, &trash); |
| 9055 | Alert("parsing [%s:%d]: 'http-response' expects 'allow', 'deny', 'redirect', " |
| 9056 | "'add-header', 'del-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', " |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 9057 | "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'track-sc*'" |
William Lallemand | 2e785f2 | 2016-05-25 01:48:42 +0200 | [diff] [blame] | 9058 | "%s%s, but got '%s'%s.\n", |
Thierry FOURNIER | ab95e65 | 2015-10-02 08:24:51 +0200 | [diff] [blame] | 9059 | file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)"); |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9060 | goto out_err; |
| 9061 | } |
| 9062 | |
| 9063 | if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) { |
| 9064 | struct acl_cond *cond; |
| 9065 | char *errmsg = NULL; |
| 9066 | |
Christopher Faulet | 1b421ea | 2017-09-22 14:38:56 +0200 | [diff] [blame] | 9067 | if ((cond = build_acl_cond(file, linenum, &proxy->acl, proxy, args+cur_arg, &errmsg)) == NULL) { |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9068 | Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n", |
| 9069 | file, linenum, args[0], errmsg); |
| 9070 | free(errmsg); |
| 9071 | goto out_err; |
| 9072 | } |
| 9073 | rule->cond = cond; |
| 9074 | } |
| 9075 | else if (*args[cur_arg]) { |
| 9076 | Alert("parsing [%s:%d]: 'http-response %s' expects" |
| 9077 | " either 'if' or 'unless' followed by a condition but found '%s'.\n", |
| 9078 | file, linenum, args[0], args[cur_arg]); |
| 9079 | goto out_err; |
| 9080 | } |
| 9081 | |
| 9082 | return rule; |
| 9083 | out_err: |
| 9084 | free(rule); |
| 9085 | return NULL; |
| 9086 | } |
| 9087 | |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9088 | /* 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] | 9089 | * 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] | 9090 | * dynamic log-format rule instead of a static string. Parameter <dir> indicates |
| 9091 | * 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] | 9092 | */ |
| 9093 | 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] | 9094 | const char **args, char **errmsg, int use_fmt, int dir) |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9095 | { |
| 9096 | struct redirect_rule *rule; |
| 9097 | int cur_arg; |
| 9098 | int type = REDIRECT_TYPE_NONE; |
| 9099 | int code = 302; |
| 9100 | const char *destination = NULL; |
| 9101 | const char *cookie = NULL; |
| 9102 | int cookie_set = 0; |
| 9103 | unsigned int flags = REDIRECT_FLAG_NONE; |
| 9104 | struct acl_cond *cond = NULL; |
| 9105 | |
| 9106 | cur_arg = 0; |
| 9107 | while (*(args[cur_arg])) { |
| 9108 | if (strcmp(args[cur_arg], "location") == 0) { |
| 9109 | if (!*args[cur_arg + 1]) |
| 9110 | goto missing_arg; |
| 9111 | |
| 9112 | type = REDIRECT_TYPE_LOCATION; |
| 9113 | cur_arg++; |
| 9114 | destination = args[cur_arg]; |
| 9115 | } |
| 9116 | else if (strcmp(args[cur_arg], "prefix") == 0) { |
| 9117 | if (!*args[cur_arg + 1]) |
| 9118 | goto missing_arg; |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9119 | type = REDIRECT_TYPE_PREFIX; |
| 9120 | cur_arg++; |
| 9121 | destination = args[cur_arg]; |
| 9122 | } |
| 9123 | else if (strcmp(args[cur_arg], "scheme") == 0) { |
| 9124 | if (!*args[cur_arg + 1]) |
| 9125 | goto missing_arg; |
| 9126 | |
| 9127 | type = REDIRECT_TYPE_SCHEME; |
| 9128 | cur_arg++; |
| 9129 | destination = args[cur_arg]; |
| 9130 | } |
| 9131 | else if (strcmp(args[cur_arg], "set-cookie") == 0) { |
| 9132 | if (!*args[cur_arg + 1]) |
| 9133 | goto missing_arg; |
| 9134 | |
| 9135 | cur_arg++; |
| 9136 | cookie = args[cur_arg]; |
| 9137 | cookie_set = 1; |
| 9138 | } |
| 9139 | else if (strcmp(args[cur_arg], "clear-cookie") == 0) { |
| 9140 | if (!*args[cur_arg + 1]) |
| 9141 | goto missing_arg; |
| 9142 | |
| 9143 | cur_arg++; |
| 9144 | cookie = args[cur_arg]; |
| 9145 | cookie_set = 0; |
| 9146 | } |
| 9147 | else if (strcmp(args[cur_arg], "code") == 0) { |
| 9148 | if (!*args[cur_arg + 1]) |
| 9149 | goto missing_arg; |
| 9150 | |
| 9151 | cur_arg++; |
| 9152 | code = atol(args[cur_arg]); |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 9153 | if (code < 301 || code > 308 || (code > 303 && code < 307)) { |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9154 | memprintf(errmsg, |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 9155 | "'%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] | 9156 | args[cur_arg - 1], args[cur_arg]); |
| 9157 | return NULL; |
| 9158 | } |
| 9159 | } |
| 9160 | else if (!strcmp(args[cur_arg],"drop-query")) { |
| 9161 | flags |= REDIRECT_FLAG_DROP_QS; |
| 9162 | } |
| 9163 | else if (!strcmp(args[cur_arg],"append-slash")) { |
| 9164 | flags |= REDIRECT_FLAG_APPEND_SLASH; |
| 9165 | } |
| 9166 | else if (strcmp(args[cur_arg], "if") == 0 || |
| 9167 | strcmp(args[cur_arg], "unless") == 0) { |
Christopher Faulet | 1b421ea | 2017-09-22 14:38:56 +0200 | [diff] [blame] | 9168 | cond = build_acl_cond(file, linenum, &proxy->acl, curproxy, (const char **)args + cur_arg, errmsg); |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9169 | if (!cond) { |
| 9170 | memprintf(errmsg, "error in condition: %s", *errmsg); |
| 9171 | return NULL; |
| 9172 | } |
| 9173 | break; |
| 9174 | } |
| 9175 | else { |
| 9176 | memprintf(errmsg, |
| 9177 | "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')", |
| 9178 | args[cur_arg]); |
| 9179 | return NULL; |
| 9180 | } |
| 9181 | cur_arg++; |
| 9182 | } |
| 9183 | |
| 9184 | if (type == REDIRECT_TYPE_NONE) { |
| 9185 | memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')"); |
| 9186 | return NULL; |
| 9187 | } |
| 9188 | |
Willy Tarreau | be4653b | 2015-05-28 15:26:58 +0200 | [diff] [blame] | 9189 | if (dir && type != REDIRECT_TYPE_LOCATION) { |
| 9190 | memprintf(errmsg, "response only supports redirect type 'location'"); |
| 9191 | return NULL; |
| 9192 | } |
| 9193 | |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 9194 | rule = calloc(1, sizeof(*rule)); |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9195 | rule->cond = cond; |
Willy Tarreau | 60e0838 | 2013-12-03 00:48:45 +0100 | [diff] [blame] | 9196 | LIST_INIT(&rule->rdr_fmt); |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 9197 | |
| 9198 | if (!use_fmt) { |
| 9199 | /* old-style static redirect rule */ |
| 9200 | rule->rdr_str = strdup(destination); |
| 9201 | rule->rdr_len = strlen(destination); |
| 9202 | } |
| 9203 | else { |
| 9204 | /* log-format based redirect rule */ |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 9205 | |
| 9206 | /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case, |
| 9207 | * if prefix == "/", we don't want to add anything, otherwise it |
| 9208 | * makes it hard for the user to configure a self-redirection. |
| 9209 | */ |
Godbach | d972203 | 2014-12-18 15:44:58 +0800 | [diff] [blame] | 9210 | curproxy->conf.args.ctx = ARGC_RDR; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 9211 | if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) { |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9212 | if (!parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP, |
| 9213 | 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] | 9214 | : (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9215 | errmsg)) { |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9216 | return NULL; |
| 9217 | } |
Willy Tarreau | 59ad1a2 | 2014-01-29 14:39:58 +0100 | [diff] [blame] | 9218 | free(curproxy->conf.lfs_file); |
| 9219 | curproxy->conf.lfs_file = strdup(curproxy->conf.args.file); |
| 9220 | curproxy->conf.lfs_line = curproxy->conf.args.line; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 9221 | } |
| 9222 | } |
| 9223 | |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9224 | if (cookie) { |
| 9225 | /* depending on cookie_set, either we want to set the cookie, or to clear it. |
| 9226 | * a clear consists in appending "; path=/; Max-Age=0;" at the end. |
| 9227 | */ |
| 9228 | rule->cookie_len = strlen(cookie); |
| 9229 | if (cookie_set) { |
| 9230 | rule->cookie_str = malloc(rule->cookie_len + 10); |
| 9231 | memcpy(rule->cookie_str, cookie, rule->cookie_len); |
| 9232 | memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10); |
| 9233 | rule->cookie_len += 9; |
| 9234 | } else { |
| 9235 | rule->cookie_str = malloc(rule->cookie_len + 21); |
| 9236 | memcpy(rule->cookie_str, cookie, rule->cookie_len); |
| 9237 | memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21); |
| 9238 | rule->cookie_len += 20; |
| 9239 | } |
| 9240 | } |
| 9241 | rule->type = type; |
| 9242 | rule->code = code; |
| 9243 | rule->flags = flags; |
| 9244 | LIST_INIT(&rule->list); |
| 9245 | return rule; |
| 9246 | |
| 9247 | missing_arg: |
| 9248 | memprintf(errmsg, "missing argument for '%s'", args[cur_arg]); |
| 9249 | return NULL; |
| 9250 | } |
| 9251 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9252 | /************************************************************************/ |
| 9253 | /* The code below is dedicated to ACL parsing and matching */ |
| 9254 | /************************************************************************/ |
| 9255 | |
| 9256 | |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9257 | /* This function ensures that the prerequisites for an L7 fetch are ready, |
| 9258 | * which means that a request or response is ready. If some data is missing, |
| 9259 | * 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] | 9260 | * to extract data from L7. If <req_vol> is non-null during a request prefetch, |
| 9261 | * another test is made to ensure the required information is not gone. |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9262 | * |
| 9263 | * The function returns : |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9264 | * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to |
| 9265 | * decide whether or not an HTTP message is present ; |
| 9266 | * 0 if the requested data cannot be fetched or if it is certain that |
| 9267 | * we'll never have any HTTP message there ; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9268 | * 1 if an HTTP message is ready |
| 9269 | */ |
James Rosewell | 91a41cb | 2015-09-18 17:11:16 +0100 | [diff] [blame] | 9270 | 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] | 9271 | const struct arg *args, struct sample *smp, int req_vol) |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9272 | { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 9273 | struct http_txn *txn; |
| 9274 | struct http_msg *msg; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9275 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 9276 | /* Note: it is possible that <s> is NULL when called before stream |
| 9277 | * initialization (eg: tcp-request connection), so this function is the |
| 9278 | * one responsible for guarding against this case for all HTTP users. |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9279 | */ |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 9280 | if (!s) |
| 9281 | return 0; |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 9282 | |
Thierry FOURNIER | ed08d6a | 2015-09-24 08:40:18 +0200 | [diff] [blame] | 9283 | if (!s->txn) { |
| 9284 | if (unlikely(!http_alloc_txn(s))) |
| 9285 | return 0; /* not enough memory */ |
| 9286 | http_init_txn(s); |
| 9287 | } |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 9288 | txn = s->txn; |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 9289 | msg = &txn->req; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9290 | |
| 9291 | /* Check for a dependency on a request */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9292 | smp->data.type = SMP_T_BOOL; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9293 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 9294 | if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 9295 | /* If the buffer does not leave enough free space at the end, |
| 9296 | * we must first realign it. |
| 9297 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 9298 | if (s->req.buf->p > s->req.buf->data && |
| 9299 | s->req.buf->i + s->req.buf->p > s->req.buf->data + s->req.buf->size - global.tune.maxrewrite) |
| 9300 | buffer_slow_realign(s->req.buf); |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 9301 | |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9302 | if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 472b1ee | 2013-10-14 22:41:30 +0200 | [diff] [blame] | 9303 | if (msg->msg_state == HTTP_MSG_ERROR) |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9304 | return 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9305 | |
| 9306 | /* Try to decode HTTP request */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 9307 | if (likely(msg->next < s->req.buf->i)) |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9308 | http_msg_analyzer(msg, &txn->hdr_idx); |
| 9309 | |
| 9310 | /* Still no valid request ? */ |
| 9311 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 3bf1b2b | 2012-08-27 20:46:07 +0200 | [diff] [blame] | 9312 | if ((msg->msg_state == HTTP_MSG_ERROR) || |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 9313 | buffer_full(s->req.buf, global.tune.maxrewrite)) { |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9314 | return 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9315 | } |
| 9316 | /* wait for final state */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9317 | smp->flags |= SMP_F_MAY_CHANGE; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9318 | return 0; |
| 9319 | } |
| 9320 | |
| 9321 | /* OK we just got a valid HTTP request. We have some minor |
| 9322 | * preparation to perform so that further checks can rely |
| 9323 | * on HTTP tests. |
| 9324 | */ |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 9325 | |
| 9326 | /* If the request was parsed but was too large, we must absolutely |
| 9327 | * return an error so that it is not processed. At the moment this |
| 9328 | * cannot happen, but if the parsers are to change in the future, |
| 9329 | * we want this check to be maintained. |
| 9330 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 9331 | if (unlikely(s->req.buf->i + s->req.buf->p > |
| 9332 | s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)) { |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 9333 | msg->err_state = msg->msg_state; |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 9334 | msg->msg_state = HTTP_MSG_ERROR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9335 | smp->data.u.sint = 1; |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 9336 | return 1; |
| 9337 | } |
| 9338 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9339 | 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] | 9340 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 9341 | s->flags |= SF_REDIRECTABLE; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9342 | |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9343 | if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn)) |
| 9344 | return 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9345 | } |
| 9346 | |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9347 | if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) { |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9348 | return 0; /* data might have moved and indexes changed */ |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9349 | } |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9350 | |
| 9351 | /* otherwise everything's ready for the request */ |
| 9352 | } |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 9353 | else { |
| 9354 | /* Check for a dependency on a response */ |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9355 | if (txn->rsp.msg_state < HTTP_MSG_BODY) { |
| 9356 | smp->flags |= SMP_F_MAY_CHANGE; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9357 | return 0; |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9358 | } |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9359 | } |
| 9360 | |
| 9361 | /* everything's OK */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9362 | smp->data.u.sint = 1; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9363 | return 1; |
| 9364 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9365 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9366 | /* 1. Check on METHOD |
| 9367 | * We use the pre-parsed method if it is known, and store its number as an |
| 9368 | * integer. If it is unknown, we use the pointer and the length. |
| 9369 | */ |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 9370 | 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] | 9371 | { |
| 9372 | int len, meth; |
| 9373 | |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 9374 | len = strlen(text); |
| 9375 | meth = find_http_meth(text, len); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9376 | |
| 9377 | pattern->val.i = meth; |
| 9378 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | 912c119 | 2014-08-29 15:15:50 +0200 | [diff] [blame] | 9379 | pattern->ptr.str = (char *)text; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9380 | pattern->len = len; |
| 9381 | } |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 9382 | else { |
| 9383 | pattern->ptr.str = NULL; |
| 9384 | pattern->len = 0; |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 9385 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9386 | return 1; |
| 9387 | } |
| 9388 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 9389 | /* This function fetches the method of current HTTP request and stores |
| 9390 | * it in the global pattern struct as a chunk. There are two possibilities : |
| 9391 | * - if the method is known (not HTTP_METH_OTHER), its identifier is stored |
| 9392 | * in <len> and <ptr> is NULL ; |
| 9393 | * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and |
| 9394 | * <len> to its length. |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 9395 | * This is intended to be used with pat_match_meth() only. |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 9396 | */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 9397 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9398 | 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] | 9399 | { |
| 9400 | int meth; |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 9401 | struct http_txn *txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9402 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 9403 | CHECK_HTTP_MESSAGE_FIRST_PERM(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9404 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 9405 | txn = smp->strm->txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9406 | meth = txn->meth; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9407 | smp->data.type = SMP_T_METH; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9408 | smp->data.u.meth.meth = meth; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9409 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9410 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 9411 | /* ensure the indexes are not affected */ |
| 9412 | return 0; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9413 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9414 | smp->data.u.meth.str.len = txn->req.sl.rq.m_l; |
| 9415 | smp->data.u.meth.str.str = txn->req.chn->buf->p; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9416 | } |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9417 | smp->flags |= SMP_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9418 | return 1; |
| 9419 | } |
| 9420 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 9421 | /* See above how the method is stored in the global pattern */ |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 9422 | 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] | 9423 | { |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 9424 | int icase; |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 9425 | struct pattern_list *lst; |
| 9426 | struct pattern *pattern; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 9427 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 9428 | list_for_each_entry(lst, &expr->patterns, list) { |
| 9429 | pattern = &lst->pat; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9430 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 9431 | /* well-known method */ |
| 9432 | if (pattern->val.i != HTTP_METH_OTHER) { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9433 | if (smp->data.u.meth.meth == pattern->val.i) |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 9434 | return pattern; |
| 9435 | else |
| 9436 | continue; |
| 9437 | } |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 9438 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 9439 | /* Other method, we must compare the strings */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9440 | if (pattern->len != smp->data.u.meth.str.len) |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 9441 | continue; |
| 9442 | |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 9443 | icase = expr->mflags & PAT_MF_IGNORE_CASE; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9444 | if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.meth.str.str, smp->data.u.meth.str.len) == 0) || |
| 9445 | (!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] | 9446 | return pattern; |
| 9447 | } |
| 9448 | return NULL; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9449 | } |
| 9450 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 9451 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9452 | 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] | 9453 | { |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 9454 | struct http_txn *txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9455 | char *ptr; |
| 9456 | int len; |
| 9457 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9458 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9459 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 9460 | txn = smp->strm->txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9461 | len = txn->req.sl.rq.v_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9462 | ptr = txn->req.chn->buf->p + txn->req.sl.rq.v; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9463 | |
| 9464 | while ((len-- > 0) && (*ptr++ != '/')); |
| 9465 | if (len <= 0) |
| 9466 | return 0; |
| 9467 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9468 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9469 | smp->data.u.str.str = ptr; |
| 9470 | smp->data.u.str.len = len; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9471 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9472 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9473 | return 1; |
| 9474 | } |
| 9475 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 9476 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9477 | 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] | 9478 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 9479 | struct http_txn *txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9480 | char *ptr; |
| 9481 | int len; |
| 9482 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9483 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9484 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 9485 | txn = smp->strm->txn; |
Willy Tarreau | f26b252 | 2012-12-14 08:33:14 +0100 | [diff] [blame] | 9486 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
| 9487 | return 0; |
| 9488 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9489 | len = txn->rsp.sl.st.v_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9490 | ptr = txn->rsp.chn->buf->p; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9491 | |
| 9492 | while ((len-- > 0) && (*ptr++ != '/')); |
| 9493 | if (len <= 0) |
| 9494 | return 0; |
| 9495 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9496 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9497 | smp->data.u.str.str = ptr; |
| 9498 | smp->data.u.str.len = len; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9499 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9500 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9501 | return 1; |
| 9502 | } |
| 9503 | |
| 9504 | /* 3. Check on Status Code. We manipulate integers here. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 9505 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9506 | 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] | 9507 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 9508 | struct http_txn *txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9509 | char *ptr; |
| 9510 | int len; |
| 9511 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9512 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9513 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 9514 | txn = smp->strm->txn; |
Willy Tarreau | f26b252 | 2012-12-14 08:33:14 +0100 | [diff] [blame] | 9515 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
| 9516 | return 0; |
| 9517 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9518 | len = txn->rsp.sl.st.c_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9519 | ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9520 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9521 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9522 | smp->data.u.sint = __strl2ui(ptr, len); |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9523 | smp->flags = SMP_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9524 | return 1; |
| 9525 | } |
| 9526 | |
Thierry Fournier | f4011dd | 2016-03-29 17:23:51 +0200 | [diff] [blame] | 9527 | static int |
| 9528 | smp_fetch_uniqueid(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 9529 | { |
| 9530 | if (LIST_ISEMPTY(&smp->sess->fe->format_unique_id)) |
| 9531 | return 0; |
| 9532 | |
| 9533 | if (!smp->strm->unique_id) { |
| 9534 | if ((smp->strm->unique_id = pool_alloc2(pool2_uniqueid)) == NULL) |
| 9535 | return 0; |
| 9536 | smp->strm->unique_id[0] = '\0'; |
| 9537 | } |
| 9538 | smp->data.u.str.len = build_logline(smp->strm, smp->strm->unique_id, |
| 9539 | UNIQUEID_LEN, &smp->sess->fe->format_unique_id); |
| 9540 | |
| 9541 | smp->data.type = SMP_T_STR; |
| 9542 | smp->data.u.str.str = smp->strm->unique_id; |
| 9543 | smp->flags = SMP_F_CONST; |
| 9544 | return 1; |
| 9545 | } |
| 9546 | |
Thierry FOURNIER | d7d8881 | 2017-04-19 15:15:14 +0200 | [diff] [blame] | 9547 | /* Returns a string block containing all headers including the |
| 9548 | * empty line wich separes headers from the body. This is useful |
| 9549 | * form some headers analysis. |
| 9550 | */ |
| 9551 | static int |
| 9552 | smp_fetch_hdrs(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 9553 | { |
| 9554 | struct http_msg *msg; |
| 9555 | struct hdr_idx *idx; |
| 9556 | struct http_txn *txn; |
| 9557 | |
| 9558 | CHECK_HTTP_MESSAGE_FIRST(); |
| 9559 | |
| 9560 | txn = smp->strm->txn; |
| 9561 | idx = &txn->hdr_idx; |
| 9562 | msg = &txn->req; |
| 9563 | |
| 9564 | smp->data.type = SMP_T_STR; |
| 9565 | smp->data.u.str.str = msg->chn->buf->p + hdr_idx_first_pos(idx); |
| 9566 | smp->data.u.str.len = msg->eoh - hdr_idx_first_pos(idx) + 1 + |
| 9567 | (msg->chn->buf->p[msg->eoh] == '\r'); |
| 9568 | |
| 9569 | return 1; |
| 9570 | } |
| 9571 | |
Thierry FOURNIER | 5617dce | 2017-04-09 05:38:19 +0200 | [diff] [blame] | 9572 | /* Returns the header request in a length/value encoded format. |
| 9573 | * This is useful for exchanges with the SPOE. |
| 9574 | * |
| 9575 | * A "length value" is a multibyte code encoding numbers. It uses the |
| 9576 | * SPOE format. The encoding is the following: |
| 9577 | * |
| 9578 | * Each couple "header name" / "header value" is composed |
| 9579 | * like this: |
| 9580 | * "length value" "header name bytes" |
| 9581 | * "length value" "header value bytes" |
| 9582 | * When the last header is reached, the header name and the header |
| 9583 | * value are empty. Their length are 0 |
| 9584 | */ |
| 9585 | static int |
| 9586 | smp_fetch_hdrs_bin(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 9587 | { |
| 9588 | struct http_msg *msg; |
| 9589 | struct chunk *temp; |
| 9590 | struct hdr_idx *idx; |
| 9591 | const char *cur_ptr, *cur_next, *p; |
| 9592 | int old_idx, cur_idx; |
| 9593 | struct hdr_idx_elem *cur_hdr; |
| 9594 | const char *hn, *hv; |
| 9595 | int hnl, hvl; |
| 9596 | int ret; |
| 9597 | struct http_txn *txn; |
| 9598 | char *buf; |
| 9599 | char *end; |
| 9600 | |
| 9601 | CHECK_HTTP_MESSAGE_FIRST(); |
| 9602 | |
| 9603 | temp = get_trash_chunk(); |
| 9604 | buf = temp->str; |
| 9605 | end = temp->str + temp->size; |
| 9606 | |
| 9607 | txn = smp->strm->txn; |
| 9608 | idx = &txn->hdr_idx; |
| 9609 | msg = &txn->req; |
| 9610 | |
| 9611 | /* Build array of headers. */ |
| 9612 | old_idx = 0; |
| 9613 | cur_next = msg->chn->buf->p + hdr_idx_first_pos(idx); |
| 9614 | while (1) { |
| 9615 | cur_idx = idx->v[old_idx].next; |
| 9616 | if (!cur_idx) |
| 9617 | break; |
| 9618 | old_idx = cur_idx; |
| 9619 | |
| 9620 | cur_hdr = &idx->v[cur_idx]; |
| 9621 | cur_ptr = cur_next; |
| 9622 | cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1; |
| 9623 | |
| 9624 | /* Now we have one full header at cur_ptr of len cur_hdr->len, |
| 9625 | * and the next header starts at cur_next. We'll check |
| 9626 | * this header in the list as well as against the default |
| 9627 | * rule. |
| 9628 | */ |
| 9629 | |
| 9630 | /* look for ': *'. */ |
| 9631 | hn = cur_ptr; |
| 9632 | for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++); |
| 9633 | if (p >= cur_ptr+cur_hdr->len) |
| 9634 | continue; |
| 9635 | hnl = p - hn; |
| 9636 | p++; |
| 9637 | while (p < cur_ptr + cur_hdr->len && (*p == ' ' || *p == '\t')) |
| 9638 | p++; |
| 9639 | if (p >= cur_ptr + cur_hdr->len) |
| 9640 | continue; |
| 9641 | hv = p; |
| 9642 | hvl = cur_ptr + cur_hdr->len-p; |
| 9643 | |
| 9644 | /* encode the header name. */ |
| 9645 | ret = encode_varint(hnl, &buf, end); |
| 9646 | if (ret == -1) |
| 9647 | return 0; |
| 9648 | if (buf + hnl > end) |
| 9649 | return 0; |
| 9650 | memcpy(buf, hn, hnl); |
| 9651 | buf += hnl; |
| 9652 | |
| 9653 | /* encode and copy the value. */ |
| 9654 | ret = encode_varint(hvl, &buf, end); |
| 9655 | if (ret == -1) |
| 9656 | return 0; |
| 9657 | if (buf + hvl > end) |
| 9658 | return 0; |
| 9659 | memcpy(buf, hv, hvl); |
| 9660 | buf += hvl; |
| 9661 | } |
| 9662 | |
| 9663 | /* encode the end of the header list with empty |
| 9664 | * header name and header value. |
| 9665 | */ |
| 9666 | ret = encode_varint(0, &buf, end); |
| 9667 | if (ret == -1) |
| 9668 | return 0; |
| 9669 | ret = encode_varint(0, &buf, end); |
| 9670 | if (ret == -1) |
| 9671 | return 0; |
| 9672 | |
| 9673 | /* Initialise sample data which will be filled. */ |
| 9674 | smp->data.type = SMP_T_BIN; |
| 9675 | smp->data.u.str.str = temp->str; |
| 9676 | smp->data.u.str.len = buf - temp->str; |
| 9677 | smp->data.u.str.size = temp->size; |
| 9678 | |
| 9679 | return 1; |
| 9680 | } |
| 9681 | |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9682 | /* returns the longest available part of the body. This requires that the body |
| 9683 | * has been waited for using http-buffer-request. |
| 9684 | */ |
| 9685 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9686 | 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] | 9687 | { |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9688 | struct http_msg *msg; |
| 9689 | unsigned long len; |
| 9690 | unsigned long block1; |
| 9691 | char *body; |
| 9692 | struct chunk *temp; |
| 9693 | |
| 9694 | CHECK_HTTP_MESSAGE_FIRST(); |
| 9695 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9696 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 9697 | msg = &smp->strm->txn->req; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9698 | else |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 9699 | msg = &smp->strm->txn->rsp; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9700 | |
| 9701 | len = http_body_bytes(msg); |
| 9702 | body = b_ptr(msg->chn->buf, -http_data_rewind(msg)); |
| 9703 | |
| 9704 | block1 = len; |
| 9705 | if (block1 > msg->chn->buf->data + msg->chn->buf->size - body) |
| 9706 | block1 = msg->chn->buf->data + msg->chn->buf->size - body; |
| 9707 | |
| 9708 | if (block1 == len) { |
| 9709 | /* buffer is not wrapped (or empty) */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9710 | smp->data.type = SMP_T_BIN; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9711 | smp->data.u.str.str = body; |
| 9712 | smp->data.u.str.len = len; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9713 | smp->flags = SMP_F_VOL_TEST | SMP_F_CONST; |
| 9714 | } |
| 9715 | else { |
| 9716 | /* buffer is wrapped, we need to defragment it */ |
| 9717 | temp = get_trash_chunk(); |
| 9718 | memcpy(temp->str, body, block1); |
| 9719 | memcpy(temp->str + block1, msg->chn->buf->data, len - block1); |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9720 | smp->data.type = SMP_T_BIN; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9721 | smp->data.u.str.str = temp->str; |
| 9722 | smp->data.u.str.len = len; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9723 | smp->flags = SMP_F_VOL_TEST; |
| 9724 | } |
| 9725 | return 1; |
| 9726 | } |
| 9727 | |
| 9728 | |
| 9729 | /* returns the available length of the body. This requires that the body |
| 9730 | * has been waited for using http-buffer-request. |
| 9731 | */ |
| 9732 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9733 | 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] | 9734 | { |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9735 | struct http_msg *msg; |
| 9736 | |
| 9737 | CHECK_HTTP_MESSAGE_FIRST(); |
| 9738 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9739 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 9740 | msg = &smp->strm->txn->req; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9741 | else |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 9742 | msg = &smp->strm->txn->rsp; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9743 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9744 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9745 | smp->data.u.sint = http_body_bytes(msg); |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9746 | |
| 9747 | smp->flags = SMP_F_VOL_TEST; |
| 9748 | return 1; |
| 9749 | } |
| 9750 | |
| 9751 | |
| 9752 | /* returns the advertised length of the body, or the advertised size of the |
| 9753 | * chunks available in the buffer. This requires that the body has been waited |
| 9754 | * for using http-buffer-request. |
| 9755 | */ |
| 9756 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9757 | 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] | 9758 | { |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9759 | struct http_msg *msg; |
| 9760 | |
| 9761 | CHECK_HTTP_MESSAGE_FIRST(); |
| 9762 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9763 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 9764 | msg = &smp->strm->txn->req; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9765 | else |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 9766 | msg = &smp->strm->txn->rsp; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9767 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9768 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9769 | smp->data.u.sint = msg->body_len; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9770 | |
| 9771 | smp->flags = SMP_F_VOL_TEST; |
| 9772 | return 1; |
| 9773 | } |
| 9774 | |
| 9775 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9776 | /* 4. Check on URL/URI. A pointer to the URI is stored. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 9777 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9778 | 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] | 9779 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 9780 | struct http_txn *txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9781 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9782 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9783 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 9784 | txn = smp->strm->txn; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9785 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9786 | smp->data.u.str.len = txn->req.sl.rq.u_l; |
| 9787 | 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] | 9788 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9789 | return 1; |
| 9790 | } |
| 9791 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9792 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9793 | 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] | 9794 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 9795 | struct http_txn *txn; |
Willy Tarreau | 4c804ec | 2013-09-30 14:37:14 +0200 | [diff] [blame] | 9796 | struct sockaddr_storage addr; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9797 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9798 | CHECK_HTTP_MESSAGE_FIRST(); |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9799 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 9800 | txn = smp->strm->txn; |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 9801 | 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] | 9802 | if (((struct sockaddr_in *)&addr)->sin_family != AF_INET) |
Willy Tarreau | f4362b3 | 2011-12-16 17:49:52 +0100 | [diff] [blame] | 9803 | return 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9804 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9805 | smp->data.type = SMP_T_IPV4; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9806 | smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9807 | smp->flags = 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9808 | return 1; |
| 9809 | } |
| 9810 | |
| 9811 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9812 | 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] | 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) |
| 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_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9825 | smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port); |
Willy Tarreau | 21e5b0e | 2012-04-23 19:25:44 +0200 | [diff] [blame] | 9826 | smp->flags = 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9827 | return 1; |
| 9828 | } |
| 9829 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9830 | /* Fetch an HTTP header. A pointer to the beginning of the value is returned. |
| 9831 | * Accepts an optional argument of type string containing the header field name, |
| 9832 | * and an optional argument of type signed or unsigned integer to request an |
| 9833 | * 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] | 9834 | * headers are considered from the first one. It does not stop on commas and |
| 9835 | * returns full lines instead (useful for User-Agent or Date for example). |
| 9836 | */ |
| 9837 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9838 | 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] | 9839 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 9840 | struct hdr_idx *idx; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 9841 | struct hdr_ctx *ctx = smp->ctx.a[0]; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 9842 | const struct http_msg *msg; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 9843 | int occ = 0; |
| 9844 | const char *name_str = NULL; |
| 9845 | int name_len = 0; |
| 9846 | |
| 9847 | if (!ctx) { |
| 9848 | /* first call */ |
| 9849 | ctx = &static_hdr_ctx; |
| 9850 | ctx->idx = 0; |
| 9851 | smp->ctx.a[0] = ctx; |
| 9852 | } |
| 9853 | |
| 9854 | if (args) { |
| 9855 | if (args[0].type != ARGT_STR) |
| 9856 | return 0; |
| 9857 | name_str = args[0].data.str.str; |
| 9858 | name_len = args[0].data.str.len; |
| 9859 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 9860 | if (args[1].type == ARGT_SINT) |
| 9861 | occ = args[1].data.sint; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 9862 | } |
| 9863 | |
| 9864 | CHECK_HTTP_MESSAGE_FIRST(); |
| 9865 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 9866 | idx = &smp->strm->txn->hdr_idx; |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9867 | 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] | 9868 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 9869 | if (ctx && !(smp->flags & SMP_F_NOT_LAST)) |
| 9870 | /* search for header from the beginning */ |
| 9871 | ctx->idx = 0; |
| 9872 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9873 | if (!occ && !(smp->opt & SMP_OPT_ITERATE)) |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 9874 | /* no explicit occurrence and single fetch => last header by default */ |
| 9875 | occ = -1; |
| 9876 | |
| 9877 | if (!occ) |
| 9878 | /* prepare to report multiple occurrences for ACL fetches */ |
| 9879 | smp->flags |= SMP_F_NOT_LAST; |
| 9880 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9881 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9882 | smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9883 | 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] | 9884 | return 1; |
| 9885 | |
| 9886 | smp->flags &= ~SMP_F_NOT_LAST; |
| 9887 | return 0; |
| 9888 | } |
| 9889 | |
| 9890 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
| 9891 | * Accepts exactly 1 argument of type string. It does not stop on commas and |
| 9892 | * returns full lines instead (useful for User-Agent or Date for example). |
| 9893 | */ |
| 9894 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9895 | 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] | 9896 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 9897 | struct hdr_idx *idx; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 9898 | struct hdr_ctx ctx; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 9899 | const struct http_msg *msg; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 9900 | int cnt; |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 9901 | const char *name = NULL; |
| 9902 | int len = 0; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 9903 | |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 9904 | if (args && args->type == ARGT_STR) { |
| 9905 | name = args->data.str.str; |
| 9906 | len = args->data.str.len; |
| 9907 | } |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 9908 | |
| 9909 | CHECK_HTTP_MESSAGE_FIRST(); |
| 9910 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 9911 | idx = &smp->strm->txn->hdr_idx; |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9912 | 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] | 9913 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 9914 | ctx.idx = 0; |
| 9915 | cnt = 0; |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 9916 | 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] | 9917 | cnt++; |
| 9918 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9919 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9920 | smp->data.u.sint = cnt; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 9921 | smp->flags = SMP_F_VOL_HDR; |
| 9922 | return 1; |
| 9923 | } |
| 9924 | |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 9925 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9926 | 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] | 9927 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 9928 | struct hdr_idx *idx; |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 9929 | struct hdr_ctx ctx; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 9930 | const struct http_msg *msg; |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 9931 | struct chunk *temp; |
| 9932 | char del = ','; |
| 9933 | |
| 9934 | if (args && args->type == ARGT_STR) |
| 9935 | del = *args[0].data.str.str; |
| 9936 | |
| 9937 | CHECK_HTTP_MESSAGE_FIRST(); |
| 9938 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 9939 | idx = &smp->strm->txn->hdr_idx; |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9940 | 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] | 9941 | |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 9942 | temp = get_trash_chunk(); |
| 9943 | |
| 9944 | ctx.idx = 0; |
| 9945 | while (http_find_next_header(msg->chn->buf->p, idx, &ctx)) { |
| 9946 | if (temp->len) |
| 9947 | temp->str[temp->len++] = del; |
| 9948 | memcpy(temp->str + temp->len, ctx.line, ctx.del); |
| 9949 | temp->len += ctx.del; |
| 9950 | } |
| 9951 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9952 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9953 | smp->data.u.str.str = temp->str; |
| 9954 | smp->data.u.str.len = temp->len; |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 9955 | smp->flags = SMP_F_VOL_HDR; |
| 9956 | return 1; |
| 9957 | } |
| 9958 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 9959 | /* Fetch an HTTP header. A pointer to the beginning of the value is returned. |
| 9960 | * Accepts an optional argument of type string containing the header field name, |
| 9961 | * and an optional argument of type signed or unsigned integer to request an |
| 9962 | * 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] | 9963 | * headers are considered from the first one. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9964 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9965 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9966 | 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] | 9967 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 9968 | struct hdr_idx *idx; |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 9969 | struct hdr_ctx *ctx = smp->ctx.a[0]; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 9970 | const struct http_msg *msg; |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9971 | int occ = 0; |
| 9972 | const char *name_str = NULL; |
| 9973 | int name_len = 0; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 9974 | |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 9975 | if (!ctx) { |
| 9976 | /* first call */ |
| 9977 | ctx = &static_hdr_ctx; |
| 9978 | ctx->idx = 0; |
Willy Tarreau | ffb6f08 | 2013-04-02 23:16:53 +0200 | [diff] [blame] | 9979 | smp->ctx.a[0] = ctx; |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 9980 | } |
| 9981 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9982 | if (args) { |
| 9983 | if (args[0].type != ARGT_STR) |
| 9984 | return 0; |
| 9985 | name_str = args[0].data.str.str; |
| 9986 | name_len = args[0].data.str.len; |
| 9987 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 9988 | if (args[1].type == ARGT_SINT) |
| 9989 | occ = args[1].data.sint; |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9990 | } |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 9991 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 9992 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9993 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 9994 | idx = &smp->strm->txn->hdr_idx; |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9995 | 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] | 9996 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9997 | if (ctx && !(smp->flags & SMP_F_NOT_LAST)) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9998 | /* search for header from the beginning */ |
| 9999 | ctx->idx = 0; |
| 10000 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10001 | if (!occ && !(smp->opt & SMP_OPT_ITERATE)) |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10002 | /* no explicit occurrence and single fetch => last header by default */ |
| 10003 | occ = -1; |
| 10004 | |
| 10005 | if (!occ) |
| 10006 | /* prepare to report multiple occurrences for ACL fetches */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10007 | smp->flags |= SMP_F_NOT_LAST; |
Willy Tarreau | 664092c | 2011-12-16 19:11:42 +0100 | [diff] [blame] | 10008 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10009 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10010 | smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10011 | 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] | 10012 | return 1; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10013 | |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10014 | smp->flags &= ~SMP_F_NOT_LAST; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10015 | return 0; |
| 10016 | } |
| 10017 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10018 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10019 | * Accepts exactly 1 argument of type string. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10020 | */ |
| 10021 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10022 | 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] | 10023 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10024 | struct hdr_idx *idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10025 | struct hdr_ctx ctx; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10026 | const struct http_msg *msg; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10027 | int cnt; |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10028 | const char *name = NULL; |
| 10029 | int len = 0; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10030 | |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10031 | if (args && args->type == ARGT_STR) { |
| 10032 | name = args->data.str.str; |
| 10033 | len = args->data.str.len; |
| 10034 | } |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10035 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10036 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10037 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10038 | idx = &smp->strm->txn->hdr_idx; |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10039 | 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] | 10040 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10041 | ctx.idx = 0; |
| 10042 | cnt = 0; |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10043 | while (http_find_header2(name, len, msg->chn->buf->p, idx, &ctx)) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10044 | cnt++; |
| 10045 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10046 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10047 | smp->data.u.sint = cnt; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10048 | smp->flags = SMP_F_VOL_HDR; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10049 | return 1; |
| 10050 | } |
| 10051 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10052 | /* Fetch an HTTP header's integer value. The integer value is returned. It |
| 10053 | * takes a mandatory argument of type string and an optional one of type int |
| 10054 | * to designate a specific occurrence. It returns an unsigned integer, which |
| 10055 | * may or may not be appropriate for everything. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10056 | */ |
| 10057 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10058 | 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] | 10059 | { |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10060 | int ret = smp_fetch_hdr(args, smp, kw, private); |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10061 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10062 | if (ret > 0) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10063 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10064 | 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] | 10065 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10066 | |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10067 | return ret; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10068 | } |
| 10069 | |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 10070 | /* Fetch an HTTP header's IP value. takes a mandatory argument of type string |
| 10071 | * and an optional one of type int to designate a specific occurrence. |
| 10072 | * It returns an IPv4 or IPv6 address. |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 10073 | */ |
| 10074 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10075 | 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] | 10076 | { |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10077 | int ret; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10078 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10079 | while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10080 | if (url2ipv4((char *)smp->data.u.str.str, &smp->data.u.ipv4)) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10081 | smp->data.type = SMP_T_IPV4; |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10082 | break; |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 10083 | } else { |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 10084 | struct chunk *temp = get_trash_chunk(); |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10085 | if (smp->data.u.str.len < temp->size - 1) { |
| 10086 | memcpy(temp->str, smp->data.u.str.str, smp->data.u.str.len); |
| 10087 | temp->str[smp->data.u.str.len] = '\0'; |
| 10088 | if (inet_pton(AF_INET6, temp->str, &smp->data.u.ipv6)) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10089 | smp->data.type = SMP_T_IPV6; |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 10090 | break; |
| 10091 | } |
| 10092 | } |
| 10093 | } |
| 10094 | |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10095 | /* if the header doesn't match an IP address, fetch next one */ |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10096 | if (!(smp->flags & SMP_F_NOT_LAST)) |
| 10097 | return 0; |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 10098 | } |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10099 | return ret; |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 10100 | } |
| 10101 | |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10102 | /* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at |
| 10103 | * the first '/' after the possible hostname, and ends before the possible '?'. |
| 10104 | */ |
| 10105 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10106 | 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] | 10107 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10108 | struct http_txn *txn; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10109 | char *ptr, *end; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10110 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10111 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10112 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10113 | txn = smp->strm->txn; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10114 | 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] | 10115 | ptr = http_get_path(txn); |
| 10116 | if (!ptr) |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10117 | return 0; |
| 10118 | |
| 10119 | /* OK, we got the '/' ! */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10120 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10121 | smp->data.u.str.str = ptr; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10122 | |
| 10123 | while (ptr < end && *ptr != '?') |
| 10124 | ptr++; |
| 10125 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10126 | smp->data.u.str.len = ptr - smp->data.u.str.str; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10127 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10128 | return 1; |
| 10129 | } |
| 10130 | |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10131 | /* This produces a concatenation of the first occurrence of the Host header |
| 10132 | * followed by the path component if it begins with a slash ('/'). This means |
| 10133 | * that '*' will not be added, resulting in exactly the first Host entry. |
| 10134 | * If no Host header is found, then the path is returned as-is. The returned |
| 10135 | * 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] | 10136 | * The returned sample is of type string. |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10137 | */ |
| 10138 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10139 | 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] | 10140 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10141 | struct http_txn *txn; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10142 | char *ptr, *end, *beg; |
| 10143 | struct hdr_ctx ctx; |
Willy Tarreau | 3caf2af | 2014-06-24 17:27:02 +0200 | [diff] [blame] | 10144 | struct chunk *temp; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10145 | |
| 10146 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10147 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10148 | txn = smp->strm->txn; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10149 | ctx.idx = 0; |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 10150 | 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] | 10151 | return smp_fetch_path(args, smp, kw, private); |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10152 | |
| 10153 | /* 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] | 10154 | temp = get_trash_chunk(); |
| 10155 | memcpy(temp->str, ctx.line + ctx.val, ctx.vlen); |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10156 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10157 | smp->data.u.str.str = temp->str; |
| 10158 | smp->data.u.str.len = ctx.vlen; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10159 | |
| 10160 | /* now retrieve the path */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 10161 | 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] | 10162 | beg = http_get_path(txn); |
| 10163 | if (!beg) |
| 10164 | beg = end; |
| 10165 | |
| 10166 | for (ptr = beg; ptr < end && *ptr != '?'; ptr++); |
| 10167 | |
| 10168 | if (beg < ptr && *beg == '/') { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10169 | memcpy(smp->data.u.str.str + smp->data.u.str.len, beg, ptr - beg); |
| 10170 | smp->data.u.str.len += ptr - beg; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10171 | } |
| 10172 | |
| 10173 | smp->flags = SMP_F_VOL_1ST; |
| 10174 | return 1; |
| 10175 | } |
| 10176 | |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 10177 | /* This produces a 32-bit hash of the concatenation of the first occurrence of |
| 10178 | * the Host header followed by the path component if it begins with a slash ('/'). |
| 10179 | * This means that '*' will not be added, resulting in exactly the first Host |
| 10180 | * 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] | 10181 | * is hashed using the path hash followed by a full avalanche hash and provides a |
| 10182 | * 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] | 10183 | * high-traffic sites without having to store whole paths. |
| 10184 | */ |
Thierry FOURNIER | 055b9d5 | 2014-07-15 16:11:07 +0200 | [diff] [blame] | 10185 | int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10186 | 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] | 10187 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10188 | struct http_txn *txn; |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 10189 | struct hdr_ctx ctx; |
| 10190 | unsigned int hash = 0; |
| 10191 | char *ptr, *beg, *end; |
| 10192 | int len; |
| 10193 | |
| 10194 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10195 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10196 | txn = smp->strm->txn; |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 10197 | ctx.idx = 0; |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 10198 | 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] | 10199 | /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */ |
| 10200 | ptr = ctx.line + ctx.val; |
| 10201 | len = ctx.vlen; |
| 10202 | while (len--) |
| 10203 | hash = *(ptr++) + (hash << 6) + (hash << 16) - hash; |
| 10204 | } |
| 10205 | |
| 10206 | /* now retrieve the path */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 10207 | 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] | 10208 | beg = http_get_path(txn); |
| 10209 | if (!beg) |
| 10210 | beg = end; |
| 10211 | |
| 10212 | for (ptr = beg; ptr < end && *ptr != '?'; ptr++); |
| 10213 | |
| 10214 | if (beg < ptr && *beg == '/') { |
| 10215 | while (beg < ptr) |
| 10216 | hash = *(beg++) + (hash << 6) + (hash << 16) - hash; |
| 10217 | } |
| 10218 | hash = full_hash(hash); |
| 10219 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10220 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10221 | smp->data.u.sint = hash; |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 10222 | smp->flags = SMP_F_VOL_1ST; |
| 10223 | return 1; |
| 10224 | } |
| 10225 | |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10226 | /* 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] | 10227 | * path as returned by smp_fetch_base32(). The idea is to have per-source and |
| 10228 | * per-path counters. The result is a binary block from 8 to 20 bytes depending |
| 10229 | * 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] | 10230 | * that in environments where IPv6 is insignificant, truncating the output to |
| 10231 | * 8 bytes would still work. |
| 10232 | */ |
| 10233 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10234 | 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] | 10235 | { |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 10236 | struct chunk *temp; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10237 | struct connection *cli_conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10238 | |
| 10239 | if (!cli_conn) |
| 10240 | return 0; |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10241 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10242 | if (!smp_fetch_base32(args, smp, kw, private)) |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10243 | return 0; |
| 10244 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 10245 | temp = get_trash_chunk(); |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10246 | *(unsigned int *)temp->str = htonl(smp->data.u.sint); |
Willy Tarreau | 5ad6e1d | 2014-07-15 21:34:06 +0200 | [diff] [blame] | 10247 | temp->len += sizeof(unsigned int); |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10248 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10249 | switch (cli_conn->addr.from.ss_family) { |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10250 | case AF_INET: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10251 | 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] | 10252 | temp->len += 4; |
| 10253 | break; |
| 10254 | case AF_INET6: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10255 | 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] | 10256 | temp->len += 16; |
| 10257 | break; |
| 10258 | default: |
| 10259 | return 0; |
| 10260 | } |
| 10261 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10262 | smp->data.u.str = *temp; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10263 | smp->data.type = SMP_T_BIN; |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10264 | return 1; |
| 10265 | } |
| 10266 | |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 10267 | /* Extracts the query string, which comes after the question mark '?'. If no |
| 10268 | * question mark is found, nothing is returned. Otherwise it returns a sample |
| 10269 | * of type string carrying the whole query string. |
| 10270 | */ |
| 10271 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10272 | 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] | 10273 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10274 | struct http_txn *txn; |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 10275 | char *ptr, *end; |
| 10276 | |
| 10277 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10278 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10279 | txn = smp->strm->txn; |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 10280 | ptr = txn->req.chn->buf->p + txn->req.sl.rq.u; |
| 10281 | end = ptr + txn->req.sl.rq.u_l; |
| 10282 | |
| 10283 | /* look up the '?' */ |
| 10284 | do { |
| 10285 | if (ptr == end) |
| 10286 | return 0; |
| 10287 | } while (*ptr++ != '?'); |
| 10288 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10289 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10290 | smp->data.u.str.str = ptr; |
| 10291 | smp->data.u.str.len = end - ptr; |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 10292 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
| 10293 | return 1; |
| 10294 | } |
| 10295 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10296 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10297 | 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] | 10298 | { |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10299 | /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged |
| 10300 | * as a layer7 ACL, which involves automatic allocation of hdr_idx. |
| 10301 | */ |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10302 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10303 | CHECK_HTTP_MESSAGE_FIRST_PERM(); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10304 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10305 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10306 | smp->data.u.sint = 1; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10307 | return 1; |
| 10308 | } |
| 10309 | |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 10310 | /* return a valid test if the current request is the first one on the connection */ |
| 10311 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10312 | 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] | 10313 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10314 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10315 | smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST); |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 10316 | return 1; |
| 10317 | } |
| 10318 | |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10319 | /* Accepts exactly 1 argument of type userlist */ |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 10320 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10321 | 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] | 10322 | { |
| 10323 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10324 | if (!args || args->type != ARGT_USR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10325 | return 0; |
| 10326 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10327 | CHECK_HTTP_MESSAGE_FIRST(); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 10328 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10329 | if (!get_http_auth(smp->strm)) |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 10330 | return 0; |
| 10331 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10332 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10333 | 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] | 10334 | smp->strm->txn->auth.pass); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 10335 | return 1; |
| 10336 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10337 | |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10338 | /* Accepts exactly 1 argument of type userlist */ |
| 10339 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10340 | 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] | 10341 | { |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10342 | if (!args || args->type != ARGT_USR) |
| 10343 | return 0; |
| 10344 | |
| 10345 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10346 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10347 | if (!get_http_auth(smp->strm)) |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10348 | return 0; |
| 10349 | |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10350 | /* if the user does not belong to the userlist or has a wrong password, |
| 10351 | * report that it unconditionally does not match. Otherwise we return |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 10352 | * a string containing the username. |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10353 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10354 | if (!check_user(args->data.usr, smp->strm->txn->auth.user, |
| 10355 | smp->strm->txn->auth.pass)) |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 10356 | return 0; |
| 10357 | |
| 10358 | /* pat_match_auth() will need the user list */ |
| 10359 | smp->ctx.a[0] = args->data.usr; |
| 10360 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10361 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10362 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10363 | smp->data.u.str.str = smp->strm->txn->auth.user; |
| 10364 | smp->data.u.str.len = strlen(smp->strm->txn->auth.user); |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10365 | |
| 10366 | return 1; |
| 10367 | } |
| 10368 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10369 | /* Try to find the next occurrence of a cookie name in a cookie header value. |
| 10370 | * The lookup begins at <hdr>. The pointer and size of the next occurrence of |
| 10371 | * the cookie value is returned into *value and *value_l, and the function |
| 10372 | * returns a pointer to the next pointer to search from if the value was found. |
| 10373 | * Otherwise if the cookie was not found, NULL is returned and neither value |
| 10374 | * nor value_l are touched. The input <hdr> string should first point to the |
| 10375 | * header's value, and the <hdr_end> pointer must point to the first character |
| 10376 | * not part of the value. <list> must be non-zero if value may represent a list |
| 10377 | * of values (cookie headers). This makes it faster to abort parsing when no |
| 10378 | * list is expected. |
| 10379 | */ |
David Carlier | 4686f79 | 2015-09-25 14:10:50 +0100 | [diff] [blame] | 10380 | char * |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10381 | extract_cookie_value(char *hdr, const char *hdr_end, |
| 10382 | char *cookie_name, size_t cookie_name_l, int list, |
Willy Tarreau | 3fb818c | 2012-04-11 17:21:08 +0200 | [diff] [blame] | 10383 | char **value, int *value_l) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10384 | { |
| 10385 | char *equal, *att_end, *att_beg, *val_beg, *val_end; |
| 10386 | char *next; |
| 10387 | |
| 10388 | /* we search at least a cookie name followed by an equal, and more |
| 10389 | * generally something like this : |
| 10390 | * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n |
| 10391 | */ |
| 10392 | for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) { |
| 10393 | /* Iterate through all cookies on this line */ |
| 10394 | |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 10395 | while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg)) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10396 | att_beg++; |
| 10397 | |
| 10398 | /* find att_end : this is the first character after the last non |
| 10399 | * space before the equal. It may be equal to hdr_end. |
| 10400 | */ |
| 10401 | equal = att_end = att_beg; |
| 10402 | |
| 10403 | while (equal < hdr_end) { |
| 10404 | if (*equal == '=' || *equal == ';' || (list && *equal == ',')) |
| 10405 | break; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 10406 | if (HTTP_IS_SPHT(*equal++)) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10407 | continue; |
| 10408 | att_end = equal; |
| 10409 | } |
| 10410 | |
| 10411 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 10412 | * is between <att_beg> and <equal>, both may be identical. |
| 10413 | */ |
| 10414 | |
| 10415 | /* look for end of cookie if there is an equal sign */ |
| 10416 | if (equal < hdr_end && *equal == '=') { |
| 10417 | /* look for the beginning of the value */ |
| 10418 | val_beg = equal + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 10419 | while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg)) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10420 | val_beg++; |
| 10421 | |
| 10422 | /* find the end of the value, respecting quotes */ |
| 10423 | next = find_cookie_value_end(val_beg, hdr_end); |
| 10424 | |
| 10425 | /* make val_end point to the first white space or delimitor after the value */ |
| 10426 | val_end = next; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 10427 | while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1))) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10428 | val_end--; |
| 10429 | } else { |
| 10430 | val_beg = val_end = next = equal; |
| 10431 | } |
| 10432 | |
| 10433 | /* We have nothing to do with attributes beginning with '$'. However, |
| 10434 | * they will automatically be removed if a header before them is removed, |
| 10435 | * since they're supposed to be linked together. |
| 10436 | */ |
| 10437 | if (*att_beg == '$') |
| 10438 | continue; |
| 10439 | |
| 10440 | /* Ignore cookies with no equal sign */ |
| 10441 | if (equal == next) |
| 10442 | continue; |
| 10443 | |
| 10444 | /* Now we have the cookie name between att_beg and att_end, and |
| 10445 | * its value between val_beg and val_end. |
| 10446 | */ |
| 10447 | |
| 10448 | if (att_end - att_beg == cookie_name_l && |
| 10449 | memcmp(att_beg, cookie_name, cookie_name_l) == 0) { |
| 10450 | /* let's return this value and indicate where to go on from */ |
| 10451 | *value = val_beg; |
| 10452 | *value_l = val_end - val_beg; |
| 10453 | return next + 1; |
| 10454 | } |
| 10455 | |
| 10456 | /* Set-Cookie headers only have the name in the first attr=value part */ |
| 10457 | if (!list) |
| 10458 | break; |
| 10459 | } |
| 10460 | |
| 10461 | return NULL; |
| 10462 | } |
| 10463 | |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10464 | /* Fetch a captured HTTP request header. The index is the position of |
| 10465 | * the "capture" option in the configuration file |
| 10466 | */ |
| 10467 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10468 | 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] | 10469 | { |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10470 | struct proxy *fe = strm_fe(smp->strm); |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10471 | int idx; |
| 10472 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 10473 | if (!args || args->type != ARGT_SINT) |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10474 | return 0; |
| 10475 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 10476 | idx = args->data.sint; |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10477 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10478 | 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] | 10479 | return 0; |
| 10480 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10481 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10482 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10483 | smp->data.u.str.str = smp->strm->req_cap[idx]; |
| 10484 | smp->data.u.str.len = strlen(smp->strm->req_cap[idx]); |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10485 | |
| 10486 | return 1; |
| 10487 | } |
| 10488 | |
| 10489 | /* Fetch a captured HTTP response header. The index is the position of |
| 10490 | * the "capture" option in the configuration file |
| 10491 | */ |
| 10492 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10493 | 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] | 10494 | { |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10495 | struct proxy *fe = strm_fe(smp->strm); |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10496 | int idx; |
| 10497 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 10498 | if (!args || args->type != ARGT_SINT) |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10499 | return 0; |
| 10500 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 10501 | idx = args->data.sint; |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10502 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10503 | 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] | 10504 | return 0; |
| 10505 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10506 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10507 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10508 | smp->data.u.str.str = smp->strm->res_cap[idx]; |
| 10509 | smp->data.u.str.len = strlen(smp->strm->res_cap[idx]); |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10510 | |
| 10511 | return 1; |
| 10512 | } |
| 10513 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10514 | /* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */ |
| 10515 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10516 | 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] | 10517 | { |
| 10518 | struct chunk *temp; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10519 | struct http_txn *txn = smp->strm->txn; |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10520 | char *ptr; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10521 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10522 | if (!txn || !txn->uri) |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10523 | return 0; |
| 10524 | |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10525 | ptr = txn->uri; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10526 | |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10527 | while (*ptr != ' ' && *ptr != '\0') /* find first space */ |
| 10528 | ptr++; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10529 | |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10530 | temp = get_trash_chunk(); |
| 10531 | temp->str = txn->uri; |
| 10532 | temp->len = ptr - txn->uri; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10533 | smp->data.u.str = *temp; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10534 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10535 | smp->flags = SMP_F_CONST; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10536 | |
| 10537 | return 1; |
| 10538 | |
| 10539 | } |
| 10540 | |
| 10541 | /* Extracts the path in the HTTP request, the txn->uri should be filled before the call */ |
| 10542 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10543 | 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] | 10544 | { |
| 10545 | struct chunk *temp; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10546 | struct http_txn *txn = smp->strm->txn; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10547 | char *ptr; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10548 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10549 | if (!txn || !txn->uri) |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10550 | return 0; |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10551 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10552 | ptr = txn->uri; |
| 10553 | |
| 10554 | while (*ptr != ' ' && *ptr != '\0') /* find first space */ |
| 10555 | ptr++; |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10556 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10557 | if (!*ptr) |
| 10558 | return 0; |
| 10559 | |
| 10560 | ptr++; /* skip the space */ |
| 10561 | |
| 10562 | temp = get_trash_chunk(); |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10563 | ptr = temp->str = http_get_path_from_string(ptr); |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10564 | if (!ptr) |
| 10565 | return 0; |
| 10566 | while (*ptr != ' ' && *ptr != '\0') /* find space after URI */ |
| 10567 | ptr++; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10568 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10569 | smp->data.u.str = *temp; |
| 10570 | smp->data.u.str.len = ptr - temp->str; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10571 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10572 | smp->flags = SMP_F_CONST; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10573 | |
| 10574 | return 1; |
| 10575 | } |
| 10576 | |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 10577 | /* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it |
| 10578 | * as a string (either "HTTP/1.0" or "HTTP/1.1"). |
| 10579 | */ |
| 10580 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10581 | 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] | 10582 | { |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10583 | struct http_txn *txn = smp->strm->txn; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 10584 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10585 | if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST) |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 10586 | return 0; |
| 10587 | |
| 10588 | if (txn->req.flags & HTTP_MSGF_VER_11) |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10589 | smp->data.u.str.str = "HTTP/1.1"; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 10590 | else |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10591 | smp->data.u.str.str = "HTTP/1.0"; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 10592 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10593 | smp->data.u.str.len = 8; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10594 | smp->data.type = SMP_T_STR; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 10595 | smp->flags = SMP_F_CONST; |
| 10596 | return 1; |
| 10597 | |
| 10598 | } |
| 10599 | |
| 10600 | /* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it |
| 10601 | * as a string (either "HTTP/1.0" or "HTTP/1.1"). |
| 10602 | */ |
| 10603 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10604 | 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] | 10605 | { |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10606 | struct http_txn *txn = smp->strm->txn; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 10607 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10608 | if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST) |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 10609 | return 0; |
| 10610 | |
| 10611 | if (txn->rsp.flags & HTTP_MSGF_VER_11) |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10612 | smp->data.u.str.str = "HTTP/1.1"; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 10613 | else |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10614 | smp->data.u.str.str = "HTTP/1.0"; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 10615 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10616 | smp->data.u.str.len = 8; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10617 | smp->data.type = SMP_T_STR; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 10618 | smp->flags = SMP_F_CONST; |
| 10619 | return 1; |
| 10620 | |
| 10621 | } |
| 10622 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10623 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10624 | /* 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] | 10625 | * 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] | 10626 | * 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] | 10627 | * 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] | 10628 | * 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] | 10629 | * Accepts exactly 1 argument of type string. If the input options indicate |
| 10630 | * 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] | 10631 | * The returned sample is of type CSTR. Can be used to parse cookies in other |
| 10632 | * files. |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10633 | */ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10634 | 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] | 10635 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10636 | struct http_txn *txn; |
| 10637 | struct hdr_idx *idx; |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 10638 | struct hdr_ctx *ctx = smp->ctx.a[2]; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10639 | const struct http_msg *msg; |
| 10640 | const char *hdr_name; |
| 10641 | int hdr_name_len; |
| 10642 | char *sol; |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10643 | int occ = 0; |
| 10644 | int found = 0; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10645 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10646 | if (!args || args->type != ARGT_STR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10647 | return 0; |
| 10648 | |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 10649 | if (!ctx) { |
| 10650 | /* first call */ |
| 10651 | ctx = &static_hdr_ctx; |
| 10652 | ctx->idx = 0; |
| 10653 | smp->ctx.a[2] = ctx; |
| 10654 | } |
| 10655 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10656 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10657 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10658 | txn = smp->strm->txn; |
| 10659 | idx = &smp->strm->txn->hdr_idx; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10660 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10661 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10662 | msg = &txn->req; |
| 10663 | hdr_name = "Cookie"; |
| 10664 | hdr_name_len = 6; |
| 10665 | } else { |
| 10666 | msg = &txn->rsp; |
| 10667 | hdr_name = "Set-Cookie"; |
| 10668 | hdr_name_len = 10; |
| 10669 | } |
| 10670 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10671 | if (!occ && !(smp->opt & SMP_OPT_ITERATE)) |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10672 | /* no explicit occurrence and single fetch => last cookie by default */ |
| 10673 | occ = -1; |
| 10674 | |
| 10675 | /* OK so basically here, either we want only one value and it's the |
| 10676 | * last one, or we want to iterate over all of them and we fetch the |
| 10677 | * next one. |
| 10678 | */ |
| 10679 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10680 | sol = msg->chn->buf->p; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10681 | if (!(smp->flags & SMP_F_NOT_LAST)) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10682 | /* search for the header from the beginning, we must first initialize |
| 10683 | * the search parameters. |
| 10684 | */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10685 | smp->ctx.a[0] = NULL; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10686 | ctx->idx = 0; |
| 10687 | } |
| 10688 | |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10689 | smp->flags |= SMP_F_VOL_HDR; |
| 10690 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10691 | while (1) { |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10692 | /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */ |
| 10693 | if (!smp->ctx.a[0]) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10694 | if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx)) |
| 10695 | goto out; |
| 10696 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10697 | if (ctx->vlen < args->data.str.len + 1) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10698 | continue; |
| 10699 | |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10700 | smp->ctx.a[0] = ctx->line + ctx->val; |
| 10701 | smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10702 | } |
| 10703 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10704 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10705 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10706 | 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] | 10707 | args->data.str.str, args->data.str.len, |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10708 | (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ, |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10709 | &smp->data.u.str.str, |
| 10710 | &smp->data.u.str.len); |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10711 | if (smp->ctx.a[0]) { |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10712 | found = 1; |
| 10713 | if (occ >= 0) { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10714 | /* one value was returned into smp->data.u.str.{str,len} */ |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10715 | smp->flags |= SMP_F_NOT_LAST; |
| 10716 | return 1; |
| 10717 | } |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10718 | } |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10719 | /* if we're looking for last occurrence, let's loop */ |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10720 | } |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10721 | /* all cookie headers and values were scanned. If we're looking for the |
| 10722 | * last occurrence, we may return it now. |
| 10723 | */ |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10724 | out: |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10725 | smp->flags &= ~SMP_F_NOT_LAST; |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10726 | return found; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10727 | } |
| 10728 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10729 | /* 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] | 10730 | * 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] | 10731 | * multiple cookies may be parsed on the same line. The returned sample is of |
| 10732 | * type UINT. Accepts exactly 1 argument of type string. |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10733 | */ |
| 10734 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10735 | 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] | 10736 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10737 | struct http_txn *txn; |
| 10738 | struct hdr_idx *idx; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10739 | struct hdr_ctx ctx; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10740 | const struct http_msg *msg; |
| 10741 | const char *hdr_name; |
| 10742 | int hdr_name_len; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10743 | int cnt; |
| 10744 | char *val_beg, *val_end; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10745 | char *sol; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10746 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10747 | if (!args || args->type != ARGT_STR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10748 | return 0; |
| 10749 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10750 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10751 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10752 | txn = smp->strm->txn; |
| 10753 | idx = &smp->strm->txn->hdr_idx; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10754 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10755 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10756 | msg = &txn->req; |
| 10757 | hdr_name = "Cookie"; |
| 10758 | hdr_name_len = 6; |
| 10759 | } else { |
| 10760 | msg = &txn->rsp; |
| 10761 | hdr_name = "Set-Cookie"; |
| 10762 | hdr_name_len = 10; |
| 10763 | } |
| 10764 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10765 | sol = msg->chn->buf->p; |
Willy Tarreau | 46787ed | 2012-04-11 17:28:40 +0200 | [diff] [blame] | 10766 | val_end = val_beg = NULL; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10767 | ctx.idx = 0; |
| 10768 | cnt = 0; |
| 10769 | |
| 10770 | while (1) { |
| 10771 | /* Note: val_beg == NULL every time we need to fetch a new header */ |
| 10772 | if (!val_beg) { |
| 10773 | if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx)) |
| 10774 | break; |
| 10775 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10776 | if (ctx.vlen < args->data.str.len + 1) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10777 | continue; |
| 10778 | |
| 10779 | val_beg = ctx.line + ctx.val; |
| 10780 | val_end = val_beg + ctx.vlen; |
| 10781 | } |
| 10782 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10783 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10784 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10785 | while ((val_beg = extract_cookie_value(val_beg, val_end, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10786 | args->data.str.str, args->data.str.len, |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10787 | (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ, |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10788 | &smp->data.u.str.str, |
| 10789 | &smp->data.u.str.len))) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10790 | cnt++; |
| 10791 | } |
| 10792 | } |
| 10793 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10794 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10795 | smp->data.u.sint = cnt; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10796 | smp->flags |= SMP_F_VOL_HDR; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10797 | return 1; |
| 10798 | } |
| 10799 | |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 10800 | /* Fetch an cookie's integer value. The integer value is returned. It |
| 10801 | * takes a mandatory argument of type string. It relies on smp_fetch_cookie(). |
| 10802 | */ |
| 10803 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10804 | 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] | 10805 | { |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10806 | int ret = smp_fetch_cookie(args, smp, kw, private); |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 10807 | |
| 10808 | if (ret > 0) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10809 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10810 | 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] | 10811 | } |
| 10812 | |
| 10813 | return ret; |
| 10814 | } |
| 10815 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10816 | /************************************************************************/ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 10817 | /* The code below is dedicated to sample fetches */ |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 10818 | /************************************************************************/ |
| 10819 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10820 | /* |
| 10821 | * Given a path string and its length, find the position of beginning of the |
| 10822 | * query string. Returns NULL if no query string is found in the path. |
| 10823 | * |
| 10824 | * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22: |
| 10825 | * |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 10826 | * find_query_string(path, n, '?') points to "yo=mama;ye=daddy" string. |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10827 | */ |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 10828 | 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] | 10829 | { |
| 10830 | char *p; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 10831 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 10832 | p = memchr(path, delim, path_l); |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10833 | return p ? p + 1 : NULL; |
| 10834 | } |
| 10835 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 10836 | static inline int is_param_delimiter(char c, char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10837 | { |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 10838 | return c == '&' || c == ';' || c == delim; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10839 | } |
| 10840 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10841 | /* after increasing a pointer value, it can exceed the first buffer |
| 10842 | * size. This function transform the value of <ptr> according with |
| 10843 | * the expected position. <chunks> is an array of the one or two |
| 10844 | * avalaible chunks. The first value is the start of the first chunk, |
| 10845 | * the second value if the end+1 of the first chunks. The third value |
| 10846 | * is NULL or the start of the second chunk and the fourth value is |
| 10847 | * the end+1 of the second chunk. The function returns 1 if does a |
| 10848 | * wrap, else returns 0. |
| 10849 | */ |
| 10850 | static inline int fix_pointer_if_wrap(const char **chunks, const char **ptr) |
| 10851 | { |
| 10852 | if (*ptr < chunks[1]) |
| 10853 | return 0; |
| 10854 | if (!chunks[2]) |
| 10855 | return 0; |
| 10856 | *ptr = chunks[2] + ( *ptr - chunks[1] ); |
| 10857 | return 1; |
| 10858 | } |
| 10859 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10860 | /* |
| 10861 | * Given a url parameter, find the starting position of the first occurence, |
| 10862 | * or NULL if the parameter is not found. |
| 10863 | * |
| 10864 | * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye", |
| 10865 | * the function will return query_string+8. |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10866 | * |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 10867 | * 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] | 10868 | * or the second chunk. The caller must be check the position before using the |
| 10869 | * result. |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10870 | */ |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10871 | static const char * |
| 10872 | find_url_param_pos(const char **chunks, |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 10873 | const char* url_param_name, size_t url_param_name_l, |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 10874 | char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10875 | { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10876 | const char *pos, *last, *equal; |
| 10877 | const char **bufs = chunks; |
| 10878 | int l1, l2; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10879 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10880 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10881 | pos = bufs[0]; |
| 10882 | last = bufs[1]; |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 10883 | while (pos < last) { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10884 | /* Check the equal. */ |
| 10885 | equal = pos + url_param_name_l; |
| 10886 | if (fix_pointer_if_wrap(chunks, &equal)) { |
| 10887 | if (equal >= chunks[3]) |
| 10888 | return NULL; |
| 10889 | } else { |
| 10890 | if (equal >= chunks[1]) |
| 10891 | return NULL; |
| 10892 | } |
| 10893 | if (*equal == '=') { |
| 10894 | if (pos + url_param_name_l > last) { |
| 10895 | /* process wrap case, we detect a wrap. In this case, the |
| 10896 | * comparison is performed in two parts. |
| 10897 | */ |
| 10898 | |
| 10899 | /* This is the end, we dont have any other chunk. */ |
| 10900 | if (bufs != chunks || !bufs[2]) |
| 10901 | return NULL; |
| 10902 | |
| 10903 | /* Compute the length of each part of the comparison. */ |
| 10904 | l1 = last - pos; |
| 10905 | l2 = url_param_name_l - l1; |
| 10906 | |
| 10907 | /* The second buffer is too short to contain the compared string. */ |
| 10908 | if (bufs[2] + l2 > bufs[3]) |
| 10909 | return NULL; |
| 10910 | |
| 10911 | if (memcmp(pos, url_param_name, l1) == 0 && |
| 10912 | memcmp(bufs[2], url_param_name+l1, l2) == 0) |
| 10913 | return pos; |
| 10914 | |
| 10915 | /* Perform wrapping and jump the string who fail the comparison. */ |
| 10916 | bufs += 2; |
| 10917 | pos = bufs[0] + l2; |
| 10918 | last = bufs[1]; |
| 10919 | |
| 10920 | } else { |
| 10921 | /* process a simple comparison. */ |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 10922 | if (memcmp(pos, url_param_name, url_param_name_l) == 0) |
| 10923 | return pos; |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10924 | pos += url_param_name_l + 1; |
| 10925 | if (fix_pointer_if_wrap(chunks, &pos)) |
| 10926 | last = bufs[2]; |
| 10927 | } |
| 10928 | } |
| 10929 | |
| 10930 | while (1) { |
| 10931 | /* Look for the next delimiter. */ |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 10932 | while (pos < last && !is_param_delimiter(*pos, delim)) |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10933 | pos++; |
| 10934 | if (pos < last) |
| 10935 | break; |
| 10936 | /* process buffer wrapping. */ |
| 10937 | if (bufs != chunks || !bufs[2]) |
| 10938 | return NULL; |
| 10939 | bufs += 2; |
| 10940 | pos = bufs[0]; |
| 10941 | last = bufs[1]; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10942 | } |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10943 | pos++; |
| 10944 | } |
| 10945 | return NULL; |
| 10946 | } |
| 10947 | |
| 10948 | /* |
Cyril Bonté | ce1ef4d | 2015-11-26 21:39:56 +0100 | [diff] [blame] | 10949 | * Given a url parameter name and a query string, find the next value. |
| 10950 | * An empty url_param_name matches the first available parameter. |
| 10951 | * If the parameter is found, 1 is returned and *vstart / *vend are updated to |
| 10952 | * respectively provide a pointer to the value and its end. |
| 10953 | * Otherwise, 0 is returned and vstart/vend are not modified. |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10954 | */ |
| 10955 | static int |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10956 | find_next_url_param(const char **chunks, |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 10957 | const char* url_param_name, size_t url_param_name_l, |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10958 | const char **vstart, const char **vend, char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10959 | { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10960 | const char *arg_start, *qs_end; |
| 10961 | const char *value_start, *value_end; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10962 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10963 | arg_start = chunks[0]; |
| 10964 | qs_end = chunks[1]; |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 10965 | if (url_param_name_l) { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10966 | /* Looks for an argument name. */ |
| 10967 | arg_start = find_url_param_pos(chunks, |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 10968 | url_param_name, url_param_name_l, |
| 10969 | delim); |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10970 | /* Check for wrapping. */ |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 10971 | if (arg_start >= qs_end) |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10972 | qs_end = chunks[3]; |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 10973 | } |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10974 | if (!arg_start) |
| 10975 | return 0; |
| 10976 | |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 10977 | if (!url_param_name_l) { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10978 | while (1) { |
| 10979 | /* looks for the first argument. */ |
| 10980 | value_start = memchr(arg_start, '=', qs_end - arg_start); |
| 10981 | if (!value_start) { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10982 | /* Check for wrapping. */ |
| 10983 | if (arg_start >= chunks[0] && |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 10984 | arg_start < chunks[1] && |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10985 | chunks[2]) { |
| 10986 | arg_start = chunks[2]; |
| 10987 | qs_end = chunks[3]; |
| 10988 | continue; |
| 10989 | } |
| 10990 | return 0; |
| 10991 | } |
| 10992 | break; |
| 10993 | } |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 10994 | value_start++; |
| 10995 | } |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10996 | else { |
| 10997 | /* Jump the argument length. */ |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 10998 | value_start = arg_start + url_param_name_l + 1; |
| 10999 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11000 | /* Check for pointer wrapping. */ |
| 11001 | if (fix_pointer_if_wrap(chunks, &value_start)) { |
| 11002 | /* Update the end pointer. */ |
| 11003 | qs_end = chunks[3]; |
| 11004 | |
| 11005 | /* Check for overflow. */ |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 11006 | if (value_start >= qs_end) |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11007 | return 0; |
| 11008 | } |
| 11009 | } |
| 11010 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11011 | value_end = value_start; |
| 11012 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11013 | while (1) { |
| 11014 | while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim)) |
| 11015 | value_end++; |
| 11016 | if (value_end < qs_end) |
| 11017 | break; |
| 11018 | /* process buffer wrapping. */ |
| 11019 | if (value_end >= chunks[0] && |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 11020 | value_end < chunks[1] && |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11021 | chunks[2]) { |
| 11022 | value_end = chunks[2]; |
| 11023 | qs_end = chunks[3]; |
| 11024 | continue; |
| 11025 | } |
| 11026 | break; |
| 11027 | } |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11028 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11029 | *vstart = value_start; |
| 11030 | *vend = value_end; |
Cyril Bonté | ce1ef4d | 2015-11-26 21:39:56 +0100 | [diff] [blame] | 11031 | return 1; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11032 | } |
| 11033 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11034 | /* This scans a URL-encoded query string. It takes an optionally wrapping |
| 11035 | * string whose first contigous chunk has its beginning in ctx->a[0] and end |
| 11036 | * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The |
| 11037 | * pointers are updated for next iteration before leaving. |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11038 | */ |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11039 | static int |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11040 | 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] | 11041 | { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11042 | const char *vstart, *vend; |
| 11043 | struct chunk *temp; |
| 11044 | const char **chunks = (const char **)smp->ctx.a; |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11045 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11046 | if (!find_next_url_param(chunks, |
Thierry FOURNIER | 0948d41 | 2015-05-20 15:22:37 +0200 | [diff] [blame] | 11047 | name, name_len, |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11048 | &vstart, &vend, |
Thierry FOURNIER | 0948d41 | 2015-05-20 15:22:37 +0200 | [diff] [blame] | 11049 | delim)) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11050 | return 0; |
| 11051 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11052 | /* Create sample. If the value is contiguous, return the pointer as CONST, |
| 11053 | * if the value is wrapped, copy-it in a buffer. |
| 11054 | */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11055 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11056 | if (chunks[2] && |
| 11057 | vstart >= chunks[0] && vstart <= chunks[1] && |
| 11058 | vend >= chunks[2] && vend <= chunks[3]) { |
| 11059 | /* Wrapped case. */ |
| 11060 | temp = get_trash_chunk(); |
| 11061 | memcpy(temp->str, vstart, chunks[1] - vstart); |
| 11062 | memcpy(temp->str + ( chunks[1] - vstart ), chunks[2], vend - chunks[2]); |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11063 | smp->data.u.str.str = temp->str; |
| 11064 | smp->data.u.str.len = ( chunks[1] - vstart ) + ( vend - chunks[2] ); |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11065 | } else { |
| 11066 | /* Contiguous case. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11067 | smp->data.u.str.str = (char *)vstart; |
| 11068 | smp->data.u.str.len = vend - vstart; |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11069 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
| 11070 | } |
| 11071 | |
| 11072 | /* Update context, check wrapping. */ |
| 11073 | chunks[0] = vend; |
| 11074 | if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) { |
| 11075 | chunks[1] = chunks[3]; |
| 11076 | chunks[2] = NULL; |
| 11077 | } |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11078 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11079 | if (chunks[0] < chunks[1]) |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11080 | smp->flags |= SMP_F_NOT_LAST; |
| 11081 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11082 | return 1; |
| 11083 | } |
| 11084 | |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11085 | /* This function iterates over each parameter of the query string. It uses |
| 11086 | * 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] | 11087 | * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL. |
| 11088 | * An optional parameter name is passed in args[0], otherwise any parameter is |
| 11089 | * considered. It supports an optional delimiter argument for the beginning of |
| 11090 | * the string in args[1], which defaults to "?". |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11091 | */ |
| 11092 | static int |
| 11093 | smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 11094 | { |
| 11095 | struct http_msg *msg; |
| 11096 | char delim = '?'; |
| 11097 | const char *name; |
| 11098 | int name_len; |
| 11099 | |
Dragan Dosen | 26f77e5 | 2015-05-25 10:02:11 +0200 | [diff] [blame] | 11100 | if (!args || |
| 11101 | (args[0].type && args[0].type != ARGT_STR) || |
| 11102 | (args[1].type && args[1].type != ARGT_STR)) |
| 11103 | return 0; |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11104 | |
Dragan Dosen | 26f77e5 | 2015-05-25 10:02:11 +0200 | [diff] [blame] | 11105 | name = ""; |
| 11106 | name_len = 0; |
| 11107 | if (args->type == ARGT_STR) { |
| 11108 | name = args->data.str.str; |
| 11109 | name_len = args->data.str.len; |
| 11110 | } |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11111 | |
Dragan Dosen | 26f77e5 | 2015-05-25 10:02:11 +0200 | [diff] [blame] | 11112 | if (args[1].type) |
| 11113 | delim = *args[1].data.str.str; |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11114 | |
Dragan Dosen | 26f77e5 | 2015-05-25 10:02:11 +0200 | [diff] [blame] | 11115 | if (!smp->ctx.a[0]) { // first call, find the query string |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11116 | CHECK_HTTP_MESSAGE_FIRST(); |
| 11117 | |
| 11118 | msg = &smp->strm->txn->req; |
| 11119 | |
| 11120 | smp->ctx.a[0] = find_param_list(msg->chn->buf->p + msg->sl.rq.u, |
| 11121 | msg->sl.rq.u_l, delim); |
| 11122 | if (!smp->ctx.a[0]) |
| 11123 | return 0; |
| 11124 | |
| 11125 | 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] | 11126 | |
| 11127 | /* Assume that the context is filled with NULL pointer |
| 11128 | * before the first call. |
| 11129 | * smp->ctx.a[2] = NULL; |
| 11130 | * smp->ctx.a[3] = NULL; |
| 11131 | */ |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11132 | } |
| 11133 | |
| 11134 | return smp_fetch_param(delim, name, name_len, args, smp, kw, private); |
| 11135 | } |
| 11136 | |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 11137 | /* This function iterates over each parameter of the body. This requires |
| 11138 | * 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] | 11139 | * ctx->a[0] and ctx->a[1] to store the beginning and end of the first |
| 11140 | * contigous part of the body, and optionally ctx->a[2..3] to reference the |
| 11141 | * optional second part if the body wraps at the end of the buffer. An optional |
| 11142 | * parameter name is passed in args[0], otherwise any parameter is considered. |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 11143 | */ |
| 11144 | static int |
| 11145 | smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 11146 | { |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 11147 | struct http_msg *msg; |
| 11148 | unsigned long len; |
| 11149 | unsigned long block1; |
| 11150 | char *body; |
| 11151 | const char *name; |
| 11152 | int name_len; |
| 11153 | |
| 11154 | if (!args || (args[0].type && args[0].type != ARGT_STR)) |
| 11155 | return 0; |
| 11156 | |
| 11157 | name = ""; |
| 11158 | name_len = 0; |
| 11159 | if (args[0].type == ARGT_STR) { |
| 11160 | name = args[0].data.str.str; |
| 11161 | name_len = args[0].data.str.len; |
| 11162 | } |
| 11163 | |
| 11164 | if (!smp->ctx.a[0]) { // first call, find the query string |
| 11165 | CHECK_HTTP_MESSAGE_FIRST(); |
| 11166 | |
| 11167 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 11168 | msg = &smp->strm->txn->req; |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 11169 | else |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 11170 | msg = &smp->strm->txn->rsp; |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 11171 | |
| 11172 | len = http_body_bytes(msg); |
| 11173 | body = b_ptr(msg->chn->buf, -http_data_rewind(msg)); |
| 11174 | |
| 11175 | block1 = len; |
| 11176 | if (block1 > msg->chn->buf->data + msg->chn->buf->size - body) |
| 11177 | block1 = msg->chn->buf->data + msg->chn->buf->size - body; |
| 11178 | |
| 11179 | if (block1 == len) { |
| 11180 | /* buffer is not wrapped (or empty) */ |
| 11181 | smp->ctx.a[0] = body; |
| 11182 | smp->ctx.a[1] = body + len; |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11183 | |
| 11184 | /* Assume that the context is filled with NULL pointer |
| 11185 | * before the first call. |
| 11186 | * smp->ctx.a[2] = NULL; |
| 11187 | * smp->ctx.a[3] = NULL; |
| 11188 | */ |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 11189 | } |
| 11190 | else { |
| 11191 | /* buffer is wrapped, we need to defragment it */ |
| 11192 | smp->ctx.a[0] = body; |
| 11193 | smp->ctx.a[1] = body + block1; |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11194 | smp->ctx.a[2] = msg->chn->buf->data; |
| 11195 | smp->ctx.a[3] = msg->chn->buf->data + ( len - block1 ); |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 11196 | } |
| 11197 | } |
| 11198 | return smp_fetch_param('&', name, name_len, args, smp, kw, private); |
| 11199 | } |
| 11200 | |
Willy Tarreau | a9fddca | 2012-07-31 07:51:48 +0200 | [diff] [blame] | 11201 | /* Return the signed integer value for the specified url parameter (see url_param |
| 11202 | * above). |
| 11203 | */ |
| 11204 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11205 | 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] | 11206 | { |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11207 | int ret = smp_fetch_url_param(args, smp, kw, private); |
Willy Tarreau | a9fddca | 2012-07-31 07:51:48 +0200 | [diff] [blame] | 11208 | |
| 11209 | if (ret > 0) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11210 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11211 | 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] | 11212 | } |
| 11213 | |
| 11214 | return ret; |
| 11215 | } |
| 11216 | |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11217 | /* This produces a 32-bit hash of the concatenation of the first occurrence of |
| 11218 | * the Host header followed by the path component if it begins with a slash ('/'). |
| 11219 | * This means that '*' will not be added, resulting in exactly the first Host |
| 11220 | * entry. If no Host header is found, then the path is used. The resulting value |
| 11221 | * is hashed using the url hash followed by a full avalanche hash and provides a |
| 11222 | * 32-bit integer value. This fetch is useful for tracking per-URL activity on |
| 11223 | * high-traffic sites without having to store whole paths. |
| 11224 | * this differs from the base32 functions in that it includes the url parameters |
| 11225 | * as well as the path |
| 11226 | */ |
| 11227 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11228 | 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] | 11229 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11230 | struct http_txn *txn; |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11231 | struct hdr_ctx ctx; |
| 11232 | unsigned int hash = 0; |
| 11233 | char *ptr, *beg, *end; |
| 11234 | int len; |
| 11235 | |
| 11236 | CHECK_HTTP_MESSAGE_FIRST(); |
| 11237 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11238 | txn = smp->strm->txn; |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11239 | ctx.idx = 0; |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 11240 | 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] | 11241 | /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */ |
| 11242 | ptr = ctx.line + ctx.val; |
| 11243 | len = ctx.vlen; |
| 11244 | while (len--) |
| 11245 | hash = *(ptr++) + (hash << 6) + (hash << 16) - hash; |
| 11246 | } |
| 11247 | |
| 11248 | /* now retrieve the path */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 11249 | 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] | 11250 | beg = http_get_path(txn); |
| 11251 | if (!beg) |
| 11252 | beg = end; |
| 11253 | |
| 11254 | for (ptr = beg; ptr < end ; ptr++); |
| 11255 | |
| 11256 | if (beg < ptr && *beg == '/') { |
| 11257 | while (beg < ptr) |
| 11258 | hash = *(beg++) + (hash << 6) + (hash << 16) - hash; |
| 11259 | } |
| 11260 | hash = full_hash(hash); |
| 11261 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11262 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11263 | smp->data.u.sint = hash; |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11264 | smp->flags = SMP_F_VOL_1ST; |
| 11265 | return 1; |
| 11266 | } |
| 11267 | |
| 11268 | /* This concatenates the source address with the 32-bit hash of the Host and |
| 11269 | * URL as returned by smp_fetch_base32(). The idea is to have per-source and |
| 11270 | * per-url counters. The result is a binary block from 8 to 20 bytes depending |
| 11271 | * on the source address length. The URL hash is stored before the address so |
| 11272 | * that in environments where IPv6 is insignificant, truncating the output to |
| 11273 | * 8 bytes would still work. |
| 11274 | */ |
| 11275 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11276 | 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] | 11277 | { |
| 11278 | struct chunk *temp; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11279 | struct connection *cli_conn = objt_conn(smp->sess->origin); |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11280 | |
Dragan Dosen | db5af61 | 2016-06-16 11:23:01 +0200 | [diff] [blame] | 11281 | if (!cli_conn) |
| 11282 | return 0; |
| 11283 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11284 | if (!smp_fetch_url32(args, smp, kw, private)) |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11285 | return 0; |
| 11286 | |
| 11287 | temp = get_trash_chunk(); |
Dragan Dosen | e5f4133 | 2016-06-16 11:08:08 +0200 | [diff] [blame] | 11288 | *(unsigned int *)temp->str = htonl(smp->data.u.sint); |
| 11289 | temp->len += sizeof(unsigned int); |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11290 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 11291 | switch (cli_conn->addr.from.ss_family) { |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11292 | case AF_INET: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 11293 | 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] | 11294 | temp->len += 4; |
| 11295 | break; |
| 11296 | case AF_INET6: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 11297 | 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] | 11298 | temp->len += 16; |
| 11299 | break; |
| 11300 | default: |
| 11301 | return 0; |
| 11302 | } |
| 11303 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11304 | smp->data.u.str = *temp; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11305 | smp->data.type = SMP_T_BIN; |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11306 | return 1; |
| 11307 | } |
| 11308 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 11309 | /* This function is used to validate the arguments passed to any "hdr" fetch |
| 11310 | * keyword. These keywords support an optional positive or negative occurrence |
| 11311 | * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It |
| 11312 | * is assumed that the types are already the correct ones. Returns 0 on error, |
| 11313 | * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an |
| 11314 | * error message in case of error, that the caller is responsible for freeing. |
| 11315 | * The initial location must either be freeable or NULL. |
| 11316 | */ |
Thierry FOURNIER | 49f45af | 2014-12-08 19:50:43 +0100 | [diff] [blame] | 11317 | int val_hdr(struct arg *arg, char **err_msg) |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 11318 | { |
| 11319 | 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] | 11320 | memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY); |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 11321 | return 0; |
| 11322 | } |
| 11323 | return 1; |
| 11324 | } |
| 11325 | |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 11326 | /* takes an UINT value on input supposed to represent the time since EPOCH, |
| 11327 | * adds an optional offset found in args[0] and emits a string representing |
| 11328 | * the date in RFC-1123/5322 format. |
| 11329 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11330 | 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] | 11331 | { |
Cyril Bonté | f78d896 | 2016-01-22 19:40:28 +0100 | [diff] [blame] | 11332 | const char day[7][4] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 11333 | const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; |
| 11334 | struct chunk *temp; |
| 11335 | struct tm *tm; |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 11336 | /* 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] | 11337 | time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL; |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 11338 | |
| 11339 | /* add offset */ |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 11340 | if (args && (args[0].type == ARGT_SINT)) |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 11341 | curr_date += args[0].data.sint; |
| 11342 | |
| 11343 | tm = gmtime(&curr_date); |
Thierry FOURNIER | fac9ccf | 2015-07-08 00:15:20 +0200 | [diff] [blame] | 11344 | if (!tm) |
| 11345 | return 0; |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 11346 | |
| 11347 | temp = get_trash_chunk(); |
| 11348 | temp->len = snprintf(temp->str, temp->size - temp->len, |
| 11349 | "%s, %02d %s %04d %02d:%02d:%02d GMT", |
| 11350 | day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year, |
| 11351 | tm->tm_hour, tm->tm_min, tm->tm_sec); |
| 11352 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11353 | smp->data.u.str = *temp; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11354 | smp->data.type = SMP_T_STR; |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 11355 | return 1; |
| 11356 | } |
| 11357 | |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11358 | /* Match language range with language tag. RFC2616 14.4: |
| 11359 | * |
| 11360 | * A language-range matches a language-tag if it exactly equals |
| 11361 | * the tag, or if it exactly equals a prefix of the tag such |
| 11362 | * that the first tag character following the prefix is "-". |
| 11363 | * |
| 11364 | * Return 1 if the strings match, else return 0. |
| 11365 | */ |
| 11366 | static inline int language_range_match(const char *range, int range_len, |
| 11367 | const char *tag, int tag_len) |
| 11368 | { |
| 11369 | const char *end = range + range_len; |
| 11370 | const char *tend = tag + tag_len; |
| 11371 | while (range < end) { |
| 11372 | if (*range == '-' && tag == tend) |
| 11373 | return 1; |
| 11374 | if (*range != *tag || tag == tend) |
| 11375 | return 0; |
| 11376 | range++; |
| 11377 | tag++; |
| 11378 | } |
| 11379 | /* Return true only if the last char of the tag is matched. */ |
| 11380 | return tag == tend; |
| 11381 | } |
| 11382 | |
| 11383 | /* 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] | 11384 | 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] | 11385 | { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11386 | const char *al = smp->data.u.str.str; |
| 11387 | const char *end = al + smp->data.u.str.len; |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11388 | const char *token; |
| 11389 | int toklen; |
| 11390 | int qvalue; |
| 11391 | const char *str; |
| 11392 | const char *w; |
| 11393 | int best_q = 0; |
| 11394 | |
| 11395 | /* Set the constant to the sample, because the output of the |
| 11396 | * function will be peek in the constant configuration string. |
| 11397 | */ |
| 11398 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11399 | smp->data.u.str.size = 0; |
| 11400 | smp->data.u.str.str = ""; |
| 11401 | smp->data.u.str.len = 0; |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11402 | |
| 11403 | /* Parse the accept language */ |
| 11404 | while (1) { |
| 11405 | |
| 11406 | /* Jump spaces, quit if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 11407 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11408 | al++; |
| 11409 | if (al >= end) |
| 11410 | break; |
| 11411 | |
| 11412 | /* Start of the fisrt word. */ |
| 11413 | token = al; |
| 11414 | |
| 11415 | /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 11416 | while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11417 | al++; |
| 11418 | if (al == token) |
| 11419 | goto expect_comma; |
| 11420 | |
| 11421 | /* Length of the token. */ |
| 11422 | toklen = al - token; |
| 11423 | qvalue = 1000; |
| 11424 | |
| 11425 | /* Check if the token exists in the list. If the token not exists, |
| 11426 | * jump to the next token. |
| 11427 | */ |
| 11428 | str = args[0].data.str.str; |
| 11429 | w = str; |
| 11430 | while (1) { |
| 11431 | if (*str == ';' || *str == '\0') { |
| 11432 | if (language_range_match(token, toklen, w, str-w)) |
| 11433 | goto look_for_q; |
| 11434 | if (*str == '\0') |
| 11435 | goto expect_comma; |
| 11436 | w = str + 1; |
| 11437 | } |
| 11438 | str++; |
| 11439 | } |
| 11440 | goto expect_comma; |
| 11441 | |
| 11442 | look_for_q: |
| 11443 | |
| 11444 | /* Jump spaces, quit if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 11445 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11446 | al++; |
| 11447 | if (al >= end) |
| 11448 | goto process_value; |
| 11449 | |
| 11450 | /* If ',' is found, process the result */ |
| 11451 | if (*al == ',') |
| 11452 | goto process_value; |
| 11453 | |
| 11454 | /* If the character is different from ';', look |
| 11455 | * for the end of the header part in best effort. |
| 11456 | */ |
| 11457 | if (*al != ';') |
| 11458 | goto expect_comma; |
| 11459 | |
| 11460 | /* Assumes that the char is ';', now expect "q=". */ |
| 11461 | al++; |
| 11462 | |
| 11463 | /* Jump spaces, process value 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 | /* Expect 'q'. If no 'q', continue in best effort */ |
| 11470 | if (*al != 'q') |
| 11471 | goto process_value; |
| 11472 | al++; |
| 11473 | |
| 11474 | /* Jump spaces, process value if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 11475 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11476 | al++; |
| 11477 | if (al >= end) |
| 11478 | goto process_value; |
| 11479 | |
| 11480 | /* Expect '='. If no '=', continue in best effort */ |
| 11481 | if (*al != '=') |
| 11482 | goto process_value; |
| 11483 | al++; |
| 11484 | |
| 11485 | /* Jump spaces, process value if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 11486 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11487 | al++; |
| 11488 | if (al >= end) |
| 11489 | goto process_value; |
| 11490 | |
| 11491 | /* Parse the q value. */ |
| 11492 | qvalue = parse_qvalue(al, &al); |
| 11493 | |
| 11494 | process_value: |
| 11495 | |
| 11496 | /* If the new q value is the best q value, then store the associated |
| 11497 | * language in the response. If qvalue is the biggest value (1000), |
| 11498 | * break the process. |
| 11499 | */ |
| 11500 | if (qvalue > best_q) { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11501 | smp->data.u.str.str = (char *)w; |
| 11502 | smp->data.u.str.len = str - w; |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11503 | if (qvalue >= 1000) |
| 11504 | break; |
| 11505 | best_q = qvalue; |
| 11506 | } |
| 11507 | |
| 11508 | expect_comma: |
| 11509 | |
| 11510 | /* Expect comma or end. If the end is detected, quit the loop. */ |
| 11511 | while (al < end && *al != ',') |
| 11512 | al++; |
| 11513 | if (al >= end) |
| 11514 | break; |
| 11515 | |
| 11516 | /* Comma is found, jump it and restart the analyzer. */ |
| 11517 | al++; |
| 11518 | } |
| 11519 | |
| 11520 | /* Set default value if required. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11521 | if (smp->data.u.str.len == 0 && args[1].type == ARGT_STR) { |
| 11522 | smp->data.u.str.str = args[1].data.str.str; |
| 11523 | smp->data.u.str.len = args[1].data.str.len; |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11524 | } |
| 11525 | |
| 11526 | /* Return true only if a matching language was found. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11527 | return smp->data.u.str.len != 0; |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11528 | } |
| 11529 | |
Thierry FOURNIER | 82ff3c9 | 2015-05-07 15:46:20 +0200 | [diff] [blame] | 11530 | /* This fetch url-decode any input string. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11531 | 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] | 11532 | { |
| 11533 | /* If the constant flag is set or if not size is avalaible at |
| 11534 | * the end of the buffer, copy the string in other buffer |
| 11535 | * before decoding. |
| 11536 | */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11537 | 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] | 11538 | struct chunk *str = get_trash_chunk(); |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11539 | memcpy(str->str, smp->data.u.str.str, smp->data.u.str.len); |
| 11540 | smp->data.u.str.str = str->str; |
| 11541 | smp->data.u.str.size = str->size; |
Thierry FOURNIER | 82ff3c9 | 2015-05-07 15:46:20 +0200 | [diff] [blame] | 11542 | smp->flags &= ~SMP_F_CONST; |
| 11543 | } |
| 11544 | |
| 11545 | /* Add final \0 required by url_decode(), and convert the input string. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11546 | smp->data.u.str.str[smp->data.u.str.len] = '\0'; |
| 11547 | smp->data.u.str.len = url_decode(smp->data.u.str.str); |
Christopher Faulet | a258479 | 2017-10-05 10:03:12 +0200 | [diff] [blame] | 11548 | return (smp->data.u.str.len >= 0); |
Thierry FOURNIER | 82ff3c9 | 2015-05-07 15:46:20 +0200 | [diff] [blame] | 11549 | } |
| 11550 | |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 11551 | static int smp_conv_req_capture(const struct arg *args, struct sample *smp, void *private) |
| 11552 | { |
| 11553 | struct proxy *fe = strm_fe(smp->strm); |
| 11554 | int idx, i; |
| 11555 | struct cap_hdr *hdr; |
| 11556 | int len; |
| 11557 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 11558 | if (!args || args->type != ARGT_SINT) |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 11559 | return 0; |
| 11560 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 11561 | idx = args->data.sint; |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 11562 | |
| 11563 | /* Check the availibity of the capture id. */ |
| 11564 | if (idx > fe->nb_req_cap - 1) |
| 11565 | return 0; |
| 11566 | |
| 11567 | /* Look for the original configuration. */ |
| 11568 | for (hdr = fe->req_cap, i = fe->nb_req_cap - 1; |
| 11569 | hdr != NULL && i != idx ; |
| 11570 | i--, hdr = hdr->next); |
| 11571 | if (!hdr) |
| 11572 | return 0; |
| 11573 | |
| 11574 | /* check for the memory allocation */ |
| 11575 | if (smp->strm->req_cap[hdr->index] == NULL) |
| 11576 | smp->strm->req_cap[hdr->index] = pool_alloc2(hdr->pool); |
| 11577 | if (smp->strm->req_cap[hdr->index] == NULL) |
| 11578 | return 0; |
| 11579 | |
| 11580 | /* Check length. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11581 | len = smp->data.u.str.len; |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 11582 | if (len > hdr->len) |
| 11583 | len = hdr->len; |
| 11584 | |
| 11585 | /* Capture input data. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11586 | memcpy(smp->strm->req_cap[idx], smp->data.u.str.str, len); |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 11587 | smp->strm->req_cap[idx][len] = '\0'; |
| 11588 | |
| 11589 | return 1; |
| 11590 | } |
| 11591 | |
| 11592 | static int smp_conv_res_capture(const struct arg *args, struct sample *smp, void *private) |
| 11593 | { |
| 11594 | struct proxy *fe = strm_fe(smp->strm); |
| 11595 | int idx, i; |
| 11596 | struct cap_hdr *hdr; |
| 11597 | int len; |
| 11598 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 11599 | if (!args || args->type != ARGT_SINT) |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 11600 | return 0; |
| 11601 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 11602 | idx = args->data.sint; |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 11603 | |
| 11604 | /* Check the availibity of the capture id. */ |
| 11605 | if (idx > fe->nb_rsp_cap - 1) |
| 11606 | return 0; |
| 11607 | |
| 11608 | /* Look for the original configuration. */ |
| 11609 | for (hdr = fe->rsp_cap, i = fe->nb_rsp_cap - 1; |
| 11610 | hdr != NULL && i != idx ; |
| 11611 | i--, hdr = hdr->next); |
| 11612 | if (!hdr) |
| 11613 | return 0; |
| 11614 | |
| 11615 | /* check for the memory allocation */ |
| 11616 | if (smp->strm->res_cap[hdr->index] == NULL) |
| 11617 | smp->strm->res_cap[hdr->index] = pool_alloc2(hdr->pool); |
| 11618 | if (smp->strm->res_cap[hdr->index] == NULL) |
| 11619 | return 0; |
| 11620 | |
| 11621 | /* Check length. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11622 | len = smp->data.u.str.len; |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 11623 | if (len > hdr->len) |
| 11624 | len = hdr->len; |
| 11625 | |
| 11626 | /* Capture input data. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11627 | memcpy(smp->strm->res_cap[idx], smp->data.u.str.str, len); |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 11628 | smp->strm->res_cap[idx][len] = '\0'; |
| 11629 | |
| 11630 | return 1; |
| 11631 | } |
| 11632 | |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11633 | /* 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] | 11634 | * takes the string from the variable 'replace' with length 'len', then modifies |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11635 | * the relevant part of the request line accordingly. Then it updates various |
| 11636 | * pointers to the next elements which were moved, and the total buffer length. |
| 11637 | * 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] | 11638 | * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal |
| 11639 | * error, though this can be revisited when this code is finally exploited. |
| 11640 | * |
| 11641 | * 'action' can be '0' to replace method, '1' to replace path, '2' to replace |
| 11642 | * query string and 3 to replace uri. |
| 11643 | * |
| 11644 | * In query string case, the mark question '?' must be set at the start of the |
| 11645 | * string by the caller, event if the replacement query string is empty. |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11646 | */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11647 | int http_replace_req_line(int action, const char *replace, int len, |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 11648 | struct proxy *px, struct stream *s) |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11649 | { |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 11650 | struct http_txn *txn = s->txn; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11651 | char *cur_ptr, *cur_end; |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11652 | int offset = 0; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11653 | int delta; |
| 11654 | |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11655 | switch (action) { |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11656 | case 0: // method |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 11657 | cur_ptr = s->req.buf->p; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11658 | cur_end = cur_ptr + txn->req.sl.rq.m_l; |
| 11659 | |
| 11660 | /* adjust req line offsets and lengths */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11661 | delta = len - offset - (cur_end - cur_ptr); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11662 | txn->req.sl.rq.m_l += delta; |
| 11663 | txn->req.sl.rq.u += delta; |
| 11664 | txn->req.sl.rq.v += delta; |
| 11665 | break; |
| 11666 | |
| 11667 | case 1: // path |
| 11668 | cur_ptr = http_get_path(txn); |
| 11669 | if (!cur_ptr) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 11670 | cur_ptr = s->req.buf->p + txn->req.sl.rq.u; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11671 | |
| 11672 | cur_end = cur_ptr; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 11673 | 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] | 11674 | cur_end++; |
| 11675 | |
| 11676 | /* adjust req line offsets and lengths */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11677 | delta = len - offset - (cur_end - cur_ptr); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11678 | txn->req.sl.rq.u_l += delta; |
| 11679 | txn->req.sl.rq.v += delta; |
| 11680 | break; |
| 11681 | |
| 11682 | case 2: // query |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11683 | offset = 1; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 11684 | cur_ptr = s->req.buf->p + txn->req.sl.rq.u; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11685 | cur_end = cur_ptr + txn->req.sl.rq.u_l; |
| 11686 | while (cur_ptr < cur_end && *cur_ptr != '?') |
| 11687 | cur_ptr++; |
| 11688 | |
| 11689 | /* skip the question mark or indicate that we must insert it |
| 11690 | * (but only if the format string is not empty then). |
| 11691 | */ |
| 11692 | if (cur_ptr < cur_end) |
| 11693 | cur_ptr++; |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11694 | else if (len > 1) |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11695 | offset = 0; |
| 11696 | |
| 11697 | /* adjust req line offsets and lengths */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11698 | delta = len - offset - (cur_end - cur_ptr); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11699 | txn->req.sl.rq.u_l += delta; |
| 11700 | txn->req.sl.rq.v += delta; |
| 11701 | break; |
| 11702 | |
| 11703 | case 3: // uri |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 11704 | cur_ptr = s->req.buf->p + txn->req.sl.rq.u; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11705 | cur_end = cur_ptr + txn->req.sl.rq.u_l; |
| 11706 | |
| 11707 | /* adjust req line offsets and lengths */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11708 | delta = len - offset - (cur_end - cur_ptr); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11709 | txn->req.sl.rq.u_l += delta; |
| 11710 | txn->req.sl.rq.v += delta; |
| 11711 | break; |
| 11712 | |
| 11713 | default: |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11714 | return -1; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11715 | } |
| 11716 | |
| 11717 | /* commit changes and adjust end of message */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11718 | 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] | 11719 | txn->req.sl.rq.l += delta; |
| 11720 | txn->hdr_idx.v[0].len += delta; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11721 | http_msg_move_end(&txn->req, delta); |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11722 | return 0; |
| 11723 | } |
| 11724 | |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 11725 | /* This function replace the HTTP status code and the associated message. The |
| 11726 | * variable <status> contains the new status code. This function never fails. |
| 11727 | */ |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 11728 | 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] | 11729 | { |
| 11730 | struct http_txn *txn = s->txn; |
| 11731 | char *cur_ptr, *cur_end; |
| 11732 | int delta; |
| 11733 | char *res; |
| 11734 | int c_l; |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 11735 | const char *msg = reason; |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 11736 | int msg_len; |
| 11737 | |
| 11738 | chunk_reset(&trash); |
| 11739 | |
| 11740 | res = ultoa_o(status, trash.str, trash.size); |
| 11741 | c_l = res - trash.str; |
| 11742 | |
| 11743 | trash.str[c_l] = ' '; |
| 11744 | trash.len = c_l + 1; |
| 11745 | |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 11746 | /* Do we have a custom reason format string? */ |
| 11747 | if (msg == NULL) |
| 11748 | msg = get_reason(status); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 11749 | msg_len = strlen(msg); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 11750 | strncpy(&trash.str[trash.len], msg, trash.size - trash.len); |
| 11751 | trash.len += msg_len; |
| 11752 | |
| 11753 | cur_ptr = s->res.buf->p + txn->rsp.sl.st.c; |
| 11754 | cur_end = s->res.buf->p + txn->rsp.sl.st.r + txn->rsp.sl.st.r_l; |
| 11755 | |
| 11756 | /* commit changes and adjust message */ |
| 11757 | delta = buffer_replace2(s->res.buf, cur_ptr, cur_end, trash.str, trash.len); |
| 11758 | |
| 11759 | /* adjust res line offsets and lengths */ |
| 11760 | txn->rsp.sl.st.r += c_l - txn->rsp.sl.st.c_l; |
| 11761 | txn->rsp.sl.st.c_l = c_l; |
| 11762 | txn->rsp.sl.st.r_l = msg_len; |
| 11763 | |
| 11764 | delta = trash.len - (cur_end - cur_ptr); |
| 11765 | txn->rsp.sl.st.l += delta; |
| 11766 | txn->hdr_idx.v[0].len += delta; |
| 11767 | http_msg_move_end(&txn->rsp, delta); |
| 11768 | } |
| 11769 | |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11770 | /* This function executes one of the set-{method,path,query,uri} actions. It |
| 11771 | * 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] | 11772 | * the action to be performed in <http.action>, previously filled by function |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11773 | * parse_set_req_line(). The replacement action is excuted by the function |
Thierry FOURNIER | 3f4bc65 | 2015-08-26 16:23:34 +0200 | [diff] [blame] | 11774 | * http_action_set_req_line(). It always returns ACT_RET_CONT. If an error |
| 11775 | * occurs the action is canceled, but the rule processing continue. |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11776 | */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 11777 | 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] | 11778 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11779 | { |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 11780 | struct chunk *replace; |
| 11781 | enum act_return ret = ACT_RET_ERR; |
| 11782 | |
| 11783 | replace = alloc_trash_chunk(); |
| 11784 | if (!replace) |
| 11785 | goto leave; |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11786 | |
| 11787 | /* If we have to create a query string, prepare a '?'. */ |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 11788 | if (rule->arg.http.action == 2) |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 11789 | replace->str[replace->len++] = '?'; |
| 11790 | replace->len += build_logline(s, replace->str + replace->len, replace->size - replace->len, |
| 11791 | &rule->arg.http.logfmt); |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11792 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 11793 | http_replace_req_line(rule->arg.http.action, replace->str, replace->len, px, s); |
| 11794 | |
| 11795 | ret = ACT_RET_CONT; |
| 11796 | |
| 11797 | leave: |
| 11798 | free_trash_chunk(replace); |
| 11799 | return ret; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11800 | } |
| 11801 | |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 11802 | /* This function is just a compliant action wrapper for "set-status". */ |
| 11803 | 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] | 11804 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 11805 | { |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 11806 | http_set_status(rule->arg.status.code, rule->arg.status.reason, s); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 11807 | return ACT_RET_CONT; |
| 11808 | } |
| 11809 | |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11810 | /* parse an http-request action among : |
| 11811 | * set-method |
| 11812 | * set-path |
| 11813 | * set-query |
| 11814 | * set-uri |
| 11815 | * |
| 11816 | * All of them accept a single argument of type string representing a log-format. |
| 11817 | * The resulting rule makes use of arg->act.p[0..1] to store the log-format list |
| 11818 | * 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] | 11819 | * 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] | 11820 | */ |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 11821 | enum act_parse_ret parse_set_req_line(const char **args, int *orig_arg, struct proxy *px, |
| 11822 | struct act_rule *rule, char **err) |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11823 | { |
| 11824 | int cur_arg = *orig_arg; |
| 11825 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 11826 | rule->action = ACT_CUSTOM; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11827 | |
| 11828 | switch (args[0][4]) { |
| 11829 | case 'm' : |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 11830 | rule->arg.http.action = 0; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11831 | rule->action_ptr = http_action_set_req_line; |
| 11832 | break; |
| 11833 | case 'p' : |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 11834 | rule->arg.http.action = 1; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11835 | rule->action_ptr = http_action_set_req_line; |
| 11836 | break; |
| 11837 | case 'q' : |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 11838 | rule->arg.http.action = 2; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11839 | rule->action_ptr = http_action_set_req_line; |
| 11840 | break; |
| 11841 | case 'u' : |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 11842 | rule->arg.http.action = 3; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11843 | rule->action_ptr = http_action_set_req_line; |
| 11844 | break; |
| 11845 | default: |
| 11846 | memprintf(err, "internal error: unhandled action '%s'", args[0]); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 11847 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11848 | } |
| 11849 | |
| 11850 | if (!*args[cur_arg] || |
| 11851 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 11852 | memprintf(err, "expects exactly 1 argument <format>"); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 11853 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11854 | } |
| 11855 | |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 11856 | LIST_INIT(&rule->arg.http.logfmt); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11857 | proxy->conf.args.ctx = ARGC_HRQ; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 11858 | if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.http.logfmt, LOG_OPT_HTTP, |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 11859 | (proxy->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] | 11860 | return ACT_RET_PRS_ERR; |
| 11861 | } |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11862 | |
| 11863 | (*orig_arg)++; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 11864 | return ACT_RET_PRS_OK; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11865 | } |
| 11866 | |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 11867 | /* parse set-status action: |
| 11868 | * This action accepts a single argument of type int representing |
| 11869 | * an http status code. It returns ACT_RET_PRS_OK on success, |
| 11870 | * ACT_RET_PRS_ERR on error. |
| 11871 | */ |
| 11872 | enum act_parse_ret parse_http_set_status(const char **args, int *orig_arg, struct proxy *px, |
| 11873 | struct act_rule *rule, char **err) |
| 11874 | { |
| 11875 | char *error; |
| 11876 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 11877 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 11878 | rule->action_ptr = action_http_set_status; |
| 11879 | |
| 11880 | /* Check if an argument is available */ |
| 11881 | if (!*args[*orig_arg]) { |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 11882 | memprintf(err, "expects 1 argument: <status>; or 3 arguments: <status> reason <fmt>"); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 11883 | return ACT_RET_PRS_ERR; |
| 11884 | } |
| 11885 | |
| 11886 | /* convert status code as integer */ |
| 11887 | rule->arg.status.code = strtol(args[*orig_arg], &error, 10); |
| 11888 | if (*error != '\0' || rule->arg.status.code < 100 || rule->arg.status.code > 999) { |
| 11889 | memprintf(err, "expects an integer status code between 100 and 999"); |
| 11890 | return ACT_RET_PRS_ERR; |
| 11891 | } |
| 11892 | |
| 11893 | (*orig_arg)++; |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 11894 | |
| 11895 | /* set custom reason string */ |
| 11896 | rule->arg.status.reason = NULL; // If null, we use the default reason for the status code. |
| 11897 | if (*args[*orig_arg] && strcmp(args[*orig_arg], "reason") == 0 && |
| 11898 | (*args[*orig_arg + 1] && strcmp(args[*orig_arg + 1], "if") != 0 && strcmp(args[*orig_arg + 1], "unless") != 0)) { |
| 11899 | (*orig_arg)++; |
| 11900 | rule->arg.status.reason = strdup(args[*orig_arg]); |
| 11901 | (*orig_arg)++; |
| 11902 | } |
| 11903 | |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 11904 | return ACT_RET_PRS_OK; |
| 11905 | } |
| 11906 | |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 11907 | /* This function executes the "capture" action. It executes a fetch expression, |
| 11908 | * turns the result into a string and puts it in a capture slot. It always |
| 11909 | * returns 1. If an error occurs the action is cancelled, but the rule |
| 11910 | * processing continues. |
| 11911 | */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 11912 | 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] | 11913 | struct session *sess, struct stream *s, int flags) |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 11914 | { |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 11915 | struct sample *key; |
Thierry FOURNIER | 32b1500 | 2015-07-31 08:56:16 +0200 | [diff] [blame] | 11916 | struct cap_hdr *h = rule->arg.cap.hdr; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 11917 | char **cap = s->req_cap; |
| 11918 | int len; |
| 11919 | |
Thierry FOURNIER | 32b1500 | 2015-07-31 08:56:16 +0200 | [diff] [blame] | 11920 | 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] | 11921 | if (!key) |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 11922 | return ACT_RET_CONT; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 11923 | |
| 11924 | if (cap[h->index] == NULL) |
| 11925 | cap[h->index] = pool_alloc2(h->pool); |
| 11926 | |
| 11927 | if (cap[h->index] == NULL) /* no more capture memory */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 11928 | return ACT_RET_CONT; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 11929 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11930 | len = key->data.u.str.len; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 11931 | if (len > h->len) |
| 11932 | len = h->len; |
| 11933 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11934 | memcpy(cap[h->index], key->data.u.str.str, len); |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 11935 | cap[h->index][len] = 0; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 11936 | return ACT_RET_CONT; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 11937 | } |
| 11938 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 11939 | /* This function executes the "capture" action and store the result in a |
| 11940 | * capture slot if exists. It executes a fetch expression, turns the result |
| 11941 | * into a string and puts it in a capture slot. It always returns 1. If an |
| 11942 | * error occurs the action is cancelled, but the rule processing continues. |
| 11943 | */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 11944 | 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] | 11945 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 11946 | { |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 11947 | struct sample *key; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 11948 | struct cap_hdr *h; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 11949 | char **cap = s->req_cap; |
| 11950 | struct proxy *fe = strm_fe(s); |
| 11951 | int len; |
| 11952 | int i; |
| 11953 | |
| 11954 | /* Look for the original configuration. */ |
| 11955 | for (h = fe->req_cap, i = fe->nb_req_cap - 1; |
Thierry FOURNIER | e209797 | 2015-07-31 08:56:35 +0200 | [diff] [blame] | 11956 | h != NULL && i != rule->arg.capid.idx ; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 11957 | i--, h = h->next); |
| 11958 | if (!h) |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 11959 | return ACT_RET_CONT; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 11960 | |
Thierry FOURNIER | e209797 | 2015-07-31 08:56:35 +0200 | [diff] [blame] | 11961 | 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] | 11962 | if (!key) |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 11963 | return ACT_RET_CONT; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 11964 | |
| 11965 | if (cap[h->index] == NULL) |
| 11966 | cap[h->index] = pool_alloc2(h->pool); |
| 11967 | |
| 11968 | if (cap[h->index] == NULL) /* no more capture memory */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 11969 | return ACT_RET_CONT; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 11970 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11971 | len = key->data.u.str.len; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 11972 | if (len > h->len) |
| 11973 | len = h->len; |
| 11974 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11975 | memcpy(cap[h->index], key->data.u.str.str, len); |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 11976 | cap[h->index][len] = 0; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 11977 | return ACT_RET_CONT; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 11978 | } |
| 11979 | |
Christopher Faulet | 29730ba | 2017-09-18 15:26:32 +0200 | [diff] [blame] | 11980 | /* Check an "http-request capture" action. |
| 11981 | * |
| 11982 | * The function returns 1 in success case, otherwise, it returns 0 and err is |
| 11983 | * filled. |
| 11984 | */ |
| 11985 | int check_http_req_capture(struct act_rule *rule, struct proxy *px, char **err) |
| 11986 | { |
| 11987 | if (rule->arg.capid.idx >= px->nb_req_cap) { |
| 11988 | memprintf(err, "unable to find capture id '%d' referenced by http-request capture rule", |
| 11989 | rule->arg.capid.idx); |
| 11990 | return 0; |
| 11991 | } |
| 11992 | |
| 11993 | return 1; |
| 11994 | } |
| 11995 | |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 11996 | /* parse an "http-request capture" action. It takes a single argument which is |
| 11997 | * 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] | 11998 | * 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] | 11999 | * 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] | 12000 | */ |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12001 | enum act_parse_ret parse_http_req_capture(const char **args, int *orig_arg, struct proxy *px, |
| 12002 | struct act_rule *rule, char **err) |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12003 | { |
| 12004 | struct sample_expr *expr; |
| 12005 | struct cap_hdr *hdr; |
| 12006 | int cur_arg; |
Willy Tarreau | 3986ac1 | 2015-05-08 16:13:42 +0200 | [diff] [blame] | 12007 | int len = 0; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12008 | |
| 12009 | for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++) |
| 12010 | if (strcmp(args[cur_arg], "if") == 0 || |
| 12011 | strcmp(args[cur_arg], "unless") == 0) |
| 12012 | break; |
| 12013 | |
| 12014 | if (cur_arg < *orig_arg + 3) { |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12015 | memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]"); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12016 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12017 | } |
| 12018 | |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12019 | cur_arg = *orig_arg; |
| 12020 | expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args); |
| 12021 | if (!expr) |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12022 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12023 | |
| 12024 | if (!(expr->fetch->val & SMP_VAL_FE_HRQ_HDR)) { |
| 12025 | memprintf(err, |
| 12026 | "fetch method '%s' extracts information from '%s', none of which is available here", |
| 12027 | args[cur_arg-1], sample_src_names(expr->fetch->use)); |
| 12028 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12029 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12030 | } |
| 12031 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12032 | if (!args[cur_arg] || !*args[cur_arg]) { |
| 12033 | memprintf(err, "expects 'len or 'id'"); |
| 12034 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12035 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12036 | } |
| 12037 | |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12038 | if (strcmp(args[cur_arg], "len") == 0) { |
| 12039 | cur_arg++; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12040 | |
| 12041 | if (!(px->cap & PR_CAP_FE)) { |
| 12042 | memprintf(err, "proxy '%s' has no frontend capability", px->id); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12043 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12044 | } |
| 12045 | |
| 12046 | proxy->conf.args.ctx = ARGC_CAP; |
| 12047 | |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12048 | if (!args[cur_arg]) { |
| 12049 | memprintf(err, "missing length value"); |
| 12050 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12051 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12052 | } |
| 12053 | /* we copy the table name for now, it will be resolved later */ |
| 12054 | len = atoi(args[cur_arg]); |
| 12055 | if (len <= 0) { |
| 12056 | memprintf(err, "length must be > 0"); |
| 12057 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12058 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12059 | } |
| 12060 | cur_arg++; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12061 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12062 | if (!len) { |
| 12063 | memprintf(err, "a positive 'len' argument is mandatory"); |
| 12064 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12065 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12066 | } |
| 12067 | |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 12068 | hdr = calloc(1, sizeof(*hdr)); |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12069 | hdr->next = px->req_cap; |
| 12070 | hdr->name = NULL; /* not a header capture */ |
| 12071 | hdr->namelen = 0; |
| 12072 | hdr->len = len; |
| 12073 | hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED); |
| 12074 | hdr->index = px->nb_req_cap++; |
| 12075 | |
| 12076 | px->req_cap = hdr; |
| 12077 | px->to_log |= LW_REQHDR; |
| 12078 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 12079 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12080 | rule->action_ptr = http_action_req_capture; |
Christopher Faulet | 29730ba | 2017-09-18 15:26:32 +0200 | [diff] [blame] | 12081 | rule->check_ptr = check_http_req_capture; |
Thierry FOURNIER | 32b1500 | 2015-07-31 08:56:16 +0200 | [diff] [blame] | 12082 | rule->arg.cap.expr = expr; |
| 12083 | rule->arg.cap.hdr = hdr; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12084 | } |
| 12085 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12086 | else if (strcmp(args[cur_arg], "id") == 0) { |
| 12087 | int id; |
| 12088 | char *error; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12089 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12090 | cur_arg++; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12091 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12092 | if (!args[cur_arg]) { |
| 12093 | memprintf(err, "missing id value"); |
| 12094 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12095 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12096 | } |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12097 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12098 | id = strtol(args[cur_arg], &error, 10); |
| 12099 | if (*error != '\0') { |
| 12100 | memprintf(err, "cannot parse id '%s'", args[cur_arg]); |
| 12101 | free(expr); |
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 | cur_arg++; |
| 12105 | |
| 12106 | proxy->conf.args.ctx = ARGC_CAP; |
| 12107 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 12108 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12109 | rule->action_ptr = http_action_req_capture_by_id; |
Christopher Faulet | 29730ba | 2017-09-18 15:26:32 +0200 | [diff] [blame] | 12110 | rule->check_ptr = check_http_req_capture; |
Thierry FOURNIER | e209797 | 2015-07-31 08:56:35 +0200 | [diff] [blame] | 12111 | rule->arg.capid.expr = expr; |
| 12112 | rule->arg.capid.idx = id; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12113 | } |
| 12114 | |
| 12115 | else { |
| 12116 | memprintf(err, "expects 'len' or 'id', found '%s'", args[cur_arg]); |
| 12117 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12118 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12119 | } |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12120 | |
| 12121 | *orig_arg = cur_arg; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12122 | return ACT_RET_PRS_OK; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12123 | } |
| 12124 | |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12125 | /* This function executes the "capture" action and store the result in a |
| 12126 | * capture slot if exists. It executes a fetch expression, turns the result |
| 12127 | * into a string and puts it in a capture slot. It always returns 1. If an |
| 12128 | * error occurs the action is cancelled, but the rule processing continues. |
| 12129 | */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12130 | 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] | 12131 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12132 | { |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12133 | struct sample *key; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12134 | struct cap_hdr *h; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12135 | char **cap = s->res_cap; |
| 12136 | struct proxy *fe = strm_fe(s); |
| 12137 | int len; |
| 12138 | int i; |
| 12139 | |
| 12140 | /* Look for the original configuration. */ |
| 12141 | for (h = fe->rsp_cap, i = fe->nb_rsp_cap - 1; |
Thierry FOURNIER | e209797 | 2015-07-31 08:56:35 +0200 | [diff] [blame] | 12142 | h != NULL && i != rule->arg.capid.idx ; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12143 | i--, h = h->next); |
| 12144 | if (!h) |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12145 | return ACT_RET_CONT; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12146 | |
Thierry FOURNIER | e209797 | 2015-07-31 08:56:35 +0200 | [diff] [blame] | 12147 | 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] | 12148 | if (!key) |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12149 | return ACT_RET_CONT; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12150 | |
| 12151 | if (cap[h->index] == NULL) |
| 12152 | cap[h->index] = pool_alloc2(h->pool); |
| 12153 | |
| 12154 | if (cap[h->index] == NULL) /* no more capture memory */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12155 | return ACT_RET_CONT; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12156 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12157 | len = key->data.u.str.len; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12158 | if (len > h->len) |
| 12159 | len = h->len; |
| 12160 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12161 | memcpy(cap[h->index], key->data.u.str.str, len); |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12162 | cap[h->index][len] = 0; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12163 | return ACT_RET_CONT; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12164 | } |
| 12165 | |
Christopher Faulet | 29730ba | 2017-09-18 15:26:32 +0200 | [diff] [blame] | 12166 | /* Check an "http-response capture" action. |
| 12167 | * |
| 12168 | * The function returns 1 in success case, otherwise, it returns 0 and err is |
| 12169 | * filled. |
| 12170 | */ |
| 12171 | int check_http_res_capture(struct act_rule *rule, struct proxy *px, char **err) |
| 12172 | { |
| 12173 | if (rule->arg.capid.idx >= px->nb_rsp_cap) { |
| 12174 | memprintf(err, "unable to find capture id '%d' referenced by http-response capture rule", |
| 12175 | rule->arg.capid.idx); |
| 12176 | return 0; |
| 12177 | } |
| 12178 | |
| 12179 | return 1; |
| 12180 | } |
| 12181 | |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12182 | /* parse an "http-response capture" action. It takes a single argument which is |
| 12183 | * a sample fetch expression. It stores the expression into arg->act.p[0] and |
| 12184 | * 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] | 12185 | * 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] | 12186 | */ |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12187 | enum act_parse_ret parse_http_res_capture(const char **args, int *orig_arg, struct proxy *px, |
| 12188 | struct act_rule *rule, char **err) |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12189 | { |
| 12190 | struct sample_expr *expr; |
| 12191 | int cur_arg; |
| 12192 | int id; |
| 12193 | char *error; |
| 12194 | |
| 12195 | for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++) |
| 12196 | if (strcmp(args[cur_arg], "if") == 0 || |
| 12197 | strcmp(args[cur_arg], "unless") == 0) |
| 12198 | break; |
| 12199 | |
| 12200 | if (cur_arg < *orig_arg + 3) { |
Willy Tarreau | 29bdb1c | 2016-06-24 15:36:34 +0200 | [diff] [blame] | 12201 | memprintf(err, "expects <expression> id <idx>"); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12202 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12203 | } |
| 12204 | |
| 12205 | cur_arg = *orig_arg; |
| 12206 | expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args); |
| 12207 | if (!expr) |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12208 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12209 | |
| 12210 | if (!(expr->fetch->val & SMP_VAL_FE_HRS_HDR)) { |
| 12211 | memprintf(err, |
| 12212 | "fetch method '%s' extracts information from '%s', none of which is available here", |
| 12213 | args[cur_arg-1], sample_src_names(expr->fetch->use)); |
| 12214 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12215 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12216 | } |
| 12217 | |
| 12218 | if (!args[cur_arg] || !*args[cur_arg]) { |
Willy Tarreau | 29bdb1c | 2016-06-24 15:36:34 +0200 | [diff] [blame] | 12219 | memprintf(err, "expects 'id'"); |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12220 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12221 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12222 | } |
| 12223 | |
| 12224 | if (strcmp(args[cur_arg], "id") != 0) { |
| 12225 | memprintf(err, "expects 'id', found '%s'", args[cur_arg]); |
| 12226 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12227 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12228 | } |
| 12229 | |
| 12230 | cur_arg++; |
| 12231 | |
| 12232 | if (!args[cur_arg]) { |
| 12233 | memprintf(err, "missing id value"); |
| 12234 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12235 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12236 | } |
| 12237 | |
| 12238 | id = strtol(args[cur_arg], &error, 10); |
| 12239 | if (*error != '\0') { |
| 12240 | memprintf(err, "cannot parse id '%s'", args[cur_arg]); |
| 12241 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12242 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12243 | } |
| 12244 | cur_arg++; |
| 12245 | |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12246 | proxy->conf.args.ctx = ARGC_CAP; |
| 12247 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 12248 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12249 | rule->action_ptr = http_action_res_capture_by_id; |
Christopher Faulet | 29730ba | 2017-09-18 15:26:32 +0200 | [diff] [blame] | 12250 | rule->check_ptr = check_http_res_capture; |
Thierry FOURNIER | e209797 | 2015-07-31 08:56:35 +0200 | [diff] [blame] | 12251 | rule->arg.capid.expr = expr; |
| 12252 | rule->arg.capid.idx = id; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12253 | |
| 12254 | *orig_arg = cur_arg; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12255 | return ACT_RET_PRS_OK; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12256 | } |
| 12257 | |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 12258 | /* |
| 12259 | * Return the struct http_req_action_kw associated to a keyword. |
| 12260 | */ |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 12261 | struct action_kw *action_http_req_custom(const char *kw) |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 12262 | { |
Thierry FOURNIER | 322a124 | 2015-08-19 09:07:47 +0200 | [diff] [blame] | 12263 | return action_lookup(&http_req_keywords.list, kw); |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 12264 | } |
| 12265 | |
| 12266 | /* |
| 12267 | * Return the struct http_res_action_kw associated to a keyword. |
| 12268 | */ |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 12269 | struct action_kw *action_http_res_custom(const char *kw) |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 12270 | { |
Thierry FOURNIER | 322a124 | 2015-08-19 09:07:47 +0200 | [diff] [blame] | 12271 | return action_lookup(&http_res_keywords.list, kw); |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 12272 | } |
| 12273 | |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12274 | |
| 12275 | /* "show errors" handler for the CLI. Returns 0 if wants to continue, 1 to stop |
| 12276 | * now. |
| 12277 | */ |
| 12278 | static int cli_parse_show_errors(char **args, struct appctx *appctx, void *private) |
| 12279 | { |
| 12280 | if (!cli_has_level(appctx, ACCESS_LVL_OPER)) |
| 12281 | return 1; |
| 12282 | |
Willy Tarreau | 234ba2d | 2016-11-25 08:39:10 +0100 | [diff] [blame] | 12283 | if (*args[2]) { |
| 12284 | struct proxy *px; |
| 12285 | |
| 12286 | px = proxy_find_by_name(args[2], 0, 0); |
| 12287 | if (px) |
| 12288 | appctx->ctx.errors.iid = px->uuid; |
| 12289 | else |
| 12290 | appctx->ctx.errors.iid = atoi(args[2]); |
| 12291 | |
| 12292 | if (!appctx->ctx.errors.iid) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 12293 | appctx->ctx.cli.severity = LOG_ERR; |
Willy Tarreau | 234ba2d | 2016-11-25 08:39:10 +0100 | [diff] [blame] | 12294 | appctx->ctx.cli.msg = "No such proxy.\n"; |
| 12295 | appctx->st0 = CLI_ST_PRINT; |
| 12296 | return 1; |
| 12297 | } |
| 12298 | } |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12299 | else |
Willy Tarreau | 234ba2d | 2016-11-25 08:39:10 +0100 | [diff] [blame] | 12300 | appctx->ctx.errors.iid = -1; // dump all proxies |
| 12301 | |
Willy Tarreau | 35069f8 | 2016-11-25 09:16:37 +0100 | [diff] [blame] | 12302 | appctx->ctx.errors.flag = 0; |
| 12303 | if (strcmp(args[3], "request") == 0) |
| 12304 | appctx->ctx.errors.flag |= 4; // ignore response |
| 12305 | else if (strcmp(args[3], "response") == 0) |
| 12306 | appctx->ctx.errors.flag |= 2; // ignore request |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12307 | appctx->ctx.errors.px = NULL; |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12308 | return 0; |
| 12309 | } |
| 12310 | |
| 12311 | /* This function dumps all captured errors onto the stream interface's |
| 12312 | * read buffer. It returns 0 if the output buffer is full and it needs |
| 12313 | * to be called again, otherwise non-zero. |
| 12314 | */ |
| 12315 | static int cli_io_handler_show_errors(struct appctx *appctx) |
| 12316 | { |
| 12317 | struct stream_interface *si = appctx->owner; |
| 12318 | extern const char *monthname[12]; |
| 12319 | |
| 12320 | if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) |
| 12321 | return 1; |
| 12322 | |
| 12323 | chunk_reset(&trash); |
| 12324 | |
| 12325 | if (!appctx->ctx.errors.px) { |
| 12326 | /* the function had not been called yet, let's prepare the |
| 12327 | * buffer for a response. |
| 12328 | */ |
| 12329 | struct tm tm; |
| 12330 | |
| 12331 | get_localtime(date.tv_sec, &tm); |
| 12332 | chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n", |
| 12333 | tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900, |
| 12334 | tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000), |
| 12335 | error_snapshot_id); |
| 12336 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 12337 | if (ci_putchk(si_ic(si), &trash) == -1) { |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12338 | /* Socket buffer full. Let's try again later from the same point */ |
| 12339 | si_applet_cant_put(si); |
| 12340 | return 0; |
| 12341 | } |
| 12342 | |
| 12343 | appctx->ctx.errors.px = proxy; |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12344 | appctx->ctx.errors.bol = 0; |
| 12345 | appctx->ctx.errors.ptr = -1; |
| 12346 | } |
| 12347 | |
| 12348 | /* we have two inner loops here, one for the proxy, the other one for |
| 12349 | * the buffer. |
| 12350 | */ |
| 12351 | while (appctx->ctx.errors.px) { |
| 12352 | struct error_snapshot *es; |
| 12353 | |
Willy Tarreau | 35069f8 | 2016-11-25 09:16:37 +0100 | [diff] [blame] | 12354 | if ((appctx->ctx.errors.flag & 1) == 0) { |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12355 | es = &appctx->ctx.errors.px->invalid_req; |
Willy Tarreau | 35069f8 | 2016-11-25 09:16:37 +0100 | [diff] [blame] | 12356 | if (appctx->ctx.errors.flag & 2) // skip req |
| 12357 | goto next; |
| 12358 | } |
| 12359 | else { |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12360 | es = &appctx->ctx.errors.px->invalid_rep; |
Willy Tarreau | 35069f8 | 2016-11-25 09:16:37 +0100 | [diff] [blame] | 12361 | if (appctx->ctx.errors.flag & 4) // skip resp |
| 12362 | goto next; |
| 12363 | } |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12364 | |
| 12365 | if (!es->when.tv_sec) |
| 12366 | goto next; |
| 12367 | |
| 12368 | if (appctx->ctx.errors.iid >= 0 && |
| 12369 | appctx->ctx.errors.px->uuid != appctx->ctx.errors.iid && |
| 12370 | es->oe->uuid != appctx->ctx.errors.iid) |
| 12371 | goto next; |
| 12372 | |
| 12373 | if (appctx->ctx.errors.ptr < 0) { |
| 12374 | /* just print headers now */ |
| 12375 | |
| 12376 | char pn[INET6_ADDRSTRLEN]; |
| 12377 | struct tm tm; |
| 12378 | int port; |
| 12379 | |
| 12380 | get_localtime(es->when.tv_sec, &tm); |
| 12381 | chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]", |
| 12382 | tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900, |
| 12383 | tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000)); |
| 12384 | |
| 12385 | switch (addr_to_str(&es->src, pn, sizeof(pn))) { |
| 12386 | case AF_INET: |
| 12387 | case AF_INET6: |
| 12388 | port = get_host_port(&es->src); |
| 12389 | break; |
| 12390 | default: |
| 12391 | port = 0; |
| 12392 | } |
| 12393 | |
Willy Tarreau | 35069f8 | 2016-11-25 09:16:37 +0100 | [diff] [blame] | 12394 | switch (appctx->ctx.errors.flag & 1) { |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12395 | case 0: |
| 12396 | chunk_appendf(&trash, |
| 12397 | " frontend %s (#%d): invalid request\n" |
| 12398 | " backend %s (#%d)", |
| 12399 | appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid, |
| 12400 | (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>", |
| 12401 | (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1); |
| 12402 | break; |
| 12403 | case 1: |
| 12404 | chunk_appendf(&trash, |
| 12405 | " backend %s (#%d): invalid response\n" |
| 12406 | " frontend %s (#%d)", |
| 12407 | appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid, |
| 12408 | es->oe->id, es->oe->uuid); |
| 12409 | break; |
| 12410 | } |
| 12411 | |
| 12412 | chunk_appendf(&trash, |
| 12413 | ", server %s (#%d), event #%u\n" |
| 12414 | " src %s:%d, session #%d, session flags 0x%08x\n" |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 12415 | " 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] | 12416 | " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n" |
| 12417 | " buffer flags 0x%08x, out %d bytes, total %lld bytes\n" |
| 12418 | " pending %d bytes, wrapping at %d, error at position %d:\n \n", |
| 12419 | es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1, |
| 12420 | es->ev_id, |
| 12421 | pn, port, es->sid, es->s_flags, |
Willy Tarreau | 0da5b3b | 2017-09-21 09:30:46 +0200 | [diff] [blame] | 12422 | 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] | 12423 | es->m_clen, es->m_blen, |
| 12424 | es->b_flags, es->b_out, es->b_tot, |
| 12425 | es->len, es->b_wrap, es->pos); |
| 12426 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 12427 | if (ci_putchk(si_ic(si), &trash) == -1) { |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12428 | /* Socket buffer full. Let's try again later from the same point */ |
| 12429 | si_applet_cant_put(si); |
| 12430 | return 0; |
| 12431 | } |
| 12432 | appctx->ctx.errors.ptr = 0; |
| 12433 | appctx->ctx.errors.sid = es->sid; |
| 12434 | } |
| 12435 | |
| 12436 | if (appctx->ctx.errors.sid != es->sid) { |
| 12437 | /* the snapshot changed while we were dumping it */ |
| 12438 | chunk_appendf(&trash, |
| 12439 | " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n"); |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 12440 | if (ci_putchk(si_ic(si), &trash) == -1) { |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12441 | si_applet_cant_put(si); |
| 12442 | return 0; |
| 12443 | } |
| 12444 | goto next; |
| 12445 | } |
| 12446 | |
| 12447 | /* OK, ptr >= 0, so we have to dump the current line */ |
| 12448 | while (es->buf && appctx->ctx.errors.ptr < es->len && appctx->ctx.errors.ptr < global.tune.bufsize) { |
| 12449 | int newptr; |
| 12450 | int newline; |
| 12451 | |
| 12452 | newline = appctx->ctx.errors.bol; |
| 12453 | newptr = dump_text_line(&trash, es->buf, global.tune.bufsize, es->len, &newline, appctx->ctx.errors.ptr); |
| 12454 | if (newptr == appctx->ctx.errors.ptr) |
| 12455 | return 0; |
| 12456 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 12457 | if (ci_putchk(si_ic(si), &trash) == -1) { |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12458 | /* Socket buffer full. Let's try again later from the same point */ |
| 12459 | si_applet_cant_put(si); |
| 12460 | return 0; |
| 12461 | } |
| 12462 | appctx->ctx.errors.ptr = newptr; |
| 12463 | appctx->ctx.errors.bol = newline; |
| 12464 | }; |
| 12465 | next: |
| 12466 | appctx->ctx.errors.bol = 0; |
| 12467 | appctx->ctx.errors.ptr = -1; |
Willy Tarreau | 35069f8 | 2016-11-25 09:16:37 +0100 | [diff] [blame] | 12468 | appctx->ctx.errors.flag ^= 1; |
| 12469 | if (!(appctx->ctx.errors.flag & 1)) |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12470 | appctx->ctx.errors.px = appctx->ctx.errors.px->next; |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12471 | } |
| 12472 | |
| 12473 | /* dump complete */ |
| 12474 | return 1; |
| 12475 | } |
| 12476 | |
| 12477 | /* register cli keywords */ |
| 12478 | static struct cli_kw_list cli_kws = {{ },{ |
| 12479 | { { "show", "errors", NULL }, |
| 12480 | "show errors : report last request and response errors for each proxy", |
| 12481 | cli_parse_show_errors, cli_io_handler_show_errors, NULL, |
| 12482 | }, |
| 12483 | {{},} |
| 12484 | }}; |
| 12485 | |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 12486 | /************************************************************************/ |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12487 | /* All supported ACL keywords must be declared here. */ |
| 12488 | /************************************************************************/ |
| 12489 | |
| 12490 | /* Note: must not be declared <const> as its list will be overwritten. |
| 12491 | * Please take care of keeping this list alphabetically sorted. |
| 12492 | */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 12493 | static struct acl_kw_list acl_kws = {ILH, { |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12494 | { "base", "base", PAT_MATCH_STR }, |
| 12495 | { "base_beg", "base", PAT_MATCH_BEG }, |
| 12496 | { "base_dir", "base", PAT_MATCH_DIR }, |
| 12497 | { "base_dom", "base", PAT_MATCH_DOM }, |
| 12498 | { "base_end", "base", PAT_MATCH_END }, |
| 12499 | { "base_len", "base", PAT_MATCH_LEN }, |
| 12500 | { "base_reg", "base", PAT_MATCH_REG }, |
| 12501 | { "base_sub", "base", PAT_MATCH_SUB }, |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 12502 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12503 | { "cook", "req.cook", PAT_MATCH_STR }, |
| 12504 | { "cook_beg", "req.cook", PAT_MATCH_BEG }, |
| 12505 | { "cook_dir", "req.cook", PAT_MATCH_DIR }, |
| 12506 | { "cook_dom", "req.cook", PAT_MATCH_DOM }, |
| 12507 | { "cook_end", "req.cook", PAT_MATCH_END }, |
| 12508 | { "cook_len", "req.cook", PAT_MATCH_LEN }, |
| 12509 | { "cook_reg", "req.cook", PAT_MATCH_REG }, |
| 12510 | { "cook_sub", "req.cook", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12511 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12512 | { "hdr", "req.hdr", PAT_MATCH_STR }, |
| 12513 | { "hdr_beg", "req.hdr", PAT_MATCH_BEG }, |
| 12514 | { "hdr_dir", "req.hdr", PAT_MATCH_DIR }, |
| 12515 | { "hdr_dom", "req.hdr", PAT_MATCH_DOM }, |
| 12516 | { "hdr_end", "req.hdr", PAT_MATCH_END }, |
| 12517 | { "hdr_len", "req.hdr", PAT_MATCH_LEN }, |
| 12518 | { "hdr_reg", "req.hdr", PAT_MATCH_REG }, |
| 12519 | { "hdr_sub", "req.hdr", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12520 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12521 | /* these two declarations uses strings with list storage (in place |
| 12522 | * of tree storage). The basic match is PAT_MATCH_STR, but the indexation |
| 12523 | * and delete functions are relative to the list management. The parse |
| 12524 | * and match method are related to the corresponding fetch methods. This |
| 12525 | * is very particular ACL declaration mode. |
| 12526 | */ |
| 12527 | { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth }, |
| 12528 | { "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] | 12529 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12530 | { "path", "path", PAT_MATCH_STR }, |
| 12531 | { "path_beg", "path", PAT_MATCH_BEG }, |
| 12532 | { "path_dir", "path", PAT_MATCH_DIR }, |
| 12533 | { "path_dom", "path", PAT_MATCH_DOM }, |
| 12534 | { "path_end", "path", PAT_MATCH_END }, |
| 12535 | { "path_len", "path", PAT_MATCH_LEN }, |
| 12536 | { "path_reg", "path", PAT_MATCH_REG }, |
| 12537 | { "path_sub", "path", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12538 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12539 | { "req_ver", "req.ver", PAT_MATCH_STR }, |
| 12540 | { "resp_ver", "res.ver", PAT_MATCH_STR }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12541 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12542 | { "scook", "res.cook", PAT_MATCH_STR }, |
| 12543 | { "scook_beg", "res.cook", PAT_MATCH_BEG }, |
| 12544 | { "scook_dir", "res.cook", PAT_MATCH_DIR }, |
| 12545 | { "scook_dom", "res.cook", PAT_MATCH_DOM }, |
| 12546 | { "scook_end", "res.cook", PAT_MATCH_END }, |
| 12547 | { "scook_len", "res.cook", PAT_MATCH_LEN }, |
| 12548 | { "scook_reg", "res.cook", PAT_MATCH_REG }, |
| 12549 | { "scook_sub", "res.cook", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12550 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12551 | { "shdr", "res.hdr", PAT_MATCH_STR }, |
| 12552 | { "shdr_beg", "res.hdr", PAT_MATCH_BEG }, |
| 12553 | { "shdr_dir", "res.hdr", PAT_MATCH_DIR }, |
| 12554 | { "shdr_dom", "res.hdr", PAT_MATCH_DOM }, |
| 12555 | { "shdr_end", "res.hdr", PAT_MATCH_END }, |
| 12556 | { "shdr_len", "res.hdr", PAT_MATCH_LEN }, |
| 12557 | { "shdr_reg", "res.hdr", PAT_MATCH_REG }, |
| 12558 | { "shdr_sub", "res.hdr", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12559 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12560 | { "url", "url", PAT_MATCH_STR }, |
| 12561 | { "url_beg", "url", PAT_MATCH_BEG }, |
| 12562 | { "url_dir", "url", PAT_MATCH_DIR }, |
| 12563 | { "url_dom", "url", PAT_MATCH_DOM }, |
| 12564 | { "url_end", "url", PAT_MATCH_END }, |
| 12565 | { "url_len", "url", PAT_MATCH_LEN }, |
| 12566 | { "url_reg", "url", PAT_MATCH_REG }, |
| 12567 | { "url_sub", "url", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12568 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12569 | { "urlp", "urlp", PAT_MATCH_STR }, |
| 12570 | { "urlp_beg", "urlp", PAT_MATCH_BEG }, |
| 12571 | { "urlp_dir", "urlp", PAT_MATCH_DIR }, |
| 12572 | { "urlp_dom", "urlp", PAT_MATCH_DOM }, |
| 12573 | { "urlp_end", "urlp", PAT_MATCH_END }, |
| 12574 | { "urlp_len", "urlp", PAT_MATCH_LEN }, |
| 12575 | { "urlp_reg", "urlp", PAT_MATCH_REG }, |
| 12576 | { "urlp_sub", "urlp", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12577 | |
Willy Tarreau | 8ed669b | 2013-01-11 15:49:37 +0100 | [diff] [blame] | 12578 | { /* END */ }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12579 | }}; |
| 12580 | |
| 12581 | /************************************************************************/ |
| 12582 | /* All supported pattern keywords must be declared here. */ |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 12583 | /************************************************************************/ |
| 12584 | /* Note: must not be declared <const> as its list will be overwritten */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 12585 | static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, { |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12586 | { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 12587 | { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 12588 | { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV }, |
| 12589 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 12590 | /* capture are allocated and are permanent in the stream */ |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 12591 | { "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] | 12592 | |
| 12593 | /* retrieve these captures from the HTTP logs */ |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 12594 | { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP }, |
| 12595 | { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP }, |
| 12596 | { "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] | 12597 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 12598 | { "capture.res.hdr", smp_fetch_capture_header_res, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP }, |
| 12599 | { "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] | 12600 | |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 12601 | /* cookie is valid in both directions (eg: for "stick ...") but cook* |
| 12602 | * are only here to match the ACL's name, are request-only and are used |
| 12603 | * for ACL compatibility only. |
| 12604 | */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12605 | { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 12606 | { "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] | 12607 | { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 12608 | { "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] | 12609 | |
| 12610 | /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are |
| 12611 | * only here to match the ACL's name, are request-only and are used for |
| 12612 | * ACL compatibility only. |
| 12613 | */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12614 | { "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] | 12615 | { "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] | 12616 | { "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] | 12617 | { "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] | 12618 | |
Willy Tarreau | 0a0daec | 2013-04-02 22:44:58 +0200 | [diff] [blame] | 12619 | { "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] | 12620 | { "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] | 12621 | { "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] | 12622 | { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP }, |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12623 | { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 12624 | { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 12625 | |
| 12626 | /* HTTP protocol on the request path */ |
| 12627 | { "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] | 12628 | { "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] | 12629 | |
| 12630 | /* HTTP version on the request path */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12631 | { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 12632 | { "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] | 12633 | |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 12634 | { "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] | 12635 | { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 12636 | { "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] | 12637 | { "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] | 12638 | |
Thierry FOURNIER | d7d8881 | 2017-04-19 15:15:14 +0200 | [diff] [blame] | 12639 | { "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] | 12640 | { "req.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV }, |
| 12641 | |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 12642 | /* HTTP version on the response path */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12643 | { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV }, |
| 12644 | { "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] | 12645 | |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 12646 | /* 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] | 12647 | { "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] | 12648 | { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 12649 | { "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] | 12650 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12651 | { "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] | 12652 | { "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] | 12653 | { "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] | 12654 | { "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] | 12655 | { "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] | 12656 | { "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] | 12657 | { "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] | 12658 | |
| 12659 | /* 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] | 12660 | { "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] | 12661 | { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV }, |
| 12662 | { "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] | 12663 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12664 | { "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] | 12665 | { "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] | 12666 | { "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] | 12667 | { "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] | 12668 | { "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] | 12669 | { "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] | 12670 | { "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] | 12671 | |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 12672 | /* 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] | 12673 | { "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] | 12674 | { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV }, |
| 12675 | { "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] | 12676 | { "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] | 12677 | |
| 12678 | /* 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] | 12679 | { "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] | 12680 | { "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] | 12681 | { "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] | 12682 | { "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] | 12683 | |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 12684 | { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP }, |
Thierry Fournier | 0e00dca | 2016-04-07 15:47:40 +0200 | [diff] [blame] | 12685 | { "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] | 12686 | { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 12687 | { "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] | 12688 | { "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] | 12689 | { "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] | 12690 | { "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] | 12691 | { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 12692 | { "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] | 12693 | { "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] | 12694 | { /* END */ }, |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 12695 | }}; |
| 12696 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 12697 | |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12698 | /************************************************************************/ |
| 12699 | /* All supported converter keywords must be declared here. */ |
| 12700 | /************************************************************************/ |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 12701 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 12702 | static struct sample_conv_kw_list sample_conv_kws = {ILH, { |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 12703 | { "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] | 12704 | { "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] | 12705 | { "capture-req", smp_conv_req_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR}, |
| 12706 | { "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] | 12707 | { "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] | 12708 | { NULL, NULL, 0, 0, 0 }, |
| 12709 | }}; |
| 12710 | |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 12711 | |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12712 | /************************************************************************/ |
| 12713 | /* All supported http-request action keywords must be declared here. */ |
| 12714 | /************************************************************************/ |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 12715 | struct action_kw_list http_req_actions = { |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12716 | .kw = { |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12717 | { "capture", parse_http_req_capture }, |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12718 | { "set-method", parse_set_req_line }, |
| 12719 | { "set-path", parse_set_req_line }, |
| 12720 | { "set-query", parse_set_req_line }, |
| 12721 | { "set-uri", parse_set_req_line }, |
Willy Tarreau | cb703b0 | 2015-04-03 09:52:01 +0200 | [diff] [blame] | 12722 | { NULL, NULL } |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12723 | } |
| 12724 | }; |
| 12725 | |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 12726 | struct action_kw_list http_res_actions = { |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12727 | .kw = { |
| 12728 | { "capture", parse_http_res_capture }, |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 12729 | { "set-status", parse_http_set_status }, |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12730 | { NULL, NULL } |
| 12731 | } |
| 12732 | }; |
| 12733 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 12734 | __attribute__((constructor)) |
| 12735 | static void __http_protocol_init(void) |
| 12736 | { |
| 12737 | acl_register_keywords(&acl_kws); |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 12738 | sample_register_fetches(&sample_fetch_keywords); |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 12739 | sample_register_convs(&sample_conv_kws); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12740 | http_req_keywords_register(&http_req_actions); |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12741 | http_res_keywords_register(&http_res_actions); |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12742 | cli_register_kw(&cli_kws); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 12743 | } |
| 12744 | |
| 12745 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 12746 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 12747 | * Local variables: |
| 12748 | * c-indent-level: 8 |
| 12749 | * c-basic-offset: 8 |
| 12750 | * End: |
| 12751 | */ |