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, |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 143 | [HTTP_ERR_429] = 429, |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 144 | [HTTP_ERR_500] = 500, |
| 145 | [HTTP_ERR_502] = 502, |
| 146 | [HTTP_ERR_503] = 503, |
| 147 | [HTTP_ERR_504] = 504, |
| 148 | }; |
| 149 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 150 | static const char *http_err_msgs[HTTP_ERR_SIZE] = { |
Willy Tarreau | ae94d4d | 2011-05-11 16:28:49 +0200 | [diff] [blame] | 151 | [HTTP_ERR_200] = |
| 152 | "HTTP/1.0 200 OK\r\n" |
| 153 | "Cache-Control: no-cache\r\n" |
| 154 | "Connection: close\r\n" |
| 155 | "Content-Type: text/html\r\n" |
| 156 | "\r\n" |
| 157 | "<html><body><h1>200 OK</h1>\nService ready.\n</body></html>\n", |
| 158 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 159 | [HTTP_ERR_400] = |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 160 | "HTTP/1.0 400 Bad request\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 161 | "Cache-Control: no-cache\r\n" |
| 162 | "Connection: close\r\n" |
| 163 | "Content-Type: text/html\r\n" |
| 164 | "\r\n" |
| 165 | "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n", |
| 166 | |
| 167 | [HTTP_ERR_403] = |
| 168 | "HTTP/1.0 403 Forbidden\r\n" |
| 169 | "Cache-Control: no-cache\r\n" |
| 170 | "Connection: close\r\n" |
| 171 | "Content-Type: text/html\r\n" |
| 172 | "\r\n" |
| 173 | "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n", |
| 174 | |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 175 | [HTTP_ERR_405] = |
| 176 | "HTTP/1.0 405 Method Not Allowed\r\n" |
| 177 | "Cache-Control: no-cache\r\n" |
| 178 | "Connection: close\r\n" |
| 179 | "Content-Type: text/html\r\n" |
| 180 | "\r\n" |
| 181 | "<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", |
| 182 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 183 | [HTTP_ERR_408] = |
| 184 | "HTTP/1.0 408 Request Time-out\r\n" |
| 185 | "Cache-Control: no-cache\r\n" |
| 186 | "Connection: close\r\n" |
| 187 | "Content-Type: text/html\r\n" |
| 188 | "\r\n" |
| 189 | "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n", |
| 190 | |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 191 | [HTTP_ERR_429] = |
| 192 | "HTTP/1.0 429 Too Many Requests\r\n" |
| 193 | "Cache-Control: no-cache\r\n" |
| 194 | "Connection: close\r\n" |
| 195 | "Content-Type: text/html\r\n" |
| 196 | "\r\n" |
| 197 | "<html><body><h1>429 Too Many Requests</h1>\nYou have sent too many requests in a given amount of time.\n</body></html>\n", |
| 198 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 199 | [HTTP_ERR_500] = |
Jarno Huuskonen | 16ad94a | 2017-01-09 14:17:10 +0200 | [diff] [blame] | 200 | "HTTP/1.0 500 Internal Server Error\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 201 | "Cache-Control: no-cache\r\n" |
| 202 | "Connection: close\r\n" |
| 203 | "Content-Type: text/html\r\n" |
| 204 | "\r\n" |
Jarno Huuskonen | 16ad94a | 2017-01-09 14:17:10 +0200 | [diff] [blame] | 205 | "<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] | 206 | |
| 207 | [HTTP_ERR_502] = |
| 208 | "HTTP/1.0 502 Bad Gateway\r\n" |
| 209 | "Cache-Control: no-cache\r\n" |
| 210 | "Connection: close\r\n" |
| 211 | "Content-Type: text/html\r\n" |
| 212 | "\r\n" |
| 213 | "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n", |
| 214 | |
| 215 | [HTTP_ERR_503] = |
| 216 | "HTTP/1.0 503 Service Unavailable\r\n" |
| 217 | "Cache-Control: no-cache\r\n" |
| 218 | "Connection: close\r\n" |
| 219 | "Content-Type: text/html\r\n" |
| 220 | "\r\n" |
| 221 | "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n", |
| 222 | |
| 223 | [HTTP_ERR_504] = |
| 224 | "HTTP/1.0 504 Gateway Time-out\r\n" |
| 225 | "Cache-Control: no-cache\r\n" |
| 226 | "Connection: close\r\n" |
| 227 | "Content-Type: text/html\r\n" |
| 228 | "\r\n" |
| 229 | "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n", |
| 230 | |
| 231 | }; |
| 232 | |
Cyril Bonté | 19979e1 | 2012-04-04 12:57:21 +0200 | [diff] [blame] | 233 | /* status codes available for the stats admin page (strictly 4 chars length) */ |
| 234 | const char *stat_status_codes[STAT_STATUS_SIZE] = { |
| 235 | [STAT_STATUS_DENY] = "DENY", |
| 236 | [STAT_STATUS_DONE] = "DONE", |
| 237 | [STAT_STATUS_ERRP] = "ERRP", |
| 238 | [STAT_STATUS_EXCD] = "EXCD", |
| 239 | [STAT_STATUS_NONE] = "NONE", |
| 240 | [STAT_STATUS_PART] = "PART", |
| 241 | [STAT_STATUS_UNKN] = "UNKN", |
| 242 | }; |
| 243 | |
| 244 | |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 245 | /* List head of all known action keywords for "http-request" */ |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 246 | struct action_kw_list http_req_keywords = { |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 247 | .list = LIST_HEAD_INIT(http_req_keywords.list) |
| 248 | }; |
| 249 | |
| 250 | /* List head of all known action keywords for "http-response" */ |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 251 | struct action_kw_list http_res_keywords = { |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 252 | .list = LIST_HEAD_INIT(http_res_keywords.list) |
| 253 | }; |
| 254 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 255 | /* We must put the messages here since GCC cannot initialize consts depending |
| 256 | * on strlen(). |
| 257 | */ |
| 258 | struct chunk http_err_chunks[HTTP_ERR_SIZE]; |
| 259 | |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 260 | /* this struct is used between calls to smp_fetch_hdr() or smp_fetch_cookie() */ |
| 261 | static struct hdr_ctx static_hdr_ctx; |
| 262 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 263 | #define FD_SETS_ARE_BITFIELDS |
| 264 | #ifdef FD_SETS_ARE_BITFIELDS |
| 265 | /* |
| 266 | * This map is used with all the FD_* macros to check whether a particular bit |
| 267 | * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes |
| 268 | * which should be encoded. When FD_ISSET() returns non-zero, it means that the |
| 269 | * byte should be encoded. Be careful to always pass bytes from 0 to 255 |
| 270 | * exclusively to the macros. |
| 271 | */ |
| 272 | fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| 273 | 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] | 274 | 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] | 275 | |
| 276 | #else |
| 277 | #error "Check if your OS uses bitfields for fd_sets" |
| 278 | #endif |
| 279 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 280 | 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] | 281 | |
David Carlier | 7365f7d | 2016-04-04 11:54:42 +0100 | [diff] [blame] | 282 | static inline int http_msg_forward_body(struct stream *s, struct http_msg *msg); |
| 283 | 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] | 284 | |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 285 | /* This function returns a reason associated with the HTTP status. |
| 286 | * This function never fails, a message is always returned. |
| 287 | */ |
| 288 | const char *get_reason(unsigned int status) |
| 289 | { |
| 290 | switch (status) { |
| 291 | case 100: return "Continue"; |
| 292 | case 101: return "Switching Protocols"; |
| 293 | case 102: return "Processing"; |
| 294 | case 200: return "OK"; |
| 295 | case 201: return "Created"; |
| 296 | case 202: return "Accepted"; |
| 297 | case 203: return "Non-Authoritative Information"; |
| 298 | case 204: return "No Content"; |
| 299 | case 205: return "Reset Content"; |
| 300 | case 206: return "Partial Content"; |
| 301 | case 207: return "Multi-Status"; |
| 302 | case 210: return "Content Different"; |
| 303 | case 226: return "IM Used"; |
| 304 | case 300: return "Multiple Choices"; |
| 305 | case 301: return "Moved Permanently"; |
| 306 | case 302: return "Moved Temporarily"; |
| 307 | case 303: return "See Other"; |
| 308 | case 304: return "Not Modified"; |
| 309 | case 305: return "Use Proxy"; |
| 310 | case 307: return "Temporary Redirect"; |
| 311 | case 308: return "Permanent Redirect"; |
| 312 | case 310: return "Too many Redirects"; |
| 313 | case 400: return "Bad Request"; |
| 314 | case 401: return "Unauthorized"; |
| 315 | case 402: return "Payment Required"; |
| 316 | case 403: return "Forbidden"; |
| 317 | case 404: return "Not Found"; |
| 318 | case 405: return "Method Not Allowed"; |
| 319 | case 406: return "Not Acceptable"; |
| 320 | case 407: return "Proxy Authentication Required"; |
| 321 | case 408: return "Request Time-out"; |
| 322 | case 409: return "Conflict"; |
| 323 | case 410: return "Gone"; |
| 324 | case 411: return "Length Required"; |
| 325 | case 412: return "Precondition Failed"; |
| 326 | case 413: return "Request Entity Too Large"; |
| 327 | case 414: return "Request-URI Too Long"; |
| 328 | case 415: return "Unsupported Media Type"; |
| 329 | case 416: return "Requested range unsatisfiable"; |
| 330 | case 417: return "Expectation failed"; |
| 331 | case 418: return "I'm a teapot"; |
| 332 | case 422: return "Unprocessable entity"; |
| 333 | case 423: return "Locked"; |
| 334 | case 424: return "Method failure"; |
| 335 | case 425: return "Unordered Collection"; |
| 336 | case 426: return "Upgrade Required"; |
| 337 | case 428: return "Precondition Required"; |
| 338 | case 429: return "Too Many Requests"; |
| 339 | case 431: return "Request Header Fields Too Large"; |
| 340 | case 449: return "Retry With"; |
| 341 | case 450: return "Blocked by Windows Parental Controls"; |
| 342 | case 451: return "Unavailable For Legal Reasons"; |
| 343 | case 456: return "Unrecoverable Error"; |
| 344 | case 499: return "client has closed connection"; |
| 345 | case 500: return "Internal Server Error"; |
| 346 | case 501: return "Not Implemented"; |
Jarno Huuskonen | 59af2df | 2016-12-28 10:49:01 +0200 | [diff] [blame] | 347 | case 502: return "Bad Gateway or Proxy Error"; |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 348 | case 503: return "Service Unavailable"; |
| 349 | case 504: return "Gateway Time-out"; |
| 350 | case 505: return "HTTP Version not supported"; |
| 351 | case 506: return "Variant also negociate"; |
| 352 | case 507: return "Insufficient storage"; |
| 353 | case 508: return "Loop detected"; |
| 354 | case 509: return "Bandwidth Limit Exceeded"; |
| 355 | case 510: return "Not extended"; |
| 356 | case 511: return "Network authentication required"; |
| 357 | case 520: return "Web server is returning an unknown error"; |
| 358 | default: |
| 359 | switch (status) { |
| 360 | case 100 ... 199: return "Informational"; |
| 361 | case 200 ... 299: return "Success"; |
| 362 | case 300 ... 399: return "Redirection"; |
| 363 | case 400 ... 499: return "Client Error"; |
| 364 | case 500 ... 599: return "Server Error"; |
| 365 | default: return "Other"; |
| 366 | } |
| 367 | } |
| 368 | } |
| 369 | |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 370 | /* This function returns HTTP_ERR_<num> (enum) matching http status code. |
| 371 | * Returned value should match codes from http_err_codes. |
| 372 | */ |
| 373 | static const int http_get_status_idx(unsigned int status) |
| 374 | { |
| 375 | switch (status) { |
| 376 | case 200: return HTTP_ERR_200; |
| 377 | case 400: return HTTP_ERR_400; |
| 378 | case 403: return HTTP_ERR_403; |
| 379 | case 405: return HTTP_ERR_405; |
| 380 | case 408: return HTTP_ERR_408; |
| 381 | case 429: return HTTP_ERR_429; |
| 382 | case 500: return HTTP_ERR_500; |
| 383 | case 502: return HTTP_ERR_502; |
| 384 | case 503: return HTTP_ERR_503; |
| 385 | case 504: return HTTP_ERR_504; |
| 386 | default: return HTTP_ERR_500; |
| 387 | } |
| 388 | } |
| 389 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 390 | void init_proto_http() |
| 391 | { |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 392 | int i; |
| 393 | char *tmp; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 394 | int msg; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 395 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 396 | for (msg = 0; msg < HTTP_ERR_SIZE; msg++) { |
| 397 | if (!http_err_msgs[msg]) { |
| 398 | Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg); |
| 399 | abort(); |
| 400 | } |
| 401 | |
| 402 | http_err_chunks[msg].str = (char *)http_err_msgs[msg]; |
| 403 | http_err_chunks[msg].len = strlen(http_err_msgs[msg]); |
| 404 | } |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 405 | |
| 406 | /* initialize the log header encoding map : '{|}"#' should be encoded with |
| 407 | * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ). |
| 408 | * URL encoding only requires '"', '#' to be encoded as well as non- |
| 409 | * printable characters above. |
| 410 | */ |
| 411 | memset(hdr_encode_map, 0, sizeof(hdr_encode_map)); |
| 412 | memset(url_encode_map, 0, sizeof(url_encode_map)); |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 413 | memset(http_encode_map, 0, sizeof(url_encode_map)); |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 414 | for (i = 0; i < 32; i++) { |
| 415 | FD_SET(i, hdr_encode_map); |
| 416 | FD_SET(i, url_encode_map); |
| 417 | } |
| 418 | for (i = 127; i < 256; i++) { |
| 419 | FD_SET(i, hdr_encode_map); |
| 420 | FD_SET(i, url_encode_map); |
| 421 | } |
| 422 | |
| 423 | tmp = "\"#{|}"; |
| 424 | while (*tmp) { |
| 425 | FD_SET(*tmp, hdr_encode_map); |
| 426 | tmp++; |
| 427 | } |
| 428 | |
| 429 | tmp = "\"#"; |
| 430 | while (*tmp) { |
| 431 | FD_SET(*tmp, url_encode_map); |
| 432 | tmp++; |
| 433 | } |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 434 | |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 435 | /* initialize the http header encoding map. The draft httpbis define the |
| 436 | * header content as: |
| 437 | * |
| 438 | * HTTP-message = start-line |
| 439 | * *( header-field CRLF ) |
| 440 | * CRLF |
| 441 | * [ message-body ] |
| 442 | * header-field = field-name ":" OWS field-value OWS |
| 443 | * field-value = *( field-content / obs-fold ) |
| 444 | * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ] |
| 445 | * obs-fold = CRLF 1*( SP / HTAB ) |
| 446 | * field-vchar = VCHAR / obs-text |
| 447 | * VCHAR = %x21-7E |
| 448 | * obs-text = %x80-FF |
| 449 | * |
| 450 | * All the chars are encoded except "VCHAR", "obs-text", SP and HTAB. |
| 451 | * The encoded chars are form 0x00 to 0x08, 0x0a to 0x1f and 0x7f. The |
| 452 | * "obs-fold" is volontary forgotten because haproxy remove this. |
| 453 | */ |
| 454 | memset(http_encode_map, 0, sizeof(http_encode_map)); |
| 455 | for (i = 0x00; i <= 0x08; i++) |
| 456 | FD_SET(i, http_encode_map); |
| 457 | for (i = 0x0a; i <= 0x1f; i++) |
| 458 | FD_SET(i, http_encode_map); |
| 459 | FD_SET(0x7f, http_encode_map); |
| 460 | |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 461 | /* memory allocations */ |
Willy Tarreau | 63986c7 | 2015-04-03 22:55:33 +0200 | [diff] [blame] | 462 | 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] | 463 | pool2_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED); |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 464 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 465 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 466 | /* |
| 467 | * We have 26 list of methods (1 per first letter), each of which can have |
| 468 | * up to 3 entries (2 valid, 1 null). |
| 469 | */ |
| 470 | struct http_method_desc { |
Willy Tarreau | c8987b3 | 2013-12-06 23:43:17 +0100 | [diff] [blame] | 471 | enum http_meth_t meth; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 472 | int len; |
| 473 | const char text[8]; |
| 474 | }; |
| 475 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 476 | const struct http_method_desc http_methods[26][3] = { |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 477 | ['C' - 'A'] = { |
| 478 | [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" }, |
| 479 | }, |
| 480 | ['D' - 'A'] = { |
| 481 | [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" }, |
| 482 | }, |
| 483 | ['G' - 'A'] = { |
| 484 | [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" }, |
| 485 | }, |
| 486 | ['H' - 'A'] = { |
| 487 | [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" }, |
| 488 | }, |
Christopher Faulet | d57ad64 | 2015-07-31 14:26:57 +0200 | [diff] [blame] | 489 | ['O' - 'A'] = { |
| 490 | [0] = { .meth = HTTP_METH_OPTIONS , .len=7, .text="OPTIONS" }, |
| 491 | }, |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 492 | ['P' - 'A'] = { |
| 493 | [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" }, |
| 494 | [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" }, |
| 495 | }, |
| 496 | ['T' - 'A'] = { |
| 497 | [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" }, |
| 498 | }, |
| 499 | /* rest is empty like this : |
Willy Tarreau | b7ce424 | 2015-09-03 17:15:21 +0200 | [diff] [blame] | 500 | * [0] = { .meth = HTTP_METH_OTHER , .len=0, .text="" }, |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 501 | */ |
| 502 | }; |
| 503 | |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 504 | const struct http_method_name http_known_methods[HTTP_METH_OTHER] = { |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 505 | [HTTP_METH_OPTIONS] = { "OPTIONS", 7 }, |
| 506 | [HTTP_METH_GET] = { "GET", 3 }, |
| 507 | [HTTP_METH_HEAD] = { "HEAD", 4 }, |
| 508 | [HTTP_METH_POST] = { "POST", 4 }, |
| 509 | [HTTP_METH_PUT] = { "PUT", 3 }, |
| 510 | [HTTP_METH_DELETE] = { "DELETE", 6 }, |
| 511 | [HTTP_METH_TRACE] = { "TRACE", 5 }, |
| 512 | [HTTP_METH_CONNECT] = { "CONNECT", 7 }, |
| 513 | }; |
| 514 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 515 | /* |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 516 | * Adds a header and its CRLF at the tail of the message's buffer, just before |
| 517 | * 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] | 518 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 519 | * of headers is automatically adjusted. The number of bytes added is returned |
| 520 | * on success, otherwise <0 is returned indicating an error. |
| 521 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 522 | 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] | 523 | { |
| 524 | int bytes, len; |
| 525 | |
| 526 | len = strlen(text); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 527 | 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] | 528 | if (!bytes) |
| 529 | return -1; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 530 | http_msg_move_end(msg, bytes); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 531 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 532 | } |
| 533 | |
| 534 | /* |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 535 | * Adds a header and its CRLF at the tail of the message's buffer, just before |
| 536 | * 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] | 537 | * the buffer is only opened and the space reserved, but nothing is copied. |
| 538 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 539 | * of headers is automatically adjusted. The number of bytes added is returned |
| 540 | * on success, otherwise <0 is returned indicating an error. |
| 541 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 542 | int http_header_add_tail2(struct http_msg *msg, |
| 543 | struct hdr_idx *hdr_idx, const char *text, int len) |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 544 | { |
| 545 | int bytes; |
| 546 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 547 | 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] | 548 | if (!bytes) |
| 549 | return -1; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 550 | http_msg_move_end(msg, bytes); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 551 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 552 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 553 | |
| 554 | /* |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 555 | * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon. |
| 556 | * If so, returns the position of the first non-space character relative to |
| 557 | * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries |
| 558 | * to return a pointer to the place after the first space. Returns 0 if the |
| 559 | * header name does not match. Checks are case-insensitive. |
| 560 | */ |
| 561 | int http_header_match2(const char *hdr, const char *end, |
| 562 | const char *name, int len) |
| 563 | { |
| 564 | const char *val; |
| 565 | |
| 566 | if (hdr + len >= end) |
| 567 | return 0; |
| 568 | if (hdr[len] != ':') |
| 569 | return 0; |
| 570 | if (strncasecmp(hdr, name, len) != 0) |
| 571 | return 0; |
| 572 | val = hdr + len + 1; |
| 573 | while (val < end && HTTP_IS_SPHT(*val)) |
| 574 | val++; |
| 575 | if ((val >= end) && (len + 2 <= end - hdr)) |
| 576 | return len + 2; /* we may replace starting from second space */ |
| 577 | return val - hdr; |
| 578 | } |
| 579 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 580 | /* Find the first or next occurrence of header <name> in message buffer <sol> |
| 581 | * using headers index <idx>, and return it in the <ctx> structure. This |
| 582 | * structure holds everything necessary to use the header and find next |
| 583 | * occurrence. If its <idx> member is 0, the header is searched from the |
| 584 | * beginning. Otherwise, the next occurrence is returned. The function returns |
| 585 | * 1 when it finds a value, and 0 when there is no more. It is very similar to |
| 586 | * http_find_header2() except that it is designed to work with full-line headers |
| 587 | * whose comma is not a delimiter but is part of the syntax. As a special case, |
| 588 | * if ctx->val is NULL when searching for a new values of a header, the current |
| 589 | * header is rescanned. This allows rescanning after a header deletion. |
| 590 | */ |
| 591 | int http_find_full_header2(const char *name, int len, |
| 592 | char *sol, struct hdr_idx *idx, |
| 593 | struct hdr_ctx *ctx) |
| 594 | { |
| 595 | char *eol, *sov; |
| 596 | int cur_idx, old_idx; |
| 597 | |
| 598 | cur_idx = ctx->idx; |
| 599 | if (cur_idx) { |
| 600 | /* We have previously returned a header, let's search another one */ |
| 601 | sol = ctx->line; |
| 602 | eol = sol + idx->v[cur_idx].len; |
| 603 | goto next_hdr; |
| 604 | } |
| 605 | |
| 606 | /* first request for this header */ |
| 607 | sol += hdr_idx_first_pos(idx); |
| 608 | old_idx = 0; |
| 609 | cur_idx = hdr_idx_first_idx(idx); |
| 610 | while (cur_idx) { |
| 611 | eol = sol + idx->v[cur_idx].len; |
| 612 | |
| 613 | if (len == 0) { |
| 614 | /* No argument was passed, we want any header. |
| 615 | * To achieve this, we simply build a fake request. */ |
| 616 | while (sol + len < eol && sol[len] != ':') |
| 617 | len++; |
| 618 | name = sol; |
| 619 | } |
| 620 | |
| 621 | if ((len < eol - sol) && |
| 622 | (sol[len] == ':') && |
| 623 | (strncasecmp(sol, name, len) == 0)) { |
| 624 | ctx->del = len; |
| 625 | sov = sol + len + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 626 | while (sov < eol && HTTP_IS_LWS(*sov)) |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 627 | sov++; |
| 628 | |
| 629 | ctx->line = sol; |
| 630 | ctx->prev = old_idx; |
| 631 | ctx->idx = cur_idx; |
| 632 | ctx->val = sov - sol; |
| 633 | ctx->tws = 0; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 634 | while (eol > sov && HTTP_IS_LWS(*(eol - 1))) { |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 635 | eol--; |
| 636 | ctx->tws++; |
| 637 | } |
| 638 | ctx->vlen = eol - sov; |
| 639 | return 1; |
| 640 | } |
| 641 | next_hdr: |
| 642 | sol = eol + idx->v[cur_idx].cr + 1; |
| 643 | old_idx = cur_idx; |
| 644 | cur_idx = idx->v[cur_idx].next; |
| 645 | } |
| 646 | return 0; |
| 647 | } |
| 648 | |
Willy Tarreau | c90dc23 | 2015-02-20 13:51:36 +0100 | [diff] [blame] | 649 | /* Find the first or next header field in message buffer <sol> using headers |
| 650 | * index <idx>, and return it in the <ctx> structure. This structure holds |
| 651 | * everything necessary to use the header and find next occurrence. If its |
| 652 | * <idx> member is 0, the first header is retrieved. Otherwise, the next |
| 653 | * occurrence is returned. The function returns 1 when it finds a value, and |
| 654 | * 0 when there is no more. It is equivalent to http_find_full_header2() with |
| 655 | * no header name. |
| 656 | */ |
| 657 | int http_find_next_header(char *sol, struct hdr_idx *idx, struct hdr_ctx *ctx) |
| 658 | { |
| 659 | char *eol, *sov; |
| 660 | int cur_idx, old_idx; |
| 661 | int len; |
| 662 | |
| 663 | cur_idx = ctx->idx; |
| 664 | if (cur_idx) { |
| 665 | /* We have previously returned a header, let's search another one */ |
| 666 | sol = ctx->line; |
| 667 | eol = sol + idx->v[cur_idx].len; |
| 668 | goto next_hdr; |
| 669 | } |
| 670 | |
| 671 | /* first request for this header */ |
| 672 | sol += hdr_idx_first_pos(idx); |
| 673 | old_idx = 0; |
| 674 | cur_idx = hdr_idx_first_idx(idx); |
| 675 | while (cur_idx) { |
| 676 | eol = sol + idx->v[cur_idx].len; |
| 677 | |
| 678 | len = 0; |
| 679 | while (1) { |
| 680 | if (len >= eol - sol) |
| 681 | goto next_hdr; |
| 682 | if (sol[len] == ':') |
| 683 | break; |
| 684 | len++; |
| 685 | } |
| 686 | |
| 687 | ctx->del = len; |
| 688 | sov = sol + len + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 689 | while (sov < eol && HTTP_IS_LWS(*sov)) |
Willy Tarreau | c90dc23 | 2015-02-20 13:51:36 +0100 | [diff] [blame] | 690 | sov++; |
| 691 | |
| 692 | ctx->line = sol; |
| 693 | ctx->prev = old_idx; |
| 694 | ctx->idx = cur_idx; |
| 695 | ctx->val = sov - sol; |
| 696 | ctx->tws = 0; |
| 697 | |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 698 | while (eol > sov && HTTP_IS_LWS(*(eol - 1))) { |
Willy Tarreau | c90dc23 | 2015-02-20 13:51:36 +0100 | [diff] [blame] | 699 | eol--; |
| 700 | ctx->tws++; |
| 701 | } |
| 702 | ctx->vlen = eol - sov; |
| 703 | return 1; |
| 704 | |
| 705 | next_hdr: |
| 706 | sol = eol + idx->v[cur_idx].cr + 1; |
| 707 | old_idx = cur_idx; |
| 708 | cur_idx = idx->v[cur_idx].next; |
| 709 | } |
| 710 | return 0; |
| 711 | } |
| 712 | |
Lukas Tribus | 2395368 | 2017-04-28 13:24:30 +0000 | [diff] [blame] | 713 | /* Find the end of the header value contained between <s> and <e>. See RFC7230, |
| 714 | * 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] | 715 | * a valid result. This works for headers defined as comma-separated lists. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 716 | */ |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 717 | char *find_hdr_value_end(char *s, const char *e) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 718 | { |
| 719 | int quoted, qdpair; |
| 720 | |
| 721 | quoted = qdpair = 0; |
Willy Tarreau | e6d9c21 | 2016-11-05 18:23:38 +0100 | [diff] [blame] | 722 | |
| 723 | #if defined(__x86_64__) || \ |
| 724 | defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) || \ |
| 725 | defined(__ARM_ARCH_7A__) |
| 726 | /* speedup: skip everything not a comma nor a double quote */ |
| 727 | for (; s <= e - sizeof(int); s += sizeof(int)) { |
| 728 | unsigned int c = *(int *)s; // comma |
| 729 | unsigned int q = c; // quote |
| 730 | |
| 731 | c ^= 0x2c2c2c2c; // contains one zero on a comma |
| 732 | q ^= 0x22222222; // contains one zero on a quote |
| 733 | |
| 734 | c = (c - 0x01010101) & ~c; // contains 0x80 below a comma |
| 735 | q = (q - 0x01010101) & ~q; // contains 0x80 below a quote |
| 736 | |
| 737 | if ((c | q) & 0x80808080) |
| 738 | break; // found a comma or a quote |
| 739 | } |
| 740 | #endif |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 741 | for (; s < e; s++) { |
| 742 | if (qdpair) qdpair = 0; |
Willy Tarreau | 0f7f51f | 2010-08-30 11:06:34 +0200 | [diff] [blame] | 743 | else if (quoted) { |
| 744 | if (*s == '\\') qdpair = 1; |
| 745 | else if (*s == '"') quoted = 0; |
| 746 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 747 | else if (*s == '"') quoted = 1; |
| 748 | else if (*s == ',') return s; |
| 749 | } |
| 750 | return s; |
| 751 | } |
| 752 | |
| 753 | /* Find the first or next occurrence of header <name> in message buffer <sol> |
| 754 | * using headers index <idx>, and return it in the <ctx> structure. This |
| 755 | * structure holds everything necessary to use the header and find next |
| 756 | * occurrence. If its <idx> member is 0, the header is searched from the |
| 757 | * beginning. Otherwise, the next occurrence is returned. The function returns |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 758 | * 1 when it finds a value, and 0 when there is no more. It is designed to work |
| 759 | * with headers defined as comma-separated lists. As a special case, if ctx->val |
| 760 | * is NULL when searching for a new values of a header, the current header is |
| 761 | * rescanned. This allows rescanning after a header deletion. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 762 | */ |
| 763 | int http_find_header2(const char *name, int len, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 764 | char *sol, struct hdr_idx *idx, |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 765 | struct hdr_ctx *ctx) |
| 766 | { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 767 | char *eol, *sov; |
| 768 | int cur_idx, old_idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 769 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 770 | cur_idx = ctx->idx; |
| 771 | if (cur_idx) { |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 772 | /* We have previously returned a value, let's search |
| 773 | * another one on the same line. |
| 774 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 775 | sol = ctx->line; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 776 | ctx->del = ctx->val + ctx->vlen + ctx->tws; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 777 | sov = sol + ctx->del; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 778 | eol = sol + idx->v[cur_idx].len; |
| 779 | |
| 780 | if (sov >= eol) |
| 781 | /* no more values in this header */ |
| 782 | goto next_hdr; |
| 783 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 784 | /* values remaining for this header, skip the comma but save it |
| 785 | * for later use (eg: for header deletion). |
| 786 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 787 | sov++; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 788 | while (sov < eol && HTTP_IS_LWS((*sov))) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 789 | sov++; |
| 790 | |
| 791 | goto return_hdr; |
| 792 | } |
| 793 | |
| 794 | /* first request for this header */ |
| 795 | sol += hdr_idx_first_pos(idx); |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 796 | old_idx = 0; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 797 | cur_idx = hdr_idx_first_idx(idx); |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 798 | while (cur_idx) { |
| 799 | eol = sol + idx->v[cur_idx].len; |
| 800 | |
Willy Tarreau | 1ad7c6d | 2007-06-10 21:42:55 +0200 | [diff] [blame] | 801 | if (len == 0) { |
| 802 | /* No argument was passed, we want any header. |
| 803 | * To achieve this, we simply build a fake request. */ |
| 804 | while (sol + len < eol && sol[len] != ':') |
| 805 | len++; |
| 806 | name = sol; |
| 807 | } |
| 808 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 809 | if ((len < eol - sol) && |
| 810 | (sol[len] == ':') && |
| 811 | (strncasecmp(sol, name, len) == 0)) { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 812 | ctx->del = len; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 813 | sov = sol + len + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 814 | while (sov < eol && HTTP_IS_LWS(*sov)) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 815 | sov++; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 816 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 817 | ctx->line = sol; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 818 | ctx->prev = old_idx; |
| 819 | return_hdr: |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 820 | ctx->idx = cur_idx; |
| 821 | ctx->val = sov - sol; |
| 822 | |
| 823 | eol = find_hdr_value_end(sov, eol); |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 824 | ctx->tws = 0; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 825 | while (eol > sov && HTTP_IS_LWS(*(eol - 1))) { |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 826 | eol--; |
| 827 | ctx->tws++; |
| 828 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 829 | ctx->vlen = eol - sov; |
| 830 | return 1; |
| 831 | } |
| 832 | next_hdr: |
| 833 | sol = eol + idx->v[cur_idx].cr + 1; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 834 | old_idx = cur_idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 835 | cur_idx = idx->v[cur_idx].next; |
| 836 | } |
| 837 | return 0; |
| 838 | } |
| 839 | |
| 840 | int http_find_header(const char *name, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 841 | char *sol, struct hdr_idx *idx, |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 842 | struct hdr_ctx *ctx) |
| 843 | { |
| 844 | return http_find_header2(name, strlen(name), sol, idx, ctx); |
| 845 | } |
| 846 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 847 | /* Remove one value of a header. This only works on a <ctx> returned by one of |
| 848 | * the http_find_header functions. The value is removed, as well as surrounding |
| 849 | * 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] | 850 | * 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] | 851 | * message <msg>. The new index is returned. If it is zero, it means there is |
| 852 | * no more header, so any processing may stop. The ctx is always left in a form |
| 853 | * that can be handled by http_find_header2() to find next occurrence. |
| 854 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 855 | 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] | 856 | { |
| 857 | int cur_idx = ctx->idx; |
| 858 | char *sol = ctx->line; |
| 859 | struct hdr_idx_elem *hdr; |
| 860 | int delta, skip_comma; |
| 861 | |
| 862 | if (!cur_idx) |
| 863 | return 0; |
| 864 | |
| 865 | hdr = &idx->v[cur_idx]; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 866 | if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 867 | /* 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] | 868 | 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] | 869 | http_msg_move_end(msg, delta); |
| 870 | idx->used--; |
| 871 | hdr->len = 0; /* unused entry */ |
| 872 | idx->v[ctx->prev].next = idx->v[ctx->idx].next; |
Willy Tarreau | 5c4784f | 2011-02-12 13:07:35 +0100 | [diff] [blame] | 873 | if (idx->tail == ctx->idx) |
| 874 | idx->tail = ctx->prev; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 875 | ctx->idx = ctx->prev; /* walk back to the end of previous header */ |
Willy Tarreau | 7c1c217 | 2015-01-07 17:23:50 +0100 | [diff] [blame] | 876 | 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] | 877 | 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] | 878 | ctx->tws = ctx->vlen = 0; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 879 | return ctx->idx; |
| 880 | } |
| 881 | |
| 882 | /* 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] | 883 | * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the |
| 884 | * last entry of the list, we remove the last separator. |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 885 | */ |
| 886 | |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 887 | skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 888 | delta = buffer_replace2(msg->chn->buf, sol + ctx->del + skip_comma, |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 889 | sol + ctx->val + ctx->vlen + ctx->tws + skip_comma, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 890 | NULL, 0); |
| 891 | hdr->len += delta; |
| 892 | http_msg_move_end(msg, delta); |
| 893 | ctx->val = ctx->del; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 894 | ctx->tws = ctx->vlen = 0; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 895 | return ctx->idx; |
| 896 | } |
| 897 | |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 898 | /* This function handles a server error at the stream interface level. The |
| 899 | * 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] | 900 | * message is copied into the input buffer. |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 901 | * 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] | 902 | * in this buffer will be lost. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 903 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 904 | static void http_server_error(struct stream *s, struct stream_interface *si, |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 905 | int err, int finst, const struct chunk *msg) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 906 | { |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 907 | FLT_STRM_CB(s, flt_http_reply(s, s->txn->status, msg)); |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 908 | channel_auto_read(si_oc(si)); |
| 909 | channel_abort(si_oc(si)); |
| 910 | channel_auto_close(si_oc(si)); |
| 911 | channel_erase(si_oc(si)); |
| 912 | channel_auto_close(si_ic(si)); |
| 913 | channel_auto_read(si_ic(si)); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 914 | if (msg) |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 915 | co_inject(si_ic(si), msg->str, msg->len); |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 916 | if (!(s->flags & SF_ERR_MASK)) |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 917 | s->flags |= err; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 918 | if (!(s->flags & SF_FINST_MASK)) |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 919 | s->flags |= finst; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 920 | } |
| 921 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 922 | /* This function returns the appropriate error location for the given stream |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 923 | * and message. |
| 924 | */ |
| 925 | |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 926 | struct chunk *http_error_message(struct stream *s) |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 927 | { |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 928 | const int msgnum = http_get_status_idx(s->txn->status); |
| 929 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 930 | if (s->be->errmsg[msgnum].str) |
| 931 | return &s->be->errmsg[msgnum]; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 932 | else if (strm_fe(s)->errmsg[msgnum].str) |
| 933 | return &strm_fe(s)->errmsg[msgnum]; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 934 | else |
| 935 | return &http_err_chunks[msgnum]; |
| 936 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 937 | |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 938 | void |
| 939 | http_reply_and_close(struct stream *s, short status, struct chunk *msg) |
| 940 | { |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 941 | s->txn->flags &= ~TX_WAIT_NEXT_RQ; |
Christopher Faulet | 3e34429 | 2015-11-24 16:24:13 +0100 | [diff] [blame] | 942 | FLT_STRM_CB(s, flt_http_reply(s, status, msg)); |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 943 | stream_int_retnclose(&s->si[0], msg); |
| 944 | } |
| 945 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 946 | /* |
Willy Tarreau | b7ce424 | 2015-09-03 17:15:21 +0200 | [diff] [blame] | 947 | * returns a known method among HTTP_METH_* or HTTP_METH_OTHER for all unknown |
| 948 | * ones. |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 949 | */ |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 950 | 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] | 951 | { |
| 952 | unsigned char m; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 953 | const struct http_method_desc *h; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 954 | |
| 955 | m = ((unsigned)*str - 'A'); |
| 956 | |
| 957 | if (m < 26) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 958 | for (h = http_methods[m]; h->len > 0; h++) { |
| 959 | if (unlikely(h->len != len)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 960 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 961 | if (likely(memcmp(str, h->text, h->len) == 0)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 962 | return h->meth; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 963 | }; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 964 | } |
Willy Tarreau | b7ce424 | 2015-09-03 17:15:21 +0200 | [diff] [blame] | 965 | return HTTP_METH_OTHER; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 966 | } |
| 967 | |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 968 | /* Parse the URI from the given transaction (which is assumed to be in request |
| 969 | * phase) and look for the "/" beginning the PATH. If not found, return NULL. |
| 970 | * It is returned otherwise. |
| 971 | */ |
Thierry FOURNIER | 3c33178 | 2015-09-17 19:33:35 +0200 | [diff] [blame] | 972 | char *http_get_path(struct http_txn *txn) |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 973 | { |
| 974 | char *ptr, *end; |
| 975 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 976 | ptr = txn->req.chn->buf->p + txn->req.sl.rq.u; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 977 | end = ptr + txn->req.sl.rq.u_l; |
| 978 | |
| 979 | if (ptr >= end) |
| 980 | return NULL; |
| 981 | |
Lukas Tribus | 2395368 | 2017-04-28 13:24:30 +0000 | [diff] [blame] | 982 | /* RFC7230, par. 2.7 : |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 983 | * Request-URI = "*" | absuri | abspath | authority |
| 984 | */ |
| 985 | |
| 986 | if (*ptr == '*') |
| 987 | return NULL; |
| 988 | |
| 989 | if (isalpha((unsigned char)*ptr)) { |
| 990 | /* this is a scheme as described by RFC3986, par. 3.1 */ |
| 991 | ptr++; |
| 992 | while (ptr < end && |
| 993 | (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')) |
| 994 | ptr++; |
| 995 | /* skip '://' */ |
| 996 | if (ptr == end || *ptr++ != ':') |
| 997 | return NULL; |
| 998 | if (ptr == end || *ptr++ != '/') |
| 999 | return NULL; |
| 1000 | if (ptr == end || *ptr++ != '/') |
| 1001 | return NULL; |
| 1002 | } |
| 1003 | /* skip [user[:passwd]@]host[:[port]] */ |
| 1004 | |
| 1005 | while (ptr < end && *ptr != '/') |
| 1006 | ptr++; |
| 1007 | |
| 1008 | if (ptr == end) |
| 1009 | return NULL; |
| 1010 | |
| 1011 | /* OK, we got the '/' ! */ |
| 1012 | return ptr; |
| 1013 | } |
| 1014 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 1015 | /* Parse the URI from the given string and look for the "/" beginning the PATH. |
| 1016 | * If not found, return NULL. It is returned otherwise. |
| 1017 | */ |
| 1018 | static char * |
| 1019 | http_get_path_from_string(char *str) |
| 1020 | { |
| 1021 | char *ptr = str; |
| 1022 | |
| 1023 | /* RFC2616, par. 5.1.2 : |
| 1024 | * Request-URI = "*" | absuri | abspath | authority |
| 1025 | */ |
| 1026 | |
| 1027 | if (*ptr == '*') |
| 1028 | return NULL; |
| 1029 | |
| 1030 | if (isalpha((unsigned char)*ptr)) { |
| 1031 | /* this is a scheme as described by RFC3986, par. 3.1 */ |
| 1032 | ptr++; |
| 1033 | while (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.') |
| 1034 | ptr++; |
| 1035 | /* skip '://' */ |
| 1036 | if (*ptr == '\0' || *ptr++ != ':') |
| 1037 | return NULL; |
| 1038 | if (*ptr == '\0' || *ptr++ != '/') |
| 1039 | return NULL; |
| 1040 | if (*ptr == '\0' || *ptr++ != '/') |
| 1041 | return NULL; |
| 1042 | } |
| 1043 | /* skip [user[:passwd]@]host[:[port]] */ |
| 1044 | |
| 1045 | while (*ptr != '\0' && *ptr != ' ' && *ptr != '/') |
| 1046 | ptr++; |
| 1047 | |
| 1048 | if (*ptr == '\0' || *ptr == ' ') |
| 1049 | return NULL; |
| 1050 | |
| 1051 | /* OK, we got the '/' ! */ |
| 1052 | return ptr; |
| 1053 | } |
| 1054 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 1055 | /* Returns a 302 for a redirectable request that reaches a server working in |
| 1056 | * in redirect mode. This may only be called just after the stream interface |
| 1057 | * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will |
| 1058 | * follow normal proxy processing. NOTE: this function is designed to support |
| 1059 | * being called once data are scheduled for forwarding. |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1060 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1061 | void http_perform_server_redirect(struct stream *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1062 | { |
| 1063 | struct http_txn *txn; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 1064 | struct server *srv; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1065 | char *path; |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 1066 | int len, rewind; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1067 | |
| 1068 | /* 1: create the response header */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1069 | trash.len = strlen(HTTP_302); |
| 1070 | memcpy(trash.str, HTTP_302, trash.len); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1071 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 1072 | srv = objt_server(s->target); |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 1073 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1074 | /* 2: add the server's prefix */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1075 | if (trash.len + srv->rdr_len > trash.size) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1076 | return; |
| 1077 | |
Willy Tarreau | dcb75c4 | 2010-01-10 00:24:22 +0100 | [diff] [blame] | 1078 | /* special prefix "/" means don't change URL */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 1079 | if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1080 | memcpy(trash.str + trash.len, srv->rdr_pfx, srv->rdr_len); |
| 1081 | trash.len += srv->rdr_len; |
Willy Tarreau | dcb75c4 | 2010-01-10 00:24:22 +0100 | [diff] [blame] | 1082 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1083 | |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 1084 | /* 3: add the request URI. Since it was already forwarded, we need |
| 1085 | * to temporarily rewind the buffer. |
| 1086 | */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 1087 | txn = s->txn; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1088 | b_rew(s->req.buf, rewind = http_hdr_rewind(&txn->req)); |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 1089 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1090 | path = http_get_path(txn); |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1091 | 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] | 1092 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1093 | b_adv(s->req.buf, rewind); |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 1094 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1095 | if (!path) |
| 1096 | return; |
| 1097 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1098 | if (trash.len + len > trash.size - 4) /* 4 for CRLF-CRLF */ |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1099 | return; |
| 1100 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1101 | memcpy(trash.str + trash.len, path, len); |
| 1102 | trash.len += len; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1103 | |
| 1104 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1105 | memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29); |
| 1106 | trash.len += 29; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1107 | } else { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1108 | memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23); |
| 1109 | trash.len += 23; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1110 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1111 | |
| 1112 | /* prepare to return without error. */ |
Willy Tarreau | 73b013b | 2012-05-21 16:31:45 +0200 | [diff] [blame] | 1113 | si_shutr(si); |
| 1114 | si_shutw(si); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1115 | si->err_type = SI_ET_NONE; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1116 | si->state = SI_ST_CLO; |
| 1117 | |
| 1118 | /* send the message */ |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 1119 | txn->status = 302; |
| 1120 | http_server_error(s, si, SF_ERR_LOCAL, SF_FINST_C, &trash); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1121 | |
| 1122 | /* 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] | 1123 | srv_inc_sess_ctr(srv); |
Bhaskar Maddala | a20cb85 | 2014-02-03 16:26:46 -0500 | [diff] [blame] | 1124 | srv_set_sess_last(srv); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1125 | } |
| 1126 | |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 1127 | /* Return the error message corresponding to si->err_type. It is assumed |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1128 | * that the server side is closed. Note that err_type is actually a |
| 1129 | * bitmask, where almost only aborts may be cumulated with other |
| 1130 | * values. We consider that aborted operations are more important |
| 1131 | * than timeouts or errors due to the fact that nobody else in the |
| 1132 | * logs might explain incomplete retries. All others should avoid |
| 1133 | * being cumulated. It should normally not be possible to have multiple |
| 1134 | * 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] | 1135 | * Note that connection errors appearing on the second request of a keep-alive |
| 1136 | * connection are not reported since this allows the client to retry. |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1137 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1138 | void http_return_srv_error(struct stream *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1139 | { |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 1140 | int err_type = si->err_type; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1141 | |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 1142 | /* set s->txn->status for http_error_message(s) */ |
| 1143 | s->txn->status = 503; |
| 1144 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1145 | if (err_type & SI_ET_QUEUE_ABRT) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1146 | http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q, |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 1147 | http_error_message(s)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1148 | else if (err_type & SI_ET_CONN_ABRT) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1149 | http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C, |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 1150 | (s->txn->flags & TX_NOT_FIRST) ? NULL : |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 1151 | http_error_message(s)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1152 | else if (err_type & SI_ET_QUEUE_TO) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1153 | http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q, |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 1154 | http_error_message(s)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1155 | else if (err_type & SI_ET_QUEUE_ERR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1156 | http_server_error(s, si, SF_ERR_SRVCL, 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_TO) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1159 | http_server_error(s, si, SF_ERR_SRVTO, 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_CONN_ERR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1163 | http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C, |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 1164 | (s->flags & SF_SRV_REUSED) ? NULL : |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 1165 | http_error_message(s)); |
Willy Tarreau | 2d400bb | 2012-05-14 12:11:47 +0200 | [diff] [blame] | 1166 | else if (err_type & SI_ET_CONN_RES) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1167 | http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C, |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 1168 | (s->txn->flags & TX_NOT_FIRST) ? NULL : |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 1169 | http_error_message(s)); |
| 1170 | else { /* SI_ET_CONN_OTHER and others */ |
| 1171 | s->txn->status = 500; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1172 | http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C, |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 1173 | http_error_message(s)); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 1174 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1175 | } |
| 1176 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1177 | extern const char sess_term_cond[8]; |
| 1178 | extern const char sess_fin_state[8]; |
| 1179 | extern const char *monthname[12]; |
Willy Tarreau | 63986c7 | 2015-04-03 22:55:33 +0200 | [diff] [blame] | 1180 | struct pool_head *pool2_http_txn; |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 1181 | struct pool_head *pool2_requri; |
Willy Tarreau | 193b8c6 | 2012-11-22 00:17:38 +0100 | [diff] [blame] | 1182 | struct pool_head *pool2_capture = NULL; |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 1183 | struct pool_head *pool2_uniqueid; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1184 | |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1185 | /* |
| 1186 | * Capture headers from message starting at <som> according to header list |
Willy Tarreau | 54da8db | 2014-06-13 16:11:48 +0200 | [diff] [blame] | 1187 | * <cap_hdr>, and fill the <cap> pointers appropriately. |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1188 | */ |
| 1189 | void capture_headers(char *som, struct hdr_idx *idx, |
| 1190 | char **cap, struct cap_hdr *cap_hdr) |
| 1191 | { |
| 1192 | char *eol, *sol, *col, *sov; |
| 1193 | int cur_idx; |
| 1194 | struct cap_hdr *h; |
| 1195 | int len; |
| 1196 | |
| 1197 | sol = som + hdr_idx_first_pos(idx); |
| 1198 | cur_idx = hdr_idx_first_idx(idx); |
| 1199 | |
| 1200 | while (cur_idx) { |
| 1201 | eol = sol + idx->v[cur_idx].len; |
| 1202 | |
| 1203 | col = sol; |
| 1204 | while (col < eol && *col != ':') |
| 1205 | col++; |
| 1206 | |
| 1207 | sov = col + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 1208 | while (sov < eol && HTTP_IS_LWS(*sov)) |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1209 | sov++; |
| 1210 | |
| 1211 | for (h = cap_hdr; h; h = h->next) { |
Willy Tarreau | 54da8db | 2014-06-13 16:11:48 +0200 | [diff] [blame] | 1212 | if (h->namelen && (h->namelen == col - sol) && |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1213 | (strncasecmp(sol, h->name, h->namelen) == 0)) { |
| 1214 | if (cap[h->index] == NULL) |
| 1215 | cap[h->index] = |
Willy Tarreau | cf7f320 | 2007-05-13 22:46:04 +0200 | [diff] [blame] | 1216 | pool_alloc2(h->pool); |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1217 | |
| 1218 | if (cap[h->index] == NULL) { |
| 1219 | Alert("HTTP capture : out of memory.\n"); |
| 1220 | continue; |
| 1221 | } |
| 1222 | |
| 1223 | len = eol - sov; |
| 1224 | if (len > h->len) |
| 1225 | len = h->len; |
| 1226 | |
| 1227 | memcpy(cap[h->index], sov, len); |
| 1228 | cap[h->index][len]=0; |
| 1229 | } |
| 1230 | } |
| 1231 | sol = eol + idx->v[cur_idx].cr + 1; |
| 1232 | cur_idx = idx->v[cur_idx].next; |
| 1233 | } |
| 1234 | } |
| 1235 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1236 | /* |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1237 | * Returns the data from Authorization header. Function may be called more |
| 1238 | * than once so data is stored in txn->auth_data. When no header is found |
| 1239 | * 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] | 1240 | * searching again for something we are unable to find anyway. However, if |
| 1241 | * 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] | 1242 | * have the credentials overwritten by another stream in parallel. |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1243 | */ |
| 1244 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1245 | int |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1246 | get_http_auth(struct stream *s) |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1247 | { |
| 1248 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 1249 | struct http_txn *txn = s->txn; |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1250 | struct chunk auth_method; |
| 1251 | struct hdr_ctx ctx; |
| 1252 | char *h, *p; |
| 1253 | int len; |
| 1254 | |
| 1255 | #ifdef DEBUG_AUTH |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1256 | printf("Auth for stream %p: %d\n", s, txn->auth.method); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1257 | #endif |
| 1258 | |
| 1259 | if (txn->auth.method == HTTP_AUTH_WRONG) |
| 1260 | return 0; |
| 1261 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1262 | txn->auth.method = HTTP_AUTH_WRONG; |
| 1263 | |
| 1264 | ctx.idx = 0; |
Willy Tarreau | 844a7e7 | 2010-01-31 21:46:18 +0100 | [diff] [blame] | 1265 | |
| 1266 | if (txn->flags & TX_USE_PX_CONN) { |
| 1267 | h = "Proxy-Authorization"; |
| 1268 | len = strlen(h); |
| 1269 | } else { |
| 1270 | h = "Authorization"; |
| 1271 | len = strlen(h); |
| 1272 | } |
| 1273 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1274 | 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] | 1275 | return 0; |
| 1276 | |
| 1277 | h = ctx.line + ctx.val; |
| 1278 | |
| 1279 | p = memchr(h, ' ', ctx.vlen); |
Willy Tarreau | 5c557d1 | 2016-03-13 08:17:02 +0100 | [diff] [blame] | 1280 | len = p - h; |
| 1281 | if (!p || len <= 0) |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1282 | return 0; |
| 1283 | |
David Carlier | 7365f7d | 2016-04-04 11:54:42 +0100 | [diff] [blame] | 1284 | if (chunk_initlen(&auth_method, h, 0, len) != 1) |
| 1285 | return 0; |
| 1286 | |
Willy Tarreau | 5c557d1 | 2016-03-13 08:17:02 +0100 | [diff] [blame] | 1287 | 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] | 1288 | |
| 1289 | if (!strncasecmp("Basic", auth_method.str, auth_method.len)) { |
Christopher Faulet | 6988f67 | 2017-07-27 15:18:52 +0200 | [diff] [blame] | 1290 | struct chunk *http_auth = get_trash_chunk(); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1291 | |
| 1292 | len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len, |
Christopher Faulet | 6988f67 | 2017-07-27 15:18:52 +0200 | [diff] [blame] | 1293 | http_auth->str, global.tune.bufsize - 1); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1294 | |
| 1295 | if (len < 0) |
| 1296 | return 0; |
| 1297 | |
| 1298 | |
Christopher Faulet | 6988f67 | 2017-07-27 15:18:52 +0200 | [diff] [blame] | 1299 | http_auth->str[len] = '\0'; |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1300 | |
Christopher Faulet | 6988f67 | 2017-07-27 15:18:52 +0200 | [diff] [blame] | 1301 | p = strchr(http_auth->str, ':'); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1302 | |
| 1303 | if (!p) |
| 1304 | return 0; |
| 1305 | |
Christopher Faulet | 6988f67 | 2017-07-27 15:18:52 +0200 | [diff] [blame] | 1306 | txn->auth.user = http_auth->str; |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1307 | *p = '\0'; |
| 1308 | txn->auth.pass = p+1; |
| 1309 | |
| 1310 | txn->auth.method = HTTP_AUTH_BASIC; |
| 1311 | return 1; |
| 1312 | } |
| 1313 | |
| 1314 | return 0; |
| 1315 | } |
| 1316 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1317 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1318 | /* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the |
| 1319 | * conversion succeeded, 0 in case of error. If the request was already 1.X, |
| 1320 | * nothing is done and 1 is returned. |
| 1321 | */ |
Willy Tarreau | 418bfcc | 2012-03-09 13:56:20 +0100 | [diff] [blame] | 1322 | static int http_upgrade_v09_to_v10(struct http_txn *txn) |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1323 | { |
| 1324 | int delta; |
| 1325 | char *cur_end; |
Willy Tarreau | 418bfcc | 2012-03-09 13:56:20 +0100 | [diff] [blame] | 1326 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1327 | |
| 1328 | if (msg->sl.rq.v_l != 0) |
| 1329 | return 1; |
| 1330 | |
Apollon Oikonomopoulos | 25a1522 | 2014-04-06 02:46:00 +0300 | [diff] [blame] | 1331 | /* RFC 1945 allows only GET for HTTP/0.9 requests */ |
| 1332 | if (txn->meth != HTTP_METH_GET) |
| 1333 | return 0; |
| 1334 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1335 | cur_end = msg->chn->buf->p + msg->sl.rq.l; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1336 | |
| 1337 | if (msg->sl.rq.u_l == 0) { |
Apollon Oikonomopoulos | 25a1522 | 2014-04-06 02:46:00 +0300 | [diff] [blame] | 1338 | /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */ |
| 1339 | return 0; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1340 | } |
| 1341 | /* add HTTP version */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1342 | 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] | 1343 | http_msg_move_end(msg, delta); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1344 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1345 | cur_end = (char *)http_parse_reqline(msg, |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1346 | HTTP_MSG_RQMETH, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1347 | msg->chn->buf->p, cur_end + 1, |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1348 | NULL, NULL); |
| 1349 | if (unlikely(!cur_end)) |
| 1350 | return 0; |
| 1351 | |
| 1352 | /* we have a full HTTP/1.0 request now and we know that |
| 1353 | * we have either a CR or an LF at <ptr>. |
| 1354 | */ |
| 1355 | hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r'); |
| 1356 | return 1; |
| 1357 | } |
| 1358 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1359 | /* Parse the Connection: header of an HTTP request, looking for both "close" |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1360 | * and "keep-alive" values. If we already know that some headers may safely |
| 1361 | * 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] | 1362 | * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses) |
| 1363 | * - 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] | 1364 | * Presence of the "Upgrade" token is also checked and reported. |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1365 | * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was |
| 1366 | * found, and TX_CON_*_SET is adjusted depending on what is left so only |
| 1367 | * harmless combinations may be removed. Do not call that after changes have |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1368 | * been processed. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1369 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1370 | 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] | 1371 | { |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1372 | struct hdr_ctx ctx; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1373 | const char *hdr_val = "Connection"; |
| 1374 | int hdr_len = 10; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1375 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1376 | if (txn->flags & TX_HDR_CONN_PRS) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1377 | return; |
| 1378 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1379 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1380 | hdr_val = "Proxy-Connection"; |
| 1381 | hdr_len = 16; |
| 1382 | } |
| 1383 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1384 | ctx.idx = 0; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1385 | txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1386 | 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] | 1387 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 1388 | txn->flags |= TX_HDR_CONN_KAL; |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1389 | if (to_del & 2) |
| 1390 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1391 | else |
| 1392 | txn->flags |= TX_CON_KAL_SET; |
| 1393 | } |
| 1394 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 1395 | txn->flags |= TX_HDR_CONN_CLO; |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1396 | if (to_del & 1) |
| 1397 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1398 | else |
| 1399 | txn->flags |= TX_CON_CLO_SET; |
| 1400 | } |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 1401 | else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) { |
| 1402 | txn->flags |= TX_HDR_CONN_UPG; |
| 1403 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1404 | } |
| 1405 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1406 | txn->flags |= TX_HDR_CONN_PRS; |
| 1407 | return; |
| 1408 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1409 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1410 | /* Apply desired changes on the Connection: header. Values may be removed and/or |
| 1411 | * added depending on the <wanted> flags, which are exclusively composed of |
| 1412 | * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The |
| 1413 | * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left. |
| 1414 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1415 | 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] | 1416 | { |
| 1417 | struct hdr_ctx ctx; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1418 | const char *hdr_val = "Connection"; |
| 1419 | int hdr_len = 10; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1420 | |
| 1421 | ctx.idx = 0; |
| 1422 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1423 | |
| 1424 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1425 | hdr_val = "Proxy-Connection"; |
| 1426 | hdr_len = 16; |
| 1427 | } |
| 1428 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1429 | txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1430 | 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] | 1431 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 1432 | if (wanted & TX_CON_KAL_SET) |
| 1433 | txn->flags |= TX_CON_KAL_SET; |
| 1434 | else |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1435 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1436 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1437 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 1438 | if (wanted & TX_CON_CLO_SET) |
| 1439 | txn->flags |= TX_CON_CLO_SET; |
| 1440 | else |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1441 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 1442 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1443 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1444 | |
| 1445 | if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 1446 | return; |
| 1447 | |
| 1448 | if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) { |
| 1449 | txn->flags |= TX_CON_CLO_SET; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1450 | hdr_val = "Connection: close"; |
| 1451 | hdr_len = 17; |
| 1452 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1453 | hdr_val = "Proxy-Connection: close"; |
| 1454 | hdr_len = 23; |
| 1455 | } |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1456 | http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1457 | } |
| 1458 | |
| 1459 | if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) { |
| 1460 | txn->flags |= TX_CON_KAL_SET; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1461 | hdr_val = "Connection: keep-alive"; |
| 1462 | hdr_len = 22; |
| 1463 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1464 | hdr_val = "Proxy-Connection: keep-alive"; |
| 1465 | hdr_len = 28; |
| 1466 | } |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1467 | http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1468 | } |
| 1469 | return; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1470 | } |
| 1471 | |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 1472 | /* Parses a qvalue and returns it multipled by 1000, from 0 to 1000. If the |
| 1473 | * value is larger than 1000, it is bound to 1000. The parser consumes up to |
| 1474 | * 1 digit, one dot and 3 digits and stops on the first invalid character. |
| 1475 | * Unparsable qvalues return 1000 as "q=1.000". |
| 1476 | */ |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 1477 | int parse_qvalue(const char *qvalue, const char **end) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 1478 | { |
| 1479 | int q = 1000; |
| 1480 | |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 1481 | if (!isdigit((unsigned char)*qvalue)) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 1482 | goto out; |
| 1483 | q = (*qvalue++ - '0') * 1000; |
| 1484 | |
| 1485 | if (*qvalue++ != '.') |
| 1486 | goto out; |
| 1487 | |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 1488 | if (!isdigit((unsigned char)*qvalue)) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 1489 | goto out; |
| 1490 | q += (*qvalue++ - '0') * 100; |
| 1491 | |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 1492 | if (!isdigit((unsigned char)*qvalue)) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 1493 | goto out; |
| 1494 | q += (*qvalue++ - '0') * 10; |
| 1495 | |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 1496 | if (!isdigit((unsigned char)*qvalue)) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 1497 | goto out; |
| 1498 | q += (*qvalue++ - '0') * 1; |
| 1499 | out: |
| 1500 | if (q > 1000) |
| 1501 | q = 1000; |
Willy Tarreau | 38b3aa5 | 2014-04-22 23:32:05 +0200 | [diff] [blame] | 1502 | if (end) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 1503 | *end = qvalue; |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 1504 | return q; |
| 1505 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 1506 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1507 | 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] | 1508 | { |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 1509 | struct proxy *fe = strm_fe(s); |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 1510 | int tmp = TX_CON_WANT_KAL; |
| 1511 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1512 | if (!((fe->options2|s->be->options2) & PR_O2_FAKE_KA)) { |
| 1513 | if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN || |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 1514 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN) |
| 1515 | tmp = TX_CON_WANT_TUN; |
| 1516 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1517 | if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 1518 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL) |
| 1519 | tmp = TX_CON_WANT_TUN; |
| 1520 | } |
| 1521 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1522 | if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL || |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 1523 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) { |
| 1524 | /* option httpclose + server_close => forceclose */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1525 | if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 1526 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL) |
| 1527 | tmp = TX_CON_WANT_CLO; |
| 1528 | else |
| 1529 | tmp = TX_CON_WANT_SCL; |
| 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_FCL || |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 1533 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL) |
| 1534 | tmp = TX_CON_WANT_CLO; |
| 1535 | |
| 1536 | if ((txn->flags & TX_CON_WANT_MSK) < tmp) |
| 1537 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp; |
| 1538 | |
| 1539 | if (!(txn->flags & TX_HDR_CONN_PRS) && |
| 1540 | (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) { |
| 1541 | /* parse the Connection header and possibly clean it */ |
| 1542 | int to_del = 0; |
| 1543 | if ((msg->flags & HTTP_MSGF_VER_11) || |
| 1544 | ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1545 | !((fe->options2|s->be->options2) & PR_O2_FAKE_KA))) |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 1546 | to_del |= 2; /* remove "keep-alive" */ |
| 1547 | if (!(msg->flags & HTTP_MSGF_VER_11)) |
| 1548 | to_del |= 1; /* remove "close" */ |
| 1549 | http_parse_connection_header(txn, msg, to_del); |
| 1550 | } |
| 1551 | |
| 1552 | /* check if client or config asks for explicit close in KAL/SCL */ |
| 1553 | if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 1554 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) && |
| 1555 | ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */ |
| 1556 | (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */ |
| 1557 | !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1558 | fe->state == PR_STSTOPPED)) /* frontend is stopping */ |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 1559 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 1560 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 1561 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 1562 | /* This stream analyser waits for a complete HTTP request. It returns 1 if the |
| 1563 | * processing can continue on next analysers, or zero if it either needs more |
| 1564 | * data or wants to immediately abort the request (eg: timeout, error, ...). It |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1565 | * 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] | 1566 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 1567 | * abort. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1568 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1569 | 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] | 1570 | { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1571 | /* |
| 1572 | * We will parse the partial (or complete) lines. |
| 1573 | * We will check the request syntax, and also join multi-line |
| 1574 | * headers. An index of all the lines will be elaborated while |
| 1575 | * parsing. |
| 1576 | * |
| 1577 | * For the parsing, we use a 28 states FSM. |
| 1578 | * |
| 1579 | * Here is the information we currently have : |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1580 | * req->buf->p = beginning of request |
| 1581 | * req->buf->p + msg->eoh = end of processed headers / start of current one |
| 1582 | * req->buf->p + req->buf->i = end of input data |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 1583 | * msg->eol = end of current header or line (LF or CRLF) |
| 1584 | * msg->next = first non-visited byte |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 1585 | * |
| 1586 | * 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] | 1587 | * we will set a few fields (txn->meth, sn->flags/SF_REDIRECTABLE). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 1588 | * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and |
| 1589 | * finally headers capture. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1590 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1591 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1592 | int cur_idx; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 1593 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 1594 | struct http_txn *txn = s->txn; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1595 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 1596 | struct hdr_ctx ctx; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1597 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1598 | 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] | 1599 | now_ms, __FUNCTION__, |
| 1600 | s, |
| 1601 | req, |
| 1602 | req->rex, req->wex, |
| 1603 | req->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1604 | req->buf->i, |
Willy Tarreau | 6bf1736 | 2009-02-24 10:48:35 +0100 | [diff] [blame] | 1605 | req->analysers); |
| 1606 | |
Willy Tarreau | 52a0c60 | 2009-08-16 22:45:38 +0200 | [diff] [blame] | 1607 | /* we're speaking HTTP here, so let's speak HTTP to the client */ |
| 1608 | s->srv_error = http_return_srv_error; |
| 1609 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 1610 | /* There's a protected area at the end of the buffer for rewriting |
| 1611 | * purposes. We don't want to start to parse the request if the |
| 1612 | * protected area is affected, because we may have to move processed |
| 1613 | * data later, which is much more complicated. |
| 1614 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1615 | 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] | 1616 | |
| 1617 | /* This point is executed when some data is avalaible for analysis, |
| 1618 | * so we log the end of the idle time. */ |
| 1619 | if (s->logs.t_idle == -1) |
| 1620 | s->logs.t_idle = tv_ms_elapsed(&s->logs.tv_accept, &now) - s->logs.t_handshake; |
| 1621 | |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 1622 | if (txn->flags & TX_NOT_FIRST) { |
Willy Tarreau | ba0902e | 2015-01-13 14:39:16 +0100 | [diff] [blame] | 1623 | if (unlikely(!channel_is_rewritable(req))) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 1624 | 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] | 1625 | goto failed_keep_alive; |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 1626 | /* 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] | 1627 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 1628 | req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | d7ad9f5 | 2013-12-31 17:26:25 +0100 | [diff] [blame] | 1629 | req->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 1630 | return 0; |
| 1631 | } |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 1632 | if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) || |
| 1633 | bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite)) |
| 1634 | buffer_slow_realign(req->buf); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 1635 | } |
| 1636 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1637 | if (likely(msg->next < req->buf->i)) /* some unparsed data are available */ |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 1638 | http_msg_analyzer(msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 1639 | } |
| 1640 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1641 | /* 1: we might have to print this header in debug mode */ |
| 1642 | if (unlikely((global.mode & MODE_DEBUG) && |
| 1643 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | 7d59e90 | 2014-10-21 19:36:09 +0200 | [diff] [blame] | 1644 | msg->msg_state >= HTTP_MSG_BODY)) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1645 | char *eol, *sol; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1646 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1647 | sol = req->buf->p; |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 1648 | /* this is a bit complex : in case of error on the request line, |
| 1649 | * we know that rq.l is still zero, so we display only the part |
| 1650 | * up to the end of the line (truncated by debug_hdr). |
| 1651 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1652 | 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] | 1653 | debug_hdr("clireq", s, sol, eol); |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 1654 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1655 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 1656 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1657 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1658 | while (cur_idx) { |
| 1659 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 1660 | debug_hdr("clihdr", s, sol, eol); |
| 1661 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 1662 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1663 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1664 | } |
| 1665 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1666 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1667 | /* |
| 1668 | * Now we quickly check if we have found a full valid request. |
| 1669 | * If not so, we check the FD and buffer states before leaving. |
| 1670 | * A full request is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 1671 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 1672 | * requests are checked first. When waiting for a second request |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1673 | * on a keep-alive stream, if we encounter and error, close, t/o, |
| 1674 | * 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] | 1675 | * 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] | 1676 | * process_stream() as a frontend error. |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 1677 | * Last, we may increase some tracked counters' http request errors on |
| 1678 | * the cases that are deliberately the client's fault. For instance, |
| 1679 | * a timeout or connection reset is not counted as an error. However |
| 1680 | * a bad request is. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1681 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1682 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 1683 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1684 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1685 | * First, let's catch bad requests. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1686 | */ |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 1687 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1688 | stream_inc_http_req_ctr(s); |
| 1689 | stream_inc_http_err_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1690 | proxy_inc_fe_req_ctr(sess->fe); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1691 | goto return_bad_req; |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 1692 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1693 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1694 | /* 1: Since we are in header mode, if there's no space |
| 1695 | * left for headers, we won't be able to free more |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1696 | * later, so the stream will never terminate. We |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1697 | * must terminate it now. |
| 1698 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1699 | if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1700 | /* FIXME: check if URI is set and return Status |
| 1701 | * 414 Request URI too long instead. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1702 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1703 | stream_inc_http_req_ctr(s); |
| 1704 | stream_inc_http_err_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1705 | proxy_inc_fe_req_ctr(sess->fe); |
Willy Tarreau | fec4d89 | 2011-09-02 20:04:57 +0200 | [diff] [blame] | 1706 | if (msg->err_pos < 0) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1707 | msg->err_pos = req->buf->i; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1708 | goto return_bad_req; |
| 1709 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1710 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1711 | /* 2: have we encountered a read error ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 1712 | else if (req->flags & CF_READ_ERROR) { |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1713 | if (!(s->flags & SF_ERR_MASK)) |
| 1714 | s->flags |= SF_ERR_CLICL; |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 1715 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 1716 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 1717 | goto failed_keep_alive; |
| 1718 | |
Willy Tarreau | 0f228a0 | 2015-05-01 15:37:53 +0200 | [diff] [blame] | 1719 | if (sess->fe->options & PR_O_IGNORE_PRB) |
| 1720 | goto failed_keep_alive; |
| 1721 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1722 | /* we cannot return any message on error */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 1723 | if (msg->err_pos >= 0) { |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 1724 | http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, sess->fe); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1725 | stream_inc_http_err_ctr(s); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 1726 | } |
| 1727 | |
Willy Tarreau | dc979f2 | 2012-12-04 10:39:01 +0100 | [diff] [blame] | 1728 | txn->status = 400; |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 1729 | msg->err_state = msg->msg_state; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1730 | msg->msg_state = HTTP_MSG_ERROR; |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 1731 | http_reply_and_close(s, txn->status, NULL); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 1732 | req->analysers &= AN_REQ_FLT_END; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1733 | stream_inc_http_req_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1734 | proxy_inc_fe_req_ctr(sess->fe); |
| 1735 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 1736 | if (sess->listener->counters) |
| 1737 | sess->listener->counters->failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 1738 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1739 | if (!(s->flags & SF_FINST_MASK)) |
| 1740 | s->flags |= SF_FINST_R; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1741 | return 0; |
| 1742 | } |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 1743 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1744 | /* 3: has the read timeout expired ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 1745 | 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] | 1746 | if (!(s->flags & SF_ERR_MASK)) |
| 1747 | s->flags |= SF_ERR_CLITO; |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 1748 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 1749 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 1750 | goto failed_keep_alive; |
| 1751 | |
Willy Tarreau | 0f228a0 | 2015-05-01 15:37:53 +0200 | [diff] [blame] | 1752 | if (sess->fe->options & PR_O_IGNORE_PRB) |
| 1753 | goto failed_keep_alive; |
| 1754 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1755 | /* read timeout : give up with an error message. */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 1756 | if (msg->err_pos >= 0) { |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 1757 | http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, sess->fe); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1758 | stream_inc_http_err_ctr(s); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 1759 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1760 | txn->status = 408; |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 1761 | msg->err_state = msg->msg_state; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1762 | msg->msg_state = HTTP_MSG_ERROR; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 1763 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 1764 | req->analysers &= AN_REQ_FLT_END; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 1765 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1766 | stream_inc_http_req_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1767 | proxy_inc_fe_req_ctr(sess->fe); |
| 1768 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 1769 | if (sess->listener->counters) |
| 1770 | sess->listener->counters->failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 1771 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1772 | if (!(s->flags & SF_FINST_MASK)) |
| 1773 | s->flags |= SF_FINST_R; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1774 | return 0; |
| 1775 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1776 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1777 | /* 4: have we encountered a close ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 1778 | else if (req->flags & CF_SHUTR) { |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1779 | if (!(s->flags & SF_ERR_MASK)) |
| 1780 | s->flags |= SF_ERR_CLICL; |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 1781 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 1782 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 1783 | goto failed_keep_alive; |
| 1784 | |
Willy Tarreau | 0f228a0 | 2015-05-01 15:37:53 +0200 | [diff] [blame] | 1785 | if (sess->fe->options & PR_O_IGNORE_PRB) |
| 1786 | goto failed_keep_alive; |
| 1787 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 1788 | if (msg->err_pos >= 0) |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 1789 | http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, sess->fe); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1790 | txn->status = 400; |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 1791 | msg->err_state = msg->msg_state; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1792 | msg->msg_state = HTTP_MSG_ERROR; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 1793 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 1794 | req->analysers &= AN_REQ_FLT_END; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1795 | stream_inc_http_err_ctr(s); |
| 1796 | stream_inc_http_req_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1797 | proxy_inc_fe_req_ctr(sess->fe); |
| 1798 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 1799 | if (sess->listener->counters) |
| 1800 | sess->listener->counters->failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 1801 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1802 | if (!(s->flags & SF_FINST_MASK)) |
| 1803 | s->flags |= SF_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 1804 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1805 | } |
| 1806 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 1807 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 1808 | req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1809 | s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 1810 | #ifdef TCP_QUICKACK |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 1811 | if (sess->listener->options & LI_O_NOQUICKACK && req->buf->i && |
| 1812 | objt_conn(sess->origin) && conn_ctrl_ready(__objt_conn(sess->origin))) { |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 1813 | /* We need more data, we have to re-enable quick-ack in case we |
| 1814 | * previously disabled it, otherwise we might cause the client |
| 1815 | * to delay next data. |
| 1816 | */ |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 1817 | 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] | 1818 | } |
| 1819 | #endif |
Willy Tarreau | 1b194fe | 2009-03-21 21:10:04 +0100 | [diff] [blame] | 1820 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 1821 | if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) { |
| 1822 | /* If the client starts to talk, let's fall back to |
| 1823 | * request timeout processing. |
| 1824 | */ |
| 1825 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 1826 | req->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 1827 | } |
| 1828 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1829 | /* just set the request timeout once at the beginning of the request */ |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 1830 | if (!tick_isset(req->analyse_exp)) { |
| 1831 | if ((msg->msg_state == HTTP_MSG_RQBEFORE) && |
| 1832 | (txn->flags & TX_WAIT_NEXT_RQ) && |
| 1833 | tick_isset(s->be->timeout.httpka)) |
| 1834 | req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka); |
| 1835 | else |
| 1836 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
| 1837 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1838 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1839 | /* we're not ready yet */ |
| 1840 | return 0; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 1841 | |
| 1842 | failed_keep_alive: |
| 1843 | /* Here we process low-level errors for keep-alive requests. In |
| 1844 | * short, if the request is not the first one and it experiences |
| 1845 | * a timeout, read error or shutdown, we just silently close so |
| 1846 | * that the client can try again. |
| 1847 | */ |
| 1848 | txn->status = 0; |
| 1849 | msg->msg_state = HTTP_MSG_RQBEFORE; |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 1850 | req->analysers &= AN_REQ_FLT_END; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 1851 | s->logs.logwait = 0; |
Willy Tarreau | abcd514 | 2013-06-11 17:18:02 +0200 | [diff] [blame] | 1852 | s->logs.level = 0; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1853 | s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 1854 | http_reply_and_close(s, txn->status, NULL); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 1855 | return 0; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1856 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1857 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 1858 | /* OK now we have a complete HTTP request with indexed headers. Let's |
| 1859 | * complete the request parsing by setting a few fields we will need |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1860 | * 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] | 1861 | * 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] | 1862 | * 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] | 1863 | * byte after the last LF. msg->sov points to the first byte of data. |
| 1864 | * msg->eol cannot be trusted because it may have been left uninitialized |
| 1865 | * (for instance in the absence of headers). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 1866 | */ |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1867 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1868 | stream_inc_http_req_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1869 | 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] | 1870 | |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 1871 | if (txn->flags & TX_WAIT_NEXT_RQ) { |
| 1872 | /* kill the pending keep-alive timeout */ |
| 1873 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
| 1874 | req->analyse_exp = TICK_ETERNITY; |
| 1875 | } |
| 1876 | |
| 1877 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 1878 | /* Maybe we found in invalid header name while we were configured not |
| 1879 | * to block on that, so we have to capture it now. |
| 1880 | */ |
| 1881 | if (unlikely(msg->err_pos >= 0)) |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 1882 | http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, sess->fe); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 1883 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1884 | /* |
| 1885 | * 1: identify the method |
| 1886 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1887 | 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] | 1888 | |
| 1889 | /* we can make use of server redirect on GET and HEAD */ |
| 1890 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1891 | s->flags |= SF_REDIRECTABLE; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 1892 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1893 | /* |
| 1894 | * 2: check if the URI matches the monitor_uri. |
| 1895 | * We have to do this for every request which gets in, because |
| 1896 | * the monitor-uri is defined by the frontend. |
| 1897 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1898 | if (unlikely((sess->fe->monitor_uri_len != 0) && |
| 1899 | (sess->fe->monitor_uri_len == msg->sl.rq.u_l) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1900 | !memcmp(req->buf->p + msg->sl.rq.u, |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1901 | sess->fe->monitor_uri, |
| 1902 | sess->fe->monitor_uri_len))) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1903 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1904 | * We have found the monitor URI |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1905 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1906 | struct acl_cond *cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 1907 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1908 | s->flags |= SF_MONITOR; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1909 | sess->fe->fe_counters.intercepted_req++; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 1910 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1911 | /* Check if we want to fail this monitor request or not */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1912 | list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 1913 | 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] | 1914 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1915 | ret = acl_pass(ret); |
| 1916 | if (cond->pol == ACL_COND_UNLESS) |
| 1917 | ret = !ret; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 1918 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1919 | if (ret) { |
| 1920 | /* we fail this request, let's return 503 service unavail */ |
| 1921 | txn->status = 503; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 1922 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1923 | if (!(s->flags & SF_ERR_MASK)) |
| 1924 | 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] | 1925 | goto return_prx_cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 1926 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1927 | } |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 1928 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1929 | /* nothing to fail, let's reply normaly */ |
| 1930 | txn->status = 200; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 1931 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1932 | if (!(s->flags & SF_ERR_MASK)) |
| 1933 | 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] | 1934 | goto return_prx_cond; |
| 1935 | } |
| 1936 | |
| 1937 | /* |
| 1938 | * 3: Maybe we have to copy the original REQURI for the logs ? |
| 1939 | * Note: we cannot log anymore if the request has been |
| 1940 | * classified as invalid. |
| 1941 | */ |
| 1942 | if (unlikely(s->logs.logwait & LW_REQ)) { |
| 1943 | /* we have a complete HTTP request that we must log */ |
| 1944 | if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) { |
| 1945 | int urilen = msg->sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1946 | |
Stéphane Cottin | 23e9e93 | 2017-05-18 08:58:41 +0200 | [diff] [blame] | 1947 | if (urilen >= global.tune.requri_len ) |
| 1948 | urilen = global.tune.requri_len - 1; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1949 | memcpy(txn->uri, req->buf->p, urilen); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1950 | txn->uri[urilen] = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1951 | |
Willy Tarreau | d79a3b2 | 2012-12-28 09:40:16 +0100 | [diff] [blame] | 1952 | if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT))) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1953 | s->do_log(s); |
| 1954 | } else { |
| 1955 | Alert("HTTP logging : out of memory.\n"); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1956 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1957 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1958 | |
Willy Tarreau | 91852eb | 2015-05-01 13:26:00 +0200 | [diff] [blame] | 1959 | /* RFC7230#2.6 has enforced the format of the HTTP version string to be |
| 1960 | * exactly one digit "." one digit. This check may be disabled using |
| 1961 | * option accept-invalid-http-request. |
| 1962 | */ |
| 1963 | if (!(sess->fe->options2 & PR_O2_REQBUG_OK)) { |
| 1964 | if (msg->sl.rq.v_l != 8) { |
| 1965 | msg->err_pos = msg->sl.rq.v; |
| 1966 | goto return_bad_req; |
| 1967 | } |
| 1968 | |
| 1969 | if (req->buf->p[msg->sl.rq.v + 4] != '/' || |
| 1970 | !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 5]) || |
| 1971 | req->buf->p[msg->sl.rq.v + 6] != '.' || |
| 1972 | !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 7])) { |
| 1973 | msg->err_pos = msg->sl.rq.v + 4; |
| 1974 | goto return_bad_req; |
| 1975 | } |
| 1976 | } |
Willy Tarreau | 1331766 | 2015-05-01 13:47:08 +0200 | [diff] [blame] | 1977 | else { |
| 1978 | /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */ |
| 1979 | if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn)) |
| 1980 | goto return_bad_req; |
| 1981 | } |
Willy Tarreau | 91852eb | 2015-05-01 13:26:00 +0200 | [diff] [blame] | 1982 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1983 | /* ... and check if the request is HTTP/1.1 or above */ |
| 1984 | if ((msg->sl.rq.v_l == 8) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1985 | ((req->buf->p[msg->sl.rq.v + 5] > '1') || |
| 1986 | ((req->buf->p[msg->sl.rq.v + 5] == '1') && |
| 1987 | (req->buf->p[msg->sl.rq.v + 7] >= '1')))) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 1988 | msg->flags |= HTTP_MSGF_VER_11; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1989 | |
| 1990 | /* "connection" has not been parsed yet */ |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 1991 | 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] | 1992 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1993 | /* if the frontend has "option http-use-proxy-header", we'll check if |
| 1994 | * we have what looks like a proxied connection instead of a connection, |
| 1995 | * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection. |
| 1996 | * Note that this is *not* RFC-compliant, however browsers and proxies |
| 1997 | * happen to do that despite being non-standard :-( |
| 1998 | * We consider that a request not beginning with either '/' or '*' is |
| 1999 | * a proxied connection, which covers both "scheme://location" and |
| 2000 | * CONNECT ip:port. |
| 2001 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2002 | if ((sess->fe->options2 & PR_O2_USE_PXHDR) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2003 | 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] | 2004 | txn->flags |= TX_USE_PX_CONN; |
| 2005 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2006 | /* transfer length unknown*/ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2007 | msg->flags &= ~HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2008 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2009 | /* 5: we may need to capture headers */ |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 2010 | if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap)) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2011 | capture_headers(req->buf->p, &txn->hdr_idx, |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 2012 | s->req_cap, sess->fe->req_cap); |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 2013 | |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 2014 | /* 6: determine the transfer-length according to RFC2616 #4.4, updated |
| 2015 | * by RFC7230#3.3.3 : |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2016 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 2017 | * The length of a message body is determined by one of the following |
| 2018 | * (in order of precedence): |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2019 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 2020 | * 1. Any response to a HEAD request and any response with a 1xx |
| 2021 | * (Informational), 204 (No Content), or 304 (Not Modified) status |
| 2022 | * code is always terminated by the first empty line after the |
| 2023 | * header fields, regardless of the header fields present in the |
| 2024 | * message, and thus cannot contain a message body. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2025 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 2026 | * 2. Any 2xx (Successful) response to a CONNECT request implies that |
| 2027 | * the connection will become a tunnel immediately after the empty |
| 2028 | * line that concludes the header fields. A client MUST ignore any |
| 2029 | * Content-Length or Transfer-Encoding header fields received in |
| 2030 | * such a message. |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2031 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 2032 | * 3. If a Transfer-Encoding header field is present and the chunked |
| 2033 | * transfer coding (Section 4.1) is the final encoding, the message |
| 2034 | * body length is determined by reading and decoding the chunked |
| 2035 | * data until the transfer coding indicates the data is complete. |
| 2036 | * |
| 2037 | * If a Transfer-Encoding header field is present in a response and |
| 2038 | * the chunked transfer coding is not the final encoding, the |
| 2039 | * message body length is determined by reading the connection until |
| 2040 | * it is closed by the server. If a Transfer-Encoding header field |
| 2041 | * is present in a request and the chunked transfer coding is not |
| 2042 | * the final encoding, the message body length cannot be determined |
| 2043 | * reliably; the server MUST respond with the 400 (Bad Request) |
| 2044 | * status code and then close the connection. |
| 2045 | * |
| 2046 | * If a message is received with both a Transfer-Encoding and a |
| 2047 | * Content-Length header field, the Transfer-Encoding overrides the |
| 2048 | * Content-Length. Such a message might indicate an attempt to |
| 2049 | * perform request smuggling (Section 9.5) or response splitting |
| 2050 | * (Section 9.4) and ought to be handled as an error. A sender MUST |
| 2051 | * remove the received Content-Length field prior to forwarding such |
| 2052 | * a message downstream. |
| 2053 | * |
| 2054 | * 4. If a message is received without Transfer-Encoding and with |
| 2055 | * either multiple Content-Length header fields having differing |
| 2056 | * field-values or a single Content-Length header field having an |
| 2057 | * invalid value, then the message framing is invalid and the |
| 2058 | * recipient MUST treat it as an unrecoverable error. If this is a |
| 2059 | * request message, the server MUST respond with a 400 (Bad Request) |
| 2060 | * status code and then close the connection. If this is a response |
| 2061 | * message received by a proxy, the proxy MUST close the connection |
| 2062 | * to the server, discard the received response, and send a 502 (Bad |
| 2063 | * Gateway) response to the client. If this is a response message |
| 2064 | * received by a user agent, the user agent MUST close the |
| 2065 | * connection to the server and discard the received response. |
| 2066 | * |
| 2067 | * 5. If a valid Content-Length header field is present without |
| 2068 | * Transfer-Encoding, its decimal value defines the expected message |
| 2069 | * body length in octets. If the sender closes the connection or |
| 2070 | * the recipient times out before the indicated number of octets are |
| 2071 | * received, the recipient MUST consider the message to be |
| 2072 | * incomplete and close the connection. |
| 2073 | * |
| 2074 | * 6. If this is a request message and none of the above are true, then |
| 2075 | * the message body length is zero (no message body is present). |
| 2076 | * |
| 2077 | * 7. Otherwise, this is a response message without a declared message |
| 2078 | * body length, so the message body length is determined by the |
| 2079 | * number of octets received prior to the server closing the |
| 2080 | * connection. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2081 | */ |
| 2082 | |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2083 | ctx.idx = 0; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2084 | /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */ |
Willy Tarreau | 4979d5c | 2015-05-01 10:06:30 +0200 | [diff] [blame] | 2085 | 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] | 2086 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
Christopher Faulet | be821b9 | 2017-03-30 11:21:53 +0200 | [diff] [blame] | 2087 | msg->flags |= HTTP_MSGF_TE_CHNK; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2088 | else if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | 34dfc60 | 2015-05-01 10:09:49 +0200 | [diff] [blame] | 2089 | /* chunked not last, return badreq */ |
| 2090 | goto return_bad_req; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2091 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2092 | } |
| 2093 | |
Willy Tarreau | 1c91391 | 2015-04-30 10:57:51 +0200 | [diff] [blame] | 2094 | /* Chunked requests must have their content-length removed */ |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2095 | ctx.idx = 0; |
Willy Tarreau | 1c91391 | 2015-04-30 10:57:51 +0200 | [diff] [blame] | 2096 | if (msg->flags & HTTP_MSGF_TE_CHNK) { |
| 2097 | while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) |
| 2098 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 2099 | } |
Willy Tarreau | 34dfc60 | 2015-05-01 10:09:49 +0200 | [diff] [blame] | 2100 | 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] | 2101 | signed long long cl; |
| 2102 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2103 | if (!ctx.vlen) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2104 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2105 | goto return_bad_req; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2106 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2107 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2108 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2109 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2110 | goto return_bad_req; /* parse failure */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2111 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2112 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2113 | if (cl < 0) { |
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 | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2118 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != 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; /* already specified, was different */ |
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 | |
Christopher Faulet | be821b9 | 2017-03-30 11:21:53 +0200 | [diff] [blame] | 2123 | msg->flags |= HTTP_MSGF_CNT_LEN; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 2124 | msg->body_len = msg->chunk_len = cl; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2125 | } |
| 2126 | |
Willy Tarreau | 34dfc60 | 2015-05-01 10:09:49 +0200 | [diff] [blame] | 2127 | /* even bodyless requests have a known length */ |
| 2128 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2129 | |
Willy Tarreau | 179085c | 2014-04-28 16:48:56 +0200 | [diff] [blame] | 2130 | /* Until set to anything else, the connection mode is set as Keep-Alive. It will |
| 2131 | * only change if both the request and the config reference something else. |
| 2132 | * Option httpclose by itself sets tunnel mode where headers are mangled. |
| 2133 | * However, if another mode is set, it will affect it (eg: server-close/ |
| 2134 | * keep-alive + httpclose = close). Note that we avoid to redo the same work |
| 2135 | * if FE and BE have the same settings (common). The method consists in |
| 2136 | * checking if options changed between the two calls (implying that either |
| 2137 | * one is non-null, or one of them is non-null and we are there for the first |
| 2138 | * time. |
| 2139 | */ |
| 2140 | if (!(txn->flags & TX_HDR_CONN_PRS) || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2141 | ((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] | 2142 | http_adjust_conn_mode(s, txn, msg); |
Willy Tarreau | 179085c | 2014-04-28 16:48:56 +0200 | [diff] [blame] | 2143 | |
Willy Tarreau | 9fbe18e | 2015-05-01 22:42:08 +0200 | [diff] [blame] | 2144 | /* we may have to wait for the request's body */ |
| 2145 | if ((s->be->options & PR_O_WREQ_BODY) && |
| 2146 | (msg->body_len || (msg->flags & HTTP_MSGF_TE_CHNK))) |
| 2147 | req->analysers |= AN_REQ_HTTP_BODY; |
| 2148 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2149 | /* end of job, return OK */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 2150 | req->analysers &= ~an_bit; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2151 | req->analyse_exp = TICK_ETERNITY; |
| 2152 | return 1; |
| 2153 | |
| 2154 | return_bad_req: |
| 2155 | /* We centralize bad requests processing here */ |
| 2156 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 2157 | /* we detected a parsing error. We want to archive this request |
| 2158 | * in the dedicated proxy area for later troubleshooting. |
| 2159 | */ |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 2160 | http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, sess->fe); |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2161 | } |
| 2162 | |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 2163 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2164 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 2165 | txn->status = 400; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 2166 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2167 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2168 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 2169 | if (sess->listener->counters) |
| 2170 | sess->listener->counters->failed_req++; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2171 | |
| 2172 | return_prx_cond: |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2173 | if (!(s->flags & SF_ERR_MASK)) |
| 2174 | s->flags |= SF_ERR_PRXCOND; |
| 2175 | if (!(s->flags & SF_FINST_MASK)) |
| 2176 | s->flags |= SF_FINST_R; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2177 | |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 2178 | req->analysers &= AN_REQ_FLT_END; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2179 | req->analyse_exp = TICK_ETERNITY; |
| 2180 | return 0; |
| 2181 | } |
| 2182 | |
Willy Tarreau | 4f8a83c | 2012-06-04 00:26:23 +0200 | [diff] [blame] | 2183 | |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 2184 | /* This function prepares an applet to handle the stats. It can deal with the |
| 2185 | * "100-continue" expectation, check that admin rules are met for POST requests, |
| 2186 | * and program a response message if something was unexpected. It cannot fail |
| 2187 | * 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] | 2188 | * 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] | 2189 | * 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] | 2190 | * is expected to have already assigned an appctx to the stream. |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 2191 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2192 | int http_handle_stats(struct stream *s, struct channel *req) |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 2193 | { |
| 2194 | struct stats_admin_rule *stats_admin_rule; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 2195 | struct stream_interface *si = &s->si[1]; |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 2196 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 2197 | struct http_txn *txn = s->txn; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 2198 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2199 | struct uri_auth *uri_auth = s->be->uri_auth; |
| 2200 | const char *uri, *h, *lookup; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2201 | struct appctx *appctx; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2202 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2203 | appctx = si_appctx(si); |
| 2204 | memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats)); |
| 2205 | appctx->st1 = appctx->st2 = 0; |
| 2206 | appctx->ctx.stats.st_code = STAT_STATUS_INIT; |
| 2207 | appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 2208 | 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] | 2209 | appctx->ctx.stats.flags |= STAT_CHUNKED; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2210 | |
| 2211 | uri = msg->chn->buf->p + msg->sl.rq.u; |
| 2212 | lookup = uri + uri_auth->uri_len; |
| 2213 | |
| 2214 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) { |
| 2215 | if (memcmp(h, ";up", 3) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2216 | appctx->ctx.stats.flags |= STAT_HIDE_DOWN; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2217 | break; |
| 2218 | } |
| 2219 | } |
| 2220 | |
| 2221 | if (uri_auth->refresh) { |
| 2222 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) { |
| 2223 | if (memcmp(h, ";norefresh", 10) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2224 | appctx->ctx.stats.flags |= STAT_NO_REFRESH; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2225 | break; |
| 2226 | } |
| 2227 | } |
| 2228 | } |
| 2229 | |
| 2230 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) { |
| 2231 | if (memcmp(h, ";csv", 4) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2232 | appctx->ctx.stats.flags &= ~STAT_FMT_HTML; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2233 | break; |
| 2234 | } |
| 2235 | } |
| 2236 | |
Willy Tarreau | 1e62df9 | 2016-01-11 18:57:53 +0100 | [diff] [blame] | 2237 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 6; h++) { |
| 2238 | if (memcmp(h, ";typed", 6) == 0) { |
| 2239 | appctx->ctx.stats.flags &= ~STAT_FMT_HTML; |
| 2240 | appctx->ctx.stats.flags |= STAT_FMT_TYPED; |
| 2241 | break; |
| 2242 | } |
| 2243 | } |
| 2244 | |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2245 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) { |
| 2246 | if (memcmp(h, ";st=", 4) == 0) { |
| 2247 | int i; |
| 2248 | h += 4; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2249 | appctx->ctx.stats.st_code = STAT_STATUS_UNKN; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2250 | for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) { |
| 2251 | if (strncmp(stat_status_codes[i], h, 4) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2252 | appctx->ctx.stats.st_code = i; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2253 | break; |
| 2254 | } |
| 2255 | } |
| 2256 | break; |
| 2257 | } |
| 2258 | } |
| 2259 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2260 | appctx->ctx.stats.scope_str = 0; |
| 2261 | appctx->ctx.stats.scope_len = 0; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2262 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) { |
| 2263 | if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) { |
| 2264 | int itx = 0; |
| 2265 | const char *h2; |
| 2266 | char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1]; |
| 2267 | const char *err; |
| 2268 | |
| 2269 | h += strlen(STAT_SCOPE_INPUT_NAME) + 1; |
| 2270 | h2 = h; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2271 | appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2272 | while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') { |
| 2273 | itx++; |
| 2274 | h++; |
| 2275 | } |
| 2276 | |
| 2277 | if (itx > STAT_SCOPE_TXT_MAXLEN) |
| 2278 | itx = STAT_SCOPE_TXT_MAXLEN; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2279 | appctx->ctx.stats.scope_len = itx; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2280 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2281 | /* 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] | 2282 | memcpy(scope_txt, h2, itx); |
| 2283 | scope_txt[itx] = '\0'; |
| 2284 | err = invalid_char(scope_txt); |
| 2285 | if (err) { |
| 2286 | /* bad char in search text => clear scope */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2287 | appctx->ctx.stats.scope_str = 0; |
| 2288 | appctx->ctx.stats.scope_len = 0; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2289 | } |
| 2290 | break; |
| 2291 | } |
| 2292 | } |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 2293 | |
| 2294 | /* now check whether we have some admin rules for this request */ |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 2295 | list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) { |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 2296 | int ret = 1; |
| 2297 | |
| 2298 | if (stats_admin_rule->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 2299 | 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] | 2300 | ret = acl_pass(ret); |
| 2301 | if (stats_admin_rule->cond->pol == ACL_COND_UNLESS) |
| 2302 | ret = !ret; |
| 2303 | } |
| 2304 | |
| 2305 | if (ret) { |
| 2306 | /* no rule, or the rule matches */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2307 | appctx->ctx.stats.flags |= STAT_ADMIN; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 2308 | break; |
| 2309 | } |
| 2310 | } |
| 2311 | |
| 2312 | /* Was the status page requested with a POST ? */ |
Willy Tarreau | b8cdf52 | 2015-05-29 01:09:15 +0200 | [diff] [blame] | 2313 | if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2314 | if (appctx->ctx.stats.flags & STAT_ADMIN) { |
Willy Tarreau | cfe7fdd | 2014-04-26 22:08:32 +0200 | [diff] [blame] | 2315 | /* we'll need the request body, possibly after sending 100-continue */ |
Willy Tarreau | b8cdf52 | 2015-05-29 01:09:15 +0200 | [diff] [blame] | 2316 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) |
| 2317 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2318 | appctx->st0 = STAT_HTTP_POST; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 2319 | } |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 2320 | else { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2321 | appctx->ctx.stats.st_code = STAT_STATUS_DENY; |
| 2322 | appctx->st0 = STAT_HTTP_LAST; |
de Lafond Guillaume | 88c278f | 2013-04-15 19:27:10 +0200 | [diff] [blame] | 2323 | } |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 2324 | } |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 2325 | else { |
| 2326 | /* So it was another method (GET/HEAD) */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2327 | appctx->st0 = STAT_HTTP_HEAD; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 2328 | } |
| 2329 | |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 2330 | s->task->nice = -32; /* small boost for HTTP statistics */ |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 2331 | return 1; |
| 2332 | } |
| 2333 | |
Lukas Tribus | 67db8df | 2013-06-23 17:37:13 +0200 | [diff] [blame] | 2334 | /* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets |
| 2335 | * (as per RFC3260 #4 and BCP37 #4.2 and #5.2). |
| 2336 | */ |
Vincent Bernat | 6e61589 | 2016-05-18 16:17:44 +0200 | [diff] [blame] | 2337 | 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] | 2338 | { |
| 2339 | #ifdef IP_TOS |
Vincent Bernat | 6e61589 | 2016-05-18 16:17:44 +0200 | [diff] [blame] | 2340 | if (from->ss_family == AF_INET) |
Lukas Tribus | 67db8df | 2013-06-23 17:37:13 +0200 | [diff] [blame] | 2341 | setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)); |
| 2342 | #endif |
| 2343 | #ifdef IPV6_TCLASS |
Vincent Bernat | 6e61589 | 2016-05-18 16:17:44 +0200 | [diff] [blame] | 2344 | if (from->ss_family == AF_INET6) { |
| 2345 | if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)from)->sin6_addr)) |
Lukas Tribus | 67db8df | 2013-06-23 17:37:13 +0200 | [diff] [blame] | 2346 | /* v4-mapped addresses need IP_TOS */ |
| 2347 | setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)); |
| 2348 | else |
| 2349 | setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos)); |
| 2350 | } |
| 2351 | #endif |
| 2352 | } |
| 2353 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2354 | int http_transform_header_str(struct stream* s, struct http_msg *msg, |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 2355 | const char* name, unsigned int name_len, |
| 2356 | const char *str, struct my_regex *re, |
| 2357 | int action) |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 2358 | { |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 2359 | struct hdr_ctx ctx; |
| 2360 | char *buf = msg->chn->buf->p; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 2361 | struct hdr_idx *idx = &s->txn->hdr_idx; |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 2362 | int (*http_find_hdr_func)(const char *name, int len, char *sol, |
| 2363 | struct hdr_idx *idx, struct hdr_ctx *ctx); |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 2364 | struct chunk *output = get_trash_chunk(); |
| 2365 | |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 2366 | ctx.idx = 0; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 2367 | |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 2368 | /* Choose the header browsing function. */ |
| 2369 | switch (action) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2370 | case ACT_HTTP_REPLACE_VAL: |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 2371 | http_find_hdr_func = http_find_header2; |
| 2372 | break; |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2373 | case ACT_HTTP_REPLACE_HDR: |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 2374 | http_find_hdr_func = http_find_full_header2; |
| 2375 | break; |
| 2376 | default: /* impossible */ |
| 2377 | return -1; |
| 2378 | } |
| 2379 | |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 2380 | while (http_find_hdr_func(name, name_len, buf, idx, &ctx)) { |
| 2381 | struct hdr_idx_elem *hdr = idx->v + ctx.idx; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 2382 | int delta; |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 2383 | char *val = ctx.line + ctx.val; |
| 2384 | char* val_end = val + ctx.vlen; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 2385 | |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 2386 | if (!regex_exec_match2(re, val, val_end-val, MAX_MATCH, pmatch, 0)) |
| 2387 | continue; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 2388 | |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 2389 | output->len = exp_replace(output->str, output->size, val, str, pmatch); |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 2390 | if (output->len == -1) |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 2391 | return -1; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 2392 | |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 2393 | 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] | 2394 | |
| 2395 | hdr->len += delta; |
| 2396 | http_msg_move_end(msg, delta); |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 2397 | |
| 2398 | /* Adjust the length of the current value of the index. */ |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 2399 | ctx.vlen += delta; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 2400 | } |
| 2401 | |
| 2402 | return 0; |
| 2403 | } |
| 2404 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2405 | static int http_transform_header(struct stream* s, struct http_msg *msg, |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 2406 | const char* name, unsigned int name_len, |
| 2407 | struct list *fmt, struct my_regex *re, |
| 2408 | int action) |
| 2409 | { |
Christopher Faulet | 07a0fec | 2017-02-08 12:17:07 +0100 | [diff] [blame] | 2410 | struct chunk *replace; |
| 2411 | int ret = -1; |
| 2412 | |
| 2413 | replace = alloc_trash_chunk(); |
| 2414 | if (!replace) |
| 2415 | goto leave; |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 2416 | |
| 2417 | replace->len = build_logline(s, replace->str, replace->size, fmt); |
| 2418 | if (replace->len >= replace->size - 1) |
Christopher Faulet | 07a0fec | 2017-02-08 12:17:07 +0100 | [diff] [blame] | 2419 | goto leave; |
| 2420 | |
| 2421 | 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] | 2422 | |
Christopher Faulet | 07a0fec | 2017-02-08 12:17:07 +0100 | [diff] [blame] | 2423 | leave: |
| 2424 | free_trash_chunk(replace); |
| 2425 | return ret; |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 2426 | } |
| 2427 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2428 | /* Executes the http-request rules <rules> for stream <s>, proxy <px> and |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 2429 | * transaction <txn>. Returns the verdict of the first rule that prevents |
| 2430 | * further processing of the request (auth, deny, ...), and defaults to |
| 2431 | * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or |
| 2432 | * 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] | 2433 | * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL |
| 2434 | * and a deny/tarpit rule is matched, it will be filled with this rule's deny |
| 2435 | * status. |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2436 | */ |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 2437 | enum rule_result |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 2438 | 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] | 2439 | { |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 2440 | struct session *sess = strm_sess(s); |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 2441 | struct http_txn *txn = s->txn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2442 | struct connection *cli_conn; |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 2443 | struct act_rule *rule; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 2444 | struct hdr_ctx ctx; |
Willy Tarreau | ae3c010 | 2014-04-28 23:22:08 +0200 | [diff] [blame] | 2445 | const char *auth_realm; |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 2446 | int act_flags = 0; |
Thierry Fournier | 4b788f7 | 2016-06-01 13:35:36 +0200 | [diff] [blame] | 2447 | int len; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2448 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 2449 | /* If "the current_rule_list" match the executed rule list, we are in |
| 2450 | * resume condition. If a resume is needed it is always in the action |
| 2451 | * and never in the ACL or converters. In this case, we initialise the |
| 2452 | * current rule, and go to the action execution point. |
| 2453 | */ |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 2454 | if (s->current_rule) { |
| 2455 | rule = s->current_rule; |
| 2456 | s->current_rule = NULL; |
| 2457 | if (s->current_rule_list == rules) |
| 2458 | goto resume_execution; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 2459 | } |
| 2460 | s->current_rule_list = rules; |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 2461 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2462 | list_for_each_entry(rule, rules, list) { |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2463 | |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 2464 | /* check optional condition */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2465 | if (rule->cond) { |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 2466 | int ret; |
| 2467 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 2468 | 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] | 2469 | ret = acl_pass(ret); |
| 2470 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2471 | if (rule->cond->pol == ACL_COND_UNLESS) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2472 | ret = !ret; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 2473 | |
| 2474 | if (!ret) /* condition not matched */ |
| 2475 | continue; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2476 | } |
| 2477 | |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 2478 | act_flags |= ACT_FLAG_FIRST; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 2479 | resume_execution: |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 2480 | switch (rule->action) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2481 | case ACT_ACTION_ALLOW: |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 2482 | return HTTP_RULE_RES_STOP; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 2483 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2484 | case ACT_ACTION_DENY: |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 2485 | if (deny_status) |
| 2486 | *deny_status = rule->deny_status; |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 2487 | return HTTP_RULE_RES_DENY; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 2488 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2489 | case ACT_HTTP_REQ_TARPIT: |
Willy Tarreau | ccbcc37 | 2012-12-27 12:37:57 +0100 | [diff] [blame] | 2490 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 2491 | if (deny_status) |
| 2492 | *deny_status = rule->deny_status; |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 2493 | return HTTP_RULE_RES_DENY; |
Willy Tarreau | ccbcc37 | 2012-12-27 12:37:57 +0100 | [diff] [blame] | 2494 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2495 | case ACT_HTTP_REQ_AUTH: |
Willy Tarreau | ae3c010 | 2014-04-28 23:22:08 +0200 | [diff] [blame] | 2496 | /* Auth might be performed on regular http-req rules as well as on stats */ |
| 2497 | auth_realm = rule->arg.auth.realm; |
| 2498 | if (!auth_realm) { |
| 2499 | if (px->uri_auth && rules == &px->uri_auth->http_req_rules) |
| 2500 | auth_realm = STATS_DEFAULT_REALM; |
| 2501 | else |
| 2502 | auth_realm = px->id; |
| 2503 | } |
| 2504 | /* send 401/407 depending on whether we use a proxy or not. We still |
| 2505 | * count one error, because normal browsing won't significantly |
| 2506 | * increase the counter but brute force attempts will. |
| 2507 | */ |
| 2508 | chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm); |
| 2509 | txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401; |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 2510 | http_reply_and_close(s, txn->status, &trash); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2511 | stream_inc_http_err_ctr(s); |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 2512 | return HTTP_RULE_RES_ABRT; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 2513 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2514 | case ACT_HTTP_REDIR: |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 2515 | if (!http_apply_redirect_rule(rule->arg.redir, s, txn)) |
| 2516 | return HTTP_RULE_RES_BADREQ; |
| 2517 | return HTTP_RULE_RES_DONE; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 2518 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2519 | case ACT_HTTP_SET_NICE: |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 2520 | s->task->nice = rule->arg.nice; |
| 2521 | break; |
| 2522 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2523 | case ACT_HTTP_SET_TOS: |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 2524 | if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 2525 | 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] | 2526 | break; |
| 2527 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2528 | case ACT_HTTP_SET_MARK: |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 2529 | #ifdef SO_MARK |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 2530 | if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 2531 | 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] | 2532 | #endif |
| 2533 | break; |
| 2534 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2535 | case ACT_HTTP_SET_LOGL: |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 2536 | s->logs.level = rule->arg.loglevel; |
| 2537 | break; |
| 2538 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2539 | case ACT_HTTP_REPLACE_HDR: |
| 2540 | case ACT_HTTP_REPLACE_VAL: |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 2541 | if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name, |
| 2542 | rule->arg.hdr_add.name_len, |
| 2543 | &rule->arg.hdr_add.fmt, |
| 2544 | &rule->arg.hdr_add.re, rule->action)) |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 2545 | return HTTP_RULE_RES_BADREQ; |
| 2546 | break; |
| 2547 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2548 | case ACT_HTTP_DEL_HDR: |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 2549 | ctx.idx = 0; |
| 2550 | /* remove all occurrences of the header */ |
| 2551 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 2552 | txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 2553 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 2554 | } |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 2555 | break; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 2556 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2557 | case ACT_HTTP_SET_HDR: |
| 2558 | case ACT_HTTP_ADD_HDR: |
Thierry Fournier | 4b788f7 | 2016-06-01 13:35:36 +0200 | [diff] [blame] | 2559 | /* The scope of the trash buffer must be limited to this function. The |
| 2560 | * build_logline() function can execute a lot of other function which |
| 2561 | * can use the trash buffer. So for limiting the scope of this global |
| 2562 | * buffer, we build first the header value using build_logline, and |
| 2563 | * after we store the header name. |
| 2564 | */ |
| 2565 | len = rule->arg.hdr_add.name_len + 2, |
| 2566 | len += build_logline(s, trash.str + len, trash.size - len, &rule->arg.hdr_add.fmt); |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 2567 | memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len); |
Thierry Fournier | 4b788f7 | 2016-06-01 13:35:36 +0200 | [diff] [blame] | 2568 | trash.str[rule->arg.hdr_add.name_len] = ':'; |
| 2569 | trash.str[rule->arg.hdr_add.name_len + 1] = ' '; |
| 2570 | trash.len = len; |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 2571 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2572 | if (rule->action == ACT_HTTP_SET_HDR) { |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 2573 | /* remove all occurrences of the header */ |
| 2574 | ctx.idx = 0; |
| 2575 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 2576 | txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 2577 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
| 2578 | } |
| 2579 | } |
| 2580 | |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 2581 | http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len); |
| 2582 | break; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2583 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2584 | case ACT_HTTP_DEL_ACL: |
| 2585 | case ACT_HTTP_DEL_MAP: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2586 | struct pat_ref *ref; |
| 2587 | char *key; |
| 2588 | int len; |
| 2589 | |
| 2590 | /* collect reference */ |
| 2591 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 2592 | if (!ref) |
| 2593 | continue; |
| 2594 | |
| 2595 | /* collect key */ |
| 2596 | len = build_logline(s, trash.str, trash.size, &rule->arg.map.key); |
| 2597 | key = trash.str; |
| 2598 | key[len] = '\0'; |
| 2599 | |
| 2600 | /* perform update */ |
| 2601 | /* returned code: 1=ok, 0=ko */ |
| 2602 | pat_ref_delete(ref, key); |
| 2603 | |
| 2604 | break; |
| 2605 | } |
| 2606 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2607 | case ACT_HTTP_ADD_ACL: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2608 | struct pat_ref *ref; |
| 2609 | char *key; |
| 2610 | struct chunk *trash_key; |
| 2611 | int len; |
| 2612 | |
| 2613 | trash_key = get_trash_chunk(); |
| 2614 | |
| 2615 | /* collect reference */ |
| 2616 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 2617 | if (!ref) |
| 2618 | continue; |
| 2619 | |
| 2620 | /* collect key */ |
| 2621 | len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key); |
| 2622 | key = trash_key->str; |
| 2623 | key[len] = '\0'; |
| 2624 | |
| 2625 | /* perform update */ |
| 2626 | /* add entry only if it does not already exist */ |
| 2627 | if (pat_ref_find_elt(ref, key) == NULL) |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 2628 | pat_ref_add(ref, key, NULL, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2629 | |
| 2630 | break; |
| 2631 | } |
| 2632 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2633 | case ACT_HTTP_SET_MAP: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2634 | struct pat_ref *ref; |
| 2635 | char *key, *value; |
| 2636 | struct chunk *trash_key, *trash_value; |
| 2637 | int len; |
| 2638 | |
| 2639 | trash_key = get_trash_chunk(); |
| 2640 | trash_value = get_trash_chunk(); |
| 2641 | |
| 2642 | /* collect reference */ |
| 2643 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 2644 | if (!ref) |
| 2645 | continue; |
| 2646 | |
| 2647 | /* collect key */ |
| 2648 | len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key); |
| 2649 | key = trash_key->str; |
| 2650 | key[len] = '\0'; |
| 2651 | |
| 2652 | /* collect value */ |
| 2653 | len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value); |
| 2654 | value = trash_value->str; |
| 2655 | value[len] = '\0'; |
| 2656 | |
| 2657 | /* perform update */ |
| 2658 | if (pat_ref_find_elt(ref, key) != NULL) |
| 2659 | /* update entry if it exists */ |
| 2660 | pat_ref_set(ref, key, value, NULL); |
| 2661 | else |
| 2662 | /* insert a new entry */ |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 2663 | pat_ref_add(ref, key, value, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2664 | |
| 2665 | break; |
| 2666 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 2667 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 2668 | case ACT_CUSTOM: |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 2669 | if ((px->options & PR_O_ABRT_CLOSE) && (s->req.flags & (CF_SHUTR|CF_READ_NULL|CF_READ_ERROR))) |
| 2670 | act_flags |= ACT_FLAG_FINAL; |
Willy Tarreau | 3945868 | 2015-09-27 10:33:15 +0200 | [diff] [blame] | 2671 | |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 2672 | switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) { |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 2673 | case ACT_RET_ERR: |
| 2674 | case ACT_RET_CONT: |
| 2675 | break; |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 2676 | case ACT_RET_STOP: |
| 2677 | return HTTP_RULE_RES_DONE; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 2678 | case ACT_RET_YIELD: |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 2679 | s->current_rule = rule; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 2680 | return HTTP_RULE_RES_YIELD; |
| 2681 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 2682 | break; |
| 2683 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2684 | case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX: |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 2685 | /* Note: only the first valid tracking parameter of each |
| 2686 | * applies. |
| 2687 | */ |
| 2688 | |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 2689 | if (stkctr_entry(&s->stkctr[http_trk_idx(rule->action)]) == NULL) { |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 2690 | struct stktable *t; |
| 2691 | struct stksess *ts; |
| 2692 | struct stktable_key *key; |
| 2693 | void *ptr; |
| 2694 | |
Thierry FOURNIER | 5ec63e0 | 2015-08-04 09:09:48 +0200 | [diff] [blame] | 2695 | t = rule->arg.trk_ctr.table.t; |
| 2696 | 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] | 2697 | |
| 2698 | if (key && (ts = stktable_get_entry(t, key))) { |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 2699 | stream_track_stkctr(&s->stkctr[http_trk_idx(rule->action)], t, ts); |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 2700 | |
| 2701 | /* let's count a new HTTP request as it's the first time we do it */ |
| 2702 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT); |
| 2703 | if (ptr) |
| 2704 | stktable_data_cast(ptr, http_req_cnt)++; |
| 2705 | |
| 2706 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE); |
| 2707 | if (ptr) |
| 2708 | update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate), |
| 2709 | t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1); |
| 2710 | |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 2711 | stkctr_set_flags(&s->stkctr[http_trk_idx(rule->action)], STKCTR_TRACK_CONTENT); |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 2712 | if (sess->fe != s->be) |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 2713 | stkctr_set_flags(&s->stkctr[http_trk_idx(rule->action)], STKCTR_TRACK_BACKEND); |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 2714 | } |
| 2715 | } |
Adis Nezirovic | 2fbcafc | 2015-07-06 15:44:30 +0200 | [diff] [blame] | 2716 | break; |
| 2717 | |
Thierry FOURNIER | 22e4901 | 2015-08-05 19:13:48 +0200 | [diff] [blame] | 2718 | /* other flags exists, but normaly, they never be matched. */ |
| 2719 | default: |
| 2720 | break; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2721 | } |
| 2722 | } |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 2723 | |
| 2724 | /* we reached the end of the rules, nothing to report */ |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 2725 | return HTTP_RULE_RES_CONT; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2726 | } |
| 2727 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2728 | |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 2729 | /* Executes the http-response rules <rules> for stream <s> and proxy <px>. It |
| 2730 | * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP, |
| 2731 | * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT |
| 2732 | * is returned, the process can continue the evaluation of next rule list. If |
| 2733 | * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ |
| 2734 | * is returned, it means the operation could not be processed and a server error |
| 2735 | * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a |
| 2736 | * deny rule. If *YIELD is returned, the caller must call again the function |
| 2737 | * with the same context. |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2738 | */ |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 2739 | static enum rule_result |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 2740 | 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] | 2741 | { |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 2742 | struct session *sess = strm_sess(s); |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 2743 | struct http_txn *txn = s->txn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2744 | struct connection *cli_conn; |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 2745 | struct act_rule *rule; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2746 | struct hdr_ctx ctx; |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 2747 | int act_flags = 0; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2748 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 2749 | /* If "the current_rule_list" match the executed rule list, we are in |
| 2750 | * resume condition. If a resume is needed it is always in the action |
| 2751 | * and never in the ACL or converters. In this case, we initialise the |
| 2752 | * current rule, and go to the action execution point. |
| 2753 | */ |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 2754 | if (s->current_rule) { |
| 2755 | rule = s->current_rule; |
| 2756 | s->current_rule = NULL; |
| 2757 | if (s->current_rule_list == rules) |
| 2758 | goto resume_execution; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 2759 | } |
| 2760 | s->current_rule_list = rules; |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 2761 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2762 | list_for_each_entry(rule, rules, list) { |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2763 | |
| 2764 | /* check optional condition */ |
| 2765 | if (rule->cond) { |
| 2766 | int ret; |
| 2767 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 2768 | 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] | 2769 | ret = acl_pass(ret); |
| 2770 | |
| 2771 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 2772 | ret = !ret; |
| 2773 | |
| 2774 | if (!ret) /* condition not matched */ |
| 2775 | continue; |
| 2776 | } |
| 2777 | |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 2778 | act_flags |= ACT_FLAG_FIRST; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 2779 | resume_execution: |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2780 | switch (rule->action) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2781 | case ACT_ACTION_ALLOW: |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 2782 | return HTTP_RULE_RES_STOP; /* "allow" rules are OK */ |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2783 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2784 | case ACT_ACTION_DENY: |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2785 | txn->flags |= TX_SVDENY; |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 2786 | return HTTP_RULE_RES_STOP; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2787 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2788 | case ACT_HTTP_SET_NICE: |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 2789 | s->task->nice = rule->arg.nice; |
| 2790 | break; |
| 2791 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2792 | case ACT_HTTP_SET_TOS: |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 2793 | if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 2794 | 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] | 2795 | break; |
| 2796 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2797 | case ACT_HTTP_SET_MARK: |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 2798 | #ifdef SO_MARK |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 2799 | if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 2800 | 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] | 2801 | #endif |
| 2802 | break; |
| 2803 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2804 | case ACT_HTTP_SET_LOGL: |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 2805 | s->logs.level = rule->arg.loglevel; |
| 2806 | break; |
| 2807 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2808 | case ACT_HTTP_REPLACE_HDR: |
| 2809 | case ACT_HTTP_REPLACE_VAL: |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 2810 | if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name, |
| 2811 | rule->arg.hdr_add.name_len, |
| 2812 | &rule->arg.hdr_add.fmt, |
| 2813 | &rule->arg.hdr_add.re, rule->action)) |
Christopher Faulet | cdade94 | 2017-02-08 12:41:31 +0100 | [diff] [blame] | 2814 | return HTTP_RULE_RES_BADREQ; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 2815 | break; |
| 2816 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2817 | case ACT_HTTP_DEL_HDR: |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2818 | ctx.idx = 0; |
| 2819 | /* remove all occurrences of the header */ |
| 2820 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 2821 | txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 2822 | http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx); |
| 2823 | } |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 2824 | break; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2825 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2826 | case ACT_HTTP_SET_HDR: |
| 2827 | case ACT_HTTP_ADD_HDR: |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2828 | chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name); |
| 2829 | memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len); |
| 2830 | trash.len = rule->arg.hdr_add.name_len; |
| 2831 | trash.str[trash.len++] = ':'; |
| 2832 | trash.str[trash.len++] = ' '; |
| 2833 | trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt); |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 2834 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2835 | if (rule->action == ACT_HTTP_SET_HDR) { |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 2836 | /* remove all occurrences of the header */ |
| 2837 | ctx.idx = 0; |
| 2838 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 2839 | txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 2840 | http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx); |
| 2841 | } |
| 2842 | } |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2843 | http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len); |
| 2844 | break; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2845 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2846 | case ACT_HTTP_DEL_ACL: |
| 2847 | case ACT_HTTP_DEL_MAP: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2848 | struct pat_ref *ref; |
| 2849 | char *key; |
| 2850 | int len; |
| 2851 | |
| 2852 | /* collect reference */ |
| 2853 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 2854 | if (!ref) |
| 2855 | continue; |
| 2856 | |
| 2857 | /* collect key */ |
| 2858 | len = build_logline(s, trash.str, trash.size, &rule->arg.map.key); |
| 2859 | key = trash.str; |
| 2860 | key[len] = '\0'; |
| 2861 | |
| 2862 | /* perform update */ |
| 2863 | /* returned code: 1=ok, 0=ko */ |
| 2864 | pat_ref_delete(ref, key); |
| 2865 | |
| 2866 | break; |
| 2867 | } |
| 2868 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2869 | case ACT_HTTP_ADD_ACL: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2870 | struct pat_ref *ref; |
| 2871 | char *key; |
| 2872 | struct chunk *trash_key; |
| 2873 | int len; |
| 2874 | |
| 2875 | trash_key = get_trash_chunk(); |
| 2876 | |
| 2877 | /* collect reference */ |
| 2878 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 2879 | if (!ref) |
| 2880 | continue; |
| 2881 | |
| 2882 | /* collect key */ |
| 2883 | len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key); |
| 2884 | key = trash_key->str; |
| 2885 | key[len] = '\0'; |
| 2886 | |
| 2887 | /* perform update */ |
| 2888 | /* check if the entry already exists */ |
| 2889 | if (pat_ref_find_elt(ref, key) == NULL) |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 2890 | pat_ref_add(ref, key, NULL, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2891 | |
| 2892 | break; |
| 2893 | } |
| 2894 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2895 | case ACT_HTTP_SET_MAP: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2896 | struct pat_ref *ref; |
| 2897 | char *key, *value; |
| 2898 | struct chunk *trash_key, *trash_value; |
| 2899 | int len; |
| 2900 | |
| 2901 | trash_key = get_trash_chunk(); |
| 2902 | trash_value = get_trash_chunk(); |
| 2903 | |
| 2904 | /* collect reference */ |
| 2905 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 2906 | if (!ref) |
| 2907 | continue; |
| 2908 | |
| 2909 | /* collect key */ |
| 2910 | len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key); |
| 2911 | key = trash_key->str; |
| 2912 | key[len] = '\0'; |
| 2913 | |
| 2914 | /* collect value */ |
| 2915 | len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value); |
| 2916 | value = trash_value->str; |
| 2917 | value[len] = '\0'; |
| 2918 | |
| 2919 | /* perform update */ |
| 2920 | if (pat_ref_find_elt(ref, key) != NULL) |
| 2921 | /* update entry if it exists */ |
| 2922 | pat_ref_set(ref, key, value, NULL); |
| 2923 | else |
| 2924 | /* insert a new entry */ |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 2925 | pat_ref_add(ref, key, value, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2926 | |
| 2927 | break; |
| 2928 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 2929 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2930 | case ACT_HTTP_REDIR: |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 2931 | if (!http_apply_redirect_rule(rule->arg.redir, s, txn)) |
| 2932 | return HTTP_RULE_RES_BADREQ; |
| 2933 | return HTTP_RULE_RES_DONE; |
| 2934 | |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 2935 | case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX: |
| 2936 | /* Note: only the first valid tracking parameter of each |
| 2937 | * applies. |
| 2938 | */ |
| 2939 | |
| 2940 | if (stkctr_entry(&s->stkctr[http_trk_idx(rule->action)]) == NULL) { |
| 2941 | struct stktable *t; |
| 2942 | struct stksess *ts; |
| 2943 | struct stktable_key *key; |
| 2944 | void *ptr; |
| 2945 | |
| 2946 | t = rule->arg.trk_ctr.table.t; |
| 2947 | key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_RES | SMP_OPT_FINAL, rule->arg.trk_ctr.expr, NULL); |
| 2948 | |
| 2949 | if (key && (ts = stktable_get_entry(t, key))) { |
| 2950 | stream_track_stkctr(&s->stkctr[http_trk_idx(rule->action)], t, ts); |
| 2951 | |
| 2952 | /* let's count a new HTTP request as it's the first time we do it */ |
| 2953 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT); |
| 2954 | if (ptr) |
| 2955 | stktable_data_cast(ptr, http_req_cnt)++; |
| 2956 | |
| 2957 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE); |
| 2958 | if (ptr) |
| 2959 | update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate), |
| 2960 | t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1); |
| 2961 | |
| 2962 | stkctr_set_flags(&s->stkctr[http_trk_idx(rule->action)], STKCTR_TRACK_CONTENT); |
| 2963 | if (sess->fe != s->be) |
| 2964 | stkctr_set_flags(&s->stkctr[http_trk_idx(rule->action)], STKCTR_TRACK_BACKEND); |
| 2965 | |
| 2966 | /* When the client triggers a 4xx from the server, it's most often due |
| 2967 | * to a missing object or permission. These events should be tracked |
| 2968 | * because if they happen often, it may indicate a brute force or a |
| 2969 | * vulnerability scan. Normally this is done when receiving the response |
| 2970 | * but here we're tracking after this ought to have been done so we have |
| 2971 | * to do it on purpose. |
| 2972 | */ |
Willy Tarreau | 3146a4c | 2016-07-26 15:22:33 +0200 | [diff] [blame] | 2973 | if ((unsigned)(txn->status - 400) < 100) { |
| 2974 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT); |
| 2975 | if (ptr) |
| 2976 | stktable_data_cast(ptr, http_err_cnt)++; |
| 2977 | |
| 2978 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE); |
| 2979 | if (ptr) |
| 2980 | update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate), |
| 2981 | t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1); |
| 2982 | } |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 2983 | } |
| 2984 | } |
| 2985 | break; |
| 2986 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 2987 | case ACT_CUSTOM: |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 2988 | if ((px->options & PR_O_ABRT_CLOSE) && (s->req.flags & (CF_SHUTR|CF_READ_NULL|CF_READ_ERROR))) |
| 2989 | act_flags |= ACT_FLAG_FINAL; |
Willy Tarreau | 3945868 | 2015-09-27 10:33:15 +0200 | [diff] [blame] | 2990 | |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 2991 | switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) { |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 2992 | case ACT_RET_ERR: |
| 2993 | case ACT_RET_CONT: |
| 2994 | break; |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 2995 | case ACT_RET_STOP: |
| 2996 | return HTTP_RULE_RES_STOP; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 2997 | case ACT_RET_YIELD: |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 2998 | s->current_rule = rule; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 2999 | return HTTP_RULE_RES_YIELD; |
| 3000 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 3001 | break; |
| 3002 | |
Thierry FOURNIER | 22e4901 | 2015-08-05 19:13:48 +0200 | [diff] [blame] | 3003 | /* other flags exists, but normaly, they never be matched. */ |
| 3004 | default: |
| 3005 | break; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3006 | } |
| 3007 | } |
| 3008 | |
| 3009 | /* we reached the end of the rules, nothing to report */ |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 3010 | return HTTP_RULE_RES_CONT; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3011 | } |
| 3012 | |
| 3013 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3014 | /* Perform an HTTP redirect based on the information in <rule>. The function |
| 3015 | * returns non-zero on success, or zero in case of a, irrecoverable error such |
| 3016 | * as too large a request to build a valid response. |
| 3017 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3018 | 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] | 3019 | { |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 3020 | struct http_msg *req = &txn->req; |
| 3021 | struct http_msg *res = &txn->rsp; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3022 | const char *msg_fmt; |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3023 | struct chunk *chunk; |
| 3024 | int ret = 0; |
| 3025 | |
| 3026 | chunk = alloc_trash_chunk(); |
| 3027 | if (!chunk) |
| 3028 | goto leave; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3029 | |
| 3030 | /* build redirect message */ |
| 3031 | switch(rule->code) { |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 3032 | case 308: |
| 3033 | msg_fmt = HTTP_308; |
| 3034 | break; |
| 3035 | case 307: |
| 3036 | msg_fmt = HTTP_307; |
| 3037 | break; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3038 | case 303: |
| 3039 | msg_fmt = HTTP_303; |
| 3040 | break; |
| 3041 | case 301: |
| 3042 | msg_fmt = HTTP_301; |
| 3043 | break; |
| 3044 | case 302: |
| 3045 | default: |
| 3046 | msg_fmt = HTTP_302; |
| 3047 | break; |
| 3048 | } |
| 3049 | |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3050 | if (unlikely(!chunk_strcpy(chunk, msg_fmt))) |
| 3051 | goto leave; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3052 | |
| 3053 | switch(rule->type) { |
| 3054 | case REDIRECT_TYPE_SCHEME: { |
| 3055 | const char *path; |
| 3056 | const char *host; |
| 3057 | struct hdr_ctx ctx; |
| 3058 | int pathlen; |
| 3059 | int hostlen; |
| 3060 | |
| 3061 | host = ""; |
| 3062 | hostlen = 0; |
| 3063 | ctx.idx = 0; |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 3064 | 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] | 3065 | host = ctx.line + ctx.val; |
| 3066 | hostlen = ctx.vlen; |
| 3067 | } |
| 3068 | |
| 3069 | path = http_get_path(txn); |
| 3070 | /* build message using path */ |
| 3071 | if (path) { |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 3072 | 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] | 3073 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 3074 | int qs = 0; |
| 3075 | while (qs < pathlen) { |
| 3076 | if (path[qs] == '?') { |
| 3077 | pathlen = qs; |
| 3078 | break; |
| 3079 | } |
| 3080 | qs++; |
| 3081 | } |
| 3082 | } |
| 3083 | } else { |
| 3084 | path = "/"; |
| 3085 | pathlen = 1; |
| 3086 | } |
| 3087 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3088 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
| 3089 | /* check if we can add scheme + "://" + host + path */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3090 | if (chunk->len + rule->rdr_len + 3 + hostlen + pathlen > chunk->size - 4) |
| 3091 | goto leave; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3092 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3093 | /* add scheme */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3094 | memcpy(chunk->str + chunk->len, rule->rdr_str, rule->rdr_len); |
| 3095 | chunk->len += rule->rdr_len; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3096 | } |
| 3097 | else { |
| 3098 | /* add scheme with executing log format */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3099 | 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] | 3100 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3101 | /* check if we can add scheme + "://" + host + path */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3102 | if (chunk->len + 3 + hostlen + pathlen > chunk->size - 4) |
| 3103 | goto leave; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3104 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3105 | /* add "://" */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3106 | memcpy(chunk->str + chunk->len, "://", 3); |
| 3107 | chunk->len += 3; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3108 | |
| 3109 | /* add host */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3110 | memcpy(chunk->str + chunk->len, host, hostlen); |
| 3111 | chunk->len += hostlen; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3112 | |
| 3113 | /* add path */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3114 | memcpy(chunk->str + chunk->len, path, pathlen); |
| 3115 | chunk->len += pathlen; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3116 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3117 | /* 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] | 3118 | if (chunk->len && chunk->str[chunk->len - 1] != '/' && |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3119 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3120 | if (chunk->len > chunk->size - 5) |
| 3121 | goto leave; |
| 3122 | chunk->str[chunk->len] = '/'; |
| 3123 | chunk->len++; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3124 | } |
| 3125 | |
| 3126 | break; |
| 3127 | } |
| 3128 | case REDIRECT_TYPE_PREFIX: { |
| 3129 | const char *path; |
| 3130 | int pathlen; |
| 3131 | |
| 3132 | path = http_get_path(txn); |
| 3133 | /* build message using path */ |
| 3134 | if (path) { |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 3135 | 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] | 3136 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 3137 | int qs = 0; |
| 3138 | while (qs < pathlen) { |
| 3139 | if (path[qs] == '?') { |
| 3140 | pathlen = qs; |
| 3141 | break; |
| 3142 | } |
| 3143 | qs++; |
| 3144 | } |
| 3145 | } |
| 3146 | } else { |
| 3147 | path = "/"; |
| 3148 | pathlen = 1; |
| 3149 | } |
| 3150 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3151 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3152 | if (chunk->len + rule->rdr_len + pathlen > chunk->size - 4) |
| 3153 | goto leave; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3154 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3155 | /* add prefix. Note that if prefix == "/", we don't want to |
| 3156 | * add anything, otherwise it makes it hard for the user to |
| 3157 | * configure a self-redirection. |
| 3158 | */ |
| 3159 | if (rule->rdr_len != 1 || *rule->rdr_str != '/') { |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3160 | memcpy(chunk->str + chunk->len, rule->rdr_str, rule->rdr_len); |
| 3161 | chunk->len += rule->rdr_len; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3162 | } |
| 3163 | } |
| 3164 | else { |
| 3165 | /* add prefix with executing log format */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3166 | 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] | 3167 | |
| 3168 | /* Check length */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3169 | if (chunk->len + pathlen > chunk->size - 4) |
| 3170 | goto leave; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3171 | } |
| 3172 | |
| 3173 | /* add path */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3174 | memcpy(chunk->str + chunk->len, path, pathlen); |
| 3175 | chunk->len += pathlen; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3176 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3177 | /* 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] | 3178 | if (chunk->len && chunk->str[chunk->len - 1] != '/' && |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3179 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3180 | if (chunk->len > chunk->size - 5) |
| 3181 | goto leave; |
| 3182 | chunk->str[chunk->len] = '/'; |
| 3183 | chunk->len++; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3184 | } |
| 3185 | |
| 3186 | break; |
| 3187 | } |
| 3188 | case REDIRECT_TYPE_LOCATION: |
| 3189 | default: |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3190 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3191 | if (chunk->len + rule->rdr_len > chunk->size - 4) |
| 3192 | goto leave; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3193 | |
| 3194 | /* add location */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3195 | memcpy(chunk->str + chunk->len, rule->rdr_str, rule->rdr_len); |
| 3196 | chunk->len += rule->rdr_len; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3197 | } |
| 3198 | else { |
| 3199 | /* add location with executing log format */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3200 | 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] | 3201 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3202 | /* Check left length */ |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3203 | if (chunk->len > chunk->size - 4) |
| 3204 | goto leave; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 3205 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3206 | break; |
| 3207 | } |
| 3208 | |
| 3209 | if (rule->cookie_len) { |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3210 | memcpy(chunk->str + chunk->len, "\r\nSet-Cookie: ", 14); |
| 3211 | chunk->len += 14; |
| 3212 | memcpy(chunk->str + chunk->len, rule->cookie_str, rule->cookie_len); |
| 3213 | chunk->len += rule->cookie_len; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3214 | } |
| 3215 | |
Willy Tarreau | 19b1412 | 2017-02-28 09:48:11 +0100 | [diff] [blame] | 3216 | /* add end of headers and the keep-alive/close status. */ |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3217 | txn->status = rule->code; |
| 3218 | /* let's log the request time */ |
| 3219 | s->logs.tv_request = now; |
| 3220 | |
Christopher Faulet | be821b9 | 2017-03-30 11:21:53 +0200 | [diff] [blame] | 3221 | 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] | 3222 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL || |
| 3223 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) { |
| 3224 | /* keep-alive possible */ |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 3225 | if (!(req->flags & HTTP_MSGF_VER_11)) { |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3226 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3227 | memcpy(chunk->str + chunk->len, "\r\nProxy-Connection: keep-alive", 30); |
| 3228 | chunk->len += 30; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3229 | } else { |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3230 | memcpy(chunk->str + chunk->len, "\r\nConnection: keep-alive", 24); |
| 3231 | chunk->len += 24; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3232 | } |
| 3233 | } |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3234 | memcpy(chunk->str + chunk->len, "\r\n\r\n", 4); |
| 3235 | chunk->len += 4; |
| 3236 | FLT_STRM_CB(s, flt_http_reply(s, txn->status, chunk)); |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3237 | co_inject(res->chn, chunk->str, chunk->len); |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3238 | /* "eat" the request */ |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 3239 | bi_fast_delete(req->chn->buf, req->sov); |
| 3240 | req->next -= req->sov; |
| 3241 | req->sov = 0; |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 3242 | 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] | 3243 | 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] | 3244 | req->msg_state = HTTP_MSG_CLOSED; |
| 3245 | res->msg_state = HTTP_MSG_DONE; |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 3246 | /* Trim any possible response */ |
| 3247 | res->chn->buf->i = 0; |
| 3248 | res->next = res->sov = 0; |
Christopher Faulet | 5d468ca | 2017-09-11 09:27:29 +0200 | [diff] [blame] | 3249 | /* let the server side turn to SI_ST_CLO */ |
| 3250 | channel_shutw_now(req->chn); |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3251 | } else { |
| 3252 | /* keep-alive not possible */ |
| 3253 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3254 | memcpy(chunk->str + chunk->len, "\r\nProxy-Connection: close\r\n\r\n", 29); |
| 3255 | chunk->len += 29; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3256 | } else { |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3257 | memcpy(chunk->str + chunk->len, "\r\nConnection: close\r\n\r\n", 23); |
| 3258 | chunk->len += 23; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3259 | } |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3260 | http_reply_and_close(s, txn->status, chunk); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 3261 | req->chn->analysers &= AN_REQ_FLT_END; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3262 | } |
| 3263 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3264 | if (!(s->flags & SF_ERR_MASK)) |
| 3265 | s->flags |= SF_ERR_LOCAL; |
| 3266 | if (!(s->flags & SF_FINST_MASK)) |
| 3267 | s->flags |= SF_FINST_R; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3268 | |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 3269 | ret = 1; |
| 3270 | leave: |
| 3271 | free_trash_chunk(chunk); |
| 3272 | return ret; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3273 | } |
| 3274 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3275 | /* This stream analyser runs all HTTP request processing which is common to |
| 3276 | * frontends and backends, which means blocking ACLs, filters, connection-close, |
| 3277 | * reqadd, stats and redirects. This is performed for the designated proxy. |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3278 | * 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] | 3279 | * either needs more data or wants to immediately abort the request (eg: deny, |
| 3280 | * error, ...). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3281 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3282 | 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] | 3283 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 3284 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3285 | struct http_txn *txn = s->txn; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3286 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3287 | struct redirect_rule *rule; |
Willy Tarreau | f4f0412 | 2010-01-28 18:10:50 +0100 | [diff] [blame] | 3288 | struct cond_wordlist *wl; |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3289 | enum rule_result verdict; |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 3290 | int deny_status = HTTP_ERR_403; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3291 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 3292 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3293 | /* we need more data */ |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3294 | goto return_prx_yield; |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3295 | } |
| 3296 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3297 | 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] | 3298 | now_ms, __FUNCTION__, |
| 3299 | s, |
| 3300 | req, |
| 3301 | req->rex, req->wex, |
| 3302 | req->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3303 | req->buf->i, |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3304 | req->analysers); |
| 3305 | |
Willy Tarreau | 6541083 | 2014-04-28 21:25:43 +0200 | [diff] [blame] | 3306 | /* just in case we have some per-backend tracking */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3307 | stream_inc_be_http_req_ctr(s); |
Willy Tarreau | 6541083 | 2014-04-28 21:25:43 +0200 | [diff] [blame] | 3308 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3309 | /* evaluate http-request rules */ |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3310 | if (!LIST_ISEMPTY(&px->http_req_rules)) { |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 3311 | 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] | 3312 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3313 | switch (verdict) { |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3314 | case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */ |
| 3315 | goto return_prx_yield; |
| 3316 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3317 | case HTTP_RULE_RES_CONT: |
| 3318 | case HTTP_RULE_RES_STOP: /* nothing to do */ |
| 3319 | break; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 3320 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3321 | case HTTP_RULE_RES_DENY: /* deny or tarpit */ |
| 3322 | if (txn->flags & TX_CLTARPIT) |
| 3323 | goto tarpit; |
| 3324 | goto deny; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 3325 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3326 | case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */ |
| 3327 | goto return_prx_cond; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 3328 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3329 | case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */ |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 3330 | goto done; |
| 3331 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3332 | case HTTP_RULE_RES_BADREQ: /* failed with a bad request */ |
| 3333 | goto return_bad_req; |
| 3334 | } |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3335 | } |
| 3336 | |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 3337 | /* OK at this stage, we know that the request was accepted according to |
| 3338 | * the http-request rules, we can check for the stats. Note that the |
| 3339 | * URI is detected *before* the req* rules in order not to be affected |
| 3340 | * by a possible reqrep, while they are processed *after* so that a |
| 3341 | * reqdeny can still block them. This clearly needs to change in 1.6! |
| 3342 | */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 3343 | if (stats_check_uri(&s->si[1], txn, px)) { |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 3344 | s->target = &http_stats_applet.obj_type; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 3345 | 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] | 3346 | txn->status = 500; |
| 3347 | s->logs.tv_request = now; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 3348 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3349 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3350 | if (!(s->flags & SF_ERR_MASK)) |
| 3351 | s->flags |= SF_ERR_RESOURCE; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 3352 | goto return_prx_cond; |
| 3353 | } |
| 3354 | |
| 3355 | /* parse the whole stats request and extract the relevant information */ |
| 3356 | http_handle_stats(s, req); |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 3357 | 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] | 3358 | /* not all actions implemented: deny, allow, auth */ |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3359 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3360 | if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */ |
| 3361 | goto deny; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 3362 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3363 | if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */ |
| 3364 | goto return_prx_cond; |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 3365 | } |
| 3366 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3367 | /* evaluate the req* rules except reqadd */ |
| 3368 | if (px->req_exp != NULL) { |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 3369 | if (apply_filters_to_request(s, req, px) < 0) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3370 | goto return_bad_req; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3371 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3372 | if (txn->flags & TX_CLDENY) |
| 3373 | goto deny; |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 3374 | |
Jarno Huuskonen | 800d176 | 2017-03-06 14:56:36 +0200 | [diff] [blame] | 3375 | if (txn->flags & TX_CLTARPIT) { |
| 3376 | deny_status = HTTP_ERR_500; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3377 | goto tarpit; |
Jarno Huuskonen | 800d176 | 2017-03-06 14:56:36 +0200 | [diff] [blame] | 3378 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3379 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3380 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3381 | /* add request headers from the rule sets in the same order */ |
| 3382 | list_for_each_entry(wl, &px->req_add, list) { |
| 3383 | if (wl->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 3384 | 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] | 3385 | ret = acl_pass(ret); |
| 3386 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 3387 | ret = !ret; |
| 3388 | if (!ret) |
| 3389 | continue; |
| 3390 | } |
| 3391 | |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 3392 | 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] | 3393 | goto return_bad_req; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 3394 | } |
| 3395 | |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 3396 | |
| 3397 | /* Proceed with the stats now. */ |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3398 | if (unlikely(objt_applet(s->target) == &http_stats_applet)) { |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3399 | /* process the stats request now */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3400 | if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */ |
| 3401 | sess->fe->fe_counters.intercepted_req++; |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 3402 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3403 | if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is |
| 3404 | s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy |
| 3405 | if (!(s->flags & SF_FINST_MASK)) |
| 3406 | s->flags |= SF_FINST_R; |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 3407 | |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 3408 | /* 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] | 3409 | req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END); |
| 3410 | req->analysers &= ~AN_REQ_FLT_XFER_DATA; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 3411 | req->analysers |= AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3412 | goto done; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3413 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 3414 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3415 | /* check whether we have some ACLs set to redirect this request */ |
| 3416 | list_for_each_entry(rule, &px->redirect_rules, list) { |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 3417 | if (rule->cond) { |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3418 | int ret; |
| 3419 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 3420 | 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] | 3421 | ret = acl_pass(ret); |
| 3422 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 3423 | ret = !ret; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3424 | if (!ret) |
| 3425 | continue; |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 3426 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3427 | if (!http_apply_redirect_rule(rule, s, txn)) |
| 3428 | goto return_bad_req; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3429 | goto done; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3430 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 3431 | |
Willy Tarreau | 2be3939 | 2010-01-03 17:24:51 +0100 | [diff] [blame] | 3432 | /* POST requests may be accompanied with an "Expect: 100-Continue" header. |
| 3433 | * If this happens, then the data will not come immediately, so we must |
| 3434 | * send all what we have without waiting. Note that due to the small gain |
| 3435 | * 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] | 3436 | * 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] | 3437 | * itself once used. |
| 3438 | */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 3439 | req->flags |= CF_SEND_DONTWAIT; |
Willy Tarreau | 2be3939 | 2010-01-03 17:24:51 +0100 | [diff] [blame] | 3440 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3441 | done: /* done with this analyser, continue with next ones that the calling |
| 3442 | * points will have set, if any. |
| 3443 | */ |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3444 | req->analyse_exp = TICK_ETERNITY; |
Thierry FOURNIER | 7566e30 | 2014-08-22 06:55:26 +0200 | [diff] [blame] | 3445 | done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */ |
| 3446 | req->analysers &= ~an_bit; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3447 | return 1; |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 3448 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3449 | tarpit: |
Willy Tarreau | 6a0bca9 | 2017-06-11 17:56:27 +0200 | [diff] [blame] | 3450 | /* Allow cookie logging |
| 3451 | */ |
| 3452 | if (s->be->cookie_name || sess->fe->capture_name) |
| 3453 | manage_client_side_cookies(s, req); |
| 3454 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3455 | /* When a connection is tarpitted, we use the tarpit timeout, |
| 3456 | * which may be the same as the connect timeout if unspecified. |
| 3457 | * If unset, then set it to zero because we really want it to |
| 3458 | * eventually expire. We build the tarpit as an analyser. |
| 3459 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 3460 | channel_erase(&s->req); |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3461 | |
| 3462 | /* wipe the request out so that we can drop the connection early |
| 3463 | * if the client closes first. |
| 3464 | */ |
| 3465 | channel_dont_connect(req); |
Bertrand Paquet | 83a2c3d | 2016-04-06 11:58:31 +0200 | [diff] [blame] | 3466 | |
Jarno Huuskonen | 800d176 | 2017-03-06 14:56:36 +0200 | [diff] [blame] | 3467 | txn->status = http_err_codes[deny_status]; |
| 3468 | |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 3469 | req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */ |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3470 | req->analysers |= AN_REQ_HTTP_TARPIT; |
| 3471 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit); |
| 3472 | if (!req->analyse_exp) |
| 3473 | req->analyse_exp = tick_add(now_ms, 0); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3474 | stream_inc_http_err_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3475 | sess->fe->fe_counters.denied_req++; |
| 3476 | if (sess->fe != s->be) |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3477 | s->be->be_counters.denied_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 3478 | if (sess->listener->counters) |
| 3479 | sess->listener->counters->denied_req++; |
Thierry FOURNIER | 7566e30 | 2014-08-22 06:55:26 +0200 | [diff] [blame] | 3480 | goto done_without_exp; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3481 | |
| 3482 | deny: /* this request was blocked (denied) */ |
Bertrand Paquet | 83a2c3d | 2016-04-06 11:58:31 +0200 | [diff] [blame] | 3483 | |
| 3484 | /* Allow cookie logging |
| 3485 | */ |
| 3486 | if (s->be->cookie_name || sess->fe->capture_name) |
| 3487 | manage_client_side_cookies(s, req); |
| 3488 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3489 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 3490 | txn->status = http_err_codes[deny_status]; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3491 | s->logs.tv_request = now; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 3492 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3493 | stream_inc_http_err_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3494 | sess->fe->fe_counters.denied_req++; |
| 3495 | if (sess->fe != s->be) |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3496 | s->be->be_counters.denied_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 3497 | if (sess->listener->counters) |
| 3498 | sess->listener->counters->denied_req++; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3499 | goto return_prx_cond; |
| 3500 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3501 | return_bad_req: |
| 3502 | /* We centralize bad requests processing here */ |
| 3503 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 3504 | /* we detected a parsing error. We want to archive this request |
| 3505 | * in the dedicated proxy area for later troubleshooting. |
| 3506 | */ |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 3507 | http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, sess->fe); |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3508 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 3509 | |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 3510 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3511 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3512 | txn->status = 400; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 3513 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3514 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3515 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 3516 | if (sess->listener->counters) |
| 3517 | sess->listener->counters->failed_req++; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 3518 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3519 | return_prx_cond: |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3520 | if (!(s->flags & SF_ERR_MASK)) |
| 3521 | s->flags |= SF_ERR_PRXCOND; |
| 3522 | if (!(s->flags & SF_FINST_MASK)) |
| 3523 | s->flags |= SF_FINST_R; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 3524 | |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 3525 | req->analysers &= AN_REQ_FLT_END; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3526 | req->analyse_exp = TICK_ETERNITY; |
| 3527 | return 0; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3528 | |
| 3529 | return_prx_yield: |
| 3530 | channel_dont_connect(req); |
| 3531 | return 0; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3532 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3533 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3534 | /* This function performs all the processing enabled for the current request. |
| 3535 | * It returns 1 if the processing can continue on next analysers, or zero if it |
| 3536 | * needs more data, encounters an error, or wants to immediately abort the |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 3537 | * request. It relies on buffers flags, and updates s->req.analysers. |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3538 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3539 | 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] | 3540 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 3541 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3542 | struct http_txn *txn = s->txn; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3543 | struct http_msg *msg = &txn->req; |
Willy Tarreau | ee335e6 | 2015-04-21 18:15:13 +0200 | [diff] [blame] | 3544 | struct connection *cli_conn = objt_conn(strm_sess(s)->origin); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3545 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 3546 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3547 | /* we need more data */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 3548 | channel_dont_connect(req); |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3549 | return 0; |
| 3550 | } |
| 3551 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3552 | 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] | 3553 | now_ms, __FUNCTION__, |
| 3554 | s, |
| 3555 | req, |
| 3556 | req->rex, req->wex, |
| 3557 | req->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3558 | req->buf->i, |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3559 | req->analysers); |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3560 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3561 | /* |
| 3562 | * Right now, we know that we have processed the entire headers |
| 3563 | * and that unwanted requests have been filtered out. We can do |
| 3564 | * whatever we want with the remaining request. Also, now we |
| 3565 | * may have separate values for ->fe, ->be. |
| 3566 | */ |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3567 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3568 | /* |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3569 | * If HTTP PROXY is set we simply get remote server address parsing |
| 3570 | * incoming request. Note that this requires that a connection is |
| 3571 | * allocated on the server side. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3572 | */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3573 | 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] | 3574 | struct connection *conn; |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 3575 | char *path; |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 3576 | |
Willy Tarreau | 9471b8c | 2013-12-15 13:31:35 +0100 | [diff] [blame] | 3577 | /* Note that for now we don't reuse existing proxy connections */ |
Willy Tarreau | 973a542 | 2015-08-05 21:47:23 +0200 | [diff] [blame] | 3578 | if (unlikely((conn = si_alloc_conn(&s->si[1])) == NULL)) { |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 3579 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 3580 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3581 | txn->status = 500; |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 3582 | req->analysers &= AN_REQ_FLT_END; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 3583 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 3584 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3585 | if (!(s->flags & SF_ERR_MASK)) |
| 3586 | s->flags |= SF_ERR_RESOURCE; |
| 3587 | if (!(s->flags & SF_FINST_MASK)) |
| 3588 | s->flags |= SF_FINST_R; |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 3589 | |
| 3590 | return 0; |
| 3591 | } |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 3592 | |
| 3593 | path = http_get_path(txn); |
| 3594 | url2sa(req->buf->p + msg->sl.rq.u, |
| 3595 | 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] | 3596 | &conn->addr.to, NULL); |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 3597 | /* if the path was found, we have to remove everything between |
| 3598 | * req->buf->p + msg->sl.rq.u and path (excluded). If it was not |
| 3599 | * found, we need to replace from req->buf->p + msg->sl.rq.u for |
| 3600 | * u_l characters by a single "/". |
| 3601 | */ |
| 3602 | if (path) { |
| 3603 | char *cur_ptr = req->buf->p; |
| 3604 | char *cur_end = cur_ptr + txn->req.sl.rq.l; |
| 3605 | int delta; |
| 3606 | |
| 3607 | delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0); |
| 3608 | http_msg_move_end(&txn->req, delta); |
| 3609 | cur_end += delta; |
| 3610 | if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL) |
| 3611 | goto return_bad_req; |
| 3612 | } |
| 3613 | else { |
| 3614 | char *cur_ptr = req->buf->p; |
| 3615 | char *cur_end = cur_ptr + txn->req.sl.rq.l; |
| 3616 | int delta; |
| 3617 | |
| 3618 | delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, |
| 3619 | req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1); |
| 3620 | http_msg_move_end(&txn->req, delta); |
| 3621 | cur_end += delta; |
| 3622 | if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL) |
| 3623 | goto return_bad_req; |
| 3624 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3625 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3626 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3627 | /* |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 3628 | * 7: Now we can work with the cookies. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3629 | * Note that doing so might move headers in the request, but |
| 3630 | * the fields will stay coherent and the URI will not move. |
| 3631 | * This should only be performed in the backend. |
| 3632 | */ |
Bertrand Paquet | 83a2c3d | 2016-04-06 11:58:31 +0200 | [diff] [blame] | 3633 | if (s->be->cookie_name || sess->fe->capture_name) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3634 | manage_client_side_cookies(s, req); |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 3635 | |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 3636 | /* add unique-id if "header-unique-id" is specified */ |
| 3637 | |
Thierry Fournier | f4011dd | 2016-03-29 17:23:51 +0200 | [diff] [blame] | 3638 | if (!LIST_ISEMPTY(&sess->fe->format_unique_id) && !s->unique_id) { |
William Lallemand | 5b7ea3a | 2013-08-28 15:44:19 +0200 | [diff] [blame] | 3639 | if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL) |
| 3640 | goto return_bad_req; |
| 3641 | s->unique_id[0] = '\0'; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3642 | 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] | 3643 | } |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 3644 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3645 | if (sess->fe->header_unique_id && s->unique_id) { |
| 3646 | 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] | 3647 | if (trash.len < 0) |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 3648 | goto return_bad_req; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3649 | 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] | 3650 | goto return_bad_req; |
| 3651 | } |
| 3652 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 3653 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3654 | * 9: add X-Forwarded-For if either the frontend or the backend |
| 3655 | * asks for it. |
| 3656 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3657 | if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) { |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 3658 | struct hdr_ctx ctx = { .idx = 0 }; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3659 | if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) && |
| 3660 | http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name, |
| 3661 | 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] | 3662 | req->buf->p, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 3663 | /* The header is set to be added only if none is present |
| 3664 | * and we found it, so don't do anything. |
| 3665 | */ |
| 3666 | } |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3667 | else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3668 | /* Add an X-Forwarded-For header unless the source IP is |
| 3669 | * in the 'except' network range. |
| 3670 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3671 | if ((!sess->fe->except_mask.s_addr || |
| 3672 | (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr) |
| 3673 | != sess->fe->except_net.s_addr) && |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3674 | (!s->be->except_mask.s_addr || |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3675 | (((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] | 3676 | != s->be->except_net.s_addr)) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 3677 | int len; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3678 | unsigned char *pn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3679 | pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr; |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 3680 | |
| 3681 | /* Note: we rely on the backend to get the header name to be used for |
| 3682 | * x-forwarded-for, because the header is really meant for the backends. |
| 3683 | * However, if the backend did not specify any option, we have to rely |
| 3684 | * on the frontend's header name. |
| 3685 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3686 | if (s->be->fwdfor_hdr_len) { |
| 3687 | len = s->be->fwdfor_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3688 | memcpy(trash.str, s->be->fwdfor_hdr_name, len); |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 3689 | } else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3690 | len = sess->fe->fwdfor_hdr_len; |
| 3691 | memcpy(trash.str, sess->fe->fwdfor_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 3692 | } |
Willy Tarreau | e9187f8 | 2014-04-14 15:27:14 +0200 | [diff] [blame] | 3693 | 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] | 3694 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3695 | 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] | 3696 | goto return_bad_req; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 3697 | } |
| 3698 | } |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3699 | else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3700 | /* FIXME: for the sake of completeness, we should also support |
| 3701 | * 'except' here, although it is mostly useless in this case. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3702 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3703 | int len; |
| 3704 | char pn[INET6_ADDRSTRLEN]; |
| 3705 | inet_ntop(AF_INET6, |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3706 | (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3707 | pn, sizeof(pn)); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3708 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3709 | /* Note: we rely on the backend to get the header name to be used for |
| 3710 | * x-forwarded-for, because the header is really meant for the backends. |
| 3711 | * However, if the backend did not specify any option, we have to rely |
| 3712 | * on the frontend's header name. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3713 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3714 | if (s->be->fwdfor_hdr_len) { |
| 3715 | len = s->be->fwdfor_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3716 | memcpy(trash.str, s->be->fwdfor_hdr_name, len); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3717 | } else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3718 | len = sess->fe->fwdfor_hdr_len; |
| 3719 | memcpy(trash.str, sess->fe->fwdfor_hdr_name, len); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3720 | } |
Willy Tarreau | e9187f8 | 2014-04-14 15:27:14 +0200 | [diff] [blame] | 3721 | len += snprintf(trash.str + len, trash.size - len, ": %s", pn); |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3722 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3723 | 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] | 3724 | goto return_bad_req; |
| 3725 | } |
| 3726 | } |
| 3727 | |
| 3728 | /* |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3729 | * 10: add X-Original-To if either the frontend or the backend |
| 3730 | * asks for it. |
| 3731 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3732 | if ((sess->fe->options | s->be->options) & PR_O_ORGTO) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3733 | |
| 3734 | /* FIXME: don't know if IPv6 can handle that case too. */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3735 | if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3736 | /* Add an X-Original-To header unless the destination IP is |
| 3737 | * in the 'except' network range. |
| 3738 | */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3739 | conn_get_to_addr(cli_conn); |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3740 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3741 | if (cli_conn->addr.to.ss_family == AF_INET && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3742 | ((!sess->fe->except_mask_to.s_addr || |
| 3743 | (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr) |
| 3744 | != sess->fe->except_to.s_addr) && |
Emeric Brun | 5bd86a8 | 2010-10-22 17:23:04 +0200 | [diff] [blame] | 3745 | (!s->be->except_mask_to.s_addr || |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3746 | (((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] | 3747 | != s->be->except_to.s_addr))) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3748 | int len; |
| 3749 | unsigned char *pn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3750 | pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr; |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3751 | |
| 3752 | /* Note: we rely on the backend to get the header name to be used for |
| 3753 | * x-original-to, because the header is really meant for the backends. |
| 3754 | * However, if the backend did not specify any option, we have to rely |
| 3755 | * on the frontend's header name. |
| 3756 | */ |
| 3757 | if (s->be->orgto_hdr_len) { |
| 3758 | len = s->be->orgto_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3759 | memcpy(trash.str, s->be->orgto_hdr_name, len); |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3760 | } else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3761 | len = sess->fe->orgto_hdr_len; |
| 3762 | memcpy(trash.str, sess->fe->orgto_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 3763 | } |
Willy Tarreau | e9187f8 | 2014-04-14 15:27:14 +0200 | [diff] [blame] | 3764 | 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] | 3765 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3766 | 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] | 3767 | goto return_bad_req; |
| 3768 | } |
| 3769 | } |
| 3770 | } |
| 3771 | |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 3772 | /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set. |
| 3773 | * If an "Upgrade" token is found, the header is left untouched in order not to have |
| 3774 | * to deal with some servers bugs : some of them fail an Upgrade if anything but |
| 3775 | * "Upgrade" is present in the Connection header. |
| 3776 | */ |
| 3777 | if (!(txn->flags & TX_HDR_CONN_UPG) && |
| 3778 | (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3779 | ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 3780 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) { |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3781 | unsigned int want_flags = 0; |
| 3782 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3783 | if (msg->flags & HTTP_MSGF_VER_11) { |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 3784 | if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3785 | ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 3786 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3787 | !((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3788 | want_flags |= TX_CON_CLO_SET; |
| 3789 | } else { |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 3790 | if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3791 | ((sess->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL && |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 3792 | (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3793 | ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3794 | want_flags |= TX_CON_KAL_SET; |
| 3795 | } |
| 3796 | |
| 3797 | 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] | 3798 | http_change_connection_header(txn, msg, want_flags); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3799 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3800 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3801 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3802 | /* If we have no server assigned yet and we're balancing on url_param |
| 3803 | * with a POST request, we may be interested in checking the body for |
| 3804 | * that parameter. This will be done in another analyser. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3805 | */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3806 | if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) && |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3807 | s->txn->meth == HTTP_METH_POST && s->be->url_param_name != NULL && |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3808 | (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) { |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 3809 | channel_dont_connect(req); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3810 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3811 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3812 | |
Christopher Faulet | be821b9 | 2017-03-30 11:21:53 +0200 | [diff] [blame] | 3813 | req->analysers &= ~AN_REQ_FLT_XFER_DATA; |
| 3814 | req->analysers |= AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 3815 | #ifdef TCP_QUICKACK |
Christopher Faulet | be821b9 | 2017-03-30 11:21:53 +0200 | [diff] [blame] | 3816 | /* We expect some data from the client. Unless we know for sure |
| 3817 | * we already have a full request, we have to re-enable quick-ack |
| 3818 | * in case we previously disabled it, otherwise we might cause |
| 3819 | * the client to delay further data. |
| 3820 | */ |
| 3821 | if ((sess->listener->options & LI_O_NOQUICKACK) && |
| 3822 | cli_conn && conn_ctrl_ready(cli_conn) && |
| 3823 | ((msg->flags & HTTP_MSGF_TE_CHNK) || |
| 3824 | (msg->body_len > req->buf->i - txn->req.eoh - 2))) |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 3825 | setsockopt(cli_conn->handle.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one)); |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 3826 | #endif |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 3827 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3828 | /************************************************************* |
| 3829 | * OK, that's finished for the headers. We have done what we * |
| 3830 | * could. Let's switch to the DATA state. * |
| 3831 | ************************************************************/ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3832 | req->analyse_exp = TICK_ETERNITY; |
| 3833 | req->analysers &= ~an_bit; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3834 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3835 | s->logs.tv_request = now; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3836 | /* OK let's go on with the BODY now */ |
| 3837 | return 1; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3838 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3839 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 3840 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
Willy Tarreau | f073a83 | 2009-03-01 23:21:47 +0100 | [diff] [blame] | 3841 | /* we detected a parsing error. We want to archive this request |
| 3842 | * in the dedicated proxy area for later troubleshooting. |
| 3843 | */ |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 3844 | http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, sess->fe); |
Willy Tarreau | f073a83 | 2009-03-01 23:21:47 +0100 | [diff] [blame] | 3845 | } |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 3846 | |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 3847 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3848 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3849 | txn->status = 400; |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 3850 | req->analysers &= AN_REQ_FLT_END; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 3851 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3852 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3853 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 3854 | if (sess->listener->counters) |
| 3855 | sess->listener->counters->failed_req++; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3856 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3857 | if (!(s->flags & SF_ERR_MASK)) |
| 3858 | s->flags |= SF_ERR_PRXCOND; |
| 3859 | if (!(s->flags & SF_FINST_MASK)) |
| 3860 | s->flags |= SF_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3861 | return 0; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 3862 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3863 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3864 | /* This function is an analyser which processes the HTTP tarpit. It always |
| 3865 | * returns zero, at the beginning because it prevents any other processing |
| 3866 | * from occurring, and at the end because it terminates the request. |
| 3867 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3868 | 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] | 3869 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3870 | struct http_txn *txn = s->txn; |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3871 | |
| 3872 | /* This connection is being tarpitted. The CLIENT side has |
| 3873 | * already set the connect expiration date to the right |
| 3874 | * timeout. We just have to check that the client is still |
| 3875 | * there and that the timeout has not expired. |
| 3876 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 3877 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 3878 | if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 && |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3879 | !tick_is_expired(req->analyse_exp, now_ms)) |
| 3880 | return 0; |
| 3881 | |
| 3882 | /* We will set the queue timer to the time spent, just for |
| 3883 | * logging purposes. We fake a 500 server error, so that the |
| 3884 | * attacker will not suspect his connection has been tarpitted. |
| 3885 | * It will not cause trouble to the logs because we can exclude |
| 3886 | * the tarpitted connections by filtering on the 'PT' status flags. |
| 3887 | */ |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3888 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 3889 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 3890 | if (!(req->flags & CF_READ_ERROR)) |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 3891 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3892 | |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 3893 | req->analysers &= AN_REQ_FLT_END; |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3894 | req->analyse_exp = TICK_ETERNITY; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3895 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3896 | if (!(s->flags & SF_ERR_MASK)) |
| 3897 | s->flags |= SF_ERR_PRXCOND; |
| 3898 | if (!(s->flags & SF_FINST_MASK)) |
| 3899 | s->flags |= SF_FINST_T; |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3900 | return 0; |
| 3901 | } |
| 3902 | |
Willy Tarreau | 5a8f947 | 2014-04-10 11:16:06 +0200 | [diff] [blame] | 3903 | /* This function is an analyser which waits for the HTTP request body. It waits |
| 3904 | * for either the buffer to be full, or the full advertised contents to have |
| 3905 | * reached the buffer. It must only be called after the standard HTTP request |
| 3906 | * processing has occurred, because it expects the request to be parsed and will |
| 3907 | * look for the Expect header. It may send a 100-Continue interim response. It |
| 3908 | * takes in input any state starting from HTTP_MSG_BODY and leaves with one of |
| 3909 | * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it |
| 3910 | * 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] | 3911 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3912 | 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] | 3913 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 3914 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3915 | struct http_txn *txn = s->txn; |
| 3916 | struct http_msg *msg = &s->txn->req; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3917 | |
| 3918 | /* We have to parse the HTTP request body to find any required data. |
| 3919 | * "balance url_param check_post" should have been the only way to get |
| 3920 | * into this. We were brought here after HTTP header analysis, so all |
| 3921 | * related structures are ready. |
| 3922 | */ |
| 3923 | |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 3924 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
| 3925 | /* This is the first call */ |
| 3926 | if (msg->msg_state < HTTP_MSG_BODY) |
| 3927 | goto missing_data; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3928 | |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 3929 | if (msg->msg_state < HTTP_MSG_100_SENT) { |
| 3930 | /* If we have HTTP/1.1 and Expect: 100-continue, then we must |
| 3931 | * send an HTTP/1.1 100 Continue intermediate response. |
| 3932 | */ |
| 3933 | if (msg->flags & HTTP_MSGF_VER_11) { |
| 3934 | struct hdr_ctx ctx; |
| 3935 | ctx.idx = 0; |
| 3936 | /* Expect is allowed in 1.1, look for it */ |
| 3937 | if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) && |
| 3938 | 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] | 3939 | 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] | 3940 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 3941 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3942 | } |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 3943 | msg->msg_state = HTTP_MSG_100_SENT; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3944 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3945 | |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 3946 | /* 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] | 3947 | * req->buf->p still points to the beginning of the message. We |
| 3948 | * must save the body in msg->next because it survives buffer |
| 3949 | * re-alignments. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3950 | */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 3951 | msg->next = msg->sov; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 3952 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3953 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3954 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 3955 | else |
| 3956 | msg->msg_state = HTTP_MSG_DATA; |
| 3957 | } |
| 3958 | |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 3959 | if (!(msg->flags & HTTP_MSGF_TE_CHNK)) { |
| 3960 | /* 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] | 3961 | if (http_body_bytes(msg) < msg->body_len) |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 3962 | goto missing_data; |
| 3963 | |
| 3964 | /* OK we have everything we need now */ |
| 3965 | goto http_end; |
| 3966 | } |
| 3967 | |
| 3968 | /* OK here we're parsing a chunked-encoded message */ |
| 3969 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3970 | if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 3971 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 3972 | * 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] | 3973 | * TRAILERS state. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3974 | */ |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 3975 | int ret = http_parse_chunk_size(msg); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3976 | |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3977 | if (!ret) |
| 3978 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3979 | else if (ret < 0) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3980 | stream_inc_http_err_ctr(s); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3981 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3982 | } |
Christopher Faulet | 113f7de | 2015-12-14 14:52:13 +0100 | [diff] [blame] | 3983 | msg->next += ret; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 3984 | msg->msg_state = msg->chunk_len ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3985 | } |
| 3986 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3987 | /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state. |
Willy Tarreau | e115b49 | 2015-05-01 23:05:14 +0200 | [diff] [blame] | 3988 | * We have the first data byte is in msg->sov + msg->sol. We're waiting |
| 3989 | * for at least a whole chunk or the whole content length bytes after |
| 3990 | * msg->sov + msg->sol. |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3991 | */ |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 3992 | if (msg->msg_state == HTTP_MSG_TRAILERS) |
| 3993 | goto http_end; |
| 3994 | |
Willy Tarreau | e115b49 | 2015-05-01 23:05:14 +0200 | [diff] [blame] | 3995 | 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] | 3996 | goto http_end; |
| 3997 | |
| 3998 | missing_data: |
Willy Tarreau | 31a1995 | 2014-04-10 11:50:37 +0200 | [diff] [blame] | 3999 | /* we get here if we need to wait for more data. If the buffer is full, |
| 4000 | * we have the maximum we can expect. |
| 4001 | */ |
| 4002 | if (buffer_full(req->buf, global.tune.maxrewrite)) |
| 4003 | goto http_end; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 4004 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4005 | 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] | 4006 | txn->status = 408; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 4007 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 4008 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4009 | if (!(s->flags & SF_ERR_MASK)) |
| 4010 | s->flags |= SF_ERR_CLITO; |
| 4011 | if (!(s->flags & SF_FINST_MASK)) |
| 4012 | s->flags |= SF_FINST_D; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4013 | goto return_err_msg; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4014 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4015 | |
| 4016 | /* we get here if we need to wait for more data */ |
Willy Tarreau | 31a1995 | 2014-04-10 11:50:37 +0200 | [diff] [blame] | 4017 | if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) { |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4018 | /* Not enough data. We'll re-use the http-request |
| 4019 | * timeout here. Ideally, we should set the timeout |
| 4020 | * relative to the accept() date. We just set the |
| 4021 | * request timeout once at the beginning of the |
| 4022 | * request. |
| 4023 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4024 | channel_dont_connect(req); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4025 | if (!tick_isset(req->analyse_exp)) |
Willy Tarreau | cd7afc0 | 2009-07-12 10:03:17 +0200 | [diff] [blame] | 4026 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4027 | return 0; |
| 4028 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4029 | |
| 4030 | http_end: |
| 4031 | /* The situation will not evolve, so let's give up on the analysis. */ |
| 4032 | s->logs.tv_request = now; /* update the request timer to reflect full request */ |
| 4033 | req->analysers &= ~an_bit; |
| 4034 | req->analyse_exp = TICK_ETERNITY; |
| 4035 | return 1; |
| 4036 | |
| 4037 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 4038 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4039 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4040 | txn->status = 400; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 4041 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4042 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4043 | if (!(s->flags & SF_ERR_MASK)) |
| 4044 | s->flags |= SF_ERR_PRXCOND; |
| 4045 | if (!(s->flags & SF_FINST_MASK)) |
| 4046 | s->flags |= SF_FINST_R; |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 4047 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4048 | return_err_msg: |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4049 | req->analysers &= AN_REQ_FLT_END; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4050 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4051 | if (sess->listener->counters) |
| 4052 | sess->listener->counters->failed_req++; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4053 | return 0; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4054 | } |
| 4055 | |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4056 | /* send a server's name with an outgoing request over an established connection. |
| 4057 | * Note: this function is designed to be called once the request has been scheduled |
| 4058 | * for being forwarded. This is the reason why it rewinds the buffer before |
| 4059 | * proceeding. |
| 4060 | */ |
Willy Tarreau | 45c0d98 | 2012-03-09 12:11:57 +0100 | [diff] [blame] | 4061 | 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] | 4062 | |
| 4063 | struct hdr_ctx ctx; |
| 4064 | |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4065 | char *hdr_name = be->server_id_hdr_name; |
| 4066 | int hdr_name_len = be->server_id_hdr_len; |
Willy Tarreau | 394db37 | 2012-10-12 22:40:39 +0200 | [diff] [blame] | 4067 | struct channel *chn = txn->req.chn; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4068 | char *hdr_val; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4069 | unsigned int old_o, old_i; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4070 | |
William Lallemand | d9e9066 | 2012-01-30 17:27:17 +0100 | [diff] [blame] | 4071 | ctx.idx = 0; |
| 4072 | |
Willy Tarreau | 211cdec | 2014-04-17 20:18:08 +0200 | [diff] [blame] | 4073 | old_o = http_hdr_rewind(&txn->req); |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4074 | if (old_o) { |
| 4075 | /* The request was already skipped, let's restore it */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4076 | b_rew(chn->buf, old_o); |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 4077 | txn->req.next += old_o; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 4078 | txn->req.sov += old_o; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4079 | } |
| 4080 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4081 | old_i = chn->buf->i; |
| 4082 | 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] | 4083 | /* remove any existing values from the header */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 4084 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4085 | } |
| 4086 | |
| 4087 | /* Add the new header requested with the server value */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4088 | hdr_val = trash.str; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4089 | memcpy(hdr_val, hdr_name, hdr_name_len); |
| 4090 | hdr_val += hdr_name_len; |
| 4091 | *hdr_val++ = ':'; |
| 4092 | *hdr_val++ = ' '; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4093 | hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val); |
| 4094 | 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] | 4095 | |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4096 | if (old_o) { |
| 4097 | /* If this was a forwarded request, we must readjust the amount of |
| 4098 | * data to be forwarded in order to take into account the size |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 4099 | * variations. Note that the current state is >= HTTP_MSG_BODY, |
| 4100 | * so we don't have to adjust ->sol. |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4101 | */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 4102 | old_o += chn->buf->i - old_i; |
| 4103 | b_adv(chn->buf, old_o); |
| 4104 | txn->req.next -= old_o; |
| 4105 | txn->req.sov -= old_o; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4106 | } |
| 4107 | |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4108 | return 0; |
| 4109 | } |
| 4110 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4111 | /* Terminate current transaction and prepare a new one. This is very tricky |
| 4112 | * right now but it works. |
| 4113 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4114 | void http_end_txn_clean_session(struct stream *s) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4115 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4116 | int prev_status = s->txn->status; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 4117 | struct proxy *fe = strm_fe(s); |
Willy Tarreau | 858b103 | 2015-12-07 17:04:59 +0100 | [diff] [blame] | 4118 | struct proxy *be = s->be; |
Willy Tarreau | 323a2d9 | 2015-08-04 19:00:17 +0200 | [diff] [blame] | 4119 | struct connection *srv_conn; |
| 4120 | struct server *srv; |
Willy Tarreau | 449d74a | 2015-08-05 17:16:33 +0200 | [diff] [blame] | 4121 | unsigned int prev_flags = s->txn->flags; |
Willy Tarreau | 068621e | 2013-12-23 15:11:25 +0100 | [diff] [blame] | 4122 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4123 | /* FIXME: We need a more portable way of releasing a backend's and a |
| 4124 | * server's connections. We need a safer way to reinitialize buffer |
| 4125 | * flags. We also need a more accurate method for computing per-request |
| 4126 | * data. |
| 4127 | */ |
Willy Tarreau | 323a2d9 | 2015-08-04 19:00:17 +0200 | [diff] [blame] | 4128 | srv_conn = objt_conn(s->si[1].end); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4129 | |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4130 | /* unless we're doing keep-alive, we want to quickly close the connection |
| 4131 | * to the server. |
| 4132 | */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4133 | if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) || |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4134 | !si_conn_ready(&s->si[1])) { |
| 4135 | s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF; |
| 4136 | si_shutr(&s->si[1]); |
| 4137 | si_shutw(&s->si[1]); |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4138 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4139 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4140 | if (s->flags & SF_BE_ASSIGNED) { |
Willy Tarreau | 858b103 | 2015-12-07 17:04:59 +0100 | [diff] [blame] | 4141 | be->beconn--; |
Willy Tarreau | 2d5cd47 | 2012-03-01 23:34:37 +0100 | [diff] [blame] | 4142 | if (unlikely(s->srv_conn)) |
| 4143 | sess_change_server(s, NULL); |
| 4144 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4145 | |
| 4146 | s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4147 | stream_process_counters(s); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4148 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4149 | if (s->txn->status) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4150 | int n; |
| 4151 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4152 | n = s->txn->status / 100; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4153 | if (n < 1 || n > 5) |
| 4154 | n = 0; |
| 4155 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4156 | if (fe->mode == PR_MODE_HTTP) { |
| 4157 | fe->fe_counters.p.http.rsp[n]++; |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 4158 | } |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4159 | if ((s->flags & SF_BE_ASSIGNED) && |
Willy Tarreau | 858b103 | 2015-12-07 17:04:59 +0100 | [diff] [blame] | 4160 | (be->mode == PR_MODE_HTTP)) { |
| 4161 | be->be_counters.p.http.rsp[n]++; |
| 4162 | be->be_counters.p.http.cum_req++; |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 4163 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4164 | } |
| 4165 | |
| 4166 | /* don't count other requests' data */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4167 | s->logs.bytes_in -= s->req.buf->i; |
| 4168 | s->logs.bytes_out -= s->res.buf->i; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4169 | |
| 4170 | /* let's do a final log if we need it */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4171 | if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait && |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4172 | !(s->flags & SF_MONITOR) && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4173 | (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4174 | s->do_log(s); |
| 4175 | } |
| 4176 | |
Willy Tarreau | d713bcc | 2014-06-25 15:36:04 +0200 | [diff] [blame] | 4177 | /* stop tracking content-based counters */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4178 | stream_stop_content_counters(s); |
| 4179 | stream_update_time_stats(s); |
Willy Tarreau | 4bfc580 | 2014-06-17 12:19:18 +0200 | [diff] [blame] | 4180 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4181 | s->logs.accept_date = date; /* user-visible date for logging */ |
| 4182 | s->logs.tv_accept = now; /* corrected date for internal use */ |
Thierry FOURNIER / OZON.IO | 4cac359 | 2016-07-28 17:19:45 +0200 | [diff] [blame] | 4183 | s->logs.t_handshake = 0; /* There are no handshake in keep alive connection. */ |
| 4184 | s->logs.t_idle = -1; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4185 | tv_zero(&s->logs.tv_request); |
| 4186 | s->logs.t_queue = -1; |
| 4187 | s->logs.t_connect = -1; |
| 4188 | s->logs.t_data = -1; |
| 4189 | s->logs.t_close = 0; |
| 4190 | s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */ |
| 4191 | s->logs.srv_queue_size = 0; /* we will get this number soon */ |
| 4192 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4193 | s->logs.bytes_in = s->req.total = s->req.buf->i; |
| 4194 | s->logs.bytes_out = s->res.total = s->res.buf->i; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4195 | |
| 4196 | if (s->pend_pos) |
| 4197 | pendconn_free(s->pend_pos); |
| 4198 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4199 | if (objt_server(s->target)) { |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4200 | if (s->flags & SF_CURR_SESS) { |
| 4201 | s->flags &= ~SF_CURR_SESS; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4202 | objt_server(s->target)->cur_sess--; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4203 | } |
Willy Tarreau | 858b103 | 2015-12-07 17:04:59 +0100 | [diff] [blame] | 4204 | if (may_dequeue_tasks(objt_server(s->target), be)) |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4205 | process_srv_queue(objt_server(s->target)); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4206 | } |
| 4207 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4208 | s->target = NULL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4209 | |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4210 | /* only release our endpoint if we don't intend to reuse the |
| 4211 | * connection. |
| 4212 | */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4213 | if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) || |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4214 | !si_conn_ready(&s->si[1])) { |
| 4215 | si_release_endpoint(&s->si[1]); |
Willy Tarreau | 323a2d9 | 2015-08-04 19:00:17 +0200 | [diff] [blame] | 4216 | srv_conn = NULL; |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4217 | } |
| 4218 | |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4219 | s->si[1].state = s->si[1].prev_state = SI_ST_INI; |
| 4220 | s->si[1].err_type = SI_ET_NONE; |
| 4221 | s->si[1].conn_retries = 0; /* used for logging too */ |
| 4222 | s->si[1].exp = TICK_ETERNITY; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4223 | 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] | 4224 | 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); |
| 4225 | 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] | 4226 | s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST); |
| 4227 | s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED); |
| 4228 | s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP); |
Willy Tarreau | 543db62 | 2012-11-15 16:41:22 +0100 | [diff] [blame] | 4229 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4230 | s->txn->meth = 0; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4231 | http_reset_txn(s); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4232 | s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ; |
Willy Tarreau | 068621e | 2013-12-23 15:11:25 +0100 | [diff] [blame] | 4233 | |
| 4234 | if (prev_status == 401 || prev_status == 407) { |
| 4235 | /* In HTTP keep-alive mode, if we receive a 401, we still have |
| 4236 | * a chance of being able to send the visitor again to the same |
| 4237 | * server over the same connection. This is required by some |
| 4238 | * broken protocols such as NTLM, and anyway whenever there is |
| 4239 | * an opportunity for sending the challenge to the proper place, |
| 4240 | * it's better to do it (at least it helps with debugging). |
| 4241 | */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4242 | s->txn->flags |= TX_PREFER_LAST; |
Willy Tarreau | bd99d58 | 2015-09-02 10:40:43 +0200 | [diff] [blame] | 4243 | if (srv_conn) |
| 4244 | srv_conn->flags |= CO_FL_PRIVATE; |
Willy Tarreau | 068621e | 2013-12-23 15:11:25 +0100 | [diff] [blame] | 4245 | } |
| 4246 | |
Willy Tarreau | 53f9685 | 2016-02-02 18:50:47 +0100 | [diff] [blame] | 4247 | /* Never ever allow to reuse a connection from a non-reuse backend */ |
| 4248 | if (srv_conn && (be->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR) |
| 4249 | srv_conn->flags |= CO_FL_PRIVATE; |
| 4250 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4251 | if (fe->options2 & PR_O2_INDEPSTR) |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4252 | s->si[1].flags |= SI_FL_INDEP_STR; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4253 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4254 | if (fe->options2 & PR_O2_NODELAY) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4255 | s->req.flags |= CF_NEVER_WAIT; |
| 4256 | s->res.flags |= CF_NEVER_WAIT; |
Willy Tarreau | 96e3121 | 2011-05-30 18:10:30 +0200 | [diff] [blame] | 4257 | } |
| 4258 | |
Willy Tarreau | 714ea78 | 2015-11-25 20:11:11 +0100 | [diff] [blame] | 4259 | /* we're removing the analysers, we MUST re-enable events detection. |
| 4260 | * We don't enable close on the response channel since it's either |
| 4261 | * already closed, or in keep-alive with an idle connection handler. |
| 4262 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4263 | channel_auto_read(&s->req); |
| 4264 | channel_auto_close(&s->req); |
| 4265 | channel_auto_read(&s->res); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4266 | |
Willy Tarreau | 1c59bd5 | 2015-11-02 20:20:11 +0100 | [diff] [blame] | 4267 | /* we're in keep-alive with an idle connection, monitor it if not already done */ |
| 4268 | if (srv_conn && LIST_ISEMPTY(&srv_conn->list)) { |
Willy Tarreau | 323a2d9 | 2015-08-04 19:00:17 +0200 | [diff] [blame] | 4269 | srv = objt_server(srv_conn->target); |
Willy Tarreau | 8dff998 | 2015-08-04 20:45:52 +0200 | [diff] [blame] | 4270 | if (!srv) |
| 4271 | si_idle_conn(&s->si[1], NULL); |
Willy Tarreau | 53f9685 | 2016-02-02 18:50:47 +0100 | [diff] [blame] | 4272 | else if (srv_conn->flags & CO_FL_PRIVATE) |
Willy Tarreau | 8dff998 | 2015-08-04 20:45:52 +0200 | [diff] [blame] | 4273 | si_idle_conn(&s->si[1], &srv->priv_conns); |
Willy Tarreau | 449d74a | 2015-08-05 17:16:33 +0200 | [diff] [blame] | 4274 | else if (prev_flags & TX_NOT_FIRST) |
| 4275 | /* note: we check the request, not the connection, but |
| 4276 | * this is valid for strategies SAFE and AGGR, and in |
| 4277 | * case of ALWS, we don't care anyway. |
| 4278 | */ |
| 4279 | si_idle_conn(&s->si[1], &srv->safe_conns); |
Willy Tarreau | 8dff998 | 2015-08-04 20:45:52 +0200 | [diff] [blame] | 4280 | else |
| 4281 | si_idle_conn(&s->si[1], &srv->idle_conns); |
Willy Tarreau | 4320eaa | 2015-08-05 11:08:30 +0200 | [diff] [blame] | 4282 | } |
Christopher Faulet | e600624 | 2017-03-10 11:52:44 +0100 | [diff] [blame] | 4283 | s->req.analysers = strm_li(s) ? strm_li(s)->analysers : 0; |
| 4284 | s->res.analysers = 0; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4285 | } |
| 4286 | |
| 4287 | |
| 4288 | /* This function updates the request state machine according to the response |
| 4289 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 4290 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 4291 | * it is only used to find when a request/response couple is complete. Both |
| 4292 | * this function and its equivalent should loop until both return zero. It |
| 4293 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 4294 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4295 | int http_sync_req_state(struct stream *s) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4296 | { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4297 | struct channel *chn = &s->req; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4298 | struct http_txn *txn = s->txn; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4299 | unsigned int old_flags = chn->flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4300 | unsigned int old_state = txn->req.msg_state; |
| 4301 | |
Christopher Faulet | 4be9803 | 2017-07-18 10:48:24 +0200 | [diff] [blame] | 4302 | if (unlikely(txn->req.msg_state < HTTP_MSG_DONE)) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4303 | return 0; |
| 4304 | |
| 4305 | if (txn->req.msg_state == HTTP_MSG_DONE) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4306 | /* No need to read anymore, the request was completely parsed. |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 4307 | * We can shut the read side unless we want to abort_on_close, |
| 4308 | * or we have a POST request. The issue with POST requests is |
| 4309 | * that some browsers still send a CRLF after the request, and |
| 4310 | * this CRLF must be read so that it does not remain in the kernel |
| 4311 | * buffers, otherwise a close could cause an RST on some systems |
| 4312 | * (eg: Linux). |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 4313 | * Note that if we're using keep-alive on the client side, we'd |
| 4314 | * rather poll now and keep the polling enabled for the whole |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4315 | * stream's life than enabling/disabling it between each |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 4316 | * response and next request. |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4317 | */ |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 4318 | if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) && |
| 4319 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) && |
| 4320 | !(s->be->options & PR_O_ABRT_CLOSE) && |
| 4321 | txn->meth != HTTP_METH_POST) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4322 | channel_dont_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4323 | |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 4324 | /* if the server closes the connection, we want to immediately react |
| 4325 | * and close the socket to save packets and syscalls. |
| 4326 | */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4327 | s->si[1].flags |= SI_FL_NOHALF; |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 4328 | |
Willy Tarreau | 7f876a1 | 2015-11-18 11:59:55 +0100 | [diff] [blame] | 4329 | /* In any case we've finished parsing the request so we must |
| 4330 | * disable Nagle when sending data because 1) we're not going |
| 4331 | * to shut this side, and 2) the server is waiting for us to |
| 4332 | * send pending data. |
| 4333 | */ |
| 4334 | chn->flags |= CF_NEVER_WAIT; |
| 4335 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4336 | if (txn->rsp.msg_state == HTTP_MSG_ERROR) |
| 4337 | goto wait_other_side; |
| 4338 | |
| 4339 | if (txn->rsp.msg_state < HTTP_MSG_DONE) { |
| 4340 | /* The server has not finished to respond, so we |
| 4341 | * don't want to move in order not to upset it. |
| 4342 | */ |
| 4343 | goto wait_other_side; |
| 4344 | } |
| 4345 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4346 | /* When we get here, it means that both the request and the |
| 4347 | * response have finished receiving. Depending on the connection |
| 4348 | * mode, we'll have to wait for the last bytes to leave in either |
| 4349 | * direction, and sometimes for a close to be effective. |
| 4350 | */ |
| 4351 | |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4352 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 4353 | /* Server-close mode : queue a connection close to the server */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4354 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) |
| 4355 | channel_shutw_now(chn); |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4356 | } |
| 4357 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 4358 | /* Option forceclose is set, or either side wants to close, |
| 4359 | * let's enforce it now that we're not expecting any new |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4360 | * data to come. The caller knows the stream is complete |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4361 | * once both states are CLOSED. |
Christopher Faulet | 1486b0a | 2017-07-18 11:42:08 +0200 | [diff] [blame] | 4362 | * |
| 4363 | * However, there is an exception if the response |
| 4364 | * length is undefined. In this case, we need to wait |
| 4365 | * the close from the server. The response will be |
| 4366 | * switched in TUNNEL mode until the end. |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4367 | */ |
Christopher Faulet | 1486b0a | 2017-07-18 11:42:08 +0200 | [diff] [blame] | 4368 | if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) && |
| 4369 | txn->rsp.msg_state != HTTP_MSG_CLOSED) |
| 4370 | goto check_channel_flags; |
| 4371 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4372 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) { |
| 4373 | channel_shutr_now(chn); |
| 4374 | channel_shutw_now(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4375 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4376 | } |
| 4377 | else { |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4378 | /* The last possible modes are keep-alive and tunnel. Tunnel mode |
| 4379 | * will not have any analyser so it needs to poll for reads. |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4380 | */ |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4381 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) { |
| 4382 | channel_auto_read(chn); |
| 4383 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4384 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4385 | } |
| 4386 | |
Christopher Faulet | 4be9803 | 2017-07-18 10:48:24 +0200 | [diff] [blame] | 4387 | goto check_channel_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4388 | } |
| 4389 | |
| 4390 | if (txn->req.msg_state == HTTP_MSG_CLOSING) { |
| 4391 | http_msg_closing: |
| 4392 | /* nothing else to forward, just waiting for the output buffer |
| 4393 | * to be empty and for the shutw_now to take effect. |
| 4394 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4395 | if (channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4396 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 4397 | goto http_msg_closed; |
| 4398 | } |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4399 | else if (chn->flags & CF_SHUTW) { |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 4400 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4401 | txn->req.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4402 | } |
Christopher Faulet | 56d2609 | 2017-07-20 11:05:10 +0200 | [diff] [blame] | 4403 | goto wait_other_side; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4404 | } |
| 4405 | |
| 4406 | if (txn->req.msg_state == HTTP_MSG_CLOSED) { |
| 4407 | http_msg_closed: |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 4408 | /* see above in MSG_DONE why we only do this in these states */ |
| 4409 | if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) && |
| 4410 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) && |
| 4411 | !(s->be->options & PR_O_ABRT_CLOSE)) |
Willy Tarreau | 2e7a165 | 2013-12-15 15:32:10 +0100 | [diff] [blame] | 4412 | channel_dont_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4413 | goto wait_other_side; |
| 4414 | } |
| 4415 | |
Christopher Faulet | 4be9803 | 2017-07-18 10:48:24 +0200 | [diff] [blame] | 4416 | check_channel_flags: |
| 4417 | /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */ |
| 4418 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) { |
| 4419 | /* if we've just closed an output, let's switch */ |
| 4420 | s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */ |
| 4421 | txn->req.msg_state = HTTP_MSG_CLOSING; |
| 4422 | goto http_msg_closing; |
| 4423 | } |
| 4424 | |
| 4425 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4426 | wait_other_side: |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4427 | return txn->req.msg_state != old_state || chn->flags != old_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4428 | } |
| 4429 | |
| 4430 | |
| 4431 | /* This function updates the response state machine according to the request |
| 4432 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 4433 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 4434 | * it is only used to find when a request/response couple is complete. Both |
| 4435 | * this function and its equivalent should loop until both return zero. It |
| 4436 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 4437 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4438 | int http_sync_res_state(struct stream *s) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4439 | { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4440 | struct channel *chn = &s->res; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4441 | struct http_txn *txn = s->txn; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4442 | unsigned int old_flags = chn->flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4443 | unsigned int old_state = txn->rsp.msg_state; |
| 4444 | |
Christopher Faulet | 4be9803 | 2017-07-18 10:48:24 +0200 | [diff] [blame] | 4445 | if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE)) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4446 | return 0; |
| 4447 | |
| 4448 | if (txn->rsp.msg_state == HTTP_MSG_DONE) { |
| 4449 | /* In theory, we don't need to read anymore, but we must |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4450 | * still monitor the server connection for a possible close |
| 4451 | * while the request is being uploaded, so we don't disable |
| 4452 | * reading. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4453 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4454 | /* channel_dont_read(chn); */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4455 | |
| 4456 | if (txn->req.msg_state == HTTP_MSG_ERROR) |
| 4457 | goto wait_other_side; |
| 4458 | |
| 4459 | if (txn->req.msg_state < HTTP_MSG_DONE) { |
| 4460 | /* The client seems to still be sending data, probably |
| 4461 | * because we got an error response during an upload. |
| 4462 | * We have the choice of either breaking the connection |
| 4463 | * or letting it pass through. Let's do the later. |
| 4464 | */ |
| 4465 | goto wait_other_side; |
| 4466 | } |
| 4467 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4468 | /* When we get here, it means that both the request and the |
| 4469 | * response have finished receiving. Depending on the connection |
| 4470 | * mode, we'll have to wait for the last bytes to leave in either |
| 4471 | * direction, and sometimes for a close to be effective. |
| 4472 | */ |
| 4473 | |
| 4474 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 4475 | /* Server-close mode : shut read and wait for the request |
| 4476 | * side to close its output buffer. The caller will detect |
| 4477 | * when we're in DONE and the other is in CLOSED and will |
| 4478 | * catch that for the final cleanup. |
| 4479 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4480 | if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW))) |
| 4481 | channel_shutr_now(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4482 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4483 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 4484 | /* Option forceclose is set, or either side wants to close, |
| 4485 | * let's enforce it now that we're not expecting any new |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4486 | * data to come. The caller knows the stream is complete |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4487 | * once both states are CLOSED. |
Christopher Faulet | 1486b0a | 2017-07-18 11:42:08 +0200 | [diff] [blame] | 4488 | * |
| 4489 | * However, there is an exception if the response length |
| 4490 | * is undefined. In this case, we switch in TUNNEL mode. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4491 | */ |
Christopher Faulet | 1486b0a | 2017-07-18 11:42:08 +0200 | [diff] [blame] | 4492 | if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN)) { |
| 4493 | channel_auto_read(chn); |
| 4494 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
| 4495 | chn->flags |= CF_NEVER_WAIT; |
| 4496 | } |
| 4497 | else if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4498 | channel_shutr_now(chn); |
| 4499 | channel_shutw_now(chn); |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4500 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4501 | } |
| 4502 | else { |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4503 | /* The last possible modes are keep-alive and tunnel. Tunnel will |
| 4504 | * need to forward remaining data. Keep-alive will need to monitor |
| 4505 | * for connection closing. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4506 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4507 | channel_auto_read(chn); |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 4508 | chn->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4509 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) |
| 4510 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4511 | } |
| 4512 | |
Christopher Faulet | 4be9803 | 2017-07-18 10:48:24 +0200 | [diff] [blame] | 4513 | goto check_channel_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4514 | } |
| 4515 | |
| 4516 | if (txn->rsp.msg_state == HTTP_MSG_CLOSING) { |
| 4517 | http_msg_closing: |
| 4518 | /* nothing else to forward, just waiting for the output buffer |
| 4519 | * to be empty and for the shutw_now to take effect. |
| 4520 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4521 | if (channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4522 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 4523 | goto http_msg_closed; |
| 4524 | } |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4525 | else if (chn->flags & CF_SHUTW) { |
Christopher Faulet | a3992e0 | 2017-07-18 10:35:55 +0200 | [diff] [blame] | 4526 | txn->rsp.err_state = txn->rsp.msg_state; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4527 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4528 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4529 | if (objt_server(s->target)) |
| 4530 | objt_server(s->target)->counters.cli_aborts++; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4531 | } |
Christopher Faulet | 56d2609 | 2017-07-20 11:05:10 +0200 | [diff] [blame] | 4532 | goto wait_other_side; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4533 | } |
| 4534 | |
| 4535 | if (txn->rsp.msg_state == HTTP_MSG_CLOSED) { |
| 4536 | http_msg_closed: |
| 4537 | /* drop any pending data */ |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 4538 | channel_truncate(chn); |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4539 | channel_auto_close(chn); |
| 4540 | channel_auto_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4541 | goto wait_other_side; |
| 4542 | } |
| 4543 | |
Christopher Faulet | 4be9803 | 2017-07-18 10:48:24 +0200 | [diff] [blame] | 4544 | check_channel_flags: |
| 4545 | /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */ |
| 4546 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) { |
| 4547 | /* if we've just closed an output, let's switch */ |
| 4548 | txn->rsp.msg_state = HTTP_MSG_CLOSING; |
| 4549 | goto http_msg_closing; |
| 4550 | } |
| 4551 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4552 | wait_other_side: |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 4553 | /* We force the response to leave immediately if we're waiting for the |
| 4554 | * other side, since there is no pending shutdown to push it out. |
| 4555 | */ |
| 4556 | if (!channel_is_empty(chn)) |
| 4557 | chn->flags |= CF_SEND_DONTWAIT; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4558 | return txn->rsp.msg_state != old_state || chn->flags != old_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4559 | } |
| 4560 | |
| 4561 | |
Christopher Faulet | 894da4c | 2017-07-18 11:29:07 +0200 | [diff] [blame] | 4562 | /* Resync the request and response state machines. */ |
| 4563 | void http_resync_states(struct stream *s) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4564 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4565 | struct http_txn *txn = s->txn; |
Christopher Faulet | 894da4c | 2017-07-18 11:29:07 +0200 | [diff] [blame] | 4566 | #ifdef DEBUG_FULL |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4567 | int old_req_state = txn->req.msg_state; |
| 4568 | int old_res_state = txn->rsp.msg_state; |
Christopher Faulet | 894da4c | 2017-07-18 11:29:07 +0200 | [diff] [blame] | 4569 | #endif |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4570 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4571 | http_sync_req_state(s); |
| 4572 | while (1) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4573 | if (!http_sync_res_state(s)) |
| 4574 | break; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4575 | if (!http_sync_req_state(s)) |
| 4576 | break; |
| 4577 | } |
Willy Tarreau | 3ce10ff | 2014-04-22 08:24:38 +0200 | [diff] [blame] | 4578 | |
Christopher Faulet | 894da4c | 2017-07-18 11:29:07 +0200 | [diff] [blame] | 4579 | DPRINTF(stderr,"[%u] %s: stream=%p old=%s,%s cur=%s,%s " |
| 4580 | "req->analysers=0x%08x res->analysers=0x%08x\n", |
| 4581 | now_ms, __FUNCTION__, s, |
Willy Tarreau | 0da5b3b | 2017-09-21 09:30:46 +0200 | [diff] [blame] | 4582 | h1_msg_state_str(old_req_state), h1_msg_state_str(old_res_state), |
| 4583 | 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] | 4584 | s->req.analysers, s->res.analysers); |
Christopher Faulet | 814d270 | 2017-03-30 11:33:44 +0200 | [diff] [blame] | 4585 | |
| 4586 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4587 | /* OK, both state machines agree on a compatible state. |
| 4588 | * There are a few cases we're interested in : |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4589 | * - HTTP_MSG_CLOSED on both sides means we've reached the end in both |
| 4590 | * directions, so let's simply disable both analysers. |
Christopher Faulet | f77bb53 | 2017-07-18 11:18:46 +0200 | [diff] [blame] | 4591 | * - HTTP_MSG_CLOSED on the response only or HTTP_MSG_ERROR on either |
| 4592 | * means we must abort the request. |
| 4593 | * - HTTP_MSG_TUNNEL on either means we have to disable analyser on |
| 4594 | * corresponding channel. |
| 4595 | * - HTTP_MSG_DONE or HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE |
| 4596 | * on the response with server-close mode means we've completed one |
| 4597 | * request and we must re-initialize the server connection. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4598 | */ |
Christopher Faulet | f77bb53 | 2017-07-18 11:18:46 +0200 | [diff] [blame] | 4599 | if (txn->req.msg_state == HTTP_MSG_CLOSED && |
| 4600 | txn->rsp.msg_state == HTTP_MSG_CLOSED) { |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4601 | s->req.analysers &= AN_REQ_FLT_END; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4602 | channel_auto_close(&s->req); |
| 4603 | channel_auto_read(&s->req); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4604 | s->res.analysers &= AN_RES_FLT_END; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4605 | channel_auto_close(&s->res); |
| 4606 | channel_auto_read(&s->res); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4607 | } |
Christopher Faulet | f77bb53 | 2017-07-18 11:18:46 +0200 | [diff] [blame] | 4608 | else if (txn->rsp.msg_state == HTTP_MSG_CLOSED || |
| 4609 | txn->rsp.msg_state == HTTP_MSG_ERROR || |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 4610 | txn->req.msg_state == HTTP_MSG_ERROR) { |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4611 | s->res.analysers &= AN_RES_FLT_END; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4612 | channel_auto_close(&s->res); |
| 4613 | channel_auto_read(&s->res); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4614 | s->req.analysers &= AN_REQ_FLT_END; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4615 | channel_abort(&s->req); |
| 4616 | channel_auto_close(&s->req); |
| 4617 | channel_auto_read(&s->req); |
| 4618 | channel_truncate(&s->req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4619 | } |
Christopher Faulet | f77bb53 | 2017-07-18 11:18:46 +0200 | [diff] [blame] | 4620 | else if (txn->req.msg_state == HTTP_MSG_TUNNEL || |
| 4621 | txn->rsp.msg_state == HTTP_MSG_TUNNEL) { |
| 4622 | if (txn->req.msg_state == HTTP_MSG_TUNNEL) { |
| 4623 | s->req.analysers &= AN_REQ_FLT_END; |
| 4624 | if (HAS_REQ_DATA_FILTERS(s)) |
| 4625 | s->req.analysers |= AN_REQ_FLT_XFER_DATA; |
| 4626 | } |
| 4627 | if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) { |
| 4628 | s->res.analysers &= AN_RES_FLT_END; |
| 4629 | if (HAS_RSP_DATA_FILTERS(s)) |
| 4630 | s->res.analysers |= AN_RES_FLT_XFER_DATA; |
| 4631 | } |
| 4632 | channel_auto_close(&s->req); |
| 4633 | channel_auto_read(&s->req); |
| 4634 | channel_auto_close(&s->res); |
| 4635 | channel_auto_read(&s->res); |
| 4636 | } |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4637 | else if ((txn->req.msg_state == HTTP_MSG_DONE || |
| 4638 | txn->req.msg_state == HTTP_MSG_CLOSED) && |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4639 | txn->rsp.msg_state == HTTP_MSG_DONE && |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4640 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL || |
| 4641 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) { |
| 4642 | /* server-close/keep-alive: terminate this transaction, |
| 4643 | * possibly killing the server connection and reinitialize |
Willy Tarreau | 3de5bd6 | 2016-05-02 16:07:07 +0200 | [diff] [blame] | 4644 | * a fresh-new transaction, but only once we're sure there's |
| 4645 | * enough room in the request and response buffer to process |
Christopher Faulet | c0c672a | 2017-03-28 11:51:33 +0200 | [diff] [blame] | 4646 | * another request. They must not hold any pending output data |
| 4647 | * and the response buffer must realigned |
| 4648 | * (realign is done is http_end_txn_clean_session). |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4649 | */ |
Christopher Faulet | 894da4c | 2017-07-18 11:29:07 +0200 | [diff] [blame] | 4650 | if (s->req.buf->o) |
Willy Tarreau | 3de5bd6 | 2016-05-02 16:07:07 +0200 | [diff] [blame] | 4651 | s->req.flags |= CF_WAKE_WRITE; |
Christopher Faulet | 894da4c | 2017-07-18 11:29:07 +0200 | [diff] [blame] | 4652 | else if (s->res.buf->o) |
Willy Tarreau | 3de5bd6 | 2016-05-02 16:07:07 +0200 | [diff] [blame] | 4653 | s->res.flags |= CF_WAKE_WRITE; |
Christopher Faulet | a81ff60 | 2017-07-18 22:01:05 +0200 | [diff] [blame] | 4654 | else { |
| 4655 | s->req.analysers = AN_REQ_FLT_END; |
| 4656 | s->res.analysers = AN_RES_FLT_END; |
| 4657 | txn->flags |= TX_WAIT_CLEANUP; |
| 4658 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4659 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4660 | } |
| 4661 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4662 | /* This function is an analyser which forwards request body (including chunk |
| 4663 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 4664 | * zero byte. The only situation where it must not be called is when we're in |
| 4665 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 4666 | * remaining data and to resync after end of body. It expects the msg_state to |
| 4667 | * 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] | 4668 | * 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] | 4669 | * 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] | 4670 | * bytes of pending data + the headers if not already done. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4671 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4672 | 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] | 4673 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4674 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4675 | struct http_txn *txn = s->txn; |
| 4676 | struct http_msg *msg = &s->txn->req; |
Christopher Faulet | 3e34429 | 2015-11-24 16:24:13 +0100 | [diff] [blame] | 4677 | int ret; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4678 | |
Christopher Faulet | 814d270 | 2017-03-30 11:33:44 +0200 | [diff] [blame] | 4679 | DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n", |
| 4680 | now_ms, __FUNCTION__, |
| 4681 | s, |
| 4682 | req, |
| 4683 | req->rex, req->wex, |
| 4684 | req->flags, |
| 4685 | req->buf->i, |
| 4686 | req->analysers); |
| 4687 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4688 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4689 | return 0; |
| 4690 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4691 | 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] | 4692 | ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) { |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 4693 | /* Output closed while we were sending data. We must abort and |
| 4694 | * wake the other side up. |
| 4695 | */ |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 4696 | msg->err_state = msg->msg_state; |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 4697 | msg->msg_state = HTTP_MSG_ERROR; |
| 4698 | http_resync_states(s); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 4699 | return 1; |
| 4700 | } |
| 4701 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4702 | /* Note that we don't have to send 100-continue back because we don't |
| 4703 | * need the data to complete our job, and it's up to the server to |
| 4704 | * decide whether to return 100, 417 or anything else in return of |
| 4705 | * an "Expect: 100-continue" header. |
| 4706 | */ |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 4707 | if (msg->msg_state == HTTP_MSG_BODY) { |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4708 | msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK) |
| 4709 | ? HTTP_MSG_CHUNK_SIZE |
| 4710 | : HTTP_MSG_DATA); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 4711 | |
| 4712 | /* TODO/filters: when http-buffer-request option is set or if a |
| 4713 | * rule on url_param exists, the first chunk size could be |
| 4714 | * already parsed. In that case, msg->next is after the chunk |
| 4715 | * size (including the CRLF after the size). So this case should |
| 4716 | * be handled to */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4717 | } |
| 4718 | |
Willy Tarreau | 7ba2354 | 2014-04-17 21:50:00 +0200 | [diff] [blame] | 4719 | /* Some post-connect processing might want us to refrain from starting to |
| 4720 | * forward data. Currently, the only reason for this is "balance url_param" |
| 4721 | * whichs need to parse/process the request after we've enabled forwarding. |
| 4722 | */ |
| 4723 | if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4724 | if (!(s->res.flags & CF_READ_ATTACHED)) { |
Willy Tarreau | 7ba2354 | 2014-04-17 21:50:00 +0200 | [diff] [blame] | 4725 | channel_auto_connect(req); |
Willy Tarreau | 644c101 | 2014-04-30 18:11:11 +0200 | [diff] [blame] | 4726 | req->flags |= CF_WAKE_CONNECT; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4727 | goto missing_data_or_waiting; |
Willy Tarreau | 7ba2354 | 2014-04-17 21:50:00 +0200 | [diff] [blame] | 4728 | } |
| 4729 | msg->flags &= ~HTTP_MSGF_WAIT_CONN; |
| 4730 | } |
| 4731 | |
Willy Tarreau | 80a92c0 | 2014-03-12 10:41:13 +0100 | [diff] [blame] | 4732 | /* in most states, we should abort in case of early close */ |
| 4733 | channel_auto_close(req); |
| 4734 | |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 4735 | if (req->to_forward) { |
| 4736 | /* We can't process the buffer's contents yet */ |
| 4737 | req->flags |= CF_WAKE_WRITE; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4738 | goto missing_data_or_waiting; |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 4739 | } |
| 4740 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4741 | if (msg->msg_state < HTTP_MSG_DONE) { |
| 4742 | ret = ((msg->flags & HTTP_MSGF_TE_CHNK) |
| 4743 | ? http_msg_forward_chunked_body(s, msg) |
| 4744 | : http_msg_forward_body(s, msg)); |
| 4745 | if (!ret) |
| 4746 | goto missing_data_or_waiting; |
| 4747 | if (ret < 0) |
| 4748 | goto return_bad_req; |
| 4749 | } |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 4750 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4751 | /* other states, DONE...TUNNEL */ |
| 4752 | /* we don't want to forward closes on DONE except in tunnel mode. */ |
| 4753 | if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) |
| 4754 | channel_dont_close(req); |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 4755 | |
Christopher Faulet | 894da4c | 2017-07-18 11:29:07 +0200 | [diff] [blame] | 4756 | http_resync_states(s); |
| 4757 | if (!(req->analysers & an_bit)) { |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4758 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 4759 | if (req->flags & CF_SHUTW) { |
| 4760 | /* request errors are most likely due to the |
| 4761 | * server aborting the transfer. */ |
| 4762 | goto aborted_xfer; |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 4763 | } |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4764 | if (msg->err_pos >= 0) |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 4765 | http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, s->be); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4766 | goto return_bad_req; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4767 | } |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4768 | return 1; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4769 | } |
| 4770 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4771 | /* If "option abortonclose" is set on the backend, we want to monitor |
| 4772 | * the client's connection and forward any shutdown notification to the |
| 4773 | * server, which will decide whether to close or to go on processing the |
| 4774 | * request. We only do that in tunnel mode, and not in other modes since |
| 4775 | * it can be abused to exhaust source ports. */ |
| 4776 | if (s->be->options & PR_O_ABRT_CLOSE) { |
| 4777 | channel_auto_read(req); |
| 4778 | if ((req->flags & (CF_SHUTR|CF_READ_NULL)) && |
| 4779 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)) |
| 4780 | s->si[1].flags |= SI_FL_NOLINGER; |
| 4781 | channel_auto_close(req); |
| 4782 | } |
| 4783 | else if (s->txn->meth == HTTP_METH_POST) { |
| 4784 | /* POST requests may require to read extra CRLF sent by broken |
| 4785 | * browsers and which could cause an RST to be sent upon close |
| 4786 | * on some systems (eg: Linux). */ |
| 4787 | channel_auto_read(req); |
| 4788 | } |
| 4789 | return 0; |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 4790 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4791 | missing_data_or_waiting: |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4792 | /* stop waiting for data if the input is closed before the end */ |
Christopher Faulet | a33510b | 2017-03-31 15:37:29 +0200 | [diff] [blame] | 4793 | if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR) { |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4794 | if (!(s->flags & SF_ERR_MASK)) |
| 4795 | s->flags |= SF_ERR_CLICL; |
| 4796 | if (!(s->flags & SF_FINST_MASK)) { |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4797 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4798 | s->flags |= SF_FINST_H; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4799 | else |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4800 | s->flags |= SF_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4801 | } |
| 4802 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4803 | sess->fe->fe_counters.cli_aborts++; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4804 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4805 | if (objt_server(s->target)) |
| 4806 | objt_server(s->target)->counters.cli_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4807 | |
| 4808 | goto return_bad_req_stats_ok; |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 4809 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4810 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4811 | /* waiting for the last bits to leave the buffer */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4812 | if (req->flags & CF_SHUTW) |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4813 | goto aborted_xfer; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4814 | |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 4815 | /* 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] | 4816 | * 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] | 4817 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4818 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4819 | channel_dont_close(req); |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 4820 | |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 4821 | /* 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] | 4822 | * 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] | 4823 | * 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] | 4824 | * modes are already handled by the stream sock layer. We must not do |
| 4825 | * this in content-length mode because it could present the MSG_MORE |
| 4826 | * flag with the last block of forwarded data, which would cause an |
| 4827 | * additional delay to be observed by the receiver. |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 4828 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4829 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4830 | req->flags |= CF_EXPECT_MORE; |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 4831 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4832 | return 0; |
| 4833 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4834 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4835 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4836 | if (sess->listener->counters) |
| 4837 | sess->listener->counters->failed_req++; |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 4838 | |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4839 | return_bad_req_stats_ok: |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 4840 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4841 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4842 | if (txn->status) { |
| 4843 | /* 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] | 4844 | http_reply_and_close(s, txn->status, NULL); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4845 | } else { |
| 4846 | txn->status = 400; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 4847 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4848 | } |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4849 | req->analysers &= AN_REQ_FLT_END; |
| 4850 | 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] | 4851 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4852 | if (!(s->flags & SF_ERR_MASK)) |
| 4853 | s->flags |= SF_ERR_PRXCOND; |
| 4854 | if (!(s->flags & SF_FINST_MASK)) { |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4855 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4856 | s->flags |= SF_FINST_H; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4857 | else |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4858 | s->flags |= SF_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4859 | } |
| 4860 | return 0; |
| 4861 | |
| 4862 | aborted_xfer: |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 4863 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4864 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4865 | if (txn->status) { |
| 4866 | /* 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] | 4867 | http_reply_and_close(s, txn->status, NULL); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4868 | } else { |
| 4869 | txn->status = 502; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 4870 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4871 | } |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4872 | req->analysers &= AN_REQ_FLT_END; |
| 4873 | 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] | 4874 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4875 | sess->fe->fe_counters.srv_aborts++; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4876 | s->be->be_counters.srv_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4877 | if (objt_server(s->target)) |
| 4878 | objt_server(s->target)->counters.srv_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4879 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4880 | if (!(s->flags & SF_ERR_MASK)) |
| 4881 | s->flags |= SF_ERR_SRVCL; |
| 4882 | if (!(s->flags & SF_FINST_MASK)) { |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4883 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4884 | s->flags |= SF_FINST_H; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4885 | else |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4886 | s->flags |= SF_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4887 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4888 | return 0; |
| 4889 | } |
| 4890 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4891 | /* This stream analyser waits for a complete HTTP response. It returns 1 if the |
| 4892 | * processing can continue on next analysers, or zero if it either needs more |
| 4893 | * data or wants to immediately abort the response (eg: timeout, error, ...). It |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4894 | * 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] | 4895 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 4896 | * abort. |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 4897 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4898 | 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] | 4899 | { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4900 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4901 | struct http_txn *txn = s->txn; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4902 | struct http_msg *msg = &txn->rsp; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4903 | struct hdr_ctx ctx; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4904 | int use_close_only; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4905 | int cur_idx; |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 4906 | int n; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 4907 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4908 | 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] | 4909 | now_ms, __FUNCTION__, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4910 | s, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 4911 | rep, |
| 4912 | rep->rex, rep->wex, |
| 4913 | rep->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4914 | rep->buf->i, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 4915 | rep->analysers); |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 4916 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4917 | /* |
| 4918 | * Now parse the partial (or complete) lines. |
| 4919 | * We will check the response syntax, and also join multi-line |
| 4920 | * headers. An index of all the lines will be elaborated while |
| 4921 | * parsing. |
| 4922 | * |
| 4923 | * For the parsing, we use a 28 states FSM. |
| 4924 | * |
| 4925 | * Here is the information we currently have : |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4926 | * rep->buf->p = beginning of response |
| 4927 | * rep->buf->p + msg->eoh = end of processed headers / start of current one |
| 4928 | * rep->buf->p + rep->buf->i = end of input data |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 4929 | * msg->eol = end of current header or line (LF or CRLF) |
| 4930 | * msg->next = first non-visited byte |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4931 | */ |
| 4932 | |
Willy Tarreau | 628c40c | 2014-04-24 19:11:26 +0200 | [diff] [blame] | 4933 | next_one: |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 4934 | /* There's a protected area at the end of the buffer for rewriting |
| 4935 | * purposes. We don't want to start to parse the request if the |
| 4936 | * protected area is affected, because we may have to move processed |
| 4937 | * data later, which is much more complicated. |
| 4938 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4939 | if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) { |
Willy Tarreau | ba0902e | 2015-01-13 14:39:16 +0100 | [diff] [blame] | 4940 | if (unlikely(!channel_is_rewritable(rep))) { |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 4941 | /* some data has still not left the buffer, wake us once that's done */ |
| 4942 | if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) |
| 4943 | goto abort_response; |
| 4944 | channel_dont_close(rep); |
| 4945 | rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | d7ad9f5 | 2013-12-31 17:26:25 +0100 | [diff] [blame] | 4946 | rep->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 4947 | return 0; |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 4948 | } |
| 4949 | |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 4950 | if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) || |
| 4951 | bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite)) |
| 4952 | buffer_slow_realign(rep->buf); |
| 4953 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4954 | if (likely(msg->next < rep->buf->i)) |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 4955 | http_msg_analyzer(msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 4956 | } |
| 4957 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4958 | /* 1: we might have to print this header in debug mode */ |
| 4959 | if (unlikely((global.mode & MODE_DEBUG) && |
| 4960 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | 7d59e90 | 2014-10-21 19:36:09 +0200 | [diff] [blame] | 4961 | msg->msg_state >= HTTP_MSG_BODY)) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4962 | char *eol, *sol; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4963 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4964 | sol = rep->buf->p; |
| 4965 | 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] | 4966 | debug_hdr("srvrep", s, sol, eol); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4967 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4968 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 4969 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4970 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4971 | while (cur_idx) { |
| 4972 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 4973 | debug_hdr("srvhdr", s, sol, eol); |
| 4974 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 4975 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
| 4976 | } |
| 4977 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4978 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4979 | /* |
| 4980 | * Now we quickly check if we have found a full valid response. |
| 4981 | * If not so, we check the FD and buffer states before leaving. |
| 4982 | * A full response is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4983 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4984 | * responses are checked first. |
| 4985 | * |
| 4986 | * Depending on whether the client is still there or not, we |
| 4987 | * may send an error response back or not. Note that normally |
| 4988 | * we should only check for HTTP status there, and check I/O |
| 4989 | * errors somewhere else. |
| 4990 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4991 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4992 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4993 | /* Invalid response */ |
| 4994 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 4995 | /* we detected a parsing error. We want to archive this response |
| 4996 | * in the dedicated proxy area for later troubleshooting. |
| 4997 | */ |
| 4998 | hdr_response_bad: |
| 4999 | if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0) |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 5000 | http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, sess->fe); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5001 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5002 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5003 | if (objt_server(s->target)) { |
| 5004 | objt_server(s->target)->counters.failed_resp++; |
| 5005 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5006 | } |
Willy Tarreau | 6464841 | 2010-03-05 10:41:54 +0100 | [diff] [blame] | 5007 | abort_response: |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5008 | channel_auto_close(rep); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 5009 | rep->analysers &= AN_RES_FLT_END; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5010 | txn->status = 502; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5011 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5012 | channel_truncate(rep); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 5013 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5014 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5015 | if (!(s->flags & SF_ERR_MASK)) |
| 5016 | s->flags |= SF_ERR_PRXCOND; |
| 5017 | if (!(s->flags & SF_FINST_MASK)) |
| 5018 | s->flags |= SF_FINST_H; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5019 | |
| 5020 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5021 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5022 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5023 | /* too large response does not fit in buffer. */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5024 | else if (buffer_full(rep->buf, global.tune.maxrewrite)) { |
Willy Tarreau | fec4d89 | 2011-09-02 20:04:57 +0200 | [diff] [blame] | 5025 | if (msg->err_pos < 0) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5026 | msg->err_pos = rep->buf->i; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5027 | goto hdr_response_bad; |
| 5028 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5029 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5030 | /* read error */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5031 | else if (rep->flags & CF_READ_ERROR) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5032 | if (msg->err_pos >= 0) |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 5033 | http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, sess->fe); |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 5034 | else if (txn->flags & TX_NOT_FIRST) |
| 5035 | goto abort_keep_alive; |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 5036 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5037 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5038 | if (objt_server(s->target)) { |
| 5039 | objt_server(s->target)->counters.failed_resp++; |
| 5040 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5041 | } |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 5042 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5043 | channel_auto_close(rep); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 5044 | rep->analysers &= AN_RES_FLT_END; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5045 | txn->status = 502; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5046 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5047 | channel_truncate(rep); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 5048 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 5049 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5050 | if (!(s->flags & SF_ERR_MASK)) |
| 5051 | s->flags |= SF_ERR_SRVCL; |
| 5052 | if (!(s->flags & SF_FINST_MASK)) |
| 5053 | s->flags |= SF_FINST_H; |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 5054 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5055 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5056 | |
Willy Tarreau | 6f0a7ba | 2014-06-23 15:22:31 +0200 | [diff] [blame] | 5057 | /* read timeout : return a 504 to the client. */ |
| 5058 | else if (rep->flags & CF_READ_TIMEOUT) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5059 | if (msg->err_pos >= 0) |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 5060 | http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, sess->fe); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5061 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5062 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5063 | if (objt_server(s->target)) { |
| 5064 | objt_server(s->target)->counters.failed_resp++; |
| 5065 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5066 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5067 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5068 | channel_auto_close(rep); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 5069 | rep->analysers &= AN_RES_FLT_END; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5070 | txn->status = 504; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5071 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5072 | channel_truncate(rep); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 5073 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 5074 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5075 | if (!(s->flags & SF_ERR_MASK)) |
| 5076 | s->flags |= SF_ERR_SRVTO; |
| 5077 | if (!(s->flags & SF_FINST_MASK)) |
| 5078 | s->flags |= SF_FINST_H; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5079 | return 0; |
| 5080 | } |
Willy Tarreau | a7c5276 | 2008-08-16 18:40:18 +0200 | [diff] [blame] | 5081 | |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 5082 | /* client abort with an abortonclose */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5083 | else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5084 | sess->fe->fe_counters.cli_aborts++; |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 5085 | s->be->be_counters.cli_aborts++; |
| 5086 | if (objt_server(s->target)) |
| 5087 | objt_server(s->target)->counters.cli_aborts++; |
| 5088 | |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 5089 | rep->analysers &= AN_RES_FLT_END; |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 5090 | channel_auto_close(rep); |
| 5091 | |
| 5092 | txn->status = 400; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5093 | channel_truncate(rep); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 5094 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 5095 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5096 | if (!(s->flags & SF_ERR_MASK)) |
| 5097 | s->flags |= SF_ERR_CLICL; |
| 5098 | if (!(s->flags & SF_FINST_MASK)) |
| 5099 | s->flags |= SF_FINST_H; |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 5100 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5101 | /* process_stream() will take care of the error */ |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 5102 | return 0; |
| 5103 | } |
| 5104 | |
Willy Tarreau | 3b8c08a | 2011-09-02 20:16:24 +0200 | [diff] [blame] | 5105 | /* 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] | 5106 | else if (rep->flags & CF_SHUTR) { |
Willy Tarreau | 3b8c08a | 2011-09-02 20:16:24 +0200 | [diff] [blame] | 5107 | if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0) |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 5108 | http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, sess->fe); |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 5109 | else if (txn->flags & TX_NOT_FIRST) |
| 5110 | goto abort_keep_alive; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5111 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5112 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5113 | if (objt_server(s->target)) { |
| 5114 | objt_server(s->target)->counters.failed_resp++; |
| 5115 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5116 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5117 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5118 | channel_auto_close(rep); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 5119 | rep->analysers &= AN_RES_FLT_END; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5120 | txn->status = 502; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5121 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5122 | channel_truncate(rep); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 5123 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5124 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5125 | if (!(s->flags & SF_ERR_MASK)) |
| 5126 | s->flags |= SF_ERR_SRVCL; |
| 5127 | if (!(s->flags & SF_FINST_MASK)) |
| 5128 | s->flags |= SF_FINST_H; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5129 | return 0; |
| 5130 | } |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 5131 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5132 | /* write error to client (we don't send any message then) */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5133 | else if (rep->flags & CF_WRITE_ERROR) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5134 | if (msg->err_pos >= 0) |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 5135 | http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, sess->fe); |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 5136 | else if (txn->flags & TX_NOT_FIRST) |
| 5137 | goto abort_keep_alive; |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 5138 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5139 | s->be->be_counters.failed_resp++; |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 5140 | rep->analysers &= AN_RES_FLT_END; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5141 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5142 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5143 | if (!(s->flags & SF_ERR_MASK)) |
| 5144 | s->flags |= SF_ERR_CLICL; |
| 5145 | if (!(s->flags & SF_FINST_MASK)) |
| 5146 | s->flags |= SF_FINST_H; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5147 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5148 | /* process_stream() will take care of the error */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5149 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5150 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5151 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5152 | channel_dont_close(rep); |
Willy Tarreau | 3f3997e | 2013-12-15 15:21:32 +0100 | [diff] [blame] | 5153 | rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5154 | return 0; |
| 5155 | } |
| 5156 | |
| 5157 | /* More interesting part now : we know that we have a complete |
| 5158 | * response which at least looks like HTTP. We have an indicator |
| 5159 | * of each header's length, so we can parse them quickly. |
| 5160 | */ |
| 5161 | |
| 5162 | if (unlikely(msg->err_pos >= 0)) |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 5163 | http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, sess->fe); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5164 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5165 | /* |
| 5166 | * 1: get the status code |
| 5167 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5168 | n = rep->buf->p[msg->sl.st.c] - '0'; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5169 | if (n < 1 || n > 5) |
| 5170 | n = 0; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5171 | /* when the client triggers a 4xx from the server, it's most often due |
| 5172 | * to a missing object or permission. These events should be tracked |
| 5173 | * because if they happen often, it may indicate a brute force or a |
| 5174 | * vulnerability scan. |
| 5175 | */ |
| 5176 | if (n == 4) |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5177 | stream_inc_http_err_ctr(s); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5178 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5179 | if (objt_server(s->target)) |
| 5180 | objt_server(s->target)->counters.p.http.rsp[n]++; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5181 | |
Willy Tarreau | 91852eb | 2015-05-01 13:26:00 +0200 | [diff] [blame] | 5182 | /* RFC7230#2.6 has enforced the format of the HTTP version string to be |
| 5183 | * exactly one digit "." one digit. This check may be disabled using |
| 5184 | * option accept-invalid-http-response. |
| 5185 | */ |
| 5186 | if (!(s->be->options2 & PR_O2_RSPBUG_OK)) { |
| 5187 | if (msg->sl.st.v_l != 8) { |
| 5188 | msg->err_pos = 0; |
| 5189 | goto hdr_response_bad; |
| 5190 | } |
| 5191 | |
| 5192 | if (rep->buf->p[4] != '/' || |
| 5193 | !isdigit((unsigned char)rep->buf->p[5]) || |
| 5194 | rep->buf->p[6] != '.' || |
| 5195 | !isdigit((unsigned char)rep->buf->p[7])) { |
| 5196 | msg->err_pos = 4; |
| 5197 | goto hdr_response_bad; |
| 5198 | } |
| 5199 | } |
| 5200 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5201 | /* check if the response is HTTP/1.1 or above */ |
| 5202 | if ((msg->sl.st.v_l == 8) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5203 | ((rep->buf->p[5] > '1') || |
| 5204 | ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1')))) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5205 | msg->flags |= HTTP_MSGF_VER_11; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5206 | |
| 5207 | /* "connection" has not been parsed yet */ |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 5208 | 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] | 5209 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5210 | /* transfer length unknown*/ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5211 | msg->flags &= ~HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5212 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5213 | 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] | 5214 | |
Willy Tarreau | 3965040 | 2010-03-15 19:44:39 +0100 | [diff] [blame] | 5215 | /* Adjust server's health based on status code. Note: status codes 501 |
| 5216 | * and 505 are triggered on demand by client request, so we must not |
| 5217 | * count them as server failures. |
| 5218 | */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5219 | if (objt_server(s->target)) { |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 5220 | 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] | 5221 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK); |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 5222 | else |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5223 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS); |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 5224 | } |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5225 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5226 | /* |
Willy Tarreau | a14ad72 | 2017-07-07 11:36:32 +0200 | [diff] [blame] | 5227 | * We may be facing a 100-continue response, or any other informational |
| 5228 | * 1xx response which is non-final, in which case this is not the right |
| 5229 | * response, and we're waiting for the next one. Let's allow this response |
| 5230 | * to go to the client and wait for the next one. There's an exception for |
| 5231 | * 101 which is used later in the code to switch protocols. |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5232 | */ |
Willy Tarreau | a14ad72 | 2017-07-07 11:36:32 +0200 | [diff] [blame] | 5233 | if (txn->status < 200 && |
| 5234 | (txn->status == 100 || txn->status >= 102)) { |
Willy Tarreau | 628c40c | 2014-04-24 19:11:26 +0200 | [diff] [blame] | 5235 | hdr_idx_init(&txn->hdr_idx); |
| 5236 | msg->next -= channel_forward(rep, msg->next); |
| 5237 | msg->msg_state = HTTP_MSG_RPBEFORE; |
| 5238 | txn->status = 0; |
| 5239 | s->logs.t_data = -1; /* was not a response yet */ |
Christopher Faulet | 3e34429 | 2015-11-24 16:24:13 +0100 | [diff] [blame] | 5240 | FLT_STRM_CB(s, flt_http_reset(s, msg)); |
Willy Tarreau | 628c40c | 2014-04-24 19:11:26 +0200 | [diff] [blame] | 5241 | goto next_one; |
Willy Tarreau | a14ad72 | 2017-07-07 11:36:32 +0200 | [diff] [blame] | 5242 | } |
Willy Tarreau | 628c40c | 2014-04-24 19:11:26 +0200 | [diff] [blame] | 5243 | |
Willy Tarreau | a14ad72 | 2017-07-07 11:36:32 +0200 | [diff] [blame] | 5244 | /* |
| 5245 | * 2: check for cacheability. |
| 5246 | */ |
| 5247 | |
| 5248 | switch (txn->status) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5249 | case 200: |
| 5250 | case 203: |
| 5251 | case 206: |
| 5252 | case 300: |
| 5253 | case 301: |
| 5254 | case 410: |
| 5255 | /* RFC2616 @13.4: |
| 5256 | * "A response received with a status code of |
| 5257 | * 200, 203, 206, 300, 301 or 410 MAY be stored |
| 5258 | * by a cache (...) unless a cache-control |
| 5259 | * directive prohibits caching." |
| 5260 | * |
| 5261 | * RFC2616 @9.5: POST method : |
| 5262 | * "Responses to this method are not cacheable, |
| 5263 | * unless the response includes appropriate |
| 5264 | * Cache-Control or Expires header fields." |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5265 | */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5266 | if (likely(txn->meth != HTTP_METH_POST) && |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 5267 | ((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] | 5268 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
| 5269 | break; |
| 5270 | default: |
| 5271 | break; |
| 5272 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5273 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5274 | /* |
| 5275 | * 3: we may need to capture headers |
| 5276 | */ |
| 5277 | s->logs.logwait &= ~LW_RESP; |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 5278 | if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap)) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5279 | capture_headers(rep->buf->p, &txn->hdr_idx, |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 5280 | s->res_cap, sess->fe->rsp_cap); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5281 | |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 5282 | /* 4: determine the transfer-length according to RFC2616 #4.4, updated |
| 5283 | * by RFC7230#3.3.3 : |
| 5284 | * |
| 5285 | * The length of a message body is determined by one of the following |
| 5286 | * (in order of precedence): |
| 5287 | * |
Christopher Faulet | d1cd209 | 2016-11-28 10:14:03 +0100 | [diff] [blame] | 5288 | * 1. Any 2xx (Successful) response to a CONNECT request implies that |
| 5289 | * the connection will become a tunnel immediately after the empty |
| 5290 | * line that concludes the header fields. A client MUST ignore |
| 5291 | * any Content-Length or Transfer-Encoding header fields received |
| 5292 | * in such a message. Any 101 response (Switching Protocols) is |
| 5293 | * managed in the same manner. |
| 5294 | * |
| 5295 | * 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] | 5296 | * (Informational), 204 (No Content), or 304 (Not Modified) status |
| 5297 | * code is always terminated by the first empty line after the |
| 5298 | * header fields, regardless of the header fields present in the |
| 5299 | * message, and thus cannot contain a message body. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5300 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 5301 | * 3. If a Transfer-Encoding header field is present and the chunked |
| 5302 | * transfer coding (Section 4.1) is the final encoding, the message |
| 5303 | * body length is determined by reading and decoding the chunked |
| 5304 | * data until the transfer coding indicates the data is complete. |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5305 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 5306 | * If a Transfer-Encoding header field is present in a response and |
| 5307 | * the chunked transfer coding is not the final encoding, the |
| 5308 | * message body length is determined by reading the connection until |
| 5309 | * it is closed by the server. If a Transfer-Encoding header field |
| 5310 | * is present in a request and the chunked transfer coding is not |
| 5311 | * the final encoding, the message body length cannot be determined |
| 5312 | * reliably; the server MUST respond with the 400 (Bad Request) |
| 5313 | * status code and then close the connection. |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5314 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 5315 | * If a message is received with both a Transfer-Encoding and a |
| 5316 | * Content-Length header field, the Transfer-Encoding overrides the |
| 5317 | * Content-Length. Such a message might indicate an attempt to |
| 5318 | * perform request smuggling (Section 9.5) or response splitting |
| 5319 | * (Section 9.4) and ought to be handled as an error. A sender MUST |
| 5320 | * remove the received Content-Length field prior to forwarding such |
| 5321 | * a message downstream. |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5322 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 5323 | * 4. If a message is received without Transfer-Encoding and with |
| 5324 | * either multiple Content-Length header fields having differing |
| 5325 | * field-values or a single Content-Length header field having an |
| 5326 | * invalid value, then the message framing is invalid and the |
| 5327 | * recipient MUST treat it as an unrecoverable error. If this is a |
| 5328 | * request message, the server MUST respond with a 400 (Bad Request) |
| 5329 | * status code and then close the connection. If this is a response |
| 5330 | * message received by a proxy, the proxy MUST close the connection |
| 5331 | * to the server, discard the received response, and send a 502 (Bad |
| 5332 | * Gateway) response to the client. If this is a response message |
| 5333 | * received by a user agent, the user agent MUST close the |
| 5334 | * connection to the server and discard the received response. |
| 5335 | * |
| 5336 | * 5. If a valid Content-Length header field is present without |
| 5337 | * Transfer-Encoding, its decimal value defines the expected message |
| 5338 | * body length in octets. If the sender closes the connection or |
| 5339 | * the recipient times out before the indicated number of octets are |
| 5340 | * received, the recipient MUST consider the message to be |
| 5341 | * incomplete and close the connection. |
| 5342 | * |
| 5343 | * 6. If this is a request message and none of the above are true, then |
| 5344 | * the message body length is zero (no message body is present). |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5345 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 5346 | * 7. Otherwise, this is a response message without a declared message |
| 5347 | * body length, so the message body length is determined by the |
| 5348 | * number of octets received prior to the server closing the |
| 5349 | * connection. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5350 | */ |
| 5351 | |
| 5352 | /* Skip parsing if no content length is possible. The response flags |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 5353 | * 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] | 5354 | * 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] | 5355 | * FIXME: should we parse anyway and return an error on chunked encoding ? |
| 5356 | */ |
Christopher Faulet | d1cd209 | 2016-11-28 10:14:03 +0100 | [diff] [blame] | 5357 | if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || |
| 5358 | txn->status == 101)) { |
| 5359 | /* Either we've established an explicit tunnel, or we're |
| 5360 | * switching the protocol. In both cases, we're very unlikely |
| 5361 | * to understand the next protocols. We have to switch to tunnel |
| 5362 | * mode, so that we transfer the request and responses then let |
| 5363 | * this protocol pass unmodified. When we later implement specific |
| 5364 | * parsers for such protocols, we'll want to check the Upgrade |
| 5365 | * header which contains information about that protocol for |
| 5366 | * responses with status 101 (eg: see RFC2817 about TLS). |
| 5367 | */ |
| 5368 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN; |
| 5369 | msg->flags |= HTTP_MSGF_XFER_LEN; |
| 5370 | goto end; |
| 5371 | } |
| 5372 | |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5373 | if (txn->meth == HTTP_METH_HEAD || |
| 5374 | (txn->status >= 100 && txn->status < 200) || |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5375 | txn->status == 204 || txn->status == 304) { |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5376 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5377 | goto skip_content_length; |
| 5378 | } |
| 5379 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5380 | use_close_only = 0; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5381 | ctx.idx = 0; |
Willy Tarreau | 4979d5c | 2015-05-01 10:06:30 +0200 | [diff] [blame] | 5382 | 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] | 5383 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5384 | msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
| 5385 | else if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5386 | /* bad transfer-encoding (chunked followed by something else) */ |
| 5387 | use_close_only = 1; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5388 | msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5389 | break; |
| 5390 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5391 | } |
| 5392 | |
Willy Tarreau | 1c91391 | 2015-04-30 10:57:51 +0200 | [diff] [blame] | 5393 | /* Chunked responses must have their content-length removed */ |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5394 | ctx.idx = 0; |
Willy Tarreau | b4d0c03 | 2015-05-01 10:25:45 +0200 | [diff] [blame] | 5395 | if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) { |
Willy Tarreau | 1c91391 | 2015-04-30 10:57:51 +0200 | [diff] [blame] | 5396 | while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) |
| 5397 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 5398 | } |
Willy Tarreau | b4d0c03 | 2015-05-01 10:25:45 +0200 | [diff] [blame] | 5399 | 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] | 5400 | signed long long cl; |
| 5401 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5402 | if (!ctx.vlen) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5403 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5404 | goto hdr_response_bad; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5405 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5406 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5407 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5408 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5409 | goto hdr_response_bad; /* parse failure */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5410 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5411 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5412 | if (cl < 0) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5413 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5414 | goto hdr_response_bad; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5415 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5416 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5417 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5418 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5419 | goto hdr_response_bad; /* already specified, was different */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5420 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5421 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5422 | msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 5423 | msg->body_len = msg->chunk_len = cl; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5424 | } |
| 5425 | |
Christopher Faulet | d1cd209 | 2016-11-28 10:14:03 +0100 | [diff] [blame] | 5426 | skip_content_length: |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5427 | /* Now we have to check if we need to modify the Connection header. |
| 5428 | * This is more difficult on the response than it is on the request, |
| 5429 | * because we can have two different HTTP versions and we don't know |
| 5430 | * how the client will interprete a response. For instance, let's say |
| 5431 | * that the client sends a keep-alive request in HTTP/1.0 and gets an |
| 5432 | * HTTP/1.1 response without any header. Maybe it will bound itself to |
| 5433 | * HTTP/1.0 because it only knows about it, and will consider the lack |
| 5434 | * of header as a close, or maybe it knows HTTP/1.1 and can consider |
| 5435 | * the lack of header as a keep-alive. Thus we will use two flags |
| 5436 | * indicating how a request MAY be understood by the client. In case |
| 5437 | * of multiple possibilities, we'll fix the header to be explicit. If |
| 5438 | * ambiguous cases such as both close and keepalive are seen, then we |
| 5439 | * will fall back to explicit close. Note that we won't take risks with |
| 5440 | * HTTP/1.0 clients which may not necessarily understand keep-alive. |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5441 | * See doc/internals/connection-header.txt for the complete matrix. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5442 | */ |
Christopher Faulet | d1cd209 | 2016-11-28 10:14:03 +0100 | [diff] [blame] | 5443 | if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) && |
| 5444 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN || |
| 5445 | ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
| 5446 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) { |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5447 | int to_del = 0; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5448 | |
Willy Tarreau | 70dffda | 2014-01-30 03:07:23 +0100 | [diff] [blame] | 5449 | /* this situation happens when combining pretend-keepalive with httpclose. */ |
| 5450 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5451 | ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 5452 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) |
Willy Tarreau | 70dffda | 2014-01-30 03:07:23 +0100 | [diff] [blame] | 5453 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 5454 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5455 | /* on unknown transfer length, we must close */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5456 | if (!(msg->flags & HTTP_MSGF_XFER_LEN) && |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5457 | (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) |
| 5458 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5459 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5460 | /* now adjust header transformations depending on current state */ |
| 5461 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN || |
| 5462 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 5463 | to_del |= 2; /* remove "keep-alive" on any response */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5464 | if (!(msg->flags & HTTP_MSGF_VER_11)) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5465 | to_del |= 1; /* remove "close" for HTTP/1.0 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5466 | } |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5467 | else { /* SCL / KAL */ |
| 5468 | to_del |= 1; /* remove "close" on any response */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5469 | if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5470 | to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5471 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5472 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5473 | /* Parse and remove some headers from the connection header */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 5474 | http_parse_connection_header(txn, msg, to_del); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5475 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5476 | /* Some keep-alive responses are converted to Server-close if |
| 5477 | * the server wants to close. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5478 | */ |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5479 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) { |
| 5480 | if ((txn->flags & TX_HDR_CONN_CLO) || |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5481 | (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11))) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5482 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 5483 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5484 | } |
| 5485 | |
Christopher Faulet | d1cd209 | 2016-11-28 10:14:03 +0100 | [diff] [blame] | 5486 | end: |
Willy Tarreau | 7959a55 | 2013-09-23 16:44:27 +0200 | [diff] [blame] | 5487 | /* we want to have the response time before we start processing it */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 5488 | s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now); |
Willy Tarreau | 7959a55 | 2013-09-23 16:44:27 +0200 | [diff] [blame] | 5489 | |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 5490 | /* end of job, return OK */ |
| 5491 | rep->analysers &= ~an_bit; |
| 5492 | rep->analyse_exp = TICK_ETERNITY; |
| 5493 | channel_auto_close(rep); |
| 5494 | return 1; |
| 5495 | |
| 5496 | abort_keep_alive: |
| 5497 | /* A keep-alive request to the server failed on a network error. |
| 5498 | * The client is required to retry. We need to close without returning |
| 5499 | * any other information so that the client retries. |
| 5500 | */ |
| 5501 | txn->status = 0; |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 5502 | rep->analysers &= AN_RES_FLT_END; |
| 5503 | s->req.analysers &= AN_REQ_FLT_END; |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 5504 | channel_auto_close(rep); |
| 5505 | s->logs.logwait = 0; |
| 5506 | s->logs.level = 0; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5507 | s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5508 | channel_truncate(rep); |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 5509 | http_reply_and_close(s, txn->status, NULL); |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 5510 | return 0; |
| 5511 | } |
| 5512 | |
| 5513 | /* This function performs all the processing enabled for the current response. |
| 5514 | * 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] | 5515 | * 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] | 5516 | * other functions. It works like process_request (see indications above). |
| 5517 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5518 | 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] | 5519 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 5520 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5521 | struct http_txn *txn = s->txn; |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 5522 | struct http_msg *msg = &txn->rsp; |
| 5523 | struct proxy *cur_proxy; |
| 5524 | struct cond_wordlist *wl; |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 5525 | enum rule_result ret = HTTP_RULE_RES_CONT; |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 5526 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5527 | 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] | 5528 | now_ms, __FUNCTION__, |
| 5529 | s, |
| 5530 | rep, |
| 5531 | rep->rex, rep->wex, |
| 5532 | rep->flags, |
| 5533 | rep->buf->i, |
| 5534 | rep->analysers); |
| 5535 | |
| 5536 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */ |
| 5537 | return 0; |
| 5538 | |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 5539 | /* The stats applet needs to adjust the Connection header but we don't |
| 5540 | * apply any filter there. |
| 5541 | */ |
Willy Tarreau | 612adb8 | 2015-03-10 15:25:54 +0100 | [diff] [blame] | 5542 | if (unlikely(objt_applet(s->target) == &http_stats_applet)) { |
| 5543 | rep->analysers &= ~an_bit; |
| 5544 | rep->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 5545 | goto skip_filters; |
Willy Tarreau | 612adb8 | 2015-03-10 15:25:54 +0100 | [diff] [blame] | 5546 | } |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 5547 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5548 | /* |
| 5549 | * We will have to evaluate the filters. |
| 5550 | * As opposed to version 1.2, now they will be evaluated in the |
| 5551 | * filters order and not in the header order. This means that |
| 5552 | * each filter has to be validated among all headers. |
| 5553 | * |
| 5554 | * Filters are tried with ->be first, then with ->fe if it is |
| 5555 | * different from ->be. |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 5556 | * |
| 5557 | * Maybe we are in resume condiion. In this case I choose the |
| 5558 | * "struct proxy" which contains the rule list matching the resume |
| 5559 | * pointer. If none of theses "struct proxy" match, I initialise |
| 5560 | * the process with the first one. |
| 5561 | * |
| 5562 | * In fact, I check only correspondance betwwen the current list |
| 5563 | * pointer and the ->fe rule list. If it doesn't match, I initialize |
| 5564 | * the loop with the ->be. |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5565 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5566 | if (s->current_rule_list == &sess->fe->http_res_rules) |
| 5567 | cur_proxy = sess->fe; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 5568 | else |
| 5569 | cur_proxy = s->be; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5570 | while (1) { |
| 5571 | struct proxy *rule_set = cur_proxy; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5572 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5573 | /* evaluate http-response rules */ |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 5574 | if (ret == HTTP_RULE_RES_CONT) { |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 5575 | 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] | 5576 | |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 5577 | if (ret == HTTP_RULE_RES_BADREQ) |
| 5578 | goto return_srv_prx_502; |
| 5579 | |
| 5580 | if (ret == HTTP_RULE_RES_DONE) { |
| 5581 | rep->analysers &= ~an_bit; |
| 5582 | rep->analyse_exp = TICK_ETERNITY; |
| 5583 | return 1; |
| 5584 | } |
| 5585 | } |
| 5586 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 5587 | /* we need to be called again. */ |
| 5588 | if (ret == HTTP_RULE_RES_YIELD) { |
| 5589 | channel_dont_close(rep); |
| 5590 | return 0; |
| 5591 | } |
| 5592 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5593 | /* try headers filters */ |
| 5594 | if (rule_set->rsp_exp != NULL) { |
| 5595 | if (apply_filters_to_response(s, rep, rule_set) < 0) { |
| 5596 | return_bad_resp: |
| 5597 | if (objt_server(s->target)) { |
| 5598 | objt_server(s->target)->counters.failed_resp++; |
| 5599 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5600 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5601 | s->be->be_counters.failed_resp++; |
| 5602 | return_srv_prx_502: |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 5603 | rep->analysers &= AN_RES_FLT_END; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5604 | txn->status = 502; |
| 5605 | s->logs.t_data = -1; /* was not a valid response */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5606 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5607 | channel_truncate(rep); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 5608 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5609 | if (!(s->flags & SF_ERR_MASK)) |
| 5610 | s->flags |= SF_ERR_PRXCOND; |
| 5611 | if (!(s->flags & SF_FINST_MASK)) |
| 5612 | s->flags |= SF_FINST_H; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5613 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5614 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5615 | } |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5616 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5617 | /* has the response been denied ? */ |
| 5618 | if (txn->flags & TX_SVDENY) { |
| 5619 | if (objt_server(s->target)) |
| 5620 | objt_server(s->target)->counters.failed_secu++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5621 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5622 | s->be->be_counters.denied_resp++; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5623 | sess->fe->fe_counters.denied_resp++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 5624 | if (sess->listener->counters) |
| 5625 | sess->listener->counters->denied_resp++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5626 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5627 | goto return_srv_prx_502; |
| 5628 | } |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 5629 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5630 | /* add response headers from the rule sets in the same order */ |
| 5631 | list_for_each_entry(wl, &rule_set->rsp_add, list) { |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 5632 | if (txn->status < 200 && txn->status != 101) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5633 | break; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5634 | if (wl->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 5635 | 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] | 5636 | ret = acl_pass(ret); |
| 5637 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 5638 | ret = !ret; |
| 5639 | if (!ret) |
| 5640 | continue; |
| 5641 | } |
| 5642 | if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0)) |
| 5643 | goto return_bad_resp; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5644 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5645 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5646 | /* check whether we're already working on the frontend */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5647 | if (cur_proxy == sess->fe) |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5648 | break; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5649 | cur_proxy = sess->fe; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5650 | } |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5651 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 5652 | /* After this point, this anayzer can't return yield, so we can |
| 5653 | * remove the bit corresponding to this analyzer from the list. |
| 5654 | * |
| 5655 | * Note that the intermediate returns and goto found previously |
| 5656 | * reset the analyzers. |
| 5657 | */ |
| 5658 | rep->analysers &= ~an_bit; |
| 5659 | rep->analyse_exp = TICK_ETERNITY; |
| 5660 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5661 | /* OK that's all we can do for 1xx responses */ |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 5662 | if (unlikely(txn->status < 200 && txn->status != 101)) |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5663 | goto skip_header_mangling; |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5664 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5665 | /* |
| 5666 | * Now check for a server cookie. |
| 5667 | */ |
Willy Tarreau | 53a09d5 | 2015-08-10 18:59:40 +0200 | [diff] [blame] | 5668 | 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] | 5669 | manage_server_side_cookies(s, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5670 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5671 | /* |
| 5672 | * Check for cache-control or pragma headers if required. |
| 5673 | */ |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 5674 | 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] | 5675 | check_response_for_cacheability(s, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5676 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5677 | /* |
| 5678 | * Add server cookie in the response if needed |
| 5679 | */ |
| 5680 | if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) && |
| 5681 | !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) && |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5682 | (!(s->flags & SF_DIRECT) || |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5683 | ((s->be->cookie_maxidle || txn->cookie_last_date) && |
| 5684 | (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) || |
| 5685 | (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date |
| 5686 | (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date |
| 5687 | (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) && |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5688 | !(s->flags & SF_IGNORE_PRST)) { |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5689 | /* the server is known, it's not the one the client requested, or the |
| 5690 | * cookie's last seen date needs to be refreshed. We have to |
| 5691 | * insert a set-cookie here, except if we want to insert only on POST |
| 5692 | * requests and this one isn't. Note that servers which don't have cookies |
| 5693 | * (eg: some backup servers) will return a full cookie removal request. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5694 | */ |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5695 | if (!objt_server(s->target)->cookie) { |
| 5696 | chunk_printf(&trash, |
| 5697 | "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/", |
| 5698 | s->be->cookie_name); |
| 5699 | } |
| 5700 | else { |
| 5701 | 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] | 5702 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5703 | if (s->be->cookie_maxidle || s->be->cookie_maxlife) { |
| 5704 | /* emit last_date, which is mandatory */ |
| 5705 | trash.str[trash.len++] = COOKIE_DELIM_DATE; |
| 5706 | s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len); |
| 5707 | trash.len += 5; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5708 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5709 | if (s->be->cookie_maxlife) { |
| 5710 | /* emit first_date, which is either the original one or |
| 5711 | * the current date. |
| 5712 | */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 5713 | trash.str[trash.len++] = COOKIE_DELIM_DATE; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5714 | s30tob64(txn->cookie_first_date ? |
| 5715 | txn->cookie_first_date >> 2 : |
| 5716 | (date.tv_sec+3) >> 2, trash.str + trash.len); |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 5717 | trash.len += 5; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5718 | } |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5719 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5720 | chunk_appendf(&trash, "; path=/"); |
| 5721 | } |
Willy Tarreau | 4992dd2 | 2012-05-31 21:02:17 +0200 | [diff] [blame] | 5722 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5723 | if (s->be->cookie_domain) |
| 5724 | chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain); |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5725 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5726 | if (s->be->ck_opts & PR_CK_HTTPONLY) |
| 5727 | chunk_appendf(&trash, "; HttpOnly"); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5728 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5729 | if (s->be->ck_opts & PR_CK_SECURE) |
| 5730 | chunk_appendf(&trash, "; Secure"); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5731 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5732 | if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0)) |
| 5733 | goto return_bad_resp; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5734 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5735 | txn->flags &= ~TX_SCK_MASK; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5736 | if (objt_server(s->target)->cookie && (s->flags & SF_DIRECT)) |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5737 | /* the server did not change, only the date was updated */ |
| 5738 | txn->flags |= TX_SCK_UPDATED; |
| 5739 | else |
| 5740 | txn->flags |= TX_SCK_INSERTED; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5741 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5742 | /* Here, we will tell an eventual cache on the client side that we don't |
| 5743 | * want it to cache this reply because HTTP/1.0 caches also cache cookies ! |
| 5744 | * Some caches understand the correct form: 'no-cache="set-cookie"', but |
| 5745 | * 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] | 5746 | */ |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5747 | if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5748 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5749 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5750 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5751 | if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, |
| 5752 | "Cache-control: private", 22) < 0)) |
| 5753 | goto return_bad_resp; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5754 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5755 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5756 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5757 | /* |
| 5758 | * Check if result will be cacheable with a cookie. |
| 5759 | * We'll block the response if security checks have caught |
| 5760 | * nasty things such as a cacheable cookie. |
| 5761 | */ |
| 5762 | if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) == |
| 5763 | (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) && |
| 5764 | (s->be->options & PR_O_CHK_CACHE)) { |
| 5765 | /* we're in presence of a cacheable response containing |
| 5766 | * a set-cookie header. We'll block it as requested by |
| 5767 | * the 'checkcache' option, and send an alert. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5768 | */ |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5769 | if (objt_server(s->target)) |
| 5770 | objt_server(s->target)->counters.failed_secu++; |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5771 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5772 | s->be->be_counters.denied_resp++; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5773 | sess->fe->fe_counters.denied_resp++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 5774 | if (sess->listener->counters) |
| 5775 | sess->listener->counters->denied_resp++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5776 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5777 | Alert("Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 5778 | s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>"); |
| 5779 | send_log(s->be, LOG_ALERT, |
| 5780 | "Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 5781 | s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>"); |
| 5782 | goto return_srv_prx_502; |
| 5783 | } |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 5784 | |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 5785 | skip_filters: |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5786 | /* |
| 5787 | * Adjust "Connection: close" or "Connection: keep-alive" if needed. |
| 5788 | * If an "Upgrade" token is found, the header is left untouched in order |
| 5789 | * 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] | 5790 | * if anything but "Upgrade" is present in the Connection header. We don't |
| 5791 | * want to touch any 101 response either since it's switching to another |
| 5792 | * protocol. |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5793 | */ |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 5794 | if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) && |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5795 | (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5796 | ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5797 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) { |
| 5798 | unsigned int want_flags = 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5799 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5800 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 5801 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 5802 | /* we want a keep-alive response here. Keep-alive header |
| 5803 | * required if either side is not 1.1. |
| 5804 | */ |
| 5805 | if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11)) |
| 5806 | want_flags |= TX_CON_KAL_SET; |
| 5807 | } |
| 5808 | else { |
| 5809 | /* we want a close response here. Close header required if |
| 5810 | * the server is 1.1, regardless of the client. |
| 5811 | */ |
| 5812 | if (msg->flags & HTTP_MSGF_VER_11) |
| 5813 | want_flags |= TX_CON_CLO_SET; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5814 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5815 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5816 | if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 5817 | http_change_connection_header(txn, msg, want_flags); |
| 5818 | } |
| 5819 | |
| 5820 | skip_header_mangling: |
Christopher Faulet | 69744d9 | 2017-03-30 10:54:35 +0200 | [diff] [blame] | 5821 | /* Always enter in the body analyzer */ |
| 5822 | rep->analysers &= ~AN_RES_FLT_XFER_DATA; |
| 5823 | rep->analysers |= AN_RES_HTTP_XFER_BODY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5824 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5825 | /* if the user wants to log as soon as possible, without counting |
| 5826 | * bytes from the server, then this is the right moment. We have |
| 5827 | * to temporarily assign bytes_out to log what we currently have. |
| 5828 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5829 | if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) { |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5830 | s->logs.t_close = s->logs.t_data; /* to get a valid end date */ |
| 5831 | s->logs.bytes_out = txn->rsp.eoh; |
| 5832 | s->do_log(s); |
| 5833 | s->logs.bytes_out = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5834 | } |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 5835 | return 1; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5836 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5837 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5838 | /* This function is an analyser which forwards response body (including chunk |
| 5839 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 5840 | * zero byte. The only situation where it must not be called is when we're in |
| 5841 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 5842 | * remaining data and to resync after end of body. It expects the msg_state to |
| 5843 | * 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] | 5844 | * 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] | 5845 | * |
| 5846 | * It is capable of compressing response data both in content-length mode and |
| 5847 | * in chunked mode. The state machines follows different flows depending on |
| 5848 | * whether content-length and chunked modes are used, since there are no |
| 5849 | * trailers in content-length : |
| 5850 | * |
| 5851 | * chk-mode cl-mode |
| 5852 | * ,----- BODY -----. |
| 5853 | * / \ |
| 5854 | * V size > 0 V chk-mode |
| 5855 | * .--> SIZE -------------> DATA -------------> CRLF |
| 5856 | * | | size == 0 | last byte | |
| 5857 | * | v final crlf v inspected | |
| 5858 | * | TRAILERS -----------> DONE | |
| 5859 | * | | |
| 5860 | * `----------------------------------------------' |
| 5861 | * |
| 5862 | * Compression only happens in the DATA state, and must be flushed in final |
| 5863 | * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding |
| 5864 | * is performed at once on final states for all bytes parsed, or when leaving |
| 5865 | * on missing data. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5866 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5867 | 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] | 5868 | { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5869 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5870 | struct http_txn *txn = s->txn; |
| 5871 | struct http_msg *msg = &s->txn->rsp; |
Christopher Faulet | 3e34429 | 2015-11-24 16:24:13 +0100 | [diff] [blame] | 5872 | int ret; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5873 | |
Christopher Faulet | 814d270 | 2017-03-30 11:33:44 +0200 | [diff] [blame] | 5874 | DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n", |
| 5875 | now_ms, __FUNCTION__, |
| 5876 | s, |
| 5877 | res, |
| 5878 | res->rex, res->wex, |
| 5879 | res->flags, |
| 5880 | res->buf->i, |
| 5881 | res->analysers); |
| 5882 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5883 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 5884 | return 0; |
| 5885 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5886 | 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] | 5887 | ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) || |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 5888 | !s->req.analysers) { |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 5889 | /* Output closed while we were sending data. We must abort and |
| 5890 | * wake the other side up. |
| 5891 | */ |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 5892 | msg->err_state = msg->msg_state; |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 5893 | msg->msg_state = HTTP_MSG_ERROR; |
| 5894 | http_resync_states(s); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 5895 | return 1; |
| 5896 | } |
| 5897 | |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 5898 | /* in most states, we should abort in case of early close */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5899 | channel_auto_close(res); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 5900 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 5901 | if (msg->msg_state == HTTP_MSG_BODY) { |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5902 | msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK) |
| 5903 | ? HTTP_MSG_CHUNK_SIZE |
| 5904 | : HTTP_MSG_DATA); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5905 | } |
| 5906 | |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 5907 | if (res->to_forward) { |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5908 | /* We can't process the buffer's contents yet */ |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 5909 | res->flags |= CF_WAKE_WRITE; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5910 | goto missing_data_or_waiting; |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 5911 | } |
| 5912 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5913 | if (msg->msg_state < HTTP_MSG_DONE) { |
| 5914 | ret = ((msg->flags & HTTP_MSGF_TE_CHNK) |
| 5915 | ? http_msg_forward_chunked_body(s, msg) |
| 5916 | : http_msg_forward_body(s, msg)); |
| 5917 | if (!ret) |
| 5918 | goto missing_data_or_waiting; |
| 5919 | if (ret < 0) |
| 5920 | goto return_bad_res; |
| 5921 | } |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 5922 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5923 | /* other states, DONE...TUNNEL */ |
| 5924 | /* for keep-alive we don't want to forward closes on DONE */ |
| 5925 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 5926 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
| 5927 | channel_dont_close(res); |
Willy Tarreau | 3ce10ff | 2014-04-22 08:24:38 +0200 | [diff] [blame] | 5928 | |
Christopher Faulet | 894da4c | 2017-07-18 11:29:07 +0200 | [diff] [blame] | 5929 | http_resync_states(s); |
| 5930 | if (!(res->analysers & an_bit)) { |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5931 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 5932 | if (res->flags & CF_SHUTW) { |
| 5933 | /* response errors are most likely due to the |
| 5934 | * client aborting the transfer. */ |
| 5935 | goto aborted_xfer; |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 5936 | } |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5937 | if (msg->err_pos >= 0) |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 5938 | http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, strm_fe(s)); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5939 | goto return_bad_res; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5940 | } |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5941 | return 1; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5942 | } |
Willy Tarreau | f51d03c | 2016-05-02 15:25:15 +0200 | [diff] [blame] | 5943 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5944 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5945 | missing_data_or_waiting: |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 5946 | if (res->flags & CF_SHUTW) |
| 5947 | goto aborted_xfer; |
| 5948 | |
| 5949 | /* stop waiting for data if the input is closed before the end. If the |
| 5950 | * client side was already closed, it means that the client has aborted, |
| 5951 | * so we don't want to count this as a server abort. Otherwise it's a |
| 5952 | * server abort. |
| 5953 | */ |
Christopher Faulet | a33510b | 2017-03-31 15:37:29 +0200 | [diff] [blame] | 5954 | if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5955 | if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW)) |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 5956 | goto aborted_xfer; |
Christopher Faulet | a46bbd8 | 2015-06-19 09:00:58 +0200 | [diff] [blame] | 5957 | /* If we have some pending data, we continue the processing */ |
| 5958 | if (!buffer_pending(res->buf)) { |
| 5959 | if (!(s->flags & SF_ERR_MASK)) |
| 5960 | s->flags |= SF_ERR_SRVCL; |
| 5961 | s->be->be_counters.srv_aborts++; |
| 5962 | if (objt_server(s->target)) |
| 5963 | objt_server(s->target)->counters.srv_aborts++; |
| 5964 | goto return_bad_res_stats_ok; |
| 5965 | } |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 5966 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5967 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 5968 | /* 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] | 5969 | if (!s->req.analysers) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5970 | goto return_bad_res; |
| 5971 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5972 | /* When TE: chunked is used, we need to get there again to parse |
| 5973 | * 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] | 5974 | * set CF_DONTCLOSE. Similarly, if keep-alive is set on the client side |
| 5975 | * or if there are filters registered on the stream, we don't want to |
| 5976 | * forward a close |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5977 | */ |
Christopher Faulet | 69744d9 | 2017-03-30 10:54:35 +0200 | [diff] [blame] | 5978 | if ((msg->flags & HTTP_MSGF_TE_CHNK) || |
Christopher Faulet | f1cc5d0 | 2017-02-08 09:45:13 +0100 | [diff] [blame] | 5979 | HAS_DATA_FILTERS(s, res) || |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5980 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 5981 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5982 | channel_dont_close(res); |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5983 | |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5984 | /* 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] | 5985 | * 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] | 5986 | * 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] | 5987 | * modes are already handled by the stream sock layer. We must not do |
| 5988 | * this in content-length mode because it could present the MSG_MORE |
| 5989 | * flag with the last block of forwarded data, which would cause an |
| 5990 | * additional delay to be observed by the receiver. |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5991 | */ |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 5992 | if ((msg->flags & HTTP_MSGF_TE_CHNK) || (msg->flags & HTTP_MSGF_COMPRESSING)) |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5993 | res->flags |= CF_EXPECT_MORE; |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5994 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5995 | /* the stream handler will take care of timeouts and errors */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5996 | return 0; |
| 5997 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 5998 | return_bad_res: /* let's centralize all bad responses */ |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5999 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6000 | if (objt_server(s->target)) |
| 6001 | objt_server(s->target)->counters.failed_resp++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6002 | |
| 6003 | return_bad_res_stats_ok: |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 6004 | txn->rsp.err_state = txn->rsp.msg_state; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6005 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 6006 | /* don't send any error message as we're in the body */ |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 6007 | http_reply_and_close(s, txn->status, NULL); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 6008 | res->analysers &= AN_RES_FLT_END; |
| 6009 | 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] | 6010 | if (objt_server(s->target)) |
| 6011 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6012 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6013 | if (!(s->flags & SF_ERR_MASK)) |
| 6014 | s->flags |= SF_ERR_PRXCOND; |
| 6015 | if (!(s->flags & SF_FINST_MASK)) |
| 6016 | s->flags |= SF_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6017 | return 0; |
| 6018 | |
| 6019 | aborted_xfer: |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 6020 | txn->rsp.err_state = txn->rsp.msg_state; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6021 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
| 6022 | /* don't send any error message as we're in the body */ |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 6023 | http_reply_and_close(s, txn->status, NULL); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 6024 | res->analysers &= AN_RES_FLT_END; |
| 6025 | 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] | 6026 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6027 | sess->fe->fe_counters.cli_aborts++; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 6028 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6029 | if (objt_server(s->target)) |
| 6030 | objt_server(s->target)->counters.cli_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6031 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6032 | if (!(s->flags & SF_ERR_MASK)) |
| 6033 | s->flags |= SF_ERR_CLICL; |
| 6034 | if (!(s->flags & SF_FINST_MASK)) |
| 6035 | s->flags |= SF_FINST_D; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6036 | return 0; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6037 | } |
| 6038 | |
| 6039 | |
| 6040 | static inline int |
| 6041 | http_msg_forward_body(struct stream *s, struct http_msg *msg) |
| 6042 | { |
| 6043 | struct channel *chn = msg->chn; |
| 6044 | int ret; |
| 6045 | |
| 6046 | /* Here we have the guarantee to be in HTTP_MSG_DATA or HTTP_MSG_ENDING state */ |
| 6047 | |
| 6048 | if (msg->msg_state == HTTP_MSG_ENDING) |
| 6049 | goto ending; |
| 6050 | |
| 6051 | /* Neither content-length, nor transfer-encoding was found, so we must |
| 6052 | * read the body until the server connection is closed. In that case, we |
| 6053 | * eat data as they come. Of course, this happens for response only. */ |
| 6054 | if (!(msg->flags & HTTP_MSGF_XFER_LEN)) { |
| 6055 | unsigned long long len = (chn->buf->i - msg->next); |
| 6056 | msg->chunk_len += len; |
| 6057 | msg->body_len += len; |
| 6058 | } |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 6059 | ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg), |
| 6060 | /* default_ret */ MIN(msg->chunk_len, chn->buf->i - msg->next), |
| 6061 | /* on_error */ goto error); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6062 | msg->next += ret; |
| 6063 | msg->chunk_len -= ret; |
| 6064 | if (msg->chunk_len) { |
| 6065 | /* input empty or output full */ |
| 6066 | if (chn->buf->i > msg->next) |
| 6067 | chn->flags |= CF_WAKE_WRITE; |
| 6068 | goto missing_data_or_waiting; |
| 6069 | } |
| 6070 | |
Christopher Faulet | 1486b0a | 2017-07-18 11:42:08 +0200 | [diff] [blame] | 6071 | /* This check can only be true for a response. HTTP_MSGF_XFER_LEN is |
| 6072 | * always set for a request. */ |
| 6073 | if (!(msg->flags & HTTP_MSGF_XFER_LEN)) { |
| 6074 | /* The server still sending data that should be filtered */ |
| 6075 | if (!(chn->flags & CF_SHUTR) && HAS_DATA_FILTERS(s, chn)) |
| 6076 | goto missing_data_or_waiting; |
| 6077 | } |
Christopher Faulet | f1cc5d0 | 2017-02-08 09:45:13 +0100 | [diff] [blame] | 6078 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6079 | msg->msg_state = HTTP_MSG_ENDING; |
| 6080 | |
| 6081 | ending: |
| 6082 | /* we may have some pending data starting at res->buf->p such as a last |
| 6083 | * chunk of data or trailers. */ |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 6084 | ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next), |
| 6085 | /* default_ret */ msg->next, |
| 6086 | /* on_error */ goto error); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6087 | b_adv(chn->buf, ret); |
| 6088 | msg->next -= ret; |
Willy Tarreau | 9962f8f | 2016-06-28 11:52:08 +0200 | [diff] [blame] | 6089 | if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)) |
| 6090 | msg->sov -= ret; |
Christopher Faulet | a9300a3 | 2016-06-28 15:54:44 +0200 | [diff] [blame] | 6091 | if (msg->next) |
| 6092 | goto waiting; |
Willy Tarreau | 9962f8f | 2016-06-28 11:52:08 +0200 | [diff] [blame] | 6093 | |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 6094 | FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg), |
| 6095 | /* default_ret */ 1, |
| 6096 | /* on_error */ goto error, |
| 6097 | /* on_wait */ goto waiting); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6098 | msg->msg_state = HTTP_MSG_DONE; |
| 6099 | return 1; |
| 6100 | |
| 6101 | missing_data_or_waiting: |
| 6102 | /* we may have some pending data starting at chn->buf->p */ |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 6103 | ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next), |
| 6104 | /* default_ret */ msg->next, |
| 6105 | /* on_error */ goto error); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6106 | b_adv(chn->buf, ret); |
| 6107 | msg->next -= ret; |
| 6108 | if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0) |
| 6109 | msg->sov -= ret; |
Christopher Faulet | 75e2eb6 | 2015-12-15 10:41:47 +0100 | [diff] [blame] | 6110 | if (!HAS_DATA_FILTERS(s, chn)) |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6111 | msg->chunk_len -= channel_forward(chn, msg->chunk_len); |
Christopher Faulet | a9300a3 | 2016-06-28 15:54:44 +0200 | [diff] [blame] | 6112 | waiting: |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6113 | return 0; |
| 6114 | error: |
| 6115 | return -1; |
| 6116 | } |
| 6117 | |
| 6118 | static inline int |
| 6119 | http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg) |
| 6120 | { |
| 6121 | struct channel *chn = msg->chn; |
| 6122 | int ret; |
| 6123 | |
| 6124 | /* Here we have the guarantee to be in one of the following state: |
| 6125 | * HTTP_MSG_DATA, HTTP_MSG_CHUNK_SIZE, HTTP_MSG_CHUNK_CRLF, |
| 6126 | * HTTP_MSG_TRAILERS or HTTP_MSG_ENDING. */ |
| 6127 | |
| 6128 | switch_states: |
| 6129 | switch (msg->msg_state) { |
| 6130 | case HTTP_MSG_DATA: |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 6131 | ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg), |
| 6132 | /* default_ret */ MIN(msg->chunk_len, chn->buf->i - msg->next), |
| 6133 | /* on_error */ goto error); |
| 6134 | msg->next += ret; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6135 | msg->chunk_len -= ret; |
| 6136 | if (msg->chunk_len) { |
| 6137 | /* input empty or output full */ |
| 6138 | if (chn->buf->i > msg->next) |
| 6139 | chn->flags |= CF_WAKE_WRITE; |
| 6140 | goto missing_data_or_waiting; |
| 6141 | } |
| 6142 | |
| 6143 | /* nothing left to forward for this chunk*/ |
| 6144 | msg->msg_state = HTTP_MSG_CHUNK_CRLF; |
| 6145 | /* fall through for HTTP_MSG_CHUNK_CRLF */ |
| 6146 | |
| 6147 | case HTTP_MSG_CHUNK_CRLF: |
| 6148 | /* we want the CRLF after the data */ |
| 6149 | ret = http_skip_chunk_crlf(msg); |
| 6150 | if (ret == 0) |
| 6151 | goto missing_data_or_waiting; |
| 6152 | if (ret < 0) |
| 6153 | goto chunk_parsing_error; |
Christopher Faulet | 113f7de | 2015-12-14 14:52:13 +0100 | [diff] [blame] | 6154 | msg->next += ret; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6155 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 6156 | /* fall through for HTTP_MSG_CHUNK_SIZE */ |
| 6157 | |
| 6158 | case HTTP_MSG_CHUNK_SIZE: |
| 6159 | /* read the chunk size and assign it to ->chunk_len, |
| 6160 | * then set ->next to point to the body and switch to |
| 6161 | * DATA or TRAILERS state. |
| 6162 | */ |
| 6163 | ret = http_parse_chunk_size(msg); |
| 6164 | if (ret == 0) |
| 6165 | goto missing_data_or_waiting; |
| 6166 | if (ret < 0) |
| 6167 | goto chunk_parsing_error; |
Christopher Faulet | 113f7de | 2015-12-14 14:52:13 +0100 | [diff] [blame] | 6168 | msg->next += ret; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6169 | if (msg->chunk_len) { |
| 6170 | msg->msg_state = HTTP_MSG_DATA; |
| 6171 | goto switch_states; |
| 6172 | } |
| 6173 | msg->msg_state = HTTP_MSG_TRAILERS; |
| 6174 | /* fall through for HTTP_MSG_TRAILERS */ |
| 6175 | |
| 6176 | case HTTP_MSG_TRAILERS: |
| 6177 | ret = http_forward_trailers(msg); |
| 6178 | if (ret < 0) |
| 6179 | goto chunk_parsing_error; |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 6180 | FLT_STRM_DATA_CB(s, chn, flt_http_chunk_trailers(s, msg), |
| 6181 | /* default_ret */ 1, |
| 6182 | /* on_error */ goto error); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6183 | msg->next += msg->sol; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6184 | if (!ret) |
| 6185 | goto missing_data_or_waiting; |
| 6186 | break; |
| 6187 | |
| 6188 | case HTTP_MSG_ENDING: |
| 6189 | goto ending; |
| 6190 | |
| 6191 | default: |
| 6192 | /* This should no happen in this function */ |
| 6193 | goto error; |
| 6194 | } |
| 6195 | |
| 6196 | msg->msg_state = HTTP_MSG_ENDING; |
| 6197 | ending: |
| 6198 | /* we may have some pending data starting at res->buf->p such as a last |
| 6199 | * chunk of data or trailers. */ |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 6200 | 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] | 6201 | /* default_ret */ msg->next, |
| 6202 | /* on_error */ goto error); |
| 6203 | b_adv(chn->buf, ret); |
| 6204 | msg->next -= ret; |
Willy Tarreau | 9962f8f | 2016-06-28 11:52:08 +0200 | [diff] [blame] | 6205 | if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)) |
| 6206 | msg->sov -= ret; |
Christopher Faulet | a9300a3 | 2016-06-28 15:54:44 +0200 | [diff] [blame] | 6207 | if (msg->next) |
| 6208 | goto waiting; |
Willy Tarreau | 9962f8f | 2016-06-28 11:52:08 +0200 | [diff] [blame] | 6209 | |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 6210 | FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg), |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6211 | /* default_ret */ 1, |
| 6212 | /* on_error */ goto error, |
| 6213 | /* on_wait */ goto waiting); |
| 6214 | msg->msg_state = HTTP_MSG_DONE; |
| 6215 | return 1; |
| 6216 | |
| 6217 | missing_data_or_waiting: |
| 6218 | /* we may have some pending data starting at chn->buf->p */ |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 6219 | 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] | 6220 | /* default_ret */ msg->next, |
| 6221 | /* on_error */ goto error); |
| 6222 | b_adv(chn->buf, ret); |
| 6223 | msg->next -= ret; |
| 6224 | if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0) |
| 6225 | msg->sov -= ret; |
Christopher Faulet | 75e2eb6 | 2015-12-15 10:41:47 +0100 | [diff] [blame] | 6226 | if (!HAS_DATA_FILTERS(s, chn)) |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6227 | msg->chunk_len -= channel_forward(chn, msg->chunk_len); |
Christopher Faulet | a9300a3 | 2016-06-28 15:54:44 +0200 | [diff] [blame] | 6228 | waiting: |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6229 | return 0; |
| 6230 | |
| 6231 | chunk_parsing_error: |
| 6232 | if (msg->err_pos >= 0) { |
| 6233 | if (chn->flags & CF_ISRESP) |
| 6234 | http_capture_bad_message(&s->be->invalid_rep, s, msg, |
| 6235 | msg->msg_state, strm_fe(s)); |
| 6236 | else |
| 6237 | http_capture_bad_message(&strm_fe(s)->invalid_req, s, |
| 6238 | msg, msg->msg_state, s->be); |
| 6239 | } |
| 6240 | error: |
| 6241 | return -1; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6242 | } |
| 6243 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 6244 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6245 | /* Iterate the same filter through all request headers. |
| 6246 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6247 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 6248 | * DENY stats. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6249 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6250 | 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] | 6251 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6252 | char *cur_ptr, *cur_end, *cur_next; |
| 6253 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 6254 | struct http_txn *txn = s->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6255 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6256 | int delta; |
Willy Tarreau | 0f7562b | 2007-01-07 15:46:13 +0100 | [diff] [blame] | 6257 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6258 | last_hdr = 0; |
| 6259 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6260 | cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6261 | old_idx = 0; |
| 6262 | |
| 6263 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6264 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6265 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6266 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6267 | (exp->action == ACT_ALLOW || |
| 6268 | exp->action == ACT_DENY || |
| 6269 | exp->action == ACT_TARPIT)) |
| 6270 | return 0; |
| 6271 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6272 | cur_idx = txn->hdr_idx.v[old_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6273 | if (!cur_idx) |
| 6274 | break; |
| 6275 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6276 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6277 | cur_ptr = cur_next; |
| 6278 | cur_end = cur_ptr + cur_hdr->len; |
| 6279 | cur_next = cur_end + cur_hdr->cr + 1; |
| 6280 | |
| 6281 | /* Now we have one header between cur_ptr and cur_end, |
| 6282 | * and the next header starts at cur_next. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6283 | */ |
| 6284 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 6285 | 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] | 6286 | switch (exp->action) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6287 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6288 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6289 | last_hdr = 1; |
| 6290 | break; |
| 6291 | |
| 6292 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6293 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6294 | last_hdr = 1; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6295 | break; |
| 6296 | |
| 6297 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6298 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6299 | last_hdr = 1; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6300 | break; |
| 6301 | |
| 6302 | case ACT_REPLACE: |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 6303 | trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch); |
| 6304 | if (trash.len < 0) |
| 6305 | return -1; |
| 6306 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6307 | 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] | 6308 | /* FIXME: if the user adds a newline in the replacement, the |
| 6309 | * index will not be recalculated for now, and the new line |
| 6310 | * will not be counted as a new header. |
| 6311 | */ |
| 6312 | |
| 6313 | cur_end += delta; |
| 6314 | cur_next += delta; |
| 6315 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6316 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6317 | break; |
| 6318 | |
| 6319 | case ACT_REMOVE: |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6320 | delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6321 | cur_next += delta; |
| 6322 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6323 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6324 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 6325 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6326 | cur_hdr->len = 0; |
| 6327 | cur_end = NULL; /* null-term has been rewritten */ |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 6328 | cur_idx = old_idx; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6329 | break; |
| 6330 | |
| 6331 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6332 | } |
| 6333 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6334 | /* keep the link from this header to next one in case of later |
| 6335 | * removal of next header. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6336 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6337 | old_idx = cur_idx; |
| 6338 | } |
| 6339 | return 0; |
| 6340 | } |
| 6341 | |
| 6342 | |
| 6343 | /* Apply the filter to the request line. |
| 6344 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 6345 | * or -1 if a replacement resulted in an invalid request line. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6346 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 6347 | * DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6348 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6349 | 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] | 6350 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6351 | char *cur_ptr, *cur_end; |
| 6352 | int done; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 6353 | struct http_txn *txn = s->txn; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6354 | int delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6355 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6356 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6357 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6358 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6359 | (exp->action == ACT_ALLOW || |
| 6360 | exp->action == ACT_DENY || |
| 6361 | exp->action == ACT_TARPIT)) |
| 6362 | return 0; |
| 6363 | else if (exp->action == ACT_REMOVE) |
| 6364 | return 0; |
| 6365 | |
| 6366 | done = 0; |
| 6367 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6368 | cur_ptr = req->buf->p; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6369 | cur_end = cur_ptr + txn->req.sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6370 | |
| 6371 | /* Now we have the request line between cur_ptr and cur_end */ |
| 6372 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 6373 | 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] | 6374 | switch (exp->action) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6375 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6376 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6377 | done = 1; |
| 6378 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 6379 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6380 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6381 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6382 | done = 1; |
| 6383 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 6384 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6385 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6386 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6387 | done = 1; |
| 6388 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 6389 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6390 | case ACT_REPLACE: |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 6391 | trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch); |
| 6392 | if (trash.len < 0) |
| 6393 | return -1; |
| 6394 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6395 | 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] | 6396 | /* FIXME: if the user adds a newline in the replacement, the |
| 6397 | * index will not be recalculated for now, and the new line |
| 6398 | * will not be counted as a new header. |
| 6399 | */ |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 6400 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6401 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6402 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 6403 | cur_end = (char *)http_parse_reqline(&txn->req, |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6404 | HTTP_MSG_RQMETH, |
| 6405 | cur_ptr, cur_end + 1, |
| 6406 | NULL, NULL); |
| 6407 | if (unlikely(!cur_end)) |
| 6408 | return -1; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 6409 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6410 | /* we have a full request and we know that we have either a CR |
| 6411 | * or an LF at <ptr>. |
| 6412 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6413 | txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l); |
| 6414 | 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] | 6415 | /* there is no point trying this regex on headers */ |
| 6416 | return 1; |
| 6417 | } |
| 6418 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6419 | return done; |
| 6420 | } |
Willy Tarreau | 97de624 | 2006-12-27 17:18:38 +0100 | [diff] [blame] | 6421 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6422 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6423 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6424 | /* |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6425 | * 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] | 6426 | * 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] | 6427 | * unparsable request. Since it can manage the switch to another backend, it |
| 6428 | * updates the per-proxy DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6429 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6430 | 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] | 6431 | { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 6432 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 6433 | struct http_txn *txn = s->txn; |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6434 | struct hdr_exp *exp; |
| 6435 | |
| 6436 | for (exp = px->req_exp; exp; exp = exp->next) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6437 | int ret; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6438 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6439 | /* |
| 6440 | * The interleaving of transformations and verdicts |
| 6441 | * makes it difficult to decide to continue or stop |
| 6442 | * the evaluation. |
| 6443 | */ |
| 6444 | |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6445 | if (txn->flags & (TX_CLDENY|TX_CLTARPIT)) |
| 6446 | break; |
| 6447 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6448 | if ((txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6449 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6450 | exp->action == ACT_TARPIT || exp->action == ACT_PASS)) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6451 | continue; |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6452 | |
| 6453 | /* if this filter had a condition, evaluate it now and skip to |
| 6454 | * next filter if the condition does not match. |
| 6455 | */ |
| 6456 | if (exp->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 6457 | 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] | 6458 | ret = acl_pass(ret); |
| 6459 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 6460 | ret = !ret; |
| 6461 | |
| 6462 | if (!ret) |
| 6463 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6464 | } |
| 6465 | |
| 6466 | /* Apply the filter to the request line. */ |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6467 | ret = apply_filter_to_req_line(s, req, exp); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6468 | if (unlikely(ret < 0)) |
| 6469 | return -1; |
| 6470 | |
| 6471 | if (likely(ret == 0)) { |
| 6472 | /* The filter did not match the request, it can be |
| 6473 | * iterated through all headers. |
| 6474 | */ |
Willy Tarreau | 34d4c3c | 2015-01-30 20:58:58 +0100 | [diff] [blame] | 6475 | if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0)) |
| 6476 | return -1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6477 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6478 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6479 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6480 | } |
| 6481 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6482 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6483 | /* Find the end of a cookie value contained between <s> and <e>. It works the |
| 6484 | * same way as with headers above except that the semi-colon also ends a token. |
| 6485 | * See RFC2965 for more information. Note that it requires a valid header to |
| 6486 | * return a valid result. |
| 6487 | */ |
| 6488 | char *find_cookie_value_end(char *s, const char *e) |
| 6489 | { |
| 6490 | int quoted, qdpair; |
| 6491 | |
| 6492 | quoted = qdpair = 0; |
| 6493 | for (; s < e; s++) { |
| 6494 | if (qdpair) qdpair = 0; |
| 6495 | else if (quoted) { |
| 6496 | if (*s == '\\') qdpair = 1; |
| 6497 | else if (*s == '"') quoted = 0; |
| 6498 | } |
| 6499 | else if (*s == '"') quoted = 1; |
| 6500 | else if (*s == ',' || *s == ';') return s; |
| 6501 | } |
| 6502 | return s; |
| 6503 | } |
| 6504 | |
| 6505 | /* Delete a value in a header between delimiters <from> and <next> in buffer |
| 6506 | * <buf>. The number of characters displaced is returned, and the pointer to |
| 6507 | * the first delimiter is updated if required. The function tries as much as |
| 6508 | * possible to respect the following principles : |
| 6509 | * - replace <from> delimiter by the <next> one unless <from> points to a |
| 6510 | * colon, in which case <next> is simply removed |
| 6511 | * - set exactly one space character after the new first delimiter, unless |
| 6512 | * there are not enough characters in the block being moved to do so. |
| 6513 | * - remove unneeded spaces before the previous delimiter and after the new |
| 6514 | * one. |
| 6515 | * |
| 6516 | * It is the caller's responsibility to ensure that : |
| 6517 | * - <from> points to a valid delimiter or the colon ; |
| 6518 | * - <next> points to a valid delimiter or the final CR/LF ; |
| 6519 | * - there are non-space chars before <from> ; |
| 6520 | * - there is a CR/LF at or after <next>. |
| 6521 | */ |
Willy Tarreau | af81935 | 2012-08-27 22:08:00 +0200 | [diff] [blame] | 6522 | int del_hdr_value(struct buffer *buf, char **from, char *next) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6523 | { |
| 6524 | char *prev = *from; |
| 6525 | |
| 6526 | if (*prev == ':') { |
| 6527 | /* We're removing the first value, preserve the colon and add a |
| 6528 | * space if possible. |
| 6529 | */ |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 6530 | if (!HTTP_IS_CRLF(*next)) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6531 | next++; |
| 6532 | prev++; |
| 6533 | if (prev < next) |
| 6534 | *prev++ = ' '; |
| 6535 | |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 6536 | while (HTTP_IS_SPHT(*next)) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6537 | next++; |
| 6538 | } else { |
| 6539 | /* Remove useless spaces before the old delimiter. */ |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 6540 | while (HTTP_IS_SPHT(*(prev-1))) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6541 | prev--; |
| 6542 | *from = prev; |
| 6543 | |
| 6544 | /* copy the delimiter and if possible a space if we're |
| 6545 | * not at the end of the line. |
| 6546 | */ |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 6547 | if (!HTTP_IS_CRLF(*next)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6548 | *prev++ = *next++; |
| 6549 | if (prev + 1 < next) |
| 6550 | *prev++ = ' '; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 6551 | while (HTTP_IS_SPHT(*next)) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6552 | next++; |
| 6553 | } |
| 6554 | } |
| 6555 | return buffer_replace2(buf, prev, next, NULL, 0); |
| 6556 | } |
| 6557 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6558 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 6559 | * 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] | 6560 | * desirable to call it only when needed. This code is quite complex because |
| 6561 | * of the multiple very crappy and ambiguous syntaxes we have to support. it |
| 6562 | * highly recommended not to touch this part without a good reason ! |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6563 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6564 | void manage_client_side_cookies(struct stream *s, struct channel *req) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6565 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 6566 | struct http_txn *txn = s->txn; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6567 | struct session *sess = s->sess; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6568 | int preserve_hdr; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6569 | int cur_idx, old_idx; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6570 | char *hdr_beg, *hdr_end, *hdr_next, *del_from; |
| 6571 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6572 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6573 | /* Iterate through the headers, we start with the start line. */ |
Willy Tarreau | 83969f4 | 2007-01-22 08:55:47 +0100 | [diff] [blame] | 6574 | old_idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6575 | hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6576 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6577 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6578 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6579 | int val; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6580 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6581 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6582 | hdr_beg = hdr_next; |
| 6583 | hdr_end = hdr_beg + cur_hdr->len; |
| 6584 | hdr_next = hdr_end + cur_hdr->cr + 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6585 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6586 | /* We have one full header between hdr_beg and hdr_end, and the |
| 6587 | * next header starts at hdr_next. We're only interested in |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6588 | * "Cookie:" headers. |
| 6589 | */ |
| 6590 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6591 | val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6); |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6592 | if (!val) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6593 | old_idx = cur_idx; |
| 6594 | continue; |
| 6595 | } |
| 6596 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6597 | del_from = NULL; /* nothing to be deleted */ |
| 6598 | preserve_hdr = 0; /* assume we may kill the whole header */ |
| 6599 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6600 | /* Now look for cookies. Conforming to RFC2109, we have to support |
| 6601 | * attributes whose name begin with a '$', and associate them with |
| 6602 | * the right cookie, if we want to delete this cookie. |
| 6603 | * So there are 3 cases for each cookie read : |
| 6604 | * 1) it's a special attribute, beginning with a '$' : ignore it. |
| 6605 | * 2) it's a server id cookie that we *MAY* want to delete : save |
| 6606 | * some pointers on it (last semi-colon, beginning of cookie...) |
| 6607 | * 3) it's an application cookie : we *MAY* have to delete a previous |
| 6608 | * "special" cookie. |
| 6609 | * At the end of loop, if a "special" cookie remains, we may have to |
| 6610 | * remove it. If no application cookie persists in the header, we |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6611 | * *MUST* delete it. |
| 6612 | * |
| 6613 | * Note: RFC2965 is unclear about the processing of spaces around |
| 6614 | * the equal sign in the ATTR=VALUE form. A careful inspection of |
| 6615 | * the RFC explicitly allows spaces before it, and not within the |
| 6616 | * tokens (attrs or values). An inspection of RFC2109 allows that |
| 6617 | * too but section 10.1.3 lets one think that spaces may be allowed |
| 6618 | * after the equal sign too, resulting in some (rare) buggy |
| 6619 | * implementations trying to do that. So let's do what servers do. |
| 6620 | * Latest ietf draft forbids spaces all around. Also, earlier RFCs |
| 6621 | * allowed quoted strings in values, with any possible character |
| 6622 | * after a backslash, including control chars and delimitors, which |
| 6623 | * causes parsing to become ambiguous. Browsers also allow spaces |
| 6624 | * within values even without quotes. |
| 6625 | * |
| 6626 | * We have to keep multiple pointers in order to support cookie |
| 6627 | * removal at the beginning, middle or end of header without |
| 6628 | * corrupting the header. All of these headers are valid : |
| 6629 | * |
| 6630 | * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n |
| 6631 | * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n |
| 6632 | * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n |
| 6633 | * | | | | | | | | | |
| 6634 | * | | | | | | | | hdr_end <--+ |
| 6635 | * | | | | | | | +--> next |
| 6636 | * | | | | | | +----> val_end |
| 6637 | * | | | | | +-----------> val_beg |
| 6638 | * | | | | +--------------> equal |
| 6639 | * | | | +----------------> att_end |
| 6640 | * | | +---------------------> att_beg |
| 6641 | * | +--------------------------> prev |
| 6642 | * +--------------------------------> hdr_beg |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6643 | */ |
| 6644 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6645 | for (prev = hdr_beg + 6; prev < hdr_end; prev = next) { |
| 6646 | /* Iterate through all cookies on this line */ |
| 6647 | |
| 6648 | /* find att_beg */ |
| 6649 | att_beg = prev + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 6650 | while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg)) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6651 | att_beg++; |
| 6652 | |
| 6653 | /* find att_end : this is the first character after the last non |
| 6654 | * space before the equal. It may be equal to hdr_end. |
| 6655 | */ |
| 6656 | equal = att_end = att_beg; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6657 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6658 | while (equal < hdr_end) { |
| 6659 | if (*equal == '=' || *equal == ',' || *equal == ';') |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6660 | break; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 6661 | if (HTTP_IS_SPHT(*equal++)) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6662 | continue; |
| 6663 | att_end = equal; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6664 | } |
| 6665 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6666 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 6667 | * is between <att_beg> and <equal>, both may be identical. |
| 6668 | */ |
| 6669 | |
| 6670 | /* look for end of cookie if there is an equal sign */ |
| 6671 | if (equal < hdr_end && *equal == '=') { |
| 6672 | /* look for the beginning of the value */ |
| 6673 | val_beg = equal + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 6674 | while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg)) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6675 | val_beg++; |
| 6676 | |
| 6677 | /* find the end of the value, respecting quotes */ |
| 6678 | next = find_cookie_value_end(val_beg, hdr_end); |
| 6679 | |
| 6680 | /* make val_end point to the first white space or delimitor after the value */ |
| 6681 | val_end = next; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 6682 | while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1))) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6683 | val_end--; |
| 6684 | } else { |
| 6685 | val_beg = val_end = next = equal; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 6686 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6687 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6688 | /* We have nothing to do with attributes beginning with '$'. However, |
| 6689 | * they will automatically be removed if a header before them is removed, |
| 6690 | * since they're supposed to be linked together. |
| 6691 | */ |
| 6692 | if (*att_beg == '$') |
| 6693 | continue; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6694 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6695 | /* Ignore cookies with no equal sign */ |
| 6696 | if (equal == next) { |
| 6697 | /* This is not our cookie, so we must preserve it. But if we already |
| 6698 | * scheduled another cookie for removal, we cannot remove the |
| 6699 | * complete header, but we can remove the previous block itself. |
| 6700 | */ |
| 6701 | preserve_hdr = 1; |
| 6702 | if (del_from != NULL) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6703 | int delta = del_hdr_value(req->buf, &del_from, prev); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6704 | val_end += delta; |
| 6705 | next += delta; |
| 6706 | hdr_end += delta; |
| 6707 | hdr_next += delta; |
| 6708 | cur_hdr->len += delta; |
| 6709 | http_msg_move_end(&txn->req, delta); |
| 6710 | prev = del_from; |
| 6711 | del_from = NULL; |
| 6712 | } |
| 6713 | continue; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 6714 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6715 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6716 | /* if there are spaces around the equal sign, we need to |
| 6717 | * strip them otherwise we'll get trouble for cookie captures, |
| 6718 | * or even for rewrites. Since this happens extremely rarely, |
| 6719 | * it does not hurt performance. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6720 | */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6721 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 6722 | int stripped_before = 0; |
| 6723 | int stripped_after = 0; |
| 6724 | |
| 6725 | if (att_end != equal) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6726 | stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6727 | equal += stripped_before; |
| 6728 | val_beg += stripped_before; |
| 6729 | } |
| 6730 | |
| 6731 | if (val_beg > equal + 1) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6732 | stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6733 | val_beg += stripped_after; |
| 6734 | stripped_before += stripped_after; |
| 6735 | } |
| 6736 | |
| 6737 | val_end += stripped_before; |
| 6738 | next += stripped_before; |
| 6739 | hdr_end += stripped_before; |
| 6740 | hdr_next += stripped_before; |
| 6741 | cur_hdr->len += stripped_before; |
| 6742 | http_msg_move_end(&txn->req, stripped_before); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6743 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6744 | /* now everything is as on the diagram above */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6745 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6746 | /* First, let's see if we want to capture this cookie. We check |
| 6747 | * that we don't already have a client side cookie, because we |
| 6748 | * can only capture one. Also as an optimisation, we ignore |
| 6749 | * cookies shorter than the declared name. |
| 6750 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6751 | if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL && |
| 6752 | (val_end - att_beg >= sess->fe->capture_namelen) && |
| 6753 | memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6754 | int log_len = val_end - att_beg; |
| 6755 | |
| 6756 | if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) { |
| 6757 | Alert("HTTP logging : out of memory.\n"); |
| 6758 | } else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6759 | if (log_len > sess->fe->capture_len) |
| 6760 | log_len = sess->fe->capture_len; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6761 | memcpy(txn->cli_cookie, att_beg, log_len); |
| 6762 | txn->cli_cookie[log_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6763 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6764 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6765 | |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6766 | /* Persistence cookies in passive, rewrite or insert mode have the |
| 6767 | * following form : |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6768 | * |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6769 | * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]] |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6770 | * |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6771 | * For cookies in prefix mode, the form is : |
| 6772 | * |
| 6773 | * Cookie: NAME=SRV~VALUE |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6774 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6775 | if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) && |
| 6776 | (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) { |
| 6777 | struct server *srv = s->be->srv; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6778 | char *delim; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6779 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6780 | /* if we're in cookie prefix mode, we'll search the delimitor so that we |
| 6781 | * have the server ID between val_beg and delim, and the original cookie between |
| 6782 | * delim+1 and val_end. Otherwise, delim==val_end : |
| 6783 | * |
| 6784 | * Cookie: NAME=SRV; # in all but prefix modes |
| 6785 | * Cookie: NAME=SRV~OPAQUE ; # in prefix mode |
| 6786 | * | || || | |+-> next |
| 6787 | * | || || | +--> val_end |
| 6788 | * | || || +---------> delim |
| 6789 | * | || |+------------> val_beg |
| 6790 | * | || +-------------> att_end = equal |
| 6791 | * | |+-----------------> att_beg |
| 6792 | * | +------------------> prev |
| 6793 | * +-------------------------> hdr_beg |
| 6794 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6795 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6796 | if (s->be->ck_opts & PR_CK_PFX) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6797 | for (delim = val_beg; delim < val_end; delim++) |
| 6798 | if (*delim == COOKIE_DELIM) |
| 6799 | break; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6800 | } else { |
| 6801 | char *vbar1; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6802 | delim = val_end; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6803 | /* Now check if the cookie contains a date field, which would |
| 6804 | * appear after a vertical bar ('|') just after the server name |
| 6805 | * and before the delimiter. |
| 6806 | */ |
| 6807 | vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg); |
| 6808 | if (vbar1) { |
| 6809 | /* OK, so left of the bar is the server's cookie and |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6810 | * right is the last seen date. It is a base64 encoded |
| 6811 | * 30-bit value representing the UNIX date since the |
| 6812 | * epoch in 4-second quantities. |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6813 | */ |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6814 | int val; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6815 | delim = vbar1++; |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6816 | if (val_end - vbar1 >= 5) { |
| 6817 | val = b64tos30(vbar1); |
| 6818 | if (val > 0) |
| 6819 | txn->cookie_last_date = val << 2; |
| 6820 | } |
| 6821 | /* look for a second vertical bar */ |
| 6822 | vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1); |
| 6823 | if (vbar1 && (val_end - vbar1 > 5)) { |
| 6824 | val = b64tos30(vbar1 + 1); |
| 6825 | if (val > 0) |
| 6826 | txn->cookie_first_date = val << 2; |
| 6827 | } |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6828 | } |
| 6829 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6830 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6831 | /* if the cookie has an expiration date and the proxy wants to check |
| 6832 | * it, then we do that now. We first check if the cookie is too old, |
| 6833 | * then only if it has expired. We detect strict overflow because the |
| 6834 | * time resolution here is not great (4 seconds). Cookies with dates |
| 6835 | * in the future are ignored if their offset is beyond one day. This |
| 6836 | * allows an admin to fix timezone issues without expiring everyone |
| 6837 | * and at the same time avoids keeping unwanted side effects for too |
| 6838 | * long. |
| 6839 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6840 | if (txn->cookie_first_date && s->be->cookie_maxlife && |
| 6841 | (((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] | 6842 | ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) { |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6843 | txn->flags &= ~TX_CK_MASK; |
| 6844 | txn->flags |= TX_CK_OLD; |
| 6845 | delim = val_beg; // let's pretend we have not found the cookie |
| 6846 | txn->cookie_first_date = 0; |
| 6847 | txn->cookie_last_date = 0; |
| 6848 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6849 | else if (txn->cookie_last_date && s->be->cookie_maxidle && |
| 6850 | (((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] | 6851 | ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) { |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6852 | txn->flags &= ~TX_CK_MASK; |
| 6853 | txn->flags |= TX_CK_EXPIRED; |
| 6854 | delim = val_beg; // let's pretend we have not found the cookie |
| 6855 | txn->cookie_first_date = 0; |
| 6856 | txn->cookie_last_date = 0; |
| 6857 | } |
| 6858 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6859 | /* Here, we'll look for the first running server which supports the cookie. |
| 6860 | * This allows to share a same cookie between several servers, for example |
| 6861 | * to dedicate backup servers to specific servers only. |
| 6862 | * However, to prevent clients from sticking to cookie-less backup server |
| 6863 | * when they have incidentely learned an empty cookie, we simply ignore |
| 6864 | * empty cookies and mark them as invalid. |
| 6865 | * The same behaviour is applied when persistence must be ignored. |
| 6866 | */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6867 | if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED))) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6868 | srv = NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6869 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6870 | while (srv) { |
| 6871 | if (srv->cookie && (srv->cklen == delim - val_beg) && |
| 6872 | !memcmp(val_beg, srv->cookie, delim - val_beg)) { |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 6873 | if ((srv->cur_state != SRV_ST_STOPPED) || |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6874 | (s->be->options & PR_O_PERSIST) || |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6875 | (s->flags & SF_FORCE_PRST)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6876 | /* we found the server and we can use it */ |
| 6877 | txn->flags &= ~TX_CK_MASK; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 6878 | 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] | 6879 | s->flags |= SF_DIRECT | SF_ASSIGNED; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6880 | s->target = &srv->obj_type; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6881 | break; |
| 6882 | } else { |
| 6883 | /* we found a server, but it's down, |
| 6884 | * mark it as such and go on in case |
| 6885 | * another one is available. |
| 6886 | */ |
| 6887 | txn->flags &= ~TX_CK_MASK; |
| 6888 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6889 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6890 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6891 | srv = srv->next; |
| 6892 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6893 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6894 | 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] | 6895 | /* no server matched this cookie or we deliberately skipped it */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6896 | txn->flags &= ~TX_CK_MASK; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6897 | if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED))) |
Willy Tarreau | c89ccb6 | 2012-04-05 21:18:22 +0200 | [diff] [blame] | 6898 | txn->flags |= TX_CK_UNUSED; |
| 6899 | else |
| 6900 | txn->flags |= TX_CK_INVALID; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6901 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6902 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6903 | /* depending on the cookie mode, we may have to either : |
| 6904 | * - delete the complete cookie if we're in insert+indirect mode, so that |
| 6905 | * the server never sees it ; |
| 6906 | * - remove the server id from the cookie value, and tag the cookie as an |
| 6907 | * application cookie so that it does not get accidentely removed later, |
| 6908 | * if we're in cookie prefix mode |
| 6909 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6910 | if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6911 | int delta; /* negative */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6912 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6913 | delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6914 | val_end += delta; |
| 6915 | next += delta; |
| 6916 | hdr_end += delta; |
| 6917 | hdr_next += delta; |
| 6918 | cur_hdr->len += delta; |
| 6919 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6920 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6921 | del_from = NULL; |
| 6922 | preserve_hdr = 1; /* we want to keep this cookie */ |
| 6923 | } |
| 6924 | else if (del_from == NULL && |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6925 | (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] | 6926 | del_from = prev; |
| 6927 | } |
| 6928 | } else { |
| 6929 | /* This is not our cookie, so we must preserve it. But if we already |
| 6930 | * scheduled another cookie for removal, we cannot remove the |
| 6931 | * complete header, but we can remove the previous block itself. |
| 6932 | */ |
| 6933 | preserve_hdr = 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6934 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6935 | if (del_from != NULL) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6936 | int delta = del_hdr_value(req->buf, &del_from, prev); |
Willy Tarreau | b810554 | 2010-11-24 18:31:28 +0100 | [diff] [blame] | 6937 | if (att_beg >= del_from) |
| 6938 | att_beg += delta; |
| 6939 | if (att_end >= del_from) |
| 6940 | att_end += delta; |
| 6941 | val_beg += delta; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6942 | val_end += delta; |
| 6943 | next += delta; |
| 6944 | hdr_end += delta; |
| 6945 | hdr_next += delta; |
| 6946 | cur_hdr->len += delta; |
| 6947 | http_msg_move_end(&txn->req, delta); |
| 6948 | prev = del_from; |
| 6949 | del_from = NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6950 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6951 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6952 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6953 | /* continue with next cookie on this header line */ |
| 6954 | att_beg = next; |
| 6955 | } /* for each cookie */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6956 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6957 | /* There are no more cookies on this line. |
| 6958 | * We may still have one (or several) marked for deletion at the |
| 6959 | * end of the line. We must do this now in two ways : |
| 6960 | * - if some cookies must be preserved, we only delete from the |
| 6961 | * mark to the end of line ; |
| 6962 | * - if nothing needs to be preserved, simply delete the whole header |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6963 | */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6964 | if (del_from) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6965 | int delta; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6966 | if (preserve_hdr) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6967 | delta = del_hdr_value(req->buf, &del_from, hdr_end); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6968 | hdr_end = del_from; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6969 | cur_hdr->len += delta; |
| 6970 | } else { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6971 | delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6972 | |
| 6973 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6974 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 6975 | txn->hdr_idx.used--; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6976 | cur_hdr->len = 0; |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 6977 | cur_idx = old_idx; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6978 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6979 | hdr_next += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6980 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6981 | } |
| 6982 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6983 | /* check next header */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6984 | old_idx = cur_idx; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6985 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6986 | } |
| 6987 | |
| 6988 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6989 | /* Iterate the same filter through all response headers contained in <rtr>. |
| 6990 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
| 6991 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6992 | 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] | 6993 | { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6994 | char *cur_ptr, *cur_end, *cur_next; |
| 6995 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 6996 | struct http_txn *txn = s->txn; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6997 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6998 | int delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6999 | |
| 7000 | last_hdr = 0; |
| 7001 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7002 | cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7003 | old_idx = 0; |
| 7004 | |
| 7005 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7006 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7007 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7008 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7009 | (exp->action == ACT_ALLOW || |
| 7010 | exp->action == ACT_DENY)) |
| 7011 | return 0; |
| 7012 | |
| 7013 | cur_idx = txn->hdr_idx.v[old_idx].next; |
| 7014 | if (!cur_idx) |
| 7015 | break; |
| 7016 | |
| 7017 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 7018 | cur_ptr = cur_next; |
| 7019 | cur_end = cur_ptr + cur_hdr->len; |
| 7020 | cur_next = cur_end + cur_hdr->cr + 1; |
| 7021 | |
| 7022 | /* Now we have one header between cur_ptr and cur_end, |
| 7023 | * and the next header starts at cur_next. |
| 7024 | */ |
| 7025 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 7026 | 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] | 7027 | switch (exp->action) { |
| 7028 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7029 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7030 | last_hdr = 1; |
| 7031 | break; |
| 7032 | |
| 7033 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7034 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7035 | last_hdr = 1; |
| 7036 | break; |
| 7037 | |
| 7038 | case ACT_REPLACE: |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 7039 | trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch); |
| 7040 | if (trash.len < 0) |
| 7041 | return -1; |
| 7042 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7043 | 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] | 7044 | /* FIXME: if the user adds a newline in the replacement, the |
| 7045 | * index will not be recalculated for now, and the new line |
| 7046 | * will not be counted as a new header. |
| 7047 | */ |
| 7048 | |
| 7049 | cur_end += delta; |
| 7050 | cur_next += delta; |
| 7051 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7052 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7053 | break; |
| 7054 | |
| 7055 | case ACT_REMOVE: |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7056 | delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7057 | cur_next += delta; |
| 7058 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7059 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7060 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 7061 | txn->hdr_idx.used--; |
| 7062 | cur_hdr->len = 0; |
| 7063 | cur_end = NULL; /* null-term has been rewritten */ |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 7064 | cur_idx = old_idx; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7065 | break; |
| 7066 | |
| 7067 | } |
| 7068 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7069 | |
| 7070 | /* keep the link from this header to next one in case of later |
| 7071 | * removal of next header. |
| 7072 | */ |
| 7073 | old_idx = cur_idx; |
| 7074 | } |
| 7075 | return 0; |
| 7076 | } |
| 7077 | |
| 7078 | |
| 7079 | /* Apply the filter to the status line in the response buffer <rtr>. |
| 7080 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 7081 | * or -1 if a replacement resulted in an invalid status line. |
| 7082 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7083 | 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] | 7084 | { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7085 | char *cur_ptr, *cur_end; |
| 7086 | int done; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7087 | struct http_txn *txn = s->txn; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7088 | int delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7089 | |
| 7090 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7091 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7092 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7093 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7094 | (exp->action == ACT_ALLOW || |
| 7095 | exp->action == ACT_DENY)) |
| 7096 | return 0; |
| 7097 | else if (exp->action == ACT_REMOVE) |
| 7098 | return 0; |
| 7099 | |
| 7100 | done = 0; |
| 7101 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7102 | cur_ptr = rtr->buf->p; |
Willy Tarreau | 1ba0e5f | 2010-06-07 13:57:32 +0200 | [diff] [blame] | 7103 | cur_end = cur_ptr + txn->rsp.sl.st.l; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7104 | |
| 7105 | /* Now we have the status line between cur_ptr and cur_end */ |
| 7106 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 7107 | 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] | 7108 | switch (exp->action) { |
| 7109 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7110 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7111 | done = 1; |
| 7112 | break; |
| 7113 | |
| 7114 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7115 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7116 | done = 1; |
| 7117 | break; |
| 7118 | |
| 7119 | case ACT_REPLACE: |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 7120 | trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch); |
| 7121 | if (trash.len < 0) |
| 7122 | return -1; |
| 7123 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7124 | 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] | 7125 | /* FIXME: if the user adds a newline in the replacement, the |
| 7126 | * index will not be recalculated for now, and the new line |
| 7127 | * will not be counted as a new header. |
| 7128 | */ |
| 7129 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7130 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7131 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 7132 | cur_end = (char *)http_parse_stsline(&txn->rsp, |
Willy Tarreau | 0278576 | 2007-04-03 14:45:44 +0200 | [diff] [blame] | 7133 | HTTP_MSG_RPVER, |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7134 | cur_ptr, cur_end + 1, |
| 7135 | NULL, NULL); |
| 7136 | if (unlikely(!cur_end)) |
| 7137 | return -1; |
| 7138 | |
| 7139 | /* we have a full respnse and we know that we have either a CR |
| 7140 | * or an LF at <ptr>. |
| 7141 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7142 | 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] | 7143 | 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] | 7144 | /* there is no point trying this regex on headers */ |
| 7145 | return 1; |
| 7146 | } |
| 7147 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7148 | return done; |
| 7149 | } |
| 7150 | |
| 7151 | |
| 7152 | |
| 7153 | /* |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7154 | * 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] | 7155 | * Returns 0 if everything is alright, or -1 in case a replacement lead to an |
| 7156 | * unparsable response. |
| 7157 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7158 | 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] | 7159 | { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 7160 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7161 | struct http_txn *txn = s->txn; |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7162 | struct hdr_exp *exp; |
| 7163 | |
| 7164 | for (exp = px->rsp_exp; exp; exp = exp->next) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7165 | int ret; |
| 7166 | |
| 7167 | /* |
| 7168 | * The interleaving of transformations and verdicts |
| 7169 | * makes it difficult to decide to continue or stop |
| 7170 | * the evaluation. |
| 7171 | */ |
| 7172 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7173 | if (txn->flags & TX_SVDENY) |
| 7174 | break; |
| 7175 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7176 | if ((txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7177 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 7178 | exp->action == ACT_PASS)) { |
| 7179 | exp = exp->next; |
| 7180 | continue; |
| 7181 | } |
| 7182 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7183 | /* if this filter had a condition, evaluate it now and skip to |
| 7184 | * next filter if the condition does not match. |
| 7185 | */ |
| 7186 | if (exp->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 7187 | 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] | 7188 | ret = acl_pass(ret); |
| 7189 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 7190 | ret = !ret; |
| 7191 | if (!ret) |
| 7192 | continue; |
| 7193 | } |
| 7194 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7195 | /* Apply the filter to the status line. */ |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7196 | ret = apply_filter_to_sts_line(s, rtr, exp); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7197 | if (unlikely(ret < 0)) |
| 7198 | return -1; |
| 7199 | |
| 7200 | if (likely(ret == 0)) { |
| 7201 | /* The filter did not match the response, it can be |
| 7202 | * iterated through all headers. |
| 7203 | */ |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 7204 | if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0)) |
| 7205 | return -1; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7206 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7207 | } |
| 7208 | return 0; |
| 7209 | } |
| 7210 | |
| 7211 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7212 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 7213 | * 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] | 7214 | * desirable to call it only when needed. This function is also used when we |
| 7215 | * 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] | 7216 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7217 | void manage_server_side_cookies(struct stream *s, struct channel *res) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7218 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7219 | struct http_txn *txn = s->txn; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7220 | struct session *sess = s->sess; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 7221 | struct server *srv; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7222 | int is_cookie2; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7223 | int cur_idx, old_idx, delta; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7224 | char *hdr_beg, *hdr_end, *hdr_next; |
| 7225 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7226 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7227 | /* Iterate through the headers. |
| 7228 | * we start with the start line. |
| 7229 | */ |
| 7230 | old_idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7231 | hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7232 | |
| 7233 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 7234 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7235 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7236 | |
| 7237 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7238 | hdr_beg = hdr_next; |
| 7239 | hdr_end = hdr_beg + cur_hdr->len; |
| 7240 | hdr_next = hdr_end + cur_hdr->cr + 1; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7241 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7242 | /* We have one full header between hdr_beg and hdr_end, and the |
| 7243 | * next header starts at hdr_next. We're only interested in |
| 7244 | * "Set-Cookie" and "Set-Cookie2" headers. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7245 | */ |
| 7246 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7247 | is_cookie2 = 0; |
| 7248 | prev = hdr_beg + 10; |
| 7249 | val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10); |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7250 | if (!val) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7251 | val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11); |
| 7252 | if (!val) { |
| 7253 | old_idx = cur_idx; |
| 7254 | continue; |
| 7255 | } |
| 7256 | is_cookie2 = 1; |
| 7257 | prev = hdr_beg + 11; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7258 | } |
| 7259 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7260 | /* OK, right now we know we have a Set-Cookie* at hdr_beg, and |
| 7261 | * <prev> points to the colon. |
| 7262 | */ |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7263 | txn->flags |= TX_SCK_PRESENT; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7264 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7265 | /* Maybe we only wanted to see if there was a Set-Cookie (eg: |
| 7266 | * check-cache is enabled) and we are not interested in checking |
| 7267 | * them. Warning, the cookie capture is declared in the frontend. |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 7268 | */ |
Willy Tarreau | 53a09d5 | 2015-08-10 18:59:40 +0200 | [diff] [blame] | 7269 | if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7270 | return; |
| 7271 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7272 | /* OK so now we know we have to process this response cookie. |
| 7273 | * The format of the Set-Cookie header is slightly different |
| 7274 | * from the format of the Cookie header in that it does not |
| 7275 | * support the comma as a cookie delimiter (thus the header |
| 7276 | * cannot be folded) because the Expires attribute described in |
| 7277 | * the original Netscape's spec may contain an unquoted date |
| 7278 | * with a comma inside. We have to live with this because |
| 7279 | * many browsers don't support Max-Age and some browsers don't |
| 7280 | * support quoted strings. However the Set-Cookie2 header is |
| 7281 | * clean. |
| 7282 | * |
| 7283 | * We have to keep multiple pointers in order to support cookie |
| 7284 | * removal at the beginning, middle or end of header without |
| 7285 | * corrupting the header (in case of set-cookie2). A special |
| 7286 | * pointer, <scav> points to the beginning of the set-cookie-av |
| 7287 | * fields after the first semi-colon. The <next> pointer points |
| 7288 | * either to the end of line (set-cookie) or next unquoted comma |
| 7289 | * (set-cookie2). All of these headers are valid : |
| 7290 | * |
| 7291 | * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n |
| 7292 | * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n |
| 7293 | * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n |
| 7294 | * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n |
| 7295 | * | | | | | | | | | | |
| 7296 | * | | | | | | | | +-> next hdr_end <--+ |
| 7297 | * | | | | | | | +------------> scav |
| 7298 | * | | | | | | +--------------> val_end |
| 7299 | * | | | | | +--------------------> val_beg |
| 7300 | * | | | | +----------------------> equal |
| 7301 | * | | | +------------------------> att_end |
| 7302 | * | | +----------------------------> att_beg |
| 7303 | * | +------------------------------> prev |
| 7304 | * +-----------------------------------------> hdr_beg |
| 7305 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7306 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7307 | for (; prev < hdr_end; prev = next) { |
| 7308 | /* Iterate through all cookies on this line */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7309 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7310 | /* find att_beg */ |
| 7311 | att_beg = prev + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 7312 | while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg)) |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7313 | att_beg++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7314 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7315 | /* find att_end : this is the first character after the last non |
| 7316 | * space before the equal. It may be equal to hdr_end. |
| 7317 | */ |
| 7318 | equal = att_end = att_beg; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7319 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7320 | while (equal < hdr_end) { |
| 7321 | if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ',')) |
| 7322 | break; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 7323 | if (HTTP_IS_SPHT(*equal++)) |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7324 | continue; |
| 7325 | att_end = equal; |
| 7326 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7327 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7328 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 7329 | * is between <att_beg> and <equal>, both may be identical. |
| 7330 | */ |
| 7331 | |
| 7332 | /* look for end of cookie if there is an equal sign */ |
| 7333 | if (equal < hdr_end && *equal == '=') { |
| 7334 | /* look for the beginning of the value */ |
| 7335 | val_beg = equal + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 7336 | while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg)) |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7337 | val_beg++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7338 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7339 | /* find the end of the value, respecting quotes */ |
| 7340 | next = find_cookie_value_end(val_beg, hdr_end); |
| 7341 | |
| 7342 | /* make val_end point to the first white space or delimitor after the value */ |
| 7343 | val_end = next; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 7344 | while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1))) |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7345 | val_end--; |
| 7346 | } else { |
| 7347 | /* <equal> points to next comma, semi-colon or EOL */ |
| 7348 | val_beg = val_end = next = equal; |
| 7349 | } |
| 7350 | |
| 7351 | if (next < hdr_end) { |
| 7352 | /* Set-Cookie2 supports multiple cookies, and <next> points to |
| 7353 | * a colon or semi-colon before the end. So skip all attr-value |
| 7354 | * pairs and look for the next comma. For Set-Cookie, since |
| 7355 | * commas are permitted in values, skip to the end. |
| 7356 | */ |
| 7357 | if (is_cookie2) |
| 7358 | next = find_hdr_value_end(next, hdr_end); |
| 7359 | else |
| 7360 | next = hdr_end; |
| 7361 | } |
| 7362 | |
| 7363 | /* Now everything is as on the diagram above */ |
| 7364 | |
| 7365 | /* Ignore cookies with no equal sign */ |
| 7366 | if (equal == val_end) |
| 7367 | continue; |
| 7368 | |
| 7369 | /* If there are spaces around the equal sign, we need to |
| 7370 | * strip them otherwise we'll get trouble for cookie captures, |
| 7371 | * or even for rewrites. Since this happens extremely rarely, |
| 7372 | * it does not hurt performance. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7373 | */ |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7374 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 7375 | int stripped_before = 0; |
| 7376 | int stripped_after = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7377 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7378 | if (att_end != equal) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7379 | stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7380 | equal += stripped_before; |
| 7381 | val_beg += stripped_before; |
| 7382 | } |
| 7383 | |
| 7384 | if (val_beg > equal + 1) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7385 | stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7386 | val_beg += stripped_after; |
| 7387 | stripped_before += stripped_after; |
| 7388 | } |
| 7389 | |
| 7390 | val_end += stripped_before; |
| 7391 | next += stripped_before; |
| 7392 | hdr_end += stripped_before; |
| 7393 | hdr_next += stripped_before; |
| 7394 | cur_hdr->len += stripped_before; |
Willy Tarreau | 1fc1f45 | 2011-04-07 22:35:37 +0200 | [diff] [blame] | 7395 | http_msg_move_end(&txn->rsp, stripped_before); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7396 | } |
| 7397 | |
| 7398 | /* First, let's see if we want to capture this cookie. We check |
| 7399 | * that we don't already have a server side cookie, because we |
| 7400 | * can only capture one. Also as an optimisation, we ignore |
| 7401 | * cookies shorter than the declared name. |
| 7402 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7403 | if (sess->fe->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 7404 | txn->srv_cookie == NULL && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7405 | (val_end - att_beg >= sess->fe->capture_namelen) && |
| 7406 | memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7407 | int log_len = val_end - att_beg; |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 7408 | if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7409 | Alert("HTTP logging : out of memory.\n"); |
| 7410 | } |
Willy Tarreau | f70fc75 | 2010-11-19 11:27:18 +0100 | [diff] [blame] | 7411 | else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7412 | if (log_len > sess->fe->capture_len) |
| 7413 | log_len = sess->fe->capture_len; |
Willy Tarreau | f70fc75 | 2010-11-19 11:27:18 +0100 | [diff] [blame] | 7414 | memcpy(txn->srv_cookie, att_beg, log_len); |
| 7415 | txn->srv_cookie[log_len] = 0; |
| 7416 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7417 | } |
| 7418 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7419 | srv = objt_server(s->target); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7420 | /* now check if we need to process it for persistence */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 7421 | if (!(s->flags & SF_IGNORE_PRST) && |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7422 | (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) && |
| 7423 | (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) { |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7424 | /* assume passive cookie by default */ |
| 7425 | txn->flags &= ~TX_SCK_MASK; |
| 7426 | txn->flags |= TX_SCK_FOUND; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7427 | |
| 7428 | /* If the cookie is in insert mode on a known server, we'll delete |
| 7429 | * this occurrence because we'll insert another one later. |
| 7430 | * 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] | 7431 | * a direct access. |
| 7432 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7433 | if (s->be->ck_opts & PR_CK_PSV) { |
Willy Tarreau | ba4c5be | 2010-10-23 12:46:42 +0200 | [diff] [blame] | 7434 | /* The "preserve" flag was set, we don't want to touch the |
| 7435 | * server's cookie. |
| 7436 | */ |
| 7437 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7438 | else if ((srv && (s->be->ck_opts & PR_CK_INS)) || |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 7439 | ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7440 | /* this cookie must be deleted */ |
| 7441 | if (*prev == ':' && next == hdr_end) { |
| 7442 | /* whole header */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7443 | delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7444 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 7445 | txn->hdr_idx.used--; |
| 7446 | cur_hdr->len = 0; |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 7447 | cur_idx = old_idx; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7448 | hdr_next += delta; |
| 7449 | http_msg_move_end(&txn->rsp, delta); |
| 7450 | /* note: while both invalid now, <next> and <hdr_end> |
| 7451 | * are still equal, so the for() will stop as expected. |
| 7452 | */ |
| 7453 | } else { |
| 7454 | /* just remove the value */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7455 | int delta = del_hdr_value(res->buf, &prev, next); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7456 | next = prev; |
| 7457 | hdr_end += delta; |
| 7458 | hdr_next += delta; |
| 7459 | cur_hdr->len += delta; |
| 7460 | http_msg_move_end(&txn->rsp, delta); |
| 7461 | } |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7462 | txn->flags &= ~TX_SCK_MASK; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7463 | txn->flags |= TX_SCK_DELETED; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7464 | /* and go on with next cookie */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7465 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7466 | else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7467 | /* replace bytes val_beg->val_end with the cookie name associated |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7468 | * with this server since we know it. |
| 7469 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7470 | 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] | 7471 | next += delta; |
| 7472 | hdr_end += delta; |
| 7473 | hdr_next += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7474 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7475 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7476 | |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7477 | txn->flags &= ~TX_SCK_MASK; |
| 7478 | txn->flags |= TX_SCK_REPLACED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7479 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7480 | else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7481 | /* insert the cookie name associated with this server |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7482 | * before existing cookie, and insert a delimiter between them.. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7483 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7484 | 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] | 7485 | next += delta; |
| 7486 | hdr_end += delta; |
| 7487 | hdr_next += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7488 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7489 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7490 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 7491 | val_beg[srv->cklen] = COOKIE_DELIM; |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7492 | txn->flags &= ~TX_SCK_MASK; |
| 7493 | txn->flags |= TX_SCK_REPLACED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7494 | } |
| 7495 | } |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7496 | /* that's done for this cookie, check the next one on the same |
| 7497 | * line when next != hdr_end (only if is_cookie2). |
| 7498 | */ |
| 7499 | } |
| 7500 | /* check next header */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7501 | old_idx = cur_idx; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7502 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7503 | } |
| 7504 | |
| 7505 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7506 | /* |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7507 | * Check if response is cacheable or not. Updates s->flags. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7508 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7509 | void check_response_for_cacheability(struct stream *s, struct channel *rtr) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7510 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7511 | struct http_txn *txn = s->txn; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7512 | char *p1, *p2; |
| 7513 | |
| 7514 | char *cur_ptr, *cur_end, *cur_next; |
| 7515 | int cur_idx; |
| 7516 | |
Willy Tarreau | 5df5187 | 2007-11-25 16:20:08 +0100 | [diff] [blame] | 7517 | if (!(txn->flags & TX_CACHEABLE)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7518 | return; |
| 7519 | |
| 7520 | /* Iterate through the headers. |
| 7521 | * we start with the start line. |
| 7522 | */ |
| 7523 | cur_idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7524 | cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7525 | |
| 7526 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 7527 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7528 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7529 | |
| 7530 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 7531 | cur_ptr = cur_next; |
| 7532 | cur_end = cur_ptr + cur_hdr->len; |
| 7533 | cur_next = cur_end + cur_hdr->cr + 1; |
| 7534 | |
| 7535 | /* We have one full header between cur_ptr and cur_end, and the |
| 7536 | * next header starts at cur_next. We're only interested in |
| 7537 | * "Cookie:" headers. |
| 7538 | */ |
| 7539 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7540 | val = http_header_match2(cur_ptr, cur_end, "Pragma", 6); |
| 7541 | if (val) { |
| 7542 | if ((cur_end - (cur_ptr + val) >= 8) && |
| 7543 | strncasecmp(cur_ptr + val, "no-cache", 8) == 0) { |
| 7544 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 7545 | return; |
| 7546 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7547 | } |
| 7548 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7549 | val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13); |
| 7550 | if (!val) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7551 | continue; |
| 7552 | |
| 7553 | /* OK, right now we know we have a cache-control header at cur_ptr */ |
| 7554 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7555 | p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7556 | |
| 7557 | if (p1 >= cur_end) /* no more info */ |
| 7558 | continue; |
| 7559 | |
| 7560 | /* p1 is at the beginning of the value */ |
| 7561 | p2 = p1; |
| 7562 | |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 7563 | while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7564 | p2++; |
| 7565 | |
| 7566 | /* we have a complete value between p1 and p2 */ |
| 7567 | if (p2 < cur_end && *p2 == '=') { |
| 7568 | /* we have something of the form no-cache="set-cookie" */ |
| 7569 | if ((cur_end - p1 >= 21) && |
| 7570 | strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0 |
| 7571 | && (p1[20] == '"' || p1[20] == ',')) |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7572 | txn->flags &= ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7573 | continue; |
| 7574 | } |
| 7575 | |
| 7576 | /* OK, so we know that either p2 points to the end of string or to a comma */ |
| 7577 | if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) || |
Willy Tarreau | 5b15f90 | 2013-07-04 12:46:56 +0200 | [diff] [blame] | 7578 | ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) || |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7579 | ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) || |
| 7580 | ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) || |
| 7581 | ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7582 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7583 | return; |
| 7584 | } |
| 7585 | |
| 7586 | if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7587 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7588 | continue; |
| 7589 | } |
| 7590 | } |
| 7591 | } |
| 7592 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7593 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7594 | /* |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7595 | * 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] | 7596 | * for the current backend. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7597 | * |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7598 | * 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] | 7599 | * uri_auth field is valid. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7600 | * |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 7601 | * Returns 1 if stats should be provided, otherwise 0. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7602 | */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7603 | 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] | 7604 | { |
| 7605 | struct uri_auth *uri_auth = backend->uri_auth; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7606 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7607 | const char *uri = msg->chn->buf->p+ msg->sl.rq.u; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7608 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 7609 | if (!uri_auth) |
| 7610 | return 0; |
| 7611 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7612 | 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] | 7613 | return 0; |
| 7614 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7615 | /* check URI size */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7616 | if (uri_auth->uri_len > msg->sl.rq.u_l) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7617 | return 0; |
| 7618 | |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 7619 | if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7620 | return 0; |
| 7621 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7622 | return 1; |
| 7623 | } |
| 7624 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7625 | /* |
| 7626 | * 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] | 7627 | * By default it tries to report the error position as msg->err_pos. However if |
| 7628 | * this one is not set, it will then report msg->next, which is the last known |
| 7629 | * 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] | 7630 | * displays buffers as a contiguous area starting at buf->p. |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7631 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7632 | void http_capture_bad_message(struct error_snapshot *es, struct stream *s, |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 7633 | struct http_msg *msg, |
Willy Tarreau | 0da5b3b | 2017-09-21 09:30:46 +0200 | [diff] [blame] | 7634 | enum h1_state state, struct proxy *other_end) |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7635 | { |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 7636 | struct session *sess = strm_sess(s); |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 7637 | struct channel *chn = msg->chn; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 7638 | int len1, len2; |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 7639 | |
Willy Tarreau | f3764b7 | 2016-03-31 13:45:10 +0200 | [diff] [blame] | 7640 | es->len = MIN(chn->buf->i, global.tune.bufsize); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7641 | len1 = chn->buf->data + chn->buf->size - chn->buf->p; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 7642 | len1 = MIN(len1, es->len); |
| 7643 | len2 = es->len - len1; /* remaining data if buffer wraps */ |
| 7644 | |
Willy Tarreau | f3764b7 | 2016-03-31 13:45:10 +0200 | [diff] [blame] | 7645 | if (!es->buf) |
| 7646 | es->buf = malloc(global.tune.bufsize); |
| 7647 | |
| 7648 | if (es->buf) { |
| 7649 | memcpy(es->buf, chn->buf->p, len1); |
| 7650 | if (len2) |
| 7651 | memcpy(es->buf + len1, chn->buf->data, len2); |
| 7652 | } |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 7653 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7654 | if (msg->err_pos >= 0) |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 7655 | es->pos = msg->err_pos; |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 7656 | else |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 7657 | es->pos = msg->next; |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 7658 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7659 | es->when = date; // user-visible date |
| 7660 | es->sid = s->uniq_id; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 7661 | es->srv = objt_server(s->target); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7662 | es->oe = other_end; |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 7663 | if (objt_conn(sess->origin)) |
| 7664 | es->src = __objt_conn(sess->origin)->addr.from; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7665 | else |
| 7666 | memset(&es->src, 0, sizeof(es->src)); |
| 7667 | |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 7668 | es->state = state; |
Willy Tarreau | 10479e4 | 2010-12-12 14:00:34 +0100 | [diff] [blame] | 7669 | es->ev_id = error_snapshot_id++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 7670 | es->b_flags = chn->flags; |
Willy Tarreau | d04b1bc | 2012-05-08 11:03:10 +0200 | [diff] [blame] | 7671 | es->s_flags = s->flags; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7672 | es->t_flags = s->txn->flags; |
Willy Tarreau | d04b1bc | 2012-05-08 11:03:10 +0200 | [diff] [blame] | 7673 | es->m_flags = msg->flags; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7674 | es->b_out = chn->buf->o; |
| 7675 | es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 7676 | es->b_tot = chn->total; |
Willy Tarreau | d04b1bc | 2012-05-08 11:03:10 +0200 | [diff] [blame] | 7677 | es->m_clen = msg->chunk_len; |
| 7678 | es->m_blen = msg->body_len; |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7679 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7680 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7681 | /* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of |
| 7682 | * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is |
| 7683 | * performed over the whole headers. Otherwise it must contain a valid header |
| 7684 | * context, initialised with ctx->idx=0 for the first lookup in a series. If |
| 7685 | * <occ> is positive or null, occurrence #occ from the beginning (or last ctx) |
| 7686 | * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less |
| 7687 | * 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] | 7688 | * -1. The value fetch stops at commas, so this function is suited for use with |
| 7689 | * list headers. |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7690 | * 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] | 7691 | */ |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 7692 | 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] | 7693 | struct hdr_idx *idx, int occ, |
| 7694 | struct hdr_ctx *ctx, char **vptr, int *vlen) |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7695 | { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7696 | struct hdr_ctx local_ctx; |
| 7697 | char *ptr_hist[MAX_HDR_HISTORY]; |
| 7698 | int len_hist[MAX_HDR_HISTORY]; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7699 | unsigned int hist_ptr; |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7700 | int found; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7701 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7702 | if (!ctx) { |
| 7703 | local_ctx.idx = 0; |
| 7704 | ctx = &local_ctx; |
| 7705 | } |
| 7706 | |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7707 | if (occ >= 0) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7708 | /* search from the beginning */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7709 | while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7710 | occ--; |
| 7711 | if (occ <= 0) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7712 | *vptr = ctx->line + ctx->val; |
| 7713 | *vlen = ctx->vlen; |
| 7714 | return 1; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7715 | } |
| 7716 | } |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7717 | return 0; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7718 | } |
| 7719 | |
| 7720 | /* negative occurrence, we scan all the list then walk back */ |
| 7721 | if (-occ > MAX_HDR_HISTORY) |
| 7722 | return 0; |
| 7723 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7724 | found = hist_ptr = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7725 | while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7726 | ptr_hist[hist_ptr] = ctx->line + ctx->val; |
| 7727 | len_hist[hist_ptr] = ctx->vlen; |
| 7728 | if (++hist_ptr >= MAX_HDR_HISTORY) |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7729 | hist_ptr = 0; |
| 7730 | found++; |
| 7731 | } |
| 7732 | if (-occ > found) |
| 7733 | return 0; |
| 7734 | /* 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] | 7735 | * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have |
| 7736 | * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ] |
| 7737 | * to remain in the 0..9 range. |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7738 | */ |
Willy Tarreau | 67dad27 | 2013-06-12 22:27:44 +0200 | [diff] [blame] | 7739 | hist_ptr += occ + MAX_HDR_HISTORY; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7740 | if (hist_ptr >= MAX_HDR_HISTORY) |
| 7741 | hist_ptr -= MAX_HDR_HISTORY; |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7742 | *vptr = ptr_hist[hist_ptr]; |
| 7743 | *vlen = len_hist[hist_ptr]; |
| 7744 | return 1; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7745 | } |
| 7746 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 7747 | /* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of |
| 7748 | * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is |
| 7749 | * performed over the whole headers. Otherwise it must contain a valid header |
| 7750 | * context, initialised with ctx->idx=0 for the first lookup in a series. If |
| 7751 | * <occ> is positive or null, occurrence #occ from the beginning (or last ctx) |
| 7752 | * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less |
| 7753 | * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is |
| 7754 | * -1. This function differs from http_get_hdr() in that it only returns full |
| 7755 | * line header values and does not stop at commas. |
| 7756 | * The return value is 0 if nothing was found, or non-zero otherwise. |
| 7757 | */ |
| 7758 | unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen, |
| 7759 | struct hdr_idx *idx, int occ, |
| 7760 | struct hdr_ctx *ctx, char **vptr, int *vlen) |
| 7761 | { |
| 7762 | struct hdr_ctx local_ctx; |
| 7763 | char *ptr_hist[MAX_HDR_HISTORY]; |
| 7764 | int len_hist[MAX_HDR_HISTORY]; |
| 7765 | unsigned int hist_ptr; |
| 7766 | int found; |
| 7767 | |
| 7768 | if (!ctx) { |
| 7769 | local_ctx.idx = 0; |
| 7770 | ctx = &local_ctx; |
| 7771 | } |
| 7772 | |
| 7773 | if (occ >= 0) { |
| 7774 | /* search from the beginning */ |
| 7775 | while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
| 7776 | occ--; |
| 7777 | if (occ <= 0) { |
| 7778 | *vptr = ctx->line + ctx->val; |
| 7779 | *vlen = ctx->vlen; |
| 7780 | return 1; |
| 7781 | } |
| 7782 | } |
| 7783 | return 0; |
| 7784 | } |
| 7785 | |
| 7786 | /* negative occurrence, we scan all the list then walk back */ |
| 7787 | if (-occ > MAX_HDR_HISTORY) |
| 7788 | return 0; |
| 7789 | |
| 7790 | found = hist_ptr = 0; |
| 7791 | while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
| 7792 | ptr_hist[hist_ptr] = ctx->line + ctx->val; |
| 7793 | len_hist[hist_ptr] = ctx->vlen; |
| 7794 | if (++hist_ptr >= MAX_HDR_HISTORY) |
| 7795 | hist_ptr = 0; |
| 7796 | found++; |
| 7797 | } |
| 7798 | if (-occ > found) |
| 7799 | return 0; |
Nenad Merdanovic | 69ad4b9 | 2016-03-29 13:14:30 +0200 | [diff] [blame] | 7800 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 7801 | /* 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] | 7802 | * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have |
| 7803 | * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ] |
| 7804 | * to remain in the 0..9 range. |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 7805 | */ |
Nenad Merdanovic | 69ad4b9 | 2016-03-29 13:14:30 +0200 | [diff] [blame] | 7806 | hist_ptr += occ + MAX_HDR_HISTORY; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 7807 | if (hist_ptr >= MAX_HDR_HISTORY) |
| 7808 | hist_ptr -= MAX_HDR_HISTORY; |
| 7809 | *vptr = ptr_hist[hist_ptr]; |
| 7810 | *vlen = len_hist[hist_ptr]; |
| 7811 | return 1; |
| 7812 | } |
| 7813 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 7814 | /* |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 7815 | * Print a debug line with a header. Always stop at the first CR or LF char, |
| 7816 | * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an |
| 7817 | * arrow is printed after the line which contains the pointer. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7818 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7819 | 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] | 7820 | { |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 7821 | struct session *sess = strm_sess(s); |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7822 | int max; |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 7823 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7824 | 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] | 7825 | dir, |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 7826 | objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1, |
| 7827 | objt_conn(s->si[1].end) ? (unsigned short)objt_conn(s->si[1].end)->handle.fd : -1); |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 7828 | |
| 7829 | for (max = 0; start + max < end; max++) |
| 7830 | if (start[max] == '\r' || start[max] == '\n') |
| 7831 | break; |
| 7832 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7833 | UBOUND(max, trash.size - trash.len - 3); |
| 7834 | trash.len += strlcpy2(trash.str + trash.len, start, max + 1); |
| 7835 | trash.str[trash.len++] = '\n'; |
Willy Tarreau | 89efaed | 2013-12-13 15:14:55 +0100 | [diff] [blame] | 7836 | shut_your_big_mouth_gcc(write(1, trash.str, trash.len)); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7837 | } |
| 7838 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7839 | |
| 7840 | /* Allocate a new HTTP transaction for stream <s> unless there is one already. |
| 7841 | * The hdr_idx is allocated as well. In case of allocation failure, everything |
| 7842 | * allocated is freed and NULL is returned. Otherwise the new transaction is |
| 7843 | * assigned to the stream and returned. |
| 7844 | */ |
| 7845 | struct http_txn *http_alloc_txn(struct stream *s) |
| 7846 | { |
| 7847 | struct http_txn *txn = s->txn; |
| 7848 | |
| 7849 | if (txn) |
| 7850 | return txn; |
| 7851 | |
| 7852 | txn = pool_alloc2(pool2_http_txn); |
| 7853 | if (!txn) |
| 7854 | return txn; |
| 7855 | |
| 7856 | txn->hdr_idx.size = global.tune.max_http_hdr; |
| 7857 | txn->hdr_idx.v = pool_alloc2(pool2_hdr_idx); |
| 7858 | if (!txn->hdr_idx.v) { |
| 7859 | pool_free2(pool2_http_txn, txn); |
| 7860 | return NULL; |
| 7861 | } |
| 7862 | |
| 7863 | s->txn = txn; |
| 7864 | return txn; |
| 7865 | } |
| 7866 | |
Thierry FOURNIER | fd50f0b | 2015-09-25 18:53:18 +0200 | [diff] [blame] | 7867 | void http_txn_reset_req(struct http_txn *txn) |
| 7868 | { |
| 7869 | txn->req.flags = 0; |
| 7870 | txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */ |
| 7871 | txn->req.next = 0; |
| 7872 | txn->req.chunk_len = 0LL; |
| 7873 | txn->req.body_len = 0LL; |
| 7874 | txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */ |
| 7875 | } |
| 7876 | |
| 7877 | void http_txn_reset_res(struct http_txn *txn) |
| 7878 | { |
| 7879 | txn->rsp.flags = 0; |
| 7880 | txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */ |
| 7881 | txn->rsp.next = 0; |
| 7882 | txn->rsp.chunk_len = 0LL; |
| 7883 | txn->rsp.body_len = 0LL; |
| 7884 | txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */ |
| 7885 | } |
| 7886 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7887 | /* |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7888 | * 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] | 7889 | * the required fields are properly allocated and that we only need to (re)init |
| 7890 | * them. This should be used before processing any new request. |
| 7891 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7892 | void http_init_txn(struct stream *s) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7893 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7894 | struct http_txn *txn = s->txn; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 7895 | struct proxy *fe = strm_fe(s); |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7896 | |
| 7897 | txn->flags = 0; |
| 7898 | txn->status = -1; |
| 7899 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7900 | txn->cookie_first_date = 0; |
| 7901 | txn->cookie_last_date = 0; |
| 7902 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7903 | txn->srv_cookie = NULL; |
| 7904 | txn->cli_cookie = NULL; |
| 7905 | txn->uri = NULL; |
| 7906 | |
Thierry FOURNIER | fd50f0b | 2015-09-25 18:53:18 +0200 | [diff] [blame] | 7907 | http_txn_reset_req(txn); |
| 7908 | http_txn_reset_res(txn); |
| 7909 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 7910 | txn->req.chn = &s->req; |
| 7911 | txn->rsp.chn = &s->res; |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 7912 | |
| 7913 | txn->auth.method = HTTP_AUTH_UNKNOWN; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7914 | |
| 7915 | txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */ |
| 7916 | if (fe->options2 & PR_O2_REQBUG_OK) |
| 7917 | txn->req.err_pos = -1; /* let buggy requests pass */ |
| 7918 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7919 | if (txn->hdr_idx.v) |
| 7920 | hdr_idx_init(&txn->hdr_idx); |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 7921 | |
| 7922 | vars_init(&s->vars_txn, SCOPE_TXN); |
| 7923 | vars_init(&s->vars_reqres, SCOPE_REQ); |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7924 | } |
| 7925 | |
| 7926 | /* to be used at the end of a transaction */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7927 | void http_end_txn(struct stream *s) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7928 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7929 | struct http_txn *txn = s->txn; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 7930 | struct proxy *fe = strm_fe(s); |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7931 | |
| 7932 | /* these ones will have been dynamically allocated */ |
| 7933 | pool_free2(pool2_requri, txn->uri); |
| 7934 | pool_free2(pool2_capture, txn->cli_cookie); |
| 7935 | pool_free2(pool2_capture, txn->srv_cookie); |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 7936 | pool_free2(pool2_uniqueid, s->unique_id); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 7937 | |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 7938 | s->unique_id = NULL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7939 | txn->uri = NULL; |
| 7940 | txn->srv_cookie = NULL; |
| 7941 | txn->cli_cookie = NULL; |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 7942 | |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 7943 | if (s->req_cap) { |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 7944 | struct cap_hdr *h; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7945 | for (h = fe->req_cap; h; h = h->next) |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 7946 | pool_free2(h->pool, s->req_cap[h->index]); |
| 7947 | memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *)); |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 7948 | } |
| 7949 | |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 7950 | if (s->res_cap) { |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 7951 | struct cap_hdr *h; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7952 | for (h = fe->rsp_cap; h; h = h->next) |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 7953 | pool_free2(h->pool, s->res_cap[h->index]); |
| 7954 | memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *)); |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 7955 | } |
| 7956 | |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 7957 | vars_prune(&s->vars_txn, s->sess, s); |
| 7958 | vars_prune(&s->vars_reqres, s->sess, s); |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7959 | } |
| 7960 | |
| 7961 | /* 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] | 7962 | void http_reset_txn(struct stream *s) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7963 | { |
| 7964 | http_end_txn(s); |
| 7965 | http_init_txn(s); |
| 7966 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 7967 | /* reinitialise the current rule list pointer to NULL. We are sure that |
| 7968 | * any rulelist match the NULL pointer. |
| 7969 | */ |
| 7970 | s->current_rule_list = NULL; |
| 7971 | |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 7972 | s->be = strm_fe(s); |
| 7973 | s->logs.logwait = strm_fe(s)->to_log; |
Willy Tarreau | abcd514 | 2013-06-11 17:18:02 +0200 | [diff] [blame] | 7974 | s->logs.level = 0; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7975 | stream_del_srv_conn(s); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 7976 | s->target = NULL; |
Emeric Brun | b982a3d | 2010-01-04 15:45:53 +0100 | [diff] [blame] | 7977 | /* re-init store persistence */ |
| 7978 | s->store_count = 0; |
Willy Tarreau | 1f0da24 | 2014-01-25 11:01:50 +0100 | [diff] [blame] | 7979 | s->uniq_id = global.req_count++; |
Emeric Brun | b982a3d | 2010-01-04 15:45:53 +0100 | [diff] [blame] | 7980 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7981 | s->pend_pos = NULL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7982 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 7983 | s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */ |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7984 | |
Willy Tarreau | 739cfba | 2010-01-25 23:11:14 +0100 | [diff] [blame] | 7985 | /* We must trim any excess data from the response buffer, because we |
| 7986 | * may have blocked an invalid response from a server that we don't |
| 7987 | * want to accidentely forward once we disable the analysers, nor do |
| 7988 | * we want those data to come along with next response. A typical |
| 7989 | * example of such data would be from a buggy server responding to |
| 7990 | * a HEAD with some data, or sending more than the advertised |
| 7991 | * content-length. |
| 7992 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 7993 | if (unlikely(s->res.buf->i)) |
| 7994 | s->res.buf->i = 0; |
Willy Tarreau | 739cfba | 2010-01-25 23:11:14 +0100 | [diff] [blame] | 7995 | |
Christopher Faulet | c0c672a | 2017-03-28 11:51:33 +0200 | [diff] [blame] | 7996 | /* Now we can realign the response buffer */ |
| 7997 | buffer_realign(s->res.buf); |
| 7998 | |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 7999 | s->req.rto = strm_fe(s)->timeout.client; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 8000 | s->req.wto = TICK_ETERNITY; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8001 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 8002 | s->res.rto = TICK_ETERNITY; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 8003 | s->res.wto = strm_fe(s)->timeout.client; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8004 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 8005 | s->req.rex = TICK_ETERNITY; |
| 8006 | s->req.wex = TICK_ETERNITY; |
| 8007 | s->req.analyse_exp = TICK_ETERNITY; |
| 8008 | s->res.rex = TICK_ETERNITY; |
| 8009 | s->res.wex = TICK_ETERNITY; |
| 8010 | s->res.analyse_exp = TICK_ETERNITY; |
Hongbo Long | e39683c | 2017-03-10 18:41:51 +0100 | [diff] [blame] | 8011 | s->si[1].hcto = TICK_ETERNITY; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8012 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8013 | |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 8014 | void free_http_res_rules(struct list *r) |
| 8015 | { |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 8016 | struct act_rule *tr, *pr; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 8017 | |
| 8018 | list_for_each_entry_safe(pr, tr, r, list) { |
| 8019 | LIST_DEL(&pr->list); |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 8020 | regex_free(&pr->arg.hdr_add.re); |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 8021 | free(pr); |
| 8022 | } |
| 8023 | } |
| 8024 | |
| 8025 | void free_http_req_rules(struct list *r) |
| 8026 | { |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 8027 | struct act_rule *tr, *pr; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8028 | |
| 8029 | list_for_each_entry_safe(pr, tr, r, list) { |
| 8030 | LIST_DEL(&pr->list); |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8031 | if (pr->action == ACT_HTTP_REQ_AUTH) |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 8032 | free(pr->arg.auth.realm); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8033 | |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 8034 | regex_free(&pr->arg.hdr_add.re); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8035 | free(pr); |
| 8036 | } |
| 8037 | } |
| 8038 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8039 | /* parse an "http-request" rule */ |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 8040 | 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] | 8041 | { |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 8042 | struct act_rule *rule; |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 8043 | struct action_kw *custom = NULL; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8044 | int cur_arg; |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 8045 | char *error; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8046 | |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 8047 | rule = calloc(1, sizeof(*rule)); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8048 | if (!rule) { |
| 8049 | Alert("parsing [%s:%d]: out of memory.\n", file, linenum); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8050 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8051 | } |
| 8052 | |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 8053 | if (!strcmp(args[0], "allow")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8054 | rule->action = ACT_ACTION_ALLOW; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8055 | cur_arg = 1; |
Jarno Huuskonen | 800d176 | 2017-03-06 14:56:36 +0200 | [diff] [blame] | 8056 | } 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] | 8057 | int code; |
| 8058 | int hc; |
| 8059 | |
Jarno Huuskonen | 800d176 | 2017-03-06 14:56:36 +0200 | [diff] [blame] | 8060 | if (!strcmp(args[0], "tarpit")) { |
| 8061 | rule->action = ACT_HTTP_REQ_TARPIT; |
| 8062 | rule->deny_status = HTTP_ERR_500; |
| 8063 | } |
| 8064 | else { |
| 8065 | rule->action = ACT_ACTION_DENY; |
| 8066 | rule->deny_status = HTTP_ERR_403; |
| 8067 | } |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8068 | cur_arg = 1; |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 8069 | if (strcmp(args[cur_arg], "deny_status") == 0) { |
| 8070 | cur_arg++; |
| 8071 | if (!args[cur_arg]) { |
| 8072 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing status code.\n", |
| 8073 | file, linenum, proxy_type_str(proxy), proxy->id, args[0]); |
| 8074 | goto out_err; |
| 8075 | } |
| 8076 | |
| 8077 | code = atol(args[cur_arg]); |
| 8078 | cur_arg++; |
| 8079 | for (hc = 0; hc < HTTP_ERR_SIZE; hc++) { |
| 8080 | if (http_err_codes[hc] == code) { |
| 8081 | rule->deny_status = hc; |
| 8082 | break; |
| 8083 | } |
| 8084 | } |
| 8085 | |
| 8086 | if (hc >= HTTP_ERR_SIZE) { |
Jarno Huuskonen | 800d176 | 2017-03-06 14:56:36 +0200 | [diff] [blame] | 8087 | Warning("parsing [%s:%d] : status code %d not handled, using default code %d.\n", |
| 8088 | file, linenum, code, http_err_codes[rule->deny_status]); |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 8089 | } |
| 8090 | } |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8091 | } else if (!strcmp(args[0], "auth")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8092 | rule->action = ACT_HTTP_REQ_AUTH; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8093 | cur_arg = 1; |
| 8094 | |
| 8095 | while(*args[cur_arg]) { |
| 8096 | if (!strcmp(args[cur_arg], "realm")) { |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 8097 | rule->arg.auth.realm = strdup(args[cur_arg + 1]); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8098 | cur_arg+=2; |
| 8099 | continue; |
| 8100 | } else |
| 8101 | break; |
| 8102 | } |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 8103 | } else if (!strcmp(args[0], "set-nice")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8104 | rule->action = ACT_HTTP_SET_NICE; |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 8105 | cur_arg = 1; |
| 8106 | |
| 8107 | if (!*args[cur_arg] || |
| 8108 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 8109 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n", |
| 8110 | file, linenum, args[0]); |
| 8111 | goto out_err; |
| 8112 | } |
| 8113 | rule->arg.nice = atoi(args[cur_arg]); |
| 8114 | if (rule->arg.nice < -1024) |
| 8115 | rule->arg.nice = -1024; |
| 8116 | else if (rule->arg.nice > 1024) |
| 8117 | rule->arg.nice = 1024; |
| 8118 | cur_arg++; |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 8119 | } else if (!strcmp(args[0], "set-tos")) { |
| 8120 | #ifdef IP_TOS |
| 8121 | char *err; |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8122 | rule->action = ACT_HTTP_SET_TOS; |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 8123 | cur_arg = 1; |
| 8124 | |
| 8125 | if (!*args[cur_arg] || |
| 8126 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 8127 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n", |
| 8128 | file, linenum, args[0]); |
| 8129 | goto out_err; |
| 8130 | } |
| 8131 | |
| 8132 | rule->arg.tos = strtol(args[cur_arg], &err, 0); |
| 8133 | if (err && *err != '\0') { |
| 8134 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n", |
| 8135 | file, linenum, err, args[0]); |
| 8136 | goto out_err; |
| 8137 | } |
| 8138 | cur_arg++; |
| 8139 | #else |
| 8140 | Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]); |
| 8141 | goto out_err; |
| 8142 | #endif |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 8143 | } else if (!strcmp(args[0], "set-mark")) { |
| 8144 | #ifdef SO_MARK |
| 8145 | char *err; |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8146 | rule->action = ACT_HTTP_SET_MARK; |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 8147 | cur_arg = 1; |
| 8148 | |
| 8149 | if (!*args[cur_arg] || |
| 8150 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 8151 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n", |
| 8152 | file, linenum, args[0]); |
| 8153 | goto out_err; |
| 8154 | } |
| 8155 | |
| 8156 | rule->arg.mark = strtoul(args[cur_arg], &err, 0); |
| 8157 | if (err && *err != '\0') { |
| 8158 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n", |
| 8159 | file, linenum, err, args[0]); |
| 8160 | goto out_err; |
| 8161 | } |
| 8162 | cur_arg++; |
| 8163 | global.last_checks |= LSTCHK_NETADM; |
| 8164 | #else |
| 8165 | Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]); |
| 8166 | goto out_err; |
| 8167 | #endif |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 8168 | } else if (!strcmp(args[0], "set-log-level")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8169 | rule->action = ACT_HTTP_SET_LOGL; |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 8170 | cur_arg = 1; |
| 8171 | |
| 8172 | if (!*args[cur_arg] || |
| 8173 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 8174 | bad_log_level: |
| 8175 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n", |
| 8176 | file, linenum, args[0]); |
| 8177 | goto out_err; |
| 8178 | } |
| 8179 | if (strcmp(args[cur_arg], "silent") == 0) |
| 8180 | rule->arg.loglevel = -1; |
| 8181 | else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0) |
| 8182 | goto bad_log_level; |
| 8183 | cur_arg++; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 8184 | } 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] | 8185 | 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] | 8186 | cur_arg = 1; |
| 8187 | |
Willy Tarreau | 8d1c516 | 2013-04-03 14:13:58 +0200 | [diff] [blame] | 8188 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 8189 | (*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] | 8190 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n", |
| 8191 | file, linenum, args[0]); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8192 | goto out_err; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 8193 | } |
| 8194 | |
| 8195 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 8196 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 8197 | LIST_INIT(&rule->arg.hdr_add.fmt); |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 8198 | |
Thierry FOURNIER | 1c0054f | 2013-11-20 15:09:52 +0100 | [diff] [blame] | 8199 | proxy->conf.args.ctx = ARGC_HRQ; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8200 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8201 | 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] | 8202 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) { |
| 8203 | Alert("parsing [%s:%d]: 'http-request %s': %s.\n", |
| 8204 | file, linenum, args[0], error); |
| 8205 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8206 | goto out_err; |
| 8207 | } |
Willy Tarreau | 59ad1a2 | 2014-01-29 14:39:58 +0100 | [diff] [blame] | 8208 | free(proxy->conf.lfs_file); |
| 8209 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 8210 | proxy->conf.lfs_line = proxy->conf.args.line; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 8211 | cur_arg += 2; |
Willy Tarreau | b854392 | 2014-06-17 18:58:26 +0200 | [diff] [blame] | 8212 | } 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] | 8213 | 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] | 8214 | cur_arg = 1; |
| 8215 | |
| 8216 | if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] || |
Baptiste Assmann | 92df370 | 2014-06-24 11:10:00 +0200 | [diff] [blame] | 8217 | (*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] | 8218 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n", |
| 8219 | file, linenum, args[0]); |
| 8220 | goto out_err; |
| 8221 | } |
| 8222 | |
| 8223 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 8224 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 8225 | LIST_INIT(&rule->arg.hdr_add.fmt); |
| 8226 | |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 8227 | error = NULL; |
| 8228 | if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) { |
| 8229 | Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum, |
| 8230 | args[cur_arg + 1], error); |
| 8231 | free(error); |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 8232 | goto out_err; |
| 8233 | } |
| 8234 | |
| 8235 | proxy->conf.args.ctx = ARGC_HRQ; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8236 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8237 | 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] | 8238 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) { |
| 8239 | Alert("parsing [%s:%d]: 'http-request %s': %s.\n", |
| 8240 | file, linenum, args[0], error); |
| 8241 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8242 | goto out_err; |
| 8243 | } |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 8244 | |
| 8245 | free(proxy->conf.lfs_file); |
| 8246 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 8247 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 8248 | cur_arg += 3; |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 8249 | } else if (strcmp(args[0], "del-header") == 0) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8250 | rule->action = ACT_HTTP_DEL_HDR; |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 8251 | cur_arg = 1; |
| 8252 | |
| 8253 | if (!*args[cur_arg] || |
| 8254 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 8255 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 8256 | file, linenum, args[0]); |
| 8257 | goto out_err; |
| 8258 | } |
| 8259 | |
| 8260 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 8261 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 8262 | |
| 8263 | proxy->conf.args.ctx = ARGC_HRQ; |
| 8264 | free(proxy->conf.lfs_file); |
| 8265 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 8266 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 8267 | cur_arg += 1; |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 8268 | } else if (strncmp(args[0], "track-sc", 8) == 0 && |
| 8269 | args[0][9] == '\0' && args[0][8] >= '0' && |
Willy Tarreau | e1cfc1f | 2014-10-17 11:53:05 +0200 | [diff] [blame] | 8270 | args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */ |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 8271 | struct sample_expr *expr; |
| 8272 | unsigned int where; |
| 8273 | char *err = NULL; |
| 8274 | |
| 8275 | cur_arg = 1; |
| 8276 | proxy->conf.args.ctx = ARGC_TRK; |
| 8277 | |
| 8278 | expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args); |
| 8279 | if (!expr) { |
| 8280 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n", |
| 8281 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], err); |
| 8282 | free(err); |
| 8283 | goto out_err; |
| 8284 | } |
| 8285 | |
| 8286 | where = 0; |
| 8287 | if (proxy->cap & PR_CAP_FE) |
| 8288 | where |= SMP_VAL_FE_HRQ_HDR; |
| 8289 | if (proxy->cap & PR_CAP_BE) |
| 8290 | where |= SMP_VAL_BE_HRQ_HDR; |
| 8291 | |
| 8292 | if (!(expr->fetch->val & where)) { |
| 8293 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :" |
| 8294 | " fetch method '%s' extracts information from '%s', none of which is available here.\n", |
| 8295 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], |
| 8296 | args[cur_arg-1], sample_src_names(expr->fetch->use)); |
| 8297 | free(expr); |
| 8298 | goto out_err; |
| 8299 | } |
| 8300 | |
| 8301 | if (strcmp(args[cur_arg], "table") == 0) { |
| 8302 | cur_arg++; |
| 8303 | if (!args[cur_arg]) { |
| 8304 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing table name.\n", |
| 8305 | file, linenum, proxy_type_str(proxy), proxy->id, args[0]); |
| 8306 | free(expr); |
| 8307 | goto out_err; |
| 8308 | } |
| 8309 | /* we copy the table name for now, it will be resolved later */ |
Thierry FOURNIER | 5ec63e0 | 2015-08-04 09:09:48 +0200 | [diff] [blame] | 8310 | rule->arg.trk_ctr.table.n = strdup(args[cur_arg]); |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 8311 | cur_arg++; |
| 8312 | } |
Thierry FOURNIER | 5ec63e0 | 2015-08-04 09:09:48 +0200 | [diff] [blame] | 8313 | rule->arg.trk_ctr.expr = expr; |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8314 | rule->action = ACT_ACTION_TRK_SC0 + args[0][8] - '0'; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8315 | } else if (strcmp(args[0], "redirect") == 0) { |
| 8316 | struct redirect_rule *redir; |
Willy Tarreau | 6d4890c | 2013-11-18 18:04:25 +0100 | [diff] [blame] | 8317 | char *errmsg = NULL; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8318 | |
Willy Tarreau | be4653b | 2015-05-28 15:26:58 +0200 | [diff] [blame] | 8319 | 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] | 8320 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n", |
| 8321 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
| 8322 | goto out_err; |
| 8323 | } |
| 8324 | |
| 8325 | /* this redirect rule might already contain a parsed condition which |
| 8326 | * we'll pass to the http-request rule. |
| 8327 | */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8328 | rule->action = ACT_HTTP_REDIR; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8329 | rule->arg.redir = redir; |
| 8330 | rule->cond = redir->cond; |
| 8331 | redir->cond = NULL; |
| 8332 | cur_arg = 2; |
| 8333 | return rule; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8334 | } else if (strncmp(args[0], "add-acl", 7) == 0) { |
| 8335 | /* http-request add-acl(<reference (acl name)>) <key pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8336 | rule->action = ACT_HTTP_ADD_ACL; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8337 | /* |
| 8338 | * '+ 8' for 'add-acl(' |
| 8339 | * '- 9' for 'add-acl(' + trailing ')' |
| 8340 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 8341 | 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] | 8342 | |
| 8343 | cur_arg = 1; |
| 8344 | |
| 8345 | if (!*args[cur_arg] || |
| 8346 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 8347 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 8348 | file, linenum, args[0]); |
| 8349 | goto out_err; |
| 8350 | } |
| 8351 | |
| 8352 | LIST_INIT(&rule->arg.map.key); |
| 8353 | proxy->conf.args.ctx = ARGC_HRQ; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8354 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8355 | 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] | 8356 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) { |
| 8357 | Alert("parsing [%s:%d]: 'http-request %s': %s.\n", |
| 8358 | file, linenum, args[0], error); |
| 8359 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8360 | goto out_err; |
| 8361 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8362 | free(proxy->conf.lfs_file); |
| 8363 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 8364 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 8365 | cur_arg += 1; |
| 8366 | } else if (strncmp(args[0], "del-acl", 7) == 0) { |
| 8367 | /* http-request del-acl(<reference (acl name)>) <key pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8368 | rule->action = ACT_HTTP_DEL_ACL; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8369 | /* |
| 8370 | * '+ 8' for 'del-acl(' |
| 8371 | * '- 9' for 'del-acl(' + trailing ')' |
| 8372 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 8373 | 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] | 8374 | |
| 8375 | cur_arg = 1; |
| 8376 | |
| 8377 | if (!*args[cur_arg] || |
| 8378 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 8379 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 8380 | file, linenum, args[0]); |
| 8381 | goto out_err; |
| 8382 | } |
| 8383 | |
| 8384 | LIST_INIT(&rule->arg.map.key); |
| 8385 | proxy->conf.args.ctx = ARGC_HRQ; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8386 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8387 | 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] | 8388 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) { |
| 8389 | Alert("parsing [%s:%d]: 'http-request %s': %s.\n", |
| 8390 | file, linenum, args[0], error); |
| 8391 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8392 | goto out_err; |
| 8393 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8394 | free(proxy->conf.lfs_file); |
| 8395 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 8396 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 8397 | cur_arg += 1; |
| 8398 | } else if (strncmp(args[0], "del-map", 7) == 0) { |
| 8399 | /* http-request del-map(<reference (map name)>) <key pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8400 | rule->action = ACT_HTTP_DEL_MAP; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8401 | /* |
| 8402 | * '+ 8' for 'del-map(' |
| 8403 | * '- 9' for 'del-map(' + trailing ')' |
| 8404 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 8405 | 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] | 8406 | |
| 8407 | cur_arg = 1; |
| 8408 | |
| 8409 | if (!*args[cur_arg] || |
| 8410 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 8411 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 8412 | file, linenum, args[0]); |
| 8413 | goto out_err; |
| 8414 | } |
| 8415 | |
| 8416 | LIST_INIT(&rule->arg.map.key); |
| 8417 | proxy->conf.args.ctx = ARGC_HRQ; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8418 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8419 | 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] | 8420 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) { |
| 8421 | Alert("parsing [%s:%d]: 'http-request %s': %s.\n", |
| 8422 | file, linenum, args[0], error); |
| 8423 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8424 | goto out_err; |
| 8425 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8426 | free(proxy->conf.lfs_file); |
| 8427 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 8428 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 8429 | cur_arg += 1; |
| 8430 | } else if (strncmp(args[0], "set-map", 7) == 0) { |
| 8431 | /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8432 | rule->action = ACT_HTTP_SET_MAP; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8433 | /* |
| 8434 | * '+ 8' for 'set-map(' |
| 8435 | * '- 9' for 'set-map(' + trailing ')' |
| 8436 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 8437 | 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] | 8438 | |
| 8439 | cur_arg = 1; |
| 8440 | |
| 8441 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 8442 | (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) { |
| 8443 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n", |
| 8444 | file, linenum, args[0]); |
| 8445 | goto out_err; |
| 8446 | } |
| 8447 | |
| 8448 | LIST_INIT(&rule->arg.map.key); |
| 8449 | LIST_INIT(&rule->arg.map.value); |
| 8450 | proxy->conf.args.ctx = ARGC_HRQ; |
| 8451 | |
| 8452 | /* key pattern */ |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8453 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8454 | 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] | 8455 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) { |
| 8456 | Alert("parsing [%s:%d]: 'http-request %s' key: %s.\n", |
| 8457 | file, linenum, args[0], error); |
| 8458 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8459 | goto out_err; |
| 8460 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8461 | |
| 8462 | /* value pattern */ |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8463 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8464 | 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] | 8465 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) { |
| 8466 | Alert("parsing [%s:%d]: 'http-request %s' pattern: %s.\n", |
| 8467 | file, linenum, args[0], error); |
| 8468 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8469 | goto out_err; |
| 8470 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8471 | free(proxy->conf.lfs_file); |
| 8472 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 8473 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 8474 | |
| 8475 | cur_arg += 2; |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 8476 | } else if (((custom = action_http_req_custom(args[0])) != NULL)) { |
| 8477 | char *errmsg = NULL; |
| 8478 | cur_arg = 1; |
| 8479 | /* try in the module list */ |
Thierry FOURNIER | 5563e4b | 2015-08-14 19:20:07 +0200 | [diff] [blame] | 8480 | rule->from = ACT_F_HTTP_REQ; |
Thierry FOURNIER | 85c6c97 | 2015-09-22 19:14:35 +0200 | [diff] [blame] | 8481 | rule->kw = custom; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 8482 | 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] | 8483 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n", |
| 8484 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
| 8485 | free(errmsg); |
| 8486 | goto out_err; |
| 8487 | } |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8488 | } else { |
Thierry FOURNIER | ab95e65 | 2015-10-02 08:24:51 +0200 | [diff] [blame] | 8489 | action_build_list(&http_req_keywords.list, &trash); |
| 8490 | Alert("parsing [%s:%d]: 'http-request' expects 'allow', 'deny', 'auth', 'redirect', " |
| 8491 | "'tarpit', 'add-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', " |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 8492 | "'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] | 8493 | "%s%s, but got '%s'%s.\n", |
Thierry FOURNIER | ab95e65 | 2015-10-02 08:24:51 +0200 | [diff] [blame] | 8494 | file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)"); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8495 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8496 | } |
| 8497 | |
| 8498 | if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) { |
| 8499 | struct acl_cond *cond; |
Willy Tarreau | b7451bb | 2012-04-27 12:38:15 +0200 | [diff] [blame] | 8500 | char *errmsg = NULL; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8501 | |
Willy Tarreau | b7451bb | 2012-04-27 12:38:15 +0200 | [diff] [blame] | 8502 | if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) { |
| 8503 | Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n", |
| 8504 | file, linenum, args[0], errmsg); |
| 8505 | free(errmsg); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8506 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8507 | } |
| 8508 | rule->cond = cond; |
| 8509 | } |
| 8510 | else if (*args[cur_arg]) { |
| 8511 | Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or" |
| 8512 | " either 'if' or 'unless' followed by a condition but found '%s'.\n", |
| 8513 | file, linenum, args[0], args[cur_arg]); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8514 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8515 | } |
| 8516 | |
| 8517 | return rule; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8518 | out_err: |
| 8519 | free(rule); |
| 8520 | return NULL; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8521 | } |
| 8522 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8523 | /* parse an "http-respose" rule */ |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 8524 | 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] | 8525 | { |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 8526 | struct act_rule *rule; |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 8527 | struct action_kw *custom = NULL; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8528 | int cur_arg; |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 8529 | char *error; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8530 | |
| 8531 | rule = calloc(1, sizeof(*rule)); |
| 8532 | if (!rule) { |
| 8533 | Alert("parsing [%s:%d]: out of memory.\n", file, linenum); |
| 8534 | goto out_err; |
| 8535 | } |
| 8536 | |
| 8537 | if (!strcmp(args[0], "allow")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8538 | rule->action = ACT_ACTION_ALLOW; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8539 | cur_arg = 1; |
| 8540 | } else if (!strcmp(args[0], "deny")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8541 | rule->action = ACT_ACTION_DENY; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8542 | cur_arg = 1; |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 8543 | } else if (!strcmp(args[0], "set-nice")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8544 | rule->action = ACT_HTTP_SET_NICE; |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 8545 | cur_arg = 1; |
| 8546 | |
| 8547 | if (!*args[cur_arg] || |
| 8548 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 8549 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n", |
| 8550 | file, linenum, args[0]); |
| 8551 | goto out_err; |
| 8552 | } |
| 8553 | rule->arg.nice = atoi(args[cur_arg]); |
| 8554 | if (rule->arg.nice < -1024) |
| 8555 | rule->arg.nice = -1024; |
| 8556 | else if (rule->arg.nice > 1024) |
| 8557 | rule->arg.nice = 1024; |
| 8558 | cur_arg++; |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 8559 | } else if (!strcmp(args[0], "set-tos")) { |
| 8560 | #ifdef IP_TOS |
| 8561 | char *err; |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8562 | rule->action = ACT_HTTP_SET_TOS; |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 8563 | cur_arg = 1; |
| 8564 | |
| 8565 | if (!*args[cur_arg] || |
| 8566 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 8567 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n", |
| 8568 | file, linenum, args[0]); |
| 8569 | goto out_err; |
| 8570 | } |
| 8571 | |
| 8572 | rule->arg.tos = strtol(args[cur_arg], &err, 0); |
| 8573 | if (err && *err != '\0') { |
| 8574 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n", |
| 8575 | file, linenum, err, args[0]); |
| 8576 | goto out_err; |
| 8577 | } |
| 8578 | cur_arg++; |
| 8579 | #else |
| 8580 | Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]); |
| 8581 | goto out_err; |
| 8582 | #endif |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 8583 | } else if (!strcmp(args[0], "set-mark")) { |
| 8584 | #ifdef SO_MARK |
| 8585 | char *err; |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8586 | rule->action = ACT_HTTP_SET_MARK; |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 8587 | cur_arg = 1; |
| 8588 | |
| 8589 | if (!*args[cur_arg] || |
| 8590 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 8591 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n", |
| 8592 | file, linenum, args[0]); |
| 8593 | goto out_err; |
| 8594 | } |
| 8595 | |
| 8596 | rule->arg.mark = strtoul(args[cur_arg], &err, 0); |
| 8597 | if (err && *err != '\0') { |
| 8598 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n", |
| 8599 | file, linenum, err, args[0]); |
| 8600 | goto out_err; |
| 8601 | } |
| 8602 | cur_arg++; |
| 8603 | global.last_checks |= LSTCHK_NETADM; |
| 8604 | #else |
| 8605 | Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]); |
| 8606 | goto out_err; |
| 8607 | #endif |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 8608 | } else if (!strcmp(args[0], "set-log-level")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8609 | rule->action = ACT_HTTP_SET_LOGL; |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 8610 | cur_arg = 1; |
| 8611 | |
| 8612 | if (!*args[cur_arg] || |
| 8613 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 8614 | bad_log_level: |
| 8615 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n", |
| 8616 | file, linenum, args[0]); |
| 8617 | goto out_err; |
| 8618 | } |
| 8619 | if (strcmp(args[cur_arg], "silent") == 0) |
| 8620 | rule->arg.loglevel = -1; |
Ruoshan Huang | dd01678 | 2016-06-15 22:16:03 +0800 | [diff] [blame] | 8621 | 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] | 8622 | goto bad_log_level; |
| 8623 | cur_arg++; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8624 | } 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] | 8625 | 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] | 8626 | cur_arg = 1; |
| 8627 | |
| 8628 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 8629 | (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) { |
| 8630 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n", |
| 8631 | file, linenum, args[0]); |
| 8632 | goto out_err; |
| 8633 | } |
| 8634 | |
| 8635 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 8636 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 8637 | LIST_INIT(&rule->arg.hdr_add.fmt); |
| 8638 | |
Thierry FOURNIER | 1c0054f | 2013-11-20 15:09:52 +0100 | [diff] [blame] | 8639 | proxy->conf.args.ctx = ARGC_HRS; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8640 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8641 | 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] | 8642 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) { |
| 8643 | Alert("parsing [%s:%d]: 'http-response %s': %s.\n", |
| 8644 | file, linenum, args[0], error); |
| 8645 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8646 | goto out_err; |
| 8647 | } |
Willy Tarreau | 59ad1a2 | 2014-01-29 14:39:58 +0100 | [diff] [blame] | 8648 | free(proxy->conf.lfs_file); |
| 8649 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 8650 | proxy->conf.lfs_line = proxy->conf.args.line; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8651 | cur_arg += 2; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 8652 | } 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] | 8653 | 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] | 8654 | cur_arg = 1; |
| 8655 | |
| 8656 | if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] || |
Baptiste Assmann | 12cb00b | 2014-08-08 17:29:06 +0200 | [diff] [blame] | 8657 | (*args[cur_arg+3] && strcmp(args[cur_arg+3], "if") != 0 && strcmp(args[cur_arg+3], "unless") != 0)) { |
| 8658 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n", |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 8659 | file, linenum, args[0]); |
| 8660 | goto out_err; |
| 8661 | } |
| 8662 | |
| 8663 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 8664 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 8665 | LIST_INIT(&rule->arg.hdr_add.fmt); |
| 8666 | |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 8667 | error = NULL; |
| 8668 | if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) { |
| 8669 | Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum, |
| 8670 | args[cur_arg + 1], error); |
| 8671 | free(error); |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 8672 | goto out_err; |
| 8673 | } |
| 8674 | |
| 8675 | proxy->conf.args.ctx = ARGC_HRQ; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8676 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8677 | 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] | 8678 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) { |
| 8679 | Alert("parsing [%s:%d]: 'http-response %s': %s.\n", |
| 8680 | file, linenum, args[0], error); |
| 8681 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8682 | goto out_err; |
| 8683 | } |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 8684 | |
| 8685 | free(proxy->conf.lfs_file); |
| 8686 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 8687 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 8688 | cur_arg += 3; |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 8689 | } else if (strcmp(args[0], "del-header") == 0) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8690 | rule->action = ACT_HTTP_DEL_HDR; |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 8691 | cur_arg = 1; |
| 8692 | |
| 8693 | if (!*args[cur_arg] || |
| 8694 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 8695 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 8696 | file, linenum, args[0]); |
| 8697 | goto out_err; |
| 8698 | } |
| 8699 | |
| 8700 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 8701 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 8702 | |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8703 | proxy->conf.args.ctx = ARGC_HRS; |
| 8704 | free(proxy->conf.lfs_file); |
| 8705 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 8706 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 8707 | cur_arg += 1; |
| 8708 | } else if (strncmp(args[0], "add-acl", 7) == 0) { |
| 8709 | /* http-request add-acl(<reference (acl name)>) <key pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8710 | rule->action = ACT_HTTP_ADD_ACL; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8711 | /* |
| 8712 | * '+ 8' for 'add-acl(' |
| 8713 | * '- 9' for 'add-acl(' + trailing ')' |
| 8714 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 8715 | 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] | 8716 | |
| 8717 | cur_arg = 1; |
| 8718 | |
| 8719 | if (!*args[cur_arg] || |
| 8720 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 8721 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 8722 | file, linenum, args[0]); |
| 8723 | goto out_err; |
| 8724 | } |
| 8725 | |
| 8726 | LIST_INIT(&rule->arg.map.key); |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 8727 | proxy->conf.args.ctx = ARGC_HRS; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8728 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8729 | 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] | 8730 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) { |
| 8731 | Alert("parsing [%s:%d]: 'http-response %s': %s.\n", |
| 8732 | file, linenum, args[0], error); |
| 8733 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8734 | goto out_err; |
| 8735 | } |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 8736 | free(proxy->conf.lfs_file); |
| 8737 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 8738 | proxy->conf.lfs_line = proxy->conf.args.line; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8739 | |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 8740 | cur_arg += 1; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8741 | } else if (strncmp(args[0], "del-acl", 7) == 0) { |
| 8742 | /* http-response del-acl(<reference (acl name)>) <key pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8743 | rule->action = ACT_HTTP_DEL_ACL; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8744 | /* |
| 8745 | * '+ 8' for 'del-acl(' |
| 8746 | * '- 9' for 'del-acl(' + trailing ')' |
| 8747 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 8748 | 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] | 8749 | |
| 8750 | cur_arg = 1; |
| 8751 | |
| 8752 | if (!*args[cur_arg] || |
| 8753 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 8754 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 8755 | file, linenum, args[0]); |
| 8756 | goto out_err; |
| 8757 | } |
| 8758 | |
| 8759 | LIST_INIT(&rule->arg.map.key); |
| 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], proxy, &rule->arg.map.key, 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 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [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; |
| 8772 | cur_arg += 1; |
| 8773 | } else if (strncmp(args[0], "del-map", 7) == 0) { |
| 8774 | /* http-response del-map(<reference (map name)>) <key pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8775 | rule->action = ACT_HTTP_DEL_MAP; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8776 | /* |
| 8777 | * '+ 8' for 'del-map(' |
| 8778 | * '- 9' for 'del-map(' + trailing ')' |
| 8779 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 8780 | 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] | 8781 | |
| 8782 | cur_arg = 1; |
| 8783 | |
| 8784 | if (!*args[cur_arg] || |
| 8785 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 8786 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 8787 | file, linenum, args[0]); |
| 8788 | goto out_err; |
| 8789 | } |
| 8790 | |
| 8791 | LIST_INIT(&rule->arg.map.key); |
| 8792 | proxy->conf.args.ctx = ARGC_HRS; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8793 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8794 | 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] | 8795 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) { |
| 8796 | Alert("parsing [%s:%d]: 'http-response %s' %s.\n", |
| 8797 | file, linenum, args[0], error); |
| 8798 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8799 | goto out_err; |
| 8800 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8801 | free(proxy->conf.lfs_file); |
| 8802 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 8803 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 8804 | cur_arg += 1; |
| 8805 | } else if (strncmp(args[0], "set-map", 7) == 0) { |
| 8806 | /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8807 | rule->action = ACT_HTTP_SET_MAP; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8808 | /* |
| 8809 | * '+ 8' for 'set-map(' |
| 8810 | * '- 9' for 'set-map(' + trailing ')' |
| 8811 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 8812 | 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] | 8813 | |
| 8814 | cur_arg = 1; |
| 8815 | |
| 8816 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 8817 | (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) { |
| 8818 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n", |
| 8819 | file, linenum, args[0]); |
| 8820 | goto out_err; |
| 8821 | } |
| 8822 | |
| 8823 | LIST_INIT(&rule->arg.map.key); |
| 8824 | LIST_INIT(&rule->arg.map.value); |
| 8825 | |
| 8826 | proxy->conf.args.ctx = ARGC_HRS; |
| 8827 | |
| 8828 | /* key pattern */ |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8829 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8830 | 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] | 8831 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) { |
| 8832 | Alert("parsing [%s:%d]: 'http-response %s' name: %s.\n", |
| 8833 | file, linenum, args[0], error); |
| 8834 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8835 | goto out_err; |
| 8836 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8837 | |
| 8838 | /* value pattern */ |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 8839 | error = NULL; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8840 | 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] | 8841 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) { |
| 8842 | Alert("parsing [%s:%d]: 'http-response %s' value: %s.\n", |
| 8843 | file, linenum, args[0], error); |
| 8844 | free(error); |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 8845 | goto out_err; |
| 8846 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 8847 | |
| 8848 | free(proxy->conf.lfs_file); |
| 8849 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 8850 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 8851 | |
| 8852 | cur_arg += 2; |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 8853 | } else if (strcmp(args[0], "redirect") == 0) { |
| 8854 | struct redirect_rule *redir; |
| 8855 | char *errmsg = NULL; |
| 8856 | |
| 8857 | if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 1)) == NULL) { |
| 8858 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n", |
| 8859 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
| 8860 | goto out_err; |
| 8861 | } |
| 8862 | |
| 8863 | /* this redirect rule might already contain a parsed condition which |
| 8864 | * we'll pass to the http-request rule. |
| 8865 | */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 8866 | rule->action = ACT_HTTP_REDIR; |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 8867 | rule->arg.redir = redir; |
| 8868 | rule->cond = redir->cond; |
| 8869 | redir->cond = NULL; |
| 8870 | cur_arg = 2; |
| 8871 | return rule; |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 8872 | } else if (strncmp(args[0], "track-sc", 8) == 0 && |
| 8873 | args[0][9] == '\0' && args[0][8] >= '0' && |
| 8874 | args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */ |
| 8875 | struct sample_expr *expr; |
| 8876 | unsigned int where; |
| 8877 | char *err = NULL; |
| 8878 | |
| 8879 | cur_arg = 1; |
| 8880 | proxy->conf.args.ctx = ARGC_TRK; |
| 8881 | |
| 8882 | expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args); |
| 8883 | if (!expr) { |
| 8884 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n", |
| 8885 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], err); |
| 8886 | free(err); |
| 8887 | goto out_err; |
| 8888 | } |
| 8889 | |
| 8890 | where = 0; |
| 8891 | if (proxy->cap & PR_CAP_FE) |
| 8892 | where |= SMP_VAL_FE_HRS_HDR; |
| 8893 | if (proxy->cap & PR_CAP_BE) |
| 8894 | where |= SMP_VAL_BE_HRS_HDR; |
| 8895 | |
| 8896 | if (!(expr->fetch->val & where)) { |
| 8897 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule :" |
| 8898 | " fetch method '%s' extracts information from '%s', none of which is available here.\n", |
| 8899 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], |
| 8900 | args[cur_arg-1], sample_src_names(expr->fetch->use)); |
| 8901 | free(expr); |
| 8902 | goto out_err; |
| 8903 | } |
| 8904 | |
| 8905 | if (strcmp(args[cur_arg], "table") == 0) { |
| 8906 | cur_arg++; |
| 8907 | if (!args[cur_arg]) { |
| 8908 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : missing table name.\n", |
| 8909 | file, linenum, proxy_type_str(proxy), proxy->id, args[0]); |
| 8910 | free(expr); |
| 8911 | goto out_err; |
| 8912 | } |
| 8913 | /* we copy the table name for now, it will be resolved later */ |
| 8914 | rule->arg.trk_ctr.table.n = strdup(args[cur_arg]); |
| 8915 | cur_arg++; |
| 8916 | } |
| 8917 | rule->arg.trk_ctr.expr = expr; |
| 8918 | rule->action = ACT_ACTION_TRK_SC0 + args[0][8] - '0'; |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 8919 | } else if (((custom = action_http_res_custom(args[0])) != NULL)) { |
| 8920 | char *errmsg = NULL; |
| 8921 | cur_arg = 1; |
| 8922 | /* try in the module list */ |
Thierry FOURNIER | 5563e4b | 2015-08-14 19:20:07 +0200 | [diff] [blame] | 8923 | rule->from = ACT_F_HTTP_RES; |
Thierry FOURNIER | 85c6c97 | 2015-09-22 19:14:35 +0200 | [diff] [blame] | 8924 | rule->kw = custom; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 8925 | 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] | 8926 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n", |
| 8927 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
| 8928 | free(errmsg); |
| 8929 | goto out_err; |
| 8930 | } |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8931 | } else { |
Thierry FOURNIER | ab95e65 | 2015-10-02 08:24:51 +0200 | [diff] [blame] | 8932 | action_build_list(&http_res_keywords.list, &trash); |
| 8933 | Alert("parsing [%s:%d]: 'http-response' expects 'allow', 'deny', 'redirect', " |
| 8934 | "'add-header', 'del-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', " |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 8935 | "'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] | 8936 | "%s%s, but got '%s'%s.\n", |
Thierry FOURNIER | ab95e65 | 2015-10-02 08:24:51 +0200 | [diff] [blame] | 8937 | file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)"); |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 8938 | goto out_err; |
| 8939 | } |
| 8940 | |
| 8941 | if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) { |
| 8942 | struct acl_cond *cond; |
| 8943 | char *errmsg = NULL; |
| 8944 | |
| 8945 | if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) { |
| 8946 | Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n", |
| 8947 | file, linenum, args[0], errmsg); |
| 8948 | free(errmsg); |
| 8949 | goto out_err; |
| 8950 | } |
| 8951 | rule->cond = cond; |
| 8952 | } |
| 8953 | else if (*args[cur_arg]) { |
| 8954 | Alert("parsing [%s:%d]: 'http-response %s' expects" |
| 8955 | " either 'if' or 'unless' followed by a condition but found '%s'.\n", |
| 8956 | file, linenum, args[0], args[cur_arg]); |
| 8957 | goto out_err; |
| 8958 | } |
| 8959 | |
| 8960 | return rule; |
| 8961 | out_err: |
| 8962 | free(rule); |
| 8963 | return NULL; |
| 8964 | } |
| 8965 | |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 8966 | /* 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] | 8967 | * 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] | 8968 | * dynamic log-format rule instead of a static string. Parameter <dir> indicates |
| 8969 | * 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] | 8970 | */ |
| 8971 | 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] | 8972 | const char **args, char **errmsg, int use_fmt, int dir) |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 8973 | { |
| 8974 | struct redirect_rule *rule; |
| 8975 | int cur_arg; |
| 8976 | int type = REDIRECT_TYPE_NONE; |
| 8977 | int code = 302; |
| 8978 | const char *destination = NULL; |
| 8979 | const char *cookie = NULL; |
| 8980 | int cookie_set = 0; |
| 8981 | unsigned int flags = REDIRECT_FLAG_NONE; |
| 8982 | struct acl_cond *cond = NULL; |
| 8983 | |
| 8984 | cur_arg = 0; |
| 8985 | while (*(args[cur_arg])) { |
| 8986 | if (strcmp(args[cur_arg], "location") == 0) { |
| 8987 | if (!*args[cur_arg + 1]) |
| 8988 | goto missing_arg; |
| 8989 | |
| 8990 | type = REDIRECT_TYPE_LOCATION; |
| 8991 | cur_arg++; |
| 8992 | destination = args[cur_arg]; |
| 8993 | } |
| 8994 | else if (strcmp(args[cur_arg], "prefix") == 0) { |
| 8995 | if (!*args[cur_arg + 1]) |
| 8996 | goto missing_arg; |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 8997 | type = REDIRECT_TYPE_PREFIX; |
| 8998 | cur_arg++; |
| 8999 | destination = args[cur_arg]; |
| 9000 | } |
| 9001 | else if (strcmp(args[cur_arg], "scheme") == 0) { |
| 9002 | if (!*args[cur_arg + 1]) |
| 9003 | goto missing_arg; |
| 9004 | |
| 9005 | type = REDIRECT_TYPE_SCHEME; |
| 9006 | cur_arg++; |
| 9007 | destination = args[cur_arg]; |
| 9008 | } |
| 9009 | else if (strcmp(args[cur_arg], "set-cookie") == 0) { |
| 9010 | if (!*args[cur_arg + 1]) |
| 9011 | goto missing_arg; |
| 9012 | |
| 9013 | cur_arg++; |
| 9014 | cookie = args[cur_arg]; |
| 9015 | cookie_set = 1; |
| 9016 | } |
| 9017 | else if (strcmp(args[cur_arg], "clear-cookie") == 0) { |
| 9018 | if (!*args[cur_arg + 1]) |
| 9019 | goto missing_arg; |
| 9020 | |
| 9021 | cur_arg++; |
| 9022 | cookie = args[cur_arg]; |
| 9023 | cookie_set = 0; |
| 9024 | } |
| 9025 | else if (strcmp(args[cur_arg], "code") == 0) { |
| 9026 | if (!*args[cur_arg + 1]) |
| 9027 | goto missing_arg; |
| 9028 | |
| 9029 | cur_arg++; |
| 9030 | code = atol(args[cur_arg]); |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 9031 | if (code < 301 || code > 308 || (code > 303 && code < 307)) { |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9032 | memprintf(errmsg, |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 9033 | "'%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] | 9034 | args[cur_arg - 1], args[cur_arg]); |
| 9035 | return NULL; |
| 9036 | } |
| 9037 | } |
| 9038 | else if (!strcmp(args[cur_arg],"drop-query")) { |
| 9039 | flags |= REDIRECT_FLAG_DROP_QS; |
| 9040 | } |
| 9041 | else if (!strcmp(args[cur_arg],"append-slash")) { |
| 9042 | flags |= REDIRECT_FLAG_APPEND_SLASH; |
| 9043 | } |
| 9044 | else if (strcmp(args[cur_arg], "if") == 0 || |
| 9045 | strcmp(args[cur_arg], "unless") == 0) { |
| 9046 | cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg); |
| 9047 | if (!cond) { |
| 9048 | memprintf(errmsg, "error in condition: %s", *errmsg); |
| 9049 | return NULL; |
| 9050 | } |
| 9051 | break; |
| 9052 | } |
| 9053 | else { |
| 9054 | memprintf(errmsg, |
| 9055 | "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')", |
| 9056 | args[cur_arg]); |
| 9057 | return NULL; |
| 9058 | } |
| 9059 | cur_arg++; |
| 9060 | } |
| 9061 | |
| 9062 | if (type == REDIRECT_TYPE_NONE) { |
| 9063 | memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')"); |
| 9064 | return NULL; |
| 9065 | } |
| 9066 | |
Willy Tarreau | be4653b | 2015-05-28 15:26:58 +0200 | [diff] [blame] | 9067 | if (dir && type != REDIRECT_TYPE_LOCATION) { |
| 9068 | memprintf(errmsg, "response only supports redirect type 'location'"); |
| 9069 | return NULL; |
| 9070 | } |
| 9071 | |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 9072 | rule = calloc(1, sizeof(*rule)); |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9073 | rule->cond = cond; |
Willy Tarreau | 60e0838 | 2013-12-03 00:48:45 +0100 | [diff] [blame] | 9074 | LIST_INIT(&rule->rdr_fmt); |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 9075 | |
| 9076 | if (!use_fmt) { |
| 9077 | /* old-style static redirect rule */ |
| 9078 | rule->rdr_str = strdup(destination); |
| 9079 | rule->rdr_len = strlen(destination); |
| 9080 | } |
| 9081 | else { |
| 9082 | /* log-format based redirect rule */ |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 9083 | |
| 9084 | /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case, |
| 9085 | * if prefix == "/", we don't want to add anything, otherwise it |
| 9086 | * makes it hard for the user to configure a self-redirection. |
| 9087 | */ |
Godbach | d972203 | 2014-12-18 15:44:58 +0800 | [diff] [blame] | 9088 | curproxy->conf.args.ctx = ARGC_RDR; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 9089 | if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) { |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9090 | if (!parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP, |
| 9091 | 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] | 9092 | : (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9093 | errmsg)) { |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 9094 | return NULL; |
| 9095 | } |
Willy Tarreau | 59ad1a2 | 2014-01-29 14:39:58 +0100 | [diff] [blame] | 9096 | free(curproxy->conf.lfs_file); |
| 9097 | curproxy->conf.lfs_file = strdup(curproxy->conf.args.file); |
| 9098 | curproxy->conf.lfs_line = curproxy->conf.args.line; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 9099 | } |
| 9100 | } |
| 9101 | |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9102 | if (cookie) { |
| 9103 | /* depending on cookie_set, either we want to set the cookie, or to clear it. |
| 9104 | * a clear consists in appending "; path=/; Max-Age=0;" at the end. |
| 9105 | */ |
| 9106 | rule->cookie_len = strlen(cookie); |
| 9107 | if (cookie_set) { |
| 9108 | rule->cookie_str = malloc(rule->cookie_len + 10); |
| 9109 | memcpy(rule->cookie_str, cookie, rule->cookie_len); |
| 9110 | memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10); |
| 9111 | rule->cookie_len += 9; |
| 9112 | } else { |
| 9113 | rule->cookie_str = malloc(rule->cookie_len + 21); |
| 9114 | memcpy(rule->cookie_str, cookie, rule->cookie_len); |
| 9115 | memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21); |
| 9116 | rule->cookie_len += 20; |
| 9117 | } |
| 9118 | } |
| 9119 | rule->type = type; |
| 9120 | rule->code = code; |
| 9121 | rule->flags = flags; |
| 9122 | LIST_INIT(&rule->list); |
| 9123 | return rule; |
| 9124 | |
| 9125 | missing_arg: |
| 9126 | memprintf(errmsg, "missing argument for '%s'", args[cur_arg]); |
| 9127 | return NULL; |
| 9128 | } |
| 9129 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9130 | /************************************************************************/ |
| 9131 | /* The code below is dedicated to ACL parsing and matching */ |
| 9132 | /************************************************************************/ |
| 9133 | |
| 9134 | |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9135 | /* This function ensures that the prerequisites for an L7 fetch are ready, |
| 9136 | * which means that a request or response is ready. If some data is missing, |
| 9137 | * 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] | 9138 | * to extract data from L7. If <req_vol> is non-null during a request prefetch, |
| 9139 | * another test is made to ensure the required information is not gone. |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9140 | * |
| 9141 | * The function returns : |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9142 | * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to |
| 9143 | * decide whether or not an HTTP message is present ; |
| 9144 | * 0 if the requested data cannot be fetched or if it is certain that |
| 9145 | * we'll never have any HTTP message there ; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9146 | * 1 if an HTTP message is ready |
| 9147 | */ |
James Rosewell | 91a41cb | 2015-09-18 17:11:16 +0100 | [diff] [blame] | 9148 | 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] | 9149 | const struct arg *args, struct sample *smp, int req_vol) |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9150 | { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 9151 | struct http_txn *txn; |
| 9152 | struct http_msg *msg; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9153 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 9154 | /* Note: it is possible that <s> is NULL when called before stream |
| 9155 | * initialization (eg: tcp-request connection), so this function is the |
| 9156 | * one responsible for guarding against this case for all HTTP users. |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9157 | */ |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 9158 | if (!s) |
| 9159 | return 0; |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 9160 | |
Thierry FOURNIER | ed08d6a | 2015-09-24 08:40:18 +0200 | [diff] [blame] | 9161 | if (!s->txn) { |
| 9162 | if (unlikely(!http_alloc_txn(s))) |
| 9163 | return 0; /* not enough memory */ |
| 9164 | http_init_txn(s); |
| 9165 | } |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 9166 | txn = s->txn; |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 9167 | msg = &txn->req; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9168 | |
| 9169 | /* Check for a dependency on a request */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9170 | smp->data.type = SMP_T_BOOL; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9171 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 9172 | if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 9173 | /* If the buffer does not leave enough free space at the end, |
| 9174 | * we must first realign it. |
| 9175 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 9176 | if (s->req.buf->p > s->req.buf->data && |
| 9177 | s->req.buf->i + s->req.buf->p > s->req.buf->data + s->req.buf->size - global.tune.maxrewrite) |
| 9178 | buffer_slow_realign(s->req.buf); |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 9179 | |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9180 | if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 472b1ee | 2013-10-14 22:41:30 +0200 | [diff] [blame] | 9181 | if (msg->msg_state == HTTP_MSG_ERROR) |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9182 | return 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9183 | |
| 9184 | /* Try to decode HTTP request */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 9185 | if (likely(msg->next < s->req.buf->i)) |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9186 | http_msg_analyzer(msg, &txn->hdr_idx); |
| 9187 | |
| 9188 | /* Still no valid request ? */ |
| 9189 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 3bf1b2b | 2012-08-27 20:46:07 +0200 | [diff] [blame] | 9190 | if ((msg->msg_state == HTTP_MSG_ERROR) || |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 9191 | buffer_full(s->req.buf, global.tune.maxrewrite)) { |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9192 | return 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9193 | } |
| 9194 | /* wait for final state */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9195 | smp->flags |= SMP_F_MAY_CHANGE; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9196 | return 0; |
| 9197 | } |
| 9198 | |
| 9199 | /* OK we just got a valid HTTP request. We have some minor |
| 9200 | * preparation to perform so that further checks can rely |
| 9201 | * on HTTP tests. |
| 9202 | */ |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 9203 | |
| 9204 | /* If the request was parsed but was too large, we must absolutely |
| 9205 | * return an error so that it is not processed. At the moment this |
| 9206 | * cannot happen, but if the parsers are to change in the future, |
| 9207 | * we want this check to be maintained. |
| 9208 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 9209 | if (unlikely(s->req.buf->i + s->req.buf->p > |
| 9210 | s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)) { |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 9211 | msg->err_state = msg->msg_state; |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 9212 | msg->msg_state = HTTP_MSG_ERROR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9213 | smp->data.u.sint = 1; |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 9214 | return 1; |
| 9215 | } |
| 9216 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9217 | 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] | 9218 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 9219 | s->flags |= SF_REDIRECTABLE; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9220 | |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9221 | if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn)) |
| 9222 | return 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9223 | } |
| 9224 | |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9225 | if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) { |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9226 | return 0; /* data might have moved and indexes changed */ |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9227 | } |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9228 | |
| 9229 | /* otherwise everything's ready for the request */ |
| 9230 | } |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 9231 | else { |
| 9232 | /* Check for a dependency on a response */ |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9233 | if (txn->rsp.msg_state < HTTP_MSG_BODY) { |
| 9234 | smp->flags |= SMP_F_MAY_CHANGE; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9235 | return 0; |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 9236 | } |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9237 | } |
| 9238 | |
| 9239 | /* everything's OK */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9240 | smp->data.u.sint = 1; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 9241 | return 1; |
| 9242 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9243 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9244 | /* 1. Check on METHOD |
| 9245 | * We use the pre-parsed method if it is known, and store its number as an |
| 9246 | * integer. If it is unknown, we use the pointer and the length. |
| 9247 | */ |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 9248 | 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] | 9249 | { |
| 9250 | int len, meth; |
| 9251 | |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 9252 | len = strlen(text); |
| 9253 | meth = find_http_meth(text, len); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9254 | |
| 9255 | pattern->val.i = meth; |
| 9256 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | 912c119 | 2014-08-29 15:15:50 +0200 | [diff] [blame] | 9257 | pattern->ptr.str = (char *)text; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9258 | pattern->len = len; |
| 9259 | } |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 9260 | else { |
| 9261 | pattern->ptr.str = NULL; |
| 9262 | pattern->len = 0; |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 9263 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9264 | return 1; |
| 9265 | } |
| 9266 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 9267 | /* This function fetches the method of current HTTP request and stores |
| 9268 | * it in the global pattern struct as a chunk. There are two possibilities : |
| 9269 | * - if the method is known (not HTTP_METH_OTHER), its identifier is stored |
| 9270 | * in <len> and <ptr> is NULL ; |
| 9271 | * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and |
| 9272 | * <len> to its length. |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 9273 | * This is intended to be used with pat_match_meth() only. |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 9274 | */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 9275 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9276 | 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] | 9277 | { |
| 9278 | int meth; |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 9279 | struct http_txn *txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9280 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 9281 | CHECK_HTTP_MESSAGE_FIRST_PERM(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9282 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 9283 | txn = smp->strm->txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9284 | meth = txn->meth; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9285 | smp->data.type = SMP_T_METH; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9286 | smp->data.u.meth.meth = meth; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9287 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9288 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 9289 | /* ensure the indexes are not affected */ |
| 9290 | return 0; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9291 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9292 | smp->data.u.meth.str.len = txn->req.sl.rq.m_l; |
| 9293 | smp->data.u.meth.str.str = txn->req.chn->buf->p; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9294 | } |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9295 | smp->flags |= SMP_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9296 | return 1; |
| 9297 | } |
| 9298 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 9299 | /* See above how the method is stored in the global pattern */ |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 9300 | 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] | 9301 | { |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 9302 | int icase; |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 9303 | struct pattern_list *lst; |
| 9304 | struct pattern *pattern; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 9305 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 9306 | list_for_each_entry(lst, &expr->patterns, list) { |
| 9307 | pattern = &lst->pat; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9308 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 9309 | /* well-known method */ |
| 9310 | if (pattern->val.i != HTTP_METH_OTHER) { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9311 | if (smp->data.u.meth.meth == pattern->val.i) |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 9312 | return pattern; |
| 9313 | else |
| 9314 | continue; |
| 9315 | } |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 9316 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 9317 | /* Other method, we must compare the strings */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9318 | if (pattern->len != smp->data.u.meth.str.len) |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 9319 | continue; |
| 9320 | |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 9321 | icase = expr->mflags & PAT_MF_IGNORE_CASE; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9322 | if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.meth.str.str, smp->data.u.meth.str.len) == 0) || |
| 9323 | (!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] | 9324 | return pattern; |
| 9325 | } |
| 9326 | return NULL; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9327 | } |
| 9328 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 9329 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9330 | 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] | 9331 | { |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 9332 | struct http_txn *txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9333 | char *ptr; |
| 9334 | int len; |
| 9335 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9336 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9337 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 9338 | txn = smp->strm->txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9339 | len = txn->req.sl.rq.v_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9340 | ptr = txn->req.chn->buf->p + txn->req.sl.rq.v; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9341 | |
| 9342 | while ((len-- > 0) && (*ptr++ != '/')); |
| 9343 | if (len <= 0) |
| 9344 | return 0; |
| 9345 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9346 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9347 | smp->data.u.str.str = ptr; |
| 9348 | smp->data.u.str.len = len; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9349 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9350 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9351 | return 1; |
| 9352 | } |
| 9353 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 9354 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9355 | 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] | 9356 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 9357 | struct http_txn *txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9358 | char *ptr; |
| 9359 | int len; |
| 9360 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9361 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9362 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 9363 | txn = smp->strm->txn; |
Willy Tarreau | f26b252 | 2012-12-14 08:33:14 +0100 | [diff] [blame] | 9364 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
| 9365 | return 0; |
| 9366 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9367 | len = txn->rsp.sl.st.v_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9368 | ptr = txn->rsp.chn->buf->p; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9369 | |
| 9370 | while ((len-- > 0) && (*ptr++ != '/')); |
| 9371 | if (len <= 0) |
| 9372 | return 0; |
| 9373 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9374 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9375 | smp->data.u.str.str = ptr; |
| 9376 | smp->data.u.str.len = len; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9377 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9378 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9379 | return 1; |
| 9380 | } |
| 9381 | |
| 9382 | /* 3. Check on Status Code. We manipulate integers here. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 9383 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9384 | 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] | 9385 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 9386 | struct http_txn *txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9387 | char *ptr; |
| 9388 | int len; |
| 9389 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9390 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9391 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 9392 | txn = smp->strm->txn; |
Willy Tarreau | f26b252 | 2012-12-14 08:33:14 +0100 | [diff] [blame] | 9393 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
| 9394 | return 0; |
| 9395 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9396 | len = txn->rsp.sl.st.c_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9397 | ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9398 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9399 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9400 | smp->data.u.sint = __strl2ui(ptr, len); |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9401 | smp->flags = SMP_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9402 | return 1; |
| 9403 | } |
| 9404 | |
Thierry Fournier | f4011dd | 2016-03-29 17:23:51 +0200 | [diff] [blame] | 9405 | static int |
| 9406 | smp_fetch_uniqueid(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 9407 | { |
| 9408 | if (LIST_ISEMPTY(&smp->sess->fe->format_unique_id)) |
| 9409 | return 0; |
| 9410 | |
| 9411 | if (!smp->strm->unique_id) { |
| 9412 | if ((smp->strm->unique_id = pool_alloc2(pool2_uniqueid)) == NULL) |
| 9413 | return 0; |
| 9414 | smp->strm->unique_id[0] = '\0'; |
| 9415 | } |
| 9416 | smp->data.u.str.len = build_logline(smp->strm, smp->strm->unique_id, |
| 9417 | UNIQUEID_LEN, &smp->sess->fe->format_unique_id); |
| 9418 | |
| 9419 | smp->data.type = SMP_T_STR; |
| 9420 | smp->data.u.str.str = smp->strm->unique_id; |
| 9421 | smp->flags = SMP_F_CONST; |
| 9422 | return 1; |
| 9423 | } |
| 9424 | |
Thierry FOURNIER | d7d8881 | 2017-04-19 15:15:14 +0200 | [diff] [blame] | 9425 | /* Returns a string block containing all headers including the |
| 9426 | * empty line wich separes headers from the body. This is useful |
| 9427 | * form some headers analysis. |
| 9428 | */ |
| 9429 | static int |
| 9430 | smp_fetch_hdrs(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 9431 | { |
| 9432 | struct http_msg *msg; |
| 9433 | struct hdr_idx *idx; |
| 9434 | struct http_txn *txn; |
| 9435 | |
| 9436 | CHECK_HTTP_MESSAGE_FIRST(); |
| 9437 | |
| 9438 | txn = smp->strm->txn; |
| 9439 | idx = &txn->hdr_idx; |
| 9440 | msg = &txn->req; |
| 9441 | |
| 9442 | smp->data.type = SMP_T_STR; |
| 9443 | smp->data.u.str.str = msg->chn->buf->p + hdr_idx_first_pos(idx); |
| 9444 | smp->data.u.str.len = msg->eoh - hdr_idx_first_pos(idx) + 1 + |
| 9445 | (msg->chn->buf->p[msg->eoh] == '\r'); |
| 9446 | |
| 9447 | return 1; |
| 9448 | } |
| 9449 | |
Thierry FOURNIER | 5617dce | 2017-04-09 05:38:19 +0200 | [diff] [blame] | 9450 | /* Returns the header request in a length/value encoded format. |
| 9451 | * This is useful for exchanges with the SPOE. |
| 9452 | * |
| 9453 | * A "length value" is a multibyte code encoding numbers. It uses the |
| 9454 | * SPOE format. The encoding is the following: |
| 9455 | * |
| 9456 | * Each couple "header name" / "header value" is composed |
| 9457 | * like this: |
| 9458 | * "length value" "header name bytes" |
| 9459 | * "length value" "header value bytes" |
| 9460 | * When the last header is reached, the header name and the header |
| 9461 | * value are empty. Their length are 0 |
| 9462 | */ |
| 9463 | static int |
| 9464 | smp_fetch_hdrs_bin(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 9465 | { |
| 9466 | struct http_msg *msg; |
| 9467 | struct chunk *temp; |
| 9468 | struct hdr_idx *idx; |
| 9469 | const char *cur_ptr, *cur_next, *p; |
| 9470 | int old_idx, cur_idx; |
| 9471 | struct hdr_idx_elem *cur_hdr; |
| 9472 | const char *hn, *hv; |
| 9473 | int hnl, hvl; |
| 9474 | int ret; |
| 9475 | struct http_txn *txn; |
| 9476 | char *buf; |
| 9477 | char *end; |
| 9478 | |
| 9479 | CHECK_HTTP_MESSAGE_FIRST(); |
| 9480 | |
| 9481 | temp = get_trash_chunk(); |
| 9482 | buf = temp->str; |
| 9483 | end = temp->str + temp->size; |
| 9484 | |
| 9485 | txn = smp->strm->txn; |
| 9486 | idx = &txn->hdr_idx; |
| 9487 | msg = &txn->req; |
| 9488 | |
| 9489 | /* Build array of headers. */ |
| 9490 | old_idx = 0; |
| 9491 | cur_next = msg->chn->buf->p + hdr_idx_first_pos(idx); |
| 9492 | while (1) { |
| 9493 | cur_idx = idx->v[old_idx].next; |
| 9494 | if (!cur_idx) |
| 9495 | break; |
| 9496 | old_idx = cur_idx; |
| 9497 | |
| 9498 | cur_hdr = &idx->v[cur_idx]; |
| 9499 | cur_ptr = cur_next; |
| 9500 | cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1; |
| 9501 | |
| 9502 | /* Now we have one full header at cur_ptr of len cur_hdr->len, |
| 9503 | * and the next header starts at cur_next. We'll check |
| 9504 | * this header in the list as well as against the default |
| 9505 | * rule. |
| 9506 | */ |
| 9507 | |
| 9508 | /* look for ': *'. */ |
| 9509 | hn = cur_ptr; |
| 9510 | for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++); |
| 9511 | if (p >= cur_ptr+cur_hdr->len) |
| 9512 | continue; |
| 9513 | hnl = p - hn; |
| 9514 | p++; |
| 9515 | while (p < cur_ptr + cur_hdr->len && (*p == ' ' || *p == '\t')) |
| 9516 | p++; |
| 9517 | if (p >= cur_ptr + cur_hdr->len) |
| 9518 | continue; |
| 9519 | hv = p; |
| 9520 | hvl = cur_ptr + cur_hdr->len-p; |
| 9521 | |
| 9522 | /* encode the header name. */ |
| 9523 | ret = encode_varint(hnl, &buf, end); |
| 9524 | if (ret == -1) |
| 9525 | return 0; |
| 9526 | if (buf + hnl > end) |
| 9527 | return 0; |
| 9528 | memcpy(buf, hn, hnl); |
| 9529 | buf += hnl; |
| 9530 | |
| 9531 | /* encode and copy the value. */ |
| 9532 | ret = encode_varint(hvl, &buf, end); |
| 9533 | if (ret == -1) |
| 9534 | return 0; |
| 9535 | if (buf + hvl > end) |
| 9536 | return 0; |
| 9537 | memcpy(buf, hv, hvl); |
| 9538 | buf += hvl; |
| 9539 | } |
| 9540 | |
| 9541 | /* encode the end of the header list with empty |
| 9542 | * header name and header value. |
| 9543 | */ |
| 9544 | ret = encode_varint(0, &buf, end); |
| 9545 | if (ret == -1) |
| 9546 | return 0; |
| 9547 | ret = encode_varint(0, &buf, end); |
| 9548 | if (ret == -1) |
| 9549 | return 0; |
| 9550 | |
| 9551 | /* Initialise sample data which will be filled. */ |
| 9552 | smp->data.type = SMP_T_BIN; |
| 9553 | smp->data.u.str.str = temp->str; |
| 9554 | smp->data.u.str.len = buf - temp->str; |
| 9555 | smp->data.u.str.size = temp->size; |
| 9556 | |
| 9557 | return 1; |
| 9558 | } |
| 9559 | |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9560 | /* returns the longest available part of the body. This requires that the body |
| 9561 | * has been waited for using http-buffer-request. |
| 9562 | */ |
| 9563 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9564 | 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] | 9565 | { |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9566 | struct http_msg *msg; |
| 9567 | unsigned long len; |
| 9568 | unsigned long block1; |
| 9569 | char *body; |
| 9570 | struct chunk *temp; |
| 9571 | |
| 9572 | CHECK_HTTP_MESSAGE_FIRST(); |
| 9573 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9574 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 9575 | msg = &smp->strm->txn->req; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9576 | else |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 9577 | msg = &smp->strm->txn->rsp; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9578 | |
| 9579 | len = http_body_bytes(msg); |
| 9580 | body = b_ptr(msg->chn->buf, -http_data_rewind(msg)); |
| 9581 | |
| 9582 | block1 = len; |
| 9583 | if (block1 > msg->chn->buf->data + msg->chn->buf->size - body) |
| 9584 | block1 = msg->chn->buf->data + msg->chn->buf->size - body; |
| 9585 | |
| 9586 | if (block1 == len) { |
| 9587 | /* buffer is not wrapped (or empty) */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9588 | smp->data.type = SMP_T_BIN; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9589 | smp->data.u.str.str = body; |
| 9590 | smp->data.u.str.len = len; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9591 | smp->flags = SMP_F_VOL_TEST | SMP_F_CONST; |
| 9592 | } |
| 9593 | else { |
| 9594 | /* buffer is wrapped, we need to defragment it */ |
| 9595 | temp = get_trash_chunk(); |
| 9596 | memcpy(temp->str, body, block1); |
| 9597 | memcpy(temp->str + block1, msg->chn->buf->data, len - block1); |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9598 | smp->data.type = SMP_T_BIN; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9599 | smp->data.u.str.str = temp->str; |
| 9600 | smp->data.u.str.len = len; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9601 | smp->flags = SMP_F_VOL_TEST; |
| 9602 | } |
| 9603 | return 1; |
| 9604 | } |
| 9605 | |
| 9606 | |
| 9607 | /* returns the available length of the body. This requires that the body |
| 9608 | * has been waited for using http-buffer-request. |
| 9609 | */ |
| 9610 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9611 | 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] | 9612 | { |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9613 | struct http_msg *msg; |
| 9614 | |
| 9615 | CHECK_HTTP_MESSAGE_FIRST(); |
| 9616 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9617 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 9618 | msg = &smp->strm->txn->req; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9619 | else |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 9620 | msg = &smp->strm->txn->rsp; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9621 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9622 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9623 | smp->data.u.sint = http_body_bytes(msg); |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9624 | |
| 9625 | smp->flags = SMP_F_VOL_TEST; |
| 9626 | return 1; |
| 9627 | } |
| 9628 | |
| 9629 | |
| 9630 | /* returns the advertised length of the body, or the advertised size of the |
| 9631 | * chunks available in the buffer. This requires that the body has been waited |
| 9632 | * for using http-buffer-request. |
| 9633 | */ |
| 9634 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9635 | 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] | 9636 | { |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9637 | struct http_msg *msg; |
| 9638 | |
| 9639 | CHECK_HTTP_MESSAGE_FIRST(); |
| 9640 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9641 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 9642 | msg = &smp->strm->txn->req; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9643 | else |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 9644 | msg = &smp->strm->txn->rsp; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9645 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9646 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9647 | smp->data.u.sint = msg->body_len; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 9648 | |
| 9649 | smp->flags = SMP_F_VOL_TEST; |
| 9650 | return 1; |
| 9651 | } |
| 9652 | |
| 9653 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9654 | /* 4. Check on URL/URI. A pointer to the URI is stored. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 9655 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9656 | 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] | 9657 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 9658 | struct http_txn *txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9659 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9660 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9661 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 9662 | txn = smp->strm->txn; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9663 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9664 | smp->data.u.str.len = txn->req.sl.rq.u_l; |
| 9665 | 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] | 9666 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9667 | return 1; |
| 9668 | } |
| 9669 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9670 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9671 | 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] | 9672 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 9673 | struct http_txn *txn; |
Willy Tarreau | 4c804ec | 2013-09-30 14:37:14 +0200 | [diff] [blame] | 9674 | struct sockaddr_storage addr; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9675 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9676 | CHECK_HTTP_MESSAGE_FIRST(); |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9677 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 9678 | txn = smp->strm->txn; |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 9679 | 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] | 9680 | if (((struct sockaddr_in *)&addr)->sin_family != AF_INET) |
Willy Tarreau | f4362b3 | 2011-12-16 17:49:52 +0100 | [diff] [blame] | 9681 | return 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9682 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9683 | smp->data.type = SMP_T_IPV4; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9684 | smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9685 | smp->flags = 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9686 | return 1; |
| 9687 | } |
| 9688 | |
| 9689 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9690 | 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] | 9691 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 9692 | struct http_txn *txn; |
Willy Tarreau | 4c804ec | 2013-09-30 14:37:14 +0200 | [diff] [blame] | 9693 | struct sockaddr_storage addr; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9694 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9695 | CHECK_HTTP_MESSAGE_FIRST(); |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9696 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 9697 | txn = smp->strm->txn; |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 9698 | 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] | 9699 | if (((struct sockaddr_in *)&addr)->sin_family != AF_INET) |
| 9700 | return 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9701 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9702 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9703 | smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port); |
Willy Tarreau | 21e5b0e | 2012-04-23 19:25:44 +0200 | [diff] [blame] | 9704 | smp->flags = 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 9705 | return 1; |
| 9706 | } |
| 9707 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9708 | /* Fetch an HTTP header. A pointer to the beginning of the value is returned. |
| 9709 | * Accepts an optional argument of type string containing the header field name, |
| 9710 | * and an optional argument of type signed or unsigned integer to request an |
| 9711 | * 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] | 9712 | * headers are considered from the first one. It does not stop on commas and |
| 9713 | * returns full lines instead (useful for User-Agent or Date for example). |
| 9714 | */ |
| 9715 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9716 | 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] | 9717 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 9718 | struct hdr_idx *idx; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 9719 | struct hdr_ctx *ctx = smp->ctx.a[0]; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 9720 | const struct http_msg *msg; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 9721 | int occ = 0; |
| 9722 | const char *name_str = NULL; |
| 9723 | int name_len = 0; |
| 9724 | |
| 9725 | if (!ctx) { |
| 9726 | /* first call */ |
| 9727 | ctx = &static_hdr_ctx; |
| 9728 | ctx->idx = 0; |
| 9729 | smp->ctx.a[0] = ctx; |
| 9730 | } |
| 9731 | |
| 9732 | if (args) { |
| 9733 | if (args[0].type != ARGT_STR) |
| 9734 | return 0; |
| 9735 | name_str = args[0].data.str.str; |
| 9736 | name_len = args[0].data.str.len; |
| 9737 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 9738 | if (args[1].type == ARGT_SINT) |
| 9739 | occ = args[1].data.sint; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 9740 | } |
| 9741 | |
| 9742 | CHECK_HTTP_MESSAGE_FIRST(); |
| 9743 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 9744 | idx = &smp->strm->txn->hdr_idx; |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9745 | 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] | 9746 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 9747 | if (ctx && !(smp->flags & SMP_F_NOT_LAST)) |
| 9748 | /* search for header from the beginning */ |
| 9749 | ctx->idx = 0; |
| 9750 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9751 | if (!occ && !(smp->opt & SMP_OPT_ITERATE)) |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 9752 | /* no explicit occurrence and single fetch => last header by default */ |
| 9753 | occ = -1; |
| 9754 | |
| 9755 | if (!occ) |
| 9756 | /* prepare to report multiple occurrences for ACL fetches */ |
| 9757 | smp->flags |= SMP_F_NOT_LAST; |
| 9758 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9759 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9760 | smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9761 | 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] | 9762 | return 1; |
| 9763 | |
| 9764 | smp->flags &= ~SMP_F_NOT_LAST; |
| 9765 | return 0; |
| 9766 | } |
| 9767 | |
| 9768 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
| 9769 | * Accepts exactly 1 argument of type string. It does not stop on commas and |
| 9770 | * returns full lines instead (useful for User-Agent or Date for example). |
| 9771 | */ |
| 9772 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9773 | 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] | 9774 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 9775 | struct hdr_idx *idx; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 9776 | struct hdr_ctx ctx; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 9777 | const struct http_msg *msg; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 9778 | int cnt; |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 9779 | const char *name = NULL; |
| 9780 | int len = 0; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 9781 | |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 9782 | if (args && args->type == ARGT_STR) { |
| 9783 | name = args->data.str.str; |
| 9784 | len = args->data.str.len; |
| 9785 | } |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 9786 | |
| 9787 | CHECK_HTTP_MESSAGE_FIRST(); |
| 9788 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 9789 | idx = &smp->strm->txn->hdr_idx; |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9790 | 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] | 9791 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 9792 | ctx.idx = 0; |
| 9793 | cnt = 0; |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 9794 | 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] | 9795 | cnt++; |
| 9796 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9797 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9798 | smp->data.u.sint = cnt; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 9799 | smp->flags = SMP_F_VOL_HDR; |
| 9800 | return 1; |
| 9801 | } |
| 9802 | |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 9803 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9804 | 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] | 9805 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 9806 | struct hdr_idx *idx; |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 9807 | struct hdr_ctx ctx; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 9808 | const struct http_msg *msg; |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 9809 | struct chunk *temp; |
| 9810 | char del = ','; |
| 9811 | |
| 9812 | if (args && args->type == ARGT_STR) |
| 9813 | del = *args[0].data.str.str; |
| 9814 | |
| 9815 | CHECK_HTTP_MESSAGE_FIRST(); |
| 9816 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 9817 | idx = &smp->strm->txn->hdr_idx; |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9818 | 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] | 9819 | |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 9820 | temp = get_trash_chunk(); |
| 9821 | |
| 9822 | ctx.idx = 0; |
| 9823 | while (http_find_next_header(msg->chn->buf->p, idx, &ctx)) { |
| 9824 | if (temp->len) |
| 9825 | temp->str[temp->len++] = del; |
| 9826 | memcpy(temp->str + temp->len, ctx.line, ctx.del); |
| 9827 | temp->len += ctx.del; |
| 9828 | } |
| 9829 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9830 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9831 | smp->data.u.str.str = temp->str; |
| 9832 | smp->data.u.str.len = temp->len; |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 9833 | smp->flags = SMP_F_VOL_HDR; |
| 9834 | return 1; |
| 9835 | } |
| 9836 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 9837 | /* Fetch an HTTP header. A pointer to the beginning of the value is returned. |
| 9838 | * Accepts an optional argument of type string containing the header field name, |
| 9839 | * and an optional argument of type signed or unsigned integer to request an |
| 9840 | * 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] | 9841 | * headers are considered from the first one. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9842 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9843 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9844 | 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] | 9845 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 9846 | struct hdr_idx *idx; |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 9847 | struct hdr_ctx *ctx = smp->ctx.a[0]; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 9848 | const struct http_msg *msg; |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9849 | int occ = 0; |
| 9850 | const char *name_str = NULL; |
| 9851 | int name_len = 0; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 9852 | |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 9853 | if (!ctx) { |
| 9854 | /* first call */ |
| 9855 | ctx = &static_hdr_ctx; |
| 9856 | ctx->idx = 0; |
Willy Tarreau | ffb6f08 | 2013-04-02 23:16:53 +0200 | [diff] [blame] | 9857 | smp->ctx.a[0] = ctx; |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 9858 | } |
| 9859 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9860 | if (args) { |
| 9861 | if (args[0].type != ARGT_STR) |
| 9862 | return 0; |
| 9863 | name_str = args[0].data.str.str; |
| 9864 | name_len = args[0].data.str.len; |
| 9865 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 9866 | if (args[1].type == ARGT_SINT) |
| 9867 | occ = args[1].data.sint; |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9868 | } |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 9869 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 9870 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9871 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 9872 | idx = &smp->strm->txn->hdr_idx; |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9873 | 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] | 9874 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9875 | if (ctx && !(smp->flags & SMP_F_NOT_LAST)) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9876 | /* search for header from the beginning */ |
| 9877 | ctx->idx = 0; |
| 9878 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9879 | if (!occ && !(smp->opt & SMP_OPT_ITERATE)) |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9880 | /* no explicit occurrence and single fetch => last header by default */ |
| 9881 | occ = -1; |
| 9882 | |
| 9883 | if (!occ) |
| 9884 | /* prepare to report multiple occurrences for ACL fetches */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9885 | smp->flags |= SMP_F_NOT_LAST; |
Willy Tarreau | 664092c | 2011-12-16 19:11:42 +0100 | [diff] [blame] | 9886 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9887 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9888 | smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9889 | 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] | 9890 | return 1; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9891 | |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9892 | smp->flags &= ~SMP_F_NOT_LAST; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9893 | return 0; |
| 9894 | } |
| 9895 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9896 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 9897 | * Accepts exactly 1 argument of type string. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9898 | */ |
| 9899 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9900 | 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] | 9901 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 9902 | struct hdr_idx *idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9903 | struct hdr_ctx ctx; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 9904 | const struct http_msg *msg; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9905 | int cnt; |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 9906 | const char *name = NULL; |
| 9907 | int len = 0; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9908 | |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 9909 | if (args && args->type == ARGT_STR) { |
| 9910 | name = args->data.str.str; |
| 9911 | len = args->data.str.len; |
| 9912 | } |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 9913 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 9914 | CHECK_HTTP_MESSAGE_FIRST(); |
| 9915 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 9916 | idx = &smp->strm->txn->hdr_idx; |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9917 | 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] | 9918 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9919 | ctx.idx = 0; |
| 9920 | cnt = 0; |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 9921 | while (http_find_header2(name, len, msg->chn->buf->p, idx, &ctx)) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9922 | cnt++; |
| 9923 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9924 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9925 | smp->data.u.sint = cnt; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9926 | smp->flags = SMP_F_VOL_HDR; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9927 | return 1; |
| 9928 | } |
| 9929 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9930 | /* Fetch an HTTP header's integer value. The integer value is returned. It |
| 9931 | * takes a mandatory argument of type string and an optional one of type int |
| 9932 | * to designate a specific occurrence. It returns an unsigned integer, which |
| 9933 | * may or may not be appropriate for everything. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9934 | */ |
| 9935 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9936 | 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] | 9937 | { |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9938 | int ret = smp_fetch_hdr(args, smp, kw, private); |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 9939 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9940 | if (ret > 0) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9941 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9942 | 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] | 9943 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9944 | |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 9945 | return ret; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9946 | } |
| 9947 | |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 9948 | /* Fetch an HTTP header's IP value. takes a mandatory argument of type string |
| 9949 | * and an optional one of type int to designate a specific occurrence. |
| 9950 | * It returns an IPv4 or IPv6 address. |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 9951 | */ |
| 9952 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9953 | 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] | 9954 | { |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 9955 | int ret; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 9956 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9957 | while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9958 | if (url2ipv4((char *)smp->data.u.str.str, &smp->data.u.ipv4)) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9959 | smp->data.type = SMP_T_IPV4; |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 9960 | break; |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 9961 | } else { |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 9962 | struct chunk *temp = get_trash_chunk(); |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9963 | if (smp->data.u.str.len < temp->size - 1) { |
| 9964 | memcpy(temp->str, smp->data.u.str.str, smp->data.u.str.len); |
| 9965 | temp->str[smp->data.u.str.len] = '\0'; |
| 9966 | if (inet_pton(AF_INET6, temp->str, &smp->data.u.ipv6)) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9967 | smp->data.type = SMP_T_IPV6; |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 9968 | break; |
| 9969 | } |
| 9970 | } |
| 9971 | } |
| 9972 | |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 9973 | /* if the header doesn't match an IP address, fetch next one */ |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9974 | if (!(smp->flags & SMP_F_NOT_LAST)) |
| 9975 | return 0; |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 9976 | } |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 9977 | return ret; |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 9978 | } |
| 9979 | |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 9980 | /* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at |
| 9981 | * the first '/' after the possible hostname, and ends before the possible '?'. |
| 9982 | */ |
| 9983 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9984 | 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] | 9985 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 9986 | struct http_txn *txn; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 9987 | char *ptr, *end; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 9988 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9989 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 9990 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 9991 | txn = smp->strm->txn; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9992 | 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] | 9993 | ptr = http_get_path(txn); |
| 9994 | if (!ptr) |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 9995 | return 0; |
| 9996 | |
| 9997 | /* OK, we got the '/' ! */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 9998 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 9999 | smp->data.u.str.str = ptr; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10000 | |
| 10001 | while (ptr < end && *ptr != '?') |
| 10002 | ptr++; |
| 10003 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10004 | smp->data.u.str.len = ptr - smp->data.u.str.str; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10005 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10006 | return 1; |
| 10007 | } |
| 10008 | |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10009 | /* This produces a concatenation of the first occurrence of the Host header |
| 10010 | * followed by the path component if it begins with a slash ('/'). This means |
| 10011 | * that '*' will not be added, resulting in exactly the first Host entry. |
| 10012 | * If no Host header is found, then the path is returned as-is. The returned |
| 10013 | * 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] | 10014 | * The returned sample is of type string. |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10015 | */ |
| 10016 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10017 | 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] | 10018 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10019 | struct http_txn *txn; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10020 | char *ptr, *end, *beg; |
| 10021 | struct hdr_ctx ctx; |
Willy Tarreau | 3caf2af | 2014-06-24 17:27:02 +0200 | [diff] [blame] | 10022 | struct chunk *temp; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10023 | |
| 10024 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10025 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10026 | txn = smp->strm->txn; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10027 | ctx.idx = 0; |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 10028 | 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] | 10029 | return smp_fetch_path(args, smp, kw, private); |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10030 | |
| 10031 | /* 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] | 10032 | temp = get_trash_chunk(); |
| 10033 | memcpy(temp->str, ctx.line + ctx.val, ctx.vlen); |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10034 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10035 | smp->data.u.str.str = temp->str; |
| 10036 | smp->data.u.str.len = ctx.vlen; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10037 | |
| 10038 | /* now retrieve the path */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 10039 | 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] | 10040 | beg = http_get_path(txn); |
| 10041 | if (!beg) |
| 10042 | beg = end; |
| 10043 | |
| 10044 | for (ptr = beg; ptr < end && *ptr != '?'; ptr++); |
| 10045 | |
| 10046 | if (beg < ptr && *beg == '/') { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10047 | memcpy(smp->data.u.str.str + smp->data.u.str.len, beg, ptr - beg); |
| 10048 | smp->data.u.str.len += ptr - beg; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10049 | } |
| 10050 | |
| 10051 | smp->flags = SMP_F_VOL_1ST; |
| 10052 | return 1; |
| 10053 | } |
| 10054 | |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 10055 | /* This produces a 32-bit hash of the concatenation of the first occurrence of |
| 10056 | * the Host header followed by the path component if it begins with a slash ('/'). |
| 10057 | * This means that '*' will not be added, resulting in exactly the first Host |
| 10058 | * 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] | 10059 | * is hashed using the path hash followed by a full avalanche hash and provides a |
| 10060 | * 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] | 10061 | * high-traffic sites without having to store whole paths. |
| 10062 | */ |
Thierry FOURNIER | 055b9d5 | 2014-07-15 16:11:07 +0200 | [diff] [blame] | 10063 | int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10064 | 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] | 10065 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10066 | struct http_txn *txn; |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 10067 | struct hdr_ctx ctx; |
| 10068 | unsigned int hash = 0; |
| 10069 | char *ptr, *beg, *end; |
| 10070 | int len; |
| 10071 | |
| 10072 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10073 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10074 | txn = smp->strm->txn; |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 10075 | ctx.idx = 0; |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 10076 | 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] | 10077 | /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */ |
| 10078 | ptr = ctx.line + ctx.val; |
| 10079 | len = ctx.vlen; |
| 10080 | while (len--) |
| 10081 | hash = *(ptr++) + (hash << 6) + (hash << 16) - hash; |
| 10082 | } |
| 10083 | |
| 10084 | /* now retrieve the path */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 10085 | 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] | 10086 | beg = http_get_path(txn); |
| 10087 | if (!beg) |
| 10088 | beg = end; |
| 10089 | |
| 10090 | for (ptr = beg; ptr < end && *ptr != '?'; ptr++); |
| 10091 | |
| 10092 | if (beg < ptr && *beg == '/') { |
| 10093 | while (beg < ptr) |
| 10094 | hash = *(beg++) + (hash << 6) + (hash << 16) - hash; |
| 10095 | } |
| 10096 | hash = full_hash(hash); |
| 10097 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10098 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10099 | smp->data.u.sint = hash; |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 10100 | smp->flags = SMP_F_VOL_1ST; |
| 10101 | return 1; |
| 10102 | } |
| 10103 | |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10104 | /* 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] | 10105 | * path as returned by smp_fetch_base32(). The idea is to have per-source and |
| 10106 | * per-path counters. The result is a binary block from 8 to 20 bytes depending |
| 10107 | * 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] | 10108 | * that in environments where IPv6 is insignificant, truncating the output to |
| 10109 | * 8 bytes would still work. |
| 10110 | */ |
| 10111 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10112 | 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] | 10113 | { |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 10114 | struct chunk *temp; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10115 | struct connection *cli_conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10116 | |
| 10117 | if (!cli_conn) |
| 10118 | return 0; |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10119 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10120 | if (!smp_fetch_base32(args, smp, kw, private)) |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10121 | return 0; |
| 10122 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 10123 | temp = get_trash_chunk(); |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10124 | *(unsigned int *)temp->str = htonl(smp->data.u.sint); |
Willy Tarreau | 5ad6e1d | 2014-07-15 21:34:06 +0200 | [diff] [blame] | 10125 | temp->len += sizeof(unsigned int); |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10126 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10127 | switch (cli_conn->addr.from.ss_family) { |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10128 | case AF_INET: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10129 | 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] | 10130 | temp->len += 4; |
| 10131 | break; |
| 10132 | case AF_INET6: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10133 | 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] | 10134 | temp->len += 16; |
| 10135 | break; |
| 10136 | default: |
| 10137 | return 0; |
| 10138 | } |
| 10139 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10140 | smp->data.u.str = *temp; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10141 | smp->data.type = SMP_T_BIN; |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10142 | return 1; |
| 10143 | } |
| 10144 | |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 10145 | /* Extracts the query string, which comes after the question mark '?'. If no |
| 10146 | * question mark is found, nothing is returned. Otherwise it returns a sample |
| 10147 | * of type string carrying the whole query string. |
| 10148 | */ |
| 10149 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10150 | 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] | 10151 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10152 | struct http_txn *txn; |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 10153 | char *ptr, *end; |
| 10154 | |
| 10155 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10156 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10157 | txn = smp->strm->txn; |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 10158 | ptr = txn->req.chn->buf->p + txn->req.sl.rq.u; |
| 10159 | end = ptr + txn->req.sl.rq.u_l; |
| 10160 | |
| 10161 | /* look up the '?' */ |
| 10162 | do { |
| 10163 | if (ptr == end) |
| 10164 | return 0; |
| 10165 | } while (*ptr++ != '?'); |
| 10166 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10167 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10168 | smp->data.u.str.str = ptr; |
| 10169 | smp->data.u.str.len = end - ptr; |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 10170 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
| 10171 | return 1; |
| 10172 | } |
| 10173 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10174 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10175 | 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] | 10176 | { |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10177 | /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged |
| 10178 | * as a layer7 ACL, which involves automatic allocation of hdr_idx. |
| 10179 | */ |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10180 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10181 | CHECK_HTTP_MESSAGE_FIRST_PERM(); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10182 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10183 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10184 | smp->data.u.sint = 1; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10185 | return 1; |
| 10186 | } |
| 10187 | |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 10188 | /* return a valid test if the current request is the first one on the connection */ |
| 10189 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10190 | 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] | 10191 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10192 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10193 | smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST); |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 10194 | return 1; |
| 10195 | } |
| 10196 | |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10197 | /* Accepts exactly 1 argument of type userlist */ |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 10198 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10199 | 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] | 10200 | { |
| 10201 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10202 | if (!args || args->type != ARGT_USR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10203 | return 0; |
| 10204 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10205 | CHECK_HTTP_MESSAGE_FIRST(); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 10206 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10207 | if (!get_http_auth(smp->strm)) |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 10208 | return 0; |
| 10209 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10210 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10211 | 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] | 10212 | smp->strm->txn->auth.pass); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 10213 | return 1; |
| 10214 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10215 | |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10216 | /* Accepts exactly 1 argument of type userlist */ |
| 10217 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10218 | 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] | 10219 | { |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10220 | if (!args || args->type != ARGT_USR) |
| 10221 | return 0; |
| 10222 | |
| 10223 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10224 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10225 | if (!get_http_auth(smp->strm)) |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10226 | return 0; |
| 10227 | |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10228 | /* if the user does not belong to the userlist or has a wrong password, |
| 10229 | * report that it unconditionally does not match. Otherwise we return |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 10230 | * a string containing the username. |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10231 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10232 | if (!check_user(args->data.usr, smp->strm->txn->auth.user, |
| 10233 | smp->strm->txn->auth.pass)) |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 10234 | return 0; |
| 10235 | |
| 10236 | /* pat_match_auth() will need the user list */ |
| 10237 | smp->ctx.a[0] = args->data.usr; |
| 10238 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10239 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10240 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10241 | smp->data.u.str.str = smp->strm->txn->auth.user; |
| 10242 | smp->data.u.str.len = strlen(smp->strm->txn->auth.user); |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10243 | |
| 10244 | return 1; |
| 10245 | } |
| 10246 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10247 | /* Try to find the next occurrence of a cookie name in a cookie header value. |
| 10248 | * The lookup begins at <hdr>. The pointer and size of the next occurrence of |
| 10249 | * the cookie value is returned into *value and *value_l, and the function |
| 10250 | * returns a pointer to the next pointer to search from if the value was found. |
| 10251 | * Otherwise if the cookie was not found, NULL is returned and neither value |
| 10252 | * nor value_l are touched. The input <hdr> string should first point to the |
| 10253 | * header's value, and the <hdr_end> pointer must point to the first character |
| 10254 | * not part of the value. <list> must be non-zero if value may represent a list |
| 10255 | * of values (cookie headers). This makes it faster to abort parsing when no |
| 10256 | * list is expected. |
| 10257 | */ |
David Carlier | 4686f79 | 2015-09-25 14:10:50 +0100 | [diff] [blame] | 10258 | char * |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10259 | extract_cookie_value(char *hdr, const char *hdr_end, |
| 10260 | char *cookie_name, size_t cookie_name_l, int list, |
Willy Tarreau | 3fb818c | 2012-04-11 17:21:08 +0200 | [diff] [blame] | 10261 | char **value, int *value_l) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10262 | { |
| 10263 | char *equal, *att_end, *att_beg, *val_beg, *val_end; |
| 10264 | char *next; |
| 10265 | |
| 10266 | /* we search at least a cookie name followed by an equal, and more |
| 10267 | * generally something like this : |
| 10268 | * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n |
| 10269 | */ |
| 10270 | for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) { |
| 10271 | /* Iterate through all cookies on this line */ |
| 10272 | |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 10273 | while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg)) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10274 | att_beg++; |
| 10275 | |
| 10276 | /* find att_end : this is the first character after the last non |
| 10277 | * space before the equal. It may be equal to hdr_end. |
| 10278 | */ |
| 10279 | equal = att_end = att_beg; |
| 10280 | |
| 10281 | while (equal < hdr_end) { |
| 10282 | if (*equal == '=' || *equal == ';' || (list && *equal == ',')) |
| 10283 | break; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 10284 | if (HTTP_IS_SPHT(*equal++)) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10285 | continue; |
| 10286 | att_end = equal; |
| 10287 | } |
| 10288 | |
| 10289 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 10290 | * is between <att_beg> and <equal>, both may be identical. |
| 10291 | */ |
| 10292 | |
| 10293 | /* look for end of cookie if there is an equal sign */ |
| 10294 | if (equal < hdr_end && *equal == '=') { |
| 10295 | /* look for the beginning of the value */ |
| 10296 | val_beg = equal + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 10297 | while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg)) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10298 | val_beg++; |
| 10299 | |
| 10300 | /* find the end of the value, respecting quotes */ |
| 10301 | next = find_cookie_value_end(val_beg, hdr_end); |
| 10302 | |
| 10303 | /* make val_end point to the first white space or delimitor after the value */ |
| 10304 | val_end = next; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 10305 | while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1))) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10306 | val_end--; |
| 10307 | } else { |
| 10308 | val_beg = val_end = next = equal; |
| 10309 | } |
| 10310 | |
| 10311 | /* We have nothing to do with attributes beginning with '$'. However, |
| 10312 | * they will automatically be removed if a header before them is removed, |
| 10313 | * since they're supposed to be linked together. |
| 10314 | */ |
| 10315 | if (*att_beg == '$') |
| 10316 | continue; |
| 10317 | |
| 10318 | /* Ignore cookies with no equal sign */ |
| 10319 | if (equal == next) |
| 10320 | continue; |
| 10321 | |
| 10322 | /* Now we have the cookie name between att_beg and att_end, and |
| 10323 | * its value between val_beg and val_end. |
| 10324 | */ |
| 10325 | |
| 10326 | if (att_end - att_beg == cookie_name_l && |
| 10327 | memcmp(att_beg, cookie_name, cookie_name_l) == 0) { |
| 10328 | /* let's return this value and indicate where to go on from */ |
| 10329 | *value = val_beg; |
| 10330 | *value_l = val_end - val_beg; |
| 10331 | return next + 1; |
| 10332 | } |
| 10333 | |
| 10334 | /* Set-Cookie headers only have the name in the first attr=value part */ |
| 10335 | if (!list) |
| 10336 | break; |
| 10337 | } |
| 10338 | |
| 10339 | return NULL; |
| 10340 | } |
| 10341 | |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10342 | /* Fetch a captured HTTP request header. The index is the position of |
| 10343 | * the "capture" option in the configuration file |
| 10344 | */ |
| 10345 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10346 | 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] | 10347 | { |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10348 | struct proxy *fe = strm_fe(smp->strm); |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10349 | int idx; |
| 10350 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 10351 | if (!args || args->type != ARGT_SINT) |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10352 | return 0; |
| 10353 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 10354 | idx = args->data.sint; |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10355 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10356 | 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] | 10357 | return 0; |
| 10358 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10359 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10360 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10361 | smp->data.u.str.str = smp->strm->req_cap[idx]; |
| 10362 | smp->data.u.str.len = strlen(smp->strm->req_cap[idx]); |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10363 | |
| 10364 | return 1; |
| 10365 | } |
| 10366 | |
| 10367 | /* Fetch a captured HTTP response header. The index is the position of |
| 10368 | * the "capture" option in the configuration file |
| 10369 | */ |
| 10370 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10371 | 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] | 10372 | { |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10373 | struct proxy *fe = strm_fe(smp->strm); |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10374 | int idx; |
| 10375 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 10376 | if (!args || args->type != ARGT_SINT) |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10377 | return 0; |
| 10378 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 10379 | idx = args->data.sint; |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10380 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10381 | 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] | 10382 | return 0; |
| 10383 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10384 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10385 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10386 | smp->data.u.str.str = smp->strm->res_cap[idx]; |
| 10387 | smp->data.u.str.len = strlen(smp->strm->res_cap[idx]); |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 10388 | |
| 10389 | return 1; |
| 10390 | } |
| 10391 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10392 | /* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */ |
| 10393 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10394 | 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] | 10395 | { |
| 10396 | struct chunk *temp; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10397 | struct http_txn *txn = smp->strm->txn; |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10398 | char *ptr; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10399 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10400 | if (!txn || !txn->uri) |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10401 | return 0; |
| 10402 | |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10403 | ptr = txn->uri; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10404 | |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10405 | while (*ptr != ' ' && *ptr != '\0') /* find first space */ |
| 10406 | ptr++; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10407 | |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10408 | temp = get_trash_chunk(); |
| 10409 | temp->str = txn->uri; |
| 10410 | temp->len = ptr - txn->uri; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10411 | smp->data.u.str = *temp; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10412 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10413 | smp->flags = SMP_F_CONST; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10414 | |
| 10415 | return 1; |
| 10416 | |
| 10417 | } |
| 10418 | |
| 10419 | /* Extracts the path in the HTTP request, the txn->uri should be filled before the call */ |
| 10420 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10421 | 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] | 10422 | { |
| 10423 | struct chunk *temp; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10424 | struct http_txn *txn = smp->strm->txn; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10425 | char *ptr; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10426 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10427 | if (!txn || !txn->uri) |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10428 | return 0; |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10429 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10430 | ptr = txn->uri; |
| 10431 | |
| 10432 | while (*ptr != ' ' && *ptr != '\0') /* find first space */ |
| 10433 | ptr++; |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10434 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10435 | if (!*ptr) |
| 10436 | return 0; |
| 10437 | |
| 10438 | ptr++; /* skip the space */ |
| 10439 | |
| 10440 | temp = get_trash_chunk(); |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 10441 | ptr = temp->str = http_get_path_from_string(ptr); |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10442 | if (!ptr) |
| 10443 | return 0; |
| 10444 | while (*ptr != ' ' && *ptr != '\0') /* find space after URI */ |
| 10445 | ptr++; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10446 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10447 | smp->data.u.str = *temp; |
| 10448 | smp->data.u.str.len = ptr - temp->str; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10449 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10450 | smp->flags = SMP_F_CONST; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10451 | |
| 10452 | return 1; |
| 10453 | } |
| 10454 | |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 10455 | /* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it |
| 10456 | * as a string (either "HTTP/1.0" or "HTTP/1.1"). |
| 10457 | */ |
| 10458 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10459 | 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] | 10460 | { |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10461 | struct http_txn *txn = smp->strm->txn; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 10462 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10463 | if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST) |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 10464 | return 0; |
| 10465 | |
| 10466 | if (txn->req.flags & HTTP_MSGF_VER_11) |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10467 | smp->data.u.str.str = "HTTP/1.1"; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 10468 | else |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10469 | smp->data.u.str.str = "HTTP/1.0"; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 10470 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10471 | smp->data.u.str.len = 8; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10472 | smp->data.type = SMP_T_STR; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 10473 | smp->flags = SMP_F_CONST; |
| 10474 | return 1; |
| 10475 | |
| 10476 | } |
| 10477 | |
| 10478 | /* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it |
| 10479 | * as a string (either "HTTP/1.0" or "HTTP/1.1"). |
| 10480 | */ |
| 10481 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10482 | 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] | 10483 | { |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10484 | struct http_txn *txn = smp->strm->txn; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 10485 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10486 | if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST) |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 10487 | return 0; |
| 10488 | |
| 10489 | if (txn->rsp.flags & HTTP_MSGF_VER_11) |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10490 | smp->data.u.str.str = "HTTP/1.1"; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 10491 | else |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10492 | smp->data.u.str.str = "HTTP/1.0"; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 10493 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10494 | smp->data.u.str.len = 8; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10495 | smp->data.type = SMP_T_STR; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 10496 | smp->flags = SMP_F_CONST; |
| 10497 | return 1; |
| 10498 | |
| 10499 | } |
| 10500 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 10501 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10502 | /* 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] | 10503 | * 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] | 10504 | * 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] | 10505 | * 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] | 10506 | * 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] | 10507 | * Accepts exactly 1 argument of type string. If the input options indicate |
| 10508 | * 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] | 10509 | * The returned sample is of type CSTR. Can be used to parse cookies in other |
| 10510 | * files. |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10511 | */ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10512 | 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] | 10513 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10514 | struct http_txn *txn; |
| 10515 | struct hdr_idx *idx; |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 10516 | struct hdr_ctx *ctx = smp->ctx.a[2]; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10517 | const struct http_msg *msg; |
| 10518 | const char *hdr_name; |
| 10519 | int hdr_name_len; |
| 10520 | char *sol; |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10521 | int occ = 0; |
| 10522 | int found = 0; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10523 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10524 | if (!args || args->type != ARGT_STR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10525 | return 0; |
| 10526 | |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 10527 | if (!ctx) { |
| 10528 | /* first call */ |
| 10529 | ctx = &static_hdr_ctx; |
| 10530 | ctx->idx = 0; |
| 10531 | smp->ctx.a[2] = ctx; |
| 10532 | } |
| 10533 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10534 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10535 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10536 | txn = smp->strm->txn; |
| 10537 | idx = &smp->strm->txn->hdr_idx; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10538 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10539 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10540 | msg = &txn->req; |
| 10541 | hdr_name = "Cookie"; |
| 10542 | hdr_name_len = 6; |
| 10543 | } else { |
| 10544 | msg = &txn->rsp; |
| 10545 | hdr_name = "Set-Cookie"; |
| 10546 | hdr_name_len = 10; |
| 10547 | } |
| 10548 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10549 | if (!occ && !(smp->opt & SMP_OPT_ITERATE)) |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10550 | /* no explicit occurrence and single fetch => last cookie by default */ |
| 10551 | occ = -1; |
| 10552 | |
| 10553 | /* OK so basically here, either we want only one value and it's the |
| 10554 | * last one, or we want to iterate over all of them and we fetch the |
| 10555 | * next one. |
| 10556 | */ |
| 10557 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10558 | sol = msg->chn->buf->p; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10559 | if (!(smp->flags & SMP_F_NOT_LAST)) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10560 | /* search for the header from the beginning, we must first initialize |
| 10561 | * the search parameters. |
| 10562 | */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10563 | smp->ctx.a[0] = NULL; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10564 | ctx->idx = 0; |
| 10565 | } |
| 10566 | |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10567 | smp->flags |= SMP_F_VOL_HDR; |
| 10568 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10569 | while (1) { |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10570 | /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */ |
| 10571 | if (!smp->ctx.a[0]) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10572 | if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx)) |
| 10573 | goto out; |
| 10574 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10575 | if (ctx->vlen < args->data.str.len + 1) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10576 | continue; |
| 10577 | |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10578 | smp->ctx.a[0] = ctx->line + ctx->val; |
| 10579 | smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10580 | } |
| 10581 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10582 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10583 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10584 | 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] | 10585 | args->data.str.str, args->data.str.len, |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10586 | (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ, |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10587 | &smp->data.u.str.str, |
| 10588 | &smp->data.u.str.len); |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10589 | if (smp->ctx.a[0]) { |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10590 | found = 1; |
| 10591 | if (occ >= 0) { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10592 | /* one value was returned into smp->data.u.str.{str,len} */ |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10593 | smp->flags |= SMP_F_NOT_LAST; |
| 10594 | return 1; |
| 10595 | } |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10596 | } |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10597 | /* if we're looking for last occurrence, let's loop */ |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10598 | } |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10599 | /* all cookie headers and values were scanned. If we're looking for the |
| 10600 | * last occurrence, we may return it now. |
| 10601 | */ |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10602 | out: |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10603 | smp->flags &= ~SMP_F_NOT_LAST; |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 10604 | return found; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10605 | } |
| 10606 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10607 | /* 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] | 10608 | * 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] | 10609 | * multiple cookies may be parsed on the same line. The returned sample is of |
| 10610 | * type UINT. Accepts exactly 1 argument of type string. |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10611 | */ |
| 10612 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10613 | 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] | 10614 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10615 | struct http_txn *txn; |
| 10616 | struct hdr_idx *idx; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10617 | struct hdr_ctx ctx; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10618 | const struct http_msg *msg; |
| 10619 | const char *hdr_name; |
| 10620 | int hdr_name_len; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10621 | int cnt; |
| 10622 | char *val_beg, *val_end; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10623 | char *sol; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10624 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10625 | if (!args || args->type != ARGT_STR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10626 | return 0; |
| 10627 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10628 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10629 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10630 | txn = smp->strm->txn; |
| 10631 | idx = &smp->strm->txn->hdr_idx; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10632 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10633 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10634 | msg = &txn->req; |
| 10635 | hdr_name = "Cookie"; |
| 10636 | hdr_name_len = 6; |
| 10637 | } else { |
| 10638 | msg = &txn->rsp; |
| 10639 | hdr_name = "Set-Cookie"; |
| 10640 | hdr_name_len = 10; |
| 10641 | } |
| 10642 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10643 | sol = msg->chn->buf->p; |
Willy Tarreau | 46787ed | 2012-04-11 17:28:40 +0200 | [diff] [blame] | 10644 | val_end = val_beg = NULL; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10645 | ctx.idx = 0; |
| 10646 | cnt = 0; |
| 10647 | |
| 10648 | while (1) { |
| 10649 | /* Note: val_beg == NULL every time we need to fetch a new header */ |
| 10650 | if (!val_beg) { |
| 10651 | if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx)) |
| 10652 | break; |
| 10653 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10654 | if (ctx.vlen < args->data.str.len + 1) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10655 | continue; |
| 10656 | |
| 10657 | val_beg = ctx.line + ctx.val; |
| 10658 | val_end = val_beg + ctx.vlen; |
| 10659 | } |
| 10660 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10661 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10662 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10663 | while ((val_beg = extract_cookie_value(val_beg, val_end, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10664 | args->data.str.str, args->data.str.len, |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10665 | (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ, |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10666 | &smp->data.u.str.str, |
| 10667 | &smp->data.u.str.len))) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10668 | cnt++; |
| 10669 | } |
| 10670 | } |
| 10671 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10672 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10673 | smp->data.u.sint = cnt; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10674 | smp->flags |= SMP_F_VOL_HDR; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10675 | return 1; |
| 10676 | } |
| 10677 | |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 10678 | /* Fetch an cookie's integer value. The integer value is returned. It |
| 10679 | * takes a mandatory argument of type string. It relies on smp_fetch_cookie(). |
| 10680 | */ |
| 10681 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10682 | 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] | 10683 | { |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10684 | int ret = smp_fetch_cookie(args, smp, kw, private); |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 10685 | |
| 10686 | if (ret > 0) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10687 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10688 | 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] | 10689 | } |
| 10690 | |
| 10691 | return ret; |
| 10692 | } |
| 10693 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10694 | /************************************************************************/ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 10695 | /* The code below is dedicated to sample fetches */ |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 10696 | /************************************************************************/ |
| 10697 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10698 | /* |
| 10699 | * Given a path string and its length, find the position of beginning of the |
| 10700 | * query string. Returns NULL if no query string is found in the path. |
| 10701 | * |
| 10702 | * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22: |
| 10703 | * |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 10704 | * find_query_string(path, n, '?') points to "yo=mama;ye=daddy" string. |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10705 | */ |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 10706 | 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] | 10707 | { |
| 10708 | char *p; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 10709 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 10710 | p = memchr(path, delim, path_l); |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10711 | return p ? p + 1 : NULL; |
| 10712 | } |
| 10713 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 10714 | static inline int is_param_delimiter(char c, char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10715 | { |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 10716 | return c == '&' || c == ';' || c == delim; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10717 | } |
| 10718 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10719 | /* after increasing a pointer value, it can exceed the first buffer |
| 10720 | * size. This function transform the value of <ptr> according with |
| 10721 | * the expected position. <chunks> is an array of the one or two |
| 10722 | * avalaible chunks. The first value is the start of the first chunk, |
| 10723 | * the second value if the end+1 of the first chunks. The third value |
| 10724 | * is NULL or the start of the second chunk and the fourth value is |
| 10725 | * the end+1 of the second chunk. The function returns 1 if does a |
| 10726 | * wrap, else returns 0. |
| 10727 | */ |
| 10728 | static inline int fix_pointer_if_wrap(const char **chunks, const char **ptr) |
| 10729 | { |
| 10730 | if (*ptr < chunks[1]) |
| 10731 | return 0; |
| 10732 | if (!chunks[2]) |
| 10733 | return 0; |
| 10734 | *ptr = chunks[2] + ( *ptr - chunks[1] ); |
| 10735 | return 1; |
| 10736 | } |
| 10737 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10738 | /* |
| 10739 | * Given a url parameter, find the starting position of the first occurence, |
| 10740 | * or NULL if the parameter is not found. |
| 10741 | * |
| 10742 | * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye", |
| 10743 | * the function will return query_string+8. |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10744 | * |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 10745 | * 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] | 10746 | * or the second chunk. The caller must be check the position before using the |
| 10747 | * result. |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10748 | */ |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10749 | static const char * |
| 10750 | find_url_param_pos(const char **chunks, |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 10751 | const char* url_param_name, size_t url_param_name_l, |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 10752 | char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10753 | { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10754 | const char *pos, *last, *equal; |
| 10755 | const char **bufs = chunks; |
| 10756 | int l1, l2; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10757 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10758 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10759 | pos = bufs[0]; |
| 10760 | last = bufs[1]; |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 10761 | while (pos < last) { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10762 | /* Check the equal. */ |
| 10763 | equal = pos + url_param_name_l; |
| 10764 | if (fix_pointer_if_wrap(chunks, &equal)) { |
| 10765 | if (equal >= chunks[3]) |
| 10766 | return NULL; |
| 10767 | } else { |
| 10768 | if (equal >= chunks[1]) |
| 10769 | return NULL; |
| 10770 | } |
| 10771 | if (*equal == '=') { |
| 10772 | if (pos + url_param_name_l > last) { |
| 10773 | /* process wrap case, we detect a wrap. In this case, the |
| 10774 | * comparison is performed in two parts. |
| 10775 | */ |
| 10776 | |
| 10777 | /* This is the end, we dont have any other chunk. */ |
| 10778 | if (bufs != chunks || !bufs[2]) |
| 10779 | return NULL; |
| 10780 | |
| 10781 | /* Compute the length of each part of the comparison. */ |
| 10782 | l1 = last - pos; |
| 10783 | l2 = url_param_name_l - l1; |
| 10784 | |
| 10785 | /* The second buffer is too short to contain the compared string. */ |
| 10786 | if (bufs[2] + l2 > bufs[3]) |
| 10787 | return NULL; |
| 10788 | |
| 10789 | if (memcmp(pos, url_param_name, l1) == 0 && |
| 10790 | memcmp(bufs[2], url_param_name+l1, l2) == 0) |
| 10791 | return pos; |
| 10792 | |
| 10793 | /* Perform wrapping and jump the string who fail the comparison. */ |
| 10794 | bufs += 2; |
| 10795 | pos = bufs[0] + l2; |
| 10796 | last = bufs[1]; |
| 10797 | |
| 10798 | } else { |
| 10799 | /* process a simple comparison. */ |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 10800 | if (memcmp(pos, url_param_name, url_param_name_l) == 0) |
| 10801 | return pos; |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10802 | pos += url_param_name_l + 1; |
| 10803 | if (fix_pointer_if_wrap(chunks, &pos)) |
| 10804 | last = bufs[2]; |
| 10805 | } |
| 10806 | } |
| 10807 | |
| 10808 | while (1) { |
| 10809 | /* Look for the next delimiter. */ |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 10810 | while (pos < last && !is_param_delimiter(*pos, delim)) |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10811 | pos++; |
| 10812 | if (pos < last) |
| 10813 | break; |
| 10814 | /* process buffer wrapping. */ |
| 10815 | if (bufs != chunks || !bufs[2]) |
| 10816 | return NULL; |
| 10817 | bufs += 2; |
| 10818 | pos = bufs[0]; |
| 10819 | last = bufs[1]; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10820 | } |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10821 | pos++; |
| 10822 | } |
| 10823 | return NULL; |
| 10824 | } |
| 10825 | |
| 10826 | /* |
Cyril Bonté | ce1ef4d | 2015-11-26 21:39:56 +0100 | [diff] [blame] | 10827 | * Given a url parameter name and a query string, find the next value. |
| 10828 | * An empty url_param_name matches the first available parameter. |
| 10829 | * If the parameter is found, 1 is returned and *vstart / *vend are updated to |
| 10830 | * respectively provide a pointer to the value and its end. |
| 10831 | * Otherwise, 0 is returned and vstart/vend are not modified. |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10832 | */ |
| 10833 | static int |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10834 | find_next_url_param(const char **chunks, |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 10835 | const char* url_param_name, size_t url_param_name_l, |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10836 | const char **vstart, const char **vend, char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10837 | { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10838 | const char *arg_start, *qs_end; |
| 10839 | const char *value_start, *value_end; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10840 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10841 | arg_start = chunks[0]; |
| 10842 | qs_end = chunks[1]; |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 10843 | if (url_param_name_l) { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10844 | /* Looks for an argument name. */ |
| 10845 | arg_start = find_url_param_pos(chunks, |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 10846 | url_param_name, url_param_name_l, |
| 10847 | delim); |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10848 | /* Check for wrapping. */ |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 10849 | if (arg_start >= qs_end) |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10850 | qs_end = chunks[3]; |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 10851 | } |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10852 | if (!arg_start) |
| 10853 | return 0; |
| 10854 | |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 10855 | if (!url_param_name_l) { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10856 | while (1) { |
| 10857 | /* looks for the first argument. */ |
| 10858 | value_start = memchr(arg_start, '=', qs_end - arg_start); |
| 10859 | if (!value_start) { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10860 | /* Check for wrapping. */ |
| 10861 | if (arg_start >= chunks[0] && |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 10862 | arg_start < chunks[1] && |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10863 | chunks[2]) { |
| 10864 | arg_start = chunks[2]; |
| 10865 | qs_end = chunks[3]; |
| 10866 | continue; |
| 10867 | } |
| 10868 | return 0; |
| 10869 | } |
| 10870 | break; |
| 10871 | } |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 10872 | value_start++; |
| 10873 | } |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10874 | else { |
| 10875 | /* Jump the argument length. */ |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 10876 | value_start = arg_start + url_param_name_l + 1; |
| 10877 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10878 | /* Check for pointer wrapping. */ |
| 10879 | if (fix_pointer_if_wrap(chunks, &value_start)) { |
| 10880 | /* Update the end pointer. */ |
| 10881 | qs_end = chunks[3]; |
| 10882 | |
| 10883 | /* Check for overflow. */ |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 10884 | if (value_start >= qs_end) |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10885 | return 0; |
| 10886 | } |
| 10887 | } |
| 10888 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10889 | value_end = value_start; |
| 10890 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10891 | while (1) { |
| 10892 | while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim)) |
| 10893 | value_end++; |
| 10894 | if (value_end < qs_end) |
| 10895 | break; |
| 10896 | /* process buffer wrapping. */ |
| 10897 | if (value_end >= chunks[0] && |
Willy Tarreau | f662582 | 2015-12-27 14:51:01 +0100 | [diff] [blame] | 10898 | value_end < chunks[1] && |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10899 | chunks[2]) { |
| 10900 | value_end = chunks[2]; |
| 10901 | qs_end = chunks[3]; |
| 10902 | continue; |
| 10903 | } |
| 10904 | break; |
| 10905 | } |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10906 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10907 | *vstart = value_start; |
| 10908 | *vend = value_end; |
Cyril Bonté | ce1ef4d | 2015-11-26 21:39:56 +0100 | [diff] [blame] | 10909 | return 1; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10910 | } |
| 10911 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10912 | /* This scans a URL-encoded query string. It takes an optionally wrapping |
| 10913 | * string whose first contigous chunk has its beginning in ctx->a[0] and end |
| 10914 | * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The |
| 10915 | * pointers are updated for next iteration before leaving. |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 10916 | */ |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10917 | static int |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 10918 | 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] | 10919 | { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10920 | const char *vstart, *vend; |
| 10921 | struct chunk *temp; |
| 10922 | const char **chunks = (const char **)smp->ctx.a; |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 10923 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10924 | if (!find_next_url_param(chunks, |
Thierry FOURNIER | 0948d41 | 2015-05-20 15:22:37 +0200 | [diff] [blame] | 10925 | name, name_len, |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10926 | &vstart, &vend, |
Thierry FOURNIER | 0948d41 | 2015-05-20 15:22:37 +0200 | [diff] [blame] | 10927 | delim)) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10928 | return 0; |
| 10929 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10930 | /* Create sample. If the value is contiguous, return the pointer as CONST, |
| 10931 | * if the value is wrapped, copy-it in a buffer. |
| 10932 | */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10933 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10934 | if (chunks[2] && |
| 10935 | vstart >= chunks[0] && vstart <= chunks[1] && |
| 10936 | vend >= chunks[2] && vend <= chunks[3]) { |
| 10937 | /* Wrapped case. */ |
| 10938 | temp = get_trash_chunk(); |
| 10939 | memcpy(temp->str, vstart, chunks[1] - vstart); |
| 10940 | memcpy(temp->str + ( chunks[1] - vstart ), chunks[2], vend - chunks[2]); |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10941 | smp->data.u.str.str = temp->str; |
| 10942 | smp->data.u.str.len = ( chunks[1] - vstart ) + ( vend - chunks[2] ); |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10943 | } else { |
| 10944 | /* Contiguous case. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10945 | smp->data.u.str.str = (char *)vstart; |
| 10946 | smp->data.u.str.len = vend - vstart; |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10947 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
| 10948 | } |
| 10949 | |
| 10950 | /* Update context, check wrapping. */ |
| 10951 | chunks[0] = vend; |
| 10952 | if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) { |
| 10953 | chunks[1] = chunks[3]; |
| 10954 | chunks[2] = NULL; |
| 10955 | } |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 10956 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 10957 | if (chunks[0] < chunks[1]) |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 10958 | smp->flags |= SMP_F_NOT_LAST; |
| 10959 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 10960 | return 1; |
| 10961 | } |
| 10962 | |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 10963 | /* This function iterates over each parameter of the query string. It uses |
| 10964 | * 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] | 10965 | * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL. |
| 10966 | * An optional parameter name is passed in args[0], otherwise any parameter is |
| 10967 | * considered. It supports an optional delimiter argument for the beginning of |
| 10968 | * the string in args[1], which defaults to "?". |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 10969 | */ |
| 10970 | static int |
| 10971 | smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 10972 | { |
| 10973 | struct http_msg *msg; |
| 10974 | char delim = '?'; |
| 10975 | const char *name; |
| 10976 | int name_len; |
| 10977 | |
Dragan Dosen | 26f77e5 | 2015-05-25 10:02:11 +0200 | [diff] [blame] | 10978 | if (!args || |
| 10979 | (args[0].type && args[0].type != ARGT_STR) || |
| 10980 | (args[1].type && args[1].type != ARGT_STR)) |
| 10981 | return 0; |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 10982 | |
Dragan Dosen | 26f77e5 | 2015-05-25 10:02:11 +0200 | [diff] [blame] | 10983 | name = ""; |
| 10984 | name_len = 0; |
| 10985 | if (args->type == ARGT_STR) { |
| 10986 | name = args->data.str.str; |
| 10987 | name_len = args->data.str.len; |
| 10988 | } |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 10989 | |
Dragan Dosen | 26f77e5 | 2015-05-25 10:02:11 +0200 | [diff] [blame] | 10990 | if (args[1].type) |
| 10991 | delim = *args[1].data.str.str; |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 10992 | |
Dragan Dosen | 26f77e5 | 2015-05-25 10:02:11 +0200 | [diff] [blame] | 10993 | if (!smp->ctx.a[0]) { // first call, find the query string |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 10994 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10995 | |
| 10996 | msg = &smp->strm->txn->req; |
| 10997 | |
| 10998 | smp->ctx.a[0] = find_param_list(msg->chn->buf->p + msg->sl.rq.u, |
| 10999 | msg->sl.rq.u_l, delim); |
| 11000 | if (!smp->ctx.a[0]) |
| 11001 | return 0; |
| 11002 | |
| 11003 | 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] | 11004 | |
| 11005 | /* Assume that the context is filled with NULL pointer |
| 11006 | * before the first call. |
| 11007 | * smp->ctx.a[2] = NULL; |
| 11008 | * smp->ctx.a[3] = NULL; |
| 11009 | */ |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11010 | } |
| 11011 | |
| 11012 | return smp_fetch_param(delim, name, name_len, args, smp, kw, private); |
| 11013 | } |
| 11014 | |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 11015 | /* This function iterates over each parameter of the body. This requires |
| 11016 | * 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] | 11017 | * ctx->a[0] and ctx->a[1] to store the beginning and end of the first |
| 11018 | * contigous part of the body, and optionally ctx->a[2..3] to reference the |
| 11019 | * optional second part if the body wraps at the end of the buffer. An optional |
| 11020 | * parameter name is passed in args[0], otherwise any parameter is considered. |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 11021 | */ |
| 11022 | static int |
| 11023 | smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 11024 | { |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 11025 | struct http_msg *msg; |
| 11026 | unsigned long len; |
| 11027 | unsigned long block1; |
| 11028 | char *body; |
| 11029 | const char *name; |
| 11030 | int name_len; |
| 11031 | |
| 11032 | if (!args || (args[0].type && args[0].type != ARGT_STR)) |
| 11033 | return 0; |
| 11034 | |
| 11035 | name = ""; |
| 11036 | name_len = 0; |
| 11037 | if (args[0].type == ARGT_STR) { |
| 11038 | name = args[0].data.str.str; |
| 11039 | name_len = args[0].data.str.len; |
| 11040 | } |
| 11041 | |
| 11042 | if (!smp->ctx.a[0]) { // first call, find the query string |
| 11043 | CHECK_HTTP_MESSAGE_FIRST(); |
| 11044 | |
| 11045 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 11046 | msg = &smp->strm->txn->req; |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 11047 | else |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 11048 | msg = &smp->strm->txn->rsp; |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 11049 | |
| 11050 | len = http_body_bytes(msg); |
| 11051 | body = b_ptr(msg->chn->buf, -http_data_rewind(msg)); |
| 11052 | |
| 11053 | block1 = len; |
| 11054 | if (block1 > msg->chn->buf->data + msg->chn->buf->size - body) |
| 11055 | block1 = msg->chn->buf->data + msg->chn->buf->size - body; |
| 11056 | |
| 11057 | if (block1 == len) { |
| 11058 | /* buffer is not wrapped (or empty) */ |
| 11059 | smp->ctx.a[0] = body; |
| 11060 | smp->ctx.a[1] = body + len; |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11061 | |
| 11062 | /* Assume that the context is filled with NULL pointer |
| 11063 | * before the first call. |
| 11064 | * smp->ctx.a[2] = NULL; |
| 11065 | * smp->ctx.a[3] = NULL; |
| 11066 | */ |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 11067 | } |
| 11068 | else { |
| 11069 | /* buffer is wrapped, we need to defragment it */ |
| 11070 | smp->ctx.a[0] = body; |
| 11071 | smp->ctx.a[1] = body + block1; |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11072 | smp->ctx.a[2] = msg->chn->buf->data; |
| 11073 | smp->ctx.a[3] = msg->chn->buf->data + ( len - block1 ); |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 11074 | } |
| 11075 | } |
| 11076 | return smp_fetch_param('&', name, name_len, args, smp, kw, private); |
| 11077 | } |
| 11078 | |
Willy Tarreau | a9fddca | 2012-07-31 07:51:48 +0200 | [diff] [blame] | 11079 | /* Return the signed integer value for the specified url parameter (see url_param |
| 11080 | * above). |
| 11081 | */ |
| 11082 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11083 | 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] | 11084 | { |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11085 | int ret = smp_fetch_url_param(args, smp, kw, private); |
Willy Tarreau | a9fddca | 2012-07-31 07:51:48 +0200 | [diff] [blame] | 11086 | |
| 11087 | if (ret > 0) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11088 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11089 | 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] | 11090 | } |
| 11091 | |
| 11092 | return ret; |
| 11093 | } |
| 11094 | |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11095 | /* This produces a 32-bit hash of the concatenation of the first occurrence of |
| 11096 | * the Host header followed by the path component if it begins with a slash ('/'). |
| 11097 | * This means that '*' will not be added, resulting in exactly the first Host |
| 11098 | * entry. If no Host header is found, then the path is used. The resulting value |
| 11099 | * is hashed using the url hash followed by a full avalanche hash and provides a |
| 11100 | * 32-bit integer value. This fetch is useful for tracking per-URL activity on |
| 11101 | * high-traffic sites without having to store whole paths. |
| 11102 | * this differs from the base32 functions in that it includes the url parameters |
| 11103 | * as well as the path |
| 11104 | */ |
| 11105 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11106 | 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] | 11107 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11108 | struct http_txn *txn; |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11109 | struct hdr_ctx ctx; |
| 11110 | unsigned int hash = 0; |
| 11111 | char *ptr, *beg, *end; |
| 11112 | int len; |
| 11113 | |
| 11114 | CHECK_HTTP_MESSAGE_FIRST(); |
| 11115 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11116 | txn = smp->strm->txn; |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11117 | ctx.idx = 0; |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 11118 | 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] | 11119 | /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */ |
| 11120 | ptr = ctx.line + ctx.val; |
| 11121 | len = ctx.vlen; |
| 11122 | while (len--) |
| 11123 | hash = *(ptr++) + (hash << 6) + (hash << 16) - hash; |
| 11124 | } |
| 11125 | |
| 11126 | /* now retrieve the path */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 11127 | 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] | 11128 | beg = http_get_path(txn); |
| 11129 | if (!beg) |
| 11130 | beg = end; |
| 11131 | |
| 11132 | for (ptr = beg; ptr < end ; ptr++); |
| 11133 | |
| 11134 | if (beg < ptr && *beg == '/') { |
| 11135 | while (beg < ptr) |
| 11136 | hash = *(beg++) + (hash << 6) + (hash << 16) - hash; |
| 11137 | } |
| 11138 | hash = full_hash(hash); |
| 11139 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11140 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11141 | smp->data.u.sint = hash; |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11142 | smp->flags = SMP_F_VOL_1ST; |
| 11143 | return 1; |
| 11144 | } |
| 11145 | |
| 11146 | /* This concatenates the source address with the 32-bit hash of the Host and |
| 11147 | * URL as returned by smp_fetch_base32(). The idea is to have per-source and |
| 11148 | * per-url counters. The result is a binary block from 8 to 20 bytes depending |
| 11149 | * on the source address length. The URL hash is stored before the address so |
| 11150 | * that in environments where IPv6 is insignificant, truncating the output to |
| 11151 | * 8 bytes would still work. |
| 11152 | */ |
| 11153 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11154 | 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] | 11155 | { |
| 11156 | struct chunk *temp; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11157 | struct connection *cli_conn = objt_conn(smp->sess->origin); |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11158 | |
Dragan Dosen | db5af61 | 2016-06-16 11:23:01 +0200 | [diff] [blame] | 11159 | if (!cli_conn) |
| 11160 | return 0; |
| 11161 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11162 | if (!smp_fetch_url32(args, smp, kw, private)) |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11163 | return 0; |
| 11164 | |
| 11165 | temp = get_trash_chunk(); |
Dragan Dosen | e5f4133 | 2016-06-16 11:08:08 +0200 | [diff] [blame] | 11166 | *(unsigned int *)temp->str = htonl(smp->data.u.sint); |
| 11167 | temp->len += sizeof(unsigned int); |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11168 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 11169 | switch (cli_conn->addr.from.ss_family) { |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11170 | case AF_INET: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 11171 | 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] | 11172 | temp->len += 4; |
| 11173 | break; |
| 11174 | case AF_INET6: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 11175 | 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] | 11176 | temp->len += 16; |
| 11177 | break; |
| 11178 | default: |
| 11179 | return 0; |
| 11180 | } |
| 11181 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11182 | smp->data.u.str = *temp; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11183 | smp->data.type = SMP_T_BIN; |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11184 | return 1; |
| 11185 | } |
| 11186 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 11187 | /* This function is used to validate the arguments passed to any "hdr" fetch |
| 11188 | * keyword. These keywords support an optional positive or negative occurrence |
| 11189 | * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It |
| 11190 | * is assumed that the types are already the correct ones. Returns 0 on error, |
| 11191 | * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an |
| 11192 | * error message in case of error, that the caller is responsible for freeing. |
| 11193 | * The initial location must either be freeable or NULL. |
| 11194 | */ |
Thierry FOURNIER | 49f45af | 2014-12-08 19:50:43 +0100 | [diff] [blame] | 11195 | int val_hdr(struct arg *arg, char **err_msg) |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 11196 | { |
| 11197 | 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] | 11198 | memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY); |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 11199 | return 0; |
| 11200 | } |
| 11201 | return 1; |
| 11202 | } |
| 11203 | |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 11204 | /* takes an UINT value on input supposed to represent the time since EPOCH, |
| 11205 | * adds an optional offset found in args[0] and emits a string representing |
| 11206 | * the date in RFC-1123/5322 format. |
| 11207 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11208 | 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] | 11209 | { |
Cyril Bonté | f78d896 | 2016-01-22 19:40:28 +0100 | [diff] [blame] | 11210 | const char day[7][4] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 11211 | const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; |
| 11212 | struct chunk *temp; |
| 11213 | struct tm *tm; |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 11214 | /* 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] | 11215 | time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL; |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 11216 | |
| 11217 | /* add offset */ |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 11218 | if (args && (args[0].type == ARGT_SINT)) |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 11219 | curr_date += args[0].data.sint; |
| 11220 | |
| 11221 | tm = gmtime(&curr_date); |
Thierry FOURNIER | fac9ccf | 2015-07-08 00:15:20 +0200 | [diff] [blame] | 11222 | if (!tm) |
| 11223 | return 0; |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 11224 | |
| 11225 | temp = get_trash_chunk(); |
| 11226 | temp->len = snprintf(temp->str, temp->size - temp->len, |
| 11227 | "%s, %02d %s %04d %02d:%02d:%02d GMT", |
| 11228 | day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year, |
| 11229 | tm->tm_hour, tm->tm_min, tm->tm_sec); |
| 11230 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11231 | smp->data.u.str = *temp; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11232 | smp->data.type = SMP_T_STR; |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 11233 | return 1; |
| 11234 | } |
| 11235 | |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11236 | /* Match language range with language tag. RFC2616 14.4: |
| 11237 | * |
| 11238 | * A language-range matches a language-tag if it exactly equals |
| 11239 | * the tag, or if it exactly equals a prefix of the tag such |
| 11240 | * that the first tag character following the prefix is "-". |
| 11241 | * |
| 11242 | * Return 1 if the strings match, else return 0. |
| 11243 | */ |
| 11244 | static inline int language_range_match(const char *range, int range_len, |
| 11245 | const char *tag, int tag_len) |
| 11246 | { |
| 11247 | const char *end = range + range_len; |
| 11248 | const char *tend = tag + tag_len; |
| 11249 | while (range < end) { |
| 11250 | if (*range == '-' && tag == tend) |
| 11251 | return 1; |
| 11252 | if (*range != *tag || tag == tend) |
| 11253 | return 0; |
| 11254 | range++; |
| 11255 | tag++; |
| 11256 | } |
| 11257 | /* Return true only if the last char of the tag is matched. */ |
| 11258 | return tag == tend; |
| 11259 | } |
| 11260 | |
| 11261 | /* 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] | 11262 | 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] | 11263 | { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11264 | const char *al = smp->data.u.str.str; |
| 11265 | const char *end = al + smp->data.u.str.len; |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11266 | const char *token; |
| 11267 | int toklen; |
| 11268 | int qvalue; |
| 11269 | const char *str; |
| 11270 | const char *w; |
| 11271 | int best_q = 0; |
| 11272 | |
| 11273 | /* Set the constant to the sample, because the output of the |
| 11274 | * function will be peek in the constant configuration string. |
| 11275 | */ |
| 11276 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11277 | smp->data.u.str.size = 0; |
| 11278 | smp->data.u.str.str = ""; |
| 11279 | smp->data.u.str.len = 0; |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11280 | |
| 11281 | /* Parse the accept language */ |
| 11282 | while (1) { |
| 11283 | |
| 11284 | /* Jump spaces, quit if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 11285 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11286 | al++; |
| 11287 | if (al >= end) |
| 11288 | break; |
| 11289 | |
| 11290 | /* Start of the fisrt word. */ |
| 11291 | token = al; |
| 11292 | |
| 11293 | /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 11294 | while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11295 | al++; |
| 11296 | if (al == token) |
| 11297 | goto expect_comma; |
| 11298 | |
| 11299 | /* Length of the token. */ |
| 11300 | toklen = al - token; |
| 11301 | qvalue = 1000; |
| 11302 | |
| 11303 | /* Check if the token exists in the list. If the token not exists, |
| 11304 | * jump to the next token. |
| 11305 | */ |
| 11306 | str = args[0].data.str.str; |
| 11307 | w = str; |
| 11308 | while (1) { |
| 11309 | if (*str == ';' || *str == '\0') { |
| 11310 | if (language_range_match(token, toklen, w, str-w)) |
| 11311 | goto look_for_q; |
| 11312 | if (*str == '\0') |
| 11313 | goto expect_comma; |
| 11314 | w = str + 1; |
| 11315 | } |
| 11316 | str++; |
| 11317 | } |
| 11318 | goto expect_comma; |
| 11319 | |
| 11320 | look_for_q: |
| 11321 | |
| 11322 | /* Jump spaces, quit if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 11323 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11324 | al++; |
| 11325 | if (al >= end) |
| 11326 | goto process_value; |
| 11327 | |
| 11328 | /* If ',' is found, process the result */ |
| 11329 | if (*al == ',') |
| 11330 | goto process_value; |
| 11331 | |
| 11332 | /* If the character is different from ';', look |
| 11333 | * for the end of the header part in best effort. |
| 11334 | */ |
| 11335 | if (*al != ';') |
| 11336 | goto expect_comma; |
| 11337 | |
| 11338 | /* Assumes that the char is ';', now expect "q=". */ |
| 11339 | al++; |
| 11340 | |
| 11341 | /* Jump spaces, process value if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 11342 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11343 | al++; |
| 11344 | if (al >= end) |
| 11345 | goto process_value; |
| 11346 | |
| 11347 | /* Expect 'q'. If no 'q', continue in best effort */ |
| 11348 | if (*al != 'q') |
| 11349 | goto process_value; |
| 11350 | al++; |
| 11351 | |
| 11352 | /* Jump spaces, process value if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 11353 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11354 | al++; |
| 11355 | if (al >= end) |
| 11356 | goto process_value; |
| 11357 | |
| 11358 | /* Expect '='. If no '=', continue in best effort */ |
| 11359 | if (*al != '=') |
| 11360 | goto process_value; |
| 11361 | al++; |
| 11362 | |
| 11363 | /* Jump spaces, process value if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 11364 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11365 | al++; |
| 11366 | if (al >= end) |
| 11367 | goto process_value; |
| 11368 | |
| 11369 | /* Parse the q value. */ |
| 11370 | qvalue = parse_qvalue(al, &al); |
| 11371 | |
| 11372 | process_value: |
| 11373 | |
| 11374 | /* If the new q value is the best q value, then store the associated |
| 11375 | * language in the response. If qvalue is the biggest value (1000), |
| 11376 | * break the process. |
| 11377 | */ |
| 11378 | if (qvalue > best_q) { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11379 | smp->data.u.str.str = (char *)w; |
| 11380 | smp->data.u.str.len = str - w; |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11381 | if (qvalue >= 1000) |
| 11382 | break; |
| 11383 | best_q = qvalue; |
| 11384 | } |
| 11385 | |
| 11386 | expect_comma: |
| 11387 | |
| 11388 | /* Expect comma or end. If the end is detected, quit the loop. */ |
| 11389 | while (al < end && *al != ',') |
| 11390 | al++; |
| 11391 | if (al >= end) |
| 11392 | break; |
| 11393 | |
| 11394 | /* Comma is found, jump it and restart the analyzer. */ |
| 11395 | al++; |
| 11396 | } |
| 11397 | |
| 11398 | /* Set default value if required. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11399 | if (smp->data.u.str.len == 0 && args[1].type == ARGT_STR) { |
| 11400 | smp->data.u.str.str = args[1].data.str.str; |
| 11401 | smp->data.u.str.len = args[1].data.str.len; |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11402 | } |
| 11403 | |
| 11404 | /* Return true only if a matching language was found. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11405 | return smp->data.u.str.len != 0; |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11406 | } |
| 11407 | |
Thierry FOURNIER | 82ff3c9 | 2015-05-07 15:46:20 +0200 | [diff] [blame] | 11408 | /* This fetch url-decode any input string. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11409 | 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] | 11410 | { |
| 11411 | /* If the constant flag is set or if not size is avalaible at |
| 11412 | * the end of the buffer, copy the string in other buffer |
| 11413 | * before decoding. |
| 11414 | */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11415 | 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] | 11416 | struct chunk *str = get_trash_chunk(); |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11417 | memcpy(str->str, smp->data.u.str.str, smp->data.u.str.len); |
| 11418 | smp->data.u.str.str = str->str; |
| 11419 | smp->data.u.str.size = str->size; |
Thierry FOURNIER | 82ff3c9 | 2015-05-07 15:46:20 +0200 | [diff] [blame] | 11420 | smp->flags &= ~SMP_F_CONST; |
| 11421 | } |
| 11422 | |
| 11423 | /* Add final \0 required by url_decode(), and convert the input string. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11424 | smp->data.u.str.str[smp->data.u.str.len] = '\0'; |
| 11425 | smp->data.u.str.len = url_decode(smp->data.u.str.str); |
Christopher Faulet | a258479 | 2017-10-05 10:03:12 +0200 | [diff] [blame] | 11426 | return (smp->data.u.str.len >= 0); |
Thierry FOURNIER | 82ff3c9 | 2015-05-07 15:46:20 +0200 | [diff] [blame] | 11427 | } |
| 11428 | |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 11429 | static int smp_conv_req_capture(const struct arg *args, struct sample *smp, void *private) |
| 11430 | { |
| 11431 | struct proxy *fe = strm_fe(smp->strm); |
| 11432 | int idx, i; |
| 11433 | struct cap_hdr *hdr; |
| 11434 | int len; |
| 11435 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 11436 | if (!args || args->type != ARGT_SINT) |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 11437 | return 0; |
| 11438 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 11439 | idx = args->data.sint; |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 11440 | |
| 11441 | /* Check the availibity of the capture id. */ |
| 11442 | if (idx > fe->nb_req_cap - 1) |
| 11443 | return 0; |
| 11444 | |
| 11445 | /* Look for the original configuration. */ |
| 11446 | for (hdr = fe->req_cap, i = fe->nb_req_cap - 1; |
| 11447 | hdr != NULL && i != idx ; |
| 11448 | i--, hdr = hdr->next); |
| 11449 | if (!hdr) |
| 11450 | return 0; |
| 11451 | |
| 11452 | /* check for the memory allocation */ |
| 11453 | if (smp->strm->req_cap[hdr->index] == NULL) |
| 11454 | smp->strm->req_cap[hdr->index] = pool_alloc2(hdr->pool); |
| 11455 | if (smp->strm->req_cap[hdr->index] == NULL) |
| 11456 | return 0; |
| 11457 | |
| 11458 | /* Check length. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11459 | len = smp->data.u.str.len; |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 11460 | if (len > hdr->len) |
| 11461 | len = hdr->len; |
| 11462 | |
| 11463 | /* Capture input data. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11464 | memcpy(smp->strm->req_cap[idx], smp->data.u.str.str, len); |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 11465 | smp->strm->req_cap[idx][len] = '\0'; |
| 11466 | |
| 11467 | return 1; |
| 11468 | } |
| 11469 | |
| 11470 | static int smp_conv_res_capture(const struct arg *args, struct sample *smp, void *private) |
| 11471 | { |
| 11472 | struct proxy *fe = strm_fe(smp->strm); |
| 11473 | int idx, i; |
| 11474 | struct cap_hdr *hdr; |
| 11475 | int len; |
| 11476 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 11477 | if (!args || args->type != ARGT_SINT) |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 11478 | return 0; |
| 11479 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 11480 | idx = args->data.sint; |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 11481 | |
| 11482 | /* Check the availibity of the capture id. */ |
| 11483 | if (idx > fe->nb_rsp_cap - 1) |
| 11484 | return 0; |
| 11485 | |
| 11486 | /* Look for the original configuration. */ |
| 11487 | for (hdr = fe->rsp_cap, i = fe->nb_rsp_cap - 1; |
| 11488 | hdr != NULL && i != idx ; |
| 11489 | i--, hdr = hdr->next); |
| 11490 | if (!hdr) |
| 11491 | return 0; |
| 11492 | |
| 11493 | /* check for the memory allocation */ |
| 11494 | if (smp->strm->res_cap[hdr->index] == NULL) |
| 11495 | smp->strm->res_cap[hdr->index] = pool_alloc2(hdr->pool); |
| 11496 | if (smp->strm->res_cap[hdr->index] == NULL) |
| 11497 | return 0; |
| 11498 | |
| 11499 | /* Check length. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11500 | len = smp->data.u.str.len; |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 11501 | if (len > hdr->len) |
| 11502 | len = hdr->len; |
| 11503 | |
| 11504 | /* Capture input data. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11505 | memcpy(smp->strm->res_cap[idx], smp->data.u.str.str, len); |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 11506 | smp->strm->res_cap[idx][len] = '\0'; |
| 11507 | |
| 11508 | return 1; |
| 11509 | } |
| 11510 | |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11511 | /* 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] | 11512 | * takes the string from the variable 'replace' with length 'len', then modifies |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11513 | * the relevant part of the request line accordingly. Then it updates various |
| 11514 | * pointers to the next elements which were moved, and the total buffer length. |
| 11515 | * 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] | 11516 | * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal |
| 11517 | * error, though this can be revisited when this code is finally exploited. |
| 11518 | * |
| 11519 | * 'action' can be '0' to replace method, '1' to replace path, '2' to replace |
| 11520 | * query string and 3 to replace uri. |
| 11521 | * |
| 11522 | * In query string case, the mark question '?' must be set at the start of the |
| 11523 | * string by the caller, event if the replacement query string is empty. |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11524 | */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11525 | int http_replace_req_line(int action, const char *replace, int len, |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 11526 | struct proxy *px, struct stream *s) |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11527 | { |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 11528 | struct http_txn *txn = s->txn; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11529 | char *cur_ptr, *cur_end; |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11530 | int offset = 0; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11531 | int delta; |
| 11532 | |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11533 | switch (action) { |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11534 | case 0: // method |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 11535 | cur_ptr = s->req.buf->p; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11536 | cur_end = cur_ptr + txn->req.sl.rq.m_l; |
| 11537 | |
| 11538 | /* adjust req line offsets and lengths */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11539 | delta = len - offset - (cur_end - cur_ptr); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11540 | txn->req.sl.rq.m_l += delta; |
| 11541 | txn->req.sl.rq.u += delta; |
| 11542 | txn->req.sl.rq.v += delta; |
| 11543 | break; |
| 11544 | |
| 11545 | case 1: // path |
| 11546 | cur_ptr = http_get_path(txn); |
| 11547 | if (!cur_ptr) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 11548 | cur_ptr = s->req.buf->p + txn->req.sl.rq.u; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11549 | |
| 11550 | cur_end = cur_ptr; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 11551 | 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] | 11552 | cur_end++; |
| 11553 | |
| 11554 | /* adjust req line offsets and lengths */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11555 | delta = len - offset - (cur_end - cur_ptr); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11556 | txn->req.sl.rq.u_l += delta; |
| 11557 | txn->req.sl.rq.v += delta; |
| 11558 | break; |
| 11559 | |
| 11560 | case 2: // query |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11561 | offset = 1; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 11562 | cur_ptr = s->req.buf->p + txn->req.sl.rq.u; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11563 | cur_end = cur_ptr + txn->req.sl.rq.u_l; |
| 11564 | while (cur_ptr < cur_end && *cur_ptr != '?') |
| 11565 | cur_ptr++; |
| 11566 | |
| 11567 | /* skip the question mark or indicate that we must insert it |
| 11568 | * (but only if the format string is not empty then). |
| 11569 | */ |
| 11570 | if (cur_ptr < cur_end) |
| 11571 | cur_ptr++; |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11572 | else if (len > 1) |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11573 | offset = 0; |
| 11574 | |
| 11575 | /* adjust req line offsets and lengths */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11576 | delta = len - offset - (cur_end - cur_ptr); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11577 | txn->req.sl.rq.u_l += delta; |
| 11578 | txn->req.sl.rq.v += delta; |
| 11579 | break; |
| 11580 | |
| 11581 | case 3: // uri |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 11582 | cur_ptr = s->req.buf->p + txn->req.sl.rq.u; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11583 | cur_end = cur_ptr + txn->req.sl.rq.u_l; |
| 11584 | |
| 11585 | /* adjust req line offsets and lengths */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11586 | delta = len - offset - (cur_end - cur_ptr); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11587 | txn->req.sl.rq.u_l += delta; |
| 11588 | txn->req.sl.rq.v += delta; |
| 11589 | break; |
| 11590 | |
| 11591 | default: |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11592 | return -1; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11593 | } |
| 11594 | |
| 11595 | /* commit changes and adjust end of message */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11596 | 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] | 11597 | txn->req.sl.rq.l += delta; |
| 11598 | txn->hdr_idx.v[0].len += delta; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11599 | http_msg_move_end(&txn->req, delta); |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11600 | return 0; |
| 11601 | } |
| 11602 | |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 11603 | /* This function replace the HTTP status code and the associated message. The |
| 11604 | * variable <status> contains the new status code. This function never fails. |
| 11605 | */ |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 11606 | 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] | 11607 | { |
| 11608 | struct http_txn *txn = s->txn; |
| 11609 | char *cur_ptr, *cur_end; |
| 11610 | int delta; |
| 11611 | char *res; |
| 11612 | int c_l; |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 11613 | const char *msg = reason; |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 11614 | int msg_len; |
| 11615 | |
| 11616 | chunk_reset(&trash); |
| 11617 | |
| 11618 | res = ultoa_o(status, trash.str, trash.size); |
| 11619 | c_l = res - trash.str; |
| 11620 | |
| 11621 | trash.str[c_l] = ' '; |
| 11622 | trash.len = c_l + 1; |
| 11623 | |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 11624 | /* Do we have a custom reason format string? */ |
| 11625 | if (msg == NULL) |
| 11626 | msg = get_reason(status); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 11627 | msg_len = strlen(msg); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 11628 | strncpy(&trash.str[trash.len], msg, trash.size - trash.len); |
| 11629 | trash.len += msg_len; |
| 11630 | |
| 11631 | cur_ptr = s->res.buf->p + txn->rsp.sl.st.c; |
| 11632 | cur_end = s->res.buf->p + txn->rsp.sl.st.r + txn->rsp.sl.st.r_l; |
| 11633 | |
| 11634 | /* commit changes and adjust message */ |
| 11635 | delta = buffer_replace2(s->res.buf, cur_ptr, cur_end, trash.str, trash.len); |
| 11636 | |
| 11637 | /* adjust res line offsets and lengths */ |
| 11638 | txn->rsp.sl.st.r += c_l - txn->rsp.sl.st.c_l; |
| 11639 | txn->rsp.sl.st.c_l = c_l; |
| 11640 | txn->rsp.sl.st.r_l = msg_len; |
| 11641 | |
| 11642 | delta = trash.len - (cur_end - cur_ptr); |
| 11643 | txn->rsp.sl.st.l += delta; |
| 11644 | txn->hdr_idx.v[0].len += delta; |
| 11645 | http_msg_move_end(&txn->rsp, delta); |
| 11646 | } |
| 11647 | |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11648 | /* This function executes one of the set-{method,path,query,uri} actions. It |
| 11649 | * 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] | 11650 | * the action to be performed in <http.action>, previously filled by function |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11651 | * parse_set_req_line(). The replacement action is excuted by the function |
Thierry FOURNIER | 3f4bc65 | 2015-08-26 16:23:34 +0200 | [diff] [blame] | 11652 | * http_action_set_req_line(). It always returns ACT_RET_CONT. If an error |
| 11653 | * occurs the action is canceled, but the rule processing continue. |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11654 | */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 11655 | 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] | 11656 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11657 | { |
| 11658 | chunk_reset(&trash); |
| 11659 | |
| 11660 | /* If we have to create a query string, prepare a '?'. */ |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 11661 | if (rule->arg.http.action == 2) |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11662 | trash.str[trash.len++] = '?'; |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 11663 | trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.http.logfmt); |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 11664 | |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 11665 | http_replace_req_line(rule->arg.http.action, trash.str, trash.len, px, s); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 11666 | return ACT_RET_CONT; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11667 | } |
| 11668 | |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 11669 | /* This function is just a compliant action wrapper for "set-status". */ |
| 11670 | 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] | 11671 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 11672 | { |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 11673 | http_set_status(rule->arg.status.code, rule->arg.status.reason, s); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 11674 | return ACT_RET_CONT; |
| 11675 | } |
| 11676 | |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11677 | /* parse an http-request action among : |
| 11678 | * set-method |
| 11679 | * set-path |
| 11680 | * set-query |
| 11681 | * set-uri |
| 11682 | * |
| 11683 | * All of them accept a single argument of type string representing a log-format. |
| 11684 | * The resulting rule makes use of arg->act.p[0..1] to store the log-format list |
| 11685 | * 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] | 11686 | * 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] | 11687 | */ |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 11688 | enum act_parse_ret parse_set_req_line(const char **args, int *orig_arg, struct proxy *px, |
| 11689 | struct act_rule *rule, char **err) |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11690 | { |
| 11691 | int cur_arg = *orig_arg; |
| 11692 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 11693 | rule->action = ACT_CUSTOM; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11694 | |
| 11695 | switch (args[0][4]) { |
| 11696 | case 'm' : |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 11697 | rule->arg.http.action = 0; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11698 | rule->action_ptr = http_action_set_req_line; |
| 11699 | break; |
| 11700 | case 'p' : |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 11701 | rule->arg.http.action = 1; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11702 | rule->action_ptr = http_action_set_req_line; |
| 11703 | break; |
| 11704 | case 'q' : |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 11705 | rule->arg.http.action = 2; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11706 | rule->action_ptr = http_action_set_req_line; |
| 11707 | break; |
| 11708 | case 'u' : |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 11709 | rule->arg.http.action = 3; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11710 | rule->action_ptr = http_action_set_req_line; |
| 11711 | break; |
| 11712 | default: |
| 11713 | memprintf(err, "internal error: unhandled action '%s'", args[0]); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 11714 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11715 | } |
| 11716 | |
| 11717 | if (!*args[cur_arg] || |
| 11718 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 11719 | memprintf(err, "expects exactly 1 argument <format>"); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 11720 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11721 | } |
| 11722 | |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 11723 | LIST_INIT(&rule->arg.http.logfmt); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11724 | proxy->conf.args.ctx = ARGC_HRQ; |
Thierry FOURNIER / OZON.IO | 59fd511 | 2016-11-22 23:50:02 +0100 | [diff] [blame] | 11725 | 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] | 11726 | (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] | 11727 | return ACT_RET_PRS_ERR; |
| 11728 | } |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11729 | |
| 11730 | (*orig_arg)++; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 11731 | return ACT_RET_PRS_OK; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 11732 | } |
| 11733 | |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 11734 | /* parse set-status action: |
| 11735 | * This action accepts a single argument of type int representing |
| 11736 | * an http status code. It returns ACT_RET_PRS_OK on success, |
| 11737 | * ACT_RET_PRS_ERR on error. |
| 11738 | */ |
| 11739 | enum act_parse_ret parse_http_set_status(const char **args, int *orig_arg, struct proxy *px, |
| 11740 | struct act_rule *rule, char **err) |
| 11741 | { |
| 11742 | char *error; |
| 11743 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 11744 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 11745 | rule->action_ptr = action_http_set_status; |
| 11746 | |
| 11747 | /* Check if an argument is available */ |
| 11748 | if (!*args[*orig_arg]) { |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 11749 | memprintf(err, "expects 1 argument: <status>; or 3 arguments: <status> reason <fmt>"); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 11750 | return ACT_RET_PRS_ERR; |
| 11751 | } |
| 11752 | |
| 11753 | /* convert status code as integer */ |
| 11754 | rule->arg.status.code = strtol(args[*orig_arg], &error, 10); |
| 11755 | if (*error != '\0' || rule->arg.status.code < 100 || rule->arg.status.code > 999) { |
| 11756 | memprintf(err, "expects an integer status code between 100 and 999"); |
| 11757 | return ACT_RET_PRS_ERR; |
| 11758 | } |
| 11759 | |
| 11760 | (*orig_arg)++; |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 11761 | |
| 11762 | /* set custom reason string */ |
| 11763 | rule->arg.status.reason = NULL; // If null, we use the default reason for the status code. |
| 11764 | if (*args[*orig_arg] && strcmp(args[*orig_arg], "reason") == 0 && |
| 11765 | (*args[*orig_arg + 1] && strcmp(args[*orig_arg + 1], "if") != 0 && strcmp(args[*orig_arg + 1], "unless") != 0)) { |
| 11766 | (*orig_arg)++; |
| 11767 | rule->arg.status.reason = strdup(args[*orig_arg]); |
| 11768 | (*orig_arg)++; |
| 11769 | } |
| 11770 | |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 11771 | return ACT_RET_PRS_OK; |
| 11772 | } |
| 11773 | |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 11774 | /* This function executes the "capture" action. It executes a fetch expression, |
| 11775 | * turns the result into a string and puts it in a capture slot. It always |
| 11776 | * returns 1. If an error occurs the action is cancelled, but the rule |
| 11777 | * processing continues. |
| 11778 | */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 11779 | 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] | 11780 | struct session *sess, struct stream *s, int flags) |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 11781 | { |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 11782 | struct sample *key; |
Thierry FOURNIER | 32b1500 | 2015-07-31 08:56:16 +0200 | [diff] [blame] | 11783 | struct cap_hdr *h = rule->arg.cap.hdr; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 11784 | char **cap = s->req_cap; |
| 11785 | int len; |
| 11786 | |
Thierry FOURNIER | 32b1500 | 2015-07-31 08:56:16 +0200 | [diff] [blame] | 11787 | 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] | 11788 | if (!key) |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 11789 | return ACT_RET_CONT; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 11790 | |
| 11791 | if (cap[h->index] == NULL) |
| 11792 | cap[h->index] = pool_alloc2(h->pool); |
| 11793 | |
| 11794 | if (cap[h->index] == NULL) /* no more capture memory */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 11795 | return ACT_RET_CONT; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 11796 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11797 | len = key->data.u.str.len; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 11798 | if (len > h->len) |
| 11799 | len = h->len; |
| 11800 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11801 | memcpy(cap[h->index], key->data.u.str.str, len); |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 11802 | cap[h->index][len] = 0; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 11803 | return ACT_RET_CONT; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 11804 | } |
| 11805 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 11806 | /* This function executes the "capture" action and store the result in a |
| 11807 | * capture slot if exists. It executes a fetch expression, turns the result |
| 11808 | * into a string and puts it in a capture slot. It always returns 1. If an |
| 11809 | * error occurs the action is cancelled, but the rule processing continues. |
| 11810 | */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 11811 | 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] | 11812 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 11813 | { |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 11814 | struct sample *key; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 11815 | struct cap_hdr *h; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 11816 | char **cap = s->req_cap; |
| 11817 | struct proxy *fe = strm_fe(s); |
| 11818 | int len; |
| 11819 | int i; |
| 11820 | |
| 11821 | /* Look for the original configuration. */ |
| 11822 | for (h = fe->req_cap, i = fe->nb_req_cap - 1; |
Thierry FOURNIER | e209797 | 2015-07-31 08:56:35 +0200 | [diff] [blame] | 11823 | h != NULL && i != rule->arg.capid.idx ; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 11824 | i--, h = h->next); |
| 11825 | if (!h) |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 11826 | return ACT_RET_CONT; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 11827 | |
Thierry FOURNIER | e209797 | 2015-07-31 08:56:35 +0200 | [diff] [blame] | 11828 | 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] | 11829 | if (!key) |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 11830 | return ACT_RET_CONT; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 11831 | |
| 11832 | if (cap[h->index] == NULL) |
| 11833 | cap[h->index] = pool_alloc2(h->pool); |
| 11834 | |
| 11835 | if (cap[h->index] == NULL) /* no more capture memory */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 11836 | return ACT_RET_CONT; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 11837 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11838 | len = key->data.u.str.len; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 11839 | if (len > h->len) |
| 11840 | len = h->len; |
| 11841 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11842 | memcpy(cap[h->index], key->data.u.str.str, len); |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 11843 | cap[h->index][len] = 0; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 11844 | return ACT_RET_CONT; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 11845 | } |
| 11846 | |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 11847 | /* parse an "http-request capture" action. It takes a single argument which is |
| 11848 | * 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] | 11849 | * 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] | 11850 | * 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] | 11851 | */ |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 11852 | enum act_parse_ret parse_http_req_capture(const char **args, int *orig_arg, struct proxy *px, |
| 11853 | struct act_rule *rule, char **err) |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 11854 | { |
| 11855 | struct sample_expr *expr; |
| 11856 | struct cap_hdr *hdr; |
| 11857 | int cur_arg; |
Willy Tarreau | 3986ac1 | 2015-05-08 16:13:42 +0200 | [diff] [blame] | 11858 | int len = 0; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 11859 | |
| 11860 | for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++) |
| 11861 | if (strcmp(args[cur_arg], "if") == 0 || |
| 11862 | strcmp(args[cur_arg], "unless") == 0) |
| 11863 | break; |
| 11864 | |
| 11865 | if (cur_arg < *orig_arg + 3) { |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 11866 | memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]"); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 11867 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 11868 | } |
| 11869 | |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 11870 | cur_arg = *orig_arg; |
| 11871 | expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args); |
| 11872 | if (!expr) |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 11873 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 11874 | |
| 11875 | if (!(expr->fetch->val & SMP_VAL_FE_HRQ_HDR)) { |
| 11876 | memprintf(err, |
| 11877 | "fetch method '%s' extracts information from '%s', none of which is available here", |
| 11878 | args[cur_arg-1], sample_src_names(expr->fetch->use)); |
| 11879 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 11880 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 11881 | } |
| 11882 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 11883 | if (!args[cur_arg] || !*args[cur_arg]) { |
| 11884 | memprintf(err, "expects 'len or 'id'"); |
| 11885 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 11886 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 11887 | } |
| 11888 | |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 11889 | if (strcmp(args[cur_arg], "len") == 0) { |
| 11890 | cur_arg++; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 11891 | |
| 11892 | if (!(px->cap & PR_CAP_FE)) { |
| 11893 | memprintf(err, "proxy '%s' has no frontend capability", px->id); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 11894 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 11895 | } |
| 11896 | |
| 11897 | proxy->conf.args.ctx = ARGC_CAP; |
| 11898 | |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 11899 | if (!args[cur_arg]) { |
| 11900 | memprintf(err, "missing length value"); |
| 11901 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 11902 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 11903 | } |
| 11904 | /* we copy the table name for now, it will be resolved later */ |
| 11905 | len = atoi(args[cur_arg]); |
| 11906 | if (len <= 0) { |
| 11907 | memprintf(err, "length must be > 0"); |
| 11908 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 11909 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 11910 | } |
| 11911 | cur_arg++; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 11912 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 11913 | if (!len) { |
| 11914 | memprintf(err, "a positive 'len' argument is mandatory"); |
| 11915 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 11916 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 11917 | } |
| 11918 | |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 11919 | hdr = calloc(1, sizeof(*hdr)); |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 11920 | hdr->next = px->req_cap; |
| 11921 | hdr->name = NULL; /* not a header capture */ |
| 11922 | hdr->namelen = 0; |
| 11923 | hdr->len = len; |
| 11924 | hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED); |
| 11925 | hdr->index = px->nb_req_cap++; |
| 11926 | |
| 11927 | px->req_cap = hdr; |
| 11928 | px->to_log |= LW_REQHDR; |
| 11929 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 11930 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 11931 | rule->action_ptr = http_action_req_capture; |
Thierry FOURNIER | 32b1500 | 2015-07-31 08:56:16 +0200 | [diff] [blame] | 11932 | rule->arg.cap.expr = expr; |
| 11933 | rule->arg.cap.hdr = hdr; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 11934 | } |
| 11935 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 11936 | else if (strcmp(args[cur_arg], "id") == 0) { |
| 11937 | int id; |
| 11938 | char *error; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 11939 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 11940 | cur_arg++; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 11941 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 11942 | if (!args[cur_arg]) { |
| 11943 | memprintf(err, "missing id value"); |
| 11944 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 11945 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 11946 | } |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 11947 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 11948 | id = strtol(args[cur_arg], &error, 10); |
| 11949 | if (*error != '\0') { |
| 11950 | memprintf(err, "cannot parse id '%s'", args[cur_arg]); |
| 11951 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 11952 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 11953 | } |
| 11954 | cur_arg++; |
| 11955 | |
| 11956 | proxy->conf.args.ctx = ARGC_CAP; |
| 11957 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 11958 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 11959 | rule->action_ptr = http_action_req_capture_by_id; |
Thierry FOURNIER | e209797 | 2015-07-31 08:56:35 +0200 | [diff] [blame] | 11960 | rule->arg.capid.expr = expr; |
| 11961 | rule->arg.capid.idx = id; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 11962 | } |
| 11963 | |
| 11964 | else { |
| 11965 | memprintf(err, "expects 'len' or 'id', found '%s'", args[cur_arg]); |
| 11966 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 11967 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 11968 | } |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 11969 | |
| 11970 | *orig_arg = cur_arg; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 11971 | return ACT_RET_PRS_OK; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 11972 | } |
| 11973 | |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 11974 | /* This function executes the "capture" action and store the result in a |
| 11975 | * capture slot if exists. It executes a fetch expression, turns the result |
| 11976 | * into a string and puts it in a capture slot. It always returns 1. If an |
| 11977 | * error occurs the action is cancelled, but the rule processing continues. |
| 11978 | */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 11979 | 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] | 11980 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 11981 | { |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 11982 | struct sample *key; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 11983 | struct cap_hdr *h; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 11984 | char **cap = s->res_cap; |
| 11985 | struct proxy *fe = strm_fe(s); |
| 11986 | int len; |
| 11987 | int i; |
| 11988 | |
| 11989 | /* Look for the original configuration. */ |
| 11990 | for (h = fe->rsp_cap, i = fe->nb_rsp_cap - 1; |
Thierry FOURNIER | e209797 | 2015-07-31 08:56:35 +0200 | [diff] [blame] | 11991 | h != NULL && i != rule->arg.capid.idx ; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 11992 | i--, h = h->next); |
| 11993 | if (!h) |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 11994 | return ACT_RET_CONT; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 11995 | |
Thierry FOURNIER | e209797 | 2015-07-31 08:56:35 +0200 | [diff] [blame] | 11996 | 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] | 11997 | if (!key) |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 11998 | return ACT_RET_CONT; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 11999 | |
| 12000 | if (cap[h->index] == NULL) |
| 12001 | cap[h->index] = pool_alloc2(h->pool); |
| 12002 | |
| 12003 | if (cap[h->index] == NULL) /* no more capture memory */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12004 | return ACT_RET_CONT; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12005 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12006 | len = key->data.u.str.len; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12007 | if (len > h->len) |
| 12008 | len = h->len; |
| 12009 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12010 | memcpy(cap[h->index], key->data.u.str.str, len); |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12011 | cap[h->index][len] = 0; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12012 | return ACT_RET_CONT; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12013 | } |
| 12014 | |
| 12015 | /* parse an "http-response capture" action. It takes a single argument which is |
| 12016 | * a sample fetch expression. It stores the expression into arg->act.p[0] and |
| 12017 | * 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] | 12018 | * 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] | 12019 | */ |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12020 | enum act_parse_ret parse_http_res_capture(const char **args, int *orig_arg, struct proxy *px, |
| 12021 | struct act_rule *rule, char **err) |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12022 | { |
| 12023 | struct sample_expr *expr; |
| 12024 | int cur_arg; |
| 12025 | int id; |
| 12026 | char *error; |
| 12027 | |
| 12028 | for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++) |
| 12029 | if (strcmp(args[cur_arg], "if") == 0 || |
| 12030 | strcmp(args[cur_arg], "unless") == 0) |
| 12031 | break; |
| 12032 | |
| 12033 | if (cur_arg < *orig_arg + 3) { |
Willy Tarreau | 29bdb1c | 2016-06-24 15:36:34 +0200 | [diff] [blame] | 12034 | memprintf(err, "expects <expression> id <idx>"); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12035 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12036 | } |
| 12037 | |
| 12038 | cur_arg = *orig_arg; |
| 12039 | expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args); |
| 12040 | if (!expr) |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12041 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12042 | |
| 12043 | if (!(expr->fetch->val & SMP_VAL_FE_HRS_HDR)) { |
| 12044 | memprintf(err, |
| 12045 | "fetch method '%s' extracts information from '%s', none of which is available here", |
| 12046 | args[cur_arg-1], sample_src_names(expr->fetch->use)); |
| 12047 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12048 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12049 | } |
| 12050 | |
| 12051 | if (!args[cur_arg] || !*args[cur_arg]) { |
Willy Tarreau | 29bdb1c | 2016-06-24 15:36:34 +0200 | [diff] [blame] | 12052 | memprintf(err, "expects 'id'"); |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12053 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12054 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12055 | } |
| 12056 | |
| 12057 | if (strcmp(args[cur_arg], "id") != 0) { |
| 12058 | memprintf(err, "expects 'id', found '%s'", args[cur_arg]); |
| 12059 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12060 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12061 | } |
| 12062 | |
| 12063 | cur_arg++; |
| 12064 | |
| 12065 | if (!args[cur_arg]) { |
| 12066 | memprintf(err, "missing id value"); |
| 12067 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12068 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12069 | } |
| 12070 | |
| 12071 | id = strtol(args[cur_arg], &error, 10); |
| 12072 | if (*error != '\0') { |
| 12073 | memprintf(err, "cannot parse id '%s'", args[cur_arg]); |
| 12074 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12075 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12076 | } |
| 12077 | cur_arg++; |
| 12078 | |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12079 | proxy->conf.args.ctx = ARGC_CAP; |
| 12080 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 12081 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12082 | rule->action_ptr = http_action_res_capture_by_id; |
Thierry FOURNIER | e209797 | 2015-07-31 08:56:35 +0200 | [diff] [blame] | 12083 | rule->arg.capid.expr = expr; |
| 12084 | rule->arg.capid.idx = id; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12085 | |
| 12086 | *orig_arg = cur_arg; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12087 | return ACT_RET_PRS_OK; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12088 | } |
| 12089 | |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 12090 | /* |
| 12091 | * Return the struct http_req_action_kw associated to a keyword. |
| 12092 | */ |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 12093 | struct action_kw *action_http_req_custom(const char *kw) |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 12094 | { |
Thierry FOURNIER | 322a124 | 2015-08-19 09:07:47 +0200 | [diff] [blame] | 12095 | return action_lookup(&http_req_keywords.list, kw); |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 12096 | } |
| 12097 | |
| 12098 | /* |
| 12099 | * Return the struct http_res_action_kw associated to a keyword. |
| 12100 | */ |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 12101 | struct action_kw *action_http_res_custom(const char *kw) |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 12102 | { |
Thierry FOURNIER | 322a124 | 2015-08-19 09:07:47 +0200 | [diff] [blame] | 12103 | return action_lookup(&http_res_keywords.list, kw); |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 12104 | } |
| 12105 | |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12106 | |
| 12107 | /* "show errors" handler for the CLI. Returns 0 if wants to continue, 1 to stop |
| 12108 | * now. |
| 12109 | */ |
| 12110 | static int cli_parse_show_errors(char **args, struct appctx *appctx, void *private) |
| 12111 | { |
| 12112 | if (!cli_has_level(appctx, ACCESS_LVL_OPER)) |
| 12113 | return 1; |
| 12114 | |
Willy Tarreau | 234ba2d | 2016-11-25 08:39:10 +0100 | [diff] [blame] | 12115 | if (*args[2]) { |
| 12116 | struct proxy *px; |
| 12117 | |
| 12118 | px = proxy_find_by_name(args[2], 0, 0); |
| 12119 | if (px) |
| 12120 | appctx->ctx.errors.iid = px->uuid; |
| 12121 | else |
| 12122 | appctx->ctx.errors.iid = atoi(args[2]); |
| 12123 | |
| 12124 | if (!appctx->ctx.errors.iid) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 12125 | appctx->ctx.cli.severity = LOG_ERR; |
Willy Tarreau | 234ba2d | 2016-11-25 08:39:10 +0100 | [diff] [blame] | 12126 | appctx->ctx.cli.msg = "No such proxy.\n"; |
| 12127 | appctx->st0 = CLI_ST_PRINT; |
| 12128 | return 1; |
| 12129 | } |
| 12130 | } |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12131 | else |
Willy Tarreau | 234ba2d | 2016-11-25 08:39:10 +0100 | [diff] [blame] | 12132 | appctx->ctx.errors.iid = -1; // dump all proxies |
| 12133 | |
Willy Tarreau | 35069f8 | 2016-11-25 09:16:37 +0100 | [diff] [blame] | 12134 | appctx->ctx.errors.flag = 0; |
| 12135 | if (strcmp(args[3], "request") == 0) |
| 12136 | appctx->ctx.errors.flag |= 4; // ignore response |
| 12137 | else if (strcmp(args[3], "response") == 0) |
| 12138 | appctx->ctx.errors.flag |= 2; // ignore request |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12139 | appctx->ctx.errors.px = NULL; |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12140 | return 0; |
| 12141 | } |
| 12142 | |
| 12143 | /* This function dumps all captured errors onto the stream interface's |
| 12144 | * read buffer. It returns 0 if the output buffer is full and it needs |
| 12145 | * to be called again, otherwise non-zero. |
| 12146 | */ |
| 12147 | static int cli_io_handler_show_errors(struct appctx *appctx) |
| 12148 | { |
| 12149 | struct stream_interface *si = appctx->owner; |
| 12150 | extern const char *monthname[12]; |
| 12151 | |
| 12152 | if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) |
| 12153 | return 1; |
| 12154 | |
| 12155 | chunk_reset(&trash); |
| 12156 | |
| 12157 | if (!appctx->ctx.errors.px) { |
| 12158 | /* the function had not been called yet, let's prepare the |
| 12159 | * buffer for a response. |
| 12160 | */ |
| 12161 | struct tm tm; |
| 12162 | |
| 12163 | get_localtime(date.tv_sec, &tm); |
| 12164 | chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n", |
| 12165 | tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900, |
| 12166 | tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000), |
| 12167 | error_snapshot_id); |
| 12168 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 12169 | if (ci_putchk(si_ic(si), &trash) == -1) { |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12170 | /* Socket buffer full. Let's try again later from the same point */ |
| 12171 | si_applet_cant_put(si); |
| 12172 | return 0; |
| 12173 | } |
| 12174 | |
| 12175 | appctx->ctx.errors.px = proxy; |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12176 | appctx->ctx.errors.bol = 0; |
| 12177 | appctx->ctx.errors.ptr = -1; |
| 12178 | } |
| 12179 | |
| 12180 | /* we have two inner loops here, one for the proxy, the other one for |
| 12181 | * the buffer. |
| 12182 | */ |
| 12183 | while (appctx->ctx.errors.px) { |
| 12184 | struct error_snapshot *es; |
| 12185 | |
Willy Tarreau | 35069f8 | 2016-11-25 09:16:37 +0100 | [diff] [blame] | 12186 | if ((appctx->ctx.errors.flag & 1) == 0) { |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12187 | es = &appctx->ctx.errors.px->invalid_req; |
Willy Tarreau | 35069f8 | 2016-11-25 09:16:37 +0100 | [diff] [blame] | 12188 | if (appctx->ctx.errors.flag & 2) // skip req |
| 12189 | goto next; |
| 12190 | } |
| 12191 | else { |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12192 | es = &appctx->ctx.errors.px->invalid_rep; |
Willy Tarreau | 35069f8 | 2016-11-25 09:16:37 +0100 | [diff] [blame] | 12193 | if (appctx->ctx.errors.flag & 4) // skip resp |
| 12194 | goto next; |
| 12195 | } |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12196 | |
| 12197 | if (!es->when.tv_sec) |
| 12198 | goto next; |
| 12199 | |
| 12200 | if (appctx->ctx.errors.iid >= 0 && |
| 12201 | appctx->ctx.errors.px->uuid != appctx->ctx.errors.iid && |
| 12202 | es->oe->uuid != appctx->ctx.errors.iid) |
| 12203 | goto next; |
| 12204 | |
| 12205 | if (appctx->ctx.errors.ptr < 0) { |
| 12206 | /* just print headers now */ |
| 12207 | |
| 12208 | char pn[INET6_ADDRSTRLEN]; |
| 12209 | struct tm tm; |
| 12210 | int port; |
| 12211 | |
| 12212 | get_localtime(es->when.tv_sec, &tm); |
| 12213 | chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]", |
| 12214 | tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900, |
| 12215 | tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000)); |
| 12216 | |
| 12217 | switch (addr_to_str(&es->src, pn, sizeof(pn))) { |
| 12218 | case AF_INET: |
| 12219 | case AF_INET6: |
| 12220 | port = get_host_port(&es->src); |
| 12221 | break; |
| 12222 | default: |
| 12223 | port = 0; |
| 12224 | } |
| 12225 | |
Willy Tarreau | 35069f8 | 2016-11-25 09:16:37 +0100 | [diff] [blame] | 12226 | switch (appctx->ctx.errors.flag & 1) { |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12227 | case 0: |
| 12228 | chunk_appendf(&trash, |
| 12229 | " frontend %s (#%d): invalid request\n" |
| 12230 | " backend %s (#%d)", |
| 12231 | appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid, |
| 12232 | (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>", |
| 12233 | (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1); |
| 12234 | break; |
| 12235 | case 1: |
| 12236 | chunk_appendf(&trash, |
| 12237 | " backend %s (#%d): invalid response\n" |
| 12238 | " frontend %s (#%d)", |
| 12239 | appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid, |
| 12240 | es->oe->id, es->oe->uuid); |
| 12241 | break; |
| 12242 | } |
| 12243 | |
| 12244 | chunk_appendf(&trash, |
| 12245 | ", server %s (#%d), event #%u\n" |
| 12246 | " src %s:%d, session #%d, session flags 0x%08x\n" |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 12247 | " 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] | 12248 | " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n" |
| 12249 | " buffer flags 0x%08x, out %d bytes, total %lld bytes\n" |
| 12250 | " pending %d bytes, wrapping at %d, error at position %d:\n \n", |
| 12251 | es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1, |
| 12252 | es->ev_id, |
| 12253 | pn, port, es->sid, es->s_flags, |
Willy Tarreau | 0da5b3b | 2017-09-21 09:30:46 +0200 | [diff] [blame] | 12254 | 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] | 12255 | es->m_clen, es->m_blen, |
| 12256 | es->b_flags, es->b_out, es->b_tot, |
| 12257 | es->len, es->b_wrap, es->pos); |
| 12258 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 12259 | if (ci_putchk(si_ic(si), &trash) == -1) { |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12260 | /* Socket buffer full. Let's try again later from the same point */ |
| 12261 | si_applet_cant_put(si); |
| 12262 | return 0; |
| 12263 | } |
| 12264 | appctx->ctx.errors.ptr = 0; |
| 12265 | appctx->ctx.errors.sid = es->sid; |
| 12266 | } |
| 12267 | |
| 12268 | if (appctx->ctx.errors.sid != es->sid) { |
| 12269 | /* the snapshot changed while we were dumping it */ |
| 12270 | chunk_appendf(&trash, |
| 12271 | " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n"); |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 12272 | if (ci_putchk(si_ic(si), &trash) == -1) { |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12273 | si_applet_cant_put(si); |
| 12274 | return 0; |
| 12275 | } |
| 12276 | goto next; |
| 12277 | } |
| 12278 | |
| 12279 | /* OK, ptr >= 0, so we have to dump the current line */ |
| 12280 | while (es->buf && appctx->ctx.errors.ptr < es->len && appctx->ctx.errors.ptr < global.tune.bufsize) { |
| 12281 | int newptr; |
| 12282 | int newline; |
| 12283 | |
| 12284 | newline = appctx->ctx.errors.bol; |
| 12285 | newptr = dump_text_line(&trash, es->buf, global.tune.bufsize, es->len, &newline, appctx->ctx.errors.ptr); |
| 12286 | if (newptr == appctx->ctx.errors.ptr) |
| 12287 | return 0; |
| 12288 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 12289 | if (ci_putchk(si_ic(si), &trash) == -1) { |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12290 | /* Socket buffer full. Let's try again later from the same point */ |
| 12291 | si_applet_cant_put(si); |
| 12292 | return 0; |
| 12293 | } |
| 12294 | appctx->ctx.errors.ptr = newptr; |
| 12295 | appctx->ctx.errors.bol = newline; |
| 12296 | }; |
| 12297 | next: |
| 12298 | appctx->ctx.errors.bol = 0; |
| 12299 | appctx->ctx.errors.ptr = -1; |
Willy Tarreau | 35069f8 | 2016-11-25 09:16:37 +0100 | [diff] [blame] | 12300 | appctx->ctx.errors.flag ^= 1; |
| 12301 | if (!(appctx->ctx.errors.flag & 1)) |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12302 | appctx->ctx.errors.px = appctx->ctx.errors.px->next; |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12303 | } |
| 12304 | |
| 12305 | /* dump complete */ |
| 12306 | return 1; |
| 12307 | } |
| 12308 | |
| 12309 | /* register cli keywords */ |
| 12310 | static struct cli_kw_list cli_kws = {{ },{ |
| 12311 | { { "show", "errors", NULL }, |
| 12312 | "show errors : report last request and response errors for each proxy", |
| 12313 | cli_parse_show_errors, cli_io_handler_show_errors, NULL, |
| 12314 | }, |
| 12315 | {{},} |
| 12316 | }}; |
| 12317 | |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 12318 | /************************************************************************/ |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12319 | /* All supported ACL keywords must be declared here. */ |
| 12320 | /************************************************************************/ |
| 12321 | |
| 12322 | /* Note: must not be declared <const> as its list will be overwritten. |
| 12323 | * Please take care of keeping this list alphabetically sorted. |
| 12324 | */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 12325 | static struct acl_kw_list acl_kws = {ILH, { |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12326 | { "base", "base", PAT_MATCH_STR }, |
| 12327 | { "base_beg", "base", PAT_MATCH_BEG }, |
| 12328 | { "base_dir", "base", PAT_MATCH_DIR }, |
| 12329 | { "base_dom", "base", PAT_MATCH_DOM }, |
| 12330 | { "base_end", "base", PAT_MATCH_END }, |
| 12331 | { "base_len", "base", PAT_MATCH_LEN }, |
| 12332 | { "base_reg", "base", PAT_MATCH_REG }, |
| 12333 | { "base_sub", "base", PAT_MATCH_SUB }, |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 12334 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12335 | { "cook", "req.cook", PAT_MATCH_STR }, |
| 12336 | { "cook_beg", "req.cook", PAT_MATCH_BEG }, |
| 12337 | { "cook_dir", "req.cook", PAT_MATCH_DIR }, |
| 12338 | { "cook_dom", "req.cook", PAT_MATCH_DOM }, |
| 12339 | { "cook_end", "req.cook", PAT_MATCH_END }, |
| 12340 | { "cook_len", "req.cook", PAT_MATCH_LEN }, |
| 12341 | { "cook_reg", "req.cook", PAT_MATCH_REG }, |
| 12342 | { "cook_sub", "req.cook", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12343 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12344 | { "hdr", "req.hdr", PAT_MATCH_STR }, |
| 12345 | { "hdr_beg", "req.hdr", PAT_MATCH_BEG }, |
| 12346 | { "hdr_dir", "req.hdr", PAT_MATCH_DIR }, |
| 12347 | { "hdr_dom", "req.hdr", PAT_MATCH_DOM }, |
| 12348 | { "hdr_end", "req.hdr", PAT_MATCH_END }, |
| 12349 | { "hdr_len", "req.hdr", PAT_MATCH_LEN }, |
| 12350 | { "hdr_reg", "req.hdr", PAT_MATCH_REG }, |
| 12351 | { "hdr_sub", "req.hdr", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12352 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12353 | /* these two declarations uses strings with list storage (in place |
| 12354 | * of tree storage). The basic match is PAT_MATCH_STR, but the indexation |
| 12355 | * and delete functions are relative to the list management. The parse |
| 12356 | * and match method are related to the corresponding fetch methods. This |
| 12357 | * is very particular ACL declaration mode. |
| 12358 | */ |
| 12359 | { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth }, |
| 12360 | { "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] | 12361 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12362 | { "path", "path", PAT_MATCH_STR }, |
| 12363 | { "path_beg", "path", PAT_MATCH_BEG }, |
| 12364 | { "path_dir", "path", PAT_MATCH_DIR }, |
| 12365 | { "path_dom", "path", PAT_MATCH_DOM }, |
| 12366 | { "path_end", "path", PAT_MATCH_END }, |
| 12367 | { "path_len", "path", PAT_MATCH_LEN }, |
| 12368 | { "path_reg", "path", PAT_MATCH_REG }, |
| 12369 | { "path_sub", "path", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12370 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12371 | { "req_ver", "req.ver", PAT_MATCH_STR }, |
| 12372 | { "resp_ver", "res.ver", PAT_MATCH_STR }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12373 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12374 | { "scook", "res.cook", PAT_MATCH_STR }, |
| 12375 | { "scook_beg", "res.cook", PAT_MATCH_BEG }, |
| 12376 | { "scook_dir", "res.cook", PAT_MATCH_DIR }, |
| 12377 | { "scook_dom", "res.cook", PAT_MATCH_DOM }, |
| 12378 | { "scook_end", "res.cook", PAT_MATCH_END }, |
| 12379 | { "scook_len", "res.cook", PAT_MATCH_LEN }, |
| 12380 | { "scook_reg", "res.cook", PAT_MATCH_REG }, |
| 12381 | { "scook_sub", "res.cook", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12382 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12383 | { "shdr", "res.hdr", PAT_MATCH_STR }, |
| 12384 | { "shdr_beg", "res.hdr", PAT_MATCH_BEG }, |
| 12385 | { "shdr_dir", "res.hdr", PAT_MATCH_DIR }, |
| 12386 | { "shdr_dom", "res.hdr", PAT_MATCH_DOM }, |
| 12387 | { "shdr_end", "res.hdr", PAT_MATCH_END }, |
| 12388 | { "shdr_len", "res.hdr", PAT_MATCH_LEN }, |
| 12389 | { "shdr_reg", "res.hdr", PAT_MATCH_REG }, |
| 12390 | { "shdr_sub", "res.hdr", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12391 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12392 | { "url", "url", PAT_MATCH_STR }, |
| 12393 | { "url_beg", "url", PAT_MATCH_BEG }, |
| 12394 | { "url_dir", "url", PAT_MATCH_DIR }, |
| 12395 | { "url_dom", "url", PAT_MATCH_DOM }, |
| 12396 | { "url_end", "url", PAT_MATCH_END }, |
| 12397 | { "url_len", "url", PAT_MATCH_LEN }, |
| 12398 | { "url_reg", "url", PAT_MATCH_REG }, |
| 12399 | { "url_sub", "url", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12400 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12401 | { "urlp", "urlp", PAT_MATCH_STR }, |
| 12402 | { "urlp_beg", "urlp", PAT_MATCH_BEG }, |
| 12403 | { "urlp_dir", "urlp", PAT_MATCH_DIR }, |
| 12404 | { "urlp_dom", "urlp", PAT_MATCH_DOM }, |
| 12405 | { "urlp_end", "urlp", PAT_MATCH_END }, |
| 12406 | { "urlp_len", "urlp", PAT_MATCH_LEN }, |
| 12407 | { "urlp_reg", "urlp", PAT_MATCH_REG }, |
| 12408 | { "urlp_sub", "urlp", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12409 | |
Willy Tarreau | 8ed669b | 2013-01-11 15:49:37 +0100 | [diff] [blame] | 12410 | { /* END */ }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12411 | }}; |
| 12412 | |
| 12413 | /************************************************************************/ |
| 12414 | /* All supported pattern keywords must be declared here. */ |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 12415 | /************************************************************************/ |
| 12416 | /* Note: must not be declared <const> as its list will be overwritten */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 12417 | static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, { |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12418 | { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 12419 | { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 12420 | { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV }, |
| 12421 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 12422 | /* capture are allocated and are permanent in the stream */ |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 12423 | { "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] | 12424 | |
| 12425 | /* retrieve these captures from the HTTP logs */ |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 12426 | { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP }, |
| 12427 | { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP }, |
| 12428 | { "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] | 12429 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 12430 | { "capture.res.hdr", smp_fetch_capture_header_res, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP }, |
| 12431 | { "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] | 12432 | |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 12433 | /* cookie is valid in both directions (eg: for "stick ...") but cook* |
| 12434 | * are only here to match the ACL's name, are request-only and are used |
| 12435 | * for ACL compatibility only. |
| 12436 | */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12437 | { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 12438 | { "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] | 12439 | { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 12440 | { "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] | 12441 | |
| 12442 | /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are |
| 12443 | * only here to match the ACL's name, are request-only and are used for |
| 12444 | * ACL compatibility only. |
| 12445 | */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12446 | { "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] | 12447 | { "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] | 12448 | { "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] | 12449 | { "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] | 12450 | |
Willy Tarreau | 0a0daec | 2013-04-02 22:44:58 +0200 | [diff] [blame] | 12451 | { "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] | 12452 | { "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] | 12453 | { "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] | 12454 | { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP }, |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12455 | { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 12456 | { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 12457 | |
| 12458 | /* HTTP protocol on the request path */ |
| 12459 | { "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] | 12460 | { "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] | 12461 | |
| 12462 | /* HTTP version on the request path */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12463 | { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 12464 | { "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] | 12465 | |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 12466 | { "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] | 12467 | { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 12468 | { "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] | 12469 | { "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] | 12470 | |
Thierry FOURNIER | d7d8881 | 2017-04-19 15:15:14 +0200 | [diff] [blame] | 12471 | { "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] | 12472 | { "req.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV }, |
| 12473 | |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 12474 | /* HTTP version on the response path */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12475 | { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV }, |
| 12476 | { "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] | 12477 | |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 12478 | /* 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] | 12479 | { "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] | 12480 | { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 12481 | { "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] | 12482 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12483 | { "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] | 12484 | { "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] | 12485 | { "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] | 12486 | { "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] | 12487 | { "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] | 12488 | { "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] | 12489 | { "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] | 12490 | |
| 12491 | /* 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] | 12492 | { "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] | 12493 | { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV }, |
| 12494 | { "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] | 12495 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12496 | { "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] | 12497 | { "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] | 12498 | { "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] | 12499 | { "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] | 12500 | { "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] | 12501 | { "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] | 12502 | { "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] | 12503 | |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 12504 | /* 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] | 12505 | { "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] | 12506 | { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV }, |
| 12507 | { "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] | 12508 | { "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] | 12509 | |
| 12510 | /* 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] | 12511 | { "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] | 12512 | { "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] | 12513 | { "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] | 12514 | { "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] | 12515 | |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 12516 | { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP }, |
Thierry Fournier | 0e00dca | 2016-04-07 15:47:40 +0200 | [diff] [blame] | 12517 | { "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] | 12518 | { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 12519 | { "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] | 12520 | { "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] | 12521 | { "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] | 12522 | { "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] | 12523 | { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 12524 | { "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] | 12525 | { "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] | 12526 | { /* END */ }, |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 12527 | }}; |
| 12528 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 12529 | |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12530 | /************************************************************************/ |
| 12531 | /* All supported converter keywords must be declared here. */ |
| 12532 | /************************************************************************/ |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 12533 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 12534 | static struct sample_conv_kw_list sample_conv_kws = {ILH, { |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 12535 | { "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] | 12536 | { "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] | 12537 | { "capture-req", smp_conv_req_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR}, |
| 12538 | { "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] | 12539 | { "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] | 12540 | { NULL, NULL, 0, 0, 0 }, |
| 12541 | }}; |
| 12542 | |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 12543 | |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12544 | /************************************************************************/ |
| 12545 | /* All supported http-request action keywords must be declared here. */ |
| 12546 | /************************************************************************/ |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 12547 | struct action_kw_list http_req_actions = { |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12548 | .kw = { |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12549 | { "capture", parse_http_req_capture }, |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12550 | { "set-method", parse_set_req_line }, |
| 12551 | { "set-path", parse_set_req_line }, |
| 12552 | { "set-query", parse_set_req_line }, |
| 12553 | { "set-uri", parse_set_req_line }, |
Willy Tarreau | cb703b0 | 2015-04-03 09:52:01 +0200 | [diff] [blame] | 12554 | { NULL, NULL } |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12555 | } |
| 12556 | }; |
| 12557 | |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 12558 | struct action_kw_list http_res_actions = { |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12559 | .kw = { |
| 12560 | { "capture", parse_http_res_capture }, |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 12561 | { "set-status", parse_http_set_status }, |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12562 | { NULL, NULL } |
| 12563 | } |
| 12564 | }; |
| 12565 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 12566 | __attribute__((constructor)) |
| 12567 | static void __http_protocol_init(void) |
| 12568 | { |
| 12569 | acl_register_keywords(&acl_kws); |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 12570 | sample_register_fetches(&sample_fetch_keywords); |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 12571 | sample_register_convs(&sample_conv_kws); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12572 | http_req_keywords_register(&http_req_actions); |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12573 | http_res_keywords_register(&http_res_actions); |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 12574 | cli_register_kw(&cli_kws); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 12575 | } |
| 12576 | |
| 12577 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 12578 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 12579 | * Local variables: |
| 12580 | * c-indent-level: 8 |
| 12581 | * c-basic-offset: 8 |
| 12582 | * End: |
| 12583 | */ |