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 | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 42 | #include <types/global.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 43 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 44 | #include <proto/acl.h> |
Thierry FOURNIER | 322a124 | 2015-08-19 09:07:47 +0200 | [diff] [blame] | 45 | #include <proto/action.h> |
Willy Tarreau | 61612d4 | 2012-04-19 18:42:05 +0200 | [diff] [blame] | 46 | #include <proto/arg.h> |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 47 | #include <proto/auth.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 48 | #include <proto/backend.h> |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 49 | #include <proto/channel.h> |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 50 | #include <proto/checks.h> |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 51 | #include <proto/compression.h> |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 52 | #include <proto/dumpstats.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 53 | #include <proto/fd.h> |
Willy Tarreau | 03fa5df | 2010-05-24 21:02:37 +0200 | [diff] [blame] | 54 | #include <proto/frontend.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 55 | #include <proto/log.h> |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 56 | #include <proto/hdr_idx.h> |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 57 | #include <proto/pattern.h> |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 58 | #include <proto/proto_tcp.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 59 | #include <proto/proto_http.h> |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 60 | #include <proto/proxy.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 61 | #include <proto/queue.h> |
Willy Tarreau | cd3b094 | 2012-04-27 21:52:18 +0200 | [diff] [blame] | 62 | #include <proto/sample.h> |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 63 | #include <proto/server.h> |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 64 | #include <proto/stream.h> |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 65 | #include <proto/stream_interface.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 66 | #include <proto/task.h> |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 67 | #include <proto/pattern.h> |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 68 | #include <proto/vars.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 69 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 70 | const char HTTP_100[] = |
| 71 | "HTTP/1.1 100 Continue\r\n\r\n"; |
| 72 | |
| 73 | const struct chunk http_100_chunk = { |
| 74 | .str = (char *)&HTTP_100, |
| 75 | .len = sizeof(HTTP_100)-1 |
| 76 | }; |
| 77 | |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 78 | /* Warning: no "connection" header is provided with the 3xx messages below */ |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 79 | const char *HTTP_301 = |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 80 | "HTTP/1.1 301 Moved Permanently\r\n" |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 81 | "Content-length: 0\r\n" |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 82 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 83 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 84 | const char *HTTP_302 = |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 85 | "HTTP/1.1 302 Found\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 86 | "Cache-Control: no-cache\r\n" |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 87 | "Content-length: 0\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 88 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 89 | |
| 90 | /* same as 302 except that the browser MUST retry with the GET method */ |
| 91 | const char *HTTP_303 = |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 92 | "HTTP/1.1 303 See Other\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 93 | "Cache-Control: no-cache\r\n" |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 94 | "Content-length: 0\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 95 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 96 | |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 97 | |
| 98 | /* same as 302 except that the browser MUST retry with the same method */ |
| 99 | const char *HTTP_307 = |
| 100 | "HTTP/1.1 307 Temporary Redirect\r\n" |
| 101 | "Cache-Control: no-cache\r\n" |
| 102 | "Content-length: 0\r\n" |
| 103 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 104 | |
| 105 | /* same as 301 except that the browser MUST retry with the same method */ |
| 106 | const char *HTTP_308 = |
| 107 | "HTTP/1.1 308 Permanent Redirect\r\n" |
| 108 | "Content-length: 0\r\n" |
| 109 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 110 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 111 | /* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */ |
| 112 | const char *HTTP_401_fmt = |
| 113 | "HTTP/1.0 401 Unauthorized\r\n" |
| 114 | "Cache-Control: no-cache\r\n" |
| 115 | "Connection: close\r\n" |
Willy Tarreau | 791d66d | 2006-07-08 16:53:38 +0200 | [diff] [blame] | 116 | "Content-Type: text/html\r\n" |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 117 | "WWW-Authenticate: Basic realm=\"%s\"\r\n" |
| 118 | "\r\n" |
| 119 | "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n"; |
| 120 | |
Willy Tarreau | 844a7e7 | 2010-01-31 21:46:18 +0100 | [diff] [blame] | 121 | const char *HTTP_407_fmt = |
| 122 | "HTTP/1.0 407 Unauthorized\r\n" |
| 123 | "Cache-Control: no-cache\r\n" |
| 124 | "Connection: close\r\n" |
| 125 | "Content-Type: text/html\r\n" |
| 126 | "Proxy-Authenticate: Basic realm=\"%s\"\r\n" |
| 127 | "\r\n" |
Godbach | 1f1fae6 | 2014-12-17 16:32:05 +0800 | [diff] [blame] | 128 | "<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] | 129 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 130 | |
| 131 | const int http_err_codes[HTTP_ERR_SIZE] = { |
Willy Tarreau | ae94d4d | 2011-05-11 16:28:49 +0200 | [diff] [blame] | 132 | [HTTP_ERR_200] = 200, /* used by "monitor-uri" */ |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 133 | [HTTP_ERR_400] = 400, |
| 134 | [HTTP_ERR_403] = 403, |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 135 | [HTTP_ERR_405] = 405, |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 136 | [HTTP_ERR_408] = 408, |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 137 | [HTTP_ERR_429] = 429, |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 138 | [HTTP_ERR_500] = 500, |
| 139 | [HTTP_ERR_502] = 502, |
| 140 | [HTTP_ERR_503] = 503, |
| 141 | [HTTP_ERR_504] = 504, |
| 142 | }; |
| 143 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 144 | static const char *http_err_msgs[HTTP_ERR_SIZE] = { |
Willy Tarreau | ae94d4d | 2011-05-11 16:28:49 +0200 | [diff] [blame] | 145 | [HTTP_ERR_200] = |
| 146 | "HTTP/1.0 200 OK\r\n" |
| 147 | "Cache-Control: no-cache\r\n" |
| 148 | "Connection: close\r\n" |
| 149 | "Content-Type: text/html\r\n" |
| 150 | "\r\n" |
| 151 | "<html><body><h1>200 OK</h1>\nService ready.\n</body></html>\n", |
| 152 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 153 | [HTTP_ERR_400] = |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 154 | "HTTP/1.0 400 Bad request\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 155 | "Cache-Control: no-cache\r\n" |
| 156 | "Connection: close\r\n" |
| 157 | "Content-Type: text/html\r\n" |
| 158 | "\r\n" |
| 159 | "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n", |
| 160 | |
| 161 | [HTTP_ERR_403] = |
| 162 | "HTTP/1.0 403 Forbidden\r\n" |
| 163 | "Cache-Control: no-cache\r\n" |
| 164 | "Connection: close\r\n" |
| 165 | "Content-Type: text/html\r\n" |
| 166 | "\r\n" |
| 167 | "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n", |
| 168 | |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 169 | [HTTP_ERR_405] = |
| 170 | "HTTP/1.0 405 Method Not Allowed\r\n" |
| 171 | "Cache-Control: no-cache\r\n" |
| 172 | "Connection: close\r\n" |
| 173 | "Content-Type: text/html\r\n" |
| 174 | "\r\n" |
| 175 | "<html><body><h1>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", |
| 176 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 177 | [HTTP_ERR_408] = |
| 178 | "HTTP/1.0 408 Request Time-out\r\n" |
| 179 | "Cache-Control: no-cache\r\n" |
| 180 | "Connection: close\r\n" |
| 181 | "Content-Type: text/html\r\n" |
| 182 | "\r\n" |
| 183 | "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n", |
| 184 | |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 185 | [HTTP_ERR_429] = |
| 186 | "HTTP/1.0 429 Too Many Requests\r\n" |
| 187 | "Cache-Control: no-cache\r\n" |
| 188 | "Connection: close\r\n" |
| 189 | "Content-Type: text/html\r\n" |
| 190 | "\r\n" |
| 191 | "<html><body><h1>429 Too Many Requests</h1>\nYou have sent too many requests in a given amount of time.\n</body></html>\n", |
| 192 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 193 | [HTTP_ERR_500] = |
| 194 | "HTTP/1.0 500 Server Error\r\n" |
| 195 | "Cache-Control: no-cache\r\n" |
| 196 | "Connection: close\r\n" |
| 197 | "Content-Type: text/html\r\n" |
| 198 | "\r\n" |
| 199 | "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n", |
| 200 | |
| 201 | [HTTP_ERR_502] = |
| 202 | "HTTP/1.0 502 Bad Gateway\r\n" |
| 203 | "Cache-Control: no-cache\r\n" |
| 204 | "Connection: close\r\n" |
| 205 | "Content-Type: text/html\r\n" |
| 206 | "\r\n" |
| 207 | "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n", |
| 208 | |
| 209 | [HTTP_ERR_503] = |
| 210 | "HTTP/1.0 503 Service Unavailable\r\n" |
| 211 | "Cache-Control: no-cache\r\n" |
| 212 | "Connection: close\r\n" |
| 213 | "Content-Type: text/html\r\n" |
| 214 | "\r\n" |
| 215 | "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n", |
| 216 | |
| 217 | [HTTP_ERR_504] = |
| 218 | "HTTP/1.0 504 Gateway Time-out\r\n" |
| 219 | "Cache-Control: no-cache\r\n" |
| 220 | "Connection: close\r\n" |
| 221 | "Content-Type: text/html\r\n" |
| 222 | "\r\n" |
| 223 | "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n", |
| 224 | |
| 225 | }; |
| 226 | |
Cyril Bonté | 19979e1 | 2012-04-04 12:57:21 +0200 | [diff] [blame] | 227 | /* status codes available for the stats admin page (strictly 4 chars length) */ |
| 228 | const char *stat_status_codes[STAT_STATUS_SIZE] = { |
| 229 | [STAT_STATUS_DENY] = "DENY", |
| 230 | [STAT_STATUS_DONE] = "DONE", |
| 231 | [STAT_STATUS_ERRP] = "ERRP", |
| 232 | [STAT_STATUS_EXCD] = "EXCD", |
| 233 | [STAT_STATUS_NONE] = "NONE", |
| 234 | [STAT_STATUS_PART] = "PART", |
| 235 | [STAT_STATUS_UNKN] = "UNKN", |
| 236 | }; |
| 237 | |
| 238 | |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 239 | /* List head of all known action keywords for "http-request" */ |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 240 | struct action_kw_list http_req_keywords = { |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 241 | .list = LIST_HEAD_INIT(http_req_keywords.list) |
| 242 | }; |
| 243 | |
| 244 | /* List head of all known action keywords for "http-response" */ |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 245 | struct action_kw_list http_res_keywords = { |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 246 | .list = LIST_HEAD_INIT(http_res_keywords.list) |
| 247 | }; |
| 248 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 249 | /* We must put the messages here since GCC cannot initialize consts depending |
| 250 | * on strlen(). |
| 251 | */ |
| 252 | struct chunk http_err_chunks[HTTP_ERR_SIZE]; |
| 253 | |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 254 | /* this struct is used between calls to smp_fetch_hdr() or smp_fetch_cookie() */ |
| 255 | static struct hdr_ctx static_hdr_ctx; |
| 256 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 257 | #define FD_SETS_ARE_BITFIELDS |
| 258 | #ifdef FD_SETS_ARE_BITFIELDS |
| 259 | /* |
| 260 | * This map is used with all the FD_* macros to check whether a particular bit |
| 261 | * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes |
| 262 | * which should be encoded. When FD_ISSET() returns non-zero, it means that the |
| 263 | * byte should be encoded. Be careful to always pass bytes from 0 to 255 |
| 264 | * exclusively to the macros. |
| 265 | */ |
| 266 | fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| 267 | 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] | 268 | 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] | 269 | |
| 270 | #else |
| 271 | #error "Check if your OS uses bitfields for fd_sets" |
| 272 | #endif |
| 273 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 274 | 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] | 275 | |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 276 | /* This function returns a reason associated with the HTTP status. |
| 277 | * This function never fails, a message is always returned. |
| 278 | */ |
| 279 | const char *get_reason(unsigned int status) |
| 280 | { |
| 281 | switch (status) { |
| 282 | case 100: return "Continue"; |
| 283 | case 101: return "Switching Protocols"; |
| 284 | case 102: return "Processing"; |
| 285 | case 200: return "OK"; |
| 286 | case 201: return "Created"; |
| 287 | case 202: return "Accepted"; |
| 288 | case 203: return "Non-Authoritative Information"; |
| 289 | case 204: return "No Content"; |
| 290 | case 205: return "Reset Content"; |
| 291 | case 206: return "Partial Content"; |
| 292 | case 207: return "Multi-Status"; |
| 293 | case 210: return "Content Different"; |
| 294 | case 226: return "IM Used"; |
| 295 | case 300: return "Multiple Choices"; |
| 296 | case 301: return "Moved Permanently"; |
| 297 | case 302: return "Moved Temporarily"; |
| 298 | case 303: return "See Other"; |
| 299 | case 304: return "Not Modified"; |
| 300 | case 305: return "Use Proxy"; |
| 301 | case 307: return "Temporary Redirect"; |
| 302 | case 308: return "Permanent Redirect"; |
| 303 | case 310: return "Too many Redirects"; |
| 304 | case 400: return "Bad Request"; |
| 305 | case 401: return "Unauthorized"; |
| 306 | case 402: return "Payment Required"; |
| 307 | case 403: return "Forbidden"; |
| 308 | case 404: return "Not Found"; |
| 309 | case 405: return "Method Not Allowed"; |
| 310 | case 406: return "Not Acceptable"; |
| 311 | case 407: return "Proxy Authentication Required"; |
| 312 | case 408: return "Request Time-out"; |
| 313 | case 409: return "Conflict"; |
| 314 | case 410: return "Gone"; |
| 315 | case 411: return "Length Required"; |
| 316 | case 412: return "Precondition Failed"; |
| 317 | case 413: return "Request Entity Too Large"; |
| 318 | case 414: return "Request-URI Too Long"; |
| 319 | case 415: return "Unsupported Media Type"; |
| 320 | case 416: return "Requested range unsatisfiable"; |
| 321 | case 417: return "Expectation failed"; |
| 322 | case 418: return "I'm a teapot"; |
| 323 | case 422: return "Unprocessable entity"; |
| 324 | case 423: return "Locked"; |
| 325 | case 424: return "Method failure"; |
| 326 | case 425: return "Unordered Collection"; |
| 327 | case 426: return "Upgrade Required"; |
| 328 | case 428: return "Precondition Required"; |
| 329 | case 429: return "Too Many Requests"; |
| 330 | case 431: return "Request Header Fields Too Large"; |
| 331 | case 449: return "Retry With"; |
| 332 | case 450: return "Blocked by Windows Parental Controls"; |
| 333 | case 451: return "Unavailable For Legal Reasons"; |
| 334 | case 456: return "Unrecoverable Error"; |
| 335 | case 499: return "client has closed connection"; |
| 336 | case 500: return "Internal Server Error"; |
| 337 | case 501: return "Not Implemented"; |
| 338 | case 502: return "Bad Gateway ou Proxy Error"; |
| 339 | case 503: return "Service Unavailable"; |
| 340 | case 504: return "Gateway Time-out"; |
| 341 | case 505: return "HTTP Version not supported"; |
| 342 | case 506: return "Variant also negociate"; |
| 343 | case 507: return "Insufficient storage"; |
| 344 | case 508: return "Loop detected"; |
| 345 | case 509: return "Bandwidth Limit Exceeded"; |
| 346 | case 510: return "Not extended"; |
| 347 | case 511: return "Network authentication required"; |
| 348 | case 520: return "Web server is returning an unknown error"; |
| 349 | default: |
| 350 | switch (status) { |
| 351 | case 100 ... 199: return "Informational"; |
| 352 | case 200 ... 299: return "Success"; |
| 353 | case 300 ... 399: return "Redirection"; |
| 354 | case 400 ... 499: return "Client Error"; |
| 355 | case 500 ... 599: return "Server Error"; |
| 356 | default: return "Other"; |
| 357 | } |
| 358 | } |
| 359 | } |
| 360 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 361 | void init_proto_http() |
| 362 | { |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 363 | int i; |
| 364 | char *tmp; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 365 | int msg; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 366 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 367 | for (msg = 0; msg < HTTP_ERR_SIZE; msg++) { |
| 368 | if (!http_err_msgs[msg]) { |
| 369 | Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg); |
| 370 | abort(); |
| 371 | } |
| 372 | |
| 373 | http_err_chunks[msg].str = (char *)http_err_msgs[msg]; |
| 374 | http_err_chunks[msg].len = strlen(http_err_msgs[msg]); |
| 375 | } |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 376 | |
| 377 | /* initialize the log header encoding map : '{|}"#' should be encoded with |
| 378 | * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ). |
| 379 | * URL encoding only requires '"', '#' to be encoded as well as non- |
| 380 | * printable characters above. |
| 381 | */ |
| 382 | memset(hdr_encode_map, 0, sizeof(hdr_encode_map)); |
| 383 | memset(url_encode_map, 0, sizeof(url_encode_map)); |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 384 | memset(http_encode_map, 0, sizeof(url_encode_map)); |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 385 | for (i = 0; i < 32; i++) { |
| 386 | FD_SET(i, hdr_encode_map); |
| 387 | FD_SET(i, url_encode_map); |
| 388 | } |
| 389 | for (i = 127; i < 256; i++) { |
| 390 | FD_SET(i, hdr_encode_map); |
| 391 | FD_SET(i, url_encode_map); |
| 392 | } |
| 393 | |
| 394 | tmp = "\"#{|}"; |
| 395 | while (*tmp) { |
| 396 | FD_SET(*tmp, hdr_encode_map); |
| 397 | tmp++; |
| 398 | } |
| 399 | |
| 400 | tmp = "\"#"; |
| 401 | while (*tmp) { |
| 402 | FD_SET(*tmp, url_encode_map); |
| 403 | tmp++; |
| 404 | } |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 405 | |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 406 | /* initialize the http header encoding map. The draft httpbis define the |
| 407 | * header content as: |
| 408 | * |
| 409 | * HTTP-message = start-line |
| 410 | * *( header-field CRLF ) |
| 411 | * CRLF |
| 412 | * [ message-body ] |
| 413 | * header-field = field-name ":" OWS field-value OWS |
| 414 | * field-value = *( field-content / obs-fold ) |
| 415 | * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ] |
| 416 | * obs-fold = CRLF 1*( SP / HTAB ) |
| 417 | * field-vchar = VCHAR / obs-text |
| 418 | * VCHAR = %x21-7E |
| 419 | * obs-text = %x80-FF |
| 420 | * |
| 421 | * All the chars are encoded except "VCHAR", "obs-text", SP and HTAB. |
| 422 | * The encoded chars are form 0x00 to 0x08, 0x0a to 0x1f and 0x7f. The |
| 423 | * "obs-fold" is volontary forgotten because haproxy remove this. |
| 424 | */ |
| 425 | memset(http_encode_map, 0, sizeof(http_encode_map)); |
| 426 | for (i = 0x00; i <= 0x08; i++) |
| 427 | FD_SET(i, http_encode_map); |
| 428 | for (i = 0x0a; i <= 0x1f; i++) |
| 429 | FD_SET(i, http_encode_map); |
| 430 | FD_SET(0x7f, http_encode_map); |
| 431 | |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 432 | /* memory allocations */ |
Willy Tarreau | 63986c7 | 2015-04-03 22:55:33 +0200 | [diff] [blame] | 433 | pool2_http_txn = create_pool("http_txn", sizeof(struct http_txn), MEM_F_SHARED); |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 434 | pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED); |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 435 | pool2_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED); |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 436 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 437 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 438 | /* |
| 439 | * We have 26 list of methods (1 per first letter), each of which can have |
| 440 | * up to 3 entries (2 valid, 1 null). |
| 441 | */ |
| 442 | struct http_method_desc { |
Willy Tarreau | c8987b3 | 2013-12-06 23:43:17 +0100 | [diff] [blame] | 443 | enum http_meth_t meth; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 444 | int len; |
| 445 | const char text[8]; |
| 446 | }; |
| 447 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 448 | const struct http_method_desc http_methods[26][3] = { |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 449 | ['C' - 'A'] = { |
| 450 | [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" }, |
| 451 | }, |
| 452 | ['D' - 'A'] = { |
| 453 | [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" }, |
| 454 | }, |
| 455 | ['G' - 'A'] = { |
| 456 | [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" }, |
| 457 | }, |
| 458 | ['H' - 'A'] = { |
| 459 | [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" }, |
| 460 | }, |
Christopher Faulet | d57ad64 | 2015-07-31 14:26:57 +0200 | [diff] [blame] | 461 | ['O' - 'A'] = { |
| 462 | [0] = { .meth = HTTP_METH_OPTIONS , .len=7, .text="OPTIONS" }, |
| 463 | }, |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 464 | ['P' - 'A'] = { |
| 465 | [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" }, |
| 466 | [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" }, |
| 467 | }, |
| 468 | ['T' - 'A'] = { |
| 469 | [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" }, |
| 470 | }, |
| 471 | /* rest is empty like this : |
Willy Tarreau | b7ce424 | 2015-09-03 17:15:21 +0200 | [diff] [blame] | 472 | * [0] = { .meth = HTTP_METH_OTHER , .len=0, .text="" }, |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 473 | */ |
| 474 | }; |
| 475 | |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 476 | const struct http_method_name http_known_methods[HTTP_METH_OTHER] = { |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 477 | [HTTP_METH_OPTIONS] = { "OPTIONS", 7 }, |
| 478 | [HTTP_METH_GET] = { "GET", 3 }, |
| 479 | [HTTP_METH_HEAD] = { "HEAD", 4 }, |
| 480 | [HTTP_METH_POST] = { "POST", 4 }, |
| 481 | [HTTP_METH_PUT] = { "PUT", 3 }, |
| 482 | [HTTP_METH_DELETE] = { "DELETE", 6 }, |
| 483 | [HTTP_METH_TRACE] = { "TRACE", 5 }, |
| 484 | [HTTP_METH_CONNECT] = { "CONNECT", 7 }, |
| 485 | }; |
| 486 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 487 | /* It is about twice as fast on recent architectures to lookup a byte in a |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 488 | * table than to perform a boolean AND or OR between two tests. Refer to |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 489 | * RFC2616 for those chars. |
| 490 | */ |
| 491 | |
| 492 | const char http_is_spht[256] = { |
| 493 | [' '] = 1, ['\t'] = 1, |
| 494 | }; |
| 495 | |
| 496 | const char http_is_crlf[256] = { |
| 497 | ['\r'] = 1, ['\n'] = 1, |
| 498 | }; |
| 499 | |
| 500 | const char http_is_lws[256] = { |
| 501 | [' '] = 1, ['\t'] = 1, |
| 502 | ['\r'] = 1, ['\n'] = 1, |
| 503 | }; |
| 504 | |
| 505 | const char http_is_sep[256] = { |
| 506 | ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1, |
| 507 | ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1, |
| 508 | ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1, |
| 509 | ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1, |
| 510 | [' '] = 1, ['\t'] = 1, ['\\'] = 1, |
| 511 | }; |
| 512 | |
| 513 | const char http_is_ctl[256] = { |
| 514 | [0 ... 31] = 1, |
| 515 | [127] = 1, |
| 516 | }; |
| 517 | |
| 518 | /* |
| 519 | * A token is any ASCII char that is neither a separator nor a CTL char. |
| 520 | * Do not overwrite values in assignment since gcc-2.95 will not handle |
| 521 | * them correctly. Instead, define every non-CTL char's status. |
| 522 | */ |
| 523 | const char http_is_token[256] = { |
| 524 | [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1, |
| 525 | ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1, |
| 526 | ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1, |
| 527 | [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0, |
| 528 | ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, |
| 529 | ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1, |
| 530 | ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0, |
| 531 | ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0, |
| 532 | ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1, |
| 533 | ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1, |
| 534 | ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1, |
| 535 | ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1, |
| 536 | ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1, |
| 537 | ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1, |
| 538 | ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0, |
| 539 | ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1, |
| 540 | ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1, |
| 541 | ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1, |
| 542 | ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1, |
| 543 | ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1, |
| 544 | ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1, |
| 545 | ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1, |
| 546 | ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0, |
| 547 | ['|'] = 1, ['}'] = 0, ['~'] = 1, |
| 548 | }; |
| 549 | |
| 550 | |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 551 | /* |
| 552 | * An http ver_token is any ASCII which can be found in an HTTP version, |
| 553 | * which includes 'H', 'T', 'P', '/', '.' and any digit. |
| 554 | */ |
| 555 | const char http_is_ver_token[256] = { |
| 556 | ['.'] = 1, ['/'] = 1, |
| 557 | ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1, |
| 558 | ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1, |
Thierry FOURNIER | 63d692c | 2015-02-28 19:03:56 +0100 | [diff] [blame] | 559 | ['H'] = 1, ['P'] = 1, ['R'] = 1, ['S'] = 1, ['T'] = 1, |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 560 | }; |
| 561 | |
| 562 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 563 | /* |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 564 | * Adds a header and its CRLF at the tail of the message's buffer, just before |
| 565 | * 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] | 566 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 567 | * of headers is automatically adjusted. The number of bytes added is returned |
| 568 | * on success, otherwise <0 is returned indicating an error. |
| 569 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 570 | 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] | 571 | { |
| 572 | int bytes, len; |
| 573 | |
| 574 | len = strlen(text); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 575 | 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] | 576 | if (!bytes) |
| 577 | return -1; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 578 | http_msg_move_end(msg, bytes); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 579 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 580 | } |
| 581 | |
| 582 | /* |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 583 | * Adds a header and its CRLF at the tail of the message's buffer, just before |
| 584 | * 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] | 585 | * the buffer is only opened and the space reserved, but nothing is copied. |
| 586 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 587 | * of headers is automatically adjusted. The number of bytes added is returned |
| 588 | * on success, otherwise <0 is returned indicating an error. |
| 589 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 590 | int http_header_add_tail2(struct http_msg *msg, |
| 591 | struct hdr_idx *hdr_idx, const char *text, int len) |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 592 | { |
| 593 | int bytes; |
| 594 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 595 | 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] | 596 | if (!bytes) |
| 597 | return -1; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 598 | http_msg_move_end(msg, bytes); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 599 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 600 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 601 | |
| 602 | /* |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 603 | * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon. |
| 604 | * If so, returns the position of the first non-space character relative to |
| 605 | * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries |
| 606 | * to return a pointer to the place after the first space. Returns 0 if the |
| 607 | * header name does not match. Checks are case-insensitive. |
| 608 | */ |
| 609 | int http_header_match2(const char *hdr, const char *end, |
| 610 | const char *name, int len) |
| 611 | { |
| 612 | const char *val; |
| 613 | |
| 614 | if (hdr + len >= end) |
| 615 | return 0; |
| 616 | if (hdr[len] != ':') |
| 617 | return 0; |
| 618 | if (strncasecmp(hdr, name, len) != 0) |
| 619 | return 0; |
| 620 | val = hdr + len + 1; |
| 621 | while (val < end && HTTP_IS_SPHT(*val)) |
| 622 | val++; |
| 623 | if ((val >= end) && (len + 2 <= end - hdr)) |
| 624 | return len + 2; /* we may replace starting from second space */ |
| 625 | return val - hdr; |
| 626 | } |
| 627 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 628 | /* Find the first or next occurrence of header <name> in message buffer <sol> |
| 629 | * using headers index <idx>, and return it in the <ctx> structure. This |
| 630 | * structure holds everything necessary to use the header and find next |
| 631 | * occurrence. If its <idx> member is 0, the header is searched from the |
| 632 | * beginning. Otherwise, the next occurrence is returned. The function returns |
| 633 | * 1 when it finds a value, and 0 when there is no more. It is very similar to |
| 634 | * http_find_header2() except that it is designed to work with full-line headers |
| 635 | * whose comma is not a delimiter but is part of the syntax. As a special case, |
| 636 | * if ctx->val is NULL when searching for a new values of a header, the current |
| 637 | * header is rescanned. This allows rescanning after a header deletion. |
| 638 | */ |
| 639 | int http_find_full_header2(const char *name, int len, |
| 640 | char *sol, struct hdr_idx *idx, |
| 641 | struct hdr_ctx *ctx) |
| 642 | { |
| 643 | char *eol, *sov; |
| 644 | int cur_idx, old_idx; |
| 645 | |
| 646 | cur_idx = ctx->idx; |
| 647 | if (cur_idx) { |
| 648 | /* We have previously returned a header, let's search another one */ |
| 649 | sol = ctx->line; |
| 650 | eol = sol + idx->v[cur_idx].len; |
| 651 | goto next_hdr; |
| 652 | } |
| 653 | |
| 654 | /* first request for this header */ |
| 655 | sol += hdr_idx_first_pos(idx); |
| 656 | old_idx = 0; |
| 657 | cur_idx = hdr_idx_first_idx(idx); |
| 658 | while (cur_idx) { |
| 659 | eol = sol + idx->v[cur_idx].len; |
| 660 | |
| 661 | if (len == 0) { |
| 662 | /* No argument was passed, we want any header. |
| 663 | * To achieve this, we simply build a fake request. */ |
| 664 | while (sol + len < eol && sol[len] != ':') |
| 665 | len++; |
| 666 | name = sol; |
| 667 | } |
| 668 | |
| 669 | if ((len < eol - sol) && |
| 670 | (sol[len] == ':') && |
| 671 | (strncasecmp(sol, name, len) == 0)) { |
| 672 | ctx->del = len; |
| 673 | sov = sol + len + 1; |
| 674 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 675 | sov++; |
| 676 | |
| 677 | ctx->line = sol; |
| 678 | ctx->prev = old_idx; |
| 679 | ctx->idx = cur_idx; |
| 680 | ctx->val = sov - sol; |
| 681 | ctx->tws = 0; |
| 682 | while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) { |
| 683 | eol--; |
| 684 | ctx->tws++; |
| 685 | } |
| 686 | ctx->vlen = eol - sov; |
| 687 | return 1; |
| 688 | } |
| 689 | next_hdr: |
| 690 | sol = eol + idx->v[cur_idx].cr + 1; |
| 691 | old_idx = cur_idx; |
| 692 | cur_idx = idx->v[cur_idx].next; |
| 693 | } |
| 694 | return 0; |
| 695 | } |
| 696 | |
Willy Tarreau | c90dc23 | 2015-02-20 13:51:36 +0100 | [diff] [blame] | 697 | /* Find the first or next header field in message buffer <sol> using headers |
| 698 | * index <idx>, and return it in the <ctx> structure. This structure holds |
| 699 | * everything necessary to use the header and find next occurrence. If its |
| 700 | * <idx> member is 0, the first header is retrieved. Otherwise, the next |
| 701 | * occurrence is returned. The function returns 1 when it finds a value, and |
| 702 | * 0 when there is no more. It is equivalent to http_find_full_header2() with |
| 703 | * no header name. |
| 704 | */ |
| 705 | int http_find_next_header(char *sol, struct hdr_idx *idx, struct hdr_ctx *ctx) |
| 706 | { |
| 707 | char *eol, *sov; |
| 708 | int cur_idx, old_idx; |
| 709 | int len; |
| 710 | |
| 711 | cur_idx = ctx->idx; |
| 712 | if (cur_idx) { |
| 713 | /* We have previously returned a header, let's search another one */ |
| 714 | sol = ctx->line; |
| 715 | eol = sol + idx->v[cur_idx].len; |
| 716 | goto next_hdr; |
| 717 | } |
| 718 | |
| 719 | /* first request for this header */ |
| 720 | sol += hdr_idx_first_pos(idx); |
| 721 | old_idx = 0; |
| 722 | cur_idx = hdr_idx_first_idx(idx); |
| 723 | while (cur_idx) { |
| 724 | eol = sol + idx->v[cur_idx].len; |
| 725 | |
| 726 | len = 0; |
| 727 | while (1) { |
| 728 | if (len >= eol - sol) |
| 729 | goto next_hdr; |
| 730 | if (sol[len] == ':') |
| 731 | break; |
| 732 | len++; |
| 733 | } |
| 734 | |
| 735 | ctx->del = len; |
| 736 | sov = sol + len + 1; |
| 737 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 738 | sov++; |
| 739 | |
| 740 | ctx->line = sol; |
| 741 | ctx->prev = old_idx; |
| 742 | ctx->idx = cur_idx; |
| 743 | ctx->val = sov - sol; |
| 744 | ctx->tws = 0; |
| 745 | |
| 746 | while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) { |
| 747 | eol--; |
| 748 | ctx->tws++; |
| 749 | } |
| 750 | ctx->vlen = eol - sov; |
| 751 | return 1; |
| 752 | |
| 753 | next_hdr: |
| 754 | sol = eol + idx->v[cur_idx].cr + 1; |
| 755 | old_idx = cur_idx; |
| 756 | cur_idx = idx->v[cur_idx].next; |
| 757 | } |
| 758 | return 0; |
| 759 | } |
| 760 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 761 | /* Find the end of the header value contained between <s> and <e>. See RFC2616, |
| 762 | * par 2.2 for more information. Note that it requires a valid header to return |
| 763 | * a valid result. This works for headers defined as comma-separated lists. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 764 | */ |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 765 | char *find_hdr_value_end(char *s, const char *e) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 766 | { |
| 767 | int quoted, qdpair; |
| 768 | |
| 769 | quoted = qdpair = 0; |
| 770 | for (; s < e; s++) { |
| 771 | if (qdpair) qdpair = 0; |
Willy Tarreau | 0f7f51f | 2010-08-30 11:06:34 +0200 | [diff] [blame] | 772 | else if (quoted) { |
| 773 | if (*s == '\\') qdpair = 1; |
| 774 | else if (*s == '"') quoted = 0; |
| 775 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 776 | else if (*s == '"') quoted = 1; |
| 777 | else if (*s == ',') return s; |
| 778 | } |
| 779 | return s; |
| 780 | } |
| 781 | |
| 782 | /* Find the first or next occurrence of header <name> in message buffer <sol> |
| 783 | * using headers index <idx>, and return it in the <ctx> structure. This |
| 784 | * structure holds everything necessary to use the header and find next |
| 785 | * occurrence. If its <idx> member is 0, the header is searched from the |
| 786 | * beginning. Otherwise, the next occurrence is returned. The function returns |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 787 | * 1 when it finds a value, and 0 when there is no more. It is designed to work |
| 788 | * with headers defined as comma-separated lists. As a special case, if ctx->val |
| 789 | * is NULL when searching for a new values of a header, the current header is |
| 790 | * rescanned. This allows rescanning after a header deletion. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 791 | */ |
| 792 | int http_find_header2(const char *name, int len, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 793 | char *sol, struct hdr_idx *idx, |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 794 | struct hdr_ctx *ctx) |
| 795 | { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 796 | char *eol, *sov; |
| 797 | int cur_idx, old_idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 798 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 799 | cur_idx = ctx->idx; |
| 800 | if (cur_idx) { |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 801 | /* We have previously returned a value, let's search |
| 802 | * another one on the same line. |
| 803 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 804 | sol = ctx->line; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 805 | ctx->del = ctx->val + ctx->vlen + ctx->tws; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 806 | sov = sol + ctx->del; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 807 | eol = sol + idx->v[cur_idx].len; |
| 808 | |
| 809 | if (sov >= eol) |
| 810 | /* no more values in this header */ |
| 811 | goto next_hdr; |
| 812 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 813 | /* values remaining for this header, skip the comma but save it |
| 814 | * for later use (eg: for header deletion). |
| 815 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 816 | sov++; |
| 817 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 818 | sov++; |
| 819 | |
| 820 | goto return_hdr; |
| 821 | } |
| 822 | |
| 823 | /* first request for this header */ |
| 824 | sol += hdr_idx_first_pos(idx); |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 825 | old_idx = 0; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 826 | cur_idx = hdr_idx_first_idx(idx); |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 827 | while (cur_idx) { |
| 828 | eol = sol + idx->v[cur_idx].len; |
| 829 | |
Willy Tarreau | 1ad7c6d | 2007-06-10 21:42:55 +0200 | [diff] [blame] | 830 | if (len == 0) { |
| 831 | /* No argument was passed, we want any header. |
| 832 | * To achieve this, we simply build a fake request. */ |
| 833 | while (sol + len < eol && sol[len] != ':') |
| 834 | len++; |
| 835 | name = sol; |
| 836 | } |
| 837 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 838 | if ((len < eol - sol) && |
| 839 | (sol[len] == ':') && |
| 840 | (strncasecmp(sol, name, len) == 0)) { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 841 | ctx->del = len; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 842 | sov = sol + len + 1; |
| 843 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 844 | sov++; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 845 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 846 | ctx->line = sol; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 847 | ctx->prev = old_idx; |
| 848 | return_hdr: |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 849 | ctx->idx = cur_idx; |
| 850 | ctx->val = sov - sol; |
| 851 | |
| 852 | eol = find_hdr_value_end(sov, eol); |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 853 | ctx->tws = 0; |
Willy Tarreau | 275600b | 2011-09-16 08:11:26 +0200 | [diff] [blame] | 854 | while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) { |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 855 | eol--; |
| 856 | ctx->tws++; |
| 857 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 858 | ctx->vlen = eol - sov; |
| 859 | return 1; |
| 860 | } |
| 861 | next_hdr: |
| 862 | sol = eol + idx->v[cur_idx].cr + 1; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 863 | old_idx = cur_idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 864 | cur_idx = idx->v[cur_idx].next; |
| 865 | } |
| 866 | return 0; |
| 867 | } |
| 868 | |
| 869 | int http_find_header(const char *name, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 870 | char *sol, struct hdr_idx *idx, |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 871 | struct hdr_ctx *ctx) |
| 872 | { |
| 873 | return http_find_header2(name, strlen(name), sol, idx, ctx); |
| 874 | } |
| 875 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 876 | /* Remove one value of a header. This only works on a <ctx> returned by one of |
| 877 | * the http_find_header functions. The value is removed, as well as surrounding |
| 878 | * 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] | 879 | * 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] | 880 | * message <msg>. The new index is returned. If it is zero, it means there is |
| 881 | * no more header, so any processing may stop. The ctx is always left in a form |
| 882 | * that can be handled by http_find_header2() to find next occurrence. |
| 883 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 884 | 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] | 885 | { |
| 886 | int cur_idx = ctx->idx; |
| 887 | char *sol = ctx->line; |
| 888 | struct hdr_idx_elem *hdr; |
| 889 | int delta, skip_comma; |
| 890 | |
| 891 | if (!cur_idx) |
| 892 | return 0; |
| 893 | |
| 894 | hdr = &idx->v[cur_idx]; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 895 | if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 896 | /* 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] | 897 | 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] | 898 | http_msg_move_end(msg, delta); |
| 899 | idx->used--; |
| 900 | hdr->len = 0; /* unused entry */ |
| 901 | idx->v[ctx->prev].next = idx->v[ctx->idx].next; |
Willy Tarreau | 5c4784f | 2011-02-12 13:07:35 +0100 | [diff] [blame] | 902 | if (idx->tail == ctx->idx) |
| 903 | idx->tail = ctx->prev; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 904 | ctx->idx = ctx->prev; /* walk back to the end of previous header */ |
Willy Tarreau | 7c1c217 | 2015-01-07 17:23:50 +0100 | [diff] [blame] | 905 | 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] | 906 | 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] | 907 | ctx->tws = ctx->vlen = 0; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 908 | return ctx->idx; |
| 909 | } |
| 910 | |
| 911 | /* 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] | 912 | * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the |
| 913 | * last entry of the list, we remove the last separator. |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 914 | */ |
| 915 | |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 916 | skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 917 | delta = buffer_replace2(msg->chn->buf, sol + ctx->del + skip_comma, |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 918 | sol + ctx->val + ctx->vlen + ctx->tws + skip_comma, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 919 | NULL, 0); |
| 920 | hdr->len += delta; |
| 921 | http_msg_move_end(msg, delta); |
| 922 | ctx->val = ctx->del; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 923 | ctx->tws = ctx->vlen = 0; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 924 | return ctx->idx; |
| 925 | } |
| 926 | |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 927 | /* This function handles a server error at the stream interface level. The |
| 928 | * stream interface is assumed to be already in a closed state. An optional |
| 929 | * message is copied into the input buffer, and an HTTP status code stored. |
| 930 | * 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] | 931 | * in this buffer will be lost. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 932 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 933 | static void http_server_error(struct stream *s, struct stream_interface *si, |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 934 | int err, int finst, int status, const struct chunk *msg) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 935 | { |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 936 | channel_auto_read(si_oc(si)); |
| 937 | channel_abort(si_oc(si)); |
| 938 | channel_auto_close(si_oc(si)); |
| 939 | channel_erase(si_oc(si)); |
| 940 | channel_auto_close(si_ic(si)); |
| 941 | channel_auto_read(si_ic(si)); |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 942 | if (status > 0 && msg) { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 943 | s->txn->status = status; |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 944 | bo_inject(si_ic(si), msg->str, msg->len); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 945 | } |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 946 | if (!(s->flags & SF_ERR_MASK)) |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 947 | s->flags |= err; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 948 | if (!(s->flags & SF_FINST_MASK)) |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 949 | s->flags |= finst; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 950 | } |
| 951 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 952 | /* This function returns the appropriate error location for the given stream |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 953 | * and message. |
| 954 | */ |
| 955 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 956 | struct chunk *http_error_message(struct stream *s, int msgnum) |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 957 | { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 958 | if (s->be->errmsg[msgnum].str) |
| 959 | return &s->be->errmsg[msgnum]; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 960 | else if (strm_fe(s)->errmsg[msgnum].str) |
| 961 | return &strm_fe(s)->errmsg[msgnum]; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 962 | else |
| 963 | return &http_err_chunks[msgnum]; |
| 964 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 965 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 966 | /* |
Willy Tarreau | b7ce424 | 2015-09-03 17:15:21 +0200 | [diff] [blame] | 967 | * returns a known method among HTTP_METH_* or HTTP_METH_OTHER for all unknown |
| 968 | * ones. |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 969 | */ |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 970 | 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] | 971 | { |
| 972 | unsigned char m; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 973 | const struct http_method_desc *h; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 974 | |
| 975 | m = ((unsigned)*str - 'A'); |
| 976 | |
| 977 | if (m < 26) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 978 | for (h = http_methods[m]; h->len > 0; h++) { |
| 979 | if (unlikely(h->len != len)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 980 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 981 | if (likely(memcmp(str, h->text, h->len) == 0)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 982 | return h->meth; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 983 | }; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 984 | } |
Willy Tarreau | b7ce424 | 2015-09-03 17:15:21 +0200 | [diff] [blame] | 985 | return HTTP_METH_OTHER; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 986 | } |
| 987 | |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 988 | /* Parse the URI from the given transaction (which is assumed to be in request |
| 989 | * phase) and look for the "/" beginning the PATH. If not found, return NULL. |
| 990 | * It is returned otherwise. |
| 991 | */ |
Thierry FOURNIER | 3c33178 | 2015-09-17 19:33:35 +0200 | [diff] [blame] | 992 | char *http_get_path(struct http_txn *txn) |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 993 | { |
| 994 | char *ptr, *end; |
| 995 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 996 | ptr = txn->req.chn->buf->p + txn->req.sl.rq.u; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 997 | end = ptr + txn->req.sl.rq.u_l; |
| 998 | |
| 999 | if (ptr >= end) |
| 1000 | return NULL; |
| 1001 | |
| 1002 | /* RFC2616, par. 5.1.2 : |
| 1003 | * Request-URI = "*" | absuri | abspath | authority |
| 1004 | */ |
| 1005 | |
| 1006 | if (*ptr == '*') |
| 1007 | return NULL; |
| 1008 | |
| 1009 | if (isalpha((unsigned char)*ptr)) { |
| 1010 | /* this is a scheme as described by RFC3986, par. 3.1 */ |
| 1011 | ptr++; |
| 1012 | while (ptr < end && |
| 1013 | (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')) |
| 1014 | ptr++; |
| 1015 | /* skip '://' */ |
| 1016 | if (ptr == end || *ptr++ != ':') |
| 1017 | return NULL; |
| 1018 | if (ptr == end || *ptr++ != '/') |
| 1019 | return NULL; |
| 1020 | if (ptr == end || *ptr++ != '/') |
| 1021 | return NULL; |
| 1022 | } |
| 1023 | /* skip [user[:passwd]@]host[:[port]] */ |
| 1024 | |
| 1025 | while (ptr < end && *ptr != '/') |
| 1026 | ptr++; |
| 1027 | |
| 1028 | if (ptr == end) |
| 1029 | return NULL; |
| 1030 | |
| 1031 | /* OK, we got the '/' ! */ |
| 1032 | return ptr; |
| 1033 | } |
| 1034 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 1035 | /* Parse the URI from the given string and look for the "/" beginning the PATH. |
| 1036 | * If not found, return NULL. It is returned otherwise. |
| 1037 | */ |
| 1038 | static char * |
| 1039 | http_get_path_from_string(char *str) |
| 1040 | { |
| 1041 | char *ptr = str; |
| 1042 | |
| 1043 | /* RFC2616, par. 5.1.2 : |
| 1044 | * Request-URI = "*" | absuri | abspath | authority |
| 1045 | */ |
| 1046 | |
| 1047 | if (*ptr == '*') |
| 1048 | return NULL; |
| 1049 | |
| 1050 | if (isalpha((unsigned char)*ptr)) { |
| 1051 | /* this is a scheme as described by RFC3986, par. 3.1 */ |
| 1052 | ptr++; |
| 1053 | while (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.') |
| 1054 | ptr++; |
| 1055 | /* skip '://' */ |
| 1056 | if (*ptr == '\0' || *ptr++ != ':') |
| 1057 | return NULL; |
| 1058 | if (*ptr == '\0' || *ptr++ != '/') |
| 1059 | return NULL; |
| 1060 | if (*ptr == '\0' || *ptr++ != '/') |
| 1061 | return NULL; |
| 1062 | } |
| 1063 | /* skip [user[:passwd]@]host[:[port]] */ |
| 1064 | |
| 1065 | while (*ptr != '\0' && *ptr != ' ' && *ptr != '/') |
| 1066 | ptr++; |
| 1067 | |
| 1068 | if (*ptr == '\0' || *ptr == ' ') |
| 1069 | return NULL; |
| 1070 | |
| 1071 | /* OK, we got the '/' ! */ |
| 1072 | return ptr; |
| 1073 | } |
| 1074 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 1075 | /* Returns a 302 for a redirectable request that reaches a server working in |
| 1076 | * in redirect mode. This may only be called just after the stream interface |
| 1077 | * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will |
| 1078 | * follow normal proxy processing. NOTE: this function is designed to support |
| 1079 | * being called once data are scheduled for forwarding. |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1080 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1081 | void http_perform_server_redirect(struct stream *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1082 | { |
| 1083 | struct http_txn *txn; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 1084 | struct server *srv; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1085 | char *path; |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 1086 | int len, rewind; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1087 | |
| 1088 | /* 1: create the response header */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1089 | trash.len = strlen(HTTP_302); |
| 1090 | memcpy(trash.str, HTTP_302, trash.len); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1091 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 1092 | srv = objt_server(s->target); |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 1093 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1094 | /* 2: add the server's prefix */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1095 | if (trash.len + srv->rdr_len > trash.size) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1096 | return; |
| 1097 | |
Willy Tarreau | dcb75c4 | 2010-01-10 00:24:22 +0100 | [diff] [blame] | 1098 | /* special prefix "/" means don't change URL */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 1099 | if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1100 | memcpy(trash.str + trash.len, srv->rdr_pfx, srv->rdr_len); |
| 1101 | trash.len += srv->rdr_len; |
Willy Tarreau | dcb75c4 | 2010-01-10 00:24:22 +0100 | [diff] [blame] | 1102 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1103 | |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 1104 | /* 3: add the request URI. Since it was already forwarded, we need |
| 1105 | * to temporarily rewind the buffer. |
| 1106 | */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 1107 | txn = s->txn; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1108 | b_rew(s->req.buf, rewind = http_hdr_rewind(&txn->req)); |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 1109 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1110 | path = http_get_path(txn); |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1111 | 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] | 1112 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1113 | b_adv(s->req.buf, rewind); |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 1114 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1115 | if (!path) |
| 1116 | return; |
| 1117 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1118 | if (trash.len + len > trash.size - 4) /* 4 for CRLF-CRLF */ |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1119 | return; |
| 1120 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1121 | memcpy(trash.str + trash.len, path, len); |
| 1122 | trash.len += len; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1123 | |
| 1124 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1125 | memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29); |
| 1126 | trash.len += 29; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1127 | } else { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 1128 | memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23); |
| 1129 | trash.len += 23; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1130 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1131 | |
| 1132 | /* prepare to return without error. */ |
Willy Tarreau | 73b013b | 2012-05-21 16:31:45 +0200 | [diff] [blame] | 1133 | si_shutr(si); |
| 1134 | si_shutw(si); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1135 | si->err_type = SI_ET_NONE; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1136 | si->state = SI_ST_CLO; |
| 1137 | |
| 1138 | /* send the message */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1139 | http_server_error(s, si, SF_ERR_LOCAL, SF_FINST_C, 302, &trash); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1140 | |
| 1141 | /* 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] | 1142 | srv_inc_sess_ctr(srv); |
Bhaskar Maddala | a20cb85 | 2014-02-03 16:26:46 -0500 | [diff] [blame] | 1143 | srv_set_sess_last(srv); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1144 | } |
| 1145 | |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 1146 | /* Return the error message corresponding to si->err_type. It is assumed |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1147 | * that the server side is closed. Note that err_type is actually a |
| 1148 | * bitmask, where almost only aborts may be cumulated with other |
| 1149 | * values. We consider that aborted operations are more important |
| 1150 | * than timeouts or errors due to the fact that nobody else in the |
| 1151 | * logs might explain incomplete retries. All others should avoid |
| 1152 | * being cumulated. It should normally not be possible to have multiple |
| 1153 | * 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] | 1154 | * Note that connection errors appearing on the second request of a keep-alive |
| 1155 | * connection are not reported since this allows the client to retry. |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1156 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1157 | void http_return_srv_error(struct stream *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1158 | { |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 1159 | int err_type = si->err_type; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1160 | |
| 1161 | if (err_type & SI_ET_QUEUE_ABRT) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1162 | http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q, |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 1163 | 503, http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1164 | else if (err_type & SI_ET_CONN_ABRT) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1165 | http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C, |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 1166 | 503, (s->txn->flags & TX_NOT_FIRST) ? NULL : |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 1167 | http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1168 | else if (err_type & SI_ET_QUEUE_TO) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1169 | http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q, |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 1170 | 503, http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1171 | else if (err_type & SI_ET_QUEUE_ERR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1172 | http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q, |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 1173 | 503, http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1174 | else if (err_type & SI_ET_CONN_TO) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1175 | http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C, |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 1176 | 503, (s->txn->flags & TX_NOT_FIRST) ? NULL : |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 1177 | http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1178 | else if (err_type & SI_ET_CONN_ERR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1179 | http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C, |
| 1180 | 503, (s->flags & SF_SRV_REUSED) ? NULL : |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 1181 | http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | 2d400bb | 2012-05-14 12:11:47 +0200 | [diff] [blame] | 1182 | else if (err_type & SI_ET_CONN_RES) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1183 | http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C, |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 1184 | 503, (s->txn->flags & TX_NOT_FIRST) ? NULL : |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 1185 | http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1186 | else /* SI_ET_CONN_OTHER and others */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1187 | http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C, |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 1188 | 500, http_error_message(s, HTTP_ERR_500)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1189 | } |
| 1190 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1191 | extern const char sess_term_cond[8]; |
| 1192 | extern const char sess_fin_state[8]; |
| 1193 | extern const char *monthname[12]; |
Willy Tarreau | 63986c7 | 2015-04-03 22:55:33 +0200 | [diff] [blame] | 1194 | struct pool_head *pool2_http_txn; |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 1195 | struct pool_head *pool2_requri; |
Willy Tarreau | 193b8c6 | 2012-11-22 00:17:38 +0100 | [diff] [blame] | 1196 | struct pool_head *pool2_capture = NULL; |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 1197 | struct pool_head *pool2_uniqueid; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1198 | |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1199 | /* |
| 1200 | * Capture headers from message starting at <som> according to header list |
Willy Tarreau | 54da8db | 2014-06-13 16:11:48 +0200 | [diff] [blame] | 1201 | * <cap_hdr>, and fill the <cap> pointers appropriately. |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1202 | */ |
| 1203 | void capture_headers(char *som, struct hdr_idx *idx, |
| 1204 | char **cap, struct cap_hdr *cap_hdr) |
| 1205 | { |
| 1206 | char *eol, *sol, *col, *sov; |
| 1207 | int cur_idx; |
| 1208 | struct cap_hdr *h; |
| 1209 | int len; |
| 1210 | |
| 1211 | sol = som + hdr_idx_first_pos(idx); |
| 1212 | cur_idx = hdr_idx_first_idx(idx); |
| 1213 | |
| 1214 | while (cur_idx) { |
| 1215 | eol = sol + idx->v[cur_idx].len; |
| 1216 | |
| 1217 | col = sol; |
| 1218 | while (col < eol && *col != ':') |
| 1219 | col++; |
| 1220 | |
| 1221 | sov = col + 1; |
| 1222 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 1223 | sov++; |
| 1224 | |
| 1225 | for (h = cap_hdr; h; h = h->next) { |
Willy Tarreau | 54da8db | 2014-06-13 16:11:48 +0200 | [diff] [blame] | 1226 | if (h->namelen && (h->namelen == col - sol) && |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1227 | (strncasecmp(sol, h->name, h->namelen) == 0)) { |
| 1228 | if (cap[h->index] == NULL) |
| 1229 | cap[h->index] = |
Willy Tarreau | cf7f320 | 2007-05-13 22:46:04 +0200 | [diff] [blame] | 1230 | pool_alloc2(h->pool); |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1231 | |
| 1232 | if (cap[h->index] == NULL) { |
| 1233 | Alert("HTTP capture : out of memory.\n"); |
| 1234 | continue; |
| 1235 | } |
| 1236 | |
| 1237 | len = eol - sov; |
| 1238 | if (len > h->len) |
| 1239 | len = h->len; |
| 1240 | |
| 1241 | memcpy(cap[h->index], sov, len); |
| 1242 | cap[h->index][len]=0; |
| 1243 | } |
| 1244 | } |
| 1245 | sol = eol + idx->v[cur_idx].cr + 1; |
| 1246 | cur_idx = idx->v[cur_idx].next; |
| 1247 | } |
| 1248 | } |
| 1249 | |
| 1250 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1251 | /* either we find an LF at <ptr> or we jump to <bad>. |
| 1252 | */ |
| 1253 | #define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0) |
| 1254 | |
| 1255 | /* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK, |
| 1256 | * otherwise to <http_msg_ood> with <state> set to <st>. |
| 1257 | */ |
| 1258 | #define EAT_AND_JUMP_OR_RETURN(good, st) do { \ |
| 1259 | ptr++; \ |
| 1260 | if (likely(ptr < end)) \ |
| 1261 | goto good; \ |
| 1262 | else { \ |
| 1263 | state = (st); \ |
| 1264 | goto http_msg_ood; \ |
| 1265 | } \ |
| 1266 | } while (0) |
| 1267 | |
| 1268 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1269 | /* |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 1270 | * This function parses a status line between <ptr> and <end>, starting with |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1271 | * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP, |
| 1272 | * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others |
| 1273 | * will give undefined results. |
| 1274 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1275 | * and that msg->sol points to the beginning of the response. |
| 1276 | * If a complete line is found (which implies that at least one CR or LF is |
| 1277 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1278 | * returned indicating an incomplete line (which does not mean that parts have |
| 1279 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1280 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1281 | * upon next call. |
| 1282 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1283 | * This function was intentionally designed to be called from |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1284 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1285 | * within its state machine and use the same macros, hence the need for same |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1286 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1287 | */ |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1288 | const char *http_parse_stsline(struct http_msg *msg, |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1289 | enum ht_state state, const char *ptr, const char *end, |
| 1290 | unsigned int *ret_ptr, enum ht_state *ret_state) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1291 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1292 | const char *msg_start = msg->chn->buf->p; |
Willy Tarreau | 62f791e | 2012-03-09 11:32:30 +0100 | [diff] [blame] | 1293 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1294 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1295 | case HTTP_MSG_RPVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1296 | http_msg_rpver: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1297 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1298 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER); |
| 1299 | |
| 1300 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1301 | msg->sl.st.v_l = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1302 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 1303 | } |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1304 | state = HTTP_MSG_ERROR; |
| 1305 | break; |
| 1306 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1307 | case HTTP_MSG_RPVER_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1308 | http_msg_rpver_sp: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1309 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1310 | msg->sl.st.c = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1311 | goto http_msg_rpcode; |
| 1312 | } |
| 1313 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1314 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 1315 | /* so it's a CR/LF, this is invalid */ |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1316 | state = HTTP_MSG_ERROR; |
| 1317 | break; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1318 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1319 | case HTTP_MSG_RPCODE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1320 | http_msg_rpcode: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1321 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 1322 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE); |
| 1323 | |
| 1324 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1325 | msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1326 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1327 | } |
| 1328 | |
| 1329 | /* so it's a CR/LF, so there is no reason phrase */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1330 | msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1331 | http_msg_rsp_reason: |
| 1332 | /* FIXME: should we support HTTP responses without any reason phrase ? */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1333 | msg->sl.st.r = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1334 | msg->sl.st.r_l = 0; |
| 1335 | goto http_msg_rpline_eol; |
| 1336 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1337 | case HTTP_MSG_RPCODE_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1338 | http_msg_rpcode_sp: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1339 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1340 | msg->sl.st.r = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1341 | goto http_msg_rpreason; |
| 1342 | } |
| 1343 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1344 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1345 | /* so it's a CR/LF, so there is no reason phrase */ |
| 1346 | goto http_msg_rsp_reason; |
| 1347 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1348 | case HTTP_MSG_RPREASON: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1349 | http_msg_rpreason: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1350 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1351 | EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON); |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1352 | msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1353 | http_msg_rpline_eol: |
| 1354 | /* We have seen the end of line. Note that we do not |
| 1355 | * necessarily have the \n yet, but at least we know that we |
| 1356 | * have EITHER \r OR \n, otherwise the response would not be |
| 1357 | * complete. We can then record the response length and return |
| 1358 | * to the caller which will be able to register it. |
| 1359 | */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1360 | msg->sl.st.l = ptr - msg_start - msg->sol; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1361 | return ptr; |
| 1362 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1363 | default: |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1364 | #ifdef DEBUG_FULL |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1365 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1366 | exit(1); |
| 1367 | #endif |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1368 | ; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1369 | } |
| 1370 | |
| 1371 | http_msg_ood: |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1372 | /* out of valid data */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1373 | if (ret_state) |
| 1374 | *ret_state = state; |
| 1375 | if (ret_ptr) |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1376 | *ret_ptr = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1377 | return NULL; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1378 | } |
| 1379 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1380 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1381 | * This function parses a request line between <ptr> and <end>, starting with |
| 1382 | * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP, |
| 1383 | * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others |
| 1384 | * will give undefined results. |
| 1385 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1386 | * and that msg->sol points to the beginning of the request. |
| 1387 | * If a complete line is found (which implies that at least one CR or LF is |
| 1388 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1389 | * returned indicating an incomplete line (which does not mean that parts have |
| 1390 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1391 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1392 | * upon next call. |
| 1393 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1394 | * This function was intentionally designed to be called from |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1395 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1396 | * within its state machine and use the same macros, hence the need for same |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1397 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1398 | */ |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1399 | const char *http_parse_reqline(struct http_msg *msg, |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1400 | enum ht_state state, const char *ptr, const char *end, |
| 1401 | unsigned int *ret_ptr, enum ht_state *ret_state) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1402 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1403 | const char *msg_start = msg->chn->buf->p; |
Willy Tarreau | 62f791e | 2012-03-09 11:32:30 +0100 | [diff] [blame] | 1404 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1405 | switch (state) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1406 | case HTTP_MSG_RQMETH: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1407 | http_msg_rqmeth: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1408 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1409 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1410 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1411 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1412 | msg->sl.rq.m_l = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1413 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1414 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1415 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1416 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1417 | /* HTTP 0.9 request */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1418 | msg->sl.rq.m_l = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1419 | http_msg_req09_uri: |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1420 | msg->sl.rq.u = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1421 | http_msg_req09_uri_e: |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1422 | msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1423 | http_msg_req09_ver: |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1424 | msg->sl.rq.v = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1425 | msg->sl.rq.v_l = 0; |
| 1426 | goto http_msg_rqline_eol; |
| 1427 | } |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1428 | state = HTTP_MSG_ERROR; |
| 1429 | break; |
| 1430 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1431 | case HTTP_MSG_RQMETH_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1432 | http_msg_rqmeth_sp: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1433 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1434 | msg->sl.rq.u = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1435 | goto http_msg_rquri; |
| 1436 | } |
| 1437 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1438 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1439 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1440 | goto http_msg_req09_uri; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1441 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1442 | case HTTP_MSG_RQURI: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1443 | http_msg_rquri: |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1444 | if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1445 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1446 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1447 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1448 | msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1449 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1450 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1451 | |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1452 | if (likely((unsigned char)*ptr >= 128)) { |
Willy Tarreau | 422246e | 2012-01-07 23:54:13 +0100 | [diff] [blame] | 1453 | /* non-ASCII chars are forbidden unless option |
| 1454 | * accept-invalid-http-request is enabled in the frontend. |
| 1455 | * In any case, we capture the faulty char. |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1456 | */ |
Willy Tarreau | 422246e | 2012-01-07 23:54:13 +0100 | [diff] [blame] | 1457 | if (msg->err_pos < -1) |
| 1458 | goto invalid_char; |
| 1459 | if (msg->err_pos == -1) |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1460 | msg->err_pos = ptr - msg_start; |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1461 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI); |
| 1462 | } |
| 1463 | |
| 1464 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1465 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1466 | goto http_msg_req09_uri_e; |
| 1467 | } |
| 1468 | |
| 1469 | /* OK forbidden chars, 0..31 or 127 */ |
Willy Tarreau | 422246e | 2012-01-07 23:54:13 +0100 | [diff] [blame] | 1470 | invalid_char: |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1471 | msg->err_pos = ptr - msg_start; |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1472 | state = HTTP_MSG_ERROR; |
| 1473 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1474 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1475 | case HTTP_MSG_RQURI_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1476 | http_msg_rquri_sp: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1477 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1478 | msg->sl.rq.v = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1479 | goto http_msg_rqver; |
| 1480 | } |
| 1481 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1482 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1483 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1484 | goto http_msg_req09_ver; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1485 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1486 | case HTTP_MSG_RQVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1487 | http_msg_rqver: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1488 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1489 | EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER); |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1490 | |
| 1491 | if (likely(HTTP_IS_CRLF(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1492 | msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v; |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1493 | http_msg_rqline_eol: |
| 1494 | /* We have seen the end of line. Note that we do not |
| 1495 | * necessarily have the \n yet, but at least we know that we |
| 1496 | * have EITHER \r OR \n, otherwise the request would not be |
| 1497 | * complete. We can then record the request length and return |
| 1498 | * to the caller which will be able to register it. |
| 1499 | */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1500 | msg->sl.rq.l = ptr - msg_start - msg->sol; |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1501 | return ptr; |
| 1502 | } |
| 1503 | |
| 1504 | /* neither an HTTP_VER token nor a CRLF */ |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1505 | state = HTTP_MSG_ERROR; |
| 1506 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1507 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1508 | default: |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1509 | #ifdef DEBUG_FULL |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1510 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1511 | exit(1); |
| 1512 | #endif |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1513 | ; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1514 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1515 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1516 | http_msg_ood: |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1517 | /* out of valid data */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1518 | if (ret_state) |
| 1519 | *ret_state = state; |
| 1520 | if (ret_ptr) |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1521 | *ret_ptr = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1522 | return NULL; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1523 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1524 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1525 | /* |
| 1526 | * Returns the data from Authorization header. Function may be called more |
| 1527 | * than once so data is stored in txn->auth_data. When no header is found |
| 1528 | * 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] | 1529 | * searching again for something we are unable to find anyway. However, if |
| 1530 | * 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] | 1531 | * have the credentials overwritten by another stream in parallel. |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1532 | */ |
| 1533 | |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 1534 | /* This bufffer is initialized in the file 'src/haproxy.c'. This length is |
| 1535 | * set according to global.tune.bufsize. |
| 1536 | */ |
Willy Tarreau | 7e2c647 | 2012-10-29 20:44:36 +0100 | [diff] [blame] | 1537 | char *get_http_auth_buff; |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1538 | |
| 1539 | int |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1540 | get_http_auth(struct stream *s) |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1541 | { |
| 1542 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 1543 | struct http_txn *txn = s->txn; |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1544 | struct chunk auth_method; |
| 1545 | struct hdr_ctx ctx; |
| 1546 | char *h, *p; |
| 1547 | int len; |
| 1548 | |
| 1549 | #ifdef DEBUG_AUTH |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1550 | printf("Auth for stream %p: %d\n", s, txn->auth.method); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1551 | #endif |
| 1552 | |
| 1553 | if (txn->auth.method == HTTP_AUTH_WRONG) |
| 1554 | return 0; |
| 1555 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1556 | txn->auth.method = HTTP_AUTH_WRONG; |
| 1557 | |
| 1558 | ctx.idx = 0; |
Willy Tarreau | 844a7e7 | 2010-01-31 21:46:18 +0100 | [diff] [blame] | 1559 | |
| 1560 | if (txn->flags & TX_USE_PX_CONN) { |
| 1561 | h = "Proxy-Authorization"; |
| 1562 | len = strlen(h); |
| 1563 | } else { |
| 1564 | h = "Authorization"; |
| 1565 | len = strlen(h); |
| 1566 | } |
| 1567 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1568 | 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] | 1569 | return 0; |
| 1570 | |
| 1571 | h = ctx.line + ctx.val; |
| 1572 | |
| 1573 | p = memchr(h, ' ', ctx.vlen); |
| 1574 | if (!p || p == h) |
| 1575 | return 0; |
| 1576 | |
| 1577 | chunk_initlen(&auth_method, h, 0, p-h); |
| 1578 | chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1); |
| 1579 | |
| 1580 | if (!strncasecmp("Basic", auth_method.str, auth_method.len)) { |
| 1581 | |
| 1582 | len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len, |
Willy Tarreau | 7e2c647 | 2012-10-29 20:44:36 +0100 | [diff] [blame] | 1583 | get_http_auth_buff, global.tune.bufsize - 1); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1584 | |
| 1585 | if (len < 0) |
| 1586 | return 0; |
| 1587 | |
| 1588 | |
| 1589 | get_http_auth_buff[len] = '\0'; |
| 1590 | |
| 1591 | p = strchr(get_http_auth_buff, ':'); |
| 1592 | |
| 1593 | if (!p) |
| 1594 | return 0; |
| 1595 | |
| 1596 | txn->auth.user = get_http_auth_buff; |
| 1597 | *p = '\0'; |
| 1598 | txn->auth.pass = p+1; |
| 1599 | |
| 1600 | txn->auth.method = HTTP_AUTH_BASIC; |
| 1601 | return 1; |
| 1602 | } |
| 1603 | |
| 1604 | return 0; |
| 1605 | } |
| 1606 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1607 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1608 | /* |
| 1609 | * This function parses an HTTP message, either a request or a response, |
Willy Tarreau | 8b1323e | 2012-03-09 14:46:19 +0100 | [diff] [blame] | 1610 | * depending on the initial msg->msg_state. The caller is responsible for |
| 1611 | * ensuring that the message does not wrap. The function can be preempted |
| 1612 | * everywhere when data are missing and recalled at the exact same location |
| 1613 | * with no information loss. The message may even be realigned between two |
| 1614 | * calls. The header index is re-initialized when switching from |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1615 | * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 1616 | * fields. Note that msg->sol will be initialized after completing the first |
| 1617 | * state, so that none of the msg pointers has to be initialized prior to the |
| 1618 | * first call. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1619 | */ |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 1620 | void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1621 | { |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1622 | enum ht_state state; /* updated only when leaving the FSM */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1623 | register char *ptr, *end; /* request pointers, to avoid dereferences */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1624 | struct buffer *buf; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1625 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1626 | state = msg->msg_state; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1627 | buf = msg->chn->buf; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1628 | ptr = buf->p + msg->next; |
| 1629 | end = buf->p + buf->i; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1630 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1631 | if (unlikely(ptr >= end)) |
| 1632 | goto http_msg_ood; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1633 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1634 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1635 | /* |
| 1636 | * First, states that are specific to the response only. |
| 1637 | * We check them first so that request and headers are |
| 1638 | * closer to each other (accessed more often). |
| 1639 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1640 | case HTTP_MSG_RPBEFORE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1641 | http_msg_rpbefore: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1642 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1643 | /* we have a start of message, but we have to check |
| 1644 | * first if we need to remove some CRLF. We can only |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 1645 | * do this when o=0. |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1646 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1647 | if (unlikely(ptr != buf->p)) { |
| 1648 | if (buf->o) |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1649 | goto http_msg_ood; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1650 | /* Remove empty leading lines, as recommended by RFC2616. */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1651 | bi_fast_delete(buf, ptr - buf->p); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1652 | } |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 1653 | msg->sol = 0; |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 1654 | msg->sl.st.l = 0; /* used in debug mode */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1655 | hdr_idx_init(idx); |
| 1656 | state = HTTP_MSG_RPVER; |
| 1657 | goto http_msg_rpver; |
| 1658 | } |
| 1659 | |
| 1660 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1661 | goto http_msg_invalid; |
| 1662 | |
| 1663 | if (unlikely(*ptr == '\n')) |
| 1664 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1665 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR); |
| 1666 | /* stop here */ |
| 1667 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1668 | case HTTP_MSG_RPBEFORE_CR: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1669 | http_msg_rpbefore_cr: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1670 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1671 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1672 | /* stop here */ |
| 1673 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1674 | case HTTP_MSG_RPVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1675 | http_msg_rpver: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1676 | case HTTP_MSG_RPVER_SP: |
| 1677 | case HTTP_MSG_RPCODE: |
| 1678 | case HTTP_MSG_RPCODE_SP: |
| 1679 | case HTTP_MSG_RPREASON: |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1680 | ptr = (char *)http_parse_stsline(msg, |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1681 | state, ptr, end, |
| 1682 | &msg->next, &msg->msg_state); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1683 | if (unlikely(!ptr)) |
| 1684 | return; |
| 1685 | |
| 1686 | /* we have a full response and we know that we have either a CR |
| 1687 | * or an LF at <ptr>. |
| 1688 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1689 | hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r'); |
| 1690 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1691 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1692 | if (likely(*ptr == '\r')) |
| 1693 | EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END); |
| 1694 | goto http_msg_rpline_end; |
| 1695 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1696 | case HTTP_MSG_RPLINE_END: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1697 | http_msg_rpline_end: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1698 | /* msg->sol must point to the first of CR or LF. */ |
| 1699 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1700 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST); |
| 1701 | /* stop here */ |
| 1702 | |
| 1703 | /* |
| 1704 | * Second, states that are specific to the request only |
| 1705 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1706 | case HTTP_MSG_RQBEFORE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1707 | http_msg_rqbefore: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1708 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1709 | /* we have a start of message, but we have to check |
| 1710 | * first if we need to remove some CRLF. We can only |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 1711 | * do this when o=0. |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1712 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1713 | if (likely(ptr != buf->p)) { |
| 1714 | if (buf->o) |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1715 | goto http_msg_ood; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1716 | /* Remove empty leading lines, as recommended by RFC2616. */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1717 | bi_fast_delete(buf, ptr - buf->p); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1718 | } |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 1719 | msg->sol = 0; |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 1720 | msg->sl.rq.l = 0; /* used in debug mode */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1721 | state = HTTP_MSG_RQMETH; |
| 1722 | goto http_msg_rqmeth; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1723 | } |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1724 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1725 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1726 | goto http_msg_invalid; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1727 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1728 | if (unlikely(*ptr == '\n')) |
| 1729 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
| 1730 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1731 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1732 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1733 | case HTTP_MSG_RQBEFORE_CR: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1734 | http_msg_rqbefore_cr: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1735 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1736 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1737 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1738 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1739 | case HTTP_MSG_RQMETH: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1740 | http_msg_rqmeth: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1741 | case HTTP_MSG_RQMETH_SP: |
| 1742 | case HTTP_MSG_RQURI: |
| 1743 | case HTTP_MSG_RQURI_SP: |
| 1744 | case HTTP_MSG_RQVER: |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1745 | ptr = (char *)http_parse_reqline(msg, |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1746 | state, ptr, end, |
| 1747 | &msg->next, &msg->msg_state); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1748 | if (unlikely(!ptr)) |
| 1749 | return; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1750 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1751 | /* we have a full request and we know that we have either a CR |
| 1752 | * or an LF at <ptr>. |
| 1753 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1754 | hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r'); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1755 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1756 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1757 | if (likely(*ptr == '\r')) |
| 1758 | EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1759 | goto http_msg_rqline_end; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1760 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1761 | case HTTP_MSG_RQLINE_END: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1762 | http_msg_rqline_end: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1763 | /* check for HTTP/0.9 request : no version information available. |
| 1764 | * msg->sol must point to the first of CR or LF. |
| 1765 | */ |
| 1766 | if (unlikely(msg->sl.rq.v_l == 0)) |
| 1767 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1768 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1769 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1770 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1771 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1772 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1773 | /* |
| 1774 | * Common states below |
| 1775 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1776 | case HTTP_MSG_HDR_FIRST: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1777 | http_msg_hdr_first: |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1778 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1779 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1780 | goto http_msg_hdr_name; |
| 1781 | } |
| 1782 | |
| 1783 | if (likely(*ptr == '\r')) |
| 1784 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1785 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1786 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1787 | case HTTP_MSG_HDR_NAME: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1788 | http_msg_hdr_name: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1789 | /* assumes msg->sol points to the first char */ |
| 1790 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1791 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1792 | |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1793 | if (likely(*ptr == ':')) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1794 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1795 | |
Willy Tarreau | 32a4ec0 | 2009-04-02 11:35:18 +0200 | [diff] [blame] | 1796 | if (likely(msg->err_pos < -1) || *ptr == '\n') |
| 1797 | goto http_msg_invalid; |
| 1798 | |
| 1799 | if (msg->err_pos == -1) /* capture error pointer */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1800 | msg->err_pos = ptr - buf->p; /* >= 0 now */ |
Willy Tarreau | 32a4ec0 | 2009-04-02 11:35:18 +0200 | [diff] [blame] | 1801 | |
| 1802 | /* and we still accept this non-token character */ |
| 1803 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME); |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1804 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1805 | case HTTP_MSG_HDR_L1_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1806 | http_msg_hdr_l1_sp: |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1807 | /* assumes msg->sol points to the first char */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1808 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1809 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP); |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1810 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1811 | /* header value can be basically anything except CR/LF */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1812 | msg->sov = ptr - buf->p; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1813 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1814 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1815 | goto http_msg_hdr_val; |
| 1816 | } |
| 1817 | |
| 1818 | if (likely(*ptr == '\r')) |
| 1819 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF); |
| 1820 | goto http_msg_hdr_l1_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1821 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1822 | case HTTP_MSG_HDR_L1_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1823 | http_msg_hdr_l1_lf: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1824 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1825 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1826 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1827 | case HTTP_MSG_HDR_L1_LWS: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1828 | http_msg_hdr_l1_lws: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1829 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1830 | /* replace HT,CR,LF with spaces */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1831 | for (; buf->p + msg->sov < ptr; msg->sov++) |
| 1832 | buf->p[msg->sov] = ' '; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1833 | goto http_msg_hdr_l1_sp; |
| 1834 | } |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 1835 | /* we had a header consisting only in spaces ! */ |
Willy Tarreau | 12e48b3 | 2012-03-05 16:57:34 +0100 | [diff] [blame] | 1836 | msg->eol = msg->sov; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1837 | goto http_msg_complete_header; |
| 1838 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1839 | case HTTP_MSG_HDR_VAL: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1840 | http_msg_hdr_val: |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1841 | /* assumes msg->sol points to the first char, and msg->sov |
| 1842 | * points to the first character of the value. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1843 | */ |
| 1844 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1845 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1846 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1847 | msg->eol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1848 | /* Note: we could also copy eol into ->eoh so that we have the |
| 1849 | * real header end in case it ends with lots of LWS, but is this |
| 1850 | * really needed ? |
| 1851 | */ |
| 1852 | if (likely(*ptr == '\r')) |
| 1853 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF); |
| 1854 | goto http_msg_hdr_l2_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1855 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1856 | case HTTP_MSG_HDR_L2_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1857 | http_msg_hdr_l2_lf: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1858 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1859 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1860 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1861 | case HTTP_MSG_HDR_L2_LWS: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1862 | http_msg_hdr_l2_lws: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1863 | if (unlikely(HTTP_IS_SPHT(*ptr))) { |
| 1864 | /* LWS: replace HT,CR,LF with spaces */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1865 | for (; buf->p + msg->eol < ptr; msg->eol++) |
| 1866 | buf->p[msg->eol] = ' '; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1867 | goto http_msg_hdr_val; |
| 1868 | } |
| 1869 | http_msg_complete_header: |
| 1870 | /* |
| 1871 | * It was a new header, so the last one is finished. |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1872 | * Assumes msg->sol points to the first char, msg->sov points |
| 1873 | * to the first character of the value and msg->eol to the |
| 1874 | * first CR or LF so we know how the line ends. We insert last |
| 1875 | * header into the index. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1876 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1877 | if (unlikely(hdr_idx_add(msg->eol - msg->sol, buf->p[msg->eol] == '\r', |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1878 | idx, idx->tail) < 0)) |
| 1879 | goto http_msg_invalid; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1880 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1881 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1882 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1883 | goto http_msg_hdr_name; |
| 1884 | } |
| 1885 | |
| 1886 | if (likely(*ptr == '\r')) |
| 1887 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1888 | goto http_msg_last_lf; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1889 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1890 | case HTTP_MSG_LAST_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1891 | http_msg_last_lf: |
Willy Tarreau | 0558a02 | 2014-03-13 15:48:45 +0100 | [diff] [blame] | 1892 | /* Assumes msg->sol points to the first of either CR or LF. |
| 1893 | * Sets ->sov and ->next to the total header length, ->eoh to |
| 1894 | * the last CRLF, and ->eol to the last CRLF length (1 or 2). |
| 1895 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1896 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1897 | ptr++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1898 | msg->sov = msg->next = ptr - buf->p; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1899 | msg->eoh = msg->sol; |
| 1900 | msg->sol = 0; |
Willy Tarreau | 0558a02 | 2014-03-13 15:48:45 +0100 | [diff] [blame] | 1901 | msg->eol = msg->sov - msg->eoh; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1902 | msg->msg_state = HTTP_MSG_BODY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1903 | return; |
Willy Tarreau | b56928a | 2012-04-16 14:51:55 +0200 | [diff] [blame] | 1904 | |
| 1905 | case HTTP_MSG_ERROR: |
| 1906 | /* this may only happen if we call http_msg_analyser() twice with an error */ |
| 1907 | break; |
| 1908 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1909 | default: |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1910 | #ifdef DEBUG_FULL |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1911 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1912 | exit(1); |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1913 | #endif |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 1914 | ; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1915 | } |
| 1916 | http_msg_ood: |
| 1917 | /* out of data */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1918 | msg->msg_state = state; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1919 | msg->next = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1920 | return; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1921 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1922 | http_msg_invalid: |
| 1923 | /* invalid message */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1924 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1925 | msg->next = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1926 | return; |
| 1927 | } |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1928 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1929 | /* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the |
| 1930 | * conversion succeeded, 0 in case of error. If the request was already 1.X, |
| 1931 | * nothing is done and 1 is returned. |
| 1932 | */ |
Willy Tarreau | 418bfcc | 2012-03-09 13:56:20 +0100 | [diff] [blame] | 1933 | static int http_upgrade_v09_to_v10(struct http_txn *txn) |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1934 | { |
| 1935 | int delta; |
| 1936 | char *cur_end; |
Willy Tarreau | 418bfcc | 2012-03-09 13:56:20 +0100 | [diff] [blame] | 1937 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1938 | |
| 1939 | if (msg->sl.rq.v_l != 0) |
| 1940 | return 1; |
| 1941 | |
Apollon Oikonomopoulos | 25a1522 | 2014-04-06 02:46:00 +0300 | [diff] [blame] | 1942 | /* RFC 1945 allows only GET for HTTP/0.9 requests */ |
| 1943 | if (txn->meth != HTTP_METH_GET) |
| 1944 | return 0; |
| 1945 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1946 | cur_end = msg->chn->buf->p + msg->sl.rq.l; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1947 | delta = 0; |
| 1948 | |
| 1949 | if (msg->sl.rq.u_l == 0) { |
Apollon Oikonomopoulos | 25a1522 | 2014-04-06 02:46:00 +0300 | [diff] [blame] | 1950 | /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */ |
| 1951 | return 0; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1952 | } |
| 1953 | /* add HTTP version */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1954 | 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] | 1955 | http_msg_move_end(msg, delta); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1956 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1957 | cur_end = (char *)http_parse_reqline(msg, |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1958 | HTTP_MSG_RQMETH, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1959 | msg->chn->buf->p, cur_end + 1, |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1960 | NULL, NULL); |
| 1961 | if (unlikely(!cur_end)) |
| 1962 | return 0; |
| 1963 | |
| 1964 | /* we have a full HTTP/1.0 request now and we know that |
| 1965 | * we have either a CR or an LF at <ptr>. |
| 1966 | */ |
| 1967 | hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r'); |
| 1968 | return 1; |
| 1969 | } |
| 1970 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1971 | /* Parse the Connection: header of an HTTP request, looking for both "close" |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1972 | * and "keep-alive" values. If we already know that some headers may safely |
| 1973 | * 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] | 1974 | * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses) |
| 1975 | * - 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] | 1976 | * Presence of the "Upgrade" token is also checked and reported. |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1977 | * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was |
| 1978 | * found, and TX_CON_*_SET is adjusted depending on what is left so only |
| 1979 | * harmless combinations may be removed. Do not call that after changes have |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1980 | * been processed. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1981 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1982 | 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] | 1983 | { |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1984 | struct hdr_ctx ctx; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1985 | const char *hdr_val = "Connection"; |
| 1986 | int hdr_len = 10; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1987 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1988 | if (txn->flags & TX_HDR_CONN_PRS) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1989 | return; |
| 1990 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1991 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1992 | hdr_val = "Proxy-Connection"; |
| 1993 | hdr_len = 16; |
| 1994 | } |
| 1995 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1996 | ctx.idx = 0; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1997 | txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1998 | 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] | 1999 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 2000 | txn->flags |= TX_HDR_CONN_KAL; |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 2001 | if (to_del & 2) |
| 2002 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2003 | else |
| 2004 | txn->flags |= TX_CON_KAL_SET; |
| 2005 | } |
| 2006 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 2007 | txn->flags |= TX_HDR_CONN_CLO; |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 2008 | if (to_del & 1) |
| 2009 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2010 | else |
| 2011 | txn->flags |= TX_CON_CLO_SET; |
| 2012 | } |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 2013 | else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) { |
| 2014 | txn->flags |= TX_HDR_CONN_UPG; |
| 2015 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2016 | } |
| 2017 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2018 | txn->flags |= TX_HDR_CONN_PRS; |
| 2019 | return; |
| 2020 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2021 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2022 | /* Apply desired changes on the Connection: header. Values may be removed and/or |
| 2023 | * added depending on the <wanted> flags, which are exclusively composed of |
| 2024 | * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The |
| 2025 | * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left. |
| 2026 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 2027 | 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] | 2028 | { |
| 2029 | struct hdr_ctx ctx; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2030 | const char *hdr_val = "Connection"; |
| 2031 | int hdr_len = 10; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2032 | |
| 2033 | ctx.idx = 0; |
| 2034 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2035 | |
| 2036 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 2037 | hdr_val = "Proxy-Connection"; |
| 2038 | hdr_len = 16; |
| 2039 | } |
| 2040 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2041 | txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2042 | 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] | 2043 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 2044 | if (wanted & TX_CON_KAL_SET) |
| 2045 | txn->flags |= TX_CON_KAL_SET; |
| 2046 | else |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 2047 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2048 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2049 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 2050 | if (wanted & TX_CON_CLO_SET) |
| 2051 | txn->flags |= TX_CON_CLO_SET; |
| 2052 | else |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 2053 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 2054 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2055 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2056 | |
| 2057 | if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 2058 | return; |
| 2059 | |
| 2060 | if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) { |
| 2061 | txn->flags |= TX_CON_CLO_SET; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2062 | hdr_val = "Connection: close"; |
| 2063 | hdr_len = 17; |
| 2064 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 2065 | hdr_val = "Proxy-Connection: close"; |
| 2066 | hdr_len = 23; |
| 2067 | } |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 2068 | http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2069 | } |
| 2070 | |
| 2071 | if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) { |
| 2072 | txn->flags |= TX_CON_KAL_SET; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2073 | hdr_val = "Connection: keep-alive"; |
| 2074 | hdr_len = 22; |
| 2075 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 2076 | hdr_val = "Proxy-Connection: keep-alive"; |
| 2077 | hdr_len = 28; |
| 2078 | } |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 2079 | http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2080 | } |
| 2081 | return; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2082 | } |
| 2083 | |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 2084 | /* Parse the chunk size at msg->next. Once done, it adjusts ->next to point to |
| 2085 | * the first byte of data after the chunk size, so that we know we can forward |
| 2086 | * exactly msg->next bytes. msg->sol contains the exact number of bytes forming |
| 2087 | * the chunk size. That way it is always possible to differentiate between the |
| 2088 | * start of the body and the start of the data. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2089 | * Return >0 on success, 0 when some data is missing, <0 on error. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2090 | * Note: this function is designed to parse wrapped CRLF at the end of the buffer. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2091 | */ |
Willy Tarreau | 24e6d97 | 2012-10-26 00:49:52 +0200 | [diff] [blame] | 2092 | static inline int http_parse_chunk_size(struct http_msg *msg) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2093 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2094 | const struct buffer *buf = msg->chn->buf; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2095 | const char *ptr = b_ptr(buf, msg->next); |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 2096 | const char *ptr_old = ptr; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2097 | const char *end = buf->data + buf->size; |
| 2098 | const char *stop = bi_end(buf); |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2099 | unsigned int chunk = 0; |
| 2100 | |
| 2101 | /* The chunk size is in the following form, though we are only |
| 2102 | * interested in the size and CRLF : |
| 2103 | * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF |
| 2104 | */ |
| 2105 | while (1) { |
| 2106 | int c; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 2107 | if (ptr == stop) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2108 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2109 | c = hex2i(*ptr); |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2110 | if (c < 0) /* not a hex digit anymore */ |
| 2111 | break; |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 2112 | if (unlikely(++ptr >= end)) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2113 | ptr = buf->data; |
Willy Tarreau | 431946e | 2012-02-24 19:20:12 +0100 | [diff] [blame] | 2114 | if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */ |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2115 | goto error; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2116 | chunk = (chunk << 4) + c; |
| 2117 | } |
| 2118 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2119 | /* empty size not allowed */ |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 2120 | if (unlikely(ptr == ptr_old)) |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2121 | goto error; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2122 | |
| 2123 | while (http_is_spht[(unsigned char)*ptr]) { |
| 2124 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2125 | ptr = buf->data; |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 2126 | if (unlikely(ptr == stop)) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2127 | return 0; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2128 | } |
| 2129 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2130 | /* Up to there, we know that at least one byte is present at *ptr. Check |
| 2131 | * for the end of chunk size. |
| 2132 | */ |
| 2133 | while (1) { |
| 2134 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 2135 | /* we now have a CR or an LF at ptr */ |
| 2136 | if (likely(*ptr == '\r')) { |
| 2137 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2138 | ptr = buf->data; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 2139 | if (ptr == stop) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2140 | return 0; |
| 2141 | } |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2142 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2143 | if (*ptr != '\n') |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2144 | goto error; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2145 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2146 | ptr = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2147 | /* done */ |
| 2148 | break; |
| 2149 | } |
| 2150 | else if (*ptr == ';') { |
| 2151 | /* chunk extension, ends at next CRLF */ |
| 2152 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2153 | ptr = buf->data; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 2154 | if (ptr == stop) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2155 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2156 | |
| 2157 | while (!HTTP_IS_CRLF(*ptr)) { |
| 2158 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2159 | ptr = buf->data; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 2160 | if (ptr == stop) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2161 | return 0; |
| 2162 | } |
| 2163 | /* we have a CRLF now, loop above */ |
| 2164 | continue; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2165 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2166 | else |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2167 | goto error; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2168 | } |
| 2169 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2170 | /* OK we found our CRLF and now <ptr> points to the next byte, |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 2171 | * which may or may not be present. We save that into ->next, |
| 2172 | * and the number of bytes parsed into msg->sol. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2173 | */ |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 2174 | msg->sol = ptr - ptr_old; |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 2175 | if (unlikely(ptr < ptr_old)) |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 2176 | msg->sol += buf->size; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2177 | msg->next = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 2178 | msg->chunk_len = chunk; |
| 2179 | msg->body_len += chunk; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2180 | msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2181 | return 1; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2182 | error: |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2183 | msg->err_pos = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2184 | return -1; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2185 | } |
| 2186 | |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 2187 | /* This function skips trailers in the buffer associated with HTTP |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 2188 | * message <msg>. The first visited position is msg->next. If the end of |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2189 | * the trailers is found, it is automatically scheduled to be forwarded, |
| 2190 | * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0. |
| 2191 | * If not enough data are available, the function does not change anything |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 2192 | * except maybe msg->next if it could parse some lines, and returns zero. |
| 2193 | * If a parse error is encountered, the function returns < 0 and does not |
| 2194 | * change anything except maybe msg->next. Note that the message must |
| 2195 | * already be in HTTP_MSG_TRAILERS state before calling this function, |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2196 | * which implies that all non-trailers data have already been scheduled for |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 2197 | * forwarding, and that msg->next exactly matches the length of trailers |
| 2198 | * already parsed and not forwarded. It is also important to note that this |
| 2199 | * function is designed to be able to parse wrapped headers at end of buffer. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2200 | */ |
Willy Tarreau | 24e6d97 | 2012-10-26 00:49:52 +0200 | [diff] [blame] | 2201 | static int http_forward_trailers(struct http_msg *msg) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2202 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2203 | const struct buffer *buf = msg->chn->buf; |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 2204 | |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 2205 | /* we have msg->next which points to next line. Look for CRLF. */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2206 | while (1) { |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 2207 | const char *p1 = NULL, *p2 = NULL; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2208 | const char *ptr = b_ptr(buf, msg->next); |
| 2209 | const char *stop = bi_end(buf); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2210 | int bytes; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2211 | |
| 2212 | /* scan current line and stop at LF or CRLF */ |
| 2213 | while (1) { |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 2214 | if (ptr == stop) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2215 | return 0; |
| 2216 | |
| 2217 | if (*ptr == '\n') { |
| 2218 | if (!p1) |
| 2219 | p1 = ptr; |
| 2220 | p2 = ptr; |
| 2221 | break; |
| 2222 | } |
| 2223 | |
| 2224 | if (*ptr == '\r') { |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2225 | if (p1) { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2226 | msg->err_pos = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2227 | return -1; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2228 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2229 | p1 = ptr; |
| 2230 | } |
| 2231 | |
| 2232 | ptr++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2233 | if (ptr >= buf->data + buf->size) |
| 2234 | ptr = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2235 | } |
| 2236 | |
| 2237 | /* after LF; point to beginning of next line */ |
| 2238 | p2++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2239 | if (p2 >= buf->data + buf->size) |
| 2240 | p2 = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2241 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2242 | bytes = p2 - b_ptr(buf, msg->next); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2243 | if (bytes < 0) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2244 | bytes += buf->size; |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2245 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2246 | if (p1 == b_ptr(buf, msg->next)) { |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2247 | /* LF/CRLF at beginning of line => end of trailers at p2. |
| 2248 | * Everything was scheduled for forwarding, there's nothing |
| 2249 | * left from this message. |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 2250 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2251 | msg->next = buffer_count(buf, buf->p, p2); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2252 | msg->msg_state = HTTP_MSG_DONE; |
| 2253 | return 1; |
| 2254 | } |
| 2255 | /* OK, next line then */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2256 | msg->next = buffer_count(buf, buf->p, p2); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2257 | } |
| 2258 | } |
| 2259 | |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 2260 | /* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF |
| 2261 | * or a possible LF alone at the end of a chunk. It automatically adjusts |
| 2262 | * msg->next in order to include this part into the next forwarding phase. |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 2263 | * Note that the caller must ensure that ->p points to the first byte to parse. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2264 | * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If |
| 2265 | * not enough data are available, the function does not change anything and |
| 2266 | * returns zero. If a parse error is encountered, the function returns < 0 and |
| 2267 | * does not change anything. Note: this function is designed to parse wrapped |
| 2268 | * CRLF at the end of the buffer. |
| 2269 | */ |
Willy Tarreau | 24e6d97 | 2012-10-26 00:49:52 +0200 | [diff] [blame] | 2270 | static inline int http_skip_chunk_crlf(struct http_msg *msg) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2271 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2272 | const struct buffer *buf = msg->chn->buf; |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 2273 | const char *ptr; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2274 | int bytes; |
| 2275 | |
| 2276 | /* NB: we'll check data availabilty at the end. It's not a |
| 2277 | * problem because whatever we match first will be checked |
| 2278 | * against the correct length. |
| 2279 | */ |
| 2280 | bytes = 1; |
Willy Tarreau | 0669d7d | 2014-04-17 11:40:10 +0200 | [diff] [blame] | 2281 | ptr = b_ptr(buf, msg->next); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2282 | if (*ptr == '\r') { |
| 2283 | bytes++; |
| 2284 | ptr++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2285 | if (ptr >= buf->data + buf->size) |
| 2286 | ptr = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2287 | } |
| 2288 | |
Willy Tarreau | 0669d7d | 2014-04-17 11:40:10 +0200 | [diff] [blame] | 2289 | if (msg->next + bytes > buf->i) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2290 | return 0; |
| 2291 | |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2292 | if (*ptr != '\n') { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2293 | msg->err_pos = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2294 | return -1; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2295 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2296 | |
| 2297 | ptr++; |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 2298 | if (unlikely(ptr >= buf->data + buf->size)) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 2299 | ptr = buf->data; |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 2300 | /* Advance ->next to allow the CRLF to be forwarded */ |
Willy Tarreau | 0669d7d | 2014-04-17 11:40:10 +0200 | [diff] [blame] | 2301 | msg->next += bytes; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2302 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 2303 | return 1; |
| 2304 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2305 | |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2306 | /* Parses a qvalue and returns it multipled by 1000, from 0 to 1000. If the |
| 2307 | * value is larger than 1000, it is bound to 1000. The parser consumes up to |
| 2308 | * 1 digit, one dot and 3 digits and stops on the first invalid character. |
| 2309 | * Unparsable qvalues return 1000 as "q=1.000". |
| 2310 | */ |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 2311 | int parse_qvalue(const char *qvalue, const char **end) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2312 | { |
| 2313 | int q = 1000; |
| 2314 | |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 2315 | if (!isdigit((unsigned char)*qvalue)) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2316 | goto out; |
| 2317 | q = (*qvalue++ - '0') * 1000; |
| 2318 | |
| 2319 | if (*qvalue++ != '.') |
| 2320 | goto out; |
| 2321 | |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 2322 | if (!isdigit((unsigned char)*qvalue)) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2323 | goto out; |
| 2324 | q += (*qvalue++ - '0') * 100; |
| 2325 | |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 2326 | if (!isdigit((unsigned char)*qvalue)) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2327 | goto out; |
| 2328 | q += (*qvalue++ - '0') * 10; |
| 2329 | |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 2330 | if (!isdigit((unsigned char)*qvalue)) |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2331 | goto out; |
| 2332 | q += (*qvalue++ - '0') * 1; |
| 2333 | out: |
| 2334 | if (q > 1000) |
| 2335 | q = 1000; |
Willy Tarreau | 38b3aa5 | 2014-04-22 23:32:05 +0200 | [diff] [blame] | 2336 | if (end) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 2337 | *end = qvalue; |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2338 | return q; |
| 2339 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2340 | |
| 2341 | /* |
| 2342 | * Selects a compression algorithm depending on the client request. |
Willy Tarreau | 05d8460 | 2012-10-26 02:11:25 +0200 | [diff] [blame] | 2343 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2344 | int select_compression_request_header(struct stream *s, struct buffer *req) |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2345 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 2346 | struct http_txn *txn = s->txn; |
Willy Tarreau | 70737d1 | 2012-10-27 00:34:28 +0200 | [diff] [blame] | 2347 | struct http_msg *msg = &txn->req; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2348 | struct hdr_ctx ctx; |
| 2349 | struct comp_algo *comp_algo = NULL; |
Willy Tarreau | 3c7b97b | 2012-10-26 14:50:26 +0200 | [diff] [blame] | 2350 | struct comp_algo *comp_algo_back = NULL; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2351 | |
Finn Arne Gangstad | cbb9a4b | 2012-10-29 21:43:01 +0100 | [diff] [blame] | 2352 | /* Disable compression for older user agents announcing themselves as "Mozilla/4" |
| 2353 | * unless they are known good (MSIE 6 with XP SP2, or MSIE 7 and later). |
Willy Tarreau | 05d8460 | 2012-10-26 02:11:25 +0200 | [diff] [blame] | 2354 | * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details. |
| 2355 | */ |
| 2356 | ctx.idx = 0; |
| 2357 | if (http_find_header2("User-Agent", 10, req->p, &txn->hdr_idx, &ctx) && |
| 2358 | ctx.vlen >= 9 && |
Finn Arne Gangstad | cbb9a4b | 2012-10-29 21:43:01 +0100 | [diff] [blame] | 2359 | memcmp(ctx.line + ctx.val, "Mozilla/4", 9) == 0 && |
| 2360 | (ctx.vlen < 31 || |
| 2361 | memcmp(ctx.line + ctx.val + 25, "MSIE ", 5) != 0 || |
| 2362 | ctx.line[ctx.val + 30] < '6' || |
| 2363 | (ctx.line[ctx.val + 30] == '6' && |
| 2364 | (ctx.vlen < 54 || memcmp(ctx.line + 51, "SV1", 3) != 0)))) { |
| 2365 | s->comp_algo = NULL; |
| 2366 | return 0; |
Willy Tarreau | 05d8460 | 2012-10-26 02:11:25 +0200 | [diff] [blame] | 2367 | } |
| 2368 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2369 | /* search for the algo in the backend in priority or the frontend */ |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 2370 | if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (strm_fe(s)->comp && (comp_algo_back = strm_fe(s)->comp->algos))) { |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2371 | int best_q = 0; |
| 2372 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2373 | ctx.idx = 0; |
| 2374 | while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2375 | const char *qval; |
| 2376 | int q; |
| 2377 | int toklen; |
| 2378 | |
| 2379 | /* try to isolate the token from the optional q-value */ |
| 2380 | toklen = 0; |
| 2381 | while (toklen < ctx.vlen && http_is_token[(unsigned char)*(ctx.line + ctx.val + toklen)]) |
| 2382 | toklen++; |
| 2383 | |
| 2384 | qval = ctx.line + ctx.val + toklen; |
| 2385 | while (1) { |
| 2386 | while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval]) |
| 2387 | qval++; |
| 2388 | |
| 2389 | if (qval >= ctx.line + ctx.val + ctx.vlen || *qval != ';') { |
| 2390 | qval = NULL; |
| 2391 | break; |
| 2392 | } |
| 2393 | qval++; |
Willy Tarreau | 70737d1 | 2012-10-27 00:34:28 +0200 | [diff] [blame] | 2394 | |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2395 | while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval]) |
| 2396 | qval++; |
Willy Tarreau | 70737d1 | 2012-10-27 00:34:28 +0200 | [diff] [blame] | 2397 | |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2398 | if (qval >= ctx.line + ctx.val + ctx.vlen) { |
| 2399 | qval = NULL; |
| 2400 | break; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2401 | } |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2402 | if (strncmp(qval, "q=", MIN(ctx.line + ctx.val + ctx.vlen - qval, 2)) == 0) |
| 2403 | break; |
| 2404 | |
| 2405 | while (qval < ctx.line + ctx.val + ctx.vlen && *qval != ';') |
| 2406 | qval++; |
| 2407 | } |
| 2408 | |
| 2409 | /* here we have qval pointing to the first "q=" attribute or NULL if not found */ |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 2410 | q = qval ? parse_qvalue(qval + 2, NULL) : 1000; |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2411 | |
| 2412 | if (q <= best_q) |
| 2413 | continue; |
| 2414 | |
| 2415 | for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) { |
| 2416 | if (*(ctx.line + ctx.val) == '*' || |
Willy Tarreau | 615105e | 2015-03-28 16:40:46 +0100 | [diff] [blame] | 2417 | word_match(ctx.line + ctx.val, toklen, comp_algo->ua_name, comp_algo->ua_name_len)) { |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2418 | s->comp_algo = comp_algo; |
| 2419 | best_q = q; |
| 2420 | break; |
| 2421 | } |
| 2422 | } |
| 2423 | } |
| 2424 | } |
| 2425 | |
| 2426 | /* remove all occurrences of the header when "compression offload" is set */ |
| 2427 | if (s->comp_algo) { |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 2428 | if ((s->be->comp && s->be->comp->offload) || (strm_fe(s)->comp && strm_fe(s)->comp->offload)) { |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2429 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 2430 | ctx.idx = 0; |
| 2431 | while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) { |
| 2432 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2433 | } |
| 2434 | } |
Willy Tarreau | 0e9b1b4 | 2014-03-19 12:07:52 +0100 | [diff] [blame] | 2435 | return 1; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2436 | } |
| 2437 | |
| 2438 | /* identity is implicit does not require headers */ |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 2439 | if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (strm_fe(s)->comp && (comp_algo_back = strm_fe(s)->comp->algos))) { |
Willy Tarreau | 3c7b97b | 2012-10-26 14:50:26 +0200 | [diff] [blame] | 2440 | for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) { |
Willy Tarreau | 615105e | 2015-03-28 16:40:46 +0100 | [diff] [blame] | 2441 | if (comp_algo->cfg_name_len == 8 && memcmp(comp_algo->cfg_name, "identity", 8) == 0) { |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2442 | s->comp_algo = comp_algo; |
| 2443 | return 1; |
| 2444 | } |
| 2445 | } |
| 2446 | } |
| 2447 | |
| 2448 | s->comp_algo = NULL; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2449 | return 0; |
| 2450 | } |
| 2451 | |
| 2452 | /* |
| 2453 | * Selects a comression algorithm depending of the server response. |
| 2454 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2455 | int select_compression_response_header(struct stream *s, struct buffer *res) |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2456 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 2457 | struct http_txn *txn = s->txn; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2458 | struct http_msg *msg = &txn->rsp; |
| 2459 | struct hdr_ctx ctx; |
| 2460 | struct comp_type *comp_type; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2461 | |
| 2462 | /* no common compression algorithm was found in request header */ |
| 2463 | if (s->comp_algo == NULL) |
| 2464 | goto fail; |
| 2465 | |
| 2466 | /* HTTP < 1.1 should not be compressed */ |
Willy Tarreau | 7257550 | 2013-12-24 14:41:35 +0100 | [diff] [blame] | 2467 | if (!(msg->flags & HTTP_MSGF_VER_11) || !(txn->req.flags & HTTP_MSGF_VER_11)) |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2468 | goto fail; |
| 2469 | |
Jesse Hathaway | 2468d4e | 2015-03-06 20:16:15 +0000 | [diff] [blame] | 2470 | /* compress 200,201,202,203 responses only */ |
| 2471 | if ((txn->status != 200) && |
| 2472 | (txn->status != 201) && |
| 2473 | (txn->status != 202) && |
| 2474 | (txn->status != 203)) |
William Lallemand | d300261 | 2012-11-26 14:34:47 +0100 | [diff] [blame] | 2475 | goto fail; |
| 2476 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2477 | /* Content-Length is null */ |
| 2478 | if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->body_len == 0) |
| 2479 | goto fail; |
| 2480 | |
| 2481 | /* content is already compressed */ |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2482 | ctx.idx = 0; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2483 | if (http_find_header2("Content-Encoding", 16, res->p, &txn->hdr_idx, &ctx)) |
| 2484 | goto fail; |
| 2485 | |
Willy Tarreau | 56e9ffa | 2013-01-05 16:20:35 +0100 | [diff] [blame] | 2486 | /* no compression when Cache-Control: no-transform is present in the message */ |
| 2487 | ctx.idx = 0; |
| 2488 | while (http_find_header2("Cache-Control", 13, res->p, &txn->hdr_idx, &ctx)) { |
| 2489 | if (word_match(ctx.line + ctx.val, ctx.vlen, "no-transform", 12)) |
| 2490 | goto fail; |
| 2491 | } |
| 2492 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2493 | comp_type = NULL; |
| 2494 | |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2495 | /* we don't want to compress multipart content-types, nor content-types that are |
| 2496 | * not listed in the "compression type" directive if any. If no content-type was |
| 2497 | * found but configuration requires one, we don't compress either. Backend has |
| 2498 | * the priority. |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2499 | */ |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2500 | ctx.idx = 0; |
| 2501 | if (http_find_header2("Content-Type", 12, res->p, &txn->hdr_idx, &ctx)) { |
| 2502 | if (ctx.vlen >= 9 && strncasecmp("multipart", ctx.line+ctx.val, 9) == 0) |
| 2503 | goto fail; |
| 2504 | |
| 2505 | if ((s->be->comp && (comp_type = s->be->comp->types)) || |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 2506 | (strm_fe(s)->comp && (comp_type = strm_fe(s)->comp->types))) { |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2507 | for (; comp_type; comp_type = comp_type->next) { |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2508 | if (ctx.vlen >= comp_type->name_len && |
| 2509 | strncasecmp(ctx.line+ctx.val, comp_type->name, comp_type->name_len) == 0) |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2510 | /* this Content-Type should be compressed */ |
| 2511 | break; |
| 2512 | } |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2513 | /* this Content-Type should not be compressed */ |
| 2514 | if (comp_type == NULL) |
| 2515 | goto fail; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2516 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2517 | } |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2518 | else { /* no content-type header */ |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 2519 | if ((s->be->comp && s->be->comp->types) || (strm_fe(s)->comp && strm_fe(s)->comp->types)) |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2520 | goto fail; /* a content-type was required */ |
William Lallemand | d300261 | 2012-11-26 14:34:47 +0100 | [diff] [blame] | 2521 | } |
| 2522 | |
William Lallemand | d85f917 | 2012-11-09 17:05:39 +0100 | [diff] [blame] | 2523 | /* limit compression rate */ |
| 2524 | if (global.comp_rate_lim > 0) |
| 2525 | if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim) |
| 2526 | goto fail; |
| 2527 | |
William Lallemand | 072a2bf | 2012-11-20 17:01:01 +0100 | [diff] [blame] | 2528 | /* limit cpu usage */ |
| 2529 | if (idle_pct < compress_min_idle) |
| 2530 | goto fail; |
| 2531 | |
William Lallemand | 4c49fae | 2012-11-07 15:00:23 +0100 | [diff] [blame] | 2532 | /* initialize compression */ |
William Lallemand | f374783 | 2012-11-09 12:33:10 +0100 | [diff] [blame] | 2533 | if (s->comp_algo->init(&s->comp_ctx, global.tune.comp_maxlevel) < 0) |
William Lallemand | 4c49fae | 2012-11-07 15:00:23 +0100 | [diff] [blame] | 2534 | goto fail; |
| 2535 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2536 | s->flags |= SF_COMP_READY; |
William Lallemand | ec3e389 | 2012-11-12 17:02:18 +0100 | [diff] [blame] | 2537 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2538 | /* remove Content-Length header */ |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2539 | ctx.idx = 0; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2540 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && http_find_header2("Content-Length", 14, res->p, &txn->hdr_idx, &ctx)) |
| 2541 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 2542 | |
| 2543 | /* add Transfer-Encoding header */ |
| 2544 | if (!(msg->flags & HTTP_MSGF_TE_CHNK)) |
| 2545 | http_header_add_tail2(&txn->rsp, &txn->hdr_idx, "Transfer-Encoding: chunked", 26); |
| 2546 | |
| 2547 | /* |
| 2548 | * Add Content-Encoding header when it's not identity encoding. |
| 2549 | * RFC 2616 : Identity encoding: This content-coding is used only in the |
| 2550 | * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding |
| 2551 | * header. |
| 2552 | */ |
Willy Tarreau | 615105e | 2015-03-28 16:40:46 +0100 | [diff] [blame] | 2553 | if (s->comp_algo->cfg_name_len != 8 || memcmp(s->comp_algo->cfg_name, "identity", 8) != 0) { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 2554 | trash.len = 18; |
| 2555 | memcpy(trash.str, "Content-Encoding: ", trash.len); |
Willy Tarreau | 615105e | 2015-03-28 16:40:46 +0100 | [diff] [blame] | 2556 | memcpy(trash.str + trash.len, s->comp_algo->ua_name, s->comp_algo->ua_name_len); |
| 2557 | trash.len += s->comp_algo->ua_name_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 2558 | trash.str[trash.len] = '\0'; |
| 2559 | http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len); |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2560 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2561 | return 1; |
| 2562 | |
| 2563 | fail: |
Willy Tarreau | b97b619 | 2012-11-19 14:55:02 +0100 | [diff] [blame] | 2564 | s->comp_algo = NULL; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2565 | return 0; |
| 2566 | } |
| 2567 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2568 | 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] | 2569 | { |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 2570 | struct proxy *fe = strm_fe(s); |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 2571 | int tmp = TX_CON_WANT_KAL; |
| 2572 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2573 | if (!((fe->options2|s->be->options2) & PR_O2_FAKE_KA)) { |
| 2574 | if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN || |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 2575 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN) |
| 2576 | tmp = TX_CON_WANT_TUN; |
| 2577 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2578 | if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 2579 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL) |
| 2580 | tmp = TX_CON_WANT_TUN; |
| 2581 | } |
| 2582 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2583 | if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL || |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 2584 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) { |
| 2585 | /* option httpclose + server_close => forceclose */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2586 | if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 2587 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL) |
| 2588 | tmp = TX_CON_WANT_CLO; |
| 2589 | else |
| 2590 | tmp = TX_CON_WANT_SCL; |
| 2591 | } |
| 2592 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2593 | if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL || |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 2594 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL) |
| 2595 | tmp = TX_CON_WANT_CLO; |
| 2596 | |
| 2597 | if ((txn->flags & TX_CON_WANT_MSK) < tmp) |
| 2598 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp; |
| 2599 | |
| 2600 | if (!(txn->flags & TX_HDR_CONN_PRS) && |
| 2601 | (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) { |
| 2602 | /* parse the Connection header and possibly clean it */ |
| 2603 | int to_del = 0; |
| 2604 | if ((msg->flags & HTTP_MSGF_VER_11) || |
| 2605 | ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2606 | !((fe->options2|s->be->options2) & PR_O2_FAKE_KA))) |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 2607 | to_del |= 2; /* remove "keep-alive" */ |
| 2608 | if (!(msg->flags & HTTP_MSGF_VER_11)) |
| 2609 | to_del |= 1; /* remove "close" */ |
| 2610 | http_parse_connection_header(txn, msg, to_del); |
| 2611 | } |
| 2612 | |
| 2613 | /* check if client or config asks for explicit close in KAL/SCL */ |
| 2614 | if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 2615 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) && |
| 2616 | ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */ |
| 2617 | (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */ |
| 2618 | !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2619 | fe->state == PR_STSTOPPED)) /* frontend is stopping */ |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 2620 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 2621 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2622 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2623 | /* This stream analyser waits for a complete HTTP request. It returns 1 if the |
| 2624 | * processing can continue on next analysers, or zero if it either needs more |
| 2625 | * data or wants to immediately abort the request (eg: timeout, error, ...). It |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 2626 | * 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] | 2627 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 2628 | * abort. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2629 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2630 | 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] | 2631 | { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2632 | /* |
| 2633 | * We will parse the partial (or complete) lines. |
| 2634 | * We will check the request syntax, and also join multi-line |
| 2635 | * headers. An index of all the lines will be elaborated while |
| 2636 | * parsing. |
| 2637 | * |
| 2638 | * For the parsing, we use a 28 states FSM. |
| 2639 | * |
| 2640 | * Here is the information we currently have : |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2641 | * req->buf->p = beginning of request |
| 2642 | * req->buf->p + msg->eoh = end of processed headers / start of current one |
| 2643 | * req->buf->p + req->buf->i = end of input data |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 2644 | * msg->eol = end of current header or line (LF or CRLF) |
| 2645 | * msg->next = first non-visited byte |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2646 | * |
| 2647 | * 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] | 2648 | * we will set a few fields (txn->meth, sn->flags/SF_REDIRECTABLE). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2649 | * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and |
| 2650 | * finally headers capture. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2651 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2652 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2653 | int cur_idx; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 2654 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 2655 | struct http_txn *txn = s->txn; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2656 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2657 | struct hdr_ctx ctx; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2658 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2659 | 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] | 2660 | now_ms, __FUNCTION__, |
| 2661 | s, |
| 2662 | req, |
| 2663 | req->rex, req->wex, |
| 2664 | req->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2665 | req->buf->i, |
Willy Tarreau | 6bf1736 | 2009-02-24 10:48:35 +0100 | [diff] [blame] | 2666 | req->analysers); |
| 2667 | |
Willy Tarreau | 52a0c60 | 2009-08-16 22:45:38 +0200 | [diff] [blame] | 2668 | /* we're speaking HTTP here, so let's speak HTTP to the client */ |
| 2669 | s->srv_error = http_return_srv_error; |
| 2670 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2671 | /* There's a protected area at the end of the buffer for rewriting |
| 2672 | * purposes. We don't want to start to parse the request if the |
| 2673 | * protected area is affected, because we may have to move processed |
| 2674 | * data later, which is much more complicated. |
| 2675 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2676 | if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) { |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 2677 | if (txn->flags & TX_NOT_FIRST) { |
Willy Tarreau | ba0902e | 2015-01-13 14:39:16 +0100 | [diff] [blame] | 2678 | if (unlikely(!channel_is_rewritable(req))) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2679 | 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] | 2680 | goto failed_keep_alive; |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2681 | /* 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] | 2682 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2683 | req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | d7ad9f5 | 2013-12-31 17:26:25 +0100 | [diff] [blame] | 2684 | req->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2685 | return 0; |
| 2686 | } |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 2687 | if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) || |
| 2688 | bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite)) |
| 2689 | buffer_slow_realign(req->buf); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2690 | } |
| 2691 | |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2692 | /* Note that we have the same problem with the response ; we |
| 2693 | * may want to send a redirect, error or anything which requires |
| 2694 | * some spare space. So we'll ensure that we have at least |
| 2695 | * maxrewrite bytes available in the response buffer before |
| 2696 | * processing that one. This will only affect pipelined |
| 2697 | * keep-alive requests. |
| 2698 | */ |
| 2699 | if ((txn->flags & TX_NOT_FIRST) && |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 2700 | unlikely(!channel_is_rewritable(&s->res) || |
| 2701 | bi_end(s->res.buf) < b_ptr(s->res.buf, txn->rsp.next) || |
| 2702 | bi_end(s->res.buf) > s->res.buf->data + s->res.buf->size - global.tune.maxrewrite)) { |
| 2703 | if (s->res.buf->o) { |
| 2704 | if (s->res.flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) |
Willy Tarreau | 6464841 | 2010-03-05 10:41:54 +0100 | [diff] [blame] | 2705 | goto failed_keep_alive; |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2706 | /* don't let a connection request be initiated */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 2707 | channel_dont_connect(req); |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 2708 | s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
| 2709 | s->res.flags |= CF_WAKE_WRITE; |
| 2710 | s->res.analysers |= an_bit; /* wake us up once it changes */ |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2711 | return 0; |
| 2712 | } |
| 2713 | } |
| 2714 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2715 | if (likely(msg->next < req->buf->i)) /* some unparsed data are available */ |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 2716 | http_msg_analyzer(msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2717 | } |
| 2718 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2719 | /* 1: we might have to print this header in debug mode */ |
| 2720 | if (unlikely((global.mode & MODE_DEBUG) && |
| 2721 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | 7d59e90 | 2014-10-21 19:36:09 +0200 | [diff] [blame] | 2722 | msg->msg_state >= HTTP_MSG_BODY)) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2723 | char *eol, *sol; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2724 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2725 | sol = req->buf->p; |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 2726 | /* this is a bit complex : in case of error on the request line, |
| 2727 | * we know that rq.l is still zero, so we display only the part |
| 2728 | * up to the end of the line (truncated by debug_hdr). |
| 2729 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2730 | 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] | 2731 | debug_hdr("clireq", s, sol, eol); |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 2732 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2733 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 2734 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2735 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2736 | while (cur_idx) { |
| 2737 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 2738 | debug_hdr("clihdr", s, sol, eol); |
| 2739 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 2740 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2741 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2742 | } |
| 2743 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2744 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2745 | /* |
| 2746 | * Now we quickly check if we have found a full valid request. |
| 2747 | * If not so, we check the FD and buffer states before leaving. |
| 2748 | * A full request is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2749 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2750 | * requests are checked first. When waiting for a second request |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2751 | * on a keep-alive stream, if we encounter and error, close, t/o, |
| 2752 | * 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] | 2753 | * 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] | 2754 | * process_stream() as a frontend error. |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2755 | * Last, we may increase some tracked counters' http request errors on |
| 2756 | * the cases that are deliberately the client's fault. For instance, |
| 2757 | * a timeout or connection reset is not counted as an error. However |
| 2758 | * a bad request is. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2759 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2760 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2761 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2762 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2763 | * First, let's catch bad requests. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2764 | */ |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2765 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2766 | stream_inc_http_req_ctr(s); |
| 2767 | stream_inc_http_err_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2768 | proxy_inc_fe_req_ctr(sess->fe); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2769 | goto return_bad_req; |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2770 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2771 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2772 | /* 1: Since we are in header mode, if there's no space |
| 2773 | * left for headers, we won't be able to free more |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2774 | * later, so the stream will never terminate. We |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2775 | * must terminate it now. |
| 2776 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2777 | if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2778 | /* FIXME: check if URI is set and return Status |
| 2779 | * 414 Request URI too long instead. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2780 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2781 | stream_inc_http_req_ctr(s); |
| 2782 | stream_inc_http_err_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2783 | proxy_inc_fe_req_ctr(sess->fe); |
Willy Tarreau | fec4d89 | 2011-09-02 20:04:57 +0200 | [diff] [blame] | 2784 | if (msg->err_pos < 0) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2785 | msg->err_pos = req->buf->i; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2786 | goto return_bad_req; |
| 2787 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2788 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2789 | /* 2: have we encountered a read error ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2790 | else if (req->flags & CF_READ_ERROR) { |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2791 | if (!(s->flags & SF_ERR_MASK)) |
| 2792 | s->flags |= SF_ERR_CLICL; |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2793 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2794 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2795 | goto failed_keep_alive; |
| 2796 | |
Willy Tarreau | 0f228a0 | 2015-05-01 15:37:53 +0200 | [diff] [blame] | 2797 | if (sess->fe->options & PR_O_IGNORE_PRB) |
| 2798 | goto failed_keep_alive; |
| 2799 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2800 | /* we cannot return any message on error */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2801 | if (msg->err_pos >= 0) { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2802 | http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2803 | stream_inc_http_err_ctr(s); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2804 | } |
| 2805 | |
Willy Tarreau | dc979f2 | 2012-12-04 10:39:01 +0100 | [diff] [blame] | 2806 | txn->status = 400; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 2807 | stream_int_retnclose(&s->si[0], NULL); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2808 | msg->msg_state = HTTP_MSG_ERROR; |
| 2809 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2810 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2811 | stream_inc_http_req_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2812 | proxy_inc_fe_req_ctr(sess->fe); |
| 2813 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 2814 | if (sess->listener->counters) |
| 2815 | sess->listener->counters->failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2816 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2817 | if (!(s->flags & SF_FINST_MASK)) |
| 2818 | s->flags |= SF_FINST_R; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2819 | return 0; |
| 2820 | } |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2821 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2822 | /* 3: has the read timeout expired ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2823 | 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] | 2824 | if (!(s->flags & SF_ERR_MASK)) |
| 2825 | s->flags |= SF_ERR_CLITO; |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2826 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2827 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2828 | goto failed_keep_alive; |
| 2829 | |
Willy Tarreau | 0f228a0 | 2015-05-01 15:37:53 +0200 | [diff] [blame] | 2830 | if (sess->fe->options & PR_O_IGNORE_PRB) |
| 2831 | goto failed_keep_alive; |
| 2832 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2833 | /* read timeout : give up with an error message. */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2834 | if (msg->err_pos >= 0) { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2835 | http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2836 | stream_inc_http_err_ctr(s); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2837 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2838 | txn->status = 408; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 2839 | stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_408)); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2840 | msg->msg_state = HTTP_MSG_ERROR; |
| 2841 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2842 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2843 | stream_inc_http_req_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2844 | proxy_inc_fe_req_ctr(sess->fe); |
| 2845 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 2846 | if (sess->listener->counters) |
| 2847 | sess->listener->counters->failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2848 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2849 | if (!(s->flags & SF_FINST_MASK)) |
| 2850 | s->flags |= SF_FINST_R; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2851 | return 0; |
| 2852 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2853 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2854 | /* 4: have we encountered a close ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2855 | else if (req->flags & CF_SHUTR) { |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2856 | if (!(s->flags & SF_ERR_MASK)) |
| 2857 | s->flags |= SF_ERR_CLICL; |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2858 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2859 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2860 | goto failed_keep_alive; |
| 2861 | |
Willy Tarreau | 0f228a0 | 2015-05-01 15:37:53 +0200 | [diff] [blame] | 2862 | if (sess->fe->options & PR_O_IGNORE_PRB) |
| 2863 | goto failed_keep_alive; |
| 2864 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 2865 | if (msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2866 | http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2867 | txn->status = 400; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 2868 | stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400)); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2869 | msg->msg_state = HTTP_MSG_ERROR; |
| 2870 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2871 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2872 | stream_inc_http_err_ctr(s); |
| 2873 | stream_inc_http_req_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2874 | proxy_inc_fe_req_ctr(sess->fe); |
| 2875 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 2876 | if (sess->listener->counters) |
| 2877 | sess->listener->counters->failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2878 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2879 | if (!(s->flags & SF_FINST_MASK)) |
| 2880 | s->flags |= SF_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2881 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2882 | } |
| 2883 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 2884 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2885 | req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 2886 | s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 2887 | #ifdef TCP_QUICKACK |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 2888 | if (sess->listener->options & LI_O_NOQUICKACK && req->buf->i && |
| 2889 | objt_conn(sess->origin) && conn_ctrl_ready(__objt_conn(sess->origin))) { |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 2890 | /* We need more data, we have to re-enable quick-ack in case we |
| 2891 | * previously disabled it, otherwise we might cause the client |
| 2892 | * to delay next data. |
| 2893 | */ |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 2894 | setsockopt(__objt_conn(sess->origin)->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one)); |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 2895 | } |
| 2896 | #endif |
Willy Tarreau | 1b194fe | 2009-03-21 21:10:04 +0100 | [diff] [blame] | 2897 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2898 | if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) { |
| 2899 | /* If the client starts to talk, let's fall back to |
| 2900 | * request timeout processing. |
| 2901 | */ |
| 2902 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2903 | req->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2904 | } |
| 2905 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2906 | /* just set the request timeout once at the beginning of the request */ |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2907 | if (!tick_isset(req->analyse_exp)) { |
| 2908 | if ((msg->msg_state == HTTP_MSG_RQBEFORE) && |
| 2909 | (txn->flags & TX_WAIT_NEXT_RQ) && |
| 2910 | tick_isset(s->be->timeout.httpka)) |
| 2911 | req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka); |
| 2912 | else |
| 2913 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
| 2914 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2915 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2916 | /* we're not ready yet */ |
| 2917 | return 0; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2918 | |
| 2919 | failed_keep_alive: |
| 2920 | /* Here we process low-level errors for keep-alive requests. In |
| 2921 | * short, if the request is not the first one and it experiences |
| 2922 | * a timeout, read error or shutdown, we just silently close so |
| 2923 | * that the client can try again. |
| 2924 | */ |
| 2925 | txn->status = 0; |
| 2926 | msg->msg_state = HTTP_MSG_RQBEFORE; |
| 2927 | req->analysers = 0; |
| 2928 | s->logs.logwait = 0; |
Willy Tarreau | abcd514 | 2013-06-11 17:18:02 +0200 | [diff] [blame] | 2929 | s->logs.level = 0; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 2930 | s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 2931 | stream_int_retnclose(&s->si[0], NULL); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2932 | return 0; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2933 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2934 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2935 | /* OK now we have a complete HTTP request with indexed headers. Let's |
| 2936 | * complete the request parsing by setting a few fields we will need |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2937 | * 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] | 2938 | * 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] | 2939 | * 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] | 2940 | * byte after the last LF. msg->sov points to the first byte of data. |
| 2941 | * msg->eol cannot be trusted because it may have been left uninitialized |
| 2942 | * (for instance in the absence of headers). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2943 | */ |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 2944 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2945 | stream_inc_http_req_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2946 | 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] | 2947 | |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2948 | if (txn->flags & TX_WAIT_NEXT_RQ) { |
| 2949 | /* kill the pending keep-alive timeout */ |
| 2950 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
| 2951 | req->analyse_exp = TICK_ETERNITY; |
| 2952 | } |
| 2953 | |
| 2954 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2955 | /* Maybe we found in invalid header name while we were configured not |
| 2956 | * to block on that, so we have to capture it now. |
| 2957 | */ |
| 2958 | if (unlikely(msg->err_pos >= 0)) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2959 | http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 2960 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2961 | /* |
| 2962 | * 1: identify the method |
| 2963 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2964 | 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] | 2965 | |
| 2966 | /* we can make use of server redirect on GET and HEAD */ |
| 2967 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2968 | s->flags |= SF_REDIRECTABLE; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2969 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2970 | /* |
| 2971 | * 2: check if the URI matches the monitor_uri. |
| 2972 | * We have to do this for every request which gets in, because |
| 2973 | * the monitor-uri is defined by the frontend. |
| 2974 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2975 | if (unlikely((sess->fe->monitor_uri_len != 0) && |
| 2976 | (sess->fe->monitor_uri_len == msg->sl.rq.u_l) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2977 | !memcmp(req->buf->p + msg->sl.rq.u, |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2978 | sess->fe->monitor_uri, |
| 2979 | sess->fe->monitor_uri_len))) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2980 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2981 | * We have found the monitor URI |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2982 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2983 | struct acl_cond *cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2984 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2985 | s->flags |= SF_MONITOR; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2986 | sess->fe->fe_counters.intercepted_req++; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2987 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2988 | /* Check if we want to fail this monitor request or not */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2989 | list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 2990 | 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] | 2991 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2992 | ret = acl_pass(ret); |
| 2993 | if (cond->pol == ACL_COND_UNLESS) |
| 2994 | ret = !ret; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2995 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2996 | if (ret) { |
| 2997 | /* we fail this request, let's return 503 service unavail */ |
| 2998 | txn->status = 503; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 2999 | stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3000 | if (!(s->flags & SF_ERR_MASK)) |
| 3001 | 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] | 3002 | goto return_prx_cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 3003 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3004 | } |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 3005 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3006 | /* nothing to fail, let's reply normaly */ |
| 3007 | txn->status = 200; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 3008 | stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_200)); |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3009 | if (!(s->flags & SF_ERR_MASK)) |
| 3010 | 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] | 3011 | goto return_prx_cond; |
| 3012 | } |
| 3013 | |
| 3014 | /* |
| 3015 | * 3: Maybe we have to copy the original REQURI for the logs ? |
| 3016 | * Note: we cannot log anymore if the request has been |
| 3017 | * classified as invalid. |
| 3018 | */ |
| 3019 | if (unlikely(s->logs.logwait & LW_REQ)) { |
| 3020 | /* we have a complete HTTP request that we must log */ |
| 3021 | if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) { |
| 3022 | int urilen = msg->sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3023 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3024 | if (urilen >= REQURI_LEN) |
| 3025 | urilen = REQURI_LEN - 1; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3026 | memcpy(txn->uri, req->buf->p, urilen); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3027 | txn->uri[urilen] = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3028 | |
Willy Tarreau | d79a3b2 | 2012-12-28 09:40:16 +0100 | [diff] [blame] | 3029 | if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT))) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3030 | s->do_log(s); |
| 3031 | } else { |
| 3032 | Alert("HTTP logging : out of memory.\n"); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3033 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3034 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3035 | |
Willy Tarreau | 91852eb | 2015-05-01 13:26:00 +0200 | [diff] [blame] | 3036 | /* RFC7230#2.6 has enforced the format of the HTTP version string to be |
| 3037 | * exactly one digit "." one digit. This check may be disabled using |
| 3038 | * option accept-invalid-http-request. |
| 3039 | */ |
| 3040 | if (!(sess->fe->options2 & PR_O2_REQBUG_OK)) { |
| 3041 | if (msg->sl.rq.v_l != 8) { |
| 3042 | msg->err_pos = msg->sl.rq.v; |
| 3043 | goto return_bad_req; |
| 3044 | } |
| 3045 | |
| 3046 | if (req->buf->p[msg->sl.rq.v + 4] != '/' || |
| 3047 | !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 5]) || |
| 3048 | req->buf->p[msg->sl.rq.v + 6] != '.' || |
| 3049 | !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 7])) { |
| 3050 | msg->err_pos = msg->sl.rq.v + 4; |
| 3051 | goto return_bad_req; |
| 3052 | } |
| 3053 | } |
Willy Tarreau | 1331766 | 2015-05-01 13:47:08 +0200 | [diff] [blame] | 3054 | else { |
| 3055 | /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */ |
| 3056 | if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn)) |
| 3057 | goto return_bad_req; |
| 3058 | } |
Willy Tarreau | 91852eb | 2015-05-01 13:26:00 +0200 | [diff] [blame] | 3059 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 3060 | /* ... and check if the request is HTTP/1.1 or above */ |
| 3061 | if ((msg->sl.rq.v_l == 8) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3062 | ((req->buf->p[msg->sl.rq.v + 5] > '1') || |
| 3063 | ((req->buf->p[msg->sl.rq.v + 5] == '1') && |
| 3064 | (req->buf->p[msg->sl.rq.v + 7] >= '1')))) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3065 | msg->flags |= HTTP_MSGF_VER_11; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 3066 | |
| 3067 | /* "connection" has not been parsed yet */ |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 3068 | 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] | 3069 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 3070 | /* if the frontend has "option http-use-proxy-header", we'll check if |
| 3071 | * we have what looks like a proxied connection instead of a connection, |
| 3072 | * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection. |
| 3073 | * Note that this is *not* RFC-compliant, however browsers and proxies |
| 3074 | * happen to do that despite being non-standard :-( |
| 3075 | * We consider that a request not beginning with either '/' or '*' is |
| 3076 | * a proxied connection, which covers both "scheme://location" and |
| 3077 | * CONNECT ip:port. |
| 3078 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3079 | if ((sess->fe->options2 & PR_O2_USE_PXHDR) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3080 | 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] | 3081 | txn->flags |= TX_USE_PX_CONN; |
| 3082 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 3083 | /* transfer length unknown*/ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3084 | msg->flags &= ~HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 3085 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3086 | /* 5: we may need to capture headers */ |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 3087 | if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap)) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3088 | capture_headers(req->buf->p, &txn->hdr_idx, |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 3089 | s->req_cap, sess->fe->req_cap); |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 3090 | |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 3091 | /* 6: determine the transfer-length according to RFC2616 #4.4, updated |
| 3092 | * by RFC7230#3.3.3 : |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 3093 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 3094 | * The length of a message body is determined by one of the following |
| 3095 | * (in order of precedence): |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3096 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 3097 | * 1. Any response to a HEAD request and any response with a 1xx |
| 3098 | * (Informational), 204 (No Content), or 304 (Not Modified) status |
| 3099 | * code is always terminated by the first empty line after the |
| 3100 | * header fields, regardless of the header fields present in the |
| 3101 | * message, and thus cannot contain a message body. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3102 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 3103 | * 2. Any 2xx (Successful) response to a CONNECT request implies that |
| 3104 | * the connection will become a tunnel immediately after the empty |
| 3105 | * line that concludes the header fields. A client MUST ignore any |
| 3106 | * Content-Length or Transfer-Encoding header fields received in |
| 3107 | * such a message. |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 3108 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 3109 | * 3. If a Transfer-Encoding header field is present and the chunked |
| 3110 | * transfer coding (Section 4.1) is the final encoding, the message |
| 3111 | * body length is determined by reading and decoding the chunked |
| 3112 | * data until the transfer coding indicates the data is complete. |
| 3113 | * |
| 3114 | * If a Transfer-Encoding header field is present in a response and |
| 3115 | * the chunked transfer coding is not the final encoding, the |
| 3116 | * message body length is determined by reading the connection until |
| 3117 | * it is closed by the server. If a Transfer-Encoding header field |
| 3118 | * is present in a request and the chunked transfer coding is not |
| 3119 | * the final encoding, the message body length cannot be determined |
| 3120 | * reliably; the server MUST respond with the 400 (Bad Request) |
| 3121 | * status code and then close the connection. |
| 3122 | * |
| 3123 | * If a message is received with both a Transfer-Encoding and a |
| 3124 | * Content-Length header field, the Transfer-Encoding overrides the |
| 3125 | * Content-Length. Such a message might indicate an attempt to |
| 3126 | * perform request smuggling (Section 9.5) or response splitting |
| 3127 | * (Section 9.4) and ought to be handled as an error. A sender MUST |
| 3128 | * remove the received Content-Length field prior to forwarding such |
| 3129 | * a message downstream. |
| 3130 | * |
| 3131 | * 4. If a message is received without Transfer-Encoding and with |
| 3132 | * either multiple Content-Length header fields having differing |
| 3133 | * field-values or a single Content-Length header field having an |
| 3134 | * invalid value, then the message framing is invalid and the |
| 3135 | * recipient MUST treat it as an unrecoverable error. If this is a |
| 3136 | * request message, the server MUST respond with a 400 (Bad Request) |
| 3137 | * status code and then close the connection. If this is a response |
| 3138 | * message received by a proxy, the proxy MUST close the connection |
| 3139 | * to the server, discard the received response, and send a 502 (Bad |
| 3140 | * Gateway) response to the client. If this is a response message |
| 3141 | * received by a user agent, the user agent MUST close the |
| 3142 | * connection to the server and discard the received response. |
| 3143 | * |
| 3144 | * 5. If a valid Content-Length header field is present without |
| 3145 | * Transfer-Encoding, its decimal value defines the expected message |
| 3146 | * body length in octets. If the sender closes the connection or |
| 3147 | * the recipient times out before the indicated number of octets are |
| 3148 | * received, the recipient MUST consider the message to be |
| 3149 | * incomplete and close the connection. |
| 3150 | * |
| 3151 | * 6. If this is a request message and none of the above are true, then |
| 3152 | * the message body length is zero (no message body is present). |
| 3153 | * |
| 3154 | * 7. Otherwise, this is a response message without a declared message |
| 3155 | * body length, so the message body length is determined by the |
| 3156 | * number of octets received prior to the server closing the |
| 3157 | * connection. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3158 | */ |
| 3159 | |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3160 | ctx.idx = 0; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 3161 | /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */ |
Willy Tarreau | 4979d5c | 2015-05-01 10:06:30 +0200 | [diff] [blame] | 3162 | 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] | 3163 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3164 | msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
| 3165 | else if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | 34dfc60 | 2015-05-01 10:09:49 +0200 | [diff] [blame] | 3166 | /* chunked not last, return badreq */ |
| 3167 | goto return_bad_req; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 3168 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3169 | } |
| 3170 | |
Willy Tarreau | 1c91391 | 2015-04-30 10:57:51 +0200 | [diff] [blame] | 3171 | /* Chunked requests must have their content-length removed */ |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3172 | ctx.idx = 0; |
Willy Tarreau | 1c91391 | 2015-04-30 10:57:51 +0200 | [diff] [blame] | 3173 | if (msg->flags & HTTP_MSGF_TE_CHNK) { |
| 3174 | while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) |
| 3175 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 3176 | } |
Willy Tarreau | 34dfc60 | 2015-05-01 10:09:49 +0200 | [diff] [blame] | 3177 | 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] | 3178 | signed long long cl; |
| 3179 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3180 | if (!ctx.vlen) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3181 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3182 | goto return_bad_req; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3183 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3184 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3185 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3186 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3187 | goto return_bad_req; /* parse failure */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3188 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3189 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3190 | if (cl < 0) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3191 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3192 | goto return_bad_req; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3193 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3194 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3195 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3196 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3197 | goto return_bad_req; /* already specified, was different */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 3198 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3199 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3200 | msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 3201 | msg->body_len = msg->chunk_len = cl; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 3202 | } |
| 3203 | |
Willy Tarreau | 34dfc60 | 2015-05-01 10:09:49 +0200 | [diff] [blame] | 3204 | /* even bodyless requests have a known length */ |
| 3205 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 3206 | |
Willy Tarreau | 179085c | 2014-04-28 16:48:56 +0200 | [diff] [blame] | 3207 | /* Until set to anything else, the connection mode is set as Keep-Alive. It will |
| 3208 | * only change if both the request and the config reference something else. |
| 3209 | * Option httpclose by itself sets tunnel mode where headers are mangled. |
| 3210 | * However, if another mode is set, it will affect it (eg: server-close/ |
| 3211 | * keep-alive + httpclose = close). Note that we avoid to redo the same work |
| 3212 | * if FE and BE have the same settings (common). The method consists in |
| 3213 | * checking if options changed between the two calls (implying that either |
| 3214 | * one is non-null, or one of them is non-null and we are there for the first |
| 3215 | * time. |
| 3216 | */ |
| 3217 | if (!(txn->flags & TX_HDR_CONN_PRS) || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3218 | ((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] | 3219 | http_adjust_conn_mode(s, txn, msg); |
Willy Tarreau | 179085c | 2014-04-28 16:48:56 +0200 | [diff] [blame] | 3220 | |
Willy Tarreau | 9fbe18e | 2015-05-01 22:42:08 +0200 | [diff] [blame] | 3221 | /* we may have to wait for the request's body */ |
| 3222 | if ((s->be->options & PR_O_WREQ_BODY) && |
| 3223 | (msg->body_len || (msg->flags & HTTP_MSGF_TE_CHNK))) |
| 3224 | req->analysers |= AN_REQ_HTTP_BODY; |
| 3225 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3226 | /* end of job, return OK */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 3227 | req->analysers &= ~an_bit; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3228 | req->analyse_exp = TICK_ETERNITY; |
| 3229 | return 1; |
| 3230 | |
| 3231 | return_bad_req: |
| 3232 | /* We centralize bad requests processing here */ |
| 3233 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 3234 | /* we detected a parsing error. We want to archive this request |
| 3235 | * in the dedicated proxy area for later troubleshooting. |
| 3236 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3237 | http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe); |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3238 | } |
| 3239 | |
| 3240 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3241 | txn->status = 400; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 3242 | stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3243 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3244 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 3245 | if (sess->listener->counters) |
| 3246 | sess->listener->counters->failed_req++; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3247 | |
| 3248 | return_prx_cond: |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3249 | if (!(s->flags & SF_ERR_MASK)) |
| 3250 | s->flags |= SF_ERR_PRXCOND; |
| 3251 | if (!(s->flags & SF_FINST_MASK)) |
| 3252 | s->flags |= SF_FINST_R; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3253 | |
| 3254 | req->analysers = 0; |
| 3255 | req->analyse_exp = TICK_ETERNITY; |
| 3256 | return 0; |
| 3257 | } |
| 3258 | |
Willy Tarreau | 4f8a83c | 2012-06-04 00:26:23 +0200 | [diff] [blame] | 3259 | |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 3260 | /* This function prepares an applet to handle the stats. It can deal with the |
| 3261 | * "100-continue" expectation, check that admin rules are met for POST requests, |
| 3262 | * and program a response message if something was unexpected. It cannot fail |
| 3263 | * 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] | 3264 | * 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] | 3265 | * 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] | 3266 | * is expected to have already assigned an appctx to the stream. |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3267 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3268 | int http_handle_stats(struct stream *s, struct channel *req) |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3269 | { |
| 3270 | struct stats_admin_rule *stats_admin_rule; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 3271 | struct stream_interface *si = &s->si[1]; |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 3272 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3273 | struct http_txn *txn = s->txn; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3274 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3275 | struct uri_auth *uri_auth = s->be->uri_auth; |
| 3276 | const char *uri, *h, *lookup; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3277 | struct appctx *appctx; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3278 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3279 | appctx = si_appctx(si); |
| 3280 | memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats)); |
| 3281 | appctx->st1 = appctx->st2 = 0; |
| 3282 | appctx->ctx.stats.st_code = STAT_STATUS_INIT; |
| 3283 | appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3284 | 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] | 3285 | appctx->ctx.stats.flags |= STAT_CHUNKED; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3286 | |
| 3287 | uri = msg->chn->buf->p + msg->sl.rq.u; |
| 3288 | lookup = uri + uri_auth->uri_len; |
| 3289 | |
| 3290 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) { |
| 3291 | if (memcmp(h, ";up", 3) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3292 | appctx->ctx.stats.flags |= STAT_HIDE_DOWN; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3293 | break; |
| 3294 | } |
| 3295 | } |
| 3296 | |
| 3297 | if (uri_auth->refresh) { |
| 3298 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) { |
| 3299 | if (memcmp(h, ";norefresh", 10) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3300 | appctx->ctx.stats.flags |= STAT_NO_REFRESH; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3301 | break; |
| 3302 | } |
| 3303 | } |
| 3304 | } |
| 3305 | |
| 3306 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) { |
| 3307 | if (memcmp(h, ";csv", 4) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3308 | appctx->ctx.stats.flags &= ~STAT_FMT_HTML; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3309 | break; |
| 3310 | } |
| 3311 | } |
| 3312 | |
| 3313 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) { |
| 3314 | if (memcmp(h, ";st=", 4) == 0) { |
| 3315 | int i; |
| 3316 | h += 4; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3317 | appctx->ctx.stats.st_code = STAT_STATUS_UNKN; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3318 | for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) { |
| 3319 | if (strncmp(stat_status_codes[i], h, 4) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3320 | appctx->ctx.stats.st_code = i; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3321 | break; |
| 3322 | } |
| 3323 | } |
| 3324 | break; |
| 3325 | } |
| 3326 | } |
| 3327 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3328 | appctx->ctx.stats.scope_str = 0; |
| 3329 | appctx->ctx.stats.scope_len = 0; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3330 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) { |
| 3331 | if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) { |
| 3332 | int itx = 0; |
| 3333 | const char *h2; |
| 3334 | char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1]; |
| 3335 | const char *err; |
| 3336 | |
| 3337 | h += strlen(STAT_SCOPE_INPUT_NAME) + 1; |
| 3338 | h2 = h; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3339 | appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3340 | while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') { |
| 3341 | itx++; |
| 3342 | h++; |
| 3343 | } |
| 3344 | |
| 3345 | if (itx > STAT_SCOPE_TXT_MAXLEN) |
| 3346 | itx = STAT_SCOPE_TXT_MAXLEN; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3347 | appctx->ctx.stats.scope_len = itx; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3348 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3349 | /* 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] | 3350 | memcpy(scope_txt, h2, itx); |
| 3351 | scope_txt[itx] = '\0'; |
| 3352 | err = invalid_char(scope_txt); |
| 3353 | if (err) { |
| 3354 | /* bad char in search text => clear scope */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3355 | appctx->ctx.stats.scope_str = 0; |
| 3356 | appctx->ctx.stats.scope_len = 0; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3357 | } |
| 3358 | break; |
| 3359 | } |
| 3360 | } |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3361 | |
| 3362 | /* now check whether we have some admin rules for this request */ |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 3363 | list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) { |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3364 | int ret = 1; |
| 3365 | |
| 3366 | if (stats_admin_rule->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 3367 | 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] | 3368 | ret = acl_pass(ret); |
| 3369 | if (stats_admin_rule->cond->pol == ACL_COND_UNLESS) |
| 3370 | ret = !ret; |
| 3371 | } |
| 3372 | |
| 3373 | if (ret) { |
| 3374 | /* no rule, or the rule matches */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3375 | appctx->ctx.stats.flags |= STAT_ADMIN; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3376 | break; |
| 3377 | } |
| 3378 | } |
| 3379 | |
| 3380 | /* Was the status page requested with a POST ? */ |
Willy Tarreau | b8cdf52 | 2015-05-29 01:09:15 +0200 | [diff] [blame] | 3381 | if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3382 | if (appctx->ctx.stats.flags & STAT_ADMIN) { |
Willy Tarreau | cfe7fdd | 2014-04-26 22:08:32 +0200 | [diff] [blame] | 3383 | /* we'll need the request body, possibly after sending 100-continue */ |
Willy Tarreau | b8cdf52 | 2015-05-29 01:09:15 +0200 | [diff] [blame] | 3384 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) |
| 3385 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3386 | appctx->st0 = STAT_HTTP_POST; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3387 | } |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 3388 | else { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3389 | appctx->ctx.stats.st_code = STAT_STATUS_DENY; |
| 3390 | appctx->st0 = STAT_HTTP_LAST; |
de Lafond Guillaume | 88c278f | 2013-04-15 19:27:10 +0200 | [diff] [blame] | 3391 | } |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3392 | } |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 3393 | else { |
| 3394 | /* So it was another method (GET/HEAD) */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 3395 | appctx->st0 = STAT_HTTP_HEAD; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3396 | } |
| 3397 | |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3398 | s->task->nice = -32; /* small boost for HTTP statistics */ |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3399 | return 1; |
| 3400 | } |
| 3401 | |
Lukas Tribus | 67db8df | 2013-06-23 17:37:13 +0200 | [diff] [blame] | 3402 | /* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets |
| 3403 | * (as per RFC3260 #4 and BCP37 #4.2 and #5.2). |
| 3404 | */ |
Thierry FOURNIER | 7fe75e0 | 2015-03-16 12:03:44 +0100 | [diff] [blame] | 3405 | void inet_set_tos(int fd, struct sockaddr_storage from, int tos) |
Lukas Tribus | 67db8df | 2013-06-23 17:37:13 +0200 | [diff] [blame] | 3406 | { |
| 3407 | #ifdef IP_TOS |
| 3408 | if (from.ss_family == AF_INET) |
| 3409 | setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)); |
| 3410 | #endif |
| 3411 | #ifdef IPV6_TCLASS |
| 3412 | if (from.ss_family == AF_INET6) { |
| 3413 | if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&from)->sin6_addr)) |
| 3414 | /* v4-mapped addresses need IP_TOS */ |
| 3415 | setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)); |
| 3416 | else |
| 3417 | setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos)); |
| 3418 | } |
| 3419 | #endif |
| 3420 | } |
| 3421 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3422 | int http_transform_header_str(struct stream* s, struct http_msg *msg, |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 3423 | const char* name, unsigned int name_len, |
| 3424 | const char *str, struct my_regex *re, |
| 3425 | int action) |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3426 | { |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 3427 | struct hdr_ctx ctx; |
| 3428 | char *buf = msg->chn->buf->p; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3429 | struct hdr_idx *idx = &s->txn->hdr_idx; |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 3430 | int (*http_find_hdr_func)(const char *name, int len, char *sol, |
| 3431 | struct hdr_idx *idx, struct hdr_ctx *ctx); |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3432 | struct chunk *output = get_trash_chunk(); |
| 3433 | |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 3434 | ctx.idx = 0; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3435 | |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3436 | /* Choose the header browsing function. */ |
| 3437 | switch (action) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3438 | case ACT_HTTP_REPLACE_VAL: |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3439 | http_find_hdr_func = http_find_header2; |
| 3440 | break; |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3441 | case ACT_HTTP_REPLACE_HDR: |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3442 | http_find_hdr_func = http_find_full_header2; |
| 3443 | break; |
| 3444 | default: /* impossible */ |
| 3445 | return -1; |
| 3446 | } |
| 3447 | |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 3448 | while (http_find_hdr_func(name, name_len, buf, idx, &ctx)) { |
| 3449 | struct hdr_idx_elem *hdr = idx->v + ctx.idx; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3450 | int delta; |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 3451 | char *val = ctx.line + ctx.val; |
| 3452 | char* val_end = val + ctx.vlen; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3453 | |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3454 | if (!regex_exec_match2(re, val, val_end-val, MAX_MATCH, pmatch, 0)) |
| 3455 | continue; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3456 | |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 3457 | output->len = exp_replace(output->str, output->size, val, str, pmatch); |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3458 | if (output->len == -1) |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3459 | return -1; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3460 | |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3461 | 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] | 3462 | |
| 3463 | hdr->len += delta; |
| 3464 | http_msg_move_end(msg, delta); |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 3465 | |
| 3466 | /* Adjust the length of the current value of the index. */ |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 3467 | ctx.vlen += delta; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3468 | } |
| 3469 | |
| 3470 | return 0; |
| 3471 | } |
| 3472 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3473 | static int http_transform_header(struct stream* s, struct http_msg *msg, |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 3474 | const char* name, unsigned int name_len, |
| 3475 | struct list *fmt, struct my_regex *re, |
| 3476 | int action) |
| 3477 | { |
| 3478 | struct chunk *replace = get_trash_chunk(); |
| 3479 | |
| 3480 | replace->len = build_logline(s, replace->str, replace->size, fmt); |
| 3481 | if (replace->len >= replace->size - 1) |
| 3482 | return -1; |
| 3483 | |
| 3484 | return http_transform_header_str(s, msg, name, name_len, replace->str, re, action); |
| 3485 | } |
| 3486 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3487 | /* Executes the http-request rules <rules> for stream <s>, proxy <px> and |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3488 | * transaction <txn>. Returns the verdict of the first rule that prevents |
| 3489 | * further processing of the request (auth, deny, ...), and defaults to |
| 3490 | * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or |
| 3491 | * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT |
| 3492 | * on txn->flags if it encounters a tarpit rule. |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3493 | */ |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3494 | enum rule_result |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 3495 | http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3496 | { |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 3497 | struct session *sess = strm_sess(s); |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 3498 | struct http_txn *txn = s->txn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3499 | struct connection *cli_conn; |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 3500 | struct act_rule *rule; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 3501 | struct hdr_ctx ctx; |
Willy Tarreau | ae3c010 | 2014-04-28 23:22:08 +0200 | [diff] [blame] | 3502 | const char *auth_realm; |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 3503 | int act_flags = 0; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3504 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3505 | /* If "the current_rule_list" match the executed rule list, we are in |
| 3506 | * resume condition. If a resume is needed it is always in the action |
| 3507 | * and never in the ACL or converters. In this case, we initialise the |
| 3508 | * current rule, and go to the action execution point. |
| 3509 | */ |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 3510 | if (s->current_rule) { |
| 3511 | rule = s->current_rule; |
| 3512 | s->current_rule = NULL; |
| 3513 | if (s->current_rule_list == rules) |
| 3514 | goto resume_execution; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3515 | } |
| 3516 | s->current_rule_list = rules; |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 3517 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3518 | list_for_each_entry(rule, rules, list) { |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3519 | |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3520 | /* check optional condition */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3521 | if (rule->cond) { |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3522 | int ret; |
| 3523 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 3524 | 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] | 3525 | ret = acl_pass(ret); |
| 3526 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3527 | if (rule->cond->pol == ACL_COND_UNLESS) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3528 | ret = !ret; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3529 | |
| 3530 | if (!ret) /* condition not matched */ |
| 3531 | continue; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3532 | } |
| 3533 | |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 3534 | act_flags |= ACT_FLAG_FIRST; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3535 | resume_execution: |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3536 | switch (rule->action) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3537 | case ACT_ACTION_ALLOW: |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3538 | return HTTP_RULE_RES_STOP; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3539 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3540 | case ACT_ACTION_DENY: |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 3541 | txn->rule_deny_status = rule->deny_status; |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3542 | return HTTP_RULE_RES_DENY; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3543 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3544 | case ACT_HTTP_REQ_TARPIT: |
Willy Tarreau | ccbcc37 | 2012-12-27 12:37:57 +0100 | [diff] [blame] | 3545 | txn->flags |= TX_CLTARPIT; |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 3546 | txn->rule_deny_status = rule->deny_status; |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3547 | return HTTP_RULE_RES_DENY; |
Willy Tarreau | ccbcc37 | 2012-12-27 12:37:57 +0100 | [diff] [blame] | 3548 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3549 | case ACT_HTTP_REQ_AUTH: |
Willy Tarreau | ae3c010 | 2014-04-28 23:22:08 +0200 | [diff] [blame] | 3550 | /* Auth might be performed on regular http-req rules as well as on stats */ |
| 3551 | auth_realm = rule->arg.auth.realm; |
| 3552 | if (!auth_realm) { |
| 3553 | if (px->uri_auth && rules == &px->uri_auth->http_req_rules) |
| 3554 | auth_realm = STATS_DEFAULT_REALM; |
| 3555 | else |
| 3556 | auth_realm = px->id; |
| 3557 | } |
| 3558 | /* send 401/407 depending on whether we use a proxy or not. We still |
| 3559 | * count one error, because normal browsing won't significantly |
| 3560 | * increase the counter but brute force attempts will. |
| 3561 | */ |
| 3562 | chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm); |
| 3563 | txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401; |
| 3564 | stream_int_retnclose(&s->si[0], &trash); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3565 | stream_inc_http_err_ctr(s); |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3566 | return HTTP_RULE_RES_ABRT; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3567 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3568 | case ACT_HTTP_REDIR: |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3569 | if (!http_apply_redirect_rule(rule->arg.redir, s, txn)) |
| 3570 | return HTTP_RULE_RES_BADREQ; |
| 3571 | return HTTP_RULE_RES_DONE; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 3572 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3573 | case ACT_HTTP_SET_NICE: |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 3574 | s->task->nice = rule->arg.nice; |
| 3575 | break; |
| 3576 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3577 | case ACT_HTTP_SET_TOS: |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 3578 | if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3579 | inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos); |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 3580 | break; |
| 3581 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3582 | case ACT_HTTP_SET_MARK: |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 3583 | #ifdef SO_MARK |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 3584 | if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3585 | setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark)); |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 3586 | #endif |
| 3587 | break; |
| 3588 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3589 | case ACT_HTTP_SET_LOGL: |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 3590 | s->logs.level = rule->arg.loglevel; |
| 3591 | break; |
| 3592 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3593 | case ACT_HTTP_REPLACE_HDR: |
| 3594 | case ACT_HTTP_REPLACE_VAL: |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 3595 | if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name, |
| 3596 | rule->arg.hdr_add.name_len, |
| 3597 | &rule->arg.hdr_add.fmt, |
| 3598 | &rule->arg.hdr_add.re, rule->action)) |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3599 | return HTTP_RULE_RES_BADREQ; |
| 3600 | break; |
| 3601 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3602 | case ACT_HTTP_DEL_HDR: |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3603 | ctx.idx = 0; |
| 3604 | /* remove all occurrences of the header */ |
| 3605 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 3606 | txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 3607 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 3608 | } |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 3609 | break; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3610 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3611 | case ACT_HTTP_SET_HDR: |
| 3612 | case ACT_HTTP_ADD_HDR: |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3613 | chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name); |
| 3614 | memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len); |
| 3615 | trash.len = rule->arg.hdr_add.name_len; |
| 3616 | trash.str[trash.len++] = ':'; |
| 3617 | trash.str[trash.len++] = ' '; |
| 3618 | 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] | 3619 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3620 | if (rule->action == ACT_HTTP_SET_HDR) { |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 3621 | /* remove all occurrences of the header */ |
| 3622 | ctx.idx = 0; |
| 3623 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 3624 | txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 3625 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
| 3626 | } |
| 3627 | } |
| 3628 | |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3629 | http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len); |
| 3630 | break; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3631 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3632 | case ACT_HTTP_DEL_ACL: |
| 3633 | case ACT_HTTP_DEL_MAP: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3634 | struct pat_ref *ref; |
| 3635 | char *key; |
| 3636 | int len; |
| 3637 | |
| 3638 | /* collect reference */ |
| 3639 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3640 | if (!ref) |
| 3641 | continue; |
| 3642 | |
| 3643 | /* collect key */ |
| 3644 | len = build_logline(s, trash.str, trash.size, &rule->arg.map.key); |
| 3645 | key = trash.str; |
| 3646 | key[len] = '\0'; |
| 3647 | |
| 3648 | /* perform update */ |
| 3649 | /* returned code: 1=ok, 0=ko */ |
| 3650 | pat_ref_delete(ref, key); |
| 3651 | |
| 3652 | break; |
| 3653 | } |
| 3654 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3655 | case ACT_HTTP_ADD_ACL: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3656 | struct pat_ref *ref; |
| 3657 | char *key; |
| 3658 | struct chunk *trash_key; |
| 3659 | int len; |
| 3660 | |
| 3661 | trash_key = get_trash_chunk(); |
| 3662 | |
| 3663 | /* collect reference */ |
| 3664 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3665 | if (!ref) |
| 3666 | continue; |
| 3667 | |
| 3668 | /* collect key */ |
| 3669 | len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key); |
| 3670 | key = trash_key->str; |
| 3671 | key[len] = '\0'; |
| 3672 | |
| 3673 | /* perform update */ |
| 3674 | /* add entry only if it does not already exist */ |
| 3675 | if (pat_ref_find_elt(ref, key) == NULL) |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 3676 | pat_ref_add(ref, key, NULL, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3677 | |
| 3678 | break; |
| 3679 | } |
| 3680 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3681 | case ACT_HTTP_SET_MAP: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3682 | struct pat_ref *ref; |
| 3683 | char *key, *value; |
| 3684 | struct chunk *trash_key, *trash_value; |
| 3685 | int len; |
| 3686 | |
| 3687 | trash_key = get_trash_chunk(); |
| 3688 | trash_value = get_trash_chunk(); |
| 3689 | |
| 3690 | /* collect reference */ |
| 3691 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3692 | if (!ref) |
| 3693 | continue; |
| 3694 | |
| 3695 | /* collect key */ |
| 3696 | len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key); |
| 3697 | key = trash_key->str; |
| 3698 | key[len] = '\0'; |
| 3699 | |
| 3700 | /* collect value */ |
| 3701 | len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value); |
| 3702 | value = trash_value->str; |
| 3703 | value[len] = '\0'; |
| 3704 | |
| 3705 | /* perform update */ |
| 3706 | if (pat_ref_find_elt(ref, key) != NULL) |
| 3707 | /* update entry if it exists */ |
| 3708 | pat_ref_set(ref, key, value, NULL); |
| 3709 | else |
| 3710 | /* insert a new entry */ |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 3711 | pat_ref_add(ref, key, value, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3712 | |
| 3713 | break; |
| 3714 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 3715 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 3716 | case ACT_CUSTOM: |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 3717 | if ((px->options & PR_O_ABRT_CLOSE) && (s->req.flags & (CF_SHUTR|CF_READ_NULL|CF_READ_ERROR))) |
| 3718 | act_flags |= ACT_FLAG_FINAL; |
Willy Tarreau | 3945868 | 2015-09-27 10:33:15 +0200 | [diff] [blame] | 3719 | |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 3720 | switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) { |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 3721 | case ACT_RET_ERR: |
| 3722 | case ACT_RET_CONT: |
| 3723 | break; |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 3724 | case ACT_RET_STOP: |
| 3725 | return HTTP_RULE_RES_DONE; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 3726 | case ACT_RET_YIELD: |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 3727 | s->current_rule = rule; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3728 | return HTTP_RULE_RES_YIELD; |
| 3729 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 3730 | break; |
| 3731 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3732 | case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX: |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 3733 | /* Note: only the first valid tracking parameter of each |
| 3734 | * applies. |
| 3735 | */ |
| 3736 | |
| 3737 | if (stkctr_entry(&s->stkctr[http_req_trk_idx(rule->action)]) == NULL) { |
| 3738 | struct stktable *t; |
| 3739 | struct stksess *ts; |
| 3740 | struct stktable_key *key; |
| 3741 | void *ptr; |
| 3742 | |
Thierry FOURNIER | 5ec63e0 | 2015-08-04 09:09:48 +0200 | [diff] [blame] | 3743 | t = rule->arg.trk_ctr.table.t; |
| 3744 | 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] | 3745 | |
| 3746 | if (key && (ts = stktable_get_entry(t, key))) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3747 | stream_track_stkctr(&s->stkctr[http_req_trk_idx(rule->action)], t, ts); |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 3748 | |
| 3749 | /* let's count a new HTTP request as it's the first time we do it */ |
| 3750 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT); |
| 3751 | if (ptr) |
| 3752 | stktable_data_cast(ptr, http_req_cnt)++; |
| 3753 | |
| 3754 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE); |
| 3755 | if (ptr) |
| 3756 | update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate), |
| 3757 | t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1); |
| 3758 | |
| 3759 | stkctr_set_flags(&s->stkctr[http_req_trk_idx(rule->action)], STKCTR_TRACK_CONTENT); |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 3760 | if (sess->fe != s->be) |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 3761 | stkctr_set_flags(&s->stkctr[http_req_trk_idx(rule->action)], STKCTR_TRACK_BACKEND); |
| 3762 | } |
| 3763 | } |
Adis Nezirovic | 2fbcafc | 2015-07-06 15:44:30 +0200 | [diff] [blame] | 3764 | break; |
| 3765 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3766 | case ACT_HTTP_REQ_SET_SRC: |
Adis Nezirovic | 2fbcafc | 2015-07-06 15:44:30 +0200 | [diff] [blame] | 3767 | if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) { |
| 3768 | struct sample *smp; |
| 3769 | |
Thierry FOURNIER | a002dc9 | 2015-07-31 08:50:51 +0200 | [diff] [blame] | 3770 | smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.expr, SMP_T_ADDR); |
Adis Nezirovic | 2fbcafc | 2015-07-06 15:44:30 +0200 | [diff] [blame] | 3771 | |
| 3772 | if (smp) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3773 | if (smp->data.type == SMP_T_IPV4) { |
Adis Nezirovic | 2fbcafc | 2015-07-06 15:44:30 +0200 | [diff] [blame] | 3774 | ((struct sockaddr_in *)&cli_conn->addr.from)->sin_family = AF_INET; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 3775 | ((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr = smp->data.u.ipv4.s_addr; |
Adis Nezirovic | 2fbcafc | 2015-07-06 15:44:30 +0200 | [diff] [blame] | 3776 | ((struct sockaddr_in *)&cli_conn->addr.from)->sin_port = 0; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3777 | } else if (smp->data.type == SMP_T_IPV6) { |
Adis Nezirovic | 2fbcafc | 2015-07-06 15:44:30 +0200 | [diff] [blame] | 3778 | ((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_family = AF_INET6; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 3779 | memcpy(&((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, &smp->data.u.ipv6, sizeof(struct in6_addr)); |
Adis Nezirovic | 2fbcafc | 2015-07-06 15:44:30 +0200 | [diff] [blame] | 3780 | ((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_port = 0; |
| 3781 | } |
| 3782 | } |
| 3783 | } |
| 3784 | break; |
Thierry FOURNIER | 22e4901 | 2015-08-05 19:13:48 +0200 | [diff] [blame] | 3785 | |
| 3786 | /* other flags exists, but normaly, they never be matched. */ |
| 3787 | default: |
| 3788 | break; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3789 | } |
| 3790 | } |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3791 | |
| 3792 | /* we reached the end of the rules, nothing to report */ |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3793 | return HTTP_RULE_RES_CONT; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3794 | } |
| 3795 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3796 | |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 3797 | /* Executes the http-response rules <rules> for stream <s> and proxy <px>. It |
| 3798 | * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP, |
| 3799 | * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT |
| 3800 | * is returned, the process can continue the evaluation of next rule list. If |
| 3801 | * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ |
| 3802 | * is returned, it means the operation could not be processed and a server error |
| 3803 | * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a |
| 3804 | * deny rule. If *YIELD is returned, the caller must call again the function |
| 3805 | * with the same context. |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3806 | */ |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 3807 | static enum rule_result |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 3808 | 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] | 3809 | { |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 3810 | struct session *sess = strm_sess(s); |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 3811 | struct http_txn *txn = s->txn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3812 | struct connection *cli_conn; |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 3813 | struct act_rule *rule; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3814 | struct hdr_ctx ctx; |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 3815 | int act_flags = 0; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3816 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3817 | /* If "the current_rule_list" match the executed rule list, we are in |
| 3818 | * resume condition. If a resume is needed it is always in the action |
| 3819 | * and never in the ACL or converters. In this case, we initialise the |
| 3820 | * current rule, and go to the action execution point. |
| 3821 | */ |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 3822 | if (s->current_rule) { |
| 3823 | rule = s->current_rule; |
| 3824 | s->current_rule = NULL; |
| 3825 | if (s->current_rule_list == rules) |
| 3826 | goto resume_execution; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3827 | } |
| 3828 | s->current_rule_list = rules; |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 3829 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3830 | list_for_each_entry(rule, rules, list) { |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3831 | |
| 3832 | /* check optional condition */ |
| 3833 | if (rule->cond) { |
| 3834 | int ret; |
| 3835 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 3836 | 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] | 3837 | ret = acl_pass(ret); |
| 3838 | |
| 3839 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 3840 | ret = !ret; |
| 3841 | |
| 3842 | if (!ret) /* condition not matched */ |
| 3843 | continue; |
| 3844 | } |
| 3845 | |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 3846 | act_flags |= ACT_FLAG_FIRST; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3847 | resume_execution: |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3848 | switch (rule->action) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3849 | case ACT_ACTION_ALLOW: |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 3850 | return HTTP_RULE_RES_STOP; /* "allow" rules are OK */ |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3851 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3852 | case ACT_ACTION_DENY: |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3853 | txn->flags |= TX_SVDENY; |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 3854 | return HTTP_RULE_RES_STOP; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3855 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3856 | case ACT_HTTP_SET_NICE: |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 3857 | s->task->nice = rule->arg.nice; |
| 3858 | break; |
| 3859 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3860 | case ACT_HTTP_SET_TOS: |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 3861 | if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3862 | inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos); |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 3863 | break; |
| 3864 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3865 | case ACT_HTTP_SET_MARK: |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 3866 | #ifdef SO_MARK |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 3867 | if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3868 | setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark)); |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 3869 | #endif |
| 3870 | break; |
| 3871 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3872 | case ACT_HTTP_SET_LOGL: |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 3873 | s->logs.level = rule->arg.loglevel; |
| 3874 | break; |
| 3875 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3876 | case ACT_HTTP_REPLACE_HDR: |
| 3877 | case ACT_HTTP_REPLACE_VAL: |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 3878 | if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name, |
| 3879 | rule->arg.hdr_add.name_len, |
| 3880 | &rule->arg.hdr_add.fmt, |
| 3881 | &rule->arg.hdr_add.re, rule->action)) |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 3882 | return HTTP_RULE_RES_STOP; /* note: we should report an error here */ |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 3883 | break; |
| 3884 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3885 | case ACT_HTTP_DEL_HDR: |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3886 | ctx.idx = 0; |
| 3887 | /* remove all occurrences of the header */ |
| 3888 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 3889 | txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 3890 | http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx); |
| 3891 | } |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 3892 | break; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3893 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3894 | case ACT_HTTP_SET_HDR: |
| 3895 | case ACT_HTTP_ADD_HDR: |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3896 | chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name); |
| 3897 | memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len); |
| 3898 | trash.len = rule->arg.hdr_add.name_len; |
| 3899 | trash.str[trash.len++] = ':'; |
| 3900 | trash.str[trash.len++] = ' '; |
| 3901 | 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] | 3902 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3903 | if (rule->action == ACT_HTTP_SET_HDR) { |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 3904 | /* remove all occurrences of the header */ |
| 3905 | ctx.idx = 0; |
| 3906 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 3907 | txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 3908 | http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx); |
| 3909 | } |
| 3910 | } |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 3911 | http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len); |
| 3912 | break; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3913 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3914 | case ACT_HTTP_DEL_ACL: |
| 3915 | case ACT_HTTP_DEL_MAP: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3916 | struct pat_ref *ref; |
| 3917 | char *key; |
| 3918 | int len; |
| 3919 | |
| 3920 | /* collect reference */ |
| 3921 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3922 | if (!ref) |
| 3923 | continue; |
| 3924 | |
| 3925 | /* collect key */ |
| 3926 | len = build_logline(s, trash.str, trash.size, &rule->arg.map.key); |
| 3927 | key = trash.str; |
| 3928 | key[len] = '\0'; |
| 3929 | |
| 3930 | /* perform update */ |
| 3931 | /* returned code: 1=ok, 0=ko */ |
| 3932 | pat_ref_delete(ref, key); |
| 3933 | |
| 3934 | break; |
| 3935 | } |
| 3936 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3937 | case ACT_HTTP_ADD_ACL: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3938 | struct pat_ref *ref; |
| 3939 | char *key; |
| 3940 | struct chunk *trash_key; |
| 3941 | int len; |
| 3942 | |
| 3943 | trash_key = get_trash_chunk(); |
| 3944 | |
| 3945 | /* collect reference */ |
| 3946 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3947 | if (!ref) |
| 3948 | continue; |
| 3949 | |
| 3950 | /* collect key */ |
| 3951 | len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key); |
| 3952 | key = trash_key->str; |
| 3953 | key[len] = '\0'; |
| 3954 | |
| 3955 | /* perform update */ |
| 3956 | /* check if the entry already exists */ |
| 3957 | if (pat_ref_find_elt(ref, key) == NULL) |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 3958 | pat_ref_add(ref, key, NULL, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3959 | |
| 3960 | break; |
| 3961 | } |
| 3962 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3963 | case ACT_HTTP_SET_MAP: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3964 | struct pat_ref *ref; |
| 3965 | char *key, *value; |
| 3966 | struct chunk *trash_key, *trash_value; |
| 3967 | int len; |
| 3968 | |
| 3969 | trash_key = get_trash_chunk(); |
| 3970 | trash_value = get_trash_chunk(); |
| 3971 | |
| 3972 | /* collect reference */ |
| 3973 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 3974 | if (!ref) |
| 3975 | continue; |
| 3976 | |
| 3977 | /* collect key */ |
| 3978 | len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key); |
| 3979 | key = trash_key->str; |
| 3980 | key[len] = '\0'; |
| 3981 | |
| 3982 | /* collect value */ |
| 3983 | len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value); |
| 3984 | value = trash_value->str; |
| 3985 | value[len] = '\0'; |
| 3986 | |
| 3987 | /* perform update */ |
| 3988 | if (pat_ref_find_elt(ref, key) != NULL) |
| 3989 | /* update entry if it exists */ |
| 3990 | pat_ref_set(ref, key, value, NULL); |
| 3991 | else |
| 3992 | /* insert a new entry */ |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 3993 | pat_ref_add(ref, key, value, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 3994 | |
| 3995 | break; |
| 3996 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 3997 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 3998 | case ACT_HTTP_REDIR: |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 3999 | if (!http_apply_redirect_rule(rule->arg.redir, s, txn)) |
| 4000 | return HTTP_RULE_RES_BADREQ; |
| 4001 | return HTTP_RULE_RES_DONE; |
| 4002 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 4003 | case ACT_CUSTOM: |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 4004 | if ((px->options & PR_O_ABRT_CLOSE) && (s->req.flags & (CF_SHUTR|CF_READ_NULL|CF_READ_ERROR))) |
| 4005 | act_flags |= ACT_FLAG_FINAL; |
Willy Tarreau | 3945868 | 2015-09-27 10:33:15 +0200 | [diff] [blame] | 4006 | |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 4007 | switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) { |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 4008 | case ACT_RET_ERR: |
| 4009 | case ACT_RET_CONT: |
| 4010 | break; |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 4011 | case ACT_RET_STOP: |
| 4012 | return HTTP_RULE_RES_STOP; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 4013 | case ACT_RET_YIELD: |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 4014 | s->current_rule = rule; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 4015 | return HTTP_RULE_RES_YIELD; |
| 4016 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 4017 | break; |
| 4018 | |
Thierry FOURNIER | 22e4901 | 2015-08-05 19:13:48 +0200 | [diff] [blame] | 4019 | /* other flags exists, but normaly, they never be matched. */ |
| 4020 | default: |
| 4021 | break; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 4022 | } |
| 4023 | } |
| 4024 | |
| 4025 | /* we reached the end of the rules, nothing to report */ |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 4026 | return HTTP_RULE_RES_CONT; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 4027 | } |
| 4028 | |
| 4029 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4030 | /* Perform an HTTP redirect based on the information in <rule>. The function |
| 4031 | * returns non-zero on success, or zero in case of a, irrecoverable error such |
| 4032 | * as too large a request to build a valid response. |
| 4033 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4034 | 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] | 4035 | { |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 4036 | struct http_msg *req = &txn->req; |
| 4037 | struct http_msg *res = &txn->rsp; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4038 | const char *msg_fmt; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4039 | const char *location; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4040 | |
| 4041 | /* build redirect message */ |
| 4042 | switch(rule->code) { |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 4043 | case 308: |
| 4044 | msg_fmt = HTTP_308; |
| 4045 | break; |
| 4046 | case 307: |
| 4047 | msg_fmt = HTTP_307; |
| 4048 | break; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4049 | case 303: |
| 4050 | msg_fmt = HTTP_303; |
| 4051 | break; |
| 4052 | case 301: |
| 4053 | msg_fmt = HTTP_301; |
| 4054 | break; |
| 4055 | case 302: |
| 4056 | default: |
| 4057 | msg_fmt = HTTP_302; |
| 4058 | break; |
| 4059 | } |
| 4060 | |
| 4061 | if (unlikely(!chunk_strcpy(&trash, msg_fmt))) |
| 4062 | return 0; |
| 4063 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4064 | location = trash.str + trash.len; |
| 4065 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4066 | switch(rule->type) { |
| 4067 | case REDIRECT_TYPE_SCHEME: { |
| 4068 | const char *path; |
| 4069 | const char *host; |
| 4070 | struct hdr_ctx ctx; |
| 4071 | int pathlen; |
| 4072 | int hostlen; |
| 4073 | |
| 4074 | host = ""; |
| 4075 | hostlen = 0; |
| 4076 | ctx.idx = 0; |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 4077 | 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] | 4078 | host = ctx.line + ctx.val; |
| 4079 | hostlen = ctx.vlen; |
| 4080 | } |
| 4081 | |
| 4082 | path = http_get_path(txn); |
| 4083 | /* build message using path */ |
| 4084 | if (path) { |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 4085 | 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] | 4086 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 4087 | int qs = 0; |
| 4088 | while (qs < pathlen) { |
| 4089 | if (path[qs] == '?') { |
| 4090 | pathlen = qs; |
| 4091 | break; |
| 4092 | } |
| 4093 | qs++; |
| 4094 | } |
| 4095 | } |
| 4096 | } else { |
| 4097 | path = "/"; |
| 4098 | pathlen = 1; |
| 4099 | } |
| 4100 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4101 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
| 4102 | /* check if we can add scheme + "://" + host + path */ |
| 4103 | if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4) |
| 4104 | return 0; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4105 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4106 | /* add scheme */ |
| 4107 | memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len); |
| 4108 | trash.len += rule->rdr_len; |
| 4109 | } |
| 4110 | else { |
| 4111 | /* add scheme with executing log format */ |
| 4112 | trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt); |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4113 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4114 | /* check if we can add scheme + "://" + host + path */ |
| 4115 | if (trash.len + 3 + hostlen + pathlen > trash.size - 4) |
| 4116 | return 0; |
| 4117 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4118 | /* add "://" */ |
| 4119 | memcpy(trash.str + trash.len, "://", 3); |
| 4120 | trash.len += 3; |
| 4121 | |
| 4122 | /* add host */ |
| 4123 | memcpy(trash.str + trash.len, host, hostlen); |
| 4124 | trash.len += hostlen; |
| 4125 | |
| 4126 | /* add path */ |
| 4127 | memcpy(trash.str + trash.len, path, pathlen); |
| 4128 | trash.len += pathlen; |
| 4129 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4130 | /* append a slash at the end of the location if needed and missing */ |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4131 | if (trash.len && trash.str[trash.len - 1] != '/' && |
| 4132 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
| 4133 | if (trash.len > trash.size - 5) |
| 4134 | return 0; |
| 4135 | trash.str[trash.len] = '/'; |
| 4136 | trash.len++; |
| 4137 | } |
| 4138 | |
| 4139 | break; |
| 4140 | } |
| 4141 | case REDIRECT_TYPE_PREFIX: { |
| 4142 | const char *path; |
| 4143 | int pathlen; |
| 4144 | |
| 4145 | path = http_get_path(txn); |
| 4146 | /* build message using path */ |
| 4147 | if (path) { |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 4148 | 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] | 4149 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 4150 | int qs = 0; |
| 4151 | while (qs < pathlen) { |
| 4152 | if (path[qs] == '?') { |
| 4153 | pathlen = qs; |
| 4154 | break; |
| 4155 | } |
| 4156 | qs++; |
| 4157 | } |
| 4158 | } |
| 4159 | } else { |
| 4160 | path = "/"; |
| 4161 | pathlen = 1; |
| 4162 | } |
| 4163 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4164 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
| 4165 | if (trash.len + rule->rdr_len + pathlen > trash.size - 4) |
| 4166 | return 0; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4167 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4168 | /* add prefix. Note that if prefix == "/", we don't want to |
| 4169 | * add anything, otherwise it makes it hard for the user to |
| 4170 | * configure a self-redirection. |
| 4171 | */ |
| 4172 | if (rule->rdr_len != 1 || *rule->rdr_str != '/') { |
| 4173 | memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len); |
| 4174 | trash.len += rule->rdr_len; |
| 4175 | } |
| 4176 | } |
| 4177 | else { |
| 4178 | /* add prefix with executing log format */ |
| 4179 | trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt); |
| 4180 | |
| 4181 | /* Check length */ |
| 4182 | if (trash.len + pathlen > trash.size - 4) |
| 4183 | return 0; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4184 | } |
| 4185 | |
| 4186 | /* add path */ |
| 4187 | memcpy(trash.str + trash.len, path, pathlen); |
| 4188 | trash.len += pathlen; |
| 4189 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4190 | /* append a slash at the end of the location if needed and missing */ |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4191 | if (trash.len && trash.str[trash.len - 1] != '/' && |
| 4192 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
| 4193 | if (trash.len > trash.size - 5) |
| 4194 | return 0; |
| 4195 | trash.str[trash.len] = '/'; |
| 4196 | trash.len++; |
| 4197 | } |
| 4198 | |
| 4199 | break; |
| 4200 | } |
| 4201 | case REDIRECT_TYPE_LOCATION: |
| 4202 | default: |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4203 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
| 4204 | if (trash.len + rule->rdr_len > trash.size - 4) |
| 4205 | return 0; |
| 4206 | |
| 4207 | /* add location */ |
| 4208 | memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len); |
| 4209 | trash.len += rule->rdr_len; |
| 4210 | } |
| 4211 | else { |
| 4212 | /* add location with executing log format */ |
| 4213 | trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt); |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4214 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4215 | /* Check left length */ |
| 4216 | if (trash.len > trash.size - 4) |
| 4217 | return 0; |
| 4218 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4219 | break; |
| 4220 | } |
| 4221 | |
| 4222 | if (rule->cookie_len) { |
| 4223 | memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14); |
| 4224 | trash.len += 14; |
| 4225 | memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len); |
| 4226 | trash.len += rule->cookie_len; |
| 4227 | memcpy(trash.str + trash.len, "\r\n", 2); |
| 4228 | trash.len += 2; |
| 4229 | } |
| 4230 | |
| 4231 | /* add end of headers and the keep-alive/close status. |
| 4232 | * We may choose to set keep-alive if the Location begins |
| 4233 | * with a slash, because the client will come back to the |
| 4234 | * same server. |
| 4235 | */ |
| 4236 | txn->status = rule->code; |
| 4237 | /* let's log the request time */ |
| 4238 | s->logs.tv_request = now; |
| 4239 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 4240 | if (*location == '/' && |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 4241 | (req->flags & HTTP_MSGF_XFER_LEN) && |
Willy Tarreau | 2de8a50 | 2015-05-28 17:23:54 +0200 | [diff] [blame] | 4242 | ((!(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] | 4243 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL || |
| 4244 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) { |
| 4245 | /* keep-alive possible */ |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 4246 | if (!(req->flags & HTTP_MSGF_VER_11)) { |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4247 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 4248 | memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30); |
| 4249 | trash.len += 30; |
| 4250 | } else { |
| 4251 | memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24); |
| 4252 | trash.len += 24; |
| 4253 | } |
| 4254 | } |
| 4255 | memcpy(trash.str + trash.len, "\r\n\r\n", 4); |
| 4256 | trash.len += 4; |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 4257 | bo_inject(res->chn, trash.str, trash.len); |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4258 | /* "eat" the request */ |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 4259 | bi_fast_delete(req->chn->buf, req->sov); |
| 4260 | req->next -= req->sov; |
| 4261 | req->sov = 0; |
| 4262 | s->req.analysers = AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4263 | s->res.analysers = AN_RES_HTTP_XFER_BODY; |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 4264 | req->msg_state = HTTP_MSG_CLOSED; |
| 4265 | res->msg_state = HTTP_MSG_DONE; |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 4266 | /* Trim any possible response */ |
| 4267 | res->chn->buf->i = 0; |
| 4268 | res->next = res->sov = 0; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4269 | } else { |
| 4270 | /* keep-alive not possible */ |
| 4271 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 4272 | memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29); |
| 4273 | trash.len += 29; |
| 4274 | } else { |
| 4275 | memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23); |
| 4276 | trash.len += 23; |
| 4277 | } |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4278 | stream_int_retnclose(&s->si[0], &trash); |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 4279 | req->chn->analysers = 0; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4280 | } |
| 4281 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4282 | if (!(s->flags & SF_ERR_MASK)) |
| 4283 | s->flags |= SF_ERR_LOCAL; |
| 4284 | if (!(s->flags & SF_FINST_MASK)) |
| 4285 | s->flags |= SF_FINST_R; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4286 | |
| 4287 | return 1; |
| 4288 | } |
| 4289 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4290 | /* This stream analyser runs all HTTP request processing which is common to |
| 4291 | * frontends and backends, which means blocking ACLs, filters, connection-close, |
| 4292 | * reqadd, stats and redirects. This is performed for the designated proxy. |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 4293 | * 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] | 4294 | * either needs more data or wants to immediately abort the request (eg: deny, |
| 4295 | * error, ...). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 4296 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4297 | 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] | 4298 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4299 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4300 | struct http_txn *txn = s->txn; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 4301 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4302 | struct redirect_rule *rule; |
Willy Tarreau | f4f0412 | 2010-01-28 18:10:50 +0100 | [diff] [blame] | 4303 | struct cond_wordlist *wl; |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4304 | enum rule_result verdict; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 4305 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4306 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 4307 | /* we need more data */ |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 4308 | goto return_prx_yield; |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 4309 | } |
| 4310 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4311 | 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] | 4312 | now_ms, __FUNCTION__, |
| 4313 | s, |
| 4314 | req, |
| 4315 | req->rex, req->wex, |
| 4316 | req->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4317 | req->buf->i, |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 4318 | req->analysers); |
| 4319 | |
Willy Tarreau | 6541083 | 2014-04-28 21:25:43 +0200 | [diff] [blame] | 4320 | /* just in case we have some per-backend tracking */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4321 | stream_inc_be_http_req_ctr(s); |
Willy Tarreau | 6541083 | 2014-04-28 21:25:43 +0200 | [diff] [blame] | 4322 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 4323 | /* evaluate http-request rules */ |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4324 | if (!LIST_ISEMPTY(&px->http_req_rules)) { |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 4325 | verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s); |
Willy Tarreau | 5142594 | 2010-02-01 10:40:19 +0100 | [diff] [blame] | 4326 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4327 | switch (verdict) { |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 4328 | case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */ |
| 4329 | goto return_prx_yield; |
| 4330 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4331 | case HTTP_RULE_RES_CONT: |
| 4332 | case HTTP_RULE_RES_STOP: /* nothing to do */ |
| 4333 | break; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4334 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4335 | case HTTP_RULE_RES_DENY: /* deny or tarpit */ |
| 4336 | if (txn->flags & TX_CLTARPIT) |
| 4337 | goto tarpit; |
| 4338 | goto deny; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4339 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4340 | case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */ |
| 4341 | goto return_prx_cond; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4342 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4343 | case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */ |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4344 | goto done; |
| 4345 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4346 | case HTTP_RULE_RES_BADREQ: /* failed with a bad request */ |
| 4347 | goto return_bad_req; |
| 4348 | } |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4349 | } |
| 4350 | |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4351 | /* OK at this stage, we know that the request was accepted according to |
| 4352 | * the http-request rules, we can check for the stats. Note that the |
| 4353 | * URI is detected *before* the req* rules in order not to be affected |
| 4354 | * by a possible reqrep, while they are processed *after* so that a |
| 4355 | * reqdeny can still block them. This clearly needs to change in 1.6! |
| 4356 | */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4357 | if (stats_check_uri(&s->si[1], txn, px)) { |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4358 | s->target = &http_stats_applet.obj_type; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4359 | 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] | 4360 | txn->status = 500; |
| 4361 | s->logs.tv_request = now; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4362 | stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_500)); |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 4363 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4364 | if (!(s->flags & SF_ERR_MASK)) |
| 4365 | s->flags |= SF_ERR_RESOURCE; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4366 | goto return_prx_cond; |
| 4367 | } |
| 4368 | |
| 4369 | /* parse the whole stats request and extract the relevant information */ |
| 4370 | http_handle_stats(s, req); |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 4371 | verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s); |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4372 | /* not all actions implemented: deny, allow, auth */ |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4373 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4374 | if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */ |
| 4375 | goto deny; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4376 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4377 | if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */ |
| 4378 | goto return_prx_cond; |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 4379 | } |
| 4380 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4381 | /* evaluate the req* rules except reqadd */ |
| 4382 | if (px->req_exp != NULL) { |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 4383 | if (apply_filters_to_request(s, req, px) < 0) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4384 | goto return_bad_req; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4385 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4386 | if (txn->flags & TX_CLDENY) |
| 4387 | goto deny; |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 4388 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4389 | if (txn->flags & TX_CLTARPIT) |
| 4390 | goto tarpit; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4391 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4392 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 4393 | /* add request headers from the rule sets in the same order */ |
| 4394 | list_for_each_entry(wl, &px->req_add, list) { |
| 4395 | if (wl->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 4396 | 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] | 4397 | ret = acl_pass(ret); |
| 4398 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 4399 | ret = !ret; |
| 4400 | if (!ret) |
| 4401 | continue; |
| 4402 | } |
| 4403 | |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 4404 | 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] | 4405 | goto return_bad_req; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 4406 | } |
| 4407 | |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 4408 | |
| 4409 | /* Proceed with the stats now. */ |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 4410 | if (unlikely(objt_applet(s->target) == &http_stats_applet)) { |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 4411 | /* process the stats request now */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4412 | if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */ |
| 4413 | sess->fe->fe_counters.intercepted_req++; |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 4414 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4415 | if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is |
| 4416 | s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy |
| 4417 | if (!(s->flags & SF_FINST_MASK)) |
| 4418 | s->flags |= SF_FINST_R; |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 4419 | |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 4420 | /* we may want to compress the stats page */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4421 | if (sess->fe->comp || s->be->comp) |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 4422 | select_compression_request_header(s, req->buf); |
| 4423 | |
| 4424 | /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */ |
Willy Tarreau | 5506e3f | 2014-11-20 22:23:10 +0100 | [diff] [blame] | 4425 | req->analysers = (req->analysers & AN_REQ_HTTP_BODY) | AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4426 | goto done; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4427 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4428 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4429 | /* check whether we have some ACLs set to redirect this request */ |
| 4430 | list_for_each_entry(rule, &px->redirect_rules, list) { |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 4431 | if (rule->cond) { |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4432 | int ret; |
| 4433 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 4434 | 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] | 4435 | ret = acl_pass(ret); |
| 4436 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 4437 | ret = !ret; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4438 | if (!ret) |
| 4439 | continue; |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 4440 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 4441 | if (!http_apply_redirect_rule(rule, s, txn)) |
| 4442 | goto return_bad_req; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4443 | goto done; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4444 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 4445 | |
Willy Tarreau | 2be3939 | 2010-01-03 17:24:51 +0100 | [diff] [blame] | 4446 | /* POST requests may be accompanied with an "Expect: 100-Continue" header. |
| 4447 | * If this happens, then the data will not come immediately, so we must |
| 4448 | * send all what we have without waiting. Note that due to the small gain |
| 4449 | * 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] | 4450 | * 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] | 4451 | * itself once used. |
| 4452 | */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4453 | req->flags |= CF_SEND_DONTWAIT; |
Willy Tarreau | 2be3939 | 2010-01-03 17:24:51 +0100 | [diff] [blame] | 4454 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4455 | done: /* done with this analyser, continue with next ones that the calling |
| 4456 | * points will have set, if any. |
| 4457 | */ |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4458 | req->analyse_exp = TICK_ETERNITY; |
Thierry FOURNIER | 7566e30 | 2014-08-22 06:55:26 +0200 | [diff] [blame] | 4459 | done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */ |
| 4460 | req->analysers &= ~an_bit; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4461 | return 1; |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 4462 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4463 | tarpit: |
| 4464 | /* When a connection is tarpitted, we use the tarpit timeout, |
| 4465 | * which may be the same as the connect timeout if unspecified. |
| 4466 | * If unset, then set it to zero because we really want it to |
| 4467 | * eventually expire. We build the tarpit as an analyser. |
| 4468 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4469 | channel_erase(&s->req); |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4470 | |
| 4471 | /* wipe the request out so that we can drop the connection early |
| 4472 | * if the client closes first. |
| 4473 | */ |
| 4474 | channel_dont_connect(req); |
| 4475 | req->analysers = 0; /* remove switching rules etc... */ |
| 4476 | req->analysers |= AN_REQ_HTTP_TARPIT; |
| 4477 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit); |
| 4478 | if (!req->analyse_exp) |
| 4479 | req->analyse_exp = tick_add(now_ms, 0); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4480 | stream_inc_http_err_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4481 | sess->fe->fe_counters.denied_req++; |
| 4482 | if (sess->fe != s->be) |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4483 | s->be->be_counters.denied_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4484 | if (sess->listener->counters) |
| 4485 | sess->listener->counters->denied_req++; |
Thierry FOURNIER | 7566e30 | 2014-08-22 06:55:26 +0200 | [diff] [blame] | 4486 | goto done_without_exp; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4487 | |
| 4488 | deny: /* this request was blocked (denied) */ |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 4489 | txn->flags |= TX_CLDENY; |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 4490 | txn->status = http_err_codes[txn->rule_deny_status]; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4491 | s->logs.tv_request = now; |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 4492 | stream_int_retnclose(&s->si[0], http_error_message(s, txn->rule_deny_status)); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4493 | stream_inc_http_err_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4494 | sess->fe->fe_counters.denied_req++; |
| 4495 | if (sess->fe != s->be) |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4496 | s->be->be_counters.denied_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4497 | if (sess->listener->counters) |
| 4498 | sess->listener->counters->denied_req++; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 4499 | goto return_prx_cond; |
| 4500 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4501 | return_bad_req: |
| 4502 | /* We centralize bad requests processing here */ |
| 4503 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 4504 | /* we detected a parsing error. We want to archive this request |
| 4505 | * in the dedicated proxy area for later troubleshooting. |
| 4506 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4507 | http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe); |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4508 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 4509 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4510 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4511 | txn->status = 400; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4512 | stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4513 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4514 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4515 | if (sess->listener->counters) |
| 4516 | sess->listener->counters->failed_req++; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 4517 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4518 | return_prx_cond: |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4519 | if (!(s->flags & SF_ERR_MASK)) |
| 4520 | s->flags |= SF_ERR_PRXCOND; |
| 4521 | if (!(s->flags & SF_FINST_MASK)) |
| 4522 | s->flags |= SF_FINST_R; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 4523 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4524 | req->analysers = 0; |
| 4525 | req->analyse_exp = TICK_ETERNITY; |
| 4526 | return 0; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 4527 | |
| 4528 | return_prx_yield: |
| 4529 | channel_dont_connect(req); |
| 4530 | return 0; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4531 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4532 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4533 | /* This function performs all the processing enabled for the current request. |
| 4534 | * It returns 1 if the processing can continue on next analysers, or zero if it |
| 4535 | * needs more data, encounters an error, or wants to immediately abort the |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4536 | * request. It relies on buffers flags, and updates s->req.analysers. |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4537 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4538 | 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] | 4539 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4540 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4541 | struct http_txn *txn = s->txn; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4542 | struct http_msg *msg = &txn->req; |
Willy Tarreau | ee335e6 | 2015-04-21 18:15:13 +0200 | [diff] [blame] | 4543 | struct connection *cli_conn = objt_conn(strm_sess(s)->origin); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4544 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4545 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 4546 | /* we need more data */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4547 | channel_dont_connect(req); |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 4548 | return 0; |
| 4549 | } |
| 4550 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4551 | 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] | 4552 | now_ms, __FUNCTION__, |
| 4553 | s, |
| 4554 | req, |
| 4555 | req->rex, req->wex, |
| 4556 | req->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4557 | req->buf->i, |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4558 | req->analysers); |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4559 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4560 | if (sess->fe->comp || s->be->comp) |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 4561 | select_compression_request_header(s, req->buf); |
| 4562 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4563 | /* |
| 4564 | * Right now, we know that we have processed the entire headers |
| 4565 | * and that unwanted requests have been filtered out. We can do |
| 4566 | * whatever we want with the remaining request. Also, now we |
| 4567 | * may have separate values for ->fe, ->be. |
| 4568 | */ |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4569 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4570 | /* |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4571 | * If HTTP PROXY is set we simply get remote server address parsing |
| 4572 | * incoming request. Note that this requires that a connection is |
| 4573 | * allocated on the server side. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4574 | */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4575 | 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] | 4576 | struct connection *conn; |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 4577 | char *path; |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 4578 | |
Willy Tarreau | 9471b8c | 2013-12-15 13:31:35 +0100 | [diff] [blame] | 4579 | /* Note that for now we don't reuse existing proxy connections */ |
Willy Tarreau | 973a542 | 2015-08-05 21:47:23 +0200 | [diff] [blame] | 4580 | if (unlikely((conn = si_alloc_conn(&s->si[1])) == NULL)) { |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 4581 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4582 | txn->status = 500; |
| 4583 | req->analysers = 0; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4584 | stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_500)); |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 4585 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4586 | if (!(s->flags & SF_ERR_MASK)) |
| 4587 | s->flags |= SF_ERR_RESOURCE; |
| 4588 | if (!(s->flags & SF_FINST_MASK)) |
| 4589 | s->flags |= SF_FINST_R; |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 4590 | |
| 4591 | return 0; |
| 4592 | } |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 4593 | |
| 4594 | path = http_get_path(txn); |
| 4595 | url2sa(req->buf->p + msg->sl.rq.u, |
| 4596 | 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] | 4597 | &conn->addr.to, NULL); |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 4598 | /* if the path was found, we have to remove everything between |
| 4599 | * req->buf->p + msg->sl.rq.u and path (excluded). If it was not |
| 4600 | * found, we need to replace from req->buf->p + msg->sl.rq.u for |
| 4601 | * u_l characters by a single "/". |
| 4602 | */ |
| 4603 | if (path) { |
| 4604 | char *cur_ptr = req->buf->p; |
| 4605 | char *cur_end = cur_ptr + txn->req.sl.rq.l; |
| 4606 | int delta; |
| 4607 | |
| 4608 | delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0); |
| 4609 | http_msg_move_end(&txn->req, delta); |
| 4610 | cur_end += delta; |
| 4611 | if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL) |
| 4612 | goto return_bad_req; |
| 4613 | } |
| 4614 | else { |
| 4615 | char *cur_ptr = req->buf->p; |
| 4616 | char *cur_end = cur_ptr + txn->req.sl.rq.l; |
| 4617 | int delta; |
| 4618 | |
| 4619 | delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, |
| 4620 | req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1); |
| 4621 | http_msg_move_end(&txn->req, delta); |
| 4622 | cur_end += delta; |
| 4623 | if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL) |
| 4624 | goto return_bad_req; |
| 4625 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4626 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4627 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4628 | /* |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 4629 | * 7: Now we can work with the cookies. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4630 | * Note that doing so might move headers in the request, but |
| 4631 | * the fields will stay coherent and the URI will not move. |
| 4632 | * This should only be performed in the backend. |
| 4633 | */ |
Willy Tarreau | 53a09d5 | 2015-08-10 18:59:40 +0200 | [diff] [blame] | 4634 | if ((s->be->cookie_name || sess->fe->capture_name) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4635 | && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) |
| 4636 | manage_client_side_cookies(s, req); |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 4637 | |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 4638 | /* add unique-id if "header-unique-id" is specified */ |
| 4639 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4640 | if (!LIST_ISEMPTY(&sess->fe->format_unique_id)) { |
William Lallemand | 5b7ea3a | 2013-08-28 15:44:19 +0200 | [diff] [blame] | 4641 | if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL) |
| 4642 | goto return_bad_req; |
| 4643 | s->unique_id[0] = '\0'; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4644 | 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] | 4645 | } |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 4646 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4647 | if (sess->fe->header_unique_id && s->unique_id) { |
| 4648 | 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] | 4649 | if (trash.len < 0) |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 4650 | goto return_bad_req; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4651 | 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] | 4652 | goto return_bad_req; |
| 4653 | } |
| 4654 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 4655 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4656 | * 9: add X-Forwarded-For if either the frontend or the backend |
| 4657 | * asks for it. |
| 4658 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4659 | if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) { |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 4660 | struct hdr_ctx ctx = { .idx = 0 }; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4661 | if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) && |
| 4662 | http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name, |
| 4663 | 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] | 4664 | req->buf->p, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 4665 | /* The header is set to be added only if none is present |
| 4666 | * and we found it, so don't do anything. |
| 4667 | */ |
| 4668 | } |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4669 | else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4670 | /* Add an X-Forwarded-For header unless the source IP is |
| 4671 | * in the 'except' network range. |
| 4672 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4673 | if ((!sess->fe->except_mask.s_addr || |
| 4674 | (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr) |
| 4675 | != sess->fe->except_net.s_addr) && |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4676 | (!s->be->except_mask.s_addr || |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4677 | (((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] | 4678 | != s->be->except_net.s_addr)) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 4679 | int len; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4680 | unsigned char *pn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4681 | pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr; |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 4682 | |
| 4683 | /* Note: we rely on the backend to get the header name to be used for |
| 4684 | * x-forwarded-for, because the header is really meant for the backends. |
| 4685 | * However, if the backend did not specify any option, we have to rely |
| 4686 | * on the frontend's header name. |
| 4687 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4688 | if (s->be->fwdfor_hdr_len) { |
| 4689 | len = s->be->fwdfor_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4690 | memcpy(trash.str, s->be->fwdfor_hdr_name, len); |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 4691 | } else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4692 | len = sess->fe->fwdfor_hdr_len; |
| 4693 | memcpy(trash.str, sess->fe->fwdfor_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 4694 | } |
Willy Tarreau | e9187f8 | 2014-04-14 15:27:14 +0200 | [diff] [blame] | 4695 | 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] | 4696 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4697 | 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] | 4698 | goto return_bad_req; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 4699 | } |
| 4700 | } |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4701 | else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4702 | /* FIXME: for the sake of completeness, we should also support |
| 4703 | * 'except' here, although it is mostly useless in this case. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 4704 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4705 | int len; |
| 4706 | char pn[INET6_ADDRSTRLEN]; |
| 4707 | inet_ntop(AF_INET6, |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4708 | (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4709 | pn, sizeof(pn)); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 4710 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4711 | /* Note: we rely on the backend to get the header name to be used for |
| 4712 | * x-forwarded-for, because the header is really meant for the backends. |
| 4713 | * However, if the backend did not specify any option, we have to rely |
| 4714 | * on the frontend's header name. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 4715 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4716 | if (s->be->fwdfor_hdr_len) { |
| 4717 | len = s->be->fwdfor_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4718 | memcpy(trash.str, s->be->fwdfor_hdr_name, len); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4719 | } else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4720 | len = sess->fe->fwdfor_hdr_len; |
| 4721 | memcpy(trash.str, sess->fe->fwdfor_hdr_name, len); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 4722 | } |
Willy Tarreau | e9187f8 | 2014-04-14 15:27:14 +0200 | [diff] [blame] | 4723 | len += snprintf(trash.str + len, trash.size - len, ": %s", pn); |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 4724 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4725 | 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] | 4726 | goto return_bad_req; |
| 4727 | } |
| 4728 | } |
| 4729 | |
| 4730 | /* |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4731 | * 10: add X-Original-To if either the frontend or the backend |
| 4732 | * asks for it. |
| 4733 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4734 | if ((sess->fe->options | s->be->options) & PR_O_ORGTO) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4735 | |
| 4736 | /* FIXME: don't know if IPv6 can handle that case too. */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4737 | if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4738 | /* Add an X-Original-To header unless the destination IP is |
| 4739 | * in the 'except' network range. |
| 4740 | */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4741 | conn_get_to_addr(cli_conn); |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4742 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4743 | if (cli_conn->addr.to.ss_family == AF_INET && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4744 | ((!sess->fe->except_mask_to.s_addr || |
| 4745 | (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr) |
| 4746 | != sess->fe->except_to.s_addr) && |
Emeric Brun | 5bd86a8 | 2010-10-22 17:23:04 +0200 | [diff] [blame] | 4747 | (!s->be->except_mask_to.s_addr || |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4748 | (((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] | 4749 | != s->be->except_to.s_addr))) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4750 | int len; |
| 4751 | unsigned char *pn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4752 | pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr; |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4753 | |
| 4754 | /* Note: we rely on the backend to get the header name to be used for |
| 4755 | * x-original-to, because the header is really meant for the backends. |
| 4756 | * However, if the backend did not specify any option, we have to rely |
| 4757 | * on the frontend's header name. |
| 4758 | */ |
| 4759 | if (s->be->orgto_hdr_len) { |
| 4760 | len = s->be->orgto_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4761 | memcpy(trash.str, s->be->orgto_hdr_name, len); |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 4762 | } else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4763 | len = sess->fe->orgto_hdr_len; |
| 4764 | memcpy(trash.str, sess->fe->orgto_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 4765 | } |
Willy Tarreau | e9187f8 | 2014-04-14 15:27:14 +0200 | [diff] [blame] | 4766 | 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] | 4767 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4768 | 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] | 4769 | goto return_bad_req; |
| 4770 | } |
| 4771 | } |
| 4772 | } |
| 4773 | |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 4774 | /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set. |
| 4775 | * If an "Upgrade" token is found, the header is left untouched in order not to have |
| 4776 | * to deal with some servers bugs : some of them fail an Upgrade if anything but |
| 4777 | * "Upgrade" is present in the Connection header. |
| 4778 | */ |
| 4779 | if (!(txn->flags & TX_HDR_CONN_UPG) && |
| 4780 | (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4781 | ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 4782 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) { |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 4783 | unsigned int want_flags = 0; |
| 4784 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4785 | if (msg->flags & HTTP_MSGF_VER_11) { |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 4786 | if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4787 | ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 4788 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4789 | !((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 4790 | want_flags |= TX_CON_CLO_SET; |
| 4791 | } else { |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 4792 | if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4793 | ((sess->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL && |
Willy Tarreau | 02bce8b | 2014-01-30 00:15:28 +0100 | [diff] [blame] | 4794 | (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4795 | ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 4796 | want_flags |= TX_CON_KAL_SET; |
| 4797 | } |
| 4798 | |
| 4799 | 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] | 4800 | http_change_connection_header(txn, msg, want_flags); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4801 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4802 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 4803 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4804 | /* If we have no server assigned yet and we're balancing on url_param |
| 4805 | * with a POST request, we may be interested in checking the body for |
| 4806 | * that parameter. This will be done in another analyser. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4807 | */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4808 | if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) && |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4809 | s->txn->meth == HTTP_METH_POST && s->be->url_param_name != NULL && |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4810 | (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) { |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4811 | channel_dont_connect(req); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4812 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4813 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4814 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4815 | if (msg->flags & HTTP_MSGF_XFER_LEN) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4816 | req->analysers |= AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 4817 | #ifdef TCP_QUICKACK |
| 4818 | /* We expect some data from the client. Unless we know for sure |
| 4819 | * we already have a full request, we have to re-enable quick-ack |
| 4820 | * in case we previously disabled it, otherwise we might cause |
| 4821 | * the client to delay further data. |
| 4822 | */ |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4823 | if ((sess->listener->options & LI_O_NOQUICKACK) && |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 4824 | cli_conn && conn_ctrl_ready(cli_conn) && |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4825 | ((msg->flags & HTTP_MSGF_TE_CHNK) || |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4826 | (msg->body_len > req->buf->i - txn->req.eoh - 2))) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 4827 | setsockopt(cli_conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one)); |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 4828 | #endif |
| 4829 | } |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 4830 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4831 | /************************************************************* |
| 4832 | * OK, that's finished for the headers. We have done what we * |
| 4833 | * could. Let's switch to the DATA state. * |
| 4834 | ************************************************************/ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4835 | req->analyse_exp = TICK_ETERNITY; |
| 4836 | req->analysers &= ~an_bit; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4837 | |
Willy Tarreau | 7bb68ab | 2012-05-13 14:48:59 +0200 | [diff] [blame] | 4838 | /* if the server closes the connection, we want to immediately react |
| 4839 | * and close the socket to save packets and syscalls. |
| 4840 | */ |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 4841 | if (!(req->analysers & AN_REQ_HTTP_XFER_BODY)) |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4842 | s->si[1].flags |= SI_FL_NOHALF; |
Willy Tarreau | 7bb68ab | 2012-05-13 14:48:59 +0200 | [diff] [blame] | 4843 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4844 | s->logs.tv_request = now; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4845 | /* OK let's go on with the BODY now */ |
| 4846 | return 1; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 4847 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4848 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 4849 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
Willy Tarreau | f073a83 | 2009-03-01 23:21:47 +0100 | [diff] [blame] | 4850 | /* we detected a parsing error. We want to archive this request |
| 4851 | * in the dedicated proxy area for later troubleshooting. |
| 4852 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4853 | http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe); |
Willy Tarreau | f073a83 | 2009-03-01 23:21:47 +0100 | [diff] [blame] | 4854 | } |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 4855 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 4856 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4857 | txn->status = 400; |
| 4858 | req->analysers = 0; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4859 | stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4860 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4861 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4862 | if (sess->listener->counters) |
| 4863 | sess->listener->counters->failed_req++; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 4864 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4865 | if (!(s->flags & SF_ERR_MASK)) |
| 4866 | s->flags |= SF_ERR_PRXCOND; |
| 4867 | if (!(s->flags & SF_FINST_MASK)) |
| 4868 | s->flags |= SF_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 4869 | return 0; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 4870 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 4871 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4872 | /* This function is an analyser which processes the HTTP tarpit. It always |
| 4873 | * returns zero, at the beginning because it prevents any other processing |
| 4874 | * from occurring, and at the end because it terminates the request. |
| 4875 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4876 | 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] | 4877 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4878 | struct http_txn *txn = s->txn; |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4879 | |
| 4880 | /* This connection is being tarpitted. The CLIENT side has |
| 4881 | * already set the connect expiration date to the right |
| 4882 | * timeout. We just have to check that the client is still |
| 4883 | * there and that the timeout has not expired. |
| 4884 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4885 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4886 | if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 && |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4887 | !tick_is_expired(req->analyse_exp, now_ms)) |
| 4888 | return 0; |
| 4889 | |
| 4890 | /* We will set the queue timer to the time spent, just for |
| 4891 | * logging purposes. We fake a 500 server error, so that the |
| 4892 | * attacker will not suspect his connection has been tarpitted. |
| 4893 | * It will not cause trouble to the logs because we can exclude |
| 4894 | * the tarpitted connections by filtering on the 'PT' status flags. |
| 4895 | */ |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4896 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 4897 | |
| 4898 | txn->status = 500; |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4899 | if (!(req->flags & CF_READ_ERROR)) |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4900 | stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_500)); |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4901 | |
| 4902 | req->analysers = 0; |
| 4903 | req->analyse_exp = TICK_ETERNITY; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4904 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4905 | if (!(s->flags & SF_ERR_MASK)) |
| 4906 | s->flags |= SF_ERR_PRXCOND; |
| 4907 | if (!(s->flags & SF_FINST_MASK)) |
| 4908 | s->flags |= SF_FINST_T; |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4909 | return 0; |
| 4910 | } |
| 4911 | |
Willy Tarreau | 5a8f947 | 2014-04-10 11:16:06 +0200 | [diff] [blame] | 4912 | /* This function is an analyser which waits for the HTTP request body. It waits |
| 4913 | * for either the buffer to be full, or the full advertised contents to have |
| 4914 | * reached the buffer. It must only be called after the standard HTTP request |
| 4915 | * processing has occurred, because it expects the request to be parsed and will |
| 4916 | * look for the Expect header. It may send a 100-Continue interim response. It |
| 4917 | * takes in input any state starting from HTTP_MSG_BODY and leaves with one of |
| 4918 | * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it |
| 4919 | * 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] | 4920 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4921 | 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] | 4922 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4923 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4924 | struct http_txn *txn = s->txn; |
| 4925 | struct http_msg *msg = &s->txn->req; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4926 | |
| 4927 | /* We have to parse the HTTP request body to find any required data. |
| 4928 | * "balance url_param check_post" should have been the only way to get |
| 4929 | * into this. We were brought here after HTTP header analysis, so all |
| 4930 | * related structures are ready. |
| 4931 | */ |
| 4932 | |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4933 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
| 4934 | /* This is the first call */ |
| 4935 | if (msg->msg_state < HTTP_MSG_BODY) |
| 4936 | goto missing_data; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4937 | |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4938 | if (msg->msg_state < HTTP_MSG_100_SENT) { |
| 4939 | /* If we have HTTP/1.1 and Expect: 100-continue, then we must |
| 4940 | * send an HTTP/1.1 100 Continue intermediate response. |
| 4941 | */ |
| 4942 | if (msg->flags & HTTP_MSGF_VER_11) { |
| 4943 | struct hdr_ctx ctx; |
| 4944 | ctx.idx = 0; |
| 4945 | /* Expect is allowed in 1.1, look for it */ |
| 4946 | if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) && |
| 4947 | unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4948 | bo_inject(&s->res, http_100_chunk.str, http_100_chunk.len); |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4949 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4950 | } |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4951 | msg->msg_state = HTTP_MSG_100_SENT; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4952 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4953 | |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 4954 | /* 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] | 4955 | * req->buf->p still points to the beginning of the message. We |
| 4956 | * must save the body in msg->next because it survives buffer |
| 4957 | * re-alignments. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4958 | */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 4959 | msg->next = msg->sov; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4960 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4961 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4962 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 4963 | else |
| 4964 | msg->msg_state = HTTP_MSG_DATA; |
| 4965 | } |
| 4966 | |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4967 | if (!(msg->flags & HTTP_MSGF_TE_CHNK)) { |
| 4968 | /* 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] | 4969 | if (http_body_bytes(msg) < msg->body_len) |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4970 | goto missing_data; |
| 4971 | |
| 4972 | /* OK we have everything we need now */ |
| 4973 | goto http_end; |
| 4974 | } |
| 4975 | |
| 4976 | /* OK here we're parsing a chunked-encoded message */ |
| 4977 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4978 | if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 4979 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4980 | * 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] | 4981 | * TRAILERS state. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 4982 | */ |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 4983 | int ret = http_parse_chunk_size(msg); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4984 | |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 4985 | if (!ret) |
| 4986 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4987 | else if (ret < 0) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4988 | stream_inc_http_err_ctr(s); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4989 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4990 | } |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4991 | } |
| 4992 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4993 | /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state. |
Willy Tarreau | e115b49 | 2015-05-01 23:05:14 +0200 | [diff] [blame] | 4994 | * We have the first data byte is in msg->sov + msg->sol. We're waiting |
| 4995 | * for at least a whole chunk or the whole content length bytes after |
| 4996 | * msg->sov + msg->sol. |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4997 | */ |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 4998 | if (msg->msg_state == HTTP_MSG_TRAILERS) |
| 4999 | goto http_end; |
| 5000 | |
Willy Tarreau | e115b49 | 2015-05-01 23:05:14 +0200 | [diff] [blame] | 5001 | 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] | 5002 | goto http_end; |
| 5003 | |
| 5004 | missing_data: |
Willy Tarreau | 31a1995 | 2014-04-10 11:50:37 +0200 | [diff] [blame] | 5005 | /* we get here if we need to wait for more data. If the buffer is full, |
| 5006 | * we have the maximum we can expect. |
| 5007 | */ |
| 5008 | if (buffer_full(req->buf, global.tune.maxrewrite)) |
| 5009 | goto http_end; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 5010 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5011 | 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] | 5012 | txn->status = 408; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5013 | stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_408)); |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +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_CLITO; |
| 5017 | if (!(s->flags & SF_FINST_MASK)) |
| 5018 | s->flags |= SF_FINST_D; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 5019 | goto return_err_msg; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 5020 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 5021 | |
| 5022 | /* we get here if we need to wait for more data */ |
Willy Tarreau | 31a1995 | 2014-04-10 11:50:37 +0200 | [diff] [blame] | 5023 | if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) { |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 5024 | /* Not enough data. We'll re-use the http-request |
| 5025 | * timeout here. Ideally, we should set the timeout |
| 5026 | * relative to the accept() date. We just set the |
| 5027 | * request timeout once at the beginning of the |
| 5028 | * request. |
| 5029 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5030 | channel_dont_connect(req); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 5031 | if (!tick_isset(req->analyse_exp)) |
Willy Tarreau | cd7afc0 | 2009-07-12 10:03:17 +0200 | [diff] [blame] | 5032 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 5033 | return 0; |
| 5034 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 5035 | |
| 5036 | http_end: |
| 5037 | /* The situation will not evolve, so let's give up on the analysis. */ |
| 5038 | s->logs.tv_request = now; /* update the request timer to reflect full request */ |
| 5039 | req->analysers &= ~an_bit; |
| 5040 | req->analyse_exp = TICK_ETERNITY; |
| 5041 | return 1; |
| 5042 | |
| 5043 | return_bad_req: /* let's centralize all bad requests */ |
| 5044 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 5045 | txn->status = 400; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5046 | stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400)); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 5047 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5048 | if (!(s->flags & SF_ERR_MASK)) |
| 5049 | s->flags |= SF_ERR_PRXCOND; |
| 5050 | if (!(s->flags & SF_FINST_MASK)) |
| 5051 | s->flags |= SF_FINST_R; |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 5052 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 5053 | return_err_msg: |
| 5054 | req->analysers = 0; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5055 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 5056 | if (sess->listener->counters) |
| 5057 | sess->listener->counters->failed_req++; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 5058 | return 0; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 5059 | } |
| 5060 | |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 5061 | /* send a server's name with an outgoing request over an established connection. |
| 5062 | * Note: this function is designed to be called once the request has been scheduled |
| 5063 | * for being forwarded. This is the reason why it rewinds the buffer before |
| 5064 | * proceeding. |
| 5065 | */ |
Willy Tarreau | 45c0d98 | 2012-03-09 12:11:57 +0100 | [diff] [blame] | 5066 | 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] | 5067 | |
| 5068 | struct hdr_ctx ctx; |
| 5069 | |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 5070 | char *hdr_name = be->server_id_hdr_name; |
| 5071 | int hdr_name_len = be->server_id_hdr_len; |
Willy Tarreau | 394db37 | 2012-10-12 22:40:39 +0200 | [diff] [blame] | 5072 | struct channel *chn = txn->req.chn; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 5073 | char *hdr_val; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 5074 | unsigned int old_o, old_i; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 5075 | |
William Lallemand | d9e9066 | 2012-01-30 17:27:17 +0100 | [diff] [blame] | 5076 | ctx.idx = 0; |
| 5077 | |
Willy Tarreau | 211cdec | 2014-04-17 20:18:08 +0200 | [diff] [blame] | 5078 | old_o = http_hdr_rewind(&txn->req); |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 5079 | if (old_o) { |
| 5080 | /* The request was already skipped, let's restore it */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5081 | b_rew(chn->buf, old_o); |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 5082 | txn->req.next += old_o; |
| 5083 | txn->req.sov += old_o; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 5084 | } |
| 5085 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5086 | old_i = chn->buf->i; |
| 5087 | 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] | 5088 | /* remove any existing values from the header */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 5089 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 5090 | } |
| 5091 | |
| 5092 | /* Add the new header requested with the server value */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 5093 | hdr_val = trash.str; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 5094 | memcpy(hdr_val, hdr_name, hdr_name_len); |
| 5095 | hdr_val += hdr_name_len; |
| 5096 | *hdr_val++ = ':'; |
| 5097 | *hdr_val++ = ' '; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 5098 | hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val); |
| 5099 | 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] | 5100 | |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 5101 | if (old_o) { |
| 5102 | /* If this was a forwarded request, we must readjust the amount of |
| 5103 | * data to be forwarded in order to take into account the size |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 5104 | * variations. Note that the current state is >= HTTP_MSG_BODY, |
| 5105 | * so we don't have to adjust ->sol. |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 5106 | */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 5107 | old_o += chn->buf->i - old_i; |
| 5108 | b_adv(chn->buf, old_o); |
| 5109 | txn->req.next -= old_o; |
| 5110 | txn->req.sov -= old_o; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 5111 | } |
| 5112 | |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 5113 | return 0; |
| 5114 | } |
| 5115 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5116 | /* Terminate current transaction and prepare a new one. This is very tricky |
| 5117 | * right now but it works. |
| 5118 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5119 | void http_end_txn_clean_session(struct stream *s) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5120 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5121 | int prev_status = s->txn->status; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 5122 | struct proxy *fe = strm_fe(s); |
Willy Tarreau | 323a2d9 | 2015-08-04 19:00:17 +0200 | [diff] [blame] | 5123 | struct connection *srv_conn; |
| 5124 | struct server *srv; |
Willy Tarreau | 449d74a | 2015-08-05 17:16:33 +0200 | [diff] [blame] | 5125 | unsigned int prev_flags = s->txn->flags; |
Willy Tarreau | 068621e | 2013-12-23 15:11:25 +0100 | [diff] [blame] | 5126 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5127 | /* FIXME: We need a more portable way of releasing a backend's and a |
| 5128 | * server's connections. We need a safer way to reinitialize buffer |
| 5129 | * flags. We also need a more accurate method for computing per-request |
| 5130 | * data. |
| 5131 | */ |
Willy Tarreau | 323a2d9 | 2015-08-04 19:00:17 +0200 | [diff] [blame] | 5132 | srv_conn = objt_conn(s->si[1].end); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5133 | |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5134 | /* unless we're doing keep-alive, we want to quickly close the connection |
| 5135 | * to the server. |
| 5136 | */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5137 | if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) || |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5138 | !si_conn_ready(&s->si[1])) { |
| 5139 | s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF; |
| 5140 | si_shutr(&s->si[1]); |
| 5141 | si_shutw(&s->si[1]); |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5142 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5143 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5144 | if (s->flags & SF_BE_ASSIGNED) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5145 | s->be->beconn--; |
Willy Tarreau | 2d5cd47 | 2012-03-01 23:34:37 +0100 | [diff] [blame] | 5146 | if (unlikely(s->srv_conn)) |
| 5147 | sess_change_server(s, NULL); |
| 5148 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5149 | |
| 5150 | s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5151 | stream_process_counters(s); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5152 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5153 | if (s->txn->status) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5154 | int n; |
| 5155 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5156 | n = s->txn->status / 100; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5157 | if (n < 1 || n > 5) |
| 5158 | n = 0; |
| 5159 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5160 | if (fe->mode == PR_MODE_HTTP) { |
| 5161 | fe->fe_counters.p.http.rsp[n]++; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5162 | if (s->comp_algo && (s->flags & SF_COMP_READY)) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5163 | fe->fe_counters.p.http.comp_rsp++; |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 5164 | } |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5165 | if ((s->flags & SF_BE_ASSIGNED) && |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 5166 | (s->be->mode == PR_MODE_HTTP)) { |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5167 | s->be->be_counters.p.http.rsp[n]++; |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 5168 | s->be->be_counters.p.http.cum_req++; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5169 | if (s->comp_algo && (s->flags & SF_COMP_READY)) |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 5170 | s->be->be_counters.p.http.comp_rsp++; |
| 5171 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5172 | } |
| 5173 | |
| 5174 | /* don't count other requests' data */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5175 | s->logs.bytes_in -= s->req.buf->i; |
| 5176 | s->logs.bytes_out -= s->res.buf->i; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5177 | |
| 5178 | /* let's do a final log if we need it */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5179 | if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait && |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5180 | !(s->flags & SF_MONITOR) && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5181 | (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5182 | s->do_log(s); |
| 5183 | } |
| 5184 | |
Willy Tarreau | d713bcc | 2014-06-25 15:36:04 +0200 | [diff] [blame] | 5185 | /* stop tracking content-based counters */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5186 | stream_stop_content_counters(s); |
| 5187 | stream_update_time_stats(s); |
Willy Tarreau | 4bfc580 | 2014-06-17 12:19:18 +0200 | [diff] [blame] | 5188 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5189 | s->logs.accept_date = date; /* user-visible date for logging */ |
| 5190 | s->logs.tv_accept = now; /* corrected date for internal use */ |
| 5191 | tv_zero(&s->logs.tv_request); |
| 5192 | s->logs.t_queue = -1; |
| 5193 | s->logs.t_connect = -1; |
| 5194 | s->logs.t_data = -1; |
| 5195 | s->logs.t_close = 0; |
| 5196 | s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */ |
| 5197 | s->logs.srv_queue_size = 0; /* we will get this number soon */ |
| 5198 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5199 | s->logs.bytes_in = s->req.total = s->req.buf->i; |
| 5200 | s->logs.bytes_out = s->res.total = s->res.buf->i; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5201 | |
| 5202 | if (s->pend_pos) |
| 5203 | pendconn_free(s->pend_pos); |
| 5204 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5205 | if (objt_server(s->target)) { |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5206 | if (s->flags & SF_CURR_SESS) { |
| 5207 | s->flags &= ~SF_CURR_SESS; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5208 | objt_server(s->target)->cur_sess--; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5209 | } |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5210 | if (may_dequeue_tasks(objt_server(s->target), s->be)) |
| 5211 | process_srv_queue(objt_server(s->target)); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5212 | } |
| 5213 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5214 | s->target = NULL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5215 | |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5216 | /* only release our endpoint if we don't intend to reuse the |
| 5217 | * connection. |
| 5218 | */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5219 | if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) || |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5220 | !si_conn_ready(&s->si[1])) { |
| 5221 | si_release_endpoint(&s->si[1]); |
Willy Tarreau | 323a2d9 | 2015-08-04 19:00:17 +0200 | [diff] [blame] | 5222 | srv_conn = NULL; |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5223 | } |
| 5224 | |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5225 | s->si[1].state = s->si[1].prev_state = SI_ST_INI; |
| 5226 | s->si[1].err_type = SI_ET_NONE; |
| 5227 | s->si[1].conn_retries = 0; /* used for logging too */ |
| 5228 | s->si[1].exp = TICK_ETERNITY; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5229 | 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] | 5230 | 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); |
| 5231 | 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] | 5232 | s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST); |
| 5233 | s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED); |
| 5234 | s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP); |
Willy Tarreau | 543db62 | 2012-11-15 16:41:22 +0100 | [diff] [blame] | 5235 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5236 | s->txn->meth = 0; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5237 | http_reset_txn(s); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5238 | s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ; |
Willy Tarreau | 068621e | 2013-12-23 15:11:25 +0100 | [diff] [blame] | 5239 | |
| 5240 | if (prev_status == 401 || prev_status == 407) { |
| 5241 | /* In HTTP keep-alive mode, if we receive a 401, we still have |
| 5242 | * a chance of being able to send the visitor again to the same |
| 5243 | * server over the same connection. This is required by some |
| 5244 | * broken protocols such as NTLM, and anyway whenever there is |
| 5245 | * an opportunity for sending the challenge to the proper place, |
| 5246 | * it's better to do it (at least it helps with debugging). |
| 5247 | */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5248 | s->txn->flags |= TX_PREFER_LAST; |
Willy Tarreau | bd99d58 | 2015-09-02 10:40:43 +0200 | [diff] [blame] | 5249 | if (srv_conn) |
| 5250 | srv_conn->flags |= CO_FL_PRIVATE; |
Willy Tarreau | 068621e | 2013-12-23 15:11:25 +0100 | [diff] [blame] | 5251 | } |
| 5252 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5253 | if (fe->options2 & PR_O2_INDEPSTR) |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5254 | s->si[1].flags |= SI_FL_INDEP_STR; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5255 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5256 | if (fe->options2 & PR_O2_NODELAY) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5257 | s->req.flags |= CF_NEVER_WAIT; |
| 5258 | s->res.flags |= CF_NEVER_WAIT; |
Willy Tarreau | 96e3121 | 2011-05-30 18:10:30 +0200 | [diff] [blame] | 5259 | } |
| 5260 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5261 | /* if the request buffer is not empty, it means we're |
| 5262 | * about to process another request, so send pending |
| 5263 | * data with MSG_MORE to merge TCP packets when possible. |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 5264 | * Just don't do this if the buffer is close to be full, |
| 5265 | * because the request will wait for it to flush a little |
| 5266 | * bit before proceeding. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5267 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5268 | if (s->req.buf->i) { |
| 5269 | if (s->res.buf->o && |
| 5270 | !buffer_full(s->res.buf, global.tune.maxrewrite) && |
| 5271 | bi_end(s->res.buf) <= s->res.buf->data + s->res.buf->size - global.tune.maxrewrite) |
| 5272 | s->res.flags |= CF_EXPECT_MORE; |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 5273 | } |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 5274 | |
| 5275 | /* we're removing the analysers, we MUST re-enable events detection */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5276 | channel_auto_read(&s->req); |
| 5277 | channel_auto_close(&s->req); |
| 5278 | channel_auto_read(&s->res); |
| 5279 | channel_auto_close(&s->res); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5280 | |
Willy Tarreau | 1c59bd5 | 2015-11-02 20:20:11 +0100 | [diff] [blame] | 5281 | /* we're in keep-alive with an idle connection, monitor it if not already done */ |
| 5282 | if (srv_conn && LIST_ISEMPTY(&srv_conn->list)) { |
Willy Tarreau | 323a2d9 | 2015-08-04 19:00:17 +0200 | [diff] [blame] | 5283 | srv = objt_server(srv_conn->target); |
Willy Tarreau | 8dff998 | 2015-08-04 20:45:52 +0200 | [diff] [blame] | 5284 | if (!srv) |
| 5285 | si_idle_conn(&s->si[1], NULL); |
| 5286 | else if ((srv_conn->flags & CO_FL_PRIVATE) || |
| 5287 | ((s->be->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)) |
| 5288 | si_idle_conn(&s->si[1], &srv->priv_conns); |
Willy Tarreau | 449d74a | 2015-08-05 17:16:33 +0200 | [diff] [blame] | 5289 | else if (prev_flags & TX_NOT_FIRST) |
| 5290 | /* note: we check the request, not the connection, but |
| 5291 | * this is valid for strategies SAFE and AGGR, and in |
| 5292 | * case of ALWS, we don't care anyway. |
| 5293 | */ |
| 5294 | si_idle_conn(&s->si[1], &srv->safe_conns); |
Willy Tarreau | 8dff998 | 2015-08-04 20:45:52 +0200 | [diff] [blame] | 5295 | else |
| 5296 | si_idle_conn(&s->si[1], &srv->idle_conns); |
Willy Tarreau | 4320eaa | 2015-08-05 11:08:30 +0200 | [diff] [blame] | 5297 | } |
Willy Tarreau | 2737562 | 2013-12-17 00:00:28 +0100 | [diff] [blame] | 5298 | |
Willy Tarreau | c29d0cd | 2015-09-23 12:18:14 +0200 | [diff] [blame] | 5299 | s->req.analysers = strm_li(s) ? strm_li(s)->analysers : 0; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5300 | s->res.analysers = 0; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5301 | } |
| 5302 | |
| 5303 | |
| 5304 | /* This function updates the request state machine according to the response |
| 5305 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 5306 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 5307 | * it is only used to find when a request/response couple is complete. Both |
| 5308 | * this function and its equivalent should loop until both return zero. It |
| 5309 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 5310 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5311 | int http_sync_req_state(struct stream *s) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5312 | { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5313 | struct channel *chn = &s->req; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5314 | struct http_txn *txn = s->txn; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5315 | unsigned int old_flags = chn->flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5316 | unsigned int old_state = txn->req.msg_state; |
| 5317 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5318 | if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) |
| 5319 | return 0; |
| 5320 | |
| 5321 | if (txn->req.msg_state == HTTP_MSG_DONE) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 5322 | /* No need to read anymore, the request was completely parsed. |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 5323 | * We can shut the read side unless we want to abort_on_close, |
| 5324 | * or we have a POST request. The issue with POST requests is |
| 5325 | * that some browsers still send a CRLF after the request, and |
| 5326 | * this CRLF must be read so that it does not remain in the kernel |
| 5327 | * buffers, otherwise a close could cause an RST on some systems |
| 5328 | * (eg: Linux). |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 5329 | * Note that if we're using keep-alive on the client side, we'd |
| 5330 | * rather poll now and keep the polling enabled for the whole |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5331 | * stream's life than enabling/disabling it between each |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 5332 | * response and next request. |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 5333 | */ |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 5334 | if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) && |
| 5335 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) && |
| 5336 | !(s->be->options & PR_O_ABRT_CLOSE) && |
| 5337 | txn->meth != HTTP_METH_POST) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5338 | channel_dont_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5339 | |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 5340 | /* if the server closes the connection, we want to immediately react |
| 5341 | * and close the socket to save packets and syscalls. |
| 5342 | */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5343 | s->si[1].flags |= SI_FL_NOHALF; |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 5344 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5345 | if (txn->rsp.msg_state == HTTP_MSG_ERROR) |
| 5346 | goto wait_other_side; |
| 5347 | |
| 5348 | if (txn->rsp.msg_state < HTTP_MSG_DONE) { |
| 5349 | /* The server has not finished to respond, so we |
| 5350 | * don't want to move in order not to upset it. |
| 5351 | */ |
| 5352 | goto wait_other_side; |
| 5353 | } |
| 5354 | |
| 5355 | if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) { |
| 5356 | /* if any side switches to tunnel mode, the other one does too */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5357 | channel_auto_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5358 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 5359 | chn->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5360 | goto wait_other_side; |
| 5361 | } |
| 5362 | |
| 5363 | /* When we get here, it means that both the request and the |
| 5364 | * response have finished receiving. Depending on the connection |
| 5365 | * mode, we'll have to wait for the last bytes to leave in either |
| 5366 | * direction, and sometimes for a close to be effective. |
| 5367 | */ |
| 5368 | |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5369 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 5370 | /* Server-close mode : queue a connection close to the server */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5371 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) |
| 5372 | channel_shutw_now(chn); |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5373 | } |
| 5374 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 5375 | /* Option forceclose is set, or either side wants to close, |
| 5376 | * let's enforce it now that we're not expecting any new |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5377 | * data to come. The caller knows the stream is complete |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5378 | * once both states are CLOSED. |
| 5379 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5380 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) { |
| 5381 | channel_shutr_now(chn); |
| 5382 | channel_shutw_now(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5383 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5384 | } |
| 5385 | else { |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5386 | /* The last possible modes are keep-alive and tunnel. Tunnel mode |
| 5387 | * will not have any analyser so it needs to poll for reads. |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5388 | */ |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5389 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) { |
| 5390 | channel_auto_read(chn); |
| 5391 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
| 5392 | chn->flags |= CF_NEVER_WAIT; |
| 5393 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5394 | } |
| 5395 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5396 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5397 | /* if we've just closed an output, let's switch */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5398 | s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */ |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5399 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5400 | if (!channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5401 | txn->req.msg_state = HTTP_MSG_CLOSING; |
| 5402 | goto http_msg_closing; |
| 5403 | } |
| 5404 | else { |
| 5405 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 5406 | goto http_msg_closed; |
| 5407 | } |
| 5408 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5409 | goto wait_other_side; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5410 | } |
| 5411 | |
| 5412 | if (txn->req.msg_state == HTTP_MSG_CLOSING) { |
| 5413 | http_msg_closing: |
| 5414 | /* nothing else to forward, just waiting for the output buffer |
| 5415 | * to be empty and for the shutw_now to take effect. |
| 5416 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5417 | if (channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5418 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 5419 | goto http_msg_closed; |
| 5420 | } |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5421 | else if (chn->flags & CF_SHUTW) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5422 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 5423 | goto wait_other_side; |
| 5424 | } |
| 5425 | } |
| 5426 | |
| 5427 | if (txn->req.msg_state == HTTP_MSG_CLOSED) { |
| 5428 | http_msg_closed: |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 5429 | /* see above in MSG_DONE why we only do this in these states */ |
| 5430 | if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) && |
| 5431 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) && |
| 5432 | !(s->be->options & PR_O_ABRT_CLOSE)) |
Willy Tarreau | 2e7a165 | 2013-12-15 15:32:10 +0100 | [diff] [blame] | 5433 | channel_dont_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5434 | goto wait_other_side; |
| 5435 | } |
| 5436 | |
| 5437 | wait_other_side: |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5438 | return txn->req.msg_state != old_state || chn->flags != old_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5439 | } |
| 5440 | |
| 5441 | |
| 5442 | /* This function updates the response state machine according to the request |
| 5443 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 5444 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 5445 | * it is only used to find when a request/response couple is complete. Both |
| 5446 | * this function and its equivalent should loop until both return zero. It |
| 5447 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 5448 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5449 | int http_sync_res_state(struct stream *s) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5450 | { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5451 | struct channel *chn = &s->res; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5452 | struct http_txn *txn = s->txn; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5453 | unsigned int old_flags = chn->flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5454 | unsigned int old_state = txn->rsp.msg_state; |
| 5455 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5456 | if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY)) |
| 5457 | return 0; |
| 5458 | |
| 5459 | if (txn->rsp.msg_state == HTTP_MSG_DONE) { |
| 5460 | /* In theory, we don't need to read anymore, but we must |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 5461 | * still monitor the server connection for a possible close |
| 5462 | * while the request is being uploaded, so we don't disable |
| 5463 | * reading. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5464 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5465 | /* channel_dont_read(chn); */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5466 | |
| 5467 | if (txn->req.msg_state == HTTP_MSG_ERROR) |
| 5468 | goto wait_other_side; |
| 5469 | |
| 5470 | if (txn->req.msg_state < HTTP_MSG_DONE) { |
| 5471 | /* The client seems to still be sending data, probably |
| 5472 | * because we got an error response during an upload. |
| 5473 | * We have the choice of either breaking the connection |
| 5474 | * or letting it pass through. Let's do the later. |
| 5475 | */ |
| 5476 | goto wait_other_side; |
| 5477 | } |
| 5478 | |
| 5479 | if (txn->req.msg_state == HTTP_MSG_TUNNEL) { |
| 5480 | /* if any side switches to tunnel mode, the other one does too */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5481 | channel_auto_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5482 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 5483 | chn->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5484 | goto wait_other_side; |
| 5485 | } |
| 5486 | |
| 5487 | /* When we get here, it means that both the request and the |
| 5488 | * response have finished receiving. Depending on the connection |
| 5489 | * mode, we'll have to wait for the last bytes to leave in either |
| 5490 | * direction, and sometimes for a close to be effective. |
| 5491 | */ |
| 5492 | |
| 5493 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 5494 | /* Server-close mode : shut read and wait for the request |
| 5495 | * side to close its output buffer. The caller will detect |
| 5496 | * when we're in DONE and the other is in CLOSED and will |
| 5497 | * catch that for the final cleanup. |
| 5498 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5499 | if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW))) |
| 5500 | channel_shutr_now(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5501 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5502 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 5503 | /* Option forceclose is set, or either side wants to close, |
| 5504 | * let's enforce it now that we're not expecting any new |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5505 | * data to come. The caller knows the stream is complete |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5506 | * once both states are CLOSED. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5507 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5508 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) { |
| 5509 | channel_shutr_now(chn); |
| 5510 | channel_shutw_now(chn); |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 5511 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5512 | } |
| 5513 | else { |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5514 | /* The last possible modes are keep-alive and tunnel. Tunnel will |
| 5515 | * need to forward remaining data. Keep-alive will need to monitor |
| 5516 | * for connection closing. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5517 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5518 | channel_auto_read(chn); |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 5519 | chn->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5520 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) |
| 5521 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5522 | } |
| 5523 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5524 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5525 | /* if we've just closed an output, let's switch */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5526 | if (!channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5527 | txn->rsp.msg_state = HTTP_MSG_CLOSING; |
| 5528 | goto http_msg_closing; |
| 5529 | } |
| 5530 | else { |
| 5531 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 5532 | goto http_msg_closed; |
| 5533 | } |
| 5534 | } |
| 5535 | goto wait_other_side; |
| 5536 | } |
| 5537 | |
| 5538 | if (txn->rsp.msg_state == HTTP_MSG_CLOSING) { |
| 5539 | http_msg_closing: |
| 5540 | /* nothing else to forward, just waiting for the output buffer |
| 5541 | * to be empty and for the shutw_now to take effect. |
| 5542 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5543 | if (channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5544 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 5545 | goto http_msg_closed; |
| 5546 | } |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5547 | else if (chn->flags & CF_SHUTW) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5548 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5549 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5550 | if (objt_server(s->target)) |
| 5551 | objt_server(s->target)->counters.cli_aborts++; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5552 | goto wait_other_side; |
| 5553 | } |
| 5554 | } |
| 5555 | |
| 5556 | if (txn->rsp.msg_state == HTTP_MSG_CLOSED) { |
| 5557 | http_msg_closed: |
| 5558 | /* drop any pending data */ |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5559 | channel_truncate(chn); |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5560 | channel_auto_close(chn); |
| 5561 | channel_auto_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5562 | goto wait_other_side; |
| 5563 | } |
| 5564 | |
| 5565 | wait_other_side: |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 5566 | /* We force the response to leave immediately if we're waiting for the |
| 5567 | * other side, since there is no pending shutdown to push it out. |
| 5568 | */ |
| 5569 | if (!channel_is_empty(chn)) |
| 5570 | chn->flags |= CF_SEND_DONTWAIT; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 5571 | return txn->rsp.msg_state != old_state || chn->flags != old_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5572 | } |
| 5573 | |
| 5574 | |
| 5575 | /* Resync the request and response state machines. Return 1 if either state |
| 5576 | * changes. |
| 5577 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5578 | int http_resync_states(struct stream *s) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5579 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5580 | struct http_txn *txn = s->txn; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5581 | int old_req_state = txn->req.msg_state; |
| 5582 | int old_res_state = txn->rsp.msg_state; |
| 5583 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5584 | http_sync_req_state(s); |
| 5585 | while (1) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5586 | if (!http_sync_res_state(s)) |
| 5587 | break; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5588 | if (!http_sync_req_state(s)) |
| 5589 | break; |
| 5590 | } |
Willy Tarreau | 3ce10ff | 2014-04-22 08:24:38 +0200 | [diff] [blame] | 5591 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5592 | /* OK, both state machines agree on a compatible state. |
| 5593 | * There are a few cases we're interested in : |
| 5594 | * - HTTP_MSG_TUNNEL on either means we have to disable both analysers |
| 5595 | * - HTTP_MSG_CLOSED on both sides means we've reached the end in both |
| 5596 | * directions, so let's simply disable both analysers. |
| 5597 | * - HTTP_MSG_CLOSED on the response only means we must abort the |
| 5598 | * request. |
| 5599 | * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response |
| 5600 | * with server-close mode means we've completed one request and we |
| 5601 | * must re-initialize the server connection. |
| 5602 | */ |
| 5603 | |
| 5604 | if (txn->req.msg_state == HTTP_MSG_TUNNEL || |
| 5605 | txn->rsp.msg_state == HTTP_MSG_TUNNEL || |
| 5606 | (txn->req.msg_state == HTTP_MSG_CLOSED && |
| 5607 | txn->rsp.msg_state == HTTP_MSG_CLOSED)) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5608 | s->req.analysers = 0; |
| 5609 | channel_auto_close(&s->req); |
| 5610 | channel_auto_read(&s->req); |
| 5611 | s->res.analysers = 0; |
| 5612 | channel_auto_close(&s->res); |
| 5613 | channel_auto_read(&s->res); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5614 | } |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 5615 | else if ((txn->req.msg_state >= HTTP_MSG_DONE && |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5616 | (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->res.flags & CF_SHUTW))) || |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 5617 | txn->rsp.msg_state == HTTP_MSG_ERROR || |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 5618 | txn->req.msg_state == HTTP_MSG_ERROR) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5619 | s->res.analysers = 0; |
| 5620 | channel_auto_close(&s->res); |
| 5621 | channel_auto_read(&s->res); |
| 5622 | s->req.analysers = 0; |
| 5623 | channel_abort(&s->req); |
| 5624 | channel_auto_close(&s->req); |
| 5625 | channel_auto_read(&s->req); |
| 5626 | channel_truncate(&s->req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5627 | } |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5628 | else if ((txn->req.msg_state == HTTP_MSG_DONE || |
| 5629 | txn->req.msg_state == HTTP_MSG_CLOSED) && |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5630 | txn->rsp.msg_state == HTTP_MSG_DONE && |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 5631 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL || |
| 5632 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) { |
| 5633 | /* server-close/keep-alive: terminate this transaction, |
| 5634 | * possibly killing the server connection and reinitialize |
| 5635 | * a fresh-new transaction. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5636 | */ |
| 5637 | http_end_txn_clean_session(s); |
| 5638 | } |
| 5639 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5640 | return txn->req.msg_state != old_req_state || |
| 5641 | txn->rsp.msg_state != old_res_state; |
| 5642 | } |
| 5643 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5644 | /* This function is an analyser which forwards request body (including chunk |
| 5645 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 5646 | * zero byte. The only situation where it must not be called is when we're in |
| 5647 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 5648 | * remaining data and to resync after end of body. It expects the msg_state to |
| 5649 | * 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] | 5650 | * 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] | 5651 | * 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] | 5652 | * bytes of pending data + the headers if not already done. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5653 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5654 | 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] | 5655 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 5656 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5657 | struct http_txn *txn = s->txn; |
| 5658 | struct http_msg *msg = &s->txn->req; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5659 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5660 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 5661 | return 0; |
| 5662 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5663 | 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] | 5664 | ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) { |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 5665 | /* Output closed while we were sending data. We must abort and |
| 5666 | * wake the other side up. |
| 5667 | */ |
| 5668 | msg->msg_state = HTTP_MSG_ERROR; |
| 5669 | http_resync_states(s); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 5670 | return 1; |
| 5671 | } |
| 5672 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5673 | /* Note that we don't have to send 100-continue back because we don't |
| 5674 | * need the data to complete our job, and it's up to the server to |
| 5675 | * decide whether to return 100, 417 or anything else in return of |
| 5676 | * an "Expect: 100-continue" header. |
| 5677 | */ |
| 5678 | |
Willy Tarreau | bb2e669 | 2014-07-10 19:06:10 +0200 | [diff] [blame] | 5679 | if (msg->sov > 0) { |
Willy Tarreau | b59c7bf | 2014-04-22 14:29:58 +0200 | [diff] [blame] | 5680 | /* we have msg->sov which points to the first byte of message |
| 5681 | * body, and req->buf.p still points to the beginning of the |
| 5682 | * message. We forward the headers now, as we don't need them |
| 5683 | * anymore, and we want to flush them. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5684 | */ |
Willy Tarreau | b59c7bf | 2014-04-22 14:29:58 +0200 | [diff] [blame] | 5685 | b_adv(req->buf, msg->sov); |
| 5686 | msg->next -= msg->sov; |
| 5687 | msg->sov = 0; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 5688 | |
Willy Tarreau | b59c7bf | 2014-04-22 14:29:58 +0200 | [diff] [blame] | 5689 | /* The previous analysers guarantee that the state is somewhere |
| 5690 | * between MSG_BODY and the first MSG_DATA. So msg->sol and |
| 5691 | * msg->next are always correct. |
| 5692 | */ |
| 5693 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
| 5694 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
| 5695 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 5696 | else |
| 5697 | msg->msg_state = HTTP_MSG_DATA; |
| 5698 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5699 | } |
| 5700 | |
Willy Tarreau | 7ba2354 | 2014-04-17 21:50:00 +0200 | [diff] [blame] | 5701 | /* Some post-connect processing might want us to refrain from starting to |
| 5702 | * forward data. Currently, the only reason for this is "balance url_param" |
| 5703 | * whichs need to parse/process the request after we've enabled forwarding. |
| 5704 | */ |
| 5705 | if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5706 | if (!(s->res.flags & CF_READ_ATTACHED)) { |
Willy Tarreau | 7ba2354 | 2014-04-17 21:50:00 +0200 | [diff] [blame] | 5707 | channel_auto_connect(req); |
Willy Tarreau | 644c101 | 2014-04-30 18:11:11 +0200 | [diff] [blame] | 5708 | req->flags |= CF_WAKE_CONNECT; |
Willy Tarreau | 7ba2354 | 2014-04-17 21:50:00 +0200 | [diff] [blame] | 5709 | goto missing_data; |
| 5710 | } |
| 5711 | msg->flags &= ~HTTP_MSGF_WAIT_CONN; |
| 5712 | } |
| 5713 | |
Willy Tarreau | 80a92c0 | 2014-03-12 10:41:13 +0100 | [diff] [blame] | 5714 | /* in most states, we should abort in case of early close */ |
| 5715 | channel_auto_close(req); |
| 5716 | |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 5717 | if (req->to_forward) { |
| 5718 | /* We can't process the buffer's contents yet */ |
| 5719 | req->flags |= CF_WAKE_WRITE; |
| 5720 | goto missing_data; |
| 5721 | } |
| 5722 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5723 | while (1) { |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5724 | if (msg->msg_state == HTTP_MSG_DATA) { |
| 5725 | /* must still forward */ |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5726 | /* we may have some pending data starting at req->buf->p */ |
| 5727 | if (msg->chunk_len > req->buf->i - msg->next) { |
Willy Tarreau | 4afd70a | 2014-01-25 02:26:39 +0100 | [diff] [blame] | 5728 | req->flags |= CF_WAKE_WRITE; |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5729 | goto missing_data; |
Willy Tarreau | 4afd70a | 2014-01-25 02:26:39 +0100 | [diff] [blame] | 5730 | } |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5731 | msg->next += msg->chunk_len; |
| 5732 | msg->chunk_len = 0; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5733 | |
| 5734 | /* nothing left to forward */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5735 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 5736 | msg->msg_state = HTTP_MSG_CHUNK_CRLF; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5737 | else |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5738 | msg->msg_state = HTTP_MSG_DONE; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5739 | } |
| 5740 | else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 5741 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 5742 | * set ->next to point to the body and switch to DATA or |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5743 | * TRAILERS state. |
| 5744 | */ |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 5745 | int ret = http_parse_chunk_size(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5746 | |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 5747 | if (ret == 0) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5748 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5749 | else if (ret < 0) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5750 | stream_inc_http_err_ctr(s); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5751 | if (msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5752 | http_capture_bad_message(&sess->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, s->be); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5753 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5754 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5755 | /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5756 | } |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 5757 | else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5758 | /* we want the CRLF after the data */ |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 5759 | int ret = http_skip_chunk_crlf(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5760 | |
| 5761 | if (ret == 0) |
| 5762 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5763 | else if (ret < 0) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5764 | stream_inc_http_err_ctr(s); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5765 | if (msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5766 | http_capture_bad_message(&sess->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_CRLF, s->be); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5767 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5768 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5769 | /* we're in MSG_CHUNK_SIZE now */ |
| 5770 | } |
| 5771 | else if (msg->msg_state == HTTP_MSG_TRAILERS) { |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 5772 | int ret = http_forward_trailers(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5773 | |
| 5774 | if (ret == 0) |
| 5775 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5776 | else if (ret < 0) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5777 | stream_inc_http_err_ctr(s); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5778 | if (msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5779 | http_capture_bad_message(&sess->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5780 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5781 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5782 | /* we're in HTTP_MSG_DONE now */ |
| 5783 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5784 | else { |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5785 | int old_state = msg->msg_state; |
| 5786 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5787 | /* other states, DONE...TUNNEL */ |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5788 | |
| 5789 | /* we may have some pending data starting at req->buf->p |
| 5790 | * such as last chunk of data or trailers. |
| 5791 | */ |
| 5792 | b_adv(req->buf, msg->next); |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5793 | if (unlikely(!(s->req.flags & CF_WROTE_DATA))) |
Willy Tarreau | bb2e669 | 2014-07-10 19:06:10 +0200 | [diff] [blame] | 5794 | msg->sov -= msg->next; |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5795 | msg->next = 0; |
| 5796 | |
Willy Tarreau | bbfb6c4 | 2015-05-11 18:30:33 +0200 | [diff] [blame] | 5797 | /* we don't want to forward closes on DONE except in |
| 5798 | * tunnel mode. |
| 5799 | */ |
| 5800 | if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5801 | channel_dont_close(req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5802 | if (http_resync_states(s)) { |
| 5803 | /* some state changes occurred, maybe the analyser |
| 5804 | * was disabled too. |
Willy Tarreau | face839 | 2010-01-03 11:37:54 +0100 | [diff] [blame] | 5805 | */ |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5806 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5807 | if (req->flags & CF_SHUTW) { |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5808 | /* request errors are most likely due to |
| 5809 | * the server aborting the transfer. |
| 5810 | */ |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5811 | goto aborted_xfer; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5812 | } |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5813 | if (msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5814 | http_capture_bad_message(&sess->fe->invalid_req, s, msg, old_state, s->be); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5815 | goto return_bad_req; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5816 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5817 | return 1; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 5818 | } |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 5819 | |
| 5820 | /* If "option abortonclose" is set on the backend, we |
| 5821 | * want to monitor the client's connection and forward |
| 5822 | * any shutdown notification to the server, which will |
| 5823 | * decide whether to close or to go on processing the |
Willy Tarreau | bbfb6c4 | 2015-05-11 18:30:33 +0200 | [diff] [blame] | 5824 | * request. We only do that in tunnel mode, and not in |
| 5825 | * other modes since it can be abused to exhaust source |
| 5826 | * ports. |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 5827 | */ |
| 5828 | if (s->be->options & PR_O_ABRT_CLOSE) { |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5829 | channel_auto_read(req); |
Willy Tarreau | bbfb6c4 | 2015-05-11 18:30:33 +0200 | [diff] [blame] | 5830 | if ((req->flags & (CF_SHUTR|CF_READ_NULL)) && |
| 5831 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)) |
| 5832 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5833 | channel_auto_close(req); |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 5834 | } |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5835 | else if (s->txn->meth == HTTP_METH_POST) { |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 5836 | /* POST requests may require to read extra CRLF |
| 5837 | * sent by broken browsers and which could cause |
| 5838 | * an RST to be sent upon close on some systems |
| 5839 | * (eg: Linux). |
| 5840 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5841 | channel_auto_read(req); |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 5842 | } |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 5843 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5844 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5845 | } |
| 5846 | } |
| 5847 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5848 | missing_data: |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5849 | /* we may have some pending data starting at req->buf->p */ |
| 5850 | b_adv(req->buf, msg->next); |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5851 | if (unlikely(!(s->req.flags & CF_WROTE_DATA))) |
Willy Tarreau | bb2e669 | 2014-07-10 19:06:10 +0200 | [diff] [blame] | 5852 | msg->sov -= msg->next + MIN(msg->chunk_len, req->buf->i); |
| 5853 | |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5854 | msg->next = 0; |
| 5855 | msg->chunk_len -= channel_forward(req, msg->chunk_len); |
| 5856 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5857 | /* stop waiting for data if the input is closed before the end */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5858 | if (req->flags & CF_SHUTR) { |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5859 | if (!(s->flags & SF_ERR_MASK)) |
| 5860 | s->flags |= SF_ERR_CLICL; |
| 5861 | if (!(s->flags & SF_FINST_MASK)) { |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5862 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5863 | s->flags |= SF_FINST_H; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5864 | else |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5865 | s->flags |= SF_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5866 | } |
| 5867 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5868 | sess->fe->fe_counters.cli_aborts++; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5869 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5870 | if (objt_server(s->target)) |
| 5871 | objt_server(s->target)->counters.cli_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5872 | |
| 5873 | goto return_bad_req_stats_ok; |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 5874 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5875 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5876 | /* waiting for the last bits to leave the buffer */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5877 | if (req->flags & CF_SHUTW) |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5878 | goto aborted_xfer; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5879 | |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5880 | /* 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] | 5881 | * 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] | 5882 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5883 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5884 | channel_dont_close(req); |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5885 | |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5886 | /* 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] | 5887 | * 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] | 5888 | * 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] | 5889 | * modes are already handled by the stream sock layer. We must not do |
| 5890 | * this in content-length mode because it could present the MSG_MORE |
| 5891 | * flag with the last block of forwarded data, which would cause an |
| 5892 | * additional delay to be observed by the receiver. |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5893 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5894 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5895 | req->flags |= CF_EXPECT_MORE; |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5896 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5897 | return 0; |
| 5898 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5899 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5900 | sess->fe->fe_counters.failed_req++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 5901 | if (sess->listener->counters) |
| 5902 | sess->listener->counters->failed_req++; |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5903 | |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5904 | return_bad_req_stats_ok: |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 5905 | /* we may have some pending data starting at req->buf->p */ |
| 5906 | b_adv(req->buf, msg->next); |
| 5907 | msg->next = 0; |
| 5908 | |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5909 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 5910 | if (txn->status) { |
| 5911 | /* Note: we don't send any error if some data were already sent */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5912 | stream_int_retnclose(&s->si[0], NULL); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5913 | } else { |
| 5914 | txn->status = 400; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5915 | stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400)); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5916 | } |
| 5917 | req->analysers = 0; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5918 | s->res.analysers = 0; /* we're in data phase, we want to abort both directions */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5919 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5920 | if (!(s->flags & SF_ERR_MASK)) |
| 5921 | s->flags |= SF_ERR_PRXCOND; |
| 5922 | if (!(s->flags & SF_FINST_MASK)) { |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5923 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5924 | s->flags |= SF_FINST_H; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5925 | else |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5926 | s->flags |= SF_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5927 | } |
| 5928 | return 0; |
| 5929 | |
| 5930 | aborted_xfer: |
| 5931 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 5932 | if (txn->status) { |
| 5933 | /* Note: we don't send any error if some data were already sent */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5934 | stream_int_retnclose(&s->si[0], NULL); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5935 | } else { |
| 5936 | txn->status = 502; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5937 | stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502)); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5938 | } |
| 5939 | req->analysers = 0; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5940 | s->res.analysers = 0; /* we're in data phase, we want to abort both directions */ |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5941 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5942 | sess->fe->fe_counters.srv_aborts++; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5943 | s->be->be_counters.srv_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5944 | if (objt_server(s->target)) |
| 5945 | objt_server(s->target)->counters.srv_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5946 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5947 | if (!(s->flags & SF_ERR_MASK)) |
| 5948 | s->flags |= SF_ERR_SRVCL; |
| 5949 | if (!(s->flags & SF_FINST_MASK)) { |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5950 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5951 | s->flags |= SF_FINST_H; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5952 | else |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5953 | s->flags |= SF_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5954 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5955 | return 0; |
| 5956 | } |
| 5957 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5958 | /* This stream analyser waits for a complete HTTP response. It returns 1 if the |
| 5959 | * processing can continue on next analysers, or zero if it either needs more |
| 5960 | * data or wants to immediately abort the response (eg: timeout, error, ...). It |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5961 | * 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] | 5962 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 5963 | * abort. |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 5964 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5965 | 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] | 5966 | { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5967 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5968 | struct http_txn *txn = s->txn; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5969 | struct http_msg *msg = &txn->rsp; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5970 | struct hdr_ctx ctx; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5971 | int use_close_only; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5972 | int cur_idx; |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 5973 | int n; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 5974 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5975 | 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] | 5976 | now_ms, __FUNCTION__, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5977 | s, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 5978 | rep, |
| 5979 | rep->rex, rep->wex, |
| 5980 | rep->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5981 | rep->buf->i, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 5982 | rep->analysers); |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 5983 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5984 | /* |
| 5985 | * Now parse the partial (or complete) lines. |
| 5986 | * We will check the response syntax, and also join multi-line |
| 5987 | * headers. An index of all the lines will be elaborated while |
| 5988 | * parsing. |
| 5989 | * |
| 5990 | * For the parsing, we use a 28 states FSM. |
| 5991 | * |
| 5992 | * Here is the information we currently have : |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5993 | * rep->buf->p = beginning of response |
| 5994 | * rep->buf->p + msg->eoh = end of processed headers / start of current one |
| 5995 | * rep->buf->p + rep->buf->i = end of input data |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 5996 | * msg->eol = end of current header or line (LF or CRLF) |
| 5997 | * msg->next = first non-visited byte |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5998 | */ |
| 5999 | |
Willy Tarreau | 628c40c | 2014-04-24 19:11:26 +0200 | [diff] [blame] | 6000 | next_one: |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 6001 | /* There's a protected area at the end of the buffer for rewriting |
| 6002 | * purposes. We don't want to start to parse the request if the |
| 6003 | * protected area is affected, because we may have to move processed |
| 6004 | * data later, which is much more complicated. |
| 6005 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6006 | if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) { |
Willy Tarreau | ba0902e | 2015-01-13 14:39:16 +0100 | [diff] [blame] | 6007 | if (unlikely(!channel_is_rewritable(rep))) { |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 6008 | /* some data has still not left the buffer, wake us once that's done */ |
| 6009 | if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) |
| 6010 | goto abort_response; |
| 6011 | channel_dont_close(rep); |
| 6012 | rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | d7ad9f5 | 2013-12-31 17:26:25 +0100 | [diff] [blame] | 6013 | rep->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 6014 | return 0; |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 6015 | } |
| 6016 | |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 6017 | if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) || |
| 6018 | bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite)) |
| 6019 | buffer_slow_realign(rep->buf); |
| 6020 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6021 | if (likely(msg->next < rep->buf->i)) |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 6022 | http_msg_analyzer(msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 6023 | } |
| 6024 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6025 | /* 1: we might have to print this header in debug mode */ |
| 6026 | if (unlikely((global.mode & MODE_DEBUG) && |
| 6027 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | 7d59e90 | 2014-10-21 19:36:09 +0200 | [diff] [blame] | 6028 | msg->msg_state >= HTTP_MSG_BODY)) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6029 | char *eol, *sol; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6030 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6031 | sol = rep->buf->p; |
| 6032 | 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] | 6033 | debug_hdr("srvrep", s, sol, eol); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6034 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6035 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 6036 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6037 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6038 | while (cur_idx) { |
| 6039 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 6040 | debug_hdr("srvhdr", s, sol, eol); |
| 6041 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 6042 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
| 6043 | } |
| 6044 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6045 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6046 | /* |
| 6047 | * Now we quickly check if we have found a full valid response. |
| 6048 | * If not so, we check the FD and buffer states before leaving. |
| 6049 | * A full response is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6050 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6051 | * responses are checked first. |
| 6052 | * |
| 6053 | * Depending on whether the client is still there or not, we |
| 6054 | * may send an error response back or not. Note that normally |
| 6055 | * we should only check for HTTP status there, and check I/O |
| 6056 | * errors somewhere else. |
| 6057 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6058 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6059 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6060 | /* Invalid response */ |
| 6061 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 6062 | /* we detected a parsing error. We want to archive this response |
| 6063 | * in the dedicated proxy area for later troubleshooting. |
| 6064 | */ |
| 6065 | hdr_response_bad: |
| 6066 | if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6067 | http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6068 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 6069 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6070 | if (objt_server(s->target)) { |
| 6071 | objt_server(s->target)->counters.failed_resp++; |
| 6072 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 6073 | } |
Willy Tarreau | 6464841 | 2010-03-05 10:41:54 +0100 | [diff] [blame] | 6074 | abort_response: |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6075 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6076 | rep->analysers = 0; |
| 6077 | txn->status = 502; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 6078 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 6079 | channel_truncate(rep); |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 6080 | stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502)); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6081 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6082 | if (!(s->flags & SF_ERR_MASK)) |
| 6083 | s->flags |= SF_ERR_PRXCOND; |
| 6084 | if (!(s->flags & SF_FINST_MASK)) |
| 6085 | s->flags |= SF_FINST_H; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6086 | |
| 6087 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6088 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6089 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6090 | /* too large response does not fit in buffer. */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6091 | else if (buffer_full(rep->buf, global.tune.maxrewrite)) { |
Willy Tarreau | fec4d89 | 2011-09-02 20:04:57 +0200 | [diff] [blame] | 6092 | if (msg->err_pos < 0) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6093 | msg->err_pos = rep->buf->i; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6094 | goto hdr_response_bad; |
| 6095 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6096 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6097 | /* read error */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 6098 | else if (rep->flags & CF_READ_ERROR) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6099 | if (msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6100 | http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe); |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 6101 | else if (txn->flags & TX_NOT_FIRST) |
| 6102 | goto abort_keep_alive; |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 6103 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 6104 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6105 | if (objt_server(s->target)) { |
| 6106 | objt_server(s->target)->counters.failed_resp++; |
| 6107 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 6108 | } |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 6109 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6110 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6111 | rep->analysers = 0; |
| 6112 | txn->status = 502; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 6113 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 6114 | channel_truncate(rep); |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 6115 | stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502)); |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 6116 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6117 | if (!(s->flags & SF_ERR_MASK)) |
| 6118 | s->flags |= SF_ERR_SRVCL; |
| 6119 | if (!(s->flags & SF_FINST_MASK)) |
| 6120 | s->flags |= SF_FINST_H; |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 6121 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6122 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6123 | |
Willy Tarreau | 6f0a7ba | 2014-06-23 15:22:31 +0200 | [diff] [blame] | 6124 | /* read timeout : return a 504 to the client. */ |
| 6125 | else if (rep->flags & CF_READ_TIMEOUT) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6126 | if (msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6127 | http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe); |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 6128 | else if (txn->flags & TX_NOT_FIRST) |
| 6129 | goto abort_keep_alive; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 6130 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 6131 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6132 | if (objt_server(s->target)) { |
| 6133 | objt_server(s->target)->counters.failed_resp++; |
| 6134 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 6135 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 6136 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6137 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6138 | rep->analysers = 0; |
| 6139 | txn->status = 504; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 6140 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 6141 | channel_truncate(rep); |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 6142 | stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_504)); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 6143 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6144 | if (!(s->flags & SF_ERR_MASK)) |
| 6145 | s->flags |= SF_ERR_SRVTO; |
| 6146 | if (!(s->flags & SF_FINST_MASK)) |
| 6147 | s->flags |= SF_FINST_H; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6148 | return 0; |
| 6149 | } |
Willy Tarreau | a7c5276 | 2008-08-16 18:40:18 +0200 | [diff] [blame] | 6150 | |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6151 | /* client abort with an abortonclose */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6152 | 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] | 6153 | sess->fe->fe_counters.cli_aborts++; |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6154 | s->be->be_counters.cli_aborts++; |
| 6155 | if (objt_server(s->target)) |
| 6156 | objt_server(s->target)->counters.cli_aborts++; |
| 6157 | |
| 6158 | rep->analysers = 0; |
| 6159 | channel_auto_close(rep); |
| 6160 | |
| 6161 | txn->status = 400; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 6162 | channel_truncate(rep); |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 6163 | stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400)); |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6164 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6165 | if (!(s->flags & SF_ERR_MASK)) |
| 6166 | s->flags |= SF_ERR_CLICL; |
| 6167 | if (!(s->flags & SF_FINST_MASK)) |
| 6168 | s->flags |= SF_FINST_H; |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6169 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6170 | /* process_stream() will take care of the error */ |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 6171 | return 0; |
| 6172 | } |
| 6173 | |
Willy Tarreau | 3b8c08a | 2011-09-02 20:16:24 +0200 | [diff] [blame] | 6174 | /* 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] | 6175 | else if (rep->flags & CF_SHUTR) { |
Willy Tarreau | 3b8c08a | 2011-09-02 20:16:24 +0200 | [diff] [blame] | 6176 | if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6177 | http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe); |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 6178 | else if (txn->flags & TX_NOT_FIRST) |
| 6179 | goto abort_keep_alive; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 6180 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 6181 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6182 | if (objt_server(s->target)) { |
| 6183 | objt_server(s->target)->counters.failed_resp++; |
| 6184 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 6185 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 6186 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6187 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6188 | rep->analysers = 0; |
| 6189 | txn->status = 502; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 6190 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 6191 | channel_truncate(rep); |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 6192 | stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502)); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 6193 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6194 | if (!(s->flags & SF_ERR_MASK)) |
| 6195 | s->flags |= SF_ERR_SRVCL; |
| 6196 | if (!(s->flags & SF_FINST_MASK)) |
| 6197 | s->flags |= SF_FINST_H; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6198 | return 0; |
| 6199 | } |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 6200 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6201 | /* write error to client (we don't send any message then) */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 6202 | else if (rep->flags & CF_WRITE_ERROR) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6203 | if (msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6204 | http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe); |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 6205 | else if (txn->flags & TX_NOT_FIRST) |
| 6206 | goto abort_keep_alive; |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 6207 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 6208 | s->be->be_counters.failed_resp++; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6209 | rep->analysers = 0; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6210 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6211 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6212 | if (!(s->flags & SF_ERR_MASK)) |
| 6213 | s->flags |= SF_ERR_CLICL; |
| 6214 | if (!(s->flags & SF_FINST_MASK)) |
| 6215 | s->flags |= SF_FINST_H; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6216 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6217 | /* process_stream() will take care of the error */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6218 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6219 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 6220 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6221 | channel_dont_close(rep); |
Willy Tarreau | 3f3997e | 2013-12-15 15:21:32 +0100 | [diff] [blame] | 6222 | rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6223 | return 0; |
| 6224 | } |
| 6225 | |
| 6226 | /* More interesting part now : we know that we have a complete |
| 6227 | * response which at least looks like HTTP. We have an indicator |
| 6228 | * of each header's length, so we can parse them quickly. |
| 6229 | */ |
| 6230 | |
| 6231 | if (unlikely(msg->err_pos >= 0)) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6232 | http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6233 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6234 | /* |
| 6235 | * 1: get the status code |
| 6236 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6237 | n = rep->buf->p[msg->sl.st.c] - '0'; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6238 | if (n < 1 || n > 5) |
| 6239 | n = 0; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 6240 | /* when the client triggers a 4xx from the server, it's most often due |
| 6241 | * to a missing object or permission. These events should be tracked |
| 6242 | * because if they happen often, it may indicate a brute force or a |
| 6243 | * vulnerability scan. |
| 6244 | */ |
| 6245 | if (n == 4) |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6246 | stream_inc_http_err_ctr(s); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 6247 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6248 | if (objt_server(s->target)) |
| 6249 | objt_server(s->target)->counters.p.http.rsp[n]++; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6250 | |
Willy Tarreau | 91852eb | 2015-05-01 13:26:00 +0200 | [diff] [blame] | 6251 | /* RFC7230#2.6 has enforced the format of the HTTP version string to be |
| 6252 | * exactly one digit "." one digit. This check may be disabled using |
| 6253 | * option accept-invalid-http-response. |
| 6254 | */ |
| 6255 | if (!(s->be->options2 & PR_O2_RSPBUG_OK)) { |
| 6256 | if (msg->sl.st.v_l != 8) { |
| 6257 | msg->err_pos = 0; |
| 6258 | goto hdr_response_bad; |
| 6259 | } |
| 6260 | |
| 6261 | if (rep->buf->p[4] != '/' || |
| 6262 | !isdigit((unsigned char)rep->buf->p[5]) || |
| 6263 | rep->buf->p[6] != '.' || |
| 6264 | !isdigit((unsigned char)rep->buf->p[7])) { |
| 6265 | msg->err_pos = 4; |
| 6266 | goto hdr_response_bad; |
| 6267 | } |
| 6268 | } |
| 6269 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6270 | /* check if the response is HTTP/1.1 or above */ |
| 6271 | if ((msg->sl.st.v_l == 8) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6272 | ((rep->buf->p[5] > '1') || |
| 6273 | ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1')))) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6274 | msg->flags |= HTTP_MSGF_VER_11; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6275 | |
| 6276 | /* "connection" has not been parsed yet */ |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 6277 | 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] | 6278 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6279 | /* transfer length unknown*/ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6280 | msg->flags &= ~HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6281 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6282 | 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] | 6283 | |
Willy Tarreau | 3965040 | 2010-03-15 19:44:39 +0100 | [diff] [blame] | 6284 | /* Adjust server's health based on status code. Note: status codes 501 |
| 6285 | * and 505 are triggered on demand by client request, so we must not |
| 6286 | * count them as server failures. |
| 6287 | */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6288 | if (objt_server(s->target)) { |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 6289 | 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] | 6290 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK); |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 6291 | else |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6292 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS); |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 6293 | } |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 6294 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6295 | /* |
| 6296 | * 2: check for cacheability. |
| 6297 | */ |
| 6298 | |
| 6299 | switch (txn->status) { |
Willy Tarreau | 628c40c | 2014-04-24 19:11:26 +0200 | [diff] [blame] | 6300 | case 100: |
| 6301 | /* |
| 6302 | * We may be facing a 100-continue response, in which case this |
| 6303 | * is not the right response, and we're waiting for the next one. |
| 6304 | * Let's allow this response to go to the client and wait for the |
| 6305 | * next one. |
| 6306 | */ |
| 6307 | hdr_idx_init(&txn->hdr_idx); |
| 6308 | msg->next -= channel_forward(rep, msg->next); |
| 6309 | msg->msg_state = HTTP_MSG_RPBEFORE; |
| 6310 | txn->status = 0; |
| 6311 | s->logs.t_data = -1; /* was not a response yet */ |
| 6312 | goto next_one; |
| 6313 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6314 | case 200: |
| 6315 | case 203: |
| 6316 | case 206: |
| 6317 | case 300: |
| 6318 | case 301: |
| 6319 | case 410: |
| 6320 | /* RFC2616 @13.4: |
| 6321 | * "A response received with a status code of |
| 6322 | * 200, 203, 206, 300, 301 or 410 MAY be stored |
| 6323 | * by a cache (...) unless a cache-control |
| 6324 | * directive prohibits caching." |
| 6325 | * |
| 6326 | * RFC2616 @9.5: POST method : |
| 6327 | * "Responses to this method are not cacheable, |
| 6328 | * unless the response includes appropriate |
| 6329 | * Cache-Control or Expires header fields." |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6330 | */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6331 | if (likely(txn->meth != HTTP_METH_POST) && |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 6332 | ((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] | 6333 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
| 6334 | break; |
| 6335 | default: |
| 6336 | break; |
| 6337 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6338 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6339 | /* |
| 6340 | * 3: we may need to capture headers |
| 6341 | */ |
| 6342 | s->logs.logwait &= ~LW_RESP; |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 6343 | if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap)) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6344 | capture_headers(rep->buf->p, &txn->hdr_idx, |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 6345 | s->res_cap, sess->fe->rsp_cap); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 6346 | |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 6347 | /* 4: determine the transfer-length according to RFC2616 #4.4, updated |
| 6348 | * by RFC7230#3.3.3 : |
| 6349 | * |
| 6350 | * The length of a message body is determined by one of the following |
| 6351 | * (in order of precedence): |
| 6352 | * |
| 6353 | * 1. Any response to a HEAD request and any response with a 1xx |
| 6354 | * (Informational), 204 (No Content), or 304 (Not Modified) status |
| 6355 | * code is always terminated by the first empty line after the |
| 6356 | * header fields, regardless of the header fields present in the |
| 6357 | * message, and thus cannot contain a message body. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6358 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 6359 | * 2. Any 2xx (Successful) response to a CONNECT request implies that |
| 6360 | * the connection will become a tunnel immediately after the empty |
| 6361 | * line that concludes the header fields. A client MUST ignore any |
| 6362 | * Content-Length or Transfer-Encoding header fields received in |
| 6363 | * such a message. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6364 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 6365 | * 3. If a Transfer-Encoding header field is present and the chunked |
| 6366 | * transfer coding (Section 4.1) is the final encoding, the message |
| 6367 | * body length is determined by reading and decoding the chunked |
| 6368 | * data until the transfer coding indicates the data is complete. |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6369 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 6370 | * If a Transfer-Encoding header field is present in a response and |
| 6371 | * the chunked transfer coding is not the final encoding, the |
| 6372 | * message body length is determined by reading the connection until |
| 6373 | * it is closed by the server. If a Transfer-Encoding header field |
| 6374 | * is present in a request and the chunked transfer coding is not |
| 6375 | * the final encoding, the message body length cannot be determined |
| 6376 | * reliably; the server MUST respond with the 400 (Bad Request) |
| 6377 | * status code and then close the connection. |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6378 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 6379 | * If a message is received with both a Transfer-Encoding and a |
| 6380 | * Content-Length header field, the Transfer-Encoding overrides the |
| 6381 | * Content-Length. Such a message might indicate an attempt to |
| 6382 | * perform request smuggling (Section 9.5) or response splitting |
| 6383 | * (Section 9.4) and ought to be handled as an error. A sender MUST |
| 6384 | * remove the received Content-Length field prior to forwarding such |
| 6385 | * a message downstream. |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6386 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 6387 | * 4. If a message is received without Transfer-Encoding and with |
| 6388 | * either multiple Content-Length header fields having differing |
| 6389 | * field-values or a single Content-Length header field having an |
| 6390 | * invalid value, then the message framing is invalid and the |
| 6391 | * recipient MUST treat it as an unrecoverable error. If this is a |
| 6392 | * request message, the server MUST respond with a 400 (Bad Request) |
| 6393 | * status code and then close the connection. If this is a response |
| 6394 | * message received by a proxy, the proxy MUST close the connection |
| 6395 | * to the server, discard the received response, and send a 502 (Bad |
| 6396 | * Gateway) response to the client. If this is a response message |
| 6397 | * received by a user agent, the user agent MUST close the |
| 6398 | * connection to the server and discard the received response. |
| 6399 | * |
| 6400 | * 5. If a valid Content-Length header field is present without |
| 6401 | * Transfer-Encoding, its decimal value defines the expected message |
| 6402 | * body length in octets. If the sender closes the connection or |
| 6403 | * the recipient times out before the indicated number of octets are |
| 6404 | * received, the recipient MUST consider the message to be |
| 6405 | * incomplete and close the connection. |
| 6406 | * |
| 6407 | * 6. If this is a request message and none of the above are true, then |
| 6408 | * the message body length is zero (no message body is present). |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6409 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 6410 | * 7. Otherwise, this is a response message without a declared message |
| 6411 | * body length, so the message body length is determined by the |
| 6412 | * number of octets received prior to the server closing the |
| 6413 | * connection. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6414 | */ |
| 6415 | |
| 6416 | /* Skip parsing if no content length is possible. The response flags |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 6417 | * 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] | 6418 | * 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] | 6419 | * FIXME: should we parse anyway and return an error on chunked encoding ? |
| 6420 | */ |
| 6421 | if (txn->meth == HTTP_METH_HEAD || |
| 6422 | (txn->status >= 100 && txn->status < 200) || |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6423 | txn->status == 204 || txn->status == 304) { |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6424 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Willy Tarreau | 9101535 | 2012-11-27 07:31:33 +0100 | [diff] [blame] | 6425 | s->comp_algo = NULL; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6426 | goto skip_content_length; |
| 6427 | } |
| 6428 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6429 | use_close_only = 0; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6430 | ctx.idx = 0; |
Willy Tarreau | 4979d5c | 2015-05-01 10:06:30 +0200 | [diff] [blame] | 6431 | 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] | 6432 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6433 | msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
| 6434 | else if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6435 | /* bad transfer-encoding (chunked followed by something else) */ |
| 6436 | use_close_only = 1; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6437 | msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 6438 | break; |
| 6439 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6440 | } |
| 6441 | |
Willy Tarreau | 1c91391 | 2015-04-30 10:57:51 +0200 | [diff] [blame] | 6442 | /* Chunked responses must have their content-length removed */ |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6443 | ctx.idx = 0; |
Willy Tarreau | b4d0c03 | 2015-05-01 10:25:45 +0200 | [diff] [blame] | 6444 | if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) { |
Willy Tarreau | 1c91391 | 2015-04-30 10:57:51 +0200 | [diff] [blame] | 6445 | while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) |
| 6446 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 6447 | } |
Willy Tarreau | b4d0c03 | 2015-05-01 10:25:45 +0200 | [diff] [blame] | 6448 | 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] | 6449 | signed long long cl; |
| 6450 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6451 | if (!ctx.vlen) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6452 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6453 | goto hdr_response_bad; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6454 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6455 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6456 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6457 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6458 | goto hdr_response_bad; /* parse failure */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6459 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6460 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6461 | if (cl < 0) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6462 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6463 | goto hdr_response_bad; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6464 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6465 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6466 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6467 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6468 | goto hdr_response_bad; /* already specified, was different */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 6469 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6470 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6471 | msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 6472 | msg->body_len = msg->chunk_len = cl; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6473 | } |
| 6474 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6475 | if (sess->fe->comp || s->be->comp) |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 6476 | select_compression_response_header(s, rep->buf); |
| 6477 | |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 6478 | skip_content_length: |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6479 | /* Now we have to check if we need to modify the Connection header. |
| 6480 | * This is more difficult on the response than it is on the request, |
| 6481 | * because we can have two different HTTP versions and we don't know |
| 6482 | * how the client will interprete a response. For instance, let's say |
| 6483 | * that the client sends a keep-alive request in HTTP/1.0 and gets an |
| 6484 | * HTTP/1.1 response without any header. Maybe it will bound itself to |
| 6485 | * HTTP/1.0 because it only knows about it, and will consider the lack |
| 6486 | * of header as a close, or maybe it knows HTTP/1.1 and can consider |
| 6487 | * the lack of header as a keep-alive. Thus we will use two flags |
| 6488 | * indicating how a request MAY be understood by the client. In case |
| 6489 | * of multiple possibilities, we'll fix the header to be explicit. If |
| 6490 | * ambiguous cases such as both close and keepalive are seen, then we |
| 6491 | * will fall back to explicit close. Note that we won't take risks with |
| 6492 | * HTTP/1.0 clients which may not necessarily understand keep-alive. |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6493 | * See doc/internals/connection-header.txt for the complete matrix. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6494 | */ |
| 6495 | |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 6496 | if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || |
| 6497 | txn->status == 101)) { |
| 6498 | /* Either we've established an explicit tunnel, or we're |
| 6499 | * switching the protocol. In both cases, we're very unlikely |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 6500 | * to understand the next protocols. We have to switch to tunnel |
| 6501 | * mode, so that we transfer the request and responses then let |
| 6502 | * this protocol pass unmodified. When we later implement specific |
| 6503 | * parsers for such protocols, we'll want to check the Upgrade |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 6504 | * header which contains information about that protocol for |
| 6505 | * responses with status 101 (eg: see RFC2817 about TLS). |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 6506 | */ |
| 6507 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN; |
| 6508 | } |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 6509 | else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) && |
| 6510 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6511 | ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6512 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) { |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6513 | int to_del = 0; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6514 | |
Willy Tarreau | 70dffda | 2014-01-30 03:07:23 +0100 | [diff] [blame] | 6515 | /* this situation happens when combining pretend-keepalive with httpclose. */ |
| 6516 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6517 | ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6518 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) |
Willy Tarreau | 70dffda | 2014-01-30 03:07:23 +0100 | [diff] [blame] | 6519 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 6520 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6521 | /* on unknown transfer length, we must close */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6522 | if (!(msg->flags & HTTP_MSGF_XFER_LEN) && |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6523 | (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) |
| 6524 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6525 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6526 | /* now adjust header transformations depending on current state */ |
| 6527 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN || |
| 6528 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 6529 | to_del |= 2; /* remove "keep-alive" on any response */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6530 | if (!(msg->flags & HTTP_MSGF_VER_11)) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6531 | to_del |= 1; /* remove "close" for HTTP/1.0 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6532 | } |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6533 | else { /* SCL / KAL */ |
| 6534 | to_del |= 1; /* remove "close" on any response */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6535 | if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6536 | to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6537 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6538 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6539 | /* Parse and remove some headers from the connection header */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 6540 | http_parse_connection_header(txn, msg, to_del); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6541 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6542 | /* Some keep-alive responses are converted to Server-close if |
| 6543 | * the server wants to close. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6544 | */ |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6545 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) { |
| 6546 | if ((txn->flags & TX_HDR_CONN_CLO) || |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 6547 | (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11))) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6548 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 6549 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 6550 | } |
| 6551 | |
Willy Tarreau | 7959a55 | 2013-09-23 16:44:27 +0200 | [diff] [blame] | 6552 | /* we want to have the response time before we start processing it */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6553 | s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now); |
Willy Tarreau | 7959a55 | 2013-09-23 16:44:27 +0200 | [diff] [blame] | 6554 | |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 6555 | /* end of job, return OK */ |
| 6556 | rep->analysers &= ~an_bit; |
| 6557 | rep->analyse_exp = TICK_ETERNITY; |
| 6558 | channel_auto_close(rep); |
| 6559 | return 1; |
| 6560 | |
| 6561 | abort_keep_alive: |
| 6562 | /* A keep-alive request to the server failed on a network error. |
| 6563 | * The client is required to retry. We need to close without returning |
| 6564 | * any other information so that the client retries. |
| 6565 | */ |
| 6566 | txn->status = 0; |
| 6567 | rep->analysers = 0; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6568 | s->req.analysers = 0; |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 6569 | channel_auto_close(rep); |
| 6570 | s->logs.logwait = 0; |
| 6571 | s->logs.level = 0; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6572 | s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 6573 | channel_truncate(rep); |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 6574 | stream_int_retnclose(&s->si[0], NULL); |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 6575 | return 0; |
| 6576 | } |
| 6577 | |
| 6578 | /* This function performs all the processing enabled for the current response. |
| 6579 | * 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] | 6580 | * 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] | 6581 | * other functions. It works like process_request (see indications above). |
| 6582 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6583 | 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] | 6584 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 6585 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 6586 | struct http_txn *txn = s->txn; |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 6587 | struct http_msg *msg = &txn->rsp; |
| 6588 | struct proxy *cur_proxy; |
| 6589 | struct cond_wordlist *wl; |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 6590 | enum rule_result ret = HTTP_RULE_RES_CONT; |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 6591 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6592 | 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] | 6593 | now_ms, __FUNCTION__, |
| 6594 | s, |
| 6595 | rep, |
| 6596 | rep->rex, rep->wex, |
| 6597 | rep->flags, |
| 6598 | rep->buf->i, |
| 6599 | rep->analysers); |
| 6600 | |
| 6601 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */ |
| 6602 | return 0; |
| 6603 | |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 6604 | /* The stats applet needs to adjust the Connection header but we don't |
| 6605 | * apply any filter there. |
| 6606 | */ |
Willy Tarreau | 612adb8 | 2015-03-10 15:25:54 +0100 | [diff] [blame] | 6607 | if (unlikely(objt_applet(s->target) == &http_stats_applet)) { |
| 6608 | rep->analysers &= ~an_bit; |
| 6609 | rep->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 6610 | goto skip_filters; |
Willy Tarreau | 612adb8 | 2015-03-10 15:25:54 +0100 | [diff] [blame] | 6611 | } |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 6612 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6613 | /* |
| 6614 | * We will have to evaluate the filters. |
| 6615 | * As opposed to version 1.2, now they will be evaluated in the |
| 6616 | * filters order and not in the header order. This means that |
| 6617 | * each filter has to be validated among all headers. |
| 6618 | * |
| 6619 | * Filters are tried with ->be first, then with ->fe if it is |
| 6620 | * different from ->be. |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 6621 | * |
| 6622 | * Maybe we are in resume condiion. In this case I choose the |
| 6623 | * "struct proxy" which contains the rule list matching the resume |
| 6624 | * pointer. If none of theses "struct proxy" match, I initialise |
| 6625 | * the process with the first one. |
| 6626 | * |
| 6627 | * In fact, I check only correspondance betwwen the current list |
| 6628 | * pointer and the ->fe rule list. If it doesn't match, I initialize |
| 6629 | * the loop with the ->be. |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6630 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6631 | if (s->current_rule_list == &sess->fe->http_res_rules) |
| 6632 | cur_proxy = sess->fe; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 6633 | else |
| 6634 | cur_proxy = s->be; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6635 | while (1) { |
| 6636 | struct proxy *rule_set = cur_proxy; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6637 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6638 | /* evaluate http-response rules */ |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 6639 | if (ret == HTTP_RULE_RES_CONT) { |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 6640 | 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] | 6641 | |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 6642 | if (ret == HTTP_RULE_RES_BADREQ) |
| 6643 | goto return_srv_prx_502; |
| 6644 | |
| 6645 | if (ret == HTTP_RULE_RES_DONE) { |
| 6646 | rep->analysers &= ~an_bit; |
| 6647 | rep->analyse_exp = TICK_ETERNITY; |
| 6648 | return 1; |
| 6649 | } |
| 6650 | } |
| 6651 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 6652 | /* we need to be called again. */ |
| 6653 | if (ret == HTTP_RULE_RES_YIELD) { |
| 6654 | channel_dont_close(rep); |
| 6655 | return 0; |
| 6656 | } |
| 6657 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6658 | /* try headers filters */ |
| 6659 | if (rule_set->rsp_exp != NULL) { |
| 6660 | if (apply_filters_to_response(s, rep, rule_set) < 0) { |
| 6661 | return_bad_resp: |
| 6662 | if (objt_server(s->target)) { |
| 6663 | objt_server(s->target)->counters.failed_resp++; |
| 6664 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 6665 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6666 | s->be->be_counters.failed_resp++; |
| 6667 | return_srv_prx_502: |
| 6668 | rep->analysers = 0; |
| 6669 | txn->status = 502; |
| 6670 | s->logs.t_data = -1; /* was not a valid response */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 6671 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 6672 | channel_truncate(rep); |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 6673 | stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502)); |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6674 | if (!(s->flags & SF_ERR_MASK)) |
| 6675 | s->flags |= SF_ERR_PRXCOND; |
| 6676 | if (!(s->flags & SF_FINST_MASK)) |
| 6677 | s->flags |= SF_FINST_H; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6678 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6679 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6680 | } |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 6681 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6682 | /* has the response been denied ? */ |
| 6683 | if (txn->flags & TX_SVDENY) { |
| 6684 | if (objt_server(s->target)) |
| 6685 | objt_server(s->target)->counters.failed_secu++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 6686 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6687 | s->be->be_counters.denied_resp++; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6688 | sess->fe->fe_counters.denied_resp++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 6689 | if (sess->listener->counters) |
| 6690 | sess->listener->counters->denied_resp++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6691 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6692 | goto return_srv_prx_502; |
| 6693 | } |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 6694 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6695 | /* add response headers from the rule sets in the same order */ |
| 6696 | list_for_each_entry(wl, &rule_set->rsp_add, list) { |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 6697 | if (txn->status < 200 && txn->status != 101) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6698 | break; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6699 | if (wl->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 6700 | 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] | 6701 | ret = acl_pass(ret); |
| 6702 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 6703 | ret = !ret; |
| 6704 | if (!ret) |
| 6705 | continue; |
| 6706 | } |
| 6707 | if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0)) |
| 6708 | goto return_bad_resp; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6709 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6710 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6711 | /* check whether we're already working on the frontend */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6712 | if (cur_proxy == sess->fe) |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6713 | break; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6714 | cur_proxy = sess->fe; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6715 | } |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 6716 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 6717 | /* After this point, this anayzer can't return yield, so we can |
| 6718 | * remove the bit corresponding to this analyzer from the list. |
| 6719 | * |
| 6720 | * Note that the intermediate returns and goto found previously |
| 6721 | * reset the analyzers. |
| 6722 | */ |
| 6723 | rep->analysers &= ~an_bit; |
| 6724 | rep->analyse_exp = TICK_ETERNITY; |
| 6725 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6726 | /* OK that's all we can do for 1xx responses */ |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 6727 | if (unlikely(txn->status < 200 && txn->status != 101)) |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6728 | goto skip_header_mangling; |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 6729 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6730 | /* |
| 6731 | * Now check for a server cookie. |
| 6732 | */ |
Willy Tarreau | 53a09d5 | 2015-08-10 18:59:40 +0200 | [diff] [blame] | 6733 | 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] | 6734 | manage_server_side_cookies(s, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6735 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6736 | /* |
| 6737 | * Check for cache-control or pragma headers if required. |
| 6738 | */ |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 6739 | 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] | 6740 | check_response_for_cacheability(s, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6741 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6742 | /* |
| 6743 | * Add server cookie in the response if needed |
| 6744 | */ |
| 6745 | if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) && |
| 6746 | !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) && |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6747 | (!(s->flags & SF_DIRECT) || |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6748 | ((s->be->cookie_maxidle || txn->cookie_last_date) && |
| 6749 | (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) || |
| 6750 | (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date |
| 6751 | (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date |
| 6752 | (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) && |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6753 | !(s->flags & SF_IGNORE_PRST)) { |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6754 | /* the server is known, it's not the one the client requested, or the |
| 6755 | * cookie's last seen date needs to be refreshed. We have to |
| 6756 | * insert a set-cookie here, except if we want to insert only on POST |
| 6757 | * requests and this one isn't. Note that servers which don't have cookies |
| 6758 | * (eg: some backup servers) will return a full cookie removal request. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6759 | */ |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6760 | if (!objt_server(s->target)->cookie) { |
| 6761 | chunk_printf(&trash, |
| 6762 | "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/", |
| 6763 | s->be->cookie_name); |
| 6764 | } |
| 6765 | else { |
| 6766 | 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] | 6767 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6768 | if (s->be->cookie_maxidle || s->be->cookie_maxlife) { |
| 6769 | /* emit last_date, which is mandatory */ |
| 6770 | trash.str[trash.len++] = COOKIE_DELIM_DATE; |
| 6771 | s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len); |
| 6772 | trash.len += 5; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6773 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6774 | if (s->be->cookie_maxlife) { |
| 6775 | /* emit first_date, which is either the original one or |
| 6776 | * the current date. |
| 6777 | */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6778 | trash.str[trash.len++] = COOKIE_DELIM_DATE; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6779 | s30tob64(txn->cookie_first_date ? |
| 6780 | txn->cookie_first_date >> 2 : |
| 6781 | (date.tv_sec+3) >> 2, trash.str + trash.len); |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6782 | trash.len += 5; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6783 | } |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6784 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6785 | chunk_appendf(&trash, "; path=/"); |
| 6786 | } |
Willy Tarreau | 4992dd2 | 2012-05-31 21:02:17 +0200 | [diff] [blame] | 6787 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6788 | if (s->be->cookie_domain) |
| 6789 | chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain); |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6790 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6791 | if (s->be->ck_opts & PR_CK_HTTPONLY) |
| 6792 | chunk_appendf(&trash, "; HttpOnly"); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6793 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6794 | if (s->be->ck_opts & PR_CK_SECURE) |
| 6795 | chunk_appendf(&trash, "; Secure"); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6796 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6797 | if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0)) |
| 6798 | goto return_bad_resp; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6799 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6800 | txn->flags &= ~TX_SCK_MASK; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6801 | if (objt_server(s->target)->cookie && (s->flags & SF_DIRECT)) |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6802 | /* the server did not change, only the date was updated */ |
| 6803 | txn->flags |= TX_SCK_UPDATED; |
| 6804 | else |
| 6805 | txn->flags |= TX_SCK_INSERTED; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6806 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6807 | /* Here, we will tell an eventual cache on the client side that we don't |
| 6808 | * want it to cache this reply because HTTP/1.0 caches also cache cookies ! |
| 6809 | * Some caches understand the correct form: 'no-cache="set-cookie"', but |
| 6810 | * 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] | 6811 | */ |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6812 | if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6813 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6814 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 6815 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6816 | if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, |
| 6817 | "Cache-control: private", 22) < 0)) |
| 6818 | goto return_bad_resp; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6819 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6820 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6821 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6822 | /* |
| 6823 | * Check if result will be cacheable with a cookie. |
| 6824 | * We'll block the response if security checks have caught |
| 6825 | * nasty things such as a cacheable cookie. |
| 6826 | */ |
| 6827 | if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) == |
| 6828 | (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) && |
| 6829 | (s->be->options & PR_O_CHK_CACHE)) { |
| 6830 | /* we're in presence of a cacheable response containing |
| 6831 | * a set-cookie header. We'll block it as requested by |
| 6832 | * the 'checkcache' option, and send an alert. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6833 | */ |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6834 | if (objt_server(s->target)) |
| 6835 | objt_server(s->target)->counters.failed_secu++; |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 6836 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6837 | s->be->be_counters.denied_resp++; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6838 | sess->fe->fe_counters.denied_resp++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 6839 | if (sess->listener->counters) |
| 6840 | sess->listener->counters->denied_resp++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6841 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6842 | Alert("Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 6843 | s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>"); |
| 6844 | send_log(s->be, LOG_ALERT, |
| 6845 | "Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 6846 | s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>"); |
| 6847 | goto return_srv_prx_502; |
| 6848 | } |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 6849 | |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 6850 | skip_filters: |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6851 | /* |
| 6852 | * Adjust "Connection: close" or "Connection: keep-alive" if needed. |
| 6853 | * If an "Upgrade" token is found, the header is left untouched in order |
| 6854 | * 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] | 6855 | * if anything but "Upgrade" is present in the Connection header. We don't |
| 6856 | * want to touch any 101 response either since it's switching to another |
| 6857 | * protocol. |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6858 | */ |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 6859 | if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) && |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6860 | (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6861 | ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL || |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6862 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) { |
| 6863 | unsigned int want_flags = 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6864 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6865 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 6866 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 6867 | /* we want a keep-alive response here. Keep-alive header |
| 6868 | * required if either side is not 1.1. |
| 6869 | */ |
| 6870 | if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11)) |
| 6871 | want_flags |= TX_CON_KAL_SET; |
| 6872 | } |
| 6873 | else { |
| 6874 | /* we want a close response here. Close header required if |
| 6875 | * the server is 1.1, regardless of the client. |
| 6876 | */ |
| 6877 | if (msg->flags & HTTP_MSGF_VER_11) |
| 6878 | want_flags |= TX_CON_CLO_SET; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6879 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6880 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6881 | if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 6882 | http_change_connection_header(txn, msg, want_flags); |
| 6883 | } |
| 6884 | |
| 6885 | skip_header_mangling: |
| 6886 | if ((msg->flags & HTTP_MSGF_XFER_LEN) || |
| 6887 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) |
| 6888 | rep->analysers |= AN_RES_HTTP_XFER_BODY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6889 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6890 | /* if the user wants to log as soon as possible, without counting |
| 6891 | * bytes from the server, then this is the right moment. We have |
| 6892 | * to temporarily assign bytes_out to log what we currently have. |
| 6893 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6894 | if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) { |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 6895 | s->logs.t_close = s->logs.t_data; /* to get a valid end date */ |
| 6896 | s->logs.bytes_out = txn->rsp.eoh; |
| 6897 | s->do_log(s); |
| 6898 | s->logs.bytes_out = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6899 | } |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 6900 | return 1; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 6901 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6902 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6903 | /* This function is an analyser which forwards response body (including chunk |
| 6904 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 6905 | * zero byte. The only situation where it must not be called is when we're in |
| 6906 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 6907 | * remaining data and to resync after end of body. It expects the msg_state to |
| 6908 | * 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] | 6909 | * 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] | 6910 | * |
| 6911 | * It is capable of compressing response data both in content-length mode and |
| 6912 | * in chunked mode. The state machines follows different flows depending on |
| 6913 | * whether content-length and chunked modes are used, since there are no |
| 6914 | * trailers in content-length : |
| 6915 | * |
| 6916 | * chk-mode cl-mode |
| 6917 | * ,----- BODY -----. |
| 6918 | * / \ |
| 6919 | * V size > 0 V chk-mode |
| 6920 | * .--> SIZE -------------> DATA -------------> CRLF |
| 6921 | * | | size == 0 | last byte | |
| 6922 | * | v final crlf v inspected | |
| 6923 | * | TRAILERS -----------> DONE | |
| 6924 | * | | |
| 6925 | * `----------------------------------------------' |
| 6926 | * |
| 6927 | * Compression only happens in the DATA state, and must be flushed in final |
| 6928 | * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding |
| 6929 | * is performed at once on final states for all bytes parsed, or when leaving |
| 6930 | * on missing data. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6931 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6932 | 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] | 6933 | { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6934 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 6935 | struct http_txn *txn = s->txn; |
| 6936 | struct http_msg *msg = &s->txn->rsp; |
Willy Tarreau | f2f7d6b | 2014-11-24 11:55:08 +0100 | [diff] [blame] | 6937 | static struct buffer *tmpbuf = &buf_empty; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 6938 | int compressing = 0; |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 6939 | int ret; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6940 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 6941 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 6942 | return 0; |
| 6943 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 6944 | 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] | 6945 | ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) || |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6946 | !s->req.analysers) { |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 6947 | /* Output closed while we were sending data. We must abort and |
| 6948 | * wake the other side up. |
| 6949 | */ |
| 6950 | msg->msg_state = HTTP_MSG_ERROR; |
| 6951 | http_resync_states(s); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 6952 | return 1; |
| 6953 | } |
| 6954 | |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 6955 | /* in most states, we should abort in case of early close */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6956 | channel_auto_close(res); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 6957 | |
Willy Tarreau | bb2e669 | 2014-07-10 19:06:10 +0200 | [diff] [blame] | 6958 | if (msg->sov > 0) { |
Willy Tarreau | b59c7bf | 2014-04-22 14:29:58 +0200 | [diff] [blame] | 6959 | /* we have msg->sov which points to the first byte of message |
| 6960 | * body, and res->buf.p still points to the beginning of the |
| 6961 | * message. We forward the headers now, as we don't need them |
| 6962 | * anymore, and we want to flush them. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6963 | */ |
Willy Tarreau | b59c7bf | 2014-04-22 14:29:58 +0200 | [diff] [blame] | 6964 | b_adv(res->buf, msg->sov); |
| 6965 | msg->next -= msg->sov; |
| 6966 | msg->sov = 0; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 6967 | |
Willy Tarreau | b59c7bf | 2014-04-22 14:29:58 +0200 | [diff] [blame] | 6968 | /* The previous analysers guarantee that the state is somewhere |
| 6969 | * between MSG_BODY and the first MSG_DATA. So msg->sol and |
| 6970 | * msg->next are always correct. |
| 6971 | */ |
| 6972 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
| 6973 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
| 6974 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 6975 | else |
| 6976 | msg->msg_state = HTTP_MSG_DATA; |
| 6977 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6978 | } |
| 6979 | |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 6980 | if (res->to_forward) { |
| 6981 | /* We can't process the buffer's contents yet */ |
| 6982 | res->flags |= CF_WAKE_WRITE; |
| 6983 | goto missing_data; |
| 6984 | } |
| 6985 | |
Willy Tarreau | 32b5ab2 | 2014-04-21 11:27:29 +0200 | [diff] [blame] | 6986 | if (unlikely(s->comp_algo != NULL) && msg->msg_state < HTTP_MSG_TRAILERS) { |
| 6987 | /* We need a compression buffer in the DATA state to put the |
| 6988 | * output of compressed data, and in CRLF state to let the |
| 6989 | * TRAILERS state finish the job of removing the trailing CRLF. |
| 6990 | */ |
Willy Tarreau | f2f7d6b | 2014-11-24 11:55:08 +0100 | [diff] [blame] | 6991 | if (unlikely(!tmpbuf->size)) { |
Willy Tarreau | 32b5ab2 | 2014-04-21 11:27:29 +0200 | [diff] [blame] | 6992 | /* this is the first time we need the compression buffer */ |
Willy Tarreau | e583ea5 | 2014-11-24 11:30:16 +0100 | [diff] [blame] | 6993 | if (b_alloc(&tmpbuf) == NULL) |
Willy Tarreau | 32b5ab2 | 2014-04-21 11:27:29 +0200 | [diff] [blame] | 6994 | goto aborted_xfer; /* no memory */ |
| 6995 | } |
| 6996 | |
| 6997 | ret = http_compression_buffer_init(s, res->buf, tmpbuf); |
Willy Tarreau | 4afd70a | 2014-01-25 02:26:39 +0100 | [diff] [blame] | 6998 | if (ret < 0) { |
| 6999 | res->flags |= CF_WAKE_WRITE; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 7000 | goto missing_data; /* not enough spaces in buffers */ |
Willy Tarreau | 4afd70a | 2014-01-25 02:26:39 +0100 | [diff] [blame] | 7001 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 7002 | compressing = 1; |
| 7003 | } |
| 7004 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 7005 | while (1) { |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 7006 | switch (msg->msg_state - HTTP_MSG_DATA) { |
| 7007 | case HTTP_MSG_DATA - HTTP_MSG_DATA: /* must still forward */ |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 7008 | /* we may have some pending data starting at res->buf->p */ |
| 7009 | if (unlikely(s->comp_algo)) { |
Willy Tarreau | 7f2f8d5 | 2014-04-18 00:20:14 +0200 | [diff] [blame] | 7010 | ret = http_compression_buffer_add_data(s, res->buf, tmpbuf); |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 7011 | if (ret < 0) |
| 7012 | goto aborted_xfer; |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 7013 | |
Willy Tarreau | d5a6783 | 2014-04-21 10:54:27 +0200 | [diff] [blame] | 7014 | if (msg->chunk_len) { |
| 7015 | /* input empty or output full */ |
| 7016 | if (res->buf->i > msg->next) |
| 7017 | res->flags |= CF_WAKE_WRITE; |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 7018 | goto missing_data; |
| 7019 | } |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 7020 | } |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 7021 | else { |
Willy Tarreau | d5a6783 | 2014-04-21 10:54:27 +0200 | [diff] [blame] | 7022 | if (msg->chunk_len > res->buf->i - msg->next) { |
| 7023 | /* output full */ |
| 7024 | res->flags |= CF_WAKE_WRITE; |
| 7025 | goto missing_data; |
| 7026 | } |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 7027 | msg->next += msg->chunk_len; |
| 7028 | msg->chunk_len = 0; |
| 7029 | } |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 7030 | |
| 7031 | /* nothing left to forward */ |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 7032 | if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 7033 | msg->msg_state = HTTP_MSG_CHUNK_CRLF; |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 7034 | } else { |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 7035 | msg->msg_state = HTTP_MSG_DONE; |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 7036 | break; |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 7037 | } |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 7038 | /* fall through for HTTP_MSG_CHUNK_CRLF */ |
| 7039 | |
| 7040 | case HTTP_MSG_CHUNK_CRLF - HTTP_MSG_DATA: |
| 7041 | /* we want the CRLF after the data */ |
| 7042 | |
| 7043 | ret = http_skip_chunk_crlf(msg); |
| 7044 | if (ret == 0) |
| 7045 | goto missing_data; |
| 7046 | else if (ret < 0) { |
| 7047 | if (msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7048 | http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_CRLF, sess->fe); |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 7049 | goto return_bad_res; |
| 7050 | } |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 7051 | /* we're in MSG_CHUNK_SIZE now, fall through */ |
| 7052 | |
| 7053 | case HTTP_MSG_CHUNK_SIZE - HTTP_MSG_DATA: |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 7054 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 7055 | * set ->next to point to the body and switch to DATA or |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 7056 | * TRAILERS state. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 7057 | */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 7058 | |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 7059 | ret = http_parse_chunk_size(msg); |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 7060 | if (ret == 0) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 7061 | goto missing_data; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 7062 | else if (ret < 0) { |
| 7063 | if (msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7064 | http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_SIZE, sess->fe); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 7065 | goto return_bad_res; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 7066 | } |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 7067 | /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */ |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 7068 | break; |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 7069 | |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 7070 | case HTTP_MSG_TRAILERS - HTTP_MSG_DATA: |
Willy Tarreau | 168ebc5 | 2014-04-18 00:53:43 +0200 | [diff] [blame] | 7071 | if (unlikely(compressing)) { |
| 7072 | /* we need to flush output contents before syncing FSMs */ |
| 7073 | http_compression_buffer_end(s, &res->buf, &tmpbuf, 1); |
| 7074 | compressing = 0; |
| 7075 | } |
| 7076 | |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 7077 | ret = http_forward_trailers(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 7078 | if (ret == 0) |
| 7079 | goto missing_data; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 7080 | else if (ret < 0) { |
| 7081 | if (msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7082 | http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_TRAILERS, sess->fe); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 7083 | goto return_bad_res; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 7084 | } |
Willy Tarreau | 168ebc5 | 2014-04-18 00:53:43 +0200 | [diff] [blame] | 7085 | /* we're in HTTP_MSG_DONE now, fall through */ |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 7086 | |
| 7087 | default: |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 7088 | /* other states, DONE...TUNNEL */ |
Willy Tarreau | 168ebc5 | 2014-04-18 00:53:43 +0200 | [diff] [blame] | 7089 | if (unlikely(compressing)) { |
| 7090 | /* we need to flush output contents before syncing FSMs */ |
| 7091 | http_compression_buffer_end(s, &res->buf, &tmpbuf, 1); |
| 7092 | compressing = 0; |
| 7093 | } |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 7094 | |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 7095 | /* we may have some pending data starting at res->buf->p |
| 7096 | * such as a last chunk of data or trailers. |
| 7097 | */ |
| 7098 | b_adv(res->buf, msg->next); |
| 7099 | msg->next = 0; |
| 7100 | |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 7101 | ret = msg->msg_state; |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 7102 | /* for keep-alive we don't want to forward closes on DONE */ |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 7103 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 7104 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 7105 | channel_dont_close(res); |
Willy Tarreau | 3ce10ff | 2014-04-22 08:24:38 +0200 | [diff] [blame] | 7106 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 7107 | if (http_resync_states(s)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 7108 | /* some state changes occurred, maybe the analyser |
| 7109 | * was disabled too. |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 7110 | */ |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 7111 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 7112 | if (res->flags & CF_SHUTW) { |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 7113 | /* response errors are most likely due to |
| 7114 | * the client aborting the transfer. |
| 7115 | */ |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 7116 | goto aborted_xfer; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 7117 | } |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 7118 | if (msg->err_pos >= 0) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7119 | http_capture_bad_message(&s->be->invalid_rep, s, msg, ret, sess->fe); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 7120 | goto return_bad_res; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 7121 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 7122 | return 1; |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 7123 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 7124 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 7125 | } |
| 7126 | } |
| 7127 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 7128 | missing_data: |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 7129 | /* we may have some pending data starting at res->buf->p */ |
Willy Tarreau | 168ebc5 | 2014-04-18 00:53:43 +0200 | [diff] [blame] | 7130 | if (unlikely(compressing)) { |
| 7131 | http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS); |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 7132 | compressing = 0; |
| 7133 | } |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 7134 | |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 7135 | if ((s->comp_algo == NULL || msg->msg_state >= HTTP_MSG_TRAILERS)) { |
| 7136 | b_adv(res->buf, msg->next); |
| 7137 | msg->next = 0; |
| 7138 | msg->chunk_len -= channel_forward(res, msg->chunk_len); |
| 7139 | } |
| 7140 | |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 7141 | if (res->flags & CF_SHUTW) |
| 7142 | goto aborted_xfer; |
| 7143 | |
| 7144 | /* stop waiting for data if the input is closed before the end. If the |
| 7145 | * client side was already closed, it means that the client has aborted, |
| 7146 | * so we don't want to count this as a server abort. Otherwise it's a |
| 7147 | * server abort. |
| 7148 | */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 7149 | if (res->flags & CF_SHUTR) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 7150 | if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW)) |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 7151 | goto aborted_xfer; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 7152 | if (!(s->flags & SF_ERR_MASK)) |
| 7153 | s->flags |= SF_ERR_SRVCL; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 7154 | s->be->be_counters.srv_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 7155 | if (objt_server(s->target)) |
| 7156 | objt_server(s->target)->counters.srv_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 7157 | goto return_bad_res_stats_ok; |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 7158 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 7159 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 7160 | /* 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] | 7161 | if (!s->req.analysers) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 7162 | goto return_bad_res; |
| 7163 | |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 7164 | /* 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] | 7165 | * chunks even if the server has closed, so we don't want to set CF_DONTCLOSE. |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 7166 | * Similarly, with keep-alive on the client side, we don't want to forward a |
| 7167 | * close. |
| 7168 | */ |
Willy Tarreau | 08b4d79 | 2012-10-27 01:36:34 +0200 | [diff] [blame] | 7169 | if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo || |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 7170 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 7171 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 7172 | channel_dont_close(res); |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 7173 | |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 7174 | /* 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] | 7175 | * 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] | 7176 | * 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] | 7177 | * modes are already handled by the stream sock layer. We must not do |
| 7178 | * this in content-length mode because it could present the MSG_MORE |
| 7179 | * flag with the last block of forwarded data, which would cause an |
| 7180 | * additional delay to be observed by the receiver. |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 7181 | */ |
Willy Tarreau | 08b4d79 | 2012-10-27 01:36:34 +0200 | [diff] [blame] | 7182 | if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo) |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 7183 | res->flags |= CF_EXPECT_MORE; |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 7184 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7185 | /* the stream handler will take care of timeouts and errors */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 7186 | return 0; |
| 7187 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 7188 | return_bad_res: /* let's centralize all bad responses */ |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 7189 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 7190 | if (objt_server(s->target)) |
| 7191 | objt_server(s->target)->counters.failed_resp++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 7192 | |
| 7193 | return_bad_res_stats_ok: |
Willy Tarreau | d01f426 | 2014-04-21 11:00:13 +0200 | [diff] [blame] | 7194 | if (unlikely(compressing)) { |
Willy Tarreau | 168ebc5 | 2014-04-18 00:53:43 +0200 | [diff] [blame] | 7195 | http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS); |
Willy Tarreau | d01f426 | 2014-04-21 11:00:13 +0200 | [diff] [blame] | 7196 | compressing = 0; |
| 7197 | } |
| 7198 | |
Willy Tarreau | c623c17 | 2014-04-18 09:53:50 +0200 | [diff] [blame] | 7199 | /* we may have some pending data starting at res->buf->p */ |
| 7200 | if (s->comp_algo == NULL) { |
| 7201 | b_adv(res->buf, msg->next); |
| 7202 | msg->next = 0; |
| 7203 | } |
| 7204 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 7205 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 7206 | /* don't send any error message as we're in the body */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 7207 | stream_int_retnclose(&s->si[0], NULL); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 7208 | res->analysers = 0; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 7209 | s->req.analysers = 0; /* we're in data phase, we want to abort both directions */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 7210 | if (objt_server(s->target)) |
| 7211 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 7212 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 7213 | if (!(s->flags & SF_ERR_MASK)) |
| 7214 | s->flags |= SF_ERR_PRXCOND; |
| 7215 | if (!(s->flags & SF_FINST_MASK)) |
| 7216 | s->flags |= SF_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 7217 | return 0; |
| 7218 | |
| 7219 | aborted_xfer: |
Willy Tarreau | 6fef8ae | 2014-04-22 21:22:06 +0200 | [diff] [blame] | 7220 | if (unlikely(compressing)) { |
| 7221 | http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS); |
| 7222 | compressing = 0; |
| 7223 | } |
| 7224 | |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 7225 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
| 7226 | /* don't send any error message as we're in the body */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 7227 | stream_int_retnclose(&s->si[0], NULL); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 7228 | res->analysers = 0; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 7229 | s->req.analysers = 0; /* we're in data phase, we want to abort both directions */ |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 7230 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7231 | sess->fe->fe_counters.cli_aborts++; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 7232 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 7233 | if (objt_server(s->target)) |
| 7234 | objt_server(s->target)->counters.cli_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 7235 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 7236 | if (!(s->flags & SF_ERR_MASK)) |
| 7237 | s->flags |= SF_ERR_CLICL; |
| 7238 | if (!(s->flags & SF_FINST_MASK)) |
| 7239 | s->flags |= SF_FINST_D; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 7240 | return 0; |
| 7241 | } |
| 7242 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7243 | /* Iterate the same filter through all request headers. |
| 7244 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7245 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 7246 | * DENY stats. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7247 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7248 | 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] | 7249 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7250 | char *cur_ptr, *cur_end, *cur_next; |
| 7251 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7252 | struct http_txn *txn = s->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7253 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7254 | int delta; |
Willy Tarreau | 0f7562b | 2007-01-07 15:46:13 +0100 | [diff] [blame] | 7255 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7256 | last_hdr = 0; |
| 7257 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7258 | cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7259 | old_idx = 0; |
| 7260 | |
| 7261 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7262 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7263 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7264 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7265 | (exp->action == ACT_ALLOW || |
| 7266 | exp->action == ACT_DENY || |
| 7267 | exp->action == ACT_TARPIT)) |
| 7268 | return 0; |
| 7269 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7270 | cur_idx = txn->hdr_idx.v[old_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7271 | if (!cur_idx) |
| 7272 | break; |
| 7273 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7274 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7275 | cur_ptr = cur_next; |
| 7276 | cur_end = cur_ptr + cur_hdr->len; |
| 7277 | cur_next = cur_end + cur_hdr->cr + 1; |
| 7278 | |
| 7279 | /* Now we have one header between cur_ptr and cur_end, |
| 7280 | * and the next header starts at cur_next. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7281 | */ |
| 7282 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 7283 | 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] | 7284 | switch (exp->action) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7285 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7286 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7287 | last_hdr = 1; |
| 7288 | break; |
| 7289 | |
| 7290 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7291 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7292 | last_hdr = 1; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7293 | break; |
| 7294 | |
| 7295 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7296 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7297 | last_hdr = 1; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7298 | break; |
| 7299 | |
| 7300 | case ACT_REPLACE: |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 7301 | trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch); |
| 7302 | if (trash.len < 0) |
| 7303 | return -1; |
| 7304 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7305 | 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] | 7306 | /* FIXME: if the user adds a newline in the replacement, the |
| 7307 | * index will not be recalculated for now, and the new line |
| 7308 | * will not be counted as a new header. |
| 7309 | */ |
| 7310 | |
| 7311 | cur_end += delta; |
| 7312 | cur_next += delta; |
| 7313 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7314 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7315 | break; |
| 7316 | |
| 7317 | case ACT_REMOVE: |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7318 | delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7319 | cur_next += delta; |
| 7320 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7321 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7322 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 7323 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7324 | cur_hdr->len = 0; |
| 7325 | cur_end = NULL; /* null-term has been rewritten */ |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 7326 | cur_idx = old_idx; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7327 | break; |
| 7328 | |
| 7329 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7330 | } |
| 7331 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7332 | /* keep the link from this header to next one in case of later |
| 7333 | * removal of next header. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7334 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7335 | old_idx = cur_idx; |
| 7336 | } |
| 7337 | return 0; |
| 7338 | } |
| 7339 | |
| 7340 | |
| 7341 | /* Apply the filter to the request line. |
| 7342 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 7343 | * or -1 if a replacement resulted in an invalid request line. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7344 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 7345 | * DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7346 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7347 | 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] | 7348 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7349 | char *cur_ptr, *cur_end; |
| 7350 | int done; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7351 | struct http_txn *txn = s->txn; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7352 | int delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7353 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7354 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7355 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7356 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7357 | (exp->action == ACT_ALLOW || |
| 7358 | exp->action == ACT_DENY || |
| 7359 | exp->action == ACT_TARPIT)) |
| 7360 | return 0; |
| 7361 | else if (exp->action == ACT_REMOVE) |
| 7362 | return 0; |
| 7363 | |
| 7364 | done = 0; |
| 7365 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7366 | cur_ptr = req->buf->p; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7367 | cur_end = cur_ptr + txn->req.sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7368 | |
| 7369 | /* Now we have the request line between cur_ptr and cur_end */ |
| 7370 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 7371 | 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] | 7372 | switch (exp->action) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7373 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7374 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7375 | done = 1; |
| 7376 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 7377 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7378 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7379 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7380 | done = 1; |
| 7381 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 7382 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7383 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7384 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7385 | done = 1; |
| 7386 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 7387 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7388 | case ACT_REPLACE: |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 7389 | trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch); |
| 7390 | if (trash.len < 0) |
| 7391 | return -1; |
| 7392 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7393 | 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] | 7394 | /* FIXME: if the user adds a newline in the replacement, the |
| 7395 | * index will not be recalculated for now, and the new line |
| 7396 | * will not be counted as a new header. |
| 7397 | */ |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 7398 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7399 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7400 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 7401 | cur_end = (char *)http_parse_reqline(&txn->req, |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7402 | HTTP_MSG_RQMETH, |
| 7403 | cur_ptr, cur_end + 1, |
| 7404 | NULL, NULL); |
| 7405 | if (unlikely(!cur_end)) |
| 7406 | return -1; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 7407 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7408 | /* we have a full request and we know that we have either a CR |
| 7409 | * or an LF at <ptr>. |
| 7410 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7411 | txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l); |
| 7412 | 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] | 7413 | /* there is no point trying this regex on headers */ |
| 7414 | return 1; |
| 7415 | } |
| 7416 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7417 | return done; |
| 7418 | } |
Willy Tarreau | 97de624 | 2006-12-27 17:18:38 +0100 | [diff] [blame] | 7419 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7420 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7421 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7422 | /* |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7423 | * 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] | 7424 | * 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] | 7425 | * unparsable request. Since it can manage the switch to another backend, it |
| 7426 | * updates the per-proxy DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7427 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7428 | 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] | 7429 | { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 7430 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7431 | struct http_txn *txn = s->txn; |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7432 | struct hdr_exp *exp; |
| 7433 | |
| 7434 | for (exp = px->req_exp; exp; exp = exp->next) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7435 | int ret; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7436 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7437 | /* |
| 7438 | * The interleaving of transformations and verdicts |
| 7439 | * makes it difficult to decide to continue or stop |
| 7440 | * the evaluation. |
| 7441 | */ |
| 7442 | |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7443 | if (txn->flags & (TX_CLDENY|TX_CLTARPIT)) |
| 7444 | break; |
| 7445 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7446 | if ((txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7447 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7448 | exp->action == ACT_TARPIT || exp->action == ACT_PASS)) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7449 | continue; |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7450 | |
| 7451 | /* if this filter had a condition, evaluate it now and skip to |
| 7452 | * next filter if the condition does not match. |
| 7453 | */ |
| 7454 | if (exp->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 7455 | 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] | 7456 | ret = acl_pass(ret); |
| 7457 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 7458 | ret = !ret; |
| 7459 | |
| 7460 | if (!ret) |
| 7461 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7462 | } |
| 7463 | |
| 7464 | /* Apply the filter to the request line. */ |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 7465 | ret = apply_filter_to_req_line(s, req, exp); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7466 | if (unlikely(ret < 0)) |
| 7467 | return -1; |
| 7468 | |
| 7469 | if (likely(ret == 0)) { |
| 7470 | /* The filter did not match the request, it can be |
| 7471 | * iterated through all headers. |
| 7472 | */ |
Willy Tarreau | 34d4c3c | 2015-01-30 20:58:58 +0100 | [diff] [blame] | 7473 | if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0)) |
| 7474 | return -1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7475 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7476 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7477 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7478 | } |
| 7479 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7480 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7481 | /* Find the end of a cookie value contained between <s> and <e>. It works the |
| 7482 | * same way as with headers above except that the semi-colon also ends a token. |
| 7483 | * See RFC2965 for more information. Note that it requires a valid header to |
| 7484 | * return a valid result. |
| 7485 | */ |
| 7486 | char *find_cookie_value_end(char *s, const char *e) |
| 7487 | { |
| 7488 | int quoted, qdpair; |
| 7489 | |
| 7490 | quoted = qdpair = 0; |
| 7491 | for (; s < e; s++) { |
| 7492 | if (qdpair) qdpair = 0; |
| 7493 | else if (quoted) { |
| 7494 | if (*s == '\\') qdpair = 1; |
| 7495 | else if (*s == '"') quoted = 0; |
| 7496 | } |
| 7497 | else if (*s == '"') quoted = 1; |
| 7498 | else if (*s == ',' || *s == ';') return s; |
| 7499 | } |
| 7500 | return s; |
| 7501 | } |
| 7502 | |
| 7503 | /* Delete a value in a header between delimiters <from> and <next> in buffer |
| 7504 | * <buf>. The number of characters displaced is returned, and the pointer to |
| 7505 | * the first delimiter is updated if required. The function tries as much as |
| 7506 | * possible to respect the following principles : |
| 7507 | * - replace <from> delimiter by the <next> one unless <from> points to a |
| 7508 | * colon, in which case <next> is simply removed |
| 7509 | * - set exactly one space character after the new first delimiter, unless |
| 7510 | * there are not enough characters in the block being moved to do so. |
| 7511 | * - remove unneeded spaces before the previous delimiter and after the new |
| 7512 | * one. |
| 7513 | * |
| 7514 | * It is the caller's responsibility to ensure that : |
| 7515 | * - <from> points to a valid delimiter or the colon ; |
| 7516 | * - <next> points to a valid delimiter or the final CR/LF ; |
| 7517 | * - there are non-space chars before <from> ; |
| 7518 | * - there is a CR/LF at or after <next>. |
| 7519 | */ |
Willy Tarreau | af81935 | 2012-08-27 22:08:00 +0200 | [diff] [blame] | 7520 | int del_hdr_value(struct buffer *buf, char **from, char *next) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7521 | { |
| 7522 | char *prev = *from; |
| 7523 | |
| 7524 | if (*prev == ':') { |
| 7525 | /* We're removing the first value, preserve the colon and add a |
| 7526 | * space if possible. |
| 7527 | */ |
| 7528 | if (!http_is_crlf[(unsigned char)*next]) |
| 7529 | next++; |
| 7530 | prev++; |
| 7531 | if (prev < next) |
| 7532 | *prev++ = ' '; |
| 7533 | |
| 7534 | while (http_is_spht[(unsigned char)*next]) |
| 7535 | next++; |
| 7536 | } else { |
| 7537 | /* Remove useless spaces before the old delimiter. */ |
| 7538 | while (http_is_spht[(unsigned char)*(prev-1)]) |
| 7539 | prev--; |
| 7540 | *from = prev; |
| 7541 | |
| 7542 | /* copy the delimiter and if possible a space if we're |
| 7543 | * not at the end of the line. |
| 7544 | */ |
| 7545 | if (!http_is_crlf[(unsigned char)*next]) { |
| 7546 | *prev++ = *next++; |
| 7547 | if (prev + 1 < next) |
| 7548 | *prev++ = ' '; |
| 7549 | while (http_is_spht[(unsigned char)*next]) |
| 7550 | next++; |
| 7551 | } |
| 7552 | } |
| 7553 | return buffer_replace2(buf, prev, next, NULL, 0); |
| 7554 | } |
| 7555 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7556 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 7557 | * 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] | 7558 | * desirable to call it only when needed. This code is quite complex because |
| 7559 | * of the multiple very crappy and ambiguous syntaxes we have to support. it |
| 7560 | * highly recommended not to touch this part without a good reason ! |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7561 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7562 | void manage_client_side_cookies(struct stream *s, struct channel *req) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7563 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7564 | struct http_txn *txn = s->txn; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7565 | struct session *sess = s->sess; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7566 | int preserve_hdr; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7567 | int cur_idx, old_idx; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7568 | char *hdr_beg, *hdr_end, *hdr_next, *del_from; |
| 7569 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7570 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7571 | /* Iterate through the headers, we start with the start line. */ |
Willy Tarreau | 83969f4 | 2007-01-22 08:55:47 +0100 | [diff] [blame] | 7572 | old_idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7573 | hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7574 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7575 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7576 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7577 | int val; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7578 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7579 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7580 | hdr_beg = hdr_next; |
| 7581 | hdr_end = hdr_beg + cur_hdr->len; |
| 7582 | hdr_next = hdr_end + cur_hdr->cr + 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7583 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7584 | /* We have one full header between hdr_beg and hdr_end, and the |
| 7585 | * next header starts at hdr_next. We're only interested in |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7586 | * "Cookie:" headers. |
| 7587 | */ |
| 7588 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7589 | val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6); |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7590 | if (!val) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7591 | old_idx = cur_idx; |
| 7592 | continue; |
| 7593 | } |
| 7594 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7595 | del_from = NULL; /* nothing to be deleted */ |
| 7596 | preserve_hdr = 0; /* assume we may kill the whole header */ |
| 7597 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7598 | /* Now look for cookies. Conforming to RFC2109, we have to support |
| 7599 | * attributes whose name begin with a '$', and associate them with |
| 7600 | * the right cookie, if we want to delete this cookie. |
| 7601 | * So there are 3 cases for each cookie read : |
| 7602 | * 1) it's a special attribute, beginning with a '$' : ignore it. |
| 7603 | * 2) it's a server id cookie that we *MAY* want to delete : save |
| 7604 | * some pointers on it (last semi-colon, beginning of cookie...) |
| 7605 | * 3) it's an application cookie : we *MAY* have to delete a previous |
| 7606 | * "special" cookie. |
| 7607 | * At the end of loop, if a "special" cookie remains, we may have to |
| 7608 | * remove it. If no application cookie persists in the header, we |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7609 | * *MUST* delete it. |
| 7610 | * |
| 7611 | * Note: RFC2965 is unclear about the processing of spaces around |
| 7612 | * the equal sign in the ATTR=VALUE form. A careful inspection of |
| 7613 | * the RFC explicitly allows spaces before it, and not within the |
| 7614 | * tokens (attrs or values). An inspection of RFC2109 allows that |
| 7615 | * too but section 10.1.3 lets one think that spaces may be allowed |
| 7616 | * after the equal sign too, resulting in some (rare) buggy |
| 7617 | * implementations trying to do that. So let's do what servers do. |
| 7618 | * Latest ietf draft forbids spaces all around. Also, earlier RFCs |
| 7619 | * allowed quoted strings in values, with any possible character |
| 7620 | * after a backslash, including control chars and delimitors, which |
| 7621 | * causes parsing to become ambiguous. Browsers also allow spaces |
| 7622 | * within values even without quotes. |
| 7623 | * |
| 7624 | * We have to keep multiple pointers in order to support cookie |
| 7625 | * removal at the beginning, middle or end of header without |
| 7626 | * corrupting the header. All of these headers are valid : |
| 7627 | * |
| 7628 | * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n |
| 7629 | * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n |
| 7630 | * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n |
| 7631 | * | | | | | | | | | |
| 7632 | * | | | | | | | | hdr_end <--+ |
| 7633 | * | | | | | | | +--> next |
| 7634 | * | | | | | | +----> val_end |
| 7635 | * | | | | | +-----------> val_beg |
| 7636 | * | | | | +--------------> equal |
| 7637 | * | | | +----------------> att_end |
| 7638 | * | | +---------------------> att_beg |
| 7639 | * | +--------------------------> prev |
| 7640 | * +--------------------------------> hdr_beg |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7641 | */ |
| 7642 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7643 | for (prev = hdr_beg + 6; prev < hdr_end; prev = next) { |
| 7644 | /* Iterate through all cookies on this line */ |
| 7645 | |
| 7646 | /* find att_beg */ |
| 7647 | att_beg = prev + 1; |
| 7648 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 7649 | att_beg++; |
| 7650 | |
| 7651 | /* find att_end : this is the first character after the last non |
| 7652 | * space before the equal. It may be equal to hdr_end. |
| 7653 | */ |
| 7654 | equal = att_end = att_beg; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7655 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7656 | while (equal < hdr_end) { |
| 7657 | if (*equal == '=' || *equal == ',' || *equal == ';') |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7658 | break; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7659 | if (http_is_spht[(unsigned char)*equal++]) |
| 7660 | continue; |
| 7661 | att_end = equal; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7662 | } |
| 7663 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7664 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 7665 | * is between <att_beg> and <equal>, both may be identical. |
| 7666 | */ |
| 7667 | |
| 7668 | /* look for end of cookie if there is an equal sign */ |
| 7669 | if (equal < hdr_end && *equal == '=') { |
| 7670 | /* look for the beginning of the value */ |
| 7671 | val_beg = equal + 1; |
| 7672 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 7673 | val_beg++; |
| 7674 | |
| 7675 | /* find the end of the value, respecting quotes */ |
| 7676 | next = find_cookie_value_end(val_beg, hdr_end); |
| 7677 | |
| 7678 | /* make val_end point to the first white space or delimitor after the value */ |
| 7679 | val_end = next; |
| 7680 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 7681 | val_end--; |
| 7682 | } else { |
| 7683 | val_beg = val_end = next = equal; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 7684 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7685 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7686 | /* We have nothing to do with attributes beginning with '$'. However, |
| 7687 | * they will automatically be removed if a header before them is removed, |
| 7688 | * since they're supposed to be linked together. |
| 7689 | */ |
| 7690 | if (*att_beg == '$') |
| 7691 | continue; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7692 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7693 | /* Ignore cookies with no equal sign */ |
| 7694 | if (equal == next) { |
| 7695 | /* This is not our cookie, so we must preserve it. But if we already |
| 7696 | * scheduled another cookie for removal, we cannot remove the |
| 7697 | * complete header, but we can remove the previous block itself. |
| 7698 | */ |
| 7699 | preserve_hdr = 1; |
| 7700 | if (del_from != NULL) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7701 | int delta = del_hdr_value(req->buf, &del_from, prev); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7702 | val_end += delta; |
| 7703 | next += delta; |
| 7704 | hdr_end += delta; |
| 7705 | hdr_next += delta; |
| 7706 | cur_hdr->len += delta; |
| 7707 | http_msg_move_end(&txn->req, delta); |
| 7708 | prev = del_from; |
| 7709 | del_from = NULL; |
| 7710 | } |
| 7711 | continue; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 7712 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7713 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7714 | /* if there are spaces around the equal sign, we need to |
| 7715 | * strip them otherwise we'll get trouble for cookie captures, |
| 7716 | * or even for rewrites. Since this happens extremely rarely, |
| 7717 | * it does not hurt performance. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7718 | */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7719 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 7720 | int stripped_before = 0; |
| 7721 | int stripped_after = 0; |
| 7722 | |
| 7723 | if (att_end != equal) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7724 | stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7725 | equal += stripped_before; |
| 7726 | val_beg += stripped_before; |
| 7727 | } |
| 7728 | |
| 7729 | if (val_beg > equal + 1) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7730 | stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7731 | val_beg += stripped_after; |
| 7732 | stripped_before += stripped_after; |
| 7733 | } |
| 7734 | |
| 7735 | val_end += stripped_before; |
| 7736 | next += stripped_before; |
| 7737 | hdr_end += stripped_before; |
| 7738 | hdr_next += stripped_before; |
| 7739 | cur_hdr->len += stripped_before; |
| 7740 | http_msg_move_end(&txn->req, stripped_before); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7741 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7742 | /* now everything is as on the diagram above */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7743 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7744 | /* First, let's see if we want to capture this cookie. We check |
| 7745 | * that we don't already have a client side cookie, because we |
| 7746 | * can only capture one. Also as an optimisation, we ignore |
| 7747 | * cookies shorter than the declared name. |
| 7748 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7749 | if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL && |
| 7750 | (val_end - att_beg >= sess->fe->capture_namelen) && |
| 7751 | memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7752 | int log_len = val_end - att_beg; |
| 7753 | |
| 7754 | if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) { |
| 7755 | Alert("HTTP logging : out of memory.\n"); |
| 7756 | } else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7757 | if (log_len > sess->fe->capture_len) |
| 7758 | log_len = sess->fe->capture_len; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7759 | memcpy(txn->cli_cookie, att_beg, log_len); |
| 7760 | txn->cli_cookie[log_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7761 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7762 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7763 | |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7764 | /* Persistence cookies in passive, rewrite or insert mode have the |
| 7765 | * following form : |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7766 | * |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7767 | * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]] |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7768 | * |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7769 | * For cookies in prefix mode, the form is : |
| 7770 | * |
| 7771 | * Cookie: NAME=SRV~VALUE |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7772 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7773 | if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) && |
| 7774 | (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) { |
| 7775 | struct server *srv = s->be->srv; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7776 | char *delim; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7777 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7778 | /* if we're in cookie prefix mode, we'll search the delimitor so that we |
| 7779 | * have the server ID between val_beg and delim, and the original cookie between |
| 7780 | * delim+1 and val_end. Otherwise, delim==val_end : |
| 7781 | * |
| 7782 | * Cookie: NAME=SRV; # in all but prefix modes |
| 7783 | * Cookie: NAME=SRV~OPAQUE ; # in prefix mode |
| 7784 | * | || || | |+-> next |
| 7785 | * | || || | +--> val_end |
| 7786 | * | || || +---------> delim |
| 7787 | * | || |+------------> val_beg |
| 7788 | * | || +-------------> att_end = equal |
| 7789 | * | |+-----------------> att_beg |
| 7790 | * | +------------------> prev |
| 7791 | * +-------------------------> hdr_beg |
| 7792 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7793 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7794 | if (s->be->ck_opts & PR_CK_PFX) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7795 | for (delim = val_beg; delim < val_end; delim++) |
| 7796 | if (*delim == COOKIE_DELIM) |
| 7797 | break; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7798 | } else { |
| 7799 | char *vbar1; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7800 | delim = val_end; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7801 | /* Now check if the cookie contains a date field, which would |
| 7802 | * appear after a vertical bar ('|') just after the server name |
| 7803 | * and before the delimiter. |
| 7804 | */ |
| 7805 | vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg); |
| 7806 | if (vbar1) { |
| 7807 | /* OK, so left of the bar is the server's cookie and |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7808 | * right is the last seen date. It is a base64 encoded |
| 7809 | * 30-bit value representing the UNIX date since the |
| 7810 | * epoch in 4-second quantities. |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7811 | */ |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7812 | int val; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7813 | delim = vbar1++; |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7814 | if (val_end - vbar1 >= 5) { |
| 7815 | val = b64tos30(vbar1); |
| 7816 | if (val > 0) |
| 7817 | txn->cookie_last_date = val << 2; |
| 7818 | } |
| 7819 | /* look for a second vertical bar */ |
| 7820 | vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1); |
| 7821 | if (vbar1 && (val_end - vbar1 > 5)) { |
| 7822 | val = b64tos30(vbar1 + 1); |
| 7823 | if (val > 0) |
| 7824 | txn->cookie_first_date = val << 2; |
| 7825 | } |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 7826 | } |
| 7827 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7828 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7829 | /* if the cookie has an expiration date and the proxy wants to check |
| 7830 | * it, then we do that now. We first check if the cookie is too old, |
| 7831 | * then only if it has expired. We detect strict overflow because the |
| 7832 | * time resolution here is not great (4 seconds). Cookies with dates |
| 7833 | * in the future are ignored if their offset is beyond one day. This |
| 7834 | * allows an admin to fix timezone issues without expiring everyone |
| 7835 | * and at the same time avoids keeping unwanted side effects for too |
| 7836 | * long. |
| 7837 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7838 | if (txn->cookie_first_date && s->be->cookie_maxlife && |
| 7839 | (((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] | 7840 | ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) { |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7841 | txn->flags &= ~TX_CK_MASK; |
| 7842 | txn->flags |= TX_CK_OLD; |
| 7843 | delim = val_beg; // let's pretend we have not found the cookie |
| 7844 | txn->cookie_first_date = 0; |
| 7845 | txn->cookie_last_date = 0; |
| 7846 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7847 | else if (txn->cookie_last_date && s->be->cookie_maxidle && |
| 7848 | (((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] | 7849 | ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) { |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7850 | txn->flags &= ~TX_CK_MASK; |
| 7851 | txn->flags |= TX_CK_EXPIRED; |
| 7852 | delim = val_beg; // let's pretend we have not found the cookie |
| 7853 | txn->cookie_first_date = 0; |
| 7854 | txn->cookie_last_date = 0; |
| 7855 | } |
| 7856 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7857 | /* Here, we'll look for the first running server which supports the cookie. |
| 7858 | * This allows to share a same cookie between several servers, for example |
| 7859 | * to dedicate backup servers to specific servers only. |
| 7860 | * However, to prevent clients from sticking to cookie-less backup server |
| 7861 | * when they have incidentely learned an empty cookie, we simply ignore |
| 7862 | * empty cookies and mark them as invalid. |
| 7863 | * The same behaviour is applied when persistence must be ignored. |
| 7864 | */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 7865 | if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED))) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7866 | srv = NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7867 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7868 | while (srv) { |
| 7869 | if (srv->cookie && (srv->cklen == delim - val_beg) && |
| 7870 | !memcmp(val_beg, srv->cookie, delim - val_beg)) { |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 7871 | if ((srv->state != SRV_ST_STOPPED) || |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7872 | (s->be->options & PR_O_PERSIST) || |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 7873 | (s->flags & SF_FORCE_PRST)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7874 | /* we found the server and we can use it */ |
| 7875 | txn->flags &= ~TX_CK_MASK; |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 7876 | txn->flags |= (srv->state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 7877 | s->flags |= SF_DIRECT | SF_ASSIGNED; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7878 | s->target = &srv->obj_type; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7879 | break; |
| 7880 | } else { |
| 7881 | /* we found a server, but it's down, |
| 7882 | * mark it as such and go on in case |
| 7883 | * another one is available. |
| 7884 | */ |
| 7885 | txn->flags &= ~TX_CK_MASK; |
| 7886 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7887 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7888 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7889 | srv = srv->next; |
| 7890 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7891 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7892 | 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] | 7893 | /* no server matched this cookie or we deliberately skipped it */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7894 | txn->flags &= ~TX_CK_MASK; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 7895 | if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED))) |
Willy Tarreau | c89ccb6 | 2012-04-05 21:18:22 +0200 | [diff] [blame] | 7896 | txn->flags |= TX_CK_UNUSED; |
| 7897 | else |
| 7898 | txn->flags |= TX_CK_INVALID; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7899 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7900 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7901 | /* depending on the cookie mode, we may have to either : |
| 7902 | * - delete the complete cookie if we're in insert+indirect mode, so that |
| 7903 | * the server never sees it ; |
| 7904 | * - remove the server id from the cookie value, and tag the cookie as an |
| 7905 | * application cookie so that it does not get accidentely removed later, |
| 7906 | * if we're in cookie prefix mode |
| 7907 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7908 | if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7909 | int delta; /* negative */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7910 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7911 | delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7912 | val_end += delta; |
| 7913 | next += delta; |
| 7914 | hdr_end += delta; |
| 7915 | hdr_next += delta; |
| 7916 | cur_hdr->len += delta; |
| 7917 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7918 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7919 | del_from = NULL; |
| 7920 | preserve_hdr = 1; /* we want to keep this cookie */ |
| 7921 | } |
| 7922 | else if (del_from == NULL && |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7923 | (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] | 7924 | del_from = prev; |
| 7925 | } |
| 7926 | } else { |
| 7927 | /* This is not our cookie, so we must preserve it. But if we already |
| 7928 | * scheduled another cookie for removal, we cannot remove the |
| 7929 | * complete header, but we can remove the previous block itself. |
| 7930 | */ |
| 7931 | preserve_hdr = 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7932 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7933 | if (del_from != NULL) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7934 | int delta = del_hdr_value(req->buf, &del_from, prev); |
Willy Tarreau | b810554 | 2010-11-24 18:31:28 +0100 | [diff] [blame] | 7935 | if (att_beg >= del_from) |
| 7936 | att_beg += delta; |
| 7937 | if (att_end >= del_from) |
| 7938 | att_end += delta; |
| 7939 | val_beg += delta; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7940 | val_end += delta; |
| 7941 | next += delta; |
| 7942 | hdr_end += delta; |
| 7943 | hdr_next += delta; |
| 7944 | cur_hdr->len += delta; |
| 7945 | http_msg_move_end(&txn->req, delta); |
| 7946 | prev = del_from; |
| 7947 | del_from = NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7948 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7949 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7950 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7951 | /* continue with next cookie on this header line */ |
| 7952 | att_beg = next; |
| 7953 | } /* for each cookie */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7954 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7955 | /* There are no more cookies on this line. |
| 7956 | * We may still have one (or several) marked for deletion at the |
| 7957 | * end of the line. We must do this now in two ways : |
| 7958 | * - if some cookies must be preserved, we only delete from the |
| 7959 | * mark to the end of line ; |
| 7960 | * - if nothing needs to be preserved, simply delete the whole header |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7961 | */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7962 | if (del_from) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7963 | int delta; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7964 | if (preserve_hdr) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7965 | delta = del_hdr_value(req->buf, &del_from, hdr_end); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7966 | hdr_end = del_from; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7967 | cur_hdr->len += delta; |
| 7968 | } else { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7969 | delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7970 | |
| 7971 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7972 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 7973 | txn->hdr_idx.used--; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7974 | cur_hdr->len = 0; |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 7975 | cur_idx = old_idx; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7976 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7977 | hdr_next += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7978 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7979 | } |
| 7980 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7981 | /* check next header */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7982 | old_idx = cur_idx; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 7983 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7984 | } |
| 7985 | |
| 7986 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7987 | /* Iterate the same filter through all response headers contained in <rtr>. |
| 7988 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
| 7989 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7990 | 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] | 7991 | { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7992 | char *cur_ptr, *cur_end, *cur_next; |
| 7993 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7994 | struct http_txn *txn = s->txn; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7995 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7996 | int delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7997 | |
| 7998 | last_hdr = 0; |
| 7999 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8000 | cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8001 | old_idx = 0; |
| 8002 | |
| 8003 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8004 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8005 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8006 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8007 | (exp->action == ACT_ALLOW || |
| 8008 | exp->action == ACT_DENY)) |
| 8009 | return 0; |
| 8010 | |
| 8011 | cur_idx = txn->hdr_idx.v[old_idx].next; |
| 8012 | if (!cur_idx) |
| 8013 | break; |
| 8014 | |
| 8015 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 8016 | cur_ptr = cur_next; |
| 8017 | cur_end = cur_ptr + cur_hdr->len; |
| 8018 | cur_next = cur_end + cur_hdr->cr + 1; |
| 8019 | |
| 8020 | /* Now we have one header between cur_ptr and cur_end, |
| 8021 | * and the next header starts at cur_next. |
| 8022 | */ |
| 8023 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 8024 | 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] | 8025 | switch (exp->action) { |
| 8026 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8027 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8028 | last_hdr = 1; |
| 8029 | break; |
| 8030 | |
| 8031 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8032 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8033 | last_hdr = 1; |
| 8034 | break; |
| 8035 | |
| 8036 | case ACT_REPLACE: |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 8037 | trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch); |
| 8038 | if (trash.len < 0) |
| 8039 | return -1; |
| 8040 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 8041 | 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] | 8042 | /* FIXME: if the user adds a newline in the replacement, the |
| 8043 | * index will not be recalculated for now, and the new line |
| 8044 | * will not be counted as a new header. |
| 8045 | */ |
| 8046 | |
| 8047 | cur_end += delta; |
| 8048 | cur_next += delta; |
| 8049 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 8050 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8051 | break; |
| 8052 | |
| 8053 | case ACT_REMOVE: |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8054 | delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8055 | cur_next += delta; |
| 8056 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 8057 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8058 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 8059 | txn->hdr_idx.used--; |
| 8060 | cur_hdr->len = 0; |
| 8061 | cur_end = NULL; /* null-term has been rewritten */ |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 8062 | cur_idx = old_idx; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8063 | break; |
| 8064 | |
| 8065 | } |
| 8066 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8067 | |
| 8068 | /* keep the link from this header to next one in case of later |
| 8069 | * removal of next header. |
| 8070 | */ |
| 8071 | old_idx = cur_idx; |
| 8072 | } |
| 8073 | return 0; |
| 8074 | } |
| 8075 | |
| 8076 | |
| 8077 | /* Apply the filter to the status line in the response buffer <rtr>. |
| 8078 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 8079 | * or -1 if a replacement resulted in an invalid status line. |
| 8080 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8081 | 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] | 8082 | { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8083 | char *cur_ptr, *cur_end; |
| 8084 | int done; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8085 | struct http_txn *txn = s->txn; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 8086 | int delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8087 | |
| 8088 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8089 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8090 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8091 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8092 | (exp->action == ACT_ALLOW || |
| 8093 | exp->action == ACT_DENY)) |
| 8094 | return 0; |
| 8095 | else if (exp->action == ACT_REMOVE) |
| 8096 | return 0; |
| 8097 | |
| 8098 | done = 0; |
| 8099 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8100 | cur_ptr = rtr->buf->p; |
Willy Tarreau | 1ba0e5f | 2010-06-07 13:57:32 +0200 | [diff] [blame] | 8101 | cur_end = cur_ptr + txn->rsp.sl.st.l; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8102 | |
| 8103 | /* Now we have the status line between cur_ptr and cur_end */ |
| 8104 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 8105 | 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] | 8106 | switch (exp->action) { |
| 8107 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8108 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8109 | done = 1; |
| 8110 | break; |
| 8111 | |
| 8112 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8113 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8114 | done = 1; |
| 8115 | break; |
| 8116 | |
| 8117 | case ACT_REPLACE: |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 8118 | trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch); |
| 8119 | if (trash.len < 0) |
| 8120 | return -1; |
| 8121 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 8122 | 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] | 8123 | /* FIXME: if the user adds a newline in the replacement, the |
| 8124 | * index will not be recalculated for now, and the new line |
| 8125 | * will not be counted as a new header. |
| 8126 | */ |
| 8127 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 8128 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8129 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8130 | cur_end = (char *)http_parse_stsline(&txn->rsp, |
Willy Tarreau | 0278576 | 2007-04-03 14:45:44 +0200 | [diff] [blame] | 8131 | HTTP_MSG_RPVER, |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8132 | cur_ptr, cur_end + 1, |
| 8133 | NULL, NULL); |
| 8134 | if (unlikely(!cur_end)) |
| 8135 | return -1; |
| 8136 | |
| 8137 | /* we have a full respnse and we know that we have either a CR |
| 8138 | * or an LF at <ptr>. |
| 8139 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8140 | 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] | 8141 | 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] | 8142 | /* there is no point trying this regex on headers */ |
| 8143 | return 1; |
| 8144 | } |
| 8145 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8146 | return done; |
| 8147 | } |
| 8148 | |
| 8149 | |
| 8150 | |
| 8151 | /* |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8152 | * 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] | 8153 | * Returns 0 if everything is alright, or -1 in case a replacement lead to an |
| 8154 | * unparsable response. |
| 8155 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8156 | 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] | 8157 | { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 8158 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8159 | struct http_txn *txn = s->txn; |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 8160 | struct hdr_exp *exp; |
| 8161 | |
| 8162 | for (exp = px->rsp_exp; exp; exp = exp->next) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8163 | int ret; |
| 8164 | |
| 8165 | /* |
| 8166 | * The interleaving of transformations and verdicts |
| 8167 | * makes it difficult to decide to continue or stop |
| 8168 | * the evaluation. |
| 8169 | */ |
| 8170 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 8171 | if (txn->flags & TX_SVDENY) |
| 8172 | break; |
| 8173 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8174 | if ((txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8175 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 8176 | exp->action == ACT_PASS)) { |
| 8177 | exp = exp->next; |
| 8178 | continue; |
| 8179 | } |
| 8180 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 8181 | /* if this filter had a condition, evaluate it now and skip to |
| 8182 | * next filter if the condition does not match. |
| 8183 | */ |
| 8184 | if (exp->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 8185 | 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] | 8186 | ret = acl_pass(ret); |
| 8187 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 8188 | ret = !ret; |
| 8189 | if (!ret) |
| 8190 | continue; |
| 8191 | } |
| 8192 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8193 | /* Apply the filter to the status line. */ |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 8194 | ret = apply_filter_to_sts_line(s, rtr, exp); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8195 | if (unlikely(ret < 0)) |
| 8196 | return -1; |
| 8197 | |
| 8198 | if (likely(ret == 0)) { |
| 8199 | /* The filter did not match the response, it can be |
| 8200 | * iterated through all headers. |
| 8201 | */ |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 8202 | if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0)) |
| 8203 | return -1; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8204 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8205 | } |
| 8206 | return 0; |
| 8207 | } |
| 8208 | |
| 8209 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8210 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 8211 | * 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] | 8212 | * desirable to call it only when needed. This function is also used when we |
| 8213 | * 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] | 8214 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8215 | void manage_server_side_cookies(struct stream *s, struct channel *res) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8216 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8217 | struct http_txn *txn = s->txn; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 8218 | struct session *sess = s->sess; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 8219 | struct server *srv; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8220 | int is_cookie2; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8221 | int cur_idx, old_idx, delta; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8222 | char *hdr_beg, *hdr_end, *hdr_next; |
| 8223 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8224 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8225 | /* Iterate through the headers. |
| 8226 | * we start with the start line. |
| 8227 | */ |
| 8228 | old_idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8229 | hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8230 | |
| 8231 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 8232 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8233 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8234 | |
| 8235 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8236 | hdr_beg = hdr_next; |
| 8237 | hdr_end = hdr_beg + cur_hdr->len; |
| 8238 | hdr_next = hdr_end + cur_hdr->cr + 1; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8239 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8240 | /* We have one full header between hdr_beg and hdr_end, and the |
| 8241 | * next header starts at hdr_next. We're only interested in |
| 8242 | * "Set-Cookie" and "Set-Cookie2" headers. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8243 | */ |
| 8244 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8245 | is_cookie2 = 0; |
| 8246 | prev = hdr_beg + 10; |
| 8247 | val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10); |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8248 | if (!val) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8249 | val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11); |
| 8250 | if (!val) { |
| 8251 | old_idx = cur_idx; |
| 8252 | continue; |
| 8253 | } |
| 8254 | is_cookie2 = 1; |
| 8255 | prev = hdr_beg + 11; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8256 | } |
| 8257 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8258 | /* OK, right now we know we have a Set-Cookie* at hdr_beg, and |
| 8259 | * <prev> points to the colon. |
| 8260 | */ |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 8261 | txn->flags |= TX_SCK_PRESENT; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8262 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8263 | /* Maybe we only wanted to see if there was a Set-Cookie (eg: |
| 8264 | * check-cache is enabled) and we are not interested in checking |
| 8265 | * them. Warning, the cookie capture is declared in the frontend. |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 8266 | */ |
Willy Tarreau | 53a09d5 | 2015-08-10 18:59:40 +0200 | [diff] [blame] | 8267 | if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8268 | return; |
| 8269 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8270 | /* OK so now we know we have to process this response cookie. |
| 8271 | * The format of the Set-Cookie header is slightly different |
| 8272 | * from the format of the Cookie header in that it does not |
| 8273 | * support the comma as a cookie delimiter (thus the header |
| 8274 | * cannot be folded) because the Expires attribute described in |
| 8275 | * the original Netscape's spec may contain an unquoted date |
| 8276 | * with a comma inside. We have to live with this because |
| 8277 | * many browsers don't support Max-Age and some browsers don't |
| 8278 | * support quoted strings. However the Set-Cookie2 header is |
| 8279 | * clean. |
| 8280 | * |
| 8281 | * We have to keep multiple pointers in order to support cookie |
| 8282 | * removal at the beginning, middle or end of header without |
| 8283 | * corrupting the header (in case of set-cookie2). A special |
| 8284 | * pointer, <scav> points to the beginning of the set-cookie-av |
| 8285 | * fields after the first semi-colon. The <next> pointer points |
| 8286 | * either to the end of line (set-cookie) or next unquoted comma |
| 8287 | * (set-cookie2). All of these headers are valid : |
| 8288 | * |
| 8289 | * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n |
| 8290 | * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n |
| 8291 | * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n |
| 8292 | * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n |
| 8293 | * | | | | | | | | | | |
| 8294 | * | | | | | | | | +-> next hdr_end <--+ |
| 8295 | * | | | | | | | +------------> scav |
| 8296 | * | | | | | | +--------------> val_end |
| 8297 | * | | | | | +--------------------> val_beg |
| 8298 | * | | | | +----------------------> equal |
| 8299 | * | | | +------------------------> att_end |
| 8300 | * | | +----------------------------> att_beg |
| 8301 | * | +------------------------------> prev |
| 8302 | * +-----------------------------------------> hdr_beg |
| 8303 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8304 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8305 | for (; prev < hdr_end; prev = next) { |
| 8306 | /* Iterate through all cookies on this line */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8307 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8308 | /* find att_beg */ |
| 8309 | att_beg = prev + 1; |
| 8310 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 8311 | att_beg++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8312 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8313 | /* find att_end : this is the first character after the last non |
| 8314 | * space before the equal. It may be equal to hdr_end. |
| 8315 | */ |
| 8316 | equal = att_end = att_beg; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8317 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8318 | while (equal < hdr_end) { |
| 8319 | if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ',')) |
| 8320 | break; |
| 8321 | if (http_is_spht[(unsigned char)*equal++]) |
| 8322 | continue; |
| 8323 | att_end = equal; |
| 8324 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8325 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8326 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 8327 | * is between <att_beg> and <equal>, both may be identical. |
| 8328 | */ |
| 8329 | |
| 8330 | /* look for end of cookie if there is an equal sign */ |
| 8331 | if (equal < hdr_end && *equal == '=') { |
| 8332 | /* look for the beginning of the value */ |
| 8333 | val_beg = equal + 1; |
| 8334 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 8335 | val_beg++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8336 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8337 | /* find the end of the value, respecting quotes */ |
| 8338 | next = find_cookie_value_end(val_beg, hdr_end); |
| 8339 | |
| 8340 | /* make val_end point to the first white space or delimitor after the value */ |
| 8341 | val_end = next; |
| 8342 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 8343 | val_end--; |
| 8344 | } else { |
| 8345 | /* <equal> points to next comma, semi-colon or EOL */ |
| 8346 | val_beg = val_end = next = equal; |
| 8347 | } |
| 8348 | |
| 8349 | if (next < hdr_end) { |
| 8350 | /* Set-Cookie2 supports multiple cookies, and <next> points to |
| 8351 | * a colon or semi-colon before the end. So skip all attr-value |
| 8352 | * pairs and look for the next comma. For Set-Cookie, since |
| 8353 | * commas are permitted in values, skip to the end. |
| 8354 | */ |
| 8355 | if (is_cookie2) |
| 8356 | next = find_hdr_value_end(next, hdr_end); |
| 8357 | else |
| 8358 | next = hdr_end; |
| 8359 | } |
| 8360 | |
| 8361 | /* Now everything is as on the diagram above */ |
| 8362 | |
| 8363 | /* Ignore cookies with no equal sign */ |
| 8364 | if (equal == val_end) |
| 8365 | continue; |
| 8366 | |
| 8367 | /* If there are spaces around the equal sign, we need to |
| 8368 | * strip them otherwise we'll get trouble for cookie captures, |
| 8369 | * or even for rewrites. Since this happens extremely rarely, |
| 8370 | * it does not hurt performance. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8371 | */ |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8372 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 8373 | int stripped_before = 0; |
| 8374 | int stripped_after = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8375 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8376 | if (att_end != equal) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8377 | stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8378 | equal += stripped_before; |
| 8379 | val_beg += stripped_before; |
| 8380 | } |
| 8381 | |
| 8382 | if (val_beg > equal + 1) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8383 | stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8384 | val_beg += stripped_after; |
| 8385 | stripped_before += stripped_after; |
| 8386 | } |
| 8387 | |
| 8388 | val_end += stripped_before; |
| 8389 | next += stripped_before; |
| 8390 | hdr_end += stripped_before; |
| 8391 | hdr_next += stripped_before; |
| 8392 | cur_hdr->len += stripped_before; |
Willy Tarreau | 1fc1f45 | 2011-04-07 22:35:37 +0200 | [diff] [blame] | 8393 | http_msg_move_end(&txn->rsp, stripped_before); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8394 | } |
| 8395 | |
| 8396 | /* First, let's see if we want to capture this cookie. We check |
| 8397 | * that we don't already have a server side cookie, because we |
| 8398 | * can only capture one. Also as an optimisation, we ignore |
| 8399 | * cookies shorter than the declared name. |
| 8400 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 8401 | if (sess->fe->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 8402 | txn->srv_cookie == NULL && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 8403 | (val_end - att_beg >= sess->fe->capture_namelen) && |
| 8404 | memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8405 | int log_len = val_end - att_beg; |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 8406 | if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8407 | Alert("HTTP logging : out of memory.\n"); |
| 8408 | } |
Willy Tarreau | f70fc75 | 2010-11-19 11:27:18 +0100 | [diff] [blame] | 8409 | else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 8410 | if (log_len > sess->fe->capture_len) |
| 8411 | log_len = sess->fe->capture_len; |
Willy Tarreau | f70fc75 | 2010-11-19 11:27:18 +0100 | [diff] [blame] | 8412 | memcpy(txn->srv_cookie, att_beg, log_len); |
| 8413 | txn->srv_cookie[log_len] = 0; |
| 8414 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8415 | } |
| 8416 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8417 | srv = objt_server(s->target); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8418 | /* now check if we need to process it for persistence */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 8419 | if (!(s->flags & SF_IGNORE_PRST) && |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8420 | (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) && |
| 8421 | (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) { |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 8422 | /* assume passive cookie by default */ |
| 8423 | txn->flags &= ~TX_SCK_MASK; |
| 8424 | txn->flags |= TX_SCK_FOUND; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8425 | |
| 8426 | /* If the cookie is in insert mode on a known server, we'll delete |
| 8427 | * this occurrence because we'll insert another one later. |
| 8428 | * 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] | 8429 | * a direct access. |
| 8430 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8431 | if (s->be->ck_opts & PR_CK_PSV) { |
Willy Tarreau | ba4c5be | 2010-10-23 12:46:42 +0200 | [diff] [blame] | 8432 | /* The "preserve" flag was set, we don't want to touch the |
| 8433 | * server's cookie. |
| 8434 | */ |
| 8435 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8436 | else if ((srv && (s->be->ck_opts & PR_CK_INS)) || |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 8437 | ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8438 | /* this cookie must be deleted */ |
| 8439 | if (*prev == ':' && next == hdr_end) { |
| 8440 | /* whole header */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8441 | delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8442 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 8443 | txn->hdr_idx.used--; |
| 8444 | cur_hdr->len = 0; |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 8445 | cur_idx = old_idx; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8446 | hdr_next += delta; |
| 8447 | http_msg_move_end(&txn->rsp, delta); |
| 8448 | /* note: while both invalid now, <next> and <hdr_end> |
| 8449 | * are still equal, so the for() will stop as expected. |
| 8450 | */ |
| 8451 | } else { |
| 8452 | /* just remove the value */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8453 | int delta = del_hdr_value(res->buf, &prev, next); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8454 | next = prev; |
| 8455 | hdr_end += delta; |
| 8456 | hdr_next += delta; |
| 8457 | cur_hdr->len += delta; |
| 8458 | http_msg_move_end(&txn->rsp, delta); |
| 8459 | } |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 8460 | txn->flags &= ~TX_SCK_MASK; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8461 | txn->flags |= TX_SCK_DELETED; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8462 | /* and go on with next cookie */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8463 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8464 | else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8465 | /* replace bytes val_beg->val_end with the cookie name associated |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8466 | * with this server since we know it. |
| 8467 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8468 | 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] | 8469 | next += delta; |
| 8470 | hdr_end += delta; |
| 8471 | hdr_next += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8472 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 8473 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8474 | |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 8475 | txn->flags &= ~TX_SCK_MASK; |
| 8476 | txn->flags |= TX_SCK_REPLACED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8477 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8478 | else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8479 | /* insert the cookie name associated with this server |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8480 | * before existing cookie, and insert a delimiter between them.. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8481 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8482 | 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] | 8483 | next += delta; |
| 8484 | hdr_end += delta; |
| 8485 | hdr_next += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8486 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 8487 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8488 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 8489 | val_beg[srv->cklen] = COOKIE_DELIM; |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 8490 | txn->flags &= ~TX_SCK_MASK; |
| 8491 | txn->flags |= TX_SCK_REPLACED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8492 | } |
| 8493 | } |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8494 | /* that's done for this cookie, check the next one on the same |
| 8495 | * line when next != hdr_end (only if is_cookie2). |
| 8496 | */ |
| 8497 | } |
| 8498 | /* check next header */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8499 | old_idx = cur_idx; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 8500 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8501 | } |
| 8502 | |
| 8503 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8504 | /* |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8505 | * Check if response is cacheable or not. Updates s->flags. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8506 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8507 | void check_response_for_cacheability(struct stream *s, struct channel *rtr) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8508 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8509 | struct http_txn *txn = s->txn; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8510 | char *p1, *p2; |
| 8511 | |
| 8512 | char *cur_ptr, *cur_end, *cur_next; |
| 8513 | int cur_idx; |
| 8514 | |
Willy Tarreau | 5df5187 | 2007-11-25 16:20:08 +0100 | [diff] [blame] | 8515 | if (!(txn->flags & TX_CACHEABLE)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8516 | return; |
| 8517 | |
| 8518 | /* Iterate through the headers. |
| 8519 | * we start with the start line. |
| 8520 | */ |
| 8521 | cur_idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8522 | cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8523 | |
| 8524 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 8525 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8526 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8527 | |
| 8528 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 8529 | cur_ptr = cur_next; |
| 8530 | cur_end = cur_ptr + cur_hdr->len; |
| 8531 | cur_next = cur_end + cur_hdr->cr + 1; |
| 8532 | |
| 8533 | /* We have one full header between cur_ptr and cur_end, and the |
| 8534 | * next header starts at cur_next. We're only interested in |
| 8535 | * "Cookie:" headers. |
| 8536 | */ |
| 8537 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8538 | val = http_header_match2(cur_ptr, cur_end, "Pragma", 6); |
| 8539 | if (val) { |
| 8540 | if ((cur_end - (cur_ptr + val) >= 8) && |
| 8541 | strncasecmp(cur_ptr + val, "no-cache", 8) == 0) { |
| 8542 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 8543 | return; |
| 8544 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8545 | } |
| 8546 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8547 | val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13); |
| 8548 | if (!val) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8549 | continue; |
| 8550 | |
| 8551 | /* OK, right now we know we have a cache-control header at cur_ptr */ |
| 8552 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 8553 | p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8554 | |
| 8555 | if (p1 >= cur_end) /* no more info */ |
| 8556 | continue; |
| 8557 | |
| 8558 | /* p1 is at the beginning of the value */ |
| 8559 | p2 = p1; |
| 8560 | |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 8561 | while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8562 | p2++; |
| 8563 | |
| 8564 | /* we have a complete value between p1 and p2 */ |
| 8565 | if (p2 < cur_end && *p2 == '=') { |
| 8566 | /* we have something of the form no-cache="set-cookie" */ |
| 8567 | if ((cur_end - p1 >= 21) && |
| 8568 | strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0 |
| 8569 | && (p1[20] == '"' || p1[20] == ',')) |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8570 | txn->flags &= ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8571 | continue; |
| 8572 | } |
| 8573 | |
| 8574 | /* OK, so we know that either p2 points to the end of string or to a comma */ |
| 8575 | if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) || |
Willy Tarreau | 5b15f90 | 2013-07-04 12:46:56 +0200 | [diff] [blame] | 8576 | ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) || |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8577 | ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) || |
| 8578 | ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) || |
| 8579 | ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8580 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8581 | return; |
| 8582 | } |
| 8583 | |
| 8584 | if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 8585 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 8586 | continue; |
| 8587 | } |
| 8588 | } |
| 8589 | } |
| 8590 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8591 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8592 | /* |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 8593 | * 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] | 8594 | * for the current backend. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8595 | * |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 8596 | * 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] | 8597 | * uri_auth field is valid. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8598 | * |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 8599 | * Returns 1 if stats should be provided, otherwise 0. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8600 | */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 8601 | 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] | 8602 | { |
| 8603 | struct uri_auth *uri_auth = backend->uri_auth; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 8604 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8605 | const char *uri = msg->chn->buf->p+ msg->sl.rq.u; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8606 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 8607 | if (!uri_auth) |
| 8608 | return 0; |
| 8609 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 8610 | 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] | 8611 | return 0; |
| 8612 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 8613 | /* check URI size */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 8614 | if (uri_auth->uri_len > msg->sl.rq.u_l) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8615 | return 0; |
| 8616 | |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 8617 | if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8618 | return 0; |
| 8619 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8620 | return 1; |
| 8621 | } |
| 8622 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8623 | /* |
| 8624 | * 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] | 8625 | * By default it tries to report the error position as msg->err_pos. However if |
| 8626 | * this one is not set, it will then report msg->next, which is the last known |
| 8627 | * 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] | 8628 | * displays buffers as a contiguous area starting at buf->p. |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8629 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8630 | void http_capture_bad_message(struct error_snapshot *es, struct stream *s, |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 8631 | struct http_msg *msg, |
Willy Tarreau | 3770f23 | 2013-12-07 00:01:53 +0100 | [diff] [blame] | 8632 | enum ht_state state, struct proxy *other_end) |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8633 | { |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 8634 | struct session *sess = strm_sess(s); |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 8635 | struct channel *chn = msg->chn; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8636 | int len1, len2; |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 8637 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8638 | es->len = MIN(chn->buf->i, sizeof(es->buf)); |
| 8639 | len1 = chn->buf->data + chn->buf->size - chn->buf->p; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8640 | len1 = MIN(len1, es->len); |
| 8641 | len2 = es->len - len1; /* remaining data if buffer wraps */ |
| 8642 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8643 | memcpy(es->buf, chn->buf->p, len1); |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8644 | if (len2) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8645 | memcpy(es->buf + len1, chn->buf->data, len2); |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 8646 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8647 | if (msg->err_pos >= 0) |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8648 | es->pos = msg->err_pos; |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 8649 | else |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 8650 | es->pos = msg->next; |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 8651 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8652 | es->when = date; // user-visible date |
| 8653 | es->sid = s->uniq_id; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 8654 | es->srv = objt_server(s->target); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8655 | es->oe = other_end; |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 8656 | if (objt_conn(sess->origin)) |
| 8657 | es->src = __objt_conn(sess->origin)->addr.from; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8658 | else |
| 8659 | memset(&es->src, 0, sizeof(es->src)); |
| 8660 | |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 8661 | es->state = state; |
Willy Tarreau | 10479e4 | 2010-12-12 14:00:34 +0100 | [diff] [blame] | 8662 | es->ev_id = error_snapshot_id++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 8663 | es->b_flags = chn->flags; |
Willy Tarreau | d04b1bc | 2012-05-08 11:03:10 +0200 | [diff] [blame] | 8664 | es->s_flags = s->flags; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8665 | es->t_flags = s->txn->flags; |
Willy Tarreau | d04b1bc | 2012-05-08 11:03:10 +0200 | [diff] [blame] | 8666 | es->m_flags = msg->flags; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8667 | es->b_out = chn->buf->o; |
| 8668 | es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 8669 | es->b_tot = chn->total; |
Willy Tarreau | d04b1bc | 2012-05-08 11:03:10 +0200 | [diff] [blame] | 8670 | es->m_clen = msg->chunk_len; |
| 8671 | es->m_blen = msg->body_len; |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 8672 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 8673 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8674 | /* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of |
| 8675 | * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is |
| 8676 | * performed over the whole headers. Otherwise it must contain a valid header |
| 8677 | * context, initialised with ctx->idx=0 for the first lookup in a series. If |
| 8678 | * <occ> is positive or null, occurrence #occ from the beginning (or last ctx) |
| 8679 | * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less |
| 8680 | * 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] | 8681 | * -1. The value fetch stops at commas, so this function is suited for use with |
| 8682 | * list headers. |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8683 | * 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] | 8684 | */ |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 8685 | 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] | 8686 | struct hdr_idx *idx, int occ, |
| 8687 | struct hdr_ctx *ctx, char **vptr, int *vlen) |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8688 | { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8689 | struct hdr_ctx local_ctx; |
| 8690 | char *ptr_hist[MAX_HDR_HISTORY]; |
| 8691 | int len_hist[MAX_HDR_HISTORY]; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8692 | unsigned int hist_ptr; |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8693 | int found; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8694 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8695 | if (!ctx) { |
| 8696 | local_ctx.idx = 0; |
| 8697 | ctx = &local_ctx; |
| 8698 | } |
| 8699 | |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8700 | if (occ >= 0) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8701 | /* search from the beginning */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8702 | while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8703 | occ--; |
| 8704 | if (occ <= 0) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8705 | *vptr = ctx->line + ctx->val; |
| 8706 | *vlen = ctx->vlen; |
| 8707 | return 1; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8708 | } |
| 8709 | } |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8710 | return 0; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8711 | } |
| 8712 | |
| 8713 | /* negative occurrence, we scan all the list then walk back */ |
| 8714 | if (-occ > MAX_HDR_HISTORY) |
| 8715 | return 0; |
| 8716 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8717 | found = hist_ptr = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8718 | while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8719 | ptr_hist[hist_ptr] = ctx->line + ctx->val; |
| 8720 | len_hist[hist_ptr] = ctx->vlen; |
| 8721 | if (++hist_ptr >= MAX_HDR_HISTORY) |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8722 | hist_ptr = 0; |
| 8723 | found++; |
| 8724 | } |
| 8725 | if (-occ > found) |
| 8726 | return 0; |
| 8727 | /* 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] | 8728 | * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have |
| 8729 | * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ] |
| 8730 | * to remain in the 0..9 range. |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8731 | */ |
Willy Tarreau | 67dad27 | 2013-06-12 22:27:44 +0200 | [diff] [blame] | 8732 | hist_ptr += occ + MAX_HDR_HISTORY; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8733 | if (hist_ptr >= MAX_HDR_HISTORY) |
| 8734 | hist_ptr -= MAX_HDR_HISTORY; |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8735 | *vptr = ptr_hist[hist_ptr]; |
| 8736 | *vlen = len_hist[hist_ptr]; |
| 8737 | return 1; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 8738 | } |
| 8739 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 8740 | /* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of |
| 8741 | * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is |
| 8742 | * performed over the whole headers. Otherwise it must contain a valid header |
| 8743 | * context, initialised with ctx->idx=0 for the first lookup in a series. If |
| 8744 | * <occ> is positive or null, occurrence #occ from the beginning (or last ctx) |
| 8745 | * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less |
| 8746 | * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is |
| 8747 | * -1. This function differs from http_get_hdr() in that it only returns full |
| 8748 | * line header values and does not stop at commas. |
| 8749 | * The return value is 0 if nothing was found, or non-zero otherwise. |
| 8750 | */ |
| 8751 | unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen, |
| 8752 | struct hdr_idx *idx, int occ, |
| 8753 | struct hdr_ctx *ctx, char **vptr, int *vlen) |
| 8754 | { |
| 8755 | struct hdr_ctx local_ctx; |
| 8756 | char *ptr_hist[MAX_HDR_HISTORY]; |
| 8757 | int len_hist[MAX_HDR_HISTORY]; |
| 8758 | unsigned int hist_ptr; |
| 8759 | int found; |
| 8760 | |
| 8761 | if (!ctx) { |
| 8762 | local_ctx.idx = 0; |
| 8763 | ctx = &local_ctx; |
| 8764 | } |
| 8765 | |
| 8766 | if (occ >= 0) { |
| 8767 | /* search from the beginning */ |
| 8768 | while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
| 8769 | occ--; |
| 8770 | if (occ <= 0) { |
| 8771 | *vptr = ctx->line + ctx->val; |
| 8772 | *vlen = ctx->vlen; |
| 8773 | return 1; |
| 8774 | } |
| 8775 | } |
| 8776 | return 0; |
| 8777 | } |
| 8778 | |
| 8779 | /* negative occurrence, we scan all the list then walk back */ |
| 8780 | if (-occ > MAX_HDR_HISTORY) |
| 8781 | return 0; |
| 8782 | |
| 8783 | found = hist_ptr = 0; |
| 8784 | while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
| 8785 | ptr_hist[hist_ptr] = ctx->line + ctx->val; |
| 8786 | len_hist[hist_ptr] = ctx->vlen; |
| 8787 | if (++hist_ptr >= MAX_HDR_HISTORY) |
| 8788 | hist_ptr = 0; |
| 8789 | found++; |
| 8790 | } |
| 8791 | if (-occ > found) |
| 8792 | return 0; |
| 8793 | /* OK now we have the last occurrence in [hist_ptr-1], and we need to |
| 8794 | * find occurrence -occ, so we have to check [hist_ptr+occ]. |
| 8795 | */ |
| 8796 | hist_ptr += occ; |
| 8797 | if (hist_ptr >= MAX_HDR_HISTORY) |
| 8798 | hist_ptr -= MAX_HDR_HISTORY; |
| 8799 | *vptr = ptr_hist[hist_ptr]; |
| 8800 | *vlen = len_hist[hist_ptr]; |
| 8801 | return 1; |
| 8802 | } |
| 8803 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 8804 | /* |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 8805 | * Print a debug line with a header. Always stop at the first CR or LF char, |
| 8806 | * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an |
| 8807 | * arrow is printed after the line which contains the pointer. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8808 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8809 | 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] | 8810 | { |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 8811 | struct session *sess = strm_sess(s); |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 8812 | int max; |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 8813 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 8814 | 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] | 8815 | dir, |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 8816 | objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->t.sock.fd : -1, |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 8817 | objt_conn(s->si[1].end) ? (unsigned short)objt_conn(s->si[1].end)->t.sock.fd : -1); |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 8818 | |
| 8819 | for (max = 0; start + max < end; max++) |
| 8820 | if (start[max] == '\r' || start[max] == '\n') |
| 8821 | break; |
| 8822 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 8823 | UBOUND(max, trash.size - trash.len - 3); |
| 8824 | trash.len += strlcpy2(trash.str + trash.len, start, max + 1); |
| 8825 | trash.str[trash.len++] = '\n'; |
Willy Tarreau | 89efaed | 2013-12-13 15:14:55 +0100 | [diff] [blame] | 8826 | shut_your_big_mouth_gcc(write(1, trash.str, trash.len)); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8827 | } |
| 8828 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8829 | |
| 8830 | /* Allocate a new HTTP transaction for stream <s> unless there is one already. |
| 8831 | * The hdr_idx is allocated as well. In case of allocation failure, everything |
| 8832 | * allocated is freed and NULL is returned. Otherwise the new transaction is |
| 8833 | * assigned to the stream and returned. |
| 8834 | */ |
| 8835 | struct http_txn *http_alloc_txn(struct stream *s) |
| 8836 | { |
| 8837 | struct http_txn *txn = s->txn; |
| 8838 | |
| 8839 | if (txn) |
| 8840 | return txn; |
| 8841 | |
| 8842 | txn = pool_alloc2(pool2_http_txn); |
| 8843 | if (!txn) |
| 8844 | return txn; |
| 8845 | |
| 8846 | txn->hdr_idx.size = global.tune.max_http_hdr; |
| 8847 | txn->hdr_idx.v = pool_alloc2(pool2_hdr_idx); |
| 8848 | if (!txn->hdr_idx.v) { |
| 8849 | pool_free2(pool2_http_txn, txn); |
| 8850 | return NULL; |
| 8851 | } |
| 8852 | |
| 8853 | s->txn = txn; |
| 8854 | return txn; |
| 8855 | } |
| 8856 | |
Thierry FOURNIER | fd50f0b | 2015-09-25 18:53:18 +0200 | [diff] [blame] | 8857 | void http_txn_reset_req(struct http_txn *txn) |
| 8858 | { |
| 8859 | txn->req.flags = 0; |
| 8860 | txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */ |
| 8861 | txn->req.next = 0; |
| 8862 | txn->req.chunk_len = 0LL; |
| 8863 | txn->req.body_len = 0LL; |
| 8864 | txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */ |
| 8865 | } |
| 8866 | |
| 8867 | void http_txn_reset_res(struct http_txn *txn) |
| 8868 | { |
| 8869 | txn->rsp.flags = 0; |
| 8870 | txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */ |
| 8871 | txn->rsp.next = 0; |
| 8872 | txn->rsp.chunk_len = 0LL; |
| 8873 | txn->rsp.body_len = 0LL; |
| 8874 | txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */ |
| 8875 | } |
| 8876 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8877 | /* |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8878 | * 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] | 8879 | * the required fields are properly allocated and that we only need to (re)init |
| 8880 | * them. This should be used before processing any new request. |
| 8881 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8882 | void http_init_txn(struct stream *s) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8883 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8884 | struct http_txn *txn = s->txn; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 8885 | struct proxy *fe = strm_fe(s); |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8886 | |
| 8887 | txn->flags = 0; |
| 8888 | txn->status = -1; |
| 8889 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 8890 | txn->cookie_first_date = 0; |
| 8891 | txn->cookie_last_date = 0; |
| 8892 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8893 | txn->srv_cookie = NULL; |
| 8894 | txn->cli_cookie = NULL; |
| 8895 | txn->uri = NULL; |
| 8896 | |
Thierry FOURNIER | fd50f0b | 2015-09-25 18:53:18 +0200 | [diff] [blame] | 8897 | http_txn_reset_req(txn); |
| 8898 | http_txn_reset_res(txn); |
| 8899 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 8900 | txn->req.chn = &s->req; |
| 8901 | txn->rsp.chn = &s->res; |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 8902 | |
| 8903 | txn->auth.method = HTTP_AUTH_UNKNOWN; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8904 | |
| 8905 | txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */ |
| 8906 | if (fe->options2 & PR_O2_REQBUG_OK) |
| 8907 | txn->req.err_pos = -1; /* let buggy requests pass */ |
| 8908 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8909 | if (txn->hdr_idx.v) |
| 8910 | hdr_idx_init(&txn->hdr_idx); |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 8911 | |
| 8912 | vars_init(&s->vars_txn, SCOPE_TXN); |
| 8913 | vars_init(&s->vars_reqres, SCOPE_REQ); |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8914 | } |
| 8915 | |
| 8916 | /* to be used at the end of a transaction */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8917 | void http_end_txn(struct stream *s) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8918 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 8919 | struct http_txn *txn = s->txn; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 8920 | struct proxy *fe = strm_fe(s); |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8921 | |
Willy Tarreau | 7519560 | 2014-03-11 15:48:55 +0100 | [diff] [blame] | 8922 | /* release any possible compression context */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 8923 | if (s->flags & SF_COMP_READY) |
Willy Tarreau | 7519560 | 2014-03-11 15:48:55 +0100 | [diff] [blame] | 8924 | s->comp_algo->end(&s->comp_ctx); |
| 8925 | s->comp_algo = NULL; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 8926 | s->flags &= ~SF_COMP_READY; |
Willy Tarreau | 7519560 | 2014-03-11 15:48:55 +0100 | [diff] [blame] | 8927 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8928 | /* these ones will have been dynamically allocated */ |
| 8929 | pool_free2(pool2_requri, txn->uri); |
| 8930 | pool_free2(pool2_capture, txn->cli_cookie); |
| 8931 | pool_free2(pool2_capture, txn->srv_cookie); |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 8932 | pool_free2(pool2_uniqueid, s->unique_id); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 8933 | |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 8934 | s->unique_id = NULL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8935 | txn->uri = NULL; |
| 8936 | txn->srv_cookie = NULL; |
| 8937 | txn->cli_cookie = NULL; |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8938 | |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 8939 | if (s->req_cap) { |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8940 | struct cap_hdr *h; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 8941 | for (h = fe->req_cap; h; h = h->next) |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 8942 | pool_free2(h->pool, s->req_cap[h->index]); |
| 8943 | memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *)); |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8944 | } |
| 8945 | |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 8946 | if (s->res_cap) { |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8947 | struct cap_hdr *h; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 8948 | for (h = fe->rsp_cap; h; h = h->next) |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 8949 | pool_free2(h->pool, s->res_cap[h->index]); |
| 8950 | memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *)); |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8951 | } |
| 8952 | |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 8953 | vars_prune(&s->vars_txn, s); |
| 8954 | vars_prune(&s->vars_reqres, s); |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8955 | } |
| 8956 | |
| 8957 | /* 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] | 8958 | void http_reset_txn(struct stream *s) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8959 | { |
| 8960 | http_end_txn(s); |
| 8961 | http_init_txn(s); |
| 8962 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 8963 | /* reinitialise the current rule list pointer to NULL. We are sure that |
| 8964 | * any rulelist match the NULL pointer. |
| 8965 | */ |
| 8966 | s->current_rule_list = NULL; |
| 8967 | |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 8968 | s->be = strm_fe(s); |
| 8969 | s->logs.logwait = strm_fe(s)->to_log; |
Willy Tarreau | abcd514 | 2013-06-11 17:18:02 +0200 | [diff] [blame] | 8970 | s->logs.level = 0; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8971 | stream_del_srv_conn(s); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 8972 | s->target = NULL; |
Emeric Brun | b982a3d | 2010-01-04 15:45:53 +0100 | [diff] [blame] | 8973 | /* re-init store persistence */ |
| 8974 | s->store_count = 0; |
Willy Tarreau | 1f0da24 | 2014-01-25 11:01:50 +0100 | [diff] [blame] | 8975 | s->uniq_id = global.req_count++; |
Emeric Brun | b982a3d | 2010-01-04 15:45:53 +0100 | [diff] [blame] | 8976 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8977 | s->pend_pos = NULL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8978 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 8979 | s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */ |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8980 | |
Willy Tarreau | 739cfba | 2010-01-25 23:11:14 +0100 | [diff] [blame] | 8981 | /* We must trim any excess data from the response buffer, because we |
| 8982 | * may have blocked an invalid response from a server that we don't |
| 8983 | * want to accidentely forward once we disable the analysers, nor do |
| 8984 | * we want those data to come along with next response. A typical |
| 8985 | * example of such data would be from a buggy server responding to |
| 8986 | * a HEAD with some data, or sending more than the advertised |
| 8987 | * content-length. |
| 8988 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 8989 | if (unlikely(s->res.buf->i)) |
| 8990 | s->res.buf->i = 0; |
Willy Tarreau | 739cfba | 2010-01-25 23:11:14 +0100 | [diff] [blame] | 8991 | |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 8992 | s->req.rto = strm_fe(s)->timeout.client; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 8993 | s->req.wto = TICK_ETERNITY; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8994 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 8995 | s->res.rto = TICK_ETERNITY; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 8996 | s->res.wto = strm_fe(s)->timeout.client; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8997 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 8998 | s->req.rex = TICK_ETERNITY; |
| 8999 | s->req.wex = TICK_ETERNITY; |
| 9000 | s->req.analyse_exp = TICK_ETERNITY; |
| 9001 | s->res.rex = TICK_ETERNITY; |
| 9002 | s->res.wex = TICK_ETERNITY; |
| 9003 | s->res.analyse_exp = TICK_ETERNITY; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 9004 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 9005 | |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9006 | void free_http_res_rules(struct list *r) |
| 9007 | { |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 9008 | struct act_rule *tr, *pr; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9009 | |
| 9010 | list_for_each_entry_safe(pr, tr, r, list) { |
| 9011 | LIST_DEL(&pr->list); |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 9012 | regex_free(&pr->arg.hdr_add.re); |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9013 | free(pr); |
| 9014 | } |
| 9015 | } |
| 9016 | |
| 9017 | void free_http_req_rules(struct list *r) |
| 9018 | { |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 9019 | struct act_rule *tr, *pr; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9020 | |
| 9021 | list_for_each_entry_safe(pr, tr, r, list) { |
| 9022 | LIST_DEL(&pr->list); |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9023 | if (pr->action == ACT_HTTP_REQ_AUTH) |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 9024 | free(pr->arg.auth.realm); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9025 | |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 9026 | regex_free(&pr->arg.hdr_add.re); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9027 | free(pr); |
| 9028 | } |
| 9029 | } |
| 9030 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9031 | /* parse an "http-request" rule */ |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 9032 | 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] | 9033 | { |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 9034 | struct act_rule *rule; |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 9035 | struct action_kw *custom = NULL; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9036 | int cur_arg; |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 9037 | char *error; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9038 | |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 9039 | rule = (struct act_rule*)calloc(1, sizeof(struct act_rule)); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9040 | if (!rule) { |
| 9041 | Alert("parsing [%s:%d]: out of memory.\n", file, linenum); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9042 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9043 | } |
| 9044 | |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 9045 | rule->deny_status = HTTP_ERR_403; |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 9046 | if (!strcmp(args[0], "allow")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9047 | rule->action = ACT_ACTION_ALLOW; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9048 | cur_arg = 1; |
Willy Tarreau | 5bd6759 | 2014-04-28 22:00:46 +0200 | [diff] [blame] | 9049 | } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block")) { |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 9050 | int code; |
| 9051 | int hc; |
| 9052 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9053 | rule->action = ACT_ACTION_DENY; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9054 | cur_arg = 1; |
CJ Ess | 108b1dd | 2015-04-07 12:03:37 -0400 | [diff] [blame] | 9055 | if (strcmp(args[cur_arg], "deny_status") == 0) { |
| 9056 | cur_arg++; |
| 9057 | if (!args[cur_arg]) { |
| 9058 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing status code.\n", |
| 9059 | file, linenum, proxy_type_str(proxy), proxy->id, args[0]); |
| 9060 | goto out_err; |
| 9061 | } |
| 9062 | |
| 9063 | code = atol(args[cur_arg]); |
| 9064 | cur_arg++; |
| 9065 | for (hc = 0; hc < HTTP_ERR_SIZE; hc++) { |
| 9066 | if (http_err_codes[hc] == code) { |
| 9067 | rule->deny_status = hc; |
| 9068 | break; |
| 9069 | } |
| 9070 | } |
| 9071 | |
| 9072 | if (hc >= HTTP_ERR_SIZE) { |
| 9073 | Warning("parsing [%s:%d] : status code %d not handled, using default code 403.\n", |
| 9074 | file, linenum, code); |
| 9075 | } |
| 9076 | } |
Willy Tarreau | ccbcc37 | 2012-12-27 12:37:57 +0100 | [diff] [blame] | 9077 | } else if (!strcmp(args[0], "tarpit")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9078 | rule->action = ACT_HTTP_REQ_TARPIT; |
Willy Tarreau | ccbcc37 | 2012-12-27 12:37:57 +0100 | [diff] [blame] | 9079 | cur_arg = 1; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9080 | } else if (!strcmp(args[0], "auth")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9081 | rule->action = ACT_HTTP_REQ_AUTH; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9082 | cur_arg = 1; |
| 9083 | |
| 9084 | while(*args[cur_arg]) { |
| 9085 | if (!strcmp(args[cur_arg], "realm")) { |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 9086 | rule->arg.auth.realm = strdup(args[cur_arg + 1]); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9087 | cur_arg+=2; |
| 9088 | continue; |
| 9089 | } else |
| 9090 | break; |
| 9091 | } |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 9092 | } else if (!strcmp(args[0], "set-nice")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9093 | rule->action = ACT_HTTP_SET_NICE; |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 9094 | cur_arg = 1; |
| 9095 | |
| 9096 | if (!*args[cur_arg] || |
| 9097 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9098 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n", |
| 9099 | file, linenum, args[0]); |
| 9100 | goto out_err; |
| 9101 | } |
| 9102 | rule->arg.nice = atoi(args[cur_arg]); |
| 9103 | if (rule->arg.nice < -1024) |
| 9104 | rule->arg.nice = -1024; |
| 9105 | else if (rule->arg.nice > 1024) |
| 9106 | rule->arg.nice = 1024; |
| 9107 | cur_arg++; |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 9108 | } else if (!strcmp(args[0], "set-tos")) { |
| 9109 | #ifdef IP_TOS |
| 9110 | char *err; |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9111 | rule->action = ACT_HTTP_SET_TOS; |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 9112 | cur_arg = 1; |
| 9113 | |
| 9114 | if (!*args[cur_arg] || |
| 9115 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9116 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n", |
| 9117 | file, linenum, args[0]); |
| 9118 | goto out_err; |
| 9119 | } |
| 9120 | |
| 9121 | rule->arg.tos = strtol(args[cur_arg], &err, 0); |
| 9122 | if (err && *err != '\0') { |
| 9123 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n", |
| 9124 | file, linenum, err, args[0]); |
| 9125 | goto out_err; |
| 9126 | } |
| 9127 | cur_arg++; |
| 9128 | #else |
| 9129 | Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]); |
| 9130 | goto out_err; |
| 9131 | #endif |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 9132 | } else if (!strcmp(args[0], "set-mark")) { |
| 9133 | #ifdef SO_MARK |
| 9134 | char *err; |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9135 | rule->action = ACT_HTTP_SET_MARK; |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 9136 | cur_arg = 1; |
| 9137 | |
| 9138 | if (!*args[cur_arg] || |
| 9139 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9140 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n", |
| 9141 | file, linenum, args[0]); |
| 9142 | goto out_err; |
| 9143 | } |
| 9144 | |
| 9145 | rule->arg.mark = strtoul(args[cur_arg], &err, 0); |
| 9146 | if (err && *err != '\0') { |
| 9147 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n", |
| 9148 | file, linenum, err, args[0]); |
| 9149 | goto out_err; |
| 9150 | } |
| 9151 | cur_arg++; |
| 9152 | global.last_checks |= LSTCHK_NETADM; |
| 9153 | #else |
| 9154 | Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]); |
| 9155 | goto out_err; |
| 9156 | #endif |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 9157 | } else if (!strcmp(args[0], "set-log-level")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9158 | rule->action = ACT_HTTP_SET_LOGL; |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 9159 | cur_arg = 1; |
| 9160 | |
| 9161 | if (!*args[cur_arg] || |
| 9162 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9163 | bad_log_level: |
| 9164 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n", |
| 9165 | file, linenum, args[0]); |
| 9166 | goto out_err; |
| 9167 | } |
| 9168 | if (strcmp(args[cur_arg], "silent") == 0) |
| 9169 | rule->arg.loglevel = -1; |
| 9170 | else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0) |
| 9171 | goto bad_log_level; |
| 9172 | cur_arg++; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 9173 | } 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] | 9174 | 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] | 9175 | cur_arg = 1; |
| 9176 | |
Willy Tarreau | 8d1c516 | 2013-04-03 14:13:58 +0200 | [diff] [blame] | 9177 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 9178 | (*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] | 9179 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n", |
| 9180 | file, linenum, args[0]); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9181 | goto out_err; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 9182 | } |
| 9183 | |
| 9184 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9185 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9186 | LIST_INIT(&rule->arg.hdr_add.fmt); |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 9187 | |
Thierry FOURNIER | 1c0054f | 2013-11-20 15:09:52 +0100 | [diff] [blame] | 9188 | proxy->conf.args.ctx = ARGC_HRQ; |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 9189 | parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP, |
Thierry FOURNIER | eeaa951 | 2014-02-11 14:00:19 +0100 | [diff] [blame] | 9190 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9191 | file, linenum); |
Willy Tarreau | 59ad1a2 | 2014-01-29 14:39:58 +0100 | [diff] [blame] | 9192 | free(proxy->conf.lfs_file); |
| 9193 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9194 | proxy->conf.lfs_line = proxy->conf.args.line; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 9195 | cur_arg += 2; |
Willy Tarreau | b854392 | 2014-06-17 18:58:26 +0200 | [diff] [blame] | 9196 | } 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] | 9197 | 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] | 9198 | cur_arg = 1; |
| 9199 | |
| 9200 | if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] || |
Baptiste Assmann | 92df370 | 2014-06-24 11:10:00 +0200 | [diff] [blame] | 9201 | (*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] | 9202 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n", |
| 9203 | file, linenum, args[0]); |
| 9204 | goto out_err; |
| 9205 | } |
| 9206 | |
| 9207 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9208 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9209 | LIST_INIT(&rule->arg.hdr_add.fmt); |
| 9210 | |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 9211 | error = NULL; |
| 9212 | if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) { |
| 9213 | Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum, |
| 9214 | args[cur_arg + 1], error); |
| 9215 | free(error); |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9216 | goto out_err; |
| 9217 | } |
| 9218 | |
| 9219 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9220 | parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP, |
| 9221 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9222 | file, linenum); |
| 9223 | |
| 9224 | free(proxy->conf.lfs_file); |
| 9225 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9226 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9227 | cur_arg += 3; |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9228 | } else if (strcmp(args[0], "del-header") == 0) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9229 | rule->action = ACT_HTTP_DEL_HDR; |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9230 | cur_arg = 1; |
| 9231 | |
| 9232 | if (!*args[cur_arg] || |
| 9233 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9234 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 9235 | file, linenum, args[0]); |
| 9236 | goto out_err; |
| 9237 | } |
| 9238 | |
| 9239 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9240 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9241 | |
| 9242 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9243 | free(proxy->conf.lfs_file); |
| 9244 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9245 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9246 | cur_arg += 1; |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 9247 | } else if (strncmp(args[0], "track-sc", 8) == 0 && |
| 9248 | args[0][9] == '\0' && args[0][8] >= '0' && |
Willy Tarreau | e1cfc1f | 2014-10-17 11:53:05 +0200 | [diff] [blame] | 9249 | args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */ |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 9250 | struct sample_expr *expr; |
| 9251 | unsigned int where; |
| 9252 | char *err = NULL; |
| 9253 | |
| 9254 | cur_arg = 1; |
| 9255 | proxy->conf.args.ctx = ARGC_TRK; |
| 9256 | |
| 9257 | expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args); |
| 9258 | if (!expr) { |
| 9259 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n", |
| 9260 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], err); |
| 9261 | free(err); |
| 9262 | goto out_err; |
| 9263 | } |
| 9264 | |
| 9265 | where = 0; |
| 9266 | if (proxy->cap & PR_CAP_FE) |
| 9267 | where |= SMP_VAL_FE_HRQ_HDR; |
| 9268 | if (proxy->cap & PR_CAP_BE) |
| 9269 | where |= SMP_VAL_BE_HRQ_HDR; |
| 9270 | |
| 9271 | if (!(expr->fetch->val & where)) { |
| 9272 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :" |
| 9273 | " fetch method '%s' extracts information from '%s', none of which is available here.\n", |
| 9274 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], |
| 9275 | args[cur_arg-1], sample_src_names(expr->fetch->use)); |
| 9276 | free(expr); |
| 9277 | goto out_err; |
| 9278 | } |
| 9279 | |
| 9280 | if (strcmp(args[cur_arg], "table") == 0) { |
| 9281 | cur_arg++; |
| 9282 | if (!args[cur_arg]) { |
| 9283 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing table name.\n", |
| 9284 | file, linenum, proxy_type_str(proxy), proxy->id, args[0]); |
| 9285 | free(expr); |
| 9286 | goto out_err; |
| 9287 | } |
| 9288 | /* we copy the table name for now, it will be resolved later */ |
Thierry FOURNIER | 5ec63e0 | 2015-08-04 09:09:48 +0200 | [diff] [blame] | 9289 | rule->arg.trk_ctr.table.n = strdup(args[cur_arg]); |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 9290 | cur_arg++; |
| 9291 | } |
Thierry FOURNIER | 5ec63e0 | 2015-08-04 09:09:48 +0200 | [diff] [blame] | 9292 | rule->arg.trk_ctr.expr = expr; |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9293 | rule->action = ACT_ACTION_TRK_SC0 + args[0][8] - '0'; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9294 | } else if (strcmp(args[0], "redirect") == 0) { |
| 9295 | struct redirect_rule *redir; |
Willy Tarreau | 6d4890c | 2013-11-18 18:04:25 +0100 | [diff] [blame] | 9296 | char *errmsg = NULL; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9297 | |
Willy Tarreau | be4653b | 2015-05-28 15:26:58 +0200 | [diff] [blame] | 9298 | 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] | 9299 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n", |
| 9300 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
| 9301 | goto out_err; |
| 9302 | } |
| 9303 | |
| 9304 | /* this redirect rule might already contain a parsed condition which |
| 9305 | * we'll pass to the http-request rule. |
| 9306 | */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9307 | rule->action = ACT_HTTP_REDIR; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9308 | rule->arg.redir = redir; |
| 9309 | rule->cond = redir->cond; |
| 9310 | redir->cond = NULL; |
| 9311 | cur_arg = 2; |
| 9312 | return rule; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9313 | } else if (strncmp(args[0], "add-acl", 7) == 0) { |
| 9314 | /* http-request add-acl(<reference (acl name)>) <key pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9315 | rule->action = ACT_HTTP_ADD_ACL; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9316 | /* |
| 9317 | * '+ 8' for 'add-acl(' |
| 9318 | * '- 9' for 'add-acl(' + trailing ')' |
| 9319 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9320 | 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] | 9321 | |
| 9322 | cur_arg = 1; |
| 9323 | |
| 9324 | if (!*args[cur_arg] || |
| 9325 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9326 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 9327 | file, linenum, args[0]); |
| 9328 | goto out_err; |
| 9329 | } |
| 9330 | |
| 9331 | LIST_INIT(&rule->arg.map.key); |
| 9332 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9333 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9334 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9335 | file, linenum); |
| 9336 | free(proxy->conf.lfs_file); |
| 9337 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9338 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9339 | cur_arg += 1; |
| 9340 | } else if (strncmp(args[0], "del-acl", 7) == 0) { |
| 9341 | /* http-request del-acl(<reference (acl name)>) <key pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9342 | rule->action = ACT_HTTP_DEL_ACL; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9343 | /* |
| 9344 | * '+ 8' for 'del-acl(' |
| 9345 | * '- 9' for 'del-acl(' + trailing ')' |
| 9346 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9347 | 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] | 9348 | |
| 9349 | cur_arg = 1; |
| 9350 | |
| 9351 | if (!*args[cur_arg] || |
| 9352 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9353 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 9354 | file, linenum, args[0]); |
| 9355 | goto out_err; |
| 9356 | } |
| 9357 | |
| 9358 | LIST_INIT(&rule->arg.map.key); |
| 9359 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9360 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9361 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9362 | file, linenum); |
| 9363 | free(proxy->conf.lfs_file); |
| 9364 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9365 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9366 | cur_arg += 1; |
| 9367 | } else if (strncmp(args[0], "del-map", 7) == 0) { |
| 9368 | /* http-request del-map(<reference (map name)>) <key pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9369 | rule->action = ACT_HTTP_DEL_MAP; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9370 | /* |
| 9371 | * '+ 8' for 'del-map(' |
| 9372 | * '- 9' for 'del-map(' + trailing ')' |
| 9373 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9374 | 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] | 9375 | |
| 9376 | cur_arg = 1; |
| 9377 | |
| 9378 | if (!*args[cur_arg] || |
| 9379 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9380 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n", |
| 9381 | file, linenum, args[0]); |
| 9382 | goto out_err; |
| 9383 | } |
| 9384 | |
| 9385 | LIST_INIT(&rule->arg.map.key); |
| 9386 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9387 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9388 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9389 | file, linenum); |
| 9390 | free(proxy->conf.lfs_file); |
| 9391 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9392 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9393 | cur_arg += 1; |
| 9394 | } else if (strncmp(args[0], "set-map", 7) == 0) { |
| 9395 | /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9396 | rule->action = ACT_HTTP_SET_MAP; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9397 | /* |
| 9398 | * '+ 8' for 'set-map(' |
| 9399 | * '- 9' for 'set-map(' + trailing ')' |
| 9400 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9401 | 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] | 9402 | |
| 9403 | cur_arg = 1; |
| 9404 | |
| 9405 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 9406 | (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) { |
| 9407 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n", |
| 9408 | file, linenum, args[0]); |
| 9409 | goto out_err; |
| 9410 | } |
| 9411 | |
| 9412 | LIST_INIT(&rule->arg.map.key); |
| 9413 | LIST_INIT(&rule->arg.map.value); |
| 9414 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9415 | |
| 9416 | /* key pattern */ |
| 9417 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9418 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9419 | file, linenum); |
| 9420 | |
| 9421 | /* value pattern */ |
| 9422 | parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP, |
| 9423 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 9424 | file, linenum); |
| 9425 | free(proxy->conf.lfs_file); |
| 9426 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9427 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9428 | |
| 9429 | cur_arg += 2; |
Adis Nezirovic | 2fbcafc | 2015-07-06 15:44:30 +0200 | [diff] [blame] | 9430 | } else if (strncmp(args[0], "set-src", 7) == 0) { |
| 9431 | struct sample_expr *expr; |
| 9432 | unsigned int where; |
| 9433 | char *err = NULL; |
| 9434 | |
| 9435 | cur_arg = 1; |
| 9436 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9437 | |
| 9438 | expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args); |
| 9439 | if (!expr) { |
| 9440 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n", |
| 9441 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], err); |
| 9442 | free(err); |
| 9443 | goto out_err; |
| 9444 | } |
| 9445 | |
| 9446 | where = 0; |
| 9447 | if (proxy->cap & PR_CAP_FE) |
| 9448 | where |= SMP_VAL_FE_HRQ_HDR; |
| 9449 | if (proxy->cap & PR_CAP_BE) |
| 9450 | where |= SMP_VAL_BE_HRQ_HDR; |
| 9451 | |
| 9452 | if (!(expr->fetch->val & where)) { |
| 9453 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :" |
| 9454 | " fetch method '%s' extracts information from '%s', none of which is available here.\n", |
| 9455 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], |
| 9456 | args[cur_arg-1], sample_src_names(expr->fetch->use)); |
| 9457 | free(expr); |
| 9458 | goto out_err; |
| 9459 | } |
| 9460 | |
Thierry FOURNIER | a002dc9 | 2015-07-31 08:50:51 +0200 | [diff] [blame] | 9461 | rule->arg.expr = expr; |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9462 | rule->action = ACT_HTTP_REQ_SET_SRC; |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 9463 | } else if (((custom = action_http_req_custom(args[0])) != NULL)) { |
| 9464 | char *errmsg = NULL; |
| 9465 | cur_arg = 1; |
| 9466 | /* try in the module list */ |
Thierry FOURNIER | 5563e4b | 2015-08-14 19:20:07 +0200 | [diff] [blame] | 9467 | rule->from = ACT_F_HTTP_REQ; |
Thierry FOURNIER | 85c6c97 | 2015-09-22 19:14:35 +0200 | [diff] [blame] | 9468 | rule->kw = custom; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 9469 | 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] | 9470 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n", |
| 9471 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
| 9472 | free(errmsg); |
| 9473 | goto out_err; |
| 9474 | } |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9475 | } else { |
Thierry FOURNIER | ab95e65 | 2015-10-02 08:24:51 +0200 | [diff] [blame] | 9476 | action_build_list(&http_req_keywords.list, &trash); |
| 9477 | Alert("parsing [%s:%d]: 'http-request' expects 'allow', 'deny', 'auth', 'redirect', " |
| 9478 | "'tarpit', 'add-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', " |
| 9479 | "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', " |
| 9480 | "'set-src'%s%s, but got '%s'%s.\n", |
| 9481 | file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)"); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9482 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9483 | } |
| 9484 | |
| 9485 | if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) { |
| 9486 | struct acl_cond *cond; |
Willy Tarreau | b7451bb | 2012-04-27 12:38:15 +0200 | [diff] [blame] | 9487 | char *errmsg = NULL; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9488 | |
Willy Tarreau | b7451bb | 2012-04-27 12:38:15 +0200 | [diff] [blame] | 9489 | if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) { |
| 9490 | Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n", |
| 9491 | file, linenum, args[0], errmsg); |
| 9492 | free(errmsg); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9493 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9494 | } |
| 9495 | rule->cond = cond; |
| 9496 | } |
| 9497 | else if (*args[cur_arg]) { |
| 9498 | Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or" |
| 9499 | " either 'if' or 'unless' followed by a condition but found '%s'.\n", |
| 9500 | file, linenum, args[0], args[cur_arg]); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9501 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9502 | } |
| 9503 | |
| 9504 | return rule; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 9505 | out_err: |
| 9506 | free(rule); |
| 9507 | return NULL; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 9508 | } |
| 9509 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9510 | /* parse an "http-respose" rule */ |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 9511 | 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] | 9512 | { |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 9513 | struct act_rule *rule; |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 9514 | struct action_kw *custom = NULL; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9515 | int cur_arg; |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 9516 | char *error; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9517 | |
| 9518 | rule = calloc(1, sizeof(*rule)); |
| 9519 | if (!rule) { |
| 9520 | Alert("parsing [%s:%d]: out of memory.\n", file, linenum); |
| 9521 | goto out_err; |
| 9522 | } |
| 9523 | |
| 9524 | if (!strcmp(args[0], "allow")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9525 | rule->action = ACT_ACTION_ALLOW; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9526 | cur_arg = 1; |
| 9527 | } else if (!strcmp(args[0], "deny")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9528 | rule->action = ACT_ACTION_DENY; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9529 | cur_arg = 1; |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 9530 | } else if (!strcmp(args[0], "set-nice")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9531 | rule->action = ACT_HTTP_SET_NICE; |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 9532 | cur_arg = 1; |
| 9533 | |
| 9534 | if (!*args[cur_arg] || |
| 9535 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9536 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n", |
| 9537 | file, linenum, args[0]); |
| 9538 | goto out_err; |
| 9539 | } |
| 9540 | rule->arg.nice = atoi(args[cur_arg]); |
| 9541 | if (rule->arg.nice < -1024) |
| 9542 | rule->arg.nice = -1024; |
| 9543 | else if (rule->arg.nice > 1024) |
| 9544 | rule->arg.nice = 1024; |
| 9545 | cur_arg++; |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 9546 | } else if (!strcmp(args[0], "set-tos")) { |
| 9547 | #ifdef IP_TOS |
| 9548 | char *err; |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9549 | rule->action = ACT_HTTP_SET_TOS; |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 9550 | cur_arg = 1; |
| 9551 | |
| 9552 | if (!*args[cur_arg] || |
| 9553 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9554 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n", |
| 9555 | file, linenum, args[0]); |
| 9556 | goto out_err; |
| 9557 | } |
| 9558 | |
| 9559 | rule->arg.tos = strtol(args[cur_arg], &err, 0); |
| 9560 | if (err && *err != '\0') { |
| 9561 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n", |
| 9562 | file, linenum, err, args[0]); |
| 9563 | goto out_err; |
| 9564 | } |
| 9565 | cur_arg++; |
| 9566 | #else |
| 9567 | Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]); |
| 9568 | goto out_err; |
| 9569 | #endif |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 9570 | } else if (!strcmp(args[0], "set-mark")) { |
| 9571 | #ifdef SO_MARK |
| 9572 | char *err; |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9573 | rule->action = ACT_HTTP_SET_MARK; |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 9574 | cur_arg = 1; |
| 9575 | |
| 9576 | if (!*args[cur_arg] || |
| 9577 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9578 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n", |
| 9579 | file, linenum, args[0]); |
| 9580 | goto out_err; |
| 9581 | } |
| 9582 | |
| 9583 | rule->arg.mark = strtoul(args[cur_arg], &err, 0); |
| 9584 | if (err && *err != '\0') { |
| 9585 | Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n", |
| 9586 | file, linenum, err, args[0]); |
| 9587 | goto out_err; |
| 9588 | } |
| 9589 | cur_arg++; |
| 9590 | global.last_checks |= LSTCHK_NETADM; |
| 9591 | #else |
| 9592 | Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]); |
| 9593 | goto out_err; |
| 9594 | #endif |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 9595 | } else if (!strcmp(args[0], "set-log-level")) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9596 | rule->action = ACT_HTTP_SET_LOGL; |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 9597 | cur_arg = 1; |
| 9598 | |
| 9599 | if (!*args[cur_arg] || |
| 9600 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 9601 | bad_log_level: |
| 9602 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n", |
| 9603 | file, linenum, args[0]); |
| 9604 | goto out_err; |
| 9605 | } |
| 9606 | if (strcmp(args[cur_arg], "silent") == 0) |
| 9607 | rule->arg.loglevel = -1; |
| 9608 | else if ((rule->arg.loglevel = get_log_level(args[cur_arg] + 1)) == 0) |
| 9609 | goto bad_log_level; |
| 9610 | cur_arg++; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9611 | } 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] | 9612 | 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] | 9613 | cur_arg = 1; |
| 9614 | |
| 9615 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 9616 | (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) { |
| 9617 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n", |
| 9618 | file, linenum, args[0]); |
| 9619 | goto out_err; |
| 9620 | } |
| 9621 | |
| 9622 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9623 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9624 | LIST_INIT(&rule->arg.hdr_add.fmt); |
| 9625 | |
Thierry FOURNIER | 1c0054f | 2013-11-20 15:09:52 +0100 | [diff] [blame] | 9626 | proxy->conf.args.ctx = ARGC_HRS; |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 9627 | parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP, |
Thierry FOURNIER | eeaa951 | 2014-02-11 14:00:19 +0100 | [diff] [blame] | 9628 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9629 | file, linenum); |
Willy Tarreau | 59ad1a2 | 2014-01-29 14:39:58 +0100 | [diff] [blame] | 9630 | free(proxy->conf.lfs_file); |
| 9631 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9632 | proxy->conf.lfs_line = proxy->conf.args.line; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9633 | cur_arg += 2; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9634 | } 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] | 9635 | 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] | 9636 | cur_arg = 1; |
| 9637 | |
| 9638 | if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] || |
Baptiste Assmann | 12cb00b | 2014-08-08 17:29:06 +0200 | [diff] [blame] | 9639 | (*args[cur_arg+3] && strcmp(args[cur_arg+3], "if") != 0 && strcmp(args[cur_arg+3], "unless") != 0)) { |
| 9640 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n", |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9641 | file, linenum, args[0]); |
| 9642 | goto out_err; |
| 9643 | } |
| 9644 | |
| 9645 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9646 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9647 | LIST_INIT(&rule->arg.hdr_add.fmt); |
| 9648 | |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 9649 | error = NULL; |
| 9650 | if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) { |
| 9651 | Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum, |
| 9652 | args[cur_arg + 1], error); |
| 9653 | free(error); |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 9654 | goto out_err; |
| 9655 | } |
| 9656 | |
| 9657 | proxy->conf.args.ctx = ARGC_HRQ; |
| 9658 | parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP, |
| 9659 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9660 | file, linenum); |
| 9661 | |
| 9662 | free(proxy->conf.lfs_file); |
| 9663 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9664 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9665 | cur_arg += 3; |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9666 | } else if (strcmp(args[0], "del-header") == 0) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9667 | rule->action = ACT_HTTP_DEL_HDR; |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9668 | cur_arg = 1; |
| 9669 | |
| 9670 | if (!*args[cur_arg] || |
| 9671 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9672 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 9673 | file, linenum, args[0]); |
| 9674 | goto out_err; |
| 9675 | } |
| 9676 | |
| 9677 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 9678 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 9679 | |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9680 | proxy->conf.args.ctx = ARGC_HRS; |
| 9681 | free(proxy->conf.lfs_file); |
| 9682 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9683 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9684 | cur_arg += 1; |
| 9685 | } else if (strncmp(args[0], "add-acl", 7) == 0) { |
| 9686 | /* http-request add-acl(<reference (acl name)>) <key pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9687 | rule->action = ACT_HTTP_ADD_ACL; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9688 | /* |
| 9689 | * '+ 8' for 'add-acl(' |
| 9690 | * '- 9' for 'add-acl(' + trailing ')' |
| 9691 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9692 | 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] | 9693 | |
| 9694 | cur_arg = 1; |
| 9695 | |
| 9696 | if (!*args[cur_arg] || |
| 9697 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9698 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 9699 | file, linenum, args[0]); |
| 9700 | goto out_err; |
| 9701 | } |
| 9702 | |
| 9703 | LIST_INIT(&rule->arg.map.key); |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9704 | proxy->conf.args.ctx = ARGC_HRS; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9705 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9706 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9707 | file, linenum); |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9708 | free(proxy->conf.lfs_file); |
| 9709 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9710 | proxy->conf.lfs_line = proxy->conf.args.line; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9711 | |
Thierry FOURNIER | dad3d1d | 2014-04-22 18:07:25 +0200 | [diff] [blame] | 9712 | cur_arg += 1; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9713 | } else if (strncmp(args[0], "del-acl", 7) == 0) { |
| 9714 | /* http-response del-acl(<reference (acl name)>) <key pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9715 | rule->action = ACT_HTTP_DEL_ACL; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9716 | /* |
| 9717 | * '+ 8' for 'del-acl(' |
| 9718 | * '- 9' for 'del-acl(' + trailing ')' |
| 9719 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9720 | 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] | 9721 | |
| 9722 | cur_arg = 1; |
| 9723 | |
| 9724 | if (!*args[cur_arg] || |
| 9725 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9726 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 9727 | file, linenum, args[0]); |
| 9728 | goto out_err; |
| 9729 | } |
| 9730 | |
| 9731 | LIST_INIT(&rule->arg.map.key); |
| 9732 | proxy->conf.args.ctx = ARGC_HRS; |
| 9733 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9734 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9735 | file, linenum); |
| 9736 | free(proxy->conf.lfs_file); |
| 9737 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9738 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9739 | cur_arg += 1; |
| 9740 | } else if (strncmp(args[0], "del-map", 7) == 0) { |
| 9741 | /* http-response del-map(<reference (map name)>) <key pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9742 | rule->action = ACT_HTTP_DEL_MAP; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9743 | /* |
| 9744 | * '+ 8' for 'del-map(' |
| 9745 | * '- 9' for 'del-map(' + trailing ')' |
| 9746 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9747 | 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] | 9748 | |
| 9749 | cur_arg = 1; |
| 9750 | |
| 9751 | if (!*args[cur_arg] || |
| 9752 | (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) { |
| 9753 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n", |
| 9754 | file, linenum, args[0]); |
| 9755 | goto out_err; |
| 9756 | } |
| 9757 | |
| 9758 | LIST_INIT(&rule->arg.map.key); |
| 9759 | proxy->conf.args.ctx = ARGC_HRS; |
| 9760 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9761 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9762 | file, linenum); |
| 9763 | free(proxy->conf.lfs_file); |
| 9764 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9765 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9766 | cur_arg += 1; |
| 9767 | } else if (strncmp(args[0], "set-map", 7) == 0) { |
| 9768 | /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9769 | rule->action = ACT_HTTP_SET_MAP; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 9770 | /* |
| 9771 | * '+ 8' for 'set-map(' |
| 9772 | * '- 9' for 'set-map(' + trailing ')' |
| 9773 | */ |
Willy Tarreau | 6c09c2c | 2014-04-25 21:38:08 +0200 | [diff] [blame] | 9774 | 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] | 9775 | |
| 9776 | cur_arg = 1; |
| 9777 | |
| 9778 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 9779 | (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) { |
| 9780 | Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n", |
| 9781 | file, linenum, args[0]); |
| 9782 | goto out_err; |
| 9783 | } |
| 9784 | |
| 9785 | LIST_INIT(&rule->arg.map.key); |
| 9786 | LIST_INIT(&rule->arg.map.value); |
| 9787 | |
| 9788 | proxy->conf.args.ctx = ARGC_HRS; |
| 9789 | |
| 9790 | /* key pattern */ |
| 9791 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP, |
| 9792 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9793 | file, linenum); |
| 9794 | |
| 9795 | /* value pattern */ |
| 9796 | parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP, |
| 9797 | (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, |
| 9798 | file, linenum); |
| 9799 | |
| 9800 | free(proxy->conf.lfs_file); |
| 9801 | proxy->conf.lfs_file = strdup(proxy->conf.args.file); |
| 9802 | proxy->conf.lfs_line = proxy->conf.args.line; |
| 9803 | |
| 9804 | cur_arg += 2; |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 9805 | } else if (strcmp(args[0], "redirect") == 0) { |
| 9806 | struct redirect_rule *redir; |
| 9807 | char *errmsg = NULL; |
| 9808 | |
| 9809 | if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 1)) == NULL) { |
| 9810 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n", |
| 9811 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
| 9812 | goto out_err; |
| 9813 | } |
| 9814 | |
| 9815 | /* this redirect rule might already contain a parsed condition which |
| 9816 | * we'll pass to the http-request rule. |
| 9817 | */ |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 9818 | rule->action = ACT_HTTP_REDIR; |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 9819 | rule->arg.redir = redir; |
| 9820 | rule->cond = redir->cond; |
| 9821 | redir->cond = NULL; |
| 9822 | cur_arg = 2; |
| 9823 | return rule; |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 9824 | } else if (((custom = action_http_res_custom(args[0])) != NULL)) { |
| 9825 | char *errmsg = NULL; |
| 9826 | cur_arg = 1; |
| 9827 | /* try in the module list */ |
Thierry FOURNIER | 5563e4b | 2015-08-14 19:20:07 +0200 | [diff] [blame] | 9828 | rule->from = ACT_F_HTTP_RES; |
Thierry FOURNIER | 85c6c97 | 2015-09-22 19:14:35 +0200 | [diff] [blame] | 9829 | rule->kw = custom; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 9830 | 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] | 9831 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n", |
| 9832 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
| 9833 | free(errmsg); |
| 9834 | goto out_err; |
| 9835 | } |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9836 | } else { |
Thierry FOURNIER | ab95e65 | 2015-10-02 08:24:51 +0200 | [diff] [blame] | 9837 | action_build_list(&http_res_keywords.list, &trash); |
| 9838 | Alert("parsing [%s:%d]: 'http-response' expects 'allow', 'deny', 'redirect', " |
| 9839 | "'add-header', 'del-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', " |
| 9840 | "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', " |
| 9841 | "'set-src'%s%s, but got '%s'%s.\n", |
| 9842 | file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)"); |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 9843 | goto out_err; |
| 9844 | } |
| 9845 | |
| 9846 | if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) { |
| 9847 | struct acl_cond *cond; |
| 9848 | char *errmsg = NULL; |
| 9849 | |
| 9850 | if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) { |
| 9851 | Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n", |
| 9852 | file, linenum, args[0], errmsg); |
| 9853 | free(errmsg); |
| 9854 | goto out_err; |
| 9855 | } |
| 9856 | rule->cond = cond; |
| 9857 | } |
| 9858 | else if (*args[cur_arg]) { |
| 9859 | Alert("parsing [%s:%d]: 'http-response %s' expects" |
| 9860 | " either 'if' or 'unless' followed by a condition but found '%s'.\n", |
| 9861 | file, linenum, args[0], args[cur_arg]); |
| 9862 | goto out_err; |
| 9863 | } |
| 9864 | |
| 9865 | return rule; |
| 9866 | out_err: |
| 9867 | free(rule); |
| 9868 | return NULL; |
| 9869 | } |
| 9870 | |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9871 | /* 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] | 9872 | * 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] | 9873 | * dynamic log-format rule instead of a static string. Parameter <dir> indicates |
| 9874 | * 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] | 9875 | */ |
| 9876 | 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] | 9877 | const char **args, char **errmsg, int use_fmt, int dir) |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9878 | { |
| 9879 | struct redirect_rule *rule; |
| 9880 | int cur_arg; |
| 9881 | int type = REDIRECT_TYPE_NONE; |
| 9882 | int code = 302; |
| 9883 | const char *destination = NULL; |
| 9884 | const char *cookie = NULL; |
| 9885 | int cookie_set = 0; |
| 9886 | unsigned int flags = REDIRECT_FLAG_NONE; |
| 9887 | struct acl_cond *cond = NULL; |
| 9888 | |
| 9889 | cur_arg = 0; |
| 9890 | while (*(args[cur_arg])) { |
| 9891 | if (strcmp(args[cur_arg], "location") == 0) { |
| 9892 | if (!*args[cur_arg + 1]) |
| 9893 | goto missing_arg; |
| 9894 | |
| 9895 | type = REDIRECT_TYPE_LOCATION; |
| 9896 | cur_arg++; |
| 9897 | destination = args[cur_arg]; |
| 9898 | } |
| 9899 | else if (strcmp(args[cur_arg], "prefix") == 0) { |
| 9900 | if (!*args[cur_arg + 1]) |
| 9901 | goto missing_arg; |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9902 | type = REDIRECT_TYPE_PREFIX; |
| 9903 | cur_arg++; |
| 9904 | destination = args[cur_arg]; |
| 9905 | } |
| 9906 | else if (strcmp(args[cur_arg], "scheme") == 0) { |
| 9907 | if (!*args[cur_arg + 1]) |
| 9908 | goto missing_arg; |
| 9909 | |
| 9910 | type = REDIRECT_TYPE_SCHEME; |
| 9911 | cur_arg++; |
| 9912 | destination = args[cur_arg]; |
| 9913 | } |
| 9914 | else if (strcmp(args[cur_arg], "set-cookie") == 0) { |
| 9915 | if (!*args[cur_arg + 1]) |
| 9916 | goto missing_arg; |
| 9917 | |
| 9918 | cur_arg++; |
| 9919 | cookie = args[cur_arg]; |
| 9920 | cookie_set = 1; |
| 9921 | } |
| 9922 | else if (strcmp(args[cur_arg], "clear-cookie") == 0) { |
| 9923 | if (!*args[cur_arg + 1]) |
| 9924 | goto missing_arg; |
| 9925 | |
| 9926 | cur_arg++; |
| 9927 | cookie = args[cur_arg]; |
| 9928 | cookie_set = 0; |
| 9929 | } |
| 9930 | else if (strcmp(args[cur_arg], "code") == 0) { |
| 9931 | if (!*args[cur_arg + 1]) |
| 9932 | goto missing_arg; |
| 9933 | |
| 9934 | cur_arg++; |
| 9935 | code = atol(args[cur_arg]); |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 9936 | if (code < 301 || code > 308 || (code > 303 && code < 307)) { |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9937 | memprintf(errmsg, |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 9938 | "'%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] | 9939 | args[cur_arg - 1], args[cur_arg]); |
| 9940 | return NULL; |
| 9941 | } |
| 9942 | } |
| 9943 | else if (!strcmp(args[cur_arg],"drop-query")) { |
| 9944 | flags |= REDIRECT_FLAG_DROP_QS; |
| 9945 | } |
| 9946 | else if (!strcmp(args[cur_arg],"append-slash")) { |
| 9947 | flags |= REDIRECT_FLAG_APPEND_SLASH; |
| 9948 | } |
| 9949 | else if (strcmp(args[cur_arg], "if") == 0 || |
| 9950 | strcmp(args[cur_arg], "unless") == 0) { |
| 9951 | cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg); |
| 9952 | if (!cond) { |
| 9953 | memprintf(errmsg, "error in condition: %s", *errmsg); |
| 9954 | return NULL; |
| 9955 | } |
| 9956 | break; |
| 9957 | } |
| 9958 | else { |
| 9959 | memprintf(errmsg, |
| 9960 | "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')", |
| 9961 | args[cur_arg]); |
| 9962 | return NULL; |
| 9963 | } |
| 9964 | cur_arg++; |
| 9965 | } |
| 9966 | |
| 9967 | if (type == REDIRECT_TYPE_NONE) { |
| 9968 | memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')"); |
| 9969 | return NULL; |
| 9970 | } |
| 9971 | |
Willy Tarreau | be4653b | 2015-05-28 15:26:58 +0200 | [diff] [blame] | 9972 | if (dir && type != REDIRECT_TYPE_LOCATION) { |
| 9973 | memprintf(errmsg, "response only supports redirect type 'location'"); |
| 9974 | return NULL; |
| 9975 | } |
| 9976 | |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 9977 | rule = (struct redirect_rule *)calloc(1, sizeof(*rule)); |
| 9978 | rule->cond = cond; |
Willy Tarreau | 60e0838 | 2013-12-03 00:48:45 +0100 | [diff] [blame] | 9979 | LIST_INIT(&rule->rdr_fmt); |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 9980 | |
| 9981 | if (!use_fmt) { |
| 9982 | /* old-style static redirect rule */ |
| 9983 | rule->rdr_str = strdup(destination); |
| 9984 | rule->rdr_len = strlen(destination); |
| 9985 | } |
| 9986 | else { |
| 9987 | /* log-format based redirect rule */ |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 9988 | |
| 9989 | /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case, |
| 9990 | * if prefix == "/", we don't want to add anything, otherwise it |
| 9991 | * makes it hard for the user to configure a self-redirection. |
| 9992 | */ |
Godbach | d972203 | 2014-12-18 15:44:58 +0800 | [diff] [blame] | 9993 | curproxy->conf.args.ctx = ARGC_RDR; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 9994 | if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) { |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 9995 | parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP, |
Willy Tarreau | be4653b | 2015-05-28 15:26:58 +0200 | [diff] [blame] | 9996 | dir ? (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRS_HDR : SMP_VAL_BE_HRS_HDR |
| 9997 | : (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
Thierry FOURNIER | eeaa951 | 2014-02-11 14:00:19 +0100 | [diff] [blame] | 9998 | file, linenum); |
Willy Tarreau | 59ad1a2 | 2014-01-29 14:39:58 +0100 | [diff] [blame] | 9999 | free(curproxy->conf.lfs_file); |
| 10000 | curproxy->conf.lfs_file = strdup(curproxy->conf.args.file); |
| 10001 | curproxy->conf.lfs_line = curproxy->conf.args.line; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 10002 | } |
| 10003 | } |
| 10004 | |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 10005 | if (cookie) { |
| 10006 | /* depending on cookie_set, either we want to set the cookie, or to clear it. |
| 10007 | * a clear consists in appending "; path=/; Max-Age=0;" at the end. |
| 10008 | */ |
| 10009 | rule->cookie_len = strlen(cookie); |
| 10010 | if (cookie_set) { |
| 10011 | rule->cookie_str = malloc(rule->cookie_len + 10); |
| 10012 | memcpy(rule->cookie_str, cookie, rule->cookie_len); |
| 10013 | memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10); |
| 10014 | rule->cookie_len += 9; |
| 10015 | } else { |
| 10016 | rule->cookie_str = malloc(rule->cookie_len + 21); |
| 10017 | memcpy(rule->cookie_str, cookie, rule->cookie_len); |
| 10018 | memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21); |
| 10019 | rule->cookie_len += 20; |
| 10020 | } |
| 10021 | } |
| 10022 | rule->type = type; |
| 10023 | rule->code = code; |
| 10024 | rule->flags = flags; |
| 10025 | LIST_INIT(&rule->list); |
| 10026 | return rule; |
| 10027 | |
| 10028 | missing_arg: |
| 10029 | memprintf(errmsg, "missing argument for '%s'", args[cur_arg]); |
| 10030 | return NULL; |
| 10031 | } |
| 10032 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10033 | /************************************************************************/ |
| 10034 | /* The code below is dedicated to ACL parsing and matching */ |
| 10035 | /************************************************************************/ |
| 10036 | |
| 10037 | |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10038 | /* This function ensures that the prerequisites for an L7 fetch are ready, |
| 10039 | * which means that a request or response is ready. If some data is missing, |
| 10040 | * 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] | 10041 | * to extract data from L7. If <req_vol> is non-null during a request prefetch, |
| 10042 | * another test is made to ensure the required information is not gone. |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10043 | * |
| 10044 | * The function returns : |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 10045 | * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to |
| 10046 | * decide whether or not an HTTP message is present ; |
| 10047 | * 0 if the requested data cannot be fetched or if it is certain that |
| 10048 | * we'll never have any HTTP message there ; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10049 | * 1 if an HTTP message is ready |
| 10050 | */ |
James Rosewell | 91a41cb | 2015-09-18 17:11:16 +0100 | [diff] [blame] | 10051 | 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] | 10052 | const struct arg *args, struct sample *smp, int req_vol) |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10053 | { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 10054 | struct http_txn *txn; |
| 10055 | struct http_msg *msg; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10056 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 10057 | /* Note: this function may only be used from places where |
| 10058 | * http_init_txn() has already been done, and implies that <s>, |
| 10059 | * <txn>, and <hdr_idx.v> are properly set. An extra check protects |
| 10060 | * against an eventual mistake in the fetch capability matrix. |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10061 | */ |
| 10062 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 10063 | if (!s) |
| 10064 | return 0; |
Thierry FOURNIER | ed08d6a | 2015-09-24 08:40:18 +0200 | [diff] [blame] | 10065 | if (!s->txn) { |
| 10066 | if (unlikely(!http_alloc_txn(s))) |
| 10067 | return 0; /* not enough memory */ |
| 10068 | http_init_txn(s); |
| 10069 | } |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 10070 | txn = s->txn; |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 10071 | msg = &txn->req; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10072 | |
| 10073 | /* Check for a dependency on a request */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10074 | smp->data.type = SMP_T_BOOL; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10075 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 10076 | if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 10077 | /* If the buffer does not leave enough free space at the end, |
| 10078 | * we must first realign it. |
| 10079 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 10080 | if (s->req.buf->p > s->req.buf->data && |
| 10081 | s->req.buf->i + s->req.buf->p > s->req.buf->data + s->req.buf->size - global.tune.maxrewrite) |
| 10082 | buffer_slow_realign(s->req.buf); |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 10083 | |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10084 | if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 472b1ee | 2013-10-14 22:41:30 +0200 | [diff] [blame] | 10085 | if (msg->msg_state == HTTP_MSG_ERROR) |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 10086 | return 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10087 | |
| 10088 | /* Try to decode HTTP request */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 10089 | if (likely(msg->next < s->req.buf->i)) |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10090 | http_msg_analyzer(msg, &txn->hdr_idx); |
| 10091 | |
| 10092 | /* Still no valid request ? */ |
| 10093 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 3bf1b2b | 2012-08-27 20:46:07 +0200 | [diff] [blame] | 10094 | if ((msg->msg_state == HTTP_MSG_ERROR) || |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 10095 | buffer_full(s->req.buf, global.tune.maxrewrite)) { |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 10096 | return 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10097 | } |
| 10098 | /* wait for final state */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10099 | smp->flags |= SMP_F_MAY_CHANGE; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10100 | return 0; |
| 10101 | } |
| 10102 | |
| 10103 | /* OK we just got a valid HTTP request. We have some minor |
| 10104 | * preparation to perform so that further checks can rely |
| 10105 | * on HTTP tests. |
| 10106 | */ |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 10107 | |
| 10108 | /* If the request was parsed but was too large, we must absolutely |
| 10109 | * return an error so that it is not processed. At the moment this |
| 10110 | * cannot happen, but if the parsers are to change in the future, |
| 10111 | * we want this check to be maintained. |
| 10112 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 10113 | if (unlikely(s->req.buf->i + s->req.buf->p > |
| 10114 | s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)) { |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 10115 | msg->msg_state = HTTP_MSG_ERROR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10116 | smp->data.u.sint = 1; |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 10117 | return 1; |
| 10118 | } |
| 10119 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10120 | 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] | 10121 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 10122 | s->flags |= SF_REDIRECTABLE; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10123 | |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 10124 | if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn)) |
| 10125 | return 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10126 | } |
| 10127 | |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 10128 | if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) { |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10129 | return 0; /* data might have moved and indexes changed */ |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 10130 | } |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10131 | |
| 10132 | /* otherwise everything's ready for the request */ |
| 10133 | } |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10134 | else { |
| 10135 | /* Check for a dependency on a response */ |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 10136 | if (txn->rsp.msg_state < HTTP_MSG_BODY) { |
| 10137 | smp->flags |= SMP_F_MAY_CHANGE; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10138 | return 0; |
Willy Tarreau | 506d050 | 2013-07-06 13:29:24 +0200 | [diff] [blame] | 10139 | } |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10140 | } |
| 10141 | |
| 10142 | /* everything's OK */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10143 | smp->data.u.sint = 1; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 10144 | return 1; |
| 10145 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10146 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10147 | /* 1. Check on METHOD |
| 10148 | * We use the pre-parsed method if it is known, and store its number as an |
| 10149 | * integer. If it is unknown, we use the pointer and the length. |
| 10150 | */ |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 10151 | 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] | 10152 | { |
| 10153 | int len, meth; |
| 10154 | |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 10155 | len = strlen(text); |
| 10156 | meth = find_http_meth(text, len); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10157 | |
| 10158 | pattern->val.i = meth; |
| 10159 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | 912c119 | 2014-08-29 15:15:50 +0200 | [diff] [blame] | 10160 | pattern->ptr.str = (char *)text; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10161 | pattern->len = len; |
| 10162 | } |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 10163 | else { |
| 10164 | pattern->ptr.str = NULL; |
| 10165 | pattern->len = 0; |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 10166 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10167 | return 1; |
| 10168 | } |
| 10169 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 10170 | /* This function fetches the method of current HTTP request and stores |
| 10171 | * it in the global pattern struct as a chunk. There are two possibilities : |
| 10172 | * - if the method is known (not HTTP_METH_OTHER), its identifier is stored |
| 10173 | * in <len> and <ptr> is NULL ; |
| 10174 | * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and |
| 10175 | * <len> to its length. |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 10176 | * This is intended to be used with pat_match_meth() only. |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 10177 | */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 10178 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10179 | 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] | 10180 | { |
| 10181 | int meth; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10182 | struct http_txn *txn = smp->strm->txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10183 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10184 | CHECK_HTTP_MESSAGE_FIRST_PERM(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10185 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10186 | meth = txn->meth; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10187 | smp->data.type = SMP_T_METH; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10188 | smp->data.u.meth.meth = meth; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10189 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10190 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 10191 | /* ensure the indexes are not affected */ |
| 10192 | return 0; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10193 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10194 | smp->data.u.meth.str.len = txn->req.sl.rq.m_l; |
| 10195 | smp->data.u.meth.str.str = txn->req.chn->buf->p; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10196 | } |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10197 | smp->flags |= SMP_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10198 | return 1; |
| 10199 | } |
| 10200 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 10201 | /* See above how the method is stored in the global pattern */ |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 10202 | 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] | 10203 | { |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 10204 | int icase; |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 10205 | struct pattern_list *lst; |
| 10206 | struct pattern *pattern; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 10207 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 10208 | list_for_each_entry(lst, &expr->patterns, list) { |
| 10209 | pattern = &lst->pat; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10210 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 10211 | /* well-known method */ |
| 10212 | if (pattern->val.i != HTTP_METH_OTHER) { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10213 | if (smp->data.u.meth.meth == pattern->val.i) |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 10214 | return pattern; |
| 10215 | else |
| 10216 | continue; |
| 10217 | } |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 10218 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 10219 | /* Other method, we must compare the strings */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10220 | if (pattern->len != smp->data.u.meth.str.len) |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 10221 | continue; |
| 10222 | |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 10223 | icase = expr->mflags & PAT_MF_IGNORE_CASE; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10224 | if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.meth.str.str, smp->data.u.meth.str.len) == 0) || |
| 10225 | (!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] | 10226 | return pattern; |
| 10227 | } |
| 10228 | return NULL; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10229 | } |
| 10230 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 10231 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10232 | 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] | 10233 | { |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10234 | struct http_txn *txn = smp->strm->txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10235 | char *ptr; |
| 10236 | int len; |
| 10237 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10238 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10239 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10240 | len = txn->req.sl.rq.v_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10241 | ptr = txn->req.chn->buf->p + txn->req.sl.rq.v; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10242 | |
| 10243 | while ((len-- > 0) && (*ptr++ != '/')); |
| 10244 | if (len <= 0) |
| 10245 | return 0; |
| 10246 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10247 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10248 | smp->data.u.str.str = ptr; |
| 10249 | smp->data.u.str.len = len; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10250 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10251 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10252 | return 1; |
| 10253 | } |
| 10254 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 10255 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10256 | 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] | 10257 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10258 | struct http_txn *txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10259 | char *ptr; |
| 10260 | int len; |
| 10261 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10262 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10263 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10264 | txn = smp->strm->txn; |
Willy Tarreau | f26b252 | 2012-12-14 08:33:14 +0100 | [diff] [blame] | 10265 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
| 10266 | return 0; |
| 10267 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10268 | len = txn->rsp.sl.st.v_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10269 | ptr = txn->rsp.chn->buf->p; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10270 | |
| 10271 | while ((len-- > 0) && (*ptr++ != '/')); |
| 10272 | if (len <= 0) |
| 10273 | return 0; |
| 10274 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10275 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10276 | smp->data.u.str.str = ptr; |
| 10277 | smp->data.u.str.len = len; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10278 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10279 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10280 | return 1; |
| 10281 | } |
| 10282 | |
| 10283 | /* 3. Check on Status Code. We manipulate integers here. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 10284 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10285 | 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] | 10286 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10287 | struct http_txn *txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10288 | char *ptr; |
| 10289 | int len; |
| 10290 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10291 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10292 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10293 | txn = smp->strm->txn; |
Willy Tarreau | f26b252 | 2012-12-14 08:33:14 +0100 | [diff] [blame] | 10294 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
| 10295 | return 0; |
| 10296 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10297 | len = txn->rsp.sl.st.c_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10298 | ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10299 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10300 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10301 | smp->data.u.sint = __strl2ui(ptr, len); |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10302 | smp->flags = SMP_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10303 | return 1; |
| 10304 | } |
| 10305 | |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10306 | /* returns the longest available part of the body. This requires that the body |
| 10307 | * has been waited for using http-buffer-request. |
| 10308 | */ |
| 10309 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10310 | 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] | 10311 | { |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10312 | struct http_txn *txn = smp->strm->txn; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10313 | struct http_msg *msg; |
| 10314 | unsigned long len; |
| 10315 | unsigned long block1; |
| 10316 | char *body; |
| 10317 | struct chunk *temp; |
| 10318 | |
| 10319 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10320 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10321 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10322 | msg = &txn->req; |
| 10323 | else |
| 10324 | msg = &txn->rsp; |
| 10325 | |
| 10326 | len = http_body_bytes(msg); |
| 10327 | body = b_ptr(msg->chn->buf, -http_data_rewind(msg)); |
| 10328 | |
| 10329 | block1 = len; |
| 10330 | if (block1 > msg->chn->buf->data + msg->chn->buf->size - body) |
| 10331 | block1 = msg->chn->buf->data + msg->chn->buf->size - body; |
| 10332 | |
| 10333 | if (block1 == len) { |
| 10334 | /* buffer is not wrapped (or empty) */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10335 | smp->data.type = SMP_T_BIN; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10336 | smp->data.u.str.str = body; |
| 10337 | smp->data.u.str.len = len; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10338 | smp->flags = SMP_F_VOL_TEST | SMP_F_CONST; |
| 10339 | } |
| 10340 | else { |
| 10341 | /* buffer is wrapped, we need to defragment it */ |
| 10342 | temp = get_trash_chunk(); |
| 10343 | memcpy(temp->str, body, block1); |
| 10344 | memcpy(temp->str + block1, msg->chn->buf->data, len - block1); |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10345 | smp->data.type = SMP_T_BIN; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10346 | smp->data.u.str.str = temp->str; |
| 10347 | smp->data.u.str.len = len; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10348 | smp->flags = SMP_F_VOL_TEST; |
| 10349 | } |
| 10350 | return 1; |
| 10351 | } |
| 10352 | |
| 10353 | |
| 10354 | /* returns the available length of the body. This requires that the body |
| 10355 | * has been waited for using http-buffer-request. |
| 10356 | */ |
| 10357 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10358 | 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] | 10359 | { |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10360 | struct http_txn *txn = smp->strm->txn; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10361 | struct http_msg *msg; |
| 10362 | |
| 10363 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10364 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10365 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10366 | msg = &txn->req; |
| 10367 | else |
| 10368 | msg = &txn->rsp; |
| 10369 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10370 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10371 | smp->data.u.sint = http_body_bytes(msg); |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10372 | |
| 10373 | smp->flags = SMP_F_VOL_TEST; |
| 10374 | return 1; |
| 10375 | } |
| 10376 | |
| 10377 | |
| 10378 | /* returns the advertised length of the body, or the advertised size of the |
| 10379 | * chunks available in the buffer. This requires that the body has been waited |
| 10380 | * for using http-buffer-request. |
| 10381 | */ |
| 10382 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10383 | 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] | 10384 | { |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10385 | struct http_txn *txn = smp->strm->txn; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10386 | struct http_msg *msg; |
| 10387 | |
| 10388 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10389 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10390 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10391 | msg = &txn->req; |
| 10392 | else |
| 10393 | msg = &txn->rsp; |
| 10394 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10395 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10396 | smp->data.u.sint = msg->body_len; |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 10397 | |
| 10398 | smp->flags = SMP_F_VOL_TEST; |
| 10399 | return 1; |
| 10400 | } |
| 10401 | |
| 10402 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10403 | /* 4. Check on URL/URI. A pointer to the URI is stored. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 10404 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10405 | 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] | 10406 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10407 | struct http_txn *txn; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10408 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10409 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10410 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10411 | txn = smp->strm->txn; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10412 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10413 | smp->data.u.str.len = txn->req.sl.rq.u_l; |
| 10414 | 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] | 10415 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10416 | return 1; |
| 10417 | } |
| 10418 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10419 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10420 | 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] | 10421 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10422 | struct http_txn *txn; |
Willy Tarreau | 4c804ec | 2013-09-30 14:37:14 +0200 | [diff] [blame] | 10423 | struct sockaddr_storage addr; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10424 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10425 | CHECK_HTTP_MESSAGE_FIRST(); |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10426 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10427 | txn = smp->strm->txn; |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 10428 | 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] | 10429 | if (((struct sockaddr_in *)&addr)->sin_family != AF_INET) |
Willy Tarreau | f4362b3 | 2011-12-16 17:49:52 +0100 | [diff] [blame] | 10430 | return 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10431 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10432 | smp->data.type = SMP_T_IPV4; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10433 | smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10434 | smp->flags = 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10435 | return 1; |
| 10436 | } |
| 10437 | |
| 10438 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10439 | 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] | 10440 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10441 | struct http_txn *txn; |
Willy Tarreau | 4c804ec | 2013-09-30 14:37:14 +0200 | [diff] [blame] | 10442 | struct sockaddr_storage addr; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10443 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10444 | CHECK_HTTP_MESSAGE_FIRST(); |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10445 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10446 | txn = smp->strm->txn; |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 10447 | 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] | 10448 | if (((struct sockaddr_in *)&addr)->sin_family != AF_INET) |
| 10449 | return 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10450 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10451 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10452 | smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port); |
Willy Tarreau | 21e5b0e | 2012-04-23 19:25:44 +0200 | [diff] [blame] | 10453 | smp->flags = 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 10454 | return 1; |
| 10455 | } |
| 10456 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10457 | /* Fetch an HTTP header. A pointer to the beginning of the value is returned. |
| 10458 | * Accepts an optional argument of type string containing the header field name, |
| 10459 | * and an optional argument of type signed or unsigned integer to request an |
| 10460 | * 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] | 10461 | * headers are considered from the first one. It does not stop on commas and |
| 10462 | * returns full lines instead (useful for User-Agent or Date for example). |
| 10463 | */ |
| 10464 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10465 | 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] | 10466 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10467 | struct hdr_idx *idx; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10468 | struct hdr_ctx *ctx = smp->ctx.a[0]; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10469 | const struct http_msg *msg; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10470 | int occ = 0; |
| 10471 | const char *name_str = NULL; |
| 10472 | int name_len = 0; |
| 10473 | |
| 10474 | if (!ctx) { |
| 10475 | /* first call */ |
| 10476 | ctx = &static_hdr_ctx; |
| 10477 | ctx->idx = 0; |
| 10478 | smp->ctx.a[0] = ctx; |
| 10479 | } |
| 10480 | |
| 10481 | if (args) { |
| 10482 | if (args[0].type != ARGT_STR) |
| 10483 | return 0; |
| 10484 | name_str = args[0].data.str.str; |
| 10485 | name_len = args[0].data.str.len; |
| 10486 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 10487 | if (args[1].type == ARGT_SINT) |
| 10488 | occ = args[1].data.sint; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10489 | } |
| 10490 | |
| 10491 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10492 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10493 | idx = &smp->strm->txn->hdr_idx; |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10494 | 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] | 10495 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10496 | if (ctx && !(smp->flags & SMP_F_NOT_LAST)) |
| 10497 | /* search for header from the beginning */ |
| 10498 | ctx->idx = 0; |
| 10499 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10500 | if (!occ && !(smp->opt & SMP_OPT_ITERATE)) |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10501 | /* no explicit occurrence and single fetch => last header by default */ |
| 10502 | occ = -1; |
| 10503 | |
| 10504 | if (!occ) |
| 10505 | /* prepare to report multiple occurrences for ACL fetches */ |
| 10506 | smp->flags |= SMP_F_NOT_LAST; |
| 10507 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10508 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10509 | smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10510 | 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] | 10511 | return 1; |
| 10512 | |
| 10513 | smp->flags &= ~SMP_F_NOT_LAST; |
| 10514 | return 0; |
| 10515 | } |
| 10516 | |
| 10517 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
| 10518 | * Accepts exactly 1 argument of type string. It does not stop on commas and |
| 10519 | * returns full lines instead (useful for User-Agent or Date for example). |
| 10520 | */ |
| 10521 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10522 | 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] | 10523 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10524 | struct hdr_idx *idx; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10525 | struct hdr_ctx ctx; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10526 | const struct http_msg *msg; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10527 | int cnt; |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10528 | const char *name = NULL; |
| 10529 | int len = 0; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10530 | |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10531 | if (args && args->type == ARGT_STR) { |
| 10532 | name = args->data.str.str; |
| 10533 | len = args->data.str.len; |
| 10534 | } |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10535 | |
| 10536 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10537 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10538 | idx = &smp->strm->txn->hdr_idx; |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10539 | 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] | 10540 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10541 | ctx.idx = 0; |
| 10542 | cnt = 0; |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10543 | 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] | 10544 | cnt++; |
| 10545 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10546 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10547 | smp->data.u.sint = cnt; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10548 | smp->flags = SMP_F_VOL_HDR; |
| 10549 | return 1; |
| 10550 | } |
| 10551 | |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 10552 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10553 | 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] | 10554 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10555 | struct hdr_idx *idx; |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 10556 | struct hdr_ctx ctx; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10557 | const struct http_msg *msg; |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 10558 | struct chunk *temp; |
| 10559 | char del = ','; |
| 10560 | |
| 10561 | if (args && args->type == ARGT_STR) |
| 10562 | del = *args[0].data.str.str; |
| 10563 | |
| 10564 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10565 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10566 | idx = &smp->strm->txn->hdr_idx; |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10567 | 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] | 10568 | |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 10569 | temp = get_trash_chunk(); |
| 10570 | |
| 10571 | ctx.idx = 0; |
| 10572 | while (http_find_next_header(msg->chn->buf->p, idx, &ctx)) { |
| 10573 | if (temp->len) |
| 10574 | temp->str[temp->len++] = del; |
| 10575 | memcpy(temp->str + temp->len, ctx.line, ctx.del); |
| 10576 | temp->len += ctx.del; |
| 10577 | } |
| 10578 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10579 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10580 | smp->data.u.str.str = temp->str; |
| 10581 | smp->data.u.str.len = temp->len; |
Willy Tarreau | eb27ec7 | 2015-02-20 13:55:29 +0100 | [diff] [blame] | 10582 | smp->flags = SMP_F_VOL_HDR; |
| 10583 | return 1; |
| 10584 | } |
| 10585 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 10586 | /* Fetch an HTTP header. A pointer to the beginning of the value is returned. |
| 10587 | * Accepts an optional argument of type string containing the header field name, |
| 10588 | * and an optional argument of type signed or unsigned integer to request an |
| 10589 | * 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] | 10590 | * headers are considered from the first one. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10591 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10592 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10593 | 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] | 10594 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10595 | struct hdr_idx *idx; |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 10596 | struct hdr_ctx *ctx = smp->ctx.a[0]; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10597 | const struct http_msg *msg; |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10598 | int occ = 0; |
| 10599 | const char *name_str = NULL; |
| 10600 | int name_len = 0; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10601 | |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 10602 | if (!ctx) { |
| 10603 | /* first call */ |
| 10604 | ctx = &static_hdr_ctx; |
| 10605 | ctx->idx = 0; |
Willy Tarreau | ffb6f08 | 2013-04-02 23:16:53 +0200 | [diff] [blame] | 10606 | smp->ctx.a[0] = ctx; |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 10607 | } |
| 10608 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10609 | if (args) { |
| 10610 | if (args[0].type != ARGT_STR) |
| 10611 | return 0; |
| 10612 | name_str = args[0].data.str.str; |
| 10613 | name_len = args[0].data.str.len; |
| 10614 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 10615 | if (args[1].type == ARGT_SINT) |
| 10616 | occ = args[1].data.sint; |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10617 | } |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10618 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10619 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10620 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10621 | idx = &smp->strm->txn->hdr_idx; |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10622 | 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] | 10623 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10624 | if (ctx && !(smp->flags & SMP_F_NOT_LAST)) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10625 | /* search for header from the beginning */ |
| 10626 | ctx->idx = 0; |
| 10627 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10628 | if (!occ && !(smp->opt & SMP_OPT_ITERATE)) |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10629 | /* no explicit occurrence and single fetch => last header by default */ |
| 10630 | occ = -1; |
| 10631 | |
| 10632 | if (!occ) |
| 10633 | /* prepare to report multiple occurrences for ACL fetches */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10634 | smp->flags |= SMP_F_NOT_LAST; |
Willy Tarreau | 664092c | 2011-12-16 19:11:42 +0100 | [diff] [blame] | 10635 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10636 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10637 | smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10638 | 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] | 10639 | return 1; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10640 | |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10641 | smp->flags &= ~SMP_F_NOT_LAST; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10642 | return 0; |
| 10643 | } |
| 10644 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10645 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10646 | * Accepts exactly 1 argument of type string. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10647 | */ |
| 10648 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10649 | 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] | 10650 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10651 | struct hdr_idx *idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10652 | struct hdr_ctx ctx; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10653 | const struct http_msg *msg; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10654 | int cnt; |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10655 | const char *name = NULL; |
| 10656 | int len = 0; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10657 | |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10658 | if (args && args->type == ARGT_STR) { |
| 10659 | name = args->data.str.str; |
| 10660 | len = args->data.str.len; |
| 10661 | } |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10662 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10663 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10664 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10665 | idx = &smp->strm->txn->hdr_idx; |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10666 | 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] | 10667 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10668 | ctx.idx = 0; |
| 10669 | cnt = 0; |
Willy Tarreau | 601a4d1 | 2015-04-01 19:16:09 +0200 | [diff] [blame] | 10670 | while (http_find_header2(name, len, msg->chn->buf->p, idx, &ctx)) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10671 | cnt++; |
| 10672 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10673 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10674 | smp->data.u.sint = cnt; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 10675 | smp->flags = SMP_F_VOL_HDR; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10676 | return 1; |
| 10677 | } |
| 10678 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10679 | /* Fetch an HTTP header's integer value. The integer value is returned. It |
| 10680 | * takes a mandatory argument of type string and an optional one of type int |
| 10681 | * to designate a specific occurrence. It returns an unsigned integer, which |
| 10682 | * may or may not be appropriate for everything. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10683 | */ |
| 10684 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10685 | 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] | 10686 | { |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10687 | int ret = smp_fetch_hdr(args, smp, kw, private); |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10688 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 10689 | if (ret > 0) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10690 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10691 | 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] | 10692 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10693 | |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10694 | return ret; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10695 | } |
| 10696 | |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 10697 | /* Fetch an HTTP header's IP value. takes a mandatory argument of type string |
| 10698 | * and an optional one of type int to designate a specific occurrence. |
| 10699 | * It returns an IPv4 or IPv6 address. |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 10700 | */ |
| 10701 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10702 | 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] | 10703 | { |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10704 | int ret; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 10705 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10706 | while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10707 | if (url2ipv4((char *)smp->data.u.str.str, &smp->data.u.ipv4)) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10708 | smp->data.type = SMP_T_IPV4; |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10709 | break; |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 10710 | } else { |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 10711 | struct chunk *temp = get_trash_chunk(); |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10712 | if (smp->data.u.str.len < temp->size - 1) { |
| 10713 | memcpy(temp->str, smp->data.u.str.str, smp->data.u.str.len); |
| 10714 | temp->str[smp->data.u.str.len] = '\0'; |
| 10715 | if (inet_pton(AF_INET6, temp->str, &smp->data.u.ipv6)) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10716 | smp->data.type = SMP_T_IPV6; |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 10717 | break; |
| 10718 | } |
| 10719 | } |
| 10720 | } |
| 10721 | |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10722 | /* if the header doesn't match an IP address, fetch next one */ |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 10723 | if (!(smp->flags & SMP_F_NOT_LAST)) |
| 10724 | return 0; |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 10725 | } |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 10726 | return ret; |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 10727 | } |
| 10728 | |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10729 | /* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at |
| 10730 | * the first '/' after the possible hostname, and ends before the possible '?'. |
| 10731 | */ |
| 10732 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10733 | 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] | 10734 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10735 | struct http_txn *txn; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10736 | char *ptr, *end; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 10737 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10738 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 10739 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10740 | txn = smp->strm->txn; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 10741 | 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] | 10742 | ptr = http_get_path(txn); |
| 10743 | if (!ptr) |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10744 | return 0; |
| 10745 | |
| 10746 | /* OK, we got the '/' ! */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10747 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10748 | smp->data.u.str.str = ptr; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10749 | |
| 10750 | while (ptr < end && *ptr != '?') |
| 10751 | ptr++; |
| 10752 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10753 | smp->data.u.str.len = ptr - smp->data.u.str.str; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10754 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 10755 | return 1; |
| 10756 | } |
| 10757 | |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10758 | /* This produces a concatenation of the first occurrence of the Host header |
| 10759 | * followed by the path component if it begins with a slash ('/'). This means |
| 10760 | * that '*' will not be added, resulting in exactly the first Host entry. |
| 10761 | * If no Host header is found, then the path is returned as-is. The returned |
| 10762 | * 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] | 10763 | * The returned sample is of type string. |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10764 | */ |
| 10765 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10766 | 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] | 10767 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10768 | struct http_txn *txn; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10769 | char *ptr, *end, *beg; |
| 10770 | struct hdr_ctx ctx; |
Willy Tarreau | 3caf2af | 2014-06-24 17:27:02 +0200 | [diff] [blame] | 10771 | struct chunk *temp; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10772 | |
| 10773 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10774 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10775 | txn = smp->strm->txn; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10776 | ctx.idx = 0; |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 10777 | 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] | 10778 | return smp_fetch_path(args, smp, kw, private); |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10779 | |
| 10780 | /* 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] | 10781 | temp = get_trash_chunk(); |
| 10782 | memcpy(temp->str, ctx.line + ctx.val, ctx.vlen); |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10783 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10784 | smp->data.u.str.str = temp->str; |
| 10785 | smp->data.u.str.len = ctx.vlen; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10786 | |
| 10787 | /* now retrieve the path */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 10788 | 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] | 10789 | beg = http_get_path(txn); |
| 10790 | if (!beg) |
| 10791 | beg = end; |
| 10792 | |
| 10793 | for (ptr = beg; ptr < end && *ptr != '?'; ptr++); |
| 10794 | |
| 10795 | if (beg < ptr && *beg == '/') { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10796 | memcpy(smp->data.u.str.str + smp->data.u.str.len, beg, ptr - beg); |
| 10797 | smp->data.u.str.len += ptr - beg; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 10798 | } |
| 10799 | |
| 10800 | smp->flags = SMP_F_VOL_1ST; |
| 10801 | return 1; |
| 10802 | } |
| 10803 | |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 10804 | /* This produces a 32-bit hash of the concatenation of the first occurrence of |
| 10805 | * the Host header followed by the path component if it begins with a slash ('/'). |
| 10806 | * This means that '*' will not be added, resulting in exactly the first Host |
| 10807 | * 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] | 10808 | * is hashed using the path hash followed by a full avalanche hash and provides a |
| 10809 | * 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] | 10810 | * high-traffic sites without having to store whole paths. |
| 10811 | */ |
Thierry FOURNIER | 055b9d5 | 2014-07-15 16:11:07 +0200 | [diff] [blame] | 10812 | int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10813 | 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] | 10814 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10815 | struct http_txn *txn; |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 10816 | struct hdr_ctx ctx; |
| 10817 | unsigned int hash = 0; |
| 10818 | char *ptr, *beg, *end; |
| 10819 | int len; |
| 10820 | |
| 10821 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10822 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10823 | txn = smp->strm->txn; |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 10824 | ctx.idx = 0; |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 10825 | 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] | 10826 | /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */ |
| 10827 | ptr = ctx.line + ctx.val; |
| 10828 | len = ctx.vlen; |
| 10829 | while (len--) |
| 10830 | hash = *(ptr++) + (hash << 6) + (hash << 16) - hash; |
| 10831 | } |
| 10832 | |
| 10833 | /* now retrieve the path */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 10834 | 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] | 10835 | beg = http_get_path(txn); |
| 10836 | if (!beg) |
| 10837 | beg = end; |
| 10838 | |
| 10839 | for (ptr = beg; ptr < end && *ptr != '?'; ptr++); |
| 10840 | |
| 10841 | if (beg < ptr && *beg == '/') { |
| 10842 | while (beg < ptr) |
| 10843 | hash = *(beg++) + (hash << 6) + (hash << 16) - hash; |
| 10844 | } |
| 10845 | hash = full_hash(hash); |
| 10846 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10847 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10848 | smp->data.u.sint = hash; |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 10849 | smp->flags = SMP_F_VOL_1ST; |
| 10850 | return 1; |
| 10851 | } |
| 10852 | |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10853 | /* 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] | 10854 | * path as returned by smp_fetch_base32(). The idea is to have per-source and |
| 10855 | * per-path counters. The result is a binary block from 8 to 20 bytes depending |
| 10856 | * 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] | 10857 | * that in environments where IPv6 is insignificant, truncating the output to |
| 10858 | * 8 bytes would still work. |
| 10859 | */ |
| 10860 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10861 | 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] | 10862 | { |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 10863 | struct chunk *temp; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10864 | struct connection *cli_conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10865 | |
| 10866 | if (!cli_conn) |
| 10867 | return 0; |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10868 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10869 | if (!smp_fetch_base32(args, smp, kw, private)) |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10870 | return 0; |
| 10871 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 10872 | temp = get_trash_chunk(); |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10873 | *(unsigned int *)temp->str = htonl(smp->data.u.sint); |
Willy Tarreau | 5ad6e1d | 2014-07-15 21:34:06 +0200 | [diff] [blame] | 10874 | temp->len += sizeof(unsigned int); |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10875 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10876 | switch (cli_conn->addr.from.ss_family) { |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10877 | case AF_INET: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10878 | 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] | 10879 | temp->len += 4; |
| 10880 | break; |
| 10881 | case AF_INET6: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 10882 | 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] | 10883 | temp->len += 16; |
| 10884 | break; |
| 10885 | default: |
| 10886 | return 0; |
| 10887 | } |
| 10888 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10889 | smp->data.u.str = *temp; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10890 | smp->data.type = SMP_T_BIN; |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 10891 | return 1; |
| 10892 | } |
| 10893 | |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 10894 | /* Extracts the query string, which comes after the question mark '?'. If no |
| 10895 | * question mark is found, nothing is returned. Otherwise it returns a sample |
| 10896 | * of type string carrying the whole query string. |
| 10897 | */ |
| 10898 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10899 | 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] | 10900 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 10901 | struct http_txn *txn; |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 10902 | char *ptr, *end; |
| 10903 | |
| 10904 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10905 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10906 | txn = smp->strm->txn; |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 10907 | ptr = txn->req.chn->buf->p + txn->req.sl.rq.u; |
| 10908 | end = ptr + txn->req.sl.rq.u_l; |
| 10909 | |
| 10910 | /* look up the '?' */ |
| 10911 | do { |
| 10912 | if (ptr == end) |
| 10913 | return 0; |
| 10914 | } while (*ptr++ != '?'); |
| 10915 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10916 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10917 | smp->data.u.str.str = ptr; |
| 10918 | smp->data.u.str.len = end - ptr; |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 10919 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
| 10920 | return 1; |
| 10921 | } |
| 10922 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10923 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10924 | 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] | 10925 | { |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10926 | /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged |
| 10927 | * as a layer7 ACL, which involves automatic allocation of hdr_idx. |
| 10928 | */ |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10929 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10930 | CHECK_HTTP_MESSAGE_FIRST_PERM(); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10931 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10932 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10933 | smp->data.u.sint = 1; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 10934 | return 1; |
| 10935 | } |
| 10936 | |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 10937 | /* return a valid test if the current request is the first one on the connection */ |
| 10938 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10939 | 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] | 10940 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10941 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10942 | smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST); |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 10943 | return 1; |
| 10944 | } |
| 10945 | |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10946 | /* Accepts exactly 1 argument of type userlist */ |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 10947 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10948 | 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] | 10949 | { |
| 10950 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 10951 | if (!args || args->type != ARGT_USR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 10952 | return 0; |
| 10953 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 10954 | CHECK_HTTP_MESSAGE_FIRST(); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 10955 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10956 | if (!get_http_auth(smp->strm)) |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 10957 | return 0; |
| 10958 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10959 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10960 | 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] | 10961 | smp->strm->txn->auth.pass); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 10962 | return 1; |
| 10963 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 10964 | |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10965 | /* Accepts exactly 1 argument of type userlist */ |
| 10966 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 10967 | 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] | 10968 | { |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10969 | if (!args || args->type != ARGT_USR) |
| 10970 | return 0; |
| 10971 | |
| 10972 | CHECK_HTTP_MESSAGE_FIRST(); |
| 10973 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10974 | if (!get_http_auth(smp->strm)) |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10975 | return 0; |
| 10976 | |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10977 | /* if the user does not belong to the userlist or has a wrong password, |
| 10978 | * report that it unconditionally does not match. Otherwise we return |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 10979 | * a string containing the username. |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10980 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 10981 | if (!check_user(args->data.usr, smp->strm->txn->auth.user, |
| 10982 | smp->strm->txn->auth.pass)) |
Thierry FOURNIER | 9eec0a6 | 2014-01-22 18:38:02 +0100 | [diff] [blame] | 10983 | return 0; |
| 10984 | |
| 10985 | /* pat_match_auth() will need the user list */ |
| 10986 | smp->ctx.a[0] = args->data.usr; |
| 10987 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 10988 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10989 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 10990 | smp->data.u.str.str = smp->strm->txn->auth.user; |
| 10991 | smp->data.u.str.len = strlen(smp->strm->txn->auth.user); |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 10992 | |
| 10993 | return 1; |
| 10994 | } |
| 10995 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 10996 | /* Try to find the next occurrence of a cookie name in a cookie header value. |
| 10997 | * The lookup begins at <hdr>. The pointer and size of the next occurrence of |
| 10998 | * the cookie value is returned into *value and *value_l, and the function |
| 10999 | * returns a pointer to the next pointer to search from if the value was found. |
| 11000 | * Otherwise if the cookie was not found, NULL is returned and neither value |
| 11001 | * nor value_l are touched. The input <hdr> string should first point to the |
| 11002 | * header's value, and the <hdr_end> pointer must point to the first character |
| 11003 | * not part of the value. <list> must be non-zero if value may represent a list |
| 11004 | * of values (cookie headers). This makes it faster to abort parsing when no |
| 11005 | * list is expected. |
| 11006 | */ |
David Carlier | 4686f79 | 2015-09-25 14:10:50 +0100 | [diff] [blame] | 11007 | char * |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11008 | extract_cookie_value(char *hdr, const char *hdr_end, |
| 11009 | char *cookie_name, size_t cookie_name_l, int list, |
Willy Tarreau | 3fb818c | 2012-04-11 17:21:08 +0200 | [diff] [blame] | 11010 | char **value, int *value_l) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11011 | { |
| 11012 | char *equal, *att_end, *att_beg, *val_beg, *val_end; |
| 11013 | char *next; |
| 11014 | |
| 11015 | /* we search at least a cookie name followed by an equal, and more |
| 11016 | * generally something like this : |
| 11017 | * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n |
| 11018 | */ |
| 11019 | for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) { |
| 11020 | /* Iterate through all cookies on this line */ |
| 11021 | |
| 11022 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 11023 | att_beg++; |
| 11024 | |
| 11025 | /* find att_end : this is the first character after the last non |
| 11026 | * space before the equal. It may be equal to hdr_end. |
| 11027 | */ |
| 11028 | equal = att_end = att_beg; |
| 11029 | |
| 11030 | while (equal < hdr_end) { |
| 11031 | if (*equal == '=' || *equal == ';' || (list && *equal == ',')) |
| 11032 | break; |
| 11033 | if (http_is_spht[(unsigned char)*equal++]) |
| 11034 | continue; |
| 11035 | att_end = equal; |
| 11036 | } |
| 11037 | |
| 11038 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 11039 | * is between <att_beg> and <equal>, both may be identical. |
| 11040 | */ |
| 11041 | |
| 11042 | /* look for end of cookie if there is an equal sign */ |
| 11043 | if (equal < hdr_end && *equal == '=') { |
| 11044 | /* look for the beginning of the value */ |
| 11045 | val_beg = equal + 1; |
| 11046 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 11047 | val_beg++; |
| 11048 | |
| 11049 | /* find the end of the value, respecting quotes */ |
| 11050 | next = find_cookie_value_end(val_beg, hdr_end); |
| 11051 | |
| 11052 | /* make val_end point to the first white space or delimitor after the value */ |
| 11053 | val_end = next; |
| 11054 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 11055 | val_end--; |
| 11056 | } else { |
| 11057 | val_beg = val_end = next = equal; |
| 11058 | } |
| 11059 | |
| 11060 | /* We have nothing to do with attributes beginning with '$'. However, |
| 11061 | * they will automatically be removed if a header before them is removed, |
| 11062 | * since they're supposed to be linked together. |
| 11063 | */ |
| 11064 | if (*att_beg == '$') |
| 11065 | continue; |
| 11066 | |
| 11067 | /* Ignore cookies with no equal sign */ |
| 11068 | if (equal == next) |
| 11069 | continue; |
| 11070 | |
| 11071 | /* Now we have the cookie name between att_beg and att_end, and |
| 11072 | * its value between val_beg and val_end. |
| 11073 | */ |
| 11074 | |
| 11075 | if (att_end - att_beg == cookie_name_l && |
| 11076 | memcmp(att_beg, cookie_name, cookie_name_l) == 0) { |
| 11077 | /* let's return this value and indicate where to go on from */ |
| 11078 | *value = val_beg; |
| 11079 | *value_l = val_end - val_beg; |
| 11080 | return next + 1; |
| 11081 | } |
| 11082 | |
| 11083 | /* Set-Cookie headers only have the name in the first attr=value part */ |
| 11084 | if (!list) |
| 11085 | break; |
| 11086 | } |
| 11087 | |
| 11088 | return NULL; |
| 11089 | } |
| 11090 | |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 11091 | /* Fetch a captured HTTP request header. The index is the position of |
| 11092 | * the "capture" option in the configuration file |
| 11093 | */ |
| 11094 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11095 | 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] | 11096 | { |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11097 | struct proxy *fe = strm_fe(smp->strm); |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 11098 | int idx; |
| 11099 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 11100 | if (!args || args->type != ARGT_SINT) |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 11101 | return 0; |
| 11102 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 11103 | idx = args->data.sint; |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 11104 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11105 | 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] | 11106 | return 0; |
| 11107 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11108 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11109 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11110 | smp->data.u.str.str = smp->strm->req_cap[idx]; |
| 11111 | smp->data.u.str.len = strlen(smp->strm->req_cap[idx]); |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 11112 | |
| 11113 | return 1; |
| 11114 | } |
| 11115 | |
| 11116 | /* Fetch a captured HTTP response header. The index is the position of |
| 11117 | * the "capture" option in the configuration file |
| 11118 | */ |
| 11119 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11120 | 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] | 11121 | { |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11122 | struct proxy *fe = strm_fe(smp->strm); |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 11123 | int idx; |
| 11124 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 11125 | if (!args || args->type != ARGT_SINT) |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 11126 | return 0; |
| 11127 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 11128 | idx = args->data.sint; |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 11129 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11130 | 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] | 11131 | return 0; |
| 11132 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11133 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11134 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11135 | smp->data.u.str.str = smp->strm->res_cap[idx]; |
| 11136 | smp->data.u.str.len = strlen(smp->strm->res_cap[idx]); |
William Lallemand | a43ba4e | 2014-01-28 18:14:25 +0100 | [diff] [blame] | 11137 | |
| 11138 | return 1; |
| 11139 | } |
| 11140 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11141 | /* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */ |
| 11142 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11143 | 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] | 11144 | { |
| 11145 | struct chunk *temp; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11146 | struct http_txn *txn = smp->strm->txn; |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 11147 | char *ptr; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11148 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11149 | if (!txn || !txn->uri) |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11150 | return 0; |
| 11151 | |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 11152 | ptr = txn->uri; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11153 | |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 11154 | while (*ptr != ' ' && *ptr != '\0') /* find first space */ |
| 11155 | ptr++; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11156 | |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 11157 | temp = get_trash_chunk(); |
| 11158 | temp->str = txn->uri; |
| 11159 | temp->len = ptr - txn->uri; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11160 | smp->data.u.str = *temp; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11161 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11162 | smp->flags = SMP_F_CONST; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11163 | |
| 11164 | return 1; |
| 11165 | |
| 11166 | } |
| 11167 | |
| 11168 | /* Extracts the path in the HTTP request, the txn->uri should be filled before the call */ |
| 11169 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11170 | 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] | 11171 | { |
| 11172 | struct chunk *temp; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11173 | struct http_txn *txn = smp->strm->txn; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11174 | char *ptr; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11175 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11176 | if (!txn || !txn->uri) |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11177 | return 0; |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 11178 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11179 | ptr = txn->uri; |
| 11180 | |
| 11181 | while (*ptr != ' ' && *ptr != '\0') /* find first space */ |
| 11182 | ptr++; |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 11183 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11184 | if (!*ptr) |
| 11185 | return 0; |
| 11186 | |
| 11187 | ptr++; /* skip the space */ |
| 11188 | |
| 11189 | temp = get_trash_chunk(); |
William Lallemand | 96a7785 | 2014-02-05 00:30:02 +0100 | [diff] [blame] | 11190 | ptr = temp->str = http_get_path_from_string(ptr); |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11191 | if (!ptr) |
| 11192 | return 0; |
| 11193 | while (*ptr != ' ' && *ptr != '\0') /* find space after URI */ |
| 11194 | ptr++; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11195 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11196 | smp->data.u.str = *temp; |
| 11197 | smp->data.u.str.len = ptr - temp->str; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11198 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11199 | smp->flags = SMP_F_CONST; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11200 | |
| 11201 | return 1; |
| 11202 | } |
| 11203 | |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11204 | /* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it |
| 11205 | * as a string (either "HTTP/1.0" or "HTTP/1.1"). |
| 11206 | */ |
| 11207 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11208 | 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] | 11209 | { |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11210 | struct http_txn *txn = smp->strm->txn; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11211 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11212 | if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST) |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11213 | return 0; |
| 11214 | |
| 11215 | if (txn->req.flags & HTTP_MSGF_VER_11) |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11216 | smp->data.u.str.str = "HTTP/1.1"; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11217 | else |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11218 | smp->data.u.str.str = "HTTP/1.0"; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11219 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11220 | smp->data.u.str.len = 8; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11221 | smp->data.type = SMP_T_STR; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11222 | smp->flags = SMP_F_CONST; |
| 11223 | return 1; |
| 11224 | |
| 11225 | } |
| 11226 | |
| 11227 | /* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it |
| 11228 | * as a string (either "HTTP/1.0" or "HTTP/1.1"). |
| 11229 | */ |
| 11230 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11231 | 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] | 11232 | { |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11233 | struct http_txn *txn = smp->strm->txn; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11234 | |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11235 | if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST) |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11236 | return 0; |
| 11237 | |
| 11238 | if (txn->rsp.flags & HTTP_MSGF_VER_11) |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11239 | smp->data.u.str.str = "HTTP/1.1"; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11240 | else |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11241 | smp->data.u.str.str = "HTTP/1.0"; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11242 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11243 | smp->data.u.str.len = 8; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11244 | smp->data.type = SMP_T_STR; |
Willy Tarreau | 3c1b5ec | 2014-04-24 23:41:57 +0200 | [diff] [blame] | 11245 | smp->flags = SMP_F_CONST; |
| 11246 | return 1; |
| 11247 | |
| 11248 | } |
| 11249 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 11250 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11251 | /* 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] | 11252 | * 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] | 11253 | * 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] | 11254 | * 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] | 11255 | * 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] | 11256 | * Accepts exactly 1 argument of type string. If the input options indicate |
| 11257 | * 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] | 11258 | * The returned sample is of type CSTR. Can be used to parse cookies in other |
| 11259 | * files. |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11260 | */ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11261 | 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] | 11262 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11263 | struct http_txn *txn; |
| 11264 | struct hdr_idx *idx; |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 11265 | struct hdr_ctx *ctx = smp->ctx.a[2]; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11266 | const struct http_msg *msg; |
| 11267 | const char *hdr_name; |
| 11268 | int hdr_name_len; |
| 11269 | char *sol; |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11270 | int occ = 0; |
| 11271 | int found = 0; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11272 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 11273 | if (!args || args->type != ARGT_STR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 11274 | return 0; |
| 11275 | |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 11276 | if (!ctx) { |
| 11277 | /* first call */ |
| 11278 | ctx = &static_hdr_ctx; |
| 11279 | ctx->idx = 0; |
| 11280 | smp->ctx.a[2] = ctx; |
| 11281 | } |
| 11282 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11283 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11284 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11285 | txn = smp->strm->txn; |
| 11286 | idx = &smp->strm->txn->hdr_idx; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11287 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11288 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11289 | msg = &txn->req; |
| 11290 | hdr_name = "Cookie"; |
| 11291 | hdr_name_len = 6; |
| 11292 | } else { |
| 11293 | msg = &txn->rsp; |
| 11294 | hdr_name = "Set-Cookie"; |
| 11295 | hdr_name_len = 10; |
| 11296 | } |
| 11297 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11298 | if (!occ && !(smp->opt & SMP_OPT_ITERATE)) |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11299 | /* no explicit occurrence and single fetch => last cookie by default */ |
| 11300 | occ = -1; |
| 11301 | |
| 11302 | /* OK so basically here, either we want only one value and it's the |
| 11303 | * last one, or we want to iterate over all of them and we fetch the |
| 11304 | * next one. |
| 11305 | */ |
| 11306 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 11307 | sol = msg->chn->buf->p; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11308 | if (!(smp->flags & SMP_F_NOT_LAST)) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11309 | /* search for the header from the beginning, we must first initialize |
| 11310 | * the search parameters. |
| 11311 | */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11312 | smp->ctx.a[0] = NULL; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11313 | ctx->idx = 0; |
| 11314 | } |
| 11315 | |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11316 | smp->flags |= SMP_F_VOL_HDR; |
| 11317 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11318 | while (1) { |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11319 | /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */ |
| 11320 | if (!smp->ctx.a[0]) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11321 | if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx)) |
| 11322 | goto out; |
| 11323 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 11324 | if (ctx->vlen < args->data.str.len + 1) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11325 | continue; |
| 11326 | |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11327 | smp->ctx.a[0] = ctx->line + ctx->val; |
| 11328 | smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11329 | } |
| 11330 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11331 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11332 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11333 | 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] | 11334 | args->data.str.str, args->data.str.len, |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11335 | (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ, |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11336 | &smp->data.u.str.str, |
| 11337 | &smp->data.u.str.len); |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11338 | if (smp->ctx.a[0]) { |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11339 | found = 1; |
| 11340 | if (occ >= 0) { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11341 | /* one value was returned into smp->data.u.str.{str,len} */ |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11342 | smp->flags |= SMP_F_NOT_LAST; |
| 11343 | return 1; |
| 11344 | } |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11345 | } |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11346 | /* if we're looking for last occurrence, let's loop */ |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11347 | } |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11348 | /* all cookie headers and values were scanned. If we're looking for the |
| 11349 | * last occurrence, we may return it now. |
| 11350 | */ |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11351 | out: |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11352 | smp->flags &= ~SMP_F_NOT_LAST; |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 11353 | return found; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11354 | } |
| 11355 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11356 | /* 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] | 11357 | * 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] | 11358 | * multiple cookies may be parsed on the same line. The returned sample is of |
| 11359 | * type UINT. Accepts exactly 1 argument of type string. |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11360 | */ |
| 11361 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11362 | 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] | 11363 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11364 | struct http_txn *txn; |
| 11365 | struct hdr_idx *idx; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11366 | struct hdr_ctx ctx; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11367 | const struct http_msg *msg; |
| 11368 | const char *hdr_name; |
| 11369 | int hdr_name_len; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11370 | int cnt; |
| 11371 | char *val_beg, *val_end; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11372 | char *sol; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11373 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 11374 | if (!args || args->type != ARGT_STR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 11375 | return 0; |
| 11376 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11377 | CHECK_HTTP_MESSAGE_FIRST(); |
| 11378 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11379 | txn = smp->strm->txn; |
| 11380 | idx = &smp->strm->txn->hdr_idx; |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11381 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11382 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 11383 | msg = &txn->req; |
| 11384 | hdr_name = "Cookie"; |
| 11385 | hdr_name_len = 6; |
| 11386 | } else { |
| 11387 | msg = &txn->rsp; |
| 11388 | hdr_name = "Set-Cookie"; |
| 11389 | hdr_name_len = 10; |
| 11390 | } |
| 11391 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 11392 | sol = msg->chn->buf->p; |
Willy Tarreau | 46787ed | 2012-04-11 17:28:40 +0200 | [diff] [blame] | 11393 | val_end = val_beg = NULL; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11394 | ctx.idx = 0; |
| 11395 | cnt = 0; |
| 11396 | |
| 11397 | while (1) { |
| 11398 | /* Note: val_beg == NULL every time we need to fetch a new header */ |
| 11399 | if (!val_beg) { |
| 11400 | if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx)) |
| 11401 | break; |
| 11402 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 11403 | if (ctx.vlen < args->data.str.len + 1) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11404 | continue; |
| 11405 | |
| 11406 | val_beg = ctx.line + ctx.val; |
| 11407 | val_end = val_beg + ctx.vlen; |
| 11408 | } |
| 11409 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11410 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 11411 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11412 | while ((val_beg = extract_cookie_value(val_beg, val_end, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 11413 | args->data.str.str, args->data.str.len, |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11414 | (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ, |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11415 | &smp->data.u.str.str, |
| 11416 | &smp->data.u.str.len))) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11417 | cnt++; |
| 11418 | } |
| 11419 | } |
| 11420 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11421 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11422 | smp->data.u.sint = cnt; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 11423 | smp->flags |= SMP_F_VOL_HDR; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 11424 | return 1; |
| 11425 | } |
| 11426 | |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 11427 | /* Fetch an cookie's integer value. The integer value is returned. It |
| 11428 | * takes a mandatory argument of type string. It relies on smp_fetch_cookie(). |
| 11429 | */ |
| 11430 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11431 | 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] | 11432 | { |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11433 | int ret = smp_fetch_cookie(args, smp, kw, private); |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 11434 | |
| 11435 | if (ret > 0) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11436 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11437 | 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] | 11438 | } |
| 11439 | |
| 11440 | return ret; |
| 11441 | } |
| 11442 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 11443 | /************************************************************************/ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 11444 | /* The code below is dedicated to sample fetches */ |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 11445 | /************************************************************************/ |
| 11446 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11447 | /* |
| 11448 | * Given a path string and its length, find the position of beginning of the |
| 11449 | * query string. Returns NULL if no query string is found in the path. |
| 11450 | * |
| 11451 | * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22: |
| 11452 | * |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11453 | * find_query_string(path, n, '?') points to "yo=mama;ye=daddy" string. |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11454 | */ |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11455 | 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] | 11456 | { |
| 11457 | char *p; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 11458 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11459 | p = memchr(path, delim, path_l); |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11460 | return p ? p + 1 : NULL; |
| 11461 | } |
| 11462 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11463 | static inline int is_param_delimiter(char c, char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11464 | { |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11465 | return c == '&' || c == ';' || c == delim; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11466 | } |
| 11467 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11468 | /* after increasing a pointer value, it can exceed the first buffer |
| 11469 | * size. This function transform the value of <ptr> according with |
| 11470 | * the expected position. <chunks> is an array of the one or two |
| 11471 | * avalaible chunks. The first value is the start of the first chunk, |
| 11472 | * the second value if the end+1 of the first chunks. The third value |
| 11473 | * is NULL or the start of the second chunk and the fourth value is |
| 11474 | * the end+1 of the second chunk. The function returns 1 if does a |
| 11475 | * wrap, else returns 0. |
| 11476 | */ |
| 11477 | static inline int fix_pointer_if_wrap(const char **chunks, const char **ptr) |
| 11478 | { |
| 11479 | if (*ptr < chunks[1]) |
| 11480 | return 0; |
| 11481 | if (!chunks[2]) |
| 11482 | return 0; |
| 11483 | *ptr = chunks[2] + ( *ptr - chunks[1] ); |
| 11484 | return 1; |
| 11485 | } |
| 11486 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11487 | /* |
| 11488 | * Given a url parameter, find the starting position of the first occurence, |
| 11489 | * or NULL if the parameter is not found. |
| 11490 | * |
| 11491 | * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye", |
| 11492 | * the function will return query_string+8. |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11493 | * |
| 11494 | * Warning:this function returns a pointer that can be point to the first chunk |
| 11495 | * or the second chunk. The caller must be check the position before using the |
| 11496 | * result. |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11497 | */ |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11498 | static const char * |
| 11499 | find_url_param_pos(const char **chunks, |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11500 | const char* url_param_name, size_t url_param_name_l, |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11501 | char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11502 | { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11503 | const char *pos, *last, *equal; |
| 11504 | const char **bufs = chunks; |
| 11505 | int l1, l2; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11506 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11507 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11508 | pos = bufs[0]; |
| 11509 | last = bufs[1]; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11510 | while (pos <= last) { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11511 | /* Check the equal. */ |
| 11512 | equal = pos + url_param_name_l; |
| 11513 | if (fix_pointer_if_wrap(chunks, &equal)) { |
| 11514 | if (equal >= chunks[3]) |
| 11515 | return NULL; |
| 11516 | } else { |
| 11517 | if (equal >= chunks[1]) |
| 11518 | return NULL; |
| 11519 | } |
| 11520 | if (*equal == '=') { |
| 11521 | if (pos + url_param_name_l > last) { |
| 11522 | /* process wrap case, we detect a wrap. In this case, the |
| 11523 | * comparison is performed in two parts. |
| 11524 | */ |
| 11525 | |
| 11526 | /* This is the end, we dont have any other chunk. */ |
| 11527 | if (bufs != chunks || !bufs[2]) |
| 11528 | return NULL; |
| 11529 | |
| 11530 | /* Compute the length of each part of the comparison. */ |
| 11531 | l1 = last - pos; |
| 11532 | l2 = url_param_name_l - l1; |
| 11533 | |
| 11534 | /* The second buffer is too short to contain the compared string. */ |
| 11535 | if (bufs[2] + l2 > bufs[3]) |
| 11536 | return NULL; |
| 11537 | |
| 11538 | if (memcmp(pos, url_param_name, l1) == 0 && |
| 11539 | memcmp(bufs[2], url_param_name+l1, l2) == 0) |
| 11540 | return pos; |
| 11541 | |
| 11542 | /* Perform wrapping and jump the string who fail the comparison. */ |
| 11543 | bufs += 2; |
| 11544 | pos = bufs[0] + l2; |
| 11545 | last = bufs[1]; |
| 11546 | |
| 11547 | } else { |
| 11548 | /* process a simple comparison. */ |
| 11549 | if (memcmp(pos, url_param_name, url_param_name_l) == 0) { |
| 11550 | return pos; } |
| 11551 | pos += url_param_name_l + 1; |
| 11552 | if (fix_pointer_if_wrap(chunks, &pos)) |
| 11553 | last = bufs[2]; |
| 11554 | } |
| 11555 | } |
| 11556 | |
| 11557 | while (1) { |
| 11558 | /* Look for the next delimiter. */ |
| 11559 | while (pos <= last && !is_param_delimiter(*pos, delim)) |
| 11560 | pos++; |
| 11561 | if (pos < last) |
| 11562 | break; |
| 11563 | /* process buffer wrapping. */ |
| 11564 | if (bufs != chunks || !bufs[2]) |
| 11565 | return NULL; |
| 11566 | bufs += 2; |
| 11567 | pos = bufs[0]; |
| 11568 | last = bufs[1]; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11569 | } |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11570 | pos++; |
| 11571 | } |
| 11572 | return NULL; |
| 11573 | } |
| 11574 | |
| 11575 | /* |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11576 | * Given a url parameter name and a query string, returns its value and size |
| 11577 | * into *value and *value_l respectively, and returns non-zero. An empty |
| 11578 | * url_param_name matches the first available parameter. If the parameter is |
| 11579 | * not found, zero is returned and value/value_l are not touched. |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11580 | */ |
| 11581 | static int |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11582 | find_next_url_param(const char **chunks, |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11583 | const char* url_param_name, size_t url_param_name_l, |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11584 | const char **vstart, const char **vend, char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11585 | { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11586 | const char *arg_start, *qs_end; |
| 11587 | const char *value_start, *value_end; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11588 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11589 | arg_start = chunks[0]; |
| 11590 | qs_end = chunks[1]; |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11591 | if (url_param_name_l) { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11592 | /* Looks for an argument name. */ |
| 11593 | arg_start = find_url_param_pos(chunks, |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11594 | url_param_name, url_param_name_l, |
| 11595 | delim); |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11596 | /* Check for wrapping. */ |
| 11597 | if (arg_start > qs_end) |
| 11598 | qs_end = chunks[3]; |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11599 | } |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11600 | if (!arg_start) |
| 11601 | return 0; |
| 11602 | |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11603 | if (!url_param_name_l) { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11604 | while (1) { |
| 11605 | /* looks for the first argument. */ |
| 11606 | value_start = memchr(arg_start, '=', qs_end - arg_start); |
| 11607 | if (!value_start) { |
| 11608 | |
| 11609 | /* Check for wrapping. */ |
| 11610 | if (arg_start >= chunks[0] && |
| 11611 | arg_start <= chunks[1] && |
| 11612 | chunks[2]) { |
| 11613 | arg_start = chunks[2]; |
| 11614 | qs_end = chunks[3]; |
| 11615 | continue; |
| 11616 | } |
| 11617 | return 0; |
| 11618 | } |
| 11619 | break; |
| 11620 | } |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11621 | value_start++; |
| 11622 | } |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11623 | else { |
| 11624 | /* Jump the argument length. */ |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11625 | value_start = arg_start + url_param_name_l + 1; |
| 11626 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11627 | /* Check for pointer wrapping. */ |
| 11628 | if (fix_pointer_if_wrap(chunks, &value_start)) { |
| 11629 | /* Update the end pointer. */ |
| 11630 | qs_end = chunks[3]; |
| 11631 | |
| 11632 | /* Check for overflow. */ |
| 11633 | if (value_start > qs_end) |
| 11634 | return 0; |
| 11635 | } |
| 11636 | } |
| 11637 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11638 | value_end = value_start; |
| 11639 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11640 | while (1) { |
| 11641 | while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim)) |
| 11642 | value_end++; |
| 11643 | if (value_end < qs_end) |
| 11644 | break; |
| 11645 | /* process buffer wrapping. */ |
| 11646 | if (value_end >= chunks[0] && |
| 11647 | value_end <= chunks[1] && |
| 11648 | chunks[2]) { |
| 11649 | value_end = chunks[2]; |
| 11650 | qs_end = chunks[3]; |
| 11651 | continue; |
| 11652 | } |
| 11653 | break; |
| 11654 | } |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11655 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11656 | *vstart = value_start; |
| 11657 | *vend = value_end; |
Willy Tarreau | 0013433 | 2011-01-04 14:57:34 +0100 | [diff] [blame] | 11658 | return value_end != value_start; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11659 | } |
| 11660 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11661 | /* This scans a URL-encoded query string. It takes an optionally wrapping |
| 11662 | * string whose first contigous chunk has its beginning in ctx->a[0] and end |
| 11663 | * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The |
| 11664 | * pointers are updated for next iteration before leaving. |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11665 | */ |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11666 | static int |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11667 | 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] | 11668 | { |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11669 | const char *vstart, *vend; |
| 11670 | struct chunk *temp; |
| 11671 | const char **chunks = (const char **)smp->ctx.a; |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 11672 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11673 | if (!find_next_url_param(chunks, |
Thierry FOURNIER | 0948d41 | 2015-05-20 15:22:37 +0200 | [diff] [blame] | 11674 | name, name_len, |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11675 | &vstart, &vend, |
Thierry FOURNIER | 0948d41 | 2015-05-20 15:22:37 +0200 | [diff] [blame] | 11676 | delim)) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11677 | return 0; |
| 11678 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11679 | /* Create sample. If the value is contiguous, return the pointer as CONST, |
| 11680 | * if the value is wrapped, copy-it in a buffer. |
| 11681 | */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11682 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11683 | if (chunks[2] && |
| 11684 | vstart >= chunks[0] && vstart <= chunks[1] && |
| 11685 | vend >= chunks[2] && vend <= chunks[3]) { |
| 11686 | /* Wrapped case. */ |
| 11687 | temp = get_trash_chunk(); |
| 11688 | memcpy(temp->str, vstart, chunks[1] - vstart); |
| 11689 | memcpy(temp->str + ( chunks[1] - vstart ), chunks[2], vend - chunks[2]); |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11690 | smp->data.u.str.str = temp->str; |
| 11691 | smp->data.u.str.len = ( chunks[1] - vstart ) + ( vend - chunks[2] ); |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11692 | } else { |
| 11693 | /* Contiguous case. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11694 | smp->data.u.str.str = (char *)vstart; |
| 11695 | smp->data.u.str.len = vend - vstart; |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11696 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
| 11697 | } |
| 11698 | |
| 11699 | /* Update context, check wrapping. */ |
| 11700 | chunks[0] = vend; |
| 11701 | if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) { |
| 11702 | chunks[1] = chunks[3]; |
| 11703 | chunks[2] = NULL; |
| 11704 | } |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11705 | |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11706 | if (chunks[0] < chunks[1]) |
Willy Tarreau | 1ede1da | 2015-05-07 16:06:18 +0200 | [diff] [blame] | 11707 | smp->flags |= SMP_F_NOT_LAST; |
| 11708 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 11709 | return 1; |
| 11710 | } |
| 11711 | |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11712 | /* This function iterates over each parameter of the query string. It uses |
| 11713 | * 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] | 11714 | * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL. |
| 11715 | * An optional parameter name is passed in args[0], otherwise any parameter is |
| 11716 | * considered. It supports an optional delimiter argument for the beginning of |
| 11717 | * the string in args[1], which defaults to "?". |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11718 | */ |
| 11719 | static int |
| 11720 | smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 11721 | { |
| 11722 | struct http_msg *msg; |
| 11723 | char delim = '?'; |
| 11724 | const char *name; |
| 11725 | int name_len; |
| 11726 | |
Dragan Dosen | 26f77e5 | 2015-05-25 10:02:11 +0200 | [diff] [blame] | 11727 | if (!args || |
| 11728 | (args[0].type && args[0].type != ARGT_STR) || |
| 11729 | (args[1].type && args[1].type != ARGT_STR)) |
| 11730 | return 0; |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11731 | |
Dragan Dosen | 26f77e5 | 2015-05-25 10:02:11 +0200 | [diff] [blame] | 11732 | name = ""; |
| 11733 | name_len = 0; |
| 11734 | if (args->type == ARGT_STR) { |
| 11735 | name = args->data.str.str; |
| 11736 | name_len = args->data.str.len; |
| 11737 | } |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11738 | |
Dragan Dosen | 26f77e5 | 2015-05-25 10:02:11 +0200 | [diff] [blame] | 11739 | if (args[1].type) |
| 11740 | delim = *args[1].data.str.str; |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11741 | |
Dragan Dosen | 26f77e5 | 2015-05-25 10:02:11 +0200 | [diff] [blame] | 11742 | if (!smp->ctx.a[0]) { // first call, find the query string |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11743 | CHECK_HTTP_MESSAGE_FIRST(); |
| 11744 | |
| 11745 | msg = &smp->strm->txn->req; |
| 11746 | |
| 11747 | smp->ctx.a[0] = find_param_list(msg->chn->buf->p + msg->sl.rq.u, |
| 11748 | msg->sl.rq.u_l, delim); |
| 11749 | if (!smp->ctx.a[0]) |
| 11750 | return 0; |
| 11751 | |
| 11752 | 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] | 11753 | |
| 11754 | /* Assume that the context is filled with NULL pointer |
| 11755 | * before the first call. |
| 11756 | * smp->ctx.a[2] = NULL; |
| 11757 | * smp->ctx.a[3] = NULL; |
| 11758 | */ |
Thierry FOURNIER | 4fdc74c | 2015-05-19 14:46:23 +0200 | [diff] [blame] | 11759 | } |
| 11760 | |
| 11761 | return smp_fetch_param(delim, name, name_len, args, smp, kw, private); |
| 11762 | } |
| 11763 | |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 11764 | /* This function iterates over each parameter of the body. This requires |
| 11765 | * 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] | 11766 | * ctx->a[0] and ctx->a[1] to store the beginning and end of the first |
| 11767 | * contigous part of the body, and optionally ctx->a[2..3] to reference the |
| 11768 | * optional second part if the body wraps at the end of the buffer. An optional |
| 11769 | * parameter name is passed in args[0], otherwise any parameter is considered. |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 11770 | */ |
| 11771 | static int |
| 11772 | smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 11773 | { |
| 11774 | struct http_txn *txn = smp->strm->txn; |
| 11775 | struct http_msg *msg; |
| 11776 | unsigned long len; |
| 11777 | unsigned long block1; |
| 11778 | char *body; |
| 11779 | const char *name; |
| 11780 | int name_len; |
| 11781 | |
| 11782 | if (!args || (args[0].type && args[0].type != ARGT_STR)) |
| 11783 | return 0; |
| 11784 | |
| 11785 | name = ""; |
| 11786 | name_len = 0; |
| 11787 | if (args[0].type == ARGT_STR) { |
| 11788 | name = args[0].data.str.str; |
| 11789 | name_len = args[0].data.str.len; |
| 11790 | } |
| 11791 | |
| 11792 | if (!smp->ctx.a[0]) { // first call, find the query string |
| 11793 | CHECK_HTTP_MESSAGE_FIRST(); |
| 11794 | |
| 11795 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) |
| 11796 | msg = &txn->req; |
| 11797 | else |
| 11798 | msg = &txn->rsp; |
| 11799 | |
| 11800 | len = http_body_bytes(msg); |
| 11801 | body = b_ptr(msg->chn->buf, -http_data_rewind(msg)); |
| 11802 | |
| 11803 | block1 = len; |
| 11804 | if (block1 > msg->chn->buf->data + msg->chn->buf->size - body) |
| 11805 | block1 = msg->chn->buf->data + msg->chn->buf->size - body; |
| 11806 | |
| 11807 | if (block1 == len) { |
| 11808 | /* buffer is not wrapped (or empty) */ |
| 11809 | smp->ctx.a[0] = body; |
| 11810 | smp->ctx.a[1] = body + len; |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11811 | |
| 11812 | /* Assume that the context is filled with NULL pointer |
| 11813 | * before the first call. |
| 11814 | * smp->ctx.a[2] = NULL; |
| 11815 | * smp->ctx.a[3] = NULL; |
| 11816 | */ |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 11817 | } |
| 11818 | else { |
| 11819 | /* buffer is wrapped, we need to defragment it */ |
| 11820 | smp->ctx.a[0] = body; |
| 11821 | smp->ctx.a[1] = body + block1; |
Thierry FOURNIER | 8be451c | 2015-05-20 15:28:12 +0200 | [diff] [blame] | 11822 | smp->ctx.a[2] = msg->chn->buf->data; |
| 11823 | smp->ctx.a[3] = msg->chn->buf->data + ( len - block1 ); |
Thierry FOURNIER | e28c499 | 2015-05-19 14:45:09 +0200 | [diff] [blame] | 11824 | } |
| 11825 | } |
| 11826 | return smp_fetch_param('&', name, name_len, args, smp, kw, private); |
| 11827 | } |
| 11828 | |
Willy Tarreau | a9fddca | 2012-07-31 07:51:48 +0200 | [diff] [blame] | 11829 | /* Return the signed integer value for the specified url parameter (see url_param |
| 11830 | * above). |
| 11831 | */ |
| 11832 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11833 | 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] | 11834 | { |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11835 | int ret = smp_fetch_url_param(args, smp, kw, private); |
Willy Tarreau | a9fddca | 2012-07-31 07:51:48 +0200 | [diff] [blame] | 11836 | |
| 11837 | if (ret > 0) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11838 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11839 | 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] | 11840 | } |
| 11841 | |
| 11842 | return ret; |
| 11843 | } |
| 11844 | |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11845 | /* This produces a 32-bit hash of the concatenation of the first occurrence of |
| 11846 | * the Host header followed by the path component if it begins with a slash ('/'). |
| 11847 | * This means that '*' will not be added, resulting in exactly the first Host |
| 11848 | * entry. If no Host header is found, then the path is used. The resulting value |
| 11849 | * is hashed using the url hash followed by a full avalanche hash and provides a |
| 11850 | * 32-bit integer value. This fetch is useful for tracking per-URL activity on |
| 11851 | * high-traffic sites without having to store whole paths. |
| 11852 | * this differs from the base32 functions in that it includes the url parameters |
| 11853 | * as well as the path |
| 11854 | */ |
| 11855 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11856 | 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] | 11857 | { |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 11858 | struct http_txn *txn; |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11859 | struct hdr_ctx ctx; |
| 11860 | unsigned int hash = 0; |
| 11861 | char *ptr, *beg, *end; |
| 11862 | int len; |
| 11863 | |
| 11864 | CHECK_HTTP_MESSAGE_FIRST(); |
| 11865 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11866 | txn = smp->strm->txn; |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11867 | ctx.idx = 0; |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 11868 | 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] | 11869 | /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */ |
| 11870 | ptr = ctx.line + ctx.val; |
| 11871 | len = ctx.vlen; |
| 11872 | while (len--) |
| 11873 | hash = *(ptr++) + (hash << 6) + (hash << 16) - hash; |
| 11874 | } |
| 11875 | |
| 11876 | /* now retrieve the path */ |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 11877 | 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] | 11878 | beg = http_get_path(txn); |
| 11879 | if (!beg) |
| 11880 | beg = end; |
| 11881 | |
| 11882 | for (ptr = beg; ptr < end ; ptr++); |
| 11883 | |
| 11884 | if (beg < ptr && *beg == '/') { |
| 11885 | while (beg < ptr) |
| 11886 | hash = *(beg++) + (hash << 6) + (hash << 16) - hash; |
| 11887 | } |
| 11888 | hash = full_hash(hash); |
| 11889 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11890 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11891 | smp->data.u.sint = hash; |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11892 | smp->flags = SMP_F_VOL_1ST; |
| 11893 | return 1; |
| 11894 | } |
| 11895 | |
| 11896 | /* This concatenates the source address with the 32-bit hash of the Host and |
| 11897 | * URL as returned by smp_fetch_base32(). The idea is to have per-source and |
| 11898 | * per-url counters. The result is a binary block from 8 to 20 bytes depending |
| 11899 | * on the source address length. The URL hash is stored before the address so |
| 11900 | * that in environments where IPv6 is insignificant, truncating the output to |
| 11901 | * 8 bytes would still work. |
| 11902 | */ |
| 11903 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11904 | 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] | 11905 | { |
| 11906 | struct chunk *temp; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11907 | struct connection *cli_conn = objt_conn(smp->sess->origin); |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 11908 | unsigned int hash; |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11909 | |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 11910 | if (!smp_fetch_url32(args, smp, kw, private)) |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11911 | return 0; |
| 11912 | |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 11913 | /* The returned hash is a 32 bytes integer. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11914 | hash = smp->data.u.sint; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 11915 | |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11916 | temp = get_trash_chunk(); |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 11917 | memcpy(temp->str + temp->len, &hash, sizeof(hash)); |
| 11918 | temp->len += sizeof(hash); |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11919 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 11920 | switch (cli_conn->addr.from.ss_family) { |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11921 | case AF_INET: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 11922 | 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] | 11923 | temp->len += 4; |
| 11924 | break; |
| 11925 | case AF_INET6: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 11926 | 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] | 11927 | temp->len += 16; |
| 11928 | break; |
| 11929 | default: |
| 11930 | return 0; |
| 11931 | } |
| 11932 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11933 | smp->data.u.str = *temp; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11934 | smp->data.type = SMP_T_BIN; |
Neil - HAProxy List | 39c63c5 | 2013-11-04 13:48:42 +0000 | [diff] [blame] | 11935 | return 1; |
| 11936 | } |
| 11937 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 11938 | /* This function is used to validate the arguments passed to any "hdr" fetch |
| 11939 | * keyword. These keywords support an optional positive or negative occurrence |
| 11940 | * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It |
| 11941 | * is assumed that the types are already the correct ones. Returns 0 on error, |
| 11942 | * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an |
| 11943 | * error message in case of error, that the caller is responsible for freeing. |
| 11944 | * The initial location must either be freeable or NULL. |
| 11945 | */ |
Thierry FOURNIER | 49f45af | 2014-12-08 19:50:43 +0100 | [diff] [blame] | 11946 | int val_hdr(struct arg *arg, char **err_msg) |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 11947 | { |
| 11948 | 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] | 11949 | memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY); |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 11950 | return 0; |
| 11951 | } |
| 11952 | return 1; |
| 11953 | } |
| 11954 | |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 11955 | /* takes an UINT value on input supposed to represent the time since EPOCH, |
| 11956 | * adds an optional offset found in args[0] and emits a string representing |
| 11957 | * the date in RFC-1123/5322 format. |
| 11958 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 11959 | 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] | 11960 | { |
| 11961 | const char day[7][4] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" }; |
| 11962 | const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; |
| 11963 | struct chunk *temp; |
| 11964 | struct tm *tm; |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 11965 | /* 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] | 11966 | time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL; |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 11967 | |
| 11968 | /* add offset */ |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 11969 | if (args && (args[0].type == ARGT_SINT)) |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 11970 | curr_date += args[0].data.sint; |
| 11971 | |
| 11972 | tm = gmtime(&curr_date); |
Thierry FOURNIER | fac9ccf | 2015-07-08 00:15:20 +0200 | [diff] [blame] | 11973 | if (!tm) |
| 11974 | return 0; |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 11975 | |
| 11976 | temp = get_trash_chunk(); |
| 11977 | temp->len = snprintf(temp->str, temp->size - temp->len, |
| 11978 | "%s, %02d %s %04d %02d:%02d:%02d GMT", |
| 11979 | day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year, |
| 11980 | tm->tm_hour, tm->tm_min, tm->tm_sec); |
| 11981 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 11982 | smp->data.u.str = *temp; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 11983 | smp->data.type = SMP_T_STR; |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 11984 | return 1; |
| 11985 | } |
| 11986 | |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 11987 | /* Match language range with language tag. RFC2616 14.4: |
| 11988 | * |
| 11989 | * A language-range matches a language-tag if it exactly equals |
| 11990 | * the tag, or if it exactly equals a prefix of the tag such |
| 11991 | * that the first tag character following the prefix is "-". |
| 11992 | * |
| 11993 | * Return 1 if the strings match, else return 0. |
| 11994 | */ |
| 11995 | static inline int language_range_match(const char *range, int range_len, |
| 11996 | const char *tag, int tag_len) |
| 11997 | { |
| 11998 | const char *end = range + range_len; |
| 11999 | const char *tend = tag + tag_len; |
| 12000 | while (range < end) { |
| 12001 | if (*range == '-' && tag == tend) |
| 12002 | return 1; |
| 12003 | if (*range != *tag || tag == tend) |
| 12004 | return 0; |
| 12005 | range++; |
| 12006 | tag++; |
| 12007 | } |
| 12008 | /* Return true only if the last char of the tag is matched. */ |
| 12009 | return tag == tend; |
| 12010 | } |
| 12011 | |
| 12012 | /* 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] | 12013 | 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] | 12014 | { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12015 | const char *al = smp->data.u.str.str; |
| 12016 | const char *end = al + smp->data.u.str.len; |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 12017 | const char *token; |
| 12018 | int toklen; |
| 12019 | int qvalue; |
| 12020 | const char *str; |
| 12021 | const char *w; |
| 12022 | int best_q = 0; |
| 12023 | |
| 12024 | /* Set the constant to the sample, because the output of the |
| 12025 | * function will be peek in the constant configuration string. |
| 12026 | */ |
| 12027 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12028 | smp->data.u.str.size = 0; |
| 12029 | smp->data.u.str.str = ""; |
| 12030 | smp->data.u.str.len = 0; |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 12031 | |
| 12032 | /* Parse the accept language */ |
| 12033 | while (1) { |
| 12034 | |
| 12035 | /* Jump spaces, quit if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 12036 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 12037 | al++; |
| 12038 | if (al >= end) |
| 12039 | break; |
| 12040 | |
| 12041 | /* Start of the fisrt word. */ |
| 12042 | token = al; |
| 12043 | |
| 12044 | /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 12045 | while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 12046 | al++; |
| 12047 | if (al == token) |
| 12048 | goto expect_comma; |
| 12049 | |
| 12050 | /* Length of the token. */ |
| 12051 | toklen = al - token; |
| 12052 | qvalue = 1000; |
| 12053 | |
| 12054 | /* Check if the token exists in the list. If the token not exists, |
| 12055 | * jump to the next token. |
| 12056 | */ |
| 12057 | str = args[0].data.str.str; |
| 12058 | w = str; |
| 12059 | while (1) { |
| 12060 | if (*str == ';' || *str == '\0') { |
| 12061 | if (language_range_match(token, toklen, w, str-w)) |
| 12062 | goto look_for_q; |
| 12063 | if (*str == '\0') |
| 12064 | goto expect_comma; |
| 12065 | w = str + 1; |
| 12066 | } |
| 12067 | str++; |
| 12068 | } |
| 12069 | goto expect_comma; |
| 12070 | |
| 12071 | look_for_q: |
| 12072 | |
| 12073 | /* Jump spaces, quit if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 12074 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 12075 | al++; |
| 12076 | if (al >= end) |
| 12077 | goto process_value; |
| 12078 | |
| 12079 | /* If ',' is found, process the result */ |
| 12080 | if (*al == ',') |
| 12081 | goto process_value; |
| 12082 | |
| 12083 | /* If the character is different from ';', look |
| 12084 | * for the end of the header part in best effort. |
| 12085 | */ |
| 12086 | if (*al != ';') |
| 12087 | goto expect_comma; |
| 12088 | |
| 12089 | /* Assumes that the char is ';', now expect "q=". */ |
| 12090 | al++; |
| 12091 | |
| 12092 | /* Jump spaces, process value if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 12093 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 12094 | al++; |
| 12095 | if (al >= end) |
| 12096 | goto process_value; |
| 12097 | |
| 12098 | /* Expect 'q'. If no 'q', continue in best effort */ |
| 12099 | if (*al != 'q') |
| 12100 | goto process_value; |
| 12101 | al++; |
| 12102 | |
| 12103 | /* Jump spaces, process value if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 12104 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 12105 | al++; |
| 12106 | if (al >= end) |
| 12107 | goto process_value; |
| 12108 | |
| 12109 | /* Expect '='. If no '=', continue in best effort */ |
| 12110 | if (*al != '=') |
| 12111 | goto process_value; |
| 12112 | al++; |
| 12113 | |
| 12114 | /* Jump spaces, process value if the end is detected. */ |
Willy Tarreau | 506c69a | 2014-07-08 00:59:48 +0200 | [diff] [blame] | 12115 | while (al < end && isspace((unsigned char)*al)) |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 12116 | al++; |
| 12117 | if (al >= end) |
| 12118 | goto process_value; |
| 12119 | |
| 12120 | /* Parse the q value. */ |
| 12121 | qvalue = parse_qvalue(al, &al); |
| 12122 | |
| 12123 | process_value: |
| 12124 | |
| 12125 | /* If the new q value is the best q value, then store the associated |
| 12126 | * language in the response. If qvalue is the biggest value (1000), |
| 12127 | * break the process. |
| 12128 | */ |
| 12129 | if (qvalue > best_q) { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12130 | smp->data.u.str.str = (char *)w; |
| 12131 | smp->data.u.str.len = str - w; |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 12132 | if (qvalue >= 1000) |
| 12133 | break; |
| 12134 | best_q = qvalue; |
| 12135 | } |
| 12136 | |
| 12137 | expect_comma: |
| 12138 | |
| 12139 | /* Expect comma or end. If the end is detected, quit the loop. */ |
| 12140 | while (al < end && *al != ',') |
| 12141 | al++; |
| 12142 | if (al >= end) |
| 12143 | break; |
| 12144 | |
| 12145 | /* Comma is found, jump it and restart the analyzer. */ |
| 12146 | al++; |
| 12147 | } |
| 12148 | |
| 12149 | /* Set default value if required. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12150 | if (smp->data.u.str.len == 0 && args[1].type == ARGT_STR) { |
| 12151 | smp->data.u.str.str = args[1].data.str.str; |
| 12152 | smp->data.u.str.len = args[1].data.str.len; |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 12153 | } |
| 12154 | |
| 12155 | /* Return true only if a matching language was found. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12156 | return smp->data.u.str.len != 0; |
Thierry FOURNIER | ad90351 | 2014-04-11 17:51:01 +0200 | [diff] [blame] | 12157 | } |
| 12158 | |
Thierry FOURNIER | 82ff3c9 | 2015-05-07 15:46:20 +0200 | [diff] [blame] | 12159 | /* This fetch url-decode any input string. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 12160 | 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] | 12161 | { |
| 12162 | /* If the constant flag is set or if not size is avalaible at |
| 12163 | * the end of the buffer, copy the string in other buffer |
| 12164 | * before decoding. |
| 12165 | */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12166 | 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] | 12167 | struct chunk *str = get_trash_chunk(); |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12168 | memcpy(str->str, smp->data.u.str.str, smp->data.u.str.len); |
| 12169 | smp->data.u.str.str = str->str; |
| 12170 | smp->data.u.str.size = str->size; |
Thierry FOURNIER | 82ff3c9 | 2015-05-07 15:46:20 +0200 | [diff] [blame] | 12171 | smp->flags &= ~SMP_F_CONST; |
| 12172 | } |
| 12173 | |
| 12174 | /* Add final \0 required by url_decode(), and convert the input string. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12175 | smp->data.u.str.str[smp->data.u.str.len] = '\0'; |
| 12176 | smp->data.u.str.len = url_decode(smp->data.u.str.str); |
Thierry FOURNIER | 82ff3c9 | 2015-05-07 15:46:20 +0200 | [diff] [blame] | 12177 | return 1; |
| 12178 | } |
| 12179 | |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 12180 | static int smp_conv_req_capture(const struct arg *args, struct sample *smp, void *private) |
| 12181 | { |
| 12182 | struct proxy *fe = strm_fe(smp->strm); |
| 12183 | int idx, i; |
| 12184 | struct cap_hdr *hdr; |
| 12185 | int len; |
| 12186 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 12187 | if (!args || args->type != ARGT_SINT) |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 12188 | return 0; |
| 12189 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 12190 | idx = args->data.sint; |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 12191 | |
| 12192 | /* Check the availibity of the capture id. */ |
| 12193 | if (idx > fe->nb_req_cap - 1) |
| 12194 | return 0; |
| 12195 | |
| 12196 | /* Look for the original configuration. */ |
| 12197 | for (hdr = fe->req_cap, i = fe->nb_req_cap - 1; |
| 12198 | hdr != NULL && i != idx ; |
| 12199 | i--, hdr = hdr->next); |
| 12200 | if (!hdr) |
| 12201 | return 0; |
| 12202 | |
| 12203 | /* check for the memory allocation */ |
| 12204 | if (smp->strm->req_cap[hdr->index] == NULL) |
| 12205 | smp->strm->req_cap[hdr->index] = pool_alloc2(hdr->pool); |
| 12206 | if (smp->strm->req_cap[hdr->index] == NULL) |
| 12207 | return 0; |
| 12208 | |
| 12209 | /* Check length. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12210 | len = smp->data.u.str.len; |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 12211 | if (len > hdr->len) |
| 12212 | len = hdr->len; |
| 12213 | |
| 12214 | /* Capture input data. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12215 | memcpy(smp->strm->req_cap[idx], smp->data.u.str.str, len); |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 12216 | smp->strm->req_cap[idx][len] = '\0'; |
| 12217 | |
| 12218 | return 1; |
| 12219 | } |
| 12220 | |
| 12221 | static int smp_conv_res_capture(const struct arg *args, struct sample *smp, void *private) |
| 12222 | { |
| 12223 | struct proxy *fe = strm_fe(smp->strm); |
| 12224 | int idx, i; |
| 12225 | struct cap_hdr *hdr; |
| 12226 | int len; |
| 12227 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 12228 | if (!args || args->type != ARGT_SINT) |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 12229 | return 0; |
| 12230 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 12231 | idx = args->data.sint; |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 12232 | |
| 12233 | /* Check the availibity of the capture id. */ |
| 12234 | if (idx > fe->nb_rsp_cap - 1) |
| 12235 | return 0; |
| 12236 | |
| 12237 | /* Look for the original configuration. */ |
| 12238 | for (hdr = fe->rsp_cap, i = fe->nb_rsp_cap - 1; |
| 12239 | hdr != NULL && i != idx ; |
| 12240 | i--, hdr = hdr->next); |
| 12241 | if (!hdr) |
| 12242 | return 0; |
| 12243 | |
| 12244 | /* check for the memory allocation */ |
| 12245 | if (smp->strm->res_cap[hdr->index] == NULL) |
| 12246 | smp->strm->res_cap[hdr->index] = pool_alloc2(hdr->pool); |
| 12247 | if (smp->strm->res_cap[hdr->index] == NULL) |
| 12248 | return 0; |
| 12249 | |
| 12250 | /* Check length. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12251 | len = smp->data.u.str.len; |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 12252 | if (len > hdr->len) |
| 12253 | len = hdr->len; |
| 12254 | |
| 12255 | /* Capture input data. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12256 | memcpy(smp->strm->res_cap[idx], smp->data.u.str.str, len); |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 12257 | smp->strm->res_cap[idx][len] = '\0'; |
| 12258 | |
| 12259 | return 1; |
| 12260 | } |
| 12261 | |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12262 | /* 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] | 12263 | * takes the string from the variable 'replace' with length 'len', then modifies |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12264 | * the relevant part of the request line accordingly. Then it updates various |
| 12265 | * pointers to the next elements which were moved, and the total buffer length. |
| 12266 | * 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] | 12267 | * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal |
| 12268 | * error, though this can be revisited when this code is finally exploited. |
| 12269 | * |
| 12270 | * 'action' can be '0' to replace method, '1' to replace path, '2' to replace |
| 12271 | * query string and 3 to replace uri. |
| 12272 | * |
| 12273 | * In query string case, the mark question '?' must be set at the start of the |
| 12274 | * string by the caller, event if the replacement query string is empty. |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12275 | */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12276 | int http_replace_req_line(int action, const char *replace, int len, |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 12277 | struct proxy *px, struct stream *s) |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12278 | { |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 12279 | struct http_txn *txn = s->txn; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12280 | char *cur_ptr, *cur_end; |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12281 | int offset = 0; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12282 | int delta; |
| 12283 | |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12284 | switch (action) { |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12285 | case 0: // method |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 12286 | cur_ptr = s->req.buf->p; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12287 | cur_end = cur_ptr + txn->req.sl.rq.m_l; |
| 12288 | |
| 12289 | /* adjust req line offsets and lengths */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12290 | delta = len - offset - (cur_end - cur_ptr); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12291 | txn->req.sl.rq.m_l += delta; |
| 12292 | txn->req.sl.rq.u += delta; |
| 12293 | txn->req.sl.rq.v += delta; |
| 12294 | break; |
| 12295 | |
| 12296 | case 1: // path |
| 12297 | cur_ptr = http_get_path(txn); |
| 12298 | if (!cur_ptr) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 12299 | cur_ptr = s->req.buf->p + txn->req.sl.rq.u; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12300 | |
| 12301 | cur_end = cur_ptr; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 12302 | 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] | 12303 | cur_end++; |
| 12304 | |
| 12305 | /* adjust req line offsets and lengths */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12306 | delta = len - offset - (cur_end - cur_ptr); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12307 | txn->req.sl.rq.u_l += delta; |
| 12308 | txn->req.sl.rq.v += delta; |
| 12309 | break; |
| 12310 | |
| 12311 | case 2: // query |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12312 | offset = 1; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 12313 | cur_ptr = s->req.buf->p + txn->req.sl.rq.u; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12314 | cur_end = cur_ptr + txn->req.sl.rq.u_l; |
| 12315 | while (cur_ptr < cur_end && *cur_ptr != '?') |
| 12316 | cur_ptr++; |
| 12317 | |
| 12318 | /* skip the question mark or indicate that we must insert it |
| 12319 | * (but only if the format string is not empty then). |
| 12320 | */ |
| 12321 | if (cur_ptr < cur_end) |
| 12322 | cur_ptr++; |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12323 | else if (len > 1) |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12324 | offset = 0; |
| 12325 | |
| 12326 | /* adjust req line offsets and lengths */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12327 | delta = len - offset - (cur_end - cur_ptr); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12328 | txn->req.sl.rq.u_l += delta; |
| 12329 | txn->req.sl.rq.v += delta; |
| 12330 | break; |
| 12331 | |
| 12332 | case 3: // uri |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 12333 | cur_ptr = s->req.buf->p + txn->req.sl.rq.u; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12334 | cur_end = cur_ptr + txn->req.sl.rq.u_l; |
| 12335 | |
| 12336 | /* adjust req line offsets and lengths */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12337 | delta = len - offset - (cur_end - cur_ptr); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12338 | txn->req.sl.rq.u_l += delta; |
| 12339 | txn->req.sl.rq.v += delta; |
| 12340 | break; |
| 12341 | |
| 12342 | default: |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12343 | return -1; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12344 | } |
| 12345 | |
| 12346 | /* commit changes and adjust end of message */ |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12347 | 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] | 12348 | txn->req.sl.rq.l += delta; |
| 12349 | txn->hdr_idx.v[0].len += delta; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12350 | http_msg_move_end(&txn->req, delta); |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12351 | return 0; |
| 12352 | } |
| 12353 | |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 12354 | /* This function replace the HTTP status code and the associated message. The |
| 12355 | * variable <status> contains the new status code. This function never fails. |
| 12356 | */ |
| 12357 | void http_set_status(unsigned int status, struct stream *s) |
| 12358 | { |
| 12359 | struct http_txn *txn = s->txn; |
| 12360 | char *cur_ptr, *cur_end; |
| 12361 | int delta; |
| 12362 | char *res; |
| 12363 | int c_l; |
| 12364 | const char *msg; |
| 12365 | int msg_len; |
| 12366 | |
| 12367 | chunk_reset(&trash); |
| 12368 | |
| 12369 | res = ultoa_o(status, trash.str, trash.size); |
| 12370 | c_l = res - trash.str; |
| 12371 | |
| 12372 | trash.str[c_l] = ' '; |
| 12373 | trash.len = c_l + 1; |
| 12374 | |
| 12375 | msg = get_reason(status); |
| 12376 | msg_len = strlen(msg); |
| 12377 | |
| 12378 | strncpy(&trash.str[trash.len], msg, trash.size - trash.len); |
| 12379 | trash.len += msg_len; |
| 12380 | |
| 12381 | cur_ptr = s->res.buf->p + txn->rsp.sl.st.c; |
| 12382 | cur_end = s->res.buf->p + txn->rsp.sl.st.r + txn->rsp.sl.st.r_l; |
| 12383 | |
| 12384 | /* commit changes and adjust message */ |
| 12385 | delta = buffer_replace2(s->res.buf, cur_ptr, cur_end, trash.str, trash.len); |
| 12386 | |
| 12387 | /* adjust res line offsets and lengths */ |
| 12388 | txn->rsp.sl.st.r += c_l - txn->rsp.sl.st.c_l; |
| 12389 | txn->rsp.sl.st.c_l = c_l; |
| 12390 | txn->rsp.sl.st.r_l = msg_len; |
| 12391 | |
| 12392 | delta = trash.len - (cur_end - cur_ptr); |
| 12393 | txn->rsp.sl.st.l += delta; |
| 12394 | txn->hdr_idx.v[0].len += delta; |
| 12395 | http_msg_move_end(&txn->rsp, delta); |
| 12396 | } |
| 12397 | |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12398 | /* This function executes one of the set-{method,path,query,uri} actions. It |
| 12399 | * 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] | 12400 | * the action to be performed in <http.action>, previously filled by function |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12401 | * parse_set_req_line(). The replacement action is excuted by the function |
Thierry FOURNIER | 3f4bc65 | 2015-08-26 16:23:34 +0200 | [diff] [blame] | 12402 | * http_action_set_req_line(). It always returns ACT_RET_CONT. If an error |
| 12403 | * occurs the action is canceled, but the rule processing continue. |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12404 | */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12405 | 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] | 12406 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12407 | { |
| 12408 | chunk_reset(&trash); |
| 12409 | |
| 12410 | /* If we have to create a query string, prepare a '?'. */ |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 12411 | if (rule->arg.http.action == 2) |
Thierry FOURNIER | b77aece | 2015-03-14 13:55:46 +0100 | [diff] [blame] | 12412 | trash.str[trash.len++] = '?'; |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 12413 | 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] | 12414 | |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 12415 | 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] | 12416 | return ACT_RET_CONT; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12417 | } |
| 12418 | |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 12419 | /* This function is just a compliant action wrapper for "set-status". */ |
| 12420 | 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] | 12421 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 12422 | { |
| 12423 | http_set_status(rule->arg.status.code, s); |
| 12424 | return ACT_RET_CONT; |
| 12425 | } |
| 12426 | |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12427 | /* parse an http-request action among : |
| 12428 | * set-method |
| 12429 | * set-path |
| 12430 | * set-query |
| 12431 | * set-uri |
| 12432 | * |
| 12433 | * All of them accept a single argument of type string representing a log-format. |
| 12434 | * The resulting rule makes use of arg->act.p[0..1] to store the log-format list |
| 12435 | * 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] | 12436 | * 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] | 12437 | */ |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12438 | enum act_parse_ret parse_set_req_line(const char **args, int *orig_arg, struct proxy *px, |
| 12439 | struct act_rule *rule, char **err) |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12440 | { |
| 12441 | int cur_arg = *orig_arg; |
| 12442 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 12443 | rule->action = ACT_CUSTOM; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12444 | |
| 12445 | switch (args[0][4]) { |
| 12446 | case 'm' : |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 12447 | rule->arg.http.action = 0; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12448 | rule->action_ptr = http_action_set_req_line; |
| 12449 | break; |
| 12450 | case 'p' : |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 12451 | rule->arg.http.action = 1; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12452 | rule->action_ptr = http_action_set_req_line; |
| 12453 | break; |
| 12454 | case 'q' : |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 12455 | rule->arg.http.action = 2; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12456 | rule->action_ptr = http_action_set_req_line; |
| 12457 | break; |
| 12458 | case 'u' : |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 12459 | rule->arg.http.action = 3; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12460 | rule->action_ptr = http_action_set_req_line; |
| 12461 | break; |
| 12462 | default: |
| 12463 | memprintf(err, "internal error: unhandled action '%s'", args[0]); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12464 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12465 | } |
| 12466 | |
| 12467 | if (!*args[cur_arg] || |
| 12468 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 12469 | memprintf(err, "expects exactly 1 argument <format>"); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12470 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12471 | } |
| 12472 | |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 12473 | LIST_INIT(&rule->arg.http.logfmt); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12474 | proxy->conf.args.ctx = ARGC_HRQ; |
Thierry FOURNIER | 8855a92 | 2015-07-31 08:54:25 +0200 | [diff] [blame] | 12475 | parse_logformat_string(args[cur_arg], proxy, &rule->arg.http.logfmt, LOG_OPT_HTTP, |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12476 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, |
| 12477 | proxy->conf.args.file, proxy->conf.args.line); |
| 12478 | |
| 12479 | (*orig_arg)++; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12480 | return ACT_RET_PRS_OK; |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 12481 | } |
| 12482 | |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 12483 | /* parse set-status action: |
| 12484 | * This action accepts a single argument of type int representing |
| 12485 | * an http status code. It returns ACT_RET_PRS_OK on success, |
| 12486 | * ACT_RET_PRS_ERR on error. |
| 12487 | */ |
| 12488 | enum act_parse_ret parse_http_set_status(const char **args, int *orig_arg, struct proxy *px, |
| 12489 | struct act_rule *rule, char **err) |
| 12490 | { |
| 12491 | char *error; |
| 12492 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 12493 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 12494 | rule->action_ptr = action_http_set_status; |
| 12495 | |
| 12496 | /* Check if an argument is available */ |
| 12497 | if (!*args[*orig_arg]) { |
| 12498 | memprintf(err, "expects exactly 1 argument <status>"); |
| 12499 | return ACT_RET_PRS_ERR; |
| 12500 | } |
| 12501 | |
| 12502 | /* convert status code as integer */ |
| 12503 | rule->arg.status.code = strtol(args[*orig_arg], &error, 10); |
| 12504 | if (*error != '\0' || rule->arg.status.code < 100 || rule->arg.status.code > 999) { |
| 12505 | memprintf(err, "expects an integer status code between 100 and 999"); |
| 12506 | return ACT_RET_PRS_ERR; |
| 12507 | } |
| 12508 | |
| 12509 | (*orig_arg)++; |
| 12510 | return ACT_RET_PRS_OK; |
| 12511 | } |
| 12512 | |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12513 | /* This function executes the "capture" action. It executes a fetch expression, |
| 12514 | * turns the result into a string and puts it in a capture slot. It always |
| 12515 | * returns 1. If an error occurs the action is cancelled, but the rule |
| 12516 | * processing continues. |
| 12517 | */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12518 | 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] | 12519 | struct session *sess, struct stream *s, int flags) |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12520 | { |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12521 | struct sample *key; |
Thierry FOURNIER | 32b1500 | 2015-07-31 08:56:16 +0200 | [diff] [blame] | 12522 | struct cap_hdr *h = rule->arg.cap.hdr; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12523 | char **cap = s->req_cap; |
| 12524 | int len; |
| 12525 | |
Thierry FOURNIER | 32b1500 | 2015-07-31 08:56:16 +0200 | [diff] [blame] | 12526 | 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] | 12527 | if (!key) |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12528 | return ACT_RET_CONT; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12529 | |
| 12530 | if (cap[h->index] == NULL) |
| 12531 | cap[h->index] = pool_alloc2(h->pool); |
| 12532 | |
| 12533 | if (cap[h->index] == NULL) /* no more capture memory */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12534 | return ACT_RET_CONT; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12535 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12536 | len = key->data.u.str.len; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12537 | if (len > h->len) |
| 12538 | len = h->len; |
| 12539 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12540 | memcpy(cap[h->index], key->data.u.str.str, len); |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12541 | cap[h->index][len] = 0; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12542 | return ACT_RET_CONT; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12543 | } |
| 12544 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12545 | /* This function executes the "capture" action and store the result in a |
| 12546 | * capture slot if exists. It executes a fetch expression, turns the result |
| 12547 | * into a string and puts it in a capture slot. It always returns 1. If an |
| 12548 | * error occurs the action is cancelled, but the rule processing continues. |
| 12549 | */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12550 | 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] | 12551 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12552 | { |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12553 | struct sample *key; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12554 | struct cap_hdr *h; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12555 | char **cap = s->req_cap; |
| 12556 | struct proxy *fe = strm_fe(s); |
| 12557 | int len; |
| 12558 | int i; |
| 12559 | |
| 12560 | /* Look for the original configuration. */ |
| 12561 | for (h = fe->req_cap, i = fe->nb_req_cap - 1; |
Thierry FOURNIER | e209797 | 2015-07-31 08:56:35 +0200 | [diff] [blame] | 12562 | h != NULL && i != rule->arg.capid.idx ; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12563 | i--, h = h->next); |
| 12564 | if (!h) |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12565 | return ACT_RET_CONT; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12566 | |
Thierry FOURNIER | e209797 | 2015-07-31 08:56:35 +0200 | [diff] [blame] | 12567 | 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] | 12568 | if (!key) |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12569 | return ACT_RET_CONT; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12570 | |
| 12571 | if (cap[h->index] == NULL) |
| 12572 | cap[h->index] = pool_alloc2(h->pool); |
| 12573 | |
| 12574 | if (cap[h->index] == NULL) /* no more capture memory */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12575 | return ACT_RET_CONT; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12576 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12577 | len = key->data.u.str.len; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12578 | if (len > h->len) |
| 12579 | len = h->len; |
| 12580 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12581 | memcpy(cap[h->index], key->data.u.str.str, len); |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12582 | cap[h->index][len] = 0; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12583 | return ACT_RET_CONT; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12584 | } |
| 12585 | |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12586 | /* parse an "http-request capture" action. It takes a single argument which is |
| 12587 | * 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] | 12588 | * 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] | 12589 | * 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] | 12590 | */ |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12591 | enum act_parse_ret parse_http_req_capture(const char **args, int *orig_arg, struct proxy *px, |
| 12592 | struct act_rule *rule, char **err) |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12593 | { |
| 12594 | struct sample_expr *expr; |
| 12595 | struct cap_hdr *hdr; |
| 12596 | int cur_arg; |
Willy Tarreau | 3986ac1 | 2015-05-08 16:13:42 +0200 | [diff] [blame] | 12597 | int len = 0; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12598 | |
| 12599 | for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++) |
| 12600 | if (strcmp(args[cur_arg], "if") == 0 || |
| 12601 | strcmp(args[cur_arg], "unless") == 0) |
| 12602 | break; |
| 12603 | |
| 12604 | if (cur_arg < *orig_arg + 3) { |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12605 | memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]"); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12606 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12607 | } |
| 12608 | |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12609 | cur_arg = *orig_arg; |
| 12610 | expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args); |
| 12611 | if (!expr) |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12612 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12613 | |
| 12614 | if (!(expr->fetch->val & SMP_VAL_FE_HRQ_HDR)) { |
| 12615 | memprintf(err, |
| 12616 | "fetch method '%s' extracts information from '%s', none of which is available here", |
| 12617 | args[cur_arg-1], sample_src_names(expr->fetch->use)); |
| 12618 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12619 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12620 | } |
| 12621 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12622 | if (!args[cur_arg] || !*args[cur_arg]) { |
| 12623 | memprintf(err, "expects 'len or 'id'"); |
| 12624 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12625 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12626 | } |
| 12627 | |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12628 | if (strcmp(args[cur_arg], "len") == 0) { |
| 12629 | cur_arg++; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12630 | |
| 12631 | if (!(px->cap & PR_CAP_FE)) { |
| 12632 | memprintf(err, "proxy '%s' has no frontend capability", px->id); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12633 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12634 | } |
| 12635 | |
| 12636 | proxy->conf.args.ctx = ARGC_CAP; |
| 12637 | |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12638 | if (!args[cur_arg]) { |
| 12639 | memprintf(err, "missing length value"); |
| 12640 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12641 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12642 | } |
| 12643 | /* we copy the table name for now, it will be resolved later */ |
| 12644 | len = atoi(args[cur_arg]); |
| 12645 | if (len <= 0) { |
| 12646 | memprintf(err, "length must be > 0"); |
| 12647 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12648 | return ACT_RET_PRS_ERR; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12649 | } |
| 12650 | cur_arg++; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12651 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12652 | if (!len) { |
| 12653 | memprintf(err, "a positive 'len' argument is mandatory"); |
| 12654 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12655 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12656 | } |
| 12657 | |
| 12658 | hdr = calloc(sizeof(struct cap_hdr), 1); |
| 12659 | hdr->next = px->req_cap; |
| 12660 | hdr->name = NULL; /* not a header capture */ |
| 12661 | hdr->namelen = 0; |
| 12662 | hdr->len = len; |
| 12663 | hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED); |
| 12664 | hdr->index = px->nb_req_cap++; |
| 12665 | |
| 12666 | px->req_cap = hdr; |
| 12667 | px->to_log |= LW_REQHDR; |
| 12668 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 12669 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12670 | rule->action_ptr = http_action_req_capture; |
Thierry FOURNIER | 32b1500 | 2015-07-31 08:56:16 +0200 | [diff] [blame] | 12671 | rule->arg.cap.expr = expr; |
| 12672 | rule->arg.cap.hdr = hdr; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12673 | } |
| 12674 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12675 | else if (strcmp(args[cur_arg], "id") == 0) { |
| 12676 | int id; |
| 12677 | char *error; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12678 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12679 | cur_arg++; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12680 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12681 | if (!args[cur_arg]) { |
| 12682 | memprintf(err, "missing id value"); |
| 12683 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12684 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12685 | } |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12686 | |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12687 | id = strtol(args[cur_arg], &error, 10); |
| 12688 | if (*error != '\0') { |
| 12689 | memprintf(err, "cannot parse id '%s'", args[cur_arg]); |
| 12690 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12691 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12692 | } |
| 12693 | cur_arg++; |
| 12694 | |
| 12695 | proxy->conf.args.ctx = ARGC_CAP; |
| 12696 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 12697 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12698 | rule->action_ptr = http_action_req_capture_by_id; |
Thierry FOURNIER | e209797 | 2015-07-31 08:56:35 +0200 | [diff] [blame] | 12699 | rule->arg.capid.expr = expr; |
| 12700 | rule->arg.capid.idx = id; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12701 | } |
| 12702 | |
| 12703 | else { |
| 12704 | memprintf(err, "expects 'len' or 'id', found '%s'", args[cur_arg]); |
| 12705 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12706 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 82bf70d | 2015-05-26 17:58:29 +0200 | [diff] [blame] | 12707 | } |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12708 | |
| 12709 | *orig_arg = cur_arg; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12710 | return ACT_RET_PRS_OK; |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 12711 | } |
| 12712 | |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12713 | /* This function executes the "capture" action and store the result in a |
| 12714 | * capture slot if exists. It executes a fetch expression, turns the result |
| 12715 | * into a string and puts it in a capture slot. It always returns 1. If an |
| 12716 | * error occurs the action is cancelled, but the rule processing continues. |
| 12717 | */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12718 | 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] | 12719 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12720 | { |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12721 | struct sample *key; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12722 | struct cap_hdr *h; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12723 | char **cap = s->res_cap; |
| 12724 | struct proxy *fe = strm_fe(s); |
| 12725 | int len; |
| 12726 | int i; |
| 12727 | |
| 12728 | /* Look for the original configuration. */ |
| 12729 | for (h = fe->rsp_cap, i = fe->nb_rsp_cap - 1; |
Thierry FOURNIER | e209797 | 2015-07-31 08:56:35 +0200 | [diff] [blame] | 12730 | h != NULL && i != rule->arg.capid.idx ; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12731 | i--, h = h->next); |
| 12732 | if (!h) |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12733 | return ACT_RET_CONT; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12734 | |
Thierry FOURNIER | e209797 | 2015-07-31 08:56:35 +0200 | [diff] [blame] | 12735 | 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] | 12736 | if (!key) |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12737 | return ACT_RET_CONT; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12738 | |
| 12739 | if (cap[h->index] == NULL) |
| 12740 | cap[h->index] = pool_alloc2(h->pool); |
| 12741 | |
| 12742 | if (cap[h->index] == NULL) /* no more capture memory */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12743 | return ACT_RET_CONT; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12744 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12745 | len = key->data.u.str.len; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12746 | if (len > h->len) |
| 12747 | len = h->len; |
| 12748 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 12749 | memcpy(cap[h->index], key->data.u.str.str, len); |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12750 | cap[h->index][len] = 0; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 12751 | return ACT_RET_CONT; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12752 | } |
| 12753 | |
| 12754 | /* parse an "http-response capture" action. It takes a single argument which is |
| 12755 | * a sample fetch expression. It stores the expression into arg->act.p[0] and |
| 12756 | * 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] | 12757 | * 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] | 12758 | */ |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12759 | enum act_parse_ret parse_http_res_capture(const char **args, int *orig_arg, struct proxy *px, |
| 12760 | struct act_rule *rule, char **err) |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12761 | { |
| 12762 | struct sample_expr *expr; |
| 12763 | int cur_arg; |
| 12764 | int id; |
| 12765 | char *error; |
| 12766 | |
| 12767 | for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++) |
| 12768 | if (strcmp(args[cur_arg], "if") == 0 || |
| 12769 | strcmp(args[cur_arg], "unless") == 0) |
| 12770 | break; |
| 12771 | |
| 12772 | if (cur_arg < *orig_arg + 3) { |
| 12773 | memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]"); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12774 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12775 | } |
| 12776 | |
| 12777 | cur_arg = *orig_arg; |
| 12778 | expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args); |
| 12779 | if (!expr) |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12780 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12781 | |
| 12782 | if (!(expr->fetch->val & SMP_VAL_FE_HRS_HDR)) { |
| 12783 | memprintf(err, |
| 12784 | "fetch method '%s' extracts information from '%s', none of which is available here", |
| 12785 | args[cur_arg-1], sample_src_names(expr->fetch->use)); |
| 12786 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12787 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12788 | } |
| 12789 | |
| 12790 | if (!args[cur_arg] || !*args[cur_arg]) { |
| 12791 | memprintf(err, "expects 'len or 'id'"); |
| 12792 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12793 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12794 | } |
| 12795 | |
| 12796 | if (strcmp(args[cur_arg], "id") != 0) { |
| 12797 | memprintf(err, "expects 'id', found '%s'", args[cur_arg]); |
| 12798 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12799 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12800 | } |
| 12801 | |
| 12802 | cur_arg++; |
| 12803 | |
| 12804 | if (!args[cur_arg]) { |
| 12805 | memprintf(err, "missing id value"); |
| 12806 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12807 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12808 | } |
| 12809 | |
| 12810 | id = strtol(args[cur_arg], &error, 10); |
| 12811 | if (*error != '\0') { |
| 12812 | memprintf(err, "cannot parse id '%s'", args[cur_arg]); |
| 12813 | free(expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12814 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12815 | } |
| 12816 | cur_arg++; |
| 12817 | |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12818 | proxy->conf.args.ctx = ARGC_CAP; |
| 12819 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 12820 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12821 | rule->action_ptr = http_action_res_capture_by_id; |
Thierry FOURNIER | e209797 | 2015-07-31 08:56:35 +0200 | [diff] [blame] | 12822 | rule->arg.capid.expr = expr; |
| 12823 | rule->arg.capid.idx = id; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12824 | |
| 12825 | *orig_arg = cur_arg; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 12826 | return ACT_RET_PRS_OK; |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 12827 | } |
| 12828 | |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 12829 | /* |
| 12830 | * Return the struct http_req_action_kw associated to a keyword. |
| 12831 | */ |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 12832 | struct action_kw *action_http_req_custom(const char *kw) |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 12833 | { |
Thierry FOURNIER | 322a124 | 2015-08-19 09:07:47 +0200 | [diff] [blame] | 12834 | return action_lookup(&http_req_keywords.list, kw); |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 12835 | } |
| 12836 | |
| 12837 | /* |
| 12838 | * Return the struct http_res_action_kw associated to a keyword. |
| 12839 | */ |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 12840 | struct action_kw *action_http_res_custom(const char *kw) |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 12841 | { |
Thierry FOURNIER | 322a124 | 2015-08-19 09:07:47 +0200 | [diff] [blame] | 12842 | return action_lookup(&http_res_keywords.list, kw); |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 12843 | } |
| 12844 | |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 12845 | /************************************************************************/ |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12846 | /* All supported ACL keywords must be declared here. */ |
| 12847 | /************************************************************************/ |
| 12848 | |
| 12849 | /* Note: must not be declared <const> as its list will be overwritten. |
| 12850 | * Please take care of keeping this list alphabetically sorted. |
| 12851 | */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 12852 | static struct acl_kw_list acl_kws = {ILH, { |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12853 | { "base", "base", PAT_MATCH_STR }, |
| 12854 | { "base_beg", "base", PAT_MATCH_BEG }, |
| 12855 | { "base_dir", "base", PAT_MATCH_DIR }, |
| 12856 | { "base_dom", "base", PAT_MATCH_DOM }, |
| 12857 | { "base_end", "base", PAT_MATCH_END }, |
| 12858 | { "base_len", "base", PAT_MATCH_LEN }, |
| 12859 | { "base_reg", "base", PAT_MATCH_REG }, |
| 12860 | { "base_sub", "base", PAT_MATCH_SUB }, |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 12861 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12862 | { "cook", "req.cook", PAT_MATCH_STR }, |
| 12863 | { "cook_beg", "req.cook", PAT_MATCH_BEG }, |
| 12864 | { "cook_dir", "req.cook", PAT_MATCH_DIR }, |
| 12865 | { "cook_dom", "req.cook", PAT_MATCH_DOM }, |
| 12866 | { "cook_end", "req.cook", PAT_MATCH_END }, |
| 12867 | { "cook_len", "req.cook", PAT_MATCH_LEN }, |
| 12868 | { "cook_reg", "req.cook", PAT_MATCH_REG }, |
| 12869 | { "cook_sub", "req.cook", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12870 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12871 | { "hdr", "req.hdr", PAT_MATCH_STR }, |
| 12872 | { "hdr_beg", "req.hdr", PAT_MATCH_BEG }, |
| 12873 | { "hdr_dir", "req.hdr", PAT_MATCH_DIR }, |
| 12874 | { "hdr_dom", "req.hdr", PAT_MATCH_DOM }, |
| 12875 | { "hdr_end", "req.hdr", PAT_MATCH_END }, |
| 12876 | { "hdr_len", "req.hdr", PAT_MATCH_LEN }, |
| 12877 | { "hdr_reg", "req.hdr", PAT_MATCH_REG }, |
| 12878 | { "hdr_sub", "req.hdr", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12879 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12880 | /* these two declarations uses strings with list storage (in place |
| 12881 | * of tree storage). The basic match is PAT_MATCH_STR, but the indexation |
| 12882 | * and delete functions are relative to the list management. The parse |
| 12883 | * and match method are related to the corresponding fetch methods. This |
| 12884 | * is very particular ACL declaration mode. |
| 12885 | */ |
| 12886 | { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth }, |
| 12887 | { "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] | 12888 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12889 | { "path", "path", PAT_MATCH_STR }, |
| 12890 | { "path_beg", "path", PAT_MATCH_BEG }, |
| 12891 | { "path_dir", "path", PAT_MATCH_DIR }, |
| 12892 | { "path_dom", "path", PAT_MATCH_DOM }, |
| 12893 | { "path_end", "path", PAT_MATCH_END }, |
| 12894 | { "path_len", "path", PAT_MATCH_LEN }, |
| 12895 | { "path_reg", "path", PAT_MATCH_REG }, |
| 12896 | { "path_sub", "path", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12897 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12898 | { "req_ver", "req.ver", PAT_MATCH_STR }, |
| 12899 | { "resp_ver", "res.ver", PAT_MATCH_STR }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12900 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12901 | { "scook", "res.cook", PAT_MATCH_STR }, |
| 12902 | { "scook_beg", "res.cook", PAT_MATCH_BEG }, |
| 12903 | { "scook_dir", "res.cook", PAT_MATCH_DIR }, |
| 12904 | { "scook_dom", "res.cook", PAT_MATCH_DOM }, |
| 12905 | { "scook_end", "res.cook", PAT_MATCH_END }, |
| 12906 | { "scook_len", "res.cook", PAT_MATCH_LEN }, |
| 12907 | { "scook_reg", "res.cook", PAT_MATCH_REG }, |
| 12908 | { "scook_sub", "res.cook", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12909 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12910 | { "shdr", "res.hdr", PAT_MATCH_STR }, |
| 12911 | { "shdr_beg", "res.hdr", PAT_MATCH_BEG }, |
| 12912 | { "shdr_dir", "res.hdr", PAT_MATCH_DIR }, |
| 12913 | { "shdr_dom", "res.hdr", PAT_MATCH_DOM }, |
| 12914 | { "shdr_end", "res.hdr", PAT_MATCH_END }, |
| 12915 | { "shdr_len", "res.hdr", PAT_MATCH_LEN }, |
| 12916 | { "shdr_reg", "res.hdr", PAT_MATCH_REG }, |
| 12917 | { "shdr_sub", "res.hdr", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12918 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12919 | { "url", "url", PAT_MATCH_STR }, |
| 12920 | { "url_beg", "url", PAT_MATCH_BEG }, |
| 12921 | { "url_dir", "url", PAT_MATCH_DIR }, |
| 12922 | { "url_dom", "url", PAT_MATCH_DOM }, |
| 12923 | { "url_end", "url", PAT_MATCH_END }, |
| 12924 | { "url_len", "url", PAT_MATCH_LEN }, |
| 12925 | { "url_reg", "url", PAT_MATCH_REG }, |
| 12926 | { "url_sub", "url", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12927 | |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 12928 | { "urlp", "urlp", PAT_MATCH_STR }, |
| 12929 | { "urlp_beg", "urlp", PAT_MATCH_BEG }, |
| 12930 | { "urlp_dir", "urlp", PAT_MATCH_DIR }, |
| 12931 | { "urlp_dom", "urlp", PAT_MATCH_DOM }, |
| 12932 | { "urlp_end", "urlp", PAT_MATCH_END }, |
| 12933 | { "urlp_len", "urlp", PAT_MATCH_LEN }, |
| 12934 | { "urlp_reg", "urlp", PAT_MATCH_REG }, |
| 12935 | { "urlp_sub", "urlp", PAT_MATCH_SUB }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12936 | |
Willy Tarreau | 8ed669b | 2013-01-11 15:49:37 +0100 | [diff] [blame] | 12937 | { /* END */ }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 12938 | }}; |
| 12939 | |
| 12940 | /************************************************************************/ |
| 12941 | /* All supported pattern keywords must be declared here. */ |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 12942 | /************************************************************************/ |
| 12943 | /* Note: must not be declared <const> as its list will be overwritten */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 12944 | static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, { |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12945 | { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 12946 | { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 12947 | { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV }, |
| 12948 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 12949 | /* capture are allocated and are permanent in the stream */ |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 12950 | { "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] | 12951 | |
| 12952 | /* retrieve these captures from the HTTP logs */ |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 12953 | { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP }, |
| 12954 | { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP }, |
| 12955 | { "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] | 12956 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 12957 | { "capture.res.hdr", smp_fetch_capture_header_res, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP }, |
| 12958 | { "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] | 12959 | |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 12960 | /* cookie is valid in both directions (eg: for "stick ...") but cook* |
| 12961 | * are only here to match the ACL's name, are request-only and are used |
| 12962 | * for ACL compatibility only. |
| 12963 | */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12964 | { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 12965 | { "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] | 12966 | { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 12967 | { "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] | 12968 | |
| 12969 | /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are |
| 12970 | * only here to match the ACL's name, are request-only and are used for |
| 12971 | * ACL compatibility only. |
| 12972 | */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12973 | { "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] | 12974 | { "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] | 12975 | { "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] | 12976 | { "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] | 12977 | |
Willy Tarreau | 0a0daec | 2013-04-02 22:44:58 +0200 | [diff] [blame] | 12978 | { "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] | 12979 | { "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] | 12980 | { "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] | 12981 | { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP }, |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12982 | { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Willy Tarreau | 49ad95c | 2015-01-19 15:06:26 +0100 | [diff] [blame] | 12983 | { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 12984 | |
| 12985 | /* HTTP protocol on the request path */ |
| 12986 | { "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] | 12987 | { "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] | 12988 | |
| 12989 | /* HTTP version on the request path */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12990 | { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 12991 | { "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] | 12992 | |
Willy Tarreau | a5910cc | 2015-05-02 00:46:08 +0200 | [diff] [blame] | 12993 | { "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] | 12994 | { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 12995 | { "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] | 12996 | { "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] | 12997 | |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 12998 | /* HTTP version on the response path */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 12999 | { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV }, |
| 13000 | { "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] | 13001 | |
Willy Tarreau | 18ed256 | 2013-01-14 15:56:36 +0100 | [diff] [blame] | 13002 | /* 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] | 13003 | { "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] | 13004 | { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 13005 | { "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] | 13006 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 13007 | { "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] | 13008 | { "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] | 13009 | { "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] | 13010 | { "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] | 13011 | { "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] | 13012 | { "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] | 13013 | { "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] | 13014 | |
| 13015 | /* 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] | 13016 | { "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] | 13017 | { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV }, |
| 13018 | { "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] | 13019 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 13020 | { "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] | 13021 | { "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] | 13022 | { "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] | 13023 | { "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] | 13024 | { "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] | 13025 | { "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] | 13026 | { "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] | 13027 | |
Willy Tarreau | 409bcde | 2013-01-08 00:31:00 +0100 | [diff] [blame] | 13028 | /* 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] | 13029 | { "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] | 13030 | { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV }, |
| 13031 | { "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] | 13032 | { "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] | 13033 | |
| 13034 | /* 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] | 13035 | { "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] | 13036 | { "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] | 13037 | { "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] | 13038 | { "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] | 13039 | |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 13040 | { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP }, |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 13041 | { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 13042 | { "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] | 13043 | { "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] | 13044 | { "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] | 13045 | { "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] | 13046 | { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 13047 | { "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] | 13048 | { "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] | 13049 | { /* END */ }, |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 13050 | }}; |
| 13051 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 13052 | |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 13053 | /************************************************************************/ |
| 13054 | /* All supported converter keywords must be declared here. */ |
| 13055 | /************************************************************************/ |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 13056 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 13057 | static struct sample_conv_kw_list sample_conv_kws = {ILH, { |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 13058 | { "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] | 13059 | { "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] | 13060 | { "capture-req", smp_conv_req_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR}, |
| 13061 | { "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] | 13062 | { "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] | 13063 | { NULL, NULL, 0, 0, 0 }, |
| 13064 | }}; |
| 13065 | |
Thierry FOURNIER | 35ab275 | 2015-05-28 13:22:03 +0200 | [diff] [blame] | 13066 | |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 13067 | /************************************************************************/ |
| 13068 | /* All supported http-request action keywords must be declared here. */ |
| 13069 | /************************************************************************/ |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 13070 | struct action_kw_list http_req_actions = { |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 13071 | .kw = { |
Willy Tarreau | a9083d0 | 2015-05-08 15:27:59 +0200 | [diff] [blame] | 13072 | { "capture", parse_http_req_capture }, |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 13073 | { "set-method", parse_set_req_line }, |
| 13074 | { "set-path", parse_set_req_line }, |
| 13075 | { "set-query", parse_set_req_line }, |
| 13076 | { "set-uri", parse_set_req_line }, |
Willy Tarreau | cb703b0 | 2015-04-03 09:52:01 +0200 | [diff] [blame] | 13077 | { NULL, NULL } |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 13078 | } |
| 13079 | }; |
| 13080 | |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 13081 | struct action_kw_list http_res_actions = { |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 13082 | .kw = { |
| 13083 | { "capture", parse_http_res_capture }, |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 13084 | { "set-status", parse_http_set_status }, |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 13085 | { NULL, NULL } |
| 13086 | } |
| 13087 | }; |
| 13088 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 13089 | __attribute__((constructor)) |
| 13090 | static void __http_protocol_init(void) |
| 13091 | { |
| 13092 | acl_register_keywords(&acl_kws); |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 13093 | sample_register_fetches(&sample_fetch_keywords); |
Willy Tarreau | 276fae9 | 2013-07-25 14:36:01 +0200 | [diff] [blame] | 13094 | sample_register_convs(&sample_conv_kws); |
Willy Tarreau | a0dc23f | 2015-01-22 20:46:11 +0100 | [diff] [blame] | 13095 | http_req_keywords_register(&http_req_actions); |
Thierry FOURNIER | e80fada | 2015-05-26 18:06:31 +0200 | [diff] [blame] | 13096 | http_res_keywords_register(&http_res_actions); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 13097 | } |
| 13098 | |
| 13099 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 13100 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 13101 | * Local variables: |
| 13102 | * c-indent-level: 8 |
| 13103 | * c-basic-offset: 8 |
| 13104 | * End: |
| 13105 | */ |